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

393 lines
12 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/graphics-menu-state.hpp"
  20. #include "game/states/options-menu-state.hpp"
  21. #include "game/controls.hpp"
  22. #include <engine/scene/text.hpp>
  23. #include <engine/debug/log.hpp>
  24. #include "game/fonts.hpp"
  25. #include "game/menu.hpp"
  26. #include "game/graphics.hpp"
  27. #include <engine/animation/timeline.hpp>
  28. #include "game/strings.hpp"
  29. #include <engine/utility/hash/fnv1a.hpp>
  30. using namespace hash::literals;
  31. graphics_menu_state::graphics_menu_state(::game& ctx):
  32. game_state(ctx)
  33. {
  34. debug::log_trace("Entering graphics menu state...");
  35. // Construct menu item texts
  36. fullscreen_name_text = std::make_unique<scene::text>();
  37. fullscreen_value_text = std::make_unique<scene::text>();
  38. resolution_name_text = std::make_unique<scene::text>();
  39. resolution_value_text = std::make_unique<scene::text>();
  40. v_sync_name_text = std::make_unique<scene::text>();
  41. v_sync_value_text = std::make_unique<scene::text>();
  42. aa_method_name_text = std::make_unique<scene::text>();
  43. aa_method_value_text = std::make_unique<scene::text>();
  44. font_scale_name_text = std::make_unique<scene::text>();
  45. font_scale_value_text = std::make_unique<scene::text>();
  46. dyslexia_font_name_text = std::make_unique<scene::text>();
  47. dyslexia_font_value_text = std::make_unique<scene::text>();
  48. back_text = std::make_unique<scene::text>();
  49. // Build list of menu item texts
  50. ctx.menu_item_texts.push_back({fullscreen_name_text.get(), fullscreen_value_text.get()});
  51. ctx.menu_item_texts.push_back({resolution_name_text.get(), resolution_value_text.get()});
  52. ctx.menu_item_texts.push_back({v_sync_name_text.get(), v_sync_value_text.get()});
  53. ctx.menu_item_texts.push_back({aa_method_name_text.get(), aa_method_value_text.get()});
  54. ctx.menu_item_texts.push_back({font_scale_name_text.get(), font_scale_value_text.get()});
  55. ctx.menu_item_texts.push_back({dyslexia_font_name_text.get(), dyslexia_font_value_text.get()});
  56. ctx.menu_item_texts.push_back({back_text.get(), nullptr});
  57. // Set content of menu item texts
  58. fullscreen_name_text->set_content(get_string(ctx, "graphics_menu_fullscreen"));
  59. resolution_name_text->set_content(get_string(ctx, "graphics_menu_resolution"));
  60. v_sync_name_text->set_content(get_string(ctx, "graphics_menu_v_sync"));
  61. aa_method_name_text->set_content(get_string(ctx, "graphics_menu_aa_method"));
  62. font_scale_name_text->set_content(get_string(ctx, "graphics_menu_font_scale"));
  63. dyslexia_font_name_text->set_content(get_string(ctx, "graphics_menu_dyslexia_font"));
  64. back_text->set_content(get_string(ctx, "back"));
  65. update_value_text_content();
  66. // Init menu item index
  67. ::menu::init_menu_item_index(ctx, "graphics");
  68. ::menu::update_text_color(ctx);
  69. ::menu::update_text_font(ctx);
  70. ::menu::align_text(ctx);
  71. ::menu::add_text_to_ui(ctx);
  72. ::menu::setup_animations(ctx);
  73. // Construct menu item callbacks
  74. auto toggle_fullscreen_callback = [this, &ctx]()
  75. {
  76. bool fullscreen = !ctx.window->is_fullscreen();
  77. ctx.window->set_fullscreen(fullscreen);
  78. this->update_value_text_content();
  79. ::menu::align_text(ctx);
  80. // Update fullscreen settings
  81. (*ctx.settings)["fullscreen"] = fullscreen;
  82. };
  83. auto increase_resolution_callback = [this, &ctx]()
  84. {
  85. // Increase resolution
  86. if (ctx.menu_modifier_action.is_active())
  87. ctx.render_scale += 0.05f;
  88. else
  89. ctx.render_scale += 0.25f;
  90. // Limit resolution
  91. if (ctx.render_scale > 2.0f)
  92. ctx.render_scale = 2.0f;
  93. // Update render scale setting
  94. (*ctx.settings)["render_scale"] = ctx.render_scale;
  95. // Resize framebuffers
  96. ::graphics::change_render_resolution(ctx, ctx.render_scale);
  97. // Update text
  98. this->update_value_text_content();
  99. ::menu::align_text(ctx);
  100. };
  101. auto decrease_resolution_callback = [this, &ctx]()
  102. {
  103. // Increase resolution
  104. if (ctx.menu_modifier_action.is_active())
  105. ctx.render_scale -= 0.05f;
  106. else
  107. ctx.render_scale -= 0.25f;
  108. // Limit resolution
  109. if (ctx.render_scale < 0.25f)
  110. ctx.render_scale = 0.25f;
  111. // Update render scale setting
  112. (*ctx.settings)["render_scale"] = ctx.render_scale;
  113. // Resize framebuffers
  114. ::graphics::change_render_resolution(ctx, ctx.render_scale);
  115. // Update text
  116. this->update_value_text_content();
  117. ::menu::align_text(ctx);
  118. };
  119. auto toggle_v_sync_callback = [this, &ctx]()
  120. {
  121. bool v_sync = !ctx.window->get_v_sync();
  122. // Update v-sync setting
  123. (*ctx.settings)["v_sync"] = v_sync;
  124. ctx.window->set_v_sync(v_sync);
  125. this->update_value_text_content();
  126. ::menu::align_text(ctx);
  127. };
  128. auto next_aa_method_callback = [this, &ctx]()
  129. {
  130. switch (ctx.anti_aliasing_method)
  131. {
  132. case render::anti_aliasing_method::none:
  133. ctx.anti_aliasing_method = render::anti_aliasing_method::fxaa;
  134. break;
  135. case render::anti_aliasing_method::fxaa:
  136. ctx.anti_aliasing_method = render::anti_aliasing_method::none;
  137. break;
  138. }
  139. // Update anti-aliasing method setting
  140. (*ctx.settings)["anti_aliasing_method"] = std::to_underlying(ctx.anti_aliasing_method);
  141. ::graphics::select_anti_aliasing_method(ctx, ctx.anti_aliasing_method);
  142. // Update value text
  143. this->update_value_text_content();
  144. // Refresh and realign text
  145. ::menu::refresh_text(ctx);
  146. ::menu::align_text(ctx);
  147. };
  148. auto previous_aa_method_callback = [this, &ctx]()
  149. {
  150. switch (ctx.anti_aliasing_method)
  151. {
  152. case render::anti_aliasing_method::none:
  153. ctx.anti_aliasing_method = render::anti_aliasing_method::fxaa;
  154. break;
  155. case render::anti_aliasing_method::fxaa:
  156. ctx.anti_aliasing_method = render::anti_aliasing_method::none;
  157. break;
  158. }
  159. // Update anti-aliasing method setting
  160. (*ctx.settings)["anti_aliasing_method"] = std::to_underlying(ctx.anti_aliasing_method);
  161. ::graphics::select_anti_aliasing_method(ctx, ctx.anti_aliasing_method);
  162. // Update value text
  163. this->update_value_text_content();
  164. // Refresh and realign text
  165. ::menu::refresh_text(ctx);
  166. ::menu::align_text(ctx);
  167. };
  168. auto increase_font_scale_callback = [this, &ctx]()
  169. {
  170. // Increase font scale
  171. if (ctx.menu_modifier_action.is_active())
  172. ctx.font_scale += 0.01f;
  173. else
  174. ctx.font_scale += 0.1f;
  175. // Limit font scale
  176. if (ctx.font_scale > 2.0f)
  177. ctx.font_scale = 2.0f;
  178. // Update font scale setting
  179. (*ctx.settings)["font_scale"] = ctx.font_scale;
  180. // Update value text
  181. this->update_value_text_content();
  182. // Reload fonts
  183. debug::log_trace("Reloading fonts...");
  184. ::load_fonts(ctx);
  185. debug::log_trace("Reloaded fonts");
  186. // Refresh and realign text
  187. ::menu::refresh_text(ctx);
  188. ::menu::align_text(ctx);
  189. };
  190. auto decrease_font_scale_callback = [this, &ctx]()
  191. {
  192. // Decrease font scale
  193. if (ctx.menu_modifier_action.is_active())
  194. ctx.font_scale -= 0.01f;
  195. else
  196. ctx.font_scale -= 0.1f;
  197. // Limit font scale
  198. if (ctx.font_scale < 0.1f)
  199. ctx.font_scale = 0.1f;
  200. // Update font scale setting
  201. (*ctx.settings)["font_scale"] = ctx.font_scale;
  202. // Update value text
  203. this->update_value_text_content();
  204. // Reload fonts
  205. debug::log_trace("Reloading fonts...");
  206. ::load_fonts(ctx);
  207. debug::log_trace("Reloaded fonts");
  208. // Refresh and realign text
  209. ::menu::refresh_text(ctx);
  210. ::menu::align_text(ctx);
  211. };
  212. auto toggle_dyslexia_font_callback = [this, &ctx]()
  213. {
  214. ctx.dyslexia_font = !ctx.dyslexia_font;
  215. // Update value text
  216. this->update_value_text_content();
  217. // Save dyslexia font setting
  218. (*ctx.settings)["dyslexia_font"] = ctx.dyslexia_font;
  219. // Reload fonts
  220. debug::log_trace("Reloading fonts...");
  221. ::load_fonts(ctx);
  222. debug::log_trace("Reloaded fonts");
  223. // Refresh and realign text
  224. ::menu::refresh_text(ctx);
  225. ::menu::align_text(ctx);
  226. };
  227. auto select_back_callback = [&ctx]()
  228. {
  229. // Disable menu controls
  230. ctx.function_queue.push(std::bind(::disable_menu_controls, std::ref(ctx)));
  231. ::menu::fade_out
  232. (
  233. ctx,
  234. [&ctx]()
  235. {
  236. // Queue change to options menu state
  237. ctx.function_queue.push
  238. (
  239. [&ctx]()
  240. {
  241. ctx.state_machine.pop();
  242. ctx.state_machine.emplace(std::make_unique<options_menu_state>(ctx));
  243. }
  244. );
  245. }
  246. );
  247. };
  248. // Build list of menu select callbacks
  249. ctx.menu_select_callbacks.push_back(toggle_fullscreen_callback);
  250. ctx.menu_select_callbacks.push_back(increase_resolution_callback);
  251. ctx.menu_select_callbacks.push_back(toggle_v_sync_callback);
  252. ctx.menu_select_callbacks.push_back(next_aa_method_callback);
  253. ctx.menu_select_callbacks.push_back(increase_font_scale_callback);
  254. ctx.menu_select_callbacks.push_back(toggle_dyslexia_font_callback);
  255. ctx.menu_select_callbacks.push_back(select_back_callback);
  256. // Build list of menu left callbacks
  257. ctx.menu_left_callbacks.push_back(toggle_fullscreen_callback);
  258. ctx.menu_left_callbacks.push_back(decrease_resolution_callback);
  259. ctx.menu_left_callbacks.push_back(toggle_v_sync_callback);
  260. ctx.menu_left_callbacks.push_back(previous_aa_method_callback);
  261. ctx.menu_left_callbacks.push_back(decrease_font_scale_callback);
  262. ctx.menu_left_callbacks.push_back(toggle_dyslexia_font_callback);
  263. ctx.menu_left_callbacks.push_back(nullptr);
  264. // Build list of menu right callbacks
  265. ctx.menu_right_callbacks.push_back(toggle_fullscreen_callback);
  266. ctx.menu_right_callbacks.push_back(increase_resolution_callback);
  267. ctx.menu_right_callbacks.push_back(toggle_v_sync_callback);
  268. ctx.menu_right_callbacks.push_back(next_aa_method_callback);
  269. ctx.menu_right_callbacks.push_back(increase_font_scale_callback);
  270. ctx.menu_right_callbacks.push_back(toggle_dyslexia_font_callback);
  271. ctx.menu_right_callbacks.push_back(nullptr);
  272. // Set menu back callback
  273. ctx.menu_back_callback = select_back_callback;
  274. // Enable menu controls next frame
  275. ctx.function_queue.push(std::bind(::enable_menu_controls, std::ref(ctx)));
  276. // Fade in menu
  277. ::menu::fade_in(ctx, nullptr);
  278. debug::log_trace("Entered graphics menu state");
  279. }
  280. graphics_menu_state::~graphics_menu_state()
  281. {
  282. debug::log_trace("Exiting graphics menu state...");
  283. // Destruct menu
  284. ::disable_menu_controls(ctx);
  285. ::menu::clear_callbacks(ctx);
  286. ::menu::delete_animations(ctx);
  287. ::menu::remove_text_from_ui(ctx);
  288. ::menu::delete_text(ctx);
  289. debug::log_trace("Exited graphics menu state");
  290. }
  291. void graphics_menu_state::update_value_text_content()
  292. {
  293. const bool fullscreen = ctx.window->is_fullscreen();
  294. const float render_scale = ctx.render_scale;
  295. const bool v_sync = ctx.window->get_v_sync();
  296. const int aa_method_index = static_cast<int>(ctx.anti_aliasing_method);
  297. const float font_scale = ctx.font_scale;
  298. const bool dyslexia_font = ctx.dyslexia_font;
  299. const std::string string_on = get_string(ctx, "on");
  300. const std::string string_off = get_string(ctx, "off");
  301. /*
  302. const std::string string_quality[4] =
  303. {
  304. (*ctx.strings)["off"],
  305. (*ctx.strings)["quality_low"],
  306. (*ctx.strings)["quality_medium"],
  307. (*ctx.strings)["quality_high"]
  308. };
  309. */
  310. const std::string string_aa_methods[2] =
  311. {
  312. get_string(ctx, "graphics_menu_aa_method_none"),
  313. get_string(ctx, "graphics_menu_aa_method_fxaa")
  314. };
  315. std::get<1>(ctx.menu_item_texts[0])->set_content((fullscreen) ? string_on : string_off);
  316. std::get<1>(ctx.menu_item_texts[1])->set_content(std::to_string(static_cast<int>(std::round(render_scale * 100.0f))) + "%");
  317. std::get<1>(ctx.menu_item_texts[2])->set_content((v_sync) ? string_on : string_off);
  318. std::get<1>(ctx.menu_item_texts[3])->set_content(string_aa_methods[aa_method_index]);
  319. std::get<1>(ctx.menu_item_texts[4])->set_content(std::to_string(static_cast<int>(std::round(font_scale * 100.0f))) + "%");
  320. std::get<1>(ctx.menu_item_texts[5])->set_content((dyslexia_font) ? string_on : string_off);
  321. }