diff --git a/binding-mri/input-binding.cpp b/binding-mri/input-binding.cpp index 62a383a..41f3ce1 100644 --- a/binding-mri/input-binding.cpp +++ b/binding-mri/input-binding.cpp @@ -115,6 +115,13 @@ RB_METHOD(inputMouseY) return rb_fix_new(shState->input().mouseY()); } +RB_METHOD(inputScrollV) +{ + RB_UNUSED_PARAM; + + return rb_fix_new(shState->input().scrollV()); +} + struct { @@ -168,6 +175,7 @@ inputBindingInit() _rb_define_module_function(module, "mouse_x", inputMouseX); _rb_define_module_function(module, "mouse_y", inputMouseY); + _rb_define_module_function(module, "scroll_v", inputScrollV); if (rgssVer >= 3) { diff --git a/src/eventthread.cpp b/src/eventthread.cpp index bbb8b51..448fdc4 100644 --- a/src/eventthread.cpp +++ b/src/eventthread.cpp @@ -73,6 +73,7 @@ uint8_t EventThread::keyStates[]; EventThread::JoyState EventThread::joyState; EventThread::MouseState EventThread::mouseState; EventThread::TouchState EventThread::touchState; +SDL_atomic_t EventThread::verticalScrollDistance; /* User event codes */ enum @@ -380,6 +381,11 @@ void EventThread::process(RGSSThreadData &rtData) updateCursorState(cursorInWindow, gameScreen); break; + case SDL_MOUSEWHEEL : + /* Only consider vertical scrolling for now */ + SDL_AtomicAdd(&verticalScrollDistance, event.wheel.y); + break; + case SDL_FINGERDOWN : i = event.tfinger.fingerId; touchState.fingers[i].down = true; diff --git a/src/eventthread.h b/src/eventthread.h index 0c57f2d..432ad7d 100644 --- a/src/eventthread.h +++ b/src/eventthread.h @@ -31,6 +31,7 @@ #include #include #include +#include #include @@ -76,6 +77,7 @@ public: static JoyState joyState; static MouseState mouseState; static TouchState touchState; + static SDL_atomic_t verticalScrollDistance; static bool allocUserEvents(); diff --git a/src/input.cpp b/src/input.cpp index 5bcc850..f025b5c 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -28,6 +28,7 @@ #include #include +#include #include #include @@ -287,6 +288,9 @@ struct InputPrivate Input::ButtonCode repeating; unsigned int repeatCount; + /* Cumulative vertical scroll distance since last update call */ + int vScrollDistance; + struct { int active; @@ -322,6 +326,8 @@ struct InputPrivate dir4Data.previous = Input::None; dir8Data.active = 0; + + vScrollDistance = 0; } inline ButtonState &getStateCheck(int code) @@ -640,6 +646,9 @@ void Input::update() } p->repeating = None; + + /* Fetch new cumulative scroll distance and reset counter */ + p->vScrollDistance = SDL_AtomicSet(&EventThread::verticalScrollDistance, 0); } bool Input::isPressed(int button) @@ -681,6 +690,11 @@ int Input::mouseY() return (EventThread::mouseState.y - rtData.screenOffset.y) * rtData.sizeResoRatio.y; } +int Input::scrollV() +{ + return p->vScrollDistance; +} + Input::~Input() { delete p; diff --git a/src/input.h b/src/input.h index 1daede7..1433855 100644 --- a/src/input.h +++ b/src/input.h @@ -58,6 +58,7 @@ public: /* Non-standard extensions */ int mouseX(); int mouseY(); + int scrollV(); private: Input(const RGSSThreadData &rtData);