mirror of
https://github.com/NoelFB/blah.git
synced 2025-06-29 19:25:26 +08:00
new input binding/mapping implementation
This commit is contained in:
492
src/input/binding.cpp
Normal file
492
src/input/binding.cpp
Normal file
@ -0,0 +1,492 @@
|
||||
#include <blah/input/binding.h>
|
||||
#include <blah/math/calc.h>
|
||||
#include <blah/core/time.h>
|
||||
|
||||
using namespace Blah;
|
||||
|
||||
BoundTrigger::BoundTrigger(Axis axis)
|
||||
: axis(axis)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
BoundTrigger::BoundTrigger(int controller, Axis axis, float threshold, bool positive)
|
||||
: controller(controller), axis(axis), threshold(threshold), positive(positive)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool BoundTrigger::is_down(float axis_value) const
|
||||
{
|
||||
if ((axis_value > 0 && positive) || (axis_value < 0 && !positive))
|
||||
{
|
||||
if (Calc::abs(axis_value) >= threshold)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
BoundTrigger BoundTrigger::left_stick_left(int controller, float threshold)
|
||||
{
|
||||
return BoundTrigger(controller, Axis::LeftX, threshold, false);
|
||||
}
|
||||
|
||||
BoundTrigger BoundTrigger::left_stick_right(int controller, float threshold)
|
||||
{
|
||||
return BoundTrigger(controller, Axis::LeftX, threshold, true);
|
||||
}
|
||||
|
||||
BoundTrigger BoundTrigger::left_stick_up(int controller, float threshold)
|
||||
{
|
||||
return BoundTrigger(controller, Axis::LeftY, threshold, false);
|
||||
}
|
||||
|
||||
BoundTrigger BoundTrigger::left_stick_down(int controller, float threshold)
|
||||
{
|
||||
return BoundTrigger(controller, Axis::LeftY, threshold, true);
|
||||
}
|
||||
|
||||
BoundTrigger BoundTrigger::right_stick_left(int controller, float threshold)
|
||||
{
|
||||
return BoundTrigger(controller, Axis::RightX, threshold, false);
|
||||
}
|
||||
|
||||
BoundTrigger BoundTrigger::right_stick_right(int controller, float threshold)
|
||||
{
|
||||
return BoundTrigger(controller, Axis::RightX, threshold, true);
|
||||
}
|
||||
|
||||
BoundTrigger BoundTrigger::right_stick_up(int controller, float threshold)
|
||||
{
|
||||
return BoundTrigger(controller, Axis::RightY, threshold, false);
|
||||
}
|
||||
|
||||
BoundTrigger BoundTrigger::right_stick_down(int controller, float threshold)
|
||||
{
|
||||
return BoundTrigger(controller, Axis::RightY, threshold, true);
|
||||
}
|
||||
|
||||
BoundTrigger BoundTrigger::left_trigger(int controller, float threshold)
|
||||
{
|
||||
return BoundTrigger(controller, Axis::LeftTrigger, threshold, true);
|
||||
}
|
||||
|
||||
BoundTrigger BoundTrigger::right_trigger(int controller, float threshold)
|
||||
{
|
||||
return BoundTrigger(controller, Axis::RightTrigger, threshold, true);
|
||||
}
|
||||
|
||||
BoundButton::BoundButton(Button button)
|
||||
: button(button) {}
|
||||
|
||||
BoundButton::BoundButton(int controller, Button button)
|
||||
: controller(controller), button(button) {}
|
||||
|
||||
bool Binding::pressed() const
|
||||
{
|
||||
if (m_press_consumed)
|
||||
return false;
|
||||
|
||||
if (m_last_press_time >= 0 && (Time::seconds - m_last_press_time) <= press_buffer)
|
||||
return true;
|
||||
|
||||
return m_pressed;
|
||||
}
|
||||
|
||||
bool Binding::released() const
|
||||
{
|
||||
if (m_release_consumed)
|
||||
return false;
|
||||
|
||||
if (m_last_release_time >= 0 && (Time::seconds - m_last_release_time) <= release_buffer)
|
||||
return true;
|
||||
|
||||
return m_released;
|
||||
}
|
||||
|
||||
bool Binding::down() const
|
||||
{
|
||||
return m_down;
|
||||
}
|
||||
|
||||
float Binding::value() const
|
||||
{
|
||||
return m_value;
|
||||
}
|
||||
|
||||
int Binding::sign() const
|
||||
{
|
||||
return (int)Calc::sign(m_value);
|
||||
}
|
||||
|
||||
double Binding::timestamp() const
|
||||
{
|
||||
return m_last_timestamp;
|
||||
}
|
||||
|
||||
void Binding::update()
|
||||
{
|
||||
m_press_consumed = false;
|
||||
m_release_consumed = false;
|
||||
|
||||
if (get_pressed())
|
||||
{
|
||||
m_last_timestamp = Time::seconds;
|
||||
m_last_press_time = Time::seconds;
|
||||
m_pressed = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_pressed = false;
|
||||
}
|
||||
|
||||
if (get_released())
|
||||
{
|
||||
m_last_release_time = Time::seconds;
|
||||
m_released = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_released = false;
|
||||
}
|
||||
|
||||
m_down = get_down();
|
||||
m_value = get_value();
|
||||
}
|
||||
|
||||
void Binding::consume_press()
|
||||
{
|
||||
m_press_consumed = true;
|
||||
m_last_press_time = -1;
|
||||
}
|
||||
|
||||
void Binding::consume_release()
|
||||
{
|
||||
m_release_consumed = true;
|
||||
m_last_release_time = -1;
|
||||
}
|
||||
|
||||
void Binding::add(Key key)
|
||||
{
|
||||
keys.push_back(key);
|
||||
}
|
||||
|
||||
void Binding::add(BoundButton button)
|
||||
{
|
||||
buttons.push_back(button);
|
||||
}
|
||||
|
||||
void Binding::add(BoundTrigger trigger)
|
||||
{
|
||||
triggers.push_back(trigger);
|
||||
}
|
||||
|
||||
void Binding::add(MouseButton button)
|
||||
{
|
||||
mouse.push_back(button);
|
||||
}
|
||||
|
||||
void Binding::set_controller(int index)
|
||||
{
|
||||
for (auto& it : buttons)
|
||||
it.controller = index;
|
||||
for (auto& it : triggers)
|
||||
it.controller = index;
|
||||
}
|
||||
|
||||
void Binding::clear()
|
||||
{
|
||||
keys.clear();
|
||||
buttons.clear();
|
||||
triggers.clear();
|
||||
mouse.clear();
|
||||
}
|
||||
|
||||
bool Binding::get_pressed() const
|
||||
{
|
||||
for (auto& it : keys)
|
||||
if (Input::pressed(it))
|
||||
return true;
|
||||
|
||||
for (auto& it : mouse)
|
||||
if (Input::pressed(it))
|
||||
return true;
|
||||
|
||||
for (auto& it : buttons)
|
||||
if (Input::pressed(it.controller, it.button))
|
||||
return true;
|
||||
|
||||
for (auto& it : triggers)
|
||||
{
|
||||
if (it.controller < 0 || it.controller >= Input::max_controllers)
|
||||
continue;
|
||||
|
||||
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]))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Binding::get_released() const
|
||||
{
|
||||
for (auto& it : keys)
|
||||
if (Input::released(it))
|
||||
return true;
|
||||
|
||||
for (auto& it : mouse)
|
||||
if (Input::released(it))
|
||||
return true;
|
||||
|
||||
for (auto& it : buttons)
|
||||
if (Input::released(it.controller, it.button))
|
||||
return true;
|
||||
|
||||
for (auto& it : triggers)
|
||||
{
|
||||
if (it.controller < 0 || it.controller >= Input::max_controllers)
|
||||
continue;
|
||||
|
||||
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]))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Binding::get_down() const
|
||||
{
|
||||
for (auto& it : keys)
|
||||
if (Input::down(it))
|
||||
return true;
|
||||
|
||||
for (auto& it : mouse)
|
||||
if (Input::down(it))
|
||||
return true;
|
||||
|
||||
for (auto& it : buttons)
|
||||
if (Input::down(it.controller, it.button))
|
||||
return true;
|
||||
|
||||
for (auto& it : triggers)
|
||||
{
|
||||
if (it.controller < 0 || it.controller >= Input::max_controllers)
|
||||
continue;
|
||||
|
||||
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]))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
float Binding::get_value() const
|
||||
{
|
||||
for (auto& it : keys)
|
||||
if (Input::down(it))
|
||||
return 1.0f;
|
||||
|
||||
for (auto& it : mouse)
|
||||
if (Input::down(it))
|
||||
return 1.0f;
|
||||
|
||||
for (auto& it : buttons)
|
||||
if (Input::down(it.controller, it.button))
|
||||
return 1.0f;
|
||||
|
||||
float highest = 0;
|
||||
|
||||
for (auto& it : triggers)
|
||||
{
|
||||
if (it.controller < 0 || it.controller >= Input::max_controllers)
|
||||
continue;
|
||||
|
||||
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];
|
||||
|
||||
if (it.is_down(raw_value))
|
||||
{
|
||||
float mapped_value = Calc::clamped_map(Calc::abs(raw_value), it.threshold, 1.0f, 0.0f, 1.0f);
|
||||
if (mapped_value > highest)
|
||||
highest = mapped_value;
|
||||
}
|
||||
}
|
||||
|
||||
return highest;
|
||||
}
|
||||
|
||||
float AxisBinding::value() const
|
||||
{
|
||||
float neg = negative.value();
|
||||
float pos = positive.value();
|
||||
|
||||
// neither are down
|
||||
if (neg <= 0 && pos <= 0)
|
||||
return 0;
|
||||
|
||||
// negative-only is down
|
||||
if (neg > 0 && pos <= 0)
|
||||
return -neg;
|
||||
|
||||
// positive-only is down
|
||||
if (pos > 0 && neg <= 0)
|
||||
return pos;
|
||||
|
||||
// both are down:
|
||||
|
||||
// overlap cancel out
|
||||
if (overlap == Overlap::Cancel)
|
||||
return 0;
|
||||
|
||||
// overlap takes older
|
||||
if (overlap == Overlap::Older)
|
||||
{
|
||||
if (negative.timestamp() < positive.timestamp())
|
||||
return -neg;
|
||||
else
|
||||
return pos;
|
||||
}
|
||||
|
||||
// overlap takes newer
|
||||
if (negative.timestamp() > positive.timestamp())
|
||||
return -neg;
|
||||
else
|
||||
return pos;
|
||||
}
|
||||
|
||||
int AxisBinding::sign() const
|
||||
{
|
||||
return (int)Calc::sign(value());
|
||||
}
|
||||
|
||||
void AxisBinding::update()
|
||||
{
|
||||
negative.update();
|
||||
positive.update();
|
||||
}
|
||||
|
||||
void AxisBinding::consume_press()
|
||||
{
|
||||
negative.consume_press();
|
||||
positive.consume_press();
|
||||
}
|
||||
|
||||
void AxisBinding::consume_release()
|
||||
{
|
||||
negative.consume_release();
|
||||
positive.consume_release();
|
||||
}
|
||||
|
||||
void AxisBinding::add_left_stick_x(int controller, float threshold)
|
||||
{
|
||||
negative.add(BoundTrigger::left_stick_left(controller, threshold));
|
||||
positive.add(BoundTrigger::left_stick_right(controller, threshold));
|
||||
}
|
||||
|
||||
void AxisBinding::add_left_stick_y(int controller, float threshold)
|
||||
{
|
||||
negative.add(BoundTrigger::left_stick_up(controller, threshold));
|
||||
positive.add(BoundTrigger::left_stick_down(controller, threshold));
|
||||
}
|
||||
|
||||
void AxisBinding::add_right_stick_x(int controller, float threshold)
|
||||
{
|
||||
negative.add(BoundTrigger::right_stick_left(controller, threshold));
|
||||
positive.add(BoundTrigger::right_stick_right(controller, threshold));
|
||||
}
|
||||
|
||||
void AxisBinding::add_right_stick_y(int controller, float threshold)
|
||||
{
|
||||
negative.add(BoundTrigger::right_stick_up(controller, threshold));
|
||||
positive.add(BoundTrigger::right_stick_down(controller, threshold));
|
||||
}
|
||||
|
||||
void AxisBinding::set_controller(int index)
|
||||
{
|
||||
negative.set_controller(index);
|
||||
positive.set_controller(index);
|
||||
}
|
||||
|
||||
void AxisBinding::clear()
|
||||
{
|
||||
negative.clear();
|
||||
positive.clear();
|
||||
}
|
||||
|
||||
Vec2 StickBinding::value() const
|
||||
{
|
||||
Vec2 result = Vec2(x.value(), y.value());
|
||||
if (round_threshold > 0 && result.length() < round_threshold)
|
||||
return Vec2::zero;
|
||||
return result;
|
||||
}
|
||||
|
||||
Point StickBinding::sign() const
|
||||
{
|
||||
Vec2 result = value();
|
||||
return Point((int)Calc::sign(result.x), (int)Calc::sign(result.y));
|
||||
}
|
||||
|
||||
void StickBinding::update()
|
||||
{
|
||||
x.update();
|
||||
y.update();
|
||||
}
|
||||
|
||||
void StickBinding::consume_press()
|
||||
{
|
||||
x.consume_press();
|
||||
y.consume_press();
|
||||
}
|
||||
|
||||
void StickBinding::consume_release()
|
||||
{
|
||||
x.consume_release();
|
||||
y.consume_release();
|
||||
}
|
||||
|
||||
void StickBinding::add_dpad(int controller)
|
||||
{
|
||||
x.negative.add(BoundButton(controller, Button::Left));
|
||||
x.positive.add(BoundButton(controller, Button::Right));
|
||||
y.negative.add(BoundButton(controller, Button::Up));
|
||||
y.positive.add(BoundButton(controller, Button::Down));
|
||||
}
|
||||
|
||||
void StickBinding::add_left_stick(int controller, float threshold)
|
||||
{
|
||||
x.add_left_stick_x(controller, threshold);
|
||||
y.add_left_stick_y(controller, threshold);
|
||||
}
|
||||
|
||||
void StickBinding::add_right_stick(int controller, float threshold)
|
||||
{
|
||||
x.add_right_stick_x(controller, threshold);
|
||||
y.add_right_stick_y(controller, threshold);
|
||||
}
|
||||
|
||||
void StickBinding::set_controller(int index)
|
||||
{
|
||||
x.set_controller(index);
|
||||
y.set_controller(index);
|
||||
}
|
||||
|
||||
void StickBinding::clear()
|
||||
{
|
||||
x.clear();
|
||||
y.clear();
|
||||
}
|
70
src/input/binding_registry.cpp
Normal file
70
src/input/binding_registry.cpp
Normal file
@ -0,0 +1,70 @@
|
||||
#include <blah/input/binding_registry.h>
|
||||
|
||||
using namespace Blah;
|
||||
|
||||
Vector<std::weak_ptr<Binding>> BindingRegistry::bindings;
|
||||
Vector<std::weak_ptr<AxisBinding>> BindingRegistry::axes;
|
||||
Vector<std::weak_ptr<StickBinding>> BindingRegistry::sticks;
|
||||
|
||||
BindingRef BindingRegistry::register_binding()
|
||||
{
|
||||
auto binding = std::make_shared<Binding>();
|
||||
bindings.push_back(std::weak_ptr<Binding>(binding));
|
||||
return binding;
|
||||
}
|
||||
|
||||
AxisBindingRef BindingRegistry::register_axis()
|
||||
{
|
||||
auto binding = std::make_shared<AxisBinding>();
|
||||
axes.push_back(std::weak_ptr<AxisBinding>(binding));
|
||||
return binding;
|
||||
}
|
||||
|
||||
StickBindingRef BindingRegistry::register_stick()
|
||||
{
|
||||
auto binding = std::make_shared<StickBinding>();
|
||||
sticks.push_back(std::weak_ptr<StickBinding>(binding));
|
||||
return binding;
|
||||
}
|
||||
|
||||
void BindingRegistry::update()
|
||||
{
|
||||
for (int i = 0; i < bindings.size(); i++)
|
||||
{
|
||||
if (bindings[i].use_count() <= 0)
|
||||
{
|
||||
bindings.erase(i);
|
||||
i--;
|
||||
}
|
||||
else if (auto binding = bindings[i].lock())
|
||||
{
|
||||
binding->update();
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < axes.size(); i++)
|
||||
{
|
||||
if (axes[i].use_count() <= 0)
|
||||
{
|
||||
axes.erase(i);
|
||||
i--;
|
||||
}
|
||||
else if (auto binding = axes[i].lock())
|
||||
{
|
||||
binding->update();
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < sticks.size(); i++)
|
||||
{
|
||||
if (sticks[i].use_count() <= 0)
|
||||
{
|
||||
sticks.erase(i);
|
||||
i--;
|
||||
}
|
||||
else if (auto binding = sticks[i].lock())
|
||||
{
|
||||
binding->update();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
#include <blah/input/input.h>
|
||||
#include <blah/input/binding_registry.h>
|
||||
#include <blah/core/app.h>
|
||||
#include <blah/core/time.h>
|
||||
#include <blah/core/common.h>
|
||||
@ -66,6 +67,9 @@ void InputBackend::frame()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// update bindings
|
||||
BindingRegistry::update();
|
||||
}
|
||||
|
||||
void InputBackend::on_mouse_move(float x, float y)
|
||||
|
@ -1,166 +0,0 @@
|
||||
#include <blah/input/virtual_axis.h>
|
||||
#include <blah/core/time.h>
|
||||
#include <blah/core/common.h>
|
||||
|
||||
using namespace Blah;
|
||||
|
||||
VirtualAxis& VirtualAxis::add_keys(Key negative, Key positive)
|
||||
{
|
||||
if (m_axes_len >= Input::max_virtual_nodes)
|
||||
BLAH_ERROR("VirtualAxis Keys out of bounds!");
|
||||
else
|
||||
{
|
||||
m_keys[m_keys_len].init(negative, positive);
|
||||
m_keys_len++;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
VirtualAxis& VirtualAxis::add_buttons(int gamepad_id, Button negative, Button positive)
|
||||
{
|
||||
if (m_axes_len >= Input::max_virtual_nodes)
|
||||
BLAH_ERROR("VirtualAxis Buttons out of bounds!");
|
||||
else
|
||||
{
|
||||
m_buttons[m_buttons_len].init(gamepad_id, negative, positive);
|
||||
m_buttons_len++;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
VirtualAxis& VirtualAxis::add_axis(int gamepad_id, Axis axis, float deadzone)
|
||||
{
|
||||
if (m_axes_len >= Input::max_virtual_nodes)
|
||||
BLAH_ERROR("VirtualAxis Axes out of bounds!");
|
||||
else
|
||||
{
|
||||
m_axes[m_axes_len].init(gamepad_id, axis, deadzone);
|
||||
m_axes_len++;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
VirtualAxis& VirtualAxis::repeat(float m_repeat_delay, float m_repeat_interval)
|
||||
{
|
||||
this->m_repeat_delay = m_repeat_delay;
|
||||
this->m_repeat_interval = m_repeat_interval;
|
||||
return *this;
|
||||
}
|
||||
|
||||
VirtualAxis& VirtualAxis::press_buffer(float duration)
|
||||
{
|
||||
this->m_press_buffer = duration;
|
||||
return *this;
|
||||
}
|
||||
|
||||
VirtualAxis& VirtualAxis::release_buffer(float duration)
|
||||
{
|
||||
this->m_release_buffer = duration;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void VirtualAxis::update()
|
||||
{
|
||||
m_last_value = m_value;
|
||||
m_value = 0;
|
||||
|
||||
for (int i = 0; i < m_keys_len; i++)
|
||||
{
|
||||
m_keys[i].update();
|
||||
if (m_value == 0)
|
||||
m_value = (float)m_keys[i].value;
|
||||
}
|
||||
|
||||
for (int i = 0; i < m_buttons_len; i++)
|
||||
{
|
||||
m_buttons[i].update();
|
||||
if (m_value == 0)
|
||||
m_value = (float)m_buttons[i].value;
|
||||
}
|
||||
|
||||
for (int i = 0; i < m_axes_len; i++)
|
||||
{
|
||||
m_axes[i].update();
|
||||
if (m_value == 0)
|
||||
m_value = m_axes[i].value;
|
||||
}
|
||||
|
||||
//Valuei
|
||||
m_last_value_i = m_value_i;
|
||||
if (m_value > 0)
|
||||
m_value_i = 1;
|
||||
else if (m_value < 0)
|
||||
m_value_i = -1;
|
||||
else
|
||||
m_value_i = 0;
|
||||
|
||||
//pressed?
|
||||
m_pressed = false;
|
||||
if (m_value_i != 0 && m_last_value_i != m_value_i)
|
||||
{
|
||||
m_pressed = true;
|
||||
m_last_press_time = m_repeat_press_time = Time::seconds;
|
||||
}
|
||||
else if (m_value_i == m_last_value_i && m_value_i != 0)
|
||||
{
|
||||
if (Time::seconds - m_last_press_time <= m_press_buffer)
|
||||
m_pressed = true;
|
||||
else if (m_repeat_interval > 0 && Time::seconds >= m_repeat_press_time + m_repeat_delay)
|
||||
{
|
||||
int prev = (int)((Time::previous_seconds - m_repeat_press_time - m_repeat_delay) / m_repeat_interval);
|
||||
int cur = (int)((Time::seconds - m_repeat_press_time - m_repeat_delay) / m_repeat_interval);
|
||||
m_pressed = prev < cur;
|
||||
}
|
||||
}
|
||||
|
||||
//released?
|
||||
if (m_last_value_i != 0 && m_value_i != m_last_value_i)
|
||||
{
|
||||
m_released = true;
|
||||
m_last_release_time = Time::seconds;
|
||||
}
|
||||
else if (Time::seconds - m_last_release_time <= m_release_buffer)
|
||||
m_released = true;
|
||||
else
|
||||
m_released = false;
|
||||
}
|
||||
|
||||
void VirtualAxis::KeysNode::init(Key negative, Key positive)
|
||||
{
|
||||
this->negative = negative;
|
||||
this->positive = positive;
|
||||
}
|
||||
|
||||
void VirtualAxis::KeysNode::update()
|
||||
{
|
||||
value = Input::axis_check(value, negative, positive);
|
||||
}
|
||||
|
||||
void VirtualAxis::ButtonsNode::init(int gamepad_id, Button negative, Button positive)
|
||||
{
|
||||
this->gamepad_id = gamepad_id;
|
||||
this->negative = negative;
|
||||
this->positive = positive;
|
||||
}
|
||||
|
||||
void VirtualAxis::ButtonsNode::update()
|
||||
{
|
||||
value = Input::axis_check(value, gamepad_id, negative, positive);
|
||||
}
|
||||
|
||||
void VirtualAxis::AxisNode::init(int gamepad_id, Axis axis, float deadzone)
|
||||
{
|
||||
this->gamepad_id = gamepad_id;
|
||||
this->axis = axis;
|
||||
this->deadzone = deadzone;
|
||||
}
|
||||
|
||||
void VirtualAxis::AxisNode::update()
|
||||
{
|
||||
value = Input::axis_check(gamepad_id, axis);
|
||||
if (value < deadzone && value > -deadzone)
|
||||
value = 0;
|
||||
}
|
@ -1,174 +0,0 @@
|
||||
#include <blah/input/virtual_button.h>
|
||||
#include <blah/core/time.h>
|
||||
#include <blah/core/common.h>
|
||||
|
||||
using namespace Blah;
|
||||
|
||||
VirtualButton& VirtualButton::add_key(Key key)
|
||||
{
|
||||
if (m_keys_len >= Input::max_virtual_nodes)
|
||||
BLAH_ERROR("VirtualButton Keys out of bounds!");
|
||||
else
|
||||
{
|
||||
m_keys[m_keys_len].init(key);
|
||||
m_keys_len++;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
VirtualButton& VirtualButton::add_button(int gamepad_id, Button button)
|
||||
{
|
||||
if (m_buttons_len >= Input::max_virtual_nodes)
|
||||
BLAH_ERROR("VirtualButton Buttons out of bounds!");
|
||||
else
|
||||
{
|
||||
m_buttons[m_buttons_len].init(gamepad_id, button);
|
||||
m_buttons_len++;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
VirtualButton& VirtualButton::add_axis(int gamepad_id, Axis axis, float threshold, bool greater_than)
|
||||
{
|
||||
if (m_axes_len >= Input::max_virtual_nodes)
|
||||
BLAH_ERROR("VirtualButton Axes out of bounds!");
|
||||
else
|
||||
{
|
||||
m_axes[m_axes_len].init(gamepad_id, axis, threshold, greater_than);
|
||||
m_axes_len++;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
VirtualButton& VirtualButton::repeat(float m_repeat_delay, float m_repeat_interval)
|
||||
{
|
||||
this->m_repeat_delay = m_repeat_delay;
|
||||
this->m_repeat_interval = m_repeat_interval;
|
||||
return *this;
|
||||
}
|
||||
|
||||
VirtualButton& VirtualButton::press_buffer(float duration)
|
||||
{
|
||||
this->m_press_buffer = duration;
|
||||
return *this;
|
||||
}
|
||||
|
||||
VirtualButton& VirtualButton::release_buffer(float duration)
|
||||
{
|
||||
this->m_release_buffer = duration;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void VirtualButton::update()
|
||||
{
|
||||
m_down = false;
|
||||
m_pressed = false;
|
||||
m_released = false;
|
||||
|
||||
// Keys
|
||||
for (int i = 0; i < m_keys_len; i++)
|
||||
{
|
||||
m_keys[i].update();
|
||||
|
||||
m_down = m_down || m_keys[i].down;
|
||||
m_pressed = m_pressed || m_keys[i].pressed;
|
||||
m_released = m_released || m_keys[i].released;
|
||||
}
|
||||
|
||||
// Buttons
|
||||
for (int i = 0; i < m_buttons_len; i++)
|
||||
{
|
||||
m_buttons[i].update();
|
||||
|
||||
m_down = m_down || m_buttons[i].down;
|
||||
m_pressed = m_pressed || m_buttons[i].pressed;
|
||||
m_released = m_released || m_buttons[i].released;
|
||||
}
|
||||
|
||||
// Axes
|
||||
for (int i = 0; i < m_axes_len; i++)
|
||||
{
|
||||
m_axes[i].update();
|
||||
|
||||
m_down = m_down || m_axes[i].down;
|
||||
m_pressed = m_pressed || m_axes[i].pressed;
|
||||
m_released = m_released || m_axes[i].released;
|
||||
}
|
||||
|
||||
// pressed?
|
||||
if (m_pressed)
|
||||
{
|
||||
m_repeat_press_time = m_last_press_time = Time::seconds;
|
||||
}
|
||||
else if (Time::seconds - m_last_press_time <= m_press_buffer)
|
||||
{
|
||||
m_pressed = true;
|
||||
}
|
||||
else if (m_down && m_repeat_interval > 0 && Time::seconds >= m_repeat_press_time + m_repeat_delay)
|
||||
{
|
||||
int prev = (int)((Time::previous_seconds - m_repeat_press_time - m_repeat_delay) / m_repeat_interval);
|
||||
int cur = (int)((Time::seconds - m_repeat_press_time - m_repeat_delay) / m_repeat_interval);
|
||||
m_pressed = prev < cur;
|
||||
}
|
||||
|
||||
// released?
|
||||
if (m_released)
|
||||
m_last_release_time = Time::seconds;
|
||||
else
|
||||
m_released = Time::seconds - m_last_release_time <= m_release_buffer;
|
||||
}
|
||||
|
||||
void VirtualButton::KeyNode::init(Key key)
|
||||
{
|
||||
this->key = key;
|
||||
}
|
||||
|
||||
void VirtualButton::KeyNode::update()
|
||||
{
|
||||
down = Input::down(key);
|
||||
pressed = Input::pressed(key);
|
||||
released = Input::released(key);
|
||||
}
|
||||
|
||||
void VirtualButton::ButtonNode::init(int gamepad_id, Button button)
|
||||
{
|
||||
this->gamepad_id = gamepad_id;
|
||||
this->button = button;
|
||||
}
|
||||
|
||||
void VirtualButton::ButtonNode::update()
|
||||
{
|
||||
down = Input::down(gamepad_id, button);
|
||||
pressed = Input::pressed(gamepad_id, button);
|
||||
released = Input::released(gamepad_id, button);
|
||||
}
|
||||
|
||||
void VirtualButton::AxisNode::init(int gamepad_id, Axis axis, float threshold, bool greater_than)
|
||||
{
|
||||
this->gamepad_id = gamepad_id;
|
||||
this->axis = axis;
|
||||
this->threshold = threshold;
|
||||
this->greater_than = greater_than;
|
||||
}
|
||||
|
||||
void VirtualButton::AxisNode::update()
|
||||
{
|
||||
float curr = Input::state()->controllers[gamepad_id].axis[(int)axis];
|
||||
float prev = Input::last_state()->controllers[gamepad_id].axis[(int)axis];
|
||||
|
||||
if (greater_than)
|
||||
{
|
||||
down = curr >= threshold;
|
||||
pressed = down && prev < threshold;
|
||||
released = !down && prev >= threshold;
|
||||
}
|
||||
else
|
||||
{
|
||||
down = curr <= threshold;
|
||||
pressed = down && prev > threshold;
|
||||
released = !down && prev <= threshold;
|
||||
}
|
||||
}
|
@ -1,191 +0,0 @@
|
||||
#include <blah/input/virtual_stick.h>
|
||||
#include <blah/core/time.h>
|
||||
#include <blah/core/common.h>
|
||||
|
||||
using namespace Blah;
|
||||
|
||||
VirtualStick::VirtualStick()
|
||||
{
|
||||
this->m_i_deadzone = 0;
|
||||
}
|
||||
|
||||
VirtualStick::VirtualStick(float iDeadzone)
|
||||
{
|
||||
this->m_i_deadzone = iDeadzone;
|
||||
}
|
||||
|
||||
VirtualStick& VirtualStick::add_keys(Key left, Key right, Key up, Key down)
|
||||
{
|
||||
if (m_keys_len >= Input::max_virtual_nodes)
|
||||
BLAH_ERROR("VirtualStick Keys out of bounds!");
|
||||
else
|
||||
{
|
||||
m_keys[m_keys_len].init(left, right, up, down);
|
||||
m_keys_len++;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
VirtualStick& VirtualStick::add_buttons(int gamepad_id, Button left, Button right, Button up, Button down)
|
||||
{
|
||||
if (m_buttons_len >= Input::max_virtual_nodes)
|
||||
BLAH_ERROR("VirtualStick Buttons out of bounds!");
|
||||
else
|
||||
{
|
||||
m_buttons[m_buttons_len].init(gamepad_id, left, right, up, down);
|
||||
m_buttons_len++;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
VirtualStick& VirtualStick::add_axes(int gamepad_id, Axis horizontal, Axis vertical, float deadzone)
|
||||
{
|
||||
if (m_axes_len >= Input::max_virtual_nodes)
|
||||
BLAH_ERROR("VirtualStick Axes out of bounds!");
|
||||
else
|
||||
{
|
||||
m_axes[m_axes_len].init(gamepad_id, horizontal, vertical, deadzone);
|
||||
m_axes_len++;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
VirtualStick& VirtualStick::repeat(float repeat_delay, float repeat_interval)
|
||||
{
|
||||
this->m_repeat_delay = repeat_delay;
|
||||
this->m_repeat_interval = repeat_interval;
|
||||
return *this;
|
||||
}
|
||||
|
||||
VirtualStick& VirtualStick::press_buffer(float duration)
|
||||
{
|
||||
m_press_buffer = duration;
|
||||
return *this;
|
||||
}
|
||||
|
||||
VirtualStick& VirtualStick::release_buffer(float duration)
|
||||
{
|
||||
m_release_buffer = duration;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void VirtualStick::update()
|
||||
{
|
||||
m_last_value = m_value;
|
||||
m_value = Vec2::zero;
|
||||
|
||||
for (int i = 0; i < m_keys_len; i++)
|
||||
{
|
||||
m_keys[i].update();
|
||||
if (m_value == Vec2::zero)
|
||||
m_value = m_keys[i].value;
|
||||
}
|
||||
|
||||
for (int i = 0; i < m_buttons_len; i++)
|
||||
{
|
||||
m_buttons[i].update();
|
||||
if (m_value == Vec2::zero)
|
||||
m_value = m_buttons[i].value;
|
||||
}
|
||||
|
||||
for (int i = 0; i < m_axes_len; i++)
|
||||
{
|
||||
m_axes[i].update();
|
||||
if (m_value == Vec2::zero)
|
||||
m_value = m_axes[i].value;
|
||||
}
|
||||
|
||||
//Valuei
|
||||
m_last_value_i = m_value_i;
|
||||
if (m_value.x > m_i_deadzone)
|
||||
m_value_i.x = 1;
|
||||
else if (m_value.x < -m_i_deadzone)
|
||||
m_value_i.x = -1;
|
||||
else
|
||||
m_value_i.x = 0;
|
||||
if (m_value.y > m_i_deadzone)
|
||||
m_value_i.y = 1;
|
||||
else if (m_value.y < -m_i_deadzone)
|
||||
m_value_i.y = -1;
|
||||
else
|
||||
m_value_i.y = 0;
|
||||
|
||||
//pressed?
|
||||
m_pressed = false;
|
||||
if (m_value_i != Point::zero && m_last_value_i != m_value_i)
|
||||
{
|
||||
m_pressed = true;
|
||||
m_last_press_time = m_repeat_press_time = Time::seconds;
|
||||
}
|
||||
else if (m_value_i == m_last_value_i && m_value_i != Point::zero)
|
||||
{
|
||||
if (Time::seconds - m_last_press_time <= m_press_buffer)
|
||||
m_pressed = true;
|
||||
else if (m_repeat_interval > 0 && Time::seconds >= m_repeat_press_time + m_repeat_delay)
|
||||
{
|
||||
int prev = (int)((Time::previous_seconds - m_repeat_press_time - m_repeat_delay) / m_repeat_interval);
|
||||
int cur = (int)((Time::seconds - m_repeat_press_time - m_repeat_delay) / m_repeat_interval);
|
||||
m_pressed = prev < cur;
|
||||
}
|
||||
}
|
||||
|
||||
//released?
|
||||
if (m_last_value_i != Point::zero && m_value_i != m_last_value_i)
|
||||
{
|
||||
m_released = true;
|
||||
m_last_release_time = Time::seconds;
|
||||
}
|
||||
else if (Time::seconds - m_last_release_time <= m_release_buffer)
|
||||
m_released = true;
|
||||
else
|
||||
m_released = false;
|
||||
}
|
||||
|
||||
void VirtualStick::KeysNode::init(Key left, Key right, Key up, Key down)
|
||||
{
|
||||
this->left = left;
|
||||
this->right = right;
|
||||
this->up = up;
|
||||
this->down = down;
|
||||
}
|
||||
|
||||
void VirtualStick::KeysNode::update()
|
||||
{
|
||||
value.x = Input::axis_check(value.x, left, right);
|
||||
value.y = Input::axis_check(value.y, up, down);
|
||||
}
|
||||
|
||||
void VirtualStick::ButtonsNode::init(int gamepad_id, Button left, Button right, Button up, Button down)
|
||||
{
|
||||
this->gamepad_id = gamepad_id;
|
||||
this->left = left;
|
||||
this->right = right;
|
||||
this->up = up;
|
||||
this->down = down;
|
||||
}
|
||||
|
||||
void VirtualStick::ButtonsNode::update()
|
||||
{
|
||||
value.x = Input::axis_check(value.x, gamepad_id, left, right);
|
||||
value.y = Input::axis_check(value.y, gamepad_id, up, down);
|
||||
}
|
||||
|
||||
void VirtualStick::AxesNode::init(int gamepad_id, Axis horizontal, Axis vertical, float deadzone)
|
||||
{
|
||||
this->gamepad_id = gamepad_id;
|
||||
this->horizontal = horizontal;
|
||||
this->vertical = vertical;
|
||||
this->deadzone = deadzone;
|
||||
}
|
||||
|
||||
void VirtualStick::AxesNode::update()
|
||||
{
|
||||
value.x = Input::axis_check(gamepad_id, horizontal);
|
||||
value.y = Input::axis_check(gamepad_id, vertical);
|
||||
|
||||
if (value.length() < deadzone)
|
||||
value = Vec2::zero;
|
||||
}
|
Reference in New Issue
Block a user