intro_gui.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "error.h"
00014 #include "gui.h"
00015 #include "window_gui.h"
00016 #include "textbuf_gui.h"
00017 #include "network/network.h"
00018 #include "genworld.h"
00019 #include "network/network_gui.h"
00020 #include "network/network_content.h"
00021 #include "landscape_type.h"
00022 #include "strings_func.h"
00023 #include "fios.h"
00024 #include "ai/ai_gui.hpp"
00025 #include "gfx_func.h"
00026 #include "core/geometry_func.hpp"
00027 #include "language.h"
00028 #include "rev.h"
00029 
00030 #include "widgets/intro_widget.h"
00031 
00032 #include "table/strings.h"
00033 #include "table/sprites.h"
00034 
00035 struct SelectGameWindow : public Window {
00036 
00037   SelectGameWindow(const WindowDesc *desc) : Window()
00038   {
00039     this->CreateNestedTree(desc);
00040     this->FinishInitNested(desc, 0);
00041     this->OnInvalidateData();
00042   }
00043 
00049   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00050   {
00051     if (!gui_scope) return;
00052     this->SetWidgetLoweredState(WID_SGI_TEMPERATE_LANDSCAPE, _settings_newgame.game_creation.landscape == LT_TEMPERATE);
00053     this->SetWidgetLoweredState(WID_SGI_ARCTIC_LANDSCAPE,    _settings_newgame.game_creation.landscape == LT_ARCTIC);
00054     this->SetWidgetLoweredState(WID_SGI_TROPIC_LANDSCAPE,    _settings_newgame.game_creation.landscape == LT_TROPIC);
00055     this->SetWidgetLoweredState(WID_SGI_TOYLAND_LANDSCAPE,   _settings_newgame.game_creation.landscape == LT_TOYLAND);
00056   }
00057 
00058   virtual void SetStringParameters(int widget) const
00059   {
00060     if (widget == WID_SGI_DIFFICULTIES) SetDParam(0, STR_DIFFICULTY_LEVEL_EASY + _settings_newgame.difficulty.diff_level);
00061   }
00062 
00063   virtual void OnInit()
00064   {
00065     bool missing = _current_language->missing >= _settings_client.gui.missing_strings_threshold && !IsReleasedVersion();
00066     this->GetWidget<NWidgetStacked>(WID_SGI_TRANSLATION_SELECTION)->SetDisplayedPlane(missing ? 0 : SZSP_NONE);
00067   }
00068 
00069   virtual void DrawWidget(const Rect &r, int widget) const
00070   {
00071     switch (widget) {
00072       case WID_SGI_TRANSLATION:
00073         SetDParam(0, _current_language->missing);
00074         DrawStringMultiLine(r.left, r.right, r.top,  r.bottom, STR_INTRO_TRANSLATION, TC_FROMSTRING, SA_CENTER);
00075         break;
00076     }
00077   }
00078 
00079   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00080   {
00081     switch (widget) {
00082       case WID_SGI_DIFFICULTIES: {
00083         Dimension textdim = {0, 0};
00084         for (uint i = STR_DIFFICULTY_LEVEL_EASY; i <= STR_DIFFICULTY_LEVEL_CUSTOM; i++) {
00085           SetDParam(0, i);
00086           textdim = maxdim(textdim, GetStringBoundingBox(STR_INTRO_DIFFICULTY));
00087         }
00088         textdim.width += padding.width;
00089         textdim.height += padding.height;
00090         *size = maxdim(*size, textdim);
00091         break;
00092       }
00093 
00094       case WID_SGI_TRANSLATION: {
00095         SetDParam(0, _current_language->missing);
00096         int height = GetStringHeight(STR_INTRO_TRANSLATION, size->width);
00097         if (height > 3 * FONT_HEIGHT_NORMAL) {
00098           /* Don't let the window become too high. */
00099           Dimension textdim = GetStringBoundingBox(STR_INTRO_TRANSLATION);
00100           textdim.height *= 3;
00101           textdim.width -= textdim.width / 2;
00102           *size = maxdim(*size, textdim);
00103         } else {
00104           size->height = height + padding.height;
00105         }
00106         break;
00107       }
00108     }
00109   }
00110 
00111   virtual void OnClick(Point pt, int widget, int click_count)
00112   {
00113 #ifdef ENABLE_NETWORK
00114     /* Do not create a network server when you (just) have closed one of the game
00115      * creation/load windows for the network server. */
00116     if (IsInsideMM(widget, WID_SGI_GENERATE_GAME, WID_SGI_EDIT_SCENARIO + 1)) _is_network_server = false;
00117 #endif /* ENABLE_NETWORK */
00118 
00119     switch (widget) {
00120       case WID_SGI_GENERATE_GAME:
00121         if (_ctrl_pressed) {
00122           StartNewGameWithoutGUI(GENERATE_NEW_SEED);
00123         } else {
00124           ShowGenerateLandscape();
00125         }
00126         break;
00127 
00128       case WID_SGI_LOAD_GAME:      ShowSaveLoadDialog(SLD_LOAD_GAME); break;
00129       case WID_SGI_PLAY_SCENARIO:  ShowSaveLoadDialog(SLD_LOAD_SCENARIO); break;
00130       case WID_SGI_PLAY_HEIGHTMAP: ShowSaveLoadDialog(SLD_LOAD_HEIGHTMAP); break;
00131       case WID_SGI_EDIT_SCENARIO:  StartScenarioEditor(); break;
00132 
00133       case WID_SGI_PLAY_NETWORK:
00134         if (!_network_available) {
00135           ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
00136         } else {
00137           ShowNetworkGameWindow();
00138         }
00139         break;
00140 
00141       case WID_SGI_TEMPERATE_LANDSCAPE: case WID_SGI_ARCTIC_LANDSCAPE:
00142       case WID_SGI_TROPIC_LANDSCAPE: case WID_SGI_TOYLAND_LANDSCAPE:
00143         SetNewLandscapeType(widget - WID_SGI_TEMPERATE_LANDSCAPE);
00144         break;
00145 
00146       case WID_SGI_OPTIONS:         ShowGameOptions(); break;
00147       case WID_SGI_DIFFICULTIES:    ShowGameDifficulty(); break;
00148       case WID_SGI_SETTINGS_OPTIONS:ShowGameSettings(); break;
00149       case WID_SGI_GRF_SETTINGS:    ShowNewGRFSettings(true, true, false, &_grfconfig_newgame); break;
00150       case WID_SGI_CONTENT_DOWNLOAD:
00151         if (!_network_available) {
00152           ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
00153         } else {
00154           ShowNetworkContentListWindow();
00155         }
00156         break;
00157       case WID_SGI_AI_SETTINGS:     ShowAIConfigWindow(); break;
00158       case WID_SGI_EXIT:            HandleExitGameRequest(); break;
00159     }
00160   }
00161 };
00162 
00163 static const NWidgetPart _nested_select_game_widgets[] = {
00164   NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_INTRO_CAPTION, STR_NULL),
00165   NWidget(WWT_PANEL, COLOUR_BROWN),
00166   NWidget(NWID_SPACER), SetMinimalSize(0, 8),
00167 
00168   /* 'generate game' and 'load game' buttons */
00169   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00170     NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_GENERATE_GAME), SetMinimalSize(158, 12),
00171               SetDataTip(STR_INTRO_NEW_GAME, STR_INTRO_TOOLTIP_NEW_GAME), SetPadding(0, 0, 0, 10), SetFill(1, 0),
00172     NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_LOAD_GAME), SetMinimalSize(158, 12),
00173               SetDataTip(STR_INTRO_LOAD_GAME, STR_INTRO_TOOLTIP_LOAD_GAME), SetPadding(0, 10, 0, 0), SetFill(1, 0),
00174   EndContainer(),
00175 
00176   NWidget(NWID_SPACER), SetMinimalSize(0, 6),
00177 
00178   /* 'play scenario' and 'play heightmap' buttons */
00179   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00180     NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_PLAY_SCENARIO), SetMinimalSize(158, 12),
00181               SetDataTip(STR_INTRO_PLAY_SCENARIO, STR_INTRO_TOOLTIP_PLAY_SCENARIO), SetPadding(0, 0, 0, 10), SetFill(1, 0),
00182     NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_PLAY_HEIGHTMAP), SetMinimalSize(158, 12),
00183               SetDataTip(STR_INTRO_PLAY_HEIGHTMAP, STR_INTRO_TOOLTIP_PLAY_HEIGHTMAP), SetPadding(0, 10, 0, 0), SetFill(1, 0),
00184   EndContainer(),
00185 
00186   NWidget(NWID_SPACER), SetMinimalSize(0, 6),
00187 
00188   /* 'edit scenario' and 'play multiplayer' buttons */
00189   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00190     NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_EDIT_SCENARIO), SetMinimalSize(158, 12),
00191               SetDataTip(STR_INTRO_SCENARIO_EDITOR, STR_INTRO_TOOLTIP_SCENARIO_EDITOR), SetPadding(0, 0, 0, 10), SetFill(1, 0),
00192     NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_PLAY_NETWORK), SetMinimalSize(158, 12),
00193               SetDataTip(STR_INTRO_MULTIPLAYER, STR_INTRO_TOOLTIP_MULTIPLAYER), SetPadding(0, 10, 0, 0), SetFill(1, 0),
00194   EndContainer(),
00195 
00196   NWidget(NWID_SPACER), SetMinimalSize(0, 7),
00197 
00198   /* climate selection buttons */
00199   NWidget(NWID_HORIZONTAL),
00200     NWidget(NWID_SPACER), SetMinimalSize(10, 0), SetFill(1, 0),
00201     NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_SGI_TEMPERATE_LANDSCAPE), SetMinimalSize(77, 55),
00202               SetDataTip(SPR_SELECT_TEMPERATE, STR_INTRO_TOOLTIP_TEMPERATE),
00203     NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0),
00204     NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_SGI_ARCTIC_LANDSCAPE), SetMinimalSize(77, 55),
00205               SetDataTip(SPR_SELECT_SUB_ARCTIC, STR_INTRO_TOOLTIP_SUB_ARCTIC_LANDSCAPE),
00206     NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0),
00207     NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_SGI_TROPIC_LANDSCAPE), SetMinimalSize(77, 55),
00208               SetDataTip(SPR_SELECT_SUB_TROPICAL, STR_INTRO_TOOLTIP_SUB_TROPICAL_LANDSCAPE),
00209     NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0),
00210     NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_SGI_TOYLAND_LANDSCAPE), SetMinimalSize(77, 55),
00211               SetDataTip(SPR_SELECT_TOYLAND, STR_INTRO_TOOLTIP_TOYLAND_LANDSCAPE),
00212     NWidget(NWID_SPACER), SetMinimalSize(10, 0), SetFill(1, 0),
00213   EndContainer(),
00214 
00215   NWidget(NWID_SPACER), SetMinimalSize(0, 7),
00216   NWidget(NWID_SELECTION, INVALID_COLOUR, WID_SGI_TRANSLATION_SELECTION),
00217     NWidget(NWID_VERTICAL),
00218       NWidget(WWT_EMPTY, COLOUR_ORANGE, WID_SGI_TRANSLATION), SetMinimalSize(316, 12), SetFill(1, 0), SetPadding(0, 10, 7, 10),
00219     EndContainer(),
00220   EndContainer(),
00221 
00222   /* 'game options' and 'difficulty options' buttons */
00223   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00224     NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_OPTIONS), SetMinimalSize(158, 12),
00225               SetDataTip(STR_INTRO_GAME_OPTIONS, STR_INTRO_TOOLTIP_GAME_OPTIONS), SetPadding(0, 0, 0, 10), SetFill(1, 0),
00226     NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_DIFFICULTIES), SetMinimalSize(158, 12),
00227               SetDataTip(STR_INTRO_DIFFICULTY, STR_INTRO_TOOLTIP_DIFFICULTY_OPTIONS), SetPadding(0, 10, 0, 0), SetFill(1, 0),
00228   EndContainer(),
00229 
00230   NWidget(NWID_SPACER), SetMinimalSize(0, 6),
00231 
00232   /* 'advanced settings' and 'newgrf settings' buttons */
00233   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00234     NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_SETTINGS_OPTIONS), SetMinimalSize(158, 12),
00235               SetDataTip(STR_INTRO_ADVANCED_SETTINGS, STR_INTRO_TOOLTIP_ADVANCED_SETTINGS), SetPadding(0, 0, 0, 10), SetFill(1, 0),
00236     NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_GRF_SETTINGS), SetMinimalSize(158, 12),
00237               SetDataTip(STR_INTRO_NEWGRF_SETTINGS, STR_INTRO_TOOLTIP_NEWGRF_SETTINGS), SetPadding(0, 10, 0, 0), SetFill(1, 0),
00238   EndContainer(),
00239 
00240   NWidget(NWID_SPACER), SetMinimalSize(0, 6),
00241 
00242   /* 'online content' and 'ai settings' buttons */
00243   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00244     NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_CONTENT_DOWNLOAD), SetMinimalSize(158, 12),
00245               SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT), SetPadding(0, 0, 0, 10), SetFill(1, 0),
00246     NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_AI_SETTINGS), SetMinimalSize(158, 12),
00247               SetDataTip(STR_INTRO_SCRIPT_SETTINGS, STR_INTRO_TOOLTIP_SCRIPT_SETTINGS), SetPadding(0, 10, 0, 0), SetFill(1, 0),
00248   EndContainer(),
00249 
00250   NWidget(NWID_SPACER), SetMinimalSize(0, 6),
00251 
00252   /* 'exit program' button */
00253   NWidget(NWID_HORIZONTAL),
00254     NWidget(NWID_SPACER), SetFill(1, 0),
00255     NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_SGI_EXIT), SetMinimalSize(128, 12),
00256               SetDataTip(STR_INTRO_QUIT, STR_INTRO_TOOLTIP_QUIT),
00257     NWidget(NWID_SPACER), SetFill(1, 0),
00258   EndContainer(),
00259 
00260   NWidget(NWID_SPACER), SetMinimalSize(0, 8),
00261 
00262   EndContainer(),
00263 };
00264 
00265 static const WindowDesc _select_game_desc(
00266   WDP_CENTER, 0, 0,
00267   WC_SELECT_GAME, WC_NONE,
00268   WDF_UNCLICK_BUTTONS,
00269   _nested_select_game_widgets, lengthof(_nested_select_game_widgets)
00270 );
00271 
00272 void ShowSelectGameWindow()
00273 {
00274   new SelectGameWindow(&_select_game_desc);
00275 }
00276 
00277 static void AskExitGameCallback(Window *w, bool confirmed)
00278 {
00279   if (confirmed) _exit_game = true;
00280 }
00281 
00282 void AskExitGame()
00283 {
00284 #if defined(_WIN32)
00285     SetDParam(0, STR_OSNAME_WINDOWS);
00286 #elif defined(__APPLE__)
00287     SetDParam(0, STR_OSNAME_OSX);
00288 #elif defined(__BEOS__)
00289     SetDParam(0, STR_OSNAME_BEOS);
00290 #elif defined(__HAIKU__)
00291     SetDParam(0, STR_OSNAME_HAIKU);
00292 #elif defined(__MORPHOS__)
00293     SetDParam(0, STR_OSNAME_MORPHOS);
00294 #elif defined(__AMIGA__)
00295     SetDParam(0, STR_OSNAME_AMIGAOS);
00296 #elif defined(__OS2__)
00297     SetDParam(0, STR_OSNAME_OS2);
00298 #elif defined(SUNOS)
00299     SetDParam(0, STR_OSNAME_SUNOS);
00300 #elif defined(DOS)
00301     SetDParam(0, STR_OSNAME_DOS);
00302 #else
00303     SetDParam(0, STR_OSNAME_UNIX);
00304 #endif
00305   ShowQuery(
00306     STR_QUIT_CAPTION,
00307     STR_QUIT_ARE_YOU_SURE_YOU_WANT_TO_EXIT_OPENTTD,
00308     NULL,
00309     AskExitGameCallback
00310   );
00311 }
00312 
00313 
00314 static void AskExitToGameMenuCallback(Window *w, bool confirmed)
00315 {
00316   if (confirmed) {
00317     _switch_mode = SM_MENU;
00318     ClearErrorMessages();
00319   }
00320 }
00321 
00322 void AskExitToGameMenu()
00323 {
00324   ShowQuery(
00325     STR_ABANDON_GAME_CAPTION,
00326     (_game_mode != GM_EDITOR) ? STR_ABANDON_GAME_QUERY : STR_ABANDON_SCENARIO_QUERY,
00327     NULL,
00328     AskExitToGameMenuCallback
00329   );
00330 }