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