forked from schombert/Open-V2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_box.hpp
296 lines (227 loc) · 12.2 KB
/
text_box.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#pragma once
#include "gui.hpp"
#include "gui.h"
#undef max
#undef min
template<typename BASE, int32_t y_adjust>
void ui::display_text<BASE, y_adjust>::update_data(gui_object_tag, world_state& w) {
if constexpr(ui::detail::has_update<BASE, tagged_gui_object, text_data::alignment, ui::text_format&, world_state&>) {
auto temp_holder = w.w.gui_m.gui_objects.emplace();
temp_holder.object.size = associated_object->size;
temp_holder.object.position = associated_object->position;
BASE::update(temp_holder, align, format, w);
ui::replace_children(w.w.gui_m, tagged_gui_object{ *associated_object, self }, temp_holder);
w.w.gui_m.destroy(temp_holder);
} else if constexpr(ui::detail::has_update<BASE, tagged_gui_object, text_box_line_manager&, ui::text_format&, world_state&>) {
auto temp_holder = w.w.gui_m.gui_objects.emplace();
temp_holder.object.size = associated_object->size;
temp_holder.object.position = associated_object->position;
text_box_line_manager lm(align, associated_object->size.x - border_x * 2, line_manager::textbox{});
BASE::update(temp_holder, lm, format, w);
lm.finish_current_line();
ui::for_each_child(w.w.gui_m, temp_holder, [off = xy_pair{ int16_t(border_x), int16_t(border_y) }](tagged_gui_object o) {
o.object.position += off;
});
ui::replace_children(w.w.gui_m, tagged_gui_object{ *associated_object, self }, temp_holder);
w.w.gui_m.destroy(temp_holder);
}
}
template<typename BASE, int32_t x_size_adjust, int32_t y_size_adjust>
void ui::multiline_text<BASE, x_size_adjust, y_size_adjust>::update_data(gui_object_tag, world_state& w) {
if constexpr(ui::detail::has_update < BASE, tagged_gui_object, line_manager &, ui::text_format&, world_state& > ) {
tagged_gui_object content_frame{ w.w.gui_m.gui_objects.at(this->scrollable_region), this->scrollable_region};
auto temp_holder = w.w.gui_m.gui_objects.emplace();
temp_holder.object.size = content_frame.object.size;
temp_holder.object.position = content_frame.object.position;
line_manager lm(align, content_frame.object.size.x);
BASE::update(temp_holder, lm, format, w);
lm.finish_current_line();
ui::replace_children(w.w.gui_m, content_frame, temp_holder);
w.w.gui_m.destroy(temp_holder);
int32_t max_height = 1;
ui::for_each_child(w.w.gui_m, content_frame, [&max_height](tagged_gui_object c) { max_height = std::max(max_height, c.object.size.y + c.object.position.y); });
content_frame.object.size.y = int16_t(max_height);
if(max_height > outer_height) {
sb.set_range(w.w.gui_m, 0, max_height - outer_height);
sb.update_position(std::min(max_height - outer_height, sb.position()));
content_frame.object.position.y = int16_t(-sb.position());
ui::make_visible_immediate(*sb.associated_object);
} else {
sb.set_range(w.w.gui_m, 0, 0);
content_frame.object.position.y = 0i16;
ui::hide(*sb.associated_object);
}
}
}
template<typename BASE, int32_t x_size_adjust, int32_t y_size_adjust>
bool ui::multiline_text<BASE, x_size_adjust, y_size_adjust>::on_scroll(gui_object_tag t, world_state & ws, const scroll & s) {
if(ws.w.gui_m.gui_objects.at(scrollable_region).size.y > outer_height)
return sb.on_scroll(t, ws, s);
else
return true;
}
template<typename BASE, int32_t y_adjust>
template<typename window_type>
void ui::display_text<BASE, y_adjust>::windowed_update(window_type& w, world_state& s) {
if constexpr(ui::detail::has_windowed_update<BASE, window_type&, tagged_gui_object, text_data::alignment, ui::text_format&, world_state&>) {
auto temp_holder = s.w.gui_m.gui_objects.emplace();
temp_holder.object.size = associated_object->size;
temp_holder.object.position = associated_object->position;
BASE::windowed_update(w, temp_holder, align, format, s);
ui::replace_children(s.w.gui_m, tagged_gui_object{ *associated_object, self }, temp_holder);
s.w.gui_m.destroy(temp_holder);
} if constexpr(ui::detail::has_windowed_update<BASE, window_type&, tagged_gui_object, text_box_line_manager&, ui::text_format&, world_state&>) {
auto temp_holder = s.w.gui_m.gui_objects.emplace();
temp_holder.object.size = associated_object->size;
temp_holder.object.position = associated_object->position;
text_box_line_manager lm(align, associated_object->size.x - border_x * 2, line_manager::textbox{});
BASE::windowed_update(w, temp_holder, lm, format, s);
lm.finish_current_line();
ui::for_each_child(s.w.gui_m, temp_holder, [off = xy_pair{ int16_t(border_x), int16_t(border_y) }](tagged_gui_object o) {
o.object.position += off;
});
ui::replace_children(s.w.gui_m, tagged_gui_object{ *associated_object, self }, temp_holder);
s.w.gui_m.destroy(temp_holder);
}
}
template<typename BASE, int32_t x_size_adjust, int32_t y_size_adjust>
template<typename window_type>
std::enable_if_t<ui::detail::has_windowed_update<BASE, window_type&, ui::tagged_gui_object, ui::line_manager &, ui::text_format&, world_state&>, void> ui::multiline_text<BASE, x_size_adjust, y_size_adjust>::windowed_update(window_type& win, world_state& w) {
if constexpr(ui::detail::has_windowed_update < BASE, window_type&, tagged_gui_object, line_manager &, ui::text_format&, world_state& >) {
tagged_gui_object content_frame{ w.w.gui_m.gui_objects.at(scrollable_region), scrollable_region };
auto temp_holder = w.w.gui_m.gui_objects.emplace();
temp_holder.object.size = content_frame.object.size;
temp_holder.object.position = content_frame.object.position;
line_manager lm(align, content_frame.object.size.x);
BASE::windowed_update(win, temp_holder, lm, format, w);
lm.finish_current_line();
ui::replace_children(w.w.gui_m, content_frame, temp_holder);
w.w.gui_m.destroy(temp_holder);
int32_t max_height = 1;
ui::for_each_child(w.w.gui_m, content_frame, [&max_height](tagged_gui_object c) { max_height = std::max(max_height, c.object.size.y + c.object.position.y); });
content_frame.object.size.y = int16_t(max_height);
if(max_height > outer_height) {
sb.set_range(w.w.gui_m, 0, max_height - outer_height);
sb.update_position(std::min(max_height - outer_height, sb.position()));
content_frame.object.position.y = int16_t(-sb.position());
ui::make_visible_immediate(*sb.associated_object);
} else {
content_frame.object.position.y = 0i16;
ui::hide(*sb.associated_object);
}
}
}
template<typename BASE, int32_t x_size_adjust, int32_t y_size_adjust>
void ui::multiline_text<BASE, x_size_adjust, y_size_adjust>::create_sub_elements(tagged_gui_object self, world_state& ws) {
const auto inner_area = ws.w.gui_m.gui_objects.emplace();
inner_area.object.position = ui::xy_pair{ 0, 0 };
inner_area.object.size = ui::xy_pair{ static_cast<int16_t>(associated_object->size.x - 16), 1i16 };
scrollable_region = inner_area.id;
ui::add_to_back(ws.w.gui_m, self, inner_area);
ui::create_static_fixed_sz_scrollbar(
ws,
ws.s.gui_m.ui_definitions.standardlistbox_slider,
self,
ui::xy_pair{ static_cast<int16_t>(associated_object->size.x - 16), 0i16 },
associated_object->size.y,
sb);
sb.associate(&inner_area.object);
ui::hide(*(sb.associated_object));
}
template<typename B, int32_t x_size_adjust, int32_t y_size_adjust>
ui::tagged_gui_object ui::create_static_element(world_state& ws, ui::text_tag handle, tagged_gui_object parent, multiline_text<B, x_size_adjust, y_size_adjust>& b) {
const ui::text_def& text_def = ws.s.gui_m.ui_definitions.text[handle];
const auto[font_h, is_black, int_font_size] = graphics::unpack_font_handle(text_def.font_handle);
b.set_format(
ui::text_aligment_from_text_definition(text_def),
text_format{ is_black ? ui::text_color::black : ui::text_color::white, font_h, int_font_size });
const auto new_gobj = ws.w.gui_m.gui_objects.emplace();
new_gobj.object.associated_behavior = &b;
b.associated_object = &new_gobj.object;
new_gobj.object.position = text_def.position;
new_gobj.object.size = ui::xy_pair{ static_cast<int16_t>(text_def.max_width + x_size_adjust), static_cast<int16_t>(text_def.max_height + y_size_adjust) };
new_gobj.object.align = alignment_from_definition(text_def);
b.set_height(new_gobj.object.size.y);
b.create_sub_elements(new_gobj, ws);
ui::add_to_back(ws.w.gui_m, parent, new_gobj);
if constexpr(ui::detail::has_on_create<multiline_text<B, x_size_adjust, y_size_adjust>, multiline_text<B, x_size_adjust, y_size_adjust>&, world_state&>)
b.on_create(b, ws);
ws.w.gui_m.flag_minimal_update();
return new_gobj;
}
template<typename BASE, int32_t y_adjust>
ui::tooltip_behavior ui::display_text<BASE, y_adjust>::has_tooltip(gui_object_tag, world_state& ws, const mouse_move&) {
if constexpr(ui::detail::has_has_tooltip<BASE, world_state&>)
return BASE::has_tooltip(ws) ? tooltip_behavior::tooltip : tooltip_behavior::no_tooltip;
else
return tooltip_behavior::transparent;
}
template<typename BASE, int32_t y_adjust>
void ui::display_text<BASE, y_adjust>::create_tooltip(gui_object_tag, world_state& ws, const mouse_move&, tagged_gui_object tw) {
if constexpr(ui::detail::has_has_tooltip<BASE, world_state&>)
BASE::create_tooltip(ws, tw);
}
template<typename BASE>
ui::tooltip_behavior ui::fixed_text<BASE>::has_tooltip(gui_object_tag, world_state& ws, const mouse_move&) {
if constexpr(ui::detail::has_has_tooltip<BASE, world_state&>)
return BASE::has_tooltip(ws) ? tooltip_behavior::tooltip : tooltip_behavior::no_tooltip;
else
return tooltip_behavior::transparent;
}
template<typename BASE>
void ui::fixed_text<BASE>::create_tooltip(gui_object_tag, world_state& ws, const mouse_move&, tagged_gui_object tw) {
if constexpr(ui::detail::has_has_tooltip<BASE, world_state&>)
BASE::create_tooltip(ws, tw);
}
template<typename BASE, int32_t y_adjust>
void ui::display_text<BASE, y_adjust>::set_visibility(gui_manager& m, bool visible) {
if(visible)
ui::make_visible(m, *associated_object);
else
ui::hide(*associated_object);
}
template<typename B, int32_t y_adjust>
ui::tagged_gui_object ui::create_static_element(world_state& ws, ui::text_tag handle, tagged_gui_object parent, display_text<B, y_adjust>& b) {
const ui::text_def& text_def = ws.s.gui_m.ui_definitions.text[handle];
const auto[font_h, is_black, int_font_size] = graphics::unpack_font_handle(text_def.font_handle);
b.set_format(
ui::text_aligment_from_text_definition(text_def),
text_format{ is_black ? ui::text_color::black : ui::text_color::white, font_h, int_font_size });
const auto new_gobj = ui::detail::create_element_instance(ws, handle);
b.set_self(new_gobj.id);
new_gobj.object.associated_behavior = &b;
b.associated_object = &new_gobj.object;
new_gobj.object.position = text_def.position;
new_gobj.object.size = ui::xy_pair{ static_cast<int16_t>(text_def.max_width), static_cast<int16_t>(text_def.max_height + y_adjust) };
new_gobj.object.align = alignment_from_definition(text_def);
b.border_x = text_def.border_size.x;
b.border_y = text_def.border_size.y;
//graphics::font& this_font = ws.s.gui_m.fonts.at(font_h);
/*auto align_result = align_in_bounds(text_data::alignment::center,
1, int32_t(this_font.line_height(ui::detail::font_size_to_render_size(this_font, static_cast<int32_t>(b.format.font_size)))),
int32_t(text_def.max_width), int32_t(text_def.max_height + y_adjust));
b.border_y = align_result.second;*/
ui::add_to_back(ws.w.gui_m, parent, new_gobj);
if constexpr(ui::detail::has_on_create<display_text<B, y_adjust>, display_text<B, y_adjust>&, world_state&>)
b.on_create(b, ws);
else if constexpr(ui::detail::has_on_create<display_text<B, y_adjust>, tagged_gui_object, text_box_line_manager&, ui::text_format&, world_state&>) {
ui::clear_children(ws.w.gui_m, new_gobj);
text_box_line_manager lm(b.align, text_def.max_width - b.border_x * 2, line_manager::textbox{});
b.on_create(new_gobj, lm, b.format, ws);
ui::for_each_child(ws.w.gui_m, new_gobj, [off = text_def.border_size](tagged_gui_object o) {
o.object.position += off;
});
}
ws.w.gui_m.flag_minimal_update();
return new_gobj;
}
template<typename B>
ui::tagged_gui_object ui::create_static_element(world_state& ws, ui::text_tag handle, tagged_gui_object parent, fixed_text<B>& b) {
const auto new_gobj = ui::detail::create_element_instance(ws, handle);
new_gobj.object.associated_behavior = &b;
b.associated_object = &new_gobj.object;
ui::add_to_back(ws.w.gui_m, parent, new_gobj);
if constexpr(ui::detail::has_on_create<fixed_text<B>, fixed_text<B>&, world_state&>)
b.on_create(b, ws);
return new_gobj;
}