Update readme.md
[openttd-joker.git] / src / misc_gui.cpp
bloba2fdd5225d30134f4beb9104895b27043aa27b38
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 misc_gui.cpp GUIs for a number of misc windows. */
12 #include "stdafx.h"
13 #include "debug.h"
14 #include "landscape.h"
15 #include "error.h"
16 #include "gui.h"
17 #include <utility>
18 #include "command_func.h"
19 #include "company_func.h"
20 #include "town.h"
21 #include "string_func.h"
22 #include "company_base.h"
23 #include "texteff.hpp"
24 #include "strings_func.h"
25 #include "window_func.h"
26 #include "querystring_gui.h"
27 #include "core/geometry_func.hpp"
28 #include "newgrf_debug.h"
29 #include "newgrf_cargo.h"
30 #include "zoom_func.h"
31 #include "industry.h"
32 #include "station_base.h"
33 #include "waypoint_base.h"
34 #include "tunnelbridge_map.h"
36 #include "widgets/misc_widget.h"
38 #include "table/strings.h"
40 #include "safeguards.h"
42 /** Method to open the OSK. */
43 enum OskActivation {
44 OSKA_DISABLED, ///< The OSK shall not be activated at all.
45 OSKA_DOUBLE_CLICK, ///< Double click on the edit box opens OSK.
46 OSKA_SINGLE_CLICK, ///< Single click after focus click opens OSK.
47 OSKA_IMMEDIATELY, ///< Focusing click already opens OSK.
51 static const NWidgetPart _nested_land_info_widgets[] = {
52 NWidget(NWID_HORIZONTAL),
53 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
54 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_LAND_AREA_INFORMATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
55 NWidget(WWT_DEBUGBOX, COLOUR_GREY),
56 EndContainer(),
57 NWidget(WWT_PANEL, COLOUR_GREY, WID_LI_BACKGROUND), EndContainer(),
60 static WindowDesc _land_info_desc(
61 WDP_AUTO, "land_info", 0, 0,
62 WC_LAND_INFO, WC_NONE,
64 _nested_land_info_widgets, lengthof(_nested_land_info_widgets)
67 class LandInfoWindow : public Window {
68 enum LandInfoLines {
69 LAND_INFO_CENTERED_LINES = 32, ///< Up to 32 centered lines (arbitrary limit)
70 LAND_INFO_MULTICENTER_LINE = LAND_INFO_CENTERED_LINES, ///< One multicenter line
71 LAND_INFO_LINE_END,
74 static const uint LAND_INFO_LINE_BUFF_SIZE = 512;
76 public:
77 char landinfo_data[LAND_INFO_LINE_END][LAND_INFO_LINE_BUFF_SIZE];
78 TileIndex tile;
80 virtual void DrawWidget(const Rect &r, int widget) const
82 if (widget != WID_LI_BACKGROUND) return;
84 GfxDrawLine(r.left, r.top, r.right, r.top, PC_BLACK);
85 GfxDrawLine(r.left, r.bottom, r.right, r.bottom, PC_BLACK);
86 GfxDrawLine(r.left, r.top, r.left, r.bottom, PC_BLACK);
87 GfxDrawLine(r.right, r.top, r.right, r.bottom, PC_BLACK);
89 uint y = r.top + WD_TEXTPANEL_TOP;
90 for (uint i = 0; i < LAND_INFO_CENTERED_LINES; i++) {
91 if (StrEmpty(this->landinfo_data[i])) break;
93 DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, this->landinfo_data[i], i == 0 ? TC_LIGHT_BLUE : TC_FROMSTRING, SA_HOR_CENTER);
94 y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
95 if (i == 0) y += 4;
98 if (!StrEmpty(this->landinfo_data[LAND_INFO_MULTICENTER_LINE])) {
99 SetDParamStr(0, this->landinfo_data[LAND_INFO_MULTICENTER_LINE]);
100 DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, r.bottom - WD_TEXTPANEL_BOTTOM, STR_JUST_RAW_STRING, TC_FROMSTRING, SA_CENTER);
104 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
106 if (widget != WID_LI_BACKGROUND) return;
108 size->height = WD_TEXTPANEL_TOP + WD_TEXTPANEL_BOTTOM;
109 for (uint i = 0; i < LAND_INFO_CENTERED_LINES; i++) {
110 if (StrEmpty(this->landinfo_data[i])) break;
112 uint width = GetStringBoundingBox(this->landinfo_data[i]).width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
113 size->width = max(size->width, width);
115 size->height += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
116 if (i == 0) size->height += 4;
119 if (!StrEmpty(this->landinfo_data[LAND_INFO_MULTICENTER_LINE])) {
120 uint width = GetStringBoundingBox(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]).width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
121 size->width = max(size->width, min(300u, width));
122 SetDParamStr(0, this->landinfo_data[LAND_INFO_MULTICENTER_LINE]);
123 size->height += GetStringHeight(STR_JUST_RAW_STRING, size->width - WD_FRAMETEXT_LEFT - WD_FRAMETEXT_RIGHT);
127 LandInfoWindow(TileIndex tile) : Window(&_land_info_desc), tile(tile)
129 this->InitNested();
131 #if defined(_DEBUG)
132 # define LANDINFOD_LEVEL 0
133 #else
134 # define LANDINFOD_LEVEL 1
135 #endif
136 DEBUG(misc, LANDINFOD_LEVEL, "TILE: %#x (%i,%i)", tile, TileX(tile), TileY(tile));
137 if(IsTunnelTile(tile)) {
138 DEBUG(misc, LANDINFOD_LEVEL, "tunnel pool size: %u", (uint)Tunnel::GetPoolSize());
139 DEBUG(misc, LANDINFOD_LEVEL, "index: %#x" , Tunnel::GetByTile(tile)->index);
140 DEBUG(misc, LANDINFOD_LEVEL, "north tile: %#x" , Tunnel::GetByTile(tile)->tile_n);
141 DEBUG(misc, LANDINFOD_LEVEL, "south tile: %#x" , Tunnel::GetByTile(tile)->tile_s);
142 DEBUG(misc, LANDINFOD_LEVEL, "is chunnel: %u" , Tunnel::GetByTile(tile)->is_chunnel);
144 DEBUG(misc, LANDINFOD_LEVEL, "type = %#x", _m[tile].type);
145 DEBUG(misc, LANDINFOD_LEVEL, "height = %#x", _m[tile].height);
146 DEBUG(misc, LANDINFOD_LEVEL, "m1 = %#x", _m[tile].m1);
147 DEBUG(misc, LANDINFOD_LEVEL, "m2 = %#x", _m[tile].m2);
148 DEBUG(misc, LANDINFOD_LEVEL, "m3 = %#x", _m[tile].m3);
149 DEBUG(misc, LANDINFOD_LEVEL, "m4 = %#x", _m[tile].m4);
150 DEBUG(misc, LANDINFOD_LEVEL, "m5 = %#x", _m[tile].m5);
151 DEBUG(misc, LANDINFOD_LEVEL, "m6 = %#x", _me[tile].m6);
152 DEBUG(misc, LANDINFOD_LEVEL, "m7 = %#x", _me[tile].m7);
153 #undef LANDINFOD_LEVEL
156 virtual void OnInit()
158 Town *t = ClosestTownFromTile(tile, _settings_game.economy.dist_local_authority);
160 /* Because build_date is not set yet in every TileDesc, we make sure it is empty */
161 TileDesc td;
163 td.build_date = INVALID_DATE;
165 /* Most tiles have only one owner, but
166 * - drivethrough roadstops can be build on town owned roads (up to 2 owners) and
167 * - roads can have up to four owners (railroad, road, tram, 3rd-roadtype "highway").
169 td.owner_type[0] = STR_LAND_AREA_INFORMATION_OWNER; // At least one owner is displayed, though it might be "N/A".
170 td.owner_type[1] = STR_NULL; // STR_NULL results in skipping the owner
171 td.owner_type[2] = STR_NULL;
172 td.owner_type[3] = STR_NULL;
173 td.owner[0] = OWNER_NONE;
174 td.owner[1] = OWNER_NONE;
175 td.owner[2] = OWNER_NONE;
176 td.owner[3] = OWNER_NONE;
178 td.station_class = STR_NULL;
179 td.station_name = STR_NULL;
180 td.airport_class = STR_NULL;
181 td.airport_name = STR_NULL;
182 td.airport_tile_name = STR_NULL;
183 td.railtype = STR_NULL;
184 td.rail_speed = 0;
185 td.roadtype = STR_NULL;
186 td.road_speed = 0;
187 td.tramtype = STR_NULL;
188 td.tram_speed = 0;
190 td.grf = nullptr;
192 CargoArray acceptance;
193 AddAcceptedCargo(tile, acceptance, nullptr);
194 GetTileDesc(tile, &td);
196 uint line_nr = 0;
198 /* Tiletype */
199 SetDParam(0, td.dparam[0]);
200 SetDParam(1, td.dparam[1]);
201 GetString(this->landinfo_data[line_nr], td.str, lastof(this->landinfo_data[line_nr]));
202 line_nr++;
204 /* Up to four owners */
205 for (uint i = 0; i < 4; i++) {
206 if (td.owner_type[i] == STR_NULL) continue;
208 SetDParam(0, STR_LAND_AREA_INFORMATION_OWNER_N_A);
209 if (td.owner[i] != OWNER_NONE && td.owner[i] != OWNER_WATER) GetNameOfOwner(td.owner[i], tile);
210 GetString(this->landinfo_data[line_nr], td.owner_type[i], lastof(this->landinfo_data[line_nr]));
211 line_nr++;
214 /* Cost to clear/revenue when cleared */
215 StringID str = STR_LAND_AREA_INFORMATION_COST_TO_CLEAR_N_A;
216 Company *c = Company::GetIfValid(_local_company);
217 if (c != nullptr) {
218 Money old_money = c->money;
219 c->money = INT64_MAX;
220 assert(_current_company == _local_company);
221 CommandCost costclear = DoCommand(tile, 0, 0, DC_NONE, CMD_LANDSCAPE_CLEAR);
222 c->money = old_money;
223 if (costclear.Succeeded()) {
224 Money cost = costclear.GetCost();
225 if (cost < 0) {
226 cost = -cost; // Negate negative cost to a positive revenue
227 str = STR_LAND_AREA_INFORMATION_REVENUE_WHEN_CLEARED;
228 } else {
229 str = STR_LAND_AREA_INFORMATION_COST_TO_CLEAR;
231 SetDParam(0, cost);
234 GetString(this->landinfo_data[line_nr], str, lastof(this->landinfo_data[line_nr]));
235 line_nr++;
237 /* Location */
238 char tmp[16];
239 seprintf(tmp, lastof(tmp), "0x%.4X", tile);
240 SetDParam(0, TileX(tile));
241 SetDParam(1, TileY(tile));
242 SetDParam(2, GetTileZ(tile));
243 SetDParamStr(3, tmp);
244 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_LANDINFO_COORDS, lastof(this->landinfo_data[line_nr]));
245 line_nr++;
247 /* Local authority */
248 SetDParam(0, STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY_NONE);
249 if (t != nullptr) {
250 SetDParam(0, STR_TOWN_NAME);
251 SetDParam(1, t->index);
253 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY, lastof(this->landinfo_data[line_nr]));
254 line_nr++;
256 /* Build date */
257 if (td.build_date != INVALID_DATE) {
258 SetDParam(0, td.build_date);
259 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_BUILD_DATE, lastof(this->landinfo_data[line_nr]));
260 line_nr++;
263 /* Station class */
264 if (td.station_class != STR_NULL) {
265 SetDParam(0, td.station_class);
266 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_STATION_CLASS, lastof(this->landinfo_data[line_nr]));
267 line_nr++;
270 /* Station type name */
271 if (td.station_name != STR_NULL) {
272 SetDParam(0, td.station_name);
273 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_STATION_TYPE, lastof(this->landinfo_data[line_nr]));
274 line_nr++;
277 /* Airport class */
278 if (td.airport_class != STR_NULL) {
279 SetDParam(0, td.airport_class);
280 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_AIRPORT_CLASS, lastof(this->landinfo_data[line_nr]));
281 line_nr++;
284 /* Airport name */
285 if (td.airport_name != STR_NULL) {
286 SetDParam(0, td.airport_name);
287 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_AIRPORT_NAME, lastof(this->landinfo_data[line_nr]));
288 line_nr++;
291 /* Airport tile name */
292 if (td.airport_tile_name != STR_NULL) {
293 SetDParam(0, td.airport_tile_name);
294 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_AIRPORTTILE_NAME, lastof(this->landinfo_data[line_nr]));
295 line_nr++;
298 /* Rail type name */
299 if (td.railtype != STR_NULL) {
300 SetDParam(0, td.railtype);
301 GetString(this->landinfo_data[line_nr], STR_LANG_AREA_INFORMATION_RAIL_TYPE, lastof(this->landinfo_data[line_nr]));
302 line_nr++;
305 /* Rail speed limit */
306 if (td.rail_speed != 0) {
307 SetDParam(0, td.rail_speed);
308 GetString(this->landinfo_data[line_nr], STR_LANG_AREA_INFORMATION_RAIL_SPEED_LIMIT, lastof(this->landinfo_data[line_nr]));
309 line_nr++;
312 /* Road type name */
313 if (td.roadtype != STR_NULL) {
314 SetDParam(0, td.roadtype);
315 GetString(this->landinfo_data[line_nr], STR_LANG_AREA_INFORMATION_ROAD_TYPE, lastof(this->landinfo_data[line_nr]));
316 line_nr++;
319 /* Road speed limit */
320 if (td.road_speed != 0) {
321 SetDParam(0, td.road_speed);
322 GetString(this->landinfo_data[line_nr], STR_LANG_AREA_INFORMATION_ROAD_SPEED_LIMIT, lastof(this->landinfo_data[line_nr]));
323 line_nr++;
326 /* Tram type name */
327 if (td.tramtype != STR_NULL) {
328 SetDParam(0, td.tramtype);
329 GetString(this->landinfo_data[line_nr], STR_LANG_AREA_INFORMATION_TRAM_TYPE, lastof(this->landinfo_data[line_nr]));
330 line_nr++;
333 /* Tram speed limit */
334 if (td.tram_speed != 0) {
335 SetDParam(0, td.tram_speed);
336 GetString(this->landinfo_data[line_nr], STR_LANG_AREA_INFORMATION_TRAM_SPEED_LIMIT, lastof(this->landinfo_data[line_nr]));
337 line_nr++;
340 /* NewGRF name */
341 if (td.grf != nullptr) {
342 SetDParamStr(0, td.grf);
343 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_NEWGRF_NAME, lastof(this->landinfo_data[line_nr]));
344 line_nr++;
347 assert(line_nr < LAND_INFO_CENTERED_LINES);
349 /* Mark last line empty */
350 this->landinfo_data[line_nr][0] = '\0';
352 /* Cargo acceptance is displayed in a extra multiline */
353 char *strp = GetString(this->landinfo_data[LAND_INFO_MULTICENTER_LINE], STR_LAND_AREA_INFORMATION_CARGO_ACCEPTED, lastof(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]));
354 bool found = false;
356 for (CargoID i = 0; i < NUM_CARGO; ++i) {
357 if (acceptance[i] > 0) {
358 /* Add a comma between each item. */
359 if (found) {
360 *strp++ = ',';
361 *strp++ = ' ';
363 found = true;
365 /* If the accepted value is less than 8, show it in 1/8:ths */
366 if (acceptance[i] < 8) {
367 SetDParam(0, acceptance[i]);
368 SetDParam(1, CargoSpec::Get(i)->name);
369 strp = GetString(strp, STR_LAND_AREA_INFORMATION_CARGO_EIGHTS, lastof(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]));
370 } else {
371 strp = GetString(strp, CargoSpec::Get(i)->name, lastof(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]));
375 if (!found) this->landinfo_data[LAND_INFO_MULTICENTER_LINE][0] = '\0';
378 virtual bool IsNewGRFInspectable() const
380 return ::IsNewGRFInspectable(GetGrfSpecFeature(this->tile), this->tile);
383 virtual void ShowNewGRFInspectWindow() const
385 ::ShowNewGRFInspectWindow(GetGrfSpecFeature(this->tile), this->tile);
389 * Some data on this window has become invalid.
390 * @param data Information about the changed data.
391 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
393 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
395 if (!gui_scope) return;
396 switch (data) {
397 case 1:
398 /* ReInit, "debug" sprite might have changed */
399 this->ReInit();
400 break;
406 * Show land information window.
407 * @param tile The tile to show information about.
409 void ShowLandInfo(TileIndex tile)
411 DeleteWindowById(WC_LAND_INFO, 0);
412 new LandInfoWindow(tile);
415 static const NWidgetPart _nested_about_widgets[] = {
416 NWidget(NWID_HORIZONTAL),
417 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
418 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_ABOUT_OPENTTD, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
419 EndContainer(),
420 NWidget(WWT_PANEL, COLOUR_GREY), SetPIP(4, 2, 4),
421 NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_ABOUT_ORIGINAL_COPYRIGHT, STR_NULL),
422 NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_ABOUT_VERSION, STR_NULL),
423 NWidget(WWT_FRAME, COLOUR_GREY), SetPadding(0, 5, 1, 5),
424 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_A_SCROLLING_TEXT),
425 EndContainer(),
426 NWidget(WWT_LABEL, COLOUR_GREY, WID_A_WEBSITE), SetDataTip(STR_BLACK_RAW_STRING, STR_NULL),
427 NWidget(WWT_LABEL, COLOUR_GREY, WID_A_WEBSITE1), SetDataTip(STR_BLACK_RAW_STRING, STR_NULL),
428 NWidget(WWT_LABEL, COLOUR_GREY, WID_A_WEBSITE2), SetDataTip(STR_BLACK_RAW_STRING, STR_NULL),
429 NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_ABOUT_COPYRIGHT_OPENTTD, STR_NULL),
430 EndContainer(),
433 static WindowDesc _about_desc(
434 WDP_CENTER, nullptr, 0, 0,
435 WC_GAME_OPTIONS, WC_NONE,
437 _nested_about_widgets, lengthof(_nested_about_widgets)
440 static const char * const _credits[] = {
441 "Original design by Chris Sawyer",
442 "Original graphics by Simon Foster",
444 "The OpenTTD team (in alphabetical order):",
445 " Grzegorz Duczy\xC5\x84ski (adf88) - General coding (since 1.7.2)",
446 " Albert Hofkamp (Alberth) - GUI expert (since 0.7)",
447 " Matthijs Kooijman (blathijs) - Pathfinder-guru, Debian port (since 0.3)",
448 " Ulf Hermann (fonsinchen) - Cargo Distribution (since 1.3)",
449 " Christoph Elsenhans (frosch) - General coding (since 0.6)",
450 " Lo\xC3\xAF""c Guilloux (glx) - General / Windows Expert (since 0.4.5)",
451 " Michael Lutz (michi_cc) - Path based signals (since 0.7)",
452 " Owen Rudge (orudge) - Forum host, OS/2 port (since 0.1)",
453 " Peter Nelson (peter1138) - Spiritual descendant from NewGRF gods (since 0.4.5)",
454 " Ingo von Borstel (planetmaker) - General, Support (since 1.1)",
455 " Remko Bijker (Rubidium) - Lead coder and way more (since 0.4.5)",
456 " Jos\xC3\xA9 Soler (Terkhen) - General coding (since 1.0)",
457 " Leif Linse (Zuu) - AI/Game Script (since 1.2)",
459 "Inactive Developers:",
460 " Jean-Fran\xC3\xA7ois Claeys (Belugas) - GUI, NewGRF and more (0.4.5 - 1.0)",
461 " Bjarni Corfitzen (Bjarni) - MacOSX port, coder and vehicles (0.3 - 0.7)",
462 " Victor Fischer (Celestar) - Programming everywhere you need him to (0.3 - 0.6)",
463 " Jaroslav Mazanec (KUDr) - YAPG (Yet Another Pathfinder God) ;) (0.4.5 - 0.6)",
464 " Jonathan Coome (Maedhros) - High priest of the NewGRF Temple (0.5 - 0.6)",
465 " Attila B\xC3\xA1n (MiHaMiX) - Developer WebTranslator 1 and 2 (0.3 - 0.5)",
466 " Zden\xC4\x9Bk Sojka (SmatZ) - Bug finder and fixer (0.6 - 1.3)",
467 " Christoph Mallon (Tron) - Programmer, code correctness police (0.3 - 0.5)",
468 " Patric Stout (TrueBrain) - NoAI, NoGo, Network (0.3 - 1.2), sys op (active)",
469 " Thijs Marinussen (Yexo) - AI Framework, General (0.6 - 1.3)",
471 "Retired Developers:",
472 " Tam\xC3\xA1s Farag\xC3\xB3 (Darkvater) - Ex-Lead coder (0.3 - 0.5)",
473 " Dominik Scherer (dominik81) - Lead programmer, GUI expert (0.3 - 0.3)",
474 " Emil Djupfeld (egladil) - MacOSX (0.4.5 - 0.6)",
475 " Simon Sasburg (HackyKid) - Many bugfixes (0.4 - 0.4.5)",
476 " Ludvig Strigeus (ludde) - Original author of OpenTTD, main coder (0.1 - 0.3)",
477 " Cian Duffy (MYOB) - BeOS port / manual writing (0.1 - 0.3)",
478 " Petr Baudi\xC5\xA1 (pasky) - Many patches, NewGRF support (0.3 - 0.3)",
479 " Benedikt Br\xC3\xBCggemeier (skidd13) - Bug fixer and code reworker (0.6 - 0.7)",
480 " Serge Paquet (vurlix) - 2nd contributor after ludde (0.1 - 0.3)",
482 "Special thanks go out to:",
483 " Josef Drexler - For his great work on TTDPatch",
484 " Marcin Grzegorczyk - Track foundations and for describing TTD internals",
485 " Stefan Mei\xC3\x9Fner (sign_de) - For his work on the console",
486 " Mike Ragsdale - OpenTTD installer",
487 " Christian Rosentreter (tokai) - MorphOS / AmigaOS port",
488 " Richard Kempton (richK) - additional airports, initial TGP implementation",
490 " Alberto Demichelis - Squirrel scripting language \xC2\xA9 2003-2008",
491 " L. Peter Deutsch - MD5 implementation \xC2\xA9 1999, 2000, 2002",
492 " Michael Blunck - Pre-signals and semaphores \xC2\xA9 2003",
493 " George - Canal/Lock graphics \xC2\xA9 2003-2004",
494 " Andrew Parkhouse (andythenorth) - River graphics",
495 " David Dallaston (Pikka) - Tram tracks",
496 " All Translators - Who made OpenTTD a truly international game",
497 " Bug Reporters - Without whom OpenTTD would still be full of bugs!",
500 "Developer of this patchpack:",
501 " Andreas Schmitt (Lt_Joker)",
504 "Several features imported from the JGR Patch Pack developed by:",
505 " Jonathan G. Rennison (JGR)",
508 "And last but not least:",
509 " Chris Sawyer - For an amazing game!"
512 struct AboutWindow : public Window {
513 int text_position; ///< The top of the scrolling text
514 byte counter; ///< Used to scroll the text every 5 ticks
515 int line_height = FONT_HEIGHT_NORMAL; ///< The height of a single line
516 static const int num_visible_lines = 19; ///< The number of lines visible simultaneously
518 AboutWindow() : Window(&_about_desc)
520 this->InitNested(WN_GAME_OPTIONS_ABOUT);
522 this->counter = 5;
523 this->text_position = this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->pos_y + this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->current_y;
526 void SetStringParameters(int widget) const override
528 if (widget == WID_A_WEBSITE) SetDParamStr(0, "Main project website: http://www.openttd.org");
529 if (widget == WID_A_WEBSITE1) SetDParamStr(0, "Patchpack thread: https://www.tt-forums.net/viewtopic.php?f=33&t=74365");
530 if (widget == WID_A_WEBSITE2) SetDParamStr(0, "Patchpack Github: https://github.com/KeldorKatarn/OpenTTD_PatchPack");
533 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
535 if (widget != WID_A_SCROLLING_TEXT) return;
537 this->line_height = FONT_HEIGHT_NORMAL;
539 Dimension d{};
540 d.height = this->line_height * num_visible_lines;
541 d.width = 0;
543 for (auto text_line : _credits) {
544 d.width = max(d.width, GetStringBoundingBox(text_line).width);
547 *size = maxdim(*size, d);
550 void DrawWidget(const Rect &r, int widget) const override
552 if (widget != WID_A_SCROLLING_TEXT) return;
554 int y = this->text_position;
556 /* Show all scrolling _credits */
557 for (auto text_line : _credits) {
558 if (y >= r.top + 7 && y < r.bottom - this->line_height) {
559 DrawString(r.left, r.right, y, text_line, TC_BLACK, SA_LEFT | SA_FORCE);
562 y += this->line_height;
566 void OnTick() override
568 if (--this->counter == 0) {
569 this->counter = 5;
570 this->text_position--;
571 /* If the last text has scrolled start a new from the start */
572 if (this->text_position < int(this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->pos_y - lengthof(_credits) * this->line_height)) {
573 this->text_position = this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->pos_y + this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->current_y;
575 this->SetDirty();
580 void ShowAboutWindow()
582 DeleteWindowByClass(WC_GAME_OPTIONS);
583 new AboutWindow();
587 * Display estimated costs.
588 * @param cost Estimated cost (or income if negative).
589 * @param x X position of the notification window.
590 * @param y Y position of the notification window.
592 void ShowEstimatedCostOrIncome(Money cost, int x, int y)
594 StringID message = STR_MESSAGE_ESTIMATED_COST;
596 if (cost < 0) {
597 cost = -cost;
598 message = STR_MESSAGE_ESTIMATED_INCOME;
601 SetDParam(0, cost);
602 ShowErrorMessage(message, INVALID_STRING_ID, WL_INFO, x, y);
606 * Display animated income or costs on the map.
607 * @param x World X position of the animation location.
608 * @param y World Y position of the animation location.
609 * @param z World Z position of the animation location.
610 * @param cost Estimated cost (or income if negative).
612 void ShowCostOrIncomeAnimation(int x, int y, int z, Money cost)
614 const Point point = RemapCoords(x, y, z);
615 StringID message = STR_INCOME_FLOAT_COST;
617 if (cost < 0) {
618 cost = -cost;
619 message = STR_INCOME_FLOAT_INCOME;
622 SetDParam(0, cost);
623 AddTextEffect(message, point.x, point.y, DAY_TICKS, TE_RISING);
627 * Display animated feeder income.
628 * @param x World X position of the animation location.
629 * @param y World Y position of the animation location.
630 * @param z World Z position of the animation location.
631 * @param transfer Estimated feeder income.
632 * @param income Real income from goods being delivered to their final destination.
634 void ShowFeederIncomeAnimation(int x, int y, int z, const Money& transfer, Money income)
636 const Point point = RemapCoords(x, y, z);
638 SetDParam(0, transfer);
640 if (income == 0) {
641 AddTextEffect(STR_FEEDER, point.x, point.y, DAY_TICKS, TE_RISING);
642 } else {
643 StringID message = STR_FEEDER_COST;
645 if (income < 0) {
646 income = -income;
647 message = STR_FEEDER_INCOME;
650 SetDParam(1, income);
651 AddTextEffect(message, point.x, point.y, DAY_TICKS, TE_RISING);
656 * Display vehicle loading indicators.
657 * @param x World X position of the animation location.
658 * @param y World Y position of the animation location.
659 * @param z World Z position of the animation location.
660 * @param percent Estimated feeder income.
661 * @param string String which is drawn on the map.
662 * @return TextEffectID to be used for future updates of the loading indicators.
664 TextEffectID ShowFillingPercent(int x, int y, int z, uint8 percent, StringID string)
666 const Point point = RemapCoords(x, y, z);
668 assert(string != STR_NULL);
670 SetDParam(0, percent);
671 return AddTextEffect(string, point.x, point.y, 0, TE_STATIC);
675 * Update vehicle loading indicators.
676 * @param te_id TextEffectID to be updated.
677 * @param percent The filling percentage.
678 * @param string String which is printed.
680 void UpdateFillingPercent(TextEffectID te_id, uint8 percent, StringID string)
682 assert(string != STR_NULL);
684 SetDParam(0, percent);
685 UpdateTextEffect(te_id, string);
689 * Hide vehicle loading indicators.
690 * @param te_id TextEffectID which is supposed to be hidden.
692 void HideFillingPercent(TextEffectID *te_id)
694 if (*te_id == INVALID_TE_ID) return;
696 RemoveTextEffect(*te_id);
697 *te_id = INVALID_TE_ID;
700 static const NWidgetPart _nested_tooltips_widgets[] = {
701 NWidget(WWT_PANEL, COLOUR_GREY, WID_TT_BACKGROUND), SetMinimalSize(200, 32), EndContainer(),
704 static WindowDesc _tool_tips_desc(
705 WDP_MANUAL, nullptr, 0, 0, // Coordinates and sizes are not used,
706 WC_TOOLTIPS, WC_NONE,
707 WDF_NO_FOCUS,
708 _nested_tooltips_widgets, lengthof(_nested_tooltips_widgets)
711 /** Window for displaying a tooltip. */
712 struct TooltipsWindow : public Window
714 const char *tip; ///< The text being displayed in the window.
715 TooltipCloseCondition close_cond; ///< Condition for closing the window.
716 char buffer[DRAW_STRING_BUFFER]{}; ///< Text to draw
718 TooltipsWindow(Window *parent, const char *tip, TooltipCloseCondition close_tooltip) : Window(&_tool_tips_desc)
720 this->parent = parent;
721 this->tip = stredup(tip);
722 this->close_cond = close_tooltip;
724 this->InitNested();
726 CLRBITS(this->flags, WF_WHITE_BORDER);
729 virtual ~TooltipsWindow()
731 free(this->tip);
734 Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number) override
736 /* Find the free screen space between the main toolbar at the top, and the statusbar at the bottom.
737 * Add a fixed distance 2 so the tooltip floats free from both bars.
739 const int screen_top = GetMainViewTop() + 2;
740 const int screen_bottom = GetMainViewBottom() - 2;
742 Point point{};
744 /* Correctly position the tooltip position, watch out for window and cursor size
745 * Clamp value to below main toolbar and above statusbar. If tooltip would
746 * go below window, flip it so it is shown above the cursor */
747 point.y = Clamp(_cursor.pos.y + _cursor.total_size.y + _cursor.total_offs.y + 5, screen_top, screen_bottom);
749 if (point.y + sm_height > screen_bottom) {
750 point.y = min(_cursor.pos.y + _cursor.total_offs.y - 5, screen_bottom) - sm_height;
753 point.x = sm_width >= _screen.width ? 0 : Clamp(_cursor.pos.x - (sm_width >> 1), 0, _screen.width - sm_width);
755 return point;
758 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
760 /* There is only one widget. */
761 size->width = min(GetStringBoundingBox(this->tip).width, ScaleGUITrad(250));
762 size->height = GetStringHeight(this->tip, size->width);
764 /* Increase slightly to have some space around the box. */
765 size->width += 2 + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
766 size->height += 2 + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
769 void DrawWidget(const Rect &r, int widget) const override
771 /* There is only one widget. */
772 GfxFillRect(r.left, r.top, r.right, r.bottom, PC_BLACK);
773 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_LIGHT_YELLOW);
775 DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, r.bottom - WD_FRAMERECT_BOTTOM, this->tip, TC_FROMSTRING, SA_CENTER);
778 void OnMouseLoop() override
780 /* Always close tooltips when the cursor is not in our window. */
781 if (!_cursor.in_window) {
782 delete this;
783 return;
786 /* We can show tooltips while dragging tools. These are shown as long as
787 * we are dragging the tool. Normal tooltips work with hover or rmb. */
788 switch (this->close_cond) {
789 case TCC_RIGHT_CLICK: if (!_right_button_down) delete this; break;
790 case TCC_LEFT_CLICK: if (!_left_button_down) delete this; break;
791 case TCC_HOVER: if (!_mouse_hovering) delete this; break;
795 void OnInvalidateData(int data, bool gui_scope) override
797 if (!gui_scope) return;
799 /* This hover has ended (_mouse_hovering might be true for a new hover). */
800 if (this->close_cond == TCC_HOVER) delete this;
805 * Shows a tooltip
806 * @param parent The window this tooltip is related to.
807 * @param str String to be displayed
808 * @param close_tooltip Close condition.
810 void GuiShowTooltips(Window *parent, StringID str, TooltipCloseCondition close_tooltip)
812 char buff[DRAW_STRING_BUFFER] = "";
813 if (str != STR_NULL) GetString(buff, str, lastof(buff));
815 GuiShowTooltips(parent, buff, close_tooltip);
819 * Shows a tooltip
820 * @param parent The window this tooltip is related to.
821 * @param str String to be displayed.
822 * @param close_tooltip Close condition.
824 void GuiShowTooltips(Window *parent, const char *str, TooltipCloseCondition close_tooltip)
826 DeleteWindowById(WC_TOOLTIPS, 0);
828 if (StrEmpty(str)) return;
831 new TooltipsWindow(parent, str, close_tooltip);
835 * Get tooltip string for an industry tile.
836 * @param iid ID of the industry.
837 * @param buff Buffer to store the string in.
838 * @param last Last character of the buffer.
840 static void GetTileTooltipsForIndustry(IndustryID iid, char *buff, const char *last)
842 if (!Industry::IsValidID(iid)) return;
844 SetDParam(0, iid);
845 buff = GetString(buff, STR_TILE_TOOLTIP_INDUSTRY_NAME, last);
847 const Industry *industry = Industry::Get(iid);
848 for (size_t i = 0; i < lengthof(industry->produced_cargo); i++) {
849 if (industry->produced_cargo[i] != CT_INVALID) {
850 buff = strecpy(buff, "\n", last);
851 SetDParam(0, industry->produced_cargo[i]);
852 SetDParam(1, industry->last_month_production[i]);
853 SetDParam(2, ToPercent8(industry->last_month_pct_transported[i]));
854 buff = GetString(buff, STR_TILE_TOOLTIP_CARGO_ITEM, last);
860 * Get tooltip string for a station tile.
861 * @param sid ID of the station.
862 * @param buff Buffer to store the string in.
863 * @param last Last character of the buffer.
865 void GetTileTooltipsForStation(StationID sid, char *buff, const char *last)
867 SetDParam(0, sid);
868 if (Waypoint::IsValidID(sid)) {
869 GetString(buff, STR_TILE_TOOLTIP_WAYPOINT_NAME, last);
870 } else if (Station::IsValidID(sid)) {
871 Station *station = Station::Get(sid);
872 SetDParam(1, station->facilities);
873 buff = GetString(buff, STR_TILE_TOOLTIP_STATION_NAME_FEATURES, last);
874 for (CargoID i = 0; i < lengthof(station->goods); i++) {
875 if (station->goods[i].HasRating()) {
876 buff = strecpy(buff, "\n", last);
877 SetDParam(0, i);
878 SetDParam(1, station->goods[i].cargo.TotalCount());
879 SetDParam(2, ToPercent8(station->goods[i].rating));
880 buff = GetString(buff, STR_TILE_TOOLTIP_CARGO_ITEM, last);
887 * Get tooltip string for a town tile.
888 * @param town_id ID of the town.
889 * @return The string.
891 static StringID GetTileTooltipsForTown(TownID town_id)
893 const Town* town = Town::GetIfValid(town_id);
895 if (town == nullptr) return STR_NULL;
897 SetDParam(0, town_id);
898 SetDParam(1, town->cache.population);
900 if (town->IsGrowing()) {
901 SetDParam(2, town->GetGrowthRateInDays());
904 return town->IsGrowing() ? STR_TILE_TOOLTIP_TOWN_NAME_POP_GROWTH : STR_TILE_TOOLTIP_TOWN_NAME_POP;
908 * Get tooltip string for a company owned tile.
909 * @param cid ID of the company.
910 * @return The string.
912 static StringID GetTileTooltipsForCompany(CompanyID cid)
914 if (!Company::IsValidID(cid) || IsInteractiveCompany(cid)) return STR_NULL;
916 SetDParam(0, cid);
917 return STR_TILE_TOOLTIP_COMPANY_NAME;
921 * Show tooltip for a given tile.
922 * @param parent Parent window of the tooltip.
923 * @param tile The tile to show the tip for.
924 * @param close_tooltip Close condition for the tooltip.
926 void GuiShowTooltipsForTile(Window* parent, TileIndex tile, TooltipCloseCondition close_tooltip)
928 StringID tool_tip_string = STR_NULL;
930 if (tile < MapSize()) {
931 const TileType tile_type = GetTileType(tile);
933 switch (tile_type) {
934 case MP_STATION:
935 case MP_INDUSTRY: {
936 char buff[DRAW_STRING_BUFFER] = "";
938 if (tile_type == MP_STATION) {
939 GetTileTooltipsForStation(GetStationIndex(tile), buff, lastof(buff));
940 } else {
941 GetTileTooltipsForIndustry(GetIndustryIndex(tile), buff, lastof(buff));
944 GuiShowTooltips(parent, buff, close_tooltip);
945 return;
948 case MP_RAILWAY:
949 case MP_ROAD:
950 case MP_WATER:
951 case MP_TUNNELBRIDGE:
952 case MP_OBJECT: {
953 const Owner owner = GetTileOwner(tile);
955 if (owner != OWNER_TOWN) {
956 tool_tip_string = GetTileTooltipsForCompany(owner);
957 break;
960 /* FALLTHROUGH */
961 case MP_HOUSE:
962 tool_tip_string = GetTileTooltipsForTown(tile_type != MP_TUNNELBRIDGE ? GetTownIndex(tile) : CalcClosestTownFromTile(tile)->index);
963 break;
965 default:
966 break;
970 GuiShowTooltips(parent, tool_tip_string, close_tooltip);
973 void QueryString::HandleEditBox(Window *w, int wid)
975 if (w->IsWidgetGloballyFocused(wid) && this->text.HandleCaret()) {
976 w->SetWidgetDirty(wid);
978 /* For the OSK also invalidate the parent window */
979 if (w->window_class == WC_OSK) w->InvalidateData();
983 void QueryString::DrawEditBox(const Window *w, int wid) const
985 const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
987 assert((wi->type & WWT_MASK) == WWT_EDITBOX);
989 bool rtl = _current_text_dir == TD_RTL;
990 Dimension sprite_size = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
991 int clearbtn_width = sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT;
993 int clearbtn_left = wi->pos_x + (rtl ? 0 : wi->current_x - clearbtn_width);
994 int clearbtn_right = wi->pos_x + (rtl ? clearbtn_width : wi->current_x) - 1;
995 int left = wi->pos_x + (rtl ? clearbtn_width : 0);
996 int right = wi->pos_x + (rtl ? wi->current_x : wi->current_x - clearbtn_width) - 1;
998 int top = wi->pos_y;
999 int bottom = wi->pos_y + wi->current_y - 1;
1001 DrawFrameRect(clearbtn_left, top, clearbtn_right, bottom, wi->colour, wi->IsLowered() ? FR_LOWERED : FR_NONE);
1002 DrawSprite(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT, PAL_NONE, clearbtn_left + WD_IMGBTN_LEFT + (wi->IsLowered() ? 1 : 0), (top + bottom - sprite_size.height) / 2 + (wi->IsLowered() ? 1 : 0));
1003 if (this->text.bytes == 1) GfxFillRect(clearbtn_left + 1, top + 1, clearbtn_right - 1, bottom - 1, _colour_gradient[wi->colour & 0xF][2], FILLRECT_CHECKER);
1005 DrawFrameRect(left, top, right, bottom, wi->colour, FR_LOWERED | FR_DARKENED);
1006 GfxFillRect(left + 1, top + 1, right - 1, bottom - 1, PC_BLACK);
1008 /* Limit the drawing of the string inside the widget boundaries */
1009 DrawPixelInfo dpi;
1010 if (!FillDrawPixelInfo(&dpi, left + WD_FRAMERECT_LEFT, top + WD_FRAMERECT_TOP, right - left - WD_FRAMERECT_RIGHT, bottom - top - WD_FRAMERECT_BOTTOM)) return;
1012 DrawPixelInfo *old_dpi = _cur_dpi;
1013 _cur_dpi = &dpi;
1015 /* We will take the current widget length as maximum width, with a small
1016 * space reserved at the end for the caret to show */
1017 const Textbuf *tb = &this->text;
1018 int delta = min(0, (right - left) - tb->pixels - 10);
1020 if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
1022 /* If we have a marked area, draw a background highlight. */
1023 if (tb->marklength != 0) GfxFillRect(delta + tb->markxoffs, 0, delta + tb->markxoffs + tb->marklength - 1, bottom - top, PC_GREY);
1025 DrawString(delta, tb->pixels, 0, tb->buf, TC_YELLOW);
1026 bool focussed = w->IsWidgetGloballyFocused(wid) || IsOSKOpenedFor(w, wid);
1027 if (focussed && tb->caret) {
1028 int caret_width = GetStringBoundingBox("_").width;
1029 DrawString(tb->caretxoffs + delta, tb->caretxoffs + delta + caret_width, 0, "_", TC_WHITE);
1032 _cur_dpi = old_dpi;
1036 * Get the current caret position.
1037 * @param w Window the edit box is in.
1038 * @param wid Widget index.
1039 * @return Top-left location of the caret, relative to the window.
1041 Point QueryString::GetCaretPosition(const Window *w, int wid) const
1043 const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
1045 assert((wi->type & WWT_MASK) == WWT_EDITBOX);
1047 bool rtl = _current_text_dir == TD_RTL;
1048 Dimension sprite_size = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
1049 int clearbtn_width = sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT;
1051 int left = wi->pos_x + (rtl ? clearbtn_width : 0);
1052 int right = wi->pos_x + (rtl ? wi->current_x : wi->current_x - clearbtn_width) - 1;
1054 /* Clamp caret position to be inside out current width. */
1055 const Textbuf *tb = &this->text;
1056 int delta = min(0, (right - left) - tb->pixels - 10);
1057 if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
1059 Point pt = {left + WD_FRAMERECT_LEFT + tb->caretxoffs + delta, (int)wi->pos_y + WD_FRAMERECT_TOP};
1060 return pt;
1064 * Get the bounding rectangle for a range of the query string.
1065 * @param w Window the edit box is in.
1066 * @param wid Widget index.
1067 * @param from Start of the string range.
1068 * @param to End of the string range.
1069 * @return Rectangle encompassing the string range, relative to the window.
1071 Rect QueryString::GetBoundingRect(const Window *w, int wid, const char *from, const char *to) const
1073 const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
1075 assert((wi->type & WWT_MASK) == WWT_EDITBOX);
1077 bool rtl = _current_text_dir == TD_RTL;
1078 Dimension sprite_size = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
1079 int clearbtn_width = sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT;
1081 int left = wi->pos_x + (rtl ? clearbtn_width : 0);
1082 int right = wi->pos_x + (rtl ? wi->current_x : wi->current_x - clearbtn_width) - 1;
1084 int top = wi->pos_y + WD_FRAMERECT_TOP;
1085 int bottom = wi->pos_y + wi->current_y - 1 - WD_FRAMERECT_BOTTOM;
1087 /* Clamp caret position to be inside our current width. */
1088 const Textbuf *tb = &this->text;
1089 int delta = min(0, (right - left) - tb->pixels - 10);
1090 if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
1092 /* Get location of first and last character. */
1093 Point p1 = GetCharPosInString(tb->buf, from, FS_NORMAL);
1094 Point p2 = from != to ? GetCharPosInString(tb->buf, to, FS_NORMAL) : p1;
1096 Rect r = { Clamp(left + p1.x + delta + WD_FRAMERECT_LEFT, left, right), top, Clamp(left + p2.x + delta + WD_FRAMERECT_LEFT, left, right - WD_FRAMERECT_RIGHT), bottom };
1098 return r;
1102 * Get the character that is rendered at a position.
1103 * @param w Window the edit box is in.
1104 * @param wid Widget index.
1105 * @param pt Position to test.
1106 * @return Pointer to the character at the position or nullptr if no character is at the position.
1108 const char *QueryString::GetCharAtPosition(const Window *w, int wid, const Point &pt) const
1110 const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
1112 assert((wi->type & WWT_MASK) == WWT_EDITBOX);
1114 bool rtl = _current_text_dir == TD_RTL;
1115 Dimension sprite_size = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
1116 int clearbtn_width = sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT;
1118 int left = wi->pos_x + (rtl ? clearbtn_width : 0);
1119 int right = wi->pos_x + (rtl ? wi->current_x : wi->current_x - clearbtn_width) - 1;
1121 int top = wi->pos_y + WD_FRAMERECT_TOP;
1122 int bottom = wi->pos_y + wi->current_y - 1 - WD_FRAMERECT_BOTTOM;
1124 if (!IsInsideMM(pt.y, top, bottom)) return nullptr;
1126 /* Clamp caret position to be inside our current width. */
1127 const Textbuf *tb = &this->text;
1128 int delta = min(0, (right - left) - tb->pixels - 10);
1129 if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
1131 return ::GetCharAtPosition(tb->buf, pt.x - delta - left);
1134 void QueryString::ClickEditBox(Window *w, Point pt, int wid, int click_count, bool focus_changed)
1136 const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
1138 assert((wi->type & WWT_MASK) == WWT_EDITBOX);
1140 bool rtl = _current_text_dir == TD_RTL;
1141 int clearbtn_width = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT).width;
1143 int clearbtn_left = wi->pos_x + (rtl ? 0 : wi->current_x - clearbtn_width);
1145 if (IsInsideBS(pt.x, clearbtn_left, clearbtn_width)) {
1146 if (this->text.bytes > 1) {
1147 this->text.DeleteAll();
1148 w->HandleButtonClick(wid);
1149 w->OnEditboxChanged(wid);
1151 return;
1154 if (w->window_class != WC_OSK && _settings_client.gui.osk_activation != OSKA_DISABLED &&
1155 (!focus_changed || _settings_client.gui.osk_activation == OSKA_IMMEDIATELY) &&
1156 (click_count == 2 || _settings_client.gui.osk_activation != OSKA_DOUBLE_CLICK)) {
1157 /* Open the OSK window */
1158 ShowOnScreenKeyboard(w, wid);
1162 /** Class for the string query window. */
1163 struct QueryStringWindow : public Window
1165 QueryString editbox; ///< Editbox.
1166 QueryStringFlags flags; ///< Flags controlling behaviour of the window.
1168 QueryStringWindow(StringID str, StringID caption, uint max_bytes, uint max_chars, WindowDesc *desc, Window *parent, CharSetFilter afilter, QueryStringFlags flags) :
1169 Window(desc), editbox(max_bytes, max_chars)
1171 char *last_of = &this->editbox.text.buf[this->editbox.text.max_bytes - 1];
1172 GetString(this->editbox.text.buf, str, last_of);
1173 str_validate(this->editbox.text.buf, last_of, SVS_NONE);
1175 /* Make sure the name isn't too long for the text buffer in the number of
1176 * characters (not bytes). max_chars also counts the '\0' characters. */
1177 while (Utf8StringLength(this->editbox.text.buf) + 1 > this->editbox.text.max_chars) {
1178 *Utf8PrevChar(this->editbox.text.buf + strlen(this->editbox.text.buf)) = '\0';
1181 this->editbox.text.UpdateSize();
1183 if ((flags & QSF_ACCEPT_UNCHANGED) == 0) this->editbox.orig = stredup(this->editbox.text.buf);
1185 this->querystrings[WID_QS_TEXT] = &this->editbox;
1186 this->editbox.caption = caption;
1187 this->editbox.cancel_button = WID_QS_CANCEL;
1188 this->editbox.ok_button = WID_QS_OK;
1189 this->editbox.text.afilter = afilter;
1190 this->flags = flags;
1192 this->InitNested(WN_QUERY_STRING);
1194 this->parent = parent;
1196 this->SetFocusedWidget(WID_QS_TEXT);
1199 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1201 if (widget == WID_QS_DEFAULT && (this->flags & QSF_ENABLE_DEFAULT) == 0) {
1202 /* We don't want this widget to show! */
1203 fill->width = 0;
1204 resize->width = 0;
1205 size->width = 0;
1209 virtual void SetStringParameters(int widget) const
1211 if (widget == WID_QS_CAPTION) SetDParam(0, this->editbox.caption);
1214 void OnOk()
1216 if (this->editbox.orig == nullptr || strcmp(this->editbox.text.buf, this->editbox.orig) != 0) {
1217 /* If the parent is nullptr, the editbox is handled by general function
1218 * HandleOnEditText */
1219 if (this->parent != nullptr) {
1220 this->parent->OnQueryTextFinished(this->editbox.text.buf);
1221 } else {
1222 HandleOnEditText(this->editbox.text.buf);
1224 this->editbox.handled = true;
1228 virtual void OnClick(Point pt, int widget, int click_count)
1230 switch (widget) {
1231 case WID_QS_DEFAULT:
1232 this->editbox.text.DeleteAll();
1233 FALLTHROUGH;
1235 case WID_QS_OK:
1236 this->OnOk();
1237 FALLTHROUGH;
1239 case WID_QS_CANCEL:
1240 delete this;
1241 break;
1245 ~QueryStringWindow()
1247 if (!this->editbox.handled && this->parent != nullptr) {
1248 Window *parent = this->parent;
1249 this->parent = nullptr; // so parent doesn't try to delete us again
1250 parent->OnQueryTextFinished(nullptr);
1255 static const NWidgetPart _nested_query_string_widgets[] = {
1256 NWidget(NWID_HORIZONTAL),
1257 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1258 NWidget(WWT_CAPTION, COLOUR_GREY, WID_QS_CAPTION), SetDataTip(STR_WHITE_STRING, STR_NULL),
1259 EndContainer(),
1260 NWidget(WWT_PANEL, COLOUR_GREY),
1261 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_QS_TEXT), SetMinimalSize(256, 12), SetFill(1, 1), SetPadding(2, 2, 2, 2),
1262 EndContainer(),
1263 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1264 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_QS_DEFAULT), SetMinimalSize(87, 12), SetFill(1, 1), SetDataTip(STR_BUTTON_DEFAULT, STR_NULL),
1265 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_QS_CANCEL), SetMinimalSize(86, 12), SetFill(1, 1), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
1266 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_QS_OK), SetMinimalSize(87, 12), SetFill(1, 1), SetDataTip(STR_BUTTON_OK, STR_NULL),
1267 EndContainer(),
1270 static WindowDesc _query_string_desc(
1271 WDP_CENTER, "query_string", 0, 0,
1272 WC_QUERY_STRING, WC_NONE,
1274 _nested_query_string_widgets, lengthof(_nested_query_string_widgets)
1278 * Show a query popup window with a textbox in it.
1279 * @param str StringID for the text shown in the textbox
1280 * @param caption StringID of text shown in caption of querywindow
1281 * @param maxsize maximum size in bytes or characters (including terminating '\0') depending on flags
1282 * @param parent pointer to a Window that will handle the events (ok/cancel) of this
1283 * window. If nullptr, results are handled by global function HandleOnEditText
1284 * @param afilter filters out unwanted character input
1285 * @param flags various flags, @see QueryStringFlags
1287 void ShowQueryString(StringID str, StringID caption, uint maxsize, Window *parent, CharSetFilter afilter, QueryStringFlags flags)
1289 DeleteWindowByClass(WC_QUERY_STRING);
1290 new QueryStringWindow(str, caption, ((flags & QSF_LEN_IN_CHARS) ? MAX_CHAR_LENGTH : 1) * maxsize, maxsize, &_query_string_desc, parent, afilter, flags);
1294 * Window used for asking the user a YES/NO question.
1296 struct QueryWindow : public Window {
1297 QueryCallback proc; ///< callback function executed on closing of popup. Window* points to parent, bool is true if 'yes' clicked, false otherwise
1298 uint64 params[10]; ///< local copy of _decode_parameters
1299 StringID message; ///< message shown for query window
1300 StringID caption; ///< title of window
1302 QueryWindow(WindowDesc *desc, StringID caption, StringID message, Window *parent, QueryCallback callback) :
1303 Window(desc), proc(std::move(callback))
1305 /* Create a backup of the variadic arguments to strings because it will be
1306 * overridden pretty often. We will copy these back for drawing */
1307 CopyOutDParam(this->params, 0, lengthof(this->params));
1308 this->caption = caption;
1309 this->message = message;
1311 this->InitNested(WN_CONFIRM_POPUP_QUERY);
1313 this->parent = parent;
1314 this->left = parent->left + (parent->width / 2) - (this->width / 2);
1315 this->top = parent->top + (parent->height / 2) - (this->height / 2);
1318 ~QueryWindow()
1320 if (this->proc != nullptr) this->proc(this->parent, false);
1323 virtual void SetStringParameters(int widget) const
1325 switch (widget) {
1326 case WID_Q_CAPTION:
1327 CopyInDParam(1, this->params, lengthof(this->params));
1328 SetDParam(0, this->caption);
1329 break;
1331 case WID_Q_TEXT:
1332 CopyInDParam(0, this->params, lengthof(this->params));
1333 break;
1337 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1339 if (widget != WID_Q_TEXT) return;
1341 Dimension d = GetStringMultiLineBoundingBox(this->message, *size);
1342 d.width += WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
1343 d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
1344 *size = d;
1347 virtual void DrawWidget(const Rect &r, int widget) const
1349 if (widget != WID_Q_TEXT) return;
1351 DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top + WD_FRAMERECT_TOP, r.bottom - WD_FRAMERECT_BOTTOM,
1352 this->message, TC_FROMSTRING, SA_CENTER);
1355 virtual void OnClick(Point pt, int widget, int click_count)
1357 switch (widget) {
1358 case WID_Q_YES: {
1359 /* in the Generate New World window, clicking 'Yes' causes
1360 * DeleteNonVitalWindows() to be called - we shouldn't be in a window then */
1361 auto proc = this->proc;
1362 Window *parent = this->parent;
1363 /* Prevent the destructor calling the callback function */
1364 this->proc = nullptr;
1365 delete this;
1366 if (proc != nullptr) {
1367 proc(parent, true);
1368 proc = nullptr;
1370 break;
1372 case WID_Q_NO:
1373 delete this;
1374 break;
1378 virtual EventState OnKeyPress(WChar key, uint16 keycode)
1380 /* ESC closes the window, Enter confirms the action */
1381 switch (keycode) {
1382 case WKC_RETURN:
1383 case WKC_NUM_ENTER:
1384 if (this->proc != nullptr) {
1385 this->proc(this->parent, true);
1386 this->proc = nullptr;
1388 FALLTHROUGH;
1390 case WKC_ESC:
1391 delete this;
1392 return ES_HANDLED;
1394 return ES_NOT_HANDLED;
1398 static const NWidgetPart _nested_query_widgets[] = {
1399 NWidget(NWID_HORIZONTAL),
1400 NWidget(WWT_CLOSEBOX, COLOUR_RED),
1401 NWidget(WWT_CAPTION, COLOUR_RED, WID_Q_CAPTION), SetDataTip(STR_JUST_STRING, STR_NULL),
1402 EndContainer(),
1403 NWidget(WWT_PANEL, COLOUR_RED), SetPIP(8, 15, 8),
1404 NWidget(WWT_TEXT, COLOUR_RED, WID_Q_TEXT), SetMinimalSize(200, 12),
1405 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(20, 29, 20),
1406 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_Q_NO), SetMinimalSize(71, 12), SetFill(1, 1), SetDataTip(STR_QUIT_NO, STR_NULL),
1407 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_Q_YES), SetMinimalSize(71, 12), SetFill(1, 1), SetDataTip(STR_QUIT_YES, STR_NULL),
1408 EndContainer(),
1409 EndContainer(),
1412 static WindowDesc _query_desc(
1413 WDP_CENTER, nullptr, 0, 0,
1414 WC_CONFIRM_POPUP_QUERY, WC_NONE,
1415 WDF_MODAL,
1416 _nested_query_widgets, lengthof(_nested_query_widgets)
1420 * Show a modal confirmation window with standard 'yes' and 'no' buttons
1421 * The window is aligned to the centre of its parent.
1422 * @param caption string shown as window caption
1423 * @param message string that will be shown for the window
1424 * @param parent pointer to parent window, if this pointer is nullptr the parent becomes
1425 * the main window WC_MAIN_WINDOW
1426 * @param callback callback function pointer to set in the window descriptor
1428 void ShowQuery(StringID caption, StringID message, Window *parent, QueryCallback callback)
1430 if (parent == nullptr) parent = FindWindowById(WC_MAIN_WINDOW, 0);
1432 const Window* window;
1433 FOR_ALL_WINDOWS_FROM_BACK(window) {
1434 if (window->window_class != WC_CONFIRM_POPUP_QUERY) continue;
1436 const auto query_window = dynamic_cast<const QueryWindow*>(window);
1438 if (query_window->parent != parent) continue;
1440 delete query_window;
1441 break;
1444 new QueryWindow(&_query_desc, caption, message, parent, callback);
1447 static const NWidgetPart _nested_station_rating_tooltip_widgets[] = {
1448 NWidget(WWT_PANEL, COLOUR_GREY, WID_TT_BACKGROUND), SetMinimalSize(64, 32), EndContainer(),
1451 static WindowDesc _station_rating_tooltip_desc(
1452 WDP_MANUAL, nullptr, 0, 0,
1453 WC_STATION_RATING_TOOLTIP, WC_NONE,
1455 _nested_station_rating_tooltip_widgets, lengthof(_nested_station_rating_tooltip_widgets)
1458 static const int STATION_RATING_AGE[] = { 0, 10, 20, 33 };
1459 static const int STATION_RATING_WAITUNITS[] = { -90, -38, 14, 66, 118, 170 };
1462 struct StationRatingTooltipWindow : public Window
1464 TileType tiletype;
1465 uint16 objIndex;
1466 TooltipCloseCondition close_cond;
1467 const Station *st;
1468 const CargoSpec *cs;
1469 bool newgrf_rating_used;
1471 static const uint RATING_TOOLTIP_LINE_BUFF_SIZE = 512;
1472 static const uint RATING_TOOLTIP_MAX_LINES = 8;
1473 static const uint RATING_TOOLTIP_NEWGRF_INDENT = 20;
1475 public:
1476 char data[RATING_TOOLTIP_MAX_LINES + 1][RATING_TOOLTIP_LINE_BUFF_SIZE];
1478 StationRatingTooltipWindow(Window *parent, const Station *st, const CargoSpec *cs) : Window(&_station_rating_tooltip_desc)
1480 this->parent = parent;
1481 this->st = st;
1482 this->cs = cs;
1483 this->InitNested();
1484 CLRBITS(this->flags, WF_WHITE_BORDER);
1487 virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
1489 int scr_top = GetMainViewTop() + 2;
1490 int scr_bot = GetMainViewBottom() - 2;
1491 Point pt;
1492 pt.y = Clamp(_cursor.pos.y + _cursor.total_size.y + _cursor.total_offs.y + 5, scr_top, scr_bot);
1493 if (pt.y + sm_height > scr_bot) pt.y = min(_cursor.pos.y + _cursor.total_offs.y - 5, scr_bot) - sm_height;
1494 pt.x = sm_width >= _screen.width ? 0 : Clamp(_cursor.pos.x - (sm_width >> 1), 0, _screen.width - sm_width);
1495 return pt;
1498 int RoundRating(int rating) {
1499 return RoundDivSU(rating * 101, 256);
1502 virtual void OnInit() {
1503 const GoodsEntry *ge = &this->st->goods[this->cs->Index()];
1505 SetDParam(0, this->cs->name);
1506 GetString(this->data[0], STR_STATION_RATING_TOOLTIP_RATING_DETAILS, lastof(this->data[0]));
1507 if (!ge->HasRating()) {
1508 this->data[1][0] = '\0';
1509 return;
1512 uint line_nr = 1;
1513 int total_rating = 0;
1515 if (HasBit(cs->callback_mask, CBM_CARGO_STATION_RATING_CALC)) {
1516 uint last_speed = ge->HasVehicleEverTriedLoading() ? ge->last_speed : 0xFF;
1518 uint32 var18 = min(ge->time_since_pickup, 0xFF) | (min(ge->max_waiting_cargo, 0xFFFF) << 8) | (min(last_speed, 0xFF) << 24);
1519 uint32 var10 = (ge->last_vehicle_type == VEH_INVALID) ? 0x0 : (ge->last_vehicle_type + 0x10);
1520 uint16 callback = GetCargoCallback(CBID_CARGO_STATION_RATING_CALC, var10, var18, this->cs);
1521 int newgrf_rating = 0;
1522 if (callback != CALLBACK_FAILED) {
1523 newgrf_rating = GB(callback, 0, 14);
1524 if (HasBit(callback, 14)) newgrf_rating -= 0x4000;
1526 this->newgrf_rating_used = true;
1528 total_rating += newgrf_rating;
1529 newgrf_rating = this->RoundRating(newgrf_rating);
1531 SetDParam(0, STR_STATION_RATING_TOOLTIP_NEWGRF_RATING_0 + (newgrf_rating <= 0 ? 0 : 1));
1532 SetDParam(1, newgrf_rating);
1533 GetString(this->data[line_nr], STR_STATION_RATING_TOOLTIP_NEWGRF_RATING, lastof(this->data[line_nr]));
1534 line_nr++;
1536 SetDParam(0, min(ge->last_unprocessed_speed, 0xFF));
1538 switch (ge->last_vehicle_type)
1540 case VEH_TRAIN:
1541 SetDParam(1, STR_STATION_RATING_TOOLTIP_TRAIN);
1542 break;
1543 case VEH_ROAD:
1544 SetDParam(1, STR_STATION_RATING_TOOLTIP_ROAD_VEHICLE);
1545 break;
1546 case VEH_SHIP:
1547 SetDParam(1, STR_STATION_RATING_TOOLTIP_SHIP);
1548 break;
1549 case VEH_AIRCRAFT:
1550 SetDParam(1, STR_STATION_RATING_TOOLTIP_AIRCRAFT);
1551 break;
1552 default:
1553 SetDParam(1, STR_STATION_RATING_TOOLTIP_INVALID);
1554 break;
1557 GetString(this->data[line_nr], STR_STATION_RATING_TOOLTIP_NEWGRF_SPEED, lastof(this->data[line_nr]));
1558 line_nr++;
1560 SetDParam(0, min(ge->max_waiting_cargo, 0xFFFF));
1561 GetString(this->data[line_nr],
1562 (ge->punishment_in_effect || ge->punishment_triggered) ?
1563 STR_STATION_RATING_TOOLTIP_NEWGRF_WAITUNITS_FEEDBACK :
1564 STR_STATION_RATING_TOOLTIP_NEWGRF_WAITUNITS,
1565 lastof(this->data[line_nr]));
1566 line_nr++;
1568 SetDParam(0, ge->time_since_pickup * STATION_RATING_TICKS / DAY_TICKS);
1569 GetString(this->data[line_nr], STR_STATION_RATING_TOOLTIP_NEWGRF_WAITTIME, lastof(this->data[line_nr]));
1570 line_nr++;
1574 if (!this->newgrf_rating_used) {
1576 uint waitunits = ge->max_waiting_cargo;
1577 int waitunits_stage = 0;
1578 (ge->max_waiting_cargo > 2000) ||
1579 (waitunits_stage = 1, ge->max_waiting_cargo > 1000) ||
1580 (waitunits_stage = 2, ge->max_waiting_cargo > 500) ||
1581 (waitunits_stage = 3, ge->max_waiting_cargo > 250) ||
1582 (waitunits_stage = 4, ge->max_waiting_cargo > 125) ||
1583 (waitunits_stage = 5, true);
1584 total_rating += STATION_RATING_WAITUNITS[waitunits_stage];
1586 SetDParam(0, STR_STATION_RATING_TOOLTIP_WAITUNITS_0 + waitunits_stage);
1587 SetDParam(1, ge->max_waiting_cargo);
1588 SetDParam(2, this->RoundRating(STATION_RATING_WAITUNITS[waitunits_stage]));
1589 GetString(this->data[line_nr],
1590 (ge->punishment_in_effect || ge->punishment_triggered) ? STR_STATION_RATING_TOOLTIP_WAITUNITS_FEEDBACK : STR_STATION_RATING_TOOLTIP_WAITUNITS,
1591 lastof(this->data[line_nr]));
1592 line_nr++;
1594 int b = ge->last_speed - 15;
1595 int r_speed = b >= 0 ? b >> 2 : 0;
1596 int r_speed_round = this->RoundRating(r_speed);
1597 total_rating += r_speed;
1598 if (ge->last_speed == 255) {
1599 SetDParam(0, STR_STATION_RATING_TOOLTIP_SPEED_3);
1601 else if (r_speed_round == 0) {
1602 SetDParam(0, STR_STATION_RATING_TOOLTIP_SPEED_ZERO);
1604 else {
1605 SetDParam(0, STR_STATION_RATING_TOOLTIP_SPEED_0 + min(3, r_speed / 15));
1607 SetDParam(1, ge->last_unprocessed_speed);
1608 SetDParam(2, r_speed_round);
1610 switch (ge->last_vehicle_type)
1612 case VEH_TRAIN:
1613 SetDParam(3, STR_STATION_RATING_TOOLTIP_TRAIN);
1614 break;
1615 case VEH_ROAD:
1616 SetDParam(3, STR_STATION_RATING_TOOLTIP_ROAD_VEHICLE);
1617 break;
1618 case VEH_SHIP:
1619 SetDParam(3, STR_STATION_RATING_TOOLTIP_SHIP);
1620 break;
1621 case VEH_AIRCRAFT:
1622 SetDParam(3, STR_STATION_RATING_TOOLTIP_AIRCRAFT);
1623 break;
1624 default:
1625 SetDParam(3, STR_STATION_RATING_TOOLTIP_INVALID);
1626 break;
1629 GetString(this->data[line_nr], STR_STATION_RATING_TOOLTIP_SPEED, lastof(this->data[line_nr]));
1630 line_nr++;
1633 int age_stage = 0;
1634 (ge->last_age >= 30) ||
1635 (age_stage = 1, ge->last_age >= 20) ||
1636 (age_stage = 2, ge->last_age >= 10) ||
1637 (age_stage = 3, true);
1639 total_rating += STATION_RATING_AGE[age_stage];
1640 SetDParam(0, STR_STATION_RATING_TOOLTIP_AGE_0 + age_stage);
1641 SetDParam(1, ge->last_age);
1642 SetDParam(2, this->RoundRating(STATION_RATING_AGE[age_stage]));
1643 GetString(this->data[line_nr], STR_STATION_RATING_TOOLTIP_AGE, lastof(this->data[line_nr]));
1644 line_nr++;
1646 if (Company::IsValidID(st->owner) && HasBit(st->town->statues, st->owner)) {
1647 SetDParam(0, STR_STATION_RATING_TOOLTIP_STATUE_YES);
1648 total_rating += 26;
1650 else {
1651 SetDParam(0, STR_STATION_RATING_TOOLTIP_STATUE_NO);
1653 GetString(this->data[line_nr], STR_STATION_RATING_TOOLTIP_STATUE, lastof(this->data[line_nr]));
1654 line_nr++;
1656 SetDParam(0, ToPercent8(Clamp(total_rating, 0, 255)));
1657 GetString(this->data[line_nr], STR_STATION_RATING_TOOLTIP_TOTAL_RATING, lastof(this->data[line_nr]));
1658 line_nr++;
1660 this->data[line_nr][0] = '\0';
1663 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1665 if (widget != WID_LI_BACKGROUND) return;
1667 size->height = WD_FRAMETEXT_TOP + WD_FRAMETEXT_BOTTOM + 2;
1668 for (uint i = 0; i <= RATING_TOOLTIP_MAX_LINES; i++) {
1669 if (StrEmpty(this->data[i])) break;
1671 uint width = GetStringBoundingBox(this->data[i]).width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT + 2;
1672 if (this->newgrf_rating_used && i >= 2 && i <= 4)
1673 width += RATING_TOOLTIP_NEWGRF_INDENT;
1674 size->width = max(size->width, width);
1675 size->height += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
1677 size->height -= WD_PAR_VSEP_NORMAL;
1680 virtual void DrawWidget(const Rect &r, int widget) const
1682 uint icon_size = ScaleGUITrad(10);
1683 uint line_height = max((uint)FONT_HEIGHT_NORMAL, icon_size) + 2;
1684 uint text_ofs = (line_height - FONT_HEIGHT_NORMAL) >> 1;
1685 uint icon_ofs = (line_height - icon_size) >> 1;
1687 GfxDrawLine(r.left, r.top, r.right, r.top, PC_BLACK);
1688 GfxDrawLine(r.left, r.bottom, r.right, r.bottom, PC_BLACK);
1689 GfxDrawLine(r.left, r.top, r.left, r.bottom, PC_BLACK);
1690 GfxDrawLine(r.right, r.top, r.right, r.bottom, PC_BLACK);
1692 int y = r.top + WD_FRAMETEXT_TOP + 1;
1693 int left0 = r.left + WD_FRAMETEXT_LEFT + 1;
1694 int right0 = r.right - WD_FRAMETEXT_RIGHT - 1;
1695 DrawString(left0, right0, y, this->data[0], TC_LIGHT_BLUE, SA_CENTER);
1696 y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
1697 for (uint i = 1; i <= RATING_TOOLTIP_MAX_LINES; i++) {
1698 if (StrEmpty(this->data[i])) break;
1699 int left = left0, right = right0;
1700 if (this->newgrf_rating_used && i >= 2 && i <= 4) {
1701 if (_current_text_dir == TD_RTL) {
1702 right -= RATING_TOOLTIP_NEWGRF_INDENT;
1704 else {
1705 left += RATING_TOOLTIP_NEWGRF_INDENT;
1708 DrawString(left, right, y, this->data[i], TC_BLACK);
1709 y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
1713 virtual void OnMouseLoop()
1715 if (!_cursor.in_window || !_mouse_hovering) {
1716 delete this;
1721 void GuiShowStationRatingTooltip(Window *parent, const Station *st, const CargoSpec *cs) {
1722 DeleteWindowById(WC_STATION_RATING_TOOLTIP, 0);
1723 new StationRatingTooltipWindow(parent, st, cs);