highscore_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 "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 "hotkeys.h"
00025 
00026 #include "widgets/highscore_widget.h"
00027 
00028 struct EndGameHighScoreBaseWindow : Window {
00029   uint32 background_img;
00030   int8 rank;
00031 
00032   EndGameHighScoreBaseWindow(WindowDesc *desc) : Window(desc)
00033   {
00034     this->InitNested();
00035     CLRBITS(this->flags, WF_WHITE_BORDER);
00036     ResizeWindow(this, _screen.width - this->width, _screen.height - this->height);
00037   }
00038 
00039   /* Always draw a maximized window and within it the centered background */
00040   void SetupHighScoreEndWindow()
00041   {
00042     /* Resize window to "full-screen". */
00043     if (this->width != _screen.width || this->height != _screen.height) ResizeWindow(this, _screen.width - this->width, _screen.height - this->height);
00044 
00045     this->DrawWidgets();
00046 
00047     Point pt = this->GetTopLeft640x480();
00048     /* Center Highscore/Endscreen background */
00049     for (uint i = 0; i < 10; i++) { // the image is split into 10 50px high parts
00050       DrawSprite(this->background_img + i, PAL_NONE, pt.x, pt.y + (i * 50));
00051     }
00052   }
00053 
00055   Point GetTopLeft640x480()
00056   {
00057     Point pt = {max(0, (_screen.width  / 2) - (640 / 2)), max(0, (_screen.height / 2) - (480 / 2))};
00058     return pt;
00059   }
00060 
00061   virtual void OnClick(Point pt, int widget, int click_count)
00062   {
00063     delete this;
00064   }
00065 
00066   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
00067   {
00068     /* All keys are 'handled' by this window but we want to make
00069      * sure that 'quit' still works correctly. Not handling the
00070      * quit key is enough so the main toolbar can handle it. */
00071     if (IsQuitKey(keycode)) return ES_NOT_HANDLED;
00072 
00073     switch (keycode) {
00074       /* Keys for telling we want to go on */
00075       case WKC_RETURN:
00076       case WKC_ESC:
00077       case WKC_SPACE:
00078         delete this;
00079         return ES_HANDLED;
00080 
00081       default:
00082         /* We want to handle all keys; we don't want windows in
00083          * the background to open. Especially the ones that do
00084          * locate themselves based on the status-/toolbars. */
00085         return ES_HANDLED;
00086     }
00087   }
00088 };
00089 
00091 struct EndGameWindow : EndGameHighScoreBaseWindow {
00092   EndGameWindow(WindowDesc *desc) : EndGameHighScoreBaseWindow(desc)
00093   {
00094     /* Pause in single-player to have a look at the highscore at your own leisure */
00095     if (!_networking) DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
00096 
00097     this->background_img = SPR_TYCOON_IMG1_BEGIN;
00098 
00099     if (_local_company != COMPANY_SPECTATOR) {
00100       const Company *c = Company::Get(_local_company);
00101       if (c->old_economy[0].performance_history == SCORE_MAX) {
00102         this->background_img = SPR_TYCOON_IMG2_BEGIN;
00103       }
00104     }
00105 
00106     /* In a network game show the endscores of the custom difficulty 'network' which is
00107      * a TOP5 of that game, and not an all-time TOP5. */
00108     if (_networking) {
00109       this->window_number = SP_MULTIPLAYER;
00110       this->rank = SaveHighScoreValueNetwork();
00111     } else {
00112       /* in single player _local company is always valid */
00113       const Company *c = Company::Get(_local_company);
00114       this->window_number = SP_CUSTOM;
00115       this->rank = SaveHighScoreValue(c);
00116     }
00117 
00118     MarkWholeScreenDirty();
00119   }
00120 
00121   ~EndGameWindow()
00122   {
00123     if (!_networking) DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE); // unpause
00124     ShowHighscoreTable(this->window_number, this->rank);
00125   }
00126 
00127   virtual void OnPaint()
00128   {
00129     this->SetupHighScoreEndWindow();
00130     Point pt = this->GetTopLeft640x480();
00131 
00132     const Company *c = Company::GetIfValid(_local_company);
00133     if (c == NULL) return;
00134 
00135     /* We need to get performance from last year because the image is shown
00136      * at the start of the new year when these things have already been copied */
00137     if (this->background_img == SPR_TYCOON_IMG2_BEGIN) { // Tycoon of the century \o/
00138       SetDParam(0, c->index);
00139       SetDParam(1, c->index);
00140       SetDParam(2, EndGameGetPerformanceTitleFromValue(c->old_economy[0].performance_history));
00141       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);
00142     } else {
00143       SetDParam(0, c->index);
00144       SetDParam(1, EndGameGetPerformanceTitleFromValue(c->old_economy[0].performance_history));
00145       DrawStringMultiLine(pt.x + 36, pt.x + 640, pt.y + 140, pt.y + 206, STR_HIGHSCORE_COMPANY_ACHIEVES_STATUS, TC_FROMSTRING, SA_CENTER);
00146     }
00147   }
00148 };
00149 
00150 struct HighScoreWindow : EndGameHighScoreBaseWindow {
00151   bool game_paused_by_player; 
00152 
00153   HighScoreWindow(WindowDesc *desc, int difficulty, int8 ranking) : EndGameHighScoreBaseWindow(desc)
00154   {
00155     /* pause game to show the chart */
00156     this->game_paused_by_player = _pause_mode == PM_PAUSED_NORMAL;
00157     if (!_networking && !this->game_paused_by_player) DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
00158 
00159     /* Close all always on-top windows to get a clean screen */
00160     if (_game_mode != GM_MENU) HideVitalWindows();
00161 
00162     MarkWholeScreenDirty();
00163     this->window_number = difficulty; // show highscore chart for difficulty...
00164     this->background_img = SPR_HIGHSCORE_CHART_BEGIN; // which background to show
00165     this->rank = ranking;
00166   }
00167 
00168   ~HighScoreWindow()
00169   {
00170     if (_game_mode != GM_MENU) ShowVitalWindows();
00171 
00172     if (!_networking && !this->game_paused_by_player) DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE); // unpause
00173   }
00174 
00175   virtual void OnPaint()
00176   {
00177     const HighScore *hs = _highscore_table[this->window_number];
00178 
00179     this->SetupHighScoreEndWindow();
00180     Point pt = this->GetTopLeft640x480();
00181 
00182     SetDParam(0, ORIGINAL_END_YEAR);
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_BROWN, WID_H_BACKGROUND), SetMinimalSize(641, 481), SetResize(1, 1), EndContainer(),
00204 };
00205 
00206 static WindowDesc _highscore_desc(
00207   WDP_MANUAL, NULL, 0, 0,
00208   WC_HIGHSCORE, WC_NONE,
00209   0,
00210   _nested_highscore_widgets, lengthof(_nested_highscore_widgets)
00211 );
00212 
00213 static WindowDesc _endgame_desc(
00214   WDP_MANUAL, NULL, 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 }