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

279 lines
7.7 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/language-menu.hpp"
  20. #include "game/states/options-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 "game/fonts.hpp"
  26. namespace game {
  27. namespace state {
  28. namespace language_menu {
  29. static void update_text_font(game::context* ctx)
  30. {
  31. for (scene::text* text: ctx->language_menu_texts)
  32. {
  33. text->set_material(&ctx->menu_font_material);
  34. text->set_font(&ctx->menu_font);
  35. }
  36. }
  37. static void update_text_color(game::context* ctx)
  38. {
  39. float4 inactive_color = {1.0f, 1.0f, 1.0f, 0.5f};
  40. float4 active_color = {1.0f, 1.0f, 1.0f, 1.0f};
  41. for (std::size_t i = 0; i < ctx->language_menu_texts.size(); ++i)
  42. {
  43. scene::text* text = ctx->language_menu_texts[i];
  44. if (i == ctx->language_menu_index)
  45. text->set_color(active_color);
  46. else
  47. text->set_color(inactive_color);
  48. }
  49. }
  50. static void update_text_content(game::context* ctx)
  51. {
  52. ctx->language_menu_language_text->set_content((*ctx->strings)["language_name"]);
  53. ctx->language_menu_back_text->set_content((*ctx->strings)["back"]);
  54. }
  55. static void refresh_texts(game::context* ctx)
  56. {
  57. for (scene::text* text: ctx->language_menu_texts)
  58. {
  59. text->refresh();
  60. }
  61. }
  62. static void align_texts(game::context* ctx)
  63. {
  64. float menu_width = 0.0f;
  65. for (std::size_t i = 0; i < ctx->language_menu_texts.size(); ++i)
  66. {
  67. scene::text* text = ctx->language_menu_texts[i];
  68. // Update menu width
  69. const auto& bounds = static_cast<const geom::aabb<float>&>(text->get_local_bounds());
  70. float width = bounds.max_point.x - bounds.min_point.x;
  71. menu_width = std::max<float>(menu_width, width);
  72. }
  73. float menu_height = ctx->language_menu_texts.size() * ctx->menu_font.get_font_metrics().linespace;
  74. float menu_x = -menu_width * 0.5f;
  75. float menu_y = menu_height * 0.5f - ctx->menu_font.get_font_metrics().linespace;
  76. for (std::size_t i = 0; i < ctx->language_menu_texts.size(); ++i)
  77. {
  78. scene::text* text = ctx->language_menu_texts[i];
  79. // Align text
  80. const auto& bounds = static_cast<const geom::aabb<float>&>(text->get_local_bounds());
  81. float w = bounds.max_point.x - bounds.min_point.x;
  82. float x = -w * 0.5f;
  83. //float x = menu_x;
  84. float y = menu_y - ctx->menu_font.get_font_metrics().linespace * i;
  85. text->set_translation({std::round(x), std::round(y), 0.0f});
  86. }
  87. }
  88. static void update_text_tweens(game::context* ctx)
  89. {
  90. for (scene::text* text: ctx->language_menu_texts)
  91. {
  92. text->update_tweens();
  93. }
  94. }
  95. void enter(game::context* ctx)
  96. {
  97. ctx->ui_clear_pass->set_cleared_buffers(true, true, false);
  98. ctx->language_menu_index = 0;
  99. // Construct language menu texts
  100. ctx->language_menu_language_text = new scene::text();
  101. ctx->language_menu_back_text = new scene::text();
  102. // Build list of language menu texts
  103. ctx->language_menu_texts.push_back(ctx->language_menu_language_text);
  104. ctx->language_menu_texts.push_back(ctx->language_menu_back_text);
  105. // Construct language menu callbacks
  106. auto menu_back_callback = [ctx]()
  107. {
  108. application::state next_state;
  109. next_state.name = "options_menu";
  110. next_state.enter = std::bind(game::state::options_menu::enter, ctx);
  111. next_state.exit = std::bind(game::state::options_menu::exit, ctx);
  112. ctx->app->change_state(next_state);
  113. };
  114. auto next_language_callback = [ctx]()
  115. {
  116. if (ctx->language_menu_index != 0)
  117. return;
  118. // Increment language index
  119. ++ctx->language_index;
  120. if (ctx->language_index >= ctx->language_count)
  121. ctx->language_index = 0;
  122. // Find corresponding language code and strings
  123. ctx->language_code = (*ctx->string_table)[0][ctx->language_index + 2];
  124. ctx->strings = &ctx->string_table_map[ctx->language_code];
  125. // Update language in config
  126. (*ctx->config)["language"] = ctx->language_code;
  127. ctx->logger->log("Language changed to \"" + ctx->language_code + "\"");
  128. // Reload fonts
  129. ctx->logger->push_task("Reloading fonts");
  130. try
  131. {
  132. game::load_fonts(ctx);
  133. }
  134. catch (...)
  135. {
  136. ctx->logger->pop_task(EXIT_FAILURE);
  137. }
  138. ctx->logger->pop_task(EXIT_SUCCESS);
  139. update_text_font(ctx);
  140. update_text_content(ctx);
  141. refresh_texts(ctx);
  142. align_texts(ctx);
  143. update_text_tweens(ctx);
  144. };
  145. auto previous_language_callback = [ctx]()
  146. {
  147. if (ctx->language_menu_index != 0)
  148. return;
  149. // Increment language index
  150. --ctx->language_index;
  151. if (ctx->language_index < 0)
  152. ctx->language_index = ctx->language_count - 1;
  153. // Find corresponding language code and strings
  154. ctx->language_code = (*ctx->string_table)[0][ctx->language_index + 2];
  155. ctx->strings = &ctx->string_table_map[ctx->language_code];
  156. ctx->logger->log("Language changed to \"" + ctx->language_code + "\"");
  157. // Reload fonts
  158. ctx->logger->push_task("Reloading fonts");
  159. try
  160. {
  161. game::load_fonts(ctx);
  162. }
  163. catch (...)
  164. {
  165. ctx->logger->pop_task(EXIT_FAILURE);
  166. }
  167. ctx->logger->pop_task(EXIT_SUCCESS);
  168. update_text_font(ctx);
  169. update_text_content(ctx);
  170. refresh_texts(ctx);
  171. align_texts(ctx);
  172. update_text_tweens(ctx);
  173. };
  174. // Build list of language menu callbacks
  175. ctx->language_menu_callbacks.push_back(next_language_callback);
  176. ctx->language_menu_callbacks.push_back(menu_back_callback);
  177. ctx->controls["menu_down"]->set_activated_callback
  178. (
  179. [ctx]()
  180. {
  181. ++ctx->language_menu_index;
  182. if (ctx->language_menu_index >= ctx->language_menu_texts.size())
  183. ctx->language_menu_index = 0;
  184. update_text_color(ctx);
  185. }
  186. );
  187. ctx->controls["menu_up"]->set_activated_callback
  188. (
  189. [ctx]()
  190. {
  191. --ctx->language_menu_index;
  192. if (ctx->language_menu_index < 0)
  193. ctx->language_menu_index = ctx->language_menu_texts.size() - 1;
  194. update_text_color(ctx);
  195. }
  196. );
  197. ctx->controls["menu_left"]->set_activated_callback(previous_language_callback);
  198. ctx->controls["menu_right"]->set_activated_callback(next_language_callback);
  199. ctx->controls["menu_select"]->set_activated_callback
  200. (
  201. [ctx]()
  202. {
  203. auto callback = ctx->language_menu_callbacks[ctx->language_menu_index];
  204. if (callback != nullptr)
  205. callback();
  206. }
  207. );
  208. ctx->controls["menu_back"]->set_activated_callback(menu_back_callback);
  209. for (scene::text* text: ctx->language_menu_texts)
  210. ctx->ui_scene->add_object(text);
  211. update_text_font(ctx);
  212. update_text_color(ctx);
  213. update_text_content(ctx);
  214. align_texts(ctx);
  215. update_text_tweens(ctx);
  216. }
  217. void exit(game::context* ctx)
  218. {
  219. // Clear control callbacks
  220. ctx->controls["menu_down"]->set_activated_callback(nullptr);
  221. ctx->controls["menu_up"]->set_activated_callback(nullptr);
  222. ctx->controls["menu_left"]->set_activated_callback(nullptr);
  223. ctx->controls["menu_right"]->set_activated_callback(nullptr);
  224. ctx->controls["menu_select"]->set_activated_callback(nullptr);
  225. ctx->controls["menu_back"]->set_activated_callback(nullptr);
  226. // Clear language menu callbacks
  227. ctx->language_menu_callbacks.clear();
  228. // Destruct language menu texts
  229. for (scene::text* text: ctx->language_menu_texts)
  230. {
  231. ctx->ui_scene->remove_object(text);
  232. delete text;
  233. }
  234. ctx->language_menu_texts.clear();
  235. ctx->ui_clear_pass->set_cleared_buffers(false, true, false);
  236. }
  237. } // namespace language_menu
  238. } // namespace state
  239. } // namespace game