Implement keyboard modifiers

Fixes #13

Signed-off-by: falkTX <falktx@falktx.com>
This commit is contained in:
falkTX 2021-10-19 16:11:16 +01:00
parent 11cd37ea7a
commit c4ee1b06df
No known key found for this signature in database
GPG key ID: CDBAA37ABC74FBA0
2 changed files with 44 additions and 32 deletions

View file

@ -34,12 +34,8 @@ GLFWAPI int glfwGetKeyScancode(int key) { return 0; }
namespace rack { namespace rack {
namespace window { namespace window {
struct Window::Internal {
int mods;
DISTRHO_NAMESPACE::UI* ui;
// more stuff below
};
void WindowInit(Window* window, DISTRHO_NAMESPACE::UI* ui); void WindowInit(Window* window, DISTRHO_NAMESPACE::UI* ui);
void WindowMods(Window* window, int mods);
} }
} }
@ -170,19 +166,37 @@ protected:
// ------------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------------
bool onMouse(const MouseEvent& ev) override static int glfwMods(const uint mod) noexcept
{ {
int button;
int mods = 0; int mods = 0;
int action = ev.press ? GLFW_PRESS : GLFW_RELEASE;
if (ev.mod & kModifierControl) if (mod & kModifierControl)
mods |= GLFW_MOD_CONTROL; mods |= GLFW_MOD_CONTROL;
if (ev.mod & kModifierShift) if (mod & kModifierShift)
mods |= GLFW_MOD_SHIFT; mods |= GLFW_MOD_SHIFT;
if (ev.mod & kModifierAlt) if (mod & kModifierAlt)
mods |= GLFW_MOD_ALT; mods |= GLFW_MOD_ALT;
/*
if (glfwGetKey(win, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS || glfwGetKey(win, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS)
mods |= GLFW_MOD_SHIFT;
if (glfwGetKey(win, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS || glfwGetKey(win, GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS)
mods |= GLFW_MOD_CONTROL;
if (glfwGetKey(win, GLFW_KEY_LEFT_ALT) == GLFW_PRESS || glfwGetKey(win, GLFW_KEY_RIGHT_ALT) == GLFW_PRESS)
mods |= GLFW_MOD_ALT;
if (glfwGetKey(win, GLFW_KEY_LEFT_SUPER) == GLFW_PRESS || glfwGetKey(win, GLFW_KEY_RIGHT_SUPER) == GLFW_PRESS)
mods |= GLFW_MOD_SUPER;
*/
return mods;
}
bool onMouse(const MouseEvent& ev) override
{
const int action = ev.press ? GLFW_PRESS : GLFW_RELEASE;
const int mods = glfwMods(ev.mod);
int button;
#ifdef DISTRHO_OS_MAC #ifdef DISTRHO_OS_MAC
switch (ev.button) switch (ev.button)
{ {
@ -238,6 +252,8 @@ protected:
#endif #endif
*/ */
rack::window::WindowMods(fContext->window, mods);
const ScopedContext sc(this); const ScopedContext sc(this);
return fContext->event->handleButton(fLastMousePos, button, action, mods); return fContext->event->handleButton(fLastMousePos, button, action, mods);
} }
@ -248,6 +264,7 @@ protected:
const rack::math::Vec mouseDelta = mousePos.minus(fLastMousePos); const rack::math::Vec mouseDelta = mousePos.minus(fLastMousePos);
fLastMousePos = mousePos; fLastMousePos = mousePos;
rack::window::WindowMods(fContext->window, glfwMods(ev.mod));
const ScopedContext sc(this); const ScopedContext sc(this);
return fContext->event->handleHover(mousePos, mouseDelta); return fContext->event->handleHover(mousePos, mouseDelta);
@ -262,6 +279,8 @@ protected:
scrollDelta = scrollDelta.mult(50.0); scrollDelta = scrollDelta.mult(50.0);
#endif #endif
rack::window::WindowMods(fContext->window, glfwMods(ev.mod));
const ScopedContext sc(this); const ScopedContext sc(this);
return fContext->event->handleScroll(fLastMousePos, scrollDelta); return fContext->event->handleScroll(fLastMousePos, scrollDelta);
} }
@ -271,15 +290,16 @@ protected:
if (ev.character <= ' ' || ev.character >= kKeyDelete) if (ev.character <= ' ' || ev.character >= kKeyDelete)
return false; return false;
rack::window::WindowMods(fContext->window, glfwMods(ev.mod));
const ScopedContext sc(this); const ScopedContext sc(this);
return fContext->event->handleText(fLastMousePos, ev.character); return fContext->event->handleText(fLastMousePos, ev.character);
} }
bool onKeyboard(const KeyboardEvent& ev) override bool onKeyboard(const KeyboardEvent& ev) override
{ {
int key; const int action = ev.press ? GLFW_PRESS : GLFW_RELEASE;
int mods = 0; const int mods = glfwMods(ev.mod);
int action = ev.press ? GLFW_PRESS : GLFW_RELEASE;
/* These are unsupported in pugl right now /* These are unsupported in pugl right now
#define GLFW_KEY_KP_0 320 #define GLFW_KEY_KP_0 320
@ -301,6 +321,7 @@ protected:
#define GLFW_KEY_KP_EQUAL 336 #define GLFW_KEY_KP_EQUAL 336
*/ */
int key;
switch (ev.key) switch (ev.key)
{ {
case '\r': key = GLFW_KEY_ENTER; break; case '\r': key = GLFW_KEY_ENTER; break;
@ -346,12 +367,7 @@ protected:
default: key = ev.key; break; default: key = ev.key; break;
} }
if (ev.mod & kModifierControl) rack::window::WindowMods(fContext->window, mods);
mods |= GLFW_MOD_CONTROL;
if (ev.mod & kModifierShift)
mods |= GLFW_MOD_SHIFT;
if (ev.mod & kModifierAlt)
mods |= GLFW_MOD_ALT;
const ScopedContext sc(this); const ScopedContext sc(this);
return fContext->event->handleKey(fLastMousePos, key, ev.keycode, action, mods); return fContext->event->handleKey(fLastMousePos, key, ev.keycode, action, mods);
@ -362,6 +378,8 @@ protected:
if (focus) if (focus)
return; return;
rack::window::WindowMods(fContext->window, 0);
const ScopedContext sc(this); const ScopedContext sc(this);
fContext->event->handleLeave(); fContext->event->handleLeave();
} }

