mirror of
https://github.com/NoelFB/blah.git
synced 2024-11-25 16:18:57 +08:00
simplifying input calls & state
This commit is contained in:
parent
81e2de3553
commit
2447490033
|
@ -28,7 +28,6 @@
|
|||
#include "blah/images/image.h"
|
||||
#include "blah/images/packer.h"
|
||||
|
||||
|
||||
#include "blah/numerics/calc.h"
|
||||
#include "blah/numerics/circle.h"
|
||||
#include "blah/numerics/color.h"
|
||||
|
|
|
@ -275,22 +275,55 @@ namespace Blah
|
|||
|
||||
struct KeyboardState
|
||||
{
|
||||
// whether a key was pressed this frame
|
||||
bool pressed[Input::max_keyboard_keys];
|
||||
|
||||
// whether a key is currently held
|
||||
bool down[Input::max_keyboard_keys];
|
||||
|
||||
// whether a key was released this frame
|
||||
bool released[Input::max_keyboard_keys];
|
||||
|
||||
// the timestamp for the key being pressed
|
||||
u64 timestamp[Input::max_keyboard_keys];
|
||||
|
||||
// current text input this frame
|
||||
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
|
||||
{
|
||||
// whether a button was pressed this frame
|
||||
bool pressed[Input::max_mouse_buttons];
|
||||
|
||||
// whether a button was held this frame
|
||||
bool down[Input::max_mouse_buttons];
|
||||
|
||||
// whether a button was released this frame
|
||||
bool released[Input::max_mouse_buttons];
|
||||
|
||||
// the timestamp for the button being pressed
|
||||
u64 timestamp[Input::max_mouse_buttons];
|
||||
|
||||
// mouse position in screen coordinates
|
||||
Vec2 screen_position;
|
||||
|
||||
// mouse position in pixel coordinates
|
||||
Vec2 draw_position;
|
||||
|
||||
// mouse position on the window
|
||||
Vec2 position;
|
||||
|
||||
// mouse wheel value this frame
|
||||
Point wheel;
|
||||
};
|
||||
|
||||
|
@ -306,48 +339,63 @@ namespace Blah
|
|||
|
||||
// The current Mouse state
|
||||
MouseState mouse;
|
||||
|
||||
};
|
||||
|
||||
// Keyboard Keys
|
||||
enum class Key
|
||||
struct Keys
|
||||
{
|
||||
#define DEFINE_KEY(name, value) name = value,
|
||||
BLAH_KEY_DEFINITIONS
|
||||
#undef DEFINE_KEY
|
||||
enum Enumeration
|
||||
{
|
||||
#define DEFINE_KEY(name, value) name = value,
|
||||
BLAH_KEY_DEFINITIONS
|
||||
#undef DEFINE_KEY
|
||||
};
|
||||
};
|
||||
using Key = Keys::Enumeration;
|
||||
|
||||
// Game Controller Buttons
|
||||
enum class Button
|
||||
struct Buttons
|
||||
{
|
||||
#define DEFINE_BTN(name, value) name = value,
|
||||
BLAH_BUTTON_DEFINITIONS
|
||||
#undef DEFINE_BTN
|
||||
enum Enumeration
|
||||
{
|
||||
#define DEFINE_BTN(name, value) name = value,
|
||||
BLAH_BUTTON_DEFINITIONS
|
||||
#undef DEFINE_BTN
|
||||
};
|
||||
};
|
||||
using Button = Buttons::Enumeration;
|
||||
|
||||
// Game Controller Axes
|
||||
enum class Axis
|
||||
// Game Controller Axis
|
||||
struct Axes
|
||||
{
|
||||
None = -1,
|
||||
LeftX = 0,
|
||||
LeftY = 1,
|
||||
RightX = 2,
|
||||
RightY = 3,
|
||||
LeftTrigger = 4,
|
||||
RightTrigger = 5
|
||||
enum Enumeration
|
||||
{
|
||||
None = -1,
|
||||
LeftX = 0,
|
||||
LeftY = 1,
|
||||
RightX = 2,
|
||||
RightY = 3,
|
||||
LeftTrigger = 4,
|
||||
RightTrigger = 5,
|
||||
};
|
||||
};
|
||||
using Axis = Axes::Enumeration;
|
||||
|
||||
// Mouse Buttons
|
||||
enum class MouseButton
|
||||
struct MouseButtons
|
||||
{
|
||||
None = -1,
|
||||
Left = 0,
|
||||
Middle = 1,
|
||||
Right = 2,
|
||||
enum Enumeration
|
||||
{
|
||||
None = -1,
|
||||
Left = 0,
|
||||
Middle = 1,
|
||||
Right = 2,
|
||||
};
|
||||
};
|
||||
using MouseButton = MouseButtons::Enumeration;
|
||||
|
||||
class InputBinding;
|
||||
using InputBindingRef = Ref<InputBinding>;
|
||||
class ButtonBinding;
|
||||
using ButtonBindingRef = Ref<ButtonBinding>;
|
||||
|
||||
class AxisBinding;
|
||||
using AxisBindingRef = Ref<AxisBinding>;
|
||||
|
@ -359,7 +407,7 @@ namespace Blah
|
|||
// You must call Binding::update() every frame to poll the input state.
|
||||
// Alternatively, bindings can be registered to Input which will
|
||||
// automatically update them.
|
||||
class InputBinding
|
||||
class ButtonBinding
|
||||
{
|
||||
public:
|
||||
|
||||
|
@ -418,16 +466,16 @@ namespace Blah
|
|||
// List of bound Mouse buttons
|
||||
StackVector<MouseButton, 16> mouse;
|
||||
|
||||
InputBinding() = default;
|
||||
ButtonBinding() = default;
|
||||
|
||||
InputBinding(float press_buffer)
|
||||
ButtonBinding(float press_buffer)
|
||||
: press_buffer(press_buffer)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
template<typename ... Args>
|
||||
InputBinding(float press_buffer, const Args&... args)
|
||||
ButtonBinding(float press_buffer, const Args&... args)
|
||||
: press_buffer(press_buffer)
|
||||
{
|
||||
add(args...);
|
||||
|
@ -461,20 +509,20 @@ namespace Blah
|
|||
void consume_release();
|
||||
|
||||
// adds a key to the binding
|
||||
InputBinding& add(Key key);
|
||||
ButtonBinding& add(Key key);
|
||||
|
||||
// adds a button to the binding
|
||||
InputBinding& add(ButtonBind button);
|
||||
ButtonBinding& add(ButtonBind button);
|
||||
|
||||
// adds an trigger to the binding
|
||||
InputBinding& add(TriggerBind trigger);
|
||||
ButtonBinding& add(TriggerBind trigger);
|
||||
|
||||
// adds a mouse button to the binding
|
||||
InputBinding& add(MouseButton mouse);
|
||||
ButtonBinding& add(MouseButton mouse);
|
||||
|
||||
// adds an input to the binding
|
||||
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(second, args...);
|
||||
|
@ -482,13 +530,13 @@ namespace Blah
|
|||
}
|
||||
|
||||
// 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
|
||||
InputBinding& add_right_trigger(int controller, float threshold);
|
||||
ButtonBinding& add_right_trigger(int controller, float threshold);
|
||||
|
||||
// assigns all the bindings to the specific controller
|
||||
InputBinding& set_controller(int index);
|
||||
ButtonBinding& set_controller(int index);
|
||||
|
||||
// removes all bindings
|
||||
void clear();
|
||||
|
@ -526,17 +574,17 @@ namespace Blah
|
|||
};
|
||||
|
||||
// Negative Value Binding
|
||||
InputBinding negative;
|
||||
ButtonBinding negative;
|
||||
|
||||
// Positive Value Binding
|
||||
InputBinding positive;
|
||||
ButtonBinding positive;
|
||||
|
||||
// How to handle overlaps (ex. Left and Right are both held)
|
||||
Overlap overlap = Overlap::Newer;
|
||||
|
||||
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)
|
||||
, positive(positive)
|
||||
, overlap(overlap)
|
||||
|
@ -648,13 +696,11 @@ namespace Blah
|
|||
|
||||
namespace Input
|
||||
{
|
||||
// Returns the Input State of the current frame.
|
||||
// This pointer is only valid for the current frame and should not be stored.
|
||||
const InputState* state();
|
||||
// Input State for the current frame
|
||||
extern InputState state;
|
||||
|
||||
// Returns the Input State of the previous frame.
|
||||
// This pointer is only valid for the current frame and should not be stored.
|
||||
const InputState* last_state();
|
||||
// Input State for the previous frame
|
||||
extern InputState last_state;
|
||||
|
||||
// Gets the Mouse Position
|
||||
Vec2 mouse();
|
||||
|
@ -667,10 +713,10 @@ namespace Blah
|
|||
|
||||
// Checks if the given Mouse Button is pressed
|
||||
bool pressed(MouseButton button);
|
||||
|
||||
|
||||
// Checks if the given Mouse Button is down
|
||||
bool down(MouseButton button);
|
||||
|
||||
|
||||
// Checks if the given Mouse Button is released
|
||||
bool released(MouseButton button);
|
||||
|
||||
|
@ -695,42 +741,14 @@ namespace Blah
|
|||
// Checks if the Left or Right Alt Key is down
|
||||
bool alt();
|
||||
|
||||
// Get the current Text Input
|
||||
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
|
||||
// returns a string name of the 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);
|
||||
|
||||
// registers a new binding
|
||||
InputBindingRef register_binding(const InputBinding& binding);
|
||||
ButtonBindingRef register_binding(const ButtonBinding& binding);
|
||||
|
||||
// registers a new axis binding
|
||||
AxisBindingRef register_binding(const AxisBinding& binding);
|
||||
|
|
|
@ -295,4 +295,4 @@ namespace
|
|||
|
||||
}
|
||||
|
||||
extern const TargetRef App::backbuffer = TargetRef(new BackBuffer());
|
||||
const TargetRef App::backbuffer = TargetRef(new BackBuffer());
|
287
src/input.cpp
287
src/input.cpp
|
@ -11,26 +11,27 @@ using namespace Blah;
|
|||
|
||||
namespace
|
||||
{
|
||||
InputState g_last_state;
|
||||
InputState g_curr_state;
|
||||
InputState g_next_state;
|
||||
InputState g_empty_state;
|
||||
ControllerState g_empty_controller;
|
||||
Vector<WeakRef<InputBinding>> g_bindings;
|
||||
Vector<WeakRef<ButtonBinding>> g_buttons;
|
||||
Vector<WeakRef<AxisBinding>> g_axes;
|
||||
Vector<WeakRef<StickBinding>> g_sticks;
|
||||
}
|
||||
|
||||
InputState Blah::Input::state;
|
||||
InputState Blah::Input::last_state;
|
||||
|
||||
void InputBackend::init()
|
||||
{
|
||||
g_empty_controller.name = "Disconnected";
|
||||
for (int i = 0; i < Blah::Input::max_controllers; i++)
|
||||
g_empty_state.controllers[i].name = g_empty_controller.name;
|
||||
|
||||
g_last_state = g_empty_state;
|
||||
g_curr_state = g_empty_state;
|
||||
Input::last_state = g_empty_state;
|
||||
Input::state = g_empty_state;
|
||||
g_next_state = g_empty_state;
|
||||
g_bindings.dispose();
|
||||
g_buttons.dispose();
|
||||
g_axes.dispose();
|
||||
g_sticks.dispose();
|
||||
}
|
||||
|
@ -38,8 +39,8 @@ void InputBackend::init()
|
|||
void InputBackend::frame()
|
||||
{
|
||||
// cycle states
|
||||
g_last_state = g_curr_state;
|
||||
g_curr_state = g_next_state;
|
||||
Input::last_state = Input::state;
|
||||
Input::state = g_next_state;
|
||||
|
||||
// copy state, clear pressed / released values
|
||||
{
|
||||
|
@ -75,14 +76,14 @@ void InputBackend::frame()
|
|||
|
||||
// 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--;
|
||||
}
|
||||
else if (auto binding = g_bindings[i].lock())
|
||||
else if (auto binding = g_buttons[i].lock())
|
||||
{
|
||||
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()
|
||||
{
|
||||
return g_curr_state.mouse.position;
|
||||
return state.mouse.position;
|
||||
}
|
||||
|
||||
|
||||
Vec2 Input::mouse_draw()
|
||||
{
|
||||
return Vec2(g_curr_state.mouse.draw_position);
|
||||
return state.mouse.draw_position;
|
||||
}
|
||||
|
||||
Vec2 Input::mouse_screen()
|
||||
{
|
||||
return Vec2(g_curr_state.mouse.screen_position);
|
||||
return state.mouse.screen_position;
|
||||
}
|
||||
|
||||
bool Input::pressed(MouseButton button)
|
||||
{
|
||||
int i = (int)button;
|
||||
return i >= 0 && i < Blah::Input::max_mouse_buttons&& g_curr_state.mouse.pressed[i];
|
||||
return state.mouse.pressed[button];
|
||||
}
|
||||
|
||||
bool Input::down(MouseButton button)
|
||||
{
|
||||
int i = (int)button;
|
||||
return i >= 0 && i < Blah::Input::max_mouse_buttons&& g_curr_state.mouse.down[i];
|
||||
return state.mouse.down[button];
|
||||
}
|
||||
|
||||
bool Input::released(MouseButton button)
|
||||
{
|
||||
int i = (int)button;
|
||||
return i >= 0 && i < Blah::Input::max_mouse_buttons&& g_curr_state.mouse.released[i];
|
||||
return state.mouse.released[button];
|
||||
}
|
||||
|
||||
Point Input::mouse_wheel()
|
||||
{
|
||||
return g_curr_state.mouse.wheel;
|
||||
return state.mouse.wheel;
|
||||
}
|
||||
|
||||
bool Input::pressed(Key key)
|
||||
{
|
||||
int i = (int)key;
|
||||
return i > 0 && i < Blah::Input::max_keyboard_keys&& g_curr_state.keyboard.pressed[i];
|
||||
return state.keyboard.pressed[key];
|
||||
}
|
||||
|
||||
bool Input::down(Key key)
|
||||
{
|
||||
int i = (int)key;
|
||||
return i > 0 && i < Blah::Input::max_keyboard_keys&& g_curr_state.keyboard.down[i];
|
||||
return state.keyboard.down[key];
|
||||
}
|
||||
|
||||
bool Input::released(Key key)
|
||||
{
|
||||
int i = (int)key;
|
||||
return i > 0 && i < Blah::Input::max_keyboard_keys&& g_curr_state.keyboard.released[i];
|
||||
return state.keyboard.released[key];
|
||||
}
|
||||
|
||||
bool Input::ctrl()
|
||||
{
|
||||
return down(Key::LeftControl) || down(Key::RightControl);
|
||||
return state.keyboard.ctrl();
|
||||
}
|
||||
|
||||
bool Input::shift()
|
||||
{
|
||||
return down(Key::LeftShift) || down(Key::RightShift);
|
||||
return state.keyboard.shift();
|
||||
}
|
||||
|
||||
bool Input::alt()
|
||||
{
|
||||
return down(Key::LeftAlt) || down(Key::RightAlt);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
return state.keyboard.alt();
|
||||
}
|
||||
|
||||
const char* Input::name_of(Key key)
|
||||
|
@ -449,10 +350,10 @@ const char* Input::name_of(Button button)
|
|||
return "Unknown";
|
||||
}
|
||||
|
||||
InputBindingRef Input::register_binding(const InputBinding& binding)
|
||||
ButtonBindingRef Input::register_binding(const ButtonBinding& binding)
|
||||
{
|
||||
auto result = std::make_shared<InputBinding>(binding);
|
||||
g_bindings.push_back(WeakRef<InputBinding>(result));
|
||||
auto result = std::make_shared<ButtonBinding>(binding);
|
||||
g_buttons.push_back(WeakRef<ButtonBinding>(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)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
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))
|
||||
{
|
||||
|
@ -494,13 +395,13 @@ bool InputBinding::TriggerBind::is_down(float axis_value) const
|
|||
return false;
|
||||
}
|
||||
|
||||
InputBinding::ButtonBind::ButtonBind(Button button)
|
||||
ButtonBinding::ButtonBind::ButtonBind(Button button)
|
||||
: button(button) {}
|
||||
|
||||
InputBinding::ButtonBind::ButtonBind(int controller, Button button)
|
||||
ButtonBinding::ButtonBind::ButtonBind(int controller, Button button)
|
||||
: controller(controller), button(button) {}
|
||||
|
||||
bool InputBinding::pressed() const
|
||||
bool ButtonBinding::pressed() const
|
||||
{
|
||||
if (m_press_consumed)
|
||||
return false;
|
||||
|
@ -511,7 +412,7 @@ bool InputBinding::pressed() const
|
|||
return m_pressed;
|
||||
}
|
||||
|
||||
bool InputBinding::released() const
|
||||
bool ButtonBinding::released() const
|
||||
{
|
||||
if (m_release_consumed)
|
||||
return false;
|
||||
|
@ -522,27 +423,27 @@ bool InputBinding::released() const
|
|||
return m_released;
|
||||
}
|
||||
|
||||
bool InputBinding::down() const
|
||||
bool ButtonBinding::down() const
|
||||
{
|
||||
return m_down;
|
||||
}
|
||||
|
||||
float InputBinding::value() const
|
||||
float ButtonBinding::value() const
|
||||
{
|
||||
return m_value;
|
||||
}
|
||||
|
||||
int InputBinding::sign() const
|
||||
int ButtonBinding::sign() const
|
||||
{
|
||||
return (int)Calc::sign(m_value);
|
||||
}
|
||||
|
||||
double InputBinding::timestamp() const
|
||||
double ButtonBinding::timestamp() const
|
||||
{
|
||||
return m_last_timestamp;
|
||||
}
|
||||
|
||||
void InputBinding::update()
|
||||
void ButtonBinding::update()
|
||||
{
|
||||
m_press_consumed = false;
|
||||
m_release_consumed = false;
|
||||
|
@ -572,55 +473,55 @@ void InputBinding::update()
|
|||
m_value = get_value();
|
||||
}
|
||||
|
||||
void InputBinding::consume_press()
|
||||
void ButtonBinding::consume_press()
|
||||
{
|
||||
m_press_consumed = true;
|
||||
m_last_press_time = -1;
|
||||
}
|
||||
|
||||
void InputBinding::consume_release()
|
||||
void ButtonBinding::consume_release()
|
||||
{
|
||||
m_release_consumed = true;
|
||||
m_last_release_time = -1;
|
||||
}
|
||||
|
||||
InputBinding& InputBinding::add(Key key)
|
||||
ButtonBinding& ButtonBinding::add(Key key)
|
||||
{
|
||||
keys.push_back(key);
|
||||
return *this;
|
||||
}
|
||||
|
||||
InputBinding& InputBinding::add(ButtonBind button)
|
||||
ButtonBinding& ButtonBinding::add(ButtonBind button)
|
||||
{
|
||||
buttons.push_back(button);
|
||||
return *this;
|
||||
}
|
||||
|
||||
InputBinding& InputBinding::add(TriggerBind trigger)
|
||||
ButtonBinding& ButtonBinding::add(TriggerBind trigger)
|
||||
{
|
||||
triggers.push_back(trigger);
|
||||
return *this;
|
||||
}
|
||||
|
||||
InputBinding& InputBinding::add(MouseButton button)
|
||||
ButtonBinding& ButtonBinding::add(MouseButton button)
|
||||
{
|
||||
mouse.push_back(button);
|
||||
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));
|
||||
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));
|
||||
return *this;
|
||||
}
|
||||
|
||||
InputBinding& InputBinding::set_controller(int index)
|
||||
ButtonBinding& ButtonBinding::set_controller(int index)
|
||||
{
|
||||
for (auto& it : buttons)
|
||||
it.controller = index;
|
||||
|
@ -630,7 +531,7 @@ InputBinding& InputBinding::set_controller(int index)
|
|||
return *this;
|
||||
}
|
||||
|
||||
void InputBinding::clear()
|
||||
void ButtonBinding::clear()
|
||||
{
|
||||
keys.clear();
|
||||
buttons.clear();
|
||||
|
@ -638,18 +539,18 @@ void InputBinding::clear()
|
|||
mouse.clear();
|
||||
}
|
||||
|
||||
bool InputBinding::get_pressed() const
|
||||
bool ButtonBinding::get_pressed() const
|
||||
{
|
||||
for (auto& it : keys)
|
||||
if (Input::pressed(it))
|
||||
if (Input::state.keyboard.pressed[it])
|
||||
return true;
|
||||
|
||||
for (auto& it : mouse)
|
||||
if (Input::pressed(it))
|
||||
if (Input::state.mouse.pressed[it])
|
||||
return true;
|
||||
|
||||
for (auto& it : buttons)
|
||||
if (Input::pressed(it.controller, it.button))
|
||||
if (Input::state.controllers[it.controller].pressed[it.button])
|
||||
return true;
|
||||
|
||||
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)
|
||||
continue;
|
||||
|
||||
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]))
|
||||
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]))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool InputBinding::get_released() const
|
||||
bool ButtonBinding::get_released() const
|
||||
{
|
||||
for (auto& it : keys)
|
||||
if (Input::released(it))
|
||||
if (Input::state.keyboard.released[it])
|
||||
return true;
|
||||
|
||||
for (auto& it : mouse)
|
||||
if (Input::released(it))
|
||||
if (Input::state.mouse.released[it])
|
||||
return true;
|
||||
|
||||
for (auto& it : buttons)
|
||||
if (Input::released(it.controller, it.button))
|
||||
if (Input::state.controllers[it.controller].released[it.button])
|
||||
return true;
|
||||
|
||||
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)
|
||||
continue;
|
||||
|
||||
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]))
|
||||
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]))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool InputBinding::get_down() const
|
||||
bool ButtonBinding::get_down() const
|
||||
{
|
||||
for (auto& it : keys)
|
||||
if (Input::down(it))
|
||||
if (Input::state.keyboard.down[it])
|
||||
return true;
|
||||
|
||||
for (auto& it : mouse)
|
||||
if (Input::down(it))
|
||||
if (Input::state.mouse.down[it])
|
||||
return true;
|
||||
|
||||
for (auto& it : buttons)
|
||||
if (Input::down(it.controller, it.button))
|
||||
if (Input::state.controllers[it.controller].down[it.button])
|
||||
return true;
|
||||
|
||||
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)
|
||||
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 false;
|
||||
}
|
||||
|
||||
float InputBinding::get_value() const
|
||||
float ButtonBinding::get_value() const
|
||||
{
|
||||
for (auto& it : keys)
|
||||
if (Input::down(it))
|
||||
if (Input::state.keyboard.down[it])
|
||||
return 1.0f;
|
||||
|
||||
for (auto& it : mouse)
|
||||
if (Input::down(it))
|
||||
if (Input::state.mouse.down[it])
|
||||
return 1.0f;
|
||||
|
||||
for (auto& it : buttons)
|
||||
if (Input::down(it.controller, it.button))
|
||||
if (Input::state.controllers[it.controller].down[it.button])
|
||||
return 1.0f;
|
||||
|
||||
float highest = 0;
|
||||
|
@ -751,7 +652,7 @@ float InputBinding::get_value() const
|
|||
if ((int)it.axis < 0 || (int)it.axis >= Input::max_controller_axis)
|
||||
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))
|
||||
{
|
||||
|
@ -828,29 +729,29 @@ void AxisBinding::consume_release()
|
|||
|
||||
AxisBinding& AxisBinding::add_left_stick_x(int controller, float threshold)
|
||||
{
|
||||
negative.add(InputBinding::TriggerBind(controller, Axis::LeftX, threshold, false));
|
||||
positive.add(InputBinding::TriggerBind(controller, Axis::LeftX, threshold, true));
|
||||
negative.add(ButtonBinding::TriggerBind(controller, Axis::LeftX, threshold, false));
|
||||
positive.add(ButtonBinding::TriggerBind(controller, Axis::LeftX, threshold, true));
|
||||
return *this;
|
||||
}
|
||||
|
||||
AxisBinding& AxisBinding::add_left_stick_y(int controller, float threshold)
|
||||
{
|
||||
negative.add(InputBinding::TriggerBind(controller, Axis::LeftY, threshold, false));
|
||||
positive.add(InputBinding::TriggerBind(controller, Axis::LeftY, threshold, true));
|
||||
negative.add(ButtonBinding::TriggerBind(controller, Axis::LeftY, threshold, false));
|
||||
positive.add(ButtonBinding::TriggerBind(controller, Axis::LeftY, threshold, true));
|
||||
return *this;
|
||||
}
|
||||
|
||||
AxisBinding& AxisBinding::add_right_stick_x(int controller, float threshold)
|
||||
{
|
||||
negative.add(InputBinding::TriggerBind(controller, Axis::RightX, threshold, false));
|
||||
positive.add(InputBinding::TriggerBind(controller, Axis::RightX, threshold, true));
|
||||
negative.add(ButtonBinding::TriggerBind(controller, Axis::RightX, threshold, false));
|
||||
positive.add(ButtonBinding::TriggerBind(controller, Axis::RightX, threshold, true));
|
||||
return *this;
|
||||
}
|
||||
|
||||
AxisBinding& AxisBinding::add_right_stick_y(int controller, float threshold)
|
||||
{
|
||||
negative.add(InputBinding::TriggerBind(controller, Axis::RightY, threshold, false));
|
||||
positive.add(InputBinding::TriggerBind(controller, Axis::RightY, threshold, true));
|
||||
negative.add(ButtonBinding::TriggerBind(controller, Axis::RightY, threshold, false));
|
||||
positive.add(ButtonBinding::TriggerBind(controller, Axis::RightY, threshold, true));
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
@ -901,10 +802,10 @@ void StickBinding::consume_release()
|
|||
|
||||
StickBinding& StickBinding::add_dpad(int controller)
|
||||
{
|
||||
x.negative.add(InputBinding::ButtonBind(controller, Button::Left));
|
||||
x.positive.add(InputBinding::ButtonBind(controller, Button::Right));
|
||||
y.negative.add(InputBinding::ButtonBind(controller, Button::Up));
|
||||
y.positive.add(InputBinding::ButtonBind(controller, Button::Down));
|
||||
x.negative.add(ButtonBinding::ButtonBind(controller, Button::Left));
|
||||
x.positive.add(ButtonBinding::ButtonBind(controller, Button::Right));
|
||||
y.negative.add(ButtonBinding::ButtonBind(controller, Button::Up));
|
||||
y.positive.add(ButtonBinding::ButtonBind(controller, Button::Down));
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user