InspectorWindow.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. using System;
  2. using System.Collections.Generic;
  3. using BansheeEngine;
  4. namespace BansheeEditor
  5. {
  6. internal sealed class InspectorWindow : EditorWindow
  7. {
  8. private class InspectorData
  9. {
  10. public GUIComponentFoldout foldout;
  11. public GUIPanel panel;
  12. public Inspector inspector;
  13. public bool expanded = true;
  14. }
  15. private List<InspectorData> inspectorData = new List<InspectorData>();
  16. private GUIScrollArea inspectorScrollArea;
  17. private GUILayout inspectorLayout;
  18. private SceneObject activeSO;
  19. private GUITextBox soNameInput;
  20. private GUILayout soPrefabLayout;
  21. private bool soHasPrefab;
  22. private GUIFloatField soPosX;
  23. private GUIFloatField soPosY;
  24. private GUIFloatField soPosZ;
  25. private GUIFloatField soRotX;
  26. private GUIFloatField soRotY;
  27. private GUIFloatField soRotZ;
  28. private GUIFloatField soScaleX;
  29. private GUIFloatField soScaleY;
  30. private GUIFloatField soScaleZ;
  31. [MenuItem("Windows/Inspector", ButtonModifier.CtrlAlt, ButtonCode.I)]
  32. private static void OpenHierarchyWindow()
  33. {
  34. OpenWindow<HierarchyWindow>();
  35. }
  36. internal void SetObjectToInspect(SceneObject so)
  37. {
  38. Clear();
  39. activeSO = so;
  40. if (activeSO == null)
  41. return;
  42. inspectorScrollArea = new GUIScrollArea();
  43. GUI.AddElement(inspectorScrollArea);
  44. inspectorLayout = inspectorScrollArea.Layout;
  45. // SceneObject fields
  46. CreateSceneObjectFields();
  47. RefreshSceneObjectFields(true);
  48. // Components
  49. Component[] allComponents = so.GetComponents();
  50. for (int i = 0; i < allComponents.Length; i++)
  51. {
  52. InspectorData data = new InspectorData();
  53. data.foldout = new GUIComponentFoldout(allComponents[i].GetType().Name);
  54. inspectorLayout.AddElement(data.foldout);
  55. data.panel = inspectorLayout.AddPanel();
  56. data.inspector = GetInspector(allComponents[i].GetType());
  57. data.inspector.Initialize(this, data.panel, allComponents[i]);
  58. data.foldout.SetExpanded(true);
  59. data.foldout.OnToggled += (bool expanded) => OnComponentFoldoutToggled(data, expanded);
  60. inspectorData.Add(data);
  61. inspectorData[i].inspector.Refresh();
  62. }
  63. inspectorLayout.AddFlexibleSpace();
  64. }
  65. private void OnComponentFoldoutToggled(InspectorData inspectorData, bool expanded)
  66. {
  67. inspectorData.expanded = expanded;
  68. inspectorData.inspector.SetVisible(expanded);
  69. }
  70. private void CreateSceneObjectFields()
  71. {
  72. GUIPanel sceneObjectPanel = inspectorLayout.AddPanel();
  73. GUILayoutY sceneObjectLayout = sceneObjectPanel.AddLayoutY();
  74. GUILayoutX nameLayout = sceneObjectLayout.AddLayoutX();
  75. GUILabel nameLbl = new GUILabel("Name", GUIOption.FixedWidth(70));
  76. soNameInput = new GUITextBox(false, GUIOption.FlexibleWidth(200));
  77. soNameInput.Text = activeSO.Name;
  78. soNameInput.OnChanged += (x) => { if (activeSO != null) activeSO.Name = x; };
  79. nameLayout.AddElement(nameLbl);
  80. nameLayout.AddElement(soNameInput);
  81. nameLayout.AddFlexibleSpace();
  82. soPrefabLayout = sceneObjectLayout.AddLayoutX();
  83. GUILayoutX positionLayout = sceneObjectLayout.AddLayoutX();
  84. GUILabel positionLbl = new GUILabel("Position", GUIOption.FixedWidth(70));
  85. soPosX = new GUIFloatField(new GUIContent("X"), 10, "", GUIOption.FlexibleWidth(50));
  86. soPosY = new GUIFloatField(new GUIContent("Y"), 10, "", GUIOption.FlexibleWidth(50));
  87. soPosZ = new GUIFloatField(new GUIContent("Z"), 10, "", GUIOption.FlexibleWidth(50));
  88. soPosX.OnChanged += (x) => OnPositionChanged(0, x);
  89. soPosY.OnChanged += (y) => OnPositionChanged(1, y);
  90. soPosZ.OnChanged += (z) => OnPositionChanged(2, z);
  91. positionLayout.AddElement(positionLbl);
  92. positionLayout.AddElement(soPosX);
  93. positionLayout.AddSpace(10);
  94. positionLayout.AddElement(soPosY);
  95. positionLayout.AddSpace(10);
  96. positionLayout.AddElement(soPosZ);
  97. positionLayout.AddFlexibleSpace();
  98. GUILayoutX rotationLayout = sceneObjectLayout.AddLayoutX();
  99. GUILabel rotationLbl = new GUILabel("Rotation", GUIOption.FixedWidth(70));
  100. soRotX = new GUIFloatField(new GUIContent("X"), 10, "", GUIOption.FlexibleWidth(50));
  101. soRotY = new GUIFloatField(new GUIContent("Y"), 10, "", GUIOption.FlexibleWidth(50));
  102. soRotZ = new GUIFloatField(new GUIContent("Z"), 10, "", GUIOption.FlexibleWidth(50));
  103. soRotX.OnChanged += (x) => OnRotationChanged(0, x);
  104. soRotY.OnChanged += (y) => OnRotationChanged(1, y);
  105. soRotZ.OnChanged += (z) => OnRotationChanged(2, z);
  106. rotationLayout.AddElement(rotationLbl);
  107. rotationLayout.AddElement(soRotX);
  108. rotationLayout.AddSpace(10);
  109. rotationLayout.AddElement(soRotY);
  110. rotationLayout.AddSpace(10);
  111. rotationLayout.AddElement(soRotZ);
  112. rotationLayout.AddFlexibleSpace();
  113. GUILayoutX scaleLayout = sceneObjectLayout.AddLayoutX();
  114. GUILabel scaleLbl = new GUILabel("Scale", GUIOption.FixedWidth(70));
  115. soScaleX = new GUIFloatField(new GUIContent("X"), 10, "", GUIOption.FlexibleWidth(50));
  116. soScaleY = new GUIFloatField(new GUIContent("Y"), 10, "", GUIOption.FlexibleWidth(50));
  117. soScaleZ = new GUIFloatField(new GUIContent("Z"), 10, "", GUIOption.FlexibleWidth(50));
  118. soScaleX.OnChanged += (x) => OnScaleChanged(0, x);
  119. soScaleY.OnChanged += (y) => OnScaleChanged(1, y);
  120. soScaleX.OnChanged += (z) => OnScaleChanged(2, z);
  121. scaleLayout.AddElement(scaleLbl);
  122. scaleLayout.AddElement(soScaleX);
  123. scaleLayout.AddSpace(10);
  124. scaleLayout.AddElement(soScaleY);
  125. scaleLayout.AddSpace(10);
  126. scaleLayout.AddElement(soScaleZ);
  127. scaleLayout.AddFlexibleSpace();
  128. }
  129. private void RefreshSceneObjectFields(bool forceUpdate)
  130. {
  131. if (activeSO == null)
  132. return;
  133. soNameInput.Text = activeSO.Name;
  134. bool hasPrefab = PrefabUtility.HasPrefabLink(activeSO);
  135. if (soHasPrefab != hasPrefab || forceUpdate)
  136. {
  137. int numChildren = soPrefabLayout.GetNumChildren();
  138. for (int i = 0; i < numChildren; i++)
  139. soPrefabLayout.GetChild(0).Destroy();
  140. GUILabel prefabLabel =new GUILabel("Prefab", GUIOption.FixedWidth(70));
  141. soPrefabLayout.AddElement(prefabLabel);
  142. //if (hasPrefab) // TODO - Disabled check for preview purposes
  143. {
  144. GUIButton btnApplyPrefab = new GUIButton("Apply");
  145. GUIButton btnRevertPrefab = new GUIButton("Revert");
  146. GUIButton btnBreakPrefab = new GUIButton("Break");
  147. btnApplyPrefab.OnClick += () => PrefabUtility.ApplyPrefab(activeSO);
  148. btnRevertPrefab.OnClick += () => PrefabUtility.RevertPrefab(activeSO);
  149. btnBreakPrefab.OnClick += () => PrefabUtility.BreakPrefab(activeSO);
  150. soPrefabLayout.AddElement(btnApplyPrefab);
  151. soPrefabLayout.AddElement(btnRevertPrefab);
  152. soPrefabLayout.AddElement(btnBreakPrefab);
  153. }
  154. //else
  155. //{
  156. // GUILabel noPrefabLabel = new GUILabel("None");
  157. // soPrefabLayout.AddElement(noPrefabLabel);
  158. //}
  159. soHasPrefab = hasPrefab;
  160. }
  161. Vector3 position;
  162. Vector3 angles;
  163. if (EditorApplication.ActiveCoordinateMode == HandleCoordinateMode.World)
  164. {
  165. position = activeSO.Position;
  166. angles = activeSO.Rotation.ToEuler();
  167. }
  168. else
  169. {
  170. position = activeSO.LocalPosition;
  171. angles = activeSO.LocalRotation.ToEuler();
  172. }
  173. Vector3 scale = activeSO.LocalScale;
  174. soPosX.Value = position.x;
  175. soPosY.Value = position.y;
  176. soPosZ.Value = position.z;
  177. soRotX.Value = angles.x;
  178. soRotY.Value = angles.y;
  179. soRotZ.Value = angles.z;
  180. soScaleX.Value = scale.x;
  181. soScaleY.Value = scale.y;
  182. soScaleZ.Value = scale.z;
  183. }
  184. private void OnEditorUpdate()
  185. {
  186. RefreshSceneObjectFields(false);
  187. for (int i = 0; i < inspectorData.Count; i++)
  188. {
  189. inspectorData[i].inspector.Refresh();
  190. }
  191. }
  192. internal void Destroy()
  193. {
  194. Clear();
  195. }
  196. internal void Clear()
  197. {
  198. for (int i = 0; i < inspectorData.Count; i++)
  199. {
  200. inspectorData[i].foldout.Destroy();
  201. inspectorData[i].inspector.Destroy();
  202. }
  203. inspectorData.Clear();
  204. if (inspectorScrollArea != null)
  205. {
  206. inspectorScrollArea.Destroy();
  207. inspectorScrollArea = null;
  208. }
  209. activeSO = null;
  210. soNameInput = null;
  211. soPrefabLayout = null;
  212. soHasPrefab = false;
  213. soPosX = null;
  214. soPosY = null;
  215. soPosZ = null;
  216. soRotX = null;
  217. soRotY = null;
  218. soRotZ = null;
  219. soScaleX = null;
  220. soScaleY = null;
  221. soScaleZ = null;
  222. }
  223. private void OnPositionChanged(int idx, float value)
  224. {
  225. if (activeSO == null)
  226. return;
  227. if (EditorApplication.ActiveCoordinateMode == HandleCoordinateMode.World)
  228. {
  229. Vector3 position = activeSO.Position;
  230. position[idx] = value;
  231. activeSO.Position = position;
  232. }
  233. else
  234. {
  235. Vector3 position = activeSO.LocalPosition;
  236. position[idx] = value;
  237. activeSO.LocalPosition = position;
  238. }
  239. }
  240. private void OnRotationChanged(int idx, float value)
  241. {
  242. if (activeSO == null)
  243. return;
  244. if (EditorApplication.ActiveCoordinateMode == HandleCoordinateMode.World)
  245. {
  246. Vector3 angles = activeSO.Rotation.ToEuler();
  247. angles[idx] = value;
  248. activeSO.Rotation = Quaternion.FromEuler(angles);
  249. }
  250. else
  251. {
  252. Vector3 angles = activeSO.LocalRotation.ToEuler();
  253. angles[idx] = value;
  254. activeSO.LocalRotation = Quaternion.FromEuler(angles);
  255. Debug.Log("ROTATION CHANGED: " + idx + " - " + value + " - " + angles + " - " + activeSO.LocalRotation + " - " + activeSO.LocalRotation.ToEuler());
  256. Quaternion dbg = Quaternion.FromEuler(angles);
  257. Debug.Log("LOCAL CHECK: " + angles + " - " + dbg + " - " + dbg.ToEuler());
  258. }
  259. }
  260. private void OnScaleChanged(int idx, float value)
  261. {
  262. if (activeSO == null)
  263. return;
  264. Vector3 scale = activeSO.LocalScale;
  265. scale[idx] = value;
  266. activeSO.LocalScale = scale;
  267. }
  268. private Inspector GetInspector(Type type)
  269. {
  270. // TODO - Check if type has a custom inspector
  271. // and return the custom inspector, otherwise create a generic one
  272. return new GenericInspector();
  273. }
  274. }
  275. }