Unravel Engine C++ Reference
Loading...
Searching...
No Matches
os_gamepad.cpp
Go to the documentation of this file.
1#include "os_gamepad.hpp"
2#include <cmath>
3#include <logging/logging.h>
4
5namespace input
6{
7
8// ----------------------------------------------------------------------------
9os_gamepad::os_gamepad(int index) : index_(index), name_("Gamepad" + std::to_string(index_ + 1))
10{
11}
12
13// ----------------------------------------------------------------------------
18
20{
21 device_ = os::gamepad::open_device(index_);
22
23 if(device_.data)
24 {
25 name_ = os::gamepad::get_device_name(device_);
26
27 APPLOG_INFO("Joystick connected ({}).", name_);
28 }
29}
30
32{
33 if(device_.data)
34 {
35 os::gamepad::close_device(device_);
36
37 APPLOG_WARNING("Joystick disconnected ({}).", name_);
38 device_ = {};
39 }
40}
41
43{
44 refresh_device_ = true;
45}
46
47// ----------------------------------------------------------------------------
49{
50 if(refresh_device_)
51 {
53 }
54
55 // If we never opened or it failed, try again or bail:
56 if(!device_.data)
57 {
59 }
60
61 refresh_device_ = false;
62
63 if(!device_.data)
64 {
65 // Clear states if it’s still not available
66 button_state_map_.clear();
67 axis_map_.clear();
68
69 return;
70 }
71
72 // 1) Update the button_state_map to handle transitions
73 button_state_map_.update();
74
75 uint32_t buttons_count = os::gamepad::get_buttons_count(device_);
76 for(uint32_t b = 0; b < buttons_count; ++b)
77 {
78 auto state = os::gamepad::get_button_state(device_, b);
79
81 if(state == os::gamepad::button_state::pressed)
82 {
84 }
85
86 button_state_map_.set_state(b, bs);
87 }
88
89 uint32_t axis_count = os::gamepad::get_axis_count(device_);
90 for(int a = 0; a < axis_count; ++a)
91 {
92 float normalized = os::gamepad::get_axis_value_normalized(device_, a);
93
94 axis_map_[a] = normalized;
95 }
96}
97
98// ----------------------------------------------------------------------------
99auto os_gamepad::get_axis_value(uint32_t axis) const -> float
100{
101 if(!is_input_allowed())
102 {
103 return 0.0f;
104 }
105 auto it = axis_map_.find(axis);
106 if(it == axis_map_.end())
107 {
108 return 0.0f;
109 }
110
111 return it->second;
112}
113
114// ----------------------------------------------------------------------------
116{
117 return button_state_map_.get_state(button, button_state::up);
118}
119
120// ----------------------------------------------------------------------------
121auto os_gamepad::get_name() const -> const std::string&
122{
123 return name_;
124}
125
126// ----------------------------------------------------------------------------
127auto os_gamepad::is_connected() const -> bool
128{
129 return os::gamepad::is_device_connected(device_);
130}
131
132// ----------------------------------------------------------------------------
133auto os_gamepad::is_down(uint32_t button) const -> bool
134{
135 if(!is_input_allowed())
136 return false;
137
138 button_state st = button_state_map_.get_state(button, button_state::up);
139 return (st == button_state::down || st == button_state::pressed);
140}
141
142// ----------------------------------------------------------------------------
143auto os_gamepad::is_pressed(uint32_t button) const -> bool
144{
145 if(!is_input_allowed())
146 return false;
147
148 return button_state_map_.get_state(button, button_state::up) == button_state::pressed;
149}
150
151// ----------------------------------------------------------------------------
152auto os_gamepad::is_released(uint32_t button) const -> bool
153{
154 if(!is_input_allowed())
155 return false;
156
157 return button_state_map_.get_state(button, button_state::up) == button_state::released;
158}
159
160} // namespace input
void set_state(uint32_t button, button_state state)
auto is_released(uint32_t button) const -> bool override
auto is_connected() const -> bool override
auto is_pressed(uint32_t button) const -> bool override
~os_gamepad() override
auto is_down(uint32_t button) const -> bool override
auto get_button_state(uint32_t button) const -> button_state override
auto get_axis_value(uint32_t axis) const -> float override
auto get_name() const -> const std::string &override
os_gamepad(int index)
Definition os_gamepad.cpp:9
#define APPLOG_WARNING(...)
Definition logging.h:19
#define APPLOG_INFO(...)
Definition logging.h:18