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 "ok_menu.h"
28 #include "widget.h"
29
30 extern Input input, menuInput;
31 extern Game game;
32 extern Control control;
33
34 static Menu menu;
35 static void (*OKAction)(void);
36
37 static void loadMenuLayout(char *);
38 static void doMenu(void);
39 static void doOK(void);
40
drawOKMenu()41 void drawOKMenu()
42 {
43 int i;
44
45 drawImage(menu.background, menu.x, menu.y, FALSE, 196);
46
47 for (i=0;i<menu.widgetCount;i++)
48 {
49 drawWidget(menu.widgets[i], &menu, menu.index == i);
50 }
51 }
52
doMenu()53 static void doMenu()
54 {
55 int xAxisMoved, yAxisMoved;
56 Widget *w;
57
58 if (menuInput.attack == TRUE || input.attack == TRUE)
59 {
60 w = menu.widgets[menu.index];
61
62 playSound("sound/common/click");
63
64 if (w->clickAction != NULL)
65 {
66 w->clickAction();
67 }
68 }
69
70 xAxisMoved = input.xAxisMoved;
71 yAxisMoved = input.yAxisMoved;
72
73 memset(&menuInput, 0, sizeof(Input));
74 memset(&input, 0, sizeof(Input));
75
76 input.xAxisMoved = xAxisMoved;
77 input.yAxisMoved = yAxisMoved;
78 }
79
loadMenuLayout(char * text)80 static void loadMenuLayout(char *text)
81 {
82 int y, i, w, maxWidth;
83
84 i = 0;
85
86 menu.widgetCount = 2;
87
88 menu.widgets = malloc(sizeof(Widget *) * menu.widgetCount);
89
90 if (menu.widgets == NULL)
91 {
92 showErrorAndExit("Ran out of memory when creating OK Menu");
93 }
94
95 menu.widgets[0] = createWidget(text, NULL, NULL, NULL, NULL, -1, 10, FALSE, 255, 255, 255);
96
97 menu.widgets[1] = createWidget(_("OK"), NULL, NULL, NULL, &doOK, -1, 10, TRUE, 255, 255, 255);
98
99 y = BUTTON_PADDING + BORDER_PADDING;
100
101 maxWidth = w = 0;
102
103 for (i=0;i<menu.widgetCount;i++)
104 {
105 if (menu.widgets[i]->label != NULL && menu.widgets[i]->normalState->w > maxWidth)
106 {
107 maxWidth = menu.widgets[i]->normalState->w;
108 }
109 }
110
111 for (i=0;i<menu.widgetCount;i++)
112 {
113 menu.widgets[i]->y = y;
114
115 if (menu.widgets[i]->x != -1)
116 {
117 menu.widgets[i]->x = BUTTON_PADDING + BORDER_PADDING;
118 }
119
120 if (menu.widgets[i]->label != NULL)
121 {
122 menu.widgets[i]->label->y = y;
123
124 menu.widgets[i]->label->x = menu.widgets[i]->x + maxWidth + 10;
125
126 if (menu.widgets[i]->label->x + menu.widgets[i]->label->text->w > w)
127 {
128 w = menu.widgets[i]->label->x + menu.widgets[i]->label->text->w;
129 }
130 }
131
132 else
133 {
134 if (menu.widgets[i]->x + menu.widgets[i]->selectedState->w > w)
135 {
136 w = menu.widgets[i]->x + menu.widgets[i]->selectedState->w;
137 }
138 }
139
140 y += menu.widgets[i]->selectedState->h + BUTTON_PADDING;
141 }
142
143 menu.w = w + BUTTON_PADDING;
144 menu.h = y - BORDER_PADDING;
145
146 menu.background = addBorder(createSurface(menu.w, menu.h, FALSE), 255, 255, 255, 0, 0, 0);
147
148 menu.x = (SCREEN_WIDTH - menu.background->w) / 2;
149 menu.y = (SCREEN_HEIGHT - menu.background->h) / 2;
150 }
151
initOKMenu(char * text,void (* yes)(void))152 Menu *initOKMenu(char *text, void (*yes)(void))
153 {
154 menu.action = &doMenu;
155
156 OKAction = yes;
157
158 freeOKMenu();
159
160 loadMenuLayout(text);
161
162 menu.index = 1;
163
164 menu.returnAction = NULL;
165
166 return &menu;
167 }
168
freeOKMenu()169 void freeOKMenu()
170 {
171 int i;
172
173 if (menu.widgets != NULL)
174 {
175 for (i=0;i<menu.widgetCount;i++)
176 {
177 freeWidget(menu.widgets[i]);
178 }
179
180 free(menu.widgets);
181
182 menu.widgets = NULL;
183 }
184
185 if (menu.background != NULL)
186 {
187 destroyTexture(menu.background);
188
189 menu.background = NULL;
190 }
191 }
192
doOK()193 static void doOK()
194 {
195 OKAction();
196 }
197