# intro_gui.cpp

Go to the documentation of this file.

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