Add: Draw network password indicator lock in company drop down list. (#7079)
[openttd-github.git] / src / smallmap_gui.cpp
blobdf9fd2df2357ba4f1781f673a104279c1a0a7eb6
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 "clear_map.h"
14 #include "industry.h"
15 #include "station_map.h"
16 #include "landscape.h"
17 #include "tree_map.h"
18 #include "viewport_func.h"
19 #include "town.h"
20 #include "tunnelbridge_map.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 "guitimer_func.h"
28 #include "smallmap_gui.h"
30 #include "table/strings.h"
32 #include <bitset>
34 #include "safeguards.h"
36 static int _smallmap_industry_count; ///< Number of used industries
37 static int _smallmap_company_count; ///< Number of entries in the owner legend.
38 static int _smallmap_cargo_count; ///< Number of cargos in the link stats legend.
40 /** Link stat colours shown in legenda. */
41 static uint8 _linkstat_colours_in_legenda[] = {0, 1, 3, 5, 7, 9, 11};
43 static const int NUM_NO_COMPANY_ENTRIES = 4; ///< Number of entries in the owner legend that are not companies.
45 static const uint8 PC_ROUGH_LAND = 0x52; ///< Dark green palette colour for rough land.
46 static const uint8 PC_GRASS_LAND = 0x54; ///< Dark green palette colour for grass land.
47 static const uint8 PC_BARE_LAND = 0x37; ///< Brown palette colour for bare land.
48 static const uint8 PC_FIELDS = 0x25; ///< Light brown palette colour for fields.
49 static const uint8 PC_TREES = 0x57; ///< Green palette colour for trees.
50 static const uint8 PC_WATER = 0xCA; ///< Dark blue palette colour for water.
52 /** Macro for ordinary entry of LegendAndColour */
53 #define MK(a, b) {a, b, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, false, false}
55 /** Macro for a height legend entry with configurable colour. */
56 #define MC(col_break) {0, STR_TINY_BLACK_HEIGHT, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, false, col_break}
58 /** Macro for non-company owned property entry of LegendAndColour */
59 #define MO(a, b) {a, b, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, false, false}
61 /** Macro used for forcing a rebuild of the owner legend the first time it is used. */
62 #define MOEND() {0, 0, INVALID_INDUSTRYTYPE, 0, OWNER_NONE, true, true, false}
64 /** Macro for end of list marker in arrays of LegendAndColour */
65 #define MKEND() {0, STR_NULL, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, true, false}
67 /**
68 * Macro for break marker in arrays of LegendAndColour.
69 * It will have valid data, though
71 #define MS(a, b) {a, b, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, false, true}
73 /** Legend text giving the colours to look for on the minimap */
74 static LegendAndColour _legend_land_contours[] = {
75 MK(PC_BLACK, STR_SMALLMAP_LEGENDA_ROADS),
76 MK(PC_GREY, STR_SMALLMAP_LEGENDA_RAILROADS),
77 MK(PC_LIGHT_BLUE, STR_SMALLMAP_LEGENDA_STATIONS_AIRPORTS_DOCKS),
78 MK(PC_DARK_RED, STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES),
79 MK(PC_WHITE, STR_SMALLMAP_LEGENDA_VEHICLES),
81 /* Placeholders for the colours and heights of the legend.
82 * The following values are set at BuildLandLegend() based
83 * on each colour scheme and the maximum map height. */
84 MC(true),
85 MC(false),
86 MC(false),
87 MC(false),
88 MC(false),
89 MC(false),
90 MC(true),
91 MC(false),
92 MC(false),
93 MC(false),
94 MC(false),
95 MC(false),
96 MKEND()
99 static const LegendAndColour _legend_vehicles[] = {
100 MK(PC_RED, STR_SMALLMAP_LEGENDA_TRAINS),
101 MK(PC_YELLOW, STR_SMALLMAP_LEGENDA_ROAD_VEHICLES),
102 MK(PC_LIGHT_BLUE, STR_SMALLMAP_LEGENDA_SHIPS),
103 MK(PC_WHITE, STR_SMALLMAP_LEGENDA_AIRCRAFT),
105 MS(PC_BLACK, STR_SMALLMAP_LEGENDA_TRANSPORT_ROUTES),
106 MK(PC_DARK_RED, STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES),
107 MKEND()
110 static const LegendAndColour _legend_routes[] = {
111 MK(PC_BLACK, STR_SMALLMAP_LEGENDA_ROADS),
112 MK(PC_GREY, STR_SMALLMAP_LEGENDA_RAILROADS),
113 MK(PC_DARK_RED, STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES),
115 MS(PC_VERY_DARK_BROWN, STR_SMALLMAP_LEGENDA_RAILROAD_STATION),
116 MK(PC_ORANGE, STR_SMALLMAP_LEGENDA_TRUCK_LOADING_BAY),
117 MK(PC_YELLOW, STR_SMALLMAP_LEGENDA_BUS_STATION),
118 MK(PC_RED, STR_SMALLMAP_LEGENDA_AIRPORT_HELIPORT),
119 MK(PC_LIGHT_BLUE, STR_SMALLMAP_LEGENDA_DOCK),
120 MKEND()
123 static const LegendAndColour _legend_vegetation[] = {
124 MK(PC_ROUGH_LAND, STR_SMALLMAP_LEGENDA_ROUGH_LAND),
125 MK(PC_GRASS_LAND, STR_SMALLMAP_LEGENDA_GRASS_LAND),
126 MK(PC_BARE_LAND, STR_SMALLMAP_LEGENDA_BARE_LAND),
127 MK(PC_FIELDS, STR_SMALLMAP_LEGENDA_FIELDS),
128 MK(PC_TREES, STR_SMALLMAP_LEGENDA_TREES),
129 MK(PC_GREEN, STR_SMALLMAP_LEGENDA_FOREST),
131 MS(PC_GREY, STR_SMALLMAP_LEGENDA_ROCKS),
132 MK(PC_ORANGE, STR_SMALLMAP_LEGENDA_DESERT),
133 MK(PC_LIGHT_BLUE, STR_SMALLMAP_LEGENDA_SNOW),
134 MK(PC_BLACK, STR_SMALLMAP_LEGENDA_TRANSPORT_ROUTES),
135 MK(PC_DARK_RED, STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES),
136 MKEND()
139 static LegendAndColour _legend_land_owners[NUM_NO_COMPANY_ENTRIES + MAX_COMPANIES + 1] = {
140 MO(PC_WATER, STR_SMALLMAP_LEGENDA_WATER),
141 MO(0x00, STR_SMALLMAP_LEGENDA_NO_OWNER), // This colour will vary depending on settings.
142 MO(PC_DARK_RED, STR_SMALLMAP_LEGENDA_TOWNS),
143 MO(PC_DARK_GREY, STR_SMALLMAP_LEGENDA_INDUSTRIES),
144 /* The legend will be terminated the first time it is used. */
145 MOEND(),
148 #undef MK
149 #undef MC
150 #undef MS
151 #undef MO
152 #undef MOEND
153 #undef MKEND
155 /** Legend entries for the link stats view. */
156 static LegendAndColour _legend_linkstats[NUM_CARGO + lengthof(_linkstat_colours_in_legenda) + 1];
158 * Allow room for all industries, plus a terminator entry
159 * This is required in order to have the industry slots all filled up
161 static LegendAndColour _legend_from_industries[NUM_INDUSTRYTYPES + 1];
162 /** For connecting industry type to position in industries list(small map legend) */
163 static uint _industry_to_list_pos[NUM_INDUSTRYTYPES];
164 /** Show heightmap in industry and owner mode of smallmap window. */
165 static bool _smallmap_show_heightmap = false;
166 /** Highlight a specific industry type */
167 static IndustryType _smallmap_industry_highlight = INVALID_INDUSTRYTYPE;
168 /** State of highlight blinking */
169 static bool _smallmap_industry_highlight_state;
170 /** For connecting company ID to position in owner list (small map legend) */
171 static uint _company_to_list_pos[MAX_COMPANIES];
174 * Fills an array for the industries legends.
176 void BuildIndustriesLegend()
178 uint j = 0;
180 /* Add each name */
181 for (uint i = 0; i < NUM_INDUSTRYTYPES; i++) {
182 IndustryType ind = _sorted_industry_types[i];
183 const IndustrySpec *indsp = GetIndustrySpec(ind);
184 if (indsp->enabled) {
185 _legend_from_industries[j].legend = indsp->name;
186 _legend_from_industries[j].colour = indsp->map_colour;
187 _legend_from_industries[j].type = ind;
188 _legend_from_industries[j].show_on_map = true;
189 _legend_from_industries[j].col_break = false;
190 _legend_from_industries[j].end = false;
192 /* Store widget number for this industry type. */
193 _industry_to_list_pos[ind] = j;
194 j++;
197 /* Terminate the list */
198 _legend_from_industries[j].end = true;
200 /* Store number of enabled industries */
201 _smallmap_industry_count = j;
205 * Populate legend table for the link stat view.
207 void BuildLinkStatsLegend()
209 /* Clear the legend */
210 memset(_legend_linkstats, 0, sizeof(_legend_linkstats));
212 uint i = 0;
213 for (; i < _sorted_cargo_specs_size; ++i) {
214 const CargoSpec *cs = _sorted_cargo_specs[i];
216 _legend_linkstats[i].legend = cs->name;
217 _legend_linkstats[i].colour = cs->legend_colour;
218 _legend_linkstats[i].type = cs->Index();
219 _legend_linkstats[i].show_on_map = true;
222 _legend_linkstats[i].col_break = true;
223 _smallmap_cargo_count = i;
225 for (; i < _smallmap_cargo_count + lengthof(_linkstat_colours_in_legenda); ++i) {
226 _legend_linkstats[i].legend = STR_EMPTY;
227 _legend_linkstats[i].colour = LinkGraphOverlay::LINK_COLOURS[_linkstat_colours_in_legenda[i - _smallmap_cargo_count]];
228 _legend_linkstats[i].show_on_map = true;
231 _legend_linkstats[_smallmap_cargo_count].legend = STR_LINKGRAPH_LEGEND_UNUSED;
232 _legend_linkstats[i - 1].legend = STR_LINKGRAPH_LEGEND_OVERLOADED;
233 _legend_linkstats[(_smallmap_cargo_count + i - 1) / 2].legend = STR_LINKGRAPH_LEGEND_SATURATED;
234 _legend_linkstats[i].end = true;
237 static const LegendAndColour * const _legend_table[] = {
238 _legend_land_contours,
239 _legend_vehicles,
240 _legend_from_industries,
241 _legend_linkstats,
242 _legend_routes,
243 _legend_vegetation,
244 _legend_land_owners,
247 #define MKCOLOUR(x) TO_LE32X(x)
249 #define MKCOLOUR_XXXX(x) (MKCOLOUR(0x01010101) * (uint)(x))
250 #define MKCOLOUR_X0X0(x) (MKCOLOUR(0x01000100) * (uint)(x))
251 #define MKCOLOUR_0X0X(x) (MKCOLOUR(0x00010001) * (uint)(x))
252 #define MKCOLOUR_0XX0(x) (MKCOLOUR(0x00010100) * (uint)(x))
253 #define MKCOLOUR_X00X(x) (MKCOLOUR(0x01000001) * (uint)(x))
255 #define MKCOLOUR_XYXY(x, y) (MKCOLOUR_X0X0(x) | MKCOLOUR_0X0X(y))
256 #define MKCOLOUR_XYYX(x, y) (MKCOLOUR_X00X(x) | MKCOLOUR_0XX0(y))
258 #define MKCOLOUR_0000 MKCOLOUR_XXXX(0x00)
259 #define MKCOLOUR_0FF0 MKCOLOUR_0XX0(0xFF)
260 #define MKCOLOUR_F00F MKCOLOUR_X00X(0xFF)
261 #define MKCOLOUR_FFFF MKCOLOUR_XXXX(0xFF)
263 #include "table/heightmap_colours.h"
265 /** Colour scheme of the smallmap. */
266 struct SmallMapColourScheme {
267 uint32 *height_colours; ///< Cached colours for each level in a map.
268 const uint32 *height_colours_base; ///< Base table for determining the colours
269 size_t colour_count; ///< The number of colours.
270 uint32 default_colour; ///< Default colour of the land.
273 /** Available colour schemes for height maps. */
274 static SmallMapColourScheme _heightmap_schemes[] = {
275 {NULL, _green_map_heights, lengthof(_green_map_heights), MKCOLOUR_XXXX(0x54)}, ///< Green colour scheme.
276 {NULL, _dark_green_map_heights, lengthof(_dark_green_map_heights), MKCOLOUR_XXXX(0x62)}, ///< Dark green colour scheme.
277 {NULL, _violet_map_heights, lengthof(_violet_map_heights), MKCOLOUR_XXXX(0x82)}, ///< Violet colour scheme.
281 * (Re)build the colour tables for the legends.
283 void BuildLandLegend()
285 /* The smallmap window has never been initialized, so no need to change the legend. */
286 if (_heightmap_schemes[0].height_colours == NULL) return;
289 * The general idea of this function is to fill the legend with an appropriate evenly spaced
290 * selection of height levels. All entries with STR_TINY_BLACK_HEIGHT are reserved for this.
291 * At the moment there are twelve of these.
293 * The table below defines up to which height level a particular delta in the legend should be
294 * used. One could opt for just dividing the maximum height and use that as delta, but that
295 * creates many "ugly" legend labels, e.g. once every 950 meter. As a result, this table will
296 * reduce the number of deltas to 7: every 100m, 200m, 300m, 500m, 750m, 1000m and 1250m. The
297 * deltas are closer together at the lower numbers because going from 12 entries to just 4, as
298 * would happen when replacing 200m and 300m by 250m, would mean the legend would be short and
299 * that might not be considered appropriate.
301 * The current method yields at least 7 legend entries and at most 12. It can be increased to
302 * 8 by adding a 150m and 400m option, but especially 150m creates ugly heights.
304 * It tries to evenly space the legend items over the two columns that are there for the legend.
307 /* Table for delta; if max_height is less than the first column, use the second column as value. */
308 uint deltas[][2] = { { 24, 2 }, { 48, 4 }, { 72, 6 }, { 120, 10 }, { 180, 15 }, { 240, 20 }, { MAX_TILE_HEIGHT + 1, 25 }};
309 uint i = 0;
310 for (; _settings_game.construction.max_heightlevel >= deltas[i][0]; i++) {
311 /* Nothing to do here. */
313 uint delta = deltas[i][1];
315 int total_entries = (_settings_game.construction.max_heightlevel / delta) + 1;
316 int rows = CeilDiv(total_entries, 2);
317 int j = 0;
319 for (i = 0; i < lengthof(_legend_land_contours) - 1 && j < total_entries; i++) {
320 if (_legend_land_contours[i].legend != STR_TINY_BLACK_HEIGHT) continue;
322 _legend_land_contours[i].col_break = j % rows == 0;
323 _legend_land_contours[i].end = false;
324 _legend_land_contours[i].height = j * delta;
325 _legend_land_contours[i].colour = _heightmap_schemes[_settings_client.gui.smallmap_land_colour].height_colours[j * delta];
326 j++;
328 _legend_land_contours[i].end = true;
332 * Completes the array for the owned property legend.
334 void BuildOwnerLegend()
336 _legend_land_owners[1].colour = _heightmap_schemes[_settings_client.gui.smallmap_land_colour].default_colour;
338 int i = NUM_NO_COMPANY_ENTRIES;
339 const Company *c;
340 FOR_ALL_COMPANIES(c) {
341 _legend_land_owners[i].colour = _colour_gradient[c->colour][5];
342 _legend_land_owners[i].company = c->index;
343 _legend_land_owners[i].show_on_map = true;
344 _legend_land_owners[i].col_break = false;
345 _legend_land_owners[i].end = false;
346 _company_to_list_pos[c->index] = i;
347 i++;
350 /* Terminate the list */
351 _legend_land_owners[i].end = true;
353 /* Store maximum amount of owner legend entries. */
354 _smallmap_company_count = i;
357 struct AndOr {
358 uint32 mor;
359 uint32 mand;
362 static inline uint32 ApplyMask(uint32 colour, const AndOr *mask)
364 return (colour & mask->mand) | mask->mor;
368 /** Colour masks for "Contour" and "Routes" modes. */
369 static const AndOr _smallmap_contours_andor[] = {
370 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_CLEAR
371 {MKCOLOUR_0XX0(PC_GREY ), MKCOLOUR_F00F}, // MP_RAILWAY
372 {MKCOLOUR_0XX0(PC_BLACK ), MKCOLOUR_F00F}, // MP_ROAD
373 {MKCOLOUR_0XX0(PC_DARK_RED ), MKCOLOUR_F00F}, // MP_HOUSE
374 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_TREES
375 {MKCOLOUR_XXXX(PC_LIGHT_BLUE), MKCOLOUR_0000}, // MP_STATION
376 {MKCOLOUR_XXXX(PC_WATER ), MKCOLOUR_0000}, // MP_WATER
377 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_VOID
378 {MKCOLOUR_XXXX(PC_DARK_RED ), MKCOLOUR_0000}, // MP_INDUSTRY
379 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_TUNNELBRIDGE
380 {MKCOLOUR_0XX0(PC_DARK_RED ), MKCOLOUR_F00F}, // MP_OBJECT
381 {MKCOLOUR_0XX0(PC_GREY ), MKCOLOUR_F00F},
384 /** Colour masks for "Vehicles", "Industry", and "Vegetation" modes. */
385 static const AndOr _smallmap_vehicles_andor[] = {
386 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_CLEAR
387 {MKCOLOUR_0XX0(PC_BLACK ), MKCOLOUR_F00F}, // MP_RAILWAY
388 {MKCOLOUR_0XX0(PC_BLACK ), MKCOLOUR_F00F}, // MP_ROAD
389 {MKCOLOUR_0XX0(PC_DARK_RED ), MKCOLOUR_F00F}, // MP_HOUSE
390 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_TREES
391 {MKCOLOUR_0XX0(PC_BLACK ), MKCOLOUR_F00F}, // MP_STATION
392 {MKCOLOUR_XXXX(PC_WATER ), MKCOLOUR_0000}, // MP_WATER
393 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_VOID
394 {MKCOLOUR_XXXX(PC_DARK_RED ), MKCOLOUR_0000}, // MP_INDUSTRY
395 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_TUNNELBRIDGE
396 {MKCOLOUR_0XX0(PC_DARK_RED ), MKCOLOUR_F00F}, // MP_OBJECT
397 {MKCOLOUR_0XX0(PC_BLACK ), MKCOLOUR_F00F},
400 /** Mapping of tile type to importance of the tile (higher number means more interesting to show). */
401 static const byte _tiletype_importance[] = {
402 2, // MP_CLEAR
403 8, // MP_RAILWAY
404 7, // MP_ROAD
405 5, // MP_HOUSE
406 2, // MP_TREES
407 9, // MP_STATION
408 2, // MP_WATER
409 1, // MP_VOID
410 6, // MP_INDUSTRY
411 8, // MP_TUNNELBRIDGE
412 2, // MP_OBJECT
418 * Return the colour a tile would be displayed with in the small map in mode "Contour".
419 * @param tile The tile of which we would like to get the colour.
420 * @param t Effective tile type of the tile (see #SmallMapWindow::GetTileColours).
421 * @return The colour of tile in the small map in mode "Contour"
423 static inline uint32 GetSmallMapContoursPixels(TileIndex tile, TileType t)
425 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
426 return ApplyMask(cs->height_colours[TileHeight(tile)], &_smallmap_contours_andor[t]);
430 * Return the colour a tile would be displayed with in the small map in mode "Vehicles".
432 * @param tile The tile of which we would like to get the colour.
433 * @param t Effective tile type of the tile (see #SmallMapWindow::GetTileColours).
434 * @return The colour of tile in the small map in mode "Vehicles"
436 static inline uint32 GetSmallMapVehiclesPixels(TileIndex tile, TileType t)
438 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
439 return ApplyMask(cs->default_colour, &_smallmap_vehicles_andor[t]);
443 * Return the colour a tile would be displayed with in the small map in mode "Industries".
445 * @param tile The tile of which we would like to get the colour.
446 * @param t Effective tile type of the tile (see #SmallMapWindow::GetTileColours).
447 * @return The colour of tile in the small map in mode "Industries"
449 static inline uint32 GetSmallMapIndustriesPixels(TileIndex tile, TileType t)
451 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
452 return ApplyMask(_smallmap_show_heightmap ? cs->height_colours[TileHeight(tile)] : cs->default_colour, &_smallmap_vehicles_andor[t]);
456 * Return the colour a tile would be displayed with in the small map in mode "Routes".
458 * @param tile The tile of which we would like to get the colour.
459 * @param t Effective tile type of the tile (see #SmallMapWindow::GetTileColours).
460 * @return The colour of tile in the small map in mode "Routes"
462 static inline uint32 GetSmallMapRoutesPixels(TileIndex tile, TileType t)
464 if (t == MP_STATION) {
465 switch (GetStationType(tile)) {
466 case STATION_RAIL: return MKCOLOUR_XXXX(PC_VERY_DARK_BROWN);
467 case STATION_AIRPORT: return MKCOLOUR_XXXX(PC_RED);
468 case STATION_TRUCK: return MKCOLOUR_XXXX(PC_ORANGE);
469 case STATION_BUS: return MKCOLOUR_XXXX(PC_YELLOW);
470 case STATION_DOCK: return MKCOLOUR_XXXX(PC_LIGHT_BLUE);
471 default: return MKCOLOUR_FFFF;
473 } else if (t == MP_RAILWAY) {
474 AndOr andor = {
475 MKCOLOUR_0XX0(GetRailTypeInfo(GetRailType(tile))->map_colour),
476 _smallmap_contours_andor[t].mand
479 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
480 return ApplyMask(cs->default_colour, &andor);
483 /* Ground colour */
484 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
485 return ApplyMask(cs->default_colour, &_smallmap_contours_andor[t]);
489 * Return the colour a tile would be displayed with in the small map in mode "link stats".
491 * @param tile The tile of which we would like to get the colour.
492 * @param t Effective tile type of the tile (see #SmallMapWindow::GetTileColours).
493 * @return The colour of tile in the small map in mode "link stats"
495 static inline uint32 GetSmallMapLinkStatsPixels(TileIndex tile, TileType t)
497 return _smallmap_show_heightmap ? GetSmallMapContoursPixels(tile, t) : GetSmallMapRoutesPixels(tile, t);
500 static const uint32 _vegetation_clear_bits[] = {
501 MKCOLOUR_XXXX(PC_GRASS_LAND), ///< full grass
502 MKCOLOUR_XXXX(PC_ROUGH_LAND), ///< rough land
503 MKCOLOUR_XXXX(PC_GREY), ///< rocks
504 MKCOLOUR_XXXX(PC_FIELDS), ///< fields
505 MKCOLOUR_XXXX(PC_LIGHT_BLUE), ///< snow
506 MKCOLOUR_XXXX(PC_ORANGE), ///< desert
507 MKCOLOUR_XXXX(PC_GRASS_LAND), ///< unused
508 MKCOLOUR_XXXX(PC_GRASS_LAND), ///< unused
512 * Return the colour a tile would be displayed with in the smallmap in mode "Vegetation".
514 * @param tile The tile of which we would like to get the colour.
515 * @param t Effective tile type of the tile (see #SmallMapWindow::GetTileColours).
516 * @return The colour of tile in the smallmap in mode "Vegetation"
518 static inline uint32 GetSmallMapVegetationPixels(TileIndex tile, TileType t)
520 switch (t) {
521 case MP_CLEAR:
522 return (IsClearGround(tile, CLEAR_GRASS) && GetClearDensity(tile) < 3) ? MKCOLOUR_XXXX(PC_BARE_LAND) : _vegetation_clear_bits[GetClearGround(tile)];
524 case MP_INDUSTRY:
525 return IsTileForestIndustry(tile) ? MKCOLOUR_XXXX(PC_GREEN) : MKCOLOUR_XXXX(PC_DARK_RED);
527 case MP_TREES:
528 if (GetTreeGround(tile) == TREE_GROUND_SNOW_DESERT || GetTreeGround(tile) == TREE_GROUND_ROUGH_SNOW) {
529 return (_settings_game.game_creation.landscape == LT_ARCTIC) ? MKCOLOUR_XYYX(PC_LIGHT_BLUE, PC_TREES) : MKCOLOUR_XYYX(PC_ORANGE, PC_TREES);
531 return MKCOLOUR_XYYX(PC_GRASS_LAND, PC_TREES);
533 default:
534 return ApplyMask(MKCOLOUR_XXXX(PC_GRASS_LAND), &_smallmap_vehicles_andor[t]);
539 * Return the colour a tile would be displayed with in the small map in mode "Owner".
541 * @param tile The tile of which we would like to get the colour.
542 * @param t Effective tile type of the tile (see #SmallMapWindow::GetTileColours).
543 * @return The colour of tile in the small map in mode "Owner"
545 static inline uint32 GetSmallMapOwnerPixels(TileIndex tile, TileType t)
547 Owner o;
549 switch (t) {
550 case MP_INDUSTRY: return MKCOLOUR_XXXX(PC_DARK_GREY);
551 case MP_HOUSE: return MKCOLOUR_XXXX(PC_DARK_RED);
552 default: o = GetTileOwner(tile); break;
553 /* FIXME: For MP_ROAD there are multiple owners.
554 * GetTileOwner returns the rail owner (level crossing) resp. the owner of ROADTYPE_ROAD (normal road),
555 * even if there are no ROADTYPE_ROAD bits on the tile.
559 if ((o < MAX_COMPANIES && !_legend_land_owners[_company_to_list_pos[o]].show_on_map) || o == OWNER_NONE || o == OWNER_WATER) {
560 if (t == MP_WATER) return MKCOLOUR_XXXX(PC_WATER);
561 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
562 return _smallmap_show_heightmap ? cs->height_colours[TileHeight(tile)] : cs->default_colour;
563 } else if (o == OWNER_TOWN) {
564 return MKCOLOUR_XXXX(PC_DARK_RED);
567 return MKCOLOUR_XXXX(_legend_land_owners[_company_to_list_pos[o]].colour);
570 /** Vehicle colours in #SMT_VEHICLES mode. Indexed by #VehicleTypeByte. */
571 static const byte _vehicle_type_colours[6] = {
572 PC_RED, PC_YELLOW, PC_LIGHT_BLUE, PC_WHITE, PC_BLACK, PC_RED
576 /** Notify the industry chain window to stop sending newly selected industries. */
577 /* static */ void SmallMapWindow::BreakIndustryChainLink()
579 InvalidateWindowClassesData(WC_INDUSTRY_CARGOES, NUM_INDUSTRYTYPES);
582 inline Point SmallMapWindow::SmallmapRemapCoords(int x, int y) const
584 Point pt;
585 pt.x = (y - x) * 2;
586 pt.y = y + x;
587 return pt;
591 * Remap tile to location on this smallmap.
592 * @param tile_x X coordinate of the tile.
593 * @param tile_y Y coordinate of the tile.
594 * @return Position to draw on.
596 inline Point SmallMapWindow::RemapTile(int tile_x, int tile_y) const
598 int x_offset = tile_x - this->scroll_x / (int)TILE_SIZE;
599 int y_offset = tile_y - this->scroll_y / (int)TILE_SIZE;
601 if (this->zoom == 1) return SmallmapRemapCoords(x_offset, y_offset);
603 /* For negative offsets, round towards -inf. */
604 if (x_offset < 0) x_offset -= this->zoom - 1;
605 if (y_offset < 0) y_offset -= this->zoom - 1;
607 return SmallmapRemapCoords(x_offset / this->zoom, y_offset / this->zoom);
611 * Determine the tile relative to the base tile of the smallmap, and the pixel position at
612 * that tile for a point in the smallmap.
613 * @param px Horizontal coordinate of the pixel.
614 * @param py Vertical coordinate of the pixel.
615 * @param[out] sub Pixel position at the tile (0..3).
616 * @param add_sub Add current #subscroll to the position.
617 * @return Tile being displayed at the given position relative to #scroll_x and #scroll_y.
618 * @note The #subscroll offset is already accounted for.
620 inline Point SmallMapWindow::PixelToTile(int px, int py, int *sub, bool add_sub) const
622 if (add_sub) px += this->subscroll; // Total horizontal offset.
624 /* For each two rows down, add a x and a y tile, and
625 * For each four pixels to the right, move a tile to the right. */
626 Point pt = {((py >> 1) - (px >> 2)) * this->zoom, ((py >> 1) + (px >> 2)) * this->zoom};
627 px &= 3;
629 if (py & 1) { // Odd number of rows, handle the 2 pixel shift.
630 if (px < 2) {
631 pt.x += this->zoom;
632 px += 2;
633 } else {
634 pt.y += this->zoom;
635 px -= 2;
639 *sub = px;
640 return pt;
644 * Compute base parameters of the smallmap such that tile (\a tx, \a ty) starts at pixel (\a x, \a y).
645 * @param tx Tile x coordinate.
646 * @param ty Tile y coordinate.
647 * @param x Non-negative horizontal position in the display where the tile starts.
648 * @param y Non-negative vertical position in the display where the tile starts.
649 * @param[out] sub Value of #subscroll needed.
650 * @return #scroll_x, #scroll_y values.
652 Point SmallMapWindow::ComputeScroll(int tx, int ty, int x, int y, int *sub)
654 assert(x >= 0 && y >= 0);
656 int new_sub;
657 Point tile_xy = PixelToTile(x, y, &new_sub, false);
658 tx -= tile_xy.x;
659 ty -= tile_xy.y;
661 Point scroll;
662 if (new_sub == 0) {
663 *sub = 0;
664 scroll.x = (tx + this->zoom) * TILE_SIZE;
665 scroll.y = (ty - this->zoom) * TILE_SIZE;
666 } else {
667 *sub = 4 - new_sub;
668 scroll.x = (tx + 2 * this->zoom) * TILE_SIZE;
669 scroll.y = (ty - 2 * this->zoom) * TILE_SIZE;
671 return scroll;
675 * Initialize or change the zoom level.
676 * @param change Way to change the zoom level.
677 * @param zoom_pt Position to keep fixed while zooming.
678 * @pre \c *zoom_pt should contain a point in the smallmap display when zooming in or out.
680 void SmallMapWindow::SetZoomLevel(ZoomLevelChange change, const Point *zoom_pt)
682 static const int zoomlevels[] = {1, 2, 4, 6, 8}; // Available zoom levels. Bigger number means more zoom-out (further away).
683 static const int MIN_ZOOM_INDEX = 0;
684 static const int MAX_ZOOM_INDEX = lengthof(zoomlevels) - 1;
686 int new_index, cur_index, sub;
687 Point tile;
688 switch (change) {
689 case ZLC_INITIALIZE:
690 cur_index = - 1; // Definitely different from new_index.
691 new_index = MIN_ZOOM_INDEX;
692 tile.x = tile.y = 0;
693 break;
695 case ZLC_ZOOM_IN:
696 case ZLC_ZOOM_OUT:
697 for (cur_index = MIN_ZOOM_INDEX; cur_index <= MAX_ZOOM_INDEX; cur_index++) {
698 if (this->zoom == zoomlevels[cur_index]) break;
700 assert(cur_index <= MAX_ZOOM_INDEX);
702 tile = this->PixelToTile(zoom_pt->x, zoom_pt->y, &sub);
703 new_index = Clamp(cur_index + ((change == ZLC_ZOOM_IN) ? -1 : 1), MIN_ZOOM_INDEX, MAX_ZOOM_INDEX);
704 break;
706 default: NOT_REACHED();
709 if (new_index != cur_index) {
710 this->zoom = zoomlevels[new_index];
711 if (cur_index >= 0) {
712 Point new_tile = this->PixelToTile(zoom_pt->x, zoom_pt->y, &sub);
713 this->SetNewScroll(this->scroll_x + (tile.x - new_tile.x) * TILE_SIZE,
714 this->scroll_y + (tile.y - new_tile.y) * TILE_SIZE, sub);
715 } else if (this->map_type == SMT_LINKSTATS) {
716 this->overlay->RebuildCache();
718 this->SetWidgetDisabledState(WID_SM_ZOOM_IN, this->zoom == zoomlevels[MIN_ZOOM_INDEX]);
719 this->SetWidgetDisabledState(WID_SM_ZOOM_OUT, this->zoom == zoomlevels[MAX_ZOOM_INDEX]);
720 this->SetDirty();
725 * Decide which colours to show to the user for a group of tiles.
726 * @param ta Tile area to investigate.
727 * @return Colours to display.
729 inline uint32 SmallMapWindow::GetTileColours(const TileArea &ta) const
731 int importance = 0;
732 TileIndex tile = INVALID_TILE; // Position of the most important tile.
733 TileType et = MP_VOID; // Effective tile type at that position.
735 TILE_AREA_LOOP(ti, ta) {
736 TileType ttype = GetTileType(ti);
738 switch (ttype) {
739 case MP_TUNNELBRIDGE: {
740 TransportType tt = GetTunnelBridgeTransportType(ti);
742 switch (tt) {
743 case TRANSPORT_RAIL: ttype = MP_RAILWAY; break;
744 case TRANSPORT_ROAD: ttype = MP_ROAD; break;
745 default: ttype = MP_WATER; break;
747 break;
750 case MP_INDUSTRY:
751 /* Special handling of industries while in "Industries" smallmap view. */
752 if (this->map_type == SMT_INDUSTRY) {
753 /* If industry is allowed to be seen, use its colour on the map.
754 * This has the highest priority above any value in _tiletype_importance. */
755 IndustryType type = Industry::GetByTile(ti)->type;
756 if (_legend_from_industries[_industry_to_list_pos[type]].show_on_map) {
757 if (type == _smallmap_industry_highlight) {
758 if (_smallmap_industry_highlight_state) return MKCOLOUR_XXXX(PC_WHITE);
759 } else {
760 return GetIndustrySpec(type)->map_colour * 0x01010101;
763 /* Otherwise make it disappear */
764 ttype = IsTileOnWater(ti) ? MP_WATER : MP_CLEAR;
766 break;
768 default:
769 break;
772 if (_tiletype_importance[ttype] > importance) {
773 importance = _tiletype_importance[ttype];
774 tile = ti;
775 et = ttype;
779 switch (this->map_type) {
780 case SMT_CONTOUR:
781 return GetSmallMapContoursPixels(tile, et);
783 case SMT_VEHICLES:
784 return GetSmallMapVehiclesPixels(tile, et);
786 case SMT_INDUSTRY:
787 return GetSmallMapIndustriesPixels(tile, et);
789 case SMT_LINKSTATS:
790 return GetSmallMapLinkStatsPixels(tile, et);
792 case SMT_ROUTES:
793 return GetSmallMapRoutesPixels(tile, et);
795 case SMT_VEGETATION:
796 return GetSmallMapVegetationPixels(tile, et);
798 case SMT_OWNER:
799 return GetSmallMapOwnerPixels(tile, et);
801 default: NOT_REACHED();
806 * Draws one column of tiles of the small map in a certain mode onto the screen buffer, skipping the shifted rows in between.
808 * @param dst Pointer to a part of the screen buffer to write to.
809 * @param xc The X coordinate of the first tile in the column.
810 * @param yc The Y coordinate of the first tile in the column
811 * @param pitch Number of pixels to advance in the screen buffer each time a pixel is written.
812 * @param reps Number of lines to draw
813 * @param start_pos Position of first pixel to draw.
814 * @param end_pos Position of last pixel to draw (exclusive).
815 * @param blitter current blitter
816 * @note If pixel position is below \c 0, skip drawing.
818 void SmallMapWindow::DrawSmallMapColumn(void *dst, uint xc, uint yc, int pitch, int reps, int start_pos, int end_pos, Blitter *blitter) const
820 void *dst_ptr_abs_end = blitter->MoveTo(_screen.dst_ptr, 0, _screen.height);
821 uint min_xy = _settings_game.construction.freeform_edges ? 1 : 0;
823 do {
824 /* Check if the tile (xc,yc) is within the map range */
825 if (xc >= MapMaxX() || yc >= MapMaxY()) continue;
827 /* Check if the dst pointer points to a pixel inside the screen buffer */
828 if (dst < _screen.dst_ptr) continue;
829 if (dst >= dst_ptr_abs_end) continue;
831 /* Construct tilearea covered by (xc, yc, xc + this->zoom, yc + this->zoom) such that it is within min_xy limits. */
832 TileArea ta;
833 if (min_xy == 1 && (xc == 0 || yc == 0)) {
834 if (this->zoom == 1) continue; // The tile area is empty, don't draw anything.
836 ta = TileArea(TileXY(max(min_xy, xc), max(min_xy, yc)), this->zoom - (xc == 0), this->zoom - (yc == 0));
837 } else {
838 ta = TileArea(TileXY(xc, yc), this->zoom, this->zoom);
840 ta.ClampToMap(); // Clamp to map boundaries (may contain MP_VOID tiles!).
842 uint32 val = this->GetTileColours(ta);
843 uint8 *val8 = (uint8 *)&val;
844 int idx = max(0, -start_pos);
845 for (int pos = max(0, start_pos); pos < end_pos; pos++) {
846 blitter->SetPixel(dst, idx, 0, val8[idx]);
847 idx++;
849 /* Switch to next tile in the column */
850 } while (xc += this->zoom, yc += this->zoom, dst = blitter->MoveTo(dst, pitch, 0), --reps != 0);
854 * Adds vehicles to the smallmap.
855 * @param dpi the part of the smallmap to be drawn into
856 * @param blitter current blitter
858 void SmallMapWindow::DrawVehicles(const DrawPixelInfo *dpi, Blitter *blitter) const
860 const Vehicle *v;
861 FOR_ALL_VEHICLES(v) {
862 if (v->type == VEH_EFFECT) continue;
863 if (v->vehstatus & (VS_HIDDEN | VS_UNCLICKABLE)) continue;
865 /* Remap into flat coordinates. */
866 Point pt = this->RemapTile(v->x_pos / (int)TILE_SIZE, v->y_pos / (int)TILE_SIZE);
868 int y = pt.y - dpi->top;
869 if (!IsInsideMM(y, 0, dpi->height)) continue; // y is out of bounds.
871 bool skip = false; // Default is to draw both pixels.
872 int x = pt.x - this->subscroll - 3 - dpi->left; // Offset X coordinate.
873 if (x < 0) {
874 /* if x+1 is 0, that means we're on the very left edge,
875 * and should thus only draw a single pixel */
876 if (++x != 0) continue;
877 skip = true;
878 } else if (x >= dpi->width - 1) {
879 /* Check if we're at the very right edge, and if so draw only a single pixel */
880 if (x != dpi->width - 1) continue;
881 skip = true;
884 /* Calculate pointer to pixel and the colour */
885 byte colour = (this->map_type == SMT_VEHICLES) ? _vehicle_type_colours[v->type] : PC_WHITE;
887 /* And draw either one or two pixels depending on clipping */
888 blitter->SetPixel(dpi->dst_ptr, x, y, colour);
889 if (!skip) blitter->SetPixel(dpi->dst_ptr, x + 1, y, colour);
894 * Adds town names to the smallmap.
895 * @param dpi the part of the smallmap to be drawn into
897 void SmallMapWindow::DrawTowns(const DrawPixelInfo *dpi) const
899 const Town *t;
900 FOR_ALL_TOWNS(t) {
901 /* Remap the town coordinate */
902 Point pt = this->RemapTile(TileX(t->xy), TileY(t->xy));
903 int x = pt.x - this->subscroll - (t->cache.sign.width_small >> 1);
904 int y = pt.y;
906 /* Check if the town sign is within bounds */
907 if (x + t->cache.sign.width_small > dpi->left &&
908 x < dpi->left + dpi->width &&
909 y + FONT_HEIGHT_SMALL > dpi->top &&
910 y < dpi->top + dpi->height) {
911 /* And draw it. */
912 SetDParam(0, t->index);
913 DrawString(x, x + t->cache.sign.width_small, y, STR_SMALLMAP_TOWN);
919 * Adds map indicators to the smallmap.
921 void SmallMapWindow::DrawMapIndicators() const
923 /* Find main viewport. */
924 const ViewPort *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
926 Point upper_left_smallmap_coord = TranslateXYToTileCoord(vp, vp->left, vp->top, false);
927 Point lower_right_smallmap_coord = TranslateXYToTileCoord(vp, vp->left + vp->width - 1, vp->top + vp->height - 1, false);
929 Point upper_left = this->RemapTile(upper_left_smallmap_coord.x / (int)TILE_SIZE, upper_left_smallmap_coord.y / (int)TILE_SIZE);
930 upper_left.x -= this->subscroll;
932 Point lower_right = this->RemapTile(lower_right_smallmap_coord.x / (int)TILE_SIZE, lower_right_smallmap_coord.y / (int)TILE_SIZE);
933 lower_right.x -= this->subscroll;
935 SmallMapWindow::DrawVertMapIndicator(upper_left.x, upper_left.y, lower_right.y);
936 SmallMapWindow::DrawVertMapIndicator(lower_right.x, upper_left.y, lower_right.y);
938 SmallMapWindow::DrawHorizMapIndicator(upper_left.x, lower_right.x, upper_left.y);
939 SmallMapWindow::DrawHorizMapIndicator(upper_left.x, lower_right.x, lower_right.y);
943 * Draws the small map.
945 * Basically, the small map is draw column of pixels by column of pixels. The pixels
946 * are drawn directly into the screen buffer. The final map is drawn in multiple passes.
947 * The passes are:
948 * <ol><li>The colours of tiles in the different modes.</li>
949 * <li>Town names (optional)</li></ol>
951 * @param dpi pointer to pixel to write onto
953 void SmallMapWindow::DrawSmallMap(DrawPixelInfo *dpi) const
955 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
956 DrawPixelInfo *old_dpi;
958 old_dpi = _cur_dpi;
959 _cur_dpi = dpi;
961 /* Clear it */
962 GfxFillRect(dpi->left, dpi->top, dpi->left + dpi->width - 1, dpi->top + dpi->height - 1, PC_BLACK);
964 /* Which tile is displayed at (dpi->left, dpi->top)? */
965 int dx;
966 Point tile = this->PixelToTile(dpi->left, dpi->top, &dx);
967 int tile_x = this->scroll_x / (int)TILE_SIZE + tile.x;
968 int tile_y = this->scroll_y / (int)TILE_SIZE + tile.y;
970 void *ptr = blitter->MoveTo(dpi->dst_ptr, -dx - 4, 0);
971 int x = - dx - 4;
972 int y = 0;
974 for (;;) {
975 /* Distance from left edge */
976 if (x >= -3) {
977 if (x >= dpi->width) break; // Exit the loop.
979 int end_pos = min(dpi->width, x + 4);
980 int reps = (dpi->height - y + 1) / 2; // Number of lines.
981 if (reps > 0) {
982 this->DrawSmallMapColumn(ptr, tile_x, tile_y, dpi->pitch * 2, reps, x, end_pos, blitter);
986 if (y == 0) {
987 tile_y += this->zoom;
988 y++;
989 ptr = blitter->MoveTo(ptr, 0, 1);
990 } else {
991 tile_x -= this->zoom;
992 y--;
993 ptr = blitter->MoveTo(ptr, 0, -1);
995 ptr = blitter->MoveTo(ptr, 2, 0);
996 x += 2;
999 /* Draw vehicles */
1000 if (this->map_type == SMT_CONTOUR || this->map_type == SMT_VEHICLES) this->DrawVehicles(dpi, blitter);
1002 /* Draw link stat overlay */
1003 if (this->map_type == SMT_LINKSTATS) this->overlay->Draw(dpi);
1005 /* Draw town names */
1006 if (this->show_towns) this->DrawTowns(dpi);
1008 /* Draw map indicators */
1009 this->DrawMapIndicators();
1011 _cur_dpi = old_dpi;
1015 * Function to set up widgets depending on the information being shown on the smallmap.
1017 void SmallMapWindow::SetupWidgetData()
1019 StringID legend_tooltip;
1020 StringID enable_all_tooltip;
1021 StringID disable_all_tooltip;
1022 int plane;
1023 switch (this->map_type) {
1024 case SMT_INDUSTRY:
1025 legend_tooltip = STR_SMALLMAP_TOOLTIP_INDUSTRY_SELECTION;
1026 enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_INDUSTRIES;
1027 disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_INDUSTRIES;
1028 plane = 0;
1029 break;
1031 case SMT_OWNER:
1032 legend_tooltip = STR_SMALLMAP_TOOLTIP_COMPANY_SELECTION;
1033 enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_COMPANIES;
1034 disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_COMPANIES;
1035 plane = 0;
1036 break;
1038 case SMT_LINKSTATS:
1039 legend_tooltip = STR_SMALLMAP_TOOLTIP_CARGO_SELECTION;
1040 enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_CARGOS;
1041 disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_CARGOS;
1042 plane = 0;
1043 break;
1045 default:
1046 legend_tooltip = STR_NULL;
1047 enable_all_tooltip = STR_NULL;
1048 disable_all_tooltip = STR_NULL;
1049 plane = 1;
1050 break;
1053 this->GetWidget<NWidgetCore>(WID_SM_LEGEND)->SetDataTip(STR_NULL, legend_tooltip);
1054 this->GetWidget<NWidgetCore>(WID_SM_ENABLE_ALL)->SetDataTip(STR_SMALLMAP_ENABLE_ALL, enable_all_tooltip);
1055 this->GetWidget<NWidgetCore>(WID_SM_DISABLE_ALL)->SetDataTip(STR_SMALLMAP_DISABLE_ALL, disable_all_tooltip);
1056 this->GetWidget<NWidgetStacked>(WID_SM_SELECT_BUTTONS)->SetDisplayedPlane(plane);
1059 SmallMapWindow::SmallMapWindow(WindowDesc *desc, int window_number) : Window(desc), refresh(GUITimer(FORCE_REFRESH_PERIOD))
1061 _smallmap_industry_highlight = INVALID_INDUSTRYTYPE;
1062 this->overlay = new LinkGraphOverlay(this, WID_SM_MAP, 0, this->GetOverlayCompanyMask(), 1);
1063 this->InitNested(window_number);
1064 this->LowerWidget(this->map_type + WID_SM_CONTOUR);
1066 this->RebuildColourIndexIfNecessary();
1068 this->SetWidgetLoweredState(WID_SM_SHOW_HEIGHT, _smallmap_show_heightmap);
1070 this->SetWidgetLoweredState(WID_SM_TOGGLETOWNNAME, this->show_towns);
1072 this->SetupWidgetData();
1074 this->SetZoomLevel(ZLC_INITIALIZE, NULL);
1075 this->SmallMapCenterOnCurrentPos();
1076 this->SetOverlayCargoMask();
1079 SmallMapWindow::~SmallMapWindow()
1081 delete this->overlay;
1082 this->BreakIndustryChainLink();
1086 * Rebuilds the colour indices used for fast access to the smallmap contour colours based on the heightlevel.
1088 void SmallMapWindow::RebuildColourIndexIfNecessary()
1090 /* Rebuild colour indices if necessary. */
1091 if (SmallMapWindow::max_heightlevel == _settings_game.construction.max_heightlevel) return;
1093 for (uint n = 0; n < lengthof(_heightmap_schemes); n++) {
1094 /* The heights go from 0 up to and including maximum. */
1095 int heights = _settings_game.construction.max_heightlevel + 1;
1096 _heightmap_schemes[n].height_colours = ReallocT<uint32>(_heightmap_schemes[n].height_colours, heights);
1098 for (int z = 0; z < heights; z++) {
1099 size_t access_index = (_heightmap_schemes[n].colour_count * z) / heights;
1101 /* Choose colour by mapping the range (0..max heightlevel) on the complete colour table. */
1102 _heightmap_schemes[n].height_colours[z] = _heightmap_schemes[n].height_colours_base[access_index];
1106 SmallMapWindow::max_heightlevel = _settings_game.construction.max_heightlevel;
1107 BuildLandLegend();
1110 /* virtual */ void SmallMapWindow::SetStringParameters(int widget) const
1112 switch (widget) {
1113 case WID_SM_CAPTION:
1114 SetDParam(0, STR_SMALLMAP_TYPE_CONTOURS + this->map_type);
1115 break;
1119 /* virtual */ void SmallMapWindow::OnInit()
1121 uint min_width = 0;
1122 this->min_number_of_columns = INDUSTRY_MIN_NUMBER_OF_COLUMNS;
1123 this->min_number_of_fixed_rows = lengthof(_linkstat_colours_in_legenda);
1124 for (uint i = 0; i < lengthof(_legend_table); i++) {
1125 uint height = 0;
1126 uint num_columns = 1;
1127 for (const LegendAndColour *tbl = _legend_table[i]; !tbl->end; ++tbl) {
1128 StringID str;
1129 if (i == SMT_INDUSTRY) {
1130 SetDParam(0, tbl->legend);
1131 SetDParam(1, IndustryPool::MAX_SIZE);
1132 str = STR_SMALLMAP_INDUSTRY;
1133 } else if (i == SMT_LINKSTATS) {
1134 SetDParam(0, tbl->legend);
1135 str = STR_SMALLMAP_LINKSTATS;
1136 } else if (i == SMT_OWNER) {
1137 if (tbl->company != INVALID_COMPANY) {
1138 if (!Company::IsValidID(tbl->company)) {
1139 /* Rebuild the owner legend. */
1140 BuildOwnerLegend();
1141 this->OnInit();
1142 return;
1144 /* Non-fixed legend entries for the owner view. */
1145 SetDParam(0, tbl->company);
1146 str = STR_SMALLMAP_COMPANY;
1147 } else {
1148 str = tbl->legend;
1150 } else {
1151 if (tbl->col_break) {
1152 this->min_number_of_fixed_rows = max(this->min_number_of_fixed_rows, height);
1153 height = 0;
1154 num_columns++;
1156 height++;
1157 str = tbl->legend;
1159 min_width = max(GetStringBoundingBox(str).width, min_width);
1161 this->min_number_of_fixed_rows = max(this->min_number_of_fixed_rows, height);
1162 this->min_number_of_columns = max(this->min_number_of_columns, num_columns);
1165 /* The width of a column is the minimum width of all texts + the size of the blob + some spacing */
1166 this->column_width = min_width + LEGEND_BLOB_WIDTH + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
1169 /* virtual */ void SmallMapWindow::OnPaint()
1171 if (this->map_type == SMT_OWNER) {
1172 for (const LegendAndColour *tbl = _legend_table[this->map_type]; !tbl->end; ++tbl) {
1173 if (tbl->company != INVALID_COMPANY && !Company::IsValidID(tbl->company)) {
1174 /* Rebuild the owner legend. */
1175 BuildOwnerLegend();
1176 this->InvalidateData(1);
1177 break;
1182 this->DrawWidgets();
1185 /* virtual */ void SmallMapWindow::DrawWidget(const Rect &r, int widget) const
1187 switch (widget) {
1188 case WID_SM_MAP: {
1189 DrawPixelInfo new_dpi;
1190 if (!FillDrawPixelInfo(&new_dpi, r.left + 1, r.top + 1, r.right - r.left - 1, r.bottom - r.top - 1)) return;
1191 this->DrawSmallMap(&new_dpi);
1192 break;
1195 case WID_SM_LEGEND: {
1196 uint columns = this->GetNumberColumnsLegend(r.right - r.left + 1);
1197 uint number_of_rows = this->GetNumberRowsLegend(columns);
1198 bool rtl = _current_text_dir == TD_RTL;
1199 uint y_org = r.top + WD_FRAMERECT_TOP;
1200 uint x = rtl ? r.right - this->column_width - WD_FRAMERECT_RIGHT : r.left + WD_FRAMERECT_LEFT;
1201 uint y = y_org;
1202 uint i = 0; // Row counter for industry legend.
1203 uint row_height = FONT_HEIGHT_SMALL;
1205 uint text_left = rtl ? 0 : LEGEND_BLOB_WIDTH + WD_FRAMERECT_LEFT;
1206 uint text_right = this->column_width - 1 - (rtl ? LEGEND_BLOB_WIDTH + WD_FRAMERECT_RIGHT : 0);
1207 uint blob_left = rtl ? this->column_width - 1 - LEGEND_BLOB_WIDTH : 0;
1208 uint blob_right = rtl ? this->column_width - 1 : LEGEND_BLOB_WIDTH;
1210 StringID string = STR_NULL;
1211 switch (this->map_type) {
1212 case SMT_INDUSTRY:
1213 string = STR_SMALLMAP_INDUSTRY;
1214 break;
1215 case SMT_LINKSTATS:
1216 string = STR_SMALLMAP_LINKSTATS;
1217 break;
1218 case SMT_OWNER:
1219 string = STR_SMALLMAP_COMPANY;
1220 break;
1221 default:
1222 break;
1225 for (const LegendAndColour *tbl = _legend_table[this->map_type]; !tbl->end; ++tbl) {
1226 if (tbl->col_break || ((this->map_type == SMT_INDUSTRY || this->map_type == SMT_OWNER || this->map_type == SMT_LINKSTATS) && i++ >= number_of_rows)) {
1227 /* Column break needed, continue at top, COLUMN_WIDTH pixels
1228 * (one "row") to the right. */
1229 x += rtl ? -(int)this->column_width : this->column_width;
1230 y = y_org;
1231 i = 1;
1234 uint8 legend_colour = tbl->colour;
1236 switch (this->map_type) {
1237 case SMT_INDUSTRY:
1238 /* Industry name must be formatted, since it's not in tiny font in the specs.
1239 * So, draw with a parameter and use the STR_SMALLMAP_INDUSTRY string, which is tiny font */
1240 SetDParam(0, tbl->legend);
1241 SetDParam(1, Industry::GetIndustryTypeCount(tbl->type));
1242 if (tbl->show_on_map && tbl->type == _smallmap_industry_highlight) {
1243 legend_colour = _smallmap_industry_highlight_state ? PC_WHITE : PC_BLACK;
1245 FALLTHROUGH;
1247 case SMT_LINKSTATS:
1248 SetDParam(0, tbl->legend);
1249 FALLTHROUGH;
1251 case SMT_OWNER:
1252 if (this->map_type != SMT_OWNER || tbl->company != INVALID_COMPANY) {
1253 if (this->map_type == SMT_OWNER) SetDParam(0, tbl->company);
1254 if (!tbl->show_on_map) {
1255 /* Simply draw the string, not the black border of the legend colour.
1256 * This will enforce the idea of the disabled item */
1257 DrawString(x + text_left, x + text_right, y, string, TC_GREY);
1258 } else {
1259 DrawString(x + text_left, x + text_right, y, string, TC_BLACK);
1260 GfxFillRect(x + blob_left, y + 1, x + blob_right, y + row_height - 1, PC_BLACK); // Outer border of the legend colour
1262 break;
1264 FALLTHROUGH;
1266 default:
1267 if (this->map_type == SMT_CONTOUR) SetDParam(0, tbl->height * TILE_HEIGHT_STEP);
1268 /* Anything that is not an industry or a company is using normal process */
1269 GfxFillRect(x + blob_left, y + 1, x + blob_right, y + row_height - 1, PC_BLACK);
1270 DrawString(x + text_left, x + text_right, y, tbl->legend);
1271 break;
1273 GfxFillRect(x + blob_left + 1, y + 2, x + blob_right - 1, y + row_height - 2, legend_colour); // Legend colour
1275 y += row_height;
1282 * Select a new map type.
1283 * @param map_type New map type.
1285 void SmallMapWindow::SwitchMapType(SmallMapType map_type)
1287 this->RaiseWidget(this->map_type + WID_SM_CONTOUR);
1288 this->map_type = map_type;
1289 this->LowerWidget(this->map_type + WID_SM_CONTOUR);
1291 this->SetupWidgetData();
1293 if (map_type == SMT_LINKSTATS) this->overlay->RebuildCache();
1294 if (map_type != SMT_INDUSTRY) this->BreakIndustryChainLink();
1295 this->SetDirty();
1299 * Get the number of rows in the legend from the number of columns. Those
1300 * are at least min_number_of_fixed_rows and possibly more if there are so
1301 * many cargoes, industry types or companies that they won't fit in the
1302 * available space.
1303 * @param columns Number of columns in the legend.
1304 * @return Number of rows needed for everything to fit in.
1306 inline uint SmallMapWindow::GetNumberRowsLegend(uint columns) const
1308 /* Reserve one column for link colours */
1309 uint num_rows_linkstats = CeilDiv(_smallmap_cargo_count, columns - 1);
1310 uint num_rows_others = CeilDiv(max(_smallmap_industry_count, _smallmap_company_count), columns);
1311 return max(this->min_number_of_fixed_rows, max(num_rows_linkstats, num_rows_others));
1315 * Select and toggle a legend item. When CTRL is pressed, disable all other
1316 * items in the group defined by begin_legend_item and end_legend_item and
1317 * keep the clicked one enabled even if it was already enabled before. If
1318 * the other items in the group are all disabled already and CTRL is pressed
1319 * enable them instead.
1320 * @param click_pos the index of the item being selected
1321 * @param legend the legend from which we select
1322 * @param end_legend_item index one past the last item in the group to be inverted
1323 * @param begin_legend_item index of the first item in the group to be inverted
1325 void SmallMapWindow::SelectLegendItem(int click_pos, LegendAndColour *legend, int end_legend_item, int begin_legend_item)
1327 if (_ctrl_pressed) {
1328 /* Disable all, except the clicked one */
1329 bool changes = false;
1330 for (int i = begin_legend_item; i != end_legend_item; i++) {
1331 bool new_state = (i == click_pos);
1332 if (legend[i].show_on_map != new_state) {
1333 changes = true;
1334 legend[i].show_on_map = new_state;
1337 if (!changes) {
1338 /* Nothing changed? Then show all (again). */
1339 for (int i = begin_legend_item; i != end_legend_item; i++) {
1340 legend[i].show_on_map = true;
1343 } else {
1344 legend[click_pos].show_on_map = !legend[click_pos].show_on_map;
1347 if (this->map_type == SMT_INDUSTRY) this->BreakIndustryChainLink();
1351 * Set the link graph overlay cargo mask from the legend.
1353 void SmallMapWindow::SetOverlayCargoMask()
1355 CargoTypes cargo_mask = 0;
1356 for (int i = 0; i != _smallmap_cargo_count; ++i) {
1357 if (_legend_linkstats[i].show_on_map) SetBit(cargo_mask, _legend_linkstats[i].type);
1359 this->overlay->SetCargoMask(cargo_mask);
1363 * Determines the mouse position on the legend.
1364 * @param pt Mouse position.
1365 * @return Legend item under the mouse.
1367 int SmallMapWindow::GetPositionOnLegend(Point pt)
1369 const NWidgetBase *wi = this->GetWidget<NWidgetBase>(WID_SM_LEGEND);
1370 uint line = (pt.y - wi->pos_y - WD_FRAMERECT_TOP) / FONT_HEIGHT_SMALL;
1371 uint columns = this->GetNumberColumnsLegend(wi->current_x);
1372 uint number_of_rows = this->GetNumberRowsLegend(columns);
1373 if (line >= number_of_rows) return -1;
1375 bool rtl = _current_text_dir == TD_RTL;
1376 int x = pt.x - wi->pos_x;
1377 if (rtl) x = wi->current_x - x;
1378 uint column = (x - WD_FRAMERECT_LEFT) / this->column_width;
1380 return (column * number_of_rows) + line;
1383 /* virtual */ void SmallMapWindow::OnMouseOver(Point pt, int widget)
1385 IndustryType new_highlight = INVALID_INDUSTRYTYPE;
1386 if (widget == WID_SM_LEGEND && this->map_type == SMT_INDUSTRY) {
1387 int industry_pos = GetPositionOnLegend(pt);
1388 if (industry_pos >= 0 && industry_pos < _smallmap_industry_count) {
1389 new_highlight = _legend_from_industries[industry_pos].type;
1392 if (new_highlight != _smallmap_industry_highlight) {
1393 _smallmap_industry_highlight = new_highlight;
1394 this->refresh.SetInterval(_smallmap_industry_highlight != INVALID_INDUSTRYTYPE ? BLINK_PERIOD : FORCE_REFRESH_PERIOD);
1395 _smallmap_industry_highlight_state = true;
1396 this->SetDirty();
1400 /* virtual */ void SmallMapWindow::OnClick(Point pt, int widget, int click_count)
1402 switch (widget) {
1403 case WID_SM_MAP: { // Map window
1405 * XXX: scrolling with the left mouse button is done by subsequently
1406 * clicking with the left mouse button; clicking once centers the
1407 * large map at the selected point. So by unclicking the left mouse
1408 * button here, it gets reclicked during the next inputloop, which
1409 * would make it look like the mouse is being dragged, while it is
1410 * actually being (virtually) clicked every inputloop.
1412 _left_button_clicked = false;
1414 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1415 Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
1416 int sub;
1417 pt = this->PixelToTile(pt.x - wid->pos_x, pt.y - wid->pos_y, &sub);
1418 ScrollWindowTo(this->scroll_x + pt.x * TILE_SIZE, this->scroll_y + pt.y * TILE_SIZE, -1, w);
1420 this->SetDirty();
1421 break;
1424 case WID_SM_ZOOM_IN:
1425 case WID_SM_ZOOM_OUT: {
1426 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1427 Point pt = { (int)wid->current_x / 2, (int)wid->current_y / 2};
1428 this->SetZoomLevel((widget == WID_SM_ZOOM_IN) ? ZLC_ZOOM_IN : ZLC_ZOOM_OUT, &pt);
1429 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1430 break;
1433 case WID_SM_CONTOUR: // Show land contours
1434 case WID_SM_VEHICLES: // Show vehicles
1435 case WID_SM_INDUSTRIES: // Show industries
1436 case WID_SM_LINKSTATS: // Show route map
1437 case WID_SM_ROUTES: // Show transport routes
1438 case WID_SM_VEGETATION: // Show vegetation
1439 case WID_SM_OWNERS: // Show land owners
1440 this->SwitchMapType((SmallMapType)(widget - WID_SM_CONTOUR));
1441 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1442 break;
1444 case WID_SM_CENTERMAP: // Center the smallmap again
1445 this->SmallMapCenterOnCurrentPos();
1446 this->HandleButtonClick(WID_SM_CENTERMAP);
1447 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1448 break;
1450 case WID_SM_TOGGLETOWNNAME: // Toggle town names
1451 this->show_towns = !this->show_towns;
1452 this->SetWidgetLoweredState(WID_SM_TOGGLETOWNNAME, this->show_towns);
1454 this->SetDirty();
1455 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1456 break;
1458 case WID_SM_LEGEND: // Legend
1459 if (this->map_type == SMT_INDUSTRY || this->map_type == SMT_LINKSTATS || this->map_type == SMT_OWNER) {
1460 int click_pos = this->GetPositionOnLegend(pt);
1461 if (click_pos < 0) break;
1463 /* If industry type small map*/
1464 if (this->map_type == SMT_INDUSTRY) {
1465 /* If click on industries label, find right industry type and enable/disable it. */
1466 if (click_pos < _smallmap_industry_count) {
1467 this->SelectLegendItem(click_pos, _legend_from_industries, _smallmap_industry_count);
1469 } else if (this->map_type == SMT_LINKSTATS) {
1470 if (click_pos < _smallmap_cargo_count) {
1471 this->SelectLegendItem(click_pos, _legend_linkstats, _smallmap_cargo_count);
1472 this->SetOverlayCargoMask();
1474 } else if (this->map_type == SMT_OWNER) {
1475 if (click_pos < _smallmap_company_count) {
1476 this->SelectLegendItem(click_pos, _legend_land_owners, _smallmap_company_count, NUM_NO_COMPANY_ENTRIES);
1479 this->SetDirty();
1481 break;
1483 case WID_SM_ENABLE_ALL:
1484 case WID_SM_DISABLE_ALL: {
1485 LegendAndColour *tbl = NULL;
1486 switch (this->map_type) {
1487 case SMT_INDUSTRY:
1488 tbl = _legend_from_industries;
1489 this->BreakIndustryChainLink();
1490 break;
1491 case SMT_OWNER:
1492 tbl = &(_legend_land_owners[NUM_NO_COMPANY_ENTRIES]);
1493 break;
1494 case SMT_LINKSTATS:
1495 tbl = _legend_linkstats;
1496 break;
1497 default:
1498 NOT_REACHED();
1500 for (;!tbl->end && tbl->legend != STR_LINKGRAPH_LEGEND_UNUSED; ++tbl) {
1501 tbl->show_on_map = (widget == WID_SM_ENABLE_ALL);
1503 if (this->map_type == SMT_LINKSTATS) this->SetOverlayCargoMask();
1504 this->SetDirty();
1505 break;
1508 case WID_SM_SHOW_HEIGHT: // Enable/disable showing of heightmap.
1509 _smallmap_show_heightmap = !_smallmap_show_heightmap;
1510 this->SetWidgetLoweredState(WID_SM_SHOW_HEIGHT, _smallmap_show_heightmap);
1511 this->SetDirty();
1512 break;
1517 * Some data on this window has become invalid.
1518 * @param data Information about the changed data.
1519 * - data = 0: Displayed industries at the industry chain window have changed.
1520 * - data = 1: Companies have changed.
1521 * - data = 2: Cheat changing the maximum heightlevel has been used, rebuild our heightlevel-to-colour index
1522 * @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.
1524 /* virtual */ void SmallMapWindow::OnInvalidateData(int data, bool gui_scope)
1526 if (!gui_scope) return;
1528 switch (data) {
1529 case 1:
1530 /* The owner legend has already been rebuilt. */
1531 this->ReInit();
1532 break;
1534 case 0: {
1535 extern std::bitset<NUM_INDUSTRYTYPES> _displayed_industries;
1536 if (this->map_type != SMT_INDUSTRY) this->SwitchMapType(SMT_INDUSTRY);
1538 for (int i = 0; i != _smallmap_industry_count; i++) {
1539 _legend_from_industries[i].show_on_map = _displayed_industries.test(_legend_from_industries[i].type);
1541 break;
1544 case 2:
1545 this->RebuildColourIndexIfNecessary();
1546 break;
1548 default: NOT_REACHED();
1550 this->SetDirty();
1553 /* virtual */ bool SmallMapWindow::OnRightClick(Point pt, int widget)
1555 if (widget != WID_SM_MAP || _scrolling_viewport) return false;
1557 _scrolling_viewport = true;
1558 return true;
1561 /* virtual */ void SmallMapWindow::OnMouseWheel(int wheel)
1563 if (_settings_client.gui.scrollwheel_scrolling != 2) {
1564 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1565 int cursor_x = _cursor.pos.x - this->left - wid->pos_x;
1566 int cursor_y = _cursor.pos.y - this->top - wid->pos_y;
1567 if (IsInsideMM(cursor_x, 0, wid->current_x) && IsInsideMM(cursor_y, 0, wid->current_y)) {
1568 Point pt = {cursor_x, cursor_y};
1569 this->SetZoomLevel((wheel < 0) ? ZLC_ZOOM_IN : ZLC_ZOOM_OUT, &pt);
1574 /* virtual */ void SmallMapWindow::OnRealtimeTick(uint delta_ms)
1576 /* Update the window every now and then */
1577 if (!this->refresh.Elapsed(delta_ms)) return;
1579 if (this->map_type == SMT_LINKSTATS) {
1580 uint32 company_mask = this->GetOverlayCompanyMask();
1581 if (this->overlay->GetCompanyMask() != company_mask) {
1582 this->overlay->SetCompanyMask(company_mask);
1583 } else {
1584 this->overlay->RebuildCache();
1587 _smallmap_industry_highlight_state = !_smallmap_industry_highlight_state;
1589 this->refresh.SetInterval(_smallmap_industry_highlight != INVALID_INDUSTRYTYPE ? BLINK_PERIOD : FORCE_REFRESH_PERIOD);
1590 this->SetDirty();
1594 * Set new #scroll_x, #scroll_y, and #subscroll values after limiting them such that the center
1595 * of the smallmap always contains a part of the map.
1596 * @param sx Proposed new #scroll_x
1597 * @param sy Proposed new #scroll_y
1598 * @param sub Proposed new #subscroll
1600 void SmallMapWindow::SetNewScroll(int sx, int sy, int sub)
1602 const NWidgetBase *wi = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1603 Point hv = InverseRemapCoords(wi->current_x * ZOOM_LVL_BASE * TILE_SIZE / 2, wi->current_y * ZOOM_LVL_BASE * TILE_SIZE / 2);
1604 hv.x *= this->zoom;
1605 hv.y *= this->zoom;
1607 if (sx < -hv.x) {
1608 sx = -hv.x;
1609 sub = 0;
1611 if (sx > (int)(MapMaxX() * TILE_SIZE) - hv.x) {
1612 sx = MapMaxX() * TILE_SIZE - hv.x;
1613 sub = 0;
1615 if (sy < -hv.y) {
1616 sy = -hv.y;
1617 sub = 0;
1619 if (sy > (int)(MapMaxY() * TILE_SIZE) - hv.y) {
1620 sy = MapMaxY() * TILE_SIZE - hv.y;
1621 sub = 0;
1624 this->scroll_x = sx;
1625 this->scroll_y = sy;
1626 this->subscroll = sub;
1627 if (this->map_type == SMT_LINKSTATS) this->overlay->RebuildCache();
1630 /* virtual */ void SmallMapWindow::OnScroll(Point delta)
1632 if (_settings_client.gui.scroll_mode == VSM_VIEWPORT_RMB_FIXED || _settings_client.gui.scroll_mode == VSM_MAP_RMB_FIXED) _cursor.fix_at = true;
1634 /* While tile is at (delta.x, delta.y)? */
1635 int sub;
1636 Point pt = this->PixelToTile(delta.x, delta.y, &sub);
1637 this->SetNewScroll(this->scroll_x + pt.x * TILE_SIZE, this->scroll_y + pt.y * TILE_SIZE, sub);
1639 this->SetDirty();
1643 * Center the small map on the current center of the viewport.
1645 void SmallMapWindow::SmallMapCenterOnCurrentPos()
1647 const ViewPort *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
1648 Point viewport_center = TranslateXYToTileCoord(vp, vp->left + vp->width / 2, vp->top + vp->height / 2);
1650 int sub;
1651 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1652 Point sxy = this->ComputeScroll(viewport_center.x / (int)TILE_SIZE, viewport_center.y / (int)TILE_SIZE,
1653 max(0, (int)wid->current_x / 2 - 2), wid->current_y / 2, &sub);
1654 this->SetNewScroll(sxy.x, sxy.y, sub);
1655 this->SetDirty();
1659 * Get the center of the given station as point on the screen in the smallmap window.
1660 * @param st Station to find in the smallmap.
1661 * @return Point with coordinates of the station.
1663 Point SmallMapWindow::GetStationMiddle(const Station *st) const
1665 int x = (st->rect.right + st->rect.left + 1) / 2;
1666 int y = (st->rect.bottom + st->rect.top + 1) / 2;
1667 Point ret = this->RemapTile(x, y);
1669 /* Same magic 3 as in DrawVehicles; that's where I got it from.
1670 * No idea what it is, but without it the result looks bad.
1672 ret.x -= 3 + this->subscroll;
1673 return ret;
1676 SmallMapWindow::SmallMapType SmallMapWindow::map_type = SMT_CONTOUR;
1677 bool SmallMapWindow::show_towns = true;
1678 int SmallMapWindow::max_heightlevel = -1;
1681 * Custom container class for displaying smallmap with a vertically resizing legend panel.
1682 * The legend panel has a smallest height that depends on its width. Standard containers cannot handle this case.
1684 * @note The container assumes it has two children, the first is the display, the second is the bar with legends and selection image buttons.
1685 * Both children should be both horizontally and vertically resizable and horizontally fillable.
1686 * The bar should have a minimal size with a zero-size legends display. Child padding is not supported.
1688 class NWidgetSmallmapDisplay : public NWidgetContainer {
1689 const SmallMapWindow *smallmap_window; ///< Window manager instance.
1690 public:
1691 NWidgetSmallmapDisplay() : NWidgetContainer(NWID_VERTICAL)
1693 this->smallmap_window = NULL;
1696 virtual void SetupSmallestSize(Window *w, bool init_array)
1698 NWidgetBase *display = this->head;
1699 NWidgetBase *bar = display->next;
1701 display->SetupSmallestSize(w, init_array);
1702 bar->SetupSmallestSize(w, init_array);
1704 this->smallmap_window = dynamic_cast<SmallMapWindow *>(w);
1705 assert(this->smallmap_window != NULL);
1706 this->smallest_x = max(display->smallest_x, bar->smallest_x + smallmap_window->GetMinLegendWidth());
1707 this->smallest_y = display->smallest_y + max(bar->smallest_y, smallmap_window->GetLegendHeight(smallmap_window->min_number_of_columns));
1708 this->fill_x = max(display->fill_x, bar->fill_x);
1709 this->fill_y = (display->fill_y == 0 && bar->fill_y == 0) ? 0 : min(display->fill_y, bar->fill_y);
1710 this->resize_x = max(display->resize_x, bar->resize_x);
1711 this->resize_y = min(display->resize_y, bar->resize_y);
1714 virtual void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
1716 this->pos_x = x;
1717 this->pos_y = y;
1718 this->current_x = given_width;
1719 this->current_y = given_height;
1721 NWidgetBase *display = this->head;
1722 NWidgetBase *bar = display->next;
1724 if (sizing == ST_SMALLEST) {
1725 this->smallest_x = given_width;
1726 this->smallest_y = given_height;
1727 /* Make display and bar exactly equal to their minimal size. */
1728 display->AssignSizePosition(ST_SMALLEST, x, y, display->smallest_x, display->smallest_y, rtl);
1729 bar->AssignSizePosition(ST_SMALLEST, x, y + display->smallest_y, bar->smallest_x, bar->smallest_y, rtl);
1732 uint bar_height = max(bar->smallest_y, this->smallmap_window->GetLegendHeight(this->smallmap_window->GetNumberColumnsLegend(given_width - bar->smallest_x)));
1733 uint display_height = given_height - bar_height;
1734 display->AssignSizePosition(ST_RESIZE, x, y, given_width, display_height, rtl);
1735 bar->AssignSizePosition(ST_RESIZE, x, y + display_height, given_width, bar_height, rtl);
1738 virtual NWidgetCore *GetWidgetFromPos(int x, int y)
1740 if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
1741 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1742 NWidgetCore *widget = child_wid->GetWidgetFromPos(x, y);
1743 if (widget != NULL) return widget;
1745 return NULL;
1748 virtual void Draw(const Window *w)
1750 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) child_wid->Draw(w);
1754 /** Widget parts of the smallmap display. */
1755 static const NWidgetPart _nested_smallmap_display[] = {
1756 NWidget(WWT_PANEL, COLOUR_BROWN, WID_SM_MAP_BORDER),
1757 NWidget(WWT_INSET, COLOUR_BROWN, WID_SM_MAP), SetMinimalSize(346, 140), SetResize(1, 1), SetPadding(2, 2, 2, 2), EndContainer(),
1758 EndContainer(),
1761 /** Widget parts of the smallmap legend bar + image buttons. */
1762 static const NWidgetPart _nested_smallmap_bar[] = {
1763 NWidget(WWT_PANEL, COLOUR_BROWN),
1764 NWidget(NWID_HORIZONTAL),
1765 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SM_LEGEND), SetResize(1, 1),
1766 NWidget(NWID_VERTICAL),
1767 /* Top button row. */
1768 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1769 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_SM_ZOOM_IN),
1770 SetDataTip(SPR_IMG_ZOOMIN, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_IN), SetFill(1, 1),
1771 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_SM_CENTERMAP),
1772 SetDataTip(SPR_IMG_SMALLMAP, STR_SMALLMAP_CENTER), SetFill(1, 1),
1773 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_BLANK),
1774 SetDataTip(SPR_DOT_SMALL, STR_NULL), SetFill(1, 1),
1775 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_CONTOUR),
1776 SetDataTip(SPR_IMG_SHOW_COUNTOURS, STR_SMALLMAP_TOOLTIP_SHOW_LAND_CONTOURS_ON_MAP), SetFill(1, 1),
1777 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_VEHICLES),
1778 SetDataTip(SPR_IMG_SHOW_VEHICLES, STR_SMALLMAP_TOOLTIP_SHOW_VEHICLES_ON_MAP), SetFill(1, 1),
1779 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_INDUSTRIES),
1780 SetDataTip(SPR_IMG_INDUSTRY, STR_SMALLMAP_TOOLTIP_SHOW_INDUSTRIES_ON_MAP), SetFill(1, 1),
1781 EndContainer(),
1782 /* Bottom button row. */
1783 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1784 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_SM_ZOOM_OUT),
1785 SetDataTip(SPR_IMG_ZOOMOUT, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_OUT), SetFill(1, 1),
1786 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_TOGGLETOWNNAME),
1787 SetDataTip(SPR_IMG_TOWN, STR_SMALLMAP_TOOLTIP_TOGGLE_TOWN_NAMES_ON_OFF), SetFill(1, 1),
1788 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_LINKSTATS),
1789 SetDataTip(SPR_IMG_CARGOFLOW, STR_SMALLMAP_TOOLTIP_SHOW_LINK_STATS_ON_MAP), SetFill(1, 1),
1790 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_ROUTES),
1791 SetDataTip(SPR_IMG_SHOW_ROUTES, STR_SMALLMAP_TOOLTIP_SHOW_TRANSPORT_ROUTES_ON), SetFill(1, 1),
1792 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_VEGETATION),
1793 SetDataTip(SPR_IMG_PLANTTREES, STR_SMALLMAP_TOOLTIP_SHOW_VEGETATION_ON_MAP), SetFill(1, 1),
1794 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_OWNERS),
1795 SetDataTip(SPR_IMG_COMPANY_GENERAL, STR_SMALLMAP_TOOLTIP_SHOW_LAND_OWNERS_ON_MAP), SetFill(1, 1),
1796 EndContainer(),
1797 NWidget(NWID_SPACER), SetResize(0, 1),
1798 EndContainer(),
1799 EndContainer(),
1800 EndContainer(),
1803 static NWidgetBase *SmallMapDisplay(int *biggest_index)
1805 NWidgetContainer *map_display = new NWidgetSmallmapDisplay;
1807 MakeNWidgets(_nested_smallmap_display, lengthof(_nested_smallmap_display), biggest_index, map_display);
1808 MakeNWidgets(_nested_smallmap_bar, lengthof(_nested_smallmap_bar), biggest_index, map_display);
1809 return map_display;
1813 static const NWidgetPart _nested_smallmap_widgets[] = {
1814 NWidget(NWID_HORIZONTAL),
1815 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
1816 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_SM_CAPTION), SetDataTip(STR_SMALLMAP_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1817 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
1818 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
1819 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
1820 EndContainer(),
1821 NWidgetFunction(SmallMapDisplay), // Smallmap display and legend bar + image buttons.
1822 /* Bottom button row and resize box. */
1823 NWidget(NWID_HORIZONTAL),
1824 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_SM_SELECT_BUTTONS),
1825 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1826 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_SM_ENABLE_ALL), SetDataTip(STR_SMALLMAP_ENABLE_ALL, STR_NULL),
1827 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_SM_DISABLE_ALL), SetDataTip(STR_SMALLMAP_DISABLE_ALL, STR_NULL),
1828 NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_SM_SHOW_HEIGHT), SetDataTip(STR_SMALLMAP_SHOW_HEIGHT, STR_SMALLMAP_TOOLTIP_SHOW_HEIGHT),
1829 NWidget(WWT_PANEL, COLOUR_BROWN), SetFill(1, 0), SetResize(1, 0),
1830 EndContainer(),
1831 EndContainer(),
1832 NWidget(WWT_PANEL, COLOUR_BROWN), SetFill(1, 0), SetResize(1, 0),
1833 EndContainer(),
1834 EndContainer(),
1835 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
1836 EndContainer(),
1839 static WindowDesc _smallmap_desc(
1840 WDP_AUTO, "smallmap", 484, 314,
1841 WC_SMALLMAP, WC_NONE,
1843 _nested_smallmap_widgets, lengthof(_nested_smallmap_widgets)
1847 * Show the smallmap window.
1849 void ShowSmallMap()
1851 AllocateWindowDescFront<SmallMapWindow>(&_smallmap_desc, 0);
1855 * Scrolls the main window to given coordinates.
1856 * @param x x coordinate
1857 * @param y y coordinate
1858 * @param z z coordinate; -1 to scroll to terrain height
1859 * @param instant scroll instantly (meaningful only when smooth_scrolling is active)
1860 * @return did the viewport position change?
1862 bool ScrollMainWindowTo(int x, int y, int z, bool instant)
1864 bool res = ScrollWindowTo(x, y, z, FindWindowById(WC_MAIN_WINDOW, 0), instant);
1866 /* If a user scrolls to a tile (via what way what so ever) and already is on
1867 * that tile (e.g.: pressed twice), move the smallmap to that location,
1868 * so you directly see where you are on the smallmap. */
1870 if (res) return res;
1872 SmallMapWindow *w = dynamic_cast<SmallMapWindow*>(FindWindowById(WC_SMALLMAP, 0));
1873 if (w != NULL) w->SmallMapCenterOnCurrentPos();
1875 return res;