Project Alice
Loading...
Searching...
No Matches
immediate_mode.cpp
Go to the documentation of this file.
1#include "immediate_mode.hpp"
2#include "system_state.hpp"
3
4namespace iui {
5
6void shrink(rect& rectangle, float value) {
7 rectangle.x += value;
8 rectangle.y += value;
9 rectangle.w -= value * 2.f;
10 rectangle.h -= value * 2.f;
11};
12
13void move_to(rect& rectangle, float x, float y) {
14 rectangle.x = x;
15 rectangle.y = y;
16};
17
18rect subrect(rect& rectangle, float w, float h, alignment_horizontal align_hor, alignment_vertical align_vert) {
19 rect result{ 0.f, 0.f, w, h };
20
21 switch(align_hor) {
23 break;
25 result.x = rectangle.x + rectangle.w / 2.f - w / 2.f;
26 break;
28 result.x = rectangle.x + rectangle.w - w;
29 break;
30 default:
31 break;
32 }
33
34 switch(align_vert) {
36 break;
38 result.y = rectangle.y + rectangle.h / 2.f - h / 2.f;
39 break;
41 result.y = rectangle.y + rectangle.h - h;
42 break;
43 default:
44 break;
45 }
46
47 return result;
48};
49
51 sys::state& state,
52 rect& r,
53 float red, float green, float blue
54) {
55 if(state.mouse_x_position / state.user_settings.ui_scale >= r.x && state.mouse_x_position / state.user_settings.ui_scale <= r.x + r.w) {
56 if(state.mouse_y_position / state.user_settings.ui_scale >= r.y && state.mouse_y_position / state.user_settings.ui_scale <= r.y + r.h) {
57 over_ui = true;
58 }
59 }
60
62 state,
63 r.x , r.y , r.w , r.h , red, green, blue,
64 ui::rotation::upright, false, false
65 );
66}
67
69 sys::state& state,
70 rect& r
71) {
72 if(state.mouse_x_position / state.user_settings.ui_scale >= r.x && state.mouse_x_position / state.user_settings.ui_scale <= r.x + r.w) {
73 if(state.mouse_y_position / state.user_settings.ui_scale >= r.y && state.mouse_y_position / state.user_settings.ui_scale <= r.y + r.h) {
74 over_ui = true;
75 }
76 }
77
79 state,
80 r.x , r.y , r.w , r.h , 0.f, 0.f, 0.f,
81 ui::rotation::upright, false, false
82 );
83
85 state,
86 r.x + 1.f, r.y + 1.f, r.w - 2.f, r.h - 2.f,
87 ui::rotation::upright, false, false
88 );
89}
90
92 sys::state& state,
93 rect& r,
94 int frame, int total_frames,
95 GLuint texture_handle
96) {
97 if(state.mouse_x_position / state.user_settings.ui_scale >= r.x && state.mouse_x_position / state.user_settings.ui_scale <= r.x + r.w) {
98 if(state.mouse_y_position / state.user_settings.ui_scale >= r.y && state.mouse_y_position / state.user_settings.ui_scale <= r.y + r.h) {
99 over_ui = true;
100 }
101 }
102
104 state, ogl::color_modification::none, frame, total_frames,
105 r.x , r.y , r.w , r.h ,
106 texture_handle,
107 ui::rotation::upright, false, false
108 );
109}
110
112 sys::state& state,
113 rect& r,
114 GLuint texture_handle
115) {
116 if(state.mouse_x_position / state.user_settings.ui_scale >= r.x && state.mouse_x_position / state.user_settings.ui_scale <= r.x + r.w) {
117 if(state.mouse_y_position / state.user_settings.ui_scale >= r.y && state.mouse_y_position / state.user_settings.ui_scale <= r.y + r.h) {
118 over_ui = true;
119 }
120 }
121
124 r.x, r.y, r.w, r.h,
125 texture_handle,
126 ui::rotation::upright, false, false
127 );
128}
129
131 sys::state& state, int32_t identifier,
132 rect& r,
133 bool active
134) {
135 bool result = state.iui_state.active_element == identifier;
136
137 if(active) {
138 panel_colored(state, r, 1.0f, 0.7f, 0.7f);
139 } else {
140 panel(state, r);
141 }
142
143 if(!(state.mouse_x_position / state.user_settings.ui_scale > r.x && state.mouse_x_position / state.user_settings.ui_scale < r.x + r.w)) {
144 return false;
145 }
146 if(!(state.mouse_y_position / state.user_settings.ui_scale > r.y && state.mouse_y_position / state.user_settings.ui_scale < r.y + r.h)) {
147 return false;
148 }
149
150 panel_colored(state, r, 0.8f, 0.8f, 0.8f);
151 state.iui_state.hovered_element = identifier;
152
153 if(state.iui_state.mouse_pressed) {
154 if(state.iui_state.active_element == identifier) {
155 panel_colored(state, r, 0.9f, 0.9f, 0.9f);
156 state.iui_state.mouse_pressed = false;
157 return true;
158 }
159 state.iui_state.active_element = identifier;
160 }
161
162 return false;
163}
164
165// currently assumes that total frames == 3
167 sys::state& state, int32_t identifier,
168 rect& r,
169 int total_frames,
170 GLuint texture_handle,
171 bool active
172) {
173 bool result = state.iui_state.active_element == identifier;
174
175 if(active) {
176 panel_subsprite(state, r, 2, 3, texture_handle);
177 } else {
178 panel_subsprite(state, r, 0, 3, texture_handle);
179 }
180
181 if(!(state.mouse_x_position / state.user_settings.ui_scale > r.x && state.mouse_x_position / state.user_settings.ui_scale < r.x + r.w)) {
182 return false;
183 }
184 if(!(state.mouse_y_position / state.user_settings.ui_scale > r.y && state.mouse_y_position / state.user_settings.ui_scale < r.y + r.h)) {
185 return false;
186 }
187
188 panel_subsprite(state, r, 1, 3, texture_handle);
189 state.iui_state.hovered_element = identifier;
190
191 if(state.iui_state.mouse_pressed) {
192 if(state.iui_state.active_element == identifier) {
193 panel_subsprite(state, r, 2, 3, texture_handle);
194 state.iui_state.mouse_pressed = false;
195 return true;
196 }
197 state.iui_state.active_element = identifier;
198 }
199
200 return false;
201}
202
204 sys::state& state,
205 text::layout& layout,
206 uint16_t font_handle,
207 text::alignment align, float w, float h
208) {
210 state,
211 layout,
213 .left = 0,
214 .top = 0,
215 .right = (int16_t)w,
216 .bottom = (int16_t)h,
217 .font_id = font_handle,
218 .leading = 0,
219 .align = align,
221 }
222 );
223}
224
226 sys::state& state,
227 std::vector<text::text_chunk>& content,
228 rect& r, ogl::color3f color
229) {
230 auto font_size = state.font_collection.line_height(state, current_font);
231 auto text_height = 0.f;
232
233 for(auto& txt : content) {
234 text_height = std::max(text_height, txt.y + font_size);
235 }
236
237 for(auto& txt : content) {
239 state,
240 txt,
241 (r.x + txt.x) ,
242 (r.y + txt.y + r.h / 2.f - text_height / 2.f) ,
244 color,
246 );
247 }
248}
249
251 sys::state& state, int32_t identifier,
252 rect& r,
253 float value
254) {
255 if(auto it = gui_float_map.find(identifier); it != gui_float_map.end()) {
256 if(gui_float_map[identifier] == value) {
257 render_text(state, gui_text_chunks_map[identifier], r, { 0.f, 0.f, 0.f });
258 return;
259 }
260 }
261
262 auto layout = text::layout{};
263 auto contents = init_layout(state, layout, current_font, text::alignment::right, r.w, r.h);
264 auto box = text::open_layout_box(contents);
266 state,
267 contents,
268 box,
269 text::fp_two_places{ value }
270 );
271 text::close_layout_box(contents, box);
272 render_text(state, contents.base_layout.contents, r, { 0.f, 0.f, 0.f });
273
274 gui_text_chunks_map.insert_or_assign(identifier, std::move(layout.contents));
275 gui_float_map[identifier] = value;
276}
277
279 sys::state& state, int32_t identifier,
280 rect& r,
281 int32_t value
282) {
283 if(auto it = gui_int_map.find(identifier); it != gui_int_map.end()) {
284 if(gui_int_map[identifier] == value) {
285 render_text(state, gui_text_chunks_map[identifier], r, { 0.f, 0.f, 0.f });
286 return;
287 }
288 }
289
290 auto layout = text::layout{};
291 auto contents = init_layout(state, layout, current_font, text::alignment::center, r.w, r.h);
292 auto box = text::open_layout_box(contents);
294 state,
295 contents,
296 box,
297 text::int_wholenum{ value }
298 );
299 text::close_layout_box(contents, box);
300 render_text(state, contents.base_layout.contents, r, { 0.f, 0.f, 0.f });
301
302 gui_text_chunks_map.insert_or_assign(identifier, std::move(layout.contents));
303 gui_int_map[identifier] = value;
304}
305
307 sys::state& state, int32_t identifier,
308 rect& r,
309 float value
310) {
311 if(auto it = gui_float_map.find(identifier); it != gui_float_map.end()) {
312 if(gui_float_map[identifier] == value) {
313 render_text(state, gui_text_chunks_map[identifier], r, { 0.f, 0.f, 0.f });
314 return;
315 }
316 }
317
318 auto layout = text::layout{};
319 auto contents = init_layout(state, layout, current_font, text::alignment::right, r.w, r.h);
320 auto box = text::open_layout_box(contents);
322 state,
323 contents,
324 box,
325 text::fp_currency{ value }
326 );
327 text::close_layout_box(contents, box);
328 render_text(state, contents.base_layout.contents, r, { 0.f, 0.f, 0.f });
329
330 gui_text_chunks_map.insert_or_assign(identifier, std::move(layout.contents));
331 gui_float_map[identifier] = value;
332}
333
335 sys::state& state, int32_t identifier,
336 rect& r,
337 std::string_view key,
338 ogl::color3f color
339) {
340 if(auto it = gui_text_map.find(identifier); it != gui_text_map.end()) {
341 if(gui_text_map[identifier] == key) {
342 render_text(state, gui_text_chunks_map[identifier], r, color);
343 return;
344 }
345 }
346
347 auto layout = text::layout{};
348 auto contents = init_layout(state, layout, current_font, text::alignment::left, r.w, r.h);
349 auto box = text::open_layout_box(contents);
350 text::localised_format_box(state, contents, box, key);
351 text::close_layout_box(contents, box);
352 render_text(state, contents.base_layout.contents, r, color);
353
354 gui_text_chunks_map.insert_or_assign(identifier, std::move(layout.contents));
355 gui_text_map[identifier] = key;
356}
357
359 sys::state& state, int32_t identifier,
360 rect r,
361 std::string_view key,
362 ogl::color3f color
363) {
364 iui_state::localized_string(state, identifier, r, key, color);
365}
366
367
368void iui_state::load_description(sys::state& state, std::string_view name, element_description& local_desc) {
369 auto key = state.lookup_key(name);
370 auto def = state.ui_state.defs_by_name.find(key)->second.definition;
371 auto& desc = state.ui_defs.gui[def];
372 local_desc.w = desc.size.x;
373 local_desc.h = desc.size.y;
374 auto& gfx_def = state.ui_defs.gfx[desc.data.button.button_image];
375 auto handle = ogl::get_texture_handle(state, gfx_def.primary_texture_handle, gfx_def.is_partially_transparent());
376 local_desc.texture_handle = handle;
377}
378}
void shrink(rect &rectangle, float value)
alignment_vertical
alignment_horizontal
rect subrect(rect &rectangle, float w, float h, alignment_horizontal align_hor, alignment_vertical align_vert)
void move_to(rect &rectangle, float x, float y)
void render_colored_rect(sys::state const &state, float x, float y, float width, float height, float red, float green, float blue, ui::rotation r, bool flipped, bool rtl)
void render_subsprite(sys::state const &state, color_modification enabled, int frame, int total_frames, float x, float y, float width, float height, GLuint texture_handle, ui::rotation r, bool flipped, bool rtl)
void render_simple_rect(sys::state const &state, float x, float y, float width, float height, ui::rotation r, bool flipped, bool rtl)
GLuint get_texture_handle(sys::state &state, dcon::texture_id id, bool keep_data)
Definition: texture.cpp:1066
void add_to_layout_box(sys::state &state, layout_base &dest, layout_box &box, embedded_flag ico)
Definition: text.cpp:1165
layout_box open_layout_box(layout_base &dest, int32_t indent)
Definition: text.cpp:1823
alignment
Definition: text.hpp:36
void localised_format_box(sys::state &state, layout_base &dest, layout_box &box, std::string_view key, text::substitution_map const &sub)
Definition: text.cpp:1904
endless_layout create_endless_layout(sys::state &state, layout &dest, layout_parameters const &params)
Definition: text.cpp:1100
void close_layout_box(columnar_layout &dest, layout_box &box)
Definition: text.cpp:1831
void render_text_chunk(sys::state &state, text::text_chunk t, float x, float baseline_y, uint16_t font_id, ogl::color3f text_color, ogl::color_modification cmod)
void load_description(sys::state &state, std::string_view name, element_description &local_desc)
ankerl::unordered_dense::map< int32_t, std::vector< text::text_chunk > > gui_text_chunks_map
void localized_string(sys::state &state, int32_t identifier, rect &r, std::string_view key, ogl::color3f color={ 0.f, 0.f, 0.f })
void render_text(sys::state &state, std::vector< text::text_chunk > &content, rect &r, ogl::color3f color)
void int_whole(sys::state &state, int32_t identifier, rect &r, int32_t value)
text::endless_layout init_layout(sys::state &state, text::layout &layout, uint16_t font_handle, text::alignment align, float w, float h)
ankerl::unordered_dense::map< int32_t, int32_t > gui_int_map
ankerl::unordered_dense::map< int32_t, float > gui_float_map
bool button(sys::state &state, int32_t identifier, rect &r, bool active)
void panel(sys::state &state, rect &r)
void float_2(sys::state &state, int32_t identifier, rect &r, float value)
void localized_string_r(sys::state &state, int32_t identifier, rect r, std::string_view key, ogl::color3f color={ 0.f, 0.f, 0.f })
void panel_textured(sys::state &state, rect &r, GLuint texture_handle)
ankerl::unordered_dense::map< int32_t, std::string > gui_text_map
bool button_textured(sys::state &state, int32_t identifier, rect &r, int total_frames, GLuint texture_handle, bool active)
void panel_colored(sys::state &state, rect &r, float red, float green, float blue)
void price(sys::state &state, int32_t identifier, rect &r, float value)
void panel_subsprite(sys::state &state, rect &r, int frame, int total_frames, GLuint texture_handle)
uint16_t current_font
Holds important data about the game world, state, and other data regarding windowing,...