Skip to content

Commit 3559979

Browse files
committed
ui: sidebar undo button moved to js config
1 parent dd76357 commit 3559979

File tree

7 files changed

+72
-69
lines changed

7 files changed

+72
-69
lines changed

.github/workflows/akhenaten_android.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
cd android
3434
bash ./gradlew assembleDebug --stacktrace
3535
- name: Upload APK
36-
uses: actions/upload-artifact@v1
36+
uses: actions/upload-artifact@v4
3737
with:
3838
name: apk
3939
path: android/akhenaten/build/outputs/apk/debug/akhenaten-debug.apk

src/graphics/elements/image_button.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void image_buttons_draw(vec2i pos, image_button* buttons, int num_buttons, int s
4545
if (btn->enabled) {
4646
if (btn->pressed) {
4747
image_id += 2;
48-
} else if (btn->focused) {
48+
} else if (btn->hovered) {
4949
image_id += 1;
5050
}
5151
} else {
@@ -65,16 +65,16 @@ bool image_buttons_handle_mouse(const mouse* m, vec2i pos, image_button* buttons
6565

6666
for (int i = 0; i < num_buttons; i++) {
6767
image_button* btn = &buttons[i];
68-
if (btn->focused)
69-
btn->focused--;
68+
if (btn->hovered)
69+
btn->hovered--;
7070

7171
if (pos.x + btn->x <= m->x && pos.x + btn->x + btn->width > m->x && pos.y + btn->y <= m->y
7272
&& pos.y + btn->y + btn->height > m->y) {
7373
if (focus_button_id)
7474
*focus_button_id = i + 1;
7575

7676
if (btn->enabled) {
77-
btn->focused = 2;
77+
btn->hovered = 2;
7878
hit_button = btn;
7979
}
8080
} else if (btn->floating) {

src/graphics/elements/image_button.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct image_button {
3232
// state
3333
char pressed;
3434
char floating;
35-
uint8_t focused;
35+
uint8_t hovered;
3636
time_millis pressed_since;
3737
textid _tooltip;
3838

src/graphics/elements/tooltip.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ static void draw_button_tooltip(tooltip_context* c) {
9090
x = c->mpos.x + 50;
9191
else
9292
x = c->mpos.x + 20;
93-
} else
93+
} else {
9494
x = c->mpos.x - width - 20;
95+
}
9596

9697
switch (window_get_id()) {
9798
case WINDOW_ADVISORS:

src/graphics/elements/ui.cpp

+49-35
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,20 @@ void config_load_ui_options() {
141141

142142
static ui::element::ptr create_element(pcstr type) {
143143
ui::element::ptr elm;
144-
if (!strcmp(type, "outer_panel")) { elm = std::make_shared<ui::eouter_panel>(); } else if (!strcmp(type, "scrollbar")) { elm = std::make_shared<ui::escrollbar>(); } else if (!strcmp(type, "menu_header")) { elm = std::make_shared<ui::emenu_header>(); } else if (!strcmp(type, "inner_panel")) { elm = std::make_shared<ui::einner_panel>(); } else if (!strcmp(type, "background")) { elm = std::make_shared<ui::ebackground>(); } else if (!strcmp(type, "image")) { elm = std::make_shared<ui::eimg>(); } else if (!strcmp(type, "label")) { elm = std::make_shared<ui::elabel>(); } else if (!strcmp(type, "text")) { elm = std::make_shared<ui::etext>(); } else if (!strcmp(type, "generic_button")) { elm = std::make_shared<ui::egeneric_button>(); } else if (!strcmp(type, "image_button")) { elm = std::make_shared<ui::eimage_button>(); } else if (!strcmp(type, "resource_icon")) { elm = std::make_shared<ui::eresource_icon>(); } else if (!strcmp(type, "arrow_button")) { elm = std::make_shared<ui::earrow_button>(); } else if (!strcmp(type, "border")) { elm = std::make_shared<ui::eborder>(); } else if (!strcmp(type, "large_button")) {
144+
if (!strcmp(type, "outer_panel")) { elm = std::make_shared<ui::eouter_panel>(); }
145+
else if (!strcmp(type, "scrollbar")) { elm = std::make_shared<ui::escrollbar>(); }
146+
else if (!strcmp(type, "menu_header")) { elm = std::make_shared<ui::emenu_header>(); }
147+
else if (!strcmp(type, "inner_panel")) { elm = std::make_shared<ui::einner_panel>(); }
148+
else if (!strcmp(type, "background")) { elm = std::make_shared<ui::ebackground>(); }
149+
else if (!strcmp(type, "image")) { elm = std::make_shared<ui::eimg>(); }
150+
else if (!strcmp(type, "label")) { elm = std::make_shared<ui::elabel>(); }
151+
else if (!strcmp(type, "text")) { elm = std::make_shared<ui::etext>(); }
152+
else if (!strcmp(type, "generic_button")) { elm = std::make_shared<ui::egeneric_button>(); }
153+
else if (!strcmp(type, "image_button")) { elm = std::make_shared<ui::eimage_button>(); }
154+
else if (!strcmp(type, "resource_icon")) { elm = std::make_shared<ui::eresource_icon>(); }
155+
else if (!strcmp(type, "arrow_button")) { elm = std::make_shared<ui::earrow_button>(); }
156+
else if (!strcmp(type, "border")) { elm = std::make_shared<ui::eborder>(); }
157+
else if (!strcmp(type, "large_button")) {
145158
auto btn = std::make_shared<ui::egeneric_button>();
146159
btn->mode = 1;
147160
elm = btn;
@@ -356,8 +369,8 @@ image_button &ui::img_button(image_desc desc, vec2i pos, vec2i size, const img_b
356369
g_state.buttons.push_back(image_button{pos.x, pos.y, size.x + 4, size.y + 4, IB_NORMAL, (uint32_t)desc.pack, (uint32_t)desc.id, offsets.data[0], button_none, button_none, 0, 0, true});
357370
auto &ibutton = g_state.buttons.back().i_button;
358371

359-
ibutton.focused = !(flags & UiFlags_Grayed) && (is_button_hover(ibutton, state_offset) || !!(flags & UiFlags_Selected));
360-
ibutton.pressed = ibutton.focused && m->left.is_down;
372+
ibutton.hovered = !(flags & UiFlags_Grayed) && (is_button_hover(ibutton, state_offset) || !!(flags & UiFlags_Selected));
373+
ibutton.pressed = ibutton.hovered && m->left.is_down;
361374
ibutton.enabled = !(flags & UiFlags_Readonly);
362375

363376
time_millis current_time = time_get_millis();
@@ -373,7 +386,7 @@ image_button &ui::img_button(image_desc desc, vec2i pos, vec2i size, const img_b
373386
if (ibutton.enabled) {
374387
if (ibutton.pressed) {
375388
image_id += offsets.data[2];
376-
} else if (ibutton.focused) {
389+
} else if (ibutton.hovered) {
377390
image_id += offsets.data[1];
378391
}
379392
} else {
@@ -771,6 +784,7 @@ void ui::eimage_button::load(archive arch, element *parent, items &elems) {
771784
offsets.data[1] = arch.r_int("offset_focused", 1);
772785
offsets.data[2] = arch.r_int("offset_pressed", 2);
773786
offsets.data[3] = arch.r_int("offset_disabled", 3);
787+
_tooltip = arch.r_vec2i("tooltip");
774788

775789
pcstr name_icon_texture = arch.r_string("icon_texture");
776790
if (name_icon_texture && *name_icon_texture) {
@@ -784,52 +798,52 @@ void ui::eimage_button::draw() {
784798
UiFlags flags = (selected ? UiFlags_Selected : UiFlags_None);
785799
flags |= (readonly ? UiFlags_Readonly : UiFlags_None);
786800

801+
image_button *btn = nullptr;
802+
vec2i tsize;
787803
if (img) {
788-
ui::img_button(img, pos, size, img_desc.offset)
789-
.onclick(_func)
790-
.tooltip(_tooltip);
791-
804+
btn = &ui::img_button(img, pos, size, img_desc.offset);
792805
} else if (img_desc.id || img_desc.offset) {
793806
int img_id = image_id_from_group(img_desc.pack, img_desc.id);
794807
const image_t *img_ptr = image_get(img_id + img_desc.offset);
795808

796-
vec2i tsize;
797809
tsize.x = size.x > 0 ? size.x : img_ptr->width;
798810
tsize.y = size.y > 0 ? size.y : img_ptr->height;
799811

800-
auto &imb = ui::img_button(img_desc, pos, tsize, offsets, flags);
801-
imb.onclick(_func);
802-
imb.tooltip(_tooltip);
803-
804-
if (border && selected) {
805-
button_border_draw(doffset.x + pos.x - 4, doffset.y + pos.y - 4, tsize.x + 8, tsize.y + 8, true);
806-
}
807-
808-
if (grayed) {
809-
graphics_shade_rect(doffset + pos, tsize, 0x80);
810-
}
812+
btn = &ui::img_button(img_desc, pos, tsize, offsets, flags);
811813
} else if (texture_id > 0) {
812814
graphics_draw_from_texture(texture_id, doffset + pos, size);
813-
814-
if (border && selected) {
815-
button_border_draw(doffset.x + pos.x - 4, doffset.y + pos.y - 4, size.x + 8, size.y + 8, true);
816-
}
817-
818-
ui::img_button({ 0, 0 }, pos, size, offsets, flags)
819-
.onclick(_func)
820-
.tooltip(_tooltip);
821-
822-
if (grayed) {
823-
graphics_shade_rect(doffset + pos, size, 0x80);
824-
}
815+
tsize = size;
816+
btn = &ui::img_button({ 0, 0 }, pos, size, offsets, flags);
825817
} else if (icon_texture) {
826818
painter ctx = game.painter();
827819
ctx.draw((SDL_Texture*)icon_texture, pos, {0, 0}, size, 0xffffffff, scale, false, true);
828820

829-
ui::img_button({ 0, 0 }, pos, size, offsets, UiFlags_None)
830-
.onclick(_func)
831-
.tooltip(_tooltip);
821+
btn = &ui::img_button({ 0, 0 }, pos, size, offsets, UiFlags_None);
832822
}
823+
824+
if (!btn) {
825+
return;
826+
}
827+
828+
if (grayed) {
829+
graphics_shade_rect(doffset + pos, tsize, 0x80);
830+
return;
831+
}
832+
833+
if (readonly) {
834+
return;
835+
}
836+
837+
if (border && selected) {
838+
button_border_draw(doffset.x + pos.x - 4, doffset.y + pos.y - 4, tsize.x + 8, tsize.y + 8, true);
839+
}
840+
841+
btn->onclick(_func);
842+
btn->tooltip(_tooltip);
843+
844+
if (_tooltip.id && btn->hovered) {
845+
tooltipctx.set(TOOLTIP_BUTTON, _tooltip);
846+
}
833847
}
834848

835849
void ui::etext::load(archive arch, element* parent, items &elems) {

src/scripts/ui.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -830,17 +830,20 @@ sidebar_window = {
830830
extra_block : {pack:PACK_GENERAL, id:121, offset:2},
831831
extra_block_x : 162,
832832
ui : {
833+
// 68-xx tooltips for buttons
833834
background : image({pos:[0, 30], size: [29, 22], pack:PACK_GENERAL, id:121}),
834835
build_image : image({pos:[11, 211]}),
835836

836837
show_overlays : button({pos:[4, 30], size:[117, 20], border:false, font_hover:FONT_NORMAL_YELLOW }),
837838

838-
show_advisors : image_button({pos:[16, 173], pack:PACK_GENERAL, id:136, offset:64}),
839+
show_advisors : image_button({pos:[16, 173], pack:PACK_GENERAL, id:136, offset:64, tooltip:[68, 41]}),
840+
show_empire : image_button({pos:[90, 173], pack:PACK_GENERAL, id:136, offset:68, tooltip:[68, 42]}),
839841

840-
show_messages : image_button({pos:[46, 434], pack:PACK_GENERAL, id:136, offset:52}),
841-
goto_problem : image_button({pos:[86, 434], pack:PACK_GENERAL, id:136, offset:56}),
842-
show_briefing : image_button({pos:[116, 434], pack:PACK_GENERAL, id:136, offset:60}),
842+
show_messages : image_button({pos:[46, 434], pack:PACK_GENERAL, id:136, offset:52, tooltip:[68,33]}),
843+
goto_problem : image_button({pos:[86, 434], pack:PACK_GENERAL, id:136, offset:56, tooltip:[68,34]}),
844+
show_briefing : image_button({pos:[116, 434], pack:PACK_GENERAL, id:136, offset:60, tooltip:[68,35]}),
843845
num_messages : text({pos: [52, 450], shadow:0xff000000, font:FONT_NORMAL_BLACK_ON_DARK, color:0xffffffff }),
846+
undo_btn : image_button({pos:[9, 434], pack:PACK_GENERAL, id:136, offset:48, tooltip:[68,32]}),
844847
}
845848
}
846849

src/widget/widget_sidebar.cpp

+8-23
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@
3737

3838
static void button_collapse_expand(int param1, int param2);
3939
static void button_build(int submenu, int param2);
40-
static void button_undo(int param1, int param2);
4140
static void button_help(int param1, int param2);
42-
static void button_empire(int param1, int param2);
4341
static void button_rotate_north(int param1, int param2);
4442
static void button_rotate(int clockwise, int param2);
4543

@@ -94,12 +92,6 @@ static image_button buttons_build_expanded[] = {
9492
{COL4, ROW1, 34, 48, IB_BUILD, GROUP_SIDEBAR_BUTTONS, 36, button_build, button_none, BUILDING_MENU_HEALTH, 0, 1},
9593
{COL4, ROW2, 34, 50, IB_BUILD, GROUP_SIDEBAR_BUTTONS, 40, button_build, button_none, BUILDING_MENU_ADMINISTRATION, 0, 1},
9694
{COL4, ROW3, 34, 49, IB_BUILD, GROUP_SIDEBAR_BUTTONS, 44, button_build, button_none, BUILDING_MENU_SECURITY, 0, 1},
97-
98-
{COL1, ROW4, 35, 45, IB_NORMAL, GROUP_SIDEBAR_BUTTONS, 48, button_undo, button_none, 0, 0, 1},
99-
};
100-
101-
static image_button buttons_top_expanded[] = {
102-
{COL3 + 4, 143, 62, 36, IB_NORMAL, GROUP_SIDEBAR_BUTTONS, 68, button_empire, button_help, 0, MESSAGE_DIALOG_EMPIRE_MAP, 1},
10395
};
10496

10597
ui::sidebar_window g_sidebar;
@@ -144,7 +136,6 @@ void ui::sidebar_window::draw_buttons_expanded() {
144136

145137
image_buttons_draw({x_offset, TOP_MENU_HEIGHT}, buttons_overlays_collapse_sidebar, 1);
146138
image_buttons_draw({x_offset, TOP_MENU_HEIGHT}, buttons_build_expanded, std::size(buttons_build_expanded));
147-
image_buttons_draw({x_offset, TOP_MENU_HEIGHT}, buttons_top_expanded, std::size(buttons_top_expanded));
148139
}
149140

150141
static void refresh_build_menu_buttons() {
@@ -192,9 +183,15 @@ void ui::sidebar_window::init() {
192183
}
193184
});
194185

195-
ui["show_messages"].onclick([] { window_message_list_show(); });
196-
197186
ui["show_advisors"].onclick([] { window_advisors_show_checked(); });
187+
ui["show_empire"].onclick([] { window_empire_show_checked(); });
188+
189+
ui["undo_btn"].onclick([] {
190+
game_undo_perform();
191+
window_invalidate();
192+
});
193+
194+
ui["show_messages"].onclick([] { window_message_list_show(); });
198195

199196
ui["show_briefing"].readonly = scenario_is_custom();
200197
ui["show_briefing"].onclick([] { window_mission_briefing_show_review(); });
@@ -313,11 +310,6 @@ int widget_sidebar_city_handle_mouse(const mouse* m) {
313310
data.focus_tooltip_text_id = button_id + 19;
314311
}
315312

316-
handled |= image_buttons_handle_mouse(m, {x_offset, 24}, buttons_top_expanded, (int)std::size(buttons_top_expanded), &button_id);
317-
if (button_id) {
318-
data.focus_tooltip_text_id = button_id + 40;
319-
}
320-
321313
handled |= (sidebar_extra_handle_mouse(m) != 0);
322314
}
323315
return handled;
@@ -355,18 +347,11 @@ static void button_collapse_expand(int param1, int param2) {
355347
static void button_build(int submenu, int param2) {
356348
window_build_menu_show(submenu);
357349
}
358-
static void button_undo(int param1, int param2) {
359-
game_undo_perform();
360-
window_invalidate();
361-
}
362350

363351
static void button_help(int param1, int param2) {
364352
window_message_dialog_show(param2, -1, window_city_draw_all);
365353
}
366354

367-
static void button_empire(int param1, int param2) {
368-
window_empire_show_checked();
369-
}
370355
static void button_rotate_north(int param1, int param2) {
371356
game_orientation_rotate_north();
372357
window_invalidate();

0 commit comments

Comments
 (0)