DefaultHandleManager.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System.Collections.Generic;
  2. using BansheeEngine;
  3. namespace BansheeEditor
  4. {
  5. internal sealed class DefaultHandleManager : Handle
  6. {
  7. private struct HandledObject
  8. {
  9. public HandledObject(SceneObject so)
  10. {
  11. this.so = so;
  12. initialPosition = so.LocalPosition;
  13. initialRotation = so.LocalRotation;
  14. initialScale = so.LocalScale;
  15. }
  16. public SceneObject so;
  17. public Vector3 initialPosition;
  18. public Quaternion initialRotation;
  19. public Vector3 initialScale;
  20. }
  21. private SceneViewTool activeHandleType = SceneViewTool.View;
  22. private DefaultHandle activeHandle;
  23. private HandledObject[] activeSelection;
  24. private bool isDragged;
  25. private Vector3 initialHandlePosition;
  26. private Quaternion initialHandleRotation;
  27. private Vector3 initialHandleScale;
  28. protected override void PreInput()
  29. {
  30. SceneObject[] selectedSceneObjects = Selection.sceneObjects;
  31. if (selectedSceneObjects.Length == 0)
  32. {
  33. if (activeHandle != null)
  34. {
  35. activeHandle.Destroy();
  36. activeHandle = null;
  37. }
  38. }
  39. else
  40. {
  41. if (activeHandleType != EditorApplication.ActiveSceneTool || activeHandle == null)
  42. {
  43. if (activeHandle != null)
  44. {
  45. activeHandle.Destroy();
  46. activeHandle = null;
  47. }
  48. switch (EditorApplication.ActiveSceneTool)
  49. {
  50. case SceneViewTool.Move:
  51. activeHandle = new MoveHandle();
  52. break;
  53. case SceneViewTool.Rotate:
  54. activeHandle = new RotateHandle();
  55. break;
  56. case SceneViewTool.Scale:
  57. activeHandle = new ScaleHandle();
  58. break;
  59. }
  60. activeHandleType = EditorApplication.ActiveSceneTool;
  61. }
  62. }
  63. if (activeHandle != null)
  64. {
  65. Quaternion rotation;
  66. if (EditorApplication.ActiveCoordinateMode == HandleCoordinateMode.World)
  67. rotation = Quaternion.identity;
  68. else
  69. rotation = selectedSceneObjects[0].Rotation; // We don't average rotation in case of multi-selection
  70. Vector3 position;
  71. if (EditorApplication.ActivePivotMode == HandlePivotMode.Pivot)
  72. position = selectedSceneObjects[0].Position; // Just take pivot from the first one, no averaging
  73. else
  74. {
  75. List<SceneObject> flatenedHierarchy = new List<SceneObject>();
  76. foreach (var so in selectedSceneObjects)
  77. flatenedHierarchy.AddRange(EditorUtility.FlattenHierarchy(so));
  78. AABox selectionBounds = EditorUtility.CalculateBounds(flatenedHierarchy.ToArray());
  79. position = selectionBounds.center;
  80. }
  81. activeHandle.Position = position;
  82. activeHandle.Rotation = rotation;
  83. if (!isDragged)
  84. initialHandleScale = selectedSceneObjects[0].Scale;
  85. activeHandle.DoPreInput();
  86. }
  87. }
  88. protected override void PostInput()
  89. {
  90. if (activeHandle != null)
  91. {
  92. if (activeHandle.IsDragged())
  93. {
  94. if (!isDragged)
  95. {
  96. isDragged = true;
  97. SceneObject[] selectedSceneObjects = Selection.sceneObjects;
  98. activeSelection = new HandledObject[selectedSceneObjects.Length];
  99. for (int i = 0; i < selectedSceneObjects.Length; i++)
  100. activeSelection[i] = new HandledObject(selectedSceneObjects[0]);
  101. initialHandlePosition = activeHandle.Position;
  102. initialHandleRotation = activeHandle.Rotation;
  103. }
  104. }
  105. else
  106. {
  107. isDragged = false;
  108. activeSelection = null;
  109. }
  110. activeHandle.DoPostInput();
  111. if (activeHandle.IsDragged())
  112. {
  113. switch (activeHandleType)
  114. {
  115. case SceneViewTool.Move:
  116. MoveHandle moveHandle = (MoveHandle) activeHandle;
  117. foreach (var selectedObj in activeSelection)
  118. selectedObj.so.LocalPosition = selectedObj.initialPosition + moveHandle.Delta;
  119. break;
  120. case SceneViewTool.Rotate:
  121. {
  122. RotateHandle rotateHandle = (RotateHandle) activeHandle;
  123. // Make sure we transform relative to the handle position
  124. SceneObject temporarySO = new SceneObject("Temp");
  125. temporarySO.Position = initialHandlePosition;
  126. temporarySO.LocalRotation = initialHandleRotation;
  127. SceneObject[] originalParents = new SceneObject[activeSelection.Length];
  128. for (int i = 0; i < activeSelection.Length; i++)
  129. {
  130. originalParents[i] = activeSelection[i].so.Parent;
  131. activeSelection[i].so.LocalPosition = activeSelection[i].initialPosition;
  132. activeSelection[i].so.LocalRotation = activeSelection[i].initialRotation;
  133. activeSelection[i].so.Parent = temporarySO;
  134. }
  135. temporarySO.LocalRotation *= rotateHandle.Delta;
  136. for (int i = 0; i < activeSelection.Length; i++)
  137. activeSelection[i].so.Parent = originalParents[i];
  138. temporarySO.Destroy();
  139. }
  140. break;
  141. case SceneViewTool.Scale:
  142. {
  143. ScaleHandle scaleHandle = (ScaleHandle) activeHandle;
  144. // Make sure we transform relative to the handle position
  145. SceneObject temporarySO = new SceneObject("Temp");
  146. temporarySO.Position = activeHandle.Position;
  147. temporarySO.LocalScale = initialHandleScale;
  148. SceneObject[] originalParents = new SceneObject[activeSelection.Length];
  149. for (int i = 0; i < activeSelection.Length; i++)
  150. {
  151. originalParents[i] = activeSelection[i].so.Parent;
  152. activeSelection[i].so.LocalPosition = activeSelection[i].initialPosition;
  153. activeSelection[i].so.LocalRotation = activeSelection[i].initialRotation;
  154. activeSelection[i].so.LocalScale = activeSelection[i].initialScale;
  155. activeSelection[i].so.Parent = temporarySO;
  156. }
  157. temporarySO.LocalScale += scaleHandle.Delta;
  158. for (int i = 0; i < activeSelection.Length; i++)
  159. activeSelection[i].so.Parent = originalParents[i];
  160. temporarySO.Destroy();
  161. }
  162. break;
  163. }
  164. }
  165. }
  166. else
  167. {
  168. isDragged = false;
  169. activeSelection = null;
  170. }
  171. }
  172. protected override void Draw()
  173. {
  174. if (activeHandle != null)
  175. activeHandle.DoDraw();
  176. }
  177. }
  178. }