Skip to content

Commit aeba022

Browse files
author
Semphris
committed
Moved some code to JoystickManager
1 parent ca6c404 commit aeba022

File tree

4 files changed

+89
-37
lines changed

4 files changed

+89
-37
lines changed

src/control/input_manager.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,7 @@ InputManager::pop_user()
154154
void
155155
InputManager::on_player_removed(int player_id)
156156
{
157-
auto& map = joystick_manager->get_joystick_mapping();
158-
auto it = std::find_if(map.begin(), map.end(), [player_id](std::remove_reference<decltype(map)>::type::const_reference pair) { return pair.second == player_id; });
159-
if (it != map.end())
160-
it->second = -1;
161-
157+
joystick_manager->on_player_removed(player_id);
162158
game_controller_manager->on_player_removed(player_id);
163159
}
164160

@@ -171,10 +167,7 @@ InputManager::has_corresponsing_controller(int player_id) const
171167
}
172168
else
173169
{
174-
auto& map = joystick_manager->get_joystick_mapping();
175-
return std::find_if(map.begin(), map.end(), [player_id](std::remove_reference<decltype(map)>::type::const_reference pair) {
176-
return pair.second == player_id;
177-
}) != map.end();
170+
return joystick_manager->has_corresponding_joystick(player_id);
178171
}
179172
}
180173

src/control/joystick_manager.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,66 @@ JoystickManager::set_joy_controls(SDL_JoystickID joystick, Control id, bool valu
303303
parent->get_controller(it->second).set_control(id, value);
304304
}
305305

306+
void
307+
JoystickManager::on_player_removed(int player_id)
308+
{
309+
auto it2 = std::find_if(joysticks.begin(), joysticks.end(), [player_id](decltype(joysticks)::const_reference pair) {
310+
return pair.second == player_id;
311+
});
312+
if (it2 != joysticks.end())
313+
{
314+
it2->second = -1;
315+
// Try again, in case multiple controllers were bount to a player
316+
// Recursive call shouldn't go too deep except in hardcore scenarios
317+
on_player_removed(player_id);
318+
}
319+
}
320+
321+
bool
322+
JoystickManager::has_corresponding_joystick(int player_id) const
323+
{
324+
return std::find_if(joysticks.begin(), joysticks.end(), [player_id](decltype(joysticks)::const_reference pair) {
325+
return pair.second == player_id;
326+
}) != joysticks.end();
327+
}
328+
329+
int
330+
JoystickManager::rumble(SDL_Joystick* controller) const
331+
{
332+
#if SDL_VERSION_ATLEAST(2, 0, 9)
333+
if (g_config->multiplayer_buzz_controllers)
334+
{
335+
#if SDL_VERSION_ATLEAST(2, 0, 18)
336+
if (SDL_JoystickHasRumble(controller))
337+
{
338+
#endif
339+
// TODO: Rumble intensity setting (like volume)
340+
SDL_JoystickRumble(controller, 0xFFFF, 0xFFFF, 300);
341+
#if SDL_VERSION_ATLEAST(2, 0, 18)
342+
}
343+
else
344+
{
345+
return 1;
346+
}
347+
#endif
348+
}
349+
350+
return 0;
351+
#else
352+
return 2;
353+
#endif
354+
}
355+
356+
void
357+
JoystickManager::bind_joystick(SDL_Joystick* controller, int player_id)
358+
{
359+
joysticks[controller] = player_id;
360+
361+
if (!g_config->multiplayer_multibind)
362+
for (auto& pair2 : joysticks)
363+
if (pair2.second == player_id && pair2.first != controller)
364+
pair2.second = -1;
365+
}
366+
367+
306368
/* EOF */

src/control/joystick_manager.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ class JoystickManager final
5454

5555
int get_num_joysticks() const { return static_cast<int>(joysticks.size()); }
5656

57+
void on_player_removed(int player_id);
58+
bool has_corresponding_joystick(int player_id) const;
59+
60+
/** @returns 0 if success, 1 if controller doesn't support rumbling, 2 if game doesn't support rumbling */
61+
int rumble(SDL_Joystick* joystick) const;
62+
63+
void bind_joystick(SDL_Joystick* joystick, int player_id);
64+
5765
std::unordered_map<SDL_Joystick*, int>& get_joystick_mapping() { return joysticks; }
5866

5967
private:

src/supertux/menu/multiplayer_player_menu.cpp

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -175,38 +175,27 @@ MultiplayerPlayerMenu::MultiplayerPlayerMenu(int player_id)
175175
std::string prefix = (pair.second == -1) ? "" : (pair.second == player_id) ? "-> " : ("[" + std::to_string(pair.second + 1) + "] ");
176176

177177
add_entry(prefix + std::string(SDL_JoystickName(pair.first)), [joystick, player_id] {
178-
InputManager::current()->joystick_manager->get_joystick_mapping()[joystick] = player_id;
178+
InputManager::current()->joystick_manager->bind_joystick(joystick, player_id);
179179

180-
if (!g_config->multiplayer_multibind)
181-
for (auto& pair2 : InputManager::current()->joystick_manager->get_joystick_mapping())
182-
if (pair2.second == player_id && pair2.first != joystick)
183-
pair2.second = -1;
180+
auto err = InputManager::current()->joystick_manager->rumble(joystick);
181+
switch (err)
182+
{
183+
case 1:
184+
Dialog::show_message(_("This joystick does not support rumbling;"
185+
"\nplease check the joysticks manually."));
186+
break;
184187

185-
MenuManager::instance().set_menu(std::make_unique<MultiplayerPlayerMenu>(player_id));
188+
case 2:
189+
Dialog::show_message(_("This SuperTux build does not support "
190+
"rumbling\njoysticks; please check the "
191+
"joysticks manually."));
192+
break;
186193

187-
#if SDL_VERSION_ATLEAST(2, 0, 9)
188-
if (g_config->multiplayer_buzz_controllers)
189-
{
190-
#if SDL_VERSION_ATLEAST(2, 0, 18)
191-
if (SDL_JoystickHasRumbleTriggers(joystick))
192-
{
193-
#endif
194-
// TODO: Rumble intensity setting (like volume)
195-
SDL_JoystickRumble(joystick, 0xFFFF, 0xFFFF, 300);
196-
#if SDL_VERSION_ATLEAST(2, 0, 18)
197-
}
198-
else
199-
{
200-
Dialog::show_message(_("This joystick does not support rumbling;\n"
201-
"please check the joysticks manually."));
202-
}
203-
#endif
194+
default:
195+
break;
204196
}
205-
#else
206-
Dialog::show_message(_("This SuperTux build does not support rumbling\n"
207-
"joysticks; please check the joysticks manually.\n"
208-
"\nYou may disable this message in the Options menu."));
209-
#endif
197+
198+
MenuManager::instance().set_menu(std::make_unique<MultiplayerPlayerMenu>(player_id));
210199
});
211200
}
212201
}

0 commit comments

Comments
 (0)