1
0
mirror of https://github.com/TomHarte/CLK.git synced 2026-04-21 17:16:44 +00:00

Moves the joystick class towards accepting analogue inputs.

This commit is contained in:
Thomas Harte
2018-06-10 20:45:52 -04:00
parent a1c60152d4
commit 3ea2a4ccb8
7 changed files with 109 additions and 76 deletions
+43 -12
View File
@@ -21,29 +21,57 @@ class Joystick {
public:
virtual ~Joystick() {}
struct DigitalInput {
struct Input {
/// Defines the broad type of the input.
enum Type {
Up, Down, Left, Right, Fire,
// Half-axis inputs.
Up, Down, Left, Right,
// Full-axis inputs.
Horizontal, Vertical,
// Fire buttons.
Fire,
// Other labelled keys.
Key
} type;
union {
};
const Type type;
enum Precision {
Analogue, Digital
};
const Precision precision;
/*!
Holds extra information pertaining to the input.
@c Type::Key inputs declare the symbol printed on them.
All other types of input have an associated index, indicating whether they
are the zeroth, first, second, third, etc of those things. E.g. a joystick
may have two fire buttons, which will be buttons 0 and 1.
*/
union Info {
struct {
int index;
} control;
struct {
wchar_t symbol;
} key;
} info;
};
Info info;
// TODO: Find a way to make the above safely const; may mean not using a union.
DigitalInput(Type type, int index = 0) : type(type) {
Input(Type type, int index = 0, Precision precision = Precision::Digital) :
type(type),
precision(precision) {
info.control.index = index;
}
DigitalInput(wchar_t symbol) : type(Key) {
Input(wchar_t symbol) : type(Key), precision(Precision::Digital) {
info.key.symbol = symbol;
}
bool operator == (const DigitalInput &rhs) {
bool operator == (const Input &rhs) {
if(rhs.type != type) return false;
if(rhs.precision != precision) return false;
if(rhs.type == Key) {
return rhs.info.key.symbol == info.key.symbol;
} else {
@@ -52,13 +80,16 @@ class Joystick {
}
};
virtual std::vector<DigitalInput> get_inputs() = 0;
virtual std::vector<Input> get_inputs() = 0;
// Host interface. Note that the two set_inputs have logic to map
// between analogue and digital inputs; if you override
virtual void set_input(const Input &input, bool is_active) = 0;
virtual void set_input(const Input &input, float value) {}
// Host interface.
virtual void set_digital_input(const DigitalInput &digital_input, bool is_active) = 0;
virtual void reset_all_inputs() {
for(const auto &input: get_inputs()) {
set_digital_input(input, false);
set_input(input, false);
}
}
};