Skip to content

Commit ad8045e

Browse files
committed
pad_handler: Implement support for registering and updating analog input actions
1 parent 9ecd150 commit ad8045e

File tree

3 files changed

+90
-13
lines changed

3 files changed

+90
-13
lines changed

code/host/host_interface_base.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,13 @@ namespace host
140140
m_pad_handler->register_button_action(btn_action, input_buttons, callback);
141141
}
142142

143-
// TODO: Register analog actions
143+
// registers an action associated with an analog axis
144+
inline void pad_register_button_action(const pad_handler::analog_action ana_action, pad_handler::analog axis, std::function<void(bool pressed)> callback = nullptr)
145+
{
146+
std::lock_guard<std::mutex> lk(m_pad_handler_mutex);
147+
148+
m_pad_handler->register_analog_action(ana_action, axis, callback);
149+
}
144150

145151
// sets a callback to be called when the value of an action changes
146152
inline void pad_set_button_action_callback(const pad_handler::button_action btn_action, std::function<void(bool pressed)> callback)

code/host/pad_handler.cc

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,50 @@ namespace host
3737
register_button_action(btn_action, input_button, callback);
3838
}
3939

40-
// TODO:
41-
//void register_analog_action(const std::string_view& action, ... axis);
40+
// Register an analog action which corresponds to an input
41+
void pad_handler::register_analog_action(const analog_action ana_action, analog analog_axis, std::function<void(f32 axis)> callback)
42+
{
43+
axis axis_xy;
44+
f32 axis_val;
45+
46+
// Poll the current analog value
47+
switch (analog_axis)
48+
{
49+
case analog::left_x:
50+
case analog::left_y:
51+
axis_xy = left_stick_axis();
52+
if (analog_axis == analog::left_x)
53+
{
54+
axis_val = axis_xy.x;
55+
}
56+
else
57+
{
58+
axis_val = axis_xy.y;
59+
}
60+
break;
61+
case analog::right_x:
62+
case analog::right_y:
63+
axis_xy = right_stick_axis();
64+
if (analog_axis == analog::right_x)
65+
{
66+
axis_val = axis_xy.x;
67+
}
68+
else
69+
{
70+
axis_val = axis_xy.y;
71+
}
72+
break;
73+
default:
74+
panicf("Unrecognized analog value {}", common::to_underlying(analog_axis));
75+
return;
76+
}
77+
78+
m_analog_actions[ana_action] = {
79+
.m_value = axis_val,
80+
.m_input_key = analog_axis,
81+
.m_callback = callback
82+
};
83+
}
4284

4385
// Set a callback for a button action
4486
void pad_handler::set_button_action_callback(const button_action btn_action, std::function<void(bool pressed)> callback)
@@ -118,7 +160,39 @@ namespace host
118160
action.m_value = pressed;
119161
}
120162

121-
// TODO: Update analog
163+
// Update analog
164+
auto left_axis = left_stick_axis();
165+
auto right_axis = right_stick_axis();
166+
167+
for (auto& kv : m_analog_actions)
168+
{
169+
auto& action = kv.second;
170+
171+
switch (action.m_input_key)
172+
{
173+
case analog::left_x:
174+
action.m_value = left_axis.x;
175+
break;
176+
case analog::left_y:
177+
action.m_value = left_axis.y;
178+
break;
179+
case analog::right_x:
180+
action.m_value = right_axis.x;
181+
break;
182+
case analog::right_y:
183+
action.m_value = right_axis.y;
184+
}
185+
186+
// Currently we defer a callback to execute every frame for analog actions
187+
if (action.m_callback != nullptr)
188+
{
189+
m_analog_action_callbacks.push_back(
190+
{
191+
.m_value = action.m_value,
192+
.m_callback = action.m_callback
193+
});
194+
}
195+
}
122196
}
123197

124198
void pad_handler::do_action_callbacks()

code/host/pad_handler.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ namespace host
4545
enum class analog : u32
4646
{
4747
// TODO: Check if these properly align with the values in analog_table @ 0x3353E0
48-
left_horizonal = 1,
49-
left_vertical = 2,
50-
right_horizontal = 3,
51-
right_vertical = 4,
48+
left_x = 1,
49+
left_y = 2,
50+
right_x = 3,
51+
right_y = 4,
5252
};
5353

5454
// These values come from pad_table @ 0x3351B0
@@ -186,11 +186,8 @@ namespace host
186186

187187
// TODO: Unregistering (this one commit is getting too large anyways)
188188

189-
// TODO:
190-
/*
191189
// Register an analog action which corresponds to an input
192-
void register_analog_action(const std::string_view& action, ... axis);
193-
*/
190+
void register_analog_action(const analog_action ana_action, analog analog_axis, std::function<void(f32 axis)> callback = nullptr);
194191

195192
// Set a callback for a button action
196193
void set_button_action_callback(const button_action btn_action, std::function<void(bool pressed)> callback);
@@ -241,7 +238,7 @@ namespace host
241238
struct action_analog
242239
{
243240
f32 m_value{ 0.0f };
244-
// TODO: axis representation, need to do some digging in the game to match up values
241+
analog m_input_key{};
245242
// TODO: Consider callback behavior for analog inputs; should it be called every update?
246243
std::function<void(f32 axis)> m_callback{ nullptr };
247244
};

0 commit comments

Comments
 (0)