Antkeeper  0.0.1
main-menu-state.cpp
Go to the documentation of this file.
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 
25 #include <engine/config.hpp>
28 #include "game/controls.hpp"
29 #include "game/ecoregion.hpp"
30 #include "game/menu.hpp"
38 #include "game/strings.hpp"
39 #include "game/world.hpp"
40 #include <engine/math/vector.hpp>
43 #include <engine/render/model.hpp>
47 #include <format>
48 #include <limits>
49 
51  game_state(ctx)
52 {
53  debug::log_trace("Entering main menu state...");
54 
55  const math::fvec2 viewport_size = math::fvec2(ctx.window->get_viewport_size());
56  const math::fvec2 viewport_center = viewport_size * 0.5f;
57 
58  // Construct title text
59  title_text = std::make_unique<scene::text>();
60  title_text->set_material(ctx.title_font_material);
61  title_text->set_color({1.0f, 1.0f, 1.0f, (fade_in) ? 1.0f : 0.0f});
62  title_text->set_font(&ctx.title_font);
63  title_text->set_content(get_string(ctx, "title_antkeeper"));
64  const auto& title_aabb = title_text->get_bounds();
65  float title_w = title_aabb.max.x() - title_aabb.min.x();
66  float title_h = title_aabb.max.y() - title_aabb.min.y();
67  title_text->set_translation({std::round(viewport_center.x() - title_w * 0.5f), std::round(viewport_center.y() - title_h * 0.5f + (viewport_size.y() / 3.0f) / 2.0f), 0.0f});
68 
69  // Add text to UI
70  ctx.ui_scene->add_object(*title_text);
71 
72  // Construct title fade animation
73  title_fade_animation.set_interpolator(ease<float>::out_cubic);
74  animation_channel<float>* opacity_channel = title_fade_animation.add_channel(0);
75  title_fade_animation.set_frame_callback
76  (
77  [this, &ctx](int channel, const float& opacity)
78  {
79  math::fvec4 color = this->title_text->get_color();
80  color[3] = opacity;
81  this->title_text->set_color(color);
82  }
83  );
84  ctx.animator->add_animation(&title_fade_animation);
85 
86  // Construct menu item texts
87  start_text = std::make_unique<scene::text>();
88  options_text = std::make_unique<scene::text>();
89  extras_text = std::make_unique<scene::text>();
90  quit_text = std::make_unique<scene::text>();
91 
92  // Build list of menu item texts
93  ctx.menu_item_texts.push_back({start_text.get(), nullptr});
94  ctx.menu_item_texts.push_back({options_text.get(), nullptr});
95  ctx.menu_item_texts.push_back({extras_text.get(), nullptr});
96  ctx.menu_item_texts.push_back({quit_text.get(), nullptr});
97 
98  // Set content of menu item texts
99  start_text->set_content(get_string(ctx, "main_menu_start"));
100  options_text->set_content(get_string(ctx, "main_menu_options"));
101  extras_text->set_content(get_string(ctx, "main_menu_extras"));
102  quit_text->set_content(get_string(ctx, "main_menu_quit"));
103 
104  // Init menu item index
106 
109  ::menu::align_text(ctx, true, false, (-viewport_size.y() / 3.0f) / 2.0f);
112 
113  auto select_start_callback = [this, &ctx]()
114  {
115  // Disable menu controls
116  ctx.function_queue.push(std::bind(::disable_menu_controls, std::ref(ctx)));
117 
118  // Create change state function
119  auto change_state = [&ctx]()
120  {
121  // Queue change to nuptial state
122  ctx.function_queue.push
123  (
124  [&ctx]()
125  {
126  ctx.state_machine.pop();
127  // ctx.state_machine.emplace(std::make_unique<nuptial_flight_state>(ctx));
128  // ctx.state_machine.emplace(std::make_unique<collection_menu_state>(ctx));
129  // ctx.state_machine.emplace(std::make_unique<nest_selection_state>(ctx));
130  // ctx.state_machine.emplace(std::make_unique<nest_view_state>(ctx));
131  ctx.state_machine.emplace(std::make_unique<treadmill_experiment_state>(ctx));
132  }
133  );
134  };
135 
136  // Fade out title
137  this->fade_out_title();
138 
139  // Fade out menu
140  ::menu::fade_out(ctx, nullptr);
141 
142  // Start fade out to white
143  //ctx.fade_transition_color->set_value({1, 1, 1});
144  ctx.fade_transition_color->set({0, 0, 0});
145  ctx.fade_transition->transition(config::new_colony_fade_out_duration, false, ease<float>::out_cubic, false, change_state);
146  };
147  auto select_options_callback = [this, &ctx]()
148  {
149  // Disable menu controls
150  ctx.function_queue.push(std::bind(::disable_menu_controls, std::ref(ctx)));
151 
152  // Fade out title
153  this->fade_out_title();
154 
155  // Fade out menu
157  (
158  ctx,
159  [&ctx]()
160  {
161  // Queue change to options menu state
162  ctx.function_queue.push
163  (
164  [&ctx]()
165  {
166  ctx.state_machine.pop();
167  ctx.state_machine.emplace(std::make_unique<options_menu_state>(ctx));
168  }
169  );
170  }
171  );
172  };
173  auto select_extras_callback = [this, &ctx]()
174  {
175  // Disable menu controls
176  ctx.function_queue.push(std::bind(::disable_menu_controls, std::ref(ctx)));
177 
178  // Fade out title
179  this->fade_out_title();
180 
181  // Fade out menu
183  (
184  ctx,
185  [&ctx]()
186  {
187  // Queue change to extras menu state
188  ctx.function_queue.push
189  (
190  [&ctx]()
191  {
192  ctx.state_machine.pop();
193  ctx.state_machine.emplace(std::make_unique<extras_menu_state>(ctx));
194  }
195  );
196  }
197  );
198  };
199  auto select_quit_callback = [this, &ctx]()
200  {
201  // Disable menu controls
202  ctx.function_queue.push(std::bind(::disable_menu_controls, std::ref(ctx)));
203 
204  // Fade out title
205  this->fade_out_title();
206 
207  // Fade out menu
208  ::menu::fade_out(ctx, nullptr);
209 
210  // Fade to black then quit
211  ctx.fade_transition->transition(config::quit_fade_out_duration, false, ease<float>::out_cubic, false, [&ctx](){ctx.closed=true;});
212 
213  // Quit immediately
214  //ctx.function_queue.push([&ctx](){ctx.closed=true;});
215  };
216 
217  // Build list of menu select callbacks
218  ctx.menu_select_callbacks.push_back(select_start_callback);
219  ctx.menu_select_callbacks.push_back(select_options_callback);
220  ctx.menu_select_callbacks.push_back(select_extras_callback);
221  ctx.menu_select_callbacks.push_back(select_quit_callback);
222 
223  // Build list of menu left callbacks
224  ctx.menu_left_callbacks.push_back(nullptr);
225  ctx.menu_left_callbacks.push_back(nullptr);
226  ctx.menu_left_callbacks.push_back(nullptr);
227  ctx.menu_left_callbacks.push_back(nullptr);
228 
229  // Build list of menu right callbacks
230  ctx.menu_right_callbacks.push_back(nullptr);
231  ctx.menu_right_callbacks.push_back(nullptr);
232  ctx.menu_right_callbacks.push_back(nullptr);
233  ctx.menu_right_callbacks.push_back(nullptr);
234 
235  // Set menu back callback
236  ctx.menu_back_callback = select_quit_callback;
237 
238  if (fade_in)
239  {
240  // Fade in from black
241  ctx.fade_transition->transition(config::title_fade_in_duration, true, ease<float>::out_cubic);
242  }
243  else
244  {
245  // Fade in text
246  fade_in_title();
247  ::menu::fade_in(ctx, nullptr);
248  }
249 
250  if (ctx.entities.find("earth") == ctx.entities.end())
251  {
254  //::world::enter_ecoregion(ctx, *ctx.resource_manager->load<::ecoregion>("seedy-scrub.eco"));
255  }
256 
257  // Set world time
258  ::world::set_time(ctx, 2022, 6, 21, 12, 0, 0.0);
259 
260  // Set world time scale
262 
263  const float ev100_sunny16 = physics::light::ev::from_settings(16.0f, 1.0f / 100.0f, 100.0f);
264  ctx.surface_camera->set_exposure_value(ev100_sunny16);
265 
266 
267  // Setup and enable sky and ground passes
268  ctx.sky_pass->set_enabled(true);
269 
270  // Setup window resized callback
271  window_resized_subscription = ctx.window->get_resized_channel().subscribe
272  (
273  [&](const auto& event)
274  {
275  const math::fvec2 viewport_size = math::fvec2(event.window->get_viewport_size());
276  const math::fvec2 viewport_center = viewport_size * 0.5f;
277 
278  // Re-align title text
279  const auto& title_aabb = title_text->get_bounds();
280  float title_w = title_aabb.max.x() - title_aabb.min.x();
281  float title_h = title_aabb.max.y() - title_aabb.min.y();
282  title_text->set_translation({std::round(viewport_center.x() - title_w * 0.5f), std::round(viewport_center.y() - title_h * 0.5f + (viewport_size.y() / 3.0f) / 2.0f), 0.0f});
283 
284  ::menu::align_text(ctx, true, false, (-viewport_size.y() / 3.0f) / 2.0f);
285  }
286  );
287 
288  // Enable menu controls
289  ctx.function_queue.push(std::bind(::enable_menu_controls, std::ref(ctx)));
290 
291  debug::log_trace("Entered main menu state");
292 }
293 
295 {
296  debug::log_trace("Exiting main menu state...");
297 
298  // Destruct menu
304 
305  // Hide menu BG
306  //ctx.menu_bg_billboard->set_active(false);
307 
308  // Destruct title animation
309  ctx.animator->remove_animation(&title_fade_animation);
310 
311  // Destruct text
312  ctx.ui_scene->remove_object(*title_text);
313 
314  debug::log_trace("Exited main menu state");
315 }
316 
317 void main_menu_state::fade_in_title()
318 {
319  animation_channel<float>* opacity_channel = title_fade_animation.get_channel(0);
320  opacity_channel->remove_keyframes();
321  opacity_channel->insert_keyframe({0.0f, 0.0f});
322  opacity_channel->insert_keyframe({config::menu_fade_in_duration, 1.0f});
323  title_fade_animation.stop();
324  title_fade_animation.play();
325 }
326 
327 void main_menu_state::fade_out_title()
328 {
329  animation_channel<float>* opacity_channel = title_fade_animation.get_channel(0);
330  opacity_channel->remove_keyframes();
331  opacity_channel->insert_keyframe({0.0f, 1.0f});
332  opacity_channel->insert_keyframe({config::menu_fade_out_duration, 0.0f});
333  title_fade_animation.stop();
334  title_fade_animation.play();
335 }
void play()
Plays the animation.
Definition: animation.cpp:54
void stop()
Stops the animation, rewinds it, and resets the loop count.
Definition: animation.cpp:69
Single channel in a keyframe animation.
void remove_keyframes(float start, float end)
Removes all keyframes on [start, end).
void insert_keyframe(const keyframe &k)
Adds a keyframe to the animation.
const channel * get_channel(int id) const
Returns the channel with the specified ID.
Definition: animation.hpp:351
void set_interpolator(interpolator_type interpolator)
Sets the frame interpolator function object.
Definition: animation.hpp:339
channel * add_channel(int id)
Adds a channel to the animation.
Definition: animation.hpp:317
void set_frame_callback(std::function< void(int, const T &)> callback)
Sets the callback that's executed on each frame of animation.
Definition: animation.hpp:345
Abstract base class for game states.
Definition: game-state.hpp:29
::game & ctx
Definition: game-state.hpp:44
Definition: game.hpp:121
std::unique_ptr< scene::collection > ui_scene
Definition: game.hpp:333
std::vector< std::function< void()> > menu_left_callbacks
Definition: game.hpp:348
std::vector< std::function< void()> > menu_right_callbacks
Definition: game.hpp:349
hsm::state_machine< game_state > state_machine
Definition: game.hpp:299
std::unique_ptr< screen_transition > fade_transition
Definition: game.hpp:371
std::shared_ptr< scene::camera > surface_camera
Definition: game.hpp:357
std::unique_ptr< animator > animator
Definition: game.hpp:368
std::vector< std::function< void()> > menu_select_callbacks
Definition: game.hpp:347
std::vector< std::tuple< scene::text *, scene::text * > > menu_item_texts
Definition: game.hpp:351
std::unique_ptr< render::sky_pass > sky_pass
Definition: game.hpp:327
std::shared_ptr< app::window > window
Definition: game.hpp:166
std::queue< std::function< void()> > function_queue
Definition: game.hpp:303
std::shared_ptr< render::matvar_fvec3 > fade_transition_color
Definition: game.hpp:372
type::bitmap_font title_font
Definition: game.hpp:189
std::shared_ptr< render::material > title_font_material
Definition: game.hpp:192
std::function< void()> menu_back_callback
Definition: game.hpp:350
std::unordered_map< hash::fnv1a32_t, entity::id > entities
Definition: game.hpp:393
bool closed
Definition: game.hpp:167
virtual ~main_menu_state()
main_menu_state(::game &ctx, bool fade_in)
void enable_menu_controls(::game &ctx)
void disable_menu_controls(::game &ctx)
Color science.
Definition: aces.hpp:27
log_message< log_message_severity::trace, Args... > log_trace
Formats and logs a trace message.
Definition: log.hpp:88
Publish-subscribe messaging.
Definition: channel.hpp:32
constexpr vector< T, N > round(const vector< T, N > &x)
Performs a element-wise round operation.
Definition: vector.hpp:1489
void init_menu_item_index(::game &ctx, hash::fnv1a32_t menu_name)
Definition: menu.cpp:31
void update_text_color(::game &ctx)
Definition: menu.cpp:59
void clear_callbacks(::game &ctx)
Definition: menu.cpp:197
void delete_animations(::game &ctx)
Definition: menu.cpp:191
void align_text(::game &ctx, bool center, bool has_back, float anchor_y)
Definition: menu.cpp:73
void setup_animations(::game &ctx)
Definition: menu.cpp:206
void add_text_to_ui(::game &ctx)
Definition: menu.cpp:166
void fade_out(::game &ctx, const std::function< void()> &end_callback)
Definition: menu.cpp:263
void update_text_font(::game &ctx)
Definition: menu.cpp:44
void fade_in(::game &ctx, const std::function< void()> &end_callback)
Definition: menu.cpp:233
void remove_text_from_ui(::game &ctx)
Definition: menu.cpp:176
void delete_text(::game &ctx)
Definition: menu.cpp:186
T from_settings(T n, T t, T s)
Exposure value from exposure settings.
Definition: exposure.hpp:75
void cosmogenesis(::game &ctx)
Creates the cosmos.
Definition: world.cpp:86
void create_observer(::game &ctx)
Creates the observer.
Definition: world.cpp:98
void set_time_scale(::game &ctx, double scale)
Sets rate at which time passes.
Definition: world.cpp:197
void set_time(::game &ctx, double t)
Sets the current time.
Definition: world.cpp:157
std::string get_string(const ::game &ctx, hash::fnv1a32_t key)
Returns a localized string.
Definition: strings.cpp:23
Container for templated easing functions.
Definition: ease.hpp:71
n-dimensional vector.
Definition: vector.hpp:44
constexpr element_type & x() noexcept
Returns a reference to the first element.
Definition: vector.hpp:164
constexpr element_type & y() noexcept
Returns a reference to the second element.
Definition: vector.hpp:180