1 /*
2 Copyright (C) 2009-2021 Parallel Realities
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 */
19
20 #include "../headers.h"
21
22 #include "../audio/audio.h"
23 #include "../draw.h"
24 #include "../graphics/graphics.h"
25 #include "../init.h"
26 #include "../system/error.h"
27 #include "widget.h"
28 #include "yes_no_menu.h"
29
30 extern Input input, menuInput;
31 extern Game game;
32 extern Control control;
33
34 static Menu menu;
35 static void (*yesAction)(void);
36 static void (*noAction)(void);
37
38 static void loadMenuLayout(char *);
39 static void doMenu(void);
40 static void doYes(void);
41 static void doNo(void);
42
drawYesNoMenu()43 void drawYesNoMenu()
44 {
45 int i;
46
47 drawImage(menu.background, menu.x, menu.y, FALSE, 196);
48
49 for (i=0;i<menu.widgetCount;i++)
50 {
51 drawWidget(menu.widgets[i], &menu, menu.index == i);
52 }
53 }
54
doMenu()55 static void doMenu()
56 {
57 Widget *w;
58 int left, right, attack, xAxisMoved, yAxisMoved;
59
60 left = FALSE;
61 right = FALSE;
62 attack = FALSE;
63
64 if (menuInput.left == TRUE)
65 {
66 left = TRUE;
67 }
68
69 else if (menuInput.right == TRUE)
70 {
71 right = TRUE;
72 }
73
74 else if (menuInput.attack == TRUE)
75 {
76 attack = TRUE;
77 }
78
79 else if (input.left == TRUE)
80 {
81 left = TRUE;
82 }
83
84 else if (input.right == TRUE)
85 {
86 right = TRUE;
87 }
88
89 else if (input.attack == TRUE)
90 {
91 attack = TRUE;
92 }
93
94 if (right == TRUE)
95 {
96 menu.index++;
97
98 if (menu.index == menu.widgetCount)
99 {
100 menu.index = 2;
101 }
102
103 playSound("sound/common/click");
104 }
105
106 else if (left == TRUE)
107 {
108 menu.index--;
109
110 if (menu.index < 2)
111 {
112 menu.index = menu.widgetCount - 1;
113 }
114
115 playSound("sound/common/click");
116 }
117
118 else if (attack == TRUE)
119 {
120 w = menu.widgets[menu.index];
121
122 playSound("sound/common/click");
123
124 if (w->clickAction != NULL)
125 {
126 w->clickAction();
127 }
128 }
129
130 xAxisMoved = input.xAxisMoved;
131 yAxisMoved = input.yAxisMoved;
132
133 memset(&menuInput, 0, sizeof(Input));
134 memset(&input, 0, sizeof(Input));
135
136 input.xAxisMoved = xAxisMoved;
137 input.yAxisMoved = yAxisMoved;
138 }
139
loadMenuLayout(char * text)140 static void loadMenuLayout(char *text)
141 {
142 int x, y;
143
144 x = 0;
145 y = 0;
146
147 menu.widgetCount = 4;
148
149 menu.widgets = malloc(sizeof(Widget *) * menu.widgetCount);
150
151 if (menu.widgets == NULL)
152 {
153 showErrorAndExit("Ran out of memory when creating Yes / No Menu");
154 }
155
156 menu.widgets[0] = createWidget(text, NULL, NULL, NULL, NULL, -1, 20, FALSE, 255, 255, 255);
157
158 menu.widgets[1] = createWidget(_("Any unsaved progress will be lost"), NULL, NULL, NULL, NULL, -1, 70, FALSE, 255, 255, 255);
159
160 menu.widgets[2] = createWidget(_("Yes"), NULL, NULL, NULL, &doYes, x, y, TRUE, 255, 255, 255);
161
162 menu.widgets[3] = createWidget(_("No"), NULL, NULL, NULL, &doNo, x, y, TRUE, 255, 255, 255);
163
164 /* Resize */
165
166 menu.widgets[0]->y = BORDER_PADDING + BUTTON_PADDING;
167 menu.widgets[1]->y = menu.widgets[0]->y + menu.widgets[0]->selectedState->h + BUTTON_PADDING;
168
169 menu.widgets[2]->y = menu.widgets[1]->y + menu.widgets[1]->selectedState->h + BUTTON_PADDING;
170 menu.widgets[3]->y = menu.widgets[2]->y;
171
172 if (menu.widgets[0]->selectedState->w > menu.widgets[1]->selectedState->w)
173 {
174 menu.w = menu.widgets[0]->selectedState->w;
175 }
176
177 else
178 {
179 menu.w = menu.widgets[1]->selectedState->w;
180 }
181
182 menu.h = menu.widgets[2]->y + menu.widgets[2]->selectedState->h + BUTTON_PADDING - BORDER_PADDING;
183
184 x = menu.widgets[2]->normalState->w + BUTTON_PADDING + menu.widgets[3]->normalState->w;
185
186 menu.widgets[2]->x = (menu.w - x) / 2;
187 menu.widgets[3]->x = menu.widgets[2]->x + menu.widgets[2]->selectedState->w + BUTTON_PADDING;
188
189 menu.background = addBorder(createSurface(menu.w, menu.h, FALSE), 255, 255, 255, 0, 0, 0);
190
191 menu.x = (SCREEN_WIDTH - menu.background->w) / 2;
192 menu.y = (SCREEN_HEIGHT - menu.background->h) / 2;
193 }
194
initYesNoMenu(char * text,void (* yes)(void),void (* no)(void))195 Menu *initYesNoMenu(char *text, void (*yes)(void), void (*no)(void))
196 {
197 menu.action = &doMenu;
198
199 yesAction = yes;
200 noAction = no;
201
202 freeYesNoMenu();
203
204 loadMenuLayout(text);
205
206 menu.index = 3;
207
208 menu.returnAction = NULL;
209
210 return &menu;
211 }
212
freeYesNoMenu()213 void freeYesNoMenu()
214 {
215 int i;
216
217 if (menu.widgets != NULL)
218 {
219 for (i=0;i<menu.widgetCount;i++)
220 {
221 freeWidget(menu.widgets[i]);
222 }
223
224 free(menu.widgets);
225
226 menu.widgets = NULL;
227 }
228
229 if (menu.background != NULL)
230 {
231 destroyTexture(menu.background);
232
233 menu.background = NULL;
234 }
235 }
236
doYes()237 static void doYes()
238 {
239 yesAction();
240 }
241
doNo()242 static void doNo()
243 {
244 noAction();
245 }
246