-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add mouse binding support for camera ctl
- Loading branch information
Showing
9 changed files
with
503 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#pragma once | ||
|
||
#include "check_gl.hpp" | ||
#include <memory> | ||
#include <glm/glm.hpp> | ||
#include "KeyBinding.hpp" | ||
|
||
struct InputCtl { | ||
struct Private; | ||
std::unique_ptr<Private> const m_private; | ||
|
||
struct InputPreference { | ||
float zoom_speed = 0.2f; | ||
float orbit_speed = 1.0f; | ||
float drift_speed = 1.0f; | ||
float pan_speed = 1.0f; | ||
MouseBinding orbit_binding = {Modifier::kNone, MouseButton::kLMB}; | ||
MouseBinding drift_binding = {Modifier::kCtrl, MouseButton::kLMB}; | ||
MouseBinding pan_binding = {Modifier::kShift, MouseButton::kLMB}; | ||
MouseBinding zoom_binding = {Modifier::kNone, MouseButton::kWheel}; | ||
MouseBinding hitchcock_binding = {Modifier::kShift, MouseButton::kWheel}; | ||
// Familiar binding for Blender and Zeno users: | ||
/* MouseBinding orbit_binding = {Modifier::kNone, MouseButton::kMMB}; */ | ||
/* MouseBinding drift_binding = {Modifier::kCtrl, MouseButton::kMMB}; */ | ||
/* MouseBinding pan_binding = {Modifier::kShift, MouseButton::kMMB}; */ | ||
/* MouseBinding zoom_binding = {Modifier::kNone, MouseButton::kWheel}; */ | ||
/* MouseBinding hitchcock_binding = {Modifier::kShift, MouseButton::kWheel}; */ | ||
int zoom_axis = 1; | ||
bool clamp_cursor = true; | ||
} m_inputPref; | ||
|
||
private: | ||
void cursor_pos_callback(double xpos, double ypos); | ||
void mouse_button_callback(int button, int action, int mods); | ||
void scroll_callback(double xoffset, double yoffset); | ||
void key_callback(int key, int scancode, int action, int mods); | ||
void framebuffer_size_callback(int width, int height); | ||
|
||
public: | ||
InputCtl(); | ||
~InputCtl(); | ||
InputCtl(InputCtl &&) = delete; | ||
|
||
void register_callbacks(GLFWwindow *window); | ||
|
||
glm::mat4x4 get_view_matrix(); | ||
glm::mat4x4 get_projection_matrix(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#pragma once | ||
|
||
#include "check_gl.hpp" | ||
#include <cmath> | ||
|
||
enum class Modifier { | ||
kNone, | ||
kCtrl, | ||
kShift, | ||
kAlt, | ||
}; | ||
|
||
enum class MouseButton { | ||
kLMB, | ||
kRMB, | ||
kMMB, | ||
kWheel, | ||
kNone, | ||
}; | ||
|
||
struct MouseBinding { | ||
Modifier modifier = Modifier::kNone; | ||
MouseButton mouseBtn = MouseButton::kLMB; | ||
|
||
bool check_is_scrolled(GLFWwindow *window) { | ||
bool modPressed = true; | ||
bool shift = glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS || glfwGetKey(window, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS; | ||
bool ctrl = glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS || glfwGetKey(window, GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS; | ||
bool alt = glfwGetKey(window, GLFW_KEY_LEFT_ALT) == GLFW_PRESS || glfwGetKey(window, GLFW_KEY_RIGHT_ALT) == GLFW_PRESS; | ||
auto mods = (shift ? GLFW_MOD_SHIFT : 0) | (ctrl ? GLFW_MOD_CONTROL : 0) | (alt ? GLFW_MOD_ALT : 0); | ||
switch (modifier) { | ||
case Modifier::kShift: modPressed = mods == GLFW_MOD_SHIFT; break; | ||
case Modifier::kCtrl: modPressed = mods == GLFW_MOD_CONTROL; break; | ||
case Modifier::kAlt: modPressed = mods == GLFW_MOD_ALT; break; | ||
default: modPressed = mods == 0; break; | ||
} | ||
bool wheelScrolled = false; | ||
switch (mouseBtn) { | ||
case MouseButton::kWheel: wheelScrolled = true; break; | ||
default: break; | ||
} | ||
return wheelScrolled && modPressed; | ||
} | ||
|
||
/* bool check_is_pressed(GLFWwindow *window, int button, int action, int mods) { */ | ||
/* bool modPressed = true; */ | ||
/* mods &= GLFW_MOD_SHIFT | GLFW_MOD_CONTROL | GLFW_MOD_ALT; */ | ||
/* switch (modifier) { */ | ||
/* case Modifier::kShift: modPressed = mods == GLFW_MOD_SHIFT; break; */ | ||
/* case Modifier::kCtrl: modPressed = mods == GLFW_MOD_CONTROL; break; */ | ||
/* case Modifier::kAlt: modPressed = mods == GLFW_MOD_ALT; break; */ | ||
/* default: modPressed = mods == 0; break; */ | ||
/* } */ | ||
/* bool btnPressed = false; */ | ||
/* switch (mouseBtn) { */ | ||
/* case MouseButton::kNone: btnPressed = false; break; */ | ||
/* case MouseButton::kLMB: btnPressed = button == GLFW_MOUSE_BUTTON_LEFT; break; */ | ||
/* case MouseButton::kMMB: btnPressed = button == GLFW_MOUSE_BUTTON_MIDDLE; break; */ | ||
/* case MouseButton::kRMB: btnPressed = button == GLFW_MOUSE_BUTTON_RIGHT; break; */ | ||
/* default: break; */ | ||
/* } */ | ||
/* return btnPressed && modPressed && action == GLFW_PRESS; */ | ||
/* } */ | ||
|
||
bool check_is_pressed(GLFWwindow *window) { | ||
bool modPressed = true; | ||
bool shift = glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS || glfwGetKey(window, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS; | ||
bool ctrl = glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS || glfwGetKey(window, GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS; | ||
bool alt = glfwGetKey(window, GLFW_KEY_LEFT_ALT) == GLFW_PRESS || glfwGetKey(window, GLFW_KEY_RIGHT_ALT) == GLFW_PRESS; | ||
auto mods = (shift ? GLFW_MOD_SHIFT : 0) | (ctrl ? GLFW_MOD_CONTROL : 0) | (alt ? GLFW_MOD_ALT : 0); | ||
switch (modifier) { | ||
case Modifier::kShift: modPressed = mods == GLFW_MOD_SHIFT; break; | ||
case Modifier::kCtrl: modPressed = mods == GLFW_MOD_CONTROL; break; | ||
case Modifier::kAlt: modPressed = mods == GLFW_MOD_ALT; break; | ||
default: modPressed = mods == 0; break; | ||
} | ||
int button = GLFW_MOUSE_BUTTON_LEFT; | ||
switch (mouseBtn) { | ||
case MouseButton::kLMB: button = GLFW_MOUSE_BUTTON_LEFT; break; | ||
case MouseButton::kMMB: button = GLFW_MOUSE_BUTTON_MIDDLE; break; | ||
case MouseButton::kRMB: button = GLFW_MOUSE_BUTTON_RIGHT; break; | ||
case MouseButton::kNone: return modPressed | ||
&& glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) != GLFW_PRESS | ||
&& glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_MIDDLE) != GLFW_PRESS | ||
&& glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) != GLFW_PRESS | ||
; | ||
default: return false; | ||
} | ||
return modPressed && glfwGetMouseButton(window, button) == GLFW_PRESS; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.