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

376 lines
10 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/menu.hpp"
  20. #include "scene/text.hpp"
  21. #include "application.hpp"
  22. #include <algorithm>
  23. namespace game {
  24. namespace menu {
  25. void init_menu_item_index(game::context* ctx, const std::string& menu_name)
  26. {
  27. if (auto it = ctx->menu_item_indices.find(menu_name); it != ctx->menu_item_indices.end())
  28. {
  29. ctx->menu_item_index = &it->second;
  30. }
  31. else
  32. {
  33. ctx->menu_item_index = &ctx->menu_item_indices[menu_name];
  34. *ctx->menu_item_index = 0;
  35. }
  36. }
  37. void update_text_font(game::context* ctx)
  38. {
  39. for (auto [name, value]: ctx->menu_item_texts)
  40. {
  41. name->set_material(&ctx->menu_font_material);
  42. name->set_font(&ctx->menu_font);
  43. if (value)
  44. {
  45. value->set_material(&ctx->menu_font_material);
  46. value->set_font(&ctx->menu_font);
  47. }
  48. }
  49. }
  50. void update_text_color(game::context* ctx)
  51. {
  52. for (std::size_t i = 0; i < ctx->menu_item_texts.size(); ++i)
  53. {
  54. auto [name, value] = ctx->menu_item_texts[i];
  55. const float4& color = (i == *ctx->menu_item_index) ? active_color : inactive_color;
  56. name->set_color(color);
  57. if (value)
  58. value->set_color(color);
  59. }
  60. }
  61. void update_text_tweens(game::context* ctx)
  62. {
  63. for (auto [name, value]: ctx->menu_item_texts)
  64. {
  65. name->update_tweens();
  66. if (value)
  67. value->update_tweens();
  68. }
  69. }
  70. void align_text(game::context* ctx, bool center, bool has_back, float anchor_y)
  71. {
  72. // Calculate menu width
  73. float menu_width = 0.0f;
  74. float menu_spacing = ctx->menu_font.get_glyph_metrics(U'M').width;
  75. for (auto [name, value]: ctx->menu_item_texts)
  76. {
  77. float row_width = 0.0f;
  78. // Add name width to width
  79. const auto& name_bounds = static_cast<const geom::aabb<float>&>(name->get_local_bounds());
  80. row_width += name_bounds.max_point.x - name_bounds.min_point.x;
  81. if (value)
  82. {
  83. // Add value width to width
  84. //const auto& value_bounds = static_cast<const geom::aabb<float>&>(value->get_local_bounds());
  85. //row_width += value_bounds.max_point.x - value_bounds.min_point.x;
  86. // Add spacing to row width
  87. row_width += menu_spacing * 8.0f;
  88. }
  89. menu_width = std::max<float>(menu_width, row_width);
  90. }
  91. // Align texts
  92. float menu_height;
  93. if (has_back)
  94. menu_height = (ctx->menu_item_texts.size() - 1) * ctx->menu_font.get_font_metrics().linespace - ctx->menu_font.get_font_metrics().linegap;
  95. else
  96. menu_height = ctx->menu_item_texts.size() * ctx->menu_font.get_font_metrics().linespace - ctx->menu_font.get_font_metrics().linegap;
  97. float menu_x = -menu_width * 0.5f;
  98. float menu_y = anchor_y + menu_height * 0.5f - ctx->menu_font.get_font_metrics().size;
  99. for (std::size_t i = 0; i < ctx->menu_item_texts.size(); ++i)
  100. {
  101. auto [name, value] = ctx->menu_item_texts[i];
  102. float x = menu_x;
  103. float y = menu_y - ctx->menu_font.get_font_metrics().linespace * i;
  104. if (has_back && i == ctx->menu_item_texts.size() - 1)
  105. y -= ctx->menu_font.get_font_metrics().linespace;
  106. if (center || i == ctx->menu_item_texts.size() - 1)
  107. {
  108. const auto& name_bounds = static_cast<const geom::aabb<float>&>(name->get_local_bounds());
  109. const float name_width = name_bounds.max_point.x - name_bounds.min_point.x;
  110. x = -name_width * 0.5f;
  111. }
  112. name->set_translation({std::round(x), std::round(y), 0.0f});
  113. if (value)
  114. {
  115. const auto& value_bounds = static_cast<const geom::aabb<float>&>(value->get_local_bounds());
  116. const float value_width = value_bounds.max_point.x - value_bounds.min_point.x;
  117. if (center || i == ctx->menu_item_texts.size() - 1)
  118. x = -value_width * 0.5f;
  119. else
  120. x = menu_x + menu_width - value_width;
  121. value->set_translation({std::round(x), std::round(y), 0.0f});
  122. }
  123. }
  124. }
  125. void refresh_text(game::context* ctx)
  126. {
  127. for (auto [name, value]: ctx->menu_item_texts)
  128. {
  129. name->refresh();
  130. if (value)
  131. value->refresh();
  132. }
  133. }
  134. void add_text_to_ui(game::context* ctx)
  135. {
  136. for (auto [name, value]: ctx->menu_item_texts)
  137. {
  138. ctx->ui_scene->add_object(name);
  139. if (value)
  140. ctx->ui_scene->add_object(value);
  141. }
  142. }
  143. void remove_text_from_ui(game::context* ctx)
  144. {
  145. for (auto [name, value]: ctx->menu_item_texts)
  146. {
  147. ctx->ui_scene->remove_object(name);
  148. if (value)
  149. ctx->ui_scene->remove_object(value);
  150. }
  151. }
  152. void delete_text(game::context* ctx)
  153. {
  154. for (auto [name, value]: ctx->menu_item_texts)
  155. {
  156. delete name;
  157. if (value)
  158. delete value;
  159. }
  160. ctx->menu_item_texts.clear();
  161. }
  162. void clear_callbacks(game::context* ctx)
  163. {
  164. // Clear menu item callbacks
  165. ctx->menu_left_callbacks.clear();
  166. ctx->menu_right_callbacks.clear();
  167. ctx->menu_select_callbacks.clear();
  168. ctx->menu_back_callback = nullptr;
  169. }
  170. void setup_controls(game::context* ctx)
  171. {
  172. ctx->controls["menu_up"]->set_activated_callback
  173. (
  174. [ctx]()
  175. {
  176. --(*ctx->menu_item_index);
  177. if (*ctx->menu_item_index < 0)
  178. *ctx->menu_item_index = ctx->menu_item_texts.size() - 1;
  179. update_text_color(ctx);
  180. }
  181. );
  182. ctx->controls["menu_down"]->set_activated_callback
  183. (
  184. [ctx]()
  185. {
  186. ++(*ctx->menu_item_index);
  187. if (*ctx->menu_item_index >= ctx->menu_item_texts.size())
  188. *ctx->menu_item_index = 0;
  189. update_text_color(ctx);
  190. }
  191. );
  192. ctx->controls["menu_left"]->set_activated_callback
  193. (
  194. [ctx]()
  195. {
  196. auto callback = ctx->menu_left_callbacks[*ctx->menu_item_index];
  197. if (callback != nullptr)
  198. callback();
  199. }
  200. );
  201. ctx->controls["menu_right"]->set_activated_callback
  202. (
  203. [ctx]()
  204. {
  205. auto callback = ctx->menu_right_callbacks[*ctx->menu_item_index];
  206. if (callback != nullptr)
  207. callback();
  208. }
  209. );
  210. ctx->controls["menu_select"]->set_activated_callback
  211. (
  212. [ctx]()
  213. {
  214. auto callback = ctx->menu_select_callbacks[*ctx->menu_item_index];
  215. if (callback != nullptr)
  216. callback();
  217. }
  218. );
  219. ctx->controls["menu_back"]->set_activated_callback
  220. (
  221. [ctx]()
  222. {
  223. if (ctx->menu_back_callback != nullptr)
  224. ctx->menu_back_callback();
  225. }
  226. );
  227. ctx->menu_mouse_tracker->set_mouse_moved_callback
  228. (
  229. [ctx](const mouse_moved_event& event)
  230. {
  231. const float padding = game::menu::mouseover_padding * ctx->menu_font.get_font_metrics().size;
  232. for (std::size_t i = 0; i < ctx->menu_item_texts.size(); ++i)
  233. {
  234. auto [name, value] = ctx->menu_item_texts[i];
  235. const auto& name_bounds = static_cast<const geom::aabb<float>&>(name->get_world_bounds());
  236. float min_x = name_bounds.min_point.x;
  237. float min_y = name_bounds.min_point.y;
  238. float max_x = name_bounds.max_point.x;
  239. float max_y = name_bounds.max_point.y;
  240. if (value)
  241. {
  242. const auto& value_bounds = static_cast<const geom::aabb<float>&>(value->get_world_bounds());
  243. min_x = std::min<float>(min_x, value_bounds.min_point.x);
  244. min_y = std::min<float>(min_y, value_bounds.min_point.y);
  245. max_x = std::max<float>(max_x, value_bounds.max_point.x);
  246. max_y = std::max<float>(max_y, value_bounds.max_point.y);
  247. }
  248. const auto& viewport = ctx->app->get_viewport_dimensions();
  249. const float x = static_cast<float>(event.x - viewport[0] / 2);
  250. const float y = static_cast<float>((viewport[1] - event.y + 1) - viewport[1] / 2);
  251. min_x -= padding;
  252. min_y -= padding;
  253. max_x += padding;
  254. max_y += padding;
  255. if (x >= min_x && x <= max_x)
  256. {
  257. if (y >= min_y && y <= max_y)
  258. {
  259. *ctx->menu_item_index = i;
  260. update_text_color(ctx);
  261. break;
  262. }
  263. }
  264. }
  265. }
  266. );
  267. ctx->menu_mouse_tracker->set_mouse_button_pressed_callback
  268. (
  269. [ctx](const mouse_button_pressed_event& event)
  270. {
  271. for (std::size_t i = 0; i < ctx->menu_item_texts.size(); ++i)
  272. {
  273. auto [name, value] = ctx->menu_item_texts[i];
  274. const auto& name_bounds = static_cast<const geom::aabb<float>&>(name->get_world_bounds());
  275. float min_x = name_bounds.min_point.x;
  276. float min_y = name_bounds.min_point.y;
  277. float max_x = name_bounds.max_point.x;
  278. float max_y = name_bounds.max_point.y;
  279. if (value)
  280. {
  281. const auto& value_bounds = static_cast<const geom::aabb<float>&>(value->get_world_bounds());
  282. min_x = std::min<float>(min_x, value_bounds.min_point.x);
  283. min_y = std::min<float>(min_y, value_bounds.min_point.y);
  284. max_x = std::max<float>(max_x, value_bounds.max_point.x);
  285. max_y = std::max<float>(max_y, value_bounds.max_point.y);
  286. }
  287. const auto& viewport = ctx->app->get_viewport_dimensions();
  288. const float x = static_cast<float>(event.x - viewport[0] / 2);
  289. const float y = static_cast<float>((viewport[1] - event.y + 1) - viewport[1] / 2);
  290. if (x >= min_x && x <= max_x)
  291. {
  292. if (y >= min_y && y <= max_y)
  293. {
  294. *ctx->menu_item_index = i;
  295. update_text_color(ctx);
  296. if (event.button == 1)
  297. {
  298. auto callback = ctx->menu_select_callbacks[i];
  299. if (callback)
  300. callback();
  301. }
  302. else if (event.button == 3)
  303. {
  304. auto callback = ctx->menu_left_callbacks[i];
  305. if (callback)
  306. callback();
  307. }
  308. return;
  309. }
  310. }
  311. }
  312. }
  313. );
  314. }
  315. void clear_controls(game::context* ctx)
  316. {
  317. ctx->controls["menu_up"]->set_activated_callback(nullptr);
  318. ctx->controls["menu_down"]->set_activated_callback(nullptr);
  319. ctx->controls["menu_left"]->set_activated_callback(nullptr);
  320. ctx->controls["menu_right"]->set_activated_callback(nullptr);
  321. ctx->controls["menu_select"]->set_activated_callback(nullptr);
  322. ctx->controls["menu_back"]->set_activated_callback(nullptr);
  323. ctx->menu_mouse_tracker->set_mouse_moved_callback(nullptr);
  324. ctx->menu_mouse_tracker->set_mouse_button_pressed_callback(nullptr);
  325. }
  326. } // namespace menu
  327. } // namespace game