1 /******************************************************************************
2  *  Warmux is a convivial mass murder game.
3  *  Copyright (C) 2001-2011 Warmux Team.
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 2 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, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18  ******************************************************************************
19  * Game menu from which one may start a new game, modify options, obtain some
20  * infomations or leave the game.
21  *****************************************************************************/
22 
23 #include "menu/main_menu.h"
24 #include "gui/grid_box.h"
25 #include "gui/big/button_pic.h"
26 #include "game/config.h"
27 #include "graphic/text.h"
28 #include "graphic/video.h"
29 #include "include/app.h"
30 #include "include/constant.h"
31 #include "sound/jukebox.h"
32 #include "tool/resource_manager.h"
33 #include "tool/stats.h"
34 
35 #ifndef WIN32
36 #include <dirent.h>
37 #endif
38 
39 // Position du texte de la version
40 const int VERSION_DY = -40;
41 
~MainMenu()42 MainMenu::~MainMenu()
43 {
44   delete version_text;
45   delete website_text;
46   StatStop("Main:Menu");
47 }
48 
MainMenu()49 MainMenu::MainMenu() :
50     Menu("main_menu/bg_main", vNo)
51 {
52   Point2i size(120,110);
53   Box* box = new GridBox(2, 4, 6, true);
54 
55   play = new ButtonPic(_("Play"), "menu/ico_play", size);
56   box->AddWidget(play);
57 
58   network = new ButtonPic(_("Network Game"), "menu/ico_network_menu", size);
59   box->AddWidget(network);
60 
61   options = new ButtonPic(_("Options"), "menu/ico_options_menu", size);
62   box->AddWidget(options);
63 
64   bench = new ButtonPic(_("Benchmark"), "menu/ico_benchmark_menu", size);
65   box->AddWidget(bench);
66 
67   help = new ButtonPic(_("Help"), "menu/ico_help", size);
68   box->AddWidget(help);
69 
70   credits = new ButtonPic(_("Credits"), "menu/ico_credits", size);
71   box->AddWidget(credits);
72 
73   replay = new ButtonPic(_("Replay"), "menu/ico_replay", size);
74   box->AddWidget(replay);
75 
76   quit =  new ButtonPic(_("Quit"), "menu/ico_quit", size);
77   box->AddWidget(quit);
78 
79   widgets.AddWidget(box);
80 
81   // We must "pack" all the widgets before centering the box to compute its size
82   box->Pack();
83 
84   uint center_x = GetMainWindow().GetWidth()/2;
85   uint center_y = GetMainWindow().GetHeight()/2;
86   box->SetPosition(center_x - box->GetSizeX()/2, center_y - box->GetSizeY()/2);
87 
88   widgets.Pack();
89 
90   std::string s = _("Version") + std::string(" ")+Constants::WARMUX_VERSION;
91   version_text = new Text(s, orange_color, Font::FONT_MEDIUM, Font::FONT_BOLD, true);
92 
93   std::string s2(Constants::WEB_SITE);
94   website_text = new Text(s2, orange_color, Font::FONT_MEDIUM, Font::FONT_BOLD, true);
95 
96   if (!JukeBox::GetInstance()->IsPlayingMusic()) {
97     JukeBox::GetInstance()->PlayMusic("menu");
98   }
99 
100   StatStart("Main:Menu");
101 }
102 
Init(void)103 void MainMenu::Init(void)
104 {
105   Profile * xmlProfile = GetResourceManager().LoadXMLProfile("menu.xml", false);
106   XmlReader * xmlFile = xmlProfile->GetXMLDocument();
107 
108   const xmlNode * mainMenuNode = xmlFile->GetFirstNamedChild(xmlFile->GetRoot(), "MainMenu");
109   if (NULL == mainMenuNode) {
110     Error("MainMenu: can't load 'MainMenu' xml node from menu.xml");
111     exit(EXIT_FAILURE);
112   }
113 
114   LoadMenu(xmlProfile, mainMenuNode);
115 }
116 
SelectAction(const Widget * widget)117 void MainMenu::SelectAction(const Widget * widget)
118 {
119   if (widget == play) {
120     choice = PLAY;
121     close_menu = true;
122   } else if (widget == network) {
123     choice = NETWORK;
124     close_menu = true;
125   } else if (widget == options) {
126     choice = OPTIONS;
127     close_menu = true;
128   } else if (widget == bench) {
129     choice = BENCHMARK;
130     close_menu = true;
131   } else if (widget == help) {
132     choice = HELP;
133     close_menu = true;
134   } else if (widget == credits) {
135     choice = CREDITS;
136     close_menu = true;
137   } else if (widget == replay) {
138     choice = REPLAY;
139     close_menu = true;
140   } else if (widget == quit) {
141     choice = QUIT;
142     close_menu = true;
143   }
144 
145   // New implementation (XML custom menus)
146   /*
147   if (NULL == widget) {
148     return;
149   }
150   std::string action = widget->GetActionName();
151   if ("GoToLocalGameMenu" == action) {
152     choice = PLAY;
153     close_menu = true;
154   } else if ("GoToNetworkGameMenu" == action) {
155     choice = NETWORK;
156     close_menu = true;
157   } else if ("GoToOptionsMenu" == action) {
158     choice = OPTIONS;
159     close_menu = true;
160   } else if ("GoToHelpMenu" == action) {
161     choice = HELP;
162     close_menu = true;
163   } else if ("GoToCreditsMenu" == action) {
164     choice = CREDITS;
165     close_menu = true;
166   } else if ("Quit" == action) {
167     choice = QUIT;
168     close_menu = true;
169   }
170   */
171 }
172 
OnClickUp(const Point2i & mousePosition,int button)173 void MainMenu::OnClickUp(const Point2i &mousePosition, int button)
174 {
175   Widget* b = widgets.ClickUp(mousePosition,button);
176   if (b) {
177     SelectAction(b);
178     JukeBox::GetInstance()->Play("default", "menu/clic");
179   }
180 }
181 
OnClick(const Point2i &,int)182 void MainMenu::OnClick(const Point2i &/*mousePosition*/, int /*button*/)
183 {
184   // nothing to do while button is still not released
185 }
186 
Run()187 MainMenu::menu_item MainMenu::Run ()
188 {
189   choice = NONE;
190 
191   Menu::Run();
192 
193   ASSERT( choice != NONE );
194   return choice;
195 }
196 
signal_cancel()197 bool MainMenu::signal_cancel()
198 {
199   choice = QUIT;
200   return true;
201 }
202 
signal_ok()203 bool MainMenu::signal_ok()
204 {
205   Widget * w = widgets.GetCurrentKeyboardSelectedWidget();
206   if (w) {
207     SelectAction(w);
208   } else {
209     choice = PLAY;
210   }
211   return true;
212 }
213 
DrawBackground()214 void MainMenu::DrawBackground()
215 {
216   Surface& window = GetMainWindow();
217 
218   Menu::DrawBackground();
219 
220   version_text->DrawCenter(Point2i(window.GetWidth()/2,
221                                    window.GetHeight() + VERSION_DY));
222   website_text->DrawCenter(Point2i(window.GetWidth()/2,
223                                    window.GetHeight() + VERSION_DY/2));
224 }
225 
RedrawBackground(const Rectanglei & rect) const226 void MainMenu::RedrawBackground(const Rectanglei& rect) const
227 {
228   Surface& window = GetMainWindow();
229 
230   Menu::RedrawBackground(rect);
231 
232   Point2i version_pos(window.GetWidth()/2,
233 		      window.GetHeight() + VERSION_DY);
234   Point2i website_pos(window.GetWidth()/2,
235 		      window.GetHeight() + VERSION_DY/2);
236 
237   if (rect.Contains(version_pos)) {
238     version_text->DrawCenter(version_pos);
239   }
240 
241   if (rect.Contains(website_pos)) {
242     website_text->DrawCenter(website_pos);
243   }
244 }
245