mirror of
https://github.com/NoelFB/blah.git
synced 2024-11-28 16:58:57 +08:00
added have virtual input code
This commit is contained in:
parent
2653baed64
commit
1a8a0e0c03
|
@ -1,4 +1,167 @@
|
|||
#pragma once
|
||||
#include <blah/input/virtual_axis.h>
|
||||
#include <blah/time.h>
|
||||
#include <blah/log.h>
|
||||
|
||||
using namespace Blah;
|
||||
using namespace Blah;
|
||||
|
||||
VirtualAxis& VirtualAxis::add_keys(Key negative, Key positive)
|
||||
{
|
||||
if (m_axes_len >= BLAH_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 >= BLAH_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 >= BLAH_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::elapsed;
|
||||
}
|
||||
else if (m_value_i == m_last_value_i && m_value_i != 0)
|
||||
{
|
||||
if (Time::elapsed - m_last_press_time <= m_press_buffer)
|
||||
m_pressed = true;
|
||||
else if (m_repeat_interval > 0 && Time::elapsed >= m_repeat_press_time + m_repeat_delay)
|
||||
{
|
||||
int prev = (int)((Time::previous_elapsed - m_repeat_press_time - m_repeat_delay) / m_repeat_interval);
|
||||
int cur = (int)((Time::elapsed - 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::elapsed;
|
||||
}
|
||||
else if (Time::elapsed - 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,4 +1,175 @@
|
|||
#pragma once
|
||||
#include <blah/input/virtual_button.h>
|
||||
#include <blah/time.h>
|
||||
#include <blah/log.h>
|
||||
|
||||
using namespace Blah;
|
||||
using namespace Blah;
|
||||
|
||||
VirtualButton& VirtualButton::add_key(Key key)
|
||||
{
|
||||
if (m_keys_len >= BLAH_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 >= BLAH_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 >= BLAH_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::elapsed;
|
||||
}
|
||||
else if (Time::elapsed - m_last_press_time <= m_press_buffer)
|
||||
{
|
||||
m_pressed = true;
|
||||
}
|
||||
else if (m_down && m_repeat_interval > 0 && Time::elapsed >= m_repeat_press_time + m_repeat_delay)
|
||||
{
|
||||
int prev = (int)((Time::previous_elapsed - m_repeat_press_time - m_repeat_delay) / m_repeat_interval);
|
||||
int cur = (int)((Time::elapsed - m_repeat_press_time - m_repeat_delay) / m_repeat_interval);
|
||||
m_pressed = prev < cur;
|
||||
}
|
||||
|
||||
//released?
|
||||
if (m_released)
|
||||
m_last_release_time = Time::elapsed;
|
||||
else
|
||||
m_released = Time::elapsed - 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,4 +1,192 @@
|
|||
#pragma once
|
||||
#include <blah/input/virtual_stick.h>
|
||||
#include <blah/time.h>
|
||||
#include <blah/log.h>
|
||||
|
||||
using namespace Blah;
|
||||
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 >= BLAH_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 >= BLAH_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 >= BLAH_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::elapsed;
|
||||
}
|
||||
else if (m_value_i == m_last_value_i && m_value_i != Point::zero)
|
||||
{
|
||||
if (Time::elapsed - m_last_press_time <= m_press_buffer)
|
||||
m_pressed = true;
|
||||
else if (m_repeat_interval > 0 && Time::elapsed >= m_repeat_press_time + m_repeat_delay)
|
||||
{
|
||||
int prev = (int)((Time::previous_elapsed - m_repeat_press_time - m_repeat_delay) / m_repeat_interval);
|
||||
int cur = (int)((Time::elapsed - 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::elapsed;
|
||||
}
|
||||
else if (Time::elapsed - 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user