(svn r25652) -Fix: Improve text caret movement for complex scripts.
[openttd/fttd.git] / src / highscore_gui.cpp
blob9b832e3f5ed223aa21ce2ade9e9b4de85e5ec39a
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file highscore_gui.cpp Definition of the HighScore and EndGame windows */
12 #include "stdafx.h"
13 #include "highscore.h"
14 #include "table/strings.h"
15 #include "gfx_func.h"
16 #include "table/sprites.h"
17 #include "window_gui.h"
18 #include "window_func.h"
19 #include "network/network.h"
20 #include "command_func.h"
21 #include "company_func.h"
22 #include "company_base.h"
23 #include "strings_func.h"
24 #include "hotkeys.h"
26 #include "widgets/highscore_widget.h"
28 struct EndGameHighScoreBaseWindow : Window {
29 uint32 background_img;
30 int8 rank;
32 EndGameHighScoreBaseWindow(WindowDesc *desc) : Window(desc)
34 this->InitNested();
35 CLRBITS(this->flags, WF_WHITE_BORDER);
36 ResizeWindow(this, _screen.width - this->width, _screen.height - this->height);
39 /* Always draw a maximized window and within it the centered background */
40 void SetupHighScoreEndWindow()
42 /* Resize window to "full-screen". */
43 if (this->width != _screen.width || this->height != _screen.height) ResizeWindow(this, _screen.width - this->width, _screen.height - this->height);
45 this->DrawWidgets();
47 Point pt = this->GetTopLeft640x480();
48 /* Center Highscore/Endscreen background */
49 for (uint i = 0; i < 10; i++) { // the image is split into 10 50px high parts
50 DrawSprite(this->background_img + i, PAL_NONE, pt.x, pt.y + (i * 50));
54 /** Return the coordinate of the screen such that a window of 640x480 is centered at the screen. */
55 Point GetTopLeft640x480()
57 Point pt = {max(0, (_screen.width / 2) - (640 / 2)), max(0, (_screen.height / 2) - (480 / 2))};
58 return pt;
61 virtual void OnClick(Point pt, int widget, int click_count)
63 delete this;
66 virtual EventState OnKeyPress(uint16 key, uint16 keycode)
68 /* All keys are 'handled' by this window but we want to make
69 * sure that 'quit' still works correctly. Not handling the
70 * quit key is enough so the main toolbar can handle it. */
71 if (IsQuitKey(keycode)) return ES_NOT_HANDLED;
73 switch (keycode) {
74 /* Keys for telling we want to go on */
75 case WKC_RETURN:
76 case WKC_ESC:
77 case WKC_SPACE:
78 delete this;
79 return ES_HANDLED;
81 default:
82 /* We want to handle all keys; we don't want windows in
83 * the background to open. Especially the ones that do
84 * locate themselves based on the status-/toolbars. */
85 return ES_HANDLED;
90 /** End game window shown at the end of the game */
91 struct EndGameWindow : EndGameHighScoreBaseWindow {
92 EndGameWindow(WindowDesc *desc) : EndGameHighScoreBaseWindow(desc)
94 /* Pause in single-player to have a look at the highscore at your own leisure */
95 if (!_networking) DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
97 this->background_img = SPR_TYCOON_IMG1_BEGIN;
99 if (_local_company != COMPANY_SPECTATOR) {
100 const Company *c = Company::Get(_local_company);
101 if (c->old_economy[0].performance_history == SCORE_MAX) {
102 this->background_img = SPR_TYCOON_IMG2_BEGIN;
106 /* In a network game show the endscores of the custom difficulty 'network' which is
107 * a TOP5 of that game, and not an all-time TOP5. */
108 if (_networking) {
109 this->window_number = SP_MULTIPLAYER;
110 this->rank = SaveHighScoreValueNetwork();
111 } else {
112 /* in single player _local company is always valid */
113 const Company *c = Company::Get(_local_company);
114 this->window_number = SP_CUSTOM;
115 this->rank = SaveHighScoreValue(c);
118 MarkWholeScreenDirty();
121 ~EndGameWindow()
123 if (!_networking) DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE); // unpause
124 ShowHighscoreTable(this->window_number, this->rank);
127 virtual void OnPaint()
129 this->SetupHighScoreEndWindow();
130 Point pt = this->GetTopLeft640x480();
132 const Company *c = Company::GetIfValid(_local_company);
133 if (c == NULL) return;
135 /* We need to get performance from last year because the image is shown
136 * at the start of the new year when these things have already been copied */
137 if (this->background_img == SPR_TYCOON_IMG2_BEGIN) { // Tycoon of the century \o/
138 SetDParam(0, c->index);
139 SetDParam(1, c->index);
140 SetDParam(2, EndGameGetPerformanceTitleFromValue(c->old_economy[0].performance_history));
141 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);
142 } else {
143 SetDParam(0, c->index);
144 SetDParam(1, EndGameGetPerformanceTitleFromValue(c->old_economy[0].performance_history));
145 DrawStringMultiLine(pt.x + 36, pt.x + 640, pt.y + 140, pt.y + 206, STR_HIGHSCORE_COMPANY_ACHIEVES_STATUS, TC_FROMSTRING, SA_CENTER);
150 struct HighScoreWindow : EndGameHighScoreBaseWindow {
151 bool game_paused_by_player; ///< True if the game was paused by the player when the highscore window was opened.
153 HighScoreWindow(WindowDesc *desc, int difficulty, int8 ranking) : EndGameHighScoreBaseWindow(desc)
155 /* pause game to show the chart */
156 this->game_paused_by_player = _pause_mode == PM_PAUSED_NORMAL;
157 if (!_networking && !this->game_paused_by_player) DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
159 /* Close all always on-top windows to get a clean screen */
160 if (_game_mode != GM_MENU) HideVitalWindows();
162 MarkWholeScreenDirty();
163 this->window_number = difficulty; // show highscore chart for difficulty...
164 this->background_img = SPR_HIGHSCORE_CHART_BEGIN; // which background to show
165 this->rank = ranking;
168 ~HighScoreWindow()
170 if (_game_mode != GM_MENU) ShowVitalWindows();
172 if (!_networking && !this->game_paused_by_player) DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE); // unpause
175 virtual void OnPaint()
177 const HighScore *hs = _highscore_table[this->window_number];
179 this->SetupHighScoreEndWindow();
180 Point pt = this->GetTopLeft640x480();
182 SetDParam(0, ORIGINAL_END_YEAR);
183 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);
185 /* Draw Highscore peepz */
186 for (uint8 i = 0; i < lengthof(_highscore_table[0]); i++) {
187 SetDParam(0, i + 1);
188 DrawString(pt.x + 40, pt.x + 600, pt.y + 140 + (i * 55), STR_HIGHSCORE_POSITION);
190 if (hs[i].company[0] != '\0') {
191 TextColour colour = (this->rank == i) ? TC_RED : TC_BLACK; // draw new highscore in red
193 SetDParamStr(0, hs[i].company);
194 DrawString(pt.x + 71, pt.x + 569, pt.y + 140 + (i * 55), STR_JUST_BIG_RAW_STRING, colour);
195 SetDParam(0, hs[i].title);
196 SetDParam(1, hs[i].score);
197 DrawString(pt.x + 71, pt.x + 569, pt.y + 140 + FONT_HEIGHT_LARGE + (i * 55), STR_HIGHSCORE_STATS, colour);
203 static const NWidgetPart _nested_highscore_widgets[] = {
204 NWidget(WWT_PANEL, COLOUR_BROWN, WID_H_BACKGROUND), SetMinimalSize(641, 481), SetResize(1, 1), EndContainer(),
207 static WindowDesc _highscore_desc(
208 WDP_MANUAL, NULL, 0, 0,
209 WC_HIGHSCORE, WC_NONE,
211 _nested_highscore_widgets, lengthof(_nested_highscore_widgets)
214 static WindowDesc _endgame_desc(
215 WDP_MANUAL, NULL, 0, 0,
216 WC_ENDSCREEN, WC_NONE,
218 _nested_highscore_widgets, lengthof(_nested_highscore_widgets)
222 * Show the highscore table for a given difficulty. When called from
223 * endgame ranking is set to the top5 element that was newly added
224 * and is thus highlighted
226 void ShowHighscoreTable(int difficulty, int8 ranking)
228 DeleteWindowByClass(WC_HIGHSCORE);
229 new HighScoreWindow(&_highscore_desc, difficulty, ranking);
233 * Show the endgame victory screen in 2050. Update the new highscore
234 * if it was high enough
236 void ShowEndGameChart()
238 /* Dedicated server doesn't need the highscore window and neither does -v null. */
239 if (_network_dedicated || (!_networking && !Company::IsValidID(_local_company))) return;
241 HideVitalWindows();
242 DeleteWindowByClass(WC_ENDSCREEN);
243 new EndGameWindow(&_endgame_desc);