# intro_gui.cpp

Go to the documentation of this file.

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