💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

313 lines
9.5 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "game/states/keyboard-config-menu.hpp"
  20. #include "game/states/controls-menu.hpp"
  21. #include "application.hpp"
  22. #include "scene/text.hpp"
  23. #include "render/passes/clear-pass.hpp"
  24. #include "debug/logger.hpp"
  25. #include "resources/resource-manager.hpp"
  26. #include "game/menu.hpp"
  27. #include "game/controls.hpp"
  28. #include "animation/timeline.hpp"
  29. namespace game {
  30. namespace state {
  31. namespace keyboard_config_menu {
  32. static std::string get_binding_string(game::context* ctx, input::control* control)
  33. {
  34. std::string binding_string;
  35. auto mappings = ctx->input_event_router->get_mappings(control);
  36. for (input::mapping* mapping: *mappings)
  37. {
  38. std::string mapping_string;
  39. switch (mapping->get_type())
  40. {
  41. case input::mapping_type::key:
  42. {
  43. const input::key_mapping* key_mapping = static_cast<const input::key_mapping*>(mapping);
  44. const char* scancode_name = input::keyboard::get_scancode_name(key_mapping->scancode);
  45. mapping_string = scancode_name;
  46. break;
  47. }
  48. case input::mapping_type::mouse_wheel:
  49. {
  50. const input::mouse_wheel_mapping* wheel_mapping = static_cast<const input::mouse_wheel_mapping*>(mapping);
  51. switch (wheel_mapping->axis)
  52. {
  53. case input::mouse_wheel_axis::negative_x:
  54. mapping_string = (*ctx->strings)["mouse_wheel_left"];
  55. break;
  56. case input::mouse_wheel_axis::positive_x:
  57. mapping_string = (*ctx->strings)["mouse_wheel_right"];
  58. break;
  59. case input::mouse_wheel_axis::negative_y:
  60. mapping_string = (*ctx->strings)["mouse_wheel_down"];
  61. break;
  62. case input::mouse_wheel_axis::positive_y:
  63. mapping_string = (*ctx->strings)["mouse_wheel_up"];
  64. break;
  65. default:
  66. break;
  67. }
  68. break;
  69. }
  70. case input::mapping_type::mouse_button:
  71. {
  72. const input::mouse_button_mapping* button_mapping = static_cast<const input::mouse_button_mapping*>(mapping);
  73. if (button_mapping->button == 1)
  74. {
  75. mapping_string = (*ctx->strings)["mouse_button_left"];
  76. }
  77. else if (button_mapping->button == 2)
  78. {
  79. mapping_string = (*ctx->strings)["mouse_button_middle"];
  80. }
  81. else if (button_mapping->button == 3)
  82. {
  83. mapping_string = (*ctx->strings)["mouse_button_right"];
  84. }
  85. else
  86. {
  87. const std::string& format = (*ctx->strings)["mouse_button_n"];
  88. char buffer[64];
  89. std::snprintf(buffer, 64, format.c_str(), button_mapping->button);
  90. mapping_string = buffer;
  91. }
  92. break;
  93. }
  94. default:
  95. break;
  96. }
  97. if (!mapping_string.empty())
  98. {
  99. if (binding_string.empty())
  100. {
  101. binding_string = mapping_string;
  102. }
  103. else
  104. {
  105. binding_string += " " + mapping_string;
  106. }
  107. }
  108. }
  109. return binding_string;
  110. }
  111. static void add_control_item(game::context* ctx, const std::string& control_name)
  112. {
  113. // Get pointer to control
  114. input::control* control = ctx->controls[control_name];
  115. // Construct texts
  116. scene::text* name_text = new scene::text();
  117. scene::text* value_text = new scene::text();
  118. // Add texts to list of menu item texts
  119. ctx->menu_item_texts.push_back({name_text, value_text});
  120. // Set content of name text
  121. std::string string_name = "control_" + control_name;
  122. if (auto it = ctx->strings->find(string_name); it != ctx->strings->end())
  123. name_text->set_content(it->second);
  124. else
  125. name_text->set_content(control_name);
  126. // Set content of value text
  127. value_text->set_content(get_binding_string(ctx, control));
  128. auto select_callback = [ctx, control, value_text]()
  129. {
  130. // Clear binding string from value text
  131. value_text->set_content((*ctx->strings)["ellipsis"]);
  132. game::menu::align_text(ctx);
  133. game::menu::update_text_tweens(ctx);
  134. // Disable controls
  135. game::menu::clear_controls(ctx);
  136. // Remove keyboard and mouse event mappings from control
  137. ctx->input_event_router->remove_mappings(control, input::mapping_type::key);
  138. ctx->input_event_router->remove_mappings(control, input::mapping_type::mouse_motion);
  139. ctx->input_event_router->remove_mappings(control, input::mapping_type::mouse_wheel);
  140. ctx->input_event_router->remove_mappings(control, input::mapping_type::mouse_button);
  141. // Setup input binding listener
  142. ctx->input_listener->set_callback
  143. (
  144. [ctx, control, value_text](const event_base& event)
  145. {
  146. auto id = event.get_event_type_id();
  147. if (id == key_pressed_event::event_type_id)
  148. {
  149. // Map key pressed event to control
  150. const key_pressed_event& key_event = static_cast<const key_pressed_event&>(event);
  151. if (key_event.scancode != input::scancode::escape && key_event.scancode != input::scancode::backspace)
  152. ctx->input_event_router->add_mapping(input::key_mapping(control, key_event.keyboard, key_event.scancode));
  153. }
  154. else if (id == mouse_wheel_scrolled_event::event_type_id)
  155. {
  156. // Map mouse wheel scrolled event to control
  157. const mouse_wheel_scrolled_event& wheel_event = static_cast<const mouse_wheel_scrolled_event&>(event);
  158. input::mouse_wheel_axis axis;
  159. if (wheel_event.x < 0)
  160. axis = input::mouse_wheel_axis::negative_x;
  161. else if (wheel_event.x > 0)
  162. axis = input::mouse_wheel_axis::positive_x;
  163. else if (wheel_event.y < 0)
  164. axis = input::mouse_wheel_axis::negative_y;
  165. else if (wheel_event.y > 0)
  166. axis = input::mouse_wheel_axis::positive_y;
  167. else
  168. return;
  169. ctx->input_event_router->add_mapping(input::mouse_wheel_mapping(control, wheel_event.mouse, axis));
  170. }
  171. else if (id == mouse_button_pressed_event::event_type_id)
  172. {
  173. // Map mouse button pressed event to control
  174. const mouse_button_pressed_event& button_event = static_cast<const mouse_button_pressed_event&>(event);
  175. ctx->input_event_router->add_mapping(input::mouse_button_mapping(control, button_event.mouse, button_event.button));
  176. }
  177. else
  178. {
  179. return;
  180. }
  181. // Update menu text
  182. value_text->set_content(get_binding_string(ctx, control));
  183. game::menu::align_text(ctx);
  184. game::menu::update_text_tweens(ctx);
  185. // Disable input listener
  186. ctx->input_listener->set_enabled(false);
  187. ctx->input_listener->set_callback(nullptr);
  188. // Schedule re-enabling of menu controls
  189. timeline* timeline = ctx->timeline;
  190. float t = timeline->get_position();
  191. timeline->add_sequence({{t + game::menu::input_delay, std::bind(game::menu::setup_controls, ctx)}});
  192. }
  193. );
  194. ctx->input_listener->set_enabled(true);
  195. };
  196. // Register menu item callbacks
  197. ctx->menu_select_callbacks.push_back(select_callback);
  198. ctx->menu_left_callbacks.push_back(nullptr);
  199. ctx->menu_right_callbacks.push_back(nullptr);
  200. }
  201. void enter(game::context* ctx)
  202. {
  203. ctx->ui_clear_pass->set_cleared_buffers(true, true, false);
  204. // Add camera control menu items
  205. add_control_item(ctx, "move_forward");
  206. add_control_item(ctx, "move_back");
  207. add_control_item(ctx, "move_left");
  208. add_control_item(ctx, "move_right");
  209. add_control_item(ctx, "move_up");
  210. add_control_item(ctx, "move_down");
  211. // Add application control menu items
  212. add_control_item(ctx, "toggle_fullscreen");
  213. add_control_item(ctx, "screenshot");
  214. // Construct menu item texts
  215. scene::text* back_text = new scene::text();
  216. // Build list of menu item texts
  217. ctx->menu_item_texts.push_back({back_text, nullptr});
  218. // Set content of menu item texts
  219. back_text->set_content((*ctx->strings)["back"]);
  220. // Init menu item index
  221. game::menu::init_menu_item_index(ctx, "keyboard_config");
  222. game::menu::update_text_color(ctx);
  223. game::menu::update_text_font(ctx);
  224. game::menu::align_text(ctx);
  225. game::menu::update_text_tweens(ctx);
  226. game::menu::add_text_to_ui(ctx);
  227. // Construct menu item callbacks
  228. auto select_back_callback = [ctx]()
  229. {
  230. application::state next_state;
  231. next_state.name = "controls_menu";
  232. next_state.enter = std::bind(game::state::controls_menu::enter, ctx);
  233. next_state.exit = std::bind(game::state::controls_menu::exit, ctx);
  234. ctx->app->change_state(next_state);
  235. };
  236. // Build list of menu select callbacks
  237. ctx->menu_select_callbacks.push_back(select_back_callback);
  238. // Build list of menu left callbacks
  239. ctx->menu_left_callbacks.push_back(nullptr);
  240. // Build list of menu right callbacks
  241. ctx->menu_right_callbacks.push_back(nullptr);
  242. // Set menu back callback
  243. ctx->menu_back_callback = select_back_callback;
  244. // Schedule menu control setup
  245. timeline* timeline = ctx->timeline;
  246. float t = timeline->get_position();
  247. timeline->add_sequence({{t + game::menu::input_delay, std::bind(game::menu::setup_controls, ctx)}});
  248. }
  249. void exit(game::context* ctx)
  250. {
  251. // Destruct menu
  252. game::menu::clear_controls(ctx);
  253. game::menu::clear_callbacks(ctx);
  254. game::menu::remove_text_from_ui(ctx);
  255. game::menu::delete_text(ctx);
  256. // Save control profile
  257. game::save_control_profile(ctx);
  258. ctx->ui_clear_pass->set_cleared_buffers(false, true, false);
  259. }
  260. } // namespace keyboard_config_menu
  261. } // namespace state
  262. } // namespace game