File: main_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 (244 lines) | stat: -rw-r--r-- 6,580 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
/******************************************************************************
 *  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
 ******************************************************************************
 * Game menu from which one may start a new game, modify options, obtain some
 * infomations or leave the game.
 *****************************************************************************/

#include "menu/main_menu.h"
#include "gui/grid_box.h"
#include "gui/big/button_pic.h"
#include "game/config.h"
#include "graphic/text.h"
#include "graphic/video.h"
#include "include/app.h"
#include "include/constant.h"
#include "sound/jukebox.h"
#include "tool/resource_manager.h"
#include "tool/stats.h"

#ifndef WIN32
#include <dirent.h>
#endif

// Position du texte de la version
const int VERSION_DY = -40;

MainMenu::~MainMenu()
{
  delete version_text;
  delete website_text;
  StatStop("Main:Menu");
}

MainMenu::MainMenu() :
    Menu("main_menu/bg_main", vNo)
{
  Point2i size(120,110);
  Box* box = new GridBox(2, 4, 6, true);

  play = new ButtonPic(_("Play"), "menu/ico_play", size);
  box->AddWidget(play);

  network = new ButtonPic(_("Network Game"), "menu/ico_network_menu", size);
  box->AddWidget(network);

  options = new ButtonPic(_("Options"), "menu/ico_options_menu", size);
  box->AddWidget(options);

  bench = new ButtonPic(_("Benchmark"), "menu/ico_benchmark_menu", size);
  box->AddWidget(bench);

  help = new ButtonPic(_("Help"), "menu/ico_help", size);
  box->AddWidget(help);

  credits = new ButtonPic(_("Credits"), "menu/ico_credits", size);
  box->AddWidget(credits);

  replay = new ButtonPic(_("Replay"), "menu/ico_replay", size);
  box->AddWidget(replay);

  quit =  new ButtonPic(_("Quit"), "menu/ico_quit", size);
  box->AddWidget(quit);

  widgets.AddWidget(box);

  // We must "pack" all the widgets before centering the box to compute its size
  box->Pack();

  uint center_x = GetMainWindow().GetWidth()/2;
  uint center_y = GetMainWindow().GetHeight()/2;
  box->SetPosition(center_x - box->GetSizeX()/2, center_y - box->GetSizeY()/2);

  widgets.Pack();

  std::string s = _("Version") + std::string(" ")+Constants::WARMUX_VERSION;
  version_text = new Text(s, orange_color, Font::FONT_MEDIUM, Font::FONT_BOLD, true);

  std::string s2(Constants::WEB_SITE);
  website_text = new Text(s2, orange_color, Font::FONT_MEDIUM, Font::FONT_BOLD, true);

  if (!JukeBox::GetInstance()->IsPlayingMusic()) {
    JukeBox::GetInstance()->PlayMusic("menu");
  }

  StatStart("Main:Menu");
}

void MainMenu::Init(void)
{
  Profile * xmlProfile = GetResourceManager().LoadXMLProfile("menu.xml", false);
  XmlReader * xmlFile = xmlProfile->GetXMLDocument();

  const xmlNode * mainMenuNode = xmlFile->GetFirstNamedChild(xmlFile->GetRoot(), "MainMenu");
  if (NULL == mainMenuNode) {
    Error("MainMenu: can't load 'MainMenu' xml node from menu.xml");
    exit(EXIT_FAILURE);
  }

  LoadMenu(xmlProfile, mainMenuNode);
}

void MainMenu::SelectAction(const Widget * widget)
{
  if (widget == play) {
    choice = PLAY;
    close_menu = true;
  } else if (widget == network) {
    choice = NETWORK;
    close_menu = true;
  } else if (widget == options) {
    choice = OPTIONS;
    close_menu = true;
  } else if (widget == bench) {
    choice = BENCHMARK;
    close_menu = true;
  } else if (widget == help) {
    choice = HELP;
    close_menu = true;
  } else if (widget == credits) {
    choice = CREDITS;
    close_menu = true;
  } else if (widget == replay) {
    choice = REPLAY;
    close_menu = true;
  } else if (widget == quit) {
    choice = QUIT;
    close_menu = true;
  }

  // New implementation (XML custom menus)
  /*
  if (NULL == widget) {
    return;
  }
  std::string action = widget->GetActionName();
  if ("GoToLocalGameMenu" == action) {
    choice = PLAY;
    close_menu = true;
  } else if ("GoToNetworkGameMenu" == action) {
    choice = NETWORK;
    close_menu = true;
  } else if ("GoToOptionsMenu" == action) {
    choice = OPTIONS;
    close_menu = true;
  } else if ("GoToHelpMenu" == action) {
    choice = HELP;
    close_menu = true;
  } else if ("GoToCreditsMenu" == action) {
    choice = CREDITS;
    close_menu = true;
  } else if ("Quit" == action) {
    choice = QUIT;
    close_menu = true;
  }
  */
}

void MainMenu::OnClickUp(const Point2i &mousePosition, int button)
{
  Widget* b = widgets.ClickUp(mousePosition,button);
  if (b) {
    SelectAction(b);
    JukeBox::GetInstance()->Play("default", "menu/clic");
  }
}

void MainMenu::OnClick(const Point2i &/*mousePosition*/, int /*button*/)
{
  // nothing to do while button is still not released
}

MainMenu::menu_item MainMenu::Run ()
{
  choice = NONE;

  Menu::Run();

  ASSERT( choice != NONE );
  return choice;
}

bool MainMenu::signal_cancel()
{
  choice = QUIT;
  return true;
}

bool MainMenu::signal_ok()
{
  Widget * w = widgets.GetCurrentKeyboardSelectedWidget();
  if (w) {
    SelectAction(w);
  } else {
    choice = PLAY;
  }
  return true;
}

void MainMenu::DrawBackground()
{
  Surface& window = GetMainWindow();

  Menu::DrawBackground();

  version_text->DrawCenter(Point2i(window.GetWidth()/2,
                                   window.GetHeight() + VERSION_DY));
  website_text->DrawCenter(Point2i(window.GetWidth()/2,
                                   window.GetHeight() + VERSION_DY/2));
}

void MainMenu::RedrawBackground(const Rectanglei& rect) const
{
  Surface& window = GetMainWindow();

  Menu::RedrawBackground(rect);

  Point2i version_pos(window.GetWidth()/2,
		      window.GetHeight() + VERSION_DY);
  Point2i website_pos(window.GetWidth()/2,
		      window.GetHeight() + VERSION_DY/2);

  if (rect.Contains(version_pos)) {
    version_text->DrawCenter(version_pos);
  }

  if (rect.Contains(website_pos)) {
    website_text->DrawCenter(website_pos);
  }
}