Fix ICU iterators on leading/trailing whitespace
[openttd/fttd.git] / src / smallmap_gui.cpp
blobfddc5a8384c9f50c8abd4c86a31c27ecb30c4a37
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 smallmap_gui.cpp GUI that shows a small map of the world with metadata like owner or height. */
12 #include "stdafx.h"
13 #include "map/ground.h"
14 #include "map/tunnel.h"
15 #include "map/depot.h"
16 #include "map/rail.h"
17 #include "industry.h"
18 #include "landscape.h"
19 #include "viewport_func.h"
20 #include "town.h"
21 #include "core/endian_func.hpp"
22 #include "vehicle_base.h"
23 #include "sound_func.h"
24 #include "window_func.h"
25 #include "company_base.h"
26 #include "rail.h"
28 #include "smallmap_gui.h"
30 #include "table/strings.h"
32 static int _smallmap_industry_count; ///< Number of used industries
33 static int _smallmap_company_count; ///< Number of entries in the owner legend.
34 static int _smallmap_cargo_count; ///< Number of cargos in the link stats legend.
36 /** Link stat colours shown in legenda. */
37 static uint8 _linkstat_colours_in_legenda[] = {0, 1, 3, 5, 7, 9, 11};
39 static const int NUM_NO_COMPANY_ENTRIES = 4; ///< Number of entries in the owner legend that are not companies.
41 static const uint8 PC_ROUGH_LAND = 0x52; ///< Dark green palette colour for rough land.
42 static const uint8 PC_GRASS_LAND = 0x54; ///< Dark green palette colour for grass land.
43 static const uint8 PC_BARE_LAND = 0x37; ///< Brown palette colour for bare land.
44 static const uint8 PC_FIELDS = 0x25; ///< Light brown palette colour for fields.
45 static const uint8 PC_TREES = 0x57; ///< Green palette colour for trees.
46 static const uint8 PC_WATER = 0xCA; ///< Dark blue palette colour for water.
48 /** Macro for ordinary entry of LegendAndColour */
49 #define MK(a, b) {a, b, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, false, false}
51 /** Macro for a height legend entry with configurable colour. */
52 #define MC(height) {0, STR_TINY_BLACK_HEIGHT, INVALID_INDUSTRYTYPE, height, INVALID_COMPANY, true, false, false}
54 /** Macro for non-company owned property entry of LegendAndColour */
55 #define MO(a, b) {a, b, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, false, false}
57 /** Macro used for forcing a rebuild of the owner legend the first time it is used. */
58 #define MOEND() {0, 0, INVALID_INDUSTRYTYPE, 0, OWNER_NONE, true, true, false}
60 /** Macro for end of list marker in arrays of LegendAndColour */
61 #define MKEND() {0, STR_NULL, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, true, false}
63 /**
64 * Macro for break marker in arrays of LegendAndColour.
65 * It will have valid data, though
67 #define MS(a, b) {a, b, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, false, true}
69 /** Legend text giving the colours to look for on the minimap */
70 static LegendAndColour _legend_land_contours[] = {
71 /* The colours for the following values are set at BuildLandLegend() based on each colour scheme. */
72 MC(0),
73 MC(4),
74 MC(8),
75 MC(12),
76 MC(14),
78 MS(PC_BLACK, STR_SMALLMAP_LEGENDA_ROADS),
79 MK(PC_GREY, STR_SMALLMAP_LEGENDA_RAILROADS),
80 MK(PC_LIGHT_BLUE, STR_SMALLMAP_LEGENDA_STATIONS_AIRPORTS_DOCKS),
81 MK(PC_DARK_RED, STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES),
82 MK(PC_WHITE, STR_SMALLMAP_LEGENDA_VEHICLES),
83 MKEND()
86 static const LegendAndColour _legend_vehicles[] = {
87 MK(PC_RED, STR_SMALLMAP_LEGENDA_TRAINS),
88 MK(PC_YELLOW, STR_SMALLMAP_LEGENDA_ROAD_VEHICLES),
89 MK(PC_LIGHT_BLUE, STR_SMALLMAP_LEGENDA_SHIPS),
90 MK(PC_WHITE, STR_SMALLMAP_LEGENDA_AIRCRAFT),
92 MS(PC_BLACK, STR_SMALLMAP_LEGENDA_TRANSPORT_ROUTES),
93 MK(PC_DARK_RED, STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES),
94 MKEND()
97 static const LegendAndColour _legend_routes[] = {
98 MK(PC_BLACK, STR_SMALLMAP_LEGENDA_ROADS),
99 MK(PC_GREY, STR_SMALLMAP_LEGENDA_RAILROADS),
100 MK(PC_DARK_RED, STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES),
102 MS(PC_VERY_DARK_BROWN, STR_SMALLMAP_LEGENDA_RAILROAD_STATION),
103 MK(PC_ORANGE, STR_SMALLMAP_LEGENDA_TRUCK_LOADING_BAY),
104 MK(PC_YELLOW, STR_SMALLMAP_LEGENDA_BUS_STATION),
105 MK(PC_RED, STR_SMALLMAP_LEGENDA_AIRPORT_HELIPORT),
106 MK(PC_LIGHT_BLUE, STR_SMALLMAP_LEGENDA_DOCK),
107 MKEND()
110 static const LegendAndColour _legend_vegetation[] = {
111 MK(PC_ROUGH_LAND, STR_SMALLMAP_LEGENDA_ROUGH_LAND),
112 MK(PC_GRASS_LAND, STR_SMALLMAP_LEGENDA_GRASS_LAND),
113 MK(PC_BARE_LAND, STR_SMALLMAP_LEGENDA_BARE_LAND),
114 MK(PC_FIELDS, STR_SMALLMAP_LEGENDA_FIELDS),
115 MK(PC_TREES, STR_SMALLMAP_LEGENDA_TREES),
116 MK(PC_GREEN, STR_SMALLMAP_LEGENDA_FOREST),
118 MS(PC_GREY, STR_SMALLMAP_LEGENDA_ROCKS),
119 MK(PC_ORANGE, STR_SMALLMAP_LEGENDA_DESERT),
120 MK(PC_LIGHT_BLUE, STR_SMALLMAP_LEGENDA_SNOW),
121 MK(PC_BLACK, STR_SMALLMAP_LEGENDA_TRANSPORT_ROUTES),
122 MK(PC_DARK_RED, STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES),
123 MKEND()
126 static LegendAndColour _legend_land_owners[NUM_NO_COMPANY_ENTRIES + MAX_COMPANIES + 1] = {
127 MO(PC_WATER, STR_SMALLMAP_LEGENDA_WATER),
128 MO(0x00, STR_SMALLMAP_LEGENDA_NO_OWNER), // This colour will vary depending on settings.
129 MO(PC_DARK_RED, STR_SMALLMAP_LEGENDA_TOWNS),
130 MO(PC_DARK_GREY, STR_SMALLMAP_LEGENDA_INDUSTRIES),
131 /* The legend will be terminated the first time it is used. */
132 MOEND(),
135 #undef MK
136 #undef MC
137 #undef MS
138 #undef MO
139 #undef MOEND
140 #undef MKEND
142 /** Legend entries for the link stats view. */
143 static LegendAndColour _legend_linkstats[NUM_CARGO + lengthof(_linkstat_colours_in_legenda) + 1];
145 * Allow room for all industries, plus a terminator entry
146 * This is required in order to have the industry slots all filled up
148 static LegendAndColour _legend_from_industries[NUM_INDUSTRYTYPES + 1];
149 /** For connecting industry type to position in industries list(small map legend) */
150 static uint _industry_to_list_pos[NUM_INDUSTRYTYPES];
151 /** Show heightmap in industry and owner mode of smallmap window. */
152 static bool _smallmap_show_heightmap = false;
153 /** Highlight a specific industry type */
154 static IndustryType _smallmap_industry_highlight = INVALID_INDUSTRYTYPE;
155 /** State of highlight blinking */
156 static bool _smallmap_industry_highlight_state;
157 /** For connecting company ID to position in owner list (small map legend) */
158 static uint _company_to_list_pos[MAX_COMPANIES];
161 * Fills an array for the industries legends.
163 void BuildIndustriesLegend()
165 uint j = 0;
167 /* Add each name */
168 for (uint8 i = 0; i < NUM_INDUSTRYTYPES; i++) {
169 IndustryType ind = _sorted_industry_types[i];
170 const IndustrySpec *indsp = GetIndustrySpec(ind);
171 if (indsp->enabled) {
172 _legend_from_industries[j].legend = indsp->name;
173 _legend_from_industries[j].colour = indsp->map_colour;
174 _legend_from_industries[j].type = ind;
175 _legend_from_industries[j].show_on_map = true;
176 _legend_from_industries[j].col_break = false;
177 _legend_from_industries[j].end = false;
179 /* Store widget number for this industry type. */
180 _industry_to_list_pos[ind] = j;
181 j++;
184 /* Terminate the list */
185 _legend_from_industries[j].end = true;
187 /* Store number of enabled industries */
188 _smallmap_industry_count = j;
192 * Populate legend table for the link stat view.
194 void BuildLinkStatsLegend()
196 /* Clear the legend */
197 memset(_legend_linkstats, 0, sizeof(_legend_linkstats));
199 uint i = 0;
200 for (; i < _sorted_cargo_specs_size; ++i) {
201 const CargoSpec *cs = _sorted_cargo_specs[i];
203 _legend_linkstats[i].legend = cs->name;
204 _legend_linkstats[i].colour = cs->legend_colour;
205 _legend_linkstats[i].type = cs->Index();
206 _legend_linkstats[i].show_on_map = true;
209 _legend_linkstats[i].col_break = true;
210 _smallmap_cargo_count = i;
212 for (; i < _smallmap_cargo_count + lengthof(_linkstat_colours_in_legenda); ++i) {
213 _legend_linkstats[i].legend = STR_EMPTY;
214 _legend_linkstats[i].colour = LinkGraphOverlay::LINK_COLOURS[_linkstat_colours_in_legenda[i - _smallmap_cargo_count]];
215 _legend_linkstats[i].show_on_map = true;
218 _legend_linkstats[_smallmap_cargo_count].legend = STR_LINKGRAPH_LEGEND_UNUSED;
219 _legend_linkstats[i - 1].legend = STR_LINKGRAPH_LEGEND_OVERLOADED;
220 _legend_linkstats[(_smallmap_cargo_count + i - 1) / 2].legend = STR_LINKGRAPH_LEGEND_SATURATED;
221 _legend_linkstats[i].end = true;
224 static const LegendAndColour * const _legend_table[] = {
225 _legend_land_contours,
226 _legend_vehicles,
227 _legend_from_industries,
228 _legend_linkstats,
229 _legend_routes,
230 _legend_vegetation,
231 _legend_land_owners,
234 #define MKCOLOUR(x) TO_LE32X(x)
236 #define MKCOLOUR_XXXX(x) (MKCOLOUR(0x01010101) * (uint)(x))
237 #define MKCOLOUR_X0X0(x) (MKCOLOUR(0x01000100) * (uint)(x))
238 #define MKCOLOUR_0X0X(x) (MKCOLOUR(0x00010001) * (uint)(x))
239 #define MKCOLOUR_0XX0(x) (MKCOLOUR(0x00010100) * (uint)(x))
240 #define MKCOLOUR_X00X(x) (MKCOLOUR(0x01000001) * (uint)(x))
242 #define MKCOLOUR_XYXY(x, y) (MKCOLOUR_X0X0(x) | MKCOLOUR_0X0X(y))
243 #define MKCOLOUR_XYYX(x, y) (MKCOLOUR_X00X(x) | MKCOLOUR_0XX0(y))
245 #define MKCOLOUR_0000 MKCOLOUR_XXXX(0x00)
246 #define MKCOLOUR_0FF0 MKCOLOUR_0XX0(0xFF)
247 #define MKCOLOUR_F00F MKCOLOUR_X00X(0xFF)
248 #define MKCOLOUR_FFFF MKCOLOUR_XXXX(0xFF)
250 /** Height map colours for the green colour scheme, ordered by height. */
251 static const uint32 _green_map_heights[] = {
252 MKCOLOUR_XXXX(0x5A),
253 MKCOLOUR_XYXY(0x5A, 0x5B),
254 MKCOLOUR_XXXX(0x5B),
255 MKCOLOUR_XYXY(0x5B, 0x5C),
256 MKCOLOUR_XXXX(0x5C),
257 MKCOLOUR_XYXY(0x5C, 0x5D),
258 MKCOLOUR_XXXX(0x5D),
259 MKCOLOUR_XYXY(0x5D, 0x5E),
260 MKCOLOUR_XXXX(0x5E),
261 MKCOLOUR_XYXY(0x5E, 0x5F),
262 MKCOLOUR_XXXX(0x5F),
263 MKCOLOUR_XYXY(0x5F, 0x1F),
264 MKCOLOUR_XXXX(0x1F),
265 MKCOLOUR_XYXY(0x1F, 0x27),
266 MKCOLOUR_XXXX(0x27),
267 MKCOLOUR_XXXX(0x27),
269 assert_compile(lengthof(_green_map_heights) == MAX_TILE_HEIGHT + 1);
271 /** Height map colours for the dark green colour scheme, ordered by height. */
272 static const uint32 _dark_green_map_heights[] = {
273 MKCOLOUR_XXXX(0x60),
274 MKCOLOUR_XYXY(0x60, 0x61),
275 MKCOLOUR_XXXX(0x61),
276 MKCOLOUR_XYXY(0x61, 0x62),
277 MKCOLOUR_XXXX(0x62),
278 MKCOLOUR_XYXY(0x62, 0x63),
279 MKCOLOUR_XXXX(0x63),
280 MKCOLOUR_XYXY(0x63, 0x64),
281 MKCOLOUR_XXXX(0x64),
282 MKCOLOUR_XYXY(0x64, 0x65),
283 MKCOLOUR_XXXX(0x65),
284 MKCOLOUR_XYXY(0x65, 0x66),
285 MKCOLOUR_XXXX(0x66),
286 MKCOLOUR_XYXY(0x66, 0x67),
287 MKCOLOUR_XXXX(0x67),
288 MKCOLOUR_XXXX(0x67),
290 assert_compile(lengthof(_dark_green_map_heights) == MAX_TILE_HEIGHT + 1);
292 /** Height map colours for the violet colour scheme, ordered by height. */
293 static const uint32 _violet_map_heights[] = {
294 MKCOLOUR_XXXX(0x80),
295 MKCOLOUR_XYXY(0x80, 0x81),
296 MKCOLOUR_XXXX(0x81),
297 MKCOLOUR_XYXY(0x81, 0x82),
298 MKCOLOUR_XXXX(0x82),
299 MKCOLOUR_XYXY(0x82, 0x83),
300 MKCOLOUR_XXXX(0x83),
301 MKCOLOUR_XYXY(0x83, 0x84),
302 MKCOLOUR_XXXX(0x84),
303 MKCOLOUR_XYXY(0x84, 0x85),
304 MKCOLOUR_XXXX(0x85),
305 MKCOLOUR_XYXY(0x85, 0x86),
306 MKCOLOUR_XXXX(0x86),
307 MKCOLOUR_XYXY(0x86, 0x87),
308 MKCOLOUR_XXXX(0x87),
309 MKCOLOUR_XXXX(0x87),
311 assert_compile(lengthof(_violet_map_heights) == MAX_TILE_HEIGHT + 1);
313 /** Colour scheme of the smallmap. */
314 struct SmallMapColourScheme {
315 const uint32 *height_colours; ///< Colour of each level in a heightmap.
316 uint32 default_colour; ///< Default colour of the land.
319 /** Available colour schemes for height maps. */
320 static const SmallMapColourScheme _heightmap_schemes[] = {
321 {_green_map_heights, MKCOLOUR_XXXX(0x54)}, ///< Green colour scheme.
322 {_dark_green_map_heights, MKCOLOUR_XXXX(0x62)}, ///< Dark green colour scheme.
323 {_violet_map_heights, MKCOLOUR_XXXX(0x82)}, ///< Violet colour scheme.
327 * (Re)build the colour tables for the legends.
329 void BuildLandLegend()
331 for (LegendAndColour *lc = _legend_land_contours; lc->legend == STR_TINY_BLACK_HEIGHT; lc++) {
332 lc->colour = _heightmap_schemes[_settings_client.gui.smallmap_land_colour].height_colours[lc->height];
337 * Completes the array for the owned property legend.
339 void BuildOwnerLegend()
341 _legend_land_owners[1].colour = _heightmap_schemes[_settings_client.gui.smallmap_land_colour].default_colour;
343 int i = NUM_NO_COMPANY_ENTRIES;
344 const Company *c;
345 FOR_ALL_COMPANIES(c) {
346 _legend_land_owners[i].colour = _colour_gradient[c->colour][5];
347 _legend_land_owners[i].company = c->index;
348 _legend_land_owners[i].show_on_map = true;
349 _legend_land_owners[i].col_break = false;
350 _legend_land_owners[i].end = false;
351 _company_to_list_pos[c->index] = i;
352 i++;
355 /* Terminate the list */
356 _legend_land_owners[i].end = true;
358 /* Store maximum amount of owner legend entries. */
359 _smallmap_company_count = i;
362 struct AndOr {
363 uint32 mor;
364 uint32 mand;
367 static inline uint32 ApplyMask(uint32 colour, const AndOr *mask)
369 return (colour & mask->mand) | mask->mor;
373 /** Tile types as seen in the smallmap, in decreasing order of importance. */
374 enum SmallmapTileType {
375 SMTT_STATION,
376 SMTT_RAILWAY,
377 SMTT_ROAD,
378 SMTT_INDUSTRY,
379 SMTT_HOUSE,
380 SMTT_OBJECT,
381 SMTT_CLEAR,
382 SMTT_WATER,
383 SMTT_VOID,
386 /** Colour masks for "Contour" and "Routes" modes. */
387 static const AndOr _smallmap_contours_andor[] = {
388 {MKCOLOUR_XXXX(PC_LIGHT_BLUE), MKCOLOUR_0000}, // SMTT_STATION
389 {MKCOLOUR_0XX0(PC_GREY ), MKCOLOUR_F00F}, // SMTT_RAILWAY
390 {MKCOLOUR_0XX0(PC_BLACK ), MKCOLOUR_F00F}, // SMTT_ROAD
391 {MKCOLOUR_XXXX(PC_DARK_RED ), MKCOLOUR_0000}, // SMTT_INDUSTRY
392 {MKCOLOUR_0XX0(PC_DARK_RED ), MKCOLOUR_F00F}, // SMTT_HOUSE
393 {MKCOLOUR_0XX0(PC_DARK_RED ), MKCOLOUR_F00F}, // SMTT_OBJECT
394 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // SMTT_CLEAR
395 {MKCOLOUR_XXXX(PC_WATER ), MKCOLOUR_0000}, // SMTT_WATER
396 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // SMTT_VOID
399 /** Colour masks for "Vehicles", "Industry", and "Vegetation" modes. */
400 static const AndOr _smallmap_vehicles_andor[] = {
401 {MKCOLOUR_0XX0(PC_BLACK ), MKCOLOUR_F00F}, // SMTT_STATION
402 {MKCOLOUR_0XX0(PC_BLACK ), MKCOLOUR_F00F}, // SMTT_RAILWAY
403 {MKCOLOUR_0XX0(PC_BLACK ), MKCOLOUR_F00F}, // SMTT_ROAD
404 {MKCOLOUR_XXXX(PC_DARK_RED ), MKCOLOUR_0000}, // SMTT_INDUSTRY
405 {MKCOLOUR_0XX0(PC_DARK_RED ), MKCOLOUR_F00F}, // SMTT_HOUSE
406 {MKCOLOUR_0XX0(PC_DARK_RED ), MKCOLOUR_F00F}, // SMTT_OBJECT
407 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // SMTT_CLEAR
408 {MKCOLOUR_XXXX(PC_WATER ), MKCOLOUR_0000}, // SMTT_WATER
409 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // SMTT_VOID
413 static inline SmallmapTileType GetEffectiveTileType(TileIndex tile)
415 if (IsHouseTile(tile)) return SMTT_HOUSE;
416 if (IsIndustryTile(tile)) return SMTT_INDUSTRY;
418 switch (GetTileType(tile)) {
419 case TT_STATION: return SMTT_STATION;
420 case TT_RAILWAY: return SMTT_RAILWAY;
421 case TT_ROAD: return SMTT_ROAD;
422 case TT_OBJECT: return SMTT_OBJECT;
423 case TT_GROUND: return IsTileSubtype(tile, TT_GROUND_VOID) ? SMTT_VOID : SMTT_CLEAR;
424 case TT_WATER: return SMTT_WATER;
425 case TT_MISC:
426 switch (GetTileSubtype(tile)) {
427 default: NOT_REACHED();
428 case TT_MISC_CROSSING: return SMTT_ROAD;
429 case TT_MISC_AQUEDUCT: return SMTT_WATER;
430 case TT_MISC_TUNNEL:
431 switch (GetTunnelTransportType(tile)) {
432 case TRANSPORT_RAIL: return SMTT_RAILWAY;
433 case TRANSPORT_ROAD: return SMTT_ROAD;
434 default: NOT_REACHED();
436 case TT_MISC_DEPOT:
437 return IsRailDepot(tile) ? SMTT_RAILWAY : SMTT_ROAD;
441 NOT_REACHED();
445 * Return the colour a tile would be displayed with in the small map in mode "Contour".
446 * @param tile The tile of which we would like to get the colour.
447 * @param t Effective tile type of the tile (see #GetEffectiveTileType).
448 * @return The colour of tile in the small map in mode "Contour"
450 static inline uint32 GetSmallMapContoursPixels(TileIndex tile, SmallmapTileType t)
452 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
453 return ApplyMask(cs->height_colours[TileHeight(tile)], &_smallmap_contours_andor[t]);
457 * Return the colour a tile would be displayed with in the small map in mode "Vehicles".
459 * @param tile The tile of which we would like to get the colour.
460 * @param t Effective tile type of the tile (see #GetEffectiveTileType).
461 * @return The colour of tile in the small map in mode "Vehicles"
463 static inline uint32 GetSmallMapVehiclesPixels(TileIndex tile, SmallmapTileType t)
465 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
466 return ApplyMask(cs->default_colour, &_smallmap_vehicles_andor[t]);
470 * Return the colour a tile would be displayed with in the small map in mode "Industries".
472 * @param tile The tile of which we would like to get the colour.
473 * @param t Effective tile type of the tile (see #GetEffectiveTileType).
474 * @return The colour of tile in the small map in mode "Industries"
476 static inline uint32 GetSmallMapIndustriesPixels(TileIndex tile, SmallmapTileType t)
478 if (t == SMTT_INDUSTRY) {
479 /* If industry is allowed to be seen, use its colour on the map */
480 IndustryType type = Industry::GetByTile(tile)->type;
481 if (_legend_from_industries[_industry_to_list_pos[type]].show_on_map &&
482 (_smallmap_industry_highlight_state || type != _smallmap_industry_highlight)) {
483 return (type == _smallmap_industry_highlight ? PC_WHITE : GetIndustrySpec(Industry::GetByTile(tile)->type)->map_colour) * 0x01010101;
484 } else {
485 /* Otherwise, return the colour which will make it disappear */
486 t = (IsTileOnWater(tile) ? SMTT_WATER : SMTT_CLEAR);
490 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
491 return ApplyMask(_smallmap_show_heightmap ? cs->height_colours[TileHeight(tile)] : cs->default_colour, &_smallmap_vehicles_andor[t]);
495 * Return the colour a tile would be displayed with in the small map in mode "Routes".
497 * @param tile The tile of which we would like to get the colour.
498 * @param t Effective tile type of the tile (see #GetEffectiveTileType).
499 * @return The colour of tile in the small map in mode "Routes"
501 static inline uint32 GetSmallMapRoutesPixels(TileIndex tile, SmallmapTileType t)
503 if (t == SMTT_STATION) {
504 switch (GetStationType(tile)) {
505 case STATION_RAIL: return MKCOLOUR_XXXX(PC_VERY_DARK_BROWN);
506 case STATION_AIRPORT: return MKCOLOUR_XXXX(PC_RED);
507 case STATION_TRUCK: return MKCOLOUR_XXXX(PC_ORANGE);
508 case STATION_BUS: return MKCOLOUR_XXXX(PC_YELLOW);
509 case STATION_DOCK: return MKCOLOUR_XXXX(PC_LIGHT_BLUE);
510 default: return MKCOLOUR_FFFF;
512 } else if (t == SMTT_RAILWAY) {
513 AndOr andor = {
514 MKCOLOUR_0XX0(GetRailTypeInfo(GetRailType(tile))->map_colour),
515 _smallmap_contours_andor[t].mand
518 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
519 return ApplyMask(cs->default_colour, &andor);
522 /* Ground colour */
523 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
524 return ApplyMask(cs->default_colour, &_smallmap_contours_andor[t]);
528 * Return the colour a tile would be displayed with in the small map in mode "link stats".
530 * @param tile The tile of which we would like to get the colour.
531 * @param t Effective tile type of the tile (see #GetEffectiveTileType).
532 * @return The colour of tile in the small map in mode "link stats"
534 static inline uint32 GetSmallMapLinkStatsPixels(TileIndex tile, SmallmapTileType t)
536 return _smallmap_show_heightmap ? GetSmallMapContoursPixels(tile, t) : GetSmallMapRoutesPixels(tile, t);
539 static const uint32 _vegetation_clear_bits[] = {
540 MKCOLOUR_XXXX(PC_GRASS_LAND), ///< full grass
541 MKCOLOUR_XXXX(PC_GRASS_LAND), ///< shore
542 MKCOLOUR_XXXX(PC_ROUGH_LAND), ///< rough land
543 MKCOLOUR_XXXX(PC_GREY), ///< rocks
544 MKCOLOUR_XXXX(PC_ORANGE), ///< desert
548 * Return the colour a tile would be displayed with in the smallmap in mode "Vegetation".
550 * @param tile The tile of which we would like to get the colour.
551 * @param t Effective tile type of the tile (see #GetEffectiveTileType).
552 * @return The colour of tile in the smallmap in mode "Vegetation"
554 static inline uint32 GetSmallMapVegetationPixels(TileIndex tile, SmallmapTileType t)
556 switch (t) {
557 case SMTT_CLEAR:
558 switch (GetTileSubtype(tile)) {
559 default: NOT_REACHED();
561 case TT_GROUND_FIELDS:
562 return MKCOLOUR_XXXX(PC_FIELDS);
564 case TT_GROUND_CLEAR:
565 if (IsSnowTile(tile)) {
566 return MKCOLOUR_XXXX(PC_LIGHT_BLUE);
567 } else if (IsClearGround(tile, GROUND_GRASS) && GetClearDensity(tile) < 3) {
568 return MKCOLOUR_XXXX(PC_BARE_LAND);
569 } else {
570 return _vegetation_clear_bits[GetClearGround(tile)];
573 case TT_GROUND_TREES:
574 if (IsSnowTile(tile)) {
575 return MKCOLOUR_XYYX(PC_LIGHT_BLUE, PC_TREES);
576 } else if (GetClearGround(tile) == GROUND_DESERT) {
577 return MKCOLOUR_XYYX(PC_ORANGE, PC_TREES);
578 } else {
579 return MKCOLOUR_XYYX(PC_GRASS_LAND, PC_TREES);
583 case SMTT_INDUSTRY:
584 return IsTileForestIndustry(tile) ? MKCOLOUR_XXXX(PC_GREEN) : MKCOLOUR_XXXX(PC_DARK_RED);
586 default:
587 return ApplyMask(MKCOLOUR_XXXX(PC_GRASS_LAND), &_smallmap_vehicles_andor[t]);
592 * Return the colour a tile would be displayed with in the small map in mode "Owner".
594 * @param tile The tile of which we would like to get the colour.
595 * @param t Effective tile type of the tile (see #GetEffectiveTileType).
596 * @return The colour of tile in the small map in mode "Owner"
598 static inline uint32 GetSmallMapOwnerPixels(TileIndex tile, SmallmapTileType t)
600 Owner o;
602 switch (t) {
603 case SMTT_INDUSTRY: return MKCOLOUR_XXXX(PC_DARK_GREY);
604 case SMTT_HOUSE: return MKCOLOUR_XXXX(PC_DARK_RED);
605 default: o = GetTileOwner(tile); break;
606 /* FIXME: For roads there are multiple owners.
607 * GetTileOwner returns the rail owner (level crossing) resp. the owner of ROADTYPE_ROAD (normal road),
608 * even if there are no ROADTYPE_ROAD bits on the tile.
612 if ((o < MAX_COMPANIES && !_legend_land_owners[_company_to_list_pos[o]].show_on_map) || o == OWNER_NONE || o == OWNER_WATER) {
613 if (t == SMTT_WATER) return MKCOLOUR_XXXX(PC_WATER);
614 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
615 return _smallmap_show_heightmap ? cs->height_colours[TileHeight(tile)] : cs->default_colour;
616 } else if (o == OWNER_TOWN) {
617 return MKCOLOUR_XXXX(PC_DARK_RED);
620 return MKCOLOUR_XXXX(_legend_land_owners[_company_to_list_pos[o]].colour);
623 /** Vehicle colours in #SMT_VEHICLES mode. Indexed by #VehicleTypeByte. */
624 static const byte _vehicle_type_colours[6] = {
625 PC_RED, PC_YELLOW, PC_LIGHT_BLUE, PC_WHITE, PC_BLACK, PC_RED
629 inline Point SmallMapWindow::SmallmapRemapCoords(int x, int y) const
631 Point pt;
632 pt.x = (y - x) * 2;
633 pt.y = y + x;
634 return pt;
638 * Remap tile to location on this smallmap.
639 * @param tile_x X coordinate of the tile.
640 * @param tile_y Y coordinate of the tile.
641 * @return Position to draw on.
643 inline Point SmallMapWindow::RemapTile(int tile_x, int tile_y) const
645 int x_offset = tile_x - this->scroll_x / (int)TILE_SIZE;
646 int y_offset = tile_y - this->scroll_y / (int)TILE_SIZE;
648 if (this->zoom == 1) return SmallmapRemapCoords(x_offset, y_offset);
650 /* For negative offsets, round towards -inf. */
651 if (x_offset < 0) x_offset -= this->zoom - 1;
652 if (y_offset < 0) y_offset -= this->zoom - 1;
654 return SmallmapRemapCoords(x_offset / this->zoom, y_offset / this->zoom);
658 * Determine the tile relative to the base tile of the smallmap, and the pixel position at
659 * that tile for a point in the smallmap.
660 * @param px Horizontal coordinate of the pixel.
661 * @param py Vertical coordinate of the pixel.
662 * @param sub[out] Pixel position at the tile (0..3).
663 * @param add_sub Add current #subscroll to the position.
664 * @return Tile being displayed at the given position relative to #scroll_x and #scroll_y.
665 * @note The #subscroll offset is already accounted for.
667 inline Point SmallMapWindow::PixelToTile(int px, int py, int *sub, bool add_sub) const
669 if (add_sub) px += this->subscroll; // Total horizontal offset.
671 /* For each two rows down, add a x and a y tile, and
672 * For each four pixels to the right, move a tile to the right. */
673 Point pt = {((py >> 1) - (px >> 2)) * this->zoom, ((py >> 1) + (px >> 2)) * this->zoom};
674 px &= 3;
676 if (py & 1) { // Odd number of rows, handle the 2 pixel shift.
677 if (px < 2) {
678 pt.x += this->zoom;
679 px += 2;
680 } else {
681 pt.y += this->zoom;
682 px -= 2;
686 *sub = px;
687 return pt;
691 * Compute base parameters of the smallmap such that tile (\a tx, \a ty) starts at pixel (\a x, \a y).
692 * @param tx Tile x coordinate.
693 * @param ty Tile y coordinate.
694 * @param x Non-negative horizontal position in the display where the tile starts.
695 * @param y Non-negative vertical position in the display where the tile starts.
696 * @param sub [out] Value of #subscroll needed.
697 * @return #scroll_x, #scroll_y values.
699 Point SmallMapWindow::ComputeScroll(int tx, int ty, int x, int y, int *sub)
701 assert(x >= 0 && y >= 0);
703 int new_sub;
704 Point tile_xy = PixelToTile(x, y, &new_sub, false);
705 tx -= tile_xy.x;
706 ty -= tile_xy.y;
708 Point scroll;
709 if (new_sub == 0) {
710 *sub = 0;
711 scroll.x = (tx + this->zoom) * TILE_SIZE;
712 scroll.y = (ty - this->zoom) * TILE_SIZE;
713 } else {
714 *sub = 4 - new_sub;
715 scroll.x = (tx + 2 * this->zoom) * TILE_SIZE;
716 scroll.y = (ty - 2 * this->zoom) * TILE_SIZE;
718 return scroll;
722 * Initialize or change the zoom level.
723 * @param change Way to change the zoom level.
724 * @param zoom_pt Position to keep fixed while zooming.
725 * @pre \c *zoom_pt should contain a point in the smallmap display when zooming in or out.
727 void SmallMapWindow::SetZoomLevel(ZoomLevelChange change, const Point *zoom_pt)
729 static const int zoomlevels[] = {1, 2, 4, 6, 8}; // Available zoom levels. Bigger number means more zoom-out (further away).
730 static const int MIN_ZOOM_INDEX = 0;
731 static const int MAX_ZOOM_INDEX = lengthof(zoomlevels) - 1;
733 int new_index, cur_index, sub;
734 Point tile;
735 switch (change) {
736 case ZLC_INITIALIZE:
737 cur_index = - 1; // Definitely different from new_index.
738 new_index = MIN_ZOOM_INDEX;
739 tile.x = tile.y = 0;
740 break;
742 case ZLC_ZOOM_IN:
743 case ZLC_ZOOM_OUT:
744 for (cur_index = MIN_ZOOM_INDEX; cur_index <= MAX_ZOOM_INDEX; cur_index++) {
745 if (this->zoom == zoomlevels[cur_index]) break;
747 assert(cur_index <= MAX_ZOOM_INDEX);
749 tile = this->PixelToTile(zoom_pt->x, zoom_pt->y, &sub);
750 new_index = Clamp(cur_index + ((change == ZLC_ZOOM_IN) ? -1 : 1), MIN_ZOOM_INDEX, MAX_ZOOM_INDEX);
751 break;
753 default: NOT_REACHED();
756 if (new_index != cur_index) {
757 this->zoom = zoomlevels[new_index];
758 if (cur_index >= 0) {
759 Point new_tile = this->PixelToTile(zoom_pt->x, zoom_pt->y, &sub);
760 this->SetNewScroll(this->scroll_x + (tile.x - new_tile.x) * TILE_SIZE,
761 this->scroll_y + (tile.y - new_tile.y) * TILE_SIZE, sub);
762 } else if (this->map_type == SMT_LINKSTATS) {
763 this->overlay->RebuildCache();
765 this->SetWidgetDisabledState(WID_SM_ZOOM_IN, this->zoom == zoomlevels[MIN_ZOOM_INDEX]);
766 this->SetWidgetDisabledState(WID_SM_ZOOM_OUT, this->zoom == zoomlevels[MAX_ZOOM_INDEX]);
767 this->SetDirty();
772 * Decide which colours to show to the user for a group of tiles.
773 * @param ta Tile area to investigate.
774 * @return Colours to display.
776 inline uint32 SmallMapWindow::GetTileColours(const TileArea &ta) const
778 TileIndex tile = INVALID_TILE; // Position of the most important tile.
779 SmallmapTileType et = SMTT_VOID; // Effective tile type at that position.
781 TILE_AREA_LOOP(ti, ta) {
782 SmallmapTileType ttype = GetEffectiveTileType(ti);
783 if (ttype < et) {
784 tile = ti;
785 et = ttype;
789 switch (this->map_type) {
790 case SMT_CONTOUR:
791 return GetSmallMapContoursPixels(tile, et);
793 case SMT_VEHICLES:
794 return GetSmallMapVehiclesPixels(tile, et);
796 case SMT_INDUSTRY:
797 return GetSmallMapIndustriesPixels(tile, et);
799 case SMT_LINKSTATS:
800 return GetSmallMapLinkStatsPixels(tile, et);
802 case SMT_ROUTES:
803 return GetSmallMapRoutesPixels(tile, et);
805 case SMT_VEGETATION:
806 return GetSmallMapVegetationPixels(tile, et);
808 case SMT_OWNER:
809 return GetSmallMapOwnerPixels(tile, et);
811 default: NOT_REACHED();
816 * Draws one column of tiles of the small map in a certain mode onto the screen buffer, skipping the shifted rows in between.
818 * @param dst Pointer to a part of the screen buffer to write to.
819 * @param xc The X coordinate of the first tile in the column.
820 * @param yc The Y coordinate of the first tile in the column
821 * @param pitch Number of pixels to advance in the screen buffer each time a pixel is written.
822 * @param reps Number of lines to draw
823 * @param start_pos Position of first pixel to draw.
824 * @param end_pos Position of last pixel to draw (exclusive).
825 * @param blitter current blitter
826 * @note If pixel position is below \c 0, skip drawing.
828 void SmallMapWindow::DrawSmallMapColumn(void *dst, uint xc, uint yc, int pitch, int reps, int start_pos, int end_pos, Blitter *blitter) const
830 void *dst_ptr_abs_end = blitter->MoveTo(_screen.dst_ptr, 0, _screen.height);
831 uint min_xy = _settings_game.construction.freeform_edges ? 1 : 0;
833 do {
834 /* Check if the tile (xc,yc) is within the map range */
835 if (xc >= MapMaxX() || yc >= MapMaxY()) continue;
837 /* Check if the dst pointer points to a pixel inside the screen buffer */
838 if (dst < _screen.dst_ptr) continue;
839 if (dst >= dst_ptr_abs_end) continue;
841 /* Construct tilearea covered by (xc, yc, xc + this->zoom, yc + this->zoom) such that it is within min_xy limits. */
842 TileArea ta;
843 if (min_xy == 1 && (xc == 0 || yc == 0)) {
844 if (this->zoom == 1) continue; // The tile area is empty, don't draw anything.
846 ta = TileArea(TileXY(max(min_xy, xc), max(min_xy, yc)), this->zoom - (xc == 0), this->zoom - (yc == 0));
847 } else {
848 ta = TileArea(TileXY(xc, yc), this->zoom, this->zoom);
850 ta.ClampToMap(); // Clamp to map boundaries (may contain void tiles!).
852 uint32 val = this->GetTileColours(ta);
853 uint8 *val8 = (uint8 *)&val;
854 int idx = max(0, -start_pos);
855 for (int pos = max(0, start_pos); pos < end_pos; pos++) {
856 blitter->SetPixel(dst, idx, 0, val8[idx]);
857 idx++;
859 /* Switch to next tile in the column */
860 } while (xc += this->zoom, yc += this->zoom, dst = blitter->MoveTo(dst, pitch, 0), --reps != 0);
864 * Adds vehicles to the smallmap.
865 * @param dpi the part of the smallmap to be drawn into
866 * @param blitter current blitter
868 void SmallMapWindow::DrawVehicles(const DrawPixelInfo *dpi, Blitter *blitter) const
870 const Vehicle *v;
871 FOR_ALL_VEHICLES(v) {
872 if (v->type == VEH_EFFECT) continue;
873 if (v->vehstatus & (VS_HIDDEN | VS_UNCLICKABLE)) continue;
875 /* Remap into flat coordinates. */
876 Point pt = this->RemapTile(v->x_pos / TILE_SIZE, v->y_pos / TILE_SIZE);
878 int y = pt.y - dpi->top;
879 if (!IsInsideMM(y, 0, dpi->height)) continue; // y is out of bounds.
881 bool skip = false; // Default is to draw both pixels.
882 int x = pt.x - this->subscroll - 3 - dpi->left; // Offset X coordinate.
883 if (x < 0) {
884 /* if x+1 is 0, that means we're on the very left edge,
885 * and should thus only draw a single pixel */
886 if (++x != 0) continue;
887 skip = true;
888 } else if (x >= dpi->width - 1) {
889 /* Check if we're at the very right edge, and if so draw only a single pixel */
890 if (x != dpi->width - 1) continue;
891 skip = true;
894 /* Calculate pointer to pixel and the colour */
895 byte colour = (this->map_type == SMT_VEHICLES) ? _vehicle_type_colours[v->type] : PC_WHITE;
897 /* And draw either one or two pixels depending on clipping */
898 blitter->SetPixel(dpi->dst_ptr, x, y, colour);
899 if (!skip) blitter->SetPixel(dpi->dst_ptr, x + 1, y, colour);
904 * Adds town names to the smallmap.
905 * @param dpi the part of the smallmap to be drawn into
907 void SmallMapWindow::DrawTowns(const DrawPixelInfo *dpi) const
909 const Town *t;
910 FOR_ALL_TOWNS(t) {
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 tile = InverseRemapCoords(vp->virtual_left, vp->virtual_top);
937 Point tl = this->RemapTile(tile.x >> 4, tile.y >> 4);
938 tl.x -= this->subscroll;
940 tile = InverseRemapCoords(vp->virtual_left + vp->virtual_width, vp->virtual_top + vp->virtual_height);
941 Point br = this->RemapTile(tile.x >> 4, tile.y >> 4);
942 br.x -= this->subscroll;
944 SmallMapWindow::DrawVertMapIndicator(tl.x, tl.y, br.y);
945 SmallMapWindow::DrawVertMapIndicator(br.x, tl.y, br.y);
947 SmallMapWindow::DrawHorizMapIndicator(tl.x, br.x, tl.y);
948 SmallMapWindow::DrawHorizMapIndicator(tl.x, br.x, br.y);
952 * Draws the small map.
954 * Basically, the small map is draw column of pixels by column of pixels. The pixels
955 * are drawn directly into the screen buffer. The final map is drawn in multiple passes.
956 * The passes are:
957 * <ol><li>The colours of tiles in the different modes.</li>
958 * <li>Town names (optional)</li></ol>
960 * @param dpi pointer to pixel to write onto
962 void SmallMapWindow::DrawSmallMap(DrawPixelInfo *dpi) const
964 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
965 DrawPixelInfo *old_dpi;
967 old_dpi = _cur_dpi;
968 _cur_dpi = dpi;
970 /* Clear it */
971 GfxFillRect(dpi->left, dpi->top, dpi->left + dpi->width - 1, dpi->top + dpi->height - 1, PC_BLACK);
973 /* Which tile is displayed at (dpi->left, dpi->top)? */
974 int dx;
975 Point tile = this->PixelToTile(dpi->left, dpi->top, &dx);
976 int tile_x = this->scroll_x / (int)TILE_SIZE + tile.x;
977 int tile_y = this->scroll_y / (int)TILE_SIZE + tile.y;
979 void *ptr = blitter->MoveTo(dpi->dst_ptr, -dx - 4, 0);
980 int x = - dx - 4;
981 int y = 0;
983 for (;;) {
984 /* Distance from left edge */
985 if (x >= -3) {
986 if (x >= dpi->width) break; // Exit the loop.
988 int end_pos = min(dpi->width, x + 4);
989 int reps = (dpi->height - y + 1) / 2; // Number of lines.
990 if (reps > 0) {
991 this->DrawSmallMapColumn(ptr, tile_x, tile_y, dpi->pitch * 2, reps, x, end_pos, blitter);
995 if (y == 0) {
996 tile_y += this->zoom;
997 y++;
998 ptr = blitter->MoveTo(ptr, 0, 1);
999 } else {
1000 tile_x -= this->zoom;
1001 y--;
1002 ptr = blitter->MoveTo(ptr, 0, -1);
1004 ptr = blitter->MoveTo(ptr, 2, 0);
1005 x += 2;
1008 /* Draw vehicles */
1009 if (this->map_type == SMT_CONTOUR || this->map_type == SMT_VEHICLES) this->DrawVehicles(dpi, blitter);
1011 /* Draw link stat overlay */
1012 if (this->map_type == SMT_LINKSTATS) this->overlay->Draw(dpi);
1014 /* Draw town names */
1015 if (this->show_towns) this->DrawTowns(dpi);
1017 /* Draw map indicators */
1018 this->DrawMapIndicators();
1020 _cur_dpi = old_dpi;
1024 * Function to set up widgets depending on the information being shown on the smallmap.
1026 void SmallMapWindow::SetupWidgetData()
1028 StringID legend_tooltip;
1029 StringID enable_all_tooltip;
1030 StringID disable_all_tooltip;
1031 int plane;
1032 switch (this->map_type) {
1033 case SMT_INDUSTRY:
1034 legend_tooltip = STR_SMALLMAP_TOOLTIP_INDUSTRY_SELECTION;
1035 enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_INDUSTRIES;
1036 disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_INDUSTRIES;
1037 plane = 0;
1038 break;
1040 case SMT_OWNER:
1041 legend_tooltip = STR_SMALLMAP_TOOLTIP_COMPANY_SELECTION;
1042 enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_COMPANIES;
1043 disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_COMPANIES;
1044 plane = 0;
1045 break;
1047 case SMT_LINKSTATS:
1048 legend_tooltip = STR_SMALLMAP_TOOLTIP_CARGO_SELECTION;
1049 enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_CARGOS;
1050 disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_CARGOS;
1051 plane = 0;
1052 break;
1054 default:
1055 legend_tooltip = STR_NULL;
1056 enable_all_tooltip = STR_NULL;
1057 disable_all_tooltip = STR_NULL;
1058 plane = 1;
1059 break;
1062 this->GetWidget<NWidgetCore>(WID_SM_LEGEND)->SetDataTip(STR_NULL, legend_tooltip);
1063 this->GetWidget<NWidgetCore>(WID_SM_ENABLE_ALL)->SetDataTip(STR_SMALLMAP_ENABLE_ALL, enable_all_tooltip);
1064 this->GetWidget<NWidgetCore>(WID_SM_DISABLE_ALL)->SetDataTip(STR_SMALLMAP_DISABLE_ALL, disable_all_tooltip);
1065 this->GetWidget<NWidgetStacked>(WID_SM_SELECT_BUTTONS)->SetDisplayedPlane(plane);
1068 SmallMapWindow::SmallMapWindow(WindowDesc *desc, int window_number) : Window(desc), refresh(FORCE_REFRESH_PERIOD)
1070 _smallmap_industry_highlight = INVALID_INDUSTRYTYPE;
1071 this->overlay = new LinkGraphOverlay(this, WID_SM_MAP, 0, this->GetOverlayCompanyMask(), 1);
1072 this->InitNested(window_number);
1073 this->LowerWidget(this->map_type + WID_SM_CONTOUR);
1075 BuildLandLegend();
1076 this->SetWidgetLoweredState(WID_SM_SHOW_HEIGHT, _smallmap_show_heightmap);
1078 this->SetWidgetLoweredState(WID_SM_TOGGLETOWNNAME, this->show_towns);
1080 this->SetupWidgetData();
1082 this->SetZoomLevel(ZLC_INITIALIZE, NULL);
1083 this->SmallMapCenterOnCurrentPos();
1084 this->SetOverlayCargoMask();
1087 /* virtual */ void SmallMapWindow::SetStringParameters(int widget) const
1089 switch (widget) {
1090 case WID_SM_CAPTION:
1091 SetDParam(0, STR_SMALLMAP_TYPE_CONTOURS + this->map_type);
1092 break;
1096 /* virtual */ void SmallMapWindow::OnInit()
1098 uint min_width = 0;
1099 this->min_number_of_columns = INDUSTRY_MIN_NUMBER_OF_COLUMNS;
1100 this->min_number_of_fixed_rows = lengthof(_linkstat_colours_in_legenda);
1101 for (uint i = 0; i < lengthof(_legend_table); i++) {
1102 uint height = 0;
1103 uint num_columns = 1;
1104 for (const LegendAndColour *tbl = _legend_table[i]; !tbl->end; ++tbl) {
1105 StringID str;
1106 if (i == SMT_INDUSTRY) {
1107 SetDParam(0, tbl->legend);
1108 SetDParam(1, Industry::Pool::MAX_SIZE);
1109 str = STR_SMALLMAP_INDUSTRY;
1110 } else if (i == SMT_LINKSTATS) {
1111 SetDParam(0, tbl->legend);
1112 str = STR_SMALLMAP_LINKSTATS;
1113 } else if (i == SMT_OWNER) {
1114 if (tbl->company != INVALID_COMPANY) {
1115 if (!Company::IsValidID(tbl->company)) {
1116 /* Rebuild the owner legend. */
1117 BuildOwnerLegend();
1118 this->OnInit();
1119 return;
1121 /* Non-fixed legend entries for the owner view. */
1122 SetDParam(0, tbl->company);
1123 str = STR_SMALLMAP_COMPANY;
1124 } else {
1125 str = tbl->legend;
1127 } else {
1128 if (tbl->col_break) {
1129 this->min_number_of_fixed_rows = max(this->min_number_of_fixed_rows, height);
1130 height = 0;
1131 num_columns++;
1133 height++;
1134 str = tbl->legend;
1136 min_width = max(GetStringBoundingBox(str).width, min_width);
1138 this->min_number_of_fixed_rows = max(this->min_number_of_fixed_rows, height);
1139 this->min_number_of_columns = max(this->min_number_of_columns, num_columns);
1142 /* The width of a column is the minimum width of all texts + the size of the blob + some spacing */
1143 this->column_width = min_width + LEGEND_BLOB_WIDTH + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
1146 /* virtual */ void SmallMapWindow::OnPaint()
1148 if (this->map_type == SMT_OWNER) {
1149 for (const LegendAndColour *tbl = _legend_table[this->map_type]; !tbl->end; ++tbl) {
1150 if (tbl->company != INVALID_COMPANY && !Company::IsValidID(tbl->company)) {
1151 /* Rebuild the owner legend. */
1152 BuildOwnerLegend();
1153 this->InvalidateData(1);
1154 break;
1159 this->DrawWidgets();
1162 /* virtual */ void SmallMapWindow::DrawWidget(const Rect &r, int widget) const
1164 switch (widget) {
1165 case WID_SM_MAP: {
1166 DrawPixelInfo new_dpi;
1167 if (!FillDrawPixelInfo(&new_dpi, r.left + 1, r.top + 1, r.right - r.left - 1, r.bottom - r.top - 1)) return;
1168 this->DrawSmallMap(&new_dpi);
1169 break;
1172 case WID_SM_LEGEND: {
1173 uint columns = this->GetNumberColumnsLegend(r.right - r.left + 1);
1174 uint number_of_rows = this->GetNumberRowsLegend(columns);
1175 bool rtl = _current_text_dir == TD_RTL;
1176 uint y_org = r.top + WD_FRAMERECT_TOP;
1177 uint x = rtl ? r.right - this->column_width - WD_FRAMERECT_RIGHT : r.left + WD_FRAMERECT_LEFT;
1178 uint y = y_org;
1179 uint i = 0; // Row counter for industry legend.
1180 uint row_height = FONT_HEIGHT_SMALL;
1182 uint text_left = rtl ? 0 : LEGEND_BLOB_WIDTH + WD_FRAMERECT_LEFT;
1183 uint text_right = this->column_width - 1 - (rtl ? LEGEND_BLOB_WIDTH + WD_FRAMERECT_RIGHT : 0);
1184 uint blob_left = rtl ? this->column_width - 1 - LEGEND_BLOB_WIDTH : 0;
1185 uint blob_right = rtl ? this->column_width - 1 : LEGEND_BLOB_WIDTH;
1187 StringID string = STR_NULL;
1188 switch (this->map_type) {
1189 case SMT_INDUSTRY:
1190 string = STR_SMALLMAP_INDUSTRY;
1191 break;
1192 case SMT_LINKSTATS:
1193 string = STR_SMALLMAP_LINKSTATS;
1194 break;
1195 case SMT_OWNER:
1196 string = STR_SMALLMAP_COMPANY;
1197 break;
1198 default:
1199 break;
1202 for (const LegendAndColour *tbl = _legend_table[this->map_type]; !tbl->end; ++tbl) {
1203 if (tbl->col_break || ((this->map_type == SMT_INDUSTRY || this->map_type == SMT_OWNER || this->map_type == SMT_LINKSTATS) && i++ >= number_of_rows)) {
1204 /* Column break needed, continue at top, COLUMN_WIDTH pixels
1205 * (one "row") to the right. */
1206 x += rtl ? -(int)this->column_width : this->column_width;
1207 y = y_org;
1208 i = 1;
1211 uint8 legend_colour = tbl->colour;
1213 switch (this->map_type) {
1214 case SMT_INDUSTRY:
1215 /* Industry name must be formatted, since it's not in tiny font in the specs.
1216 * So, draw with a parameter and use the STR_SMALLMAP_INDUSTRY string, which is tiny font */
1217 SetDParam(0, tbl->legend);
1218 SetDParam(1, Industry::GetIndustryTypeCount(tbl->type));
1219 if (tbl->show_on_map && tbl->type == _smallmap_industry_highlight) {
1220 legend_colour = _smallmap_industry_highlight_state ? PC_WHITE : PC_BLACK;
1222 /* FALL THROUGH */
1223 case SMT_LINKSTATS:
1224 SetDParam(0, tbl->legend);
1225 /* FALL_THROUGH */
1226 case SMT_OWNER:
1227 if (this->map_type != SMT_OWNER || tbl->company != INVALID_COMPANY) {
1228 if (this->map_type == SMT_OWNER) SetDParam(0, tbl->company);
1229 if (!tbl->show_on_map) {
1230 /* Simply draw the string, not the black border of the legend colour.
1231 * This will enforce the idea of the disabled item */
1232 DrawString(x + text_left, x + text_right, y, string, TC_GREY);
1233 } else {
1234 DrawString(x + text_left, x + text_right, y, string, TC_BLACK);
1235 GfxFillRect(x + blob_left, y + 1, x + blob_right, y + row_height - 1, PC_BLACK); // Outer border of the legend colour
1237 break;
1239 /* FALL_THROUGH */
1240 default:
1241 if (this->map_type == SMT_CONTOUR) SetDParam(0, tbl->height * TILE_HEIGHT_STEP);
1242 /* Anything that is not an industry or a company is using normal process */
1243 GfxFillRect(x + blob_left, y + 1, x + blob_right, y + row_height - 1, PC_BLACK);
1244 DrawString(x + text_left, x + text_right, y, tbl->legend);
1245 break;
1247 GfxFillRect(x + blob_left + 1, y + 2, x + blob_right - 1, y + row_height - 2, legend_colour); // Legend colour
1249 y += row_height;
1256 * Select a new map type.
1257 * @param map_type New map type.
1259 void SmallMapWindow::SwitchMapType(SmallMapType map_type)
1261 this->RaiseWidget(this->map_type + WID_SM_CONTOUR);
1262 this->map_type = map_type;
1263 this->LowerWidget(this->map_type + WID_SM_CONTOUR);
1265 this->SetupWidgetData();
1267 if (map_type == SMT_LINKSTATS) this->overlay->RebuildCache();
1268 this->SetDirty();
1272 * Get the number of rows in the legend from the number of columns. Those
1273 * are at least min_number_of_fixed_rows and possibly more if there are so
1274 * many cargoes, industry types or companies that they won't fit in the
1275 * available space.
1276 * @param columns Number of columns in the legend.
1277 * @return Number of rows needed for everything to fit in.
1279 inline uint SmallMapWindow::GetNumberRowsLegend(uint columns) const
1281 /* Reserve one column for link colours */
1282 uint num_rows_linkstats = CeilDiv(_smallmap_cargo_count, columns - 1);
1283 uint num_rows_others = CeilDiv(max(_smallmap_industry_count, _smallmap_company_count), columns);
1284 return max(this->min_number_of_fixed_rows, max(num_rows_linkstats, num_rows_others));
1288 * Select and toggle a legend item. When CTRL is pressed, disable all other
1289 * items in the group defined by begin_legend_item and end_legend_item and
1290 * keep the clicked one enabled even if it was already enabled before. If
1291 * the other items in the group are all disabled already and CTRL is pressed
1292 * enable them instead.
1293 * @param click_pos the index of the item being selected
1294 * @param legend the legend from which we select
1295 * @param end_legend_item index one past the last item in the group to be inverted
1296 * @param begin_legend_item index of the first item in the group to be inverted
1298 void SmallMapWindow::SelectLegendItem(int click_pos, LegendAndColour *legend, int end_legend_item, int begin_legend_item)
1300 if (_ctrl_pressed) {
1301 /* Disable all, except the clicked one */
1302 bool changes = false;
1303 for (int i = begin_legend_item; i != end_legend_item; i++) {
1304 bool new_state = (i == click_pos);
1305 if (legend[i].show_on_map != new_state) {
1306 changes = true;
1307 legend[i].show_on_map = new_state;
1310 if (!changes) {
1311 /* Nothing changed? Then show all (again). */
1312 for (int i = begin_legend_item; i != end_legend_item; i++) {
1313 legend[i].show_on_map = true;
1316 } else {
1317 legend[click_pos].show_on_map = !legend[click_pos].show_on_map;
1322 * Set the link graph overlay cargo mask from the legend.
1324 void SmallMapWindow::SetOverlayCargoMask()
1326 uint32 cargo_mask = 0;
1327 for (int i = 0; i != _smallmap_cargo_count; ++i) {
1328 if (_legend_linkstats[i].show_on_map) SetBit(cargo_mask, _legend_linkstats[i].type);
1330 this->overlay->SetCargoMask(cargo_mask);
1334 * Determines the mouse position on the legend.
1335 * @param pt Mouse position.
1336 * @return Legend item under the mouse.
1338 int SmallMapWindow::GetPositionOnLegend(Point pt)
1340 const NWidgetBase *wi = this->GetWidget<NWidgetBase>(WID_SM_LEGEND);
1341 uint line = (pt.y - wi->pos_y - WD_FRAMERECT_TOP) / FONT_HEIGHT_SMALL;
1342 uint columns = this->GetNumberColumnsLegend(wi->current_x);
1343 uint number_of_rows = this->GetNumberRowsLegend(columns);
1344 if (line >= number_of_rows) return -1;
1346 bool rtl = _current_text_dir == TD_RTL;
1347 int x = pt.x - wi->pos_x;
1348 if (rtl) x = wi->current_x - x;
1349 uint column = (x - WD_FRAMERECT_LEFT) / this->column_width;
1351 return (column * number_of_rows) + line;
1354 /* virtual */ void SmallMapWindow::OnMouseOver(Point pt, int widget)
1356 IndustryType new_highlight = INVALID_INDUSTRYTYPE;
1357 if (widget == WID_SM_LEGEND && this->map_type == SMT_INDUSTRY) {
1358 int industry_pos = GetPositionOnLegend(pt);
1359 if (industry_pos >= 0 && industry_pos < _smallmap_industry_count) {
1360 new_highlight = _legend_from_industries[industry_pos].type;
1363 if (new_highlight != _smallmap_industry_highlight) {
1364 _smallmap_industry_highlight = new_highlight;
1365 this->refresh = _smallmap_industry_highlight != INVALID_INDUSTRYTYPE ? BLINK_PERIOD : FORCE_REFRESH_PERIOD;
1366 _smallmap_industry_highlight_state = true;
1367 this->SetDirty();
1371 /* virtual */ void SmallMapWindow::OnClick(Point pt, int widget, int click_count)
1373 /* User clicked something, notify the industry chain window to stop sending newly selected industries. */
1374 InvalidateWindowClassesData(WC_INDUSTRY_CARGOES, NUM_INDUSTRYTYPES);
1376 switch (widget) {
1377 case WID_SM_MAP: { // Map window
1379 * XXX: scrolling with the left mouse button is done by subsequently
1380 * clicking with the left mouse button; clicking once centers the
1381 * large map at the selected point. So by unclicking the left mouse
1382 * button here, it gets reclicked during the next inputloop, which
1383 * would make it look like the mouse is being dragged, while it is
1384 * actually being (virtually) clicked every inputloop.
1386 _left_button_clicked = false;
1388 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1389 Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
1390 int sub;
1391 pt = this->PixelToTile(pt.x - wid->pos_x, pt.y - wid->pos_y, &sub);
1392 pt = RemapCoords(this->scroll_x + pt.x * TILE_SIZE + this->zoom * (TILE_SIZE - sub * TILE_SIZE / 4),
1393 this->scroll_y + pt.y * TILE_SIZE + sub * this->zoom * TILE_SIZE / 4, 0);
1395 w->viewport->follow_vehicle = INVALID_VEHICLE;
1396 w->viewport->dest_scrollpos_x = pt.x - (w->viewport->virtual_width >> 1);
1397 w->viewport->dest_scrollpos_y = pt.y - (w->viewport->virtual_height >> 1);
1399 this->SetDirty();
1400 break;
1403 case WID_SM_ZOOM_IN:
1404 case WID_SM_ZOOM_OUT: {
1405 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1406 Point pt = {wid->current_x / 2, wid->current_y / 2};
1407 this->SetZoomLevel((widget == WID_SM_ZOOM_IN) ? ZLC_ZOOM_IN : ZLC_ZOOM_OUT, &pt);
1408 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1409 break;
1412 case WID_SM_CONTOUR: // Show land contours
1413 case WID_SM_VEHICLES: // Show vehicles
1414 case WID_SM_INDUSTRIES: // Show industries
1415 case WID_SM_LINKSTATS: // Show route map
1416 case WID_SM_ROUTES: // Show transport routes
1417 case WID_SM_VEGETATION: // Show vegetation
1418 case WID_SM_OWNERS: // Show land owners
1419 this->SwitchMapType((SmallMapType)(widget - WID_SM_CONTOUR));
1420 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1421 break;
1423 case WID_SM_CENTERMAP: // Center the smallmap again
1424 this->SmallMapCenterOnCurrentPos();
1425 this->HandleButtonClick(WID_SM_CENTERMAP);
1426 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1427 break;
1429 case WID_SM_TOGGLETOWNNAME: // Toggle town names
1430 this->show_towns = !this->show_towns;
1431 this->SetWidgetLoweredState(WID_SM_TOGGLETOWNNAME, this->show_towns);
1433 this->SetDirty();
1434 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1435 break;
1437 case WID_SM_LEGEND: // Legend
1438 if (this->map_type == SMT_INDUSTRY || this->map_type == SMT_LINKSTATS || this->map_type == SMT_OWNER) {
1439 int click_pos = this->GetPositionOnLegend(pt);
1440 if (click_pos < 0) break;
1442 /* If industry type small map*/
1443 if (this->map_type == SMT_INDUSTRY) {
1444 /* If click on industries label, find right industry type and enable/disable it. */
1445 if (click_pos < _smallmap_industry_count) {
1446 this->SelectLegendItem(click_pos, _legend_from_industries, _smallmap_industry_count);
1448 } else if (this->map_type == SMT_LINKSTATS) {
1449 if (click_pos < _smallmap_cargo_count) {
1450 this->SelectLegendItem(click_pos, _legend_linkstats, _smallmap_cargo_count);
1451 this->SetOverlayCargoMask();
1453 } else if (this->map_type == SMT_OWNER) {
1454 if (click_pos < _smallmap_company_count) {
1455 this->SelectLegendItem(click_pos, _legend_land_owners, _smallmap_company_count, NUM_NO_COMPANY_ENTRIES);
1458 this->SetDirty();
1460 break;
1462 case WID_SM_ENABLE_ALL:
1463 /* FALL THROUGH */
1464 case WID_SM_DISABLE_ALL: {
1465 LegendAndColour *tbl = NULL;
1466 switch (this->map_type) {
1467 case SMT_INDUSTRY:
1468 tbl = _legend_from_industries;
1469 break;
1470 case SMT_OWNER:
1471 tbl = &(_legend_land_owners[NUM_NO_COMPANY_ENTRIES]);
1472 break;
1473 case SMT_LINKSTATS:
1474 tbl = _legend_linkstats;
1475 break;
1476 default:
1477 NOT_REACHED();
1479 for (;!tbl->end && tbl->legend != STR_LINKGRAPH_LEGEND_UNUSED; ++tbl) {
1480 tbl->show_on_map = (widget == WID_SM_ENABLE_ALL);
1482 if (this->map_type == SMT_LINKSTATS) this->SetOverlayCargoMask();
1483 this->SetDirty();
1484 break;
1487 case WID_SM_SHOW_HEIGHT: // Enable/disable showing of heightmap.
1488 _smallmap_show_heightmap = !_smallmap_show_heightmap;
1489 this->SetWidgetLoweredState(WID_SM_SHOW_HEIGHT, _smallmap_show_heightmap);
1490 this->SetDirty();
1491 break;
1496 * Some data on this window has become invalid.
1497 * @param data Information about the changed data.
1498 * - data = 0: Displayed industries at the industry chain window have changed.
1499 * - data = 1: Companies have changed.
1500 * @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.
1502 /* virtual */ void SmallMapWindow::OnInvalidateData(int data, bool gui_scope)
1504 if (!gui_scope) return;
1505 switch (data) {
1506 case 1:
1507 /* The owner legend has already been rebuilt. */
1508 this->ReInit();
1509 break;
1511 case 0: {
1512 extern uint64 _displayed_industries;
1513 if (this->map_type != SMT_INDUSTRY) this->SwitchMapType(SMT_INDUSTRY);
1515 for (int i = 0; i != _smallmap_industry_count; i++) {
1516 _legend_from_industries[i].show_on_map = HasBit(_displayed_industries, _legend_from_industries[i].type);
1518 break;
1521 default: NOT_REACHED();
1523 this->SetDirty();
1526 /* virtual */ bool SmallMapWindow::OnRightClick(Point pt, int widget)
1528 if (widget != WID_SM_MAP || _scrolling_viewport) return false;
1530 _scrolling_viewport = true;
1531 return true;
1534 /* virtual */ void SmallMapWindow::OnMouseWheel(int wheel)
1536 if (_settings_client.gui.scrollwheel_scrolling == 0) {
1537 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1538 int cursor_x = _cursor.pos.x - this->left - wid->pos_x;
1539 int cursor_y = _cursor.pos.y - this->top - wid->pos_y;
1540 if (IsInsideMM(cursor_x, 0, wid->current_x) && IsInsideMM(cursor_y, 0, wid->current_y)) {
1541 Point pt = {cursor_x, cursor_y};
1542 this->SetZoomLevel((wheel < 0) ? ZLC_ZOOM_IN : ZLC_ZOOM_OUT, &pt);
1547 /* virtual */ void SmallMapWindow::OnTick()
1549 /* Update the window every now and then */
1550 if (--this->refresh != 0) return;
1552 if (this->map_type == SMT_LINKSTATS) {
1553 uint32 company_mask = this->GetOverlayCompanyMask();
1554 if (this->overlay->GetCompanyMask() != company_mask) {
1555 this->overlay->SetCompanyMask(company_mask);
1556 } else {
1557 this->overlay->RebuildCache();
1560 _smallmap_industry_highlight_state = !_smallmap_industry_highlight_state;
1562 this->refresh = _smallmap_industry_highlight != INVALID_INDUSTRYTYPE ? BLINK_PERIOD : FORCE_REFRESH_PERIOD;
1563 this->SetDirty();
1567 * Set new #scroll_x, #scroll_y, and #subscroll values after limiting them such that the center
1568 * of the smallmap always contains a part of the map.
1569 * @param sx Proposed new #scroll_x
1570 * @param sy Proposed new #scroll_y
1571 * @param sub Proposed new #subscroll
1573 void SmallMapWindow::SetNewScroll(int sx, int sy, int sub)
1575 const NWidgetBase *wi = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1576 Point hv = InverseRemapCoords(wi->current_x * ZOOM_LVL_BASE * TILE_SIZE / 2, wi->current_y * ZOOM_LVL_BASE * TILE_SIZE / 2);
1577 hv.x *= this->zoom;
1578 hv.y *= this->zoom;
1580 if (sx < -hv.x) {
1581 sx = -hv.x;
1582 sub = 0;
1584 if (sx > (int)(MapMaxX() * TILE_SIZE) - hv.x) {
1585 sx = MapMaxX() * TILE_SIZE - hv.x;
1586 sub = 0;
1588 if (sy < -hv.y) {
1589 sy = -hv.y;
1590 sub = 0;
1592 if (sy > (int)(MapMaxY() * TILE_SIZE) - hv.y) {
1593 sy = MapMaxY() * TILE_SIZE - hv.y;
1594 sub = 0;
1597 this->scroll_x = sx;
1598 this->scroll_y = sy;
1599 this->subscroll = sub;
1600 if (this->map_type == SMT_LINKSTATS) this->overlay->RebuildCache();
1603 /* virtual */ void SmallMapWindow::OnScroll(Point delta)
1605 _cursor.fix_at = true;
1607 /* While tile is at (delta.x, delta.y)? */
1608 int sub;
1609 Point pt = this->PixelToTile(delta.x, delta.y, &sub);
1610 this->SetNewScroll(this->scroll_x + pt.x * TILE_SIZE, this->scroll_y + pt.y * TILE_SIZE, sub);
1612 this->SetDirty();
1615 void SmallMapWindow::SmallMapCenterOnCurrentPos()
1617 const ViewPort *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
1618 Point pt = InverseRemapCoords(vp->virtual_left + vp->virtual_width / 2, vp->virtual_top + vp->virtual_height / 2);
1620 int sub;
1621 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1622 Point sxy = this->ComputeScroll(pt.x / TILE_SIZE, pt.y / TILE_SIZE, max(0, (int)wid->current_x / 2 - 2), wid->current_y / 2, &sub);
1623 this->SetNewScroll(sxy.x, sxy.y, sub);
1624 this->SetDirty();
1628 * Get the center of the given station as point on the screen in the smallmap window.
1629 * @param st Station to find in the smallmap.
1630 * @return Point with coordinates of the station.
1632 Point SmallMapWindow::GetStationMiddle(const Station *st) const
1634 int x = (st->rect.right + st->rect.left + 1) / 2;
1635 int y = (st->rect.bottom + st->rect.top + 1) / 2;
1636 Point ret = this->RemapTile(x, y);
1638 /* Same magic 3 as in DrawVehicles; that's where I got it from.
1639 * No idea what it is, but without it the result looks bad.
1641 ret.x -= 3 + this->subscroll;
1642 return ret;
1645 SmallMapWindow::SmallMapType SmallMapWindow::map_type = SMT_CONTOUR;
1646 bool SmallMapWindow::show_towns = true;
1649 * Custom container class for displaying smallmap with a vertically resizing legend panel.
1650 * The legend panel has a smallest height that depends on its width. Standard containers cannot handle this case.
1652 * @note The container assumes it has two children, the first is the display, the second is the bar with legends and selection image buttons.
1653 * Both children should be both horizontally and vertically resizable and horizontally fillable.
1654 * The bar should have a minimal size with a zero-size legends display. Child padding is not supported.
1656 class NWidgetSmallmapDisplay : public NWidgetContainer {
1657 const SmallMapWindow *smallmap_window; ///< Window manager instance.
1658 public:
1659 NWidgetSmallmapDisplay() : NWidgetContainer(NWID_VERTICAL)
1661 this->smallmap_window = NULL;
1664 virtual void SetupSmallestSize(Window *w, bool init_array)
1666 NWidgetBase *display = this->head;
1667 NWidgetBase *bar = display->next;
1669 display->SetupSmallestSize(w, init_array);
1670 bar->SetupSmallestSize(w, init_array);
1672 this->smallmap_window = dynamic_cast<SmallMapWindow *>(w);
1673 assert(this->smallmap_window != NULL);
1674 this->smallest_x = max(display->smallest_x, bar->smallest_x + smallmap_window->GetMinLegendWidth());
1675 this->smallest_y = display->smallest_y + max(bar->smallest_y, smallmap_window->GetLegendHeight(smallmap_window->min_number_of_columns));
1676 this->fill_x = max(display->fill_x, bar->fill_x);
1677 this->fill_y = (display->fill_y == 0 && bar->fill_y == 0) ? 0 : min(display->fill_y, bar->fill_y);
1678 this->resize_x = max(display->resize_x, bar->resize_x);
1679 this->resize_y = min(display->resize_y, bar->resize_y);
1682 virtual void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
1684 this->pos_x = x;
1685 this->pos_y = y;
1686 this->current_x = given_width;
1687 this->current_y = given_height;
1689 NWidgetBase *display = this->head;
1690 NWidgetBase *bar = display->next;
1692 if (sizing == ST_SMALLEST) {
1693 this->smallest_x = given_width;
1694 this->smallest_y = given_height;
1695 /* Make display and bar exactly equal to their minimal size. */
1696 display->AssignSizePosition(ST_SMALLEST, x, y, display->smallest_x, display->smallest_y, rtl);
1697 bar->AssignSizePosition(ST_SMALLEST, x, y + display->smallest_y, bar->smallest_x, bar->smallest_y, rtl);
1700 uint bar_height = max(bar->smallest_y, this->smallmap_window->GetLegendHeight(this->smallmap_window->GetNumberColumnsLegend(given_width - bar->smallest_x)));
1701 uint display_height = given_height - bar_height;
1702 display->AssignSizePosition(ST_RESIZE, x, y, given_width, display_height, rtl);
1703 bar->AssignSizePosition(ST_RESIZE, x, y + display_height, given_width, bar_height, rtl);
1706 virtual NWidgetCore *GetWidgetFromPos(int x, int y)
1708 if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
1709 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1710 NWidgetCore *widget = child_wid->GetWidgetFromPos(x, y);
1711 if (widget != NULL) return widget;
1713 return NULL;
1716 virtual void Draw(const Window *w)
1718 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) child_wid->Draw(w);
1722 /** Widget parts of the smallmap display. */
1723 static const NWidgetPart _nested_smallmap_display[] = {
1724 NWidget(WWT_PANEL, COLOUR_BROWN, WID_SM_MAP_BORDER),
1725 NWidget(WWT_INSET, COLOUR_BROWN, WID_SM_MAP), SetMinimalSize(346, 140), SetResize(1, 1), SetPadding(2, 2, 2, 2), EndContainer(),
1726 EndContainer(),
1729 /** Widget parts of the smallmap legend bar + image buttons. */
1730 static const NWidgetPart _nested_smallmap_bar[] = {
1731 NWidget(WWT_PANEL, COLOUR_BROWN),
1732 NWidget(NWID_HORIZONTAL),
1733 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SM_LEGEND), SetResize(1, 1),
1734 NWidget(NWID_VERTICAL),
1735 /* Top button row. */
1736 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1737 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_SM_ZOOM_IN),
1738 SetDataTip(SPR_IMG_ZOOMIN, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_IN), SetFill(1, 1),
1739 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_SM_CENTERMAP),
1740 SetDataTip(SPR_IMG_SMALLMAP, STR_SMALLMAP_CENTER), SetFill(1, 1),
1741 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_BLANK),
1742 SetDataTip(SPR_DOT_SMALL, STR_NULL), SetFill(1, 1),
1743 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_CONTOUR),
1744 SetDataTip(SPR_IMG_SHOW_COUNTOURS, STR_SMALLMAP_TOOLTIP_SHOW_LAND_CONTOURS_ON_MAP), SetFill(1, 1),
1745 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_VEHICLES),
1746 SetDataTip(SPR_IMG_SHOW_VEHICLES, STR_SMALLMAP_TOOLTIP_SHOW_VEHICLES_ON_MAP), SetFill(1, 1),
1747 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_INDUSTRIES),
1748 SetDataTip(SPR_IMG_INDUSTRY, STR_SMALLMAP_TOOLTIP_SHOW_INDUSTRIES_ON_MAP), SetFill(1, 1),
1749 EndContainer(),
1750 /* Bottom button row. */
1751 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1752 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_SM_ZOOM_OUT),
1753 SetDataTip(SPR_IMG_ZOOMOUT, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_OUT), SetFill(1, 1),
1754 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_TOGGLETOWNNAME),
1755 SetDataTip(SPR_IMG_TOWN, STR_SMALLMAP_TOOLTIP_TOGGLE_TOWN_NAMES_ON_OFF), SetFill(1, 1),
1756 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_LINKSTATS),
1757 SetDataTip(SPR_IMG_CARGOFLOW, STR_SMALLMAP_TOOLTIP_SHOW_LINK_STATS_ON_MAP), SetFill(1, 1),
1758 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_ROUTES),
1759 SetDataTip(SPR_IMG_SHOW_ROUTES, STR_SMALLMAP_TOOLTIP_SHOW_TRANSPORT_ROUTES_ON), SetFill(1, 1),
1760 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_VEGETATION),
1761 SetDataTip(SPR_IMG_PLANTTREES, STR_SMALLMAP_TOOLTIP_SHOW_VEGETATION_ON_MAP), SetFill(1, 1),
1762 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_OWNERS),
1763 SetDataTip(SPR_IMG_COMPANY_GENERAL, STR_SMALLMAP_TOOLTIP_SHOW_LAND_OWNERS_ON_MAP), SetFill(1, 1),
1764 EndContainer(),
1765 NWidget(NWID_SPACER), SetResize(0, 1),
1766 EndContainer(),
1767 EndContainer(),
1768 EndContainer(),
1771 static NWidgetBase *SmallMapDisplay(int *biggest_index)
1773 NWidgetContainer *map_display = new NWidgetSmallmapDisplay;
1775 MakeNWidgets(_nested_smallmap_display, lengthof(_nested_smallmap_display), biggest_index, map_display);
1776 MakeNWidgets(_nested_smallmap_bar, lengthof(_nested_smallmap_bar), biggest_index, map_display);
1777 return map_display;
1781 static const NWidgetPart _nested_smallmap_widgets[] = {
1782 NWidget(NWID_HORIZONTAL),
1783 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
1784 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_SM_CAPTION), SetDataTip(STR_SMALLMAP_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1785 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
1786 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
1787 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
1788 EndContainer(),
1789 NWidgetFunction(SmallMapDisplay), // Smallmap display and legend bar + image buttons.
1790 /* Bottom button row and resize box. */
1791 NWidget(NWID_HORIZONTAL),
1792 NWidget(WWT_PANEL, COLOUR_BROWN),
1793 NWidget(NWID_HORIZONTAL),
1794 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_SM_SELECT_BUTTONS),
1795 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1796 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_SM_ENABLE_ALL), SetDataTip(STR_SMALLMAP_ENABLE_ALL, STR_NULL),
1797 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_SM_DISABLE_ALL), SetDataTip(STR_SMALLMAP_DISABLE_ALL, STR_NULL),
1798 NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_SM_SHOW_HEIGHT), SetDataTip(STR_SMALLMAP_SHOW_HEIGHT, STR_SMALLMAP_TOOLTIP_SHOW_HEIGHT),
1799 EndContainer(),
1800 NWidget(NWID_SPACER), SetFill(1, 1),
1801 EndContainer(),
1802 NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
1803 EndContainer(),
1804 EndContainer(),
1805 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
1806 EndContainer(),
1809 static WindowDesc _smallmap_desc(
1810 WDP_AUTO, "smallmap", 484, 314,
1811 WC_SMALLMAP, WC_NONE,
1813 _nested_smallmap_widgets, lengthof(_nested_smallmap_widgets)
1817 * Show the smallmap window.
1819 void ShowSmallMap()
1821 AllocateWindowDescFront<SmallMapWindow>(&_smallmap_desc, 0);
1825 * Scrolls the main window to given coordinates.
1826 * @param x x coordinate
1827 * @param y y coordinate
1828 * @param z z coordinate; -1 to scroll to terrain height
1829 * @param instant scroll instantly (meaningful only when smooth_scrolling is active)
1830 * @return did the viewport position change?
1832 bool ScrollMainWindowTo(int x, int y, int z, bool instant)
1834 bool res = ScrollWindowTo(x, y, z, FindWindowById(WC_MAIN_WINDOW, 0), instant);
1836 /* If a user scrolls to a tile (via what way what so ever) and already is on
1837 * that tile (e.g.: pressed twice), move the smallmap to that location,
1838 * so you directly see where you are on the smallmap. */
1840 if (res) return res;
1842 SmallMapWindow *w = dynamic_cast<SmallMapWindow*>(FindWindowById(WC_SMALLMAP, 0));
1843 if (w != NULL) w->SmallMapCenterOnCurrentPos();
1845 return res;