💿🐜 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.

294 lines
9.1 KiB

  1. /*
  2. * Copyright (C) 2023 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-state.hpp"
  20. #include "game/states/controls-menu-state.hpp"
  21. #include "game/controls.hpp"
  22. #include <engine/scene/text.hpp>
  23. #include <engine/debug/log.hpp>
  24. #include <engine/resources/resource-manager.hpp>
  25. #include "game/menu.hpp"
  26. #include "game/controls.hpp"
  27. #include "game/strings.hpp"
  28. #include <engine/utility/hash/fnv1a.hpp>
  29. #include <format>
  30. #include <utility>
  31. using namespace hash::literals;
  32. keyboard_config_menu_state::keyboard_config_menu_state(::game& ctx):
  33. game_state(ctx),
  34. action_remapped(false)
  35. {
  36. debug::log_trace("Entering keyboard config menu state...");
  37. // Add control menu items
  38. add_control_item(ctx.movement_action_map, ctx.move_forward_action, "control_move_forward");
  39. add_control_item(ctx.movement_action_map, ctx.move_back_action, "control_move_back");
  40. add_control_item(ctx.movement_action_map, ctx.move_left_action, "control_move_left");
  41. add_control_item(ctx.movement_action_map, ctx.move_right_action, "control_move_right");
  42. add_control_item(ctx.movement_action_map, ctx.move_up_action, "control_move_up");
  43. add_control_item(ctx.movement_action_map, ctx.move_down_action, "control_move_down");
  44. add_control_item(ctx.movement_action_map, ctx.pause_action, "control_pause");
  45. // Construct menu item texts
  46. back_text = std::make_unique<scene::text>();
  47. // Build list of menu item texts
  48. ctx.menu_item_texts.push_back({back_text.get(), nullptr});
  49. // Set content of menu item texts
  50. back_text->set_content(get_string(ctx, "back"));
  51. // Init menu item index
  52. ::menu::init_menu_item_index(ctx, "keyboard_config");
  53. ::menu::update_text_color(ctx);
  54. ::menu::update_text_font(ctx);
  55. ::menu::align_text(ctx);
  56. ::menu::add_text_to_ui(ctx);
  57. ::menu::setup_animations(ctx);
  58. // Construct menu item callbacks
  59. auto select_back_callback = [&ctx]()
  60. {
  61. // Disable menu controls
  62. ctx.function_queue.push(std::bind(::disable_menu_controls, std::ref(ctx)));
  63. ::menu::fade_out
  64. (
  65. ctx,
  66. [&ctx]()
  67. {
  68. // Queue change to controls menu state
  69. ctx.function_queue.push
  70. (
  71. [&ctx]()
  72. {
  73. ctx.state_machine.pop();
  74. ctx.state_machine.emplace(std::make_unique<controls_menu_state>(ctx));
  75. }
  76. );
  77. }
  78. );
  79. };
  80. // Build list of menu select callbacks
  81. ctx.menu_select_callbacks.push_back(select_back_callback);
  82. // Build list of menu left callbacks
  83. ctx.menu_left_callbacks.push_back(nullptr);
  84. // Build list of menu right callbacks
  85. ctx.menu_right_callbacks.push_back(nullptr);
  86. // Set menu back callback
  87. ctx.menu_back_callback = select_back_callback;
  88. // Enable menu controls next frame
  89. ctx.function_queue.push(std::bind(::enable_menu_controls, std::ref(ctx)));
  90. // Fade in menu
  91. ::menu::fade_in(ctx, nullptr);
  92. debug::log_trace("Entered keyboard config menu state");
  93. }
  94. keyboard_config_menu_state::~keyboard_config_menu_state()
  95. {
  96. debug::log_trace("Exiting keyboard config menu state...");
  97. // Destruct menu
  98. ::disable_menu_controls(ctx);
  99. ::menu::clear_callbacks(ctx);
  100. ::menu::delete_animations(ctx);
  101. ::menu::remove_text_from_ui(ctx);
  102. ::menu::delete_text(ctx);
  103. if (action_remapped)
  104. {
  105. // Update control profile
  106. ::update_control_profile(ctx, *ctx.control_profile);
  107. // Save control profile
  108. ctx.resource_manager->set_write_path(ctx.controls_path);
  109. ctx.resource_manager->save(*ctx.control_profile, ctx.control_profile_filename);
  110. }
  111. debug::log_trace("Exited keyboard config menu state...");
  112. }
  113. std::string keyboard_config_menu_state::get_mapping_string(const input::action_map& action_map, const input::action& control)
  114. {
  115. std::string mapping_string;
  116. if (auto key_mappings = action_map.get_key_mappings(control); !key_mappings.empty())
  117. {
  118. const auto& key_mapping = key_mappings.front();
  119. // Get name of scancode string from scancode
  120. std::string scancode_string_name = std::format("scancode_{:02x}", std::to_underlying(key_mapping.scancode));
  121. // Set mapping string to scancode string
  122. mapping_string = get_string(ctx, hash::fnv1a32<char>(scancode_string_name));
  123. }
  124. else if (auto mouse_button_mappings = action_map.get_mouse_button_mappings(control); !mouse_button_mappings.empty())
  125. {
  126. const auto& mouse_button_mapping = mouse_button_mappings.front();
  127. switch (mouse_button_mapping.button)
  128. {
  129. case input::mouse_button::left:
  130. mapping_string = get_string(ctx, "mouse_button_left");
  131. break;
  132. case input::mouse_button::middle:
  133. mapping_string = get_string(ctx, "mouse_button_middle");
  134. break;
  135. case input::mouse_button::right:
  136. mapping_string = get_string(ctx, "mouse_button_right");
  137. break;
  138. default:
  139. {
  140. std::string format_string = get_string(ctx, "mouse_button_n_format");
  141. mapping_string = std::vformat(format_string, std::make_format_args(std::to_underlying(mouse_button_mapping.button)));
  142. break;
  143. }
  144. }
  145. }
  146. else if (auto mouse_scroll_mappings = action_map.get_mouse_scroll_mappings(control); !mouse_scroll_mappings.empty())
  147. {
  148. const auto& mouse_scroll_mapping = mouse_scroll_mappings.front();
  149. if (mouse_scroll_mapping.axis == input::mouse_scroll_axis::x)
  150. {
  151. if (!mouse_scroll_mapping.direction)
  152. {
  153. mapping_string = get_string(ctx, "mouse_scroll_left");
  154. }
  155. else
  156. {
  157. mapping_string = get_string(ctx, "mouse_scroll_right");
  158. }
  159. }
  160. else
  161. {
  162. if (!mouse_scroll_mapping.direction)
  163. {
  164. mapping_string = get_string(ctx, "mouse_scroll_up");
  165. }
  166. else
  167. {
  168. mapping_string = get_string(ctx, "mouse_scroll_down");
  169. }
  170. }
  171. }
  172. else
  173. {
  174. mapping_string = get_string(ctx, "control_unmapped");
  175. }
  176. return mapping_string;
  177. }
  178. void keyboard_config_menu_state::add_control_item(input::action_map& action_map, input::action& control, hash::fnv1a32_t control_name_hash)
  179. {
  180. // Construct texts
  181. auto name_text = std::make_unique<scene::text>();
  182. auto value_text = std::make_unique<scene::text>();
  183. // Add texts to list of menu item texts
  184. ctx.menu_item_texts.push_back({name_text.get(), value_text.get()});
  185. // Set control name and mapping texts
  186. name_text->set_content(get_string(ctx, control_name_hash));
  187. value_text->set_content(get_mapping_string(action_map, control));
  188. // Callback invoked when an input has been mapped to the control
  189. auto input_mapped_callback = [this, &ctx = this->ctx, action_map = &action_map, control = &control, value_text = value_text.get()](const auto& event)
  190. {
  191. this->action_remapped = true;
  192. // Remove key mappings, mouse button mappings, and mouse scroll mappings mapped to the control
  193. action_map->remove_mappings(*control, input::mapping_type::key);
  194. action_map->remove_mappings(*control, input::mapping_type::mouse_button);
  195. action_map->remove_mappings(*control, input::mapping_type::mouse_scroll);
  196. //if (event.mapping.scancode != input::scancode::escape && event.mapping.scancode != input::scancode::backspace)
  197. {
  198. // Map generated input mapping to the control
  199. action_map->add_mapping(*control, event.mapping);
  200. }
  201. // Update control mapping text
  202. value_text->set_content(this->get_mapping_string(*action_map, *control));
  203. ::menu::align_text(ctx);
  204. // Queue disabling of input mapper re-enabling of menu controls
  205. ctx.function_queue.push
  206. (
  207. [&ctx]()
  208. {
  209. ctx.input_mapper.disconnect();
  210. ::enable_menu_controls(ctx);
  211. }
  212. );
  213. };
  214. // Callback invoked when the control menu item has been selected
  215. auto select_callback = [this, &ctx = this->ctx, action_map = &action_map, control = &control, value_text = value_text.get(), input_mapped_callback]()
  216. {
  217. // Set control mapping text to "..."
  218. value_text->set_content(get_string(ctx, "control_mapping"));
  219. ::menu::align_text(ctx);
  220. // Setup input mapped callbacks
  221. key_mapped_subscription = ctx.input_mapper.get_key_mapped_channel().subscribe
  222. (
  223. input_mapped_callback
  224. );
  225. mouse_button_mapped_subscription = ctx.input_mapper.get_mouse_button_mapped_channel().subscribe
  226. (
  227. input_mapped_callback
  228. );
  229. mouse_scroll_mapped_subscription = ctx.input_mapper.get_mouse_scroll_mapped_channel().subscribe
  230. (
  231. input_mapped_callback
  232. );
  233. // Queue disabling of menu controls and enabling of input mapper
  234. ctx.function_queue.push
  235. (
  236. [&]()
  237. {
  238. ::disable_menu_controls(ctx);
  239. ctx.input_mapper.connect(ctx.input_manager->get_event_dispatcher());
  240. }
  241. );
  242. };
  243. control_item_texts.emplace_back(std::move(name_text));
  244. control_item_texts.emplace_back(std::move(value_text));
  245. // Register menu item callbacks
  246. ctx.menu_select_callbacks.push_back(select_callback);
  247. ctx.menu_left_callbacks.push_back(nullptr);
  248. ctx.menu_right_callbacks.push_back(nullptr);
  249. }