1 /*
2 * Kuklomenos
3 * Copyright (C) 2008-2009 Martin Bays <mbays@sdf.lonestar.org>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * 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, see http://www.gnu.org/licenses/.
17 */
18
19 #include <vector>
20 #include <string>
21 #include <sstream>
22 using namespace std;
23
24 #include "menu.h"
25 #include "settings.h"
26 #include "data.h"
27 #include "geom.h"
28 #include "keybindings.h"
29 #include "SDL_gfxPrimitivesDirty.h"
30
31 #include <config.h>
32
Menu(string title,Menu * m1,MenuLeaf * ml1,Menu * m2,MenuLeaf * ml2,Menu * m3,MenuLeaf * ml3,Menu * m4,MenuLeaf * ml4)33 Menu::Menu(string title,
34 Menu* m1, MenuLeaf* ml1,
35 Menu* m2, MenuLeaf* ml2,
36 Menu* m3, MenuLeaf* ml3,
37 Menu* m4, MenuLeaf* ml4) :
38 title(title)
39 {
40 menus[0] = m1;
41 menus[1] = m2;
42 menus[2] = m3;
43 menus[3] = m4;
44
45 leaves[0] = ml1;
46 leaves[1] = ml2;
47 leaves[2] = ml3;
48 leaves[3] = ml4;
49 }
50
textOfDir(int dir) const51 string Menu::textOfDir(int dir) const
52 {
53 Menu* submenu = menus[dir];
54 MenuLeaf* leaf = leaves[dir];
55
56 if (submenu)
57 return submenu->title;
58 else if (leaf)
59 return leaf->name();
60 else
61 return "";
62 }
63
name()64 string MenuLeafToggleBool::name()
65 {
66 return varName +
67 ((*var) ?
68 ": on" :
69 ": off");
70 }
71
act()72 LeafReturn MenuLeafToggleBool::act()
73 {
74 *var = !*var;
75 return LR_NONE;
76 }
77
name()78 string MenuLeafCycleAA::name()
79 {
80 switch (settings.useAA)
81 {
82 case AA_NO: return "Anti-alias: none";
83 case AA_YES: return "Anti-alias: some";
84 case AA_FORCE: default: return "Anti-alias: all";
85 }
86 }
87
act()88 LeafReturn MenuLeafCycleAA::act()
89 {
90 settings.useAA =
91 (settings.useAA == AA_NO) ? AA_YES :
92 (settings.useAA == AA_YES) ? AA_FORCE :
93 AA_NO;
94 return LR_NONE;
95 }
96
97 MenuLeafToggleBool zoomToggle("Zoom", &settings.zoomEnabled);
98 MenuLeafToggleBool rotToggle("Rotate", &settings.rotatingView);
99 MenuLeafToggleBool gridToggle("Grid", &settings.showGrid);
100 Menu viewMenu("View",
101 NULL, &zoomToggle,
102 NULL, &rotToggle,
103 NULL, &gridToggle
104 );
105
106 #ifdef SOUND
name()107 string MenuLeafCycleFreq::name()
108 {
109 ostringstream s;
110 s << "Freq: " << settings.soundFreq;
111 return s.str();
112 }
act()113 LeafReturn MenuLeafCycleFreq::act()
114 {
115 settings.soundFreq =
116 (settings.soundFreq >= 44100) ? 11025 :
117 (settings.soundFreq >= 22050) ? 44100 :
118 (settings.soundFreq >= 11025) ? 22050 :
119 11025;
120 return LR_NONE;
121 }
122 MenuLeafCycleFreq cycleFreq;
123
124 MenuLeafToggleBool soundToggle("Sound", &settings.sound);
125 MenuLeafIncVar<float> decVol(&settings.volume,
126 "Volume", -0.1, 0.1, 1);
127 MenuLeafIncVar<float> incVol(&settings.volume,
128 "Volume", 0.1, 0.1, 1);
129 MenuLeafShowVar<float> showVol(&settings.volume,
130 "Volume");
131 Menu volumeMenu("Volume",
132 NULL, &decVol,
133 NULL, NULL,
134 NULL, &incVol,
135 NULL, &showVol
136 );
137 Menu soundMenu("Sound",
138 NULL, &cycleFreq,
139 NULL, NULL,
140 NULL, &soundToggle,
141 &volumeMenu, NULL
142 );
143 #endif
144
145 MenuLeafCycleAA cycleAALeaf;
146 MenuLeafIncVar<double> decAlphaLeaf(&antialiasGamma, "AA Gamma", -0.1, 0.1);
147 MenuLeafIncVar<double> incAlphaLeaf(&antialiasGamma, "AA Gamma", 0.1, 0.1);
148 MenuLeafShowVar<double> showAlphaLeaf(&antialiasGamma, "AA Gamma");
149 Menu AAMenu("Anti-Aliasing",
150 NULL, &decAlphaLeaf,
151 NULL, &cycleAALeaf,
152 NULL, &incAlphaLeaf,
153 NULL, &showAlphaLeaf);
154
155 MenuLeafToggleBool showFPSToggle("Show FPS", &settings.showFPS);
156 MenuLeafIncVar<int> decFPSLeaf(&settings.fps, "FPS", -1, 2);
157 MenuLeafIncVar<int> incFPSLeaf(&settings.fps, "FPS", 1, 2);
158 MenuLeafShowVar<int> showFPSLeaf(&settings.fps, "FPS");
159 Menu fpsMenu("Framerate",
160 NULL, &decFPSLeaf,
161 NULL, &showFPSToggle,
162 NULL, &incFPSLeaf,
163 NULL, &showFPSLeaf
164 );
165
act()166 LeafReturn MenuLeafCycleBpp::act()
167 {
168 settings.bpp += 8;
169 if (settings.bpp > 32)
170 settings.bpp = 8;
171 return LR_NONE;
172 }
173 MenuLeafCycleBpp cycleBppLeaf;
name()174 string MenuLeafSetRes::name()
175 {
176 std::stringstream ss;
177 ss << "Set " << settings.width << "x" << settings.height << "x" << settings.bpp <<
178 ((settings.videoFlags & SDL_FULLSCREEN) ? " fs" : "");
179 return ss.str();
180 }
181 MenuLeafSetRes setResLeaf;
182
act()183 LeafReturn MenuLeafToggleFullscreen::act()
184 {
185 if (settings.videoFlags & SDL_FULLSCREEN)
186 settings.videoFlags &= ~SDL_FULLSCREEN;
187 else
188 settings.videoFlags |= SDL_FULLSCREEN;
189 return LR_NONE;
190 }
191 MenuLeafToggleFullscreen toggleFullscreen;
192
act()193 LeafReturn MenuLeafCycleRes::act()
194 {
195 std::vector<Rect> modes = getSDLModes();
196 if (modes.empty())
197 return LR_NONE;
198
199 // set to next biggest mode if exists, else smallest mode
200 Rect currentMode(settings.width, settings.height);
201
202 Rect nextBiggestMode;
203 Rect smallestMode;
204 for (std::vector<Rect>::iterator it = modes.begin();
205 it != modes.end(); it++)
206 {
207 if (Rect::cmp(*it, currentMode) == 1 &&
208 (nextBiggestMode.w == 0 ||
209 Rect::cmp(*it, nextBiggestMode) == -1))
210 nextBiggestMode = *it;
211 if (smallestMode.w == 0 || Rect::cmp(*it, smallestMode) == -1)
212 smallestMode = *it;
213 }
214
215 const Rect newMode = nextBiggestMode.w ? nextBiggestMode : smallestMode;
216 if (newMode.w == 0)
217 return LR_NONE;
218
219 settings.width = newMode.w;
220 settings.height = newMode.h;
221 return LR_NONE;
222 }
223 MenuLeafCycleRes cycleRes;
224
name()225 string MenuLeafCycleBGType::name()
226 {
227 return string("Background: " + bgTypeStrings[settings.bgType]);
228 }
act()229 LeafReturn MenuLeafCycleBGType::act()
230 {
231 if (settings.bgType == BG_LAST)
232 settings.bgType = BG_FIRST;
233 else
234 settings.bgType = BGType(settings.bgType + 1);
235 return LR_NEWBACKGROUND;
236 }
237 MenuLeafCycleBGType cycleBGType;
238
239 Menu resolutionMenu("Display",
240 NULL, &cycleRes,
241 NULL, &toggleFullscreen,
242 NULL, &cycleBppLeaf,
243 NULL, &setResLeaf
244 );
245
246 Menu graphicsMenu("Graphics",
247 &resolutionMenu, NULL,
248 NULL, &cycleBGType,
249 &fpsMenu, NULL,
250 &AAMenu, NULL
251 );
252
name()253 string MenuLeafSpeed::name()
254 {
255 return (string("Speed: ") +
256 speedString(settings.speed));
257 }
act()258 LeafReturn MenuLeafSpeed::act()
259 {
260 settings.speed = (settings.speed + 1) % 3;
261 return LR_NEWSPEED;
262 }
263 MenuLeafSpeed speed;
264
265 MenuLeafIncVar<float> decTurnRateLeaf(&settings.turnRateFactor,
266 "Turn Rate", -0.1, 0.1, 1);
267 MenuLeafIncVar<float> incTurnRateLeaf(&settings.turnRateFactor,
268 "Turn Rate", 0.1, 0.1, 1);
269 MenuLeafShowVar<float> showTurnRateLeaf(&settings.turnRateFactor,
270 "Turn Rate");
271 Menu turnRateMenu("Turn rate",
272 NULL, &decTurnRateLeaf,
273 NULL, NULL,
274 NULL, &incTurnRateLeaf,
275 NULL, &showTurnRateLeaf
276 );
277
name()278 string MenuLeafSetKey::name()
279 {
280 if (settings.commandToBind == C_NONE)
281 return string(longCommandNames[selectedKey]) + string(": ") +
282 settings.keybindings[selectedKey].getString();
283 else
284 return string(longCommandNames[selectedKey]) + string(": <press a key>");
285 }
act()286 LeafReturn MenuLeafSetKey::act()
287 {
288 settings.commandToBind = selectedKey;
289 return LR_NONE;
290 }
act()291 LeafReturn MenuLeafPrevKey::act()
292 {
293 if (*commandp == C_FIRST)
294 *commandp = settings.debug ? C_LASTDEBUG : C_LAST;
295 else
296 *commandp = command(*commandp - 1);
297 return LR_NONE;
298 }
act()299 LeafReturn MenuLeafNextKey::act()
300 {
301 if (*commandp == (settings.debug ? C_LASTDEBUG : C_LAST))
302 *commandp = C_FIRST;
303 else
304 *commandp = command(*commandp + 1);
305 return LR_NONE;
306 }
307
getConflictingCommand()308 command MenuLeafKeyConflicts::getConflictingCommand()
309 {
310 const bool menu = isMenuCommand(*commandp);
311 const Key k = settings.keybindings.get(*commandp);
312
313 if (k == KEY_NONE)
314 return C_NONE;
315
316 for (command c = C_FIRST; c <= C_LAST; c = command(c+1))
317 if (settings.keybindings.get(c) == k && c != *commandp
318 && isMenuCommand(c) == menu)
319 return c;
320 return C_NONE;
321 }
name()322 string MenuLeafKeyConflicts::name()
323 {
324 const command c = getConflictingCommand();
325 if (c == C_NONE)
326 return string("");
327 else
328 return string("Conflicts with ") + longCommandNames[c];
329 }
act()330 LeafReturn MenuLeafKeyConflicts::act()
331 {
332 const command c = getConflictingCommand();
333 if (c != C_NONE)
334 *commandp = c;
335 return LR_NONE;
336 }
337
338 MenuLeafSetKey setKey;
339 MenuLeafPrevKey prevKey(&setKey.selectedKey);
340 MenuLeafNextKey nextKey(&setKey.selectedKey);
341 MenuLeafKeyConflicts keyConflicts(&setKey.selectedKey);
342
343 Menu keybindingsMenu("Keybindings",
344 NULL, &prevKey,
345 NULL, &keyConflicts,
346 NULL, &nextKey,
347 NULL, &setKey
348 );
349
350 Menu controlMenu("Control",
351 NULL, NULL,
352 &turnRateMenu, NULL,
353 NULL, NULL,
354 &keybindingsMenu, NULL
355 );
356
357 Menu interfaceMenu("Interface",
358 &graphicsMenu, NULL,
359 NULL, NULL,
360 &controlMenu, NULL,
361 #ifdef SOUND
362 &soundMenu, NULL
363 #else
364 NULL, NULL
365 #endif
366 );
367
368 MenuLeafReturn saveLeaf("Save settings", LR_SAVESETTINGS);
369 Menu settingsMenu("Settings",
370 &interfaceMenu, NULL,
371 NULL, &saveLeaf,
372 &viewMenu, NULL,
373 NULL, &speed
374 );
375
376 MenuLeafReturn quitLeaf("Quit", LR_QUIT);
377 MenuLeafReturn surrenderLeaf("Surrender", LR_SURRENDER);
378 Menu quitMenu("Abort",
379 NULL, &quitLeaf,
380 NULL, NULL,
381 NULL, &surrenderLeaf
382 );
383
384 MenuLeafReturn resumeLeaf("Resume", LR_EXITMENU);
385
386 Menu topMenu("top",
387 NULL, NULL,
388 &settingsMenu, NULL,
389 NULL, NULL,
390 &quitMenu, NULL
391 );
392
drawMenu(SDL_Surface * surface,const Menu & menu)393 void drawMenu(SDL_Surface* surface, const Menu& menu)
394 {
395 static const Uint32 colour = 0xffffffd0;
396 int fw = 10, fh = 20;
397 int menuRad = std::max(fh, screenGeom.rad/8);
398 const int textRoom = std::max(std::max(std::max(
399 menu.textOfDir(0).length()*fw,
400 menu.textOfDir(1).length()*fw/2),
401 menu.textOfDir(2).length()*fw),
402 menu.textOfDir(3).length()*fw/2);
403
404 // check we have room:
405 if (screenGeom.centre.x - menuRad - textRoom > screenGeom.rad/5)
406 gfxPrimitivesSetFont(fontBig, 10, 20);
407 else
408 {
409 // use smaller font
410 fw = 7;
411 fh = 13;
412 menuRad = std::max(fh, screenGeom.rad/10);
413 gfxPrimitivesSetFont(fontSmall, 7, 13);
414 }
415
416 filledCircleColor(surface, screenGeom.centre.x, screenGeom.centre.y+fh/2, screenGeom.rad/30,
417 colour - 0x20);
418 aacircleColor(surface, screenGeom.centre.x, screenGeom.centre.y+fh/2, screenGeom.rad/30,
419 colour - 0x20);
420
421 const int textSizeHoriz = std::max(
422 menu.textOfDir(0).length()*fw,
423 menu.textOfDir(2).length()*fw);
424 for (int dir = 0; dir < 4; dir++)
425 {
426 const string text = menu.textOfDir(dir);
427 if (!text.empty())
428 {
429 int x=0, y=0;
430 switch (dir)
431 {
432 case 0:
433 x = screenGeom.centre.x - menuRad - textSizeHoriz/2 - text.length()*fw/2;
434 y = screenGeom.centre.y;
435 break;
436 case 1:
437 x = screenGeom.centre.x - text.length()*fw/2;
438 y = screenGeom.centre.y - menuRad;
439 break;
440 case 2:
441 x = screenGeom.centre.x + menuRad + textSizeHoriz/2 - text.length()*fw/2;
442 y = screenGeom.centre.y;
443 break;
444 case 3:
445 x = screenGeom.centre.x - text.length()*fw/2;
446 y = screenGeom.centre.y + menuRad;
447 break;
448 default:;
449 }
450 stringColor(surface, x, y, text.c_str(), colour);
451 }
452 }
453 }
454