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