Skip to content
  • Sponsor dalerank/Akhenaten

  • Notifications You must be signed in to change notification settings
  • Fork 16

Commit 0ce842a

Browse files
author
s.kushnirenko
committedJan 27, 2024·
ui: refactor: remove unused code and simplify button handling logic
1 parent 1717002 commit 0ce842a

File tree

1 file changed

+48
-98
lines changed

1 file changed

+48
-98
lines changed
 

‎src/window/window_building_info.cpp

+48-98
Original file line numberDiff line numberDiff line change
@@ -68,27 +68,8 @@
6868
#include <functional>
6969
#include <utility>
7070

71-
static void button_help(int param1, int param2);
72-
static void button_close(int param1, int param2);
73-
static void button_advisor(int advisor, int param2);
74-
7571
object_info g_building_info_context;
7672

77-
struct building_info_data {
78-
int image_button_id = 0;
79-
80-
struct {
81-
image_button l_advisor_b = {65, 0, 28, 28, IB_NORMAL, GROUP_MESSAGE_ADVISOR_BUTTONS, 9, button_advisor, button_none, ADVISOR_RATINGS, 0, 1};
82-
} buttons;
83-
84-
image_button image_buttons_help_close[2] = {
85-
{14, 0, 27, 27, IB_NORMAL, GROUP_CONTEXT_ICONS, 0, button_help, button_none, 0, 0, 1},
86-
{424, 3, 24, 24, IB_NORMAL, GROUP_CONTEXT_ICONS, 4, button_close, button_none, 0, 0, 1}
87-
};
88-
};
89-
90-
building_info_data g_building_info;
91-
9273
static int get_height_id() {
9374
auto &context = g_building_info_context;
9475
if (context.type == BUILDING_INFO_TERRAIN) {
@@ -194,16 +175,7 @@ static int get_height_id() {
194175
static void get_tooltip(tooltip_context* c) {
195176
auto &context = g_building_info_context;
196177
std::pair<int, int> tooltip;
197-
if (g_building_info.image_button_id) {
198-
tooltip.second = g_building_info.image_button_id;
199-
200-
//} else if (g_building_info.generic_button_id) {
201-
;
202-
203-
//} else if (g_building_info.debug_path_button_id) {
204-
;
205-
206-
} else if (context.type == BUILDING_INFO_LEGION) {
178+
if (context.type == BUILDING_INFO_LEGION) {
207179
tooltip.second = window_building_get_legion_info_tooltip_text(&context);
208180

209181
} else if (context.type == BUILDING_INFO_BUILDING && context.storage_show_special_orders) {
@@ -725,7 +697,6 @@ static void draw_background() {
725697
static void draw_foreground() {
726698
ui::begin_window(g_building_info_context.offset);
727699
auto &context = g_building_info_context;
728-
auto &data = g_building_info;
729700

730701
// building-specific buttons
731702
building *b = nullptr;
@@ -774,32 +745,52 @@ static void draw_foreground() {
774745
}
775746

776747
// general buttons
777-
if (context.storage_show_special_orders) {
778-
image_buttons_draw(vec2i(context.offset.x, context.y_offset_submenu + 16 * context.height_blocks_submenu - 40), g_building_info.image_buttons_help_close);
779-
} else {
780-
image_buttons_draw(context.offset + vec2i(0, 16 * context.height_blocks - 40), g_building_info.image_buttons_help_close);
781-
}
748+
int y_offset = (context.storage_show_special_orders) ? context.y_offset_submenu : 0;
749+
int height_blocks = (context.storage_show_special_orders) ? context.height_blocks_submenu : context.height_blocks;
750+
751+
ui::img_button(GROUP_CONTEXT_ICONS, vec2i(14, y_offset + 16 * height_blocks - 40), {28, 28}, 0)
752+
.onclick([&context] (int, int) {
753+
if (context.help_id > 0) {
754+
window_message_dialog_show(context.help_id, -1, window_city_draw_all);
755+
} else {
756+
window_message_dialog_show(MESSAGE_DIALOG_HELP, -1, window_city_draw_all);
757+
}
758+
window_invalidate();
759+
});
760+
761+
ui::img_button(GROUP_CONTEXT_ICONS, vec2i(16 * context.width_blocks - 40, y_offset + 16 * height_blocks - 40), {28, 28}, 4)
762+
.onclick([&context] (int, int) {
763+
if (context.storage_show_special_orders) {
764+
context.storage_show_special_orders = 0;
765+
storage_settings_backup_reset();
766+
window_invalidate();
767+
} else {
768+
window_city_show();
769+
}
770+
});
782771

783772
if (context.go_to_advisor.first) {
784773
int img_offset = (context.go_to_advisor.left_a - 1) * 3;
785774
ui::img_button(GROUP_MESSAGE_ADVISOR_BUTTONS, vec2i(40, 16 * context.height_blocks - 40), {28, 28}, img_offset)
786-
.onclick([&context] (int, int) {
787-
window_advisors_show_advisor(context.go_to_advisor.first);
788-
});
775+
.onclick([&context] (int, int) {
776+
window_advisors_show_advisor(context.go_to_advisor.first);
777+
});
789778
}
790779

791780
if (context.go_to_advisor.left_a) {
792781
int img_offset = (context.go_to_advisor.left_a - 1) * 3;
793782
ui::img_button(GROUP_MESSAGE_ADVISOR_BUTTONS, vec2i(40, 16 * context.height_blocks - 40), {28, 28}, img_offset)
794-
.onclick([&context] (int, int) {
795-
window_advisors_show_advisor(context.go_to_advisor.left_a);
796-
});
783+
.onclick([&context] (int, int) {
784+
window_advisors_show_advisor(context.go_to_advisor.left_a);
785+
});
797786
}
798787

799788
if (context.go_to_advisor.left_b) {
800-
g_building_info.buttons.l_advisor_b.parameter1 = context.go_to_advisor.left_b;
801-
g_building_info.buttons.l_advisor_b.image_offset = (context.go_to_advisor.left_b - 1) * 3;
802-
image_buttons_draw(context.offset + vec2i(0, 16 * context.height_blocks - 40), g_building_info.buttons.l_advisor_b);
789+
int img_offset = (context.go_to_advisor.left_b - 1) * 3;
790+
ui::img_button(GROUP_MESSAGE_ADVISOR_BUTTONS, vec2i(65, 16 * context.height_blocks - 40), {28, 28}, img_offset)
791+
.onclick([&context] (int, int) {
792+
window_advisors_show_advisor(context.go_to_advisor.left_b);
793+
});
803794
}
804795

805796
if (b) {
@@ -808,24 +799,24 @@ static void draw_foreground() {
808799
pcstr label = (b->state == BUILDING_STATE_VALID ? "x" : "");
809800
auto tooltip = (b->state == BUILDING_STATE_VALID) ? std::pair{54, 8} : std::pair{53, 10};
810801
ui::button(label, {400, 3 + 16 * context.height_blocks - 40}, {20, 20})
811-
.onclick([&context, b, workers_needed] (int, int) {
812-
if (workers_needed) {
813-
building_mothball_toggle(b);
814-
window_invalidate();
815-
}
816-
})
817-
.tooltip(tooltip);
802+
.onclick([&context, b, workers_needed] (int, int) {
803+
if (workers_needed) {
804+
building_mothball_toggle(b);
805+
window_invalidate();
806+
}
807+
})
808+
.tooltip(tooltip);
818809
}
819810
}
820811

821812
if (context.figure.draw_debug_path) {
822813
figure* f = figure_get(context.figure.figure_ids[0]);
823814
pcstr label = (f->draw_debug_mode ? "P" : "p");
824815
ui::button(label, {400, 3 + 16 * context.height_blocks - 40}, {20, 20})
825-
.onclick([&context, f] (int, int) {
826-
f->draw_debug_mode = f->draw_debug_mode ? 0 :FIGURE_DRAW_DEBUG_ROUTING;
827-
window_invalidate();
828-
});
816+
.onclick([&context, f] (int, int) {
817+
f->draw_debug_mode = f->draw_debug_mode ? 0 :FIGURE_DRAW_DEBUG_ROUTING;
818+
window_invalidate();
819+
});
829820
}
830821

831822
if (context.show_overlay != OVERLAY_NONE) {
@@ -904,29 +895,13 @@ static int handle_specific_building_info_mouse(const mouse *m) {
904895

905896
static void handle_input(const mouse* m, const hotkeys* h) {
906897
auto &context = g_building_info_context;
907-
auto &data = g_building_info;
908-
909-
bool button_id = 0;
910-
int tmp_btn_id;
911-
// general buttons
912-
if (context.storage_show_special_orders) {
913-
button_id |= image_buttons_handle_mouse(m, vec2i(context.offset.x, context.y_offset_submenu + 16 * context.height_blocks_submenu - 40),
914-
g_building_info.image_buttons_help_close, g_building_info.image_button_id);
915-
} else {
916-
button_id |= image_buttons_handle_mouse(m, context.offset + vec2i(0, 16 * context.height_blocks - 40),
917-
g_building_info.image_buttons_help_close, g_building_info.image_button_id);
918-
}
919-
920-
if (context.go_to_advisor.left_b) {
921-
button_id |= image_buttons_handle_mouse(m, context.offset + vec2i(0, 16 * context.height_blocks - 40), g_building_info.buttons.l_advisor_b, tmp_btn_id);
922-
}
923898

899+
bool button_id = ui::handle_mouse(m);
900+
924901
if (!button_id) {
925902
button_id |= !!handle_specific_building_info_mouse(m);
926903
}
927904

928-
button_id |= ui::handle_mouse(m);
929-
930905
if (!button_id && input_go_back_requested(m, h)) {
931906
if (context.storage_show_special_orders) {
932907
storage_settings_backup_check();
@@ -936,31 +911,6 @@ static void handle_input(const mouse* m, const hotkeys* h) {
936911
}
937912
}
938913

939-
static void button_help(int param1, int param2) {
940-
auto &context = g_building_info_context;
941-
if (context.help_id > 0) {
942-
window_message_dialog_show(context.help_id, -1, window_city_draw_all);
943-
} else {
944-
window_message_dialog_show(MESSAGE_DIALOG_HELP, -1, window_city_draw_all);
945-
}
946-
window_invalidate();
947-
}
948-
949-
static void button_close(int param1, int param2) {
950-
auto &context = g_building_info_context;
951-
if (context.storage_show_special_orders) {
952-
context.storage_show_special_orders = 0;
953-
storage_settings_backup_reset();
954-
window_invalidate();
955-
} else {
956-
window_city_show();
957-
}
958-
}
959-
960-
static void button_advisor(int advisor, int param2) {
961-
window_advisors_show_advisor((e_advisor)advisor);
962-
}
963-
964914
void window_building_info_show(const map_point& point) {
965915
window_type window = {
966916
WINDOW_BUILDING_INFO,

0 commit comments

Comments
 (0)
Please sign in to comment.