simplifying input calls & state

This commit is contained in:
Noel Berry 2021-05-09 18:37:31 -07:00
parent 81e2de3553
commit 2447490033
4 changed files with 192 additions and 274 deletions

View File

@ -28,7 +28,6 @@
#include "blah/images/image.h" #include "blah/images/image.h"
#include "blah/images/packer.h" #include "blah/images/packer.h"
#include "blah/numerics/calc.h" #include "blah/numerics/calc.h"
#include "blah/numerics/circle.h" #include "blah/numerics/circle.h"
#include "blah/numerics/color.h" #include "blah/numerics/color.h"

View File

@ -275,22 +275,55 @@ namespace Blah
struct KeyboardState struct KeyboardState
{ {
// whether a key was pressed this frame
bool pressed[Input::max_keyboard_keys]; bool pressed[Input::max_keyboard_keys];
// whether a key is currently held
bool down[Input::max_keyboard_keys]; bool down[Input::max_keyboard_keys];
// whether a key was released this frame
bool released[Input::max_keyboard_keys]; bool released[Input::max_keyboard_keys];
// the timestamp for the key being pressed
u64 timestamp[Input::max_keyboard_keys]; u64 timestamp[Input::max_keyboard_keys];
// current text input this frame
String text; String text;
// Checks if the Left or Right Ctrl Key is down
bool ctrl();
// Checks if the Left or Right Shift Key is down
bool shift();
// Checks if the Left or Right Alt Key is down
bool alt();
}; };
struct MouseState struct MouseState
{ {
// whether a button was pressed this frame
bool pressed[Input::max_mouse_buttons]; bool pressed[Input::max_mouse_buttons];
// whether a button was held this frame
bool down[Input::max_mouse_buttons]; bool down[Input::max_mouse_buttons];
// whether a button was released this frame
bool released[Input::max_mouse_buttons]; bool released[Input::max_mouse_buttons];
// the timestamp for the button being pressed
u64 timestamp[Input::max_mouse_buttons]; u64 timestamp[Input::max_mouse_buttons];
// mouse position in screen coordinates
Vec2 screen_position; Vec2 screen_position;
// mouse position in pixel coordinates
Vec2 draw_position; Vec2 draw_position;
// mouse position on the window
Vec2 position; Vec2 position;
// mouse wheel value this frame
Point wheel; Point wheel;
}; };
@ -306,27 +339,36 @@ namespace Blah
// The current Mouse state // The current Mouse state
MouseState mouse; MouseState mouse;
}; };
// Keyboard Keys // Keyboard Keys
enum class Key struct Keys
{
enum Enumeration
{ {
#define DEFINE_KEY(name, value) name = value, #define DEFINE_KEY(name, value) name = value,
BLAH_KEY_DEFINITIONS BLAH_KEY_DEFINITIONS
#undef DEFINE_KEY #undef DEFINE_KEY
}; };
};
using Key = Keys::Enumeration;
// Game Controller Buttons // Game Controller Buttons
enum class Button struct Buttons
{
enum Enumeration
{ {
#define DEFINE_BTN(name, value) name = value, #define DEFINE_BTN(name, value) name = value,
BLAH_BUTTON_DEFINITIONS BLAH_BUTTON_DEFINITIONS
#undef DEFINE_BTN #undef DEFINE_BTN
}; };
};
using Button = Buttons::Enumeration;
// Game Controller Axes // Game Controller Axis
enum class Axis struct Axes
{
enum Enumeration
{ {
None = -1, None = -1,
LeftX = 0, LeftX = 0,
@ -334,20 +376,26 @@ namespace Blah
RightX = 2, RightX = 2,
RightY = 3, RightY = 3,
LeftTrigger = 4, LeftTrigger = 4,
RightTrigger = 5 RightTrigger = 5,
}; };
};
using Axis = Axes::Enumeration;
// Mouse Buttons // Mouse Buttons
enum class MouseButton struct MouseButtons
{
enum Enumeration
{ {
None = -1, None = -1,
Left = 0, Left = 0,
Middle = 1, Middle = 1,
Right = 2, Right = 2,
}; };
};
using MouseButton = MouseButtons::Enumeration;
class InputBinding; class ButtonBinding;
using InputBindingRef = Ref<InputBinding>; using ButtonBindingRef = Ref<ButtonBinding>;
class AxisBinding; class AxisBinding;
using AxisBindingRef = Ref<AxisBinding>; using AxisBindingRef = Ref<AxisBinding>;
@ -359,7 +407,7 @@ namespace Blah
// You must call Binding::update() every frame to poll the input state. // You must call Binding::update() every frame to poll the input state.
// Alternatively, bindings can be registered to Input which will // Alternatively, bindings can be registered to Input which will
// automatically update them. // automatically update them.
class InputBinding class ButtonBinding
{ {
public: public:
@ -418,16 +466,16 @@ namespace Blah
// List of bound Mouse buttons // List of bound Mouse buttons
StackVector<MouseButton, 16> mouse; StackVector<MouseButton, 16> mouse;
InputBinding() = default; ButtonBinding() = default;
InputBinding(float press_buffer) ButtonBinding(float press_buffer)
: press_buffer(press_buffer) : press_buffer(press_buffer)
{ {
} }
template<typename ... Args> template<typename ... Args>
InputBinding(float press_buffer, const Args&... args) ButtonBinding(float press_buffer, const Args&... args)
: press_buffer(press_buffer) : press_buffer(press_buffer)
{ {
add(args...); add(args...);
@ -461,20 +509,20 @@ namespace Blah
void consume_release(); void consume_release();
// adds a key to the binding // adds a key to the binding
InputBinding& add(Key key); ButtonBinding& add(Key key);
// adds a button to the binding // adds a button to the binding
InputBinding& add(ButtonBind button); ButtonBinding& add(ButtonBind button);
// adds an trigger to the binding // adds an trigger to the binding
InputBinding& add(TriggerBind trigger); ButtonBinding& add(TriggerBind trigger);
// adds a mouse button to the binding // adds a mouse button to the binding
InputBinding& add(MouseButton mouse); ButtonBinding& add(MouseButton mouse);
// adds an input to the binding // adds an input to the binding
template<typename T, typename T2, typename ... Args> template<typename T, typename T2, typename ... Args>
InputBinding& add(T first, T2 second, const Args&... args) ButtonBinding& add(T first, T2 second, const Args&... args)
{ {
add(first); add(first);
add(second, args...); add(second, args...);
@ -482,13 +530,13 @@ namespace Blah
} }
// adds the left trigger to the binding // adds the left trigger to the binding
InputBinding& add_left_trigger(int controller, float threshold); ButtonBinding& add_left_trigger(int controller, float threshold);
// adds the right trigger to the binding // adds the right trigger to the binding
InputBinding& add_right_trigger(int controller, float threshold); ButtonBinding& add_right_trigger(int controller, float threshold);
// assigns all the bindings to the specific controller // assigns all the bindings to the specific controller
InputBinding& set_controller(int index); ButtonBinding& set_controller(int index);
// removes all bindings // removes all bindings
void clear(); void clear();
@ -526,17 +574,17 @@ namespace Blah
}; };
// Negative Value Binding // Negative Value Binding
InputBinding negative; ButtonBinding negative;
// Positive Value Binding // Positive Value Binding
InputBinding positive; ButtonBinding positive;
// How to handle overlaps (ex. Left and Right are both held) // How to handle overlaps (ex. Left and Right are both held)
Overlap overlap = Overlap::Newer; Overlap overlap = Overlap::Newer;
AxisBinding() = default; AxisBinding() = default;
AxisBinding(const InputBinding& negative, const InputBinding& positive, Overlap overlap = Overlap::Newer) AxisBinding(const ButtonBinding& negative, const ButtonBinding& positive, Overlap overlap = Overlap::Newer)
: negative(negative) : negative(negative)
, positive(positive) , positive(positive)
, overlap(overlap) , overlap(overlap)
@ -648,13 +696,11 @@ namespace Blah
namespace Input namespace Input
{ {
// Returns the Input State of the current frame. // Input State for the current frame
// This pointer is only valid for the current frame and should not be stored. extern InputState state;
const InputState* state();
// Returns the Input State of the previous frame. // Input State for the previous frame
// This pointer is only valid for the current frame and should not be stored. extern InputState last_state;
const InputState* last_state();
// Gets the Mouse Position // Gets the Mouse Position
Vec2 mouse(); Vec2 mouse();
@ -695,42 +741,14 @@ namespace Blah
// Checks if the Left or Right Alt Key is down // Checks if the Left or Right Alt Key is down
bool alt(); bool alt();
// Get the current Text Input // returns a string name of the key
const char* text();
// Gets the controller info for the current controller index.
// If the controller is not connected or the index it out of range, this will set an unconnected controller.
const ControllerState* controller(int controller_index);
// Checks if the button on the controller was pressed this frame.
// If the controller is not connected, or the index is out of range, this will return false.
bool pressed(int controller_index, Button button);
// Checks if the button on the controller was held this frame.
// If the controller is not connected, or the index is out of range, this will return false.
bool down(int controller_index, Button button);
// Checks if the button on the controller was released this frame.
// If the controller is not connected, or the index is out of range, this will return false.
bool released(int controller_index, Button button);
// returns the value of the given axis
float axis_check(int controller_index, Axis axis);
// checks the given virtual axis, described by 2 keys. `fallback` is returned if both keys are held
int axis_check(int fallback, Key negative, Key positive);
// checks the given virtual axis, described by 2 buttons. `fallback` is returned if both buttons are held
int axis_check(int fallback, int controller_index, Button negative, Button positive);
// returns a string name of the given key
const char* name_of(Key key); const char* name_of(Key key);
// returns a string name of the given button // returns a string name of the button
const char* name_of(Button button); const char* name_of(Button button);
// registers a new binding // registers a new binding
InputBindingRef register_binding(const InputBinding& binding); ButtonBindingRef register_binding(const ButtonBinding& binding);
// registers a new axis binding // registers a new axis binding
AxisBindingRef register_binding(const AxisBinding& binding); AxisBindingRef register_binding(const AxisBinding& binding);

View File

@ -295,4 +295,4 @@ namespace
} }
extern const TargetRef App::backbuffer = TargetRef(new BackBuffer()); const TargetRef App::backbuffer = TargetRef(new BackBuffer());

View File

@ -11,26 +11,27 @@ using namespace Blah;
namespace namespace
{ {
InputState g_last_state;
InputState g_curr_state;
InputState g_next_state; InputState g_next_state;
InputState g_empty_state; InputState g_empty_state;
ControllerState g_empty_controller; ControllerState g_empty_controller;
Vector<WeakRef<InputBinding>> g_bindings; Vector<WeakRef<ButtonBinding>> g_buttons;
Vector<WeakRef<AxisBinding>> g_axes; Vector<WeakRef<AxisBinding>> g_axes;
Vector<WeakRef<StickBinding>> g_sticks; Vector<WeakRef<StickBinding>> g_sticks;
} }
InputState Blah::Input::state;
InputState Blah::Input::last_state;
void InputBackend::init() void InputBackend::init()
{ {
g_empty_controller.name = "Disconnected"; g_empty_controller.name = "Disconnected";
for (int i = 0; i < Blah::Input::max_controllers; i++) for (int i = 0; i < Blah::Input::max_controllers; i++)
g_empty_state.controllers[i].name = g_empty_controller.name; g_empty_state.controllers[i].name = g_empty_controller.name;
g_last_state = g_empty_state; Input::last_state = g_empty_state;
g_curr_state = g_empty_state; Input::state = g_empty_state;
g_next_state = g_empty_state; g_next_state = g_empty_state;
g_bindings.dispose(); g_buttons.dispose();
g_axes.dispose(); g_axes.dispose();
g_sticks.dispose(); g_sticks.dispose();
} }
@ -38,8 +39,8 @@ void InputBackend::init()
void InputBackend::frame() void InputBackend::frame()
{ {
// cycle states // cycle states
g_last_state = g_curr_state; Input::last_state = Input::state;
g_curr_state = g_next_state; Input::state = g_next_state;
// copy state, clear pressed / released values // copy state, clear pressed / released values
{ {
@ -75,14 +76,14 @@ void InputBackend::frame()
// update bindings // update bindings
for (int i = 0; i < g_bindings.size(); i++) for (int i = 0; i < g_buttons.size(); i++)
{ {
if (g_bindings[i].use_count() <= 0) if (g_buttons[i].use_count() <= 0)
{ {
g_bindings.erase(i); g_buttons.erase(i);
i--; i--;
} }
else if (auto binding = g_bindings[i].lock()) else if (auto binding = g_buttons[i].lock())
{ {
binding->update(); binding->update();
} }
@ -245,184 +246,84 @@ void InputBackend::on_axis_move(int index, int axis, float value)
} }
} }
const InputState* Input::state() bool KeyboardState::ctrl()
{ {
return &g_curr_state; return down[Key::LeftControl] || down[Key::RightControl];
} }
const InputState* Input::last_state() bool KeyboardState::shift()
{ {
return &g_last_state; return down[Key::LeftShift] || down[Key::RightShift];
}
bool KeyboardState::alt()
{
return down[Key::LeftAlt] || down[Key::RightAlt];
} }
Vec2 Input::mouse() Vec2 Input::mouse()
{ {
return g_curr_state.mouse.position; return state.mouse.position;
} }
Vec2 Input::mouse_draw() Vec2 Input::mouse_draw()
{ {
return Vec2(g_curr_state.mouse.draw_position); return state.mouse.draw_position;
} }
Vec2 Input::mouse_screen() Vec2 Input::mouse_screen()
{ {
return Vec2(g_curr_state.mouse.screen_position); return state.mouse.screen_position;
} }
bool Input::pressed(MouseButton button) bool Input::pressed(MouseButton button)
{ {
int i = (int)button; return state.mouse.pressed[button];
return i >= 0 && i < Blah::Input::max_mouse_buttons&& g_curr_state.mouse.pressed[i];
} }
bool Input::down(MouseButton button) bool Input::down(MouseButton button)
{ {
int i = (int)button; return state.mouse.down[button];
return i >= 0 && i < Blah::Input::max_mouse_buttons&& g_curr_state.mouse.down[i];
} }
bool Input::released(MouseButton button) bool Input::released(MouseButton button)
{ {
int i = (int)button; return state.mouse.released[button];
return i >= 0 && i < Blah::Input::max_mouse_buttons&& g_curr_state.mouse.released[i];
} }
Point Input::mouse_wheel() Point Input::mouse_wheel()
{ {
return g_curr_state.mouse.wheel; return state.mouse.wheel;
} }
bool Input::pressed(Key key) bool Input::pressed(Key key)
{ {
int i = (int)key; return state.keyboard.pressed[key];
return i > 0 && i < Blah::Input::max_keyboard_keys&& g_curr_state.keyboard.pressed[i];
} }
bool Input::down(Key key) bool Input::down(Key key)
{ {
int i = (int)key; return state.keyboard.down[key];
return i > 0 && i < Blah::Input::max_keyboard_keys&& g_curr_state.keyboard.down[i];
} }
bool Input::released(Key key) bool Input::released(Key key)
{ {
int i = (int)key; return state.keyboard.released[key];
return i > 0 && i < Blah::Input::max_keyboard_keys&& g_curr_state.keyboard.released[i];
} }
bool Input::ctrl() bool Input::ctrl()
{ {
return down(Key::LeftControl) || down(Key::RightControl); return state.keyboard.ctrl();
} }
bool Input::shift() bool Input::shift()
{ {
return down(Key::LeftShift) || down(Key::RightShift); return state.keyboard.shift();
} }
bool Input::alt() bool Input::alt()
{ {
return down(Key::LeftAlt) || down(Key::RightAlt); return state.keyboard.alt();
}
const char* Input::text()
{
return g_curr_state.keyboard.text;
}
const ControllerState* Input::controller(int controllerIndex)
{
if (controllerIndex >= Blah::Input::max_controllers)
{
Log::warn("Trying to access a out-of-range controller at %i", controllerIndex);
return &g_empty_controller;
}
else if (!g_curr_state.controllers[controllerIndex].is_connected)
{
return &g_empty_controller;
}
else
{
return &g_curr_state.controllers[controllerIndex];
}
}
bool Input::pressed(int controllerIndex, Button button)
{
int i = (int)button;
if (controllerIndex < Blah::Input::max_controllers && i >= 0 && i < Blah::Input::max_controller_buttons)
return g_curr_state.controllers[controllerIndex].pressed[i];
return false;
}
bool Input::down(int controllerIndex, Button button)
{
int i = (int)button;
if (controllerIndex < Blah::Input::max_controllers && i >= 0 && i < Blah::Input::max_controller_buttons)
return g_curr_state.controllers[controllerIndex].down[i];
return false;
}
bool Input::released(int controllerIndex, Button button)
{
int i = (int)button;
if (controllerIndex < Blah::Input::max_controllers && i >= 0 && i < Blah::Input::max_controller_buttons)
return g_curr_state.controllers[controllerIndex].released[i];
return false;
}
float Input::axis_check(int controllerIndex, Axis axis)
{
int i = (int)axis;
if (controllerIndex < Blah::Input::max_controllers && i >= 0 && i < Blah::Input::max_controller_axis)
return g_curr_state.controllers[controllerIndex].axis[i];
return 0;
}
int Input::axis_check(int fallback, Key negative, Key positive)
{
if (Input::pressed(positive))
return 1;
else if (Input::pressed(negative))
return -1;
else
{
bool pos = Input::down(positive);
bool neg = Input::down(negative);
if (pos && neg)
return fallback;
else if (pos)
return 1;
else if (neg)
return -1;
else
return 0;
}
}
int Input::axis_check(int fallback, int controllerIndex, Button negative, Button positive)
{
if (Input::pressed(controllerIndex, positive))
return 1;
else if (Input::pressed(controllerIndex, negative))
return -1;
else
{
bool pos = Input::down(controllerIndex, positive);
bool neg = Input::down(controllerIndex, negative);
if (pos && neg)
return fallback;
else if (pos)
return 1;
else if (neg)
return -1;
else
return 0;
}
} }
const char* Input::name_of(Key key) const char* Input::name_of(Key key)
@ -449,10 +350,10 @@ const char* Input::name_of(Button button)
return "Unknown"; return "Unknown";
} }
InputBindingRef Input::register_binding(const InputBinding& binding) ButtonBindingRef Input::register_binding(const ButtonBinding& binding)
{ {
auto result = std::make_shared<InputBinding>(binding); auto result = std::make_shared<ButtonBinding>(binding);
g_bindings.push_back(WeakRef<InputBinding>(result)); g_buttons.push_back(WeakRef<ButtonBinding>(result));
return result; return result;
} }
@ -471,19 +372,19 @@ StickBindingRef Input::register_binding(const StickBinding& binding)
} }
InputBinding::TriggerBind::TriggerBind(Axis axis) ButtonBinding::TriggerBind::TriggerBind(Axis axis)
: axis(axis) : axis(axis)
{ {
} }
InputBinding::TriggerBind::TriggerBind(int controller, Axis axis, float threshold, bool positive) ButtonBinding::TriggerBind::TriggerBind(int controller, Axis axis, float threshold, bool positive)
: controller(controller), axis(axis), threshold(threshold), positive(positive) : controller(controller), axis(axis), threshold(threshold), positive(positive)
{ {
} }
bool InputBinding::TriggerBind::is_down(float axis_value) const bool ButtonBinding::TriggerBind::is_down(float axis_value) const
{ {
if ((axis_value > 0 && positive) || (axis_value < 0 && !positive)) if ((axis_value > 0 && positive) || (axis_value < 0 && !positive))
{ {
@ -494,13 +395,13 @@ bool InputBinding::TriggerBind::is_down(float axis_value) const
return false; return false;
} }
InputBinding::ButtonBind::ButtonBind(Button button) ButtonBinding::ButtonBind::ButtonBind(Button button)
: button(button) {} : button(button) {}
InputBinding::ButtonBind::ButtonBind(int controller, Button button) ButtonBinding::ButtonBind::ButtonBind(int controller, Button button)
: controller(controller), button(button) {} : controller(controller), button(button) {}
bool InputBinding::pressed() const bool ButtonBinding::pressed() const
{ {
if (m_press_consumed) if (m_press_consumed)
return false; return false;
@ -511,7 +412,7 @@ bool InputBinding::pressed() const
return m_pressed; return m_pressed;
} }
bool InputBinding::released() const bool ButtonBinding::released() const
{ {
if (m_release_consumed) if (m_release_consumed)
return false; return false;
@ -522,27 +423,27 @@ bool InputBinding::released() const
return m_released; return m_released;
} }
bool InputBinding::down() const bool ButtonBinding::down() const
{ {
return m_down; return m_down;
} }
float InputBinding::value() const float ButtonBinding::value() const
{ {
return m_value; return m_value;
} }
int InputBinding::sign() const int ButtonBinding::sign() const
{ {
return (int)Calc::sign(m_value); return (int)Calc::sign(m_value);
} }
double InputBinding::timestamp() const double ButtonBinding::timestamp() const
{ {
return m_last_timestamp; return m_last_timestamp;
} }
void InputBinding::update() void ButtonBinding::update()
{ {
m_press_consumed = false; m_press_consumed = false;
m_release_consumed = false; m_release_consumed = false;
@ -572,55 +473,55 @@ void InputBinding::update()
m_value = get_value(); m_value = get_value();
} }
void InputBinding::consume_press() void ButtonBinding::consume_press()
{ {
m_press_consumed = true; m_press_consumed = true;
m_last_press_time = -1; m_last_press_time = -1;
} }
void InputBinding::consume_release() void ButtonBinding::consume_release()
{ {
m_release_consumed = true; m_release_consumed = true;
m_last_release_time = -1; m_last_release_time = -1;
} }
InputBinding& InputBinding::add(Key key) ButtonBinding& ButtonBinding::add(Key key)
{ {
keys.push_back(key); keys.push_back(key);
return *this; return *this;
} }
InputBinding& InputBinding::add(ButtonBind button) ButtonBinding& ButtonBinding::add(ButtonBind button)
{ {
buttons.push_back(button); buttons.push_back(button);
return *this; return *this;
} }
InputBinding& InputBinding::add(TriggerBind trigger) ButtonBinding& ButtonBinding::add(TriggerBind trigger)
{ {
triggers.push_back(trigger); triggers.push_back(trigger);
return *this; return *this;
} }
InputBinding& InputBinding::add(MouseButton button) ButtonBinding& ButtonBinding::add(MouseButton button)
{ {
mouse.push_back(button); mouse.push_back(button);
return *this; return *this;
} }
InputBinding& InputBinding::add_left_trigger(int controller, float threshold) ButtonBinding& ButtonBinding::add_left_trigger(int controller, float threshold)
{ {
triggers.push_back(TriggerBind(controller, Axis::LeftTrigger, threshold, true)); triggers.push_back(TriggerBind(controller, Axis::LeftTrigger, threshold, true));
return *this; return *this;
} }
InputBinding& InputBinding::add_right_trigger(int controller, float threshold) ButtonBinding& ButtonBinding::add_right_trigger(int controller, float threshold)
{ {
triggers.push_back(TriggerBind(controller, Axis::RightTrigger, threshold, true)); triggers.push_back(TriggerBind(controller, Axis::RightTrigger, threshold, true));
return *this; return *this;
} }
InputBinding& InputBinding::set_controller(int index) ButtonBinding& ButtonBinding::set_controller(int index)
{ {
for (auto& it : buttons) for (auto& it : buttons)
it.controller = index; it.controller = index;
@ -630,7 +531,7 @@ InputBinding& InputBinding::set_controller(int index)
return *this; return *this;
} }
void InputBinding::clear() void ButtonBinding::clear()
{ {
keys.clear(); keys.clear();
buttons.clear(); buttons.clear();
@ -638,18 +539,18 @@ void InputBinding::clear()
mouse.clear(); mouse.clear();
} }
bool InputBinding::get_pressed() const bool ButtonBinding::get_pressed() const
{ {
for (auto& it : keys) for (auto& it : keys)
if (Input::pressed(it)) if (Input::state.keyboard.pressed[it])
return true; return true;
for (auto& it : mouse) for (auto& it : mouse)
if (Input::pressed(it)) if (Input::state.mouse.pressed[it])
return true; return true;
for (auto& it : buttons) for (auto& it : buttons)
if (Input::pressed(it.controller, it.button)) if (Input::state.controllers[it.controller].pressed[it.button])
return true; return true;
for (auto& it : triggers) for (auto& it : triggers)
@ -660,26 +561,26 @@ bool InputBinding::get_pressed() const
if ((int)it.axis < 0 || (int)it.axis >= Input::max_controller_axis) if ((int)it.axis < 0 || (int)it.axis >= Input::max_controller_axis)
continue; continue;
if (it.is_down(Input::state()->controllers[it.controller].axis[(int)it.axis]) && if (it.is_down(Input::state.controllers[it.controller].axis[(int)it.axis]) &&
!it.is_down(Input::last_state()->controllers[it.controller].axis[(int)it.axis])) !it.is_down(Input::last_state.controllers[it.controller].axis[(int)it.axis]))
return true; return true;
} }
return false; return false;
} }
bool InputBinding::get_released() const bool ButtonBinding::get_released() const
{ {
for (auto& it : keys) for (auto& it : keys)
if (Input::released(it)) if (Input::state.keyboard.released[it])
return true; return true;
for (auto& it : mouse) for (auto& it : mouse)
if (Input::released(it)) if (Input::state.mouse.released[it])
return true; return true;
for (auto& it : buttons) for (auto& it : buttons)
if (Input::released(it.controller, it.button)) if (Input::state.controllers[it.controller].released[it.button])
return true; return true;
for (auto& it : triggers) for (auto& it : triggers)
@ -690,26 +591,26 @@ bool InputBinding::get_released() const
if ((int)it.axis < 0 || (int)it.axis >= Input::max_controller_axis) if ((int)it.axis < 0 || (int)it.axis >= Input::max_controller_axis)
continue; continue;
if (!it.is_down(Input::state()->controllers[it.controller].axis[(int)it.axis]) && if (!it.is_down(Input::state.controllers[it.controller].axis[(int)it.axis]) &&
it.is_down(Input::last_state()->controllers[it.controller].axis[(int)it.axis])) it.is_down(Input::last_state.controllers[it.controller].axis[(int)it.axis]))
return true; return true;
} }
return false; return false;
} }
bool InputBinding::get_down() const bool ButtonBinding::get_down() const
{ {
for (auto& it : keys) for (auto& it : keys)
if (Input::down(it)) if (Input::state.keyboard.down[it])
return true; return true;
for (auto& it : mouse) for (auto& it : mouse)
if (Input::down(it)) if (Input::state.mouse.down[it])
return true; return true;
for (auto& it : buttons) for (auto& it : buttons)
if (Input::down(it.controller, it.button)) if (Input::state.controllers[it.controller].down[it.button])
return true; return true;
for (auto& it : triggers) for (auto& it : triggers)
@ -720,25 +621,25 @@ bool InputBinding::get_down() const
if ((int)it.axis < 0 || (int)it.axis >= Input::max_controller_axis) if ((int)it.axis < 0 || (int)it.axis >= Input::max_controller_axis)
continue; continue;
if (it.is_down(Input::state()->controllers[it.controller].axis[(int)it.axis])) if (it.is_down(Input::state.controllers[it.controller].axis[(int)it.axis]))
return true; return true;
} }
return false; return false;
} }
float InputBinding::get_value() const float ButtonBinding::get_value() const
{ {
for (auto& it : keys) for (auto& it : keys)
if (Input::down(it)) if (Input::state.keyboard.down[it])
return 1.0f; return 1.0f;
for (auto& it : mouse) for (auto& it : mouse)
if (Input::down(it)) if (Input::state.mouse.down[it])
return 1.0f; return 1.0f;
for (auto& it : buttons) for (auto& it : buttons)
if (Input::down(it.controller, it.button)) if (Input::state.controllers[it.controller].down[it.button])
return 1.0f; return 1.0f;
float highest = 0; float highest = 0;
@ -751,7 +652,7 @@ float InputBinding::get_value() const
if ((int)it.axis < 0 || (int)it.axis >= Input::max_controller_axis) if ((int)it.axis < 0 || (int)it.axis >= Input::max_controller_axis)
continue; continue;
float raw_value = Input::state()->controllers[it.controller].axis[(int)it.axis]; float raw_value = Input::state.controllers[it.controller].axis[(int)it.axis];
if (it.is_down(raw_value)) if (it.is_down(raw_value))
{ {
@ -828,29 +729,29 @@ void AxisBinding::consume_release()
AxisBinding& AxisBinding::add_left_stick_x(int controller, float threshold) AxisBinding& AxisBinding::add_left_stick_x(int controller, float threshold)
{ {
negative.add(InputBinding::TriggerBind(controller, Axis::LeftX, threshold, false)); negative.add(ButtonBinding::TriggerBind(controller, Axis::LeftX, threshold, false));
positive.add(InputBinding::TriggerBind(controller, Axis::LeftX, threshold, true)); positive.add(ButtonBinding::TriggerBind(controller, Axis::LeftX, threshold, true));
return *this; return *this;
} }
AxisBinding& AxisBinding::add_left_stick_y(int controller, float threshold) AxisBinding& AxisBinding::add_left_stick_y(int controller, float threshold)
{ {
negative.add(InputBinding::TriggerBind(controller, Axis::LeftY, threshold, false)); negative.add(ButtonBinding::TriggerBind(controller, Axis::LeftY, threshold, false));
positive.add(InputBinding::TriggerBind(controller, Axis::LeftY, threshold, true)); positive.add(ButtonBinding::TriggerBind(controller, Axis::LeftY, threshold, true));
return *this; return *this;
} }
AxisBinding& AxisBinding::add_right_stick_x(int controller, float threshold) AxisBinding& AxisBinding::add_right_stick_x(int controller, float threshold)
{ {
negative.add(InputBinding::TriggerBind(controller, Axis::RightX, threshold, false)); negative.add(ButtonBinding::TriggerBind(controller, Axis::RightX, threshold, false));
positive.add(InputBinding::TriggerBind(controller, Axis::RightX, threshold, true)); positive.add(ButtonBinding::TriggerBind(controller, Axis::RightX, threshold, true));
return *this; return *this;
} }
AxisBinding& AxisBinding::add_right_stick_y(int controller, float threshold) AxisBinding& AxisBinding::add_right_stick_y(int controller, float threshold)
{ {
negative.add(InputBinding::TriggerBind(controller, Axis::RightY, threshold, false)); negative.add(ButtonBinding::TriggerBind(controller, Axis::RightY, threshold, false));
positive.add(InputBinding::TriggerBind(controller, Axis::RightY, threshold, true)); positive.add(ButtonBinding::TriggerBind(controller, Axis::RightY, threshold, true));
return *this; return *this;
} }
@ -901,10 +802,10 @@ void StickBinding::consume_release()
StickBinding& StickBinding::add_dpad(int controller) StickBinding& StickBinding::add_dpad(int controller)
{ {
x.negative.add(InputBinding::ButtonBind(controller, Button::Left)); x.negative.add(ButtonBinding::ButtonBind(controller, Button::Left));
x.positive.add(InputBinding::ButtonBind(controller, Button::Right)); x.positive.add(ButtonBinding::ButtonBind(controller, Button::Right));
y.negative.add(InputBinding::ButtonBind(controller, Button::Up)); y.negative.add(ButtonBinding::ButtonBind(controller, Button::Up));
y.positive.add(InputBinding::ButtonBind(controller, Button::Down)); y.positive.add(ButtonBinding::ButtonBind(controller, Button::Down));
return *this; return *this;
} }