# intro_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: intro_gui.cpp 18324 2009-11-28 15:01:49Z rubidium $ */
    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 "openttd.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 "window_func.h"
    00024 #include "fios.h"
    00025 #include "functions.h"
    00026 #include "ai/ai_gui.hpp"
    00027 #include "gfx_func.h"
    00028
    00029 #include "table/strings.h"
    00030 #include "table/sprites.h"
    00031
    00032 static inline void SetNewLandscapeType(byte landscape)
    00033 {
    00034   _settings_newgame.game_creation.landscape = landscape;
    00035   SetWindowClassesDirty(WC_SELECT_GAME);
    00036 }
    00037
    00038 enum SelectGameIntroWidgets {
    00039   SGI_GENERATE_GAME,
    00040   SGI_LOAD_GAME,
    00041   SGI_PLAY_SCENARIO,
    00042   SGI_PLAY_HEIGHTMAP,
    00043   SGI_EDIT_SCENARIO,
    00044   SGI_PLAY_NETWORK,
    00045   SGI_TEMPERATE_LANDSCAPE,
    00046   SGI_ARCTIC_LANDSCAPE,
    00047   SGI_TROPIC_LANDSCAPE,
    00048   SGI_TOYLAND_LANDSCAPE,
    00049   SGI_OPTIONS,
    00050   SGI_DIFFICULTIES,
    00051   SGI_SETTINGS_OPTIONS,
    00052   SGI_GRF_SETTINGS,
    00053   SGI_CONTENT_DOWNLOAD,
    00054   SGI_AI_SETTINGS,
    00055   SGI_EXIT,
    00056 };
    00057
    00058 struct SelectGameWindow : public Window {
    00059
    00060   SelectGameWindow(const WindowDesc *desc) : Window()
    00061   {
    00062     this->InitNested(desc);
    00063     this->LowerWidget(_settings_newgame.game_creation.landscape + SGI_TEMPERATE_LANDSCAPE);
    00064     this->SetLandscapeButtons();
    00065   }
    00066
    00067   void SetLandscapeButtons()
    00068   {
    00069     this->SetWidgetLoweredState(SGI_TEMPERATE_LANDSCAPE, _settings_newgame.game_creation.landscape == LT_TEMPERATE);
    00070     this->SetWidgetLoweredState(SGI_ARCTIC_LANDSCAPE,    _settings_newgame.game_creation.landscape == LT_ARCTIC);
    00071     this->SetWidgetLoweredState(SGI_TROPIC_LANDSCAPE,    _settings_newgame.game_creation.landscape == LT_TROPIC);
    00072     this->SetWidgetLoweredState(SGI_TOYLAND_LANDSCAPE,   _settings_newgame.game_creation.landscape == LT_TOYLAND);
    00073   }
    00074
    00075   virtual void OnPaint()
    00076   {
    00077     this->DrawWidgets();
    00078   }
    00079
    00080   virtual void SetStringParameters(int widget) const
    00081   {
    00082     if (widget == SGI_DIFFICULTIES) SetDParam(0, STR_DIFFICULTY_LEVEL_EASY + _settings_newgame.difficulty.diff_level);
    00083   }
    00084
    00085   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00086   {
    00087     if (widget != SGI_DIFFICULTIES) return;
    00088
    00089     Dimension textdim = {0, 0};
    00090     for (uint i = STR_DIFFICULTY_LEVEL_EASY; i <= STR_DIFFICULTY_LEVEL_CUSTOM; i++) {
    00091       SetDParam(0, i);
    00092       textdim = maxdim(textdim, GetStringBoundingBox(STR_INTRO_DIFFICULTY));
    00093     }
    00094     textdim.width += padding.width;
    00095     textdim.height += padding.height;
    00096     *size = maxdim(*size, textdim);
    00097   }
    00098
    00099   virtual void OnClick(Point pt, int widget)
    00100   {
    00101 #ifdef ENABLE_NETWORK
    00102     /* Do not create a network server when you (just) have closed one of the game
    00103      * creation/load windows for the network server. */
    00104     if (IsInsideMM(widget, SGI_GENERATE_GAME, SGI_EDIT_SCENARIO + 1)) _is_network_server = false;
    00105 #endif /* ENABLE_NETWORK */
    00106
    00107     switch (widget) {
    00108       case SGI_GENERATE_GAME:
    00109         if (_ctrl_pressed) {
    00110           StartNewGameWithoutGUI(GENERATE_NEW_SEED);
    00111         } else {
    00112           ShowGenerateLandscape();
    00113         }
    00114         break;
    00115
    00116       case SGI_LOAD_GAME:      ShowSaveLoadDialog(SLD_LOAD_GAME); break;
    00117       case SGI_PLAY_SCENARIO:  ShowSaveLoadDialog(SLD_LOAD_SCENARIO); break;
    00118       case SGI_PLAY_HEIGHTMAP: ShowSaveLoadDialog(SLD_LOAD_HEIGHTMAP); break;
    00119       case SGI_EDIT_SCENARIO:  StartScenarioEditor(); break;
    00120
    00121       case SGI_PLAY_NETWORK:
    00122         if (!_network_available) {
    00123           ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, 0, 0);
    00124         } else {
    00125           ShowNetworkGameWindow();
    00126         }
    00127         break;
    00128
    00129       case SGI_TEMPERATE_LANDSCAPE: case SGI_ARCTIC_LANDSCAPE:
    00130       case SGI_TROPIC_LANDSCAPE: case SGI_TOYLAND_LANDSCAPE:
    00131         this->RaiseWidget(_settings_newgame.game_creation.landscape + SGI_TEMPERATE_LANDSCAPE);
    00132         SetNewLandscapeType(widget - SGI_TEMPERATE_LANDSCAPE);
    00133         this->SetLandscapeButtons();
    00134         break;
    00135
    00136       case SGI_OPTIONS:         ShowGameOptions(); break;
    00137       case SGI_DIFFICULTIES:    ShowGameDifficulty(); break;
    00138       case SGI_SETTINGS_OPTIONS:ShowGameSettings(); break;
    00139       case SGI_GRF_SETTINGS:    ShowNewGRFSettings(true, true, false, &_grfconfig_newgame); break;
    00140       case SGI_CONTENT_DOWNLOAD:
    00141         if (!_network_available) {
    00142           ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, 0, 0);
    00143         } else {
    00144           ShowNetworkContentListWindow();
    00145         }
    00146         break;
    00147       case SGI_AI_SETTINGS:     ShowAIConfigWindow(); break;
    00148       case SGI_EXIT:            HandleExitGameRequest(); break;
    00149     }
    00150   }
    00151 };
    00152
    00153 static const NWidgetPart _nested_select_game_widgets[] = {
    00154   NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_INTRO_CAPTION, STR_NULL),
    00155   NWidget(WWT_PANEL, COLOUR_BROWN),
    00156   NWidget(NWID_SPACER), SetMinimalSize(0, 8),
    00157
    00158   /* 'generate game' and 'load game' buttons */
    00159   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00160     NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, SGI_GENERATE_GAME), SetMinimalSize(158, 12),
    00161               SetDataTip(STR_INTRO_NEW_GAME, STR_INTRO_TOOLTIP_NEW_GAME), SetPadding(0, 0, 0, 10), SetFill(1, 0),
    00162     NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, SGI_LOAD_GAME), SetMinimalSize(158, 12),
    00163               SetDataTip(STR_INTRO_LOAD_GAME, STR_INTRO_TOOLTIP_LOAD_GAME), SetPadding(0, 10, 0, 0), SetFill(1, 0),
    00164   EndContainer(),
    00165
    00166   NWidget(NWID_SPACER), SetMinimalSize(0, 6),
    00167
    00168   /* 'play scenario' and 'play heightmap' buttons */
    00169   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00170     NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, SGI_PLAY_SCENARIO), SetMinimalSize(158, 12),
    00171               SetDataTip(STR_INTRO_PLAY_SCENARIO, STR_INTRO_TOOLTIP_PLAY_SCENARIO), SetPadding(0, 0, 0, 10), SetFill(1, 0),
    00172     NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, SGI_PLAY_HEIGHTMAP), SetMinimalSize(158, 12),
    00173               SetDataTip(STR_INTRO_PLAY_HEIGHTMAP, STR_INTRO_TOOLTIP_PLAY_HEIGHTMAP), SetPadding(0, 10, 0, 0), SetFill(1, 0),
    00174   EndContainer(),
    00175
    00176   NWidget(NWID_SPACER), SetMinimalSize(0, 6),
    00177
    00178   /* 'edit scenario' and 'play multiplayer' buttons */
    00179   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00180     NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, SGI_EDIT_SCENARIO), SetMinimalSize(158, 12),
    00181               SetDataTip(STR_INTRO_SCENARIO_EDITOR, STR_INTRO_TOOLTIP_SCENARIO_EDITOR), SetPadding(0, 0, 0, 10), SetFill(1, 0),
    00182     NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, SGI_PLAY_NETWORK), SetMinimalSize(158, 12),
    00183               SetDataTip(STR_INTRO_MULTIPLAYER, STR_INTRO_TOOLTIP_MULTIPLAYER), SetPadding(0, 10, 0, 0), SetFill(1, 0),
    00184   EndContainer(),
    00185
    00186   NWidget(NWID_SPACER), SetMinimalSize(0, 7),
    00187
    00188   /* climate selection buttons */
    00189   NWidget(NWID_HORIZONTAL),
    00190     NWidget(NWID_SPACER), SetMinimalSize(10, 0), SetFill(1, 0),
    00191     NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, SGI_TEMPERATE_LANDSCAPE), SetMinimalSize(77, 55),
    00192               SetDataTip(SPR_SELECT_TEMPERATE, STR_INTRO_TOOLTIP_TEMPERATE),
    00193     NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0),
    00194     NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, SGI_ARCTIC_LANDSCAPE), SetMinimalSize(77, 55),
    00195               SetDataTip(SPR_SELECT_SUB_ARCTIC, STR_INTRO_TOOLTIP_SUB_ARCTIC_LANDSCAPE),
    00196     NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0),
    00197     NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, SGI_TROPIC_LANDSCAPE), SetMinimalSize(77, 55),
    00198               SetDataTip(SPR_SELECT_SUB_TROPICAL, STR_INTRO_TOOLTIP_SUB_TROPICAL_LANDSCAPE),
    00199     NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0),
    00200     NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, SGI_TOYLAND_LANDSCAPE), SetMinimalSize(77, 55),
    00201               SetDataTip(SPR_SELECT_TOYLAND, STR_INTRO_TOOLTIP_TOYLAND_LANDSCAPE),
    00202     NWidget(NWID_SPACER), SetMinimalSize(10, 0), SetFill(1, 0),
    00203   EndContainer(),
    00204
    00205   NWidget(NWID_SPACER), SetMinimalSize(0, 7),
    00206
    00207   /* 'game options' and 'difficulty options' buttons */
    00208   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00209     NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, SGI_OPTIONS), SetMinimalSize(158, 12),
    00210               SetDataTip(STR_INTRO_GAME_OPTIONS, STR_INTRO_TOOLTIP_GAME_OPTIONS), SetPadding(0, 0, 0, 10), SetFill(1, 0),
    00211     NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, SGI_DIFFICULTIES), SetMinimalSize(158, 12),
    00212               SetDataTip(STR_INTRO_DIFFICULTY, STR_INTRO_TOOLTIP_DIFFICULTY_OPTIONS), SetPadding(0, 10, 0, 0), SetFill(1, 0),
    00213   EndContainer(),
    00214
    00215   NWidget(NWID_SPACER), SetMinimalSize(0, 6),
    00216
    00217   /* 'advanced settings' and 'newgrf settings' buttons */
    00218   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00219     NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, SGI_SETTINGS_OPTIONS), SetMinimalSize(158, 12),
    00220               SetDataTip(STR_INTRO_ADVANCED_SETTINGS, STR_INTRO_TOOLTIP_ADVANCED_SETTINGS), SetPadding(0, 0, 0, 10), SetFill(1, 0),
    00221     NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, SGI_GRF_SETTINGS), SetMinimalSize(158, 12),
    00222               SetDataTip(STR_INTRO_NEWGRF_SETTINGS, STR_INTRO_TOOLTIP_NEWGRF_SETTINGS), SetPadding(0, 10, 0, 0), SetFill(1, 0),
    00223   EndContainer(),
    00224
    00225   NWidget(NWID_SPACER), SetMinimalSize(0, 6),
    00226
    00227   /* 'online content' and 'ai settings' buttons */
    00228   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00229     NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, SGI_CONTENT_DOWNLOAD), SetMinimalSize(158, 12),
    00230               SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT), SetPadding(0, 0, 0, 10), SetFill(1, 0),
    00231     NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, SGI_AI_SETTINGS), SetMinimalSize(158, 12),
    00232               SetDataTip(STR_INTRO_AI_SETTINGS, STR_INTRO_TOOLTIP_AI_SETTINGS), SetPadding(0, 10, 0, 0), SetFill(1, 0),
    00233   EndContainer(),
    00234
    00235   NWidget(NWID_SPACER), SetMinimalSize(0, 6),
    00236
    00237   /* 'exit program' button */
    00238   NWidget(NWID_HORIZONTAL),
    00239     NWidget(NWID_SPACER), SetFill(1, 0),
    00240     NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, SGI_EXIT), SetMinimalSize(128, 12),
    00241               SetDataTip(STR_INTRO_QUIT, STR_INTRO_TOOLTIP_QUIT),
    00242     NWidget(NWID_SPACER), SetFill(1, 0),
    00243   EndContainer(),
    00244
    00245   NWidget(NWID_SPACER), SetMinimalSize(0, 8),
    00246
    00247   EndContainer(),
    00248 };
    00249
    00250 static const WindowDesc _select_game_desc(
    00251   WDP_CENTER, 0, 0,
    00252   WC_SELECT_GAME, WC_NONE,
    00253   WDF_UNCLICK_BUTTONS,
    00254   _nested_select_game_widgets, lengthof(_nested_select_game_widgets)
    00255 );
    00256
    00257 void ShowSelectGameWindow()
    00258 {
    00259   new SelectGameWindow(&_select_game_desc);
    00260 }
    00261
    00262 static void AskExitGameCallback(Window *w, bool confirmed)
    00263 {
    00264   if (confirmed) _exit_game = true;
    00265 }
    00266
    00267 void AskExitGame()
    00268 {
    00269 #if defined(_WIN32)
    00270     SetDParam(0, STR_OSNAME_WINDOWS);
    00271 #elif defined(__APPLE__)
    00272     SetDParam(0, STR_OSNAME_OSX);
    00273 #elif defined(__BEOS__)
    00274     SetDParam(0, STR_OSNAME_BEOS);
    00275 #elif defined(__HAIKU__)
    00276     SetDParam(0, STR_OSNAME_HAIKU);
    00277 #elif defined(__MORPHOS__)
    00278     SetDParam(0, STR_OSNAME_MORPHOS);
    00279 #elif defined(__AMIGA__)
    00280     SetDParam(0, STR_OSNAME_AMIGAOS);
    00281 #elif defined(__OS2__)
    00282     SetDParam(0, STR_OSNAME_OS2);
    00283 #elif defined(SUNOS)
    00284     SetDParam(0, STR_OSNAME_SUNOS);
    00285 #elif defined(DOS)
    00286     SetDParam(0, STR_OSNAME_DOS);
    00287 #else
    00288     SetDParam(0, STR_OSNAME_UNIX);
    00289 #endif
    00290   ShowQuery(
    00291     STR_QUIT_CAPTION,
    00292     STR_QUIT_ARE_YOU_SURE_YOU_WANT_TO_EXIT_OPENTTD,
    00293     NULL,
    00294     AskExitGameCallback
    00295   );
    00296 }
    00297
    00298
    00299 static void AskExitToGameMenuCallback(Window *w, bool confirmed)
    00300 {
    00301   if (confirmed) _switch_mode = SM_MENU;
    00302 }
    00303
    00304 void AskExitToGameMenu()
    00305 {
    00306   ShowQuery(
    00307     STR_ABANDON_GAME_CAPTION,
    00308     (_game_mode != GM_EDITOR) ? STR_ABANDON_GAME_QUERY : STR_ABANDOM_SCENARIO_QUERY,
    00309     NULL,
    00310     AskExitToGameMenuCallback
    00311   );
    00312 }