From a7e0799d2805068a45d7fa6bd82cbbf2c2eeab9c Mon Sep 17 00:00:00 2001 From: monkofthefunk Date: Wed, 27 Nov 2024 20:25:55 -0800 Subject: [PATCH] initial mouse scroll --- .../Source/Control/MouseMovementHandler.cpp | 54 +++++++++++++++++++ RetroFE/Source/Control/MouseMovementHandler.h | 20 +++++++ RetroFE/Source/Control/UserInput.cpp | 27 ++++++++-- RetroFE/Source/RetroFE.cpp | 3 +- 4 files changed, 98 insertions(+), 6 deletions(-) create mode 100644 RetroFE/Source/Control/MouseMovementHandler.cpp create mode 100644 RetroFE/Source/Control/MouseMovementHandler.h diff --git a/RetroFE/Source/Control/MouseMovementHandler.cpp b/RetroFE/Source/Control/MouseMovementHandler.cpp new file mode 100644 index 000000000..18c6ed6ca --- /dev/null +++ b/RetroFE/Source/Control/MouseMovementHandler.cpp @@ -0,0 +1,54 @@ +#include "MouseMovementHandler.h" +#include +#include "../SDL.h" + +MouseMovementHandler::MouseMovementHandler(Uint8 button) +: button_(button) +, pressed_(false) +, lastpos_(0) +{ +} + +void MouseMovementHandler::reset() +{ + pressed_= false; +} + +bool MouseMovementHandler::update(SDL_Event &e) +{ + // if postion different then pressed + if (e.type == SDL_MOUSEMOTION && !pressed_) { + // to do these might need to be settings to tweek + int buffer = 50; + int extent = 20 + buffer; + int w = SDL::getWindowWidth(0); + int h = SDL::getWindowHeight(0); + + // todo replace numbers with enum + if (button_ == 61 && e.motion.xrel < -buffer && e.motion.xrel > -extent) { + pressed_ = true; + } else if (button_ == 62 && e.motion.xrel > buffer && e.motion.xrel < extent) { + pressed_ = true; + } else if (button_ == 71 && e.motion.yrel < -buffer && e.motion.yrel > -extent) { + pressed_ = true; + } else if (button_ == 72 && e.motion.yrel > buffer && e.motion.yrel < extent) { + pressed_ = true; + } + + if (pressed_ || e.motion.x == 0 || e.motion.y == 0 || e.motion.x == w || e.motion.y == h ) { + SDL_WarpMouseInWindow(SDL::getWindow(0),w / 2, h / 2); + } + } + else { + pressed_ = false; + } + + + return pressed_; +} + +bool MouseMovementHandler::pressed() +{ + return pressed_; +} + diff --git a/RetroFE/Source/Control/MouseMovementHandler.h b/RetroFE/Source/Control/MouseMovementHandler.h new file mode 100644 index 000000000..096bcae94 --- /dev/null +++ b/RetroFE/Source/Control/MouseMovementHandler.h @@ -0,0 +1,20 @@ +#pragma once + +#include "InputHandler.h" +#include "SDL.h" + +class MouseMovementHandler : public InputHandler +{ +public: + MouseMovementHandler(Uint8 button); + bool update(SDL_Event &e); + bool pressed(); + void reset(); + void updateKeystate() {}; + +private: + Uint8 button_; + bool pressed_; + Sint32 lastpos_; +}; + diff --git a/RetroFE/Source/Control/UserInput.cpp b/RetroFE/Source/Control/UserInput.cpp index 8eca7a42b..327ac82ab 100644 --- a/RetroFE/Source/Control/UserInput.cpp +++ b/RetroFE/Source/Control/UserInput.cpp @@ -24,6 +24,7 @@ #include "JoyHatHandler.h" #include "KeyboardHandler.h" #include "MouseButtonHandler.h" +#include "MouseMovementHandler.h" UserInput::UserInput(Configuration &c) : config_(c) @@ -164,9 +165,9 @@ bool UserInput::HandleInputMapping(const std::string& token, KeyCode_E key, cons if (tokenLowered.find("mouse") == 0) { std::string mousedesc = Utils::replace(Utils::toLower(token), "mouse", ""); + int button = 0; + std::stringstream ss; if (mousedesc.find("button") == 0) { - int button = 0; - std::stringstream ss; mousedesc = Utils::replace(mousedesc, "button", ""); if (mousedesc == "left") button = SDL_BUTTON_LEFT; else if (mousedesc == "middle") button = SDL_BUTTON_MIDDLE; @@ -174,9 +175,25 @@ bool UserInput::HandleInputMapping(const std::string& token, KeyCode_E key, cons else if (mousedesc == "x1") button = SDL_BUTTON_X1; else if (mousedesc == "x2") button = SDL_BUTTON_X2; - keyHandlers_.push_back(std::pair(new MouseButtonHandler(button), key)); - LOG_INFO("Input", "Binding mouse button " + ss.str()); - found = true; + if (button) { + keyHandlers_.push_back(std::pair(new MouseButtonHandler(button), key)); + LOG_INFO("Input", "Binding mouse button " + ss.str()); + found = true; + } + } + else { + //todo replace numbers wit enum + // mousexleft + if (mousedesc == "xleft") button = 61; + else if (mousedesc == "xright") button = 62; + else if (mousedesc == "yup") button = 71; + else if (mousedesc == "ydown") button = 72; + + if (button) { + keyHandlers_.push_back(std::pair(new MouseMovementHandler(button), key)); + LOG_INFO("Input", "Binding mouse movement " + ss.str()); + found = true; + } } } else if (tokenLowered.find("joy") == 0) { diff --git a/RetroFE/Source/RetroFE.cpp b/RetroFE/Source/RetroFE.cpp index b55da588c..6544498dc 100644 --- a/RetroFE/Source/RetroFE.cpp +++ b/RetroFE/Source/RetroFE.cpp @@ -329,6 +329,7 @@ bool RetroFE::run() SDL_RestoreWindow(SDL::getWindow(0)); SDL_RaiseWindow(SDL::getWindow(0)); SDL_SetWindowGrab(SDL::getWindow(0), SDL_TRUE); + SDL_WarpMouseInWindow(SDL::getWindow(0), SDL::getWindowWidth(0) / 2, SDL::getWindowHeight(0) / 2); #ifdef WIN32 bool highPriority = false; config_.getProperty(OPTION_HIGHPRIORITY, highPriority); @@ -2193,7 +2194,7 @@ RetroFE::RETROFE_STATE RetroFE::processUserInput(Page *page) { // some how !SDL_KEYUP prevents double action input_.update(e); - if (e.type == SDL_POLLSENTINEL || (screensaver && ssExitInputs[e.type])) + if (e.type == SDL_POLLSENTINEL || e.type == SDL_MOUSEMOTION || (screensaver && ssExitInputs[e.type])) { break; }