Add standard library header includes to precompiled header. Fix several uses of strin...
[openttd-joker.git] / src / smallmap_gui.cpp
blob9faf94ebd3f013c780663c41d34ba7631cfb3b9d
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 "screenshot.h"
28 #include "smallmap_colours.h"
29 #include "smallmap_gui.h"
31 #include "table/strings.h"
33 #include <bitset>
35 #include "safeguards.h"
37 static int _smallmap_industry_count; ///< Number of used industries
38 static int _smallmap_company_count; ///< Number of entries in the owner legend.
39 static int _smallmap_cargo_count; ///< Number of cargos in the link stats legend.
41 /** Link stat colours shown in legenda. */
42 static uint8 _linkstat_colours_in_legenda[] = {0, 1, 3, 5, 7, 9, 11};
44 /** Macro for ordinary entry of LegendAndColour */
45 #define MK(a, b) {a, b, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, false, false}
47 /** Macro for a height legend entry with configurable colour. */
48 #define MC(col_break) {0, STR_TINY_BLACK_HEIGHT, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, false, col_break}
50 /** Macro for non-company owned property entry of LegendAndColour */
51 #define MO(a, b) {a, b, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, false, false}
53 /** Macro used for forcing a rebuild of the owner legend the first time it is used. */
54 #define MOEND() {0, 0, INVALID_INDUSTRYTYPE, 0, OWNER_NONE, true, true, false}
56 /** Macro for end of list marker in arrays of LegendAndColour */
57 #define MKEND() {0, STR_NULL, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, true, false}
59 /**
60 * Macro for break marker in arrays of LegendAndColour.
61 * It will have valid data, though
63 #define MS(a, b) {a, b, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, false, true}
65 /** Legend text giving the colours to look for on the minimap */
66 static LegendAndColour _legend_land_contours[] = {
67 MK(PC_BLACK, STR_SMALLMAP_LEGENDA_ROADS),
68 MK(PC_GREY, STR_SMALLMAP_LEGENDA_RAILROADS),
69 MK(PC_LIGHT_BLUE, STR_SMALLMAP_LEGENDA_STATIONS_AIRPORTS_DOCKS),
70 MK(PC_DARK_RED, STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES),
71 MK(PC_WHITE, STR_SMALLMAP_LEGENDA_VEHICLES),
73 /* Placeholders for the colours and heights of the legend.
74 * The following values are set at BuildLandLegend() based
75 * on each colour scheme and the maximum map height. */
76 MC(true),
77 MC(false),
78 MC(false),
79 MC(false),
80 MC(false),
81 MC(false),
82 MC(true),
83 MC(false),
84 MC(false),
85 MC(false),
86 MC(false),
87 MC(false),
88 MKEND()
91 static const LegendAndColour _legend_vehicles[] = {
92 MK(PC_RED, STR_SMALLMAP_LEGENDA_TRAINS),
93 MK(PC_YELLOW, STR_SMALLMAP_LEGENDA_ROAD_VEHICLES),
94 MK(PC_LIGHT_BLUE, STR_SMALLMAP_LEGENDA_SHIPS),
95 MK(PC_WHITE, STR_SMALLMAP_LEGENDA_AIRCRAFT),
97 MS(PC_BLACK, STR_SMALLMAP_LEGENDA_TRANSPORT_ROUTES),
98 MK(PC_DARK_RED, STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES),
99 MKEND()
102 static const LegendAndColour _legend_routes[] = {
103 MK(PC_BLACK, STR_SMALLMAP_LEGENDA_ROADS),
104 MK(PC_GREY, STR_SMALLMAP_LEGENDA_RAILROADS),
105 MK(PC_DARK_RED, STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES),
107 MS(PC_VERY_DARK_BROWN, STR_SMALLMAP_LEGENDA_RAILROAD_STATION),
108 MK(PC_ORANGE, STR_SMALLMAP_LEGENDA_TRUCK_LOADING_BAY),
109 MK(PC_YELLOW, STR_SMALLMAP_LEGENDA_BUS_STATION),
110 MK(PC_RED, STR_SMALLMAP_LEGENDA_AIRPORT_HELIPORT),
111 MK(PC_LIGHT_BLUE, STR_SMALLMAP_LEGENDA_DOCK),
112 MKEND()
115 static const LegendAndColour _legend_vegetation[] = {
116 MK(PC_ROUGH_LAND, STR_SMALLMAP_LEGENDA_ROUGH_LAND),
117 MK(PC_GRASS_LAND, STR_SMALLMAP_LEGENDA_GRASS_LAND),
118 MK(PC_BARE_LAND, STR_SMALLMAP_LEGENDA_BARE_LAND),
119 MK(PC_FIELDS, STR_SMALLMAP_LEGENDA_FIELDS),
120 MK(PC_TREES, STR_SMALLMAP_LEGENDA_TREES),
121 MK(PC_GREEN, STR_SMALLMAP_LEGENDA_FOREST),
123 MS(PC_GREY, STR_SMALLMAP_LEGENDA_ROCKS),
124 MK(PC_ORANGE, STR_SMALLMAP_LEGENDA_DESERT),
125 MK(PC_LIGHT_BLUE, STR_SMALLMAP_LEGENDA_SNOW),
126 MK(PC_BLACK, STR_SMALLMAP_LEGENDA_TRANSPORT_ROUTES),
127 MK(PC_DARK_RED, STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES),
128 MKEND()
131 LegendAndColour _legend_land_owners[NUM_NO_COMPANY_ENTRIES + MAX_COMPANIES + 1] = {
132 MO(PC_WATER, STR_SMALLMAP_LEGENDA_WATER),
133 MO(0x00, STR_SMALLMAP_LEGENDA_NO_OWNER), // This colour will vary depending on settings.
134 MO(PC_DARK_RED, STR_SMALLMAP_LEGENDA_TOWNS),
135 MO(PC_DARK_GREY, STR_SMALLMAP_LEGENDA_INDUSTRIES),
136 /* The legend will be terminated the first time it is used. */
137 MOEND(),
140 #undef MK
141 #undef MC
142 #undef MS
143 #undef MO
144 #undef MOEND
145 #undef MKEND
147 /** Legend entries for the link stats view. */
148 static LegendAndColour _legend_linkstats[NUM_CARGO + lengthof(_linkstat_colours_in_legenda) + 1];
150 * Allow room for all industries, plus a terminator entry
151 * This is required in order to have the industry slots all filled up
153 LegendAndColour _legend_from_industries[NUM_INDUSTRYTYPES + 1];
154 /** For connecting industry type to position in industries list(small map legend) */
155 uint _industry_to_list_pos[NUM_INDUSTRYTYPES];
156 /** Show heightmap in industry and owner mode of smallmap window. */
157 bool _smallmap_show_heightmap = false;
158 /** Highlight a specific industry type */
159 static IndustryType _smallmap_industry_highlight = INVALID_INDUSTRYTYPE;
160 /** State of highlight blinking */
161 static bool _smallmap_industry_highlight_state;
162 /** For connecting company ID to position in owner list (small map legend) */
163 uint _company_to_list_pos[MAX_COMPANIES];
166 * Fills an array for the industries legends.
168 void BuildIndustriesLegend()
170 uint j = 0;
172 /* Add each name */
173 for (uint i = 0; i < NUM_INDUSTRYTYPES; i++) {
174 IndustryType ind = _sorted_industry_types[i];
175 const IndustrySpec *indsp = GetIndustrySpec(ind);
176 if (indsp->enabled) {
177 _legend_from_industries[j].legend = indsp->name;
178 _legend_from_industries[j].colour = indsp->map_colour;
179 _legend_from_industries[j].type = ind;
180 _legend_from_industries[j].show_on_map = true;
181 _legend_from_industries[j].col_break = false;
182 _legend_from_industries[j].end = false;
184 /* Store widget number for this industry type. */
185 _industry_to_list_pos[ind] = j;
186 j++;
189 /* Terminate the list */
190 _legend_from_industries[j].end = true;
192 /* Store number of enabled industries */
193 _smallmap_industry_count = j;
197 * Populate legend table for the link stat view.
199 void BuildLinkStatsLegend()
201 /* Clear the legend */
202 memset(_legend_linkstats, 0, sizeof(_legend_linkstats));
204 uint i = 0;
205 for (; i < _sorted_cargo_specs_size; ++i) {
206 const CargoSpec *cs = _sorted_cargo_specs[i];
208 _legend_linkstats[i].legend = cs->name;
209 _legend_linkstats[i].colour = cs->legend_colour;
210 _legend_linkstats[i].type = cs->Index();
211 _legend_linkstats[i].show_on_map = true;
214 _legend_linkstats[i].col_break = true;
215 _smallmap_cargo_count = i;
217 for (; i < _smallmap_cargo_count + lengthof(_linkstat_colours_in_legenda); ++i) {
218 _legend_linkstats[i].legend = STR_EMPTY;
219 _legend_linkstats[i].colour = LinkGraphOverlay::LINK_COLOURS[_linkstat_colours_in_legenda[i - _smallmap_cargo_count]];
220 _legend_linkstats[i].show_on_map = true;
223 _legend_linkstats[_smallmap_cargo_count].legend = STR_LINKGRAPH_LEGEND_UNUSED;
224 _legend_linkstats[i - 1].legend = STR_LINKGRAPH_LEGEND_OVERLOADED;
225 _legend_linkstats[(_smallmap_cargo_count + i - 1) / 2].legend = STR_LINKGRAPH_LEGEND_SATURATED;
226 _legend_linkstats[i].end = true;
229 static const LegendAndColour * const _legend_table[] = {
230 _legend_land_contours,
231 _legend_vehicles,
232 _legend_from_industries,
233 _legend_linkstats,
234 _legend_routes,
235 _legend_vegetation,
236 _legend_land_owners,
239 /** Available colour schemes for height maps. */
240 SmallMapColourScheme _heightmap_schemes[] = {
241 {NULL, _green_map_heights, lengthof(_green_map_heights), MKCOLOUR_XXXX(0x5B)}, ///< Green colour scheme.
242 {NULL, _dark_green_map_heights, lengthof(_dark_green_map_heights), MKCOLOUR_XXXX(0x62)}, ///< Dark green colour scheme.
243 {NULL, _violet_map_heights, lengthof(_violet_map_heights), MKCOLOUR_XXXX(0x82)}, ///< Violet colour scheme.
247 * (Re)build the colour tables for the legends.
249 void BuildLandLegend()
251 /* The smallmap window has never been initialized, so no need to change the legend. */
252 if (_heightmap_schemes[0].height_colours == NULL) return;
255 * The general idea of this function is to fill the legend with an appropriate evenly spaced
256 * selection of height levels. All entries with STR_TINY_BLACK_HEIGHT are reserved for this.
257 * At the moment there are twelve of these.
259 * The table below defines up to which height level a particular delta in the legend should be
260 * used. One could opt for just dividing the maximum height and use that as delta, but that
261 * creates many "ugly" legend labels, e.g. once every 950 meter. As a result, this table will
262 * reduce the number of deltas to 7: every 100m, 200m, 300m, 500m, 750m, 1000m and 1250m. The
263 * deltas are closer together at the lower numbers because going from 12 entries to just 4, as
264 * would happen when replacing 200m and 300m by 250m, would mean the legend would be short and
265 * that might not be considered appropriate.
267 * The current method yields at least 7 legend entries and at most 12. It can be increased to
268 * 8 by adding a 150m and 400m option, but especially 150m creates ugly heights.
270 * It tries to evenly space the legend items over the two columns that are there for the legend.
273 /* Table for delta; if max_height is less than the first column, use the second column as value. */
274 uint deltas[][2] = { { 24, 2 }, { 48, 4 }, { 72, 6 }, { 120, 10 }, { 180, 15 }, { 240, 20 }, { MAX_TILE_HEIGHT + 1, 25 }};
275 uint i = 0;
276 for (; _settings_game.construction.max_heightlevel >= deltas[i][0]; i++) {
277 /* Nothing to do here. */
279 uint delta = deltas[i][1];
281 int total_entries = (_settings_game.construction.max_heightlevel / delta) + 1;
282 int rows = CeilDiv(total_entries, 2);
283 int j = 0;
285 for (i = 0; i < lengthof(_legend_land_contours) - 1 && j < total_entries; i++) {
286 if (_legend_land_contours[i].legend != STR_TINY_BLACK_HEIGHT) continue;
288 _legend_land_contours[i].col_break = j % rows == 0;
289 _legend_land_contours[i].end = false;
290 _legend_land_contours[i].height = j * delta;
291 _legend_land_contours[i].colour = _heightmap_schemes[_settings_client.gui.smallmap_land_colour].height_colours[j * delta];
292 j++;
294 _legend_land_contours[i].end = true;
298 * Completes the array for the owned property legend.
300 void BuildOwnerLegend()
302 _legend_land_owners[1].colour = _heightmap_schemes[_settings_client.gui.smallmap_land_colour].default_colour;
304 int i = NUM_NO_COMPANY_ENTRIES;
305 const Company *c;
306 FOR_ALL_COMPANIES(c) {
307 _legend_land_owners[i].colour = _colour_gradient[c->colour][5];
308 _legend_land_owners[i].company = c->index;
309 _legend_land_owners[i].show_on_map = true;
310 _legend_land_owners[i].col_break = false;
311 _legend_land_owners[i].end = false;
312 _company_to_list_pos[c->index] = i;
313 i++;
316 /* Terminate the list */
317 _legend_land_owners[i].end = true;
319 /* Store maximum amount of owner legend entries. */
320 _smallmap_company_count = i;
324 * Return the colour a tile would be displayed with in the small map in mode "Contour".
325 * @param tile The tile of which we would like to get the colour.
326 * @param t Effective tile type of the tile (see #GetTileColours).
327 * @return The colour of tile in the small map in mode "Contour"
329 static inline uint32 GetSmallMapContoursPixels(TileIndex tile, TileType t)
331 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
332 return ApplyMask(cs->height_colours[TileHeight(tile)], &_smallmap_contours_andor[t]);
336 * Return the colour a tile would be displayed with in the small map in mode "Vehicles".
338 * @param tile The tile of which we would like to get the colour.
339 * @param t Effective tile type of the tile (see #GetTileColours).
340 * @return The colour of tile in the small map in mode "Vehicles"
342 static inline uint32 GetSmallMapVehiclesPixels(TileIndex tile, TileType t)
344 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
345 return ApplyMask(cs->default_colour, &_smallmap_vehicles_andor[t]);
349 * Return the colour a tile would be displayed with in the small map in mode "Industries".
351 * @param tile The tile of which we would like to get the colour.
352 * @param t Effective tile type of the tile (see #GetTileColours).
353 * @return The colour of tile in the small map in mode "Industries"
355 static inline uint32 GetSmallMapIndustriesPixels(TileIndex tile, TileType t)
357 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
358 return ApplyMask(_smallmap_show_heightmap ? cs->height_colours[TileHeight(tile)] : cs->default_colour, &_smallmap_vehicles_andor[t]);
362 * Return the colour a tile would be displayed with in the small map in mode "Routes".
364 * @param tile The tile of which we would like to get the colour.
365 * @param t Effective tile type of the tile (see #GetTileColours).
366 * @return The colour of tile in the small map in mode "Routes"
368 static inline uint32 GetSmallMapRoutesPixels(TileIndex tile, TileType t)
370 if (t == MP_STATION) {
371 switch (GetStationType(tile)) {
372 case STATION_RAIL: return MKCOLOUR_XXXX(PC_VERY_DARK_BROWN);
373 case STATION_AIRPORT: return MKCOLOUR_XXXX(PC_RED);
374 case STATION_TRUCK: return MKCOLOUR_XXXX(PC_ORANGE);
375 case STATION_BUS: return MKCOLOUR_XXXX(PC_YELLOW);
376 case STATION_DOCK: return MKCOLOUR_XXXX(PC_LIGHT_BLUE);
377 default: return MKCOLOUR_FFFF;
381 /* Ground colour */
382 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
383 return ApplyMask(cs->default_colour, &_smallmap_contours_andor[t]);
387 * Return the colour a tile would be displayed with in the small map in mode "link stats".
389 * @param tile The tile of which we would like to get the colour.
390 * @param t Effective tile type of the tile (see #GetTileColours).
391 * @return The colour of tile in the small map in mode "link stats"
393 static inline uint32 GetSmallMapLinkStatsPixels(TileIndex tile, TileType t)
395 return _smallmap_show_heightmap ? GetSmallMapContoursPixels(tile, t) : GetSmallMapRoutesPixels(tile, t);
399 * Return the colour a tile would be displayed with in the smallmap in mode "Vegetation".
401 * @param tile The tile of which we would like to get the colour.
402 * @param t Effective tile type of the tile (see #GetTileColours).
403 * @return The colour of tile in the smallmap in mode "Vegetation"
405 static inline uint32 GetSmallMapVegetationPixels(TileIndex tile, TileType t)
407 switch (t) {
408 case MP_CLEAR:
409 return (IsClearGround(tile, CLEAR_GRASS) && GetClearDensity(tile) < 3) ? MKCOLOUR_XXXX(PC_BARE_LAND) : _vegetation_clear_bits[GetClearGround(tile)];
411 case MP_INDUSTRY:
412 return IsTileForestIndustry(tile) ? MKCOLOUR_XXXX(PC_GREEN) : MKCOLOUR_XXXX(PC_DARK_RED);
414 case MP_TREES:
415 if (GetTreeGround(tile) == TREE_GROUND_SNOW_DESERT || GetTreeGround(tile) == TREE_GROUND_ROUGH_SNOW) {
416 return (_settings_game.game_creation.landscape == LT_ARCTIC) ? MKCOLOUR_XYYX(PC_LIGHT_BLUE, PC_TREES) : MKCOLOUR_XYYX(PC_ORANGE, PC_TREES);
418 return MKCOLOUR_XYYX(PC_GRASS_LAND, PC_TREES);
420 default:
421 return ApplyMask(MKCOLOUR_XXXX(PC_GRASS_LAND), &_smallmap_vehicles_andor[t]);
426 * Return the colour a tile would be displayed with in the small map in mode "Owner".
428 * @param tile The tile of which we would like to get the colour.
429 * @param t Effective tile type of the tile (see #GetTileColours).
430 * @return The colour of tile in the small map in mode "Owner"
432 static inline uint32 GetSmallMapOwnerPixels(TileIndex tile, TileType t)
434 Owner o;
436 switch (t) {
437 case MP_INDUSTRY: return MKCOLOUR_XXXX(PC_DARK_GREY);
438 case MP_HOUSE: return MKCOLOUR_XXXX(PC_DARK_RED);
439 default: o = GetTileOwner(tile); break;
440 /* FIXME: For MP_ROAD there are multiple owners.
441 * GetTileOwner returns the rail owner (level crossing) resp. the owner of ROADTYPE_ROAD (normal road),
442 * even if there are no ROADTYPE_ROAD bits on the tile.
446 if ((o < MAX_COMPANIES && !_legend_land_owners[_company_to_list_pos[o]].show_on_map) || o == OWNER_NONE || o == OWNER_WATER) {
447 if (t == MP_WATER) return MKCOLOUR_XXXX(PC_WATER);
448 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
449 return _smallmap_show_heightmap ? cs->height_colours[TileHeight(tile)] : cs->default_colour;
450 } else if (o == OWNER_TOWN) {
451 return MKCOLOUR_XXXX(PC_DARK_RED);
454 return MKCOLOUR_XXXX(_legend_land_owners[_company_to_list_pos[o]].colour);
457 static void NotifyAllViewports(ViewportMapType map_type)
459 Window *w;
460 FOR_ALL_WINDOWS_FROM_BACK(w) {
461 if (w->viewport != NULL)
462 if (w->viewport->zoom >= ZOOM_LVL_DRAW_MAP && w->viewport->map_type == map_type)
463 w->InvalidateData();
467 /** Vehicle colours in #SMT_VEHICLES mode. Indexed by #VehicleTypeByte. */
468 static const byte _vehicle_type_colours[6] = {
469 PC_RED, PC_YELLOW, PC_LIGHT_BLUE, PC_WHITE, PC_BLACK, PC_RED
473 /** Notify the industry chain window to stop sending newly selected industries. */
474 /* static */ void SmallMapWindow::BreakIndustryChainLink()
476 InvalidateWindowClassesData(WC_INDUSTRY_CARGOES, NUM_INDUSTRYTYPES);
479 inline Point SmallMapWindow::SmallmapRemapCoords(int x, int y) const
481 Point pt;
482 pt.x = (y - x) * 2;
483 pt.y = y + x;
484 return pt;
488 * Remap tile to location on this smallmap.
489 * @param tile_x X coordinate of the tile.
490 * @param tile_y Y coordinate of the tile.
491 * @return Position to draw on.
493 inline Point SmallMapWindow::RemapTile(int tile_x, int tile_y) const
495 int x_offset = tile_x - this->scroll_x / (int)TILE_SIZE;
496 int y_offset = tile_y - this->scroll_y / (int)TILE_SIZE;
498 if (this->zoom == 1) return SmallmapRemapCoords(x_offset, y_offset);
500 /* For negative offsets, round towards -inf. */
501 if (x_offset < 0) x_offset -= this->zoom - 1;
502 if (y_offset < 0) y_offset -= this->zoom - 1;
504 return SmallmapRemapCoords(x_offset / this->zoom, y_offset / this->zoom);
508 * Determine the tile relative to the base tile of the smallmap, and the pixel position at
509 * that tile for a point in the smallmap.
510 * @param px Horizontal coordinate of the pixel.
511 * @param py Vertical coordinate of the pixel.
512 * @param sub[out] Pixel position at the tile (0..3).
513 * @param add_sub Add current #subscroll to the position.
514 * @return Tile being displayed at the given position relative to #scroll_x and #scroll_y.
515 * @note The #subscroll offset is already accounted for.
517 inline Point SmallMapWindow::PixelToTile(int px, int py, int *sub, bool add_sub) const
519 if (add_sub) px += this->subscroll; // Total horizontal offset.
521 /* For each two rows down, add a x and a y tile, and
522 * For each four pixels to the right, move a tile to the right. */
523 Point pt = {((py >> 1) - (px >> 2)) * this->zoom, ((py >> 1) + (px >> 2)) * this->zoom};
524 px &= 3;
526 if (py & 1) { // Odd number of rows, handle the 2 pixel shift.
527 if (px < 2) {
528 pt.x += this->zoom;
529 px += 2;
530 } else {
531 pt.y += this->zoom;
532 px -= 2;
536 *sub = px;
537 return pt;
541 * Compute base parameters of the smallmap such that tile (\a tx, \a ty) starts at pixel (\a x, \a y).
542 * @param tx Tile x coordinate.
543 * @param ty Tile y coordinate.
544 * @param x Non-negative horizontal position in the display where the tile starts.
545 * @param y Non-negative vertical position in the display where the tile starts.
546 * @param sub [out] Value of #subscroll needed.
547 * @return #scroll_x, #scroll_y values.
549 Point SmallMapWindow::ComputeScroll(int tx, int ty, int x, int y, int *sub)
551 assert(x >= 0 && y >= 0);
553 int new_sub;
554 Point tile_xy = PixelToTile(x, y, &new_sub, false);
555 tx -= tile_xy.x;
556 ty -= tile_xy.y;
558 Point scroll;
559 if (new_sub == 0) {
560 *sub = 0;
561 scroll.x = (tx + this->zoom) * TILE_SIZE;
562 scroll.y = (ty - this->zoom) * TILE_SIZE;
563 } else {
564 *sub = 4 - new_sub;
565 scroll.x = (tx + 2 * this->zoom) * TILE_SIZE;
566 scroll.y = (ty - 2 * this->zoom) * TILE_SIZE;
568 return scroll;
572 * Initialize or change the zoom level.
573 * @param change Way to change the zoom level.
574 * @param zoom_pt Position to keep fixed while zooming.
575 * @pre \c *zoom_pt should contain a point in the smallmap display when zooming in or out.
577 void SmallMapWindow::SetZoomLevel(ZoomLevelChange change, const Point *zoom_pt)
579 static const int zoomlevels[] = {1, 2, 4, 6, 8}; // Available zoom levels. Bigger number means more zoom-out (further away).
580 static const int MIN_ZOOM_INDEX = 0;
581 static const int MAX_ZOOM_INDEX = lengthof(zoomlevels) - 1;
583 int new_index, cur_index, sub;
584 Point tile;
585 switch (change) {
586 case ZLC_INITIALIZE:
587 cur_index = - 1; // Definitely different from new_index.
588 new_index = MIN_ZOOM_INDEX;
589 tile.x = tile.y = 0;
590 break;
592 case ZLC_ZOOM_IN:
593 case ZLC_ZOOM_OUT:
594 for (cur_index = MIN_ZOOM_INDEX; cur_index <= MAX_ZOOM_INDEX; cur_index++) {
595 if (this->zoom == zoomlevels[cur_index]) break;
597 assert(cur_index <= MAX_ZOOM_INDEX);
599 tile = this->PixelToTile(zoom_pt->x, zoom_pt->y, &sub);
600 new_index = Clamp(cur_index + ((change == ZLC_ZOOM_IN) ? -1 : 1), MIN_ZOOM_INDEX, MAX_ZOOM_INDEX);
601 break;
603 default: NOT_REACHED();
606 if (new_index != cur_index) {
607 this->zoom = zoomlevels[new_index];
608 if (cur_index >= 0) {
609 Point new_tile = this->PixelToTile(zoom_pt->x, zoom_pt->y, &sub);
610 this->SetNewScroll(this->scroll_x + (tile.x - new_tile.x) * TILE_SIZE,
611 this->scroll_y + (tile.y - new_tile.y) * TILE_SIZE, sub);
612 } else if (this->map_type == SMT_LINKSTATS) {
613 this->overlay->RebuildCache();
615 this->SetWidgetDisabledState(WID_SM_ZOOM_IN, this->zoom == zoomlevels[MIN_ZOOM_INDEX]);
616 this->SetWidgetDisabledState(WID_SM_ZOOM_OUT, this->zoom == zoomlevels[MAX_ZOOM_INDEX]);
617 this->SetDirty();
622 * Decide which colours to show to the user for a group of tiles.
623 * @param ta Tile area to investigate.
624 * @return Colours to display.
626 inline uint32 SmallMapWindow::GetTileColours(const TileArea &ta) const
628 int importance = 0;
629 TileIndex tile = INVALID_TILE; // Position of the most important tile.
630 TileType et = MP_VOID; // Effective tile type at that position.
632 TILE_AREA_LOOP(ti, ta) {
633 TileType ttype = GetTileType(ti);
635 switch (ttype) {
636 case MP_TUNNELBRIDGE: {
637 TransportType tt = GetTunnelBridgeTransportType(ti);
639 switch (tt) {
640 case TRANSPORT_RAIL: ttype = MP_RAILWAY; break;
641 case TRANSPORT_ROAD: ttype = MP_ROAD; break;
642 default: ttype = MP_WATER; break;
644 break;
647 case MP_INDUSTRY:
648 /* Special handling of industries while in "Industries" smallmap view. */
649 if (this->map_type == SMT_INDUSTRY) {
650 /* If industry is allowed to be seen, use its colour on the map.
651 * This has the highest priority above any value in _tiletype_importance. */
652 IndustryType type = Industry::GetByTile(ti)->type;
653 if (_legend_from_industries[_industry_to_list_pos[type]].show_on_map) {
654 if (type == _smallmap_industry_highlight) {
655 if (_smallmap_industry_highlight_state) return MKCOLOUR_XXXX(PC_WHITE);
656 } else {
657 return GetIndustrySpec(type)->map_colour * 0x01010101;
660 /* Otherwise make it disappear */
661 ttype = IsTileOnWater(ti) ? MP_WATER : MP_CLEAR;
663 break;
665 default:
666 break;
669 if (_tiletype_importance[ttype] > importance) {
670 importance = _tiletype_importance[ttype];
671 tile = ti;
672 et = ttype;
676 switch (this->map_type) {
677 case SMT_CONTOUR:
678 return GetSmallMapContoursPixels(tile, et);
680 case SMT_VEHICLES:
681 return GetSmallMapVehiclesPixels(tile, et);
683 case SMT_INDUSTRY:
684 return GetSmallMapIndustriesPixels(tile, et);
686 case SMT_LINKSTATS:
687 return GetSmallMapLinkStatsPixels(tile, et);
689 case SMT_ROUTES:
690 return GetSmallMapRoutesPixels(tile, et);
692 case SMT_VEGETATION:
693 return GetSmallMapVegetationPixels(tile, et);
695 case SMT_OWNER:
696 return GetSmallMapOwnerPixels(tile, et);
698 default: NOT_REACHED();
703 * Draws one column of tiles of the small map in a certain mode onto the screen buffer, skipping the shifted rows in between.
705 * @param dst Pointer to a part of the screen buffer to write to.
706 * @param xc The X coordinate of the first tile in the column.
707 * @param yc The Y coordinate of the first tile in the column
708 * @param pitch Number of pixels to advance in the screen buffer each time a pixel is written.
709 * @param reps Number of lines to draw
710 * @param start_pos Position of first pixel to draw.
711 * @param end_pos Position of last pixel to draw (exclusive).
712 * @param blitter current blitter
713 * @note If pixel position is below \c 0, skip drawing.
715 void SmallMapWindow::DrawSmallMapColumn(void *dst, uint xc, uint yc, int pitch, int reps, int start_pos, int end_pos, Blitter *blitter) const
717 void *dst_ptr_abs_end = blitter->MoveTo(_screen.dst_ptr, 0, _screen.height);
718 uint min_xy = _settings_game.construction.freeform_edges ? 1 : 0;
720 do {
721 /* Check if the tile (xc,yc) is within the map range */
722 if (xc >= MapMaxX() || yc >= MapMaxY()) continue;
724 /* Check if the dst pointer points to a pixel inside the screen buffer */
725 if (dst < _screen.dst_ptr) continue;
726 if (dst >= dst_ptr_abs_end) continue;
728 /* Construct tilearea covered by (xc, yc, xc + this->zoom, yc + this->zoom) such that it is within min_xy limits. */
729 TileArea ta;
730 if (min_xy == 1 && (xc == 0 || yc == 0)) {
731 if (this->zoom == 1) continue; // The tile area is empty, don't draw anything.
733 ta = TileArea(TileXY(max(min_xy, xc), max(min_xy, yc)), this->zoom - (xc == 0), this->zoom - (yc == 0));
734 } else {
735 ta = TileArea(TileXY(xc, yc), this->zoom, this->zoom);
737 ta.ClampToMap(); // Clamp to map boundaries (may contain MP_VOID tiles!).
739 uint32 val = this->GetTileColours(ta);
740 uint8 *val8 = (uint8 *)&val;
741 int idx = max(0, -start_pos);
742 for (int pos = max(0, start_pos); pos < end_pos; pos++) {
743 blitter->SetPixel(dst, idx, 0, val8[idx]);
744 idx++;
746 /* Switch to next tile in the column */
747 } while (xc += this->zoom, yc += this->zoom, dst = blitter->MoveTo(dst, pitch, 0), --reps != 0);
751 * Adds vehicles to the smallmap.
752 * @param dpi the part of the smallmap to be drawn into
753 * @param blitter current blitter
755 void SmallMapWindow::DrawVehicles(const DrawPixelInfo *dpi, Blitter *blitter) const
757 const Vehicle *v;
758 FOR_ALL_VEHICLES(v) {
759 if (v->type == VEH_EFFECT) continue;
760 if (v->vehstatus & (VS_HIDDEN | VS_UNCLICKABLE)) continue;
762 /* Remap into flat coordinates. */
763 Point pt = this->RemapTile(v->x_pos / (int)TILE_SIZE, v->y_pos / (int)TILE_SIZE);
765 int y = pt.y - dpi->top;
766 if (!IsInsideMM(y, 0, dpi->height)) continue; // y is out of bounds.
768 bool skip = false; // Default is to draw both pixels.
769 int x = pt.x - this->subscroll - 3 - dpi->left; // Offset X coordinate.
770 if (x < 0) {
771 /* if x+1 is 0, that means we're on the very left edge,
772 * and should thus only draw a single pixel */
773 if (++x != 0) continue;
774 skip = true;
775 } else if (x >= dpi->width - 1) {
776 /* Check if we're at the very right edge, and if so draw only a single pixel */
777 if (x != dpi->width - 1) continue;
778 skip = true;
781 /* Calculate pointer to pixel and the colour */
782 byte colour = (this->map_type == SMT_VEHICLES) ? _vehicle_type_colours[v->type] : PC_WHITE;
784 /* And draw either one or two pixels depending on clipping */
785 blitter->SetPixel(dpi->dst_ptr, x, y, colour);
786 if (!skip) blitter->SetPixel(dpi->dst_ptr, x + 1, y, colour);
791 * Adds town names to the smallmap.
792 * @param dpi the part of the smallmap to be drawn into
794 void SmallMapWindow::DrawTowns(const DrawPixelInfo *dpi) const
796 const Town *t;
797 FOR_ALL_TOWNS(t) {
798 /* Remap the town coordinate */
799 Point pt = this->RemapTile(TileX(t->xy), TileY(t->xy));
800 int x = pt.x - this->subscroll - (t->cache.sign.width_small >> 1);
801 int y = pt.y;
803 /* Check if the town sign is within bounds */
804 if (x + t->cache.sign.width_small > dpi->left &&
805 x < dpi->left + dpi->width &&
806 y + FONT_HEIGHT_SMALL > dpi->top &&
807 y < dpi->top + dpi->height) {
808 /* And draw it. */
809 SetDParam(0, t->index);
810 DrawString(x, x + t->cache.sign.width_small, y, STR_SMALLMAP_TOWN);
816 * Adds map indicators to the smallmap.
818 void SmallMapWindow::DrawMapIndicators() const
820 /* Find main viewport. */
821 const ViewPort *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
823 Point upper_left_smallmap_coord = TranslateXYToTileCoord(vp, vp->left, vp->top, false);
824 Point lower_right_smallmap_coord = TranslateXYToTileCoord(vp, vp->left + vp->width - 1, vp->top + vp->height - 1, false);
826 Point upper_left = this->RemapTile(upper_left_smallmap_coord.x / (int)TILE_SIZE, upper_left_smallmap_coord.y / (int)TILE_SIZE);
827 upper_left.x -= this->subscroll;
829 Point lower_right = this->RemapTile(lower_right_smallmap_coord.x / (int)TILE_SIZE, lower_right_smallmap_coord.y / (int)TILE_SIZE);
830 lower_right.x -= this->subscroll;
832 SmallMapWindow::DrawVertMapIndicator(upper_left.x, upper_left.y, lower_right.y);
833 SmallMapWindow::DrawVertMapIndicator(lower_right.x, upper_left.y, lower_right.y);
835 SmallMapWindow::DrawHorizMapIndicator(upper_left.x, lower_right.x, upper_left.y);
836 SmallMapWindow::DrawHorizMapIndicator(upper_left.x, lower_right.x, lower_right.y);
840 * Draws the small map.
842 * Basically, the small map is draw column of pixels by column of pixels. The pixels
843 * are drawn directly into the screen buffer. The final map is drawn in multiple passes.
844 * The passes are:
845 * <ol><li>The colours of tiles in the different modes.</li>
846 * <li>Town names (optional)</li></ol>
848 * @param dpi pointer to pixel to write onto
850 void SmallMapWindow::DrawSmallMap(DrawPixelInfo *dpi, bool draw_indicators) const
852 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
853 DrawPixelInfo *old_dpi;
855 old_dpi = _cur_dpi;
856 _cur_dpi = dpi;
858 /* Clear it */
859 GfxFillRect(dpi->left, dpi->top, dpi->left + dpi->width - 1, dpi->top + dpi->height - 1, PC_BLACK);
861 /* Which tile is displayed at (dpi->left, dpi->top)? */
862 int dx;
863 Point tile = this->PixelToTile(dpi->left, dpi->top, &dx);
864 int tile_x = this->scroll_x / (int)TILE_SIZE + tile.x;
865 int tile_y = this->scroll_y / (int)TILE_SIZE + tile.y;
867 void *ptr = blitter->MoveTo(dpi->dst_ptr, -dx - 4, 0);
868 int x = - dx - 4;
869 int y = 0;
871 for (;;) {
872 /* Distance from left edge */
873 if (x >= -3) {
874 if (x >= dpi->width) break; // Exit the loop.
876 int end_pos = min(dpi->width, x + 4);
877 int reps = (dpi->height - y + 1) / 2; // Number of lines.
878 if (reps > 0) {
879 this->DrawSmallMapColumn(ptr, tile_x, tile_y, dpi->pitch * 2, reps, x, end_pos, blitter);
883 if (y == 0) {
884 tile_y += this->zoom;
885 y++;
886 ptr = blitter->MoveTo(ptr, 0, 1);
887 } else {
888 tile_x -= this->zoom;
889 y--;
890 ptr = blitter->MoveTo(ptr, 0, -1);
892 ptr = blitter->MoveTo(ptr, 2, 0);
893 x += 2;
896 /* Draw vehicles */
897 if (this->map_type == SMT_CONTOUR || this->map_type == SMT_VEHICLES) this->DrawVehicles(dpi, blitter);
899 /* Draw link stat overlay */
900 if (this->map_type == SMT_LINKSTATS) this->overlay->Draw(dpi);
902 /* Draw town names */
903 if (this->show_towns) this->DrawTowns(dpi);
905 /* Draw map indicators */
906 if (draw_indicators) this->DrawMapIndicators();
908 _cur_dpi = old_dpi;
912 * Function to set up widgets depending on the information being shown on the smallmap.
914 void SmallMapWindow::SetupWidgetData()
916 StringID legend_tooltip;
917 StringID enable_all_tooltip;
918 StringID disable_all_tooltip;
919 int plane;
920 switch (this->map_type) {
921 case SMT_INDUSTRY:
922 legend_tooltip = STR_SMALLMAP_TOOLTIP_INDUSTRY_SELECTION;
923 enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_INDUSTRIES;
924 disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_INDUSTRIES;
925 plane = 0;
926 break;
928 case SMT_OWNER:
929 legend_tooltip = STR_SMALLMAP_TOOLTIP_COMPANY_SELECTION;
930 enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_COMPANIES;
931 disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_COMPANIES;
932 plane = 0;
933 break;
935 case SMT_LINKSTATS:
936 legend_tooltip = STR_SMALLMAP_TOOLTIP_CARGO_SELECTION;
937 enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_CARGOS;
938 disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_CARGOS;
939 plane = 0;
940 break;
942 default:
943 legend_tooltip = STR_NULL;
944 enable_all_tooltip = STR_NULL;
945 disable_all_tooltip = STR_NULL;
946 plane = 1;
947 break;
950 this->GetWidget<NWidgetCore>(WID_SM_LEGEND)->SetDataTip(STR_NULL, legend_tooltip);
951 this->GetWidget<NWidgetCore>(WID_SM_ENABLE_ALL)->SetDataTip(STR_SMALLMAP_ENABLE_ALL, enable_all_tooltip);
952 this->GetWidget<NWidgetCore>(WID_SM_DISABLE_ALL)->SetDataTip(STR_SMALLMAP_DISABLE_ALL, disable_all_tooltip);
953 this->GetWidget<NWidgetStacked>(WID_SM_SELECT_BUTTONS)->SetDisplayedPlane(plane);
956 SmallMapWindow::SmallMapWindow(WindowDesc *desc, int window_number) : Window(desc), refresh(FORCE_REFRESH_PERIOD)
958 _smallmap_industry_highlight = INVALID_INDUSTRYTYPE;
959 this->overlay = new LinkGraphOverlay(this, WID_SM_MAP, 0, this->GetOverlayCompanyMask(), 1);
960 this->InitNested(window_number);
961 this->LowerWidget(this->map_type + WID_SM_CONTOUR);
963 this->RebuildColourIndexIfNecessary();
965 this->SetWidgetLoweredState(WID_SM_SHOW_HEIGHT, _smallmap_show_heightmap);
967 this->SetWidgetLoweredState(WID_SM_TOGGLETOWNNAME, this->show_towns);
969 this->SetupWidgetData();
971 this->SetZoomLevel(ZLC_INITIALIZE, NULL);
972 this->SmallMapCenterOnCurrentPos();
973 this->SetOverlayCargoMask();
976 SmallMapWindow::~SmallMapWindow()
978 delete this->overlay;
979 this->BreakIndustryChainLink();
983 * Rebuilds the colour indices used for fast access to the smallmap contour colours based on the heightlevel.
985 void SmallMapWindow::RebuildColourIndexIfNecessary()
987 /* Rebuild colour indices if necessary. */
988 if (SmallMapWindow::max_heightlevel == _settings_game.construction.max_heightlevel) return;
990 for (uint n = 0; n < lengthof(_heightmap_schemes); n++) {
991 /* The heights go from 0 up to and including maximum. */
992 int heights = _settings_game.construction.max_heightlevel + 1;
993 _heightmap_schemes[n].height_colours = ReallocT<uint32>(_heightmap_schemes[n].height_colours, heights);
995 for (int z = 0; z < heights; z++) {
996 size_t access_index = (_heightmap_schemes[n].colour_count * z) / heights;
998 /* Choose colour by mapping the range (0..max heightlevel) on the complete colour table. */
999 _heightmap_schemes[n].height_colours[z] = _heightmap_schemes[n].height_colours_base[access_index];
1003 SmallMapWindow::max_heightlevel = _settings_game.construction.max_heightlevel;
1004 BuildLandLegend();
1007 /* virtual */ void SmallMapWindow::SetStringParameters(int widget) const
1009 switch (widget) {
1010 case WID_SM_CAPTION:
1011 SetDParam(0, STR_SMALLMAP_TYPE_CONTOURS + this->map_type);
1012 break;
1016 /* virtual */ void SmallMapWindow::OnInit()
1018 uint min_width = 0;
1019 this->min_number_of_columns = INDUSTRY_MIN_NUMBER_OF_COLUMNS;
1020 this->min_number_of_fixed_rows = lengthof(_linkstat_colours_in_legenda);
1021 for (uint i = 0; i < lengthof(_legend_table); i++) {
1022 uint height = 0;
1023 uint num_columns = 1;
1024 for (const LegendAndColour *tbl = _legend_table[i]; !tbl->end; ++tbl) {
1025 StringID str;
1026 if (i == SMT_INDUSTRY) {
1027 SetDParam(0, tbl->legend);
1028 SetDParam(1, IndustryPool::MAX_SIZE);
1029 str = STR_SMALLMAP_INDUSTRY;
1030 } else if (i == SMT_LINKSTATS) {
1031 SetDParam(0, tbl->legend);
1032 str = STR_SMALLMAP_LINKSTATS;
1033 } else if (i == SMT_OWNER) {
1034 if (tbl->company != INVALID_COMPANY) {
1035 if (!Company::IsValidID(tbl->company)) {
1036 /* Rebuild the owner legend. */
1037 BuildOwnerLegend();
1038 this->OnInit();
1039 return;
1041 /* Non-fixed legend entries for the owner view. */
1042 SetDParam(0, tbl->company);
1043 str = STR_SMALLMAP_COMPANY;
1044 } else {
1045 str = tbl->legend;
1047 } else {
1048 if (tbl->col_break) {
1049 this->min_number_of_fixed_rows = max(this->min_number_of_fixed_rows, height);
1050 height = 0;
1051 num_columns++;
1053 height++;
1054 str = tbl->legend;
1056 min_width = max(GetStringBoundingBox(str).width, min_width);
1058 this->min_number_of_fixed_rows = max(this->min_number_of_fixed_rows, height);
1059 this->min_number_of_columns = max(this->min_number_of_columns, num_columns);
1062 /* The width of a column is the minimum width of all texts + the size of the blob + some spacing */
1063 this->column_width = min_width + LEGEND_BLOB_WIDTH + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
1066 /* virtual */ void SmallMapWindow::OnPaint()
1068 if (this->map_type == SMT_OWNER) {
1069 for (const LegendAndColour *tbl = _legend_table[this->map_type]; !tbl->end; ++tbl) {
1070 if (tbl->company != INVALID_COMPANY && !Company::IsValidID(tbl->company)) {
1071 /* Rebuild the owner legend. */
1072 BuildOwnerLegend();
1073 this->InvalidateData(1);
1074 break;
1079 this->DrawWidgets();
1082 /* virtual */ void SmallMapWindow::DrawWidget(const Rect &r, int widget) const
1084 switch (widget) {
1085 case WID_SM_MAP: {
1086 DrawPixelInfo new_dpi;
1087 if (!FillDrawPixelInfo(&new_dpi, r.left + 1, r.top + 1, r.right - r.left - 1, r.bottom - r.top - 1)) return;
1088 this->DrawSmallMap(&new_dpi);
1089 break;
1092 case WID_SM_LEGEND: {
1093 uint columns = this->GetNumberColumnsLegend(r.right - r.left + 1);
1094 uint number_of_rows = this->GetNumberRowsLegend(columns);
1095 bool rtl = _current_text_dir == TD_RTL;
1096 uint y_org = r.top + WD_FRAMERECT_TOP;
1097 uint x = rtl ? r.right - this->column_width - WD_FRAMERECT_RIGHT : r.left + WD_FRAMERECT_LEFT;
1098 uint y = y_org;
1099 uint i = 0; // Row counter for industry legend.
1100 uint row_height = FONT_HEIGHT_SMALL;
1102 uint text_left = rtl ? 0 : LEGEND_BLOB_WIDTH + WD_FRAMERECT_LEFT;
1103 uint text_right = this->column_width - 1 - (rtl ? LEGEND_BLOB_WIDTH + WD_FRAMERECT_RIGHT : 0);
1104 uint blob_left = rtl ? this->column_width - 1 - LEGEND_BLOB_WIDTH : 0;
1105 uint blob_right = rtl ? this->column_width - 1 : LEGEND_BLOB_WIDTH;
1107 StringID string = STR_NULL;
1108 switch (this->map_type) {
1109 case SMT_INDUSTRY:
1110 string = STR_SMALLMAP_INDUSTRY;
1111 break;
1112 case SMT_LINKSTATS:
1113 string = STR_SMALLMAP_LINKSTATS;
1114 break;
1115 case SMT_OWNER:
1116 string = STR_SMALLMAP_COMPANY;
1117 break;
1118 default:
1119 break;
1122 for (const LegendAndColour *tbl = _legend_table[this->map_type]; !tbl->end; ++tbl) {
1123 if (tbl->col_break || ((this->map_type == SMT_INDUSTRY || this->map_type == SMT_OWNER || this->map_type == SMT_LINKSTATS) && i++ >= number_of_rows)) {
1124 /* Column break needed, continue at top, COLUMN_WIDTH pixels
1125 * (one "row") to the right. */
1126 x += rtl ? -(int)this->column_width : this->column_width;
1127 y = y_org;
1128 i = 1;
1131 uint8 legend_colour = tbl->colour;
1133 switch (this->map_type) {
1134 case SMT_INDUSTRY:
1135 /* Industry name must be formatted, since it's not in tiny font in the specs.
1136 * So, draw with a parameter and use the STR_SMALLMAP_INDUSTRY string, which is tiny font */
1137 SetDParam(0, tbl->legend);
1138 SetDParam(1, Industry::GetIndustryTypeCount(tbl->type));
1139 if (tbl->show_on_map && tbl->type == _smallmap_industry_highlight) {
1140 legend_colour = _smallmap_industry_highlight_state ? PC_WHITE : PC_BLACK;
1142 FALLTHROUGH;
1144 case SMT_LINKSTATS:
1145 SetDParam(0, tbl->legend);
1146 FALLTHROUGH;
1148 case SMT_OWNER:
1149 if (this->map_type != SMT_OWNER || tbl->company != INVALID_COMPANY) {
1150 if (this->map_type == SMT_OWNER) SetDParam(0, tbl->company);
1151 if (!tbl->show_on_map) {
1152 /* Simply draw the string, not the black border of the legend colour.
1153 * This will enforce the idea of the disabled item */
1154 DrawString(x + text_left, x + text_right, y, string, TC_GREY);
1155 } else {
1156 DrawString(x + text_left, x + text_right, y, string, TC_BLACK);
1157 GfxFillRect(x + blob_left, y + 1, x + blob_right, y + row_height - 1, PC_BLACK); // Outer border of the legend colour
1159 break;
1161 FALLTHROUGH;
1163 default:
1164 if (this->map_type == SMT_CONTOUR) SetDParam(0, tbl->height * TILE_HEIGHT_STEP);
1165 /* Anything that is not an industry or a company is using normal process */
1166 GfxFillRect(x + blob_left, y + 1, x + blob_right, y + row_height - 1, PC_BLACK);
1167 DrawString(x + text_left, x + text_right, y, tbl->legend);
1168 break;
1170 GfxFillRect(x + blob_left + 1, y + 2, x + blob_right - 1, y + row_height - 2, legend_colour); // Legend colour
1172 y += row_height;
1179 * Select a new map type.
1180 * @param map_type New map type.
1182 void SmallMapWindow::SwitchMapType(SmallMapType map_type)
1184 this->RaiseWidget(this->map_type + WID_SM_CONTOUR);
1185 this->map_type = map_type;
1186 this->LowerWidget(this->map_type + WID_SM_CONTOUR);
1188 this->SetupWidgetData();
1190 if (map_type == SMT_LINKSTATS) this->overlay->RebuildCache();
1191 if (map_type != SMT_INDUSTRY) this->BreakIndustryChainLink();
1192 this->SetDirty();
1196 * Get the number of rows in the legend from the number of columns. Those
1197 * are at least min_number_of_fixed_rows and possibly more if there are so
1198 * many cargoes, industry types or companies that they won't fit in the
1199 * available space.
1200 * @param columns Number of columns in the legend.
1201 * @return Number of rows needed for everything to fit in.
1203 inline uint SmallMapWindow::GetNumberRowsLegend(uint columns) const
1205 /* Reserve one column for link colours */
1206 uint num_rows_linkstats = CeilDiv(_smallmap_cargo_count, columns - 1);
1207 uint num_rows_others = CeilDiv(max(_smallmap_industry_count, _smallmap_company_count), columns);
1208 return max(this->min_number_of_fixed_rows, max(num_rows_linkstats, num_rows_others));
1212 * Select and toggle a legend item. When CTRL is pressed, disable all other
1213 * items in the group defined by begin_legend_item and end_legend_item and
1214 * keep the clicked one enabled even if it was already enabled before. If
1215 * the other items in the group are all disabled already and CTRL is pressed
1216 * enable them instead.
1217 * @param click_pos the index of the item being selected
1218 * @param legend the legend from which we select
1219 * @param end_legend_item index one past the last item in the group to be inverted
1220 * @param begin_legend_item index of the first item in the group to be inverted
1222 void SmallMapWindow::SelectLegendItem(int click_pos, LegendAndColour *legend, int end_legend_item, int begin_legend_item)
1224 if (_ctrl_pressed) {
1225 /* Disable all, except the clicked one */
1226 bool changes = false;
1227 for (int i = begin_legend_item; i != end_legend_item; i++) {
1228 bool new_state = (i == click_pos);
1229 if (legend[i].show_on_map != new_state) {
1230 changes = true;
1231 legend[i].show_on_map = new_state;
1234 if (!changes) {
1235 /* Nothing changed? Then show all (again). */
1236 for (int i = begin_legend_item; i != end_legend_item; i++) {
1237 legend[i].show_on_map = true;
1240 } else {
1241 legend[click_pos].show_on_map = !legend[click_pos].show_on_map;
1244 if (this->map_type == SMT_INDUSTRY) this->BreakIndustryChainLink();
1248 * Set the link graph overlay cargo mask from the legend.
1250 void SmallMapWindow::SetOverlayCargoMask()
1252 uint32 cargo_mask = 0;
1253 for (int i = 0; i != _smallmap_cargo_count; ++i) {
1254 if (_legend_linkstats[i].show_on_map) SetBit(cargo_mask, _legend_linkstats[i].type);
1256 this->overlay->SetCargoMask(cargo_mask);
1260 * Determines the mouse position on the legend.
1261 * @param pt Mouse position.
1262 * @return Legend item under the mouse.
1264 int SmallMapWindow::GetPositionOnLegend(Point pt)
1266 const NWidgetBase *wi = this->GetWidget<NWidgetBase>(WID_SM_LEGEND);
1267 uint line = (pt.y - wi->pos_y - WD_FRAMERECT_TOP) / FONT_HEIGHT_SMALL;
1268 uint columns = this->GetNumberColumnsLegend(wi->current_x);
1269 uint number_of_rows = this->GetNumberRowsLegend(columns);
1270 if (line >= number_of_rows) return -1;
1272 bool rtl = _current_text_dir == TD_RTL;
1273 int x = pt.x - wi->pos_x;
1274 if (rtl) x = wi->current_x - x;
1275 uint column = (x - WD_FRAMERECT_LEFT) / this->column_width;
1277 return (column * number_of_rows) + line;
1280 /* virtual */ void SmallMapWindow::OnMouseOver(Point pt, int widget)
1282 IndustryType new_highlight = INVALID_INDUSTRYTYPE;
1283 if (widget == WID_SM_LEGEND && this->map_type == SMT_INDUSTRY) {
1284 int industry_pos = GetPositionOnLegend(pt);
1285 if (industry_pos >= 0 && industry_pos < _smallmap_industry_count) {
1286 new_highlight = _legend_from_industries[industry_pos].type;
1289 if (new_highlight != _smallmap_industry_highlight) {
1290 _smallmap_industry_highlight = new_highlight;
1291 this->refresh = _smallmap_industry_highlight != INVALID_INDUSTRYTYPE ? BLINK_PERIOD : FORCE_REFRESH_PERIOD;
1292 _smallmap_industry_highlight_state = true;
1293 this->SetDirty();
1297 /* virtual */ void SmallMapWindow::OnClick(Point pt, int widget, int click_count)
1299 switch (widget) {
1300 case WID_SM_MAP: { // Map window
1302 * XXX: scrolling with the left mouse button is done by subsequently
1303 * clicking with the left mouse button; clicking once centers the
1304 * large map at the selected point. So by unclicking the left mouse
1305 * button here, it gets reclicked during the next inputloop, which
1306 * would make it look like the mouse is being dragged, while it is
1307 * actually being (virtually) clicked every inputloop.
1309 _left_button_clicked = false;
1311 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1312 Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
1313 int sub;
1314 pt = this->PixelToTile(pt.x - wid->pos_x, pt.y - wid->pos_y, &sub);
1315 ScrollWindowTo(this->scroll_x + pt.x * TILE_SIZE, this->scroll_y + pt.y * TILE_SIZE, -1, w);
1317 this->SetDirty();
1318 break;
1321 case WID_SM_ZOOM_IN:
1322 case WID_SM_ZOOM_OUT: {
1323 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1324 Point pt = { (int)wid->current_x / 2, (int)wid->current_y / 2};
1325 this->SetZoomLevel((widget == WID_SM_ZOOM_IN) ? ZLC_ZOOM_IN : ZLC_ZOOM_OUT, &pt);
1326 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1327 break;
1330 case WID_SM_CONTOUR: // Show land contours
1331 case WID_SM_VEHICLES: // Show vehicles
1332 case WID_SM_INDUSTRIES: // Show industries
1333 case WID_SM_LINKSTATS: // Show route map
1334 case WID_SM_ROUTES: // Show transport routes
1335 case WID_SM_VEGETATION: // Show vegetation
1336 case WID_SM_OWNERS: // Show land owners
1337 this->SwitchMapType((SmallMapType)(widget - WID_SM_CONTOUR));
1338 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1339 break;
1341 case WID_SM_CENTERMAP: // Center the smallmap again
1342 this->SmallMapCenterOnCurrentPos();
1343 this->HandleButtonClick(WID_SM_CENTERMAP);
1344 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1345 break;
1347 case WID_SM_TOGGLETOWNNAME: // Toggle town names
1348 this->show_towns = !this->show_towns;
1349 this->SetWidgetLoweredState(WID_SM_TOGGLETOWNNAME, this->show_towns);
1351 this->SetDirty();
1352 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1353 break;
1355 case WID_SM_LEGEND: // Legend
1356 if (this->map_type == SMT_INDUSTRY || this->map_type == SMT_LINKSTATS || this->map_type == SMT_OWNER) {
1357 int click_pos = this->GetPositionOnLegend(pt);
1358 if (click_pos < 0) break;
1360 /* If industry type small map*/
1361 if (this->map_type == SMT_INDUSTRY) {
1362 /* If click on industries label, find right industry type and enable/disable it. */
1363 if (click_pos < _smallmap_industry_count) {
1364 this->SelectLegendItem(click_pos, _legend_from_industries, _smallmap_industry_count);
1365 NotifyAllViewports(VPMT_INDUSTRY);
1367 } else if (this->map_type == SMT_LINKSTATS) {
1368 if (click_pos < _smallmap_cargo_count) {
1369 this->SelectLegendItem(click_pos, _legend_linkstats, _smallmap_cargo_count);
1370 this->SetOverlayCargoMask();
1372 } else if (this->map_type == SMT_OWNER) {
1373 if (click_pos < _smallmap_company_count) {
1374 this->SelectLegendItem(click_pos, _legend_land_owners, _smallmap_company_count, NUM_NO_COMPANY_ENTRIES);
1375 NotifyAllViewports(VPMT_OWNER);
1378 this->SetDirty();
1380 break;
1382 case WID_SM_ENABLE_ALL:
1383 case WID_SM_DISABLE_ALL: {
1384 LegendAndColour *tbl = NULL;
1385 switch (this->map_type) {
1386 case SMT_INDUSTRY:
1387 tbl = _legend_from_industries;
1388 this->BreakIndustryChainLink();
1389 NotifyAllViewports(VPMT_INDUSTRY);
1390 break;
1391 case SMT_OWNER:
1392 tbl = &(_legend_land_owners[NUM_NO_COMPANY_ENTRIES]);
1393 NotifyAllViewports(VPMT_OWNER);
1394 break;
1395 case SMT_LINKSTATS:
1396 tbl = _legend_linkstats;
1397 break;
1398 default:
1399 NOT_REACHED();
1401 for (;!tbl->end && tbl->legend != STR_LINKGRAPH_LEGEND_UNUSED; ++tbl) {
1402 tbl->show_on_map = (widget == WID_SM_ENABLE_ALL);
1404 if (this->map_type == SMT_LINKSTATS) this->SetOverlayCargoMask();
1405 this->SetDirty();
1406 break;
1409 case WID_SM_SHOW_HEIGHT: // Enable/disable showing of heightmap.
1410 _smallmap_show_heightmap = !_smallmap_show_heightmap;
1411 this->SetWidgetLoweredState(WID_SM_SHOW_HEIGHT, _smallmap_show_heightmap);
1412 NotifyAllViewports(VPMT_INDUSTRY);
1413 this->SetDirty();
1414 break;
1416 case WID_SM_SCREENSHOT:
1417 TakeScreenshot();
1418 break;
1423 * Some data on this window has become invalid.
1424 * @param data Information about the changed data.
1425 * - data = 0: Displayed industries at the industry chain window have changed.
1426 * - data = 1: Companies have changed.
1427 * - data = 2: Cheat changing the maximum heightlevel has been used, rebuild our heightlevel-to-colour index
1428 * @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.
1430 /* virtual */ void SmallMapWindow::OnInvalidateData(int data, bool gui_scope)
1432 if (!gui_scope) return;
1434 switch (data) {
1435 case 1:
1436 /* The owner legend has already been rebuilt. */
1437 this->ReInit();
1438 break;
1440 case 0: {
1441 extern std::bitset<NUM_INDUSTRYTYPES> _displayed_industries;
1442 if (this->map_type != SMT_INDUSTRY) this->SwitchMapType(SMT_INDUSTRY);
1444 for (int i = 0; i != _smallmap_industry_count; i++) {
1445 _legend_from_industries[i].show_on_map = _displayed_industries.test(_legend_from_industries[i].type);
1447 break;
1450 case 2:
1451 this->RebuildColourIndexIfNecessary();
1452 break;
1454 default: NOT_REACHED();
1456 this->SetDirty();
1459 /* virtual */ bool SmallMapWindow::OnRightClick(Point pt, int widget)
1461 if (widget != WID_SM_MAP || _scrolling_viewport) return false;
1463 _scrolling_viewport = this;
1464 return true;
1467 /* virtual */ void SmallMapWindow::OnMouseWheel(int wheel)
1469 if (_settings_client.gui.scrollwheel_scrolling == 0) {
1470 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1471 int cursor_x = _cursor.pos.x - this->left - wid->pos_x;
1472 int cursor_y = _cursor.pos.y - this->top - wid->pos_y;
1473 if (IsInsideMM(cursor_x, 0, wid->current_x) && IsInsideMM(cursor_y, 0, wid->current_y)) {
1474 Point pt = {cursor_x, cursor_y};
1475 this->SetZoomLevel((wheel < 0) ? ZLC_ZOOM_IN : ZLC_ZOOM_OUT, &pt);
1480 /* virtual */ void SmallMapWindow::OnTick()
1482 /* Update the window every now and then */
1483 if (--this->refresh != 0) return;
1485 if (this->map_type == SMT_LINKSTATS) {
1486 uint32 company_mask = this->GetOverlayCompanyMask();
1487 if (this->overlay->GetCompanyMask() != company_mask) {
1488 this->overlay->SetCompanyMask(company_mask);
1489 } else {
1490 this->overlay->RebuildCache();
1493 _smallmap_industry_highlight_state = !_smallmap_industry_highlight_state;
1495 this->refresh = _smallmap_industry_highlight != INVALID_INDUSTRYTYPE ? BLINK_PERIOD : FORCE_REFRESH_PERIOD;
1496 this->SetDirty();
1500 * Set new #scroll_x, #scroll_y, and #subscroll values after limiting them such that the center
1501 * of the smallmap always contains a part of the map.
1502 * @param sx Proposed new #scroll_x
1503 * @param sy Proposed new #scroll_y
1504 * @param sub Proposed new #subscroll
1506 void SmallMapWindow::SetNewScroll(int sx, int sy, int sub)
1508 const NWidgetBase *wi = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1509 Point hv = InverseRemapCoords(wi->current_x * ZOOM_LVL_BASE * TILE_SIZE / 2, wi->current_y * ZOOM_LVL_BASE * TILE_SIZE / 2);
1510 hv.x *= this->zoom;
1511 hv.y *= this->zoom;
1513 if (sx < -hv.x) {
1514 sx = -hv.x;
1515 sub = 0;
1517 if (sx > (int)(MapMaxX() * TILE_SIZE) - hv.x) {
1518 sx = MapMaxX() * TILE_SIZE - hv.x;
1519 sub = 0;
1521 if (sy < -hv.y) {
1522 sy = -hv.y;
1523 sub = 0;
1525 if (sy > (int)(MapMaxY() * TILE_SIZE) - hv.y) {
1526 sy = MapMaxY() * TILE_SIZE - hv.y;
1527 sub = 0;
1530 this->scroll_x = sx;
1531 this->scroll_y = sy;
1532 this->subscroll = sub;
1533 if (this->map_type == SMT_LINKSTATS) this->overlay->RebuildCache();
1536 /* static */ bool SmallMapWindow::MouseOverTileSearchProc(TileIndex tile, void *user_data)
1538 MouseOverTileSearchData *data = reinterpret_cast<MouseOverTileSearchData*>(user_data);
1540 TileType type = GetTileType(tile);
1541 switch (type) {
1542 /* industries - after stations */
1543 case MP_INDUSTRY:
1544 if (data->found_type != MP_INDUSTRY && data->found_type != MP_STATION) {
1545 /* check if the industry is visible */
1546 if (data->map_type != SMT_INDUSTRY ||
1547 _legend_from_industries[_industry_to_list_pos[Industry::GetByTile(tile)->type]].show_on_map) {
1548 break;
1551 return false;
1553 /* stations - most important if current smallmap marks out stations, otherwise not important */
1554 case MP_STATION:
1555 if (data->map_type == SMT_CONTOUR || data->map_type == SMT_LINKSTATS || data->map_type == SMT_ROUTES) break;
1556 return false;
1558 /* least important - towns */
1559 case MP_ROAD:
1560 case MP_TUNNELBRIDGE:
1561 case MP_HOUSE:
1562 if (data->found_type == MP_VOID && (type == MP_HOUSE || GetTileOwner(tile) == OWNER_TOWN)) break;
1563 return false;
1565 default:
1566 return false;
1569 data->found_type = type;
1570 return true;
1573 /* virtual */ void SmallMapWindow::OnToolTip(Point pt, int widget, TooltipCloseCondition close_cond)
1575 if (widget != WID_SM_MAP) {
1576 this->Window::OnToolTip(pt, widget, close_cond);
1577 return;
1580 TileIndex found_tile = INVALID_TILE;
1582 /* find out which tile area is being displayed at the point */
1583 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1584 int sub;
1585 pt = this->PixelToTile(pt.x - wid->pos_x, pt.y - wid->pos_y, &sub);
1586 pt.x += this->scroll_x / (int)TILE_SIZE;
1587 pt.y += this->scroll_y / (int)TILE_SIZE;
1588 if (IsInsideMM(pt.x, 0, MapSizeX()) && IsInsideMM(pt.y, 0, MapSizeY())) {
1589 TileArea ta(TileXY(pt.x, pt.y), this->zoom, this->zoom);
1590 ta.ClampToMap();
1591 /* search entire area, find best match */
1592 TileType best_type = (this->map_type == SMT_CONTOUR || this->map_type == SMT_LINKSTATS || this->map_type == SMT_ROUTES) ? MP_STATION : MP_INDUSTRY;
1593 SmallMapWindow::MouseOverTileSearchData search_data = { this->map_type, MP_VOID };
1594 TILE_AREA_LOOP(tile, ta) {
1595 if (MouseOverTileSearchProc(tile, &search_data)) {
1596 found_tile = tile;
1597 if (search_data.found_type == best_type) break;
1600 /* search also some surroundings, find first match */
1601 if (found_tile == INVALID_TILE) {
1602 found_tile = CircularTileSearch(TileX(ta.tile) - 1, TileY(ta.tile) - 1, 2 * this->zoom, ta.w, ta.h, SmallMapWindow::MouseOverTileSearchProc, &search_data);
1606 GuiShowTooltipsForTile(this, found_tile);
1609 /* virtual */ void SmallMapWindow::OnScroll(Point delta)
1611 _cursor.fix_at = true;
1613 /* While tile is at (delta.x, delta.y)? */
1614 int sub;
1615 Point pt = this->PixelToTile(delta.x, delta.y, &sub);
1616 this->SetNewScroll(this->scroll_x + pt.x * TILE_SIZE, this->scroll_y + pt.y * TILE_SIZE, sub);
1618 this->SetDirty();
1622 * Center the small map on the current center of the viewport.
1624 void SmallMapWindow::SmallMapCenterOnCurrentPos()
1626 const ViewPort *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
1627 Point viewport_center = TranslateXYToTileCoord(vp, vp->left + vp->width / 2, vp->top + vp->height / 2);
1629 int sub;
1630 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1631 Point sxy = this->ComputeScroll(viewport_center.x / (int)TILE_SIZE, viewport_center.y / (int)TILE_SIZE,
1632 max(0, (int)wid->current_x / 2 - 2), wid->current_y / 2, &sub);
1633 this->SetNewScroll(sxy.x, sxy.y, sub);
1634 this->SetDirty();
1638 * Get the center of the given station as point on the screen in the smallmap window.
1639 * @param st Station to find in the smallmap.
1640 * @return Point with coordinates of the station.
1642 Point SmallMapWindow::GetStationMiddle(const Station *st) const
1644 int x = (st->rect.right + st->rect.left + 1) / 2;
1645 int y = (st->rect.bottom + st->rect.top + 1) / 2;
1646 Point ret = this->RemapTile(x, y);
1648 /* Same magic 3 as in DrawVehicles; that's where I got it from.
1649 * No idea what it is, but without it the result looks bad.
1651 ret.x -= 3 + this->subscroll;
1652 return ret;
1656 * Take a screenshot of the contents of the smallmap window, at the current zoom level and mode
1657 * This calls MakeSmallMapScreenshot which uses ScreenshotCallbackHandler as the screenshot callback
1659 void SmallMapWindow::TakeScreenshot()
1661 int32 width = ((MapMaxX() + MapMaxY()) * 2) / this->zoom;
1662 int32 height = (MapMaxX() + MapMaxY() + 1) / this->zoom;
1664 int32 saved_scroll_x = this->scroll_x;
1665 int32 saved_scroll_y = this->scroll_y;
1666 int32 saved_subscroll = this->subscroll;
1667 this->subscroll = 0;
1668 MakeSmallMapScreenshot(width, height, this);
1669 this->scroll_x = saved_scroll_x;
1670 this->scroll_y = saved_scroll_y;
1671 this->subscroll = saved_subscroll;
1675 * Callback called by the screenshot code to draw a section of the smallmap to the output buffer
1676 * @param buf Videobuffer with same bitdepth as current blitter
1677 * @param y First line to render
1678 * @param pitch Pitch of the videobuffer
1679 * @param n Number of lines to render
1681 void SmallMapWindow::ScreenshotCallbackHandler(void *buf, uint y, uint pitch, uint n)
1683 DrawPixelInfo dpi, *old_dpi;
1685 /* We are no longer rendering to the screen */
1686 DrawPixelInfo old_screen = _screen;
1687 bool old_disable_anim = _screen_disable_anim;
1689 _screen.dst_ptr = buf;
1690 _screen.width = pitch;
1691 _screen.height = n;
1692 _screen.pitch = pitch;
1693 _screen_disable_anim = true;
1695 old_dpi = _cur_dpi;
1696 _cur_dpi = &dpi;
1698 dpi.dst_ptr = buf;
1699 dpi.height = n;
1700 dpi.width = ((MapMaxX() + MapMaxY()) * 2) / this->zoom;
1701 dpi.pitch = pitch;
1702 dpi.zoom = ZOOM_LVL_NORMAL;
1703 dpi.left = 0;
1704 dpi.top = y;
1706 int32 pos = (((int32)MapMaxX() + 1) * TILE_PIXELS) / 4;
1707 this->scroll_x = pos;
1708 this->scroll_y = -pos;
1710 /* make the screenshot */
1711 this->DrawSmallMap(&dpi, false);
1713 _cur_dpi = old_dpi;
1715 /* Switch back to rendering to the screen */
1716 _screen = old_screen;
1717 _screen_disable_anim = old_disable_anim;
1720 SmallMapWindow::SmallMapType SmallMapWindow::map_type = SMT_CONTOUR;
1721 bool SmallMapWindow::show_towns = true;
1722 int SmallMapWindow::max_heightlevel = -1;
1725 * Custom container class for displaying smallmap with a vertically resizing legend panel.
1726 * The legend panel has a smallest height that depends on its width. Standard containers cannot handle this case.
1728 * @note The container assumes it has two children, the first is the display, the second is the bar with legends and selection image buttons.
1729 * Both children should be both horizontally and vertically resizable and horizontally fillable.
1730 * The bar should have a minimal size with a zero-size legends display. Child padding is not supported.
1732 class NWidgetSmallmapDisplay : public NWidgetContainer {
1733 const SmallMapWindow *smallmap_window; ///< Window manager instance.
1734 public:
1735 NWidgetSmallmapDisplay() : NWidgetContainer(NWID_VERTICAL)
1737 this->smallmap_window = NULL;
1740 virtual void SetupSmallestSize(Window *w, bool init_array)
1742 NWidgetBase *display = this->head;
1743 NWidgetBase *bar = display->next;
1745 display->SetupSmallestSize(w, init_array);
1746 bar->SetupSmallestSize(w, init_array);
1748 this->smallmap_window = dynamic_cast<SmallMapWindow *>(w);
1749 assert(this->smallmap_window != NULL);
1750 this->smallest_x = max(display->smallest_x, bar->smallest_x + smallmap_window->GetMinLegendWidth());
1751 this->smallest_y = display->smallest_y + max(bar->smallest_y, smallmap_window->GetLegendHeight(smallmap_window->min_number_of_columns));
1752 this->fill_x = max(display->fill_x, bar->fill_x);
1753 this->fill_y = (display->fill_y == 0 && bar->fill_y == 0) ? 0 : min(display->fill_y, bar->fill_y);
1754 this->resize_x = max(display->resize_x, bar->resize_x);
1755 this->resize_y = min(display->resize_y, bar->resize_y);
1758 virtual void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
1760 this->pos_x = x;
1761 this->pos_y = y;
1762 this->current_x = given_width;
1763 this->current_y = given_height;
1765 NWidgetBase *display = this->head;
1766 NWidgetBase *bar = display->next;
1768 if (sizing == ST_SMALLEST) {
1769 this->smallest_x = given_width;
1770 this->smallest_y = given_height;
1771 /* Make display and bar exactly equal to their minimal size. */
1772 display->AssignSizePosition(ST_SMALLEST, x, y, display->smallest_x, display->smallest_y, rtl);
1773 bar->AssignSizePosition(ST_SMALLEST, x, y + display->smallest_y, bar->smallest_x, bar->smallest_y, rtl);
1776 uint bar_height = max(bar->smallest_y, this->smallmap_window->GetLegendHeight(this->smallmap_window->GetNumberColumnsLegend(given_width - bar->smallest_x)));
1777 uint display_height = given_height - bar_height;
1778 display->AssignSizePosition(ST_RESIZE, x, y, given_width, display_height, rtl);
1779 bar->AssignSizePosition(ST_RESIZE, x, y + display_height, given_width, bar_height, rtl);
1782 virtual NWidgetCore *GetWidgetFromPos(int x, int y)
1784 if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
1785 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1786 NWidgetCore *widget = child_wid->GetWidgetFromPos(x, y);
1787 if (widget != NULL) return widget;
1789 return NULL;
1792 virtual void Draw(const Window *w)
1794 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) child_wid->Draw(w);
1798 /** Widget parts of the smallmap display. */
1799 static const NWidgetPart _nested_smallmap_display[] = {
1800 NWidget(WWT_PANEL, COLOUR_BROWN, WID_SM_MAP_BORDER),
1801 NWidget(WWT_INSET, COLOUR_BROWN, WID_SM_MAP), SetMinimalSize(346, 140), SetResize(1, 1), SetPadding(2, 2, 2, 2), EndContainer(),
1802 EndContainer(),
1805 /** Widget parts of the smallmap legend bar + image buttons. */
1806 static const NWidgetPart _nested_smallmap_bar[] = {
1807 NWidget(WWT_PANEL, COLOUR_BROWN),
1808 NWidget(NWID_HORIZONTAL),
1809 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SM_LEGEND), SetResize(1, 1),
1810 NWidget(NWID_VERTICAL),
1811 /* Top button row. */
1812 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1813 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_SM_ZOOM_IN),
1814 SetDataTip(SPR_IMG_ZOOMIN, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_IN), SetFill(1, 1),
1815 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_SM_CENTERMAP),
1816 SetDataTip(SPR_IMG_SMALLMAP, STR_SMALLMAP_CENTER), SetFill(1, 1),
1817 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_BLANK),
1818 SetDataTip(SPR_DOT_SMALL, STR_NULL), SetFill(1, 1),
1819 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_CONTOUR),
1820 SetDataTip(SPR_IMG_SHOW_COUNTOURS, STR_SMALLMAP_TOOLTIP_SHOW_LAND_CONTOURS_ON_MAP), SetFill(1, 1),
1821 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_VEHICLES),
1822 SetDataTip(SPR_IMG_SHOW_VEHICLES, STR_SMALLMAP_TOOLTIP_SHOW_VEHICLES_ON_MAP), SetFill(1, 1),
1823 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_INDUSTRIES),
1824 SetDataTip(SPR_IMG_INDUSTRY, STR_SMALLMAP_TOOLTIP_SHOW_INDUSTRIES_ON_MAP), SetFill(1, 1),
1825 EndContainer(),
1826 /* Bottom button row. */
1827 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1828 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_SM_ZOOM_OUT),
1829 SetDataTip(SPR_IMG_ZOOMOUT, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_OUT), SetFill(1, 1),
1830 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_TOGGLETOWNNAME),
1831 SetDataTip(SPR_IMG_TOWN, STR_SMALLMAP_TOOLTIP_TOGGLE_TOWN_NAMES_ON_OFF), SetFill(1, 1),
1832 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_LINKSTATS),
1833 SetDataTip(SPR_IMG_CARGOFLOW, STR_SMALLMAP_TOOLTIP_SHOW_LINK_STATS_ON_MAP), SetFill(1, 1),
1834 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_ROUTES),
1835 SetDataTip(SPR_IMG_SHOW_ROUTES, STR_SMALLMAP_TOOLTIP_SHOW_TRANSPORT_ROUTES_ON), SetFill(1, 1),
1836 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_VEGETATION),
1837 SetDataTip(SPR_IMG_PLANTTREES, STR_SMALLMAP_TOOLTIP_SHOW_VEGETATION_ON_MAP), SetFill(1, 1),
1838 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_OWNERS),
1839 SetDataTip(SPR_IMG_COMPANY_GENERAL, STR_SMALLMAP_TOOLTIP_SHOW_LAND_OWNERS_ON_MAP), SetFill(1, 1),
1840 EndContainer(),
1841 NWidget(NWID_SPACER), SetResize(0, 1),
1842 EndContainer(),
1843 EndContainer(),
1844 EndContainer(),
1847 static NWidgetBase *SmallMapDisplay(int *biggest_index)
1849 NWidgetContainer *map_display = new NWidgetSmallmapDisplay;
1851 MakeNWidgets(_nested_smallmap_display, lengthof(_nested_smallmap_display), biggest_index, map_display);
1852 MakeNWidgets(_nested_smallmap_bar, lengthof(_nested_smallmap_bar), biggest_index, map_display);
1853 return map_display;
1857 static const NWidgetPart _nested_smallmap_widgets[] = {
1858 NWidget(NWID_HORIZONTAL),
1859 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
1860 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_SM_CAPTION), SetDataTip(STR_SMALLMAP_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1861 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
1862 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
1863 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
1864 EndContainer(),
1865 NWidgetFunction(SmallMapDisplay), // Smallmap display and legend bar + image buttons.
1866 /* Bottom button row and resize box. */
1867 NWidget(NWID_HORIZONTAL),
1868 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_SM_SCREENSHOT), SetDataTip(STR_SMALLMAP_SCREENSHOT, STR_NULL),
1869 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_SM_SELECT_BUTTONS),
1870 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1871 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_SM_ENABLE_ALL), SetDataTip(STR_SMALLMAP_ENABLE_ALL, STR_NULL),
1872 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_SM_DISABLE_ALL), SetDataTip(STR_SMALLMAP_DISABLE_ALL, STR_NULL),
1873 NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_SM_SHOW_HEIGHT), SetDataTip(STR_SMALLMAP_SHOW_HEIGHT, STR_SMALLMAP_TOOLTIP_SHOW_HEIGHT),
1874 NWidget(WWT_PANEL, COLOUR_BROWN), SetFill(1, 0), SetResize(1, 0),
1875 EndContainer(),
1876 EndContainer(),
1877 NWidget(WWT_PANEL, COLOUR_BROWN), SetFill(1, 0), SetResize(1, 0),
1878 EndContainer(),
1879 EndContainer(),
1880 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
1881 EndContainer(),
1884 static WindowDesc _smallmap_desc(
1885 WDP_AUTO, "smallmap", 484, 314,
1886 WC_SMALLMAP, WC_NONE,
1888 _nested_smallmap_widgets, lengthof(_nested_smallmap_widgets)
1892 * Show the smallmap window.
1894 void ShowSmallMap()
1896 AllocateWindowDescFront<SmallMapWindow>(&_smallmap_desc, 0);
1900 * Scrolls the main window to given coordinates.
1901 * @param x x coordinate
1902 * @param y y coordinate
1903 * @param z z coordinate; -1 to scroll to terrain height
1904 * @param instant scroll instantly (meaningful only when smooth_scrolling is active)
1905 * @return did the viewport position change?
1907 bool ScrollMainWindowTo(int x, int y, int z, bool instant)
1909 bool res = ScrollWindowTo(x, y, z, FindWindowById(WC_MAIN_WINDOW, 0), instant);
1911 /* If a user scrolls to a tile (via what way what so ever) and already is on
1912 * that tile (e.g.: pressed twice), move the smallmap to that location,
1913 * so you directly see where you are on the smallmap. */
1915 if (res) return res;
1917 SmallMapWindow *w = dynamic_cast<SmallMapWindow*>(FindWindowById(WC_SMALLMAP, 0));
1918 if (w != NULL) w->SmallMapCenterOnCurrentPos();
1920 return res;