File: menu.cpp

package info (click to toggle)
warmux 1%3A11.04.1%2Brepack2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 126,388 kB
  • sloc: cpp: 186,040; xml: 8,909; sh: 3,358; makefile: 1,052; ansic: 713
file content (492 lines) | stat: -rw-r--r-- 12,147 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/******************************************************************************
 *  Warmux is a convivial mass murder game.
 *  Copyright (C) 2001-2011 Warmux Team.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 ******************************************************************************
 * Generic menu
 *****************************************************************************/

#include <iostream>

#include "menu/menu.h"
#include "graphic/sprite.h"
#include "graphic/video.h"
#include "gui/big/button_pic.h"
#include "gui/button.h"
#include "gui/check_box.h"
#include "gui/grid_box.h"
#include "gui/horizontal_box.h"
#include "gui/label.h"
#include "gui/picture_text_cbox.h"
#include "gui/question.h"
#include "gui/spin_button.h"
#include "gui/text_box.h"
#include "gui/vertical_box.h"
#include "include/app.h"
#include "interface/keyboard.h"
#include "interface/mouse.h"
#include "sound/jukebox.h"
#include "tool/resource_manager.h"

Menu::Menu(const std::string& bg, t_action _actions)
  : actions(_actions)
  , selected_widget(NULL)
{
  close_menu = false ;
  Surface& window = GetMainWindow();

  Profile *res = GetResourceManager().LoadXMLProfile("graphism.xml", false);
  background = new Sprite(GetResourceManager().LoadImage(res, bg, false));

  b_ok = NULL;
  b_cancel = NULL;

  if (actions == vNo) {
    actions_buttons = NULL;
  } else {
    actions_buttons = new HBox(50, false, false);

    if (actions == vOk || actions == vOkCancel) {
      b_ok = new Button(res, "menu/valider");
      b_ok->SetSizeX(80);
      actions_buttons->AddWidget(b_ok);
    }

    if (actions == vCancel  || actions == vOkCancel) {
      b_cancel = new Button(res, "menu/annuler");
      b_cancel->SetSizeX(80);
      actions_buttons->AddWidget(b_cancel);
    }

    widgets.AddWidget(actions_buttons);
    widgets.Pack();
    uint x = (window.GetWidth() - actions_buttons->GetSizeX())/2;
    uint y = window.GetHeight() - actions_buttons->GetSizeY();

    actions_buttons->SetPosition(x, y);
  }

  widgets.SetContainer(this);
}

Menu::Menu(void)
  : widgets()
  , actions(vNo)
  , background(NULL)
  , selected_widget(NULL)
  , b_cancel(NULL)
  , b_ok(NULL)
  , close_menu(false)
  , actions_buttons(NULL)
{
}

Menu::~Menu()
{
  AppWarmux::GetInstance()->SetCurrentMenu(NULL);
  if (background) {
    delete background;
  }
}

void Menu::LoadMenu(Profile * profile,
                    const xmlNode * rootMenuNode)
{
  LoadBackground(profile, rootMenuNode);
  LoadWidget(profile, rootMenuNode, &widgets);
  widgets.Pack();
}

void Menu::LoadBackground(Profile * profile,
                          const xmlNode * rootMenuNode)
{
  XmlReader * xmlFile = profile->GetXMLDocument();
  std::string file("menu/pic_not_found.png");
  xmlFile->ReadStringAttr(rootMenuNode, "backgroundPicture", file);
  file = profile->relative_path + file;
  Surface surface(file.c_str());
  background = new Sprite(surface);
}

void Menu::LoadWidget(Profile * profile,
                      const xmlNode * rootMenuNode,
                      WidgetList * container)
{
  XmlReader * xmlFile = profile->GetXMLDocument();
  uint widgetCount = xmlFile->GetNbChildren(rootMenuNode);
  const xmlNode * currentNode = xmlFile->GetFirstChild(rootMenuNode);
  std::string currentNodeName;

  // For each sub-node ...
  for ( ; widgetCount > 0; --widgetCount) {

    currentNodeName = xmlFile->GetNodeName(currentNode);
    Widget * newWidget = CreateWidget(profile, currentNode, currentNodeName);

    if (newWidget) {
      if ("GridBox" == currentNodeName ||
          "HorizontalBox" == currentNodeName ||
          "VerticalBox" == currentNodeName) {
        LoadWidget(profile, currentNode, (WidgetList*)newWidget);
      }
      container->AddWidget(newWidget);
    }
    currentNode = xmlFile->GetNextSibling(currentNode);
  }

  if (container) {
    container->Pack();
  }
}

