mirror of
https://github.com/NoelFB/blah.git
synced 2024-11-25 16:18:57 +08:00
simplifying Input enums
This commit is contained in:
parent
53ebb40ffd
commit
832c8f4283
|
@ -229,33 +229,23 @@ namespace Blah
|
|||
}
|
||||
|
||||
// Keyboard Keys
|
||||
struct Keys
|
||||
{
|
||||
enum Enumeration
|
||||
enum class Key
|
||||
{
|
||||
#define DEFINE_KEY(name, value) name = value,
|
||||
BLAH_KEY_DEFINITIONS
|
||||
#undef DEFINE_KEY
|
||||
};
|
||||
};
|
||||
using Key = Keys::Enumeration;
|
||||
|
||||
// Game Controller Buttons
|
||||
struct Buttons
|
||||
{
|
||||
enum Enumeration
|
||||
enum class Button
|
||||
{
|
||||
#define DEFINE_BTN(name, value) name = value,
|
||||
BLAH_BUTTON_DEFINITIONS
|
||||
#undef DEFINE_BTN
|
||||
};
|
||||
};
|
||||
using Button = Buttons::Enumeration;
|
||||
|
||||
// Game Controller Axis
|
||||
struct Axes
|
||||
{
|
||||
enum Enumeration
|
||||
enum class Axis
|
||||
{
|
||||
None = -1,
|
||||
LeftX = 0,
|
||||
|
@ -265,21 +255,15 @@ namespace Blah
|
|||
LeftTrigger = 4,
|
||||
RightTrigger = 5,
|
||||
};
|
||||
};
|
||||
using Axis = Axes::Enumeration;
|
||||
|
||||
// Mouse Buttons
|
||||
struct MouseButtons
|
||||
{
|
||||
enum Enumeration
|
||||
enum class MouseButton
|
||||
{
|
||||
None = -1,
|
||||
Left = 0,
|
||||
Middle = 1,
|
||||
Right = 2,
|
||||
};
|
||||
};
|
||||
using MouseButton = MouseButtons::Enumeration;
|
||||
|
||||
// Controller State
|
||||
struct ControllerState
|
||||
|
@ -781,6 +765,15 @@ namespace Blah
|
|||
// Checks if the Left or Right Alt Key is down
|
||||
bool alt();
|
||||
|
||||
// Checks if the given Controller Button is pressed
|
||||
bool pressed(int controller_index, Button button);
|
||||
|
||||
// Checks if the given Controller Button is down
|
||||
bool down(int controller_index, Button button);
|
||||
|
||||
// Checks if the given Controller Button is released
|
||||
bool released(int controller_index, Button button);
|
||||
|
||||
// returns a string name of the key
|
||||
const char* name_of(Key key);
|
||||
|
||||
|
|
136
src/input.cpp
136
src/input.cpp
|
@ -133,39 +133,43 @@ void MouseState::on_move(const Vec2f& pos, const Vec2f& screen_pos)
|
|||
|
||||
void MouseState::on_press(MouseButton button)
|
||||
{
|
||||
if (button >= 0 && button < Input::max_mouse_buttons)
|
||||
int index = (int)button;
|
||||
if (index >= 0 && index < Input::max_mouse_buttons)
|
||||
{
|
||||
down[button] = true;
|
||||
pressed[button] = true;
|
||||
timestamp[button] = Time::ticks;
|
||||
down[index] = true;
|
||||
pressed[index] = true;
|
||||
timestamp[index] = Time::ticks;
|
||||
}
|
||||
}
|
||||
|
||||
void MouseState::on_release(MouseButton button)
|
||||
{
|
||||
if (button >= 0 && button < Input::max_mouse_buttons)
|
||||
int index = (int)button;
|
||||
if (index >= 0 && index < Input::max_mouse_buttons)
|
||||
{
|
||||
down[button] = false;
|
||||
released[button] = true;
|
||||
down[index] = false;
|
||||
released[index] = true;
|
||||
}
|
||||
}
|
||||
|
||||
void KeyboardState::on_press(Key key)
|
||||
{
|
||||
if (key >= 0 && key < Input::max_keyboard_keys)
|
||||
int index = (int)key;
|
||||
if (index >= 0 && index < Input::max_keyboard_keys)
|
||||
{
|
||||
down[key] = true;
|
||||
pressed[key] = true;
|
||||
timestamp[key] = Time::ticks;
|
||||
down[index] = true;
|
||||
pressed[index] = true;
|
||||
timestamp[index] = Time::ticks;
|
||||
}
|
||||
}
|
||||
|
||||
void KeyboardState::on_release(Key key)
|
||||
{
|
||||
if (key >= 0 && key < Input::max_keyboard_keys)
|
||||
int index = (int)key;
|
||||
if (index >= 0 && index < Input::max_keyboard_keys)
|
||||
{
|
||||
down[key] = false;
|
||||
released[key] = true;
|
||||
down[index] = false;
|
||||
released[index] = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -189,25 +193,28 @@ void ControllerState::on_disconnect()
|
|||
|
||||
void ControllerState::on_press(Button button)
|
||||
{
|
||||
if (button >= 0 && button < Input::max_controller_buttons && button < button_count)
|
||||
int index = (int)button;
|
||||
if (index >= 0 && index < Input::max_controller_buttons && index < button_count)
|
||||
{
|
||||
down[button] = true;
|
||||
pressed[button] = true;
|
||||
button_timestamp[button] = Time::ticks;
|
||||
down[index] = true;
|
||||
pressed[index] = true;
|
||||
button_timestamp[index] = Time::ticks;
|
||||
}
|
||||
}
|
||||
|
||||
void ControllerState::on_release(Button button)
|
||||
{
|
||||
if (button >= 0 && button < Input::max_controller_buttons && button < button_count)
|
||||
int index = (int)button;
|
||||
if (index >= 0 && index < Input::max_controller_buttons && index < button_count)
|
||||
{
|
||||
down[button] = false;
|
||||
released[button] = true;
|
||||
down[index] = false;
|
||||
released[index] = true;
|
||||
}
|
||||
}
|
||||
|
||||
void ControllerState::on_axis(Axis index, float value)
|
||||
void ControllerState::on_axis(Axis input_axis, float value)
|
||||
{
|
||||
int index = (int)input_axis;
|
||||
if (index >= 0 && index < Input::max_controller_axis && index < axis_count)
|
||||
{
|
||||
axis[index] = value;
|
||||
|
@ -217,17 +224,17 @@ void ControllerState::on_axis(Axis index, float value)
|
|||
|
||||
bool KeyboardState::ctrl()
|
||||
{
|
||||
return down[Key::LeftControl] || down[Key::RightControl];
|
||||
return down[(int)Key::LeftControl] || down[(int)Key::RightControl];
|
||||
}
|
||||
|
||||
bool KeyboardState::shift()
|
||||
{
|
||||
return down[Key::LeftShift] || down[Key::RightShift];
|
||||
return down[(int)Key::LeftShift] || down[(int)Key::RightShift];
|
||||
}
|
||||
|
||||
bool KeyboardState::alt()
|
||||
{
|
||||
return down[Key::LeftAlt] || down[Key::RightAlt];
|
||||
return down[(int)Key::LeftAlt] || down[(int)Key::RightAlt];
|
||||
}
|
||||
|
||||
Vec2f Input::mouse()
|
||||
|
@ -247,17 +254,20 @@ Vec2f Input::mouse_screen()
|
|||
|
||||
bool Input::pressed(MouseButton button)
|
||||
{
|
||||
return state.mouse.pressed[button];
|
||||
int index = (int)button;
|
||||
return index >= 0 && index < Input::max_mouse_buttons && state.mouse.pressed[index];
|
||||
}
|
||||
|
||||
bool Input::down(MouseButton button)
|
||||
{
|
||||
return state.mouse.down[button];
|
||||
int index = (int)button;
|
||||
return index >= 0 && index < Input::max_mouse_buttons && state.mouse.down[index];
|
||||
}
|
||||
|
||||
bool Input::released(MouseButton button)
|
||||
{
|
||||
return state.mouse.released[button];
|
||||
int index = (int)button;
|
||||
return index >= 0 && index < Input::max_mouse_buttons&& state.mouse.released[index];
|
||||
}
|
||||
|
||||
Point Input::mouse_wheel()
|
||||
|
@ -267,27 +277,33 @@ Point Input::mouse_wheel()
|
|||
|
||||
bool Input::pressed(Key key)
|
||||
{
|
||||
return state.keyboard.pressed[key];
|
||||
int index = (int)key;
|
||||
return index >= 0 && index < Input::max_keyboard_keys && state.keyboard.pressed[index];
|
||||
}
|
||||
|
||||
bool Input::down(Key key)
|
||||
{
|
||||
return state.keyboard.down[key];
|
||||
int index = (int)key;
|
||||
return index >= 0 && index < Input::max_keyboard_keys&& state.keyboard.down[index];
|
||||
}
|
||||
|
||||
bool Input::released(Key key)
|
||||
{
|
||||
return state.keyboard.released[key];
|
||||
int index = (int)key;
|
||||
return index >= 0 && index < Input::max_keyboard_keys&& state.keyboard.released[index];
|
||||
}
|
||||
|
||||
bool Input::repeating(Key key)
|
||||
{
|
||||
if (state.keyboard.pressed[key])
|
||||
int index = (int)key;
|
||||
if (index >= 0 && index < Input::max_keyboard_keys)
|
||||
{
|
||||
if (state.keyboard.pressed[index])
|
||||
return true;
|
||||
|
||||
if (state.keyboard.down[key])
|
||||
if (state.keyboard.down[index])
|
||||
{
|
||||
double timestamp = state.keyboard.timestamp[key] / (double)Time::ticks_per_second;
|
||||
double timestamp = state.keyboard.timestamp[index] / (double)Time::ticks_per_second;
|
||||
double current_time = Time::ticks / (double)Time::ticks_per_second;
|
||||
|
||||
if (current_time > timestamp + Input::repeat_delay)
|
||||
|
@ -296,6 +312,7 @@ bool Input::repeating(Key key)
|
|||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
@ -315,6 +332,33 @@ bool Input::alt()
|
|||
return state.keyboard.alt();
|
||||
}
|
||||
|
||||
bool Input::pressed(int controller_index, Button button)
|
||||
{
|
||||
int index = (int)button;
|
||||
return
|
||||
controller_index >= 0 && controller_index < Input::max_controllers &&
|
||||
index >= 0 && index < Input::max_controller_buttons &&
|
||||
state.controllers[controller_index].pressed[index];
|
||||
}
|
||||
|
||||
bool Input::down(int controller_index, Button button)
|
||||
{
|
||||
int index = (int)button;
|
||||
return
|
||||
controller_index >= 0 && controller_index < Input::max_controllers&&
|
||||
index >= 0 && index < Input::max_controller_buttons&&
|
||||
state.controllers[controller_index].down[index];
|
||||
}
|
||||
|
||||
bool Input::released(int controller_index, Button button)
|
||||
{
|
||||
int index = (int)button;
|
||||
return
|
||||
controller_index >= 0 && controller_index < Input::max_controllers&&
|
||||
index >= 0 && index < Input::max_controller_buttons&&
|
||||
state.controllers[controller_index].released[index];
|
||||
}
|
||||
|
||||
const char* Input::name_of(Key key)
|
||||
{
|
||||
switch (key)
|
||||
|
@ -542,15 +586,15 @@ void ButtonBinding::clear()
|
|||
bool ButtonBinding::get_pressed() const
|
||||
{
|
||||
for (auto& it : keys)
|
||||
if (Input::state.keyboard.pressed[it])
|
||||
if (Input::pressed(it))
|
||||
return true;
|
||||
|
||||
for (auto& it : mouse)
|
||||
if (Input::state.mouse.pressed[it])
|
||||
if (Input::pressed(it))
|
||||
return true;
|
||||
|
||||
for (auto& it : buttons)
|
||||
if (Input::state.controllers[it.controller].pressed[it.button])
|
||||
if (Input::pressed(it.controller, it.button))
|
||||
return true;
|
||||
|
||||
for (auto& it : triggers)
|
||||
|
@ -572,15 +616,15 @@ bool ButtonBinding::get_pressed() const
|
|||
bool ButtonBinding::get_released() const
|
||||
{
|
||||
for (auto& it : keys)
|
||||
if (Input::state.keyboard.released[it])
|
||||
if (Input::released(it))
|
||||
return true;
|
||||
|
||||
for (auto& it : mouse)
|
||||
if (Input::state.mouse.released[it])
|
||||
if (Input::released(it))
|
||||
return true;
|
||||
|
||||
for (auto& it : buttons)
|
||||
if (Input::state.controllers[it.controller].released[it.button])
|
||||
if (Input::released(it.controller, it.button))
|
||||
return true;
|
||||
|
||||
for (auto& it : triggers)
|
||||
|
@ -602,15 +646,15 @@ bool ButtonBinding::get_released() const
|
|||
bool ButtonBinding::get_down() const
|
||||
{
|
||||
for (auto& it : keys)
|
||||
if (Input::state.keyboard.down[it])
|
||||
if (Input::down(it))
|
||||
return true;
|
||||
|
||||
for (auto& it : mouse)
|
||||
if (Input::state.mouse.down[it])
|
||||
if (Input::down(it))
|
||||
return true;
|
||||
|
||||
for (auto& it : buttons)
|
||||
if (Input::state.controllers[it.controller].down[it.button])
|
||||
if (Input::down(it.controller, it.button))
|
||||
return true;
|
||||
|
||||
for (auto& it : triggers)
|
||||
|
@ -631,15 +675,15 @@ bool ButtonBinding::get_down() const
|
|||
float ButtonBinding::get_value() const
|
||||
{
|
||||
for (auto& it : keys)
|
||||
if (Input::state.keyboard.down[it])
|
||||
if (Input::down(it))
|
||||
return 1.0f;
|
||||
|
||||
for (auto& it : mouse)
|
||||
if (Input::state.mouse.down[it])
|
||||
if (Input::down(it))
|
||||
return 1.0f;
|
||||
|
||||
for (auto& it : buttons)
|
||||
if (Input::state.controllers[it.controller].down[it.button])
|
||||
if (Input::down(it.controller, it.button))
|
||||
return 1.0f;
|
||||
|
||||
float highest = 0;
|
||||
|
|
Loading…
Reference in New Issue
Block a user