DefaultHandleManager.cs 4.4 KB

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