Widget * Menu::CreateWidget(Profile * profile,
                            const xmlNode * widgetNode,
                            std::string & widgetName)
{
  Widget * widget = NULL;

  if ("Picture" == widgetName) {
    widget = new PictureWidget(profile, widgetNode);
  } else if ("GridBox" == widgetName) {
    widget = new GridBox(profile, widgetNode);
  } else if ("ButtonPic" == widgetName) {
    widget = new ButtonPic(profile, widgetNode);
  } else if ("Label" == widgetName) {
    widget = new Label(profile, widgetNode);
  } else if ("CheckBox" == widgetName) {
    widget = new CheckBox(profile, widgetNode);
  } else if ("PicTxtCheckBox" == widgetName ) {
    widget = new PictureTextCBox(profile, widgetNode);
  } else if ("VerticalBox" == widgetName) {
    widget = new VBox(profile, widgetNode);
  } else if ("HorizontalBox" == widgetName) {
    widget = new HBox(profile, widgetNode);
  } else if ("SpinButton" == widgetName) {
    widget = new SpinButton(profile, widgetNode);
  } else if ("PasswordBox" == widgetName) {
    widget = new PasswordBox(profile, widgetNode);
  } else if ("TextBox" == widgetName) {
    widget = new TextBox(profile, widgetNode);
  } else if ("Button" == widgetName) {
    widget = new Button(profile, widgetNode);
  }

  if (widget) {
    return widget->LoadXMLConfiguration() ? widget : NULL;
  }
  return NULL;
}

void Menu::play_ok_sound()
{
  JukeBox::GetInstance()->Play("default", "menu/ok");
}

void Menu::play_cancel_sound()
{
  JukeBox::GetInstance()->Play("default", "menu/cancel");
}

void Menu::play_error_sound()
{
  JukeBox::GetInstance()->Play("default", "menu/error");
}

void Menu::mouse_ok()
{
  if (signal_ok()) {
    play_ok_sound();
    close_menu = true;
  }
}

void Menu::mouse_cancel()
{
  if (signal_cancel()) {
    play_cancel_sound();
    close_menu = true;
  }
}

bool Menu::BasicOnClickUp(const Point2i &mousePosition)
{
  if (b_ok &&  b_ok->Contains(mousePosition)) {
    mouse_ok();
  } else if (b_cancel && b_cancel->Contains(mousePosition)) {
    mouse_cancel();
  } else {
    return false;
  }
  return true;
}

void Menu::key_ok()
{
  if (signal_ok()) {
    play_ok_sound();
    close_menu = true;
  }
}

void Menu::key_cancel()
{
  if (signal_cancel()) {
    play_cancel_sound();
    close_menu = true;
  }
}

void Menu::key_up()
{
  widgets.SetFocusOnPreviousWidget();
  RedrawMenu();
}

void Menu::key_down()
{
  widgets.SetFocusOnNextWidget();
  RedrawMenu();
}

void Menu::key_left()
{
}

void Menu::key_right()
{
}

void Menu::key_tab()
{
  widgets.SetFocusOnNextWidget();
}

void Menu::DisplayError(const std::string & msg)
{
  play_error_sound();

  std::cerr << msg << std::endl;

  Question question(Question::WARNING);
  question.Set(msg, true, 0);
  question.Ask();
}

void Menu::DrawBackground()
{
  if (!background) {
    return;
  }
  background->ScaleSize(GetMainWindow().GetSize()+1);
  background->Blit(GetMainWindow(), 0, 0);
}

void Menu::RedrawBackground(const Rectanglei & rect) const
{
  if (!background) {
    return;
  }
  background->Blit(GetMainWindow(), rect, rect.GetPosition());
}

void Menu::RedrawMenu()
{
  DrawBackground();
  widgets.NeedRedrawing();
}

// Class method
// Push a stupid user event to make the menu exits for SDL_WaitEvent
void Menu::WakeUpOnCallback()
{
  SDL_Event evnt;
  evnt.type = SDL_USEREVENT;
  evnt.user.code = 0;
  evnt.user.data1 = NULL;
  evnt.user.data2 = NULL;
  SDL_PushEvent(&evnt);
}

