From b7e013e36113db5e56181003653d2f65cd08401d Mon Sep 17 00:00:00 2001 From: lightmanLP <50497969+lightmanLP@users.noreply.github.com> Date: Thu, 2 Jan 2025 07:48:14 +0000 Subject: [PATCH] buffered wheel axis mapping (test) --- .../MouseWheelToAxisDirectionMapping.cpp | 8 +-- .../controller/mapping/mouse/WheelHandler.cpp | 52 +++++++++++++++---- .../controller/mapping/mouse/WheelHandler.h | 5 ++ 3 files changed, 53 insertions(+), 12 deletions(-) diff --git a/src/controller/controldevice/controller/mapping/mouse/MouseWheelToAxisDirectionMapping.cpp b/src/controller/controldevice/controller/mapping/mouse/MouseWheelToAxisDirectionMapping.cpp index 7038df86f..3ff081b7c 100644 --- a/src/controller/controldevice/controller/mapping/mouse/MouseWheelToAxisDirectionMapping.cpp +++ b/src/controller/controldevice/controller/mapping/mouse/MouseWheelToAxisDirectionMapping.cpp @@ -19,9 +19,11 @@ float MouseWheelToAxisDirectionMapping::GetNormalizedAxisDirectionValue() { return 0.0f; } - // TODO: scale input to match with MAX_AXIS_RANGE - // note: this is temporary solution to test numbers on different backends - return fmin(WheelHandler::GetInstance()->GetDirectionValue(mWheelDirection) * MAX_AXIS_RANGE, MAX_AXIS_RANGE); + if (WheelHandler::GetInstance()->GetBufferedDirectionValue(mWheelDirection) > 0) { + return MAX_AXIS_RANGE; + } else { + return 0.0f; + } } std::string MouseWheelToAxisDirectionMapping::GetAxisDirectionMappingId() { diff --git a/src/controller/controldevice/controller/mapping/mouse/WheelHandler.cpp b/src/controller/controldevice/controller/mapping/mouse/WheelHandler.cpp index 4632e71c4..8d9e00f23 100644 --- a/src/controller/controldevice/controller/mapping/mouse/WheelHandler.cpp +++ b/src/controller/controldevice/controller/mapping/mouse/WheelHandler.cpp @@ -1,5 +1,6 @@ #include "WheelHandler.h" #include "Context.h" +#include namespace Ship { WheelHandler::WheelHandler() { @@ -18,9 +19,34 @@ std::shared_ptr WheelHandler::GetInstance() { return mInstance; } +void WheelHandler::UpdateAxisBuffer(float *buf, float input) { + static const float LIMIT = 2.0f; + static const float REDUCE_STEP = 1.0f; + + // reduce buffered value + if (*buf != 0.0f) { + if (fabs(*buf) <= REDUCE_STEP) { + *buf = 0.0f; + } else { + *buf -= copysignf(REDUCE_STEP, *buf); + } + } + + // add current input to buffer + *buf += input; + + // limit buffer + if (fabs(*buf) >= LIMIT) { + *buf = copysignf(LIMIT, *buf); + } +} + void WheelHandler::Update() { mCoords = Context::GetInstance()->GetWindow()->GetMouseWheel(); + UpdateAxisBuffer(&mBufferedCoords.x, mCoords.x); + UpdateAxisBuffer(&mBufferedCoords.y, mCoords.y); + mDirections.x = mDirections.y = LUS_WHEEL_NONE; if (mCoords.x < 0) { mDirections.x = LUS_WHEEL_LEFT; @@ -42,28 +68,36 @@ WheelDirections WheelHandler::GetDirections() { return mDirections; } -float WheelHandler::GetDirectionValue(WheelDirection direction) { +float WheelHandler::CalcDirectionValue(CoordsF& coords, WheelDirection direction) { switch (direction) { case LUS_WHEEL_LEFT: - if (mCoords.x < 0) { - return -mCoords.x; + if (coords.x < 0) { + return -coords.x; } break; case LUS_WHEEL_RIGHT: - if (mCoords.x > 0) { - return mCoords.x; + if (coords.x > 0) { + return coords.x; } break; case LUS_WHEEL_DOWN: - if (mCoords.y < 0) { - return -mCoords.y; + if (coords.y < 0) { + return -coords.y; } break; case LUS_WHEEL_UP: - if (mCoords.y > 0) { - return mCoords.y; + if (coords.y > 0) { + return coords.y; } } return 0.0f; } + +float WheelHandler::GetDirectionValue(WheelDirection direction) { + return CalcDirectionValue(mCoords, direction); +} + +float WheelHandler::GetBufferedDirectionValue(WheelDirection direction) { + return CalcDirectionValue(mBufferedCoords, direction); +} } // namespace Ship diff --git a/src/controller/controldevice/controller/mapping/mouse/WheelHandler.h b/src/controller/controldevice/controller/mapping/mouse/WheelHandler.h index 9fef39c39..500e10e3c 100644 --- a/src/controller/controldevice/controller/mapping/mouse/WheelHandler.h +++ b/src/controller/controldevice/controller/mapping/mouse/WheelHandler.h @@ -21,11 +21,16 @@ class WheelHandler { CoordsF GetCoords(); WheelDirections GetDirections(); float GetDirectionValue(WheelDirection direction); + float GetBufferedDirectionValue(WheelDirection direction); private: + float CalcDirectionValue(CoordsF& coords, WheelDirection direction); + void UpdateAxisBuffer(float *buf, float input); + static std::shared_ptr mInstance; WheelDirections mDirections; CoordsF mCoords; + CoordsF mBufferedCoords; }; } // namespace Ship