highscore_gui.cpp

Go to the documentation of this file.
00001 /* $Id: highscore_gui.cpp 20286 2010-08-01 19:44:49Z 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 "highscore.h"
00014 #include "table/strings.h"
00015 #include "gfx_func.h"
00016 #include "table/sprites.h"
00017 #include "window_gui.h"
00018 #include "window_func.h"
00019 #include "network/network.h"
00020 #include "command_func.h"
00021 #include "company_func.h"
00022 #include "company_base.h"
00023 #include "strings_func.h"
00024 #include "openttd.h"
00025 #include "hotkeys.h"
00026 
00027 enum HighscoreWidgets {
00028   HSW_BACKGROUND,
00029 };
00030 
00031 struct EndGameHighScoreBaseWindow : Window {
00032   uint32 background_img;
00033   int8 rank;
00034 
00035   EndGameHighScoreBaseWindow(const WindowDesc *desc) : Window()
00036   {
00037     this->InitNested(desc);
00038     ResizeWindow(this, _screen.width - this->width, _screen.height - this->height);
00039   }
00040 
00041   /* Always draw a maximized window and within it the centered background */
00042   void SetupHighScoreEndWindow()
00043   {
00044     /* Resize window to "full-screen". */
00045     if (this->width != _screen.width || this->height != _screen.height) ResizeWindow(this, _screen.width - this->width, _screen.height - this->height);
00046 
00047     this->DrawWidgets();
00048 
00049     Point pt = this->GetTopLeft640x480();
00050     /* Center Highscore/Endscreen background */
00051     for (uint i = 0; i < 10; i++) { // the image is split into 10 50px high parts
00052       DrawSprite(this->background_img + i, PAL_NONE, pt.x, pt.y + (i * 50));
00053     }
00054   }
00055 
00057   Point GetTopLeft640x480()
00058   {
00059     Point pt = {max(0, (_screen.width  / 2) - (640 / 2)), max(0, (_screen.height / 2) - (480 / 2))};
00060     return pt;
00061   }
00062 
00063   virtual void OnClick(Point pt, int widget, int click_count)
00064   {
00065     delete this;
00066   }
00067 
00068   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
00069   {
00070     /* All keys are 'handled' by this window but we want to make
00071      * sure that 'quit' still works correctly. Not handling the
00072      * quit key is enough so the main toolbar can handle it. */
00073     if (IsQuitKey(keycode)) return ES_NOT_HANDLED;
00074 
00075     switch (keycode) {
00076       /* Keys for telling we want to go on */
00077       case WKC_RETURN:
00078       case WKC_ESC:
00079       case WKC_SPACE:
00080         delete this;
00081         return ES_HANDLED;
00082 
00083       default:
00084         /* We want to handle all keys; we don't want windows in
00085          * the background to open. Especially the ones that do
00086          * locate themselves based on the status-/toolbars. */
00087         return ES_HANDLED;
00088     }
00089   }
00090 };
00091 
00093 struct EndGameWindow : EndGameHighScoreBaseWindow {
00094   EndGameWindow(const WindowDesc *desc) : EndGameHighScoreBaseWindow(desc)
00095   {
00096     /* Pause in single-player to have a look at the highscore at your own leisure */
00097     if (!_networking) DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
00098 
00099     this->background_img = SPR_TYCOON_IMG1_BEGIN;
00100 
00101     if (_local_company != COMPANY_SPECTATOR) {
00102       const Company *c = Company::Get(_local_company);
00103       if (c->old_economy[0].performance_history == SCORE_MAX) {
00104         this->background_img = SPR_TYCOON_IMG2_BEGIN;
00105       }
00106     }
00107 
00108     /* In a network game show the endscores of the custom difficulty 'network' which is the last one
00109      * as well as generate a TOP5 of that game, and not an all-time top5. */
00110     if (_networking) {
00111       this->window_number = lengthof(_highscore_table) - 1;
00112       this->rank = SaveHighScoreValueNetwork();
00113     } else {
00114       /* in single player _local company is always valid */
00115       const Company *c = Company::Get(_local_company);
00116       this->window_number = _settings_game.difficulty.diff_level;
00117       this->rank = SaveHighScoreValue(c);
00118     }
00119 
00120     MarkWholeScreenDirty();
00121   }
00122 
00123   ~EndGameWindow()
00124   {
00125     if (!_networking) DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE); // unpause
00126     ShowHighscoreTable(this->window_number, this->rank);
00127   }
00128 
00129   virtual void OnPaint()
00130   {
00131     this->SetupHighScoreEndWindow();
00132     Point pt = this->GetTopLeft640x480();
00133 
00134     const Company *c = Company::GetIfValid(_local_company);
00135     if (c == NULL) return;
00136 
00137     /* We need to get performance from last year because the image is shown
00138      * at the start of the new year when these things have already been copied */
00139     if (this->background_img == SPR_TYCOON_IMG2_BEGIN) { // Tycoon of the century \o/
00140       SetDParam(0, c->index);
00141       SetDParam(1, c->index);
00142       SetDParam(2, EndGameGetPerformanceTitleFromValue(c->old_economy[0].performance_history));
00143       DrawStringMultiLine(pt.x + 15, pt.x + 640 - 25, pt.y + 90, pt.y + 160, STR_HIGHSCORE_PRESIDENT_OF_COMPANY_ACHIEVES_STATUS, TC_FROMSTRING, SA_CENTER);
00144     } else {
00145       SetDParam(0, c->index);
00146       SetDParam(1, EndGameGetPerformanceTitleFromValue(c->old_economy[0].performance_history));
00147       DrawStringMultiLine(pt.x + 36, pt.x + 640, pt.y + 140, pt.y + 206, STR_HIGHSCORE_COMPANY_ACHIEVES_STATUS, TC_FROMSTRING, SA_CENTER);
00148     }
00149   }
00150 };
00151 
00152 struct HighScoreWindow : EndGameHighScoreBaseWindow {
00153   HighScoreWindow(const WindowDesc *desc, int difficulty, int8 ranking) : EndGameHighScoreBaseWindow(desc)
00154   {
00155     /* pause game to show the chart */
00156     if (!_networking) DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
00157 
00158     /* Close all always on-top windows to get a clean screen */
00159     if (_game_mode != GM_MENU) HideVitalWindows();
00160 
00161     MarkWholeScreenDirty();
00162     this->window_number = difficulty; // show highscore chart for difficulty...
00163     this->background_img = SPR_HIGHSCORE_CHART_BEGIN; // which background to show
00164     this->rank = ranking;
00165   }
00166 
00167   ~HighScoreWindow()
00168   {
00169     if (_game_mode != GM_MENU) ShowVitalWindows();
00170 
00171     if (!_networking) DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE); // unpause
00172   }
00173 
00174   virtual void OnPaint()
00175   {
00176     const HighScore *hs = _highscore_table[this->window_number];
00177 
00178     this->SetupHighScoreEndWindow();
00179     Point pt = this->GetTopLeft640x480();
00180 
00181     SetDParam(0, ORIGINAL_END_YEAR);
00182     SetDParam(1, this->window_number + STR_DIFFICULTY_LEVEL_EASY);
00183     DrawStringMultiLine(pt.x + 70, pt.x + 570, pt.y, pt.y + 140, !_networking ? STR_HIGHSCORE_TOP_COMPANIES_WHO_REACHED : STR_HIGHSCORE_TOP_COMPANIES_NETWORK_GAME, TC_FROMSTRING, SA_CENTER);
00184 
00185     /* Draw Highscore peepz */
00186     for (uint8 i = 0; i < lengthof(_highscore_table[0]); i++) {
00187       SetDParam(0, i + 1);
00188       DrawString(pt.x + 40, pt.x + 600, pt.y + 140 + (i * 55), STR_HIGHSCORE_POSITION);
00189 
00190       if (hs[i].company[0] != '\0') {
00191         TextColour colour = (this->rank == i) ? TC_RED : TC_BLACK; // draw new highscore in red
00192 
00193         DrawString(pt.x + 71, pt.x + 569, pt.y + 140 + (i * 55), hs[i].company, colour);
00194         SetDParam(0, hs[i].title);
00195         SetDParam(1, hs[i].score);
00196         DrawString(pt.x + 71, pt.x + 569, pt.y + 140 + FONT_HEIGHT_LARGE + (i * 55), STR_HIGHSCORE_STATS, colour);
00197       }
00198     }
00199   }
00200 };
00201 
00202 static const NWidgetPart _nested_highscore_widgets[] = {
00203   NWidget(WWT_PANEL, COLOUR_END, HSW_BACKGROUND), SetMinimalSize(641, 481), SetResize(1, 1), EndContainer(),
00204 };
00205 
00206 static const WindowDesc _highscore_desc(
00207   WDP_MANUAL, 0, 0,
00208   WC_HIGHSCORE, WC_NONE,
00209   0,
00210   _nested_highscore_widgets, lengthof(_nested_highscore_widgets)
00211 );
00212 
00213 static const WindowDesc _endgame_desc(
00214   WDP_MANUAL, 0, 0,
00215   WC_ENDSCREEN, WC_NONE,
00216   0,
00217   _nested_highscore_widgets, lengthof(_nested_highscore_widgets)
00218 );
00219 
00225 void ShowHighscoreTable(int difficulty, int8 ranking)
00226 {
00227   DeleteWindowByClass(WC_HIGHSCORE);
00228   new HighScoreWindow(&_highscore_desc, difficulty, ranking);
00229 }
00230 
00235 void ShowEndGameChart()
00236 {
00237   /* Dedicated server doesn't need the highscore window and neither does -v null. */
00238   if (_network_dedicated || (!_networking && !Company::IsValidID(_local_company))) return;
00239 
00240   HideVitalWindows();
00241   DeleteWindowByClass(WC_ENDSCREEN);
00242   new EndGameWindow(&_endgame_desc);
00243 }

Generated on Fri Dec 31 17:15:31 2010 for OpenTTD by  doxygen 1.6.1