Gizmo3D.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Portions Copyright (c) 2008-2015 the Urho3D project.
  2. //
  3. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include <Atomic/Core/Object.h>
  25. #include <Atomic/Math/MathDefs.h>
  26. #include <Atomic/Math/Ray.h>
  27. #include <Atomic/Scene/Scene.h>
  28. #include <Atomic/Graphics/StaticModel.h>
  29. #include <Atomic/Graphics/Camera.h>
  30. #include "SceneView3D.h"
  31. using namespace Atomic;
  32. namespace Atomic
  33. {
  34. class Node;
  35. }
  36. namespace AtomicEditor
  37. {
  38. class SceneSelection;
  39. class Gizmo3DAxis
  40. {
  41. public:
  42. Ray axisRay_;
  43. bool selected_;
  44. bool lastSelected_;
  45. float t_;
  46. float d_;
  47. float lastT_;
  48. float lastD_;
  49. Gizmo3DAxis()
  50. {
  51. selected_ = false;
  52. lastSelected_ = false;
  53. t_ = 0.0;
  54. d_ = 0.0;
  55. lastT_ = 0.0;
  56. lastD_ = 0.0;
  57. }
  58. void Update(Ray cameraRay, float scale, bool drag, Node* cameraNode)
  59. {
  60. const float axisMaxD = 0.1f;
  61. const float axisMaxT = 1.0f;
  62. Vector3 closest = cameraRay.ClosestPoint(axisRay_);
  63. Vector3 projected = axisRay_.Project(closest);
  64. d_ = axisRay_.Distance(closest);
  65. t_ = (projected - axisRay_.origin_).DotProduct(axisRay_.direction_);
  66. // Determine the sign of d from a plane that goes through the camera position to the axis
  67. Plane axisPlane(cameraNode->GetPosition(), axisRay_.origin_, axisRay_.origin_ + axisRay_.direction_);
  68. if (axisPlane.Distance(closest) < 0.0)
  69. d_ = -d_;
  70. // Update selected status only when not dragging
  71. if (!drag)
  72. {
  73. selected_ = Atomic::Abs(d_) < axisMaxD * scale && t_ >= -axisMaxD * scale && t_ <= axisMaxT * scale;
  74. lastT_ = t_;
  75. lastD_ = d_;
  76. }
  77. }
  78. void Moved()
  79. {
  80. lastT_ = t_;
  81. lastD_ = d_;
  82. }
  83. };
  84. enum EditMode
  85. {
  86. EDIT_SELECT,
  87. EDIT_MOVE,
  88. EDIT_ROTATE,
  89. EDIT_SCALE
  90. };
  91. enum AxisMode
  92. {
  93. AXIS_WORLD = 0,
  94. AXIS_LOCAL
  95. };
  96. class Gizmo3D: public Object
  97. {
  98. ATOMIC_OBJECT(Gizmo3D, Object);
  99. public:
  100. Gizmo3D(Context* context);
  101. virtual ~Gizmo3D();
  102. void SetView(SceneView3D* view3D);
  103. void SetAxisMode(AxisMode mode);
  104. AxisMode GetAxisMode() const { return axisMode_; }
  105. void SetEditMode(EditMode mode);
  106. bool Selected()
  107. {
  108. return gizmoAxisX_.selected_ || gizmoAxisY_.selected_ || gizmoAxisZ_.selected_;
  109. }
  110. void Show();
  111. void Hide();
  112. void Update();
  113. Node* GetGizmoNode() { return gizmoNode_; }
  114. float GetSnapTranslationX() const;
  115. float GetSnapTranslationY() const;
  116. float GetSnapTranslationZ() const;
  117. float GetSnapRotation() const;
  118. float GetSnapScale() const;
  119. void SetSnapTranslationX(float value);
  120. void SetSnapTranslationY(float value);
  121. void SetSnapTranslationZ(float value);
  122. void SetSnapRotation(float value);
  123. void SetSnapScale(float value);
  124. private:
  125. void Position();
  126. void Use();
  127. void Drag();
  128. void Moved();
  129. void CalculateGizmoAxes();
  130. bool MoveEditNodes(Vector3 adjust);
  131. bool RotateEditNodes(Vector3 adjust);
  132. bool ScaleEditNodes(Vector3 adjust);
  133. SharedPtr<Node> gizmoNode_;
  134. WeakPtr<SceneView3D> view3D_;
  135. WeakPtr<Scene> scene_;
  136. WeakPtr<Camera> camera_;
  137. WeakPtr<StaticModel> gizmo_;
  138. WeakPtr<SceneSelection> selection_;
  139. Gizmo3DAxis gizmoAxisX_;
  140. Gizmo3DAxis gizmoAxisY_;
  141. Gizmo3DAxis gizmoAxisZ_;
  142. EditMode editMode_;
  143. EditMode lastEditMode_;
  144. AxisMode axisMode_;
  145. bool dragging_;
  146. bool cloning_;
  147. bool startClone_;
  148. // snap settings
  149. float snapTranslationX_;
  150. float snapTranslationY_;
  151. float snapTranslationZ_;
  152. float snapRotation_;
  153. float snapScale_;
  154. };
  155. }