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