ImGuizmo.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // https://github.com/CedricGuillemet/ImGuizmo
  2. // v 1.89 WIP
  3. //
  4. // The MIT License(MIT)
  5. //
  6. // Copyright(c) 2021 Cedric Guillemet
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files(the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions :
  14. //
  15. // The above copyright notice and this permission notice shall be included in all
  16. // copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. // SOFTWARE.
  25. //
  26. // -------------------------------------------------------------------------------------------
  27. // History :
  28. // 2019/11/03 View gizmo
  29. // 2016/09/11 Behind camera culling. Scaling Delta matrix not multiplied by source matrix scales. local/world rotation and translation fixed. Display message is incorrect (X: ... Y:...) in local mode.
  30. // 2016/09/09 Hatched negative axis. Snapping. Documentation update.
  31. // 2016/09/04 Axis switch and translation plan autohiding. Scale transform stability improved
  32. // 2016/09/01 Mogwai changed to Manipulate. Draw debug cube. Fixed inverted scale. Mixing scale and translation/rotation gives bad results.
  33. // 2016/08/31 First version
  34. //
  35. // -------------------------------------------------------------------------------------------
  36. // Future (no order):
  37. //
  38. // - Multi view
  39. // - display rotation/translation/scale infos in local/world space and not only local
  40. // - finish local/world matrix application
  41. // - OPERATION as bitmask
  42. //
  43. // -------------------------------------------------------------------------------------------
  44. // Example
  45. #if 0
  46. void EditTransform(const Camera& camera, matrix_t& matrix)
  47. {
  48. static ImGuizmo::OPERATION mCurrentGizmoOperation(ImGuizmo::ROTATE);
  49. static ImGuizmo::MODE mCurrentGizmoMode(ImGuizmo::WORLD);
  50. if (ImGui::IsKeyPressed(90))
  51. mCurrentGizmoOperation = ImGuizmo::TRANSLATE;
  52. if (ImGui::IsKeyPressed(69))
  53. mCurrentGizmoOperation = ImGuizmo::ROTATE;
  54. if (ImGui::IsKeyPressed(82)) // r Key
  55. mCurrentGizmoOperation = ImGuizmo::SCALE;
  56. if (ImGui::RadioButton("Translate", mCurrentGizmoOperation == ImGuizmo::TRANSLATE))
  57. mCurrentGizmoOperation = ImGuizmo::TRANSLATE;
  58. ImGui::SameLine();
  59. if (ImGui::RadioButton("Rotate", mCurrentGizmoOperation == ImGuizmo::ROTATE))
  60. mCurrentGizmoOperation = ImGuizmo::ROTATE;
  61. ImGui::SameLine();
  62. if (ImGui::RadioButton("Scale", mCurrentGizmoOperation == ImGuizmo::SCALE))
  63. mCurrentGizmoOperation = ImGuizmo::SCALE;
  64. float matrixTranslation[3], matrixRotation[3], matrixScale[3];
  65. ImGuizmo::DecomposeMatrixToComponents(matrix.m16, matrixTranslation, matrixRotation, matrixScale);
  66. ImGui::InputFloat3("Tr", matrixTranslation, 3);
  67. ImGui::InputFloat3("Rt", matrixRotation, 3);
  68. ImGui::InputFloat3("Sc", matrixScale, 3);
  69. ImGuizmo::RecomposeMatrixFromComponents(matrixTranslation, matrixRotation, matrixScale, matrix.m16);
  70. if (mCurrentGizmoOperation != ImGuizmo::SCALE)
  71. {
  72. if (ImGui::RadioButton("Local", mCurrentGizmoMode == ImGuizmo::LOCAL))
  73. mCurrentGizmoMode = ImGuizmo::LOCAL;
  74. ImGui::SameLine();
  75. if (ImGui::RadioButton("World", mCurrentGizmoMode == ImGuizmo::WORLD))
  76. mCurrentGizmoMode = ImGuizmo::WORLD;
  77. }
  78. static bool useSnap(false);
  79. if (ImGui::IsKeyPressed(83))
  80. useSnap = !useSnap;
  81. ImGui::Checkbox("", &useSnap);
  82. ImGui::SameLine();
  83. vec_t snap;
  84. switch (mCurrentGizmoOperation)
  85. {
  86. case ImGuizmo::TRANSLATE:
  87. snap = config.mSnapTranslation;
  88. ImGui::InputFloat3("Snap", &snap.x);
  89. break;
  90. case ImGuizmo::ROTATE:
  91. snap = config.mSnapRotation;
  92. ImGui::InputFloat("Angle Snap", &snap.x);
  93. break;
  94. case ImGuizmo::SCALE:
  95. snap = config.mSnapScale;
  96. ImGui::InputFloat("Scale Snap", &snap.x);
  97. break;
  98. }
  99. ImGuiIO& io = ImGui::GetIO();
  100. ImGuizmo::SetRect(0, 0, io.DisplaySize.x, io.DisplaySize.y);
  101. ImGuizmo::Manipulate(camera.mView.m16, camera.mProjection.m16, mCurrentGizmoOperation, mCurrentGizmoMode, matrix.m16, NULL, useSnap ? &snap.x : NULL);
  102. }
  103. #endif
  104. #pragma once
  105. #ifdef USE_IMGUI_API
  106. #include "imconfig.h"
  107. #endif
  108. #ifndef IMGUI_API
  109. #define IMGUI_API
  110. #endif
  111. #ifndef IMGUIZMO_NAMESPACE
  112. #define IMGUIZMO_NAMESPACE ImGuizmo
  113. #endif
  114. namespace IMGUIZMO_NAMESPACE
  115. {
  116. // call inside your own window and before Manipulate() in order to draw gizmo to that window.
  117. // Or pass a specific ImDrawList to draw to (e.g. ImGui::GetForegroundDrawList()).
  118. IMGUI_API void SetDrawlist(ImDrawList* drawlist = nullptr);
  119. // call BeginFrame right after ImGui_XXXX_NewFrame();
  120. IMGUI_API void BeginFrame();
  121. // this is necessary because when imguizmo is compiled into a dll, and imgui into another
  122. // globals are not shared between them.
  123. // More details at https://stackoverflow.com/questions/19373061/what-happens-to-global-and-static-variables-in-a-shared-library-when-it-is-dynam
  124. // expose method to set imgui context
  125. IMGUI_API void SetImGuiContext(ImGuiContext* ctx);
  126. // return true if mouse cursor is over any gizmo control (axis, plan or screen component)
  127. IMGUI_API bool IsOver();
  128. // return true if mouse IsOver or if the gizmo is in moving state
  129. IMGUI_API bool IsUsing();
  130. // return true if any gizmo is in moving state
  131. IMGUI_API bool IsUsingAny();
  132. // enable/disable the gizmo. Stay in the state until next call to Enable.
  133. // gizmo is rendered with gray half transparent color when disabled
  134. IMGUI_API void Enable(bool enable);
  135. // helper functions for manualy editing translation/rotation/scale with an input float
  136. // translation, rotation and scale float points to 3 floats each
  137. // Angles are in degrees (more suitable for human editing)
  138. // example:
  139. // float matrixTranslation[3], matrixRotation[3], matrixScale[3];
  140. // ImGuizmo::DecomposeMatrixToComponents(gizmoMatrix.m16, matrixTranslation, matrixRotation, matrixScale);
  141. // ImGui::InputFloat3("Tr", matrixTranslation, 3);
  142. // ImGui::InputFloat3("Rt", matrixRotation, 3);
  143. // ImGui::InputFloat3("Sc", matrixScale, 3);
  144. // ImGuizmo::RecomposeMatrixFromComponents(matrixTranslation, matrixRotation, matrixScale, gizmoMatrix.m16);
  145. //
  146. // These functions have some numerical stability issues for now. Use with caution.
  147. IMGUI_API void DecomposeMatrixToComponents(const float* matrix, float* translation, float* rotation, float* scale);
  148. IMGUI_API void RecomposeMatrixFromComponents(const float* translation, const float* rotation, const float* scale, float* matrix);
  149. IMGUI_API void SetRect(float x, float y, float width, float height);
  150. // default is false
  151. IMGUI_API void SetOrthographic(bool isOrthographic);
  152. // Render a cube with face color corresponding to face normal. Usefull for debug/tests
  153. IMGUI_API void DrawCubes(const float* view, const float* projection, const float* matrices, int matrixCount);
  154. IMGUI_API void DrawGrid(const float* view, const float* projection, const float* matrix, const float gridSize);
  155. // call it when you want a gizmo
  156. // Needs view and projection matrices.
  157. // matrix parameter is the source matrix (where will be gizmo be drawn) and might be transformed by the function. Return deltaMatrix is optional
  158. // translation is applied in world space
  159. enum OPERATION
  160. {
  161. TRANSLATE_X = (1u << 0),
  162. TRANSLATE_Y = (1u << 1),
  163. TRANSLATE_Z = (1u << 2),
  164. ROTATE_X = (1u << 3),
  165. ROTATE_Y = (1u << 4),
  166. ROTATE_Z = (1u << 5),
  167. ROTATE_SCREEN = (1u << 6),
  168. SCALE_X = (1u << 7),
  169. SCALE_Y = (1u << 8),
  170. SCALE_Z = (1u << 9),
  171. BOUNDS = (1u << 10),
  172. SCALE_XU = (1u << 11),
  173. SCALE_YU = (1u << 12),
  174. SCALE_ZU = (1u << 13),
  175. TRANSLATE = TRANSLATE_X | TRANSLATE_Y | TRANSLATE_Z,
  176. ROTATE = ROTATE_X | ROTATE_Y | ROTATE_Z | ROTATE_SCREEN,
  177. SCALE = SCALE_X | SCALE_Y | SCALE_Z,
  178. SCALEU = SCALE_XU | SCALE_YU | SCALE_ZU, // universal
  179. UNIVERSAL = TRANSLATE | ROTATE | SCALEU
  180. };
  181. inline OPERATION operator|(OPERATION lhs, OPERATION rhs)
  182. {
  183. return static_cast<OPERATION>(static_cast<int>(lhs) | static_cast<int>(rhs));
  184. }
  185. enum MODE
  186. {
  187. LOCAL,
  188. WORLD
  189. };
  190. IMGUI_API bool Manipulate(const float* view, const float* projection, OPERATION operation, MODE mode, float* matrix, float* deltaMatrix = NULL, const float* snap = NULL, const float* localBounds = NULL, const float* boundsSnap = NULL);
  191. //
  192. // Please note that this cubeview is patented by Autodesk : https://patents.google.com/patent/US7782319B2/en
  193. // It seems to be a defensive patent in the US. I don't think it will bring troubles using it as
  194. // other software are using the same mechanics. But just in case, you are now warned!
  195. //
  196. IMGUI_API void ViewManipulate(float* view, float length, ImVec2 position, ImVec2 size, ImU32 backgroundColor);
  197. // use this version if you did not call Manipulate before and you are just using ViewManipulate
  198. IMGUI_API void ViewManipulate(float* view, const float* projection, OPERATION operation, MODE mode, float* matrix, float length, ImVec2 position, ImVec2 size, ImU32 backgroundColor);
  199. IMGUI_API void SetID(int id);
  200. // return true if the cursor is over the operation's gizmo
  201. IMGUI_API bool IsOver(OPERATION op);
  202. IMGUI_API void SetGizmoSizeClipSpace(float value);
  203. // Allow axis to flip
  204. // When true (default), the guizmo axis flip for better visibility
  205. // When false, they always stay along the positive world/local axis
  206. IMGUI_API void AllowAxisFlip(bool value);
  207. // Configure the limit where axis are hidden
  208. IMGUI_API void SetAxisLimit(float value);
  209. // Configure the limit where planes are hiden
  210. IMGUI_API void SetPlaneLimit(float value);
  211. enum COLOR
  212. {
  213. DIRECTION_X, // directionColor[0]
  214. DIRECTION_Y, // directionColor[1]
  215. DIRECTION_Z, // directionColor[2]
  216. PLANE_X, // planeColor[0]
  217. PLANE_Y, // planeColor[1]
  218. PLANE_Z, // planeColor[2]
  219. SELECTION, // selectionColor
  220. INACTIVE, // inactiveColor
  221. TRANSLATION_LINE, // translationLineColor
  222. SCALE_LINE,
  223. ROTATION_USING_BORDER,
  224. ROTATION_USING_FILL,
  225. HATCHED_AXIS_LINES,
  226. TEXT,
  227. TEXT_SHADOW,
  228. COUNT
  229. };
  230. struct Style
  231. {
  232. IMGUI_API Style();
  233. float TranslationLineThickness; // Thickness of lines for translation gizmo
  234. float TranslationLineArrowSize; // Size of arrow at the end of lines for translation gizmo
  235. float RotationLineThickness; // Thickness of lines for rotation gizmo
  236. float RotationOuterLineThickness; // Thickness of line surrounding the rotation gizmo
  237. float ScaleLineThickness; // Thickness of lines for scale gizmo
  238. float ScaleLineCircleSize; // Size of circle at the end of lines for scale gizmo
  239. float HatchedAxisLineThickness; // Thickness of hatched axis lines
  240. float CenterCircleSize; // Size of circle at the center of the translate/scale gizmo
  241. ImVec4 Colors[COLOR::COUNT];
  242. };
  243. IMGUI_API Style& GetStyle();
  244. }