highscore_gui.cpp

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

Generated on Wed Mar 31 22:43:23 2010 for OpenTTD by  doxygen 1.6.1