CameraInspector.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. using System.Collections.Generic;
  2. using BansheeEngine;
  3. namespace BansheeEditor
  4. {
  5. /// <summary>
  6. /// Renders an inspector for the <see cref="Camera"/> component.
  7. /// </summary>
  8. [CustomInspector(typeof(Camera))]
  9. public class CameraInspector : Inspector
  10. {
  11. private GUIEnumField projectionTypeField = new GUIEnumField(typeof(ProjectionType), new LocEdString("Projection type"));
  12. private GUISliderField fieldOfView = new GUISliderField(1, 360, new LocEdString("Field of view"));
  13. private GUIFloatField orthoHeight = new GUIFloatField(new LocEdString("Orthographic height"));
  14. private GUIFloatField aspectField = new GUIFloatField(new LocEdString("Aspect ratio"));
  15. private GUIFloatField nearPlaneField = new GUIFloatField(new LocEdString("Near plane"));
  16. private GUIFloatField farPlaneField = new GUIFloatField(new LocEdString("Far plane"));
  17. private GUIFloatField viewportXField = new GUIFloatField(new LocEdString("X"), 30);
  18. private GUIFloatField viewportYField = new GUIFloatField(new LocEdString("Y"), 30);
  19. private GUIFloatField viewportWidthField = new GUIFloatField(new LocEdString("Width"), 30);
  20. private GUIFloatField viewportHeightField = new GUIFloatField(new LocEdString("Height"), 30);
  21. private GUIEnumField clearFlagsFields = new GUIEnumField(typeof (ClearFlags), true, new LocEdString("Clear flags"));
  22. private GUIIntField clearStencilField = new GUIIntField(new LocEdString("Clear stencil"));
  23. private GUIFloatField clearDepthField = new GUIFloatField(new LocEdString("Clear depth"));
  24. private GUIColorField clearColorField = new GUIColorField(new LocEdString("Clear color"));
  25. private GUIIntField priorityField = new GUIIntField(new LocEdString("Render priority"));
  26. private GUIListBoxField layersField = new GUIListBoxField(Layers.Names, true, new LocEdString("Layers"));
  27. private GUIToggleField mainField = new GUIToggleField(new LocEdString("Main"));
  28. private ulong layersValue = 0;
  29. private InspectableState modifyState;
  30. /// <inheritdoc/>
  31. protected internal override void Initialize()
  32. {
  33. BuildGUI();
  34. }
  35. /// <inheritdoc/>
  36. protected internal override InspectableState Refresh()
  37. {
  38. Camera camera = InspectedObject as Camera;
  39. if (camera == null)
  40. return InspectableState.NotModified;
  41. ProjectionType projType = camera.ProjectionType;
  42. if (projectionTypeField.Value != (ulong)projType)
  43. {
  44. projectionTypeField.Value = (ulong)projType;
  45. ToggleTypeSpecificFields(projType);
  46. }
  47. fieldOfView.Value = camera.FieldOfView.Degrees;
  48. orthoHeight.Value = camera.OrthoHeight;
  49. aspectField.Value = camera.AspectRatio;
  50. nearPlaneField.Value = camera.NearClipPlane;
  51. farPlaneField.Value = camera.FarClipPlane;
  52. viewportXField.Value = camera.ViewportRect.x;
  53. viewportYField.Value = camera.ViewportRect.y;
  54. viewportWidthField.Value = camera.ViewportRect.width;
  55. viewportHeightField.Value = camera.ViewportRect.height;
  56. clearFlagsFields.Value = (ulong)camera.ClearFlags;
  57. clearStencilField.Value = camera.ClearStencil;
  58. clearDepthField.Value = camera.ClearDepth;
  59. clearColorField.Value = camera.ClearColor;
  60. priorityField.Value = camera.Priority;
  61. mainField.Value = camera.Main;
  62. if (layersValue != camera.Layers)
  63. {
  64. bool[] states = new bool[64];
  65. for (int i = 0; i < states.Length; i++)
  66. states[i] = (camera.Layers & Layers.Values[i]) == Layers.Values[i];
  67. layersField.States = states;
  68. layersValue = camera.Layers;
  69. }
  70. InspectableState oldState = modifyState;
  71. if (modifyState.HasFlag(InspectableState.Modified))
  72. modifyState = InspectableState.NotModified;
  73. return oldState;
  74. }
  75. /// <summary>
  76. /// Recreates all the GUI elements used by this inspector.
  77. /// </summary>
  78. private void BuildGUI()
  79. {
  80. if (InspectedObject != null)
  81. {
  82. Camera camera = (Camera)InspectedObject;
  83. projectionTypeField.OnSelectionChanged += x =>
  84. {
  85. camera.ProjectionType = (ProjectionType)x;
  86. MarkAsModified();
  87. ConfirmModify();
  88. ToggleTypeSpecificFields((ProjectionType)x);
  89. };
  90. fieldOfView.OnChanged += x => { camera.FieldOfView = x; MarkAsModified(); };
  91. fieldOfView.OnFocusLost += ConfirmModify;
  92. orthoHeight.OnChanged += x => { camera.OrthoHeight = x; MarkAsModified(); };
  93. orthoHeight.OnConfirmed += ConfirmModify;
  94. orthoHeight.OnFocusLost += ConfirmModify;
  95. aspectField.OnChanged += x => { camera.AspectRatio = x; MarkAsModified(); };
  96. aspectField.OnConfirmed += ConfirmModify;
  97. aspectField.OnFocusLost += ConfirmModify;
  98. nearPlaneField.OnChanged += x => { camera.NearClipPlane = x; MarkAsModified(); };
  99. nearPlaneField.OnConfirmed += ConfirmModify;
  100. nearPlaneField.OnFocusLost += ConfirmModify;
  101. farPlaneField.OnChanged += x => { camera.FarClipPlane = x; MarkAsModified(); };
  102. farPlaneField.OnConfirmed += ConfirmModify;
  103. farPlaneField.OnFocusLost += ConfirmModify;
  104. viewportXField.OnChanged += x =>
  105. {
  106. Rect2 rect = camera.ViewportRect;
  107. rect.x = x;
  108. camera.ViewportRect = rect;
  109. MarkAsModified();
  110. };
  111. viewportXField.OnConfirmed += ConfirmModify;
  112. viewportXField.OnFocusLost += ConfirmModify;
  113. viewportYField.OnChanged += x =>
  114. {
  115. Rect2 rect = camera.ViewportRect;
  116. rect.y = x;
  117. camera.ViewportRect = rect;
  118. MarkAsModified();
  119. };
  120. viewportYField.OnConfirmed += ConfirmModify;
  121. viewportYField.OnFocusLost += ConfirmModify;
  122. viewportWidthField.OnChanged += x =>
  123. {
  124. Rect2 rect = camera.ViewportRect;
  125. rect.width = x;
  126. camera.ViewportRect = rect;
  127. MarkAsModified();
  128. };
  129. viewportWidthField.OnConfirmed += ConfirmModify;
  130. viewportWidthField.OnFocusLost += ConfirmModify;
  131. viewportHeightField.OnChanged += x =>
  132. {
  133. Rect2 rect = camera.ViewportRect;
  134. rect.height = x;
  135. camera.ViewportRect = rect;
  136. MarkAsModified();
  137. };
  138. viewportHeightField.OnConfirmed += ConfirmModify;
  139. viewportHeightField.OnFocusLost += ConfirmModify;
  140. clearFlagsFields.OnSelectionChanged += x =>
  141. {
  142. camera.ClearFlags = (ClearFlags) x;
  143. MarkAsModified();
  144. ConfirmModify();
  145. };
  146. clearStencilField.OnChanged += x => { camera.ClearStencil = (ushort) x; };
  147. clearStencilField.OnConfirmed += ConfirmModify;
  148. clearStencilField.OnFocusLost += ConfirmModify;
  149. clearDepthField.OnChanged += x => { camera.ClearDepth = x; };
  150. clearDepthField.OnConfirmed += ConfirmModify;
  151. clearDepthField.OnFocusLost += ConfirmModify;
  152. clearColorField.OnChanged += x =>
  153. {
  154. camera.ClearColor = x;
  155. MarkAsModified();
  156. ConfirmModify();
  157. };
  158. priorityField.OnChanged += x => { camera.Priority = x; MarkAsModified(); };
  159. priorityField.OnConfirmed += ConfirmModify;
  160. priorityField.OnFocusLost += ConfirmModify;
  161. layersField.OnSelectionChanged += x =>
  162. {
  163. ulong layers = 0;
  164. bool[] states = layersField.States;
  165. for (int i = 0; i < states.Length; i++)
  166. layers |= states[i] ? Layers.Values[i] : 0;
  167. layersValue = layers;
  168. camera.Layers = layers;
  169. MarkAsModified();
  170. ConfirmModify();
  171. };
  172. mainField.OnChanged += x =>
  173. {
  174. camera.Main = x;
  175. MarkAsModified();
  176. ConfirmModify();
  177. };
  178. Layout.AddElement(projectionTypeField);
  179. Layout.AddElement(fieldOfView);
  180. Layout.AddElement(orthoHeight);
  181. Layout.AddElement(aspectField);
  182. Layout.AddElement(nearPlaneField);
  183. Layout.AddElement(farPlaneField);
  184. GUILayoutX viewportTopLayout = Layout.AddLayoutX();
  185. viewportTopLayout.AddElement(new GUILabel(new LocEdString("Viewport"), GUIOption.FixedWidth(100)));
  186. GUILayoutY viewportContentLayout = viewportTopLayout.AddLayoutY();
  187. GUILayoutX viewportTopRow = viewportContentLayout.AddLayoutX();
  188. viewportTopRow.AddElement(viewportXField);
  189. viewportTopRow.AddElement(viewportWidthField);
  190. GUILayoutX viewportBotRow = viewportContentLayout.AddLayoutX();
  191. viewportBotRow.AddElement(viewportYField);
  192. viewportBotRow.AddElement(viewportHeightField);
  193. Layout.AddElement(clearFlagsFields);
  194. Layout.AddElement(clearColorField);
  195. Layout.AddElement(clearDepthField);
  196. Layout.AddElement(clearStencilField);
  197. Layout.AddElement(priorityField);
  198. Layout.AddElement(layersField);
  199. Layout.AddElement(mainField);
  200. ToggleTypeSpecificFields(camera.ProjectionType);
  201. }
  202. }
  203. /// <summary>
  204. /// Enables or disables different GUI elements depending on the projection type.
  205. /// </summary>
  206. /// <param name="type">Projection type to show GUI elements for.</param>
  207. private void ToggleTypeSpecificFields(ProjectionType type)
  208. {
  209. if (type == ProjectionType.Orthographic)
  210. {
  211. fieldOfView.Active = false;
  212. orthoHeight.Active = true;
  213. }
  214. else
  215. {
  216. fieldOfView.Active = true;
  217. orthoHeight.Active = false;
  218. }
  219. }
  220. /// <summary>
  221. /// Marks the contents of the inspector as modified.
  222. /// </summary>
  223. protected void MarkAsModified()
  224. {
  225. modifyState |= InspectableState.ModifyInProgress;
  226. }
  227. /// <summary>
  228. /// Confirms any queued modifications.
  229. /// </summary>
  230. protected void ConfirmModify()
  231. {
  232. if (modifyState.HasFlag(InspectableState.ModifyInProgress))
  233. modifyState |= InspectableState.Modified;
  234. }
  235. }
  236. }