Gizmo3D.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Portions Copyright (c) 2008-2015 the Urho3D project.
  2. //
  3. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  4. // LICENSE: Atomic Game Engine Editor and Tools EULA
  5. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  6. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  7. //
  8. #pragma once
  9. #include <Atomic/Core/Object.h>
  10. #include <Atomic/Math/MathDefs.h>
  11. #include <Atomic/Math/Ray.h>
  12. #include <Atomic/Scene/Scene.h>
  13. #include <Atomic/Atomic3D/StaticModel.h>
  14. #include <Atomic/Graphics/Camera.h>
  15. #include "SceneView3D.h"
  16. using namespace Atomic;
  17. namespace Atomic
  18. {
  19. class Node;
  20. }
  21. namespace AtomicEditor
  22. {
  23. class SceneSelection;
  24. class Gizmo3DAxis
  25. {
  26. public:
  27. Ray axisRay_;
  28. bool selected_;
  29. bool lastSelected_;
  30. float t_;
  31. float d_;
  32. float lastT_;
  33. float lastD_;
  34. Gizmo3DAxis()
  35. {
  36. selected_ = false;
  37. lastSelected_ = false;
  38. t_ = 0.0;
  39. d_ = 0.0;
  40. lastT_ = 0.0;
  41. lastD_ = 0.0;
  42. }
  43. void Update(Ray cameraRay, float scale, bool drag, Node* cameraNode)
  44. {
  45. const float axisMaxD = 0.1f;
  46. const float axisMaxT = 1.0f;
  47. Vector3 closest = cameraRay.ClosestPoint(axisRay_);
  48. Vector3 projected = axisRay_.Project(closest);
  49. d_ = axisRay_.Distance(closest);
  50. t_ = (projected - axisRay_.origin_).DotProduct(axisRay_.direction_);
  51. // Determine the sign of d from a plane that goes through the camera position to the axis
  52. Plane axisPlane(cameraNode->GetPosition(), axisRay_.origin_, axisRay_.origin_ + axisRay_.direction_);
  53. if (axisPlane.Distance(closest) < 0.0)
  54. d_ = -d_;
  55. // Update selected status only when not dragging
  56. if (!drag)
  57. {
  58. selected_ = Abs(d_) < axisMaxD * scale && t_ >= -axisMaxD * scale && t_ <= axisMaxT * scale;
  59. lastT_ = t_;
  60. lastD_ = d_;
  61. }
  62. }
  63. void Moved()
  64. {
  65. lastT_ = t_;
  66. lastD_ = d_;
  67. }
  68. };
  69. enum EditMode
  70. {
  71. EDIT_SELECT,
  72. EDIT_MOVE,
  73. EDIT_ROTATE,
  74. EDIT_SCALE
  75. };
  76. enum AxisMode
  77. {
  78. AXIS_WORLD = 0,
  79. AXIS_LOCAL
  80. };
  81. class Gizmo3D: public Object
  82. {
  83. OBJECT(Gizmo3D);
  84. public:
  85. Gizmo3D(Context* context);
  86. virtual ~Gizmo3D();
  87. void SetView(SceneView3D* view3D);
  88. void SetAxisMode(AxisMode mode);
  89. AxisMode GetAxisMode() const { return axisMode_; }
  90. void SetEditMode(EditMode mode);
  91. bool Selected()
  92. {
  93. return gizmoAxisX_.selected_ || gizmoAxisY_.selected_ || gizmoAxisZ_.selected_;
  94. }
  95. void Show();
  96. void Hide();
  97. void Update();
  98. Node* GetGizmoNode() { return gizmoNode_; }
  99. float GetSnapTranslationX() const;
  100. float GetSnapTranslationY() const;
  101. float GetSnapTranslationZ() const;
  102. float GetSnapRotation() const;
  103. float GetSnapScale() const;
  104. void SetSnapTranslationX(float value);
  105. void SetSnapTranslationY(float value);
  106. void SetSnapTranslationZ(float value);
  107. void SetSnapRotation(float value);
  108. void SetSnapScale(float value);
  109. private:
  110. void Position();
  111. void Use();
  112. void Drag();
  113. void Moved();
  114. void CalculateGizmoAxes();
  115. bool MoveEditNodes(Vector3 adjust);
  116. bool RotateEditNodes(Vector3 adjust);
  117. bool ScaleEditNodes(Vector3 adjust);
  118. SharedPtr<Node> gizmoNode_;
  119. WeakPtr<SceneView3D> view3D_;
  120. WeakPtr<Scene> scene_;
  121. WeakPtr<Camera> camera_;
  122. WeakPtr<StaticModel> gizmo_;
  123. WeakPtr<SceneSelection> selection_;
  124. Gizmo3DAxis gizmoAxisX_;
  125. Gizmo3DAxis gizmoAxisY_;
  126. Gizmo3DAxis gizmoAxisZ_;
  127. EditMode editMode_;
  128. EditMode lastEditMode_;
  129. AxisMode axisMode_;
  130. bool dragging_;
  131. // snap settings
  132. float snapTranslationX_;
  133. float snapTranslationY_;
  134. float snapTranslationZ_;
  135. float snapRotation_;
  136. float snapScale_;
  137. };
  138. }