Skip to content

Commit 7b29484

Browse files
authored
Implement alternative (Blender style) camera controls (#361)
1 parent 9c383cb commit 7b29484

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

GUI/OpenGL/MiniGL.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ std::vector<MiniGL::MouseMoveFct> MiniGL::m_mouseMoveFct;
7171
std::vector<MiniGL::MouseWheelFct> MiniGL::m_mouseWheelFct;
7272
GLFWwindow* MiniGL::m_glfw_window = nullptr;
7373
bool MiniGL::m_vsync = false;
74+
bool MiniGL::m_alt_camera = false;
7475
double MiniGL::m_lastTime;
7576

7677
void MiniGL::bindTexture()
@@ -470,7 +471,7 @@ void MiniGL::setClientSceneFunc (SceneFct func)
470471
scenefunc = func;
471472
}
472473

473-
void MiniGL::init(const int width, const int height, const char *name, const bool vsync, const bool maximized)
474+
void MiniGL::init(const int width, const int height, const char *name, const bool vsync, const bool maximized, const bool alt_camera)
474475
{
475476
fovy = 60;
476477
znear = 0.5f;
@@ -479,6 +480,7 @@ void MiniGL::init(const int width, const int height, const char *name, const boo
479480
m_width = width;
480481
m_height = height;
481482
m_vsync = vsync;
483+
m_alt_camera = alt_camera;
482484

483485
scenefunc = nullptr;
484486

@@ -833,6 +835,11 @@ void MiniGL::mouseWheel(GLFWwindow* window, double xoffset, double yoffset)
833835
return;
834836
}
835837

838+
if (m_alt_camera && yoffset != 0) {
839+
// translate scene in z direction
840+
move (0, 0, static_cast<Real>(yoffset) / static_cast<Real>(10.0));
841+
}
842+
836843
if (yoffset > 0)
837844
movespeed *= 2.0;
838845
else
@@ -851,7 +858,7 @@ void MiniGL::mouseMove (GLFWwindow* window, double x, double y)
851858
double d_x = mouse_pos_x_old - x;
852859
double d_y = y - mouse_pos_y_old;
853860

854-
if (mouse_button == GLFW_MOUSE_BUTTON_1)
861+
if ((!m_alt_camera && mouse_button == GLFW_MOUSE_BUTTON_1) || (m_alt_camera && mouse_button == GLFW_MOUSE_BUTTON_MIDDLE))
855862
{
856863
// translate scene in z direction
857864
if (modifier_key == GLFW_MOD_CONTROL)
@@ -864,7 +871,7 @@ void MiniGL::mouseMove (GLFWwindow* window, double x, double y)
864871
move (-static_cast<Real>(d_x) / static_cast<Real>(20.0), -static_cast<Real>(d_y) / static_cast<Real>(20.0), 0);
865872
}
866873
// rotate scene around x, y axis
867-
else if (modifier_key == GLFW_MOD_ALT)
874+
else if (modifier_key == GLFW_MOD_ALT || m_alt_camera)
868875
{
869876
rotateX(static_cast<Real>(d_y)/ static_cast<Real>(100.0));
870877
rotateY(-static_cast<Real>(d_x)/ static_cast<Real>(100.0));

GUI/OpenGL/MiniGL.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ namespace SPH
121121
static GLUquadricObj* m_sphereQuadric;
122122
static GLFWwindow* m_glfw_window;
123123
static bool m_vsync;
124+
static bool m_alt_camera;
124125
static double m_lastTime;
125126

126127
static void reshape (GLFWwindow* glfw_window, int w, int h);
@@ -158,7 +159,7 @@ namespace SPH
158159
static void setClientDestroyFunc(DestroyFct func);
159160
static void addKeyFunc(int key, int modifiers, std::function<void()> const& func);
160161
static std::vector<KeyFunction> &getKeyFunc() { return keyfunc; }
161-
static void init(const int width, const int height, const char* name, const bool vsync, const bool maximized = false);
162+
static void init(const int width, const int height, const char* name, const bool vsync, const bool maximized = false, const bool alt_camera = false);
162163
static void destroy ();
163164
static void viewport ();
164165
static void initLights ();
@@ -180,6 +181,8 @@ namespace SPH
180181
static void rgbToHsv(float r, float g, float b, float *hsv);
181182
static int getModifierKey() { return modifier_key; }
182183
static bool getVSync() { return m_vsync; }
184+
static void setAltCameraMode(const bool enabled) { m_alt_camera = enabled; }
185+
static bool getAltCameraMode() { return m_alt_camera; }
183186

184187
static void addReshapeFunc(ReshapeFct func) { m_reshapeFct.push_back(func); }
185188
static std::vector<ReshapeFct> &getReshapeFunc() { return m_reshapeFct; }

Simulator/GUI/imgui/Simulator_GUI_imgui.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Simulator_GUI_imgui::Simulator_GUI_imgui(SimulatorBase *base) :
2828
m_currentFluidModel = 0;
2929
m_currentScaleIndex = 0;
3030
m_vsync = false;
31+
m_alt_camera = false;
3132
m_iniFound = false;
3233
m_showLogWindow = true;
3334
}
@@ -62,7 +63,7 @@ void Simulator_GUI_imgui::init(const char *name)
6263
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
6364
ImGui::LoadIniSettingsFromDisk(io.IniFilename);
6465

65-
MiniGL::init(1280, 960, name, m_userSettings.vsync, m_userSettings.maximized);
66+
MiniGL::init(1280, 960, name, m_userSettings.vsync, m_userSettings.maximized, m_userSettings.alt_camera);
6667
MiniGL::initLights();
6768

6869
const Utilities::SceneLoader::Scene& scene = SceneConfiguration::getCurrent()->getScene();
@@ -145,6 +146,7 @@ void Simulator_GUI_imgui::readIni(ImGuiContext* ctx, ImGuiSettingsHandler* handl
145146
else if (sscanf(line, "scale=%d", &i) == 1) { settings->scaleIndex = i; }
146147
else if (sscanf(line, "maximized=%d", &i) == 1) { settings->maximized = (i != 0); }
147148
else if (sscanf(line, "vsync=%d", &i) == 1) { settings->vsync = (i != 0); }
149+
else if (sscanf(line, "alt_camera=%d", &i) == 1) { settings->alt_camera = (i != 0); }
148150
else if (sscanf(line, "show_log_window=%d", &i) == 1) { settings->show_log_window = (i != 0); }
149151
else if (sscanf(line, "log_filter=%d", &i) == 1) { settings->log_filter = i; }
150152
}
@@ -166,6 +168,7 @@ void Simulator_GUI_imgui::writeIni(ImGuiContext* ctx, ImGuiSettingsHandler* hand
166168
out_buf->appendf("maximized=%d\n", MiniGL::getWindowMaximized());
167169

168170
out_buf->appendf("vsync=%d\n", gui->m_vsync);
171+
out_buf->appendf("alt_camera=%d\n", gui->m_alt_camera);
169172
out_buf->appendf("show_log_window=%d\n", gui->m_showLogWindow);
170173
out_buf->appendf("log_filter=%d\n", gui->m_logWindow->getSelectedFilter());
171174
}
@@ -176,6 +179,7 @@ void Simulator_GUI_imgui::applySettings(ImGuiContext* ctx, ImGuiSettingsHandler*
176179
UserSettings* settings = (UserSettings*) &gui->m_userSettings;
177180
gui->m_currentScaleIndex = settings->scaleIndex;
178181
gui->m_vsync = settings->vsync;
182+
gui->m_alt_camera = settings->alt_camera;
179183
gui->m_showLogWindow = settings->show_log_window;
180184
gui->m_iniFound = true;
181185
gui->m_logWindow->setSelectedFilter(settings->log_filter);
@@ -294,6 +298,11 @@ void Simulator_GUI_imgui::createMenuBar()
294298
m_vsync = !m_vsync;
295299
openpopup = true;
296300
}
301+
if (ImGui::MenuItem("Alternative camera controls", "", m_alt_camera))
302+
{
303+
m_alt_camera = !m_alt_camera;
304+
MiniGL::setAltCameraMode(m_alt_camera);
305+
}
297306
if (ImGui::MenuItem("Scale - 100%", "", m_currentScaleIndex == 0))
298307
{
299308
m_currentScaleIndex = 0;

Simulator/GUI/imgui/Simulator_GUI_imgui.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ namespace SPH
1818
int win_x, win_y;
1919
int win_width, win_height;
2020
bool vsync;
21+
bool alt_camera;
2122
bool show_log_window;
2223
bool maximized;
2324
int log_filter;
2425

25-
UserSettings() { win_x = 0; win_y = 0; win_width = 1280; win_height = 960; scaleIndex = 0; vsync = false; maximized = false; log_filter = 1; }
26+
UserSettings() { win_x = 0; win_y = 0; win_width = 1280; win_height = 960; scaleIndex = 0; vsync = false; alt_camera = false; maximized = false; log_filter = 1; }
2627
};
2728

2829
class LogWindow;
@@ -43,6 +44,7 @@ namespace SPH
4344
std::vector<float> m_scales;
4445
unsigned int m_currentScaleIndex;
4546
bool m_vsync;
47+
bool m_alt_camera;
4648
bool m_showLogWindow;
4749
ImGuiContext* m_context;
4850
UserSettings m_userSettings;

0 commit comments

Comments
 (0)