2017-10-15 02:36:31 +00:00
|
|
|
//
|
|
|
|
// Joystick.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 14/10/2017.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2017 Thomas Harte. All rights reserved.
|
2017-10-15 02:36:31 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef Joystick_hpp
|
|
|
|
#define Joystick_hpp
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace Inputs {
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Provides an intermediate idealised model of a simple joystick, allowing a host
|
|
|
|
machine to toggle states, while an interested party either observes or polls.
|
|
|
|
*/
|
|
|
|
class Joystick {
|
|
|
|
public:
|
|
|
|
virtual ~Joystick() {}
|
2017-11-08 03:51:06 +00:00
|
|
|
|
2018-06-12 01:35:03 +00:00
|
|
|
/*!
|
|
|
|
Defines a single input, any individually-measured thing — a fire button or
|
|
|
|
other digital control, an analogue axis, or a button with a symbol on it.
|
|
|
|
*/
|
2018-06-11 00:45:52 +00:00
|
|
|
struct Input {
|
|
|
|
/// Defines the broad type of the input.
|
2018-02-26 00:08:50 +00:00
|
|
|
enum Type {
|
2018-06-11 00:45:52 +00:00
|
|
|
// Half-axis inputs.
|
|
|
|
Up, Down, Left, Right,
|
|
|
|
// Full-axis inputs.
|
|
|
|
Horizontal, Vertical,
|
|
|
|
// Fire buttons.
|
|
|
|
Fire,
|
|
|
|
// Other labelled keys.
|
2018-02-26 00:08:50 +00:00
|
|
|
Key
|
2018-06-11 00:45:52 +00:00
|
|
|
};
|
|
|
|
const Type type;
|
|
|
|
|
2018-06-12 01:35:03 +00:00
|
|
|
bool is_digital_axis() const {
|
|
|
|
return type < Type::Horizontal;
|
|
|
|
}
|
|
|
|
bool is_analogue_axis() const {
|
|
|
|
return type >= Type::Horizontal && type < Type::Fire;
|
|
|
|
}
|
|
|
|
bool is_axis() const {
|
|
|
|
return type < Type::Fire;
|
|
|
|
}
|
|
|
|
bool is_button() const {
|
|
|
|
return type >= Type::Fire;
|
|
|
|
}
|
|
|
|
|
2018-06-11 00:45:52 +00:00
|
|
|
enum Precision {
|
|
|
|
Analogue, Digital
|
|
|
|
};
|
2018-06-12 01:35:03 +00:00
|
|
|
Precision precision() const {
|
|
|
|
return is_analogue_axis() ? Precision::Analogue : Precision::Digital;
|
|
|
|
}
|
2018-06-11 00:45:52 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
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 {
|
2018-02-26 00:08:50 +00:00
|
|
|
struct {
|
2018-06-12 01:35:03 +00:00
|
|
|
size_t index;
|
2018-02-26 00:08:50 +00:00
|
|
|
} control;
|
|
|
|
struct {
|
|
|
|
wchar_t symbol;
|
|
|
|
} key;
|
2018-06-11 00:45:52 +00:00
|
|
|
};
|
|
|
|
Info info;
|
|
|
|
// TODO: Find a way to make the above safely const; may mean not using a union.
|
2018-02-26 00:08:50 +00:00
|
|
|
|
2018-06-12 01:35:03 +00:00
|
|
|
Input(Type type, size_t index = 0) :
|
|
|
|
type(type) {
|
2018-02-26 00:08:50 +00:00
|
|
|
info.control.index = index;
|
|
|
|
}
|
2018-06-12 01:35:03 +00:00
|
|
|
Input(wchar_t symbol) : type(Key) {
|
2018-02-26 00:08:50 +00:00
|
|
|
info.key.symbol = symbol;
|
|
|
|
}
|
|
|
|
|
2018-06-11 00:45:52 +00:00
|
|
|
bool operator == (const Input &rhs) {
|
2018-02-26 00:08:50 +00:00
|
|
|
if(rhs.type != type) return false;
|
|
|
|
if(rhs.type == Key) {
|
|
|
|
return rhs.info.key.symbol == info.key.symbol;
|
|
|
|
} else {
|
|
|
|
return rhs.info.control.index == info.control.index;
|
|
|
|
}
|
|
|
|
}
|
2017-10-15 02:36:31 +00:00
|
|
|
};
|
|
|
|
|
2018-06-12 01:35:03 +00:00
|
|
|
/// @returns The list of all inputs defined on this joystick.
|
|
|
|
virtual std::vector<Input> &get_inputs() = 0;
|
2018-06-11 00:45:52 +00:00
|
|
|
|
2018-06-12 01:35:03 +00:00
|
|
|
/*!
|
|
|
|
Sets the digital value of @c input. This may have direct effect or
|
|
|
|
influence an analogue value; e.g. if the caller declares that ::Left is
|
|
|
|
active but this joystick has only an analogue horizontal axis, this will
|
|
|
|
cause a change to that analogue value.
|
|
|
|
*/
|
2018-06-11 00:45:52 +00:00
|
|
|
virtual void set_input(const Input &input, bool is_active) = 0;
|
2018-02-26 00:08:50 +00:00
|
|
|
|
2018-06-12 01:35:03 +00:00
|
|
|
/*!
|
|
|
|
Sets the analogue value of @c input. If the input is actually digital,
|
|
|
|
or if there is a digital input with a corresponding meaning (e.g. ::Left
|
|
|
|
versus the horizontal axis), this may cause a digital input to be set.
|
|
|
|
|
|
|
|
@c value should be in the range [0.0, 1.0].
|
|
|
|
*/
|
|
|
|
virtual void set_input(const Input &input, float value) = 0;
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Sets all inputs to their resting state.
|
|
|
|
*/
|
2017-10-16 00:44:59 +00:00
|
|
|
virtual void reset_all_inputs() {
|
2018-02-26 00:08:50 +00:00
|
|
|
for(const auto &input: get_inputs()) {
|
2018-06-12 01:35:03 +00:00
|
|
|
if(input.precision() == Input::Precision::Digital)
|
|
|
|
set_input(input, false);
|
|
|
|
else
|
|
|
|
set_input(input, 0.5f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
|
|
|
ConcreteJoystick is the class that it's expected most machines will actually subclass;
|
|
|
|
it accepts a set of Inputs at construction and thereby is able to provide the
|
|
|
|
promised analogue <-> digital mapping of Joystick.
|
|
|
|
*/
|
|
|
|
class ConcreteJoystick: public Joystick {
|
|
|
|
public:
|
|
|
|
ConcreteJoystick(const std::vector<Input> &inputs) : inputs_(inputs) {
|
|
|
|
// Size and populate axis_types_, which is used for digital <-> analogue conversion.
|
|
|
|
for(const auto &input: inputs_) {
|
|
|
|
const bool is_digital_axis = input.is_digital_axis();
|
|
|
|
const bool is_analogue_axis = input.is_analogue_axis();
|
|
|
|
if(is_digital_axis || is_analogue_axis) {
|
|
|
|
const size_t required_size = static_cast<size_t>(input.info.control.index+1);
|
|
|
|
if(axis_types_.size() < required_size) {
|
|
|
|
axis_types_.resize(required_size);
|
|
|
|
}
|
|
|
|
axis_types_[static_cast<size_t>(input.info.control.index)] = is_digital_axis ? AxisType::Digital : AxisType::Analogue;
|
|
|
|
}
|
2018-02-26 00:08:50 +00:00
|
|
|
}
|
2017-10-16 00:44:59 +00:00
|
|
|
}
|
2018-06-12 01:35:03 +00:00
|
|
|
|
|
|
|
std::vector<Input> &get_inputs() override final {
|
|
|
|
return inputs_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_input(const Input &input, bool is_active) override final {
|
|
|
|
// If this is a digital setting to a digital property, just pass it along.
|
|
|
|
if(input.is_button() || axis_types_[input.info.control.index] == AxisType::Digital) {
|
|
|
|
did_set_input(input, is_active);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise this is logically to an analogue axis; for now just use some
|
|
|
|
// convenient hard-coded values. TODO: make these a function of time.
|
|
|
|
using Type = Joystick::Input::Type;
|
|
|
|
using Precision = Joystick::Input::Precision;
|
|
|
|
switch(input.type) {
|
|
|
|
default: did_set_input(input, is_active ? 1.0f : 0.0f); break;
|
|
|
|
case Type::Left: did_set_input(Input(Type::Horizontal, input.info.control.index), 0.25f); break;
|
|
|
|
case Type::Right: did_set_input(Input(Type::Horizontal, input.info.control.index), 0.75f); break;
|
|
|
|
case Type::Up: did_set_input(Input(Type::Vertical, input.info.control.index), 0.25f); break;
|
|
|
|
case Type::Down: did_set_input(Input(Type::Vertical, input.info.control.index), 0.75f); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_input(const Input &input, float value) override final {
|
|
|
|
// If this is an analogue setting to an analogue property, just pass it along.
|
|
|
|
if(!input.is_button() && axis_types_[input.info.control.index] == AxisType::Analogue) {
|
|
|
|
did_set_input(input, value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise apply a threshold test to convert to digital, with remapping from axes to digital inputs.
|
|
|
|
using Type = Joystick::Input::Type;
|
|
|
|
using Precision = Joystick::Input::Precision;
|
|
|
|
switch(input.type) {
|
|
|
|
default: did_set_input(input, value > 0.5f); break;
|
|
|
|
case Type::Horizontal:
|
|
|
|
did_set_input(Input(Type::Left, input.info.control.index), value <= 0.25f);
|
|
|
|
did_set_input(Input(Type::Right, input.info.control.index), value >= 0.25f);
|
|
|
|
break;
|
|
|
|
case Type::Vertical:
|
|
|
|
did_set_input(Input(Type::Up, input.info.control.index), value <= 0.25f);
|
|
|
|
did_set_input(Input(Type::Down, input.info.control.index), value >= 0.25f);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void did_set_input(const Input &input, float value) {
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void did_set_input(const Input &input, bool value) {
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<Input> inputs_;
|
|
|
|
|
|
|
|
enum class AxisType {
|
|
|
|
Digital,
|
|
|
|
Analogue
|
|
|
|
};
|
|
|
|
std::vector<AxisType> axis_types_;
|
2017-10-15 02:36:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* Joystick_hpp */
|