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/>.
10 /** @file smallmap_gui.cpp GUI that shows a small map of the world with metadata like owner or height. */
13 #include "clear_map.h"
15 #include "station_map.h"
16 #include "landscape.h"
18 #include "viewport_func.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"
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}
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. */
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
),
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
),
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
),
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. */
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()
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
;
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
));
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
,
232 _legend_from_industries
,
239 /** Available colour schemes for height maps. */
240 SmallMapColourScheme _heightmap_schemes
[] = {
241 {nullptr, _green_map_heights
, lengthof(_green_map_heights
), MKCOLOUR_XXXX(0x5B)}, ///< Green colour scheme.
242 {nullptr, _dark_green_map_heights
, lengthof(_dark_green_map_heights
), MKCOLOUR_XXXX(0x62)}, ///< Dark green colour scheme.
243 {nullptr, _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
== nullptr) 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 }};
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);
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
];
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
;
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
;
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
;
379 } else if (t
== MP_ROAD
) {
380 RoadTypeIdentifiers rtids
= RoadTypeIdentifiers::FromTile(tile
);
381 const RoadtypeInfo
*rti
= NULL
;
382 if (rtids
.HasRoad()) {
383 rti
= GetRoadTypeInfo(rtids
.road_identifier
);
384 } else if (rtids
.HasTram()) {
385 rti
= GetRoadTypeInfo(rtids
.tram_identifier
);
389 MKCOLOUR_0XX0(rti
->map_colour
),
390 _smallmap_contours_andor
[t
].mand
393 const SmallMapColourScheme
*cs
= &_heightmap_schemes
[_settings_client
.gui
.smallmap_land_colour
];
394 return ApplyMask(cs
->default_colour
, &andor
);
399 const SmallMapColourScheme
*cs
= &_heightmap_schemes
[_settings_client
.gui
.smallmap_land_colour
];
400 return ApplyMask(cs
->default_colour
, &_smallmap_contours_andor
[t
]);
404 * Return the colour a tile would be displayed with in the small map in mode "link stats".
406 * @param tile The tile of which we would like to get the colour.
407 * @param t Effective tile type of the tile (see #GetTileColours).
408 * @return The colour of tile in the small map in mode "link stats"
410 static inline uint32
GetSmallMapLinkStatsPixels(TileIndex tile
, TileType t
)
412 return _smallmap_show_heightmap
? GetSmallMapContoursPixels(tile
, t
) : GetSmallMapRoutesPixels(tile
, t
);
416 * Return the colour a tile would be displayed with in the smallmap in mode "Vegetation".
418 * @param tile The tile of which we would like to get the colour.
419 * @param t Effective tile type of the tile (see #GetTileColours).
420 * @return The colour of tile in the smallmap in mode "Vegetation"
422 static inline uint32
GetSmallMapVegetationPixels(TileIndex tile
, TileType t
)
426 return (IsClearGround(tile
, CLEAR_GRASS
) && GetClearDensity(tile
) < 3) ? MKCOLOUR_XXXX(PC_BARE_LAND
) : _vegetation_clear_bits
[GetClearGround(tile
)];
429 return IsTileForestIndustry(tile
) ? MKCOLOUR_XXXX(PC_GREEN
) : MKCOLOUR_XXXX(PC_DARK_RED
);
432 if (GetTreeGround(tile
) == TREE_GROUND_SNOW_DESERT
|| GetTreeGround(tile
) == TREE_GROUND_ROUGH_SNOW
) {
433 return (_settings_game
.game_creation
.landscape
== LT_ARCTIC
) ? MKCOLOUR_XYYX(PC_LIGHT_BLUE
, PC_TREES
) : MKCOLOUR_XYYX(PC_ORANGE
, PC_TREES
);
435 return MKCOLOUR_XYYX(PC_GRASS_LAND
, PC_TREES
);
438 return ApplyMask(MKCOLOUR_XXXX(PC_GRASS_LAND
), &_smallmap_vehicles_andor
[t
]);
443 * Return the colour a tile would be displayed with in the small map in mode "Owner".
445 * @param tile The tile of which we would like to get the colour.
446 * @param t Effective tile type of the tile (see #GetTileColours).
447 * @return The colour of tile in the small map in mode "Owner"
449 static inline uint32
GetSmallMapOwnerPixels(TileIndex tile
, TileType t
)
454 case MP_INDUSTRY
: return MKCOLOUR_XXXX(PC_DARK_GREY
);
455 case MP_HOUSE
: return MKCOLOUR_XXXX(PC_DARK_RED
);
456 default: o
= GetTileOwner(tile
); break;
457 /* FIXME: For MP_ROAD there are multiple owners.
458 * GetTileOwner returns the rail owner (level crossing) resp. the owner of ROADTYPE_ROAD (normal road),
459 * even if there are no ROADTYPE_ROAD bits on the tile.
463 if ((o
< MAX_COMPANIES
&& !_legend_land_owners
[_company_to_list_pos
[o
]].show_on_map
) || o
== OWNER_NONE
|| o
== OWNER_WATER
) {
464 if (t
== MP_WATER
) return MKCOLOUR_XXXX(PC_WATER
);
465 const SmallMapColourScheme
*cs
= &_heightmap_schemes
[_settings_client
.gui
.smallmap_land_colour
];
466 return _smallmap_show_heightmap
? cs
->height_colours
[TileHeight(tile
)] : cs
->default_colour
;
467 } else if (o
== OWNER_TOWN
) {
468 return MKCOLOUR_XXXX(PC_DARK_RED
);
471 return MKCOLOUR_XXXX(_legend_land_owners
[_company_to_list_pos
[o
]].colour
);
474 static void NotifyAllViewports(ViewportMapType map_type
)
477 FOR_ALL_WINDOWS_FROM_BACK(w
) {
478 if (w
->viewport
!= nullptr)
479 if (w
->viewport
->zoom
>= ZOOM_LVL_DRAW_MAP
&& w
->viewport
->map_type
== map_type
)
484 /** Vehicle colours in #SMT_VEHICLES mode. Indexed by #VehicleTypeByte. */
485 static const byte _vehicle_type_colours
[6] = {
486 PC_RED
, PC_YELLOW
, PC_LIGHT_BLUE
, PC_WHITE
, PC_BLACK
, PC_RED
490 /** Notify the industry chain window to stop sending newly selected industries. */
491 /* static */ void SmallMapWindow::BreakIndustryChainLink()
493 InvalidateWindowClassesData(WC_INDUSTRY_CARGOES
, NUM_INDUSTRYTYPES
);
496 inline Point
SmallMapWindow::SmallmapRemapCoords(int x
, int y
) const
505 * Remap tile to location on this smallmap.
506 * @param tile_x X coordinate of the tile.
507 * @param tile_y Y coordinate of the tile.
508 * @return Position to draw on.
510 inline Point
SmallMapWindow::RemapTile(int tile_x
, int tile_y
) const
512 int x_offset
= tile_x
- this->scroll_x
/ (int)TILE_SIZE
;
513 int y_offset
= tile_y
- this->scroll_y
/ (int)TILE_SIZE
;
515 if (this->zoom
== 1) return SmallmapRemapCoords(x_offset
, y_offset
);
517 /* For negative offsets, round towards -inf. */
518 if (x_offset
< 0) x_offset
-= this->zoom
- 1;
519 if (y_offset
< 0) y_offset
-= this->zoom
- 1;
521 return SmallmapRemapCoords(x_offset
/ this->zoom
, y_offset
/ this->zoom
);
525 * Determine the tile relative to the base tile of the smallmap, and the pixel position at
526 * that tile for a point in the smallmap.
527 * @param px Horizontal coordinate of the pixel.
528 * @param py Vertical coordinate of the pixel.
529 * @param sub[out] Pixel position at the tile (0..3).
530 * @param add_sub Add current #subscroll to the position.
531 * @return Tile being displayed at the given position relative to #scroll_x and #scroll_y.
532 * @note The #subscroll offset is already accounted for.
534 inline Point
SmallMapWindow::PixelToTile(int px
, int py
, int *sub
, bool add_sub
) const
536 if (add_sub
) px
+= this->subscroll
; // Total horizontal offset.
538 /* For each two rows down, add a x and a y tile, and
539 * For each four pixels to the right, move a tile to the right. */
540 Point pt
= {((py
>> 1) - (px
>> 2)) * this->zoom
, ((py
>> 1) + (px
>> 2)) * this->zoom
};
543 if (py
& 1) { // Odd number of rows, handle the 2 pixel shift.
558 * Compute base parameters of the smallmap such that tile (\a tx, \a ty) starts at pixel (\a x, \a y).
559 * @param tx Tile x coordinate.
560 * @param ty Tile y coordinate.
561 * @param x Non-negative horizontal position in the display where the tile starts.
562 * @param y Non-negative vertical position in the display where the tile starts.
563 * @param sub [out] Value of #subscroll needed.
564 * @return #scroll_x, #scroll_y values.
566 Point
SmallMapWindow::ComputeScroll(int tx
, int ty
, int x
, int y
, int *sub
)
568 assert(x
>= 0 && y
>= 0);
571 Point tile_xy
= PixelToTile(x
, y
, &new_sub
, false);
578 scroll
.x
= (tx
+ this->zoom
) * TILE_SIZE
;
579 scroll
.y
= (ty
- this->zoom
) * TILE_SIZE
;
582 scroll
.x
= (tx
+ 2 * this->zoom
) * TILE_SIZE
;
583 scroll
.y
= (ty
- 2 * this->zoom
) * TILE_SIZE
;
589 * Initialize or change the zoom level.
590 * @param change Way to change the zoom level.
591 * @param zoom_pt Position to keep fixed while zooming.
592 * @pre \c *zoom_pt should contain a point in the smallmap display when zooming in or out.
594 void SmallMapWindow::SetZoomLevel(ZoomLevelChange change
, const Point
*zoom_pt
)
596 static const int zoomlevels
[] = {1, 2, 4, 6, 8}; // Available zoom levels. Bigger number means more zoom-out (further away).
597 static const int MIN_ZOOM_INDEX
= 0;
598 static const int MAX_ZOOM_INDEX
= lengthof(zoomlevels
) - 1;
600 int new_index
, cur_index
, sub
;
604 cur_index
= - 1; // Definitely different from new_index.
605 new_index
= MIN_ZOOM_INDEX
;
611 for (cur_index
= MIN_ZOOM_INDEX
; cur_index
<= MAX_ZOOM_INDEX
; cur_index
++) {
612 if (this->zoom
== zoomlevels
[cur_index
]) break;
614 assert(cur_index
<= MAX_ZOOM_INDEX
);
616 tile
= this->PixelToTile(zoom_pt
->x
, zoom_pt
->y
, &sub
);
617 new_index
= Clamp(cur_index
+ ((change
== ZLC_ZOOM_IN
) ? -1 : 1), MIN_ZOOM_INDEX
, MAX_ZOOM_INDEX
);
620 default: NOT_REACHED();
623 if (new_index
!= cur_index
) {
624 this->zoom
= zoomlevels
[new_index
];
625 if (cur_index
>= 0) {
626 Point new_tile
= this->PixelToTile(zoom_pt
->x
, zoom_pt
->y
, &sub
);
627 this->SetNewScroll(this->scroll_x
+ (tile
.x
- new_tile
.x
) * TILE_SIZE
,
628 this->scroll_y
+ (tile
.y
- new_tile
.y
) * TILE_SIZE
, sub
);
629 } else if (this->map_type
== SMT_LINKSTATS
) {
630 this->overlay
->RebuildCache();
632 this->SetWidgetDisabledState(WID_SM_ZOOM_IN
, this->zoom
== zoomlevels
[MIN_ZOOM_INDEX
]);
633 this->SetWidgetDisabledState(WID_SM_ZOOM_OUT
, this->zoom
== zoomlevels
[MAX_ZOOM_INDEX
]);
639 * Decide which colours to show to the user for a group of tiles.
640 * @param ta Tile area to investigate.
641 * @return Colours to display.
643 inline uint32
SmallMapWindow::GetTileColours(const TileArea
&ta
) const
646 TileIndex tile
= INVALID_TILE
; // Position of the most important tile.
647 TileType et
= MP_VOID
; // Effective tile type at that position.
649 TILE_AREA_LOOP(ti
, ta
) {
650 TileType ttype
= GetTileType(ti
);
653 case MP_TUNNELBRIDGE
: {
654 TransportType tt
= GetTunnelBridgeTransportType(ti
);
657 case TRANSPORT_RAIL
: ttype
= MP_RAILWAY
; break;
658 case TRANSPORT_ROAD
: ttype
= MP_ROAD
; break;
659 default: ttype
= MP_WATER
; break;
665 /* Special handling of industries while in "Industries" smallmap view. */
666 if (this->map_type
== SMT_INDUSTRY
) {
667 /* If industry is allowed to be seen, use its colour on the map.
668 * This has the highest priority above any value in _tiletype_importance. */
669 IndustryType type
= Industry::GetByTile(ti
)->type
;
670 if (_legend_from_industries
[_industry_to_list_pos
[type
]].show_on_map
) {
671 if (type
== _smallmap_industry_highlight
) {
672 if (_smallmap_industry_highlight_state
) return MKCOLOUR_XXXX(PC_WHITE
);
674 return GetIndustrySpec(type
)->map_colour
* 0x01010101;
677 /* Otherwise make it disappear */
678 ttype
= IsTileOnWater(ti
) ? MP_WATER
: MP_CLEAR
;
686 if (_tiletype_importance
[ttype
] > importance
) {
687 importance
= _tiletype_importance
[ttype
];
693 switch (this->map_type
) {
695 return GetSmallMapContoursPixels(tile
, et
);
698 return GetSmallMapVehiclesPixels(tile
, et
);
701 return GetSmallMapIndustriesPixels(tile
, et
);
704 return GetSmallMapLinkStatsPixels(tile
, et
);
707 return GetSmallMapRoutesPixels(tile
, et
);
710 return GetSmallMapVegetationPixels(tile
, et
);
713 return GetSmallMapOwnerPixels(tile
, et
);
715 default: NOT_REACHED();
720 * Draws one column of tiles of the small map in a certain mode onto the screen buffer, skipping the shifted rows in between.
722 * @param dst Pointer to a part of the screen buffer to write to.
723 * @param xc The X coordinate of the first tile in the column.
724 * @param yc The Y coordinate of the first tile in the column
725 * @param pitch Number of pixels to advance in the screen buffer each time a pixel is written.
726 * @param reps Number of lines to draw
727 * @param start_pos Position of first pixel to draw.
728 * @param end_pos Position of last pixel to draw (exclusive).
729 * @param blitter current blitter
730 * @note If pixel position is below \c 0, skip drawing.
732 void SmallMapWindow::DrawSmallMapColumn(void *dst
, uint xc
, uint yc
, int pitch
, int reps
, int start_pos
, int end_pos
, Blitter
*blitter
) const
734 void *dst_ptr_abs_end
= blitter
->MoveTo(_screen
.dst_ptr
, 0, _screen
.height
);
735 uint min_xy
= _settings_game
.construction
.freeform_edges
? 1 : 0;
738 /* Check if the tile (xc,yc) is within the map range */
739 if (xc
>= MapMaxX() || yc
>= MapMaxY()) continue;
741 /* Check if the dst pointer points to a pixel inside the screen buffer */
742 if (dst
< _screen
.dst_ptr
) continue;
743 if (dst
>= dst_ptr_abs_end
) continue;
745 /* Construct tilearea covered by (xc, yc, xc + this->zoom, yc + this->zoom) such that it is within min_xy limits. */
747 if (min_xy
== 1 && (xc
== 0 || yc
== 0)) {
748 if (this->zoom
== 1) continue; // The tile area is empty, don't draw anything.
750 ta
= TileArea(TileXY(max(min_xy
, xc
), max(min_xy
, yc
)), this->zoom
- (xc
== 0), this->zoom
- (yc
== 0));
752 ta
= TileArea(TileXY(xc
, yc
), this->zoom
, this->zoom
);
754 ta
.ClampToMap(); // Clamp to map boundaries (may contain MP_VOID tiles!).
756 uint32 val
= this->GetTileColours(ta
);
757 uint8
*val8
= (uint8
*)&val
;
758 int idx
= max(0, -start_pos
);
759 for (int pos
= max(0, start_pos
); pos
< end_pos
; pos
++) {
760 blitter
->SetPixel(dst
, idx
, 0, val8
[idx
]);
763 /* Switch to next tile in the column */
764 } while (xc
+= this->zoom
, yc
+= this->zoom
, dst
= blitter
->MoveTo(dst
, pitch
, 0), --reps
!= 0);
768 * Adds vehicles to the smallmap.
769 * @param dpi the part of the smallmap to be drawn into
770 * @param blitter current blitter
772 void SmallMapWindow::DrawVehicles(const DrawPixelInfo
*dpi
, Blitter
*blitter
) const
775 FOR_ALL_VEHICLES(v
) {
776 if (v
->type
== VEH_EFFECT
) continue;
777 if (v
->vehstatus
& (VS_HIDDEN
| VS_UNCLICKABLE
)) continue;
779 /* Remap into flat coordinates. */
780 Point pt
= this->RemapTile(v
->x_pos
/ (int)TILE_SIZE
, v
->y_pos
/ (int)TILE_SIZE
);
782 int y
= pt
.y
- dpi
->top
;
783 if (!IsInsideMM(y
, 0, dpi
->height
)) continue; // y is out of bounds.
785 bool skip
= false; // Default is to draw both pixels.
786 int x
= pt
.x
- this->subscroll
- 3 - dpi
->left
; // Offset X coordinate.
788 /* if x+1 is 0, that means we're on the very left edge,
789 * and should thus only draw a single pixel */
790 if (++x
!= 0) continue;
792 } else if (x
>= dpi
->width
- 1) {
793 /* Check if we're at the very right edge, and if so draw only a single pixel */
794 if (x
!= dpi
->width
- 1) continue;
798 /* Calculate pointer to pixel and the colour */
799 byte colour
= (this->map_type
== SMT_VEHICLES
) ? _vehicle_type_colours
[v
->type
] : PC_WHITE
;
801 /* And draw either one or two pixels depending on clipping */
802 blitter
->SetPixel(dpi
->dst_ptr
, x
, y
, colour
);
803 if (!skip
) blitter
->SetPixel(dpi
->dst_ptr
, x
+ 1, y
, colour
);
808 * Adds town names to the smallmap.
809 * @param dpi the part of the smallmap to be drawn into
811 void SmallMapWindow::DrawTowns(const DrawPixelInfo
*dpi
) const
815 /* Remap the town coordinate */
816 Point pt
= this->RemapTile(TileX(t
->xy
), TileY(t
->xy
));
817 int x
= pt
.x
- this->subscroll
- (t
->cache
.sign
.width_small
>> 1);
820 /* Check if the town sign is within bounds */
821 if (x
+ t
->cache
.sign
.width_small
> dpi
->left
&&
822 x
< dpi
->left
+ dpi
->width
&&
823 y
+ FONT_HEIGHT_SMALL
> dpi
->top
&&
824 y
< dpi
->top
+ dpi
->height
) {
826 SetDParam(0, t
->index
);
827 DrawString(x
, x
+ t
->cache
.sign
.width_small
, y
, STR_SMALLMAP_TOWN
);
833 * Adds map indicators to the smallmap.
835 void SmallMapWindow::DrawMapIndicators() const
837 /* Find main viewport. */
838 const ViewPort
*vp
= FindWindowById(WC_MAIN_WINDOW
, 0)->viewport
;
840 Point upper_left_smallmap_coord
= TranslateXYToTileCoord(vp
, vp
->left
, vp
->top
, false);
841 Point lower_right_smallmap_coord
= TranslateXYToTileCoord(vp
, vp
->left
+ vp
->width
- 1, vp
->top
+ vp
->height
- 1, false);
843 Point upper_left
= this->RemapTile(upper_left_smallmap_coord
.x
/ (int)TILE_SIZE
, upper_left_smallmap_coord
.y
/ (int)TILE_SIZE
);
844 upper_left
.x
-= this->subscroll
;
846 Point lower_right
= this->RemapTile(lower_right_smallmap_coord
.x
/ (int)TILE_SIZE
, lower_right_smallmap_coord
.y
/ (int)TILE_SIZE
);
847 lower_right
.x
-= this->subscroll
;
849 SmallMapWindow::DrawVertMapIndicator(upper_left
.x
, upper_left
.y
, lower_right
.y
);
850 SmallMapWindow::DrawVertMapIndicator(lower_right
.x
, upper_left
.y
, lower_right
.y
);
852 SmallMapWindow::DrawHorizMapIndicator(upper_left
.x
, lower_right
.x
, upper_left
.y
);
853 SmallMapWindow::DrawHorizMapIndicator(upper_left
.x
, lower_right
.x
, lower_right
.y
);
857 * Draws the small map.
859 * Basically, the small map is draw column of pixels by column of pixels. The pixels
860 * are drawn directly into the screen buffer. The final map is drawn in multiple passes.
862 * <ol><li>The colours of tiles in the different modes.</li>
863 * <li>Town names (optional)</li></ol>
865 * @param dpi pointer to pixel to write onto
867 void SmallMapWindow::DrawSmallMap(DrawPixelInfo
*dpi
, bool draw_indicators
) const
869 Blitter
*blitter
= BlitterFactory::GetCurrentBlitter();
870 DrawPixelInfo
*old_dpi
;
876 GfxFillRect(dpi
->left
, dpi
->top
, dpi
->left
+ dpi
->width
- 1, dpi
->top
+ dpi
->height
- 1, PC_BLACK
);
878 /* Which tile is displayed at (dpi->left, dpi->top)? */
880 Point tile
= this->PixelToTile(dpi
->left
, dpi
->top
, &dx
);
881 int tile_x
= this->scroll_x
/ (int)TILE_SIZE
+ tile
.x
;
882 int tile_y
= this->scroll_y
/ (int)TILE_SIZE
+ tile
.y
;
884 void *ptr
= blitter
->MoveTo(dpi
->dst_ptr
, -dx
- 4, 0);
889 /* Distance from left edge */
891 if (x
>= dpi
->width
) break; // Exit the loop.
893 int end_pos
= min(dpi
->width
, x
+ 4);
894 int reps
= (dpi
->height
- y
+ 1) / 2; // Number of lines.
896 this->DrawSmallMapColumn(ptr
, tile_x
, tile_y
, dpi
->pitch
* 2, reps
, x
, end_pos
, blitter
);
901 tile_y
+= this->zoom
;
903 ptr
= blitter
->MoveTo(ptr
, 0, 1);
905 tile_x
-= this->zoom
;
907 ptr
= blitter
->MoveTo(ptr
, 0, -1);
909 ptr
= blitter
->MoveTo(ptr
, 2, 0);
914 if (this->map_type
== SMT_CONTOUR
|| this->map_type
== SMT_VEHICLES
) this->DrawVehicles(dpi
, blitter
);
916 /* Draw link stat overlay */
917 if (this->map_type
== SMT_LINKSTATS
) this->overlay
->Draw(dpi
);
919 /* Draw town names */
920 if (this->show_towns
) this->DrawTowns(dpi
);
922 /* Draw map indicators */
923 if (draw_indicators
) this->DrawMapIndicators();
929 * Function to set up widgets depending on the information being shown on the smallmap.
931 void SmallMapWindow::SetupWidgetData()
933 StringID legend_tooltip
;
934 StringID enable_all_tooltip
;
935 StringID disable_all_tooltip
;
937 switch (this->map_type
) {
939 legend_tooltip
= STR_SMALLMAP_TOOLTIP_INDUSTRY_SELECTION
;
940 enable_all_tooltip
= STR_SMALLMAP_TOOLTIP_ENABLE_ALL_INDUSTRIES
;
941 disable_all_tooltip
= STR_SMALLMAP_TOOLTIP_DISABLE_ALL_INDUSTRIES
;
946 legend_tooltip
= STR_SMALLMAP_TOOLTIP_COMPANY_SELECTION
;
947 enable_all_tooltip
= STR_SMALLMAP_TOOLTIP_ENABLE_ALL_COMPANIES
;
948 disable_all_tooltip
= STR_SMALLMAP_TOOLTIP_DISABLE_ALL_COMPANIES
;
953 legend_tooltip
= STR_SMALLMAP_TOOLTIP_CARGO_SELECTION
;
954 enable_all_tooltip
= STR_SMALLMAP_TOOLTIP_ENABLE_ALL_CARGOS
;
955 disable_all_tooltip
= STR_SMALLMAP_TOOLTIP_DISABLE_ALL_CARGOS
;
960 legend_tooltip
= STR_NULL
;
961 enable_all_tooltip
= STR_NULL
;
962 disable_all_tooltip
= STR_NULL
;
967 this->GetWidget
<NWidgetCore
>(WID_SM_LEGEND
)->SetDataTip(STR_NULL
, legend_tooltip
);
968 this->GetWidget
<NWidgetCore
>(WID_SM_ENABLE_ALL
)->SetDataTip(STR_SMALLMAP_ENABLE_ALL
, enable_all_tooltip
);
969 this->GetWidget
<NWidgetCore
>(WID_SM_DISABLE_ALL
)->SetDataTip(STR_SMALLMAP_DISABLE_ALL
, disable_all_tooltip
);
970 this->GetWidget
<NWidgetStacked
>(WID_SM_SELECT_BUTTONS
)->SetDisplayedPlane(plane
);
973 SmallMapWindow::SmallMapWindow(WindowDesc
*desc
, int window_number
) : Window(desc
), refresh(FORCE_REFRESH_PERIOD
)
975 _smallmap_industry_highlight
= INVALID_INDUSTRYTYPE
;
976 this->overlay
= new LinkGraphOverlay(this, WID_SM_MAP
, 0, this->GetOverlayCompanyMask(), 1);
977 this->InitNested(window_number
);
978 this->LowerWidget(this->map_type
+ WID_SM_CONTOUR
);
980 this->RebuildColourIndexIfNecessary();
982 this->SetWidgetLoweredState(WID_SM_SHOW_HEIGHT
, _smallmap_show_heightmap
);
984 this->SetWidgetLoweredState(WID_SM_TOGGLETOWNNAME
, this->show_towns
);
986 this->SetupWidgetData();
988 this->SetZoomLevel(ZLC_INITIALIZE
, nullptr);
989 this->SmallMapCenterOnCurrentPos();
990 this->SetOverlayCargoMask();
993 SmallMapWindow::~SmallMapWindow()
995 delete this->overlay
;
996 this->BreakIndustryChainLink();
1000 * Rebuilds the colour indices used for fast access to the smallmap contour colours based on the heightlevel.
1002 void SmallMapWindow::RebuildColourIndexIfNecessary()
1004 /* Rebuild colour indices if necessary. */
1005 if (SmallMapWindow::max_heightlevel
== _settings_game
.construction
.max_heightlevel
) return;
1007 for (uint n
= 0; n
< lengthof(_heightmap_schemes
); n
++) {
1008 /* The heights go from 0 up to and including maximum. */
1009 int heights
= _settings_game
.construction
.max_heightlevel
+ 1;
1010 _heightmap_schemes
[n
].height_colours
= ReallocT
<uint32
>(_heightmap_schemes
[n
].height_colours
, heights
);
1012 for (int z
= 0; z
< heights
; z
++) {
1013 size_t access_index
= (_heightmap_schemes
[n
].colour_count
* z
) / heights
;
1015 /* Choose colour by mapping the range (0..max heightlevel) on the complete colour table. */
1016 _heightmap_schemes
[n
].height_colours
[z
] = _heightmap_schemes
[n
].height_colours_base
[access_index
];
1020 SmallMapWindow::max_heightlevel
= _settings_game
.construction
.max_heightlevel
;
1024 /* virtual */ void SmallMapWindow::SetStringParameters(int widget
) const
1027 case WID_SM_CAPTION
:
1028 SetDParam(0, STR_SMALLMAP_TYPE_CONTOURS
+ this->map_type
);
1033 /* virtual */ void SmallMapWindow::OnInit()
1036 this->min_number_of_columns
= INDUSTRY_MIN_NUMBER_OF_COLUMNS
;
1037 this->min_number_of_fixed_rows
= lengthof(_linkstat_colours_in_legenda
);
1038 for (uint i
= 0; i
< lengthof(_legend_table
); i
++) {
1040 uint num_columns
= 1;
1041 for (const LegendAndColour
*tbl
= _legend_table
[i
]; !tbl
->end
; ++tbl
) {
1043 if (i
== SMT_INDUSTRY
) {
1044 SetDParam(0, tbl
->legend
);
1045 SetDParam(1, IndustryPool::MAX_SIZE
);
1046 str
= STR_SMALLMAP_INDUSTRY
;
1047 } else if (i
== SMT_LINKSTATS
) {
1048 SetDParam(0, tbl
->legend
);
1049 str
= STR_SMALLMAP_LINKSTATS
;
1050 } else if (i
== SMT_OWNER
) {
1051 if (tbl
->company
!= INVALID_COMPANY
) {
1052 if (!Company::IsValidID(tbl
->company
)) {
1053 /* Rebuild the owner legend. */
1058 /* Non-fixed legend entries for the owner view. */
1059 SetDParam(0, tbl
->company
);
1060 str
= STR_SMALLMAP_COMPANY
;
1065 if (tbl
->col_break
) {
1066 this->min_number_of_fixed_rows
= max(this->min_number_of_fixed_rows
, height
);
1073 min_width
= max(GetStringBoundingBox(str
).width
, min_width
);
1075 this->min_number_of_fixed_rows
= max(this->min_number_of_fixed_rows
, height
);
1076 this->min_number_of_columns
= max(this->min_number_of_columns
, num_columns
);
1079 /* The width of a column is the minimum width of all texts + the size of the blob + some spacing */
1080 this->column_width
= min_width
+ LEGEND_BLOB_WIDTH
+ WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
1083 /* virtual */ void SmallMapWindow::OnPaint()
1085 if (this->map_type
== SMT_OWNER
) {
1086 for (const LegendAndColour
*tbl
= _legend_table
[this->map_type
]; !tbl
->end
; ++tbl
) {
1087 if (tbl
->company
!= INVALID_COMPANY
&& !Company::IsValidID(tbl
->company
)) {
1088 /* Rebuild the owner legend. */
1090 this->InvalidateData(1);
1096 this->DrawWidgets();
1099 /* virtual */ void SmallMapWindow::DrawWidget(const Rect
&r
, int widget
) const
1103 DrawPixelInfo new_dpi
;
1104 if (!FillDrawPixelInfo(&new_dpi
, r
.left
+ 1, r
.top
+ 1, r
.right
- r
.left
- 1, r
.bottom
- r
.top
- 1)) return;
1105 this->DrawSmallMap(&new_dpi
);
1109 case WID_SM_LEGEND
: {
1110 uint columns
= this->GetNumberColumnsLegend(r
.right
- r
.left
+ 1);
1111 uint number_of_rows
= this->GetNumberRowsLegend(columns
);
1112 bool rtl
= _current_text_dir
== TD_RTL
;
1113 uint y_org
= r
.top
+ WD_FRAMERECT_TOP
;
1114 uint x
= rtl
? r
.right
- this->column_width
- WD_FRAMERECT_RIGHT
: r
.left
+ WD_FRAMERECT_LEFT
;
1116 uint i
= 0; // Row counter for industry legend.
1117 uint row_height
= FONT_HEIGHT_SMALL
;
1119 uint text_left
= rtl
? 0 : LEGEND_BLOB_WIDTH
+ WD_FRAMERECT_LEFT
;
1120 uint text_right
= this->column_width
- 1 - (rtl
? LEGEND_BLOB_WIDTH
+ WD_FRAMERECT_RIGHT
: 0);
1121 uint blob_left
= rtl
? this->column_width
- 1 - LEGEND_BLOB_WIDTH
: 0;
1122 uint blob_right
= rtl
? this->column_width
- 1 : LEGEND_BLOB_WIDTH
;
1124 StringID string
= STR_NULL
;
1125 switch (this->map_type
) {
1127 string
= STR_SMALLMAP_INDUSTRY
;
1130 string
= STR_SMALLMAP_LINKSTATS
;
1133 string
= STR_SMALLMAP_COMPANY
;
1139 for (const LegendAndColour
*tbl
= _legend_table
[this->map_type
]; !tbl
->end
; ++tbl
) {
1140 if (tbl
->col_break
|| ((this->map_type
== SMT_INDUSTRY
|| this->map_type
== SMT_OWNER
|| this->map_type
== SMT_LINKSTATS
) && i
++ >= number_of_rows
)) {
1141 /* Column break needed, continue at top, COLUMN_WIDTH pixels
1142 * (one "row") to the right. */
1143 x
+= rtl
? -(int)this->column_width
: this->column_width
;
1148 uint8 legend_colour
= tbl
->colour
;
1150 switch (this->map_type
) {
1152 /* Industry name must be formatted, since it's not in tiny font in the specs.
1153 * So, draw with a parameter and use the STR_SMALLMAP_INDUSTRY string, which is tiny font */
1154 SetDParam(0, tbl
->legend
);
1155 SetDParam(1, Industry::GetIndustryTypeCount(tbl
->type
));
1156 if (tbl
->show_on_map
&& tbl
->type
== _smallmap_industry_highlight
) {
1157 legend_colour
= _smallmap_industry_highlight_state
? PC_WHITE
: PC_BLACK
;
1162 SetDParam(0, tbl
->legend
);
1166 if (this->map_type
!= SMT_OWNER
|| tbl
->company
!= INVALID_COMPANY
) {
1167 if (this->map_type
== SMT_OWNER
) SetDParam(0, tbl
->company
);
1168 if (!tbl
->show_on_map
) {
1169 /* Simply draw the string, not the black border of the legend colour.
1170 * This will enforce the idea of the disabled item */
1171 DrawString(x
+ text_left
, x
+ text_right
, y
, string
, TC_GREY
);
1173 DrawString(x
+ text_left
, x
+ text_right
, y
, string
, TC_BLACK
);
1174 GfxFillRect(x
+ blob_left
, y
+ 1, x
+ blob_right
, y
+ row_height
- 1, PC_BLACK
); // Outer border of the legend colour
1181 if (this->map_type
== SMT_CONTOUR
) SetDParam(0, tbl
->height
* TILE_HEIGHT_STEP
);
1182 /* Anything that is not an industry or a company is using normal process */
1183 GfxFillRect(x
+ blob_left
, y
+ 1, x
+ blob_right
, y
+ row_height
- 1, PC_BLACK
);
1184 DrawString(x
+ text_left
, x
+ text_right
, y
, tbl
->legend
);
1187 GfxFillRect(x
+ blob_left
+ 1, y
+ 2, x
+ blob_right
- 1, y
+ row_height
- 2, legend_colour
); // Legend colour
1196 * Select a new map type.
1197 * @param map_type New map type.
1199 void SmallMapWindow::SwitchMapType(SmallMapType map_type
)
1201 this->RaiseWidget(this->map_type
+ WID_SM_CONTOUR
);
1202 this->map_type
= map_type
;
1203 this->LowerWidget(this->map_type
+ WID_SM_CONTOUR
);
1205 this->SetupWidgetData();
1207 if (map_type
== SMT_LINKSTATS
) this->overlay
->RebuildCache();
1208 if (map_type
!= SMT_INDUSTRY
) this->BreakIndustryChainLink();
1213 * Get the number of rows in the legend from the number of columns. Those
1214 * are at least min_number_of_fixed_rows and possibly more if there are so
1215 * many cargoes, industry types or companies that they won't fit in the
1217 * @param columns Number of columns in the legend.
1218 * @return Number of rows needed for everything to fit in.
1220 inline uint
SmallMapWindow::GetNumberRowsLegend(uint columns
) const
1222 /* Reserve one column for link colours */
1223 uint num_rows_linkstats
= CeilDiv(_smallmap_cargo_count
, columns
- 1);
1224 uint num_rows_others
= CeilDiv(max(_smallmap_industry_count
, _smallmap_company_count
), columns
);
1225 return max(this->min_number_of_fixed_rows
, max(num_rows_linkstats
, num_rows_others
));
1229 * Select and toggle a legend item. When CTRL is pressed, disable all other
1230 * items in the group defined by begin_legend_item and end_legend_item and
1231 * keep the clicked one enabled even if it was already enabled before. If
1232 * the other items in the group are all disabled already and CTRL is pressed
1233 * enable them instead.
1234 * @param click_pos the index of the item being selected
1235 * @param legend the legend from which we select
1236 * @param end_legend_item index one past the last item in the group to be inverted
1237 * @param begin_legend_item index of the first item in the group to be inverted
1239 void SmallMapWindow::SelectLegendItem(int click_pos
, LegendAndColour
*legend
, int end_legend_item
, int begin_legend_item
)
1241 if (_ctrl_pressed
) {
1242 /* Disable all, except the clicked one */
1243 bool changes
= false;
1244 for (int i
= begin_legend_item
; i
!= end_legend_item
; i
++) {
1245 bool new_state
= (i
== click_pos
);
1246 if (legend
[i
].show_on_map
!= new_state
) {
1248 legend
[i
].show_on_map
= new_state
;
1252 /* Nothing changed? Then show all (again). */
1253 for (int i
= begin_legend_item
; i
!= end_legend_item
; i
++) {
1254 legend
[i
].show_on_map
= true;
1258 legend
[click_pos
].show_on_map
= !legend
[click_pos
].show_on_map
;
1261 if (this->map_type
== SMT_INDUSTRY
) this->BreakIndustryChainLink();
1265 * Set the link graph overlay cargo mask from the legend.
1267 void SmallMapWindow::SetOverlayCargoMask()
1269 uint32 cargo_mask
= 0;
1270 for (int i
= 0; i
!= _smallmap_cargo_count
; ++i
) {
1271 if (_legend_linkstats
[i
].show_on_map
) SetBit(cargo_mask
, _legend_linkstats
[i
].type
);
1273 this->overlay
->SetCargoMask(cargo_mask
);
1277 * Determines the mouse position on the legend.
1278 * @param pt Mouse position.
1279 * @return Legend item under the mouse.
1281 int SmallMapWindow::GetPositionOnLegend(Point pt
)
1283 const NWidgetBase
*wi
= this->GetWidget
<NWidgetBase
>(WID_SM_LEGEND
);
1284 uint line
= (pt
.y
- wi
->pos_y
- WD_FRAMERECT_TOP
) / FONT_HEIGHT_SMALL
;
1285 uint columns
= this->GetNumberColumnsLegend(wi
->current_x
);
1286 uint number_of_rows
= this->GetNumberRowsLegend(columns
);
1287 if (line
>= number_of_rows
) return -1;
1289 bool rtl
= _current_text_dir
== TD_RTL
;
1290 int x
= pt
.x
- wi
->pos_x
;
1291 if (rtl
) x
= wi
->current_x
- x
;
1292 uint column
= (x
- WD_FRAMERECT_LEFT
) / this->column_width
;
1294 return (column
* number_of_rows
) + line
;
1297 /* virtual */ void SmallMapWindow::OnMouseOver(Point pt
, int widget
)
1299 IndustryType new_highlight
= INVALID_INDUSTRYTYPE
;
1300 if (widget
== WID_SM_LEGEND
&& this->map_type
== SMT_INDUSTRY
) {
1301 int industry_pos
= GetPositionOnLegend(pt
);
1302 if (industry_pos
>= 0 && industry_pos
< _smallmap_industry_count
) {
1303 new_highlight
= _legend_from_industries
[industry_pos
].type
;
1306 if (new_highlight
!= _smallmap_industry_highlight
) {
1307 _smallmap_industry_highlight
= new_highlight
;
1308 this->refresh
= _smallmap_industry_highlight
!= INVALID_INDUSTRYTYPE
? BLINK_PERIOD
: FORCE_REFRESH_PERIOD
;
1309 _smallmap_industry_highlight_state
= true;
1314 /* virtual */ void SmallMapWindow::OnClick(Point pt
, int widget
, int click_count
)
1317 case WID_SM_MAP
: { // Map window
1319 * XXX: scrolling with the left mouse button is done by subsequently
1320 * clicking with the left mouse button; clicking once centers the
1321 * large map at the selected point. So by unclicking the left mouse
1322 * button here, it gets reclicked during the next inputloop, which
1323 * would make it look like the mouse is being dragged, while it is
1324 * actually being (virtually) clicked every inputloop.
1326 _left_button_clicked
= false;
1328 const NWidgetBase
*wid
= this->GetWidget
<NWidgetBase
>(WID_SM_MAP
);
1329 Window
*w
= FindWindowById(WC_MAIN_WINDOW
, 0);
1331 pt
= this->PixelToTile(pt
.x
- wid
->pos_x
, pt
.y
- wid
->pos_y
, &sub
);
1332 ScrollWindowTo(this->scroll_x
+ pt
.x
* TILE_SIZE
, this->scroll_y
+ pt
.y
* TILE_SIZE
, -1, w
);
1338 case WID_SM_ZOOM_IN
:
1339 case WID_SM_ZOOM_OUT
: {
1340 const NWidgetBase
*wid
= this->GetWidget
<NWidgetBase
>(WID_SM_MAP
);
1341 Point pt
= { (int)wid
->current_x
/ 2, (int)wid
->current_y
/ 2};
1342 this->SetZoomLevel((widget
== WID_SM_ZOOM_IN
) ? ZLC_ZOOM_IN
: ZLC_ZOOM_OUT
, &pt
);
1343 if (_settings_client
.sound
.click_beep
) SndPlayFx(SND_15_BEEP
);
1347 case WID_SM_CONTOUR
: // Show land contours
1348 case WID_SM_VEHICLES
: // Show vehicles
1349 case WID_SM_INDUSTRIES
: // Show industries
1350 case WID_SM_LINKSTATS
: // Show route map
1351 case WID_SM_ROUTES
: // Show transport routes
1352 case WID_SM_VEGETATION
: // Show vegetation
1353 case WID_SM_OWNERS
: // Show land owners
1354 this->SwitchMapType((SmallMapType
)(widget
- WID_SM_CONTOUR
));
1355 if (_settings_client
.sound
.click_beep
) SndPlayFx(SND_15_BEEP
);
1358 case WID_SM_CENTERMAP
: // Center the smallmap again
1359 this->SmallMapCenterOnCurrentPos();
1360 this->HandleButtonClick(WID_SM_CENTERMAP
);
1361 if (_settings_client
.sound
.click_beep
) SndPlayFx(SND_15_BEEP
);
1364 case WID_SM_TOGGLETOWNNAME
: // Toggle town names
1365 this->show_towns
= !this->show_towns
;
1366 this->SetWidgetLoweredState(WID_SM_TOGGLETOWNNAME
, this->show_towns
);
1369 if (_settings_client
.sound
.click_beep
) SndPlayFx(SND_15_BEEP
);
1372 case WID_SM_LEGEND
: // Legend
1373 if (this->map_type
== SMT_INDUSTRY
|| this->map_type
== SMT_LINKSTATS
|| this->map_type
== SMT_OWNER
) {
1374 int click_pos
= this->GetPositionOnLegend(pt
);
1375 if (click_pos
< 0) break;
1377 /* If industry type small map*/
1378 if (this->map_type
== SMT_INDUSTRY
) {
1379 /* If click on industries label, find right industry type and enable/disable it. */
1380 if (click_pos
< _smallmap_industry_count
) {
1381 this->SelectLegendItem(click_pos
, _legend_from_industries
, _smallmap_industry_count
);
1382 NotifyAllViewports(VPMT_INDUSTRY
);
1384 } else if (this->map_type
== SMT_LINKSTATS
) {
1385 if (click_pos
< _smallmap_cargo_count
) {
1386 this->SelectLegendItem(click_pos
, _legend_linkstats
, _smallmap_cargo_count
);
1387 this->SetOverlayCargoMask();
1389 } else if (this->map_type
== SMT_OWNER
) {
1390 if (click_pos
< _smallmap_company_count
) {
1391 this->SelectLegendItem(click_pos
, _legend_land_owners
, _smallmap_company_count
, NUM_NO_COMPANY_ENTRIES
);
1392 NotifyAllViewports(VPMT_OWNER
);
1399 case WID_SM_ENABLE_ALL
:
1400 case WID_SM_DISABLE_ALL
: {
1401 LegendAndColour
*tbl
= nullptr;
1402 switch (this->map_type
) {
1404 tbl
= _legend_from_industries
;
1405 this->BreakIndustryChainLink();
1406 NotifyAllViewports(VPMT_INDUSTRY
);
1409 tbl
= &(_legend_land_owners
[NUM_NO_COMPANY_ENTRIES
]);
1410 NotifyAllViewports(VPMT_OWNER
);
1413 tbl
= _legend_linkstats
;
1418 for (;!tbl
->end
&& tbl
->legend
!= STR_LINKGRAPH_LEGEND_UNUSED
; ++tbl
) {
1419 tbl
->show_on_map
= (widget
== WID_SM_ENABLE_ALL
);
1421 if (this->map_type
== SMT_LINKSTATS
) this->SetOverlayCargoMask();
1426 case WID_SM_SHOW_HEIGHT
: // Enable/disable showing of heightmap.
1427 _smallmap_show_heightmap
= !_smallmap_show_heightmap
;
1428 this->SetWidgetLoweredState(WID_SM_SHOW_HEIGHT
, _smallmap_show_heightmap
);
1429 NotifyAllViewports(VPMT_INDUSTRY
);
1433 case WID_SM_SCREENSHOT
:
1440 * Some data on this window has become invalid.
1441 * @param data Information about the changed data.
1442 * - data = 0: Displayed industries at the industry chain window have changed.
1443 * - data = 1: Companies have changed.
1444 * - data = 2: Cheat changing the maximum heightlevel has been used, rebuild our heightlevel-to-colour index
1445 * @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.
1447 /* virtual */ void SmallMapWindow::OnInvalidateData(int data
, bool gui_scope
)
1449 if (!gui_scope
) return;
1453 /* The owner legend has already been rebuilt. */
1458 extern std::bitset
<NUM_INDUSTRYTYPES
> _displayed_industries
;
1459 if (this->map_type
!= SMT_INDUSTRY
) this->SwitchMapType(SMT_INDUSTRY
);
1461 for (int i
= 0; i
!= _smallmap_industry_count
; i
++) {
1462 _legend_from_industries
[i
].show_on_map
= _displayed_industries
.test(_legend_from_industries
[i
].type
);
1468 this->RebuildColourIndexIfNecessary();
1471 default: NOT_REACHED();
1476 /* virtual */ bool SmallMapWindow::OnRightClick(Point pt
, int widget
)
1478 if (widget
!= WID_SM_MAP
|| _scrolling_viewport
) return false;
1480 _scrolling_viewport
= this;
1484 /* virtual */ void SmallMapWindow::OnMouseWheel(int wheel
)
1486 if (_settings_client
.gui
.scrollwheel_scrolling
== 0) {
1487 const NWidgetBase
*wid
= this->GetWidget
<NWidgetBase
>(WID_SM_MAP
);
1488 int cursor_x
= _cursor
.pos
.x
- this->left
- wid
->pos_x
;
1489 int cursor_y
= _cursor
.pos
.y
- this->top
- wid
->pos_y
;
1490 if (IsInsideMM(cursor_x
, 0, wid
->current_x
) && IsInsideMM(cursor_y
, 0, wid
->current_y
)) {
1491 Point pt
= {cursor_x
, cursor_y
};
1492 this->SetZoomLevel((wheel
< 0) ? ZLC_ZOOM_IN
: ZLC_ZOOM_OUT
, &pt
);
1497 /* virtual */ void SmallMapWindow::OnTick()
1499 /* Update the window every now and then */
1500 if (--this->refresh
!= 0) return;
1502 if (this->map_type
== SMT_LINKSTATS
) {
1503 uint32 company_mask
= this->GetOverlayCompanyMask();
1504 if (this->overlay
->GetCompanyMask() != company_mask
) {
1505 this->overlay
->SetCompanyMask(company_mask
);
1507 this->overlay
->RebuildCache();
1510 _smallmap_industry_highlight_state
= !_smallmap_industry_highlight_state
;
1512 this->refresh
= _smallmap_industry_highlight
!= INVALID_INDUSTRYTYPE
? BLINK_PERIOD
: FORCE_REFRESH_PERIOD
;
1517 * Set new #scroll_x, #scroll_y, and #subscroll values after limiting them such that the center
1518 * of the smallmap always contains a part of the map.
1519 * @param sx Proposed new #scroll_x
1520 * @param sy Proposed new #scroll_y
1521 * @param sub Proposed new #subscroll
1523 void SmallMapWindow::SetNewScroll(int sx
, int sy
, int sub
)
1525 const NWidgetBase
*wi
= this->GetWidget
<NWidgetBase
>(WID_SM_MAP
);
1526 Point hv
= InverseRemapCoords(wi
->current_x
* ZOOM_LVL_BASE
* TILE_SIZE
/ 2, wi
->current_y
* ZOOM_LVL_BASE
* TILE_SIZE
/ 2);
1534 if (sx
> (int)(MapMaxX() * TILE_SIZE
) - hv
.x
) {
1535 sx
= MapMaxX() * TILE_SIZE
- hv
.x
;
1542 if (sy
> (int)(MapMaxY() * TILE_SIZE
) - hv
.y
) {
1543 sy
= MapMaxY() * TILE_SIZE
- hv
.y
;
1547 this->scroll_x
= sx
;
1548 this->scroll_y
= sy
;
1549 this->subscroll
= sub
;
1550 if (this->map_type
== SMT_LINKSTATS
) this->overlay
->RebuildCache();
1553 /* static */ bool SmallMapWindow::MouseOverTileSearchProc(TileIndex tile
, void *user_data
)
1555 MouseOverTileSearchData
*data
= reinterpret_cast<MouseOverTileSearchData
*>(user_data
);
1557 TileType type
= GetTileType(tile
);
1559 /* industries - after stations */
1561 if (data
->found_type
!= MP_INDUSTRY
&& data
->found_type
!= MP_STATION
) {
1562 /* check if the industry is visible */
1563 if (data
->map_type
!= SMT_INDUSTRY
||
1564 _legend_from_industries
[_industry_to_list_pos
[Industry::GetByTile(tile
)->type
]].show_on_map
) {
1570 /* stations - most important if current smallmap marks out stations, otherwise not important */
1572 if (data
->map_type
== SMT_CONTOUR
|| data
->map_type
== SMT_LINKSTATS
|| data
->map_type
== SMT_ROUTES
) break;
1575 /* least important - towns */
1577 case MP_TUNNELBRIDGE
:
1579 if (data
->found_type
== MP_VOID
&& (type
== MP_HOUSE
|| GetTileOwner(tile
) == OWNER_TOWN
)) break;
1586 data
->found_type
= type
;
1590 /* virtual */ void SmallMapWindow::OnToolTip(Point pt
, int widget
, TooltipCloseCondition close_cond
)
1592 if (widget
!= WID_SM_MAP
) {
1593 this->Window::OnToolTip(pt
, widget
, close_cond
);
1597 TileIndex found_tile
= INVALID_TILE
;
1599 /* find out which tile area is being displayed at the point */
1600 const NWidgetBase
*wid
= this->GetWidget
<NWidgetBase
>(WID_SM_MAP
);
1602 pt
= this->PixelToTile(pt
.x
- wid
->pos_x
, pt
.y
- wid
->pos_y
, &sub
);
1603 pt
.x
+= this->scroll_x
/ (int)TILE_SIZE
;
1604 pt
.y
+= this->scroll_y
/ (int)TILE_SIZE
;
1605 if (IsInsideMM(pt
.x
, 0, MapSizeX()) && IsInsideMM(pt
.y
, 0, MapSizeY())) {
1606 TileArea
ta(TileXY(pt
.x
, pt
.y
), this->zoom
, this->zoom
);
1608 /* search entire area, find best match */
1609 TileType best_type
= (this->map_type
== SMT_CONTOUR
|| this->map_type
== SMT_LINKSTATS
|| this->map_type
== SMT_ROUTES
) ? MP_STATION
: MP_INDUSTRY
;
1610 SmallMapWindow::MouseOverTileSearchData search_data
= { this->map_type
, MP_VOID
};
1611 TILE_AREA_LOOP(tile
, ta
) {
1612 if (MouseOverTileSearchProc(tile
, &search_data
)) {
1614 if (search_data
.found_type
== best_type
) break;
1617 /* search also some surroundings, find first match */
1618 if (found_tile
== INVALID_TILE
) {
1619 found_tile
= CircularTileSearch(TileX(ta
.tile
) - 1, TileY(ta
.tile
) - 1, 2 * this->zoom
, ta
.w
, ta
.h
, SmallMapWindow::MouseOverTileSearchProc
, &search_data
);
1623 GuiShowTooltipsForTile(this, found_tile
);
1626 /* virtual */ void SmallMapWindow::OnScroll(Point delta
)
1628 _cursor
.fix_at
= true;
1630 /* While tile is at (delta.x, delta.y)? */
1632 Point pt
= this->PixelToTile(delta
.x
, delta
.y
, &sub
);
1633 this->SetNewScroll(this->scroll_x
+ pt
.x
* TILE_SIZE
, this->scroll_y
+ pt
.y
* TILE_SIZE
, sub
);
1639 * Center the small map on the current center of the viewport.
1641 void SmallMapWindow::SmallMapCenterOnCurrentPos()
1643 const ViewPort
*vp
= FindWindowById(WC_MAIN_WINDOW
, 0)->viewport
;
1644 Point viewport_center
= TranslateXYToTileCoord(vp
, vp
->left
+ vp
->width
/ 2, vp
->top
+ vp
->height
/ 2);
1647 const NWidgetBase
*wid
= this->GetWidget
<NWidgetBase
>(WID_SM_MAP
);
1648 Point sxy
= this->ComputeScroll(viewport_center
.x
/ (int)TILE_SIZE
, viewport_center
.y
/ (int)TILE_SIZE
,
1649 max(0, (int)wid
->current_x
/ 2 - 2), wid
->current_y
/ 2, &sub
);
1650 this->SetNewScroll(sxy
.x
, sxy
.y
, sub
);
1655 * Get the center of the given station as point on the screen in the smallmap window.
1656 * @param st Station to find in the smallmap.
1657 * @return Point with coordinates of the station.
1659 Point
SmallMapWindow::GetStationMiddle(const Station
*st
) const
1661 int x
= (st
->rect
.right
+ st
->rect
.left
+ 1) / 2;
1662 int y
= (st
->rect
.bottom
+ st
->rect
.top
+ 1) / 2;
1663 Point ret
= this->RemapTile(x
, y
);
1665 /* Same magic 3 as in DrawVehicles; that's where I got it from.
1666 * No idea what it is, but without it the result looks bad.
1668 ret
.x
-= 3 + this->subscroll
;
1673 * Take a screenshot of the contents of the smallmap window, at the current zoom level and mode
1674 * This calls MakeSmallMapScreenshot which uses ScreenshotCallbackHandler as the screenshot callback
1676 void SmallMapWindow::TakeScreenshot()
1678 int32 width
= ((MapMaxX() + MapMaxY()) * 2) / this->zoom
;
1679 int32 height
= (MapMaxX() + MapMaxY() + 1) / this->zoom
;
1681 int32 saved_scroll_x
= this->scroll_x
;
1682 int32 saved_scroll_y
= this->scroll_y
;
1683 int32 saved_subscroll
= this->subscroll
;
1684 this->subscroll
= 0;
1685 MakeSmallMapScreenshot(width
, height
, this);
1686 this->scroll_x
= saved_scroll_x
;
1687 this->scroll_y
= saved_scroll_y
;
1688 this->subscroll
= saved_subscroll
;
1692 * Callback called by the screenshot code to draw a section of the smallmap to the output buffer
1693 * @param buf Videobuffer with same bitdepth as current blitter
1694 * @param y First line to render
1695 * @param pitch Pitch of the videobuffer
1696 * @param n Number of lines to render
1698 void SmallMapWindow::ScreenshotCallbackHandler(void *buf
, uint y
, uint pitch
, uint n
)
1700 DrawPixelInfo dpi
, *old_dpi
;
1702 /* We are no longer rendering to the screen */
1703 DrawPixelInfo old_screen
= _screen
;
1704 bool old_disable_anim
= _screen_disable_anim
;
1706 _screen
.dst_ptr
= buf
;
1707 _screen
.width
= pitch
;
1709 _screen
.pitch
= pitch
;
1710 _screen_disable_anim
= true;
1717 dpi
.width
= ((MapMaxX() + MapMaxY()) * 2) / this->zoom
;
1719 dpi
.zoom
= ZOOM_LVL_NORMAL
;
1723 int32 pos
= (((int32
)MapMaxX() + 1) * TILE_PIXELS
) / 4;
1724 this->scroll_x
= pos
;
1725 this->scroll_y
= -pos
;
1727 /* make the screenshot */
1728 this->DrawSmallMap(&dpi
, false);
1732 /* Switch back to rendering to the screen */
1733 _screen
= old_screen
;
1734 _screen_disable_anim
= old_disable_anim
;
1737 SmallMapWindow::SmallMapType
SmallMapWindow::map_type
= SMT_CONTOUR
;
1738 bool SmallMapWindow::show_towns
= true;
1739 int SmallMapWindow::max_heightlevel
= -1;
1742 * Custom container class for displaying smallmap with a vertically resizing legend panel.
1743 * The legend panel has a smallest height that depends on its width. Standard containers cannot handle this case.
1745 * @note The container assumes it has two children, the first is the display, the second is the bar with legends and selection image buttons.
1746 * Both children should be both horizontally and vertically resizable and horizontally fillable.
1747 * The bar should have a minimal size with a zero-size legends display. Child padding is not supported.
1749 class NWidgetSmallmapDisplay
: public NWidgetContainer
{
1750 const SmallMapWindow
*smallmap_window
; ///< Window manager instance.
1752 NWidgetSmallmapDisplay() : NWidgetContainer(NWID_VERTICAL
)
1754 this->smallmap_window
= nullptr;
1757 virtual void SetupSmallestSize(Window
*w
, bool init_array
)
1759 NWidgetBase
*display
= this->head
;
1760 NWidgetBase
*bar
= display
->next
;
1762 display
->SetupSmallestSize(w
, init_array
);
1763 bar
->SetupSmallestSize(w
, init_array
);
1765 this->smallmap_window
= dynamic_cast<SmallMapWindow
*>(w
);
1766 assert(this->smallmap_window
!= nullptr);
1767 this->smallest_x
= max(display
->smallest_x
, bar
->smallest_x
+ smallmap_window
->GetMinLegendWidth());
1768 this->smallest_y
= display
->smallest_y
+ max(bar
->smallest_y
, smallmap_window
->GetLegendHeight(smallmap_window
->min_number_of_columns
));
1769 this->fill_x
= max(display
->fill_x
, bar
->fill_x
);
1770 this->fill_y
= (display
->fill_y
== 0 && bar
->fill_y
== 0) ? 0 : min(display
->fill_y
, bar
->fill_y
);
1771 this->resize_x
= max(display
->resize_x
, bar
->resize_x
);
1772 this->resize_y
= min(display
->resize_y
, bar
->resize_y
);
1775 virtual void AssignSizePosition(SizingType sizing
, uint x
, uint y
, uint given_width
, uint given_height
, bool rtl
)
1779 this->current_x
= given_width
;
1780 this->current_y
= given_height
;
1782 NWidgetBase
*display
= this->head
;
1783 NWidgetBase
*bar
= display
->next
;
1785 if (sizing
== ST_SMALLEST
) {
1786 this->smallest_x
= given_width
;
1787 this->smallest_y
= given_height
;
1788 /* Make display and bar exactly equal to their minimal size. */
1789 display
->AssignSizePosition(ST_SMALLEST
, x
, y
, display
->smallest_x
, display
->smallest_y
, rtl
);
1790 bar
->AssignSizePosition(ST_SMALLEST
, x
, y
+ display
->smallest_y
, bar
->smallest_x
, bar
->smallest_y
, rtl
);
1793 uint bar_height
= max(bar
->smallest_y
, this->smallmap_window
->GetLegendHeight(this->smallmap_window
->GetNumberColumnsLegend(given_width
- bar
->smallest_x
)));
1794 uint display_height
= given_height
- bar_height
;
1795 display
->AssignSizePosition(ST_RESIZE
, x
, y
, given_width
, display_height
, rtl
);
1796 bar
->AssignSizePosition(ST_RESIZE
, x
, y
+ display_height
, given_width
, bar_height
, rtl
);
1799 virtual NWidgetCore
*GetWidgetFromPos(int x
, int y
)
1801 if (!IsInsideBS(x
, this->pos_x
, this->current_x
) || !IsInsideBS(y
, this->pos_y
, this->current_y
)) return nullptr;
1802 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= nullptr; child_wid
= child_wid
->next
) {
1803 NWidgetCore
*widget
= child_wid
->GetWidgetFromPos(x
, y
);
1804 if (widget
!= nullptr) return widget
;
1809 virtual void Draw(const Window
*w
)
1811 for (NWidgetBase
*child_wid
= this->head
; child_wid
!= nullptr; child_wid
= child_wid
->next
) child_wid
->Draw(w
);
1815 /** Widget parts of the smallmap display. */
1816 static const NWidgetPart _nested_smallmap_display
[] = {
1817 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_SM_MAP_BORDER
),
1818 NWidget(WWT_INSET
, COLOUR_BROWN
, WID_SM_MAP
), SetMinimalSize(346, 140), SetResize(1, 1), SetPadding(2, 2, 2, 2), EndContainer(),
1822 /** Widget parts of the smallmap legend bar + image buttons. */
1823 static const NWidgetPart _nested_smallmap_bar
[] = {
1824 NWidget(WWT_PANEL
, COLOUR_BROWN
),
1825 NWidget(NWID_HORIZONTAL
),
1826 NWidget(WWT_EMPTY
, INVALID_COLOUR
, WID_SM_LEGEND
), SetResize(1, 1),
1827 NWidget(NWID_VERTICAL
),
1828 /* Top button row. */
1829 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
1830 NWidget(WWT_PUSHIMGBTN
, COLOUR_BROWN
, WID_SM_ZOOM_IN
),
1831 SetDataTip(SPR_IMG_ZOOMIN
, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_IN
), SetFill(1, 1),
1832 NWidget(WWT_PUSHIMGBTN
, COLOUR_BROWN
, WID_SM_CENTERMAP
),
1833 SetDataTip(SPR_IMG_SMALLMAP
, STR_SMALLMAP_CENTER
), SetFill(1, 1),
1834 NWidget(WWT_IMGBTN
, COLOUR_BROWN
, WID_SM_BLANK
),
1835 SetDataTip(SPR_DOT_SMALL
, STR_NULL
), SetFill(1, 1),
1836 NWidget(WWT_IMGBTN
, COLOUR_BROWN
, WID_SM_CONTOUR
),
1837 SetDataTip(SPR_IMG_SHOW_COUNTOURS
, STR_SMALLMAP_TOOLTIP_SHOW_LAND_CONTOURS_ON_MAP
), SetFill(1, 1),
1838 NWidget(WWT_IMGBTN
, COLOUR_BROWN
, WID_SM_VEHICLES
),
1839 SetDataTip(SPR_IMG_SHOW_VEHICLES
, STR_SMALLMAP_TOOLTIP_SHOW_VEHICLES_ON_MAP
), SetFill(1, 1),
1840 NWidget(WWT_IMGBTN
, COLOUR_BROWN
, WID_SM_INDUSTRIES
),
1841 SetDataTip(SPR_IMG_INDUSTRY
, STR_SMALLMAP_TOOLTIP_SHOW_INDUSTRIES_ON_MAP
), SetFill(1, 1),
1843 /* Bottom button row. */
1844 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
1845 NWidget(WWT_PUSHIMGBTN
, COLOUR_BROWN
, WID_SM_ZOOM_OUT
),
1846 SetDataTip(SPR_IMG_ZOOMOUT
, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_OUT
), SetFill(1, 1),
1847 NWidget(WWT_IMGBTN
, COLOUR_BROWN
, WID_SM_TOGGLETOWNNAME
),
1848 SetDataTip(SPR_IMG_TOWN
, STR_SMALLMAP_TOOLTIP_TOGGLE_TOWN_NAMES_ON_OFF
), SetFill(1, 1),
1849 NWidget(WWT_IMGBTN
, COLOUR_BROWN
, WID_SM_LINKSTATS
),
1850 SetDataTip(SPR_IMG_CARGOFLOW
, STR_SMALLMAP_TOOLTIP_SHOW_LINK_STATS_ON_MAP
), SetFill(1, 1),
1851 NWidget(WWT_IMGBTN
, COLOUR_BROWN
, WID_SM_ROUTES
),
1852 SetDataTip(SPR_IMG_SHOW_ROUTES
, STR_SMALLMAP_TOOLTIP_SHOW_TRANSPORT_ROUTES_ON
), SetFill(1, 1),
1853 NWidget(WWT_IMGBTN
, COLOUR_BROWN
, WID_SM_VEGETATION
),
1854 SetDataTip(SPR_IMG_PLANTTREES
, STR_SMALLMAP_TOOLTIP_SHOW_VEGETATION_ON_MAP
), SetFill(1, 1),
1855 NWidget(WWT_IMGBTN
, COLOUR_BROWN
, WID_SM_OWNERS
),
1856 SetDataTip(SPR_IMG_COMPANY_GENERAL
, STR_SMALLMAP_TOOLTIP_SHOW_LAND_OWNERS_ON_MAP
), SetFill(1, 1),
1858 NWidget(NWID_SPACER
), SetResize(0, 1),
1864 static NWidgetBase
*SmallMapDisplay(int *biggest_index
)
1866 NWidgetContainer
*map_display
= new NWidgetSmallmapDisplay
;
1868 MakeNWidgets(_nested_smallmap_display
, lengthof(_nested_smallmap_display
), biggest_index
, map_display
);
1869 MakeNWidgets(_nested_smallmap_bar
, lengthof(_nested_smallmap_bar
), biggest_index
, map_display
);
1874 static const NWidgetPart _nested_smallmap_widgets
[] = {
1875 NWidget(NWID_HORIZONTAL
),
1876 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
1877 NWidget(WWT_CAPTION
, COLOUR_BROWN
, WID_SM_CAPTION
), SetDataTip(STR_SMALLMAP_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
1878 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
1879 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
1880 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
1882 NWidgetFunction(SmallMapDisplay
), // Smallmap display and legend bar + image buttons.
1883 /* Bottom button row and resize box. */
1884 NWidget(NWID_HORIZONTAL
),
1885 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_SM_SCREENSHOT
), SetDataTip(STR_SMALLMAP_SCREENSHOT
, STR_NULL
),
1886 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_SM_SELECT_BUTTONS
),
1887 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
1888 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_SM_ENABLE_ALL
), SetDataTip(STR_SMALLMAP_ENABLE_ALL
, STR_NULL
),
1889 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_SM_DISABLE_ALL
), SetDataTip(STR_SMALLMAP_DISABLE_ALL
, STR_NULL
),
1890 NWidget(WWT_TEXTBTN
, COLOUR_BROWN
, WID_SM_SHOW_HEIGHT
), SetDataTip(STR_SMALLMAP_SHOW_HEIGHT
, STR_SMALLMAP_TOOLTIP_SHOW_HEIGHT
),
1891 NWidget(WWT_PANEL
, COLOUR_BROWN
), SetFill(1, 0), SetResize(1, 0),
1894 NWidget(WWT_PANEL
, COLOUR_BROWN
), SetFill(1, 0), SetResize(1, 0),
1897 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
),
1901 static WindowDesc
_smallmap_desc(
1902 WDP_AUTO
, "smallmap", 484, 314,
1903 WC_SMALLMAP
, WC_NONE
,
1905 _nested_smallmap_widgets
, lengthof(_nested_smallmap_widgets
)
1909 * Show the smallmap window.
1913 AllocateWindowDescFront
<SmallMapWindow
>(&_smallmap_desc
, 0);
1917 * Scrolls the main window to given coordinates.
1918 * @param x x coordinate
1919 * @param y y coordinate
1920 * @param z z coordinate; -1 to scroll to terrain height
1921 * @param instant scroll instantly (meaningful only when smooth_scrolling is active)
1922 * @return did the viewport position change?
1924 bool ScrollMainWindowTo(int x
, int y
, int z
, bool instant
)
1926 bool res
= ScrollWindowTo(x
, y
, z
, FindWindowById(WC_MAIN_WINDOW
, 0), instant
);
1928 /* If a user scrolls to a tile (via what way what so ever) and already is on
1929 * that tile (e.g.: pressed twice), move the smallmap to that location,
1930 * so you directly see where you are on the smallmap. */
1932 if (res
) return res
;
1934 SmallMapWindow
*w
= dynamic_cast<SmallMapWindow
*>(FindWindowById(WC_SMALLMAP
, 0));
1935 if (w
!= nullptr) w
->SmallMapCenterOnCurrentPos();