Skip to content

Commit

Permalink
Refactor generic button code
Browse files Browse the repository at this point in the history
  • Loading branch information
crudelios committed Oct 11, 2024
1 parent 7627c30 commit b478028
Show file tree
Hide file tree
Showing 79 changed files with 1,516 additions and 1,398 deletions.
18 changes: 12 additions & 6 deletions src/graphics/generic_button.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "generic_button.h"

#include "graphics/lang_text.h"

static unsigned int get_button(const mouse *m, int x, int y, generic_button *buttons, unsigned int num_buttons)
{
for (unsigned int i = 0; i < num_buttons; i++) {
Expand All @@ -27,11 +25,19 @@ int generic_buttons_handle_mouse(const mouse *m, int x, int y, generic_button *b
}
generic_button *button = &buttons[button_id - 1];
if (m->left.went_up) {
button->left_click_handler(button->parameter1, button->parameter2);
return button->left_click_handler != button_none;
if (button->left_click_handler) {
button->left_click_handler(button);
return 1;
} else {
return 0;
}
} else if (m->right.went_up) {
button->right_click_handler(button->parameter1, button->parameter2);
return button->right_click_handler != button_none;
if (button->right_click_handler) {
button->right_click_handler(button);
return 1;
} else {
return 0;
}
} else {
return 0;
}
Expand Down
14 changes: 6 additions & 8 deletions src/graphics/generic_button.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
#ifndef GRAPHICS_CUSTOM_BUTTON_H
#define GRAPHICS_CUSTOM_BUTTON_H
#ifndef GRAPHICS_GENERIC_BUTTON_H
#define GRAPHICS_GENERIC_BUTTON_H

#include "graphics/button.h"
#include "graphics/font.h"
#include "input/mouse.h"

typedef struct {
typedef struct generic_button {
short x;
short y;
short width;
short height;
void (*left_click_handler)(int param1, int param2);
void (*right_click_handler)(int param1, int param2);
void (*left_click_handler)(const struct generic_button *button);
void (*right_click_handler)(const struct generic_button *button);
int parameter1;
int parameter2;
} generic_button;

int generic_buttons_handle_mouse(const mouse *m, int x, int y, generic_button *buttons, unsigned int num_buttons,
unsigned int *focus_button_id);

#endif // GRAPHICS_CUSTOM_BUTTON_H
#endif // GRAPHICS_GENERIC_BUTTON_H
19 changes: 10 additions & 9 deletions src/widget/city_pause_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@
#include "window/mission_selection.h"
#include "window/plain_message_dialog.h"

static void button_click(int type, int param2);
static void button_click(const generic_button *button);

static unsigned int focus_button_id;

static generic_button buttons[] = {
{192, 100, 192, 25, button_click, button_none, 1, 0},
{192, 140, 192, 25, button_click, button_none, 2, 0},
{192, 180, 192, 25, button_click, button_none, 3, 0},
{192, 220, 192, 25, button_click, button_none, 4, 0},
{192, 260, 192, 25, button_click, button_none, 5, 0},
{192, 300, 192, 25, button_click, button_none, 6, 0},
{192, 340, 192, 25, button_click, button_none, 7, 0},
{192, 100, 192, 25, button_click, 0, 1},
{192, 140, 192, 25, button_click, 0, 2},
{192, 180, 192, 25, button_click, 0, 3},
{192, 220, 192, 25, button_click, 0, 4},
{192, 260, 192, 25, button_click, 0, 5},
{192, 300, 192, 25, button_click, 0, 6},
{192, 340, 192, 25, button_click, 0, 7},
};

#define MAX_BUTTONS (sizeof(buttons) / sizeof(generic_button))
Expand Down Expand Up @@ -116,8 +116,9 @@ static void confirm_exit(int accepted, int checked)
}
}

static void button_click(int type, int param2)
static void button_click(const generic_button *button)
{
int type = button->parameter1;
if (type == 1) {
window_go_back();
} else if (type == 2) {
Expand Down
17 changes: 9 additions & 8 deletions src/widget/map_editor_pause_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ static void menu_file_confirm_exit(int accepted, int checked)
}
}

static void button_click(int type, int param2);
static void button_click(const generic_button *button);

static unsigned int focus_button_id;

static generic_button buttons[] = {
{192, 100, 192, 25, button_click, button_none, 1, 0},
{192, 140, 192, 25, button_click, button_none, 2, 0},
{192, 180, 192, 25, button_click, button_none, 3, 0},
{192, 220, 192, 25, button_click, button_none, 4, 0},
{192, 260, 192, 25, button_click, button_none, 5, 0},
{192, 300, 192, 25, button_click, button_none, 6, 0},
{192, 100, 192, 25, button_click, 0, 1},
{192, 140, 192, 25, button_click, 0, 2},
{192, 180, 192, 25, button_click, 0, 3},
{192, 220, 192, 25, button_click, 0, 4},
{192, 260, 192, 25, button_click, 0, 5},
{192, 300, 192, 25, button_click, 0, 6},
};

static void draw_foreground(void)
Expand Down Expand Up @@ -97,8 +97,9 @@ static void main_menu_confirmed(int confirmed, int checked)
}
}

