DefaultHandleManager.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System.Collections.Generic;
  2. using BansheeEngine;
  3. namespace BansheeEditor
  4. {
  5. internal sealed class DefaultHandleManager : Handle
  6. {
  7. private SceneViewTool activeHandleType = SceneViewTool.View;
  8. private DefaultHandle activeHandle;
  9. protected override void PreInput()
  10. {
  11. SceneObject[] selectedSceneObjects = Selection.sceneObjects;
  12. if (activeHandleType != EditorApplication.ActiveSceneTool || selectedSceneObjects.Length == 0)
  13. {
  14. if (activeHandle != null)
  15. {
  16. activeHandle.Destroy();
  17. activeHandle = null;
  18. }
  19. }
  20. if (activeHandleType != EditorApplication.ActiveSceneTool && selectedSceneObjects.Length > 0)
  21. {
  22. if (activeHandle != null)
  23. {
  24. activeHandle.Destroy();
  25. activeHandle = null;
  26. }
  27. switch (EditorApplication.ActiveSceneTool)
  28. {
  29. case SceneViewTool.Move:
  30. activeHandle = new MoveHandle();
  31. break;
  32. case SceneViewTool.Rotate:
  33. activeHandle = new RotateHandle();
  34. break;
  35. case SceneViewTool.Scale:
  36. activeHandle = new ScaleHandle();
  37. break;
  38. }
  39. activeHandleType = EditorApplication.ActiveSceneTool;
  40. }
  41. if (activeHandle != null)
  42. {
  43. Quaternion rotation;
  44. if (EditorApplication.HandleCoordinateMode == HandleCoordinateMode.World)
  45. rotation = Quaternion.identity;
  46. else
  47. rotation = selectedSceneObjects[0].rotation; // We don't average rotation in case of multi-selection
  48. Vector3 position;
  49. if (EditorApplication.HandlePositionMode == HandlePositionMode.Pivot)
  50. position = selectedSceneObjects[0].position; // Just take pivot from the first one, no averaging
  51. else
  52. {
  53. List<SceneObject> flatenedHierarchy = new List<SceneObject>();
  54. foreach (var so in selectedSceneObjects)
  55. {
  56. flatenedHierarchy.AddRange(EditorUtility.FlattenHierarchy(so));
  57. }
  58. AABox selectionBounds = EditorUtility.CalculateBounds(flatenedHierarchy.ToArray());
  59. position = selectionBounds.Center;
  60. }
  61. activeHandle.Position = position;
  62. activeHandle.Rotation = rotation;
  63. activeHandle.DoPreInput();
  64. }
  65. }
  66. protected override void PostInput()
  67. {
  68. if (activeHandle != null)
  69. {
  70. activeHandle.DoPostInput();
  71. SceneObject[] selectedSceneObjects = Selection.sceneObjects;
  72. switch (activeHandleType)
  73. {
  74. case SceneViewTool.Move:
  75. {
  76. MoveHandle moveHandle = (MoveHandle)activeHandle;
  77. foreach (var so in selectedSceneObjects)
  78. so.position += moveHandle.Delta;
  79. }
  80. break;
  81. case SceneViewTool.Rotate:
  82. {
  83. RotateHandle rotateHandle = (RotateHandle)activeHandle;
  84. // TODO - Add delta rotation
  85. //foreach (var so in selectedSceneObjects)
  86. // so.rotation += rotateHandle.Delta;
  87. }
  88. break;
  89. case SceneViewTool.Scale:
  90. {
  91. ScaleHandle scaleHandle = (ScaleHandle)activeHandle;
  92. // TODO - Add delta scale
  93. //foreach (var so in selectedSceneObjects)
  94. // so.localScale += scaleHandle.Delta;
  95. }
  96. break;
  97. }
  98. }
  99. }
  100. protected override void Draw()
  101. {
  102. if (activeHandle != null)
  103. activeHandle.DoDraw();
  104. }
  105. }
  106. }