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