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 b7e013e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
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,34 @@ std::shared_ptr<WheelHandler> 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;
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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<WheelHandler> mInstance;

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

0 comments on commit b7e013e

Please sign in to comment.