Skip to content

Commit a534b55

Browse files
authored
fix gizmo context bug, move to ImGuizmo fork, nonuniform scaling option (#376)
* use gizmo context stack * add test for gizmo nested show bug * add nonuniform scaling option * cleanup unused UI elements
1 parent 051879c commit a534b55

File tree

7 files changed

+42
-26
lines changed

7 files changed

+42
-26
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@
1717
url = https://github.com/epezent/implot
1818
[submodule "deps/imgui/ImGuizmo"]
1919
path = deps/imgui/ImGuizmo
20-
url = https://github.com/CedricGuillemet/ImGuizmo.git
20+
url = https://github.com/nmwsharp/ImGuizmo.git

deps/imgui/ImGuizmo

include/polyscope/transformation_gizmo.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,8 @@ class TransformationGizmo : public Widget {
5959
bool getAllowScaling();
6060
void setAllowScaling(bool newVal);
6161

62-
// sadly this is not really supported by ImGuizmo
63-
// bool getUniformScaling();
64-
// void setUniformScaling(bool newVal);
62+
bool getAllowNonUniformScaling();
63+
void setAllowNonUniformScaling(bool newVal); // must also set allowScaling to true
6564

6665
bool getInteractInLocalSpace();
6766
void setInteractInLocalSpace(bool newVal);
@@ -99,7 +98,7 @@ class TransformationGizmo : public Widget {
9998
PersistentValue<bool> allowTranslation;
10099
PersistentValue<bool> allowRotation;
101100
PersistentValue<bool> allowScaling;
102-
// PersistentValue<bool> uniformScaling; // not really supported by the ImGuizmo
101+
PersistentValue<bool> allowNonUniformScaling;
103102
PersistentValue<bool> interactInLocalSpace;
104103
PersistentValue<bool> showUIWindow;
105104
PersistentValue<float> gizmoSize;

src/polyscope.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <iostream>
88
#include <thread>
99

10+
#include "ImGuizmo.h"
1011
#include "imgui.h"
1112
#include "implot.h"
1213

@@ -269,6 +270,7 @@ void pushContext(std::function<void()> callbackFunction, bool drawDefaultUI) {
269270
#endif
270271
ImGui::SetCurrentContext(newContext);
271272
ImPlot::SetCurrentContext(newPlotContext);
273+
ImGuizmo::PushContext();
272274
#ifdef IMGUI_HAS_DOCK
273275
// Propagate GLFW window handle to new context
274276
ImGui::GetMainViewport()->PlatformHandle = oldPlatformIO.Viewports[0]->PlatformHandle;
@@ -318,6 +320,7 @@ void pushContext(std::function<void()> callbackFunction, bool drawDefaultUI) {
318320
if (!contextStack.empty()) {
319321
ImGui::SetCurrentContext(contextStack.back().context);
320322
ImPlot::SetCurrentContext(contextStack.back().plotContext);
323+
ImGuizmo::PopContext();
321324
}
322325
}
323326

src/screenshot.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
#include "stb_image_write.h"
88

9+
#include "imgui.h"
10+
#include "implot.h"
11+
#include "ImGuizmo.h"
12+
913
#include <algorithm>
1014
#include <string>
1115

@@ -69,6 +73,7 @@ std::vector<unsigned char> getRenderInBuffer(const ScreenshotOptions& options =
6973
#endif
7074
ImGui::SetCurrentContext(newContext);
7175
ImPlot::SetCurrentContext(newPlotContext);
76+
ImGuizmo::PushContext();
7277

7378
#ifdef IMGUI_HAS_DOCK
7479
// Propagate GLFW window handle to new context
@@ -101,6 +106,7 @@ std::vector<unsigned char> getRenderInBuffer(const ScreenshotOptions& options =
101106

102107
ImGui::SetCurrentContext(oldContext);
103108
ImPlot::SetCurrentContext(oldPlotContext);
109+
ImGuizmo::PopContext();
104110
}
105111

106112

src/transformation_gizmo.cpp

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ TransformationGizmo::TransformationGizmo(std::string name_, glm::mat4* externalT
2020
allowTranslation(name + "#allowTranslation", true),
2121
allowRotation(name + "#allowRotation", true),
2222
allowScaling(name + "#allowScaling", false),
23-
// uniformScaling(name + "#uniformScaling", true),
23+
allowNonUniformScaling(name + "#allowNonUniformScaling", false),
2424
interactInLocalSpace(name + "#interactInLocalSpace", false),
2525
showUIWindow(name + "#showUIWindow", false),
2626
gizmoSize(name + "#gizmoSize", 1.0)
@@ -62,12 +62,8 @@ void TransformationGizmo::draw() {
6262
if (allowRotation.get()) gizmoOpModeInt |= ImGuizmo::ROTATE;
6363
if (allowScaling.get()) {
6464
gizmoOpModeInt |= ImGuizmo::SCALEU;
65-
// if(uniformScaling.get()) {
66-
// gizmoOpModeInt |= ImGuizmo::SCALEU;
67-
// } else {
68-
// gizmoOpModeInt |= ImGuizmo::SCALE;
69-
// }
7065
}
66+
ImGuizmo::SetAllowNonUniformScaling(allowNonUniformScaling.get());
7167
ImGuizmo::OPERATION gizmoOpMode = static_cast<ImGuizmo::OPERATION>(gizmoOpModeInt);
7268

7369
ImGuizmo::MODE gizmoCoordSpace = interactInLocalSpace.get() ? ImGuizmo::LOCAL : ImGuizmo::WORLD;
@@ -94,7 +90,7 @@ void TransformationGizmo::buildMenuItems() {
9490
ImGui::MenuItem("Allow Translation", NULL, &allowTranslation.get());
9591
ImGui::MenuItem("Allow Rotation", NULL, &allowRotation.get());
9692
ImGui::MenuItem("Allow Scaling", NULL, &allowScaling.get());
97-
// ImGui::MenuItem("Uniform Scaling", NULL, &uniformScaling.get());
93+
ImGui::MenuItem("Allow Non-Uniform Scaling", NULL, &allowNonUniformScaling.get());
9894
}
9995

10096
bool TransformationGizmo::interact() {
@@ -134,27 +130,17 @@ void TransformationGizmo::buildInlineTransformUI() {
134130
ImGui::SameLine();
135131
ImGui::Checkbox("Scaling", &allowScaling.get());
136132

137-
// if (allowScaling.get()) {
138-
// ImGui::Checkbox("Uniform Scaling", &uniformScaling.get());
139-
// }
133+
if (allowScaling.get()) {
134+
ImGui::Checkbox("Allow Non-Uniform Scaling", &allowNonUniformScaling.get());
135+
}
140136

141-
static ImGuizmo::OPERATION mCurrentGizmoOperation(ImGuizmo::ROTATE);
142-
static ImGuizmo::MODE mCurrentGizmoMode(ImGuizmo::WORLD);
143-
if (ImGui::RadioButton("Translate", mCurrentGizmoOperation == ImGuizmo::TRANSLATE))
144-
mCurrentGizmoOperation = ImGuizmo::TRANSLATE;
145-
ImGui::SameLine();
146-
if (ImGui::RadioButton("Rotate", mCurrentGizmoOperation == ImGuizmo::ROTATE))
147-
mCurrentGizmoOperation = ImGuizmo::ROTATE;
148-
ImGui::SameLine();
149-
if (ImGui::RadioButton("Scale", mCurrentGizmoOperation == ImGuizmo::SCALE)) mCurrentGizmoOperation = ImGuizmo::SCALE;
150137
float matrixTranslation[3], matrixRotation[3], matrixScale[3];
151138
ImGuizmo::DecomposeMatrixToComponents(glm::value_ptr(Tref), matrixTranslation, matrixRotation, matrixScale);
152139
ImGui::InputFloat3("Tr", matrixTranslation);
153140
ImGui::InputFloat3("Rt", matrixRotation);
154141
ImGui::InputFloat3("Sc", matrixScale);
155142
ImGuizmo::RecomposeMatrixFromComponents(matrixTranslation, matrixRotation, matrixScale, glm::value_ptr(Tref));
156143

157-
// TODO these don't seem to be doing anything?
158144
if (ImGui::RadioButton("Local", interactInLocalSpace.get())) interactInLocalSpace = true;
159145
ImGui::SameLine();
160146
if (ImGui::RadioButton("World", !interactInLocalSpace.get())) interactInLocalSpace = false;
@@ -188,6 +174,9 @@ void TransformationGizmo::setAllowRotation(bool newVal) { allowRotation = newVal
188174
bool TransformationGizmo::getAllowScaling() { return allowScaling.get(); }
189175
void TransformationGizmo::setAllowScaling(bool newVal) { allowScaling = newVal; }
190176

177+
bool TransformationGizmo::getAllowNonUniformScaling() { return allowNonUniformScaling.get(); }
178+
void TransformationGizmo::setAllowNonUniformScaling(bool newVal) { allowNonUniformScaling = newVal; }
179+
191180
bool TransformationGizmo::getInteractInLocalSpace() { return interactInLocalSpace.get(); }
192181
void TransformationGizmo::setInteractInLocalSpace(bool newVal) { interactInLocalSpace = newVal; }
193182

test/src/misc_test.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ TEST_F(PolyscopeTest, TransformationGizmoTest) {
8181
gizmo.setAllowTranslation(true);
8282
gizmo.setAllowRotation(true);
8383
gizmo.setAllowScaling(true);
84+
gizmo.setAllowNonUniformScaling(true);
8485
gizmo.setInteractInLocalSpace(false);
8586
polyscope::show(3);
8687

@@ -102,6 +103,8 @@ TEST_F(PolyscopeTest, TransformationGizmoStandaloneTest) {
102103

103104
// create by name
104105
polyscope::TransformationGizmo* gizmo2 = polyscope::addTransformationGizmo("my_gizmo");
106+
gizmo2->setEnabled(true);
107+
gizmo2->setAllowScaling(true);
105108
polyscope::show(3);
106109
polyscope::removeTransformationGizmo("my_gizmo");
107110

@@ -125,6 +128,22 @@ TEST_F(PolyscopeTest, TransformationGizmoStandaloneTest) {
125128
polyscope::removeAllTransformationGizmos();
126129
}
127130

131+
TEST_F(PolyscopeTest, TransformationGizmoNestedShowTest) {
132+
133+
polyscope::TransformationGizmo* gizmo1 = polyscope::addTransformationGizmo();
134+
gizmo1->setEnabled(true);
135+
polyscope::show(3);
136+
137+
auto showCallback = [&]() {
138+
polyscope::show(3);
139+
};
140+
polyscope::state::userCallback = showCallback;
141+
polyscope::show(3);
142+
143+
polyscope::state::userCallback = nullptr;
144+
polyscope::removeAllTransformationGizmos();
145+
}
146+
128147
// ============================================================
129148
// =============== Slice Plane Tests
130149
// ============================================================

0 commit comments

Comments
 (0)