Skip to content

Commit

Permalink
buffered wheel axis mapping (test)
Browse files Browse the repository at this point in the history
  • Loading branch information
lightmanLP committed Jan 2, 2025
1 parent e98eb3c commit e703d9c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ 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);
return fmin(WheelHandler::GetInstance()->GetBufferedDirectionValue(mWheelDirection), 1.0f) * MAX_AXIS_RANGE;
}

std::string MouseWheelToAxisDirectionMapping::GetAxisDirectionMappingId() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "WheelHandler.h"
#include "Context.h"
#include <cmath>

namespace Ship {
WheelHandler::WheelHandler() {
Expand All @@ -18,9 +19,24 @@ std::shared_ptr<WheelHandler> WheelHandler::GetInstance() {
return mInstance;
}

void ApplyToBuffer(float* dst, float src) {
if (fabs(src) == 0) {
return;
}
*dst = fmin(2.0f, *dst + src);
}

void WheelHandler::Update() {
mCoords = Context::GetInstance()->GetWindow()->GetMouseWheel();

// reduce buffered value
ApplyToBuffer(&mBufferedCoords.x, -copysignf(1, mBufferedCoords.x));
ApplyToBuffer(&mBufferedCoords.y, -copysignf(1, mBufferedCoords.y));

// add new value to buffer
ApplyToBuffer(&mBufferedCoords.x, mCoords.x);
ApplyToBuffer(&mBufferedCoords.y, mCoords.y);

mDirections.x = mDirections.y = LUS_WHEEL_NONE;
if (mCoords.x < 0) {
mDirections.x = LUS_WHEEL_LEFT;
Expand All @@ -42,28 +58,36 @@ WheelDirections WheelHandler::GetDirections() {
return mDirections;
}

float WheelHandler::GetDirectionValue(WheelDirection direction) {
float 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
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ class WheelHandler {
CoordsF GetCoords();
WheelDirections GetDirections();
float GetDirectionValue(WheelDirection direction);
float GetBufferedDirectionValue(WheelDirection direction);

private:
static std::shared_ptr<WheelHandler> mInstance;

WheelDirections mDirections;
CoordsF mCoords;
CoordsF mBufferedCoords;
};
} // namespace Ship

0 comments on commit e703d9c

Please sign in to comment.