OpenTTD
intro_gui.cpp
Go to the documentation of this file.
1 /* $Id$ */
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  void OnInvalidateData(int data = 0, bool gui_scope = true) override
53  {
54  if (!gui_scope) return;
59  }
60 
61  void OnInit() override
62  {
63  bool missing_sprites = _missing_extra_graphics > 0 && !IsReleasedVersion();
64  this->GetWidget<NWidgetStacked>(WID_SGI_BASESET_SELECTION)->SetDisplayedPlane(missing_sprites ? 0 : SZSP_NONE);
65 
66  bool missing_lang = _current_language->missing >= _settings_client.gui.missing_strings_threshold && !IsReleasedVersion();
67  this->GetWidget<NWidgetStacked>(WID_SGI_TRANSLATION_SELECTION)->SetDisplayedPlane(missing_lang ? 0 : SZSP_NONE);
68  }
69 
70  void DrawWidget(const Rect &r, int widget) const override
71  {
72  switch (widget) {
73  case WID_SGI_BASESET:
75  DrawStringMultiLine(r.left, r.right, r.top, r.bottom, STR_INTRO_BASESET, TC_FROMSTRING, SA_CENTER);
76  break;
77 
80  DrawStringMultiLine(r.left, r.right, r.top, r.bottom, STR_INTRO_TRANSLATION, TC_FROMSTRING, SA_CENTER);
81  break;
82  }
83  }
84 
85  void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
86  {
87  StringID str = 0;
88  switch (widget) {
89  case WID_SGI_BASESET:
91  str = STR_INTRO_BASESET;
92  break;
93 
96  str = STR_INTRO_TRANSLATION;
97  break;
98  }
99 
100  if (str != 0) {
101  int height = GetStringHeight(str, size->width);
102  if (height > 3 * FONT_HEIGHT_NORMAL) {
103  /* Don't let the window become too high. */
104  Dimension textdim = GetStringBoundingBox(str);
105  textdim.height *= 3;
106  textdim.width -= textdim.width / 2;
107  *size = maxdim(*size, textdim);
108  } else {
109  size->height = height + padding.height;
110  }
111  }
112  }
113 
114  void OnClick(Point pt, int widget, int click_count) override
115  {
116  /* Do not create a network server when you (just) have closed one of the game
117  * creation/load windows for the network server. */
119 
120  switch (widget) {
122  if (_ctrl_pressed) {
124  } else {
126  }
127  break;
128 
133 
135  if (!_network_available) {
136  ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
137  } else {
138  ShowNetworkGameWindow();
139  }
140  break;
141 
145  break;
146 
147  case WID_SGI_OPTIONS: ShowGameOptions(); break;
148  case WID_SGI_HIGHSCORE: ShowHighscoreTable(); break;
150  case WID_SGI_GRF_SETTINGS: ShowNewGRFSettings(true, true, false, &_grfconfig_newgame); break;
152  if (!_network_available) {
153  ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
154  } else {
156  }
157  break;
159  case WID_SGI_EXIT: HandleExitGameRequest(); break;
160  }
161  }
162 };
163 
164 static const NWidgetPart _nested_select_game_widgets[] = {
165  NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_INTRO_CAPTION, STR_NULL),
166  NWidget(WWT_PANEL, COLOUR_BROWN),
168 
169  /* 'generate game' and 'load game' buttons */
171  NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_GENERATE_GAME), SetMinimalSize(158, 12),
172  SetDataTip(STR_INTRO_NEW_GAME, STR_INTRO_TOOLTIP_NEW_GAME), SetPadding(0, 0, 0, 10), SetFill(1, 0),
173  NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_LOAD_GAME), SetMinimalSize(158, 12),
174  SetDataTip(STR_INTRO_LOAD_GAME, STR_INTRO_TOOLTIP_LOAD_GAME), SetPadding(0, 10, 0, 0), SetFill(1, 0),
175  EndContainer(),
176 
178 
179  /* 'play scenario' and 'play heightmap' buttons */
181  NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_PLAY_SCENARIO), SetMinimalSize(158, 12),
182  SetDataTip(STR_INTRO_PLAY_SCENARIO, STR_INTRO_TOOLTIP_PLAY_SCENARIO), SetPadding(0, 0, 0, 10), SetFill(1, 0),
183  NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_PLAY_HEIGHTMAP), SetMinimalSize(158, 12),
184  SetDataTip(STR_INTRO_PLAY_HEIGHTMAP, STR_INTRO_TOOLTIP_PLAY_HEIGHTMAP), SetPadding(0, 10, 0, 0), SetFill(1, 0),
185  EndContainer(),
186 
188 
189  /* 'edit scenario' and 'play multiplayer' buttons */
191  NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_EDIT_SCENARIO), SetMinimalSize(158, 12),
192  SetDataTip(STR_INTRO_SCENARIO_EDITOR, STR_INTRO_TOOLTIP_SCENARIO_EDITOR), SetPadding(0, 0, 0, 10), SetFill(1, 0),
193  NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_PLAY_NETWORK), SetMinimalSize(158, 12),
194  SetDataTip(STR_INTRO_MULTIPLAYER, STR_INTRO_TOOLTIP_MULTIPLAYER), SetPadding(0, 10, 0, 0), SetFill(1, 0),
195  EndContainer(),
196 
198 
199  /* climate selection buttons */
201  NWidget(NWID_SPACER), SetMinimalSize(10, 0), SetFill(1, 0),
203  SetDataTip(SPR_SELECT_TEMPERATE, STR_INTRO_TOOLTIP_TEMPERATE),
204  NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0),
206  SetDataTip(SPR_SELECT_SUB_ARCTIC, STR_INTRO_TOOLTIP_SUB_ARCTIC_LANDSCAPE),
207  NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0),
209  SetDataTip(SPR_SELECT_SUB_TROPICAL, STR_INTRO_TOOLTIP_SUB_TROPICAL_LANDSCAPE),
210  NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0),
212  SetDataTip(SPR_SELECT_TOYLAND, STR_INTRO_TOOLTIP_TOYLAND_LANDSCAPE),
213  NWidget(NWID_SPACER), SetMinimalSize(10, 0), SetFill(1, 0),
214  EndContainer(),
215 
219  NWidget(WWT_EMPTY, COLOUR_ORANGE, WID_SGI_BASESET), SetMinimalSize(316, 12), SetFill(1, 0), SetPadding(0, 10, 7, 10),
220  EndContainer(),
221  EndContainer(),
224  NWidget(WWT_EMPTY, COLOUR_ORANGE, WID_SGI_TRANSLATION), SetMinimalSize(316, 12), SetFill(1, 0), SetPadding(0, 10, 7, 10),
225  EndContainer(),
226  EndContainer(),
227 
228  /* 'game options' and 'advanced settings' buttons */
230  NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_OPTIONS), SetMinimalSize(158, 12),
231  SetDataTip(STR_INTRO_GAME_OPTIONS, STR_INTRO_TOOLTIP_GAME_OPTIONS), SetPadding(0, 0, 0, 10), SetFill(1, 0),
233  SetDataTip(STR_INTRO_CONFIG_SETTINGS_TREE, STR_INTRO_TOOLTIP_CONFIG_SETTINGS_TREE), SetPadding(0, 10, 0, 0), SetFill(1, 0),
234  EndContainer(),
235 
237 
238  /* 'script settings' and 'newgrf settings' buttons */
240  NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_AI_SETTINGS), SetMinimalSize(158, 12),
241  SetDataTip(STR_INTRO_SCRIPT_SETTINGS, STR_INTRO_TOOLTIP_SCRIPT_SETTINGS), SetPadding(0, 0, 0, 10), SetFill(1, 0),
242  NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_GRF_SETTINGS), SetMinimalSize(158, 12),
243  SetDataTip(STR_INTRO_NEWGRF_SETTINGS, STR_INTRO_TOOLTIP_NEWGRF_SETTINGS), SetPadding(0, 10, 0, 0), SetFill(1, 0),
244  EndContainer(),
245 
247 
248  /* 'online content' and 'highscore' buttons */
251  SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT), SetPadding(0, 0, 0, 10), SetFill(1, 0),
252  NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_HIGHSCORE), SetMinimalSize(158, 12),
253  SetDataTip(STR_INTRO_HIGHSCORE, STR_INTRO_TOOLTIP_HIGHSCORE), SetPadding(0, 10, 0, 0), SetFill(1, 0),
254  EndContainer(),
255 
257 
258  /* 'exit program' button */
260  NWidget(NWID_SPACER), SetFill(1, 0),
261  NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_EXIT), SetMinimalSize(128, 12),
262  SetDataTip(STR_INTRO_QUIT, STR_INTRO_TOOLTIP_QUIT),
263  NWidget(NWID_SPACER), SetFill(1, 0),
264  EndContainer(),
265 
267 
268  EndContainer(),
269 };
270 
271 static WindowDesc _select_game_desc(
272  WDP_CENTER, nullptr, 0, 0,
274  0,
275  _nested_select_game_widgets, lengthof(_nested_select_game_widgets)
276 );
277 
278 void ShowSelectGameWindow()
279 {
280  new SelectGameWindow(&_select_game_desc);
281 }
282 
283 static void AskExitGameCallback(Window *w, bool confirmed)
284 {
285  if (confirmed) _exit_game = true;
286 }
287 
288 void AskExitGame()
289 {
290 #if defined(_WIN32)
291  SetDParam(0, STR_OSNAME_WINDOWS);
292 #elif defined(__APPLE__)
293  SetDParam(0, STR_OSNAME_OSX);
294 #elif defined(__HAIKU__)
295  SetDParam(0, STR_OSNAME_HAIKU);
296 #elif defined(__OS2__)
297  SetDParam(0, STR_OSNAME_OS2);
298 #elif defined(SUNOS)
299  SetDParam(0, STR_OSNAME_SUNOS);
300 #else
301  SetDParam(0, STR_OSNAME_UNIX);
302 #endif
303  ShowQuery(
304  STR_QUIT_CAPTION,
305  STR_QUIT_ARE_YOU_SURE_YOU_WANT_TO_EXIT_OPENTTD,
306  nullptr,
307  AskExitGameCallback
308  );
309 }
310 
311 
312 static void AskExitToGameMenuCallback(Window *w, bool confirmed)
313 {
314  if (confirmed) {
317  }
318 }
319 
320 void AskExitToGameMenu()
321 {
322  ShowQuery(
323  STR_ABANDON_GAME_CAPTION,
324  (_game_mode != GM_EDITOR) ? STR_ABANDON_GAME_QUERY : STR_ABANDON_SCENARIO_QUERY,
325  nullptr,
326  AskExitToGameMenuCallback
327  );
328 }
void OnInit() override
Notification that the nested widget tree gets initialized.
Definition: intro_gui.cpp:61
Functions related to OTTD&#39;s strings.
Empty widget, place holder to reserve space in widget array.
Definition: widget_type.h:48
Highscore button.
Definition: intro_widget.h:32
ResizeInfo resize
Resize information.
Definition: window_gui.h:324
Window(WindowDesc *desc)
Empty constructor, initialization has been moved to InitNested() called from the constructor of the d...
Definition: window.cpp:1851
byte landscape
the landscape we&#39;re currently in
void SetWidgetLoweredState(byte widget_index, bool lowered_stat)
Sets the lowered/raised status of a widget.
Definition: window_gui.h:455
Edit scenario button.
Definition: intro_widget.h:21
High level window description.
Definition: window_gui.h:168
(Toggle) Button with diff image when clicked
Definition: widget_type.h:53
Settings button.
Definition: intro_widget.h:33
GRFConfig * _grfconfig_newgame
First item in list of default GRF set up.
Switch to game intro menu.
Definition: openttd.h:32
Play heightmap button.
Definition: intro_widget.h:20
Baseset errors.
Definition: intro_widget.h:28
GUIs related to networking.
Window for configuring the AIs
Horizontal container.
Definition: widget_type.h:75
Types related to the intro widgets.
uint _missing_extra_graphics
Number of sprites provided by the fallback extra GRF, i.e. missing in the baseset.
const LanguageMetadata * _current_language
The currently loaded language.
Definition: strings.cpp:48
void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x=0, int y=0, const GRFFile *textref_stack_grffile=nullptr, uint textref_stack_size=0, const uint32 *textref_stack=nullptr)
Display an error message in a window.
Definition: error_gui.cpp:382
int GetStringHeight(const char *str, int maxw, FontSize fontsize)
Calculates height of string (in pixels).
Definition: gfx.cpp:547
void ShowAIConfigWindow()
Open the AI config window.
Definition: ai_gui.cpp:964
Functions related to world/map generation.
Stuff related to the text buffer GUI.
void CreateNestedTree(bool fill_nested=true)
Perform the first part of the initialization of a nested widget tree.
Definition: window.cpp:1812
void OnClick(Point pt, int widget, int click_count) override
A click with the left mouse button has been made on the window.
Definition: intro_gui.cpp:114
Functions, definitions and such used only by the GUI.
static const uint32 GENERATE_NEW_SEED
Create a new random seed.
Definition: genworld.h:26
bool _network_available
is network mode available?
Definition: network.cpp:56
Partial widget specification to allow NWidgets to be written nested.
Definition: widget_type.h:910
Data structure for an opened window.
Definition: window_gui.h:278
bool _ctrl_pressed
Is Ctrl pressed?
Definition: gfx.cpp:37
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
Definition: window.cpp:1828
old or new savegame
Definition: fileio_type.h:20
old or new scenario
Definition: fileio_type.h:21
static NWidgetPart SetPadding(uint8 top, uint8 right, uint8 bottom, uint8 left)
Widget part function for setting additional space around a widget.
Definition: widget_type.h:1046
static bool IsInsideMM(const T x, const size_t min, const size_t max)
Checks if a value is in an interval.
Definition: math_func.hpp:266
void StartNewGameWithoutGUI(uint32 seed)
Start a normal game without the GUI.
bool _is_network_server
Does this client wants to be a network-server?
Definition: network.cpp:58
Invisible widget that takes some space.
Definition: widget_type.h:79
Functions related to errors.
Translation selection.
Definition: intro_widget.h:29
Exit button.
Definition: intro_widget.h:37
void ShowQuery(StringID caption, StringID message, Window *parent, QueryCallbackProc *callback)
Show a modal confirmation window with standard &#39;yes&#39; and &#39;no&#39; buttons The window is aligned to the ce...
Definition: misc_gui.cpp:1262
#define FONT_HEIGHT_NORMAL
Height of characters in the normal (FS_NORMAL) font.
Definition: gfx_func.h:178
static NWidgetPart SetDataTip(uint32 data, StringID tip)
Widget part function for setting the data and tooltip.
Definition: widget_type.h:1014
Functions related to the gfx engine.
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:80
static NWidgetPart SetMinimalSize(int16 x, int16 y)
Widget part function for setting the minimal size.
Definition: widget_type.h:947
Load game button.
Definition: intro_widget.h:18
Definition of base types and functions in a cross-platform compatible way.
Center both horizontally and vertically.
Definition: gfx_func.h:106
A number of safeguards to prevent using unsafe methods.
Select tropic landscape button.
Definition: intro_widget.h:25
Normal push-button (no toggle button) with text caption.
Definition: widget_type.h:104
Geometry functions.
GameSettings _settings_newgame
Game settings for new games (updated from the intro screen).
Definition: settings.cpp:82
Simple depressed panel.
Definition: widget_type.h:50
Information about languages and their files.
void ShowGenerateLandscape()
Start with a normal game.
Translation errors.
Definition: intro_widget.h:30
Center the window.
Definition: window_gui.h:157
static NWidgetPart NWidget(WidgetType tp, Colours col, int16 idx=-1)
Widget part function for starting a new &#39;real&#39; widget.
Definition: widget_type.h:1114
Content Download button.
Definition: intro_widget.h:35
Basic functions/variables used all over the place.
Part of the network protocol handling content distribution.
void StartScenarioEditor()
Start with a scenario editor.
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
File is being loaded.
Definition: fileio_type.h:51
Display plane with zero size in both directions (none filling and resizing).
Definition: widget_type.h:390
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:18
Select toyland landscape button.
Definition: intro_widget.h:26
void ShowHighscoreTable(int difficulty=SP_CUSTOM, int8 rank=-1)
Show the highscore table for a given difficulty.
Baseset selection.
Definition: intro_widget.h:27
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition: gfx.cpp:698
Select game window; Window numbers:
Definition: window_type.h:437
Dimension maxdim(const Dimension &d1, const Dimension &d2)
Compute bounding box of both dimensions.
No window, redirects to WC_MAIN_WINDOW.
Definition: window_type.h:40
Options button.
Definition: intro_widget.h:31
uint16 missing
number of missing strings.
Definition: language.h:42
void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfig **config)
Setup the NewGRF gui.
Generate game button.
Definition: intro_widget.h:17
Declaration of functions and types defined in highscore.h and highscore_gui.h.
GUISettings gui
settings related to the GUI
Window caption (window title between closebox and stickybox)
Definition: widget_type.h:61
byte missing_strings_threshold
the number of missing strings before showing the warning
void ClearErrorMessages()
Clear all errors from the queue.
Definition: error_gui.cpp:340
Declarations for savegames operations.
Types related to the landscape.
void ShowSaveLoadDialog(AbstractFileType abstract_filetype, SaveLoadOperation fop)
Launch save/load dialog in the given mode.
Definition: fios_gui.cpp:890
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
Definition: intro_gui.cpp:52
Vertical container.
Definition: widget_type.h:77
static NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME, WWT_INSET, or WWT_PANEL).
Definition: widget_type.h:999
void DrawWidget(const Rect &r, int widget) const override
Draw the contents of a nested widget.
Definition: intro_gui.cpp:70
Play network button.
Definition: intro_widget.h:22
SwitchMode _switch_mode
The next mainloop command.
Definition: gfx.cpp:48
Select arctic landscape button.
Definition: intro_widget.h:24
NewGRF button.
Definition: intro_widget.h:34
Coordinates of a point in 2D.
heightmap file
Definition: fileio_type.h:22
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition: strings_type.h:19
void ShowGameSettings()
Open advanced settings window.
declaration of OTTD revision dependent variables
Select temperate landscape button.
Definition: intro_widget.h:23
static NWidgetPart SetFill(uint fill_x, uint fill_y)
Widget part function for setting filling.
Definition: widget_type.h:983
GameCreationSettings game_creation
settings used during the creation of a game (map)
Specification of a rectangle with absolute coordinates of all edges.
void ShowGameOptions()
Open the game options window.
void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
Update size and resize step of a widget in the window.
Definition: intro_gui.cpp:85
void SetNewLandscapeType(byte landscape)
Changes landscape type and sets genworld window dirty.
Stacked widgets, only one visible at a time (eg in a panel with tabs).
Definition: widget_type.h:80
GUI functions that shouldn&#39;t be here.
Errors (eg. saving/loading failed)
Definition: error.h:25
Dimensions (a width and height) of a rectangle in 2D.
Value of the NCB_EQUALSIZE flag.
Definition: widget_type.h:429
This file contains all sprite-related enums and defines.
void ShowNetworkContentListWindow(ContentVector *cv=nullptr, ContentType type1=CONTENT_TYPE_END, ContentType type2=CONTENT_TYPE_END)
Show the content list window with a given set of content.
int height
Height of the window (number of pixels down in y direction)
Definition: window_gui.h:322
int DrawStringMultiLine(int left, int right, int top, int bottom, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly over multiple lines.
Definition: gfx.cpp:620
Play scenario button.
Definition: intro_widget.h:19
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:201