Add: INR currency (#8136)
[openttd-github.git] / src / smallmap_gui.cpp
blobc4b650fc22fc45dc1fed655a39554e16a964e2be
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file smallmap_gui.cpp GUI that shows a small map of the world with metadata like owner or height. */
10 #include "stdafx.h"
11 #include "clear_map.h"
12 #include "industry.h"
13 #include "station_map.h"
14 #include "landscape.h"
15 #include "tree_map.h"
16 #include "viewport_func.h"
17 #include "town.h"
18 #include "tunnelbridge_map.h"
19 #include "core/endian_func.hpp"
20 #include "vehicle_base.h"
21 #include "sound_func.h"
22 #include "window_func.h"
23 #include "company_base.h"
24 #include "guitimer_func.h"
26 #include "smallmap_gui.h"
28 #include "table/strings.h"
30 #include <bitset>
32 #include "safeguards.h"
34 static int _smallmap_industry_count; ///< Number of used industries
35 static int _smallmap_company_count; ///< Number of entries in the owner legend.
36 static int _smallmap_cargo_count; ///< Number of cargos in the link stats legend.
38 /** Link stat colours shown in legenda. */
39 static uint8 _linkstat_colours_in_legenda[] = {0, 1, 3, 5, 7, 9, 11};
41 static const int NUM_NO_COMPANY_ENTRIES = 4; ///< Number of entries in the owner legend that are not companies.
43 /** Macro for ordinary entry of LegendAndColour */
44 #define MK(a, b) {a, b, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, false, false}
46 /** Macro for a height legend entry with configurable colour. */
47 #define MC(col_break) {0, STR_TINY_BLACK_HEIGHT, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, false, col_break}
49 /** Macro for non-company owned property entry of LegendAndColour */
50 #define MO(a, b) {a, b, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, false, false}
52 /** Macro used for forcing a rebuild of the owner legend the first time it is used. */
53 #define MOEND() {0, 0, INVALID_INDUSTRYTYPE, 0, OWNER_NONE, true, true, false}
55 /** Macro for end of list marker in arrays of LegendAndColour */
56 #define MKEND() {0, STR_NULL, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, true, false}
58 /**
59 * Macro for break marker in arrays of LegendAndColour.
60 * It will have valid data, though
62 #define MS(a, b) {a, b, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, false, true}
64 /** Legend text giving the colours to look for on the minimap */
65 static LegendAndColour _legend_land_contours[] = {
66 MK(PC_BLACK, STR_SMALLMAP_LEGENDA_ROADS),
67 MK(PC_GREY, STR_SMALLMAP_LEGENDA_RAILROADS),
68 MK(PC_LIGHT_BLUE, STR_SMALLMAP_LEGENDA_STATIONS_AIRPORTS_DOCKS),
69 MK(PC_DARK_RED, STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES),
70 MK(PC_WHITE, STR_SMALLMAP_LEGENDA_VEHICLES),
72 /* Placeholders for the colours and heights of the legend.
73 * The following values are set at BuildLandLegend() based
74 * on each colour scheme and the maximum map height. */
75 MC(true),
76 MC(false),
77 MC(false),
78 MC(false),
79 MC(false),
80 MC(false),
81 MC(true),
82 MC(false),
83 MC(false),
84 MC(false),
85 MC(false),
86 MC(false),
87 MKEND()
90 static const LegendAndColour _legend_vehicles[] = {
91 MK(PC_RED, STR_SMALLMAP_LEGENDA_TRAINS),
92 MK(PC_YELLOW, STR_SMALLMAP_LEGENDA_ROAD_VEHICLES),
93 MK(PC_LIGHT_BLUE, STR_SMALLMAP_LEGENDA_SHIPS),
94 MK(PC_WHITE, STR_SMALLMAP_LEGENDA_AIRCRAFT),
96 MS(PC_BLACK, STR_SMALLMAP_LEGENDA_TRANSPORT_ROUTES),
97 MK(PC_DARK_RED, STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES),
98 MKEND()
101 static const LegendAndColour _legend_routes[] = {
102 MK(PC_BLACK, STR_SMALLMAP_LEGENDA_ROADS),
103 MK(PC_GREY, STR_SMALLMAP_LEGENDA_RAILROADS),
104 MK(PC_DARK_RED, STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES),
106 MS(PC_VERY_DARK_BROWN, STR_SMALLMAP_LEGENDA_RAILROAD_STATION),
107 MK(PC_ORANGE, STR_SMALLMAP_LEGENDA_TRUCK_LOADING_BAY),
108 MK(PC_YELLOW, STR_SMALLMAP_LEGENDA_BUS_STATION),
109 MK(PC_RED, STR_SMALLMAP_LEGENDA_AIRPORT_HELIPORT),
110 MK(PC_LIGHT_BLUE, STR_SMALLMAP_LEGENDA_DOCK),
111 MKEND()
114 static const LegendAndColour _legend_vegetation[] = {
115 MK(PC_ROUGH_LAND, STR_SMALLMAP_LEGENDA_ROUGH_LAND),
116 MK(PC_GRASS_LAND, STR_SMALLMAP_LEGENDA_GRASS_LAND),
117 MK(PC_BARE_LAND, STR_SMALLMAP_LEGENDA_BARE_LAND),
118 MK(PC_FIELDS, STR_SMALLMAP_LEGENDA_FIELDS),
119 MK(PC_TREES, STR_SMALLMAP_LEGENDA_TREES),
120 MK(PC_GREEN, STR_SMALLMAP_LEGENDA_FOREST),
122 MS(PC_GREY, STR_SMALLMAP_LEGENDA_ROCKS),
123 MK(PC_ORANGE, STR_SMALLMAP_LEGENDA_DESERT),
124 MK(PC_LIGHT_BLUE, STR_SMALLMAP_LEGENDA_SNOW),
125 MK(PC_BLACK, STR_SMALLMAP_LEGENDA_TRANSPORT_ROUTES),
126 MK(PC_DARK_RED, STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES),
127 MKEND()
130 static LegendAndColour _legend_land_owners[NUM_NO_COMPANY_ENTRIES + MAX_COMPANIES + 1] = {
131 MO(PC_WATER, STR_SMALLMAP_LEGENDA_WATER),
132 MO(0x00, STR_SMALLMAP_LEGENDA_NO_OWNER), // This colour will vary depending on settings.
133 MO(PC_DARK_RED, STR_SMALLMAP_LEGENDA_TOWNS),
134 MO(PC_DARK_GREY, STR_SMALLMAP_LEGENDA_INDUSTRIES),
135 /* The legend will be terminated the first time it is used. */
136 MOEND(),
139 #undef MK
140 #undef MC
141 #undef MS
142 #undef MO
143 #undef MOEND
144 #undef MKEND
146 /** Legend entries for the link stats view. */
147 static LegendAndColour _legend_linkstats[NUM_CARGO + lengthof(_linkstat_colours_in_legenda) + 1];
149 * Allow room for all industries, plus a terminator entry
150 * This is required in order to have the industry slots all filled up
152 static LegendAndColour _legend_from_industries[NUM_INDUSTRYTYPES + 1];
153 /** For connecting industry type to position in industries list(small map legend) */
154 static uint _industry_to_list_pos[NUM_INDUSTRYTYPES];
155 /** Show heightmap in industry and owner mode of smallmap window. */
156 static bool _smallmap_show_heightmap = false;
157 /** Highlight a specific industry type */
158 static IndustryType _smallmap_industry_highlight = INVALID_INDUSTRYTYPE;
159 /** State of highlight blinking */
160 static bool _smallmap_industry_highlight_state;
161 /** For connecting company ID to position in owner list (small map legend) */
162 static uint _company_to_list_pos[MAX_COMPANIES];
165 * Fills an array for the industries legends.
167 void BuildIndustriesLegend()
169 uint j = 0;
171 /* Add each name */
172 for (IndustryType ind : _sorted_industry_types) {
173 const IndustrySpec *indsp = GetIndustrySpec(ind);
174 if (indsp->enabled) {
175 _legend_from_industries[j].legend = indsp->name;
176 _legend_from_industries[j].colour = indsp->map_colour;
177 _legend_from_industries[j].type = ind;
178 _legend_from_industries[j].show_on_map = true;
179 _legend_from_industries[j].col_break = false;
180 _legend_from_industries[j].end = false;
182 /* Store widget number for this industry type. */
183 _industry_to_list_pos[ind] = j;
184 j++;
187 /* Terminate the list */
188 _legend_from_industries[j].end = true;
190 /* Store number of enabled industries */
191 _smallmap_industry_count = j;
195 * Populate legend table for the link stat view.
197 void BuildLinkStatsLegend()
199 /* Clear the legend */
200 memset(_legend_linkstats, 0, sizeof(_legend_linkstats));
202 uint i = 0;
203 for (; i < _sorted_cargo_specs.size(); ++i) {
204 const CargoSpec *cs = _sorted_cargo_specs[i];
206 _legend_linkstats[i].legend = cs->name;
207 _legend_linkstats[i].colour = cs->legend_colour;
208 _legend_linkstats[i].type = cs->Index();
209 _legend_linkstats[i].show_on_map = true;
212 _legend_linkstats[i].col_break = true;
213 _smallmap_cargo_count = i;
215 for (; i < _smallmap_cargo_count + lengthof(_linkstat_colours_in_legenda); ++i) {
216 _legend_linkstats[i].legend = STR_EMPTY;
217 _legend_linkstats[i].colour = LinkGraphOverlay::LINK_COLOURS[_linkstat_colours_in_legenda[i - _smallmap_cargo_count]];
218 _legend_linkstats[i].show_on_map = true;
221 _legend_linkstats[_smallmap_cargo_count].legend = STR_LINKGRAPH_LEGEND_UNUSED;
222 _legend_linkstats[i - 1].legend = STR_LINKGRAPH_LEGEND_OVERLOADED;
223 _legend_linkstats[(_smallmap_cargo_count + i - 1) / 2].legend = STR_LINKGRAPH_LEGEND_SATURATED;
224 _legend_linkstats[i].end = true;
227 static const LegendAndColour * const _legend_table[] = {
228 _legend_land_contours,
229 _legend_vehicles,
230 _legend_from_industries,
231 _legend_linkstats,
232 _legend_routes,
233 _legend_vegetation,
234 _legend_land_owners,
237 #define MKCOLOUR(x) TO_LE32X(x)
239 #define MKCOLOUR_XXXX(x) (MKCOLOUR(0x01010101) * (uint)(x))
240 #define MKCOLOUR_X0X0(x) (MKCOLOUR(0x01000100) * (uint)(x))
241 #define MKCOLOUR_0X0X(x) (MKCOLOUR(0x00010001) * (uint)(x))
242 #define MKCOLOUR_0XX0(x) (MKCOLOUR(0x00010100) * (uint)(x))
243 #define MKCOLOUR_X00X(x) (MKCOLOUR(0x01000001) * (uint)(x))
245 #define MKCOLOUR_XYXY(x, y) (MKCOLOUR_X0X0(x) | MKCOLOUR_0X0X(y))
246 #define MKCOLOUR_XYYX(x, y) (MKCOLOUR_X00X(x) | MKCOLOUR_0XX0(y))
248 #define MKCOLOUR_0000 MKCOLOUR_XXXX(0x00)
249 #define MKCOLOUR_0FF0 MKCOLOUR_0XX0(0xFF)
250 #define MKCOLOUR_F00F MKCOLOUR_X00X(0xFF)
251 #define MKCOLOUR_FFFF MKCOLOUR_XXXX(0xFF)
253 #include "table/heightmap_colours.h"
255 /** Colour scheme of the smallmap. */
256 struct SmallMapColourScheme {
257 uint32 *height_colours; ///< Cached colours for each level in a map.
258 const uint32 *height_colours_base; ///< Base table for determining the colours
259 size_t colour_count; ///< The number of colours.
260 uint32 default_colour; ///< Default colour of the land.
263 /** Available colour schemes for height maps. */
264 static SmallMapColourScheme _heightmap_schemes[] = {
265 {nullptr, _green_map_heights, lengthof(_green_map_heights), MKCOLOUR_XXXX(0x54)}, ///< Green colour scheme.
266 {nullptr, _dark_green_map_heights, lengthof(_dark_green_map_heights), MKCOLOUR_XXXX(0x62)}, ///< Dark green colour scheme.
267 {nullptr, _violet_map_heights, lengthof(_violet_map_heights), MKCOLOUR_XXXX(0x81)}, ///< Violet colour scheme.
271 * (Re)build the colour tables for the legends.
273 void BuildLandLegend()
275 /* The smallmap window has never been initialized, so no need to change the legend. */
276 if (_heightmap_schemes[0].height_colours == nullptr) return;
279 * The general idea of this function is to fill the legend with an appropriate evenly spaced
280 * selection of height levels. All entries with STR_TINY_BLACK_HEIGHT are reserved for this.
281 * At the moment there are twelve of these.
283 * The table below defines up to which height level a particular delta in the legend should be
284 * used. One could opt for just dividing the maximum height and use that as delta, but that
285 * creates many "ugly" legend labels, e.g. once every 950 meter. As a result, this table will
286 * reduce the number of deltas to 7: every 100m, 200m, 300m, 500m, 750m, 1000m and 1250m. The
287 * deltas are closer together at the lower numbers because going from 12 entries to just 4, as
288 * would happen when replacing 200m and 300m by 250m, would mean the legend would be short and
289 * that might not be considered appropriate.
291 * The current method yields at least 7 legend entries and at most 12. It can be increased to
292 * 8 by adding a 150m and 400m option, but especially 150m creates ugly heights.
294 * It tries to evenly space the legend items over the two columns that are there for the legend.
297 /* Table for delta; if max_height is less than the first column, use the second column as value. */
298 uint deltas[][2] = { { 24, 2 }, { 48, 4 }, { 72, 6 }, { 120, 10 }, { 180, 15 }, { 240, 20 }, { MAX_TILE_HEIGHT + 1, 25 }};
299 uint i = 0;
300 for (; _settings_game.construction.max_heightlevel >= deltas[i][0]; i++) {
301 /* Nothing to do here. */
303 uint delta = deltas[i][1];
305 int total_entries = (_settings_game.construction.max_heightlevel / delta) + 1;
306 int rows = CeilDiv(total_entries, 2);
307 int j = 0;
309 for (i = 0; i < lengthof(_legend_land_contours) - 1 && j < total_entries; i++) {
310 if (_legend_land_contours[i].legend != STR_TINY_BLACK_HEIGHT) continue;
312 _legend_land_contours[i].col_break = j % rows == 0;
313 _legend_land_contours[i].end = false;
314 _legend_land_contours[i].height = j * delta;
315 _legend_land_contours[i].colour = _heightmap_schemes[_settings_client.gui.smallmap_land_colour].height_colours[j * delta];
316 j++;
318 _legend_land_contours[i].end = true;
322 * Completes the array for the owned property legend.
324 void BuildOwnerLegend()
326 _legend_land_owners[1].colour = _heightmap_schemes[_settings_client.gui.smallmap_land_colour].default_colour;
328 int i = NUM_NO_COMPANY_ENTRIES;
329 for (const Company *c : Company::Iterate()) {
330 _legend_land_owners[i].colour = _colour_gradient[c->colour][5];
331 _legend_land_owners[i].company = c->index;
332 _legend_land_owners[i].show_on_map = true;
333 _legend_land_owners[i].col_break = false;
334 _legend_land_owners[i].end = false;
335 _company_to_list_pos[c->index] = i;
336 i++;
339 /* Terminate the list */
340 _legend_land_owners[i].end = true;
342 /* Store maximum amount of owner legend entries. */
343 _smallmap_company_count = i;
346 struct AndOr {
347 uint32 mor;
348 uint32 mand;
351 static inline uint32 ApplyMask(uint32 colour, const AndOr *mask)
353 return (colour & mask->mand) | mask->mor;
357 /** Colour masks for "Contour" and "Routes" modes. */
358 static const AndOr _smallmap_contours_andor[] = {
359 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_CLEAR
360 {MKCOLOUR_0XX0(PC_GREY ), MKCOLOUR_F00F}, // MP_RAILWAY
361 {MKCOLOUR_0XX0(PC_BLACK ), MKCOLOUR_F00F}, // MP_ROAD
362 {MKCOLOUR_0XX0(PC_DARK_RED ), MKCOLOUR_F00F}, // MP_HOUSE
363 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_TREES
364 {MKCOLOUR_XXXX(PC_LIGHT_BLUE), MKCOLOUR_0000}, // MP_STATION
365 {MKCOLOUR_XXXX(PC_WATER ), MKCOLOUR_0000}, // MP_WATER
366 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_VOID
367 {MKCOLOUR_XXXX(PC_DARK_RED ), MKCOLOUR_0000}, // MP_INDUSTRY
368 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_TUNNELBRIDGE
369 {MKCOLOUR_0XX0(PC_DARK_RED ), MKCOLOUR_F00F}, // MP_OBJECT
370 {MKCOLOUR_0XX0(PC_GREY ), MKCOLOUR_F00F},
373 /** Colour masks for "Vehicles", "Industry", and "Vegetation" modes. */
374 static const AndOr _smallmap_vehicles_andor[] = {
375 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_CLEAR
376 {MKCOLOUR_0XX0(PC_BLACK ), MKCOLOUR_F00F}, // MP_RAILWAY
377 {MKCOLOUR_0XX0(PC_BLACK ), MKCOLOUR_F00F}, // MP_ROAD
378 {MKCOLOUR_0XX0(PC_DARK_RED ), MKCOLOUR_F00F}, // MP_HOUSE
379 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_TREES
380 {MKCOLOUR_0XX0(PC_BLACK ), MKCOLOUR_F00F}, // MP_STATION
381 {MKCOLOUR_XXXX(PC_WATER ), MKCOLOUR_0000}, // MP_WATER
382 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_VOID
383 {MKCOLOUR_XXXX(PC_DARK_RED ), MKCOLOUR_0000}, // MP_INDUSTRY
384 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_TUNNELBRIDGE
385 {MKCOLOUR_0XX0(PC_DARK_RED ), MKCOLOUR_F00F}, // MP_OBJECT
386 {MKCOLOUR_0XX0(PC_BLACK ), MKCOLOUR_F00F},
389 /** Mapping of tile type to importance of the tile (higher number means more interesting to show). */
390 static const byte _tiletype_importance[] = {
391 2, // MP_CLEAR
392 8, // MP_RAILWAY
393 7, // MP_ROAD
394 5, // MP_HOUSE
395 2, // MP_TREES
396 9, // MP_STATION
397 2, // MP_WATER
398 1, // MP_VOID
399 6, // MP_INDUSTRY
400 8, // MP_TUNNELBRIDGE
401 2, // MP_OBJECT
407 * Return the colour a tile would be displayed with in the small map in mode "Contour".
408 * @param tile The tile of which we would like to get the colour.
409 * @param t Effective tile type of the tile (see #SmallMapWindow::GetTileColours).
410 * @return The colour of tile in the small map in mode "Contour"
412 static inline uint32 GetSmallMapContoursPixels(TileIndex tile, TileType t)
414 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
415 return ApplyMask(cs->height_colours[TileHeight(tile)], &_smallmap_contours_andor[t]);
419 * Return the colour a tile would be displayed with in the small map in mode "Vehicles".
421 * @param tile The tile of which we would like to get the colour.
422 * @param t Effective tile type of the tile (see #SmallMapWindow::GetTileColours).
423 * @return The colour of tile in the small map in mode "Vehicles"
425 static inline uint32 GetSmallMapVehiclesPixels(TileIndex tile, TileType t)
427 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
428 return ApplyMask(cs->default_colour, &_smallmap_vehicles_andor[t]);
432 * Return the colour a tile would be displayed with in the small map in mode "Industries".
434 * @param tile The tile of which we would like to get the colour.
435 * @param t Effective tile type of the tile (see #SmallMapWindow::GetTileColours).
436 * @return The colour of tile in the small map in mode "Industries"
438 static inline uint32 GetSmallMapIndustriesPixels(TileIndex tile, TileType t)
440 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
441 return ApplyMask(_smallmap_show_heightmap ? cs->height_colours[TileHeight(tile)] : cs->default_colour, &_smallmap_vehicles_andor[t]);
445 * Return the colour a tile would be displayed with in the small map in mode "Routes".
447 * @param tile The tile of which we would like to get the colour.
448 * @param t Effective tile type of the tile (see #SmallMapWindow::GetTileColours).
449 * @return The colour of tile in the small map in mode "Routes"
451 static inline uint32 GetSmallMapRoutesPixels(TileIndex tile, TileType t)
453 switch (t) {
454 case MP_STATION:
455 switch (GetStationType(tile)) {
456 case STATION_RAIL: return MKCOLOUR_XXXX(PC_VERY_DARK_BROWN);
457 case STATION_AIRPORT: return MKCOLOUR_XXXX(PC_RED);
458 case STATION_TRUCK: return MKCOLOUR_XXXX(PC_ORANGE);
459 case STATION_BUS: return MKCOLOUR_XXXX(PC_YELLOW);
460 case STATION_DOCK: return MKCOLOUR_XXXX(PC_LIGHT_BLUE);
461 default: return MKCOLOUR_FFFF;
464 case MP_RAILWAY: {
465 AndOr andor = {
466 MKCOLOUR_0XX0(GetRailTypeInfo(GetRailType(tile))->map_colour),
467 _smallmap_contours_andor[t].mand
470 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
471 return ApplyMask(cs->default_colour, &andor);
474 case MP_ROAD: {
475 const RoadTypeInfo *rti = nullptr;
476 if (GetRoadTypeRoad(tile) != INVALID_ROADTYPE) {
477 rti = GetRoadTypeInfo(GetRoadTypeRoad(tile));
478 } else {
479 rti = GetRoadTypeInfo(GetRoadTypeTram(tile));
481 if (rti != nullptr) {
482 AndOr andor = {
483 MKCOLOUR_0XX0(rti->map_colour),
484 _smallmap_contours_andor[t].mand
487 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
488 return ApplyMask(cs->default_colour, &andor);
490 FALLTHROUGH;
493 default:
494 /* Ground colour */
495 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
496 return ApplyMask(cs->default_colour, &_smallmap_contours_andor[t]);
501 * Return the colour a tile would be displayed with in the small map in mode "link stats".
503 * @param tile The tile of which we would like to get the colour.
504 * @param t Effective tile type of the tile (see #SmallMapWindow::GetTileColours).
505 * @return The colour of tile in the small map in mode "link stats"
507 static inline uint32 GetSmallMapLinkStatsPixels(TileIndex tile, TileType t)
509 return _smallmap_show_heightmap ? GetSmallMapContoursPixels(tile, t) : GetSmallMapRoutesPixels(tile, t);
512 static const uint32 _vegetation_clear_bits[] = {
513 MKCOLOUR_XXXX(PC_GRASS_LAND), ///< full grass
514 MKCOLOUR_XXXX(PC_ROUGH_LAND), ///< rough land
515 MKCOLOUR_XXXX(PC_GREY), ///< rocks
516 MKCOLOUR_XXXX(PC_FIELDS), ///< fields
517 MKCOLOUR_XXXX(PC_LIGHT_BLUE), ///< snow
518 MKCOLOUR_XXXX(PC_ORANGE), ///< desert
519 MKCOLOUR_XXXX(PC_GRASS_LAND), ///< unused
520 MKCOLOUR_XXXX(PC_GRASS_LAND), ///< unused
524 * Return the colour a tile would be displayed with in the smallmap in mode "Vegetation".
526 * @param tile The tile of which we would like to get the colour.
527 * @param t Effective tile type of the tile (see #SmallMapWindow::GetTileColours).
528 * @return The colour of tile in the smallmap in mode "Vegetation"
530 static inline uint32 GetSmallMapVegetationPixels(TileIndex tile, TileType t)
532 switch (t) {
533 case MP_CLEAR:
534 return (IsClearGround(tile, CLEAR_GRASS) && GetClearDensity(tile) < 3) ? MKCOLOUR_XXXX(PC_BARE_LAND) : _vegetation_clear_bits[GetClearGround(tile)];
536 case MP_INDUSTRY:
537 return IsTileForestIndustry(tile) ? MKCOLOUR_XXXX(PC_GREEN) : MKCOLOUR_XXXX(PC_DARK_RED);
539 case MP_TREES:
540 if (GetTreeGround(tile) == TREE_GROUND_SNOW_DESERT || GetTreeGround(tile) == TREE_GROUND_ROUGH_SNOW) {
541 return (_settings_game.game_creation.landscape == LT_ARCTIC) ? MKCOLOUR_XYYX(PC_LIGHT_BLUE, PC_TREES) : MKCOLOUR_XYYX(PC_ORANGE, PC_TREES);
543 return MKCOLOUR_XYYX(PC_GRASS_LAND, PC_TREES);
545 default:
546 return ApplyMask(MKCOLOUR_XXXX(PC_GRASS_LAND), &_smallmap_vehicles_andor[t]);
551 * Return the colour a tile would be displayed with in the small map in mode "Owner".
553 * @param tile The tile of which we would like to get the colour.
554 * @param t Effective tile type of the tile (see #SmallMapWindow::GetTileColours).
555 * @return The colour of tile in the small map in mode "Owner"
557 static inline uint32 GetSmallMapOwnerPixels(TileIndex tile, TileType t)
559 Owner o;
561 switch (t) {
562 case MP_INDUSTRY: return MKCOLOUR_XXXX(PC_DARK_GREY);
563 case MP_HOUSE: return MKCOLOUR_XXXX(PC_DARK_RED);
564 default: o = GetTileOwner(tile); break;
565 /* FIXME: For MP_ROAD there are multiple owners.
566 * GetTileOwner returns the rail owner (level crossing) resp. the owner of ROADTYPE_ROAD (normal road),
567 * even if there are no ROADTYPE_ROAD bits on the tile.
571 if ((o < MAX_COMPANIES && !_legend_land_owners[_company_to_list_pos[o]].show_on_map) || o == OWNER_NONE || o == OWNER_WATER) {
572 if (t == MP_WATER) return MKCOLOUR_XXXX(PC_WATER);
573 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
574 return _smallmap_show_heightmap ? cs->height_colours[TileHeight(tile)] : cs->default_colour;
575 } else if (o == OWNER_TOWN) {
576 return MKCOLOUR_XXXX(PC_DARK_RED);
579 return MKCOLOUR_XXXX(_legend_land_owners[_company_to_list_pos[o]].colour);
582 /** Vehicle colours in #SMT_VEHICLES mode. Indexed by #VehicleType. */
583 static const byte _vehicle_type_colours[6] = {
584 PC_RED, PC_YELLOW, PC_LIGHT_BLUE, PC_WHITE, PC_BLACK, PC_RED
588 /** Notify the industry chain window to stop sending newly selected industries. */
589 /* static */ void SmallMapWindow::BreakIndustryChainLink()
591 InvalidateWindowClassesData(WC_INDUSTRY_CARGOES, NUM_INDUSTRYTYPES);
594 inline Point SmallMapWindow::SmallmapRemapCoords(int x, int y) const
596 Point pt;
597 pt.x = (y - x) * 2;
598 pt.y = y + x;
599 return pt;
603 * Remap tile to location on this smallmap.
604 * @param tile_x X coordinate of the tile.
605 * @param tile_y Y coordinate of the tile.
606 * @return Position to draw on.
608 inline Point SmallMapWindow::RemapTile(int tile_x, int tile_y) const
610 int x_offset = tile_x - this->scroll_x / (int)TILE_SIZE;
611 int y_offset = tile_y - this->scroll_y / (int)TILE_SIZE;
613 if (this->zoom == 1) return SmallmapRemapCoords(x_offset, y_offset);
615 /* For negative offsets, round towards -inf. */
616 if (x_offset < 0) x_offset -= this->zoom - 1;
617 if (y_offset < 0) y_offset -= this->zoom - 1;
619 return SmallmapRemapCoords(x_offset / this->zoom, y_offset / this->zoom);
623 * Determine the tile relative to the base tile of the smallmap, and the pixel position at
624 * that tile for a point in the smallmap.
625 * @param px Horizontal coordinate of the pixel.
626 * @param py Vertical coordinate of the pixel.
627 * @param[out] sub Pixel position at the tile (0..3).
628 * @param add_sub Add current #subscroll to the position.
629 * @return Tile being displayed at the given position relative to #scroll_x and #scroll_y.
630 * @note The #subscroll offset is already accounted for.
632 inline Point SmallMapWindow::PixelToTile(int px, int py, int *sub, bool add_sub) const
634 if (add_sub) px += this->subscroll; // Total horizontal offset.
636 /* For each two rows down, add a x and a y tile, and
637 * For each four pixels to the right, move a tile to the right. */
638 Point pt = {((py >> 1) - (px >> 2)) * this->zoom, ((py >> 1) + (px >> 2)) * this->zoom};
639 px &= 3;
641 if (py & 1) { // Odd number of rows, handle the 2 pixel shift.
642 if (px < 2) {
643 pt.x += this->zoom;
644 px += 2;
645 } else {
646 pt.y += this->zoom;
647 px -= 2;
651 *sub = px;
652 return pt;
656 * Compute base parameters of the smallmap such that tile (\a tx, \a ty) starts at pixel (\a x, \a y).
657 * @param tx Tile x coordinate.
658 * @param ty Tile y coordinate.
659 * @param x Non-negative horizontal position in the display where the tile starts.
660 * @param y Non-negative vertical position in the display where the tile starts.
661 * @param[out] sub Value of #subscroll needed.
662 * @return #scroll_x, #scroll_y values.
664 Point SmallMapWindow::ComputeScroll(int tx, int ty, int x, int y, int *sub)
666 assert(x >= 0 && y >= 0);
668 int new_sub;
669 Point tile_xy = PixelToTile(x, y, &new_sub, false);
670 tx -= tile_xy.x;
671 ty -= tile_xy.y;
673 Point scroll;
674 if (new_sub == 0) {
675 *sub = 0;
676 scroll.x = (tx + this->zoom) * TILE_SIZE;
677 scroll.y = (ty - this->zoom) * TILE_SIZE;
678 } else {
679 *sub = 4 - new_sub;
680 scroll.x = (tx + 2 * this->zoom) * TILE_SIZE;
681 scroll.y = (ty - 2 * this->zoom) * TILE_SIZE;
683 return scroll;
687 * Initialize or change the zoom level.
688 * @param change Way to change the zoom level.
689 * @param zoom_pt Position to keep fixed while zooming.
690 * @pre \c *zoom_pt should contain a point in the smallmap display when zooming in or out.
692 void SmallMapWindow::SetZoomLevel(ZoomLevelChange change, const Point *zoom_pt)
694 static const int zoomlevels[] = {1, 2, 4, 6, 8}; // Available zoom levels. Bigger number means more zoom-out (further away).
695 static const int MIN_ZOOM_INDEX = 0;
696 static const int MAX_ZOOM_INDEX = lengthof(zoomlevels) - 1;
698 int new_index, cur_index, sub;
699 Point tile;
700 switch (change) {
701 case ZLC_INITIALIZE:
702 cur_index = - 1; // Definitely different from new_index.
703 new_index = MIN_ZOOM_INDEX;
704 tile.x = tile.y = 0;
705 break;
707 case ZLC_ZOOM_IN:
708 case ZLC_ZOOM_OUT:
709 for (cur_index = MIN_ZOOM_INDEX; cur_index <= MAX_ZOOM_INDEX; cur_index++) {
710 if (this->zoom == zoomlevels[cur_index]) break;
712 assert(cur_index <= MAX_ZOOM_INDEX);
714 tile = this->PixelToTile(zoom_pt->x, zoom_pt->y, &sub);
715 new_index = Clamp(cur_index + ((change == ZLC_ZOOM_IN) ? -1 : 1), MIN_ZOOM_INDEX, MAX_ZOOM_INDEX);
716 break;
718 default: NOT_REACHED();
721 if (new_index != cur_index) {
722 this->zoom = zoomlevels[new_index];
723 if (cur_index >= 0) {
724 Point new_tile = this->PixelToTile(zoom_pt->x, zoom_pt->y, &sub);
725 this->SetNewScroll(this->scroll_x + (tile.x - new_tile.x) * TILE_SIZE,
726 this->scroll_y + (tile.y - new_tile.y) * TILE_SIZE, sub);
727 } else if (this->map_type == SMT_LINKSTATS) {
728 this->overlay->SetDirty();
730 this->SetWidgetDisabledState(WID_SM_ZOOM_IN, this->zoom == zoomlevels[MIN_ZOOM_INDEX]);
731 this->SetWidgetDisabledState(WID_SM_ZOOM_OUT, this->zoom == zoomlevels[MAX_ZOOM_INDEX]);
732 this->SetDirty();
737 * Decide which colours to show to the user for a group of tiles.
738 * @param ta Tile area to investigate.
739 * @return Colours to display.
741 inline uint32 SmallMapWindow::GetTileColours(const TileArea &ta) const
743 int importance = 0;
744 TileIndex tile = INVALID_TILE; // Position of the most important tile.
745 TileType et = MP_VOID; // Effective tile type at that position.
747 TILE_AREA_LOOP(ti, ta) {
748 TileType ttype = GetTileType(ti);
750 switch (ttype) {
751 case MP_TUNNELBRIDGE: {
752 TransportType tt = GetTunnelBridgeTransportType(ti);
754 switch (tt) {
755 case TRANSPORT_RAIL: ttype = MP_RAILWAY; break;
756 case TRANSPORT_ROAD: ttype = MP_ROAD; break;
757 default: ttype = MP_WATER; break;
759 break;
762 case MP_INDUSTRY:
763 /* Special handling of industries while in "Industries" smallmap view. */
764 if (this->map_type == SMT_INDUSTRY) {
765 /* If industry is allowed to be seen, use its colour on the map.
766 * This has the highest priority above any value in _tiletype_importance. */
767 IndustryType type = Industry::GetByTile(ti)->type;
768 if (_legend_from_industries[_industry_to_list_pos[type]].show_on_map) {
769 if (type == _smallmap_industry_highlight) {
770 if (_smallmap_industry_highlight_state) return MKCOLOUR_XXXX(PC_WHITE);
771 } else {
772 return GetIndustrySpec(type)->map_colour * 0x01010101;
775 /* Otherwise make it disappear */
776 ttype = IsTileOnWater(ti) ? MP_WATER : MP_CLEAR;
778 break;
780 default:
781 break;
784 if (_tiletype_importance[ttype] > importance) {
785 importance = _tiletype_importance[ttype];
786 tile = ti;
787 et = ttype;
791 switch (this->map_type) {
792 case SMT_CONTOUR:
793 return GetSmallMapContoursPixels(tile, et);
795 case SMT_VEHICLES:
796 return GetSmallMapVehiclesPixels(tile, et);
798 case SMT_INDUSTRY:
799 return GetSmallMapIndustriesPixels(tile, et);
801 case SMT_LINKSTATS:
802 return GetSmallMapLinkStatsPixels(tile, et);
804 case SMT_ROUTES:
805 return GetSmallMapRoutesPixels(tile, et);
807 case SMT_VEGETATION:
808 return GetSmallMapVegetationPixels(tile, et);
810 case SMT_OWNER:
811 return GetSmallMapOwnerPixels(tile, et);
813 default: NOT_REACHED();
818 * Draws one column of tiles of the small map in a certain mode onto the screen buffer, skipping the shifted rows in between.
820 * @param dst Pointer to a part of the screen buffer to write to.
821 * @param xc The X coordinate of the first tile in the column.
822 * @param yc The Y coordinate of the first tile in the column
823 * @param pitch Number of pixels to advance in the screen buffer each time a pixel is written.
824 * @param reps Number of lines to draw
825 * @param start_pos Position of first pixel to draw.
826 * @param end_pos Position of last pixel to draw (exclusive).
827 * @param blitter current blitter
828 * @note If pixel position is below \c 0, skip drawing.
830 void SmallMapWindow::DrawSmallMapColumn(void *dst, uint xc, uint yc, int pitch, int reps, int start_pos, int end_pos, Blitter *blitter) const
832 void *dst_ptr_abs_end = blitter->MoveTo(_screen.dst_ptr, 0, _screen.height);
833 uint min_xy = _settings_game.construction.freeform_edges ? 1 : 0;
835 do {
836 /* Check if the tile (xc,yc) is within the map range */
837 if (xc >= MapMaxX() || yc >= MapMaxY()) continue;
839 /* Check if the dst pointer points to a pixel inside the screen buffer */
840 if (dst < _screen.dst_ptr) continue;
841 if (dst >= dst_ptr_abs_end) continue;
843 /* Construct tilearea covered by (xc, yc, xc + this->zoom, yc + this->zoom) such that it is within min_xy limits. */
844 TileArea ta;
845 if (min_xy == 1 && (xc == 0 || yc == 0)) {
846 if (this->zoom == 1) continue; // The tile area is empty, don't draw anything.
848 ta = TileArea(TileXY(max(min_xy, xc), max(min_xy, yc)), this->zoom - (xc == 0), this->zoom - (yc == 0));
849 } else {
850 ta = TileArea(TileXY(xc, yc), this->zoom, this->zoom);
852 ta.ClampToMap(); // Clamp to map boundaries (may contain MP_VOID tiles!).
854 uint32 val = this->GetTileColours(ta);
855 uint8 *val8 = (uint8 *)&val;
856 int idx = max(0, -start_pos);
857 for (int pos = max(0, start_pos); pos < end_pos; pos++) {
858 blitter->SetPixel(dst, idx, 0, val8[idx]);
859 idx++;
861 /* Switch to next tile in the column */
862 } while (xc += this->zoom, yc += this->zoom, dst = blitter->MoveTo(dst, pitch, 0), --reps != 0);
866 * Adds vehicles to the smallmap.
867 * @param dpi the part of the smallmap to be drawn into
868 * @param blitter current blitter
870 void SmallMapWindow::DrawVehicles(const DrawPixelInfo *dpi, Blitter *blitter) const
872 for (const Vehicle *v : Vehicle::Iterate()) {
873 if (v->type == VEH_EFFECT) continue;
874 if (v->vehstatus & (VS_HIDDEN | VS_UNCLICKABLE)) continue;
876 /* Remap into flat coordinates. */
877 Point pt = this->RemapTile(v->x_pos / (int)TILE_SIZE, v->y_pos / (int)TILE_SIZE);
879 int y = pt.y - dpi->top;
880 if (!IsInsideMM(y, 0, dpi->height)) continue; // y is out of bounds.
882 bool skip = false; // Default is to draw both pixels.
883 int x = pt.x - this->subscroll - 3 - dpi->left; // Offset X coordinate.
884 if (x < 0) {
885 /* if x+1 is 0, that means we're on the very left edge,
886 * and should thus only draw a single pixel */
887 if (++x != 0) continue;
888 skip = true;
889 } else if (x >= dpi->width - 1) {
890 /* Check if we're at the very right edge, and if so draw only a single pixel */
891 if (x != dpi->width - 1) continue;
892 skip = true;
895 /* Calculate pointer to pixel and the colour */
896 byte colour = (this->map_type == SMT_VEHICLES) ? _vehicle_type_colours[v->type] : PC_WHITE;
898 /* And draw either one or two pixels depending on clipping */
899 blitter->SetPixel(dpi->dst_ptr, x, y, colour);
900 if (!skip) blitter->SetPixel(dpi->dst_ptr, x + 1, y, colour);
905 * Adds town names to the smallmap.
906 * @param dpi the part of the smallmap to be drawn into
908 void SmallMapWindow::DrawTowns(const DrawPixelInfo *dpi) const
910 for (const Town *t : Town::Iterate()) {
911 /* Remap the town coordinate */
912 Point pt = this->RemapTile(TileX(t->xy), TileY(t->xy));
913 int x = pt.x - this->subscroll - (t->cache.sign.width_small >> 1);
914 int y = pt.y;
916 /* Check if the town sign is within bounds */
917 if (x + t->cache.sign.width_small > dpi->left &&
918 x < dpi->left + dpi->width &&
919 y + FONT_HEIGHT_SMALL > dpi->top &&
920 y < dpi->top + dpi->height) {
921 /* And draw it. */
922 SetDParam(0, t->index);
923 DrawString(x, x + t->cache.sign.width_small, y, STR_SMALLMAP_TOWN);
929 * Adds map indicators to the smallmap.
931 void SmallMapWindow::DrawMapIndicators() const
933 /* Find main viewport. */
934 const ViewPort *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
936 Point upper_left_smallmap_coord = InverseRemapCoords2(vp->virtual_left, vp->virtual_top);
937 Point lower_right_smallmap_coord = InverseRemapCoords2(vp->virtual_left + vp->virtual_width - 1, vp->virtual_top + vp->virtual_height - 1);
939 Point upper_left = this->RemapTile(upper_left_smallmap_coord.x / (int)TILE_SIZE, upper_left_smallmap_coord.y / (int)TILE_SIZE);
940 upper_left.x -= this->subscroll;
942 Point lower_right = this->RemapTile(lower_right_smallmap_coord.x / (int)TILE_SIZE, lower_right_smallmap_coord.y / (int)TILE_SIZE);
943 lower_right.x -= this->subscroll;
945 SmallMapWindow::DrawVertMapIndicator(upper_left.x, upper_left.y, lower_right.y);
946 SmallMapWindow::DrawVertMapIndicator(lower_right.x, upper_left.y, lower_right.y);
948 SmallMapWindow::DrawHorizMapIndicator(upper_left.x, lower_right.x, upper_left.y);
949 SmallMapWindow::DrawHorizMapIndicator(upper_left.x, lower_right.x, lower_right.y);
953 * Draws the small map.
955 * Basically, the small map is draw column of pixels by column of pixels. The pixels
956 * are drawn directly into the screen buffer. The final map is drawn in multiple passes.
957 * The passes are:
958 * <ol><li>The colours of tiles in the different modes.</li>
959 * <li>Town names (optional)</li></ol>
961 * @param dpi pointer to pixel to write onto
963 void SmallMapWindow::DrawSmallMap(DrawPixelInfo *dpi) const
965 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
966 DrawPixelInfo *old_dpi;
968 old_dpi = _cur_dpi;
969 _cur_dpi = dpi;
971 /* Clear it */
972 GfxFillRect(dpi->left, dpi->top, dpi->left + dpi->width - 1, dpi->top + dpi->height - 1, PC_BLACK);
974 /* Which tile is displayed at (dpi->left, dpi->top)? */
975 int dx;
976 Point tile = this->PixelToTile(dpi->left, dpi->top, &dx);
977 int tile_x = this->scroll_x / (int)TILE_SIZE + tile.x;
978 int tile_y = this->scroll_y / (int)TILE_SIZE + tile.y;
980 void *ptr = blitter->MoveTo(dpi->dst_ptr, -dx - 4, 0);
981 int x = - dx - 4;
982 int y = 0;
984 for (;;) {
985 /* Distance from left edge */
986 if (x >= -3) {
987 if (x >= dpi->width) break; // Exit the loop.
989 int end_pos = min(dpi->width, x + 4);
990 int reps = (dpi->height - y + 1) / 2; // Number of lines.
991 if (reps > 0) {
992 this->DrawSmallMapColumn(ptr, tile_x, tile_y, dpi->pitch * 2, reps, x, end_pos, blitter);
996 if (y == 0) {
997 tile_y += this->zoom;
998 y++;
999 ptr = blitter->MoveTo(ptr, 0, 1);
1000 } else {
1001 tile_x -= this->zoom;
1002 y--;
1003 ptr = blitter->MoveTo(ptr, 0, -1);
1005 ptr = blitter->MoveTo(ptr, 2, 0);
1006 x += 2;
1009 /* Draw vehicles */
1010 if (this->map_type == SMT_CONTOUR || this->map_type == SMT_VEHICLES) this->DrawVehicles(dpi, blitter);
1012 /* Draw link stat overlay */
1013 if (this->map_type == SMT_LINKSTATS) this->overlay->Draw(dpi);
1015 /* Draw town names */
1016 if (this->show_towns) this->DrawTowns(dpi);
1018 /* Draw map indicators */
1019 this->DrawMapIndicators();
1021 _cur_dpi = old_dpi;
1025 * Function to set up widgets depending on the information being shown on the smallmap.
1027 void SmallMapWindow::SetupWidgetData()
1029 StringID legend_tooltip;
1030 StringID enable_all_tooltip;
1031 StringID disable_all_tooltip;
1032 int plane;
1033 switch (this->map_type) {
1034 case SMT_INDUSTRY:
1035 legend_tooltip = STR_SMALLMAP_TOOLTIP_INDUSTRY_SELECTION;
1036 enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_INDUSTRIES;
1037 disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_INDUSTRIES;
1038 plane = 0;
1039 break;
1041 case SMT_OWNER:
1042 legend_tooltip = STR_SMALLMAP_TOOLTIP_COMPANY_SELECTION;
1043 enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_COMPANIES;
1044 disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_COMPANIES;
1045 plane = 0;
1046 break;
1048 case SMT_LINKSTATS:
1049 legend_tooltip = STR_SMALLMAP_TOOLTIP_CARGO_SELECTION;
1050 enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_CARGOS;
1051 disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_CARGOS;
1052 plane = 0;
1053 break;
1055 default:
1056 legend_tooltip = STR_NULL;
1057 enable_all_tooltip = STR_NULL;
1058 disable_all_tooltip = STR_NULL;
1059 plane = 1;
1060 break;
1063 this->GetWidget<NWidgetCore>(WID_SM_LEGEND)->SetDataTip(STR_NULL, legend_tooltip);
1064 this->GetWidget<NWidgetCore>(WID_SM_ENABLE_ALL)->SetDataTip(STR_SMALLMAP_ENABLE_ALL, enable_all_tooltip);
1065 this->GetWidget<NWidgetCore>(WID_SM_DISABLE_ALL)->SetDataTip(STR_SMALLMAP_DISABLE_ALL, disable_all_tooltip);
1066 this->GetWidget<NWidgetStacked>(WID_SM_SELECT_BUTTONS)->SetDisplayedPlane(plane);
1069 SmallMapWindow::SmallMapWindow(WindowDesc *desc, int window_number) : Window(desc), refresh(GUITimer(FORCE_REFRESH_PERIOD))
1071 _smallmap_industry_highlight = INVALID_INDUSTRYTYPE;
1072 this->overlay = new LinkGraphOverlay(this, WID_SM_MAP, 0, this->GetOverlayCompanyMask(), 1);
1073 this->InitNested(window_number);
1074 this->LowerWidget(this->map_type + WID_SM_CONTOUR);
1076 this->RebuildColourIndexIfNecessary();
1078 this->SetWidgetLoweredState(WID_SM_SHOW_HEIGHT, _smallmap_show_heightmap);
1080 this->SetWidgetLoweredState(WID_SM_TOGGLETOWNNAME, this->show_towns);
1082 this->SetupWidgetData();
1084 this->SetZoomLevel(ZLC_INITIALIZE, nullptr);
1085 this->SmallMapCenterOnCurrentPos();
1086 this->SetOverlayCargoMask();
1089 SmallMapWindow::~SmallMapWindow()
1091 delete this->overlay;
1092 this->BreakIndustryChainLink();
1096 * Rebuilds the colour indices used for fast access to the smallmap contour colours based on the heightlevel.
1098 void SmallMapWindow::RebuildColourIndexIfNecessary()
1100 /* Rebuild colour indices if necessary. */
1101 if (SmallMapWindow::max_heightlevel == _settings_game.construction.max_heightlevel) return;
1103 for (uint n = 0; n < lengthof(_heightmap_schemes); n++) {
1104 /* The heights go from 0 up to and including maximum. */
1105 int heights = _settings_game.construction.max_heightlevel + 1;
1106 _heightmap_schemes[n].height_colours = ReallocT<uint32>(_heightmap_schemes[n].height_colours, heights);
1108 for (int z = 0; z < heights; z++) {
1109 size_t access_index = (_heightmap_schemes[n].colour_count * z) / heights;
1111 /* Choose colour by mapping the range (0..max heightlevel) on the complete colour table. */
1112 _heightmap_schemes[n].height_colours[z] = _heightmap_schemes[n].height_colours_base[access_index];
1116 SmallMapWindow::max_heightlevel = _settings_game.construction.max_heightlevel;
1117 BuildLandLegend();
1120 /* virtual */ void SmallMapWindow::SetStringParameters(int widget) const
1122 switch (widget) {
1123 case WID_SM_CAPTION:
1124 SetDParam(0, STR_SMALLMAP_TYPE_CONTOURS + this->map_type);
1125 break;
1129 /* virtual */ void SmallMapWindow::OnInit()
1131 uint min_width = 0;
1132 this->min_number_of_columns = INDUSTRY_MIN_NUMBER_OF_COLUMNS;
1133 this->min_number_of_fixed_rows = lengthof(_linkstat_colours_in_legenda);
1134 for (uint i = 0; i < lengthof(_legend_table); i++) {
1135 uint height = 0;
1136 uint num_columns = 1;
1137 for (const LegendAndColour *tbl = _legend_table[i]; !tbl->end; ++tbl) {
1138 StringID str;
1139 if (i == SMT_INDUSTRY) {
1140 SetDParam(0, tbl->legend);
1141 SetDParam(1, IndustryPool::MAX_SIZE);
1142 str = STR_SMALLMAP_INDUSTRY;
1143 } else if (i == SMT_LINKSTATS) {
1144 SetDParam(0, tbl->legend);
1145 str = STR_SMALLMAP_LINKSTATS;
1146 } else if (i == SMT_OWNER) {
1147 if (tbl->company != INVALID_COMPANY) {
1148 if (!Company::IsValidID(tbl->company)) {
1149 /* Rebuild the owner legend. */
1150 BuildOwnerLegend();
1151 this->OnInit();
1152 return;
1154 /* Non-fixed legend entries for the owner view. */
1155 SetDParam(0, tbl->company);
1156 str = STR_SMALLMAP_COMPANY;
1157 } else {
1158 str = tbl->legend;
1160 } else {
1161 if (tbl->col_break) {
1162 this->min_number_of_fixed_rows = max(this->min_number_of_fixed_rows, height);
1163 height = 0;
1164 num_columns++;
1166 height++;
1167 str = tbl->legend;
1169 min_width = max(GetStringBoundingBox(str).width, min_width);
1171 this->min_number_of_fixed_rows = max(this->min_number_of_fixed_rows, height);
1172 this->min_number_of_columns = max(this->min_number_of_columns, num_columns);
1175 /* The width of a column is the minimum width of all texts + the size of the blob + some spacing */
1176 this->column_width = min_width + LEGEND_BLOB_WIDTH + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
1179 /* virtual */ void SmallMapWindow::OnPaint()
1181 if (this->map_type == SMT_OWNER) {
1182 for (const LegendAndColour *tbl = _legend_table[this->map_type]; !tbl->end; ++tbl) {
1183 if (tbl->company != INVALID_COMPANY && !Company::IsValidID(tbl->company)) {
1184 /* Rebuild the owner legend. */
1185 BuildOwnerLegend();
1186 this->InvalidateData(1);
1187 break;
1192 this->DrawWidgets();
1195 /* virtual */ void SmallMapWindow::DrawWidget(const Rect &r, int widget) const
1197 switch (widget) {
1198 case WID_SM_MAP: {
1199 DrawPixelInfo new_dpi;
1200 if (!FillDrawPixelInfo(&new_dpi, r.left + 1, r.top + 1, r.right - r.left - 1, r.bottom - r.top - 1)) return;
1201 this->DrawSmallMap(&new_dpi);
1202 break;
1205 case WID_SM_LEGEND: {
1206 uint columns = this->GetNumberColumnsLegend(r.right - r.left + 1);
1207 uint number_of_rows = this->GetNumberRowsLegend(columns);
1208 bool rtl = _current_text_dir == TD_RTL;
1209 uint y_org = r.top + WD_FRAMERECT_TOP;
1210 uint x = rtl ? r.right - this->column_width - WD_FRAMERECT_RIGHT : r.left + WD_FRAMERECT_LEFT;
1211 uint y = y_org;
1212 uint i = 0; // Row counter for industry legend.
1213 uint row_height = FONT_HEIGHT_SMALL;
1215 uint text_left = rtl ? 0 : LEGEND_BLOB_WIDTH + WD_FRAMERECT_LEFT;
1216 uint text_right = this->column_width - 1 - (rtl ? LEGEND_BLOB_WIDTH + WD_FRAMERECT_RIGHT : 0);
1217 uint blob_left = rtl ? this->column_width - 1 - LEGEND_BLOB_WIDTH : 0;
1218 uint blob_right = rtl ? this->column_width - 1 : LEGEND_BLOB_WIDTH;
1220 StringID string = STR_NULL;
1221 switch (this->map_type) {
1222 case SMT_INDUSTRY:
1223 string = STR_SMALLMAP_INDUSTRY;
1224 break;
1225 case SMT_LINKSTATS:
1226 string = STR_SMALLMAP_LINKSTATS;
1227 break;
1228 case SMT_OWNER:
1229 string = STR_SMALLMAP_COMPANY;
1230 break;
1231 default:
1232 break;
1235 for (const LegendAndColour *tbl = _legend_table[this->map_type]; !tbl->end; ++tbl) {
1236 if (tbl->col_break || ((this->map_type == SMT_INDUSTRY || this->map_type == SMT_OWNER || this->map_type == SMT_LINKSTATS) && i++ >= number_of_rows)) {
1237 /* Column break needed, continue at top, COLUMN_WIDTH pixels
1238 * (one "row") to the right. */
1239 x += rtl ? -(int)this->column_width : this->column_width;
1240 y = y_org;
1241 i = 1;
1244 uint8 legend_colour = tbl->colour;
1246 switch (this->map_type) {
1247 case SMT_INDUSTRY:
1248 /* Industry name must be formatted, since it's not in tiny font in the specs.
1249 * So, draw with a parameter and use the STR_SMALLMAP_INDUSTRY string, which is tiny font */
1250 SetDParam(0, tbl->legend);
1251 SetDParam(1, Industry::GetIndustryTypeCount(tbl->type));
1252 if (tbl->show_on_map && tbl->type == _smallmap_industry_highlight) {
1253 legend_colour = _smallmap_industry_highlight_state ? PC_WHITE : PC_BLACK;
1255 FALLTHROUGH;
1257 case SMT_LINKSTATS:
1258 SetDParam(0, tbl->legend);
1259 FALLTHROUGH;
1261 case SMT_OWNER:
1262 if (this->map_type != SMT_OWNER || tbl->company != INVALID_COMPANY) {
1263 if (this->map_type == SMT_OWNER) SetDParam(0, tbl->company);
1264 if (!tbl->show_on_map) {
1265 /* Simply draw the string, not the black border of the legend colour.
1266 * This will enforce the idea of the disabled item */
1267 DrawString(x + text_left, x + text_right, y, string, TC_GREY);
1268 } else {
1269 DrawString(x + text_left, x + text_right, y, string, TC_BLACK);
1270 GfxFillRect(x + blob_left, y + 1, x + blob_right, y + row_height - 1, PC_BLACK); // Outer border of the legend colour
1272 break;
1274 FALLTHROUGH;
1276 default:
1277 if (this->map_type == SMT_CONTOUR) SetDParam(0, tbl->height * TILE_HEIGHT_STEP);
1278 /* Anything that is not an industry or a company is using normal process */
1279 GfxFillRect(x + blob_left, y + 1, x + blob_right, y + row_height - 1, PC_BLACK);
1280 DrawString(x + text_left, x + text_right, y, tbl->legend);
1281 break;
1283 GfxFillRect(x + blob_left + 1, y + 2, x + blob_right - 1, y + row_height - 2, legend_colour); // Legend colour
1285 y += row_height;
1292 * Select a new map type.
1293 * @param map_type New map type.
1295 void SmallMapWindow::SwitchMapType(SmallMapType map_type)
1297 this->RaiseWidget(this->map_type + WID_SM_CONTOUR);
1298 this->map_type = map_type;
1299 this->LowerWidget(this->map_type + WID_SM_CONTOUR);
1301 this->SetupWidgetData();
1303 if (map_type == SMT_LINKSTATS) this->overlay->SetDirty();
1304 if (map_type != SMT_INDUSTRY) this->BreakIndustryChainLink();
1305 this->SetDirty();
1309 * Get the number of rows in the legend from the number of columns. Those
1310 * are at least min_number_of_fixed_rows and possibly more if there are so
1311 * many cargoes, industry types or companies that they won't fit in the
1312 * available space.
1313 * @param columns Number of columns in the legend.
1314 * @return Number of rows needed for everything to fit in.
1316 inline uint SmallMapWindow::GetNumberRowsLegend(uint columns) const
1318 /* Reserve one column for link colours */
1319 uint num_rows_linkstats = CeilDiv(_smallmap_cargo_count, columns - 1);
1320 uint num_rows_others = CeilDiv(max(_smallmap_industry_count, _smallmap_company_count), columns);
1321 return max(this->min_number_of_fixed_rows, max(num_rows_linkstats, num_rows_others));
1325 * Select and toggle a legend item. When CTRL is pressed, disable all other
1326 * items in the group defined by begin_legend_item and end_legend_item and
1327 * keep the clicked one enabled even if it was already enabled before. If
1328 * the other items in the group are all disabled already and CTRL is pressed
1329 * enable them instead.
1330 * @param click_pos the index of the item being selected
1331 * @param legend the legend from which we select
1332 * @param end_legend_item index one past the last item in the group to be inverted
1333 * @param begin_legend_item index of the first item in the group to be inverted
1335 void SmallMapWindow::SelectLegendItem(int click_pos, LegendAndColour *legend, int end_legend_item, int begin_legend_item)
1337 if (_ctrl_pressed) {
1338 /* Disable all, except the clicked one */
1339 bool changes = false;
1340 for (int i = begin_legend_item; i != end_legend_item; i++) {
1341 bool new_state = (i == click_pos);
1342 if (legend[i].show_on_map != new_state) {
1343 changes = true;
1344 legend[i].show_on_map = new_state;
1347 if (!changes) {
1348 /* Nothing changed? Then show all (again). */
1349 for (int i = begin_legend_item; i != end_legend_item; i++) {
1350 legend[i].show_on_map = true;
1353 } else {
1354 legend[click_pos].show_on_map = !legend[click_pos].show_on_map;
1357 if (this->map_type == SMT_INDUSTRY) this->BreakIndustryChainLink();
1361 * Set the link graph overlay cargo mask from the legend.
1363 void SmallMapWindow::SetOverlayCargoMask()
1365 CargoTypes cargo_mask = 0;
1366 for (int i = 0; i != _smallmap_cargo_count; ++i) {
1367 if (_legend_linkstats[i].show_on_map) SetBit(cargo_mask, _legend_linkstats[i].type);
1369 this->overlay->SetCargoMask(cargo_mask);
1373 * Determines the mouse position on the legend.
1374 * @param pt Mouse position.
1375 * @return Legend item under the mouse.
1377 int SmallMapWindow::GetPositionOnLegend(Point pt)
1379 const NWidgetBase *wi = this->GetWidget<NWidgetBase>(WID_SM_LEGEND);
1380 uint line = (pt.y - wi->pos_y - WD_FRAMERECT_TOP) / FONT_HEIGHT_SMALL;
1381 uint columns = this->GetNumberColumnsLegend(wi->current_x);
1382 uint number_of_rows = this->GetNumberRowsLegend(columns);
1383 if (line >= number_of_rows) return -1;
1385 bool rtl = _current_text_dir == TD_RTL;
1386 int x = pt.x - wi->pos_x;
1387 if (rtl) x = wi->current_x - x;
1388 uint column = (x - WD_FRAMERECT_LEFT) / this->column_width;
1390 return (column * number_of_rows) + line;
1393 /* virtual */ void SmallMapWindow::OnMouseOver(Point pt, int widget)
1395 IndustryType new_highlight = INVALID_INDUSTRYTYPE;
1396 if (widget == WID_SM_LEGEND && this->map_type == SMT_INDUSTRY) {
1397 int industry_pos = GetPositionOnLegend(pt);
1398 if (industry_pos >= 0 && industry_pos < _smallmap_industry_count) {
1399 new_highlight = _legend_from_industries[industry_pos].type;
1402 if (new_highlight != _smallmap_industry_highlight) {
1403 _smallmap_industry_highlight = new_highlight;
1404 this->refresh.SetInterval(_smallmap_industry_highlight != INVALID_INDUSTRYTYPE ? BLINK_PERIOD : FORCE_REFRESH_PERIOD);
1405 _smallmap_industry_highlight_state = true;
1406 this->SetDirty();
1410 /* virtual */ void SmallMapWindow::OnClick(Point pt, int widget, int click_count)
1412 switch (widget) {
1413 case WID_SM_MAP: { // Map window
1414 if (click_count > 0) this->mouse_capture_widget = widget;
1416 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1417 Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
1418 int sub;
1419 pt = this->PixelToTile(pt.x - wid->pos_x, pt.y - wid->pos_y, &sub);
1420 ScrollWindowTo(this->scroll_x + pt.x * TILE_SIZE, this->scroll_y + pt.y * TILE_SIZE, -1, w);
1422 this->SetDirty();
1423 break;
1426 case WID_SM_ZOOM_IN:
1427 case WID_SM_ZOOM_OUT: {
1428 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1429 Point pt = { (int)wid->current_x / 2, (int)wid->current_y / 2};
1430 this->SetZoomLevel((widget == WID_SM_ZOOM_IN) ? ZLC_ZOOM_IN : ZLC_ZOOM_OUT, &pt);
1431 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1432 break;
1435 case WID_SM_CONTOUR: // Show land contours
1436 case WID_SM_VEHICLES: // Show vehicles
1437 case WID_SM_INDUSTRIES: // Show industries
1438 case WID_SM_LINKSTATS: // Show route map
1439 case WID_SM_ROUTES: // Show transport routes
1440 case WID_SM_VEGETATION: // Show vegetation
1441 case WID_SM_OWNERS: // Show land owners
1442 this->SwitchMapType((SmallMapType)(widget - WID_SM_CONTOUR));
1443 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1444 break;
1446 case WID_SM_CENTERMAP: // Center the smallmap again
1447 this->SmallMapCenterOnCurrentPos();
1448 this->HandleButtonClick(WID_SM_CENTERMAP);
1449 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1450 break;
1452 case WID_SM_TOGGLETOWNNAME: // Toggle town names
1453 this->show_towns = !this->show_towns;
1454 this->SetWidgetLoweredState(WID_SM_TOGGLETOWNNAME, this->show_towns);
1456 this->SetDirty();
1457 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1458 break;
1460 case WID_SM_LEGEND: // Legend
1461 if (this->map_type == SMT_INDUSTRY || this->map_type == SMT_LINKSTATS || this->map_type == SMT_OWNER) {
1462 int click_pos = this->GetPositionOnLegend(pt);
1463 if (click_pos < 0) break;
1465 /* If industry type small map*/
1466 if (this->map_type == SMT_INDUSTRY) {
1467 /* If click on industries label, find right industry type and enable/disable it. */
1468 if (click_pos < _smallmap_industry_count) {
1469 this->SelectLegendItem(click_pos, _legend_from_industries, _smallmap_industry_count);
1471 } else if (this->map_type == SMT_LINKSTATS) {
1472 if (click_pos < _smallmap_cargo_count) {
1473 this->SelectLegendItem(click_pos, _legend_linkstats, _smallmap_cargo_count);
1474 this->SetOverlayCargoMask();
1476 } else if (this->map_type == SMT_OWNER) {
1477 if (click_pos < _smallmap_company_count) {
1478 this->SelectLegendItem(click_pos, _legend_land_owners, _smallmap_company_count, NUM_NO_COMPANY_ENTRIES);
1481 this->SetDirty();
1483 break;
1485 case WID_SM_ENABLE_ALL:
1486 case WID_SM_DISABLE_ALL: {
1487 LegendAndColour *tbl = nullptr;
1488 switch (this->map_type) {
1489 case SMT_INDUSTRY:
1490 tbl = _legend_from_industries;
1491 this->BreakIndustryChainLink();
1492 break;
1493 case SMT_OWNER:
1494 tbl = &(_legend_land_owners[NUM_NO_COMPANY_ENTRIES]);
1495 break;
1496 case SMT_LINKSTATS:
1497 tbl = _legend_linkstats;
1498 break;
1499 default:
1500 NOT_REACHED();
1502 for (;!tbl->end && tbl->legend != STR_LINKGRAPH_LEGEND_UNUSED; ++tbl) {
1503 tbl->show_on_map = (widget == WID_SM_ENABLE_ALL);
1505 if (this->map_type == SMT_LINKSTATS) this->SetOverlayCargoMask();
1506 this->SetDirty();
1507 break;
1510 case WID_SM_SHOW_HEIGHT: // Enable/disable showing of heightmap.
1511 _smallmap_show_heightmap = !_smallmap_show_heightmap;
1512 this->SetWidgetLoweredState(WID_SM_SHOW_HEIGHT, _smallmap_show_heightmap);
1513 this->SetDirty();
1514 break;
1519 * Some data on this window has become invalid.
1520 * @param data Information about the changed data.
1521 * - data = 0: Displayed industries at the industry chain window have changed.
1522 * - data = 1: Companies have changed.
1523 * - data = 2: Cheat changing the maximum heightlevel has been used, rebuild our heightlevel-to-colour index
1524 * @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.
1526 /* virtual */ void SmallMapWindow::OnInvalidateData(int data, bool gui_scope)
1528 if (!gui_scope) return;
1530 switch (data) {
1531 case 1:
1532 /* The owner legend has already been rebuilt. */
1533 this->ReInit();
1534 break;
1536 case 0: {
1537 extern std::bitset<NUM_INDUSTRYTYPES> _displayed_industries;
1538 if (this->map_type != SMT_INDUSTRY) this->SwitchMapType(SMT_INDUSTRY);
1540 for (int i = 0; i != _smallmap_industry_count; i++) {
1541 _legend_from_industries[i].show_on_map = _displayed_industries.test(_legend_from_industries[i].type);
1543 break;
1546 case 2:
1547 this->RebuildColourIndexIfNecessary();
1548 break;
1550 default: NOT_REACHED();
1552 this->SetDirty();
1555 /* virtual */ bool SmallMapWindow::OnRightClick(Point pt, int widget)
1557 if (widget != WID_SM_MAP || _scrolling_viewport) return false;
1559 _scrolling_viewport = true;
1560 return true;
1563 /* virtual */ void SmallMapWindow::OnMouseWheel(int wheel)
1565 if (_settings_client.gui.scrollwheel_scrolling != 2) {
1566 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1567 int cursor_x = _cursor.pos.x - this->left - wid->pos_x;
1568 int cursor_y = _cursor.pos.y - this->top - wid->pos_y;
1569 if (IsInsideMM(cursor_x, 0, wid->current_x) && IsInsideMM(cursor_y, 0, wid->current_y)) {
1570 Point pt = {cursor_x, cursor_y};
1571 this->SetZoomLevel((wheel < 0) ? ZLC_ZOOM_IN : ZLC_ZOOM_OUT, &pt);
1576 /* virtual */ void SmallMapWindow::OnRealtimeTick(uint delta_ms)
1578 /* Update the window every now and then */
1579 if (!this->refresh.Elapsed(delta_ms)) return;
1581 if (this->map_type == SMT_LINKSTATS) {
1582 uint32 company_mask = this->GetOverlayCompanyMask();
1583 if (this->overlay->GetCompanyMask() != company_mask) {
1584 this->overlay->SetCompanyMask(company_mask);
1585 } else {
1586 this->overlay->SetDirty();
1589 _smallmap_industry_highlight_state = !_smallmap_industry_highlight_state;
1591 this->refresh.SetInterval(_smallmap_industry_highlight != INVALID_INDUSTRYTYPE ? BLINK_PERIOD : FORCE_REFRESH_PERIOD);
1592 this->SetDirty();
1596 * Set new #scroll_x, #scroll_y, and #subscroll values after limiting them such that the center
1597 * of the smallmap always contains a part of the map.
1598 * @param sx Proposed new #scroll_x
1599 * @param sy Proposed new #scroll_y
1600 * @param sub Proposed new #subscroll
1602 void SmallMapWindow::SetNewScroll(int sx, int sy, int sub)
1604 const NWidgetBase *wi = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1605 Point hv = InverseRemapCoords(wi->current_x * ZOOM_LVL_BASE * TILE_SIZE / 2, wi->current_y * ZOOM_LVL_BASE * TILE_SIZE / 2);
1606 hv.x *= this->zoom;
1607 hv.y *= this->zoom;
1609 if (sx < -hv.x) {
1610 sx = -hv.x;
1611 sub = 0;
1613 if (sx > (int)(MapMaxX() * TILE_SIZE) - hv.x) {
1614 sx = MapMaxX() * TILE_SIZE - hv.x;
1615 sub = 0;
1617 if (sy < -hv.y) {
1618 sy = -hv.y;
1619 sub = 0;
1621 if (sy > (int)(MapMaxY() * TILE_SIZE) - hv.y) {
1622 sy = MapMaxY() * TILE_SIZE - hv.y;
1623 sub = 0;
1626 this->scroll_x = sx;
1627 this->scroll_y = sy;
1628 this->subscroll = sub;
1629 if (this->map_type == SMT_LINKSTATS) this->overlay->SetDirty();
1632 /* virtual */ void SmallMapWindow::OnScroll(Point delta)
1634 if (_settings_client.gui.scroll_mode == VSM_VIEWPORT_RMB_FIXED || _settings_client.gui.scroll_mode == VSM_MAP_RMB_FIXED) _cursor.fix_at = true;
1636 /* While tile is at (delta.x, delta.y)? */
1637 int sub;
1638 Point pt = this->PixelToTile(delta.x, delta.y, &sub);
1639 this->SetNewScroll(this->scroll_x + pt.x * TILE_SIZE, this->scroll_y + pt.y * TILE_SIZE, sub);
1641 this->SetDirty();
1645 * Center the small map on the current center of the viewport.
1647 void SmallMapWindow::SmallMapCenterOnCurrentPos()
1649 const ViewPort *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
1650 Point viewport_center = InverseRemapCoords2(vp->virtual_left + vp->virtual_width / 2, vp->virtual_top + vp->virtual_height / 2);
1652 int sub;
1653 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1654 Point sxy = this->ComputeScroll(viewport_center.x / (int)TILE_SIZE, viewport_center.y / (int)TILE_SIZE,
1655 max(0, (int)wid->current_x / 2 - 2), wid->current_y / 2, &sub);
1656 this->SetNewScroll(sxy.x, sxy.y, sub);
1657 this->SetDirty();
1661 * Get the center of the given station as point on the screen in the smallmap window.
1662 * @param st Station to find in the smallmap.
1663 * @return Point with coordinates of the station.
1665 Point SmallMapWindow::GetStationMiddle(const Station *st) const
1667 int x = (st->rect.right + st->rect.left + 1) / 2;
1668 int y = (st->rect.bottom + st->rect.top + 1) / 2;
1669 Point ret = this->RemapTile(x, y);
1671 /* Same magic 3 as in DrawVehicles; that's where I got it from.
1672 * No idea what it is, but without it the result looks bad.
1674 ret.x -= 3 + this->subscroll;
1675 return ret;
1678 SmallMapWindow::SmallMapType SmallMapWindow::map_type = SMT_CONTOUR;
1679 bool SmallMapWindow::show_towns = true;
1680 int SmallMapWindow::max_heightlevel = -1;
1683 * Custom container class for displaying smallmap with a vertically resizing legend panel.
1684 * The legend panel has a smallest height that depends on its width. Standard containers cannot handle this case.
1686 * @note The container assumes it has two children, the first is the display, the second is the bar with legends and selection image buttons.
1687 * Both children should be both horizontally and vertically resizable and horizontally fillable.
1688 * The bar should have a minimal size with a zero-size legends display. Child padding is not supported.
1690 class NWidgetSmallmapDisplay : public NWidgetContainer {
1691 const SmallMapWindow *smallmap_window; ///< Window manager instance.
1692 public:
1693 NWidgetSmallmapDisplay() : NWidgetContainer(NWID_VERTICAL)
1695 this->smallmap_window = nullptr;
1698 void SetupSmallestSize(Window *w, bool init_array) override
1700 NWidgetBase *display = this->head;
1701 NWidgetBase *bar = display->next;
1703 display->SetupSmallestSize(w, init_array);
1704 bar->SetupSmallestSize(w, init_array);
1706 this->smallmap_window = dynamic_cast<SmallMapWindow *>(w);
1707 assert(this->smallmap_window != nullptr);
1708 this->smallest_x = max(display->smallest_x, bar->smallest_x + smallmap_window->GetMinLegendWidth());
1709 this->smallest_y = display->smallest_y + max(bar->smallest_y, smallmap_window->GetLegendHeight(smallmap_window->min_number_of_columns));
1710 this->fill_x = max(display->fill_x, bar->fill_x);
1711 this->fill_y = (display->fill_y == 0 && bar->fill_y == 0) ? 0 : min(display->fill_y, bar->fill_y);
1712 this->resize_x = max(display->resize_x, bar->resize_x);
1713 this->resize_y = min(display->resize_y, bar->resize_y);
1716 void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl) override
1718 this->pos_x = x;
1719 this->pos_y = y;
1720 this->current_x = given_width;
1721 this->current_y = given_height;
1723 NWidgetBase *display = this->head;
1724 NWidgetBase *bar = display->next;
1726 if (sizing == ST_SMALLEST) {
1727 this->smallest_x = given_width;
1728 this->smallest_y = given_height;
1729 /* Make display and bar exactly equal to their minimal size. */
1730 display->AssignSizePosition(ST_SMALLEST, x, y, display->smallest_x, display->smallest_y, rtl);
1731 bar->AssignSizePosition(ST_SMALLEST, x, y + display->smallest_y, bar->smallest_x, bar->smallest_y, rtl);
1734 uint bar_height = max(bar->smallest_y, this->smallmap_window->GetLegendHeight(this->smallmap_window->GetNumberColumnsLegend(given_width - bar->smallest_x)));
1735 uint display_height = given_height - bar_height;
1736 display->AssignSizePosition(ST_RESIZE, x, y, given_width, display_height, rtl);
1737 bar->AssignSizePosition(ST_RESIZE, x, y + display_height, given_width, bar_height, rtl);
1740 NWidgetCore *GetWidgetFromPos(int x, int y) override
1742 if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return nullptr;
1743 for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) {
1744 NWidgetCore *widget = child_wid->GetWidgetFromPos(x, y);
1745 if (widget != nullptr) return widget;
1747 return nullptr;
1750 void Draw(const Window *w) override
1752 for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) child_wid->Draw(w);
1756 /** Widget parts of the smallmap display. */
1757 static const NWidgetPart _nested_smallmap_display[] = {
1758 NWidget(WWT_PANEL, COLOUR_BROWN, WID_SM_MAP_BORDER),
1759 NWidget(WWT_INSET, COLOUR_BROWN, WID_SM_MAP), SetMinimalSize(346, 140), SetResize(1, 1), SetPadding(2, 2, 2, 2), EndContainer(),
1760 EndContainer(),
1763 /** Widget parts of the smallmap legend bar + image buttons. */
1764 static const NWidgetPart _nested_smallmap_bar[] = {
1765 NWidget(WWT_PANEL, COLOUR_BROWN),
1766 NWidget(NWID_HORIZONTAL),
1767 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SM_LEGEND), SetResize(1, 1),
1768 NWidget(NWID_VERTICAL),
1769 /* Top button row. */
1770 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1771 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_SM_ZOOM_IN),
1772 SetDataTip(SPR_IMG_ZOOMIN, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_IN), SetFill(1, 1),
1773 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_SM_CENTERMAP),
1774 SetDataTip(SPR_IMG_SMALLMAP, STR_SMALLMAP_CENTER), SetFill(1, 1),
1775 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_BLANK),
1776 SetDataTip(SPR_DOT_SMALL, STR_NULL), SetFill(1, 1),
1777 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_CONTOUR),
1778 SetDataTip(SPR_IMG_SHOW_COUNTOURS, STR_SMALLMAP_TOOLTIP_SHOW_LAND_CONTOURS_ON_MAP), SetFill(1, 1),
1779 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_VEHICLES),
1780 SetDataTip(SPR_IMG_SHOW_VEHICLES, STR_SMALLMAP_TOOLTIP_SHOW_VEHICLES_ON_MAP), SetFill(1, 1),
1781 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_INDUSTRIES),
1782 SetDataTip(SPR_IMG_INDUSTRY, STR_SMALLMAP_TOOLTIP_SHOW_INDUSTRIES_ON_MAP), SetFill(1, 1),
1783 EndContainer(),
1784 /* Bottom button row. */
1785 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1786 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_SM_ZOOM_OUT),
1787 SetDataTip(SPR_IMG_ZOOMOUT, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_OUT), SetFill(1, 1),
1788 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_TOGGLETOWNNAME),
1789 SetDataTip(SPR_IMG_TOWN, STR_SMALLMAP_TOOLTIP_TOGGLE_TOWN_NAMES_ON_OFF), SetFill(1, 1),
1790 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_LINKSTATS),
1791 SetDataTip(SPR_IMG_CARGOFLOW, STR_SMALLMAP_TOOLTIP_SHOW_LINK_STATS_ON_MAP), SetFill(1, 1),
1792 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_ROUTES),
1793 SetDataTip(SPR_IMG_SHOW_ROUTES, STR_SMALLMAP_TOOLTIP_SHOW_TRANSPORT_ROUTES_ON), SetFill(1, 1),
1794 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_VEGETATION),
1795 SetDataTip(SPR_IMG_PLANTTREES, STR_SMALLMAP_TOOLTIP_SHOW_VEGETATION_ON_MAP), SetFill(1, 1),
1796 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_OWNERS),
1797 SetDataTip(SPR_IMG_COMPANY_GENERAL, STR_SMALLMAP_TOOLTIP_SHOW_LAND_OWNERS_ON_MAP), SetFill(1, 1),
1798 EndContainer(),
1799 NWidget(NWID_SPACER), SetResize(0, 1),
1800 EndContainer(),
1801 EndContainer(),
1802 EndContainer(),
1805 static NWidgetBase *SmallMapDisplay(int *biggest_index)
1807 NWidgetContainer *map_display = new NWidgetSmallmapDisplay;
1809 MakeNWidgets(_nested_smallmap_display, lengthof(_nested_smallmap_display), biggest_index, map_display);
1810 MakeNWidgets(_nested_smallmap_bar, lengthof(_nested_smallmap_bar), biggest_index, map_display);
1811 return map_display;
1815 static const NWidgetPart _nested_smallmap_widgets[] = {
1816 NWidget(NWID_HORIZONTAL),
1817 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
1818 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_SM_CAPTION), SetDataTip(STR_SMALLMAP_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1819 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
1820 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
1821 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
1822 EndContainer(),
1823 NWidgetFunction(SmallMapDisplay), // Smallmap display and legend bar + image buttons.
1824 /* Bottom button row and resize box. */
1825 NWidget(NWID_HORIZONTAL),
1826 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_SM_SELECT_BUTTONS),
1827 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1828 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_SM_ENABLE_ALL), SetDataTip(STR_SMALLMAP_ENABLE_ALL, STR_NULL),
1829 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_SM_DISABLE_ALL), SetDataTip(STR_SMALLMAP_DISABLE_ALL, STR_NULL),
1830 NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_SM_SHOW_HEIGHT), SetDataTip(STR_SMALLMAP_SHOW_HEIGHT, STR_SMALLMAP_TOOLTIP_SHOW_HEIGHT),
1831 NWidget(WWT_PANEL, COLOUR_BROWN), SetFill(1, 0), SetResize(1, 0),
1832 EndContainer(),
1833 EndContainer(),
1834 NWidget(WWT_PANEL, COLOUR_BROWN), SetFill(1, 0), SetResize(1, 0),
1835 EndContainer(),
1836 EndContainer(),
1837 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
1838 EndContainer(),
1841 static WindowDesc _smallmap_desc(
1842 WDP_AUTO, "smallmap", 484, 314,
1843 WC_SMALLMAP, WC_NONE,
1845 _nested_smallmap_widgets, lengthof(_nested_smallmap_widgets)
1849 * Show the smallmap window.
1851 void ShowSmallMap()
1853 AllocateWindowDescFront<SmallMapWindow>(&_smallmap_desc, 0);
1857 * Scrolls the main window to given coordinates.
1858 * @param x x coordinate
1859 * @param y y coordinate
1860 * @param z z coordinate; -1 to scroll to terrain height
1861 * @param instant scroll instantly (meaningful only when smooth_scrolling is active)
1862 * @return did the viewport position change?
1864 bool ScrollMainWindowTo(int x, int y, int z, bool instant)
1866 bool res = ScrollWindowTo(x, y, z, FindWindowById(WC_MAIN_WINDOW, 0), instant);
1868 /* If a user scrolls to a tile (via what way what so ever) and already is on
1869 * that tile (e.g.: pressed twice), move the smallmap to that location,
1870 * so you directly see where you are on the smallmap. */
1872 if (res) return res;
1874 SmallMapWindow *w = dynamic_cast<SmallMapWindow*>(FindWindowById(WC_SMALLMAP, 0));
1875 if (w != nullptr) w->SmallMapCenterOnCurrentPos();
1877 return res;