From 3120cd96955b0d318674a418bc5eef5cb5c47a2c Mon Sep 17 00:00:00 2001 From: luckyxxl Date: Thu, 15 Feb 2018 07:50:46 +0100 Subject: [PATCH] Add windowIsKeyPressed() function --- include/window.hpp | 1 + src/app/Knob.cpp | 2 -- src/app/ModuleWidget.cpp | 4 ++-- src/ui/ScrollWidget.cpp | 8 ++++---- src/ui/TextField.cpp | 2 -- src/window.cpp | 4 ++++ 6 files changed, 11 insertions(+), 10 deletions(-) diff --git a/include/window.hpp b/include/window.hpp index 24432579..886a3b2c 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -38,6 +38,7 @@ void windowCursorLock(); void windowCursorUnlock(); bool windowIsModPressed(); bool windowIsShiftPressed(); +bool windowIsKeyPressed(int key); Vec windowGetWindowSize(); void windowSetWindowSize(Vec size); Vec windowGetWindowPos(); diff --git a/src/app/Knob.cpp b/src/app/Knob.cpp index dab3154b..cb549afc 100644 --- a/src/app/Knob.cpp +++ b/src/app/Knob.cpp @@ -1,8 +1,6 @@ #include "app.hpp" #include "window.hpp" #include "engine.hpp" -// For GLFW_KEY_LEFT_CONTROL, etc. -#include namespace rack { diff --git a/src/app/ModuleWidget.cpp b/src/app/ModuleWidget.cpp index da9a080d..43f27c22 100644 --- a/src/app/ModuleWidget.cpp +++ b/src/app/ModuleWidget.cpp @@ -219,7 +219,7 @@ void ModuleWidget::onMouseMove(EventMouseMove &e) { // Don't delete the ModuleWidget if a TextField is focused if (!gFocusedWidget) { // Instead of checking key-down events, delete the module even if key-repeat hasn't fired yet and the cursor is hovering over the widget. - if (glfwGetKey(gWindow, GLFW_KEY_DELETE) == GLFW_PRESS || glfwGetKey(gWindow, GLFW_KEY_BACKSPACE) == GLFW_PRESS) { + if (windowIsKeyPressed(GLFW_KEY_DELETE) || windowIsKeyPressed(GLFW_KEY_BACKSPACE)) { if (!windowIsModPressed() && !windowIsShiftPressed()) { gRackWidget->deleteModule(this); this->finalizeEvents(); @@ -350,4 +350,4 @@ Menu *ModuleWidget::createContextMenu() { } -} // namespace rack \ No newline at end of file +} // namespace rack diff --git a/src/ui/ScrollWidget.cpp b/src/ui/ScrollWidget.cpp index 7c59caa7..efe29484 100644 --- a/src/ui/ScrollWidget.cpp +++ b/src/ui/ScrollWidget.cpp @@ -68,16 +68,16 @@ void ScrollWidget::onMouseMove(EventMouseMove &e) { else if (windowIsModPressed()) arrowSpeed /= 4.0; - if (glfwGetKey(gWindow, GLFW_KEY_LEFT) == GLFW_PRESS) { + if (windowIsKeyPressed(GLFW_KEY_LEFT)) { offset.x -= arrowSpeed; } - if (glfwGetKey(gWindow, GLFW_KEY_RIGHT) == GLFW_PRESS) { + if (windowIsKeyPressed(GLFW_KEY_RIGHT)) { offset.x += arrowSpeed; } - if (glfwGetKey(gWindow, GLFW_KEY_UP) == GLFW_PRESS) { + if (windowIsKeyPressed(GLFW_KEY_UP)) { offset.y -= arrowSpeed; } - if (glfwGetKey(gWindow, GLFW_KEY_DOWN) == GLFW_PRESS) { + if (windowIsKeyPressed(GLFW_KEY_DOWN)) { offset.y += arrowSpeed; } } diff --git a/src/ui/TextField.cpp b/src/ui/TextField.cpp index f093ac1e..97f20f83 100644 --- a/src/ui/TextField.cpp +++ b/src/ui/TextField.cpp @@ -1,8 +1,6 @@ #include "ui.hpp" // for gVg #include "window.hpp" -// for key codes -#include namespace rack { diff --git a/src/window.cpp b/src/window.cpp index f65c00a0..ccf71bab 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -516,6 +516,10 @@ bool windowIsShiftPressed() { return glfwGetKey(gWindow, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS || glfwGetKey(gWindow, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS; } +bool windowIsKeyPressed(int key) { + return glfwGetKey(gWindow, key) == GLFW_PRESS; +} + Vec windowGetWindowSize() { int width, height; glfwGetWindowSize(gWindow, &width, &height);