Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions RetroFE/Source/Control/MouseMovementHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "MouseMovementHandler.h"
#include <SDL2/SDL.h>
#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_;
}

20 changes: 20 additions & 0 deletions RetroFE/Source/Control/MouseMovementHandler.h
Original file line number Diff line number Diff line change
@@ -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_;
};

27 changes: 22 additions & 5 deletions RetroFE/Source/Control/UserInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "JoyHatHandler.h"
#include "KeyboardHandler.h"
#include "MouseButtonHandler.h"
#include "MouseMovementHandler.h"

UserInput::UserInput(Configuration &c)
: config_(c)
Expand Down Expand Up @@ -164,19 +165,35 @@ 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;
else if (mousedesc == "right") button = SDL_BUTTON_RIGHT;
else if (mousedesc == "x1") button = SDL_BUTTON_X1;
else if (mousedesc == "x2") button = SDL_BUTTON_X2;

keyHandlers_.push_back(std::pair<InputHandler*, KeyCode_E>(new MouseButtonHandler(button), key));
LOG_INFO("Input", "Binding mouse button " + ss.str());
found = true;
if (button) {
keyHandlers_.push_back(std::pair<InputHandler*, KeyCode_E>(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<InputHandler*, KeyCode_E>(new MouseMovementHandler(button), key));
LOG_INFO("Input", "Binding mouse movement " + ss.str());
found = true;
}
}
}
else if (tokenLowered.find("joy") == 0) {
Expand Down
3 changes: 2 additions & 1 deletion RetroFE/Source/RetroFE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down