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

348 lines
9.0 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)
  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 = ctx->menu_item_texts.size() * ctx->menu_font.get_font_metrics().linespace;
  93. float menu_x = -menu_width * 0.5f;
  94. float menu_y = menu_height * 0.5f - ctx->menu_font.get_font_metrics().linespace;
  95. for (std::size_t i = 0; i < ctx->menu_item_texts.size(); ++i)
  96. {
  97. auto [name, value] = ctx->menu_item_texts[i];
  98. float x = menu_x;
  99. float y = menu_y - ctx->menu_font.get_font_metrics().linespace * i;
  100. name->set_translation({std::round(x), std::round(y), 0.0f});
  101. if (value)
  102. {
  103. const auto& value_bounds = static_cast<const geom::aabb<float>&>(value->get_local_bounds());
  104. const float value_width = value_bounds.max_point.x - value_bounds.min_point.x;
  105. x = menu_x + menu_width - value_width;
  106. value->set_translation({std::round(x), std::round(y), 0.0f});
  107. }
  108. }
  109. }
  110. void refresh_text(game::context* ctx)
  111. {
  112. for (auto [name, value]: ctx->menu_item_texts)
  113. {
  114. name->refresh();
  115. if (value)
  116. value->refresh();
  117. }
  118. }
  119. void add_text_to_ui(game::context* ctx)
  120. {
  121. for (auto [name, value]: ctx->menu_item_texts)
  122. {
  123. ctx->ui_scene->add_object(name);
  124. if (value)
  125. ctx->ui_scene->add_object(value);
  126. }
  127. }
  128. void remove_text_from_ui(game::context* ctx)
  129. {
  130. for (auto [name, value]: ctx->menu_item_texts)
  131. {
  132. ctx->ui_scene->remove_object(name);
  133. if (value)
  134. ctx->ui_scene->remove_object(value);
  135. }
  136. }
  137. void delete_text(game::context* ctx)
  138. {
  139. for (auto [name, value]: ctx->menu_item_texts)
  140. {
  141. delete name;
  142. if (value)
  143. delete value;
  144. }
  145. ctx->menu_item_texts.clear();
  146. }
  147. void clear_callbacks(game::context* ctx)
  148. {
  149. // Clear menu item callbacks
  150. ctx->menu_left_callbacks.clear();
  151. ctx->menu_right_callbacks.clear();
  152. ctx->menu_select_callbacks.clear();
  153. ctx->menu_back_callback = nullptr;
  154. }
  155. void setup_controls(game::context* ctx)
  156. {
  157. ctx->controls["menu_up"]->set_activated_callback
  158. (
  159. [ctx]()
  160. {
  161. --(*ctx->menu_item_index);
  162. if (*ctx->menu_item_index < 0)
  163. *ctx->menu_item_index = ctx->menu_item_texts.size() - 1;
  164. update_text_color(ctx);
  165. }
  166. );
  167. ctx->controls["menu_down"]->set_activated_callback
  168. (
  169. [ctx]()
  170. {
  171. ++(*ctx->menu_item_index);
  172. if (*ctx->menu_item_index >= ctx->menu_item_texts.size())
  173. *ctx->menu_item_index = 0;
  174. update_text_color(ctx);
  175. }
  176. );
  177. ctx->controls["menu_left"]->set_activated_callback
  178. (
  179. [ctx]()
  180. {
  181. auto callback = ctx->menu_left_callbacks[*ctx->menu_item_index];
  182. if (callback != nullptr)
  183. callback();
  184. }
  185. );
  186. ctx->controls["menu_right"]->set_activated_callback
  187. (
  188. [ctx]()
  189. {
  190. auto callback = ctx->menu_right_callbacks[*ctx->menu_item_index];
  191. if (callback != nullptr)
  192. callback();
  193. }
  194. );
  195. ctx->controls["menu_select"]->set_activated_callback
  196. (
  197. [ctx]()
  198. {
  199. auto callback = ctx->menu_select_callbacks[*ctx->menu_item_index];
  200. if (callback != nullptr)
  201. callback();
  202. }
  203. );
  204. ctx->controls["menu_back"]->set_activated_callback
  205. (
  206. [ctx]()
  207. {
  208. if (ctx->menu_back_callback != nullptr)
  209. ctx->menu_back_callback();
  210. }
  211. );
  212. ctx->menu_mouse_tracker->set_mouse_moved_callback
  213. (
  214. [ctx](const mouse_moved_event& event)
  215. {
  216. for (std::size_t i = 0; i < ctx->menu_item_texts.size(); ++i)
  217. {
  218. auto [name, value] = ctx->menu_item_texts[i];
  219. const auto& name_bounds = static_cast<const geom::aabb<float>&>(name->get_world_bounds());
  220. float min_x = name_bounds.min_point.x;
  221. float min_y = name_bounds.min_point.y;
  222. float max_x = name_bounds.max_point.x;
  223. float max_y = name_bounds.max_point.y;
  224. if (value)
  225. {
  226. const auto& value_bounds = static_cast<const geom::aabb<float>&>(value->get_world_bounds());
  227. min_x = std::min<float>(min_x, value_bounds.min_point.x);
  228. min_y = std::min<float>(min_y, value_bounds.min_point.y);
  229. max_x = std::max<float>(max_x, value_bounds.max_point.x);
  230. max_y = std::max<float>(max_y, value_bounds.max_point.y);
  231. }
  232. const auto& viewport = ctx->app->get_viewport_dimensions();
  233. const float x = static_cast<float>(event.x - viewport[0] / 2);
  234. const float y = static_cast<float>((viewport[1] - event.y + 1) - viewport[1] / 2);
  235. if (x >= min_x && x <= max_x)
  236. {
  237. if (y >= min_y && y <= max_y)
  238. {
  239. *ctx->menu_item_index = i;
  240. update_text_color(ctx);
  241. break;
  242. }
  243. }
  244. }
  245. }
  246. );
  247. ctx->menu_mouse_tracker->set_mouse_button_pressed_callback
  248. (
  249. [ctx](const mouse_button_pressed_event& event)
  250. {
  251. for (std::size_t i = 0; i < ctx->menu_item_texts.size(); ++i)
  252. {
  253. auto [name, value] = ctx->menu_item_texts[i];
  254. const auto& name_bounds = static_cast<const geom::aabb<float>&>(name->get_world_bounds());
  255. float min_x = name_bounds.min_point.x;
  256. float min_y = name_bounds.min_point.y;
  257. float max_x = name_bounds.max_point.x;
  258. float max_y = name_bounds.max_point.y;
  259. if (value)
  260. {
  261. const auto& value_bounds = static_cast<const geom::aabb<float>&>(value->get_world_bounds());
  262. min_x = std::min<float>(min_x, value_bounds.min_point.x);
  263. min_y = std::min<float>(min_y, value_bounds.min_point.y);
  264. max_x = std::max<float>(max_x, value_bounds.max_point.x);
  265. max_y = std::max<float>(max_y, value_bounds.max_point.y);
  266. }
  267. const auto& viewport = ctx->app->get_viewport_dimensions();
  268. const float x = static_cast<float>(event.x - viewport[0] / 2);
  269. const float y = static_cast<float>((viewport[1] - event.y + 1) - viewport[1] / 2);
  270. if (x >= min_x && x <= max_x)
  271. {
  272. if (y >= min_y && y <= max_y)
  273. {
  274. *ctx->menu_item_index = i;
  275. update_text_color(ctx);
  276. if (event.button == 1)
  277. {
  278. auto callback = ctx->menu_select_callbacks[i];
  279. if (callback)
  280. callback();
  281. }
  282. else if (event.button == 3)
  283. {
  284. auto callback = ctx->menu_left_callbacks[i];
  285. if (callback)
  286. callback();
  287. }
  288. return;
  289. }
  290. }
  291. }
  292. }
  293. );
  294. }
  295. void clear_controls(game::context* ctx)
  296. {
  297. ctx->controls["menu_up"]->set_activated_callback(nullptr);
  298. ctx->controls["menu_down"]->set_activated_callback(nullptr);
  299. ctx->controls["menu_left"]->set_activated_callback(nullptr);
  300. ctx->controls["menu_right"]->set_activated_callback(nullptr);
  301. ctx->controls["menu_select"]->set_activated_callback(nullptr);
  302. ctx->controls["menu_back"]->set_activated_callback(nullptr);
  303. ctx->menu_mouse_tracker->set_mouse_moved_callback(nullptr);
  304. ctx->menu_mouse_tracker->set_mouse_button_pressed_callback(nullptr);
  305. }
  306. } // namespace menu
  307. } // namespace game