DefaultHandleManager.cs 6.9 KB

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