summaryrefslogtreecommitdiffstats
path: root/game/client/gamepad_button.cc
diff options
context:
space:
mode:
authoruntodesu <kirill@untode.su>2025-03-15 16:22:09 +0500
committeruntodesu <kirill@untode.su>2025-03-15 16:22:09 +0500
commit3bf42c6ff3805a0d42bbc661794a95ff31bedc26 (patch)
tree05049955847504808d6bed2bb7b155f8b03807bb /game/client/gamepad_button.cc
parent02294547dcde0d4ad76e229106702261e9f10a51 (diff)
downloadvoxelius-3bf42c6ff3805a0d42bbc661794a95ff31bedc26.tar.bz2
voxelius-3bf42c6ff3805a0d42bbc661794a95ff31bedc26.zip
Add whatever I was working on for the last month
Diffstat (limited to 'game/client/gamepad_button.cc')
-rw-r--r--game/client/gamepad_button.cc91
1 files changed, 91 insertions, 0 deletions
diff --git a/game/client/gamepad_button.cc b/game/client/gamepad_button.cc
new file mode 100644
index 0000000..e22f7e7
--- /dev/null
+++ b/game/client/gamepad_button.cc
@@ -0,0 +1,91 @@
+#include "client/pch.hh"
+#include "client/gamepad_button.hh"
+
+#include "core/constexpr.hh"
+
+#include "client/gamepad.hh"
+
+constexpr static const char *UNKNOWN_BUTTON_NAME = "UNKNOWN";
+
+static const std::pair<int, const char *> button_names[] = {
+ { GLFW_GAMEPAD_BUTTON_A, "A" },
+ { GLFW_GAMEPAD_BUTTON_B, "B" },
+ { GLFW_GAMEPAD_BUTTON_X, "X" },
+ { GLFW_GAMEPAD_BUTTON_Y, "Y" },
+ { GLFW_GAMEPAD_BUTTON_LEFT_BUMPER, "L_BUMP" },
+ { GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER, "R_BUMP" },
+ { GLFW_GAMEPAD_BUTTON_BACK, "BACK" },
+ { GLFW_GAMEPAD_BUTTON_START, "START" },
+ { GLFW_GAMEPAD_BUTTON_GUIDE, "GUIDE" },
+ { GLFW_GAMEPAD_BUTTON_LEFT_THUMB, "L_THUMB" },
+ { GLFW_GAMEPAD_BUTTON_RIGHT_THUMB, "R_THUMB" },
+ { GLFW_GAMEPAD_BUTTON_DPAD_UP, "DPAD_UP" },
+ { GLFW_GAMEPAD_BUTTON_DPAD_RIGHT, "DPAD_RIGHT" },
+ { GLFW_GAMEPAD_BUTTON_DPAD_DOWN, "DPAD_DOWN" },
+ { GLFW_GAMEPAD_BUTTON_DPAD_LEFT, "DPAD_LEFT" },
+};
+
+static const char *get_button_name(int button)
+{
+ for(const auto &it : button_names) {
+ if(it.first != button)
+ continue;
+ return it.second;
+ }
+
+ return UNKNOWN_BUTTON_NAME;
+}
+
+ConfigGamepadButton::ConfigGamepadButton(void)
+{
+ m_gamepad_button = INVALID_GAMEPAD_BUTTON;
+ m_name = UNKNOWN_BUTTON_NAME;
+}
+
+ConfigGamepadButton::ConfigGamepadButton(int button)
+{
+ m_gamepad_button = button;
+ m_name = get_button_name(button);
+}
+
+const char *ConfigGamepadButton::get(void) const
+{
+ return m_name;
+}
+
+void ConfigGamepadButton::set(const char *value)
+{
+ for(const auto &it : button_names) {
+ if(!std::strcmp(it.second, value)) {
+ m_gamepad_button = it.first;
+ m_name = it.second;
+ return;
+ }
+ }
+
+ m_gamepad_button = INVALID_GAMEPAD_BUTTON;
+ m_name = UNKNOWN_BUTTON_NAME;
+}
+
+int ConfigGamepadButton::get_button(void) const
+{
+ return m_gamepad_button;
+}
+
+void ConfigGamepadButton::set_button(int button)
+{
+ m_gamepad_button = button;
+ m_name = get_button_name(button);
+}
+
+bool ConfigGamepadButton::equals(int button) const
+{
+ return m_gamepad_button == button;
+}
+
+bool ConfigGamepadButton::is_pressed(const GLFWgamepadstate &state) const
+{
+ if((m_gamepad_button < cxpr::array_size(state.buttons)) && (state.buttons[m_gamepad_button] == GLFW_PRESS))
+ return true;
+ return false;
+}