bool Menu::HandleGlobalEvent(const SDL_Event & evnt)
{
  if (evnt.type != SDL_KEYDOWN) {
    return false;
  }

  // Emergency exit
  if (evnt.key.keysym.sym == SDLK_ESCAPE
      && (SDL_GetModState() & KMOD_CTRL)) {
    AppWarmux::EmergencyExit();
    return true; // never reached
  }

  // Toggle fullscreen
#if 1
  int mod_state = SDL_GetModState();
  const Keyboard *kbd = Keyboard::GetConstInstance();
  ManMachineInterface::Key_t key = kbd->GetRegisteredAction(evnt.key.keysym.sym,
                                                            mod_state&KMOD_CTRL,
                                                            mod_state&KMOD_ALT,
                                                            mod_state&KMOD_SHIFT);
  if (key == ManMachineInterface::KEY_FULLSCREEN) {
    AppWarmux::GetInstance()->video->ToggleFullscreen();
    return true;
  }

  return false;
#else
  // There are many keys not checking that the game is running or not
  // Make chatsession static and modify correspoding code to use this
  return Keyboard::GetInstance()->HandleKeyEvent(evnt);
#endif
}

void Menu::HandleEvent(const SDL_Event& evnt)
{
  if (evnt.type == SDL_QUIT) {
#if defined MAEMO || defined __SYMBIAN32__
    AppWarmux::EmergencyExit();
#else
    key_cancel();
#endif
  } else if (evnt.type == SDL_KEYDOWN) {

    // Drop key events that are purely modifiers
    if (Keyboard::IsModifier(evnt.key.keysym.sym))
      return;

    // Allow widgets to interpret any key they want,
    // and do not reserve esc/return/delete/backspace
    bool used_by_widget = widgets.SendKey(evnt.key.keysym);

    if (!used_by_widget) {
      switch (evnt.key.keysym.sym) {
      case SDLK_ESCAPE:
        key_cancel();
        break;
      case SDLK_RETURN:
      case SDLK_KP_ENTER:
        key_ok();
        break;
      case SDLK_UP:
        key_up();
        break;
      case SDLK_DOWN:
        key_down();
        break;
      case SDLK_LEFT:
        key_left();
        break;
      case SDLK_RIGHT:
        key_right();
        break;
      case SDLK_TAB:
        key_tab();
        break;
      default:
        // should have been handle upper!
        break;
      }
    }
  } else if (evnt.type == SDL_MOUSEBUTTONUP) {
    Point2i mousePosition(evnt.button.x, evnt.button.y);

    if (!BasicOnClickUp(mousePosition)) {
      OnClickUp(mousePosition, evnt.button.button);
    }
  } else if (evnt.type == SDL_MOUSEBUTTONDOWN) {
    Point2i mousePosition(evnt.button.x, evnt.button.y);
    OnClick(mousePosition, evnt.button.button);
  }
}

void Menu::HandleEvents()
{
  // Poll and treat events
  SDL_Event evnt;

  if (!SDL_WaitEvent(&evnt)) {
    return;
  }

  do {
    // We might be set inactive while in here
    if (AppWarmux::GetInstance()->CheckInactive(evnt))
      continue;

    if (!HandleGlobalEvent(evnt)) {
      HandleEvent(evnt);
    }
  } while (SDL_PollEvent(&evnt) && !close_menu);
}

void Menu::Run(bool skip_menu)
{
  signal_begin_run();

  if (skip_menu) {
    signal_ok();
    return;
  }

  Mouse::pointer_t old_pointer = Mouse::GetInstance()->SetPointer(Mouse::POINTER_STANDARD);
  int x = 0;
  int y = 0;
  close_menu = false;

  // Display the background
  DrawBackground();

  SDL_GetMouseState(&x, &y);
  Point2i mousePosition(x, y);
  Display(mousePosition);

  SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);

  do {
    // this is the current menu (here in case we had run a submenu)
    AppWarmux::GetInstance()->SetCurrentMenu(this);

    // Poll and treat events
    HandleEvents();

    // Avoid to calculate redraw menu when comming back for closing.
    if (!close_menu) {
      SDL_GetMouseState( &x, &y );
      Point2i mousePosition(x, y);

      Display(mousePosition);
    }

  } while (!close_menu);

  Mouse::GetInstance()->SetPointer(old_pointer);
}

void Menu::Display(const Point2i& mousePosition)
{
  widgets.Update(mousePosition, last_mouse_position);
  Draw(mousePosition);
  AppWarmux::GetInstance()->video->Flip();
  last_mouse_position = mousePosition;
}

void Menu::SetActionButtonsXY(int x, int y)
{
  if (actions_buttons != NULL) {
    actions_buttons->SetPosition(x, y);
  }
}