DefaultHandleManager.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. }
  14. public SceneObject so;
  15. public Vector3 initialPosition;
  16. }
  17. private SceneViewTool activeHandleType = SceneViewTool.View;
  18. private DefaultHandle activeHandle;
  19. private HandledObject[] activeSelection;
  20. private bool isDragged;
  21. protected override void PreInput()
  22. {
  23. SceneObject[] selectedSceneObjects = Selection.sceneObjects;
  24. if (selectedSceneObjects.Length == 0)
  25. {
  26. if (activeHandle != null)
  27. {
  28. activeHandle.Destroy();
  29. activeHandle = null;
  30. }
  31. }
  32. else
  33. {
  34. if (activeHandleType != EditorApplication.ActiveSceneTool || activeHandle == null)
  35. {
  36. if (activeHandle != null)
  37. {
  38. activeHandle.Destroy();
  39. activeHandle = null;
  40. }
  41. switch (EditorApplication.ActiveSceneTool)
  42. {
  43. case SceneViewTool.Move:
  44. activeHandle = new MoveHandle();
  45. break;
  46. case SceneViewTool.Rotate:
  47. activeHandle = new RotateHandle();
  48. break;
  49. case SceneViewTool.Scale:
  50. activeHandle = new ScaleHandle();
  51. break;
  52. }
  53. activeHandleType = EditorApplication.ActiveSceneTool;
  54. }
  55. }
  56. if (activeHandle != null)
  57. {
  58. Quaternion rotation;
  59. if (EditorApplication.HandleCoordinateMode == HandleCoordinateMode.World)
  60. rotation = Quaternion.identity;
  61. else
  62. rotation = selectedSceneObjects[0].rotation; // We don't average rotation in case of multi-selection
  63. Vector3 position;
  64. if (EditorApplication.HandlePositionMode == HandlePositionMode.Pivot)
  65. position = selectedSceneObjects[0].position; // Just take pivot from the first one, no averaging
  66. else
  67. {
  68. List<SceneObject> flatenedHierarchy = new List<SceneObject>();
  69. foreach (var so in selectedSceneObjects)
  70. {
  71. flatenedHierarchy.AddRange(EditorUtility.FlattenHierarchy(so));
  72. }
  73. AABox selectionBounds = EditorUtility.CalculateBounds(flatenedHierarchy.ToArray());
  74. position = selectionBounds.center;
  75. }
  76. activeHandle.Position = position;
  77. activeHandle.Rotation = rotation;
  78. activeHandle.DoPreInput();
  79. }
  80. }
  81. protected override void PostInput()
  82. {
  83. if (activeHandle != null)
  84. {
  85. if (activeHandle.IsDragged())
  86. {
  87. if (!isDragged)
  88. {
  89. isDragged = true;
  90. SceneObject[] selectedSceneObjects = Selection.sceneObjects;
  91. activeSelection = new HandledObject[selectedSceneObjects.Length];
  92. for (int i = 0; i < selectedSceneObjects.Length; i++)
  93. activeSelection[i] = new HandledObject(selectedSceneObjects[0]);
  94. }
  95. }
  96. else
  97. {
  98. isDragged = false;
  99. activeSelection = null;
  100. }
  101. activeHandle.DoPostInput();
  102. if (activeHandle.IsDragged())
  103. {
  104. switch (activeHandleType)
  105. {
  106. case SceneViewTool.Move:
  107. {
  108. MoveHandle moveHandle = (MoveHandle) activeHandle;
  109. foreach (var selectedObj in activeSelection)
  110. selectedObj.so.position = selectedObj.initialPosition + moveHandle.Delta;
  111. }
  112. break;
  113. case SceneViewTool.Rotate:
  114. {
  115. RotateHandle rotateHandle = (RotateHandle) activeHandle;
  116. // TODO - Add delta rotation
  117. //foreach (var so in selectedSceneObjects)
  118. // so.rotation += rotateHandle.Delta;
  119. }
  120. break;
  121. case SceneViewTool.Scale:
  122. {
  123. ScaleHandle scaleHandle = (ScaleHandle) activeHandle;
  124. // TODO - Add delta scale
  125. //foreach (var so in selectedSceneObjects)
  126. // so.localScale += scaleHandle.Delta;
  127. }
  128. break;
  129. }
  130. }
  131. }
  132. else
  133. {
  134. isDragged = false;
  135. activeSelection = null;
  136. }
  137. }
  138. protected override void Draw()
  139. {
  140. if (activeHandle != null)
  141. activeHandle.DoDraw();
  142. }
  143. }
  144. }