Antkeeper  0.0.1
collection-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 
22 #include "game/controls.hpp"
23 #include <engine/scene/text.hpp>
24 #include <engine/debug/log.hpp>
25 #include "game/menu.hpp"
26 #include "game/strings.hpp"
31 
32 using namespace hash::literals;
33 
35  game_state(ctx)
36 {
37  debug::log_trace("Entering collection menu state...");
38 
39  // Construct box material
40  box_material = std::make_shared<render::material>();
41  box_material->set_blend_mode(render::material_blend_mode::translucent);
42  box_material->set_shader_template(ctx.resource_manager->load<gl::shader_template>("ui-element-untextured.glsl"));
43  box_material->set_variable("tint", std::make_shared<render::matvar_fvec4>(1, math::fvec4{0.5f, 0.5f, 0.5f, 1}));
44 
45  // Construct box billboard
46  box_billboard.set_material(box_material);
47 
48  // Construct selection material
49  selection_material = std::make_shared<render::material>();
50  selection_material->set_blend_mode(render::material_blend_mode::translucent);
51  selection_material->set_shader_template(ctx.resource_manager->load<gl::shader_template>("ui-element-untextured.glsl"));
52  box_material->set_variable("tint", std::make_shared<render::matvar_fvec4>(1, math::fvec4{1, 1, 1, 1}));
53 
54  // Construct selection billboard
55  selection_billboard.set_material(selection_material);
56 
57  // Add box and selection billboard to UI scene
58  ctx.ui_scene->add_object(box_billboard);
59  ctx.ui_scene->add_object(selection_billboard);
60 
61  row_count = 64;
62  column_count = 6;
63  selected_row = 0;
64  selected_column = 0;
65  resize_box();
66 
67  mouse_moved_subscription = ctx.input_manager->get_event_dispatcher().subscribe<input::mouse_moved_event>
68  (
69  [&](const auto& event)
70  {
71 
72  }
73  );
74 
75  mouse_button_pressed_subscription = ctx.input_manager->get_event_dispatcher().subscribe<input::mouse_button_pressed_event>
76  (
77  [&](const auto& event)
78  {
79  const auto& viewport_size = ctx.window->get_viewport_size();
80  const math::fvec2 mouse_position =
81  {
82  static_cast<float>(event.position.x()),
83  static_cast<float>(viewport_size.y() - event.position.y() + 1)
84  };
85 
86  if (box_bounds.contains(mouse_position))
87  {
88  int column = static_cast<int>((mouse_position.x() - box_bounds.min.x()) / selection_size);
89  int row = static_cast<int>((box_bounds.max.y() - mouse_position.y()) / selection_size);
90 
91  if (column != selected_column || row != selected_row)
92  {
93  selected_column = column;
94  selected_row = row;
95 
96  selection_billboard.set_translation
97  (
98  {
99  (box_bounds.min.x() + selection_size * 0.5f) + selection_size * selected_column,
100  (box_bounds.max.y() - selection_size * 0.5f) - selection_size * selected_row ,
101  0.0f
102  }
103  );
104 
105  debug::log_debug("selected colony: ({}, {})", selected_column, selected_row);
106  }
107  }
108  }
109  );
110 
111  window_resized_subscription = ctx.window->get_resized_channel().subscribe
112  (
113  [&](const auto& event)
114  {
115  this->resize_box();
116  }
117  );
118 
119  // Queue enable menu controls
120  //ctx.function_queue.push(std::bind(::enable_menu_controls, std::ref(ctx)));
121 
122  // Fade in from black
123  ctx.fade_transition->transition(config::title_fade_in_duration, true, ease<float>::out_cubic);
124 
125  debug::log_trace("Entered collection menu state");
126 }
127 
129 {
130  debug::log_trace("Exiting collection menu state...");
131 
132  // Destruct menu
133  //::disable_menu_controls(ctx);
134 
135  debug::log_trace("Exited collection menu state");
136 }
137 
138 void collection_menu_state::resize_box()
139 {
140  const float padding = 64.0f;
141  const auto viewport_size = math::fvec2(ctx.window->get_viewport_size());
142 
143  box_bounds.min.x() = viewport_size.x() * 0.5f + padding;
144  box_bounds.max.x() = viewport_size.x() - padding;
145 
146  selection_size = (box_bounds.max.x() - box_bounds.min.x()) / static_cast<float>(column_count);
147 
148  box_bounds.max.y() = viewport_size.y() - padding;
149  box_bounds.min.y() = std::max<float>(padding, box_bounds.max.y() - selection_size * row_count);
150 
151  const math::fvec2 box_size = box_bounds.size();
152  const math::fvec2 box_center = box_bounds.center();
153 
154  // Resize box
155  box_billboard.set_scale({box_size.x() * 0.5f, box_size.y() * 0.5f, 1.0f});
156  box_billboard.set_translation({box_center.x(), box_center.y(), -1.0f});
157 
158  // Resize selection
159  selection_billboard.set_scale({selection_size * 0.5f, selection_size * 0.5f, 1.0f});
160  selection_billboard.set_translation
161  (
162  {
163  (box_bounds.min.x() + selection_size * 0.5f) + selection_size * selected_column,
164  (box_bounds.max.y() - selection_size * 0.5f) - selection_size * selected_row,
165  0.0f
166  }
167  );
168 }
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::unique_ptr< screen_transition > fade_transition
Definition: game.hpp:371
std::unique_ptr< resource_manager > resource_manager
Definition: game.hpp:152
std::shared_ptr< app::window > window
Definition: game.hpp:166
std::unique_ptr< app::input_manager > input_manager
Definition: game.hpp:172
Template used to for generating one or more shader variants from a single source.
void set_material(std::shared_ptr< render::material > material)
Sets the billboard material.
Definition: billboard.cpp:125
void set_scale(const vector_type &scale)
Sets the scale of the object.
Definition: object.hpp:108
void set_translation(const vector_type &translation)
Sets the translation of the object.
Definition: object.hpp:85
log_message< log_message_severity::trace, Args... > log_trace
Formats and logs a trace message.
Definition: log.hpp:88
log_message< log_message_severity::debug, Args... > log_debug
Formats and logs a debug message.
Definition: log.hpp:102
Publish-subscribe messaging.
Definition: channel.hpp:32
User-defined literals for compile-time string hashing.
Definition: fnv1a.hpp:232
@ translucent
Material is translucent.
Container for templated easing functions.
Definition: ease.hpp:71
Event generated when a mouse button has been pressed.
Event generated when a mouse has been moved.
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