1 #include "assets/assets.h"
2 #include "city/data_private.h"
3 #include "city/race_bet.h"
4 #include "core/calc.h"
5 #include "graphics/arrow_button.h"
6 #include "graphics/generic_button.h"
7 #include "graphics/graphics.h"
8 #include "graphics/image.h"
9 #include "graphics/image_button.h"
10 #include "graphics/lang_text.h"
11 #include "graphics/panel.h"
12 #include "graphics/text.h"
13 #include "graphics/window.h"
14 #include "input/input.h"
15 #include "race_bet.h"
16 #include "translation/translation.h"
17 
18 static translation_key button_tooltips[] = {TR_WINDOW_RACE_BET_BLUE_HORSE, TR_WINDOW_RACE_BET_RED_HORSE, TR_WINDOW_RACE_BET_WHITE_HORSE, TR_WINDOW_RACE_BET_GREEN_HORSE};
19 static translation_key team_descriptions[] = { TR_WINDOW_RACE_BLUE_HORSE_DESCRIPTION, TR_WINDOW_RACE_RED_HORSE_DESCRIPTION, TR_WINDOW_RACE_WHITE_HORSE_DESCRIPTION, TR_WINDOW_RACE_GREEN_HORSE_DESCRIPTION };
20 static void arrow_button_bet(int is_down, int param2);
21 static void button_horse_selection(int option, int param2);
22 static void button_confirm(int option, int param2);
23 static void button_close(int param1, int param2);
24 
25 static generic_button buttons[] = {
26         {34, 145, 81, 91, button_horse_selection, button_none, BLUE_HORSE, 0},
27         {144, 145, 81, 91, button_horse_selection, button_none, RED_HORSE, 0},
28         {254, 145, 81, 91, button_horse_selection, button_none, WHITE_HORSE, 0},
29         {364, 145, 81, 91, button_horse_selection, button_none, GREEN_HORSE, 0}
30 };
31 
32 static arrow_button amount_buttons[] = {
33         {106, 306, 17, 24, arrow_button_bet, 1, 0},
34         {130, 306, 15, 24, arrow_button_bet, 0, 0}
35 };
36 static generic_button bet_buttons[] = {
37         {90, 354, 300, 20, button_confirm, button_none, 1, 0},
38 };
39 
40 static image_button image_button_close[] = {
41         {424, 354, 24, 24, IB_NORMAL, GROUP_CONTEXT_ICONS, 4, button_close, button_none, 0, 0, 1}
42 };
43 
44 
45 static struct {
46     int chosen_horse;
47     int bet_amount;
48     int in_progress_bet;
49     int focus_button_id;
50     int focus_button_id2;
51     int focus_button_id3;
52     int focus_image_button_id;
53     int width_blocks;
54     int height_blocks;
55 } data;
56 
init()57 static int init()
58 {
59     if (window_is(WINDOW_RACE_BET)) {
60         // don't show popup over popup
61         return 0;
62     }
63     data.in_progress_bet = city_data.games.chosen_horse ? 1 : 0;
64     data.chosen_horse = city_data.games.chosen_horse ? city_data.games.chosen_horse : NO_BET;
65     data.bet_amount = city_data.games.bet_amount ? city_data.games.bet_amount: 0;
66     data.width_blocks = 30;
67     data.height_blocks = 25;
68 
69     return 1;
70 }
71 
72 
draw_background(void)73 static void draw_background(void)
74 {
75     window_draw_underlying_window();
76 
77     graphics_in_dialog_with_size(BLOCK_SIZE * data.width_blocks, BLOCK_SIZE * data.height_blocks);
78 
79     outer_panel_draw(0, 0, data.width_blocks, data.height_blocks);
80 
81     image_draw(image_group(GROUP_RESOURCE_ICONS) + RESOURCE_DENARII, 20, 20);
82 
83     text_draw_centered(translation_for(TR_WINDOW_RACE_BET_TITLE), 0, 20, BLOCK_SIZE * data.width_blocks, FONT_LARGE_BLACK, 0);
84 
85     text_draw_multiline(translation_for(TR_WINDOW_RACE_BET_DESCRIPTION), 25, 65, 438, FONT_NORMAL_BLACK, 0);
86 
87     inner_panel_draw(18, 300, 28, 2);
88     text_draw_centered(translation_for(TR_WINDOW_RACE_BET_AMOUNT), 18, 310, 80, FONT_NORMAL_WHITE, 0);
89     int width = text_draw_number(data.bet_amount, '@', " ", 165, 310, FONT_NORMAL_WHITE, 0);
90     width += lang_text_draw(50, 15, 165 + width, 310, FONT_NORMAL_WHITE);
91     text_draw_with_money(translation_for(TR_PERSONAL_SAVINGS), city_emperor_personal_savings(), " ", "", 284, 310, 175,  FONT_NORMAL_WHITE, 0);
92 
93     translation_key horse_description = 0;
94     if (data.focus_button_id) {
95         horse_description = team_descriptions[data.focus_button_id - 1];
96     } else if (data.chosen_horse) {
97         horse_description = team_descriptions[data.chosen_horse - 1];
98     }
99     if (horse_description) {
100         text_draw_multiline(translation_for(horse_description), 25, 250, 438, FONT_NORMAL_BLACK, 0);
101     }
102 
103     text_draw_centered(translation_for(data.in_progress_bet ? TR_WINDOW_IN_PROGRESS_BET_BUTTON : TR_WINDOW_RACE_BET_BUTTON), 90, 358, 300, FONT_NORMAL_BLACK, 0);
104 
105     graphics_reset_dialog();
106 }
107 
draw_foreground(void)108 static void draw_foreground(void)
109 {
110     graphics_in_dialog_with_size(BLOCK_SIZE * data.width_blocks, BLOCK_SIZE * data.height_blocks);
111 
112     int image_id = assets_get_image_id("UI_Elements", "Hipp_Blues_UH");
113     int image_id_focus = assets_get_image_id("UI_Elements", "Hipp_Blues_H");
114 
115     for (int i = 0; i < 4; i++) {
116         int slice_id = (2 * i);
117         int id = data.focus_button_id == (i + 1) || data.chosen_horse == (i + 1) ? image_id_focus : image_id;
118         image_draw(id + slice_id, 34 + i * 110, 145);
119     }
120 
121     arrow_buttons_draw(0, 0, amount_buttons, 2);
122 
123     button_border_draw(90, 354, 300, 20, !data.in_progress_bet && data.focus_button_id3 == 1);
124     image_buttons_draw(0, 0, image_button_close, 1);
125 
126     graphics_reset_dialog();
127     window_request_refresh();
128 }
129 
handle_input(const mouse * m,const hotkeys * h)130 static void handle_input(const mouse *m, const hotkeys *h)
131 {
132     const mouse *m_dialog = mouse_in_dialog_with_size(m, data.width_blocks * BLOCK_SIZE, data.height_blocks * BLOCK_SIZE);
133 
134     if (image_buttons_handle_mouse(m_dialog, 0, 0, image_button_close, 1, &data.focus_image_button_id)) {
135         return;
136     }
137 
138     if (input_go_back_requested(m, h)) {
139         window_go_back();
140     }
141 
142     if (generic_buttons_handle_mouse(m_dialog, 0, 0, buttons, 4, &data.focus_button_id) ||
143         arrow_buttons_handle_mouse(m_dialog, 0, 0, amount_buttons, 2, &data.focus_button_id2) ||
144         generic_buttons_handle_mouse(m_dialog, 0, 0, bet_buttons, 1, &data.focus_button_id3)) {
145         return;
146     }
147 }
148 
arrow_button_bet(int is_down,int param2)149 static void arrow_button_bet(int is_down, int param2)
150 {
151     if (!data.in_progress_bet) {
152         int amount = data.bet_amount;
153         amount += is_down ? -10 : 10;
154         data.bet_amount = calc_bound(amount, 0, city_data.emperor.personal_savings);
155 
156         window_request_refresh();
157     }
158 }
159 
button_horse_selection(int option,int param2)160 static void button_horse_selection(int option, int param2)
161 {
162     if (!data.in_progress_bet) {
163         data.chosen_horse = option;
164         window_request_refresh();
165     }
166 }
167 
button_confirm(int option,int param2)168 static void button_confirm(int option, int param2)
169 {
170     // save bet and go back
171     if (!city_data.games.chosen_horse && data.chosen_horse && data.bet_amount) {
172         city_data.games.chosen_horse = data.chosen_horse;
173         city_data.games.bet_amount = data.bet_amount;
174         window_go_back();
175     }
176 }
177 
handle_tooltip(tooltip_context * c)178 static void handle_tooltip(tooltip_context *c)
179 {
180     if (data.focus_image_button_id) { // "Exit this panel"
181         c->type = TOOLTIP_BUTTON;
182         c->text_group = 68;
183         c->text_id = 2;
184     } else if (data.focus_button_id) {
185         c->type = TOOLTIP_BUTTON;
186         c->translation_key = button_tooltips[data.focus_button_id - 1];
187     }
188 }
189 
button_close(int param1,int param2)190 static void button_close(int param1, int param2)
191 {
192     window_go_back();
193 }
194 
window_race_bet_show()195 void window_race_bet_show()
196 {
197     if (init()) {
198         window_type window = {
199                 WINDOW_RACE_BET,
200                 draw_background,
201                 draw_foreground,
202                 handle_input,
203                 handle_tooltip
204         };
205         window_show(&window);
206     }
207 }
208