gizmo.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // https://github.com/CedricGuillemet/ImGuizmo
  2. // v1.91.3 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. struct ImGuiWindow;
  115. namespace IMGUIZMO_NAMESPACE
  116. {
  117. // call inside your own window and before Manipulate() in order to draw gizmo to that window.
  118. // Or pass a specific ImDrawList to draw to (e.g. ImGui::GetForegroundDrawList()).
  119. IMGUI_API void SetDrawlist(ImDrawList* drawlist = nullptr);
  120. // call BeginFrame right after ImGui_XXXX_NewFrame();
  121. IMGUI_API void BeginFrame();
  122. //
  123. IMGUI_API void Create();
  124. //
  125. IMGUI_API void Destroy();
  126. // this is necessary because when imguizmo is compiled into a dll, and imgui into another
  127. // globals are not shared between them.
  128. // More details at https://stackoverflow.com/questions/19373061/what-happens-to-global-and-static-variables-in-a-shared-library-when-it-is-dynam
  129. // expose method to set imgui context
  130. IMGUI_API void SetImGuiContext(ImGuiContext* ctx);
  131. // return true if mouse cursor is over any gizmo control (axis, plan or screen component)
  132. IMGUI_API bool IsOver();
  133. // return true if mouse IsOver or if the gizmo is in moving state
  134. IMGUI_API bool IsUsing();
  135. // return true if the view gizmo is in moving state
  136. IMGUI_API bool IsUsingViewManipulate();
  137. // only check if your mouse is over the view manipulator - no matter whether it's active or not
  138. IMGUI_API bool IsViewManipulateHovered();
  139. // return true if any gizmo is in moving state
  140. IMGUI_API bool IsUsingAny();
  141. // enable/disable the gizmo. Stay in the state until next call to Enable.
  142. // gizmo is rendered with gray half transparent color when disabled
  143. IMGUI_API void Enable(bool enable);
  144. // helper functions for manualy editing translation/rotation/scale with an input float
  145. // translation, rotation and scale float points to 3 floats each
  146. // Angles are in degrees (more suitable for human editing)
  147. // example:
  148. // float matrixTranslation[3], matrixRotation[3], matrixScale[3];
  149. // ImGuizmo::DecomposeMatrixToComponents(gizmoMatrix.m16, matrixTranslation, matrixRotation, matrixScale);
  150. // ImGui::InputFloat3("Tr", matrixTranslation, 3);
  151. // ImGui::InputFloat3("Rt", matrixRotation, 3);
  152. // ImGui::InputFloat3("Sc", matrixScale, 3);
  153. // ImGuizmo::RecomposeMatrixFromComponents(matrixTranslation, matrixRotation, matrixScale, gizmoMatrix.m16);
  154. //
  155. // These functions have some numerical stability issues for now. Use with caution.
  156. IMGUI_API void DecomposeMatrixToComponents(const float* matrix, float* translation, float* rotation, float* scale);
  157. IMGUI_API void RecomposeMatrixFromComponents(const float* translation, const float* rotation, const float* scale, float* matrix);
  158. IMGUI_API void SetRect(float x, float y, float width, float height);
  159. // default is false
  160. IMGUI_API void SetOrthographic(bool isOrthographic);
  161. // Render a cube with face color corresponding to face normal. Usefull for debug/tests
  162. IMGUI_API void DrawCubes(const float* view, const float* projection, const float* matrices, int matrixCount);
  163. IMGUI_API void DrawGrid(const float* view, const float* projection, const float* matrix, const float gridSize);
  164. // call it when you want a gizmo
  165. // Needs view and projection matrices.
  166. // matrix parameter is the source matrix (where will be gizmo be drawn) and might be transformed by the function. Return deltaMatrix is optional
  167. // translation is applied in world space
  168. enum OPERATION
  169. {
  170. TRANSLATE_X = (1u << 0),
  171. TRANSLATE_Y = (1u << 1),
  172. TRANSLATE_Z = (1u << 2),
  173. ROTATE_X = (1u << 3),
  174. ROTATE_Y = (1u << 4),
  175. ROTATE_Z = (1u << 5),
  176. ROTATE_SCREEN = (1u << 6),
  177. SCALE_X = (1u << 7),
  178. SCALE_Y = (1u << 8),
  179. SCALE_Z = (1u << 9),
  180. BOUNDS = (1u << 10),
  181. SCALE_XU = (1u << 11),
  182. SCALE_YU = (1u << 12),
  183. SCALE_ZU = (1u << 13),
  184. TRANSLATE = TRANSLATE_X | TRANSLATE_Y | TRANSLATE_Z,
  185. ROTATE = ROTATE_X | ROTATE_Y | ROTATE_Z | ROTATE_SCREEN,
  186. SCALE = SCALE_X | SCALE_Y | SCALE_Z,
  187. SCALEU = SCALE_XU | SCALE_YU | SCALE_ZU, // universal
  188. UNIVERSAL = TRANSLATE | ROTATE | SCALEU
  189. };
  190. inline OPERATION operator|(OPERATION lhs, OPERATION rhs)
  191. {
  192. return static_cast<OPERATION>(static_cast<int>(lhs) | static_cast<int>(rhs));
  193. }
  194. enum MODE
  195. {
  196. LOCAL,
  197. WORLD
  198. };
  199. 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);
  200. //
  201. // Please note that this cubeview is patented by Autodesk : https://patents.google.com/patent/US7782319B2/en
  202. // It seems to be a defensive patent in the US. I don't think it will bring troubles using it as
  203. // other software are using the same mechanics. But just in case, you are now warned!
  204. //
  205. IMGUI_API void ViewManipulate(float* view, float length, ImVec2 position, ImVec2 size, ImU32 backgroundColor);
  206. // use this version if you did not call Manipulate before and you are just using ViewManipulate
  207. IMGUI_API void ViewManipulate(float* view, const float* projection, OPERATION operation, MODE mode, float* matrix, float length, ImVec2 position, ImVec2 size, ImU32 backgroundColor);
  208. IMGUI_API void SetAlternativeWindow(ImGuiWindow* window);
  209. [[deprecated("Use PushID/PopID instead.")]]
  210. IMGUI_API void SetID(int id);
  211. // ID stack/scopes
  212. // Read the FAQ (docs/FAQ.md or http://dearimgui.org/faq) for more details about how ID are handled in dear imgui.
  213. // - Those questions are answered and impacted by understanding of the ID stack system:
  214. // - "Q: Why is my widget not reacting when I click on it?"
  215. // - "Q: How can I have widgets with an empty label?"
  216. // - "Q: How can I have multiple widgets with the same label?"
  217. // - Short version: ID are hashes of the entire ID stack. If you are creating widgets in a loop you most likely
  218. // want to push a unique identifier (e.g. object pointer, loop index) to uniquely differentiate them.
  219. // - You can also use the "Label##foobar" syntax within widget label to distinguish them from each others.
  220. // - In this header file we use the "label"/"name" terminology to denote a string that will be displayed + used as an ID,
  221. // whereas "str_id" denote a string that is only used as an ID and not normally displayed.
  222. IMGUI_API void PushID(const char* str_id); // push string into the ID stack (will hash string).
  223. IMGUI_API void PushID(const char* str_id_begin, const char* str_id_end); // push string into the ID stack (will hash string).
  224. IMGUI_API void PushID(const void* ptr_id); // push pointer into the ID stack (will hash pointer).
  225. IMGUI_API void PushID(int int_id); // push integer into the ID stack (will hash integer).
  226. IMGUI_API void PopID(); // pop from the ID stack.
  227. IMGUI_API ImGuiID GetID(const char* str_id); // calculate unique ID (hash of whole ID stack + given parameter). e.g. if you want to query into ImGuiStorage yourself
  228. IMGUI_API ImGuiID GetID(const char* str_id_begin, const char* str_id_end);
  229. IMGUI_API ImGuiID GetID(const void* ptr_id);
  230. // return true if the cursor is over the operation's gizmo
  231. IMGUI_API bool IsOver(OPERATION op);
  232. IMGUI_API void SetGizmoSizeClipSpace(float value);
  233. // Allow axis to flip
  234. // When true (default), the guizmo axis flip for better visibility
  235. // When false, they always stay along the positive world/local axis
  236. IMGUI_API void AllowAxisFlip(bool value);
  237. // Configure the limit where axis are hidden
  238. IMGUI_API void SetAxisLimit(float value);
  239. // Set an axis mask to permanently hide a given axis (true -> hidden, false -> shown)
  240. IMGUI_API void SetAxisMask(bool x, bool y, bool z);
  241. // Configure the limit where planes are hiden
  242. IMGUI_API void SetPlaneLimit(float value);
  243. // from a x,y,z point in space and using Manipulation view/projection matrix, check if mouse is in pixel radius distance of that projected point
  244. IMGUI_API bool IsOver(float* position, float pixelRadius);
  245. enum COLOR
  246. {
  247. DIRECTION_X, // directionColor[0]
  248. DIRECTION_Y, // directionColor[1]
  249. DIRECTION_Z, // directionColor[2]
  250. PLANE_X, // planeColor[0]
  251. PLANE_Y, // planeColor[1]
  252. PLANE_Z, // planeColor[2]
  253. SELECTION, // selectionColor
  254. INACTIVE, // inactiveColor
  255. TRANSLATION_LINE, // translationLineColor
  256. SCALE_LINE,
  257. ROTATION_USING_BORDER,
  258. ROTATION_USING_FILL,
  259. HATCHED_AXIS_LINES,
  260. TEXT,
  261. TEXT_SHADOW,
  262. COUNT
  263. };
  264. struct Style
  265. {
  266. IMGUI_API Style();
  267. float TranslationLineThickness; // Thickness of lines for translation gizmo
  268. float TranslationLineArrowSize; // Size of arrow at the end of lines for translation gizmo
  269. float RotationLineThickness; // Thickness of lines for rotation gizmo
  270. float RotationOuterLineThickness; // Thickness of line surrounding the rotation gizmo
  271. float ScaleLineThickness; // Thickness of lines for scale gizmo
  272. float ScaleLineCircleSize; // Size of circle at the end of lines for scale gizmo
  273. float HatchedAxisLineThickness; // Thickness of hatched axis lines
  274. float CenterCircleSize; // Size of circle at the center of the translate/scale gizmo
  275. ImVec4 Colors[COLOR::COUNT];
  276. };
  277. IMGUI_API Style& GetStyle();
  278. }