TransformGizmo.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. Copyright (C) 2013 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "Polycode.h"
  21. #include "OSBasics.h"
  22. #include "PolycodeUI.h"
  23. using namespace Polycode;
  24. class TransformGrips : public UIElement {
  25. public:
  26. TransformGrips();
  27. ~TransformGrips();
  28. void setGripRectangle(Polycode::Rectangle rectangle, Vector2 offset);
  29. void handleEvent(Event *event);
  30. Polycode::Rectangle getGripRectangle();
  31. Vector2 getAnchorPoint();
  32. private:
  33. bool transforming;
  34. UIImage *movingTransform;
  35. Polycode::Rectangle gripRectangle;
  36. Vector2 anchorPoint;
  37. Vector2 mouseBase;
  38. UIRect *mainRect;
  39. UIImage *transformTL;
  40. UIImage *transformT;
  41. UIImage *transformTR;
  42. UIImage *transformR;
  43. UIImage *transformL;
  44. UIImage *transformBL;
  45. UIImage *transformB;
  46. UIImage *transformBR;
  47. UIImage *transformOffset;
  48. std::vector<UIElement*> grips;
  49. };
  50. class TrasnformGizmoEvent : public Event {
  51. public:
  52. TrasnformGizmoEvent(int mode);
  53. static const int EVENT_TRANSLATE = 0;
  54. static const int EVENT_SCALE = 1;
  55. static const int EVENT_ROTATE = 2;
  56. int mode;
  57. };
  58. class TransformGizmo : public Entity {
  59. public:
  60. TransformGizmo(Scene *targetScene, Camera *targetCamera);
  61. ~TransformGizmo();
  62. void handleEvent(Event *event);
  63. void setTransformMode(int newMode);
  64. void setGizmoMode(int newMode);
  65. void Update();
  66. void enableSnap(bool val);
  67. void setSnapSize(Number snapSize);
  68. void setTransformSelection(std::vector<Entity*> selectedEntities);
  69. void transformSelectedEntities(const Vector3 &move, const Vector3 &scale, Number rotate);
  70. Vector3 getTransformPlanePosition();
  71. Vector3 getPositionAlongAxis();
  72. bool isConstrainedToSingleAxis();
  73. Number getTransformPlaneAngle();
  74. Number get2dAngle();
  75. Vector2 getCorrectedMousePosition();
  76. void resetTransform();
  77. void setTransformOrientation(int orientation);
  78. void updateOrientationForEntity(Entity *entity);
  79. void setTransformPlane(Number x, Number y, Number z, bool forceGlobal = false);
  80. void setTransformPlane(bool useX, bool useY, bool useZ);
  81. void setTransformPlaneFromView();
  82. void setCenterMode(int centerMode);
  83. void toggleOrientation();
  84. static const int TRANSFORM_MOVE = 0;
  85. static const int TRANSFORM_SCALE = 1;
  86. static const int TRANSFORM_ROTATE = 2;
  87. static const int TRANSFORM_SCALE_VIEW = 3;
  88. static const int TRANSFORM_ROTATE_VIEW = 4;
  89. static const int TRANSFORM_MOVE_VIEW = 5;
  90. static const int GIZMO_MODE_3D = 0;
  91. static const int GIZMO_MODE_2D_X = 1;
  92. static const int GIZMO_MODE_2D_Y = 2;
  93. static const int GIZMO_MODE_2D_Z = 3;
  94. static const int ORIENTATION_GLOBAL = 0;
  95. static const int ORIENTATION_LOCAL = 1;
  96. static const int CENTER_MODE_MEDIAN = 0;
  97. static const int CENTER_MODE_INDIVIDUAL = 1;
  98. bool enableGizmo;
  99. private:
  100. bool snapEnabled;
  101. Number snapSize;
  102. void dispatchEndEvent();
  103. int transformMode;
  104. int gizmoMode;
  105. int orientation;
  106. int startingOrientation;
  107. int centerMode;
  108. std::vector<Entity*> selectedEntities;
  109. std::vector<Vector3> entityPositions;
  110. std::vector<Vector3> oldPosition;
  111. std::vector<Vector3> oldScale;
  112. std::vector<Quaternion> oldRotation;
  113. Scene *targetScene;
  114. Camera *targetCamera;
  115. bool firstMove;
  116. CoreInput *coreInput;
  117. int mode;
  118. int previousMode;
  119. bool transforming;
  120. Vector3 transformConstraint;
  121. Vector3 transformPlane;
  122. Vector3 localTransformPlane;
  123. Number transformPlaneDistance;
  124. Matrix4 planeMatrix;
  125. Vector3 gizmoPoint;
  126. Vector3 startingPoint;
  127. Number startingAngle;
  128. Vector2 mouseStart2d;
  129. Number scaleAmount;
  130. Entity *trasnformDecorators;
  131. Entity *scaleDecorators;
  132. Entity *transformAndScaleLines;
  133. Entity *rotateDectorators;
  134. Entity *xTransformGrip;
  135. Entity *yTransformGrip;
  136. Entity *zTransformGrip;
  137. ScenePrimitive *pitchGrip;
  138. ScenePrimitive *rollGrip;
  139. ScenePrimitive *yawGrip;
  140. ScenePrimitive *viewportRotateGrip;
  141. Entity *viewportRotateGripBase;
  142. SceneMesh *xLine;
  143. SceneMesh *yLine;
  144. SceneMesh *zLine;
  145. ScenePrimitive *xArrow;
  146. ScenePrimitive *yArrow;
  147. ScenePrimitive *zArrow;
  148. ScenePrimitive *xBox;
  149. ScenePrimitive *yBox;
  150. ScenePrimitive *zBox;
  151. ScenePrimitive *outerCircle;
  152. ScenePrimitive *bgCircle;
  153. ScenePrimitive *pitchCircle;
  154. ScenePrimitive *yawCircle;
  155. ScenePrimitive *rollCircle;
  156. };
  157. class TransformGizmoMenu : public UIElement {
  158. public:
  159. TransformGizmoMenu(TransformGizmo *gizmo);
  160. ~TransformGizmoMenu();
  161. void handleEvent(Event *event);
  162. private:
  163. UIIconSelector *transformSelector;
  164. UIComboBox *orientationCombo;
  165. UIIconSelector *centerSelector;
  166. TransformGizmo *gizmo;
  167. };