View file

@ -113,6 +113,11 @@ void WindowInit(Window* const window, DISTRHO_NAMESPACE::UI* const ui)
} }
} }
void WindowMods(Window* const window, const int mods)
{
window->internal->mods = mods;
}
Window::~Window() { Window::~Window() {
if (APP->scene) { if (APP->scene) {
widget::Widget::ContextDestroyEvent e; widget::Widget::ContextDestroyEvent e;
@ -235,18 +240,7 @@ bool Window::isCursorLocked() {
int Window::getMods() { int Window::getMods() {
int mods = 0; return internal->mods;
/*
if (glfwGetKey(win, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS || glfwGetKey(win, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS)
mods |= GLFW_MOD_SHIFT;
if (glfwGetKey(win, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS || glfwGetKey(win, GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS)
mods |= GLFW_MOD_CONTROL;
if (glfwGetKey(win, GLFW_KEY_LEFT_ALT) == GLFW_PRESS || glfwGetKey(win, GLFW_KEY_RIGHT_ALT) == GLFW_PRESS)
mods |= GLFW_MOD_ALT;
if (glfwGetKey(win, GLFW_KEY_LEFT_SUPER) == GLFW_PRESS || glfwGetKey(win, GLFW_KEY_RIGHT_SUPER) == GLFW_PRESS)
mods |= GLFW_MOD_SUPER;
*/
return mods;
} }