Inspector.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System;
  5. namespace O3DWB
  6. {
  7. public class Inspector : ScriptableObject, IMessageListener
  8. {
  9. #region Private Variables
  10. [SerializeField]
  11. private Editor _editorWindow;
  12. [SerializeField]
  13. private InspectorGUIIdentifier _activeInspectorGUIIdentifier = InspectorGUIIdentifier.ObjectPlacement;
  14. [SerializeField]
  15. private InspectorGUISelectionToolbar _inpectorGUISelectionTolbar = new InspectorGUISelectionToolbar();
  16. [SerializeField]
  17. private ObjectPlacementInspectorGUI _objectPlacementInspectorGUI = new ObjectPlacementInspectorGUI();
  18. [SerializeField]
  19. private ObjectEraseInspectorGUI _objectEraseInspectorGUI = new ObjectEraseInspectorGUI();
  20. [SerializeField]
  21. private ObjectSnappingInspectorGUI _objectSnappingInspectorGUI = new ObjectSnappingInspectorGUI();
  22. [SerializeField]
  23. private ObjectSelectionInspectorGUI _objectSelectionInspectorGUI = new ObjectSelectionInspectorGUI();
  24. #endregion
  25. #region Public Properties
  26. public InspectorGUIIdentifier ActiveInspectorGUIIdentifier
  27. {
  28. get { return _activeInspectorGUIIdentifier; }
  29. set
  30. {
  31. _activeInspectorGUIIdentifier = value;
  32. InspectorGUIWasChangedMessage.SendToInterestedListeners(_activeInspectorGUIIdentifier);
  33. SceneView.RepaintAll();
  34. if (_editorWindow != null) _editorWindow.Repaint();
  35. }
  36. }
  37. public InspectorGUISelectionToolbar InspectorGUISelectionToolbar { get { return _inpectorGUISelectionTolbar; } }
  38. public ObjectPlacementInspectorGUI ObjectPlacementInspectorGUI { get { return _objectPlacementInspectorGUI; } }
  39. public ObjectEraseInspectorGUI ObjectEraseInspectorGUI { get { return _objectEraseInspectorGUI; } }
  40. public ObjectSnappingInspectorGUI ObjectSnappingInspectorGUI { get { return _objectSnappingInspectorGUI; } }
  41. public ObjectSelectionInspectorGUI ObjectSelectionInspectorGUI { get { return _objectSelectionInspectorGUI; } }
  42. public Editor EditorWindow { get { return _editorWindow; } set { if (value != null) _editorWindow = value; } }
  43. #endregion
  44. #region Constructors
  45. public Inspector()
  46. {
  47. _inpectorGUISelectionTolbar.ButtonScale = 0.25f;
  48. }
  49. #endregion
  50. #region Public Static Functions
  51. public static Inspector Get()
  52. {
  53. return Octave3DWorldBuilder.ActiveInstance.Inspector;
  54. }
  55. #endregion
  56. #region Public Methods
  57. public void Repaint()
  58. {
  59. if (_editorWindow != null) _editorWindow.Repaint();
  60. }
  61. public void Render()
  62. {
  63. RenderShowGUIHintsToggle();
  64. Octave3DWorldBuilder.ActiveInstance.ShowGUIHint("In order to use the hotkeys, the scene view window must have focus. This means that if you click on the " +
  65. "Inspector or an Editor Window to modify settings, you will then have to click again inside the scene view " +
  66. "window before you can use any hotkeys. Any mouse button can be used for the click. Another way to work around this " +
  67. "is to perform a dummy keypress which will transfer the focus back to the scene view window.");
  68. Octave3DWorldBuilder.ActiveInstance.ShowGUIHint("Almost all controls have tooltips which can contain useful info. Hover the controls with the mouse cursor to allow the tooltips to appear.");
  69. _inpectorGUISelectionTolbar.Render();
  70. RenderActionControls();
  71. GetActiveGUI().Render();
  72. }
  73. public void Initialize()
  74. {
  75. if (Octave3DWorldBuilder.ActiveInstance == null) return;
  76. _objectPlacementInspectorGUI.Initialize();
  77. _objectEraseInspectorGUI.Initialize();
  78. _objectSnappingInspectorGUI.Initialize();
  79. _objectSelectionInspectorGUI.Initialize();
  80. }
  81. #endregion
  82. #region Private Methods
  83. private void OnEnable()
  84. {
  85. MessageListenerRegistration.PerformRegistrationForInspector(this);
  86. }
  87. private void RenderShowGUIHintsToggle()
  88. {
  89. bool newBool = EditorGUILayout.ToggleLeft(GetContentForShowGUIHintsToggle(), Octave3DWorldBuilder.ActiveInstance.ShowGUIHints);
  90. if(newBool != Octave3DWorldBuilder.ActiveInstance.ShowGUIHints)
  91. {
  92. UndoEx.RecordForToolAction(Octave3DWorldBuilder.ActiveInstance);
  93. Octave3DWorldBuilder.ActiveInstance.ShowGUIHints = newBool;
  94. Octave3DWorldBuilder.ActiveInstance.RepaintAllEditorWindows();
  95. }
  96. }
  97. private GUIContent GetContentForShowGUIHintsToggle()
  98. {
  99. var content = new GUIContent();
  100. content.text = "Show GUI hints";
  101. content.tooltip = "If this is checked, the GUI will display message boxes that contain useful hints about how to use the tool.";
  102. return content;
  103. }
  104. private void RenderActionControls()
  105. {
  106. EditorGUILayout.BeginHorizontal();
  107. var content = new GUIContent();
  108. content.text = "Mesh combine...";
  109. content.tooltip = "Opens up a new window which allows you to perform mesh combine operations.";
  110. if(GUILayout.Button(content, GUILayout.Width(110.0f)))
  111. {
  112. Octave3DWorldBuilder.ActiveInstance.MeshCombineSettings.ShowEditorWindow();
  113. }
  114. RenderRefreshSceneButton();
  115. EditorGUILayout.EndHorizontal();
  116. EditorGUILayout.BeginHorizontal();
  117. RenderSaveOctave3DConfigButton();
  118. RenderLoadOctave3DConfigButton();
  119. EditorGUILayout.EndHorizontal();
  120. }
  121. private void RenderRefreshSceneButton()
  122. {
  123. if (GUILayout.Button(GetContentForRefreshSceneButton(), GUILayout.Width(EditorGUILayoutEx.PreferedActionButtonWidth * 0.45f)))
  124. {
  125. Octave3DScene.Get().Refresh(true);
  126. }
  127. }
  128. private GUIContent GetContentForRefreshSceneButton()
  129. {
  130. var content = new GUIContent();
  131. content.text = "Refresh scene";
  132. content.tooltip = "Refreshes the internal scene data. One use case for this button is when you are working with 2D sprites and you cahnge the pivot point for one " +
  133. "or more sprites. In that case the internal representation of the sprite objects needs to be rebuilt and pressing this button will do that.";
  134. return content;
  135. }
  136. private void RenderSaveOctave3DConfigButton()
  137. {
  138. if (GUILayout.Button(GetContentForSaveOctave3DConfigButton(), GUILayout.Width(EditorGUILayoutEx.PreferedActionButtonWidth * 0.73f)))
  139. {
  140. Octave3DConfigSaveWindow.Get().ShowOctave3DWindow();
  141. }
  142. }
  143. private GUIContent GetContentForSaveOctave3DConfigButton()
  144. {
  145. var content = new GUIContent();
  146. content.text = "Save Octave3D config...";
  147. content.tooltip = "Opens up a new window which allows you to save different types of settings to a config file which can be loaded when needed.";
  148. return content;
  149. }
  150. private void RenderLoadOctave3DConfigButton()
  151. {
  152. if (GUILayout.Button(GetContentForLoadOctave3DConfigButton(), GUILayout.Width(EditorGUILayoutEx.PreferedActionButtonWidth * 0.75f)))
  153. {
  154. Octave3DConfigLoadWindow.Get().ShowOctave3DWindow();
  155. }
  156. }
  157. private GUIContent GetContentForLoadOctave3DConfigButton()
  158. {
  159. var content = new GUIContent();
  160. content.text = "Load Octave3D config...";
  161. content.tooltip = "Opens up a new window which allows you to load different types of settings from a specfied config file.";
  162. return content;
  163. }
  164. private InspectorGUI GetActiveGUI()
  165. {
  166. switch(_activeInspectorGUIIdentifier)
  167. {
  168. case InspectorGUIIdentifier.ObjectPlacement:
  169. return _objectPlacementInspectorGUI;
  170. case InspectorGUIIdentifier.ObjectErase:
  171. return _objectEraseInspectorGUI;
  172. case InspectorGUIIdentifier.ObjectSelection:
  173. return _objectSelectionInspectorGUI;
  174. case InspectorGUIIdentifier.ObjectSnapping:
  175. return _objectSnappingInspectorGUI;
  176. default:
  177. return null;
  178. }
  179. }
  180. #endregion
  181. #region Message Handlers
  182. public void RespondToMessage(Message message)
  183. {
  184. switch(message.Type)
  185. {
  186. case MessageType.ToolWasReset:
  187. RespondToMessage(message as ToolWasResetMessage);
  188. break;
  189. case MessageType.ToolWasStarted:
  190. RespondToMessage(message as ToolWasStartedMessage);
  191. break;
  192. }
  193. }
  194. private void RespondToMessage(ToolWasResetMessage message)
  195. {
  196. Initialize();
  197. }
  198. private void RespondToMessage(ToolWasStartedMessage message)
  199. {
  200. Initialize();
  201. }
  202. #endregion
  203. }
  204. }
  205. #endif