DefaultHandleManager.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. {
  75. flatenedHierarchy.AddRange(EditorUtility.FlattenHierarchy(so));
  76. }
  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. }
  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. {
  112. MoveHandle moveHandle = (MoveHandle) activeHandle;
  113. foreach (var selectedObj in activeSelection)
  114. selectedObj.so.Position = selectedObj.initialPosition + moveHandle.Delta;
  115. }
  116. break;
  117. case SceneViewTool.Rotate:
  118. {
  119. RotateHandle rotateHandle = (RotateHandle) activeHandle;
  120. foreach (var selectedObj in activeSelection)
  121. selectedObj.so.Rotation = selectedObj.initialRotation * rotateHandle.Delta;
  122. }
  123. break;
  124. case SceneViewTool.Scale:
  125. {
  126. ScaleHandle scaleHandle = (ScaleHandle) activeHandle;
  127. foreach (var selectedObj in activeSelection)
  128. selectedObj.so.LocalScale = selectedObj.initialScale + scaleHandle.Delta;
  129. }
  130. break;
  131. }
  132. }
  133. }
  134. else
  135. {
  136. isDragged = false;
  137. activeSelection = null;
  138. }
  139. }
  140. protected override void Draw()
  141. {
  142. if (activeHandle != null)
  143. activeHandle.DoDraw();
  144. }
  145. }
  146. }