Skip to content

Commit 70edd59

Browse files
author
s.kushnirenko
committed
ui: unify widget system
1 parent c9205b4 commit 70edd59

File tree

8 files changed

+157
-43
lines changed

8 files changed

+157
-43
lines changed

src/graphics/elements/lang_text.cpp

+1-7
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,7 @@ int lang_text_draw_year(int year, int x_offset, int y_offset, font_t font) {
8181
}
8282
return width;
8383
}
84-
void lang_text_draw_month_year_max_width(int month,
85-
int year,
86-
int x_offset,
87-
int y_offset,
88-
int box_width,
89-
font_t font,
90-
color color) {
84+
void lang_text_draw_month_year_max_width(int month, int year, int x_offset, int y_offset, int box_width, font_t font, color color) {
9185
int month_width = lang_text_get_width(25, month, font);
9286
int ad_bc_width = lang_text_get_width(20, year >= 0 ? 1 : 0, font);
9387
int space_width = font_definition_for(font)->space_width;

src/graphics/elements/lang_text.h

+1-9
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,4 @@ inline int lang_text_draw_amount(int group, int number, int amount, vec2i offset
3030
int lang_text_draw_year(int year, int x_offset, int y_offset, font_t font);
3131
void lang_text_draw_month_year_max_width(int month, int year, int x_offset, int y_offset, int box_width, font_t font, color color);
3232

33-
int lang_text_draw_multiline(int group, int number, vec2i offset, int box_width, font_t font);
34-
35-
namespace ui {
36-
int label(int group, int number, vec2i pos, font_t font = FONT_NORMAL_BLACK_ON_LIGHT, UiFlags_ flags = UiFlags_None, int box_width = 0);
37-
int label(pcstr, vec2i pos, font_t font = FONT_NORMAL_BLACK_ON_LIGHT, UiFlags_ flags = UiFlags_None, int box_width = 0);
38-
int label_amount(int group, int number, int amount, vec2i pos, font_t font = FONT_NORMAL_BLACK_ON_LIGHT, pcstr postfix = "");
39-
int label_percent(int amount, vec2i pos, font_t font = FONT_NORMAL_BLACK_ON_LIGHT);
40-
void image(e_image_id img, vec2i pos);
41-
}
33+
int lang_text_draw_multiline(int group, int number, vec2i offset, int box_width, font_t font);

src/graphics/elements/panel.h

+1-7
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,4 @@ void unbordered_panel_draw(int x, int y, int width_blocks, int height_blocks);
1818
void label_draw(int x, int y, int width_blocks, int type);
1919
void label_draw_colored(painter &ctx, int x, int y, int width_blocks, int type, uint32_t mask);
2020

21-
void large_label_draw(int x, int y, int width_blocks, int type);
22-
23-
namespace ui {
24-
void panel(vec2i pos, vec2i size, UiFlags_ flags);
25-
void icon(vec2i pos, e_resource img);
26-
void icon(vec2i pos, e_advisor advisor);
27-
}
21+
void large_label_draw(int x, int y, int width_blocks, int type);

src/graphics/elements/ui.cpp

+49-3
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ int ui::label_percent(int amount, vec2i pos, font_t font) {
125125
return text_draw_percentage(amount, offset.x + pos.x, offset.y + pos.y, font);
126126
}
127127

128-
void ui::image(e_image_id img, vec2i pos) {
128+
void ui::eimage(e_image_id img, vec2i pos) {
129129
painter ctx = game.painter();
130130
ImageDraw::img_generic(ctx, image_group(img), pos);
131131
}
@@ -149,12 +149,58 @@ void ui::icon(vec2i pos, e_advisor adv) {
149149
ImageDraw::img_generic(ctx, image_group(IMG_ADVISOR_ICONS) + (adv - 1), offset.x + pos.x, offset.y + pos.y);
150150
}
151151

152-
153152
arrow_button &ui::arw_button(vec2i pos, bool up, bool tiny) {
154153
const vec2i offset = g_state.offset();
155154

156155
g_state.arw_buttons.push_back({pos.x, pos.y, up ? 17 : 15, 24, button_none, 0, 0});
157156
arrow_buttons_draw(offset, g_state.arw_buttons.back(), tiny);
158157

159158
return g_state.arw_buttons.back();
160-
}
159+
}
160+
161+
void ui::element::load(archive arch) {
162+
pos = arch.r_vec2i("pos");
163+
size = arch.r_size2i("size");
164+
}
165+
166+
void ui::outer_panel::draw() {
167+
ui::panel(pos, size, UiFlags_PanelOuter);
168+
}
169+
170+
void ui::outer_panel::load(archive arch) {
171+
pcstr type = arch.r_string("type");
172+
assert(!strcmp(type, "outer_panel"));
173+
174+
element::load(arch);
175+
}
176+
177+
void ui::widget::draw() {
178+
for (auto &e : elements) {
179+
e->draw();
180+
}
181+
}
182+
183+
void ui::widget::load(archive arch) {
184+
elements.clear();
185+
arch.r_objects("ui", [this] (pcstr key, archive elem) {
186+
pcstr type = elem.r_string("type");
187+
if (!strcmp(type, "outer_panel")) {
188+
elements.push_back(std::make_shared<outer_panel>());
189+
elements.back()->load(elem);
190+
} else if (!strcmp(type, "image")) {
191+
elements.push_back(std::make_shared<image>());
192+
elements.back()->load(elem);
193+
}
194+
});
195+
}
196+
197+
void ui::image::draw() {
198+
ui::icon(pos, ADVISOR_RATINGS);
199+
}
200+
201+
void ui::image::load(archive arch) {
202+
pcstr type = arch.r_string("type");
203+
assert(!strcmp(type, "image"));
204+
img = arch.r_image("image");
205+
element::load(arch);
206+
}

src/graphics/elements/ui.h

+45-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
#include <cstdint>
44

55
#include "core/vec2i.h"
6+
#include "city/resource.h"
67

78
#include "graphics/elements/generic_button.h"
89
#include "graphics/elements/image_button.h"
10+
#include "graphics/elements/lang_text.h"
11+
#include "graphics/elements/panel.h"
912
#include "graphics/image_groups.h"
1013

14+
#include "js/js_game.h"
15+
1116
struct mouse;
1217

1318
enum UiFlags_ {
@@ -22,6 +27,45 @@ void begin_frame();
2227
void begin_widget(vec2i offset, bool relative = false);
2328
void end_widget();
2429
bool handle_mouse(const mouse *m);
30+
31+
int label(int group, int number, vec2i pos, font_t font = FONT_NORMAL_BLACK_ON_LIGHT, UiFlags_ flags = UiFlags_None, int box_width = 0);
32+
int label(pcstr, vec2i pos, font_t font = FONT_NORMAL_BLACK_ON_LIGHT, UiFlags_ flags = UiFlags_None, int box_width = 0);
33+
int label_amount(int group, int number, int amount, vec2i pos, font_t font = FONT_NORMAL_BLACK_ON_LIGHT, pcstr postfix = "");
34+
int label_percent(int amount, vec2i pos, font_t font = FONT_NORMAL_BLACK_ON_LIGHT);
35+
void eimage(e_image_id img, vec2i pos);
36+
void panel(vec2i pos, vec2i size, UiFlags_ flags);
37+
void icon(vec2i pos, e_resource img);
38+
void icon(vec2i pos, e_advisor advisor);
39+
2540
pcstr str(int group, int id);
2641

27-
}
42+
struct element {
43+
vec2i pos;
44+
vec2i size;
45+
46+
virtual void draw() {}
47+
virtual void load(archive elem);
48+
49+
using ptr = std::shared_ptr<element>;
50+
};
51+
52+
struct image : public element {
53+
e_image_id img;
54+
55+
virtual void draw() override;
56+
virtual void load(archive elem) override;
57+
};
58+
59+
struct outer_panel : public element {
60+
virtual void draw() override;
61+
virtual void load(archive elem) override;
62+
};
63+
64+
struct widget {
65+
std::vector<element::ptr> elements;
66+
67+
virtual void draw();
68+
virtual void load(archive arch);
69+
};
70+
71+
} // ui

src/js/js_game.h

+40
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#pragma once
22

33
#include "mujs/mujs.h"
4+
45
#include "core/bstring.h"
56
#include "js/js_defines.h"
67
#include "core/vec2i.h"
78

89
#include <vector>
10+
#include <string>
911

1012
enum e_image_id : uint16_t;
1113

@@ -20,6 +22,23 @@ struct archive {
2022
return result;
2123
}
2224

25+
inline std::vector<std::string> r_array_str(pcstr name) {
26+
js_getproperty(vm, -1, name);
27+
std::vector<std::string> result;
28+
if (js_isarray(vm, -1)) {
29+
int length = js_getlength(vm, -1);
30+
31+
for (int i = 0; i < length; ++i) {
32+
js_getindex(vm, -1, i);
33+
std::string v = js_tostring(vm, -1);
34+
result.push_back(v);
35+
js_pop(vm, 1);
36+
}
37+
js_pop(vm, 1);
38+
}
39+
return result;
40+
}
41+
2342
template<typename T = int>
2443
inline std::vector<T> r_array_num(pcstr name) {
2544
js_getproperty(vm, -1, name);
@@ -83,6 +102,27 @@ struct archive {
83102
js_pop(vm, 1);
84103
}
85104

105+
template<typename T>
106+
inline void r_objects(pcstr name, T read_func) {
107+
this->r_section(name, [&read_func] (archive s_arch) {
108+
pcstr key;
109+
std::vector<bstring128> keys;
110+
js_pushiterator(s_arch.vm, -1, 1);
111+
while (key = js_nextiterator(s_arch.vm, -1)) {
112+
keys.push_back(key);
113+
}
114+
js_pop(s_arch.vm, 1);
115+
116+
for (const auto &key : keys) {
117+
js_getproperty(s_arch.vm, -1, key.c_str());
118+
if (js_isobject(s_arch.vm, -1)) {
119+
read_func(key.c_str(), s_arch.vm);
120+
}
121+
js_pop(s_arch.vm, 1);
122+
}
123+
});
124+
}
125+
86126
protected:
87127
template<typename T>
88128
inline bool r_array_impl(T read_func) {

src/scripts/ui.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,18 @@ main_menu_window = {
2727
}
2828

2929
advisor_rating_window = {
30-
outer_panel_pos : {x:0, y:0},
31-
outer_panel_size : {w:40, h:27},
32-
advisor_icon_image : IMG_ADVISOR_RATING_ICON,
33-
advisor_icon_pos : {x: 10, y: 10},
30+
ui : {
31+
outer_panel : {
32+
type : "outer_panel",
33+
pos : {x:0, y:0},
34+
size : {w:40, h:27},
35+
},
36+
advisor_icon : {
37+
type : "image",
38+
image : IMG_ADVISOR_RATING_ICON,
39+
pos : {x: 10, y: 10},
40+
},
41+
},
3442
header_pos : {x:60, y:17},
3543
header_population_pos : {x: 160, y:20},
3644
background_img : IMG_ADVISOR_BACKGROUND,

src/window/advisor/advisor_ratings.cpp

+8-12
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
#include "city/ratings.h"
44
#include "graphics/boilerplate.h"
5-
#include "graphics/elements/generic_button.h"
6-
#include "graphics/elements/lang_text.h"
75
#include "graphics/view/view.h"
8-
#include "graphics/elements/panel.h"
6+
#include "graphics/elements/ui.h"
97
#include "graphics/text.h"
108
#include "graphics/window.h"
119
#include "config/config.h"
@@ -19,9 +17,7 @@ ANK_REGISTER_CONFIG_ITERATOR(config_load_advisor_rating);
1917

2018
static void button_rating(int rating, int param2);
2119

22-
struct advisor_rating_window {
23-
vec2i outer_panel_pos;
24-
vec2i outer_panel_size;
20+
struct advisor_rating_window : public ui::widget {
2521
e_image_id advisor_icon_image;
2622
vec2i advisor_icon_pos;
2723
vec2i header_pos;
@@ -35,9 +31,9 @@ advisor_rating_window g_advisor_rating_window;
3531

3632
void config_load_advisor_rating() {
3733
g_config_arch.r_section("advisor_rating_window", [] (archive arch) {
34+
g_advisor_rating_window.load(arch);
35+
3836
auto &w = g_advisor_rating_window;
39-
w.outer_panel_pos = arch.r_vec2i("outer_panel_pos");
40-
w.outer_panel_size = arch.r_size2i("outer_panel_size");
4137
w.advisor_icon_image = arch.r_image("advisor_icon_image");
4238
w.advisor_icon_pos = arch.r_vec2i("advisor_icon_pos");
4339
w.header_pos = arch.r_vec2i("header_pos");
@@ -92,13 +88,13 @@ static void draw_rating(int id, int value, int open_play, int goal) {
9288
}
9389

9490
static int draw_background() {
95-
return g_advisor_rating_window.outer_panel_size.y;
91+
return 0;// g_advisor_rating_window.outer_panel_size.y;
9692
}
9793

9894
static void draw_foreground() {
9995
auto &w = g_advisor_rating_window;
100-
ui::panel(w.outer_panel_pos, w.outer_panel_size, UiFlags_PanelOuter);
101-
ui::icon(w.advisor_icon_pos, ADVISOR_RATINGS);
96+
97+
g_advisor_rating_window.draw();
10298

10399
ui::label(53, 0, w.header_pos, FONT_LARGE_BLACK_ON_LIGHT);
104100
bstring128 caption = (pcstr)ui::str(53, 7);
@@ -108,7 +104,7 @@ static void draw_foreground() {
108104
}
109105

110106
ui::label(caption, w.header_population_pos, FONT_NORMAL_BLACK_ON_LIGHT);
111-
ui::image(w.background_img, w.background_img_pos);
107+
ui::eimage(w.background_img, w.background_img_pos);
112108

113109
int open_play = scenario_is_open_play();
114110

0 commit comments

Comments
 (0)