# highscore_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: highscore_gui.cpp 18690 2010-01-02 16:05:27Z 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 "highscore.h"
    00013 #include "table/strings.h"
    00014 #include "gfx_func.h"
    00015 #include "table/sprites.h"
    00016 #include "window_gui.h"
    00017 #include "window_func.h"
    00018 #include "network/network.h"
    00019 #include "command_func.h"
    00020 #include "company_func.h"
    00021 #include "company_base.h"
    00022 #include "strings_func.h"
    00023 #include "openttd.h"
    00024
    00025 enum HighscoreWidgets {
    00026   HSW_BACKGROUND,
    00027 };
    00028
    00029 struct EndGameHighScoreBaseWindow : Window {
    00030   uint32 background_img;
    00031   int8 rank;
    00032
    00033   EndGameHighScoreBaseWindow(const WindowDesc *desc) : Window()
    00034   {
    00035     this->InitNested(desc);
    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)
    00062   {
    00063     delete this;
    00064   }
    00065
    00066   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    00067   {
    00068     switch (keycode) {
    00069       /* Keys for telling we want to go on */
    00070       case WKC_RETURN:
    00071       case WKC_ESC:
    00072       case WKC_SPACE:
    00073         delete this;
    00074         return ES_HANDLED;
    00075
    00076       /* Allow CTRL-Q to work like ALT-F4 in all cases */
    00077       case 'Q' | WKC_CTRL:
    00078       case 'Q' | WKC_META:
    00079         return ES_NOT_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(const 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 the last one
    00107      * as well as generate a TOP5 of that game, and not an all-time top5. */
    00108     if (_networking) {
    00109       this->window_number = lengthof(_highscore_table) - 1;
    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 = _settings_game.difficulty.diff_level;
    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   HighScoreWindow(const WindowDesc *desc, int difficulty, int8 ranking) : EndGameHighScoreBaseWindow(desc)
    00152   {
    00153     /* pause game to show the chart */
    00154     if (!_networking) DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
    00155
    00156     /* Close all always on-top windows to get a clean screen */
    00157     if (_game_mode != GM_MENU) HideVitalWindows();
    00158
    00159     MarkWholeScreenDirty();
    00160     this->window_number = difficulty; // show highscore chart for difficulty...
    00161     this->background_img = SPR_HIGHSCORE_CHART_BEGIN; // which background to show
    00162     this->rank = ranking;
    00163   }
    00164
    00165   ~HighScoreWindow()
    00166   {
    00167     if (_game_mode != GM_MENU) ShowVitalWindows();
    00168
    00169     if (!_networking) DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE); // unpause
    00170   }
    00171
    00172   virtual void OnPaint()
    00173   {
    00174     const HighScore *hs = _highscore_table[this->window_number];
    00175
    00176     this->SetupHighScoreEndWindow();
    00177     Point pt = this->GetTopLeft640x480();
    00178
    00179     SetDParam(0, ORIGINAL_END_YEAR);
    00180     SetDParam(1, this->window_number + STR_DIFFICULTY_LEVEL_EASY);
    00181     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);
    00182
    00183     /* Draw Highscore peepz */
    00184     for (uint8 i = 0; i < lengthof(_highscore_table[0]); i++) {
    00185       SetDParam(0, i + 1);
    00186       DrawString(pt.x + 40, pt.x + 600, pt.y + 140 + (i * 55), STR_HIGHSCORE_POSITION);
    00187
    00188       if (hs[i].company[0] != '\0') {
    00189         TextColour colour = (this->rank == i) ? TC_RED : TC_BLACK; // draw new highscore in red
    00190
    00191         DrawString(pt.x + 71, pt.x + 569, pt.y + 140 + (i * 55), hs[i].company, colour);
    00192         SetDParam(0, hs[i].title);
    00193         SetDParam(1, hs[i].score);
    00194         DrawString(pt.x + 71, pt.x + 569, pt.y + 140 + FONT_HEIGHT_LARGE + (i * 55), STR_HIGHSCORE_STATS, colour);
    00195       }
    00196     }
    00197   }
    00198 };
    00199
    00200 static const NWidgetPart _nested_highscore_widgets[] = {
    00201   NWidget(WWT_PANEL, COLOUR_END, HSW_BACKGROUND), SetMinimalSize(641, 481), SetResize(1, 1), EndContainer(),
    00202 };
    00203
    00204 static const WindowDesc _highscore_desc(
    00205   WDP_MANUAL, 0, 0,
    00206   WC_HIGHSCORE, WC_NONE,
    00207   0,
    00208   _nested_highscore_widgets, lengthof(_nested_highscore_widgets)
    00209 );
    00210
    00211 static const WindowDesc _endgame_desc(
    00212   WDP_MANUAL, 0, 0,
    00213   WC_ENDSCREEN, WC_NONE,
    00214   0,
    00215   _nested_highscore_widgets, lengthof(_nested_highscore_widgets)
    00216 );
    00217
    00221 void ShowHighscoreTable(int difficulty, int8 ranking)
    00222 {
    00223   DeleteWindowByClass(WC_HIGHSCORE);
    00224   new HighScoreWindow(&_highscore_desc, difficulty, ranking);
    00225 }
    00226
    00229 void ShowEndGameChart()
    00230 {
    00231   /* Dedicated server doesn't need the highscore window and neither does -v null. */
    00232   if (_network_dedicated || (!_networking && !Company::IsValidID(_local_company))) return;
    00233
    00234   HideVitalWindows();
    00235   DeleteWindowByClass(WC_ENDSCREEN);
    00236   new EndGameWindow(&_endgame_desc);
    00237 }