OpenTTD
intro_gui.cpp
Go to the documentation of this file.
1 /* $Id: intro_gui.cpp 27653 2016-09-04 16:06:50Z alberth $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * OpenTTD 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, version 2.
6  * OpenTTD 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.
7  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8  */
9 
12 #include "stdafx.h"
13 #include "error.h"
14 #include "gui.h"
15 #include "window_gui.h"
16 #include "textbuf_gui.h"
17 #include "network/network.h"
18 #include "genworld.h"
19 #include "network/network_gui.h"
21 #include "landscape_type.h"
22 #include "strings_func.h"
23 #include "fios.h"
24 #include "ai/ai_gui.hpp"
25 #include "gfx_func.h"
26 #include "core/geometry_func.hpp"
27 #include "language.h"
28 #include "rev.h"
29 #include "highscore.h"
30 
31 #include "widgets/intro_widget.h"
32 
33 #include "table/strings.h"
34 #include "table/sprites.h"
35 
36 #include "safeguards.h"
37 
38 struct SelectGameWindow : public Window {
39 
40  SelectGameWindow(WindowDesc *desc) : Window(desc)
41  {
42  this->CreateNestedTree();
43  this->FinishInitNested(0);
44  this->OnInvalidateData();
45  }
46 
52  virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
53  {
54  if (!gui_scope) return;
59  }
60 
61  virtual void OnInit()
62  {
63  bool missing = _current_language->missing >= _settings_client.gui.missing_strings_threshold && !IsReleasedVersion();
64  this->GetWidget<NWidgetStacked>(WID_SGI_TRANSLATION_SELECTION)->SetDisplayedPlane(missing ? 0 : SZSP_NONE);
65  }
66 
67  virtual void DrawWidget(const Rect &r, int widget) const
68  {
69  switch (widget) {
72  DrawStringMultiLine(r.left, r.right, r.top, r.bottom, STR_INTRO_TRANSLATION, TC_FROMSTRING, SA_CENTER);
73  break;
74  }
75  }
76 
77  virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
78  {
79  switch (widget) {
80  case WID_SGI_TRANSLATION: {
82  int height = GetStringHeight(STR_INTRO_TRANSLATION, size->width);
83  if (height > 3 * FONT_HEIGHT_NORMAL) {
84  /* Don't let the window become too high. */
85  Dimension textdim = GetStringBoundingBox(STR_INTRO_TRANSLATION);
86  textdim.height *= 3;
87  textdim.width -= textdim.width / 2;
88  *size = maxdim(*size, textdim);
89  } else {
90  size->height = height + padding.height;
91  }
92  break;
93  }
94  }
95  }
96 
97  virtual void OnClick(Point pt, int widget, int click_count)
98  {
99 #ifdef ENABLE_NETWORK
100  /* Do not create a network server when you (just) have closed one of the game
101  * creation/load windows for the network server. */
103 #endif /* ENABLE_NETWORK */
104 
105  switch (widget) {
107  if (_ctrl_pressed) {
109  } else {
111  }
112  break;
113 
118 
120  if (!_network_available) {
121  ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
122  } else {
123  ShowNetworkGameWindow();
124  }
125  break;
126 
130  break;
131 
132  case WID_SGI_OPTIONS: ShowGameOptions(); break;
133  case WID_SGI_HIGHSCORE: ShowHighscoreTable(); break;
135  case WID_SGI_GRF_SETTINGS: ShowNewGRFSettings(true, true, false, &_grfconfig_newgame); break;
137  if (!_network_available) {
138  ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
139  } else {
141  }
142  break;
144  case WID_SGI_EXIT: HandleExitGameRequest(); break;
145  }
146  }
147 };
148 
149 static const NWidgetPart _nested_select_game_widgets[] = {
150  NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_INTRO_CAPTION, STR_NULL),
151  NWidget(WWT_PANEL, COLOUR_BROWN),
153 
154  /* 'generate game' and 'load game' buttons */
156  NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_GENERATE_GAME), SetMinimalSize(158, 12),
157  SetDataTip(STR_INTRO_NEW_GAME, STR_INTRO_TOOLTIP_NEW_GAME), SetPadding(0, 0, 0, 10), SetFill(1, 0),
158  NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_LOAD_GAME), SetMinimalSize(158, 12),
159  SetDataTip(STR_INTRO_LOAD_GAME, STR_INTRO_TOOLTIP_LOAD_GAME), SetPadding(0, 10, 0, 0), SetFill(1, 0),
160  EndContainer(),
161 
163 
164  /* 'play scenario' and 'play heightmap' buttons */
166  NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_PLAY_SCENARIO), SetMinimalSize(158, 12),
167  SetDataTip(STR_INTRO_PLAY_SCENARIO, STR_INTRO_TOOLTIP_PLAY_SCENARIO), SetPadding(0, 0, 0, 10), SetFill(1, 0),
168  NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_PLAY_HEIGHTMAP), SetMinimalSize(158, 12),
169  SetDataTip(STR_INTRO_PLAY_HEIGHTMAP, STR_INTRO_TOOLTIP_PLAY_HEIGHTMAP), SetPadding(0, 10, 0, 0), SetFill(1, 0),
170  EndContainer(),
171 
173 
174  /* 'edit scenario' and 'play multiplayer' buttons */
176  NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_EDIT_SCENARIO), SetMinimalSize(158, 12),
177  SetDataTip(STR_INTRO_SCENARIO_EDITOR, STR_INTRO_TOOLTIP_SCENARIO_EDITOR), SetPadding(0, 0, 0, 10), SetFill(1, 0),
178  NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_PLAY_NETWORK), SetMinimalSize(158, 12),
179  SetDataTip(STR_INTRO_MULTIPLAYER, STR_INTRO_TOOLTIP_MULTIPLAYER), SetPadding(0, 10, 0, 0), SetFill(1, 0),
180  EndContainer(),
181 
183 
184  /* climate selection buttons */
186  NWidget(NWID_SPACER), SetMinimalSize(10, 0), SetFill(1, 0),
188  SetDataTip(SPR_SELECT_TEMPERATE, STR_INTRO_TOOLTIP_TEMPERATE),
189  NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0),
191  SetDataTip(SPR_SELECT_SUB_ARCTIC, STR_INTRO_TOOLTIP_SUB_ARCTIC_LANDSCAPE),
192  NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0),
194  SetDataTip(SPR_SELECT_SUB_TROPICAL, STR_INTRO_TOOLTIP_SUB_TROPICAL_LANDSCAPE),
195  NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0),
197  SetDataTip(SPR_SELECT_TOYLAND, STR_INTRO_TOOLTIP_TOYLAND_LANDSCAPE),
198  NWidget(NWID_SPACER), SetMinimalSize(10, 0), SetFill(1, 0),
199  EndContainer(),
200 
204  NWidget(WWT_EMPTY, COLOUR_ORANGE, WID_SGI_TRANSLATION), SetMinimalSize(316, 12), SetFill(1, 0), SetPadding(0, 10, 7, 10),
205  EndContainer(),
206  EndContainer(),
207 
208  /* 'game options' and 'advanced settings' buttons */
210  NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_OPTIONS), SetMinimalSize(158, 12),
211  SetDataTip(STR_INTRO_GAME_OPTIONS, STR_INTRO_TOOLTIP_GAME_OPTIONS), SetPadding(0, 0, 0, 10), SetFill(1, 0),
213  SetDataTip(STR_INTRO_CONFIG_SETTINGS_TREE, STR_INTRO_TOOLTIP_CONFIG_SETTINGS_TREE), SetPadding(0, 10, 0, 0), SetFill(1, 0),
214  EndContainer(),
215 
217 
218  /* 'script settings' and 'newgrf settings' buttons */
220  NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_AI_SETTINGS), SetMinimalSize(158, 12),
221  SetDataTip(STR_INTRO_SCRIPT_SETTINGS, STR_INTRO_TOOLTIP_SCRIPT_SETTINGS), SetPadding(0, 0, 0, 10), SetFill(1, 0),
222  NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_GRF_SETTINGS), SetMinimalSize(158, 12),
223  SetDataTip(STR_INTRO_NEWGRF_SETTINGS, STR_INTRO_TOOLTIP_NEWGRF_SETTINGS), SetPadding(0, 10, 0, 0), SetFill(1, 0),
224  EndContainer(),
225 
227 
228  /* 'online content' and 'highscore' buttons */
231  SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT), SetPadding(0, 0, 0, 10), SetFill(1, 0),
232  NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_HIGHSCORE), SetMinimalSize(158, 12),
233  SetDataTip(STR_INTRO_HIGHSCORE, STR_INTRO_TOOLTIP_HIGHSCORE), SetPadding(0, 10, 0, 0), SetFill(1, 0),
234  EndContainer(),
235 
237 
238  /* 'exit program' button */
240  NWidget(NWID_SPACER), SetFill(1, 0),
241  NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_EXIT), SetMinimalSize(128, 12),
242  SetDataTip(STR_INTRO_QUIT, STR_INTRO_TOOLTIP_QUIT),
243  NWidget(NWID_SPACER), SetFill(1, 0),
244  EndContainer(),
245 
247 
248  EndContainer(),
249 };
250 
251 static WindowDesc _select_game_desc(
252  WDP_CENTER, NULL, 0, 0,
254  0,
255  _nested_select_game_widgets, lengthof(_nested_select_game_widgets)
256 );
257 
258 void ShowSelectGameWindow()
259 {
260  new SelectGameWindow(&_select_game_desc);
261 }
262 
263 static void AskExitGameCallback(Window *w, bool confirmed)
264 {
265  if (confirmed) _exit_game = true;
266 }
267 
268 void AskExitGame()
269 {
270 #if defined(_WIN32)
271  SetDParam(0, STR_OSNAME_WINDOWS);
272 #elif defined(__APPLE__)
273  SetDParam(0, STR_OSNAME_OSX);
274 #elif defined(__BEOS__)
275  SetDParam(0, STR_OSNAME_BEOS);
276 #elif defined(__HAIKU__)
277  SetDParam(0, STR_OSNAME_HAIKU);
278 #elif defined(__MORPHOS__)
279  SetDParam(0, STR_OSNAME_MORPHOS);
280 #elif defined(__AMIGA__)
281  SetDParam(0, STR_OSNAME_AMIGAOS);
282 #elif defined(__OS2__)
283  SetDParam(0, STR_OSNAME_OS2);
284 #elif defined(SUNOS)
285  SetDParam(0, STR_OSNAME_SUNOS);
286 #elif defined(DOS)
287  SetDParam(0, STR_OSNAME_DOS);
288 #else
289  SetDParam(0, STR_OSNAME_UNIX);
290 #endif
291  ShowQuery(
292  STR_QUIT_CAPTION,
293  STR_QUIT_ARE_YOU_SURE_YOU_WANT_TO_EXIT_OPENTTD,
294  NULL,
295  AskExitGameCallback
296  );
297 }
298 
299 
300 static void AskExitToGameMenuCallback(Window *w, bool confirmed)
301 {
302  if (confirmed) {
305  }
306 }
307 
308 void AskExitToGameMenu()
309 {
310  ShowQuery(
311  STR_ABANDON_GAME_CAPTION,
312  (_game_mode != GM_EDITOR) ? STR_ABANDON_GAME_QUERY : STR_ABANDON_SCENARIO_QUERY,
313  NULL,
314  AskExitToGameMenuCallback
315  );
316 }