DefaultHandleManager.cs 8.1 KB

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