summaryrefslogtreecommitdiffstats
path: root/game/client/config/gamepad_button.cc
blob: b983baaae3b192473e03a03c9421bb5c4f1e2f26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "client/pch.hh"

#include "client/config/gamepad_button.hh"

#include "core/math/constexpr.hh"

#include "client/io/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) {
            return it.second;
        }
    }

    return UNKNOWN_BUTTON_NAME;
}

config::GamepadButton::GamepadButton(void)
{
    m_gamepad_button = io::INVALID_GAMEPAD_BUTTON;
    m_name = UNKNOWN_BUTTON_NAME;
}

config::GamepadButton::GamepadButton(int button)
{
    m_gamepad_button = button;
    m_name = get_button_name(button);
}

const char* config::GamepadButton::get(void) const
{
    return m_name;
}

void config::GamepadButton::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 = io::INVALID_GAMEPAD_BUTTON;
    m_name = UNKNOWN_BUTTON_NAME;
}

int config::GamepadButton::get_button(void) const
{
    return m_gamepad_button;
}

void config::GamepadButton::set_button(int button)
{
    m_gamepad_button = button;
    m_name = get_button_name(button);
}

bool config::GamepadButton::equals(int button) const
{
    return m_gamepad_button == button;
}

bool config::GamepadButton::is_pressed(const GLFWgamepadstate& state) const
{
    return m_gamepad_button < math::array_size(state.buttons) && state.buttons[m_gamepad_button] == GLFW_PRESS;
}