Gizmo3D.h 3.0 KB

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