summaryrefslogtreecommitdiffstats
path: root/src/game/client/io/gamepad.hh
blob: ff6af4a770e34d14828015554b61678187a2677b (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// SPDX-License-Identifier: BSD-2-Clause
// Copyright (c) 2025 Kirill Dmitrievich
// File: gamepad.hh
// Description: Gamepad support

#ifndef CLIENT_IO_GAMEPAD_HH
#define CLIENT_IO_GAMEPAD_HH
#pragma once

constexpr static int INVALID_GAMEPAD_AXIS = INT_MAX;
constexpr static int INVALID_GAMEPAD_BUTTON = INT_MAX;

// This simulates buttons using axes. When an axis
// value exceeds 1.5 times the deadzone, the event is
// queued with a GLFW_PRESS action, when it falls back
// below the threshold, the event is queued with GLFW_RELEASE action
class GamepadAxisEvent final {
public:
    constexpr explicit GamepadAxisEvent(int axis, int action);

    constexpr int axis(void) const noexcept;
    constexpr int action(void) const noexcept;

    constexpr bool is_axis(int axis) const noexcept;
    constexpr bool is_action(int action) const noexcept;

private:
    int m_axis;
    int m_action;
};

// This smears GLFW event sugar over gamepad polling
// system. Whenever it detects a state change, the event
// is queued with an appropriate action
class GamepadButtonEvent final {
public:
    constexpr explicit GamepadButtonEvent(int button, int action);

    constexpr int button(void) const noexcept;
    constexpr int action(void) const noexcept;

    constexpr bool is_button(int button) const noexcept;
    constexpr bool is_action(int action) const noexcept;

private:
    int m_action;
    int m_button;
};

namespace config
{
class Boolean;
class Float;
} // namespace config

struct GLFWgamepadstate;

namespace gamepad
{
extern bool available;
extern config::Float deadzone;
extern config::Boolean active;
extern GLFWgamepadstate state;
extern GLFWgamepadstate last_state;
} // namespace gamepad

namespace gamepad
{
void init(void);
void update_late(void);
} // namespace gamepad

constexpr GamepadAxisEvent::GamepadAxisEvent(int axis, int action) : m_axis(axis), m_action(action)
{
    // empty
}

constexpr int GamepadAxisEvent::axis(void) const noexcept
{
    return m_axis;
}

constexpr int GamepadAxisEvent::action(void) const noexcept
{
    return m_action;
}

constexpr bool GamepadAxisEvent::is_axis(int axis) const noexcept
{
    return m_axis == axis;
}

constexpr bool GamepadAxisEvent::is_action(int action) const noexcept
{
    return m_action == action;
}

constexpr GamepadButtonEvent::GamepadButtonEvent(int button, int action) : m_button(button), m_action(action)
{
    // empty
}

constexpr int GamepadButtonEvent::button(void) const noexcept
{
    return m_button;
}

constexpr int GamepadButtonEvent::action(void) const noexcept
{
    return m_action;
}

constexpr bool GamepadButtonEvent::is_button(int button) const noexcept
{
    return m_button == button;
}

constexpr bool GamepadButtonEvent::is_action(int action) const noexcept
{
    return m_action == action;
}

#endif