joystick_menu.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // SuperTux
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>,
  3. // 2007,2014 Ingo Ruhnke <grumbel@gmail.com>
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #include "supertux/menu/joystick_menu.hpp"
  18. #include <sstream>
  19. #include "control/joystick_manager.hpp"
  20. #include "gui/item_controlfield.hpp"
  21. #include "gui/item_toggle.hpp"
  22. #include "supertux/gameconfig.hpp"
  23. #include "supertux/globals.hpp"
  24. #include "util/gettext.hpp"
  25. namespace {
  26. enum {
  27. MNID_JUMP_WITH_UP = static_cast<int>(Control::CONTROLCOUNT),
  28. MNID_SCAN_JOYSTICKS,
  29. MNID_AUTO_JOYSTICK_CFG
  30. };
  31. } // namespace
  32. JoystickMenu::JoystickMenu(InputManager& input_manager) :
  33. m_input_manager(input_manager),
  34. m_joysticks_available(false),
  35. m_auto_joystick_cfg(!m_input_manager.use_game_controller())
  36. {
  37. recreate_menu();
  38. }
  39. JoystickMenu::~JoystickMenu()
  40. {}
  41. void
  42. JoystickMenu::recreate_menu()
  43. {
  44. clear();
  45. add_label(_("Setup Joystick"));
  46. add_hl();
  47. add_toggle(MNID_AUTO_JOYSTICK_CFG, _("Manual Configuration"),
  48. &m_auto_joystick_cfg)
  49. .set_help(_("Use manual configuration instead of SDL2's automatic GameController support"));
  50. if (m_input_manager.use_game_controller())
  51. {
  52. m_joysticks_available = false;
  53. }
  54. else
  55. {
  56. if (m_input_manager.joystick_manager->get_num_joysticks() > 0)
  57. {
  58. m_joysticks_available = true;
  59. add_controlfield(static_cast<int>(Control::UP), _("Up"));
  60. add_controlfield(static_cast<int>(Control::DOWN), _("Down"));
  61. add_controlfield(static_cast<int>(Control::LEFT), _("Left"));
  62. add_controlfield(static_cast<int>(Control::RIGHT), _("Right"));
  63. add_controlfield(static_cast<int>(Control::JUMP), _("Jump"));
  64. add_controlfield(static_cast<int>(Control::ACTION), _("Action"));
  65. add_controlfield(static_cast<int>(Control::START), _("Pause/Menu"));
  66. add_controlfield(static_cast<int>(Control::PEEK_LEFT), _("Peek Left"));
  67. add_controlfield(static_cast<int>(Control::PEEK_RIGHT), _("Peek Right"));
  68. add_controlfield(static_cast<int>(Control::PEEK_UP), _("Peek Up"));
  69. add_controlfield(static_cast<int>(Control::PEEK_DOWN), _("Peek Down"));
  70. if (g_config->developer_mode) {
  71. add_controlfield(static_cast<int>(Control::CONSOLE), _("Console"));
  72. add_controlfield(static_cast<int>(Control::CHEAT_MENU), _("Cheat Menu"));
  73. add_controlfield(static_cast<int>(Control::DEBUG_MENU), _("Debug Menu"));
  74. }
  75. add_toggle(MNID_JUMP_WITH_UP, _("Jump with Up"), &g_config->joystick_config.m_jump_with_up_joy);
  76. }
  77. else
  78. {
  79. m_joysticks_available = false;
  80. add_inactive(_("No Joysticks found"));
  81. add_entry(MNID_SCAN_JOYSTICKS, _("Scan for Joysticks"));
  82. }
  83. }
  84. add_hl();
  85. add_back(_("Back"));
  86. refresh();
  87. }
  88. std::string
  89. JoystickMenu::get_button_name(int button) const
  90. {
  91. if (button < 0)
  92. {
  93. return _("None");
  94. }
  95. else
  96. {
  97. std::ostringstream name;
  98. name << "Button " << button;
  99. return name.str();
  100. }
  101. }
  102. void
  103. JoystickMenu::menu_action(MenuItem& item)
  104. {
  105. if (0 <= item.get_id() && item.get_id() < static_cast<int>(Control::CONTROLCOUNT))
  106. {
  107. ItemControlField* micf = dynamic_cast<ItemControlField*>(&item);
  108. if (!micf) {
  109. return;
  110. }
  111. micf->change_input(_("Press Button"));
  112. m_input_manager.joystick_manager->bind_next_event_to(static_cast<Control>(item.get_id()));
  113. }
  114. else if (item.get_id() == MNID_AUTO_JOYSTICK_CFG)
  115. {
  116. //m_input_manager.use_game_controller(!item.toggled);
  117. m_input_manager.use_game_controller(!m_auto_joystick_cfg);
  118. m_input_manager.reset();
  119. recreate_menu();
  120. }
  121. else if (item.get_id() == MNID_SCAN_JOYSTICKS)
  122. {
  123. m_input_manager.reset();
  124. recreate_menu();
  125. }
  126. }
  127. void
  128. JoystickMenu::refresh_menu_item(Control id)
  129. {
  130. ItemControlField* itemcf = dynamic_cast<ItemControlField*>(&get_item_by_id(static_cast<int>(id)));
  131. if (!itemcf) {
  132. return;
  133. }
  134. int button = g_config->joystick_config.reversemap_joybutton(id);
  135. int axis = g_config->joystick_config.reversemap_joyaxis(id);
  136. int hat_dir = g_config->joystick_config.reversemap_joyhat(id);
  137. if (button != -1)
  138. {
  139. itemcf->change_input(get_button_name(button));
  140. }
  141. else if (axis != 0)
  142. {
  143. std::ostringstream name;
  144. name << _("Axis ");
  145. if (axis < 0)
  146. name << _("-");
  147. else
  148. name << _("+");
  149. if (abs(axis) == 1)
  150. name << _("X");
  151. else if (abs(axis) == 2)
  152. name << _("Y");
  153. else if (abs(axis) == 3)
  154. name << _("X2");
  155. else if (abs(axis) == 4)
  156. name << _("Y2");
  157. else
  158. name << abs(axis);
  159. itemcf->change_input(name.str());
  160. }
  161. else if (hat_dir != -1)
  162. {
  163. std::string name;
  164. switch (hat_dir)
  165. {
  166. case SDL_HAT_UP:
  167. name = _("Hat Up");
  168. break;
  169. case SDL_HAT_DOWN:
  170. name = _("Hat Down");
  171. break;
  172. case SDL_HAT_LEFT:
  173. name = _("Hat Left");
  174. break;
  175. case SDL_HAT_RIGHT:
  176. name = _("Hat Right");
  177. break;
  178. default:
  179. name = "Unknown hat_dir";
  180. break;
  181. }
  182. itemcf->change_input(name);
  183. }
  184. else
  185. {
  186. itemcf->change_input(_("None"));
  187. }
  188. }
  189. void
  190. JoystickMenu::refresh()
  191. {
  192. if (m_joysticks_available)
  193. {
  194. refresh_menu_item(Control::UP);
  195. refresh_menu_item(Control::DOWN);
  196. refresh_menu_item(Control::LEFT);
  197. refresh_menu_item(Control::RIGHT);
  198. refresh_menu_item(Control::JUMP);
  199. refresh_menu_item(Control::ACTION);
  200. refresh_menu_item(Control::START);
  201. refresh_menu_item(Control::PEEK_LEFT);
  202. refresh_menu_item(Control::PEEK_RIGHT);
  203. refresh_menu_item(Control::PEEK_UP);
  204. refresh_menu_item(Control::PEEK_DOWN);
  205. if (g_config->developer_mode) {
  206. refresh_menu_item(Control::CONSOLE);
  207. refresh_menu_item(Control::CHEAT_MENU);
  208. refresh_menu_item(Control::DEBUG_MENU);
  209. }
  210. }
  211. }
  212. /* EOF */