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 27036 2014-10-23 10:50:34Z rubidium $ */
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 / TILE_SIZE, v->y_pos / 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
908 Point SmallMapWindow::GetSmallMapCoordIncludingHeight(Point viewport_coord) const
909 {
910 /* First find out which tile would be there if we ignore height */
911 Point pt = InverseRemapCoords(viewport_coord.x, viewport_coord.y);
912 Point pt_without_height = {pt.x / TILE_SIZE, pt.y / TILE_SIZE};
913
914 /* Problem: There are mountains. So the tile actually displayed at the given position
915 * might be the high mountain of 30 tiles south.
916 * Unfortunately, there is no closed formula for finding such a tile.
917 * We call GetRowAtTile originally implemented for the viewport code, which performs
918 * a interval search. For details, see its documentation. */
919 int row_without_height = pt_without_height.x + pt_without_height.y;
920 int row_with_height = GetRowAtTile(viewport_coord.y, pt_without_height, false);
921 int row_offset = row_with_height - row_without_height;
922 Point pt_with_height = {pt_without_height.x + row_offset / 2, pt_without_height.y + row_offset / 2};
923 return pt_with_height;
924 }
925
929 void SmallMapWindow::DrawMapIndicators() const
930 {
931 /* Find main viewport. */
932 const ViewPort *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
933
934 Point upper_left_viewport_coord = {vp->virtual_left, vp->virtual_top};
935 Point upper_left_small_map_coord = GetSmallMapCoordIncludingHeight(upper_left_viewport_coord);
936 Point upper_left = this->RemapTile(upper_left_small_map_coord.x, upper_left_small_map_coord.y);
937 upper_left.x -= this->subscroll;
938
939 Point lower_right_viewport_coord = {vp->virtual_left + vp->virtual_width, vp->virtual_top + vp->virtual_height};
940 Point lower_right_smallmap_coord = GetSmallMapCoordIncludingHeight(lower_right_viewport_coord);
941 Point lower_right = this->RemapTile(lower_right_smallmap_coord.x, lower_right_smallmap_coord.y);
942 /* why do we do this? in my tests subscroll was zero */
943 lower_right.x -= this->subscroll;
944
945 SmallMapWindow::DrawVertMapIndicator(upper_left.x, upper_left.y, lower_right.y);
946 SmallMapWindow::DrawVertMapIndicator(lower_right.x, upper_left.y, lower_right.y);
947
948 SmallMapWindow::DrawHorizMapIndicator(upper_left.x, lower_right.x, upper_left.y);
949 SmallMapWindow::DrawHorizMapIndicator(upper_left.x, lower_right.x, lower_right.y);
950 }
951
963 void SmallMapWindow::DrawSmallMap(DrawPixelInfo *dpi) const
964 {
965 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
966 DrawPixelInfo *old_dpi;
967
968 old_dpi = _cur_dpi;
969 _cur_dpi = dpi;
970
971 /* Clear it */
972 GfxFillRect(dpi->left, dpi->top, dpi->left + dpi->width - 1, dpi->top + dpi->height - 1, PC_BLACK);
973
974 /* Which tile is displayed at (dpi->left, dpi->top)? */
975 int dx;
976 Point tile = this->PixelToTile(dpi->left, dpi->top, &dx);
977 int tile_x = this->scroll_x / (int)TILE_SIZE + tile.x;
978 int tile_y = this->scroll_y / (int)TILE_SIZE + tile.y;
979
980 void *ptr = blitter->MoveTo(dpi->dst_ptr, -dx - 4, 0);
981 int x = - dx - 4;
982 int y = 0;
983
984 for (;;) {
985 /* Distance from left edge */
986 if (x >= -3) {
987 if (x >= dpi->width) break; // Exit the loop.
988
989 int end_pos = min(dpi->width, x + 4);
990 int reps = (dpi->height - y + 1) / 2; // Number of lines.
991 if (reps > 0) {
992 this->DrawSmallMapColumn(ptr, tile_x, tile_y, dpi->pitch * 2, reps, x, end_pos, blitter);
993 }
994 }
995
996 if (y == 0) {
997 tile_y += this->zoom;
998 y++;
999 ptr = blitter->MoveTo(ptr, 0, 1);
1000 } else {
1001 tile_x -= this->zoom;
1002 y--;
1003 ptr = blitter->MoveTo(ptr, 0, -1);
1004 }
1005 ptr = blitter->MoveTo(ptr, 2, 0);
1006 x += 2;
1007 }
1008
1009 /* Draw vehicles */
1010 if (this->map_type == SMT_CONTOUR || this->map_type == SMT_VEHICLES) this->DrawVehicles(dpi, blitter);
1011
1012 /* Draw link stat overlay */
1013 if (this->map_type == SMT_LINKSTATS) this->overlay->Draw(dpi);
1014
1015 /* Draw town names */
1016 if (this->show_towns) this->DrawTowns(dpi);
1017
1018 /* Draw map indicators */
1019 this->DrawMapIndicators();
1020
1021 _cur_dpi = old_dpi;
1022 }
1023
1027 void SmallMapWindow::SetupWidgetData()
1028 {
1029 StringID legend_tooltip;
1030 StringID enable_all_tooltip;
1031 StringID disable_all_tooltip;
1032 int plane;
1033 switch (this->map_type) {
1034 case SMT_INDUSTRY:
1035 legend_tooltip = STR_SMALLMAP_TOOLTIP_INDUSTRY_SELECTION;
1036 enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_INDUSTRIES;
1037 disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_INDUSTRIES;
1038 plane = 0;
1039 break;
1040
1041 case SMT_OWNER:
1042 legend_tooltip = STR_SMALLMAP_TOOLTIP_COMPANY_SELECTION;
1043 enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_COMPANIES;
1044 disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_COMPANIES;
1045 plane = 0;
1046 break;
1047
1048 case SMT_LINKSTATS:
1049 legend_tooltip = STR_SMALLMAP_TOOLTIP_CARGO_SELECTION;
1050 enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_CARGOS;
1051 disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_CARGOS;
1052 plane = 0;
1053 break;
1054
1055 default:
1056 legend_tooltip = STR_NULL;
1057 enable_all_tooltip = STR_NULL;
1058 disable_all_tooltip = STR_NULL;
1059 plane = 1;
1060 break;
1061 }
1062
1063 this->GetWidget<NWidgetCore>(WID_SM_LEGEND)->SetDataTip(STR_NULL, legend_tooltip);
1064 this->GetWidget<NWidgetCore>(WID_SM_ENABLE_ALL)->SetDataTip(STR_SMALLMAP_ENABLE_ALL, enable_all_tooltip);
1065 this->GetWidget<NWidgetCore>(WID_SM_DISABLE_ALL)->SetDataTip(STR_SMALLMAP_DISABLE_ALL, disable_all_tooltip);
1066 this->GetWidget<NWidgetStacked>(WID_SM_SELECT_BUTTONS)->SetDisplayedPlane(plane);
1067 }
1068
1069 SmallMapWindow::SmallMapWindow(WindowDesc *desc, int window_number) : Window(desc), refresh(FORCE_REFRESH_PERIOD)
1070 {
1071 _smallmap_industry_highlight = INVALID_INDUSTRYTYPE;
1072 this->overlay = new LinkGraphOverlay(this, WID_SM_MAP, 0, this->GetOverlayCompanyMask(), 1);
1073 this->InitNested(window_number);
1074 this->LowerWidget(this->map_type + WID_SM_CONTOUR);
1075
1076 this->RebuildColourIndexIfNecessary();
1077
1078 this->SetWidgetLoweredState(WID_SM_SHOW_HEIGHT, _smallmap_show_heightmap);
1079
1080 this->SetWidgetLoweredState(WID_SM_TOGGLETOWNNAME, this->show_towns);
1081
1082 this->SetupWidgetData();
1083
1084 this->SetZoomLevel(ZLC_INITIALIZE, NULL);
1085 this->SmallMapCenterOnCurrentPos();
1086 this->SetOverlayCargoMask();
1087 }
1088
1092 void SmallMapWindow::RebuildColourIndexIfNecessary()
1093 {
1094 /* Rebuild colour indices if necessary. */
1095 if (SmallMapWindow::max_heightlevel == _settings_game.construction.max_heightlevel) return;
1096
1097 for (uint n = 0; n < lengthof(_heightmap_schemes); n++) {
1098 /* The heights go from 0 up to and including maximum. */
1099 int heights = _settings_game.construction.max_heightlevel + 1;
1100 _heightmap_schemes[n].height_colours = ReallocT<uint32>(_heightmap_schemes[n].height_colours, heights);
1101
1102 for (int z = 0; z < heights; z++) {
1103 uint access_index = (_heightmap_schemes[n].colour_count * z) / heights;
1104
1105 /* Choose colour by mapping the range (0..max heightlevel) on the complete colour table. */
1106 _heightmap_schemes[n].height_colours[z] = _heightmap_schemes[n].height_colours_base[access_index];
1107 }
1108 }
1109
1110 SmallMapWindow::max_heightlevel = _settings_game.construction.max_heightlevel;
1111 BuildLandLegend();
1112 }
1113
1114 /* virtual */ void SmallMapWindow::SetStringParameters(int widget) const
1115 {
1116 switch (widget) {
1117 case WID_SM_CAPTION:
1118 SetDParam(0, STR_SMALLMAP_TYPE_CONTOURS + this->map_type);
1119 break;
1120 }
1121 }
1122
1123 /* virtual */ void SmallMapWindow::OnInit()
1124 {
1125 uint min_width = 0;
1126 this->min_number_of_columns = INDUSTRY_MIN_NUMBER_OF_COLUMNS;
1127 this->min_number_of_fixed_rows = lengthof(_linkstat_colours_in_legenda);
1128 for (uint i = 0; i < lengthof(_legend_table); i++) {
1129 uint height = 0;
1130 uint num_columns = 1;
1131 for (const LegendAndColour *tbl = _legend_table[i]; !tbl->end; ++tbl) {
1132 StringID str;
1133 if (i == SMT_INDUSTRY) {
1134 SetDParam(0, tbl->legend);
1135 SetDParam(1, IndustryPool::MAX_SIZE);
1136 str = STR_SMALLMAP_INDUSTRY;
1137 } else if (i == SMT_LINKSTATS) {
1138 SetDParam(0, tbl->legend);
1139 str = STR_SMALLMAP_LINKSTATS;
1140 } else if (i == SMT_OWNER) {
1141 if (tbl->company != INVALID_COMPANY) {
1142 if (!Company::IsValidID(tbl->company)) {
1143 /* Rebuild the owner legend. */
1144 BuildOwnerLegend();
1145 this->OnInit();
1146 return;
1147 }
1148 /* Non-fixed legend entries for the owner view. */
1149 SetDParam(0, tbl->company);
1150 str = STR_SMALLMAP_COMPANY;
1151 } else {
1152 str = tbl->legend;
1153 }
1154 } else {
1155 if (tbl->col_break) {
1156 this->min_number_of_fixed_rows = max(this->min_number_of_fixed_rows, height);
1157 height = 0;
1158 num_columns++;
1159 }
1160 height++;
1161 str = tbl->legend;
1162 }
1163 min_width = max(GetStringBoundingBox(str).width, min_width);
1164 }
1165 this->min_number_of_fixed_rows = max(this->min_number_of_fixed_rows, height);
1166 this->min_number_of_columns = max(this->min_number_of_columns, num_columns);
1167 }
1168
1169 /* The width of a column is the minimum width of all texts + the size of the blob + some spacing */
1170 this->column_width = min_width + LEGEND_BLOB_WIDTH + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
1171 }
1172
1173 /* virtual */ void SmallMapWindow::OnPaint()
1174 {
1175 if (this->map_type == SMT_OWNER) {
1176 for (const LegendAndColour *tbl = _legend_table[this->map_type]; !tbl->end; ++tbl) {
1177 if (tbl->company != INVALID_COMPANY && !Company::IsValidID(tbl->company)) {
1178 /* Rebuild the owner legend. */
1179 BuildOwnerLegend();
1180 this->InvalidateData(1);
1181 break;
1182 }
1183 }
1184 }
1185
1186 this->DrawWidgets();
1187 }
1188
1189 /* virtual */ void SmallMapWindow::DrawWidget(const Rect &r, int widget) const
1190 {
1191 switch (widget) {
1192 case WID_SM_MAP: {
1193 DrawPixelInfo new_dpi;
1194 if (!FillDrawPixelInfo(&new_dpi, r.left + 1, r.top + 1, r.right - r.left - 1, r.bottom - r.top - 1)) return;
1195 this->DrawSmallMap(&new_dpi);
1196 break;
1197 }
1198
1199 case WID_SM_LEGEND: {
1200 uint columns = this->GetNumberColumnsLegend(r.right - r.left + 1);
1201 uint number_of_rows = this->GetNumberRowsLegend(columns);
1202 bool rtl = _current_text_dir == TD_RTL;
1203 uint y_org = r.top + WD_FRAMERECT_TOP;
1204 uint x = rtl ? r.right - this->column_width - WD_FRAMERECT_RIGHT : r.left + WD_FRAMERECT_LEFT;
1205 uint y = y_org;
1206 uint i = 0; // Row counter for industry legend.
1207 uint row_height = FONT_HEIGHT_SMALL;
1208
1209 uint text_left = rtl ? 0 : LEGEND_BLOB_WIDTH + WD_FRAMERECT_LEFT;
1210 uint text_right = this->column_width - 1 - (rtl ? LEGEND_BLOB_WIDTH + WD_FRAMERECT_RIGHT : 0);
1211 uint blob_left = rtl ? this->column_width - 1 - LEGEND_BLOB_WIDTH : 0;
1212 uint blob_right = rtl ? this->column_width - 1 : LEGEND_BLOB_WIDTH;
1213
1214 StringID string = STR_NULL;
1215 switch (this->map_type) {
1216 case SMT_INDUSTRY:
1217 string = STR_SMALLMAP_INDUSTRY;
1218 break;
1219 case SMT_LINKSTATS:
1220 string = STR_SMALLMAP_LINKSTATS;
1221 break;
1222 case SMT_OWNER:
1223 string = STR_SMALLMAP_COMPANY;
1224 break;
1225 default:
1226 break;
1227 }
1228
1229 for (const LegendAndColour *tbl = _legend_table[this->map_type]; !tbl->end; ++tbl) {
1230 if (tbl->col_break || ((this->map_type == SMT_INDUSTRY || this->map_type == SMT_OWNER || this->map_type == SMT_LINKSTATS) && i++ >= number_of_rows)) {
1231 /* Column break needed, continue at top, COLUMN_WIDTH pixels
1232 * (one "row") to the right. */
1233 x += rtl ? -(int)this->column_width : this->column_width;
1234 y = y_org;
1235 i = 1;
1236 }
1237
1238 uint8 legend_colour = tbl->colour;
1239
1240 switch (this->map_type) {
1241 case SMT_INDUSTRY:
1242 /* Industry name must be formatted, since it's not in tiny font in the specs.
1243 * So, draw with a parameter and use the STR_SMALLMAP_INDUSTRY string, which is tiny font */
1244 SetDParam(0, tbl->legend);
1245 SetDParam(1, Industry::GetIndustryTypeCount(tbl->type));
1246 if (tbl->show_on_map && tbl->type == _smallmap_industry_highlight) {
1247 legend_colour = _smallmap_industry_highlight_state ? PC_WHITE : PC_BLACK;
1248 }
1249 /* FALL THROUGH */
1250 case SMT_LINKSTATS:
1251 SetDParam(0, tbl->legend);
1252 /* FALL_THROUGH */
1253 case SMT_OWNER:
1254 if (this->map_type != SMT_OWNER || tbl->company != INVALID_COMPANY) {
1255 if (this->map_type == SMT_OWNER) SetDParam(0, tbl->company);
1256 if (!tbl->show_on_map) {
1257 /* Simply draw the string, not the black border of the legend colour.
1258 * This will enforce the idea of the disabled item */
1259 DrawString(x + text_left, x + text_right, y, string, TC_GREY);
1260 } else {
1261 DrawString(x + text_left, x + text_right, y, string, TC_BLACK);
1262 GfxFillRect(x + blob_left, y + 1, x + blob_right, y + row_height - 1, PC_BLACK); // Outer border of the legend colour
1263 }
1264 break;
1265 }
1266 /* FALL_THROUGH */
1267 default:
1268 if (this->map_type == SMT_CONTOUR) SetDParam(0, tbl->height * TILE_HEIGHT_STEP);
1269 /* Anything that is not an industry or a company is using normal process */
1270 GfxFillRect(x + blob_left, y + 1, x + blob_right, y + row_height - 1, PC_BLACK);
1271 DrawString(x + text_left, x + text_right, y, tbl->legend);
1272 break;
1273 }
1274 GfxFillRect(x + blob_left + 1, y + 2, x + blob_right - 1, y + row_height - 2, legend_colour); // Legend colour
1275
1276 y += row_height;
1277 }
1278 }
1279 }
1280 }
1281
1286 void SmallMapWindow::SwitchMapType(SmallMapType map_type)
1287 {
1288 this->RaiseWidget(this->map_type + WID_SM_CONTOUR);
1289 this->map_type = map_type;
1290 this->LowerWidget(this->map_type + WID_SM_CONTOUR);
1291
1292 this->SetupWidgetData();
1293
1294 if (map_type == SMT_LINKSTATS) this->overlay->RebuildCache();
1295 this->SetDirty();
1296 }
1297
1306 inline uint SmallMapWindow::GetNumberRowsLegend(uint columns) const
1307 {
1308 /* Reserve one column for link colours */
1309 uint num_rows_linkstats = CeilDiv(_smallmap_cargo_count, columns - 1);
1310 uint num_rows_others = CeilDiv(max(_smallmap_industry_count, _smallmap_company_count), columns);
1311 return max(this->min_number_of_fixed_rows, max(num_rows_linkstats, num_rows_others));
1312 }
1313
1325 void SmallMapWindow::SelectLegendItem(int click_pos, LegendAndColour *legend, int end_legend_item, int begin_legend_item)
1326 {
1327 if (_ctrl_pressed) {
1328 /* Disable all, except the clicked one */
1329 bool changes = false;
1330 for (int i = begin_legend_item; i != end_legend_item; i++) {
1331 bool new_state = (i == click_pos);
1332 if (legend[i].show_on_map != new_state) {
1333 changes = true;
1334 legend[i].show_on_map = new_state;
1335 }
1336 }
1337 if (!changes) {
1338 /* Nothing changed? Then show all (again). */
1339 for (int i = begin_legend_item; i != end_legend_item; i++) {
1340 legend[i].show_on_map = true;
1341 }
1342 }
1343 } else {
1344 legend[click_pos].show_on_map = !legend[click_pos].show_on_map;
1345 }
1346 }
1347
1351 void SmallMapWindow::SetOverlayCargoMask()
1352 {
1353 uint32 cargo_mask = 0;
1354 for (int i = 0; i != _smallmap_cargo_count; ++i) {
1355 if (_legend_linkstats[i].show_on_map) SetBit(cargo_mask, _legend_linkstats[i].type);
1356 }
1357 this->overlay->SetCargoMask(cargo_mask);
1358 }
1359
1365 int SmallMapWindow::GetPositionOnLegend(Point pt)
1366 {
1367 const NWidgetBase *wi = this->GetWidget<NWidgetBase>(WID_SM_LEGEND);
1368 uint line = (pt.y - wi->pos_y - WD_FRAMERECT_TOP) / FONT_HEIGHT_SMALL;
1369 uint columns = this->GetNumberColumnsLegend(wi->current_x);
1370 uint number_of_rows = this->GetNumberRowsLegend(columns);
1371 if (line >= number_of_rows) return -1;
1372
1373 bool rtl = _current_text_dir == TD_RTL;
1374 int x = pt.x - wi->pos_x;
1375 if (rtl) x = wi->current_x - x;
1376 uint column = (x - WD_FRAMERECT_LEFT) / this->column_width;
1377
1378 return (column * number_of_rows) + line;
1379 }
1380
1381 /* virtual */ void SmallMapWindow::OnMouseOver(Point pt, int widget)
1382 {
1383 IndustryType new_highlight = INVALID_INDUSTRYTYPE;
1384 if (widget == WID_SM_LEGEND && this->map_type == SMT_INDUSTRY) {
1385 int industry_pos = GetPositionOnLegend(pt);
1386 if (industry_pos >= 0 && industry_pos < _smallmap_industry_count) {
1387 new_highlight = _legend_from_industries[industry_pos].type;
1388 }
1389 }
1390 if (new_highlight != _smallmap_industry_highlight) {
1391 _smallmap_industry_highlight = new_highlight;
1392 this->refresh = _smallmap_industry_highlight != INVALID_INDUSTRYTYPE ? BLINK_PERIOD : FORCE_REFRESH_PERIOD;
1393 _smallmap_industry_highlight_state = true;
1394 this->SetDirty();
1395 }
1396 }
1397
1398 /* virtual */ void SmallMapWindow::OnClick(Point pt, int widget, int click_count)
1399 {
1400 /* User clicked something, notify the industry chain window to stop sending newly selected industries. */
1401 InvalidateWindowClassesData(WC_INDUSTRY_CARGOES, NUM_INDUSTRYTYPES);
1402
1403 switch (widget) {
1404 case WID_SM_MAP: { // Map window
1405 /*
1406 * XXX: scrolling with the left mouse button is done by subsequently
1407 * clicking with the left mouse button; clicking once centers the
1408 * large map at the selected point. So by unclicking the left mouse
1409 * button here, it gets reclicked during the next inputloop, which
1410 * would make it look like the mouse is being dragged, while it is
1411 * actually being (virtually) clicked every inputloop.
1412 */
1413 _left_button_clicked = false;
1414
1415 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1416 Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
1417 int sub;
1418 pt = this->PixelToTile(pt.x - wid->pos_x, pt.y - wid->pos_y, &sub);
1419 pt = RemapCoords(this->scroll_x + pt.x * TILE_SIZE + this->zoom * (TILE_SIZE - sub * TILE_SIZE / 4),
1420 this->scroll_y + pt.y * TILE_SIZE + sub * this->zoom * TILE_SIZE / 4, 0);
1421
1422 /* correct y coordinate according to the height level at the chosen tile
1423 * - so far we assumed height zero. Calculations here according to
1424 * TranslateXYToTileCoord in viewport.cpp */
1425 Point pt_scaled = {pt.x / (int)(4 * TILE_SIZE), pt.y / (int)(2 * TILE_SIZE)};
1426 Point tile_coord = {pt_scaled.y - pt_scaled.x, pt_scaled.y + pt_scaled.x};
1427
1428 if (tile_coord.x >= 0 && tile_coord.y >= 0
1429 && tile_coord.x < (int)MapMaxX() && tile_coord.y < (int)MapMaxY()) {
1430 int clicked_tile_height = TileHeight(TileXY(tile_coord.x, tile_coord.y));
1431 pt.y -= clicked_tile_height * TILE_HEIGHT;
1432 }
1433
1434 w->viewport->follow_vehicle = INVALID_VEHICLE;
1435 w->viewport->dest_scrollpos_x = pt.x - (w->viewport->virtual_width >> 1);
1436 w->viewport->dest_scrollpos_y = pt.y - (w->viewport->virtual_height >> 1);
1437
1438 this->SetDirty();
1439 break;
1440 }
1441
1442 case WID_SM_ZOOM_IN:
1443 case WID_SM_ZOOM_OUT: {
1444 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1445 Point pt = {wid->current_x / 2, wid->current_y / 2};
1446 this->SetZoomLevel((widget == WID_SM_ZOOM_IN) ? ZLC_ZOOM_IN : ZLC_ZOOM_OUT, &pt);
1447 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1448 break;
1449 }
1450
1451 case WID_SM_CONTOUR: // Show land contours
1452 case WID_SM_VEHICLES: // Show vehicles
1453 case WID_SM_INDUSTRIES: // Show industries
1454 case WID_SM_LINKSTATS: // Show route map
1455 case WID_SM_ROUTES: // Show transport routes
1456 case WID_SM_VEGETATION: // Show vegetation
1457 case WID_SM_OWNERS: // Show land owners
1458 this->SwitchMapType((SmallMapType)(widget - WID_SM_CONTOUR));
1459 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1460 break;
1461
1462 case WID_SM_CENTERMAP: // Center the smallmap again
1463 this->SmallMapCenterOnCurrentPos();
1464 this->HandleButtonClick(WID_SM_CENTERMAP);
1465 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1466 break;
1467
1468 case WID_SM_TOGGLETOWNNAME: // Toggle town names
1469 this->show_towns = !this->show_towns;
1470 this->SetWidgetLoweredState(WID_SM_TOGGLETOWNNAME, this->show_towns);
1471
1472 this->SetDirty();
1473 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1474 break;
1475
1476 case WID_SM_LEGEND: // Legend
1477 if (this->map_type == SMT_INDUSTRY || this->map_type == SMT_LINKSTATS || this->map_type == SMT_OWNER) {
1478 int click_pos = this->GetPositionOnLegend(pt);
1479 if (click_pos < 0) break;
1480
1481 /* If industry type small map*/
1482 if (this->map_type == SMT_INDUSTRY) {
1483 /* If click on industries label, find right industry type and enable/disable it. */
1484 if (click_pos < _smallmap_industry_count) {
1485 this->SelectLegendItem(click_pos, _legend_from_industries, _smallmap_industry_count);
1486 }
1487 } else if (this->map_type == SMT_LINKSTATS) {
1488 if (click_pos < _smallmap_cargo_count) {
1489 this->SelectLegendItem(click_pos, _legend_linkstats, _smallmap_cargo_count);
1490 this->SetOverlayCargoMask();
1491 }
1492 } else if (this->map_type == SMT_OWNER) {
1493 if (click_pos < _smallmap_company_count) {
1494 this->SelectLegendItem(click_pos, _legend_land_owners, _smallmap_company_count, NUM_NO_COMPANY_ENTRIES);
1495 }
1496 }
1497 this->SetDirty();
1498 }
1499 break;
1500
1501 case WID_SM_ENABLE_ALL:
1502 /* FALL THROUGH */
1503 case WID_SM_DISABLE_ALL: {
1504 LegendAndColour *tbl = NULL;
1505 switch (this->map_type) {
1506 case SMT_INDUSTRY:
1507 tbl = _legend_from_industries;
1508 break;
1509 case SMT_OWNER:
1510 tbl = &(_legend_land_owners[NUM_NO_COMPANY_ENTRIES]);
1511 break;
1512 case SMT_LINKSTATS:
1513 tbl = _legend_linkstats;
1514 break;
1515 default:
1516 NOT_REACHED();
1517 }
1518 for (;!tbl->end && tbl->legend != STR_LINKGRAPH_LEGEND_UNUSED; ++tbl) {
1519 tbl->show_on_map = (widget == WID_SM_ENABLE_ALL);
1520 }
1521 if (this->map_type == SMT_LINKSTATS) this->SetOverlayCargoMask();
1522 this->SetDirty();
1523 break;
1524 }
1525
1526 case WID_SM_SHOW_HEIGHT: // Enable/disable showing of heightmap.
1527 _smallmap_show_heightmap = !_smallmap_show_heightmap;
1528 this->SetWidgetLoweredState(WID_SM_SHOW_HEIGHT, _smallmap_show_heightmap);
1529 this->SetDirty();
1530 break;
1531 }
1532 }
1533
1542 /* virtual */ void SmallMapWindow::OnInvalidateData(int data, bool gui_scope)
1543 {
1544 if (!gui_scope) return;
1545
1546 switch (data) {
1547 case 1:
1548 /* The owner legend has already been rebuilt. */
1549 this->ReInit();
1550 break;
1551
1552 case 0: {
1553 extern uint64 _displayed_industries;
1554 if (this->map_type != SMT_INDUSTRY) this->SwitchMapType(SMT_INDUSTRY);
1555
1556 for (int i = 0; i != _smallmap_industry_count; i++) {
1557 _legend_from_industries[i].show_on_map = HasBit(_displayed_industries, _legend_from_industries[i].type);
1558 }
1559 break;
1560 }
1561
1562 case 2:
1563 this->RebuildColourIndexIfNecessary();
1564 break;
1565
1566 default: NOT_REACHED();
1567 }
1568 this->SetDirty();
1569 }
1570
1571 /* virtual */ bool SmallMapWindow::OnRightClick(Point pt, int widget)
1572 {
1573 if (widget != WID_SM_MAP || _scrolling_viewport) return false;
1574
1575 _scrolling_viewport = true;
1576 return true;
1577 }
1578
1579 /* virtual */ void SmallMapWindow::OnMouseWheel(int wheel)
1580 {
1581 if (_settings_client.gui.scrollwheel_scrolling == 0) {
1582 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1583 int cursor_x = _cursor.pos.x - this->left - wid->pos_x;
1584 int cursor_y = _cursor.pos.y - this->top - wid->pos_y;
1585 if (IsInsideMM(cursor_x, 0, wid->current_x) && IsInsideMM(cursor_y, 0, wid->current_y)) {
1586 Point pt = {cursor_x, cursor_y};
1587 this->SetZoomLevel((wheel < 0) ? ZLC_ZOOM_IN : ZLC_ZOOM_OUT, &pt);
1588 }
1589 }
1590 }
1591
1592 /* virtual */ void SmallMapWindow::OnTick()
1593 {
1594 /* Update the window every now and then */
1595 if (--this->refresh != 0) return;
1596
1597 if (this->map_type == SMT_LINKSTATS) {
1598 uint32 company_mask = this->GetOverlayCompanyMask();
1599 if (this->overlay->GetCompanyMask() != company_mask) {
1600 this->overlay->SetCompanyMask(company_mask);
1601 } else {
1602 this->overlay->RebuildCache();
1603 }
1604 }
1605 _smallmap_industry_highlight_state = !_smallmap_industry_highlight_state;
1606
1607 this->refresh = _smallmap_industry_highlight != INVALID_INDUSTRYTYPE ? BLINK_PERIOD : FORCE_REFRESH_PERIOD;
1608 this->SetDirty();
1609 }
1610
1618 void SmallMapWindow::SetNewScroll(int sx, int sy, int sub)
1619 {
1620 const NWidgetBase *wi = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1621 Point hv = InverseRemapCoords(wi->current_x * ZOOM_LVL_BASE * TILE_SIZE / 2, wi->current_y * ZOOM_LVL_BASE * TILE_SIZE / 2);
1622 hv.x *= this->zoom;
1623 hv.y *= this->zoom;
1624
1625 if (sx < -hv.x) {
1626 sx = -hv.x;
1627 sub = 0;
1628 }
1629 if (sx > (int)(MapMaxX() * TILE_SIZE) - hv.x) {
1630 sx = MapMaxX() * TILE_SIZE - hv.x;
1631 sub = 0;
1632 }
1633 if (sy < -hv.y) {
1634 sy = -hv.y;
1635 sub = 0;
1636 }
1637 if (sy > (int)(MapMaxY() * TILE_SIZE) - hv.y) {
1638 sy = MapMaxY() * TILE_SIZE - hv.y;
1639 sub = 0;
1640 }
1641
1642 this->scroll_x = sx;
1643 this->scroll_y = sy;
1644 this->subscroll = sub;
1645 if (this->map_type == SMT_LINKSTATS) this->overlay->RebuildCache();
1646 }
1647
1648 /* virtual */ void SmallMapWindow::OnScroll(Point delta)
1649 {
1650 _cursor.fix_at = true;
1651
1652 /* While tile is at (delta.x, delta.y)? */
1653 int sub;
1654 Point pt = this->PixelToTile(delta.x, delta.y, &sub);
1655 this->SetNewScroll(this->scroll_x + pt.x * TILE_SIZE, this->scroll_y + pt.y * TILE_SIZE, sub);
1656
1657 this->SetDirty();
1658 }
1659
1663 void SmallMapWindow::SmallMapCenterOnCurrentPos()
1664 {
1665 /* Goal: Given the viewport coordinates of the middle of the map window, find
1666 * out which tile is displayed there. */
1667
1668 /* First find out which tile would be there if we ignore height */
1669 const ViewPort *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
1670 Point viewport_center = {vp->virtual_left + vp->virtual_width / 2, vp->virtual_top + vp->virtual_height / 2};
1671 Point pt_with_height = GetSmallMapCoordIncludingHeight(viewport_center);
1672
1673 /* And finally scroll to that position. */
1674
1675 int sub;
1676 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1677 Point sxy = this->ComputeScroll(pt_with_height.x, pt_with_height.y,
1678 max(0, (int)wid->current_x / 2 - 2), wid->current_y / 2, &sub);
1679 this->SetNewScroll(sxy.x, sxy.y, sub);
1680 this->SetDirty();
1681 }
1682
1688 Point SmallMapWindow::GetStationMiddle(const Station *st) const
1689 {
1690 int x = (st->rect.right + st->rect.left + 1) / 2;
1691 int y = (st->rect.bottom + st->rect.top + 1) / 2;
1692 Point ret = this->RemapTile(x, y);
1693
1694 /* Same magic 3 as in DrawVehicles; that's where I got it from.
1695 * No idea what it is, but without it the result looks bad.
1696 */
1697 ret.x -= 3 + this->subscroll;
1698 return ret;
1699 }
1700
1701 SmallMapWindow::SmallMapType SmallMapWindow::map_type = SMT_CONTOUR;
1702 bool SmallMapWindow::show_towns = true;
1703 int SmallMapWindow::max_heightlevel = -1;
1704
1713 class NWidgetSmallmapDisplay : public NWidgetContainer {
1714 const SmallMapWindow *smallmap_window;
1715 public:
1716 NWidgetSmallmapDisplay() : NWidgetContainer(NWID_VERTICAL)
1717 {
1718 this->smallmap_window = NULL;
1719 }
1720
1721 virtual void SetupSmallestSize(Window *w, bool init_array)
1722 {
1723 NWidgetBase *display = this->head;
1724 NWidgetBase *bar = display->next;
1725
1726 display->SetupSmallestSize(w, init_array);
1727 bar->SetupSmallestSize(w, init_array);
1728
1729 this->smallmap_window = dynamic_cast<SmallMapWindow *>(w);
1730 assert(this->smallmap_window != NULL);
1731 this->smallest_x = max(display->smallest_x, bar->smallest_x + smallmap_window->GetMinLegendWidth());
1732 this->smallest_y = display->smallest_y + max(bar->smallest_y, smallmap_window->GetLegendHeight(smallmap_window->min_number_of_columns));
1733 this->fill_x = max(display->fill_x, bar->fill_x);
1734 this->fill_y = (display->fill_y == 0 && bar->fill_y == 0) ? 0 : min(display->fill_y, bar->fill_y);
1735 this->resize_x = max(display->resize_x, bar->resize_x);
1736 this->resize_y = min(display->resize_y, bar->resize_y);
1737 }
1738
1739 virtual void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
1740 {
1741 this->pos_x = x;
1742 this->pos_y = y;
1743 this->current_x = given_width;
1744 this->current_y = given_height;
1745
1746 NWidgetBase *display = this->head;
1747 NWidgetBase *bar = display->next;
1748
1749 if (sizing == ST_SMALLEST) {
1750 this->smallest_x = given_width;
1751 this->smallest_y = given_height;
1752 /* Make display and bar exactly equal to their minimal size. */
1753 display->AssignSizePosition(ST_SMALLEST, x, y, display->smallest_x, display->smallest_y, rtl);
1754 bar->AssignSizePosition(ST_SMALLEST, x, y + display->smallest_y, bar->smallest_x, bar->smallest_y, rtl);
1755 }
1756
1757 uint bar_height = max(bar->smallest_y, this->smallmap_window->GetLegendHeight(this->smallmap_window->GetNumberColumnsLegend(given_width - bar->smallest_x)));
1758 uint display_height = given_height - bar_height;
1759 display->AssignSizePosition(ST_RESIZE, x, y, given_width, display_height, rtl);
1760 bar->AssignSizePosition(ST_RESIZE, x, y + display_height, given_width, bar_height, rtl);
1761 }
1762
1763 virtual NWidgetCore *GetWidgetFromPos(int x, int y)
1764 {
1765 if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
1766 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1767 NWidgetCore *widget = child_wid->GetWidgetFromPos(x, y);
1768 if (widget != NULL) return widget;
1769 }
1770 return NULL;
1771 }
1772
1773 virtual void Draw(const Window *w)
1774 {
1775 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) child_wid->Draw(w);
1776 }
1777 };
1778
1780 static const NWidgetPart _nested_smallmap_display[] = {
1781 NWidget(WWT_PANEL, COLOUR_BROWN, WID_SM_MAP_BORDER),
1782 NWidget(WWT_INSET, COLOUR_BROWN, WID_SM_MAP), SetMinimalSize(346, 140), SetResize(1, 1), SetPadding(2, 2, 2, 2), EndContainer(),
1783 EndContainer(),
1784 };
1785
1787 static const NWidgetPart _nested_smallmap_bar[] = {
1788 NWidget(WWT_PANEL, COLOUR_BROWN),
1789 NWidget(NWID_HORIZONTAL),
1790 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SM_LEGEND), SetResize(1, 1),
1791 NWidget(NWID_VERTICAL),
1792 /* Top button row. */
1793 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1794 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_SM_ZOOM_IN),
1795 SetDataTip(SPR_IMG_ZOOMIN, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_IN), SetFill(1, 1),
1796 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_SM_CENTERMAP),
1797 SetDataTip(SPR_IMG_SMALLMAP, STR_SMALLMAP_CENTER), SetFill(1, 1),
1798 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_BLANK),
1799 SetDataTip(SPR_DOT_SMALL, STR_NULL), SetFill(1, 1),
1800 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_CONTOUR),
1801 SetDataTip(SPR_IMG_SHOW_COUNTOURS, STR_SMALLMAP_TOOLTIP_SHOW_LAND_CONTOURS_ON_MAP), SetFill(1, 1),
1802 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_VEHICLES),
1803 SetDataTip(SPR_IMG_SHOW_VEHICLES, STR_SMALLMAP_TOOLTIP_SHOW_VEHICLES_ON_MAP), SetFill(1, 1),
1804 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_INDUSTRIES),
1805 SetDataTip(SPR_IMG_INDUSTRY, STR_SMALLMAP_TOOLTIP_SHOW_INDUSTRIES_ON_MAP), SetFill(1, 1),
1806 EndContainer(),
1807 /* Bottom button row. */
1808 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1809 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_SM_ZOOM_OUT),
1810 SetDataTip(SPR_IMG_ZOOMOUT, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_OUT), SetFill(1, 1),
1811 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_TOGGLETOWNNAME),
1812 SetDataTip(SPR_IMG_TOWN, STR_SMALLMAP_TOOLTIP_TOGGLE_TOWN_NAMES_ON_OFF), SetFill(1, 1),
1813 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_LINKSTATS),
1814 SetDataTip(SPR_IMG_CARGOFLOW, STR_SMALLMAP_TOOLTIP_SHOW_LINK_STATS_ON_MAP), SetFill(1, 1),
1815 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_ROUTES),
1816 SetDataTip(SPR_IMG_SHOW_ROUTES, STR_SMALLMAP_TOOLTIP_SHOW_TRANSPORT_ROUTES_ON), SetFill(1, 1),
1817 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_VEGETATION),
1818 SetDataTip(SPR_IMG_PLANTTREES, STR_SMALLMAP_TOOLTIP_SHOW_VEGETATION_ON_MAP), SetFill(1, 1),
1819 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_OWNERS),
1820 SetDataTip(SPR_IMG_COMPANY_GENERAL, STR_SMALLMAP_TOOLTIP_SHOW_LAND_OWNERS_ON_MAP), SetFill(1, 1),
1821 EndContainer(),
1822 NWidget(NWID_SPACER), SetResize(0, 1),
1823 EndContainer(),
1824 EndContainer(),
1825 EndContainer(),
1826 };
1827
1828 static NWidgetBase *SmallMapDisplay(int *biggest_index)
1829 {
1830 NWidgetContainer *map_display = new NWidgetSmallmapDisplay;
1831
1832 MakeNWidgets(_nested_smallmap_display, lengthof(_nested_smallmap_display), biggest_index, map_display);
1833 MakeNWidgets(_nested_smallmap_bar, lengthof(_nested_smallmap_bar), biggest_index, map_display);
1834 return map_display;
1835 }
1836
1837
1838 static const NWidgetPart _nested_smallmap_widgets[] = {
1839 NWidget(NWID_HORIZONTAL),
1840 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
1841 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_SM_CAPTION), SetDataTip(STR_SMALLMAP_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1842 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
1843 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
1844 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
1845 EndContainer(),
1846 NWidgetFunction(SmallMapDisplay), // Smallmap display and legend bar + image buttons.
1847 /* Bottom button row and resize box. */
1848 NWidget(NWID_HORIZONTAL),
1849 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_SM_SELECT_BUTTONS),
1850 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1851 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_SM_ENABLE_ALL), SetDataTip(STR_SMALLMAP_ENABLE_ALL, STR_NULL),
1852 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_SM_DISABLE_ALL), SetDataTip(STR_SMALLMAP_DISABLE_ALL, STR_NULL),
1853 NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_SM_SHOW_HEIGHT), SetDataTip(STR_SMALLMAP_SHOW_HEIGHT, STR_SMALLMAP_TOOLTIP_SHOW_HEIGHT),
1854 NWidget(WWT_PANEL, COLOUR_BROWN), SetFill(1, 0), SetResize(1, 0),
1855 EndContainer(),
1856 EndContainer(),
1857 NWidget(WWT_PANEL, COLOUR_BROWN), SetFill(1, 0), SetResize(1, 0),
1858 EndContainer(),
1859 EndContainer(),
1860 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
1861 EndContainer(),
1862 };
1863
1864 static WindowDesc _smallmap_desc(
1865 WDP_AUTO, "smallmap", 484, 314,
1866 WC_SMALLMAP, WC_NONE,
1867 0,
1868 _nested_smallmap_widgets, lengthof(_nested_smallmap_widgets)
1869 );
1870
1874 void ShowSmallMap()
1875 {
1876 AllocateWindowDescFront<SmallMapWindow>(&_smallmap_desc, 0);
1877 }
1878
1887 bool ScrollMainWindowTo(int x, int y, int z, bool instant)
1888 {
1889 bool res = ScrollWindowTo(x, y, z, FindWindowById(WC_MAIN_WINDOW, 0), instant);
1890
1891 /* If a user scrolls to a tile (via what way what so ever) and already is on
1892 * that tile (e.g.: pressed twice), move the smallmap to that location,
1893 * so you directly see where you are on the smallmap. */
1894
1895 if (res) return res;
1896
1897 SmallMapWindow *w = dynamic_cast<SmallMapWindow*>(FindWindowById(WC_SMALLMAP, 0));
1898 if (w != NULL) w->SmallMapCenterOnCurrentPos();
1899
1900 return res;
1901 }