DefaultHandleManager.cs 8.2 KB

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