Skip to content

Commit 61463de

Browse files
committed
ui: sidebar messages button moved to js config
1 parent a635857 commit 61463de

File tree

9 files changed

+72
-44
lines changed

9 files changed

+72
-44
lines changed

src/graphics/elements/image_button.cpp

+17-3
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,11 @@ bool image_buttons_handle_mouse(const mouse* m, vec2i pos, image_button* buttons
113113
}
114114
hit_button->floating = 0;
115115
hit_button->pressed_since = time_get_millis();
116-
hit_button->left_click_handler(hit_button->parameter1, hit_button->parameter2);
116+
117+
if (hit_button->left_click_handler) {
118+
hit_button->left_click_handler(hit_button->parameter1, hit_button->parameter2);
119+
}
120+
117121
if (hit_button->_onclick) {
118122
hit_button->_onclick(hit_button->parameter1, hit_button->parameter2);
119123
}
@@ -122,14 +126,24 @@ bool image_buttons_handle_mouse(const mouse* m, vec2i pos, image_button* buttons
122126
hit_button->pressed = 1;
123127
hit_button->floating = 0;
124128
hit_button->pressed_since = time_get_millis();
125-
hit_button->right_click_handler(hit_button->parameter1, hit_button->parameter2);
129+
130+
if (hit_button->right_click_handler) {
131+
hit_button->right_click_handler(hit_button->parameter1, hit_button->parameter2);
132+
}
133+
134+
if (hit_button->_onrclick) {
135+
hit_button->_onrclick(hit_button->parameter1, hit_button->parameter2);
136+
}
126137
} else if (hit_button->button_type == IB_SCROLL && m->left.is_down) {
127138
time_millis delay = hit_button->pressed == 2 ? PRESSED_REPEAT_MILLIS : PRESSED_REPEAT_INITIAL_MILLIS;
128139
if (time_get_millis() - hit_button->pressed_since >= delay) {
129140
hit_button->pressed = 2;
130141
hit_button->floating = 0;
131142
hit_button->pressed_since = time_get_millis();
132-
hit_button->left_click_handler(hit_button->parameter1, hit_button->parameter2);
143+
144+
if (hit_button->left_click_handler) {
145+
hit_button->left_click_handler(hit_button->parameter1, hit_button->parameter2);
146+
}
133147
}
134148
}
135149
return true;

src/graphics/elements/image_button.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ struct image_button {
2424
uint32_t image_collection;
2525
uint32_t image_group;
2626
int image_offset;
27-
void (*left_click_handler)(int param1, int param2);
28-
void (*right_click_handler)(int param1, int param2);
27+
void (*left_click_handler)(int param1, int param2) = nullptr;
28+
void (*right_click_handler)(int param1, int param2) = nullptr;
2929
int parameter1;
3030
int parameter2;
3131
bool enabled;
@@ -36,9 +36,10 @@ struct image_button {
3636
time_millis pressed_since;
3737
textid _tooltip;
3838

39-
std::function<void(int,int)> _onclick;
39+
std::function<void(int,int)> _onclick, _onrclick;
4040

4141
template<class Func> image_button &onclick(Func f) { _onclick = f; return *this; }
42+
template<class Func> image_button &onrclick(Func f) { _onrclick = f; return *this; }
4243
inline image_button &tooltip(textid t) { _tooltip = t; return *this; }
4344
inline vec2i pos() const { return {x, y}; }
4445
inline vec2i size() const { return {width, height}; }

src/graphics/elements/menu.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

33
#include "input/mouse.h"
4-
#include "core/bstring.h"
4+
#include "core/xstring.h"
55
#include "core/svector.h"
66
#include <functional>
77

@@ -15,7 +15,7 @@ struct menu_item {
1515
int parameter;
1616
int hidden = false;
1717
bstring64 text;
18-
bstring64 id;
18+
xstring id;
1919
};
2020

2121
struct menu_header {

src/graphics/elements/ui.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,7 @@ void ui::elabel::load(archive arch, element *parent, items &elems) {
708708
_color = arch.r_uint("color");
709709
_wrap = arch.r_int("wrap");
710710
_clip_area = arch.r_bool("clip_area");
711+
_shadow_color = arch.r_uint("shadow");
711712
pcstr talign = arch.r_string("align");
712713
bool multiline = arch.r_bool("multiline");
713714
bool rich = arch.r_bool("rich");
@@ -729,7 +730,7 @@ void ui::elabel::text(pcstr v) {
729730
_text = lang_text_from_key(v);
730731
}
731732

732-
void ui::elabel::color(int v) {
733+
void ui::elabel::text_color(color v) {
733734
_color = v;
734735
}
735736

@@ -845,11 +846,17 @@ void ui::escrollbar::load(archive arch, element *parent, items &elems) {
845846
void ui::etext::draw() {
846847
const vec2i offset = g_state.offset();
847848
if (!!(_flags & UiFlags_AlignCentered)) {
849+
if (_shadow_color) {
850+
text_draw_centered((uint8_t *)_text.c_str(), offset.x + pos.x + 1, offset.y + pos.y, size.x, _font, _shadow_color);
851+
}
848852
text_draw_centered((uint8_t *)_text.c_str(), offset.x + pos.x, offset.y + pos.y, size.x, _font, _color);
849853
} else if (!!(_flags & UiFlags_LabelMultiline)) {
850854
text_draw_multiline((uint8_t *)_text.c_str(), offset.x + pos.x, offset.y + pos.y, _wrap, _font, _color);
851855
} else if (!!(_flags & UiFlags_AlignYCentered)) {
852856
int symbolh = get_letter_height((uint8_t *)"H", _font);
857+
if (_shadow_color) {
858+
text_draw((uint8_t *)_text.c_str(), offset.x + pos.x + 1, offset.y + pos.y + (size.y - symbolh) / 2, _font, _shadow_color);
859+
}
853860
text_draw((uint8_t *)_text.c_str(), offset.x + pos.x, offset.y + pos.y + (size.y - symbolh) / 2, _font, _color);
854861
} else if (!!(_flags & UiFlags_Rich)) {
855862
if (_clip_area) {
@@ -875,6 +882,9 @@ void ui::etext::draw() {
875882
graphics_reset_clip_rectangle();
876883
}
877884
} else {
885+
if (_shadow_color) {
886+
text_draw((uint8_t *)_text.c_str(), offset.x + pos.x + 1, offset.y + pos.y, _font, _shadow_color);
887+
}
878888
text_draw((uint8_t *)_text.c_str(), offset.x + pos.x, offset.y + pos.y, _font, _color);
879889
}
880890
}

src/graphics/elements/ui.h

+9-4
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ struct element {
110110
virtual int text_width() { return 0; }
111111
virtual vec2i pxsize() const { return size; }
112112
inline void text(int font, pcstr v) { this->font(font); this->text(v); }
113-
virtual void color(int) {}
113+
virtual void text_color(color) {}
114114
virtual void image(int) {}
115115
virtual void image(image_desc) {}
116116
virtual image_desc image() const { return {}; }
@@ -122,6 +122,8 @@ struct element {
122122
virtual void onclick(std::function<void(int, int)>) {}
123123
void onclick(std::function<void()> f) { onclick([f] (int, int) { f(); }); }
124124
virtual void onevent(std::function<void()>) {}
125+
virtual void onrclick(std::function<void(int, int)>) {}
126+
void onrclick(std::function<void()> f) { onrclick([f] (int, int) { f(); }); }
125127

126128
virtual emenu_header *dcast_menu_header() { return nullptr; }
127129
virtual eimage_button *dcast_image_button() { return nullptr; }
@@ -232,15 +234,16 @@ struct elabel : public element {
232234
e_font _font;
233235
e_font _link_font;
234236
vec2i _body;
235-
uint32_t _color;
237+
color _color;
238+
color _shadow_color;
236239
UiFlags _flags;
237240
int _wrap;
238241
bool _clip_area;
239242

240243
virtual void draw() override;
241244
virtual void load(archive elem, element *parent, items &elems) override;
242245
virtual void text(pcstr) override;
243-
virtual void color(int) override;
246+
virtual void text_color(color) override;
244247
virtual void font(int) override;
245248
virtual void width(int) override;
246249
};
@@ -312,14 +315,15 @@ struct eimage_button : public element {
312315
int texture_id = -1;
313316
textid _tooltip;
314317

315-
std::function<void(int, int)> _func;
318+
std::function<void(int, int)> _func, _rfunc;
316319

317320
virtual void load(archive elem, element* parent, items &elems) override;
318321
virtual void select(bool v) override { selected = v; }
319322
virtual void draw() override;
320323

321324
using element::onclick;
322325
virtual void onclick(std::function<void(int, int)> func) override { _func = func; }
326+
virtual void onrclick(std::function<void(int, int)> func) override { _rfunc = func; }
323327
virtual void tooltip(textid t) override { _tooltip = t; }
324328

325329
virtual eimage_button *dcast_image_button() override { return this; }
@@ -338,6 +342,7 @@ struct widget {
338342

339343
element& operator[](pcstr id);
340344
inline element &operator[](const bstring32 &id) { return (*this)[id.c_str()]; }
345+
inline element &operator[](const xstring &id) { return (*this)[id.c_str()]; }
341346

342347
template<typename ... Args> int label(const Args ... args) { return ui::label(args...); }
343348
template<typename ... Args> generic_button &button(const Args ... args) { return ui::button(args...); }

src/scripts/ui.js

+2
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,8 @@ sidebar_window = {
836836
overlay_text : text_center({pos:[15, 30], size:[117, 20]}),
837837

838838
goto_problem : image_button({pos:[86, 434], pack:PACK_GENERAL, id:136, offset:56}),
839+
show_messages : image_button({pos:[46, 434], pack:PACK_GENERAL, id:136, offset:52}),
840+
num_messages : text({pos: [52, 450], shadow:0xff000000, font:FONT_NORMAL_BLACK_ON_DARK, color:0xffffffff }),
839841
}
840842
}
841843

src/widget/top_menu_game.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ struct top_menu_data_t {
6464
int offset_rotate;
6565
int offset_rotate_basic;
6666

67-
bstring64 open_sub_menu;
68-
bstring64 focus_menu_id;
69-
bstring64 focus_sub_menu_id;
67+
xstring open_sub_menu;
68+
xstring focus_menu_id;
69+
xstring focus_sub_menu_id;
7070

7171
int population;
7272
int treasury;
@@ -148,7 +148,7 @@ void config_load_top_menu_bar() {
148148
for (auto &header : data.headers.elements) {
149149
auto impl = header->dcast_menu_header();
150150
if (impl) {
151-
impl->load_items(arch, header->id);
151+
impl->load_items(arch, header->id.c_str());
152152
}
153153
}
154154
});
@@ -263,7 +263,7 @@ void top_menu_bar_draw() {
263263
}
264264
}
265265

266-
static bstring64 top_menu_bar_get_selected_header(const mouse* m) {
266+
static xstring top_menu_bar_get_selected_header(const mouse* m) {
267267
auto& data = g_top_menu_data;
268268
auto &headers = g_top_menu_data.headers;
269269
for (auto &it : headers.elements) {
@@ -280,7 +280,7 @@ static bstring64 top_menu_bar_get_selected_header(const mouse* m) {
280280
return {};
281281
}
282282

283-
static bstring64 top_menu_bar_handle_mouse(const mouse* m) {
283+
static xstring top_menu_bar_handle_mouse(const mouse* m) {
284284
g_top_menu_data.focus_menu_id = top_menu_bar_get_selected_header(m);
285285
return top_menu_bar_get_selected_header(m);
286286
}
@@ -304,7 +304,7 @@ static void top_menu_calculate_menu_dimensions(menu_header& menu) {
304304
menu.calculated_height_blocks = height_pixels / 16;
305305
}
306306

307-
void top_menu_menu_draw(const bstring64 &header, const bstring64 &focus_item_id) {
307+
void top_menu_menu_draw(const xstring header, const xstring focus_item_id) {
308308
auto& menu = g_top_menu_data;
309309
auto &impl = ((ui::emenu_header *)&menu.headers[header])->impl;
310310

@@ -324,7 +324,7 @@ void top_menu_menu_draw(const bstring64 &header, const bstring64 &focus_item_id)
324324
}
325325
}
326326

327-
static bstring64 top_menu_get_subitem(const mouse* m, menu_header &menu) {
327+
static xstring top_menu_get_subitem(const mouse* m, menu_header &menu) {
328328
auto& data = g_top_menu_data;
329329
int y_offset = TOP_MENU_HEIGHT + data.offset.y * 2;
330330

@@ -343,12 +343,12 @@ static bstring64 top_menu_get_subitem(const mouse* m, menu_header &menu) {
343343
return {};
344344
}
345345

346-
bstring64 top_menu_menu_handle_mouse(const mouse* m, menu_header* menu, bstring64& focus_item_id) {
346+
xstring top_menu_menu_handle_mouse(const mouse* m, menu_header* menu, xstring& focus_item_id) {
347347
if (!menu) {
348348
return "";
349349
}
350350

351-
bstring64 item_id = top_menu_get_subitem(m, *menu);
351+
xstring item_id = top_menu_get_subitem(m, *menu);
352352
focus_item_id = item_id;
353353

354354
if (!item_id) {
@@ -781,7 +781,7 @@ static bool widget_top_menu_handle_input_submenu(const mouse* m, const hotkeys*
781781
return true;
782782
}
783783

784-
bstring64 menu_id = top_menu_bar_handle_mouse(m);
784+
xstring menu_id = top_menu_bar_handle_mouse(m);
785785
if (!!menu_id && menu_id != data.open_sub_menu) {
786786
window_invalidate();
787787
data.open_sub_menu = menu_id;
@@ -815,7 +815,7 @@ static bool handle_right_click(int type) {
815815

816816
static bool widget_top_menu_handle_mouse_menu(const mouse* m) {
817817
auto& data = g_top_menu_data;
818-
bstring64 menu_id = top_menu_bar_handle_mouse(m);
818+
xstring menu_id = top_menu_bar_handle_mouse(m);
819819
if (!!menu_id && m->left.went_up) {
820820
data.open_sub_menu = menu_id;
821821
widget_top_menu_show();

src/widget/widget_sidebar.cpp

+13-18
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ static void button_overlay(int param1, int param2);
3939
static void button_collapse_expand(int param1, int param2);
4040
static void button_build(int submenu, int param2);
4141
static void button_undo(int param1, int param2);
42-
static void button_messages(int param1, int param2);
4342
static void button_help(int param1, int param2);
4443
static void button_advisors(int param1, int param2);
4544
static void button_empire(int param1, int param2);
@@ -101,7 +100,6 @@ static image_button buttons_build_expanded[] = {
101100
{COL4, ROW3, 34, 49, IB_BUILD, GROUP_SIDEBAR_BUTTONS, 44, button_build, button_none, BUILDING_MENU_SECURITY, 0, 1},
102101

103102
{COL1, ROW4, 35, 45, IB_NORMAL, GROUP_SIDEBAR_BUTTONS, 48, button_undo, button_none, 0, 0, 1},
104-
{COL2, ROW4, 38, 45, IB_NORMAL, GROUP_SIDEBAR_BUTTONS, 52, button_messages, button_help, 0, MESSAGE_DIALOG_MESSAGES, 1},
105103
};
106104

107105
static image_button buttons_top_expanded[3] = {
@@ -136,25 +134,23 @@ static void draw_sidebar_remainder(int x_offset, bool is_collapsed) {
136134
sidebar_common_draw_relief(x_offset, relief_y_offset, {PACK_GENERAL, 121}, is_collapsed);
137135
}
138136

139-
static void draw_number_of_messages(int x_offset) {
137+
void ui::sidebar_window::draw_number_of_messages() {
140138
int messages = city_message_count();
141-
buttons_build_expanded[13].enabled = messages > 0;
142-
if (messages) {
143-
text_draw_number_centered_colored(messages, x_offset + 74, 452, 32, FONT_SMALL_PLAIN, COLOR_BLACK);
144-
text_draw_number_centered_colored(messages, x_offset + 73, 453, 32, FONT_SMALL_PLAIN, COLOR_WHITE);
145-
}
139+
140+
ui["show_messages"].readonly = (messages <= 0);
141+
ui["num_messages"] = messages > 0 ? bstring32(messages) : bstring32();
146142
}
147143

148144
static void draw_buttons_collapsed(int x_offset) {
149145
image_buttons_draw({x_offset, TOP_MENU_HEIGHT}, button_expand_sidebar, 1);
150146
image_buttons_draw({x_offset, TOP_MENU_HEIGHT}, buttons_build_collapsed, 12);
151147
}
152148

153-
void ui::sidebar_window::draw_buttons_expanded(int x_offset) {
149+
void ui::sidebar_window::draw_buttons_expanded() {
154150
buttons_build_expanded[12].enabled = game_can_undo();
155151
ui["goto_problem"].readonly = !city_message_problem_area_count();
156152
image_buttons_draw({x_offset, TOP_MENU_HEIGHT}, buttons_overlays_collapse_sidebar, 1);
157-
image_buttons_draw({x_offset, TOP_MENU_HEIGHT}, buttons_build_expanded, 15);
153+
image_buttons_draw({x_offset, TOP_MENU_HEIGHT}, buttons_build_expanded, std::size(buttons_build_expanded));
158154
image_buttons_draw({x_offset, TOP_MENU_HEIGHT}, buttons_top_expanded, 3);
159155
}
160156

@@ -198,6 +194,8 @@ void ui::sidebar_window::init() {
198194
window_invalidate();
199195
}
200196
});
197+
198+
ui["show_messages"].onclick([] { window_message_list_show(); });
201199
}
202200

203201
void ui::sidebar_window::ui_draw_foreground() {
@@ -222,9 +220,9 @@ void ui::sidebar_window::ui_draw_foreground() {
222220

223221
ui.image(extra_block, { extra_block_x, 0 });
224222

225-
draw_number_of_messages(x_offset - 26);
223+
draw_number_of_messages();
226224

227-
draw_buttons_expanded(x_offset);
225+
draw_buttons_expanded();
228226
draw_overlay_text();
229227

230228
draw_sidebar_remainder(x_offset, false);
@@ -237,11 +235,10 @@ void ui::sidebar_window::ui_draw_foreground() {
237235
int x_offset = sidebar_common_get_x_offset_collapsed();
238236
draw_buttons_collapsed(x_offset);
239237
} else {
240-
int x_offset = sidebar_common_get_x_offset_expanded();
241-
draw_buttons_expanded(x_offset);
238+
draw_buttons_expanded();
242239

243240
widget_minimap_draw({ x_offset + 12, MINIMAP_Y_OFFSET }, MINIMAP_WIDTH, MINIMAP_HEIGHT, 0);
244-
draw_number_of_messages(x_offset - 26);
241+
draw_number_of_messages();
245242
}
246243
sidebar_extra_draw_foreground();
247244

@@ -360,9 +357,7 @@ static void button_undo(int param1, int param2) {
360357
game_undo_perform();
361358
window_invalidate();
362359
}
363-
static void button_messages(int param1, int param2) {
364-
window_message_list_show();
365-
}
360+
366361
static void button_help(int param1, int param2) {
367362
window_message_dialog_show(param2, -1, window_city_draw_all);
368363
}

src/widget/widget_sidebar.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ struct sidebar_window : public autoconfig_window_t<sidebar_window> {
2222
virtual void init() override;
2323

2424
void draw_overlay_text();
25-
void draw_buttons_expanded(int x_offset);
25+
void draw_buttons_expanded();
26+
void draw_number_of_messages();
2627
};
2728

2829
}

0 commit comments

Comments
 (0)