Firstborn/Library/PackageCache/com.unity.terrain-tools@4.0.5/Editor/TerrainToolbox/TerrainToolboxWindow.cs

151 lines
4.7 KiB
C#
Raw Normal View History

2023-03-28 13:24:16 -04:00
using System.IO;
using UnityEngine;
namespace UnityEditor.TerrainTools
{
internal class TerrainToolboxWindow : EditorWindow
{
#if UNITY_2019_1_OR_NEWER
[MenuItem("Window/Terrain/Terrain Toolbox", false, 3020)]
static void CreateMangerWindow()
{
TerrainToolboxWindow window = GetWindow<TerrainToolboxWindow>("Terrain Toolbox");
window.minSize = new Vector2(200, 150);
window.Show();
}
#endif
TerrainManagerMode m_SelectedMode = TerrainManagerMode.CreateTerrain;
enum TerrainManagerMode
{
CreateTerrain = 0,
Settings = 1,
Utilities = 2,
Visualization = 3
}
internal TerrainToolboxCreateTerrain m_CreateTerrainMode;
internal TerrainToolboxSettings m_TerrainSettingsMode;
internal TerrainToolboxUtilities m_TerrainUtilitiesMode;
internal TerrainToolboxVisualization m_TerrainVisualizationMode;
const string PrefName = "TerrainToolbox.Window.Mode";
static class Styles
{
public static readonly GUIContent[] ModeToggles =
{
EditorGUIUtility.TrTextContent("Create New Terrain"),
EditorGUIUtility.TrTextContent("Terrain Settings"),
EditorGUIUtility.TrTextContent("Terrain Utilities"),
EditorGUIUtility.TrTextContent("Terrain Visualization")
};
public static readonly GUIStyle ButtonStyle = "LargeButton";
}
public void OnEnable()
{
m_CreateTerrainMode = new TerrainToolboxCreateTerrain(this);
m_TerrainSettingsMode = new TerrainToolboxSettings();
m_TerrainUtilitiesMode = new TerrainToolboxUtilities();
m_TerrainVisualizationMode = new TerrainToolboxVisualization();
m_CreateTerrainMode.LoadSettings();
m_CreateTerrainMode.OnEnable();
m_TerrainSettingsMode.LoadSettings();
m_TerrainUtilitiesMode.LoadSettings();
m_TerrainUtilitiesMode.OnLoad();
m_TerrainVisualizationMode.LoadSettings();
LoadSettings();
}
public void OnDisable()
{
m_CreateTerrainMode.OnDisable();
m_CreateTerrainMode.SaveSettings();
m_TerrainSettingsMode.SaveSettings();
m_TerrainUtilitiesMode.SaveSettings();
m_TerrainVisualizationMode.SaveSettings();
SaveSettings();
}
public void OnGUI()
{
EditorGUILayout.Space();
EditorGUILayout.BeginHorizontal();
ToggleManagerMode();
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
switch (m_SelectedMode)
{
case TerrainManagerMode.CreateTerrain:
m_CreateTerrainMode.OnGUI();
break;
case TerrainManagerMode.Settings:
m_TerrainSettingsMode.OnGUI();
break;
case TerrainManagerMode.Utilities:
m_TerrainUtilitiesMode.OnGUI();
break;
case TerrainManagerMode.Visualization:
m_TerrainVisualizationMode.OnGUI();
break;
}
}
void ToggleManagerMode()
{
EditorGUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
EditorGUI.BeginChangeCheck();
m_SelectedMode = (TerrainManagerMode)GUILayout.Toolbar((int)m_SelectedMode, Styles.ModeToggles, Styles.ButtonStyle, GUI.ToolbarButtonSize.FitToContents);
if (EditorGUI.EndChangeCheck())
{
GUIUtility.keyboardControl = 0;
}
GUILayout.FlexibleSpace();
EditorGUILayout.EndHorizontal();
}
void OnLostFocus()
{
m_TerrainUtilitiesMode.OnLostFocus();
}
void OnDestroy()
{
m_TerrainVisualizationMode.RevertMaterial();
}
void SaveSettings()
{
string filePath = ToolboxHelper.GetPrefFilePath(ToolboxHelper.ToolboxPrefsWindow);
File.WriteAllText(filePath, ((int)m_SelectedMode).ToString());
}
void LoadSettings()
{
string filePath = ToolboxHelper.GetPrefFilePath(ToolboxHelper.ToolboxPrefsWindow);
if (File.Exists(filePath))
{
string windowSettingsData = File.ReadAllText(filePath);
int value = 0;
if (int.TryParse(windowSettingsData, out value))
{
m_SelectedMode = (TerrainManagerMode)value;
}
}
}
}
}