OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

smallmap_gui.cpp
Go to the documentation of this file.
1 /* $Id: smallmap_gui.cpp 27381 2015-08-10 20:24:13Z michi_cc $ */
2
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 */
9
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
27 #include "smallmap_gui.h"
28
29 #include "table/strings.h"
30
31 #include <bitset>
32
33 #include "safeguards.h"
34
35 static int _smallmap_industry_count;
36 static int _smallmap_company_count;
37 static int _smallmap_cargo_count;
38
40 static uint8 _linkstat_colours_in_legenda[] = {0, 1, 3, 5, 7, 9, 11};
41
42 static const int NUM_NO_COMPANY_ENTRIES = 4;
43
44 static const uint8 PC_ROUGH_LAND = 0x52;
45 static const uint8 PC_GRASS_LAND = 0x54;
46 static const uint8 PC_BARE_LAND = 0x37;
47 static const uint8 PC_FIELDS = 0x25;
48 static const uint8 PC_TREES = 0x57;
49 static const uint8 PC_WATER = 0xCA;
50
52 #define MK(a, b) {a, b, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, false, false}
53
55 #define MC(col_break) {0, STR_TINY_BLACK_HEIGHT, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, false, col_break}
56
58 #define MO(a, b) {a, b, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, false, false}
59
61 #define MOEND() {0, 0, INVALID_INDUSTRYTYPE, 0, OWNER_NONE, true, true, false}
62
64 #define MKEND() {0, STR_NULL, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, true, false}
65
70 #define MS(a, b) {a, b, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, false, true}
71
73 static LegendAndColour _legend_land_contours[] = {
74 MK(PC_BLACK, STR_SMALLMAP_LEGENDA_ROADS),
75 MK(PC_GREY, STR_SMALLMAP_LEGENDA_RAILROADS),
76 MK(PC_LIGHT_BLUE, STR_SMALLMAP_LEGENDA_STATIONS_AIRPORTS_DOCKS),
77 MK(PC_DARK_RED, STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES),
78 MK(PC_WHITE, STR_SMALLMAP_LEGENDA_VEHICLES),
79
80 /* Placeholders for the colours and heights of the legend.
81 * The following values are set at BuildLandLegend() based
82 * on each colour scheme and the maximum map height. */
83 MC(true),
84 MC(false),
85 MC(false),
86 MC(false),
87 MC(false),
88 MC(false),
89 MC(true),
90 MC(false),
91 MC(false),
92 MC(false),
93 MC(false),
94 MC(false),
95 MKEND()
96 };
97
98 static const LegendAndColour _legend_vehicles[] = {
99 MK(PC_RED, STR_SMALLMAP_LEGENDA_TRAINS),
100 MK(PC_YELLOW, STR_SMALLMAP_LEGENDA_ROAD_VEHICLES),
101 MK(PC_LIGHT_BLUE, STR_SMALLMAP_LEGENDA_SHIPS),
102 MK(PC_WHITE, STR_SMALLMAP_LEGENDA_AIRCRAFT),
103
104 MS(PC_BLACK, STR_SMALLMAP_LEGENDA_TRANSPORT_ROUTES),
105 MK(PC_DARK_RED, STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES),
106 MKEND()
107 };
108
109 static const LegendAndColour _legend_routes[] = {
110 MK(PC_BLACK, STR_SMALLMAP_LEGENDA_ROADS),
111 MK(PC_GREY, STR_SMALLMAP_LEGENDA_RAILROADS),
112 MK(PC_DARK_RED, STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES),
113
114 MS(PC_VERY_DARK_BROWN, STR_SMALLMAP_LEGENDA_RAILROAD_STATION),
115 MK(PC_ORANGE, STR_SMALLMAP_LEGENDA_TRUCK_LOADING_BAY),
116 MK(PC_YELLOW, STR_SMALLMAP_LEGENDA_BUS_STATION),
117 MK(PC_RED, STR_SMALLMAP_LEGENDA_AIRPORT_HELIPORT),
118 MK(PC_LIGHT_BLUE, STR_SMALLMAP_LEGENDA_DOCK),
119 MKEND()
120 };
121
122 static const LegendAndColour _legend_vegetation[] = {
123 MK(PC_ROUGH_LAND, STR_SMALLMAP_LEGENDA_ROUGH_LAND),
124 MK(PC_GRASS_LAND, STR_SMALLMAP_LEGENDA_GRASS_LAND),
125 MK(PC_BARE_LAND, STR_SMALLMAP_LEGENDA_BARE_LAND),
126 MK(PC_FIELDS, STR_SMALLMAP_LEGENDA_FIELDS),
127 MK(PC_TREES, STR_SMALLMAP_LEGENDA_TREES),
128 MK(PC_GREEN, STR_SMALLMAP_LEGENDA_FOREST),
129
130 MS(PC_GREY, STR_SMALLMAP_LEGENDA_ROCKS),
131 MK(PC_ORANGE, STR_SMALLMAP_LEGENDA_DESERT),
132 MK(PC_LIGHT_BLUE, STR_SMALLMAP_LEGENDA_SNOW),
133 MK(PC_BLACK, STR_SMALLMAP_LEGENDA_TRANSPORT_ROUTES),
134 MK(PC_DARK_RED, STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES),
135 MKEND()
136 };
137
138 static LegendAndColour _legend_land_owners[NUM_NO_COMPANY_ENTRIES + MAX_COMPANIES + 1] = {
139 MO(PC_WATER, STR_SMALLMAP_LEGENDA_WATER),
140 MO(0x00, STR_SMALLMAP_LEGENDA_NO_OWNER), // This colour will vary depending on settings.
141 MO(PC_DARK_RED, STR_SMALLMAP_LEGENDA_TOWNS),
142 MO(PC_DARK_GREY, STR_SMALLMAP_LEGENDA_INDUSTRIES),
143 /* The legend will be terminated the first time it is used. */
144 MOEND(),
145 };
146
147 #undef MK
148 #undef MC
149 #undef MS
150 #undef MO
151 #undef MOEND
152 #undef MKEND
153
155 static LegendAndColour _legend_linkstats[NUM_CARGO + lengthof(_linkstat_colours_in_legenda) + 1];
160 static LegendAndColour _legend_from_industries[NUM_INDUSTRYTYPES + 1];
162 static uint _industry_to_list_pos[NUM_INDUSTRYTYPES];
164 static bool _smallmap_show_heightmap = false;
166 static IndustryType _smallmap_industry_highlight = INVALID_INDUSTRYTYPE;
168 static bool _smallmap_industry_highlight_state;
170 static uint _company_to_list_pos[MAX_COMPANIES];
171
175 void BuildIndustriesLegend()
176 {
177 uint j = 0;
178
179 /* Add each name */
180 for (uint i = 0; i < NUM_INDUSTRYTYPES; i++) {
181 IndustryType ind = _sorted_industry_types[i];
182 const IndustrySpec *indsp = GetIndustrySpec(ind);
183 if (indsp->enabled) {
184 _legend_from_industries[j].legend = indsp->name;
185 _legend_from_industries[j].colour = indsp->map_colour;
186 _legend_from_industries[j].type = ind;
187 _legend_from_industries[j].show_on_map = true;
188 _legend_from_industries[j].col_break = false;
189 _legend_from_industries[j].end = false;
190
191 /* Store widget number for this industry type. */
192 _industry_to_list_pos[ind] = j;
193 j++;
194 }
195 }
196 /* Terminate the list */
197 _legend_from_industries[j].end = true;
198
199 /* Store number of enabled industries */
200 _smallmap_industry_count = j;
201 }
202
206 void BuildLinkStatsLegend()
207 {
208 /* Clear the legend */
209 memset(_legend_linkstats, 0, sizeof(_legend_linkstats));
210
211 uint i = 0;
212 for (; i < _sorted_cargo_specs_size; ++i) {
213 const CargoSpec *cs = _sorted_cargo_specs[i];
214
215 _legend_linkstats[i].legend = cs->name;
216 _legend_linkstats[i].colour = cs->legend_colour;
217 _legend_linkstats[i].type = cs->Index();
218 _legend_linkstats[i].show_on_map = true;
219 }
220
221 _legend_linkstats[i].col_break = true;
222 _smallmap_cargo_count = i;
223
224 for (; i < _smallmap_cargo_count + lengthof(_linkstat_colours_in_legenda); ++i) {
225 _legend_linkstats[i].legend = STR_EMPTY;
226 _legend_linkstats[i].colour = LinkGraphOverlay::LINK_COLOURS[_linkstat_colours_in_legenda[i - _smallmap_cargo_count]];
227 _legend_linkstats[i].show_on_map = true;
228 }
229
230 _legend_linkstats[_smallmap_cargo_count].legend = STR_LINKGRAPH_LEGEND_UNUSED;
231 _legend_linkstats[i - 1].legend = STR_LINKGRAPH_LEGEND_OVERLOADED;
232 _legend_linkstats[(_smallmap_cargo_count + i - 1) / 2].legend = STR_LINKGRAPH_LEGEND_SATURATED;
233 _legend_linkstats[i].end = true;
234 }
235
236 static const LegendAndColour * const _legend_table[] = {
237 _legend_land_contours,
238 _legend_vehicles,
239 _legend_from_industries,
240 _legend_linkstats,
241 _legend_routes,
242 _legend_vegetation,
243 _legend_land_owners,
244 };
245
246 #define MKCOLOUR(x) TO_LE32X(x)
247
248 #define MKCOLOUR_XXXX(x) (MKCOLOUR(0x01010101) * (uint)(x))
249 #define MKCOLOUR_X0X0(x) (MKCOLOUR(0x01000100) * (uint)(x))
250 #define MKCOLOUR_0X0X(x) (MKCOLOUR(0x00010001) * (uint)(x))
251 #define MKCOLOUR_0XX0(x) (MKCOLOUR(0x00010100) * (uint)(x))
252 #define MKCOLOUR_X00X(x) (MKCOLOUR(0x01000001) * (uint)(x))
253
254 #define MKCOLOUR_XYXY(x, y) (MKCOLOUR_X0X0(x) | MKCOLOUR_0X0X(y))
255 #define MKCOLOUR_XYYX(x, y) (MKCOLOUR_X00X(x) | MKCOLOUR_0XX0(y))
256
257 #define MKCOLOUR_0000 MKCOLOUR_XXXX(0x00)
258 #define MKCOLOUR_0FF0 MKCOLOUR_0XX0(0xFF)
259 #define MKCOLOUR_F00F MKCOLOUR_X00X(0xFF)
260 #define MKCOLOUR_FFFF MKCOLOUR_XXXX(0xFF)
261
262 #include "table/heightmap_colours.h"
263
265 struct SmallMapColourScheme {
266 uint32 *height_colours;
267 const uint32 *height_colours_base;
268 size_t colour_count;
269 uint32 default_colour;
270 };
271
273 static SmallMapColourScheme _heightmap_schemes[] = {
274 {NULL, _green_map_heights, lengthof(_green_map_heights), MKCOLOUR_XXXX(0x54)},
275 {NULL, _dark_green_map_heights, lengthof(_dark_green_map_heights), MKCOLOUR_XXXX(0x62)},
276 {NULL, _violet_map_heights, lengthof(_violet_map_heights), MKCOLOUR_XXXX(0x82)},
277 };
278
282 void BuildLandLegend()
283 {
284 /* The smallmap window has never been initialized, so no need to change the legend. */
285 if (_heightmap_schemes[0].height_colours == NULL) return;
286
287 /*
288 * The general idea of this function is to fill the legend with an appropriate evenly spaced
289 * selection of height levels. All entries with STR_TINY_BLACK_HEIGHT are reserved for this.
290 * At the moment there are twelve of these.
291 *
292 * The table below defines up to which height level a particular delta in the legend should be
293 * used. One could opt for just dividing the maximum height and use that as delta, but that
294 * creates many "ugly" legend labels, e.g. once every 950 meter. As a result, this table will
295 * reduce the number of deltas to 7: every 100m, 200m, 300m, 500m, 750m, 1000m and 1250m. The
296 * deltas are closer together at the lower numbers because going from 12 entries to just 4, as
297 * would happen when replacing 200m and 300m by 250m, would mean the legend would be short and
298 * that might not be considered appropriate.
299 *
300 * The current method yields at least 7 legend entries and at most 12. It can be increased to
301 * 8 by adding a 150m and 400m option, but especially 150m creates ugly heights.
302 *
303 * It tries to evenly space the legend items over the two columns that are there for the legend.
304 */
305
306 /* Table for delta; if max_height is less than the first column, use the second column as value. */
307 uint deltas[][2] = { { 24, 2 }, { 48, 4 }, { 72, 6 }, { 120, 10 }, { 180, 15 }, { 240, 20 }, { MAX_TILE_HEIGHT + 1, 25 }};
308 uint i = 0;
309 for (; _settings_game.construction.max_heightlevel >= deltas[i][0]; i++) {
310 /* Nothing to do here. */
311 }
312 uint delta = deltas[i][1];
313
314 int total_entries = (_settings_game.construction.max_heightlevel / delta) + 1;
315 int rows = CeilDiv(total_entries, 2);
316 int j = 0;
317
318 for (i = 0; i < lengthof(_legend_land_contours) - 1 && j < total_entries; i++) {
319 if (_legend_land_contours[i].legend != STR_TINY_BLACK_HEIGHT) continue;
320
321 _legend_land_contours[i].col_break = j % rows == 0;
322 _legend_land_contours[i].end = false;
323 _legend_land_contours[i].height = j * delta;
324 _legend_land_contours[i].colour = _heightmap_schemes[_settings_client.gui.smallmap_land_colour].height_colours[j * delta];
325 j++;
326 }
327 _legend_land_contours[i].end = true;
328 }
329
333 void BuildOwnerLegend()
334 {
335 _legend_land_owners[1].colour = _heightmap_schemes[_settings_client.gui.smallmap_land_colour].default_colour;
336
337 int i = NUM_NO_COMPANY_ENTRIES;
338 const Company *c;
339 FOR_ALL_COMPANIES(c) {
340 _legend_land_owners[i].colour = _colour_gradient[c->colour][5];
341 _legend_land_owners[i].company = c->index;
342 _legend_land_owners[i].show_on_map = true;
343 _legend_land_owners[i].col_break = false;
344 _legend_land_owners[i].end = false;
345 _company_to_list_pos[c->index] = i;
346 i++;
347 }
348
349 /* Terminate the list */
350 _legend_land_owners[i].end = true;
351
352 /* Store maximum amount of owner legend entries. */
353 _smallmap_company_count = i;
354 }
355
356 struct AndOr {
357 uint32 mor;
358 uint32 mand;
359 };
360
361 static inline uint32 ApplyMask(uint32 colour, const AndOr *mask)
362 {
363 return (colour & mask->mand) | mask->mor;
364 }
365
366
368 static const AndOr _smallmap_contours_andor[] = {
369 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_CLEAR
370 {MKCOLOUR_0XX0(PC_GREY ), MKCOLOUR_F00F}, // MP_RAILWAY
371 {MKCOLOUR_0XX0(PC_BLACK ), MKCOLOUR_F00F}, // MP_ROAD
372 {MKCOLOUR_0XX0(PC_DARK_RED ), MKCOLOUR_F00F}, // MP_HOUSE
373 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_TREES
374 {MKCOLOUR_XXXX(PC_LIGHT_BLUE), MKCOLOUR_0000}, // MP_STATION
375 {MKCOLOUR_XXXX(PC_WATER ), MKCOLOUR_0000}, // MP_WATER
376 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_VOID
377 {MKCOLOUR_XXXX(PC_DARK_RED ), MKCOLOUR_0000}, // MP_INDUSTRY
378 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_TUNNELBRIDGE
379 {MKCOLOUR_0XX0(PC_DARK_RED ), MKCOLOUR_F00F}, // MP_OBJECT
380 {MKCOLOUR_0XX0(PC_GREY ), MKCOLOUR_F00F},
381 };
382
384 static const AndOr _smallmap_vehicles_andor[] = {
385 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_CLEAR
386 {MKCOLOUR_0XX0(PC_BLACK ), MKCOLOUR_F00F}, // MP_RAILWAY
387 {MKCOLOUR_0XX0(PC_BLACK ), MKCOLOUR_F00F}, // MP_ROAD
388 {MKCOLOUR_0XX0(PC_DARK_RED ), MKCOLOUR_F00F}, // MP_HOUSE
389 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_TREES
390 {MKCOLOUR_0XX0(PC_BLACK ), MKCOLOUR_F00F}, // MP_STATION
391 {MKCOLOUR_XXXX(PC_WATER ), MKCOLOUR_0000}, // MP_WATER
392 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_VOID
393 {MKCOLOUR_XXXX(PC_DARK_RED ), MKCOLOUR_0000}, // MP_INDUSTRY
394 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_TUNNELBRIDGE
395 {MKCOLOUR_0XX0(PC_DARK_RED ), MKCOLOUR_F00F}, // MP_OBJECT
396 {MKCOLOUR_0XX0(PC_BLACK ), MKCOLOUR_F00F},
397 };
398
400 static const byte _tiletype_importance[] = {
401 2, // MP_CLEAR
402 8, // MP_RAILWAY
403 7, // MP_ROAD
404 5, // MP_HOUSE
405 2, // MP_TREES
406 9, // MP_STATION
407 2, // MP_WATER
408 1, // MP_VOID
409 6, // MP_INDUSTRY
410 8, // MP_TUNNELBRIDGE
411 2, // MP_OBJECT
412 0,
413 };
414
415
416 static inline TileType GetEffectiveTileType(TileIndex tile)
417 {
418 TileType t = GetTileType(tile);
419
420 if (t == MP_TUNNELBRIDGE) {
421 TransportType tt = GetTunnelBridgeTransportType(tile);
422
423 switch (tt) {
424 case TRANSPORT_RAIL: t = MP_RAILWAY; break;
425 case TRANSPORT_ROAD: t = MP_ROAD; break;
426 default: t = MP_WATER; break;
427 }
428 }
429 return t;
430 }
431
438 static inline uint32 GetSmallMapContoursPixels(TileIndex tile, TileType t)
439 {
440 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
441 return ApplyMask(cs->height_colours[TileHeight(tile)], &_smallmap_contours_andor[t]);
442 }
443
451 static inline uint32 GetSmallMapVehiclesPixels(TileIndex tile, TileType t)
452 {
453 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
454 return ApplyMask(cs->default_colour, &_smallmap_vehicles_andor[t]);
455 }
456
464 static inline uint32 GetSmallMapIndustriesPixels(TileIndex tile, TileType t)
465 {
466 if (t == MP_INDUSTRY) {
467 /* If industry is allowed to be seen, use its colour on the map */
468 IndustryType type = Industry::GetByTile(tile)->type;
469 if (_legend_from_industries[_industry_to_list_pos[type]].show_on_map &&
470 (_smallmap_industry_highlight_state || type != _smallmap_industry_highlight)) {
471 return (type == _smallmap_industry_highlight ? PC_WHITE : GetIndustrySpec(Industry::GetByTile(tile)->type)->map_colour) * 0x01010101;
472 } else {
473 /* Otherwise, return the colour which will make it disappear */
474 t = (IsTileOnWater(tile) ? MP_WATER : MP_CLEAR);
475 }
476 }
477
478 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
479 return ApplyMask(_smallmap_show_heightmap ? cs->height_colours[TileHeight(tile)] : cs->default_colour, &_smallmap_vehicles_andor[t]);
480 }
481
489 static inline uint32 GetSmallMapRoutesPixels(TileIndex tile, TileType t)
490 {
491 if (t == MP_STATION) {
492 switch (GetStationType(tile)) {
493 case STATION_RAIL: return MKCOLOUR_XXXX(PC_VERY_DARK_BROWN);
494 case STATION_AIRPORT: return MKCOLOUR_XXXX(PC_RED);
495 case STATION_TRUCK: return MKCOLOUR_XXXX(PC_ORANGE);
496 case STATION_BUS: return MKCOLOUR_XXXX(PC_YELLOW);
497 case STATION_DOCK: return MKCOLOUR_XXXX(PC_LIGHT_BLUE);
498 default: return MKCOLOUR_FFFF;
499 }
500 } else if (t == MP_RAILWAY) {
501 AndOr andor = {
502 MKCOLOUR_0XX0(GetRailTypeInfo(GetRailType(tile))->map_colour),
503 _smallmap_contours_andor[t].mand
504 };
505
506 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
507 return ApplyMask(cs->default_colour, &andor);
508 }
509
510 /* Ground colour */
511 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
512 return ApplyMask(cs->default_colour, &_smallmap_contours_andor[t]);
513 }
514
522 static inline uint32 GetSmallMapLinkStatsPixels(TileIndex tile, TileType t)
523 {
524 return _smallmap_show_heightmap ? GetSmallMapContoursPixels(tile, t) : GetSmallMapRoutesPixels(tile, t);
525 }
526
527 static const uint32 _vegetation_clear_bits[] = {
528 MKCOLOUR_XXXX(PC_GRASS_LAND),
529 MKCOLOUR_XXXX(PC_ROUGH_LAND),
530 MKCOLOUR_XXXX(PC_GREY),
531 MKCOLOUR_XXXX(PC_FIELDS),
532 MKCOLOUR_XXXX(PC_LIGHT_BLUE),
533 MKCOLOUR_XXXX(PC_ORANGE),
534 MKCOLOUR_XXXX(PC_GRASS_LAND),
535 MKCOLOUR_XXXX(PC_GRASS_LAND),
536 };
537
545 static inline uint32 GetSmallMapVegetationPixels(TileIndex tile, TileType t)
546 {
547 switch (t) {
548 case MP_CLEAR:
549 return (IsClearGround(tile, CLEAR_GRASS) && GetClearDensity(tile) < 3) ? MKCOLOUR_XXXX(PC_BARE_LAND) : _vegetation_clear_bits[GetClearGround(tile)];
550
551 case MP_INDUSTRY:
552 return IsTileForestIndustry(tile) ? MKCOLOUR_XXXX(PC_GREEN) : MKCOLOUR_XXXX(PC_DARK_RED);
553
554 case MP_TREES:
555 if (GetTreeGround(tile) == TREE_GROUND_SNOW_DESERT || GetTreeGround(tile) == TREE_GROUND_ROUGH_SNOW) {
556 return (_settings_game.game_creation.landscape == LT_ARCTIC) ? MKCOLOUR_XYYX(PC_LIGHT_BLUE, PC_TREES) : MKCOLOUR_XYYX(PC_ORANGE, PC_TREES);
557 }
558 return MKCOLOUR_XYYX(PC_GRASS_LAND, PC_TREES);
559
560 default:
561 return ApplyMask(MKCOLOUR_XXXX(PC_GRASS_LAND), &_smallmap_vehicles_andor[t]);
562 }
563 }
564
572 static inline uint32 GetSmallMapOwnerPixels(TileIndex tile, TileType t)
573 {
574 Owner o;
575
576 switch (t) {
577 case MP_INDUSTRY: return MKCOLOUR_XXXX(PC_DARK_GREY);
578 case MP_HOUSE: return MKCOLOUR_XXXX(PC_DARK_RED);
579 default: o = GetTileOwner(tile); break;
580 /* FIXME: For MP_ROAD there are multiple owners.
581 * GetTileOwner returns the rail owner (level crossing) resp. the owner of ROADTYPE_ROAD (normal road),
582 * even if there are no ROADTYPE_ROAD bits on the tile.
583 */
584 }
585
586 if ((o < MAX_COMPANIES && !_legend_land_owners[_company_to_list_pos[o]].show_on_map) || o == OWNER_NONE || o == OWNER_WATER) {
587 if (t == MP_WATER) return MKCOLOUR_XXXX(PC_WATER);
588 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
589 return _smallmap_show_heightmap ? cs->height_colours[TileHeight(tile)] : cs->default_colour;
590 } else if (o == OWNER_TOWN) {
591 return MKCOLOUR_XXXX(PC_DARK_RED);
592 }
593
594 return MKCOLOUR_XXXX(_legend_land_owners[_company_to_list_pos[o]].colour);
595 }
596
598 static const byte _vehicle_type_colours[6] = {
599 PC_RED, PC_YELLOW, PC_LIGHT_BLUE, PC_WHITE, PC_BLACK, PC_RED
600 };
601
602
603 inline Point SmallMapWindow::SmallmapRemapCoords(int x, int y) const
604 {
605 Point pt;
606 pt.x = (y - x) * 2;
607 pt.y = y + x;
608 return pt;
609 }
610
617 inline Point SmallMapWindow::RemapTile(int tile_x, int tile_y) const
618 {
619 int x_offset = tile_x - this->scroll_x / (int)TILE_SIZE;
620 int y_offset = tile_y - this->scroll_y / (int)TILE_SIZE;
621
622 if (this->zoom == 1) return SmallmapRemapCoords(x_offset, y_offset);
623
624 /* For negative offsets, round towards -inf. */
625 if (x_offset < 0) x_offset -= this->zoom - 1;
626 if (y_offset < 0) y_offset -= this->zoom - 1;
627
628 return SmallmapRemapCoords(x_offset / this->zoom, y_offset / this->zoom);
629 }
630
641 inline Point SmallMapWindow::PixelToTile(int px, int py, int *sub, bool add_sub) const
642 {
643 if (add_sub) px += this->subscroll; // Total horizontal offset.
644
645 /* For each two rows down, add a x and a y tile, and
646 * For each four pixels to the right, move a tile to the right. */
647 Point pt = {((py >> 1) - (px >> 2)) * this->zoom, ((py >> 1) + (px >> 2)) * this->zoom};
648 px &= 3;
649
650 if (py & 1) { // Odd number of rows, handle the 2 pixel shift.
651 if (px < 2) {
652 pt.x += this->zoom;
653 px += 2;
654 } else {
655 pt.y += this->zoom;
656 px -= 2;
657 }
658 }
659
660 *sub = px;
661 return pt;
662 }
663
673 Point SmallMapWindow::ComputeScroll(int tx, int ty, int x, int y, int *sub)
674 {
675 assert(x >= 0 && y >= 0);
676
677 int new_sub;
678 Point tile_xy = PixelToTile(x, y, &new_sub, false);
679 tx -= tile_xy.x;
680 ty -= tile_xy.y;
681
682 Point scroll;
683 if (new_sub == 0) {
684 *sub = 0;
685 scroll.x = (tx + this->zoom) * TILE_SIZE;
686 scroll.y = (ty - this->zoom) * TILE_SIZE;
687 } else {
688 *sub = 4 - new_sub;
689 scroll.x = (tx + 2 * this->zoom) * TILE_SIZE;
690 scroll.y = (ty - 2 * this->zoom) * TILE_SIZE;
691 }
692 return scroll;
693 }
694
701 void SmallMapWindow::SetZoomLevel(ZoomLevelChange change, const Point *zoom_pt)
702 {
703 static const int zoomlevels[] = {1, 2, 4, 6, 8}; // Available zoom levels. Bigger number means more zoom-out (further away).
704 static const int MIN_ZOOM_INDEX = 0;
705 static const int MAX_ZOOM_INDEX = lengthof(zoomlevels) - 1;
706
707 int new_index, cur_index, sub;
708 Point tile;
709 switch (change) {
710 case ZLC_INITIALIZE:
711 cur_index = - 1; // Definitely different from new_index.
712 new_index = MIN_ZOOM_INDEX;
713 tile.x = tile.y = 0;
714 break;
715
716 case ZLC_ZOOM_IN:
717 case ZLC_ZOOM_OUT:
718 for (cur_index = MIN_ZOOM_INDEX; cur_index <= MAX_ZOOM_INDEX; cur_index++) {
719 if (this->zoom == zoomlevels[cur_index]) break;
720 }
721 assert(cur_index <= MAX_ZOOM_INDEX);
722
723 tile = this->PixelToTile(zoom_pt->x, zoom_pt->y, &sub);
724 new_index = Clamp(cur_index + ((change == ZLC_ZOOM_IN) ? -1 : 1), MIN_ZOOM_INDEX, MAX_ZOOM_INDEX);
725 break;
726
727 default: NOT_REACHED();
728 }
729
730 if (new_index != cur_index) {
731 this->zoom = zoomlevels[new_index];
732 if (cur_index >= 0) {
733 Point new_tile = this->PixelToTile(zoom_pt->x, zoom_pt->y, &sub);
734 this->SetNewScroll(this->scroll_x + (tile.x - new_tile.x) * TILE_SIZE,
735 this->scroll_y + (tile.y - new_tile.y) * TILE_SIZE, sub);
736 } else if (this->map_type == SMT_LINKSTATS) {
737 this->overlay->RebuildCache();
738 }
739 this->SetWidgetDisabledState(WID_SM_ZOOM_IN, this->zoom == zoomlevels[MIN_ZOOM_INDEX]);
740 this->SetWidgetDisabledState(WID_SM_ZOOM_OUT, this->zoom == zoomlevels[MAX_ZOOM_INDEX]);
741 this->SetDirty();
742 }
743 }
744
750 inline uint32 SmallMapWindow::GetTileColours(const TileArea &ta) const
751 {
752 int importance = 0;
753 TileIndex tile = INVALID_TILE; // Position of the most important tile.
754 TileType et = MP_VOID; // Effective tile type at that position.
755
756 TILE_AREA_LOOP(ti, ta) {
757 TileType ttype = GetEffectiveTileType(ti);
758 if (_tiletype_importance[ttype] > importance) {
759 importance = _tiletype_importance[ttype];
760 tile = ti;
761 et = ttype;
762 }
763 }
764
765 switch (this->map_type) {
766 case SMT_CONTOUR:
767 return GetSmallMapContoursPixels(tile, et);
768
769 case SMT_VEHICLES:
770 return GetSmallMapVehiclesPixels(tile, et);
771
772 case SMT_INDUSTRY:
773 return GetSmallMapIndustriesPixels(tile, et);
774
775 case SMT_LINKSTATS:
776 return GetSmallMapLinkStatsPixels(tile, et);
777
778 case SMT_ROUTES:
779 return GetSmallMapRoutesPixels(tile, et);
780
781 case SMT_VEGETATION:
782 return GetSmallMapVegetationPixels(tile, et);
783
784 case SMT_OWNER:
785 return GetSmallMapOwnerPixels(tile, et);
786
787 default: NOT_REACHED();
788 }
789 }
790
804 void SmallMapWindow::DrawSmallMapColumn(void *dst, uint xc, uint yc, int pitch, int reps, int start_pos, int end_pos, Blitter *blitter) const
805 {
806 void *dst_ptr_abs_end = blitter->MoveTo(_screen.dst_ptr, 0, _screen.height);
807 uint min_xy = _settings_game.construction.freeform_edges ? 1 : 0;
808
809 do {
810 /* Check if the tile (xc,yc) is within the map range */
811 if (xc >= MapMaxX() || yc >= MapMaxY()) continue;
812
813 /* Check if the dst pointer points to a pixel inside the screen buffer */
814 if (dst < _screen.dst_ptr) continue;
815 if (dst >= dst_ptr_abs_end) continue;
816
817 /* Construct tilearea covered by (xc, yc, xc + this->zoom, yc + this->zoom) such that it is within min_xy limits. */
818 TileArea ta;
819 if (min_xy == 1 && (xc == 0 || yc == 0)) {
820 if (this->zoom == 1) continue; // The tile area is empty, don't draw anything.
821
822 ta = TileArea(TileXY(max(min_xy, xc), max(min_xy, yc)), this->zoom - (xc == 0), this->zoom - (yc == 0));
823 } else {
824 ta = TileArea(TileXY(xc, yc), this->zoom, this->zoom);
825 }
826 ta.ClampToMap(); // Clamp to map boundaries (may contain MP_VOID tiles!).
827
828 uint32 val = this->GetTileColours(ta);
829 uint8 *val8 = (uint8 *)&val;
830 int idx = max(0, -start_pos);
831 for (int pos = max(0, start_pos); pos < end_pos; pos++) {
832 blitter->SetPixel(dst, idx, 0, val8[idx]);
833 idx++;
834 }
835 /* Switch to next tile in the column */
836 } while (xc += this->zoom, yc += this->zoom, dst = blitter->MoveTo(dst, pitch, 0), --reps != 0);
837 }
838
844 void SmallMapWindow::DrawVehicles(const DrawPixelInfo *dpi, Blitter *blitter) const
845 {
846 const Vehicle *v;
847 FOR_ALL_VEHICLES(v) {
848 if (v->type == VEH_EFFECT) continue;
849 if (v->vehstatus & (VS_HIDDEN | VS_UNCLICKABLE)) continue;
850
851 /* Remap into flat coordinates. */
852 Point pt = this->RemapTile(v->x_pos / (int)TILE_SIZE, v->y_pos / (int)TILE_SIZE);
853
854 int y = pt.y - dpi->top;
855 if (!IsInsideMM(y, 0, dpi->height)) continue; // y is out of bounds.
856
857 bool skip = false; // Default is to draw both pixels.
858 int x = pt.x - this->subscroll - 3 - dpi->left; // Offset X coordinate.
859 if (x < 0) {
860 /* if x+1 is 0, that means we're on the very left edge,
861 * and should thus only draw a single pixel */
862 if (++x != 0) continue;
863 skip = true;
864 } else if (x >= dpi->width - 1) {
865 /* Check if we're at the very right edge, and if so draw only a single pixel */
866 if (x != dpi->width - 1) continue;
867 skip = true;
868 }
869
870 /* Calculate pointer to pixel and the colour */
871 byte colour = (this->map_type == SMT_VEHICLES) ? _vehicle_type_colours[v->type] : PC_WHITE;
872
873 /* And draw either one or two pixels depending on clipping */
874 blitter->SetPixel(dpi->dst_ptr, x, y, colour);
875 if (!skip) blitter->SetPixel(dpi->dst_ptr, x + 1, y, colour);
876 }
877 }
878
883 void SmallMapWindow::DrawTowns(const DrawPixelInfo *dpi) const
884 {
885 const Town *t;
886 FOR_ALL_TOWNS(t) {
887 /* Remap the town coordinate */
888 Point pt = this->RemapTile(TileX(t->xy), TileY(t->xy));
889 int x = pt.x - this->subscroll - (t->cache.sign.width_small >> 1);
890 int y = pt.y;
891
892 /* Check if the town sign is within bounds */
893 if (x + t->cache.sign.width_small > dpi->left &&
894 x < dpi->left + dpi->width &&
895 y + FONT_HEIGHT_SMALL > dpi->top &&
896 y < dpi->top + dpi->height) {
897 /* And draw it. */
898 SetDParam(0, t->index);
899 DrawString(x, x + t->cache.sign.width_small, y, STR_SMALLMAP_TOWN);
900 }
901 }
902 }
903
907 void SmallMapWindow::DrawMapIndicators() const
908 {
909 /* Find main viewport. */
910 const ViewPort *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
911
912 Point upper_left_smallmap_coord = TranslateXYToTileCoord(vp, vp->left, vp->top, false);
913 Point lower_right_smallmap_coord = TranslateXYToTileCoord(vp, vp->left + vp->width - 1, vp->top + vp->height - 1, false);
914
915 Point upper_left = this->RemapTile(upper_left_smallmap_coord.x / (int)TILE_SIZE, upper_left_smallmap_coord.y / (int)TILE_SIZE);
916 upper_left.x -= this->subscroll;
917
918 Point lower_right = this->RemapTile(lower_right_smallmap_coord.x / (int)TILE_SIZE, lower_right_smallmap_coord.y / (int)TILE_SIZE);
919 lower_right.x -= this->subscroll;
920
921 SmallMapWindow::DrawVertMapIndicator(upper_left.x, upper_left.y, lower_right.y);
922 SmallMapWindow::DrawVertMapIndicator(lower_right.x, upper_left.y, lower_right.y);
923
924 SmallMapWindow::DrawHorizMapIndicator(upper_left.x, lower_right.x, upper_left.y);
925 SmallMapWindow::DrawHorizMapIndicator(upper_left.x, lower_right.x, lower_right.y);
926 }
927
939 void SmallMapWindow::DrawSmallMap(DrawPixelInfo *dpi) const
940 {
941 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
942 DrawPixelInfo *old_dpi;
943
944 old_dpi = _cur_dpi;
945 _cur_dpi = dpi;
946
947 /* Clear it */
948 GfxFillRect(dpi->left, dpi->top, dpi->left + dpi->width - 1, dpi->top + dpi->height - 1, PC_BLACK);
949
950 /* Which tile is displayed at (dpi->left, dpi->top)? */
951 int dx;
952 Point tile = this->PixelToTile(dpi->left, dpi->top, &dx);
953 int tile_x = this->scroll_x / (int)TILE_SIZE + tile.x;
954 int tile_y = this->scroll_y / (int)TILE_SIZE + tile.y;
955
956 void *ptr = blitter->MoveTo(dpi->dst_ptr, -dx - 4, 0);
957 int x = - dx - 4;
958 int y = 0;
959
960 for (;;) {
961 /* Distance from left edge */
962 if (x >= -3) {
963 if (x >= dpi->width) break; // Exit the loop.
964
965 int end_pos = min(dpi->width, x + 4);
966 int reps = (dpi->height - y + 1) / 2; // Number of lines.
967 if (reps > 0) {
968 this->DrawSmallMapColumn(ptr, tile_x, tile_y, dpi->pitch * 2, reps, x, end_pos, blitter);
969 }
970 }
971
972 if (y == 0) {
973 tile_y += this->zoom;
974 y++;
975 ptr = blitter->MoveTo(ptr, 0, 1);
976 } else {
977 tile_x -= this->zoom;
978 y--;
979 ptr = blitter->MoveTo(ptr, 0, -1);
980 }
981 ptr = blitter->MoveTo(ptr, 2, 0);
982 x += 2;
983 }
984
985 /* Draw vehicles */
986 if (this->map_type == SMT_CONTOUR || this->map_type == SMT_VEHICLES) this->DrawVehicles(dpi, blitter);
987
988 /* Draw link stat overlay */
989 if (this->map_type == SMT_LINKSTATS) this->overlay->Draw(dpi);
990
991 /* Draw town names */
992 if (this->show_towns) this->DrawTowns(dpi);
993
994 /* Draw map indicators */
995 this->DrawMapIndicators();
996
997 _cur_dpi = old_dpi;
998 }
999
1003 void SmallMapWindow::SetupWidgetData()
1004 {
1005 StringID legend_tooltip;
1006 StringID enable_all_tooltip;
1007 StringID disable_all_tooltip;
1008 int plane;
1009 switch (this->map_type) {
1010 case SMT_INDUSTRY:
1011 legend_tooltip = STR_SMALLMAP_TOOLTIP_INDUSTRY_SELECTION;
1012 enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_INDUSTRIES;
1013 disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_INDUSTRIES;
1014 plane = 0;
1015 break;
1016
1017 case SMT_OWNER:
1018 legend_tooltip = STR_SMALLMAP_TOOLTIP_COMPANY_SELECTION;
1019 enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_COMPANIES;
1020 disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_COMPANIES;
1021 plane = 0;
1022 break;
1023
1024 case SMT_LINKSTATS:
1025 legend_tooltip = STR_SMALLMAP_TOOLTIP_CARGO_SELECTION;
1026 enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_CARGOS;
1027 disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_CARGOS;
1028 plane = 0;
1029 break;
1030
1031 default:
1032 legend_tooltip = STR_NULL;
1033 enable_all_tooltip = STR_NULL;
1034 disable_all_tooltip = STR_NULL;
1035 plane = 1;
1036 break;
1037 }
1038
1039 this->GetWidget<NWidgetCore>(WID_SM_LEGEND)->SetDataTip(STR_NULL, legend_tooltip);
1040 this->GetWidget<NWidgetCore>(WID_SM_ENABLE_ALL)->SetDataTip(STR_SMALLMAP_ENABLE_ALL, enable_all_tooltip);
1041 this->GetWidget<NWidgetCore>(WID_SM_DISABLE_ALL)->SetDataTip(STR_SMALLMAP_DISABLE_ALL, disable_all_tooltip);
1042 this->GetWidget<NWidgetStacked>(WID_SM_SELECT_BUTTONS)->SetDisplayedPlane(plane);
1043 }
1044
1045 SmallMapWindow::SmallMapWindow(WindowDesc *desc, int window_number) : Window(desc), refresh(FORCE_REFRESH_PERIOD)
1046 {
1047 _smallmap_industry_highlight = INVALID_INDUSTRYTYPE;
1048 this->overlay = new LinkGraphOverlay(this, WID_SM_MAP, 0, this->GetOverlayCompanyMask(), 1);
1049 this->InitNested(window_number);
1050 this->LowerWidget(this->map_type + WID_SM_CONTOUR);
1051
1052 this->RebuildColourIndexIfNecessary();
1053
1054 this->SetWidgetLoweredState(WID_SM_SHOW_HEIGHT, _smallmap_show_heightmap);
1055
1056 this->SetWidgetLoweredState(WID_SM_TOGGLETOWNNAME, this->show_towns);
1057
1058 this->SetupWidgetData();
1059
1060 this->SetZoomLevel(ZLC_INITIALIZE, NULL);
1061 this->SmallMapCenterOnCurrentPos();
1062 this->SetOverlayCargoMask();
1063 }
1064
1068 void SmallMapWindow::RebuildColourIndexIfNecessary()
1069 {
1070 /* Rebuild colour indices if necessary. */
1071 if (SmallMapWindow::max_heightlevel == _settings_game.construction.max_heightlevel) return;
1072
1073 for (uint n = 0; n < lengthof(_heightmap_schemes); n++) {
1074 /* The heights go from 0 up to and including maximum. */
1075 int heights = _settings_game.construction.max_heightlevel + 1;
1076 _heightmap_schemes[n].height_colours = ReallocT<uint32>(_heightmap_schemes[n].height_colours, heights);
1077
1078 for (int z = 0; z < heights; z++) {
1079 size_t access_index = (_heightmap_schemes[n].colour_count * z) / heights;
1080
1081 /* Choose colour by mapping the range (0..max heightlevel) on the complete colour table. */
1082 _heightmap_schemes[n].height_colours[z] = _heightmap_schemes[n].height_colours_base[access_index];
1083 }
1084 }
1085
1086 SmallMapWindow::max_heightlevel = _settings_game.construction.max_heightlevel;
1087 BuildLandLegend();
1088 }
1089
1090 /* virtual */ void SmallMapWindow::SetStringParameters(int widget) const
1091 {
1092 switch (widget) {
1093 case WID_SM_CAPTION:
1094 SetDParam(0, STR_SMALLMAP_TYPE_CONTOURS + this->map_type);
1095 break;
1096 }
1097 }
1098
1099 /* virtual */ void SmallMapWindow::OnInit()
1100 {
1101 uint min_width = 0;
1102 this->min_number_of_columns = INDUSTRY_MIN_NUMBER_OF_COLUMNS;
1103 this->min_number_of_fixed_rows = lengthof(_linkstat_colours_in_legenda);
1104 for (uint i = 0; i < lengthof(_legend_table); i++) {
1105 uint height = 0;
1106 uint num_columns = 1;
1107 for (const LegendAndColour *tbl = _legend_table[i]; !tbl->end; ++tbl) {
1108 StringID str;
1109 if (i == SMT_INDUSTRY) {
1110 SetDParam(0, tbl->legend);
1111 SetDParam(1, IndustryPool::MAX_SIZE);
1112 str = STR_SMALLMAP_INDUSTRY;
1113 } else if (i == SMT_LINKSTATS) {
1114 SetDParam(0, tbl->legend);
1115 str = STR_SMALLMAP_LINKSTATS;
1116 } else if (i == SMT_OWNER) {
1117 if (tbl->company != INVALID_COMPANY) {
1118 if (!Company::IsValidID(tbl->company)) {
1119 /* Rebuild the owner legend. */
1120 BuildOwnerLegend();
1121 this->OnInit();
1122 return;
1123 }
1124 /* Non-fixed legend entries for the owner view. */
1125 SetDParam(0, tbl->company);
1126 str = STR_SMALLMAP_COMPANY;
1127 } else {
1128 str = tbl->legend;
1129 }
1130 } else {
1131 if (tbl->col_break) {
1132 this->min_number_of_fixed_rows = max(this->min_number_of_fixed_rows, height);
1133 height = 0;
1134 num_columns++;
1135 }
1136 height++;
1137 str = tbl->legend;
1138 }
1139 min_width = max(GetStringBoundingBox(str).width, min_width);
1140 }
1141 this->min_number_of_fixed_rows = max(this->min_number_of_fixed_rows, height);
1142 this->min_number_of_columns = max(this->min_number_of_columns, num_columns);
1143 }
1144
1145 /* The width of a column is the minimum width of all texts + the size of the blob + some spacing */
1146 this->column_width = min_width + LEGEND_BLOB_WIDTH + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
1147 }
1148
1149 /* virtual */ void SmallMapWindow::OnPaint()
1150 {
1151 if (this->map_type == SMT_OWNER) {
1152 for (const LegendAndColour *tbl = _legend_table[this->map_type]; !tbl->end; ++tbl) {
1153 if (tbl->company != INVALID_COMPANY && !Company::IsValidID(tbl->company)) {
1154 /* Rebuild the owner legend. */
1155 BuildOwnerLegend();
1156 this->InvalidateData(1);
1157 break;
1158 }
1159 }
1160 }
1161
1162 this->DrawWidgets();
1163 }
1164
1165 /* virtual */ void SmallMapWindow::DrawWidget(const Rect &r, int widget) const
1166 {
1167 switch (widget) {
1168 case WID_SM_MAP: {
1169 DrawPixelInfo new_dpi;
1170 if (!FillDrawPixelInfo(&new_dpi, r.left + 1, r.top + 1, r.right - r.left - 1, r.bottom - r.top - 1)) return;
1171 this->DrawSmallMap(&new_dpi);
1172 break;
1173 }
1174
1175 case WID_SM_LEGEND: {
1176 uint columns = this->GetNumberColumnsLegend(r.right - r.left + 1);
1177 uint number_of_rows = this->GetNumberRowsLegend(columns);
1178 bool rtl = _current_text_dir == TD_RTL;
1179 uint y_org = r.top + WD_FRAMERECT_TOP;
1180 uint x = rtl ? r.right - this->column_width - WD_FRAMERECT_RIGHT : r.left + WD_FRAMERECT_LEFT;
1181 uint y = y_org;
1182 uint i = 0; // Row counter for industry legend.
1183 uint row_height = FONT_HEIGHT_SMALL;
1184
1185 uint text_left = rtl ? 0 : LEGEND_BLOB_WIDTH + WD_FRAMERECT_LEFT;
1186 uint text_right = this->column_width - 1 - (rtl ? LEGEND_BLOB_WIDTH + WD_FRAMERECT_RIGHT : 0);
1187 uint blob_left = rtl ? this->column_width - 1 - LEGEND_BLOB_WIDTH : 0;
1188 uint blob_right = rtl ? this->column_width - 1 : LEGEND_BLOB_WIDTH;
1189
1190 StringID string = STR_NULL;
1191 switch (this->map_type) {
1192 case SMT_INDUSTRY:
1193 string = STR_SMALLMAP_INDUSTRY;
1194 break;
1195 case SMT_LINKSTATS:
1196 string = STR_SMALLMAP_LINKSTATS;
1197 break;
1198 case SMT_OWNER:
1199 string = STR_SMALLMAP_COMPANY;
1200 break;
1201 default:
1202 break;
1203 }
1204
1205 for (const LegendAndColour *tbl = _legend_table[this->map_type]; !tbl->end; ++tbl) {
1206 if (tbl->col_break || ((this->map_type == SMT_INDUSTRY || this->map_type == SMT_OWNER || this->map_type == SMT_LINKSTATS) && i++ >= number_of_rows)) {
1207 /* Column break needed, continue at top, COLUMN_WIDTH pixels
1208 * (one "row") to the right. */
1209 x += rtl ? -(int)this->column_width : this->column_width;
1210 y = y_org;
1211 i = 1;
1212 }
1213
1214 uint8 legend_colour = tbl->colour;
1215
1216 switch (this->map_type) {
1217 case SMT_INDUSTRY:
1218 /* Industry name must be formatted, since it's not in tiny font in the specs.
1219 * So, draw with a parameter and use the STR_SMALLMAP_INDUSTRY string, which is tiny font */
1220 SetDParam(0, tbl->legend);
1221 SetDParam(1, Industry::GetIndustryTypeCount(tbl->type));
1222 if (tbl->show_on_map && tbl->type == _smallmap_industry_highlight) {
1223 legend_colour = _smallmap_industry_highlight_state ? PC_WHITE : PC_BLACK;
1224 }
1225 /* FALL THROUGH */
1226 case SMT_LINKSTATS:
1227 SetDParam(0, tbl->legend);
1228 /* FALL_THROUGH */
1229 case SMT_OWNER:
1230 if (this->map_type != SMT_OWNER || tbl->company != INVALID_COMPANY) {
1231 if (this->map_type == SMT_OWNER) SetDParam(0, tbl->company);
1232 if (!tbl->show_on_map) {
1233 /* Simply draw the string, not the black border of the legend colour.
1234 * This will enforce the idea of the disabled item */
1235 DrawString(x + text_left, x + text_right, y, string, TC_GREY);
1236 } else {
1237 DrawString(x + text_left, x + text_right, y, string, TC_BLACK);
1238 GfxFillRect(x + blob_left, y + 1, x + blob_right, y + row_height - 1, PC_BLACK); // Outer border of the legend colour
1239 }
1240 break;
1241 }
1242 /* FALL_THROUGH */
1243 default:
1244 if (this->map_type == SMT_CONTOUR) SetDParam(0, tbl->height * TILE_HEIGHT_STEP);
1245 /* Anything that is not an industry or a company is using normal process */
1246 GfxFillRect(x + blob_left, y + 1, x + blob_right, y + row_height - 1, PC_BLACK);
1247 DrawString(x + text_left, x + text_right, y, tbl->legend);
1248 break;
1249 }
1250 GfxFillRect(x + blob_left + 1, y + 2, x + blob_right - 1, y + row_height - 2, legend_colour); // Legend colour
1251
1252 y += row_height;
1253 }
1254 }
1255 }
1256 }
1257
1262 void SmallMapWindow::SwitchMapType(SmallMapType map_type)
1263 {
1264 this->RaiseWidget(this->map_type + WID_SM_CONTOUR);
1265 this->map_type = map_type;
1266 this->LowerWidget(this->map_type + WID_SM_CONTOUR);
1267
1268 this->SetupWidgetData();
1269
1270 if (map_type == SMT_LINKSTATS) this->overlay->RebuildCache();
1271 this->SetDirty();
1272 }
1273
1282 inline uint SmallMapWindow::GetNumberRowsLegend(uint columns) const
1283 {
1284 /* Reserve one column for link colours */
1285 uint num_rows_linkstats = CeilDiv(_smallmap_cargo_count, columns - 1);
1286 uint num_rows_others = CeilDiv(max(_smallmap_industry_count, _smallmap_company_count), columns);
1287 return max(this->min_number_of_fixed_rows, max(num_rows_linkstats, num_rows_others));
1288 }
1289
1301 void SmallMapWindow::SelectLegendItem(int click_pos, LegendAndColour *legend, int end_legend_item, int begin_legend_item)
1302 {
1303 if (_ctrl_pressed) {
1304 /* Disable all, except the clicked one */
1305 bool changes = false;
1306 for (int i = begin_legend_item; i != end_legend_item; i++) {
1307 bool new_state = (i == click_pos);
1308 if (legend[i].show_on_map != new_state) {
1309 changes = true;
1310 legend[i].show_on_map = new_state;
1311 }
1312 }
1313 if (!changes) {
1314 /* Nothing changed? Then show all (again). */
1315 for (int i = begin_legend_item; i != end_legend_item; i++) {
1316 legend[i].show_on_map = true;
1317 }
1318 }
1319 } else {
1320 legend[click_pos].show_on_map = !legend[click_pos].show_on_map;
1321 }
1322 }
1323
1327 void SmallMapWindow::SetOverlayCargoMask()
1328 {
1329 uint32 cargo_mask = 0;
1330 for (int i = 0; i != _smallmap_cargo_count; ++i) {
1331 if (_legend_linkstats[i].show_on_map) SetBit(cargo_mask, _legend_linkstats[i].type);
1332 }
1333 this->overlay->SetCargoMask(cargo_mask);
1334 }
1335
1341 int SmallMapWindow::GetPositionOnLegend(Point pt)
1342 {
1343 const NWidgetBase *wi = this->GetWidget<NWidgetBase>(WID_SM_LEGEND);
1344 uint line = (pt.y - wi->pos_y - WD_FRAMERECT_TOP) / FONT_HEIGHT_SMALL;
1345 uint columns = this->GetNumberColumnsLegend(wi->current_x);
1346 uint number_of_rows = this->GetNumberRowsLegend(columns);
1347 if (line >= number_of_rows) return -1;
1348
1349 bool rtl = _current_text_dir == TD_RTL;
1350 int x = pt.x - wi->pos_x;
1351 if (rtl) x = wi->current_x - x;
1352 uint column = (x - WD_FRAMERECT_LEFT) / this->column_width;
1353
1354 return (column * number_of_rows) + line;
1355 }
1356
1357 /* virtual */ void SmallMapWindow::OnMouseOver(Point pt, int widget)
1358 {
1359 IndustryType new_highlight = INVALID_INDUSTRYTYPE;
1360 if (widget == WID_SM_LEGEND && this->map_type == SMT_INDUSTRY) {
1361 int industry_pos = GetPositionOnLegend(pt);
1362 if (industry_pos >= 0 && industry_pos < _smallmap_industry_count) {
1363 new_highlight = _legend_from_industries[industry_pos].type;
1364 }
1365 }
1366 if (new_highlight != _smallmap_industry_highlight) {
1367 _smallmap_industry_highlight = new_highlight;
1368 this->refresh = _smallmap_industry_highlight != INVALID_INDUSTRYTYPE ? BLINK_PERIOD : FORCE_REFRESH_PERIOD;
1369 _smallmap_industry_highlight_state = true;
1370 this->SetDirty();
1371 }
1372 }
1373
1374 /* virtual */ void SmallMapWindow::OnClick(Point pt, int widget, int click_count)
1375 {
1376 /* User clicked something, notify the industry chain window to stop sending newly selected industries. */
1377 InvalidateWindowClassesData(WC_INDUSTRY_CARGOES, NUM_INDUSTRYTYPES);
1378
1379 switch (widget) {
1380 case WID_SM_MAP: { // Map window
1381 /*
1382 * XXX: scrolling with the left mouse button is done by subsequently
1383 * clicking with the left mouse button; clicking once centers the
1384 * large map at the selected point. So by unclicking the left mouse
1385 * button here, it gets reclicked during the next inputloop, which
1386 * would make it look like the mouse is being dragged, while it is
1387 * actually being (virtually) clicked every inputloop.
1388 */
1389 _left_button_clicked = false;
1390
1391 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1392 Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
1393 int sub;
1394 pt = this->PixelToTile(pt.x - wid->pos_x, pt.y - wid->pos_y, &sub);
1395 ScrollWindowTo(this->scroll_x + pt.x * TILE_SIZE, this->scroll_y + pt.y * TILE_SIZE, -1, w);
1396
1397 this->SetDirty();
1398 break;
1399 }
1400
1401 case WID_SM_ZOOM_IN:
1402 case WID_SM_ZOOM_OUT: {
1403 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1404 Point pt = { (int)wid->current_x / 2, (int)wid->current_y / 2};
1405 this->SetZoomLevel((widget == WID_SM_ZOOM_IN) ? ZLC_ZOOM_IN : ZLC_ZOOM_OUT, &pt);
1406 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1407 break;
1408 }
1409
1410 case WID_SM_CONTOUR: // Show land contours
1411 case WID_SM_VEHICLES: // Show vehicles
1412 case WID_SM_INDUSTRIES: // Show industries
1413 case WID_SM_LINKSTATS: // Show route map
1414 case WID_SM_ROUTES: // Show transport routes
1415 case WID_SM_VEGETATION: // Show vegetation
1416 case WID_SM_OWNERS: // Show land owners
1417 this->SwitchMapType((SmallMapType)(widget - WID_SM_CONTOUR));
1418 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1419 break;
1420
1421 case WID_SM_CENTERMAP: // Center the smallmap again
1422 this->SmallMapCenterOnCurrentPos();
1423 this->HandleButtonClick(WID_SM_CENTERMAP);
1424 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1425 break;
1426
1427 case WID_SM_TOGGLETOWNNAME: // Toggle town names
1428 this->show_towns = !this->show_towns;
1429 this->SetWidgetLoweredState(WID_SM_TOGGLETOWNNAME, this->show_towns);
1430
1431 this->SetDirty();
1432 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1433 break;
1434
1435 case WID_SM_LEGEND: // Legend
1436 if (this->map_type == SMT_INDUSTRY || this->map_type == SMT_LINKSTATS || this->map_type == SMT_OWNER) {
1437 int click_pos = this->GetPositionOnLegend(pt);
1438 if (click_pos < 0) break;
1439
1440 /* If industry type small map*/
1441 if (this->map_type == SMT_INDUSTRY) {
1442 /* If click on industries label, find right industry type and enable/disable it. */
1443 if (click_pos < _smallmap_industry_count) {
1444 this->SelectLegendItem(click_pos, _legend_from_industries, _smallmap_industry_count);
1445 }
1446 } else if (this->map_type == SMT_LINKSTATS) {
1447 if (click_pos < _smallmap_cargo_count) {
1448 this->SelectLegendItem(click_pos, _legend_linkstats, _smallmap_cargo_count);
1449 this->SetOverlayCargoMask();
1450 }
1451 } else if (this->map_type == SMT_OWNER) {
1452 if (click_pos < _smallmap_company_count) {
1453 this->SelectLegendItem(click_pos, _legend_land_owners, _smallmap_company_count, NUM_NO_COMPANY_ENTRIES);
1454 }
1455 }
1456 this->SetDirty();
1457 }
1458 break;
1459
1460 case WID_SM_ENABLE_ALL:
1461 /* FALL THROUGH */
1462 case WID_SM_DISABLE_ALL: {
1463 LegendAndColour *tbl = NULL;
1464 switch (this->map_type) {
1465 case SMT_INDUSTRY:
1466 tbl = _legend_from_industries;
1467 break;
1468 case SMT_OWNER:
1469 tbl = &(_legend_land_owners[NUM_NO_COMPANY_ENTRIES]);
1470 break;
1471 case SMT_LINKSTATS:
1472 tbl = _legend_linkstats;
1473 break;
1474 default:
1475 NOT_REACHED();
1476 }
1477 for (;!tbl->end && tbl->legend != STR_LINKGRAPH_LEGEND_UNUSED; ++tbl) {
1478 tbl->show_on_map = (widget == WID_SM_ENABLE_ALL);
1479 }
1480 if (this->map_type == SMT_LINKSTATS) this->SetOverlayCargoMask();
1481 this->SetDirty();
1482 break;
1483 }
1484
1485 case WID_SM_SHOW_HEIGHT: // Enable/disable showing of heightmap.
1486 _smallmap_show_heightmap = !_smallmap_show_heightmap;
1487 this->SetWidgetLoweredState(WID_SM_SHOW_HEIGHT, _smallmap_show_heightmap);
1488 this->SetDirty();
1489 break;
1490 }
1491 }
1492
1501 /* virtual */ void SmallMapWindow::OnInvalidateData(int data, bool gui_scope)
1502 {
1503 if (!gui_scope) return;
1504
1505 switch (data) {
1506 case 1:
1507 /* The owner legend has already been rebuilt. */
1508 this->ReInit();
1509 break;
1510
1511 case 0: {
1512 extern std::bitset<NUM_INDUSTRYTYPES> _displayed_industries;
1513 if (this->map_type != SMT_INDUSTRY) this->SwitchMapType(SMT_INDUSTRY);
1514
1515 for (int i = 0; i != _smallmap_industry_count; i++) {
1516 _legend_from_industries[i].show_on_map = _displayed_industries.test(_legend_from_industries[i].type);
1517 }
1518 break;
1519 }
1520
1521 case 2:
1522 this->RebuildColourIndexIfNecessary();
1523 break;
1524
1525 default: NOT_REACHED();
1526 }
1527 this->SetDirty();
1528 }
1529
1530 /* virtual */ bool SmallMapWindow::OnRightClick(Point pt, int widget)
1531 {
1532 if (widget != WID_SM_MAP || _scrolling_viewport) return false;
1533
1534 _scrolling_viewport = true;
1535 return true;
1536 }
1537
1538 /* virtual */ void SmallMapWindow::OnMouseWheel(int wheel)
1539 {
1540 if (_settings_client.gui.scrollwheel_scrolling == 0) {
1541 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1542 int cursor_x = _cursor.pos.x - this->left - wid->pos_x;
1543 int cursor_y = _cursor.pos.y - this->top - wid->pos_y;
1544 if (IsInsideMM(cursor_x, 0, wid->current_x) && IsInsideMM(cursor_y, 0, wid->current_y)) {
1545 Point pt = {cursor_x, cursor_y};
1546 this->SetZoomLevel((wheel < 0) ? ZLC_ZOOM_IN : ZLC_ZOOM_OUT, &pt);
1547 }
1548 }
1549 }
1550
1551 /* virtual */ void SmallMapWindow::OnTick()
1552 {
1553 /* Update the window every now and then */
1554 if (--this->refresh != 0) return;
1555
1556 if (this->map_type == SMT_LINKSTATS) {
1557 uint32 company_mask = this->GetOverlayCompanyMask();
1558 if (this->overlay->GetCompanyMask() != company_mask) {
1559 this->overlay->SetCompanyMask(company_mask);
1560 } else {
1561 this->overlay->RebuildCache();
1562 }
1563 }
1564 _smallmap_industry_highlight_state = !_smallmap_industry_highlight_state;
1565
1566 this->refresh = _smallmap_industry_highlight != INVALID_INDUSTRYTYPE ? BLINK_PERIOD : FORCE_REFRESH_PERIOD;
1567 this->SetDirty();
1568 }
1569
1577 void SmallMapWindow::SetNewScroll(int sx, int sy, int sub)
1578 {
1579 const NWidgetBase *wi = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1580 Point hv = InverseRemapCoords(wi->current_x * ZOOM_LVL_BASE * TILE_SIZE / 2, wi->current_y * ZOOM_LVL_BASE * TILE_SIZE / 2);
1581 hv.x *= this->zoom;
1582 hv.y *= this->zoom;
1583
1584 if (sx < -hv.x) {
1585 sx = -hv.x;
1586 sub = 0;
1587 }
1588 if (sx > (int)(MapMaxX() * TILE_SIZE) - hv.x) {
1589 sx = MapMaxX() * TILE_SIZE - hv.x;
1590 sub = 0;
1591 }
1592 if (sy < -hv.y) {
1593 sy = -hv.y;
1594 sub = 0;
1595 }
1596 if (sy > (int)(MapMaxY() * TILE_SIZE) - hv.y) {
1597 sy = MapMaxY() * TILE_SIZE - hv.y;
1598 sub = 0;
1599 }
1600
1601 this->scroll_x = sx;
1602 this->scroll_y = sy;
1603 this->subscroll = sub;
1604 if (this->map_type == SMT_LINKSTATS) this->overlay->RebuildCache();
1605 }
1606
1607 /* virtual */ void SmallMapWindow::OnScroll(Point delta)
1608 {
1609 _cursor.fix_at = true;
1610
1611 /* While tile is at (delta.x, delta.y)? */
1612 int sub;
1613 Point pt = this->PixelToTile(delta.x, delta.y, &sub);
1614 this->SetNewScroll(this->scroll_x + pt.x * TILE_SIZE, this->scroll_y + pt.y * TILE_SIZE, sub);
1615
1616 this->SetDirty();
1617 }
1618
1622 void SmallMapWindow::SmallMapCenterOnCurrentPos()
1623 {
1624 const ViewPort *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
1625 Point viewport_center = TranslateXYToTileCoord(vp, vp->left + vp->width / 2, vp->top + vp->height / 2);
1626
1627 int sub;
1628 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1629 Point sxy = this->ComputeScroll(viewport_center.x / (int)TILE_SIZE, viewport_center.y / (int)TILE_SIZE,
1630 max(0, (int)wid->current_x / 2 - 2), wid->current_y / 2, &sub);
1631 this->SetNewScroll(sxy.x, sxy.y, sub);
1632 this->SetDirty();
1633 }
1634
1640 Point SmallMapWindow::GetStationMiddle(const Station *st) const
1641 {
1642 int x = (st->rect.right + st->rect.left + 1) / 2;
1643 int y = (st->rect.bottom + st->rect.top + 1) / 2;
1644 Point ret = this->RemapTile(x, y);
1645
1646 /* Same magic 3 as in DrawVehicles; that's where I got it from.
1647 * No idea what it is, but without it the result looks bad.
1648 */
1649 ret.x -= 3 + this->subscroll;
1650 return ret;
1651 }
1652
1653 SmallMapWindow::SmallMapType SmallMapWindow::map_type = SMT_CONTOUR;
1654 bool SmallMapWindow::show_towns = true;
1655 int SmallMapWindow::max_heightlevel = -1;
1656
1665 class NWidgetSmallmapDisplay : public NWidgetContainer {
1666 const SmallMapWindow *smallmap_window;
1667 public:
1668 NWidgetSmallmapDisplay() : NWidgetContainer(NWID_VERTICAL)
1669 {
1670 this->smallmap_window = NULL;
1671 }
1672
1673 virtual void SetupSmallestSize(Window *w, bool init_array)
1674 {
1675 NWidgetBase *display = this->head;
1676 NWidgetBase *bar = display->next;
1677
1678 display->SetupSmallestSize(w, init_array);
1679 bar->SetupSmallestSize(w, init_array);
1680
1681 this->smallmap_window = dynamic_cast<SmallMapWindow *>(w);
1682 assert(this->smallmap_window != NULL);
1683 this->smallest_x = max(display->smallest_x, bar->smallest_x + smallmap_window->GetMinLegendWidth());
1684 this->smallest_y = display->smallest_y + max(bar->smallest_y, smallmap_window->GetLegendHeight(smallmap_window->min_number_of_columns));
1685 this->fill_x = max(display->fill_x, bar->fill_x);
1686 this->fill_y = (display->fill_y == 0 && bar->fill_y == 0) ? 0 : min(display->fill_y, bar->fill_y);
1687 this->resize_x = max(display->resize_x, bar->resize_x);
1688 this->resize_y = min(display->resize_y, bar->resize_y);
1689 }
1690
1691 virtual void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
1692 {
1693 this->pos_x = x;
1694 this->pos_y = y;
1695 this->current_x = given_width;
1696 this->current_y = given_height;
1697
1698 NWidgetBase *display = this->head;
1699 NWidgetBase *bar = display->next;
1700
1701 if (sizing == ST_SMALLEST) {
1702 this->smallest_x = given_width;
1703 this->smallest_y = given_height;
1704 /* Make display and bar exactly equal to their minimal size. */
1705 display->AssignSizePosition(ST_SMALLEST, x, y, display->smallest_x, display->smallest_y, rtl);
1706 bar->AssignSizePosition(ST_SMALLEST, x, y + display->smallest_y, bar->smallest_x, bar->smallest_y, rtl);
1707 }
1708
1709 uint bar_height = max(bar->smallest_y, this->smallmap_window->GetLegendHeight(this->smallmap_window->GetNumberColumnsLegend(given_width - bar->smallest_x)));
1710 uint display_height = given_height - bar_height;
1711 display->AssignSizePosition(ST_RESIZE, x, y, given_width, display_height, rtl);
1712 bar->AssignSizePosition(ST_RESIZE, x, y + display_height, given_width, bar_height, rtl);
1713 }
1714
1715 virtual NWidgetCore *GetWidgetFromPos(int x, int y)
1716 {
1717 if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
1718 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1719 NWidgetCore *widget = child_wid->GetWidgetFromPos(x, y);
1720 if (widget != NULL) return widget;
1721 }
1722 return NULL;
1723 }
1724
1725 virtual void Draw(const Window *w)
1726 {
1727 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) child_wid->Draw(w);
1728 }
1729 };
1730
1732 static const NWidgetPart _nested_smallmap_display[] = {
1733 NWidget(WWT_PANEL, COLOUR_BROWN, WID_SM_MAP_BORDER),
1734 NWidget(WWT_INSET, COLOUR_BROWN, WID_SM_MAP), SetMinimalSize(346, 140), SetResize(1, 1), SetPadding(2, 2, 2, 2), EndContainer(),
1735 EndContainer(),
1736 };
1737
1739 static const NWidgetPart _nested_smallmap_bar[] = {
1740 NWidget(WWT_PANEL, COLOUR_BROWN),
1741 NWidget(NWID_HORIZONTAL),
1742 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SM_LEGEND), SetResize(1, 1),
1743 NWidget(NWID_VERTICAL),
1744 /* Top button row. */
1745 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1746 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_SM_ZOOM_IN),
1747 SetDataTip(SPR_IMG_ZOOMIN, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_IN), SetFill(1, 1),
1748 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_SM_CENTERMAP),
1749 SetDataTip(SPR_IMG_SMALLMAP, STR_SMALLMAP_CENTER), SetFill(1, 1),
1750 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_BLANK),
1751 SetDataTip(SPR_DOT_SMALL, STR_NULL), SetFill(1, 1),
1752 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_CONTOUR),
1753 SetDataTip(SPR_IMG_SHOW_COUNTOURS, STR_SMALLMAP_TOOLTIP_SHOW_LAND_CONTOURS_ON_MAP), SetFill(1, 1),
1754 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_VEHICLES),
1755 SetDataTip(SPR_IMG_SHOW_VEHICLES, STR_SMALLMAP_TOOLTIP_SHOW_VEHICLES_ON_MAP), SetFill(1, 1),
1756 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_INDUSTRIES),
1757 SetDataTip(SPR_IMG_INDUSTRY, STR_SMALLMAP_TOOLTIP_SHOW_INDUSTRIES_ON_MAP), SetFill(1, 1),
1758 EndContainer(),
1759 /* Bottom button row. */
1760 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1761 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_SM_ZOOM_OUT),
1762 SetDataTip(SPR_IMG_ZOOMOUT, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_OUT), SetFill(1, 1),
1763 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_TOGGLETOWNNAME),
1764 SetDataTip(SPR_IMG_TOWN, STR_SMALLMAP_TOOLTIP_TOGGLE_TOWN_NAMES_ON_OFF), SetFill(1, 1),
1765 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_LINKSTATS),
1766 SetDataTip(SPR_IMG_CARGOFLOW, STR_SMALLMAP_TOOLTIP_SHOW_LINK_STATS_ON_MAP), SetFill(1, 1),
1767 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_ROUTES),
1768 SetDataTip(SPR_IMG_SHOW_ROUTES, STR_SMALLMAP_TOOLTIP_SHOW_TRANSPORT_ROUTES_ON), SetFill(1, 1),
1769 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_VEGETATION),
1770 SetDataTip(SPR_IMG_PLANTTREES, STR_SMALLMAP_TOOLTIP_SHOW_VEGETATION_ON_MAP), SetFill(1, 1),
1771 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_OWNERS),
1772 SetDataTip(SPR_IMG_COMPANY_GENERAL, STR_SMALLMAP_TOOLTIP_SHOW_LAND_OWNERS_ON_MAP), SetFill(1, 1),
1773 EndContainer(),
1774 NWidget(NWID_SPACER), SetResize(0, 1),
1775 EndContainer(),
1776 EndContainer(),
1777 EndContainer(),
1778 };
1779
1780 static NWidgetBase *SmallMapDisplay(int *biggest_index)
1781 {
1782 NWidgetContainer *map_display = new NWidgetSmallmapDisplay;
1783
1784 MakeNWidgets(_nested_smallmap_display, lengthof(_nested_smallmap_display), biggest_index, map_display);
1785 MakeNWidgets(_nested_smallmap_bar, lengthof(_nested_smallmap_bar), biggest_index, map_display);
1786 return map_display;
1787 }
1788
1789
1790 static const NWidgetPart _nested_smallmap_widgets[] = {
1791 NWidget(NWID_HORIZONTAL),
1792 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
1793 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_SM_CAPTION), SetDataTip(STR_SMALLMAP_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1794 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
1795 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
1796 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
1797 EndContainer(),
1798 NWidgetFunction(SmallMapDisplay), // Smallmap display and legend bar + image buttons.
1799 /* Bottom button row and resize box. */
1800 NWidget(NWID_HORIZONTAL),
1801 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_SM_SELECT_BUTTONS),
1802 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1803 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_SM_ENABLE_ALL), SetDataTip(STR_SMALLMAP_ENABLE_ALL, STR_NULL),
1804 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_SM_DISABLE_ALL), SetDataTip(STR_SMALLMAP_DISABLE_ALL, STR_NULL),
1805 NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_SM_SHOW_HEIGHT), SetDataTip(STR_SMALLMAP_SHOW_HEIGHT, STR_SMALLMAP_TOOLTIP_SHOW_HEIGHT),
1806 NWidget(WWT_PANEL, COLOUR_BROWN), SetFill(1, 0), SetResize(1, 0),
1807 EndContainer(),
1808 EndContainer(),
1809 NWidget(WWT_PANEL, COLOUR_BROWN), SetFill(1, 0), SetResize(1, 0),
1810 EndContainer(),
1811 EndContainer(),
1812 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
1813 EndContainer(),
1814 };
1815
1816 static WindowDesc _smallmap_desc(
1817 WDP_AUTO, "smallmap", 484, 314,
1818 WC_SMALLMAP, WC_NONE,
1819 0,
1820 _nested_smallmap_widgets, lengthof(_nested_smallmap_widgets)
1821 );
1822
1826 void ShowSmallMap()
1827 {
1828 AllocateWindowDescFront<SmallMapWindow>(&_smallmap_desc, 0);
1829 }
1830
1839 bool ScrollMainWindowTo(int x, int y, int z, bool instant)
1840 {
1841 bool res = ScrollWindowTo(x, y, z, FindWindowById(WC_MAIN_WINDOW, 0), instant);
1842
1843 /* If a user scrolls to a tile (via what way what so ever) and already is on
1844 * that tile (e.g.: pressed twice), move the smallmap to that location,
1845 * so you directly see where you are on the smallmap. */
1846
1847 if (res) return res;
1848
1849 SmallMapWindow *w = dynamic_cast<SmallMapWindow*>(FindWindowById(WC_SMALLMAP, 0));
1850 if (w != NULL) w->SmallMapCenterOnCurrentPos();
1851
1852 return res;
1853 }