static void button_click(int type, int param2)
static void button_click(const generic_button *button)
{
int type = button->parameter1;
if (type == 1) {
window_go_back();
} else if (type == 2) {
Expand Down
15 changes: 8 additions & 7 deletions src/widget/sidebar/extra.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

static void button_game_speed(int is_down, int param2);
static void button_toggle_play_paused(int param1, int param2);
static void button_handle_request(int index, int param2);
static void button_handle_request(const generic_button *button);

static arrow_button arrow_buttons_speed[] = {
{11, 30, 17, 24, button_game_speed, 1, 0},
Expand All @@ -66,11 +66,11 @@ static image_button play_paused_button = {
};

static generic_button buttons_emperor_requests[] = {
{2, 28, 158, 20, button_handle_request, button_none, 0, 0},
{2, 76, 158, 20, button_handle_request, button_none, 1, 0},
{2, 124, 158, 20, button_handle_request, button_none, 2, 0},
{2, 172, 158, 20, button_handle_request, button_none, 3, 0},
{2, 220, 158, 20, button_handle_request, button_none, 4, 0}
{2, 28, 158, 20, button_handle_request},
{2, 76, 158, 20, button_handle_request, 0, 1},
{2, 124, 158, 20, button_handle_request, 0, 2},
{2, 172, 158, 20, button_handle_request, 0, 3},
{2, 220, 158, 20, button_handle_request, 0, 4}
};

static const char *play_pause_button_image_names[] = { "Pause Button", "Play Button" };
Expand Down Expand Up @@ -747,8 +747,9 @@ static void confirm_send_goods(int accepted, int checked)
}
}

static void button_handle_request(int index, int param2)
static void button_handle_request(const generic_button *button)
{
int index = button->parameter1;
if (data.active_requests > data.visible_requests && index == (int) data.visible_requests - 1) {
window_advisors_show_advisor(ADVISOR_IMPERIAL);
return;
Expand Down
47 changes: 24 additions & 23 deletions src/widget/sidebar/military.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ static const int LAYOUT_BUTTON_INDEXES_AUXILIARY[2][LAYOUTS_PER_LEGION] = {
static void button_military_menu(int param1, int param2);
static void button_close_military_sidebar(int param1, int param2);
static void button_cycle_legion(int cycle_forward, int param2);
static void button_select_formation_layout(int index, int param2);
static void button_go_to_legion(int param1, int param2);
static void button_return_to_fort(int param1, int param2);
static void button_empire_service(int param1, int param2);
static void button_select_formation_layout(const generic_button *button);
static void button_go_to_legion(const generic_button *button);
static void button_return_to_fort(const generic_button *button);
static void button_empire_service(const generic_button *button);

static image_button buttons_title_close[] = {
{123, 4, 39, 26, IB_NORMAL, GROUP_OK_CANCEL_SCROLL_BUTTONS, 4,
Expand All @@ -108,29 +108,29 @@ static arrow_button buttons_cycle_legion[] = {

static generic_button buttons_formation_layout[LAYOUTS_PER_LEGION - 2][LAYOUTS_PER_LEGION] = {
{
{8, 0, 46, 46, button_select_formation_layout, button_none, 0, 0},
{58, 0, 46, 46, button_select_formation_layout, button_none, 1, 0},
{108, 0, 46, 46, button_select_formation_layout, button_none, 2, 0}
{8, 0, 46, 46, button_select_formation_layout},
{58, 0, 46, 46, button_select_formation_layout, 0, 1},
{108, 0, 46, 46, button_select_formation_layout, 0, 2}
},
{
{33, 50, 46, 46, button_select_formation_layout, button_none, 0, 0},
{33, 0, 46, 46, button_select_formation_layout, button_none, 1, 0},
{83, 0, 46, 46, button_select_formation_layout, button_none, 2, 0},
{83, 50, 46, 46, button_select_formation_layout, button_none, 3, 0}
{33, 50, 46, 46, button_select_formation_layout},
{33, 0, 46, 46, button_select_formation_layout, 0, 1},
{83, 0, 46, 46, button_select_formation_layout, 0, 2},
{83, 50, 46, 46, button_select_formation_layout, 0, 3}
},
{
{33, 0, 46, 46, button_select_formation_layout, button_none, 0, 0},
{83, 0, 46, 46, button_select_formation_layout, button_none, 1, 0},
{8, 50, 46, 46, button_select_formation_layout, button_none, 2, 0},
{58, 50, 46, 46, button_select_formation_layout, button_none, 3, 0},
{108, 50, 46, 46, button_select_formation_layout, button_none, 4, 0}
{33, 0, 46, 46, button_select_formation_layout},
{83, 0, 46, 46, button_select_formation_layout, 0, 1},
{8, 50, 46, 46, button_select_formation_layout, 0, 2},
{58, 50, 46, 46, button_select_formation_layout, 0, 3},
{108, 50, 46, 46, button_select_formation_layout, 0, 4}
}
};

static generic_button buttons_bottom[] = {
{16, 0, 30, 30, button_go_to_legion, button_none, 0, 0},
{66, 0, 30, 30, button_return_to_fort, button_none, 0, 0},
{116, 0, 30, 30, button_empire_service, button_none, 0, 0},
{16, 0, 30, 30, button_go_to_legion},
{66, 0, 30, 30, button_return_to_fort},
{116, 0, 30, 30, button_empire_service},
};

typedef struct {
Expand Down Expand Up @@ -584,8 +584,9 @@ static void button_cycle_legion(int cycle_forward, int param2)
set_formation_id(legion->formation_id);
}

static void button_select_formation_layout(int index, int param2)
static void button_select_formation_layout(const generic_button *button)
{
int index = button->parameter1;
formation *m = formation_get(data.active_legion.formation_id);
if (m->in_distant_battle) {
return;
Expand All @@ -612,21 +613,21 @@ static void button_select_formation_layout(int index, int param2)
}
}

static void button_go_to_legion(int param1, int param2)
static void button_go_to_legion(const generic_button *button)
{
const formation *m = formation_get(data.active_legion.formation_id);
city_view_go_to_grid_offset(map_grid_offset(m->x_home, m->y_home));
}

static void button_return_to_fort(int param1, int param2)
static void button_return_to_fort(const generic_button *button)
{
formation *m = formation_get(data.active_legion.formation_id);
if (!m->in_distant_battle) {
formation_legion_return_home(m);
}
}

static void button_empire_service(int param1, int param2)
static void button_empire_service(const generic_button *button)
{
formation_toggle_empire_service(data.active_legion.formation_id);
formation_calculate_figures();
Expand Down
7 changes: 4 additions & 3 deletions src/window/advisor/entertainment.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "city/gods.h"
#include "city/houses.h"
#include "core/calc.h"
#include "graphics/button.h"
#include "graphics/generic_button.h"
#include "graphics/image.h"
#include "graphics/lang_text.h"
Expand All @@ -27,10 +28,10 @@

static unsigned int focus_button_id;

static void button_hold_games(int param1, int param2);
static void button_hold_games(const generic_button *button);

static generic_button hold_games_button[] = {
{102, 370, 300, 20, button_hold_games, button_none, 0, 0},
{102, 370, 300, 20, button_hold_games},
};

struct games_text {
Expand Down Expand Up @@ -207,7 +208,7 @@ static int handle_mouse(const mouse *m)
return generic_buttons_handle_mouse(m, 0, 0, hold_games_button, 1, &focus_button_id);
}

static void button_hold_games(int param1, int param2)
static void button_hold_games(const generic_button *button)
{
window_hold_games_show(0);
}
Expand Down
39 changes: 21 additions & 18 deletions src/window/advisor/imperial.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "core/string.h"
#include "empire/city.h"
#include "figure/formation_legion.h"
#include "graphics/button.h"
#include "graphics/generic_button.h"
#include "graphics/image.h"
#include "graphics/lang_text.h"
Expand All @@ -30,21 +31,21 @@
#define ADVISOR_HEIGHT 27
#define RESOURCE_INFO_MAX_TEXT 200

static void button_donate_to_city(int param1, int param2);
static void button_set_salary(int param1, int param2);
static void button_gift_to_emperor(int param1, int param2);
static void button_request(int index, int param2);
static void button_request_resource(int index, int param2);
static void button_donate_to_city(const generic_button *button);
static void button_set_salary(const generic_button *button);
static void button_gift_to_emperor(const generic_button *button);
static void button_request(const generic_button *button);
static void button_request_resource(const generic_button *button);

static generic_button imperial_buttons[] = {
{320, 367, 250, 20, button_donate_to_city, button_none, 0, 0},
{70, 393, 500, 20, button_set_salary, button_none, 0, 0},
{320, 341, 250, 20, button_gift_to_emperor, button_none, 0, 0},
{38, 96, 560, 40, button_request, button_request_resource, 0, 0},
{38, 138, 560, 40, button_request, button_request_resource, 1, 0},
{38, 180, 560, 40, button_request, button_request_resource, 2, 0},
{38, 222, 560, 40, button_request, button_request_resource, 3, 0},
{38, 264, 560, 40, button_request, button_request_resource, 4, 0},
{320, 367, 250, 20, button_donate_to_city},
{70, 393, 500, 20, button_set_salary},
{320, 341, 250, 20, button_gift_to_emperor},
{38, 96, 560, 40, button_request, button_request_resource, 0},
{38, 138, 560, 40, button_request, button_request_resource, 1},
{38, 180, 560, 40, button_request, button_request_resource, 2},
{38, 222, 560, 40, button_request, button_request_resource, 3},
{38, 264, 560, 40, button_request, button_request_resource, 4},
};

static unsigned int focus_button_id;
Expand Down Expand Up @@ -177,17 +178,17 @@ static int handle_mouse(const mouse *m)
return generic_buttons_handle_mouse(m, 0, 0, imperial_buttons, 3 + request_count, &focus_button_id);
}

static void button_donate_to_city(int param1, int param2)
static void button_donate_to_city(const generic_button *button)
{
window_donate_to_city_show();
}

static void button_set_salary(int param1, int param2)
static void button_set_salary(const generic_button *button)
{
window_set_salary_show();
}

static void button_gift_to_emperor(int param1, int param2)
static void button_gift_to_emperor(const generic_button *button)
{
window_gift_to_emperor_show();
}
Expand All @@ -213,8 +214,9 @@ static void confirm_send_goods(int accepted, int checked)
}
}

void button_request(int index, int param2)
void button_request(const generic_button *button)
{
int index = button->parameter1;
int status = city_request_get_status(index);
if (!status) {
return;
Expand Down Expand Up @@ -254,8 +256,9 @@ void button_request(int index, int param2)
}

// Used for showing the resource settings window on right click
void button_request_resource(int index, int param2)
void button_request_resource(const generic_button *button)
{
int index = button->parameter1;
// Make sure there's a request pending at this index
if (!city_request_get_status(index)) {
return;
Expand Down
Loading

0 comments on commit b478028

Please sign in to comment.