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

290 lines
7.8 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/menu.hpp"
  20. #include <engine/scene/text.hpp>
  21. #include <engine/animation/animation.hpp>
  22. #include <engine/animation/animator.hpp>
  23. #include <engine/animation/ease.hpp>
  24. #include <engine/config.hpp>
  25. #include <algorithm>
  26. #include <engine/math/vector.hpp>
  27. namespace menu {
  28. void init_menu_item_index(::game& ctx, hash::fnv1a32_t menu_name)
  29. {
  30. if (auto it = ctx.menu_item_indices.find(menu_name); it != ctx.menu_item_indices.end())
  31. {
  32. ctx.menu_item_index = &it->second;
  33. }
  34. else
  35. {
  36. ctx.menu_item_index = &ctx.menu_item_indices[menu_name];
  37. *ctx.menu_item_index = 0;
  38. }
  39. }
  40. void update_text_font(::game& ctx)
  41. {
  42. for (auto [name, value]: ctx.menu_item_texts)
  43. {
  44. name->set_material(ctx.menu_font_material);
  45. name->set_font(&ctx.menu_font);
  46. if (value)
  47. {
  48. value->set_material(ctx.menu_font_material);
  49. value->set_font(&ctx.menu_font);
  50. }
  51. }
  52. }
  53. void update_text_color(::game& ctx)
  54. {
  55. for (std::size_t i = 0; i < ctx.menu_item_texts.size(); ++i)
  56. {
  57. auto [name, value] = ctx.menu_item_texts[i];
  58. const math::fvec4& color = (i == *ctx.menu_item_index) ? config::menu_active_color : config::menu_inactive_color;
  59. name->set_color(color);
  60. if (value)
  61. value->set_color(color);
  62. }
  63. }
  64. void align_text(::game& ctx, bool center, bool has_back, float anchor_y)
  65. {
  66. const math::fvec2 viewport_size = math::fvec2(ctx.window->get_viewport_size());
  67. const math::fvec2 viewport_center = viewport_size * 0.5f;
  68. const float viewport_padding = viewport_size.y() * (1.0f / 9.0f);
  69. // Calculate menu width
  70. float m_width = ctx.menu_font.get_glyph_metrics(U'M').width;
  71. float column_spacing = m_width * 2.0f;
  72. const float min_two_column_row_width = m_width * 18.0f;
  73. float menu_width = 0.0f;
  74. for (auto [name, value]: ctx.menu_item_texts)
  75. {
  76. float row_width = 0.0f;
  77. // Add name width to row width
  78. const auto& name_bounds = name->get_bounds();
  79. row_width += name_bounds.max.x() - name_bounds.min.x();
  80. if (value)
  81. {
  82. // Add value width to row width
  83. const auto& value_bounds = value->get_bounds();
  84. row_width += value_bounds.max.x() - value_bounds.min.x();
  85. // Add column spacing to row width
  86. row_width += column_spacing;
  87. row_width = std::max<float>(min_two_column_row_width, row_width);
  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 = viewport_center.x() - menu_width * 0.5f;
  98. float menu_y = viewport_center.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. {
  106. y = viewport_padding;// + ctx.menu_font.get_font_metrics().linespace;
  107. }
  108. if (center || i == ctx.menu_item_texts.size() - 1)
  109. {
  110. const auto& name_bounds = name->get_bounds();
  111. const float name_width = name_bounds.max.x() - name_bounds.min.x();
  112. x = viewport_center.x() - name_width * 0.5f;
  113. }
  114. name->set_translation({std::round(x), std::round(y), 0.0f});
  115. if (value)
  116. {
  117. const auto& value_bounds = value->get_bounds();
  118. const float value_width = value_bounds.max.x() - value_bounds.min.x();
  119. if (center || i == ctx.menu_item_texts.size() - 1)
  120. x = viewport_center.x() - value_width * 0.5f;
  121. else
  122. x = menu_x + menu_width - value_width;
  123. value->set_translation({std::round(x), std::round(y), 0.0f});
  124. }
  125. }
  126. }
  127. void refresh_text(::game& ctx)
  128. {
  129. for (auto [name, value]: ctx.menu_item_texts)
  130. {
  131. name->refresh();
  132. if (value)
  133. value->refresh();
  134. }
  135. }
  136. void add_text_to_ui(::game& ctx)
  137. {
  138. for (auto [name, value]: ctx.menu_item_texts)
  139. {
  140. ctx.ui_scene->add_object(*name);
  141. if (value)
  142. ctx.ui_scene->add_object(*value);
  143. }
  144. }
  145. void remove_text_from_ui(::game& ctx)
  146. {
  147. for (auto [name, value]: ctx.menu_item_texts)
  148. {
  149. ctx.ui_scene->remove_object(*name);
  150. if (value)
  151. ctx.ui_scene->remove_object(*value);
  152. }
  153. }
  154. void delete_text(::game& ctx)
  155. {
  156. ctx.menu_item_texts.clear();
  157. }
  158. void delete_animations(::game& ctx)
  159. {
  160. ctx.animator->remove_animation(ctx.menu_fade_animation.get());
  161. ctx.menu_fade_animation.reset();
  162. }
  163. void clear_callbacks(::game& ctx)
  164. {
  165. // Clear menu item callbacks
  166. ctx.menu_left_callbacks.clear();
  167. ctx.menu_right_callbacks.clear();
  168. ctx.menu_select_callbacks.clear();
  169. ctx.menu_back_callback = nullptr;
  170. }
  171. void setup_animations(::game& ctx)
  172. {
  173. ctx.menu_fade_animation = std::make_unique<animation<float>>();
  174. animation_channel<float>* opacity_channel = ctx.menu_fade_animation->add_channel(0);
  175. ctx.menu_fade_animation->set_frame_callback
  176. (
  177. [&ctx](int channel, const float& opacity)
  178. {
  179. for (std::size_t i = 0; i < ctx.menu_item_texts.size(); ++i)
  180. {
  181. auto [name, value] = ctx.menu_item_texts[i];
  182. math::fvec4 color = (i == *ctx.menu_item_index) ? config::menu_active_color : config::menu_inactive_color;
  183. color[3] = color[3] * opacity;
  184. if (name)
  185. name->set_color(color);
  186. if (value)
  187. value->set_color(color);
  188. }
  189. }
  190. );
  191. ctx.animator->add_animation(ctx.menu_fade_animation.get());
  192. }
  193. void fade_in(::game& ctx, const std::function<void()>& end_callback)
  194. {
  195. ctx.menu_fade_animation->set_interpolator(ease<float>::out_cubic);
  196. animation_channel<float>* opacity_channel = ctx.menu_fade_animation->get_channel(0);
  197. opacity_channel->remove_keyframes();
  198. opacity_channel->insert_keyframe({0.0f, 0.0f});
  199. opacity_channel->insert_keyframe({config::menu_fade_in_duration, 1.0f});
  200. ctx.menu_fade_animation->set_end_callback(end_callback);
  201. for (std::size_t i = 0; i < ctx.menu_item_texts.size(); ++i)
  202. {
  203. auto [name, value] = ctx.menu_item_texts[i];
  204. math::fvec4 color = (i == *ctx.menu_item_index) ? config::menu_active_color : config::menu_inactive_color;
  205. color[3] = 0.0f;
  206. if (name)
  207. {
  208. name->set_color(color);
  209. }
  210. if (value)
  211. {
  212. value->set_color(color);
  213. }
  214. }
  215. ctx.menu_fade_animation->stop();
  216. ctx.menu_fade_animation->play();
  217. }
  218. void fade_out(::game& ctx, const std::function<void()>& end_callback)
  219. {
  220. ctx.menu_fade_animation->set_interpolator(ease<float>::out_cubic);
  221. animation_channel<float>* opacity_channel = ctx.menu_fade_animation->get_channel(0);
  222. opacity_channel->remove_keyframes();
  223. opacity_channel->insert_keyframe({0.0f, 1.0f});
  224. opacity_channel->insert_keyframe({config::menu_fade_out_duration, 0.0f});
  225. ctx.menu_fade_animation->set_end_callback(end_callback);
  226. ctx.menu_fade_animation->stop();
  227. ctx.menu_fade_animation->play();
  228. }
  229. void fade_in_bg(::game& ctx)
  230. {
  231. ctx.menu_bg_fade_out_animation->stop();
  232. ctx.menu_bg_fade_in_animation->stop();
  233. ctx.menu_bg_fade_in_animation->play();
  234. }
  235. void fade_out_bg(::game& ctx)
  236. {
  237. ctx.menu_bg_fade_in_animation->stop();
  238. ctx.menu_bg_fade_out_animation->stop();
  239. ctx.menu_bg_fade_out_animation->play();
  240. }
  241. } // namespace menu