2020-08-11 12:07:21 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <map>
|
2022-08-15 11:04:11 -04:00
|
|
|
#include <vector>
|
2020-08-11 12:07:21 -04:00
|
|
|
|
|
|
|
#include "platform.hpp"
|
|
|
|
|
2021-04-20 11:53:38 -04:00
|
|
|
namespace prism {
|
|
|
|
enum class axis {
|
|
|
|
MouseX,
|
|
|
|
MouseY,
|
2020-08-11 12:07:21 -04:00
|
|
|
|
2021-04-20 11:53:38 -04:00
|
|
|
ScrollX,
|
|
|
|
ScrollY,
|
2020-08-11 12:07:21 -04:00
|
|
|
|
2021-04-20 11:53:38 -04:00
|
|
|
LeftStickX,
|
|
|
|
LeftStickY,
|
|
|
|
|
|
|
|
RightStickX,
|
|
|
|
RightStickY
|
|
|
|
};
|
|
|
|
|
|
|
|
struct input_binding {
|
|
|
|
std::string name;
|
|
|
|
std::map<InputButton, float> buttons;
|
|
|
|
|
|
|
|
std::vector<axis> axises;
|
|
|
|
|
|
|
|
float value = 0.0f;
|
|
|
|
|
|
|
|
bool repeat = false;
|
|
|
|
InputButton last_button = InputButton::Invalid;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Input system designed for handling events in a cross platform manner and across different input types.
|
2020-08-11 12:07:21 -04:00
|
|
|
*/
|
2021-04-20 11:53:38 -04:00
|
|
|
class input_system {
|
|
|
|
public:
|
|
|
|
/// Updates input bindings and their values.
|
|
|
|
void update();
|
|
|
|
|
|
|
|
/** Add a new binding.
|
|
|
|
@param name The binding name.
|
|
|
|
*/
|
2022-08-15 11:04:11 -04:00
|
|
|
void add_binding(const std::string& name);
|
2021-04-20 11:53:38 -04:00
|
|
|
|
|
|
|
/** Ties a button-type input to the binding.
|
|
|
|
@param name The binding name.
|
|
|
|
@param button The button to associate with.
|
|
|
|
@param value The resulting input value when the button is pressed. Default is 1.0.
|
|
|
|
*/
|
2022-08-15 11:04:11 -04:00
|
|
|
void add_binding_button(const std::string& name, InputButton button, float value = 1.0f);
|
2021-04-20 11:53:38 -04:00
|
|
|
|
|
|
|
/** Ties an axis-type input to the button.
|
|
|
|
@param name The binding name.
|
|
|
|
@param button The axis to associate with.
|
|
|
|
*/
|
2022-08-15 11:04:11 -04:00
|
|
|
void add_binding_axis(const std::string& name, axis axis);
|
2021-04-20 11:53:38 -04:00
|
|
|
|
|
|
|
/** Gets the input value of a binding.
|
|
|
|
@param name The binding name.
|
|
|
|
@return The input value associated with that binding, or 0.0 if none is found.
|
|
|
|
@note If this binding is tied to a button-type input, assume that 0.0 means not pressed.
|
|
|
|
@note If this binding is tied to an axis-type input, assume that 0.0 means the stick is centered.
|
|
|
|
*/
|
2022-08-15 11:04:11 -04:00
|
|
|
float get_value(const std::string& name);
|
2021-04-20 11:53:38 -04:00
|
|
|
|
|
|
|
/** Gets whether or not the binding is pressed.
|
|
|
|
@param name The binding name.
|
2022-08-15 11:04:11 -04:00
|
|
|
@param repeating If true, return a correct value regardless if it was identical for multiple frames. If false,
|
|
|
|
any continued input past the first frame of being pressed is ignored and the input is considered released.
|
|
|
|
@return If the binding is found, return true if the binding value is 1.0. If no binding is found or if the
|
|
|
|
binding has continued pressing and repeating is turned off, returns false.
|
2021-04-20 11:53:38 -04:00
|
|
|
*/
|
2022-08-15 11:04:11 -04:00
|
|
|
bool is_pressed(const std::string& name, bool repeating = false);
|
2021-04-20 11:53:38 -04:00
|
|
|
|
|
|
|
/** Queries if the binding is repeating it's input.
|
|
|
|
@param name The binding name.
|
2022-08-15 11:04:11 -04:00
|
|
|
@return If the binding is found, returns true if the binding's values have been identical for multiple frames.
|
|
|
|
Returns false if the binding is not found.
|
2021-04-20 11:53:38 -04:00
|
|
|
*/
|
2022-08-15 11:04:11 -04:00
|
|
|
bool is_repeating(const std::string& name);
|
2021-04-20 11:53:38 -04:00
|
|
|
|
|
|
|
/// Returns all of the bindings registered with this Input system.
|
|
|
|
[[nodiscard]] std::vector<input_binding> get_bindings() const;
|
|
|
|
|
2021-10-07 17:46:28 -04:00
|
|
|
void begin_text_input();
|
|
|
|
void end_text_input();
|
|
|
|
bool is_text_input() const;
|
|
|
|
|
|
|
|
bool get_allowable_text_button(unsigned int keycode) const;
|
|
|
|
|
2021-04-20 11:53:38 -04:00
|
|
|
private:
|
|
|
|
std::vector<input_binding> _input_bindings;
|
|
|
|
std::tuple<int, int> _last_cursor_position;
|
2021-10-07 17:46:28 -04:00
|
|
|
|
|
|
|
bool _in_text_input = false;
|
2021-04-20 11:53:38 -04:00
|
|
|
};
|
2022-08-15 11:04:11 -04:00
|
|
|
} // namespace prism
|