-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (c) 2020-present, Fred Emmott <[email protected]> | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the ISC license found in the LICENSE file | ||
* in the root directory of this source tree. | ||
*/ | ||
#include "axiscurve.h" | ||
|
||
#include <cmath> | ||
|
||
namespace fredemmott::inputmapping::actions { | ||
|
||
AxisCurve::AxisCurve( | ||
float curviness, | ||
const AxisEventHandler& next | ||
): mCurviness(curviness), mNext(next) {} | ||
|
||
AxisCurve::~AxisCurve() {} | ||
|
||
void AxisCurve::map(long value) { | ||
// Normalize between -1 to 1 | ||
// | ||
// the raw range is 0-0xffff with 0x7fff as 'neutral', which means | ||
// there (0xffff - 0x7fff = 0x8000) possible positive values, but | ||
// (0x7fff - 0 = 0x7fff) negative values. | ||
// | ||
// This level of accuracy doesn't usually matter, but some games care | ||
// about actually reaching min and max values | ||
const auto scale = value > 0x7fff ? 0x8000 : 0x7fff; | ||
|
||
const double x = (value - 0x7fff) / (double) scale; | ||
const double k = mCurviness; | ||
// This is based on | ||
// https://dinodini.wordpress.com/2010/04/05/normalized-tunable-sigmoid-functions/ | ||
const auto fx = (x - (x * k)) / (k - (abs(x) * 2 * k) + 1); | ||
|
||
const auto clamped = fx > 1.0 ? 1.0 : (fx < -1.0 ? -1.0 : fx); | ||
|
||
mNext->map((clamped * scale) + 0x7fff); | ||
} | ||
|
||
} // namespace fredemmott::inputmapping::actions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright (c) 2020-present, Fred Emmott <[email protected]> | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the ISC license found in the LICENSE file | ||
* in the root directory of this source tree. | ||
*/ | ||
#pragma once | ||
|
||
#include "actionsapi.h" | ||
#include "eventhandler.h" | ||
|
||
namespace fredemmott::inputmapping::actions { | ||
|
||
/** A simple curve with tuneable 'curviness'. | ||
* | ||
* - Curviness is >= -1 and < 1 | ||
* - Curviness of 0 no curve, i.e. a straight line | ||
* - Curviness of 0.9999999 is a very slightly rounded right angle corner | ||
* - You probably want curvature between 0 and 1; 0.5 is a good starting point. | ||
* This makes the axis less sensitive near center, more sensitive when fully | ||
* deflective. A negative curviness gives you the opposite. | ||
*/ | ||
class AxisCurve : public AxisAction { | ||
public: | ||
AxisCurve(float curviness, const AxisEventHandler& next); | ||
virtual ~AxisCurve(); | ||
void map(long value); | ||
private: | ||
float mCurviness; | ||
AxisEventHandler mNext; | ||
}; | ||
|
||
} // namespace fredemmott::inputmapping::actions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters