OpenTTD Source 20261112-master-gbcbec5e63f
smallmap_gui.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "stdafx.h"
11#include "core/backup_type.hpp"
12#include "clear_map.h"
13#include "industry.h"
14#include "station_map.h"
15#include "landscape.h"
16#include "tree_map.h"
17#include "viewport_func.h"
18#include "town.h"
19#include "tunnelbridge_map.h"
20#include "core/endian_func.hpp"
21#include "vehicle_base.h"
22#include "sound_func.h"
23#include "window_func.h"
24#include "company_base.h"
25#include "company_func.h"
26#include "zoom_func.h"
27#include "strings_func.h"
28#include "blitter/factory.hpp"
30#include "timer/timer.h"
31#include "timer/timer_window.h"
32#include "smallmap_gui.h"
33#include "core/enum_type.hpp"
34
36
37#include "table/strings.h"
38
39#include <bitset>
40
41#include "safeguards.h"
42
46
51 IndustryType industry_type = IT_INVALID;
53 uint8_t height = 0;
54 CompanyID company = CompanyID::Invalid();
55 bool show_on_map = true;
56 bool end = false;
57 bool col_break = false;
58};
59
61static const uint8_t _linkstat_colours_in_legenda[] = {0, 1, 3, 5, 7, 9, 11};
62
63static const int NUM_NO_COMPANY_ENTRIES = 4;
64
67 {.legend = STR_SMALLMAP_LEGENDA_ROADS, .colour = PC_BLACK},
68 {.legend = STR_SMALLMAP_LEGENDA_RAILROADS, .colour = PC_GREY},
69 {.legend = STR_SMALLMAP_LEGENDA_STATIONS_AIRPORTS_DOCKS, .colour = PC_LIGHT_BLUE},
70 {.legend = STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES, .colour = PC_DARK_RED},
71 {.legend = STR_SMALLMAP_LEGENDA_VEHICLES, .colour = PC_WHITE},
72
73 /* Placeholders for the colours and heights of the legend.
74 * The following values are set at BuildLandLegend() based
75 * on each colour scheme and the maximum map height. */
76 {.legend = STR_TINY_BLACK_HEIGHT, .col_break = true},
77 {.legend = STR_TINY_BLACK_HEIGHT},
78 {.legend = STR_TINY_BLACK_HEIGHT},
79 {.legend = STR_TINY_BLACK_HEIGHT},
80 {.legend = STR_TINY_BLACK_HEIGHT},
81 {.legend = STR_TINY_BLACK_HEIGHT},
82 {.legend = STR_TINY_BLACK_HEIGHT, .col_break = true},
83 {.legend = STR_TINY_BLACK_HEIGHT},
84 {.legend = STR_TINY_BLACK_HEIGHT},
85 {.legend = STR_TINY_BLACK_HEIGHT},
86 {.legend = STR_TINY_BLACK_HEIGHT},
87 {.legend = STR_TINY_BLACK_HEIGHT},
88 {.end = true},
89};
90
91static const LegendAndColour _legend_vehicles[] = {
92 {.legend = STR_SMALLMAP_LEGENDA_TRAINS, .colour = PC_RED},
93 {.legend = STR_SMALLMAP_LEGENDA_ROAD_VEHICLES, .colour = PC_YELLOW},
94 {.legend = STR_SMALLMAP_LEGENDA_SHIPS, .colour = PC_LIGHT_BLUE},
95 {.legend = STR_SMALLMAP_LEGENDA_AIRCRAFT, .colour = PC_WHITE},
96
97 {.legend = STR_SMALLMAP_LEGENDA_TRANSPORT_ROUTES, .colour = PC_BLACK, .col_break = true},
98 {.legend = STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES, .colour = PC_DARK_RED},
99 {.end = true},
100};
101
102static const LegendAndColour _legend_routes[] = {
103 {.legend = STR_SMALLMAP_LEGENDA_ROADS, .colour = PC_BLACK},
104 {.legend = STR_SMALLMAP_LEGENDA_RAILROADS, .colour = PC_GREY},
105 {.legend = STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES, .colour = PC_DARK_RED},
106
107 {.legend = STR_SMALLMAP_LEGENDA_RAILROAD_STATION, .colour = PC_VERY_DARK_BROWN, .col_break = true},
108 {.legend = STR_SMALLMAP_LEGENDA_TRUCK_LOADING_BAY, .colour = PC_ORANGE},
109 {.legend = STR_SMALLMAP_LEGENDA_BUS_STATION, .colour = PC_YELLOW},
110 {.legend = STR_SMALLMAP_LEGENDA_AIRPORT_HELIPORT, .colour = PC_RED},
111 {.legend = STR_SMALLMAP_LEGENDA_DOCK, .colour = PC_LIGHT_BLUE},
112 {.end = true},
113};
114
115static const LegendAndColour _legend_vegetation[] = {
116 {.legend = STR_SMALLMAP_LEGENDA_ROUGH_LAND, .colour = PC_ROUGH_LAND},
117 {.legend = STR_SMALLMAP_LEGENDA_GRASS_LAND, .colour = PC_GRASS_LAND},
118 {.legend = STR_SMALLMAP_LEGENDA_BARE_LAND, .colour = PC_BARE_LAND},
119 {.legend = STR_SMALLMAP_LEGENDA_RAINFOREST, .colour = PC_RAINFOREST},
120 {.legend = STR_SMALLMAP_LEGENDA_FIELDS, .colour = PC_FIELDS},
121 {.legend = STR_SMALLMAP_LEGENDA_TREES, .colour = PC_TREES},
122
123 {.legend = STR_SMALLMAP_LEGENDA_FOREST, .colour = PC_GREEN, .col_break = true},
124 {.legend = STR_SMALLMAP_LEGENDA_ROCKS, .colour = PC_GREY},
125 {.legend = STR_SMALLMAP_LEGENDA_DESERT, .colour = PC_ORANGE},
126 {.legend = STR_SMALLMAP_LEGENDA_SNOW, .colour = PC_LIGHT_BLUE},
127 {.legend = STR_SMALLMAP_LEGENDA_TRANSPORT_ROUTES, .colour = PC_BLACK},
128 {.legend = STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES, .colour = PC_DARK_RED},
129 {.end = true},
130};
131
132static LegendAndColour _legend_land_owners[NUM_NO_COMPANY_ENTRIES + MAX_COMPANIES + 1] = {
133 {.legend = STR_SMALLMAP_LEGENDA_WATER, .colour = PC_WATER},
134 {.legend = STR_SMALLMAP_LEGENDA_NO_OWNER}, // This colour will vary depending on settings.
135 {.legend = STR_SMALLMAP_LEGENDA_TOWNS, .colour = PC_DARK_RED},
136 {.legend = STR_SMALLMAP_LEGENDA_INDUSTRIES, .colour = PC_DARK_GREY},
137 /* The legend will be terminated the first time it is used. */
138 {.end = true},
139};
140
153static bool _smallmap_show_heightmap = false;
155static IndustryType _smallmap_industry_highlight = IT_INVALID;
160
165{
166 uint j = 0;
167
168 /* Add each name */
169 for (IndustryType ind : _sorted_industry_types) {
170 const IndustrySpec *indsp = GetIndustrySpec(ind);
171 if (indsp->enabled) {
172 _legend_from_industries[j].legend = indsp->name;
173 _legend_from_industries[j].colour = indsp->map_colour;
174 _legend_from_industries[j].industry_type = ind;
175 _legend_from_industries[j].show_on_map = true;
176 _legend_from_industries[j].col_break = false;
177 _legend_from_industries[j].end = false;
178
179 /* Store widget number for this industry type. */
180 _industry_to_list_pos[ind] = j;
181 j++;
182 }
183 }
184 /* Terminate the list */
185 _legend_from_industries[j].end = true;
186
187 /* Store number of enabled industries */
189}
190
195{
196 /* Clear the legend */
197 std::fill(std::begin(_legend_linkstats), std::end(_legend_linkstats), LegendAndColour{});
198
199 uint i = 0;
200 for (; i < _sorted_cargo_specs.size(); ++i) {
201 const CargoSpec *cs = _sorted_cargo_specs[i];
202
203 _legend_linkstats[i].legend = cs->name;
204 _legend_linkstats[i].colour = cs->legend_colour;
205 _legend_linkstats[i].cargo_type = cs->Index();
206 _legend_linkstats[i].show_on_map = true;
207 }
208
209 _legend_linkstats[i].col_break = true;
211
213 _legend_linkstats[i].legend = STR_EMPTY;
215 _legend_linkstats[i].show_on_map = true;
216 }
217
218 _legend_linkstats[_smallmap_cargo_count].legend = STR_LINKGRAPH_LEGEND_UNUSED;
219 _legend_linkstats[i - 1].legend = STR_LINKGRAPH_LEGEND_OVERLOADED;
220 _legend_linkstats[(_smallmap_cargo_count + i - 1) / 2].legend = STR_LINKGRAPH_LEGEND_SATURATED;
221 _legend_linkstats[i].end = true;
222}
223
235
239 _legend_vehicles,
242 _legend_routes,
243 _legend_vegetation,
244 _legend_land_owners,
245};
246
247#define MKCOLOUR(x) TO_LE32(x)
248
249#define MKCOLOUR_XXXX(x) (MKCOLOUR(0x01010101) * (uint)(x.p))
250#define MKCOLOUR_0XX0(x) (MKCOLOUR(0x00010100) * (uint)(x.p))
251#define MKCOLOUR_X00X(x) (MKCOLOUR(0x01000001) * (uint)(x.p))
252
253#define MKCOLOUR_XYYX(x, y) (MKCOLOUR_X00X(x) | MKCOLOUR_0XX0(y))
254
255#define MKCOLOUR_0000 MKCOLOUR_XXXX(PixelColour{0x00})
256#define MKCOLOUR_F00F MKCOLOUR_X00X(PixelColour{0xFF})
257#define MKCOLOUR_FFFF MKCOLOUR_XXXX(PixelColour{0xFF})
258
260
263 std::array<uint32_t, MAX_TILE_HEIGHT + 1> height_colours{};
264 std::span<const uint32_t> height_colours_base;
265 uint32_t default_colour;
266};
267
270 {{}, _green_map_heights, MKCOLOUR_XXXX(PixelColour{0x54})},
271 {{}, _dark_green_map_heights, MKCOLOUR_XXXX(PixelColour{0x62})},
272 {{}, _violet_map_heights, MKCOLOUR_XXXX(PixelColour{0x81})},
273};
274
279{
280 /* The smallmap window has never been initialized, so no need to change the legend. */
281 if (_heightmap_schemes[0].height_colours.back() == 0) return;
282
283 /*
284 * The general idea of this function is to fill the legend with an appropriate evenly spaced
285 * selection of height levels. All entries with STR_TINY_BLACK_HEIGHT are reserved for this.
286 * At the moment there are twelve of these.
287 *
288 * The table below defines up to which height level a particular delta in the legend should be
289 * used. One could opt for just dividing the maximum height and use that as delta, but that
290 * creates many "ugly" legend labels, e.g. once every 950 meter. As a result, this table will
291 * reduce the number of deltas to 7: every 100m, 200m, 300m, 500m, 750m, 1000m and 1250m. The
292 * deltas are closer together at the lower numbers because going from 12 entries to just 4, as
293 * would happen when replacing 200m and 300m by 250m, would mean the legend would be short and
294 * that might not be considered appropriate.
295 *
296 * The current method yields at least 7 legend entries and at most 12. It can be increased to
297 * 8 by adding a 150m and 400m option, but especially 150m creates ugly heights.
298 *
299 * It tries to evenly space the legend items over the two columns that are there for the legend.
300 */
301
302 /* Table for delta; if max_height is less than the first column, use the second column as value. */
303 uint deltas[][2] = { { 24, 2 }, { 48, 4 }, { 72, 6 }, { 120, 10 }, { 180, 15 }, { 240, 20 }, { MAX_TILE_HEIGHT + 1, 25 }};
304 uint i = 0;
305 for (; _settings_game.construction.map_height_limit >= deltas[i][0]; i++) {
306 /* Nothing to do here. */
307 }
308 uint delta = deltas[i][1];
309
310 int total_entries = (_settings_game.construction.map_height_limit / delta) + 1;
311 int rows = CeilDiv(total_entries, 2);
312 int j = 0;
313
314 for (i = 0; i < lengthof(_legend_land_contours) - 1 && j < total_entries; i++) {
315 if (_legend_land_contours[i].legend != STR_TINY_BLACK_HEIGHT) continue;
316
317 _legend_land_contours[i].col_break = j % rows == 0;
318 _legend_land_contours[i].end = false;
319 _legend_land_contours[i].height = j * delta;
320 _legend_land_contours[i].colour = PixelColour{static_cast<uint8_t>(_heightmap_schemes[_settings_client.gui.smallmap_land_colour].height_colours[_legend_land_contours[i].height])};
321 j++;
322 }
323 _legend_land_contours[i].end = true;
324}
325
330{
331 _legend_land_owners[1].colour = PixelColour{static_cast<uint8_t>(_heightmap_schemes[_settings_client.gui.smallmap_land_colour].default_colour)};
332
334 for (const Company *c : Company::Iterate()) {
335 _legend_land_owners[i].colour = GetColourGradient(c->colour, Shade::Light);
336 _legend_land_owners[i].company = c->index;
337 _legend_land_owners[i].show_on_map = true;
338 _legend_land_owners[i].col_break = false;
339 _legend_land_owners[i].end = false;
340 _company_to_list_pos[c->index] = i;
341 i++;
342 }
343
344 /* Terminate the list */
345 _legend_land_owners[i].end = true;
346
347 /* Store maximum amount of owner legend entries. */
349}
350
351struct AndOr {
352 uint32_t mor;
353 uint32_t mand;
354};
355
362static inline uint32_t ApplyMask(uint32_t colour, const AndOr &mask)
363{
364 return (colour & mask.mand) | mask.mor;
365}
366
367
370 AndOr(MKCOLOUR_0000, MKCOLOUR_FFFF), // TileType::Clear
371 AndOr(MKCOLOUR_0XX0(PC_GREY), MKCOLOUR_F00F), // TileType::Railway
372 AndOr(MKCOLOUR_0XX0(PC_BLACK), MKCOLOUR_F00F), // TileType::Road
373 AndOr(MKCOLOUR_0XX0(PC_DARK_RED), MKCOLOUR_F00F), // TileType::House
374 AndOr(MKCOLOUR_0000, MKCOLOUR_FFFF), // TileType::Trees
375 AndOr(MKCOLOUR_XXXX(PC_LIGHT_BLUE), MKCOLOUR_0000), // TileType::Station
376 AndOr(MKCOLOUR_XXXX(PC_WATER), MKCOLOUR_0000), // TileType::Water
377 AndOr(MKCOLOUR_0000, MKCOLOUR_FFFF), // TileType::Void
378 AndOr(MKCOLOUR_XXXX(PC_DARK_RED), MKCOLOUR_0000), // TileType::Industry
379 AndOr(MKCOLOUR_0000, MKCOLOUR_FFFF), // TileType::TunnelBridge
380 AndOr(MKCOLOUR_0XX0(PC_DARK_RED), MKCOLOUR_F00F), // TileType::Object
381};
382
385 AndOr(MKCOLOUR_0000, MKCOLOUR_FFFF), // TileType::Clear
386 AndOr(MKCOLOUR_0XX0(PC_BLACK), MKCOLOUR_F00F), // TileType::Railway
387 AndOr(MKCOLOUR_0XX0(PC_BLACK), MKCOLOUR_F00F), // TileType::Road
388 AndOr(MKCOLOUR_0XX0(PC_DARK_RED), MKCOLOUR_F00F), // TileType::House
389 AndOr(MKCOLOUR_0000, MKCOLOUR_FFFF), // TileType::Trees
390 AndOr(MKCOLOUR_0XX0(PC_BLACK), MKCOLOUR_F00F), // TileType::Station
391 AndOr(MKCOLOUR_XXXX(PC_WATER), MKCOLOUR_0000), // TileType::Water
392 AndOr(MKCOLOUR_0000, MKCOLOUR_FFFF), // TileType::Void
393 AndOr(MKCOLOUR_XXXX(PC_DARK_RED), MKCOLOUR_0000), // TileType::Industry
394 AndOr(MKCOLOUR_0000, MKCOLOUR_FFFF), // TileType::TunnelBridge
395 AndOr(MKCOLOUR_0XX0(PC_DARK_RED), MKCOLOUR_F00F), // TileType::Object
396};
397
400 2, // TileType::Clear
401 8, // TileType::Railway
402 7, // TileType::Road
403 5, // TileType::House
404 2, // TileType::Trees
405 9, // TileType::Station
406 2, // TileType::Water
407 1, // TileType::Void
408 6, // TileType::Industry
409 8, // TileType::TunnelBridge
410 2, // TileType::Object
411};
412
418static bool IsDryCoast(TileIndex tile)
419{
421}
422
431{
432 if (IsDryCoast(tile)) return table[TileType::Clear];
433 return table[t];
434}
435
442static inline uint32_t GetSmallMapContoursPixels(TileIndex tile, TileType t)
443{
444 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
446}
447
454static inline uint32_t GetSmallMapVehiclesPixels(TileIndex tile, TileType t)
455{
456 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
458}
459
472
480static inline uint32_t GetSmallMapRoutesPixels(TileIndex tile, TileType t)
481{
482 switch (t) {
484 switch (GetStationType(tile)) {
485 case StationType::Rail: return MKCOLOUR_XXXX(PC_VERY_DARK_BROWN);
486 case StationType::Airport: return MKCOLOUR_XXXX(PC_RED);
487 case StationType::Truck: return MKCOLOUR_XXXX(PC_ORANGE);
488 case StationType::Bus: return MKCOLOUR_XXXX(PC_YELLOW);
489 case StationType::Dock: return MKCOLOUR_XXXX(PC_LIGHT_BLUE);
490 default: return MKCOLOUR_FFFF;
491 }
492
493 case TileType::Railway: {
494 AndOr andor = {
495 MKCOLOUR_0XX0(GetRailTypeInfo(GetRailType(tile))->map_colour),
497 };
498
499 const SmallMapColourScheme &cs = _heightmap_schemes[_settings_client.gui.smallmap_land_colour];
500 return ApplyMask(cs.default_colour, andor);
501 }
502
503 case TileType::Road: {
504 const RoadTypeInfo *rti = nullptr;
505 if (GetRoadTypeRoad(tile) != INVALID_ROADTYPE) {
506 rti = GetRoadTypeInfo(GetRoadTypeRoad(tile));
507 } else {
508 rti = GetRoadTypeInfo(GetRoadTypeTram(tile));
509 }
510 if (rti != nullptr) {
511 AndOr andor = {
512 MKCOLOUR_0XX0(rti->map_colour),
514 };
515
516 const SmallMapColourScheme &cs = _heightmap_schemes[_settings_client.gui.smallmap_land_colour];
517 return ApplyMask(cs.default_colour, andor);
518 }
519 [[fallthrough]];
520 }
521
522 default:
523 /* Ground colour */
524 const SmallMapColourScheme &cs = _heightmap_schemes[_settings_client.gui.smallmap_land_colour];
526 }
527}
528
536static inline uint32_t GetSmallMapLinkStatsPixels(TileIndex tile, TileType t)
537{
539}
540
543 MKCOLOUR_XXXX(PC_GRASS_LAND),
544 MKCOLOUR_XXXX(PC_ROUGH_LAND),
545 MKCOLOUR_XXXX(PC_GREY),
546 MKCOLOUR_XXXX(PC_FIELDS),
547 MKCOLOUR_XXXX(PC_GRASS_LAND),
548 MKCOLOUR_XXXX(PC_ORANGE),
549 MKCOLOUR_XXXX(PC_GRASS_LAND),
550 MKCOLOUR_XXXX(PC_GRASS_LAND),
551};
552
560static inline uint32_t GetSmallMapVegetationPixels(TileIndex tile, TileType t)
561{
562 switch (t) {
563 case TileType::Clear:
564 if (IsSnowTile(tile)) return MKCOLOUR_XXXX(PC_LIGHT_BLUE);
566 if (GetClearDensity(tile) < 3) return MKCOLOUR_XXXX(PC_BARE_LAND);
567 if (GetTropicZone(tile) == TropicZone::Rainforest) return MKCOLOUR_XXXX(PC_RAINFOREST);
568 }
570
571 case TileType::Water:
572 if (IsDryCoast(tile)) return (GetTropicZone(tile) == TropicZone::Rainforest) ? MKCOLOUR_XXXX(PC_RAINFOREST) : MKCOLOUR_XXXX(PC_GRASS_LAND);
573 return ApplyMask(MKCOLOUR_XXXX(PC_GRASS_LAND), _smallmap_vehicles_andor[t]);
574
576 return IsTileForestIndustry(tile) ? MKCOLOUR_XXXX(PC_GREEN) : MKCOLOUR_XXXX(PC_DARK_RED);
577
578 case TileType::Trees:
580 return (_settings_game.game_creation.landscape == LandscapeType::Arctic) ? MKCOLOUR_XYYX(PC_LIGHT_BLUE, PC_TREES) : MKCOLOUR_XYYX(PC_ORANGE, PC_TREES);
581 }
582 return (GetTropicZone(tile) == TropicZone::Rainforest) ? MKCOLOUR_XYYX(PC_RAINFOREST, PC_TREES) : MKCOLOUR_XYYX(PC_GRASS_LAND, PC_TREES);
583
584 default:
586 }
587}
588
598uint32_t GetSmallMapOwnerPixels(TileIndex tile, TileType t, IncludeHeightmap include_heightmap)
599{
600 Owner o;
601
602 switch (t) {
603 case TileType::Void: return MKCOLOUR_XXXX(PC_BLACK);
604 case TileType::Industry: return MKCOLOUR_XXXX(PC_DARK_GREY);
605 case TileType::House: return MKCOLOUR_XXXX(PC_DARK_RED);
606 case TileType::Road:
608 break;
609
610 default:
611 o = GetTileOwner(tile);
612 break;
613 }
614
615 if ((o < MAX_COMPANIES && !_legend_land_owners[_company_to_list_pos[o]].show_on_map) || o == OWNER_NONE || o == OWNER_WATER) {
616 if (t == TileType::Water && !IsDryCoast(tile)) return MKCOLOUR_XXXX(PC_WATER);
617 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
618 return ((include_heightmap == IncludeHeightmap::IfEnabled && _smallmap_show_heightmap) || include_heightmap == IncludeHeightmap::Always)
619 ? cs->height_colours[TileHeight(tile)] : cs->default_colour;
620 } else if (o == OWNER_TOWN) {
621 return MKCOLOUR_XXXX(PC_DARK_RED);
622 }
623
624 return MKCOLOUR_XXXX(_legend_land_owners[_company_to_list_pos[o]].colour);
625}
626
631
633class SmallMapWindow : public Window {
634protected:
636 enum class ZoomLevelChange : uint8_t {
640 };
641
643 static bool show_towns;
644 static bool show_ind_names;
645 static int map_height_limit;
646
647 static const uint INDUSTRY_MIN_NUMBER_OF_COLUMNS = 2;
648
651 uint column_width = 0;
652 uint legend_width = 0;
653
654 int32_t scroll_x = 0;
655 int32_t scroll_y = 0;
656 int32_t subscroll = 0;
657 int zoom = 0;
658
659 std::unique_ptr<LinkGraphOverlay> overlay{};
660
663 {
664 InvalidateWindowClassesData(WindowClass::IndustryCargoes, NUM_INDUSTRYTYPES);
665 }
666
667 static inline Point SmallmapRemapCoords(int x, int y)
668 {
669 Point pt;
670 pt.x = (y - x) * 2;
671 pt.y = y + x;
672 return pt;
673 }
674
681 static inline void DrawVertMapIndicator(int x, int y, int y2)
682 {
683 GfxFillRect(x, y, x, y + 3, PC_VERY_LIGHT_YELLOW);
684 GfxFillRect(x, y2 - 3, x, y2, PC_VERY_LIGHT_YELLOW);
685 }
686
693 static inline void DrawHorizMapIndicator(int x, int x2, int y)
694 {
695 GfxFillRect(x, y, x + 3, y, PC_VERY_LIGHT_YELLOW);
696 GfxFillRect(x2 - 3, y, x2, y, PC_VERY_LIGHT_YELLOW);
697 }
698
703 inline uint GetMinLegendWidth() const
704 {
705 return WidgetDimensions::scaled.framerect.left + this->min_number_of_columns * this->column_width;
706 }
707
713 inline uint GetNumberColumnsLegend(uint width) const
714 {
715 return width / this->column_width;
716 }
717
723 inline uint GetLegendHeight(uint num_columns) const
724 {
725 return WidgetDimensions::scaled.framerect.Vertical() +
727 }
728
738
743
745 const IntervalTimer<TimerWindow> refresh_interval = {std::chrono::milliseconds(930), [this](auto) {
746 ForceRefresh();
747 }};
748
753 {
754 /* Rebuild colour indices if necessary. */
755 if (SmallMapWindow::map_height_limit == _settings_game.construction.map_height_limit) return;
756
757 for (auto &heightmap_scheme : _heightmap_schemes) {
758 /* The heights go from 0 up to and including maximum. */
759 size_t heights = _settings_game.construction.map_height_limit + 1;
760 heightmap_scheme.height_colours.fill(heightmap_scheme.height_colours_base.back());
761
762 for (size_t z = 0; z < heights; z++) {
763 size_t access_index = (heightmap_scheme.height_colours_base.size() * z) / heights;
764
765 /* Choose colour by mapping the range (0..max heightlevel) on the complete colour table. */
766 heightmap_scheme.height_colours[z] = heightmap_scheme.height_colours_base[access_index];
767 }
768 }
769
770 SmallMapWindow::map_height_limit = _settings_game.construction.map_height_limit;
772 }
773
782 uint GetNumberRowsLegend(uint columns) const
783 {
784 /* Reserve one column for link colours */
785 uint num_rows_linkstats = CeilDiv(_smallmap_cargo_count, columns - 1);
786 uint num_rows_others = CeilDiv(std::max(_smallmap_industry_count, _smallmap_company_count), columns);
787 return std::max({this->min_number_of_fixed_rows, num_rows_linkstats, num_rows_others});
788 }
789
801 void SelectLegendItem(int click_pos, LegendAndColour *legend, int end_legend_item, int begin_legend_item = 0)
802 {
803 if (IsCtrlPressed()) {
804 /* Disable all, except the clicked one */
805 bool changes = false;
806 for (int i = begin_legend_item; i != end_legend_item; i++) {
807 bool new_state = (i == click_pos);
808 if (legend[i].show_on_map != new_state) {
809 changes = true;
810 legend[i].show_on_map = new_state;
811 }
812 }
813 if (!changes) {
814 /* Nothing changed? Then show all (again). */
815 for (int i = begin_legend_item; i != end_legend_item; i++) {
816 legend[i].show_on_map = true;
817 }
818 }
819 } else {
820 legend[click_pos].show_on_map = !legend[click_pos].show_on_map;
821 }
822
823 if (this->map_type == SmallMapType::Industries) this->BreakIndustryChainLink();
824 }
825
830 void SwitchMapType(SmallMapType map_type)
831 {
832 this->RaiseWidget(WID_SM_CONTOUR + to_underlying(this->map_type));
833 this->map_type = map_type;
834 this->LowerWidget(WID_SM_CONTOUR + to_underlying(this->map_type));
835
836 this->SetupWidgetData();
837
838 if (map_type == SmallMapType::LinkStats) this->overlay->SetDirty();
840 this->ReInit();
841 }
842
850 void SetNewScroll(int sx, int sy, int sub)
851 {
853 Point hv = InverseRemapCoords(wi->current_x * ZOOM_BASE * TILE_SIZE / 2, wi->current_y * ZOOM_BASE * TILE_SIZE / 2);
854 hv.x *= this->zoom;
855 hv.y *= this->zoom;
856
857 if (sx < -hv.x) {
858 sx = -hv.x;
859 sub = 0;
860 }
861 if (sx > (int)(Map::MaxX() * TILE_SIZE) - hv.x) {
862 sx = Map::MaxX() * TILE_SIZE - hv.x;
863 sub = 0;
864 }
865 if (sy < -hv.y) {
866 sy = -hv.y;
867 sub = 0;
868 }
869 if (sy > (int)(Map::MaxY() * TILE_SIZE) - hv.y) {
870 sy = Map::MaxY() * TILE_SIZE - hv.y;
871 sub = 0;
872 }
873
874 this->scroll_x = sx;
875 this->scroll_y = sy;
876 this->subscroll = sub;
877 if (this->map_type == SmallMapType::LinkStats) this->overlay->SetDirty();
878 }
879
883 void DrawMapIndicators() const
884 {
885 /* Find main viewport. */
886 const Viewport &vp = *GetMainWindow()->viewport;
887
888 Point upper_left_smallmap_coord = InverseRemapCoords2(vp.virtual_left, vp.virtual_top);
889 Point lower_right_smallmap_coord = InverseRemapCoords2(vp.virtual_left + vp.virtual_width - 1, vp.virtual_top + vp.virtual_height - 1);
890
891 Point upper_left = this->RemapTile(upper_left_smallmap_coord.x / (int)TILE_SIZE, upper_left_smallmap_coord.y / (int)TILE_SIZE);
892 upper_left.x -= this->subscroll;
893
894 Point lower_right = this->RemapTile(lower_right_smallmap_coord.x / (int)TILE_SIZE, lower_right_smallmap_coord.y / (int)TILE_SIZE);
895 lower_right.x -= this->subscroll;
896
897 SmallMapWindow::DrawVertMapIndicator(upper_left.x, upper_left.y, lower_right.y);
898 SmallMapWindow::DrawVertMapIndicator(lower_right.x, upper_left.y, lower_right.y);
899
900 SmallMapWindow::DrawHorizMapIndicator(upper_left.x, lower_right.x, upper_left.y);
901 SmallMapWindow::DrawHorizMapIndicator(upper_left.x, lower_right.x, lower_right.y);
902 }
903
917 void DrawSmallMapColumn(void *dst, uint xc, uint yc, int pitch, int reps, int start_pos, int end_pos, Blitter *blitter) const
918 {
919 void *dst_ptr_abs_end = blitter->MoveTo(_screen.dst_ptr, 0, _screen.height);
920 uint min_xy = _settings_game.construction.freeform_edges ? 1 : 0;
921
922 do {
923 /* Check if the tile (xc,yc) is within the map range */
924 if (xc >= Map::MaxX() || yc >= Map::MaxY()) continue;
925
926 /* Check if the dst pointer points to a pixel inside the screen buffer */
927 if (dst < _screen.dst_ptr) continue;
928 if (dst >= dst_ptr_abs_end) continue;
929
930 /* Construct tilearea covered by (xc, yc, xc + this->zoom, yc + this->zoom) such that it is within min_xy limits. */
931 TileArea ta;
932 if (min_xy == 1 && (xc == 0 || yc == 0)) {
933 if (this->zoom == 1) continue; // The tile area is empty, don't draw anything.
934
935 ta = TileArea(TileXY(std::max(min_xy, xc), std::max(min_xy, yc)), this->zoom - (xc == 0), this->zoom - (yc == 0));
936 } else {
937 ta = TileArea(TileXY(xc, yc), this->zoom, this->zoom);
938 }
939 ta.ClampToMap(); // Clamp to map boundaries (may contain TileType::Void tiles!).
940
941 uint32_t val = this->GetTileColours(ta);
942 uint8_t *val8 = (uint8_t *)&val;
943 int idx = std::max(0, -start_pos);
944 for (int pos = std::max(0, start_pos); pos < end_pos; pos++) {
945 blitter->SetPixel(dst, idx, 0, PixelColour{val8[idx]});
946 idx++;
947 }
948 /* Switch to next tile in the column */
949 } while (xc += this->zoom, yc += this->zoom, dst = blitter->MoveTo(dst, pitch, 0), --reps != 0);
950 }
951
957 void DrawVehicles(const DrawPixelInfo *dpi, Blitter *blitter) const
958 {
959 for (const Vehicle *v : Vehicle::Iterate()) {
960 if (v->type == VehicleType::Effect) continue;
961 if (v->vehstatus.Any({VehState::Hidden, VehState::Unclickable})) continue;
962
963 /* Remap into flat coordinates. */
964 Point pt = this->RemapTile(v->x_pos / (int)TILE_SIZE, v->y_pos / (int)TILE_SIZE);
965
966 int y = pt.y - dpi->top;
967 if (!IsInsideMM(y, 0, dpi->height)) continue; // y is out of bounds.
968
969 bool skip = false; // Default is to draw both pixels.
970 int x = pt.x - this->subscroll - 3 - dpi->left; // Offset X coordinate.
971 if (x < 0) {
972 /* if x+1 is 0, that means we're on the very left edge,
973 * and should thus only draw a single pixel */
974 if (++x != 0) continue;
975 skip = true;
976 } else if (x >= dpi->width - 1) {
977 /* Check if we're at the very right edge, and if so draw only a single pixel */
978 if (x != dpi->width - 1) continue;
979 skip = true;
980 }
981
982 /* Calculate pointer to pixel and the colour */
983 PixelColour colour = (this->map_type == SmallMapType::Vehicles) ? _vehicle_type_colours[v->type] : PC_WHITE;
984
985 /* And draw either one or two pixels depending on clipping */
986 blitter->SetPixel(dpi->dst_ptr, x, y, colour);
987 if (!skip) blitter->SetPixel(dpi->dst_ptr, x + 1, y, colour);
988 }
989 }
990
996 void DrawTowns(const DrawPixelInfo *dpi, const int vertical_padding) const
997 {
998 for (const Town *t : Town::Iterate()) {
999 /* Remap the town coordinate */
1000 Point pt = this->RemapTile(TileX(t->xy), TileY(t->xy));
1001 int x = pt.x - this->subscroll - (t->cache.sign.width_small >> 1);
1002 int y = pt.y + vertical_padding;
1003
1004 /* Check if the town sign is within bounds */
1005 if (x + t->cache.sign.width_small > dpi->left &&
1006 x < dpi->left + dpi->width &&
1007 y + GetCharacterHeight(FontSize::Small) > dpi->top &&
1008 y < dpi->top + dpi->height) {
1009 /* And draw it. */
1010 DrawString(x, x + t->cache.sign.width_small, y, GetString(STR_SMALLMAP_TOWN, t->index));
1011 }
1012 }
1013 }
1014
1020 void DrawIndustryNames(const DrawPixelInfo *dpi, const int vertical_padding) const
1021 {
1022 if (this->map_type != SmallMapType::Industries) return;
1023
1024 for (const Industry *i : Industry::Iterate()) {
1026 if (!tbl.show_on_map) continue;
1027
1028 /* Industry names blink together with their blobs in the smallmap. */
1029 const bool is_blinking = i->type == _smallmap_industry_highlight && !_smallmap_industry_highlight_state;
1030 if (is_blinking) continue;
1031
1032 if (_industry_to_name_string_width[i->type] == 0) {
1034 }
1035 const uint16_t &legend_text_width = _industry_to_name_string_width[i->type];
1036
1037 /* Remap the industry coordinate */
1038 const TileIndex &tile = i->location.GetCentreTile();
1039 const Point pt = this->RemapTile(TileX(tile), TileY(tile));
1040 const int x = pt.x - this->subscroll - (legend_text_width / 2);
1041 const int y = pt.y + vertical_padding;
1042
1043 /* Check if the industry name is within bounds */
1044 if (x + legend_text_width > dpi->left &&
1045 x < dpi->left + dpi->width &&
1046 y + GetCharacterHeight(FontSize::Small) > dpi->top &&
1047 y < dpi->top + dpi->height) {
1048
1049 /* And draw it. */
1050 DrawString(x, x + legend_text_width, y, tbl.legend, TextColour::White, AlignmentH::Start, false, FontSize::Small);
1051 }
1052 }
1053 }
1054
1067 {
1069 AutoRestoreBackup dpi_backup(_cur_dpi, dpi);
1070
1071 /* If freeform edges are off, draw infinite water off the edges of the map. */
1072 const PixelColour map_clear_color = (_settings_game.construction.freeform_edges ? PC_BLACK : PC_WATER);
1073 GfxFillRect(dpi->left, dpi->top, dpi->left + dpi->width - 1, dpi->top + dpi->height - 1, map_clear_color);
1074
1075 /* Which tile is displayed at (dpi->left, dpi->top)? */
1076 int dx;
1077 Point tile = this->PixelToTile(dpi->left, dpi->top, &dx);
1078 int tile_x = this->scroll_x / (int)TILE_SIZE + tile.x;
1079 int tile_y = this->scroll_y / (int)TILE_SIZE + tile.y;
1080
1081 void *ptr = blitter->MoveTo(dpi->dst_ptr, -dx - 4, 0);
1082 int x = - dx - 4;
1083 int y = 0;
1084
1085 for (;;) {
1086 /* Distance from left edge */
1087 if (x >= -3) {
1088 if (x >= dpi->width) break; // Exit the loop.
1089
1090 int end_pos = std::min(dpi->width, x + 4);
1091 int reps = (dpi->height - y + 1) / 2; // Number of lines.
1092 if (reps > 0) {
1093 this->DrawSmallMapColumn(ptr, tile_x, tile_y, dpi->pitch * 2, reps, x, end_pos, blitter);
1094 }
1095 }
1096
1097 if (y == 0) {
1098 tile_y += this->zoom;
1099 y++;
1100 ptr = blitter->MoveTo(ptr, 0, 1);
1101 } else {
1102 tile_x -= this->zoom;
1103 y--;
1104 ptr = blitter->MoveTo(ptr, 0, -1);
1105 }
1106 ptr = blitter->MoveTo(ptr, 2, 0);
1107 x += 2;
1108 }
1109
1110 /* Draw vehicles */
1111 if (this->map_type == SmallMapType::Contour || this->map_type == SmallMapType::Vehicles) this->DrawVehicles(dpi, blitter);
1112
1113 /* Draw link stat overlay */
1114 if (this->map_type == SmallMapType::LinkStats) this->overlay->Draw(dpi);
1115
1116 const int map_labels_vertical_padding = ScaleGUITrad(2);
1117
1118 /* Draw town names */
1119 if (this->show_towns) this->DrawTowns(dpi, map_labels_vertical_padding);
1120
1121 /* Draw industry names */
1122 if (this->show_ind_names) this->DrawIndustryNames(dpi, map_labels_vertical_padding);
1123
1124 /* Draw map indicators */
1125 this->DrawMapIndicators();
1126 }
1127
1134 Point RemapTile(int tile_x, int tile_y) const
1135 {
1136 int x_offset = tile_x - this->scroll_x / (int)TILE_SIZE;
1137 int y_offset = tile_y - this->scroll_y / (int)TILE_SIZE;
1138
1139 if (this->zoom == 1) return SmallmapRemapCoords(x_offset, y_offset);
1140
1141 /* For negative offsets, round towards -inf. */
1142 if (x_offset < 0) x_offset -= this->zoom - 1;
1143 if (y_offset < 0) y_offset -= this->zoom - 1;
1144
1145 return SmallmapRemapCoords(x_offset / this->zoom, y_offset / this->zoom);
1146 }
1147
1158 Point PixelToTile(int px, int py, int *sub, bool add_sub = true) const
1159 {
1160 if (add_sub) px += this->subscroll; // Total horizontal offset.
1161
1162 /* For each two rows down, add a x and a y tile, and
1163 * For each four pixels to the right, move a tile to the right. */
1164 Point pt = {((py >> 1) - (px >> 2)) * this->zoom, ((py >> 1) + (px >> 2)) * this->zoom};
1165 px &= 3;
1166
1167 if (py & 1) { // Odd number of rows, handle the 2 pixel shift.
1168 if (px < 2) {
1169 pt.x += this->zoom;
1170 px += 2;
1171 } else {
1172 pt.y += this->zoom;
1173 px -= 2;
1174 }
1175 }
1176
1177 *sub = px;
1178 return pt;
1179 }
1180
1190 Point ComputeScroll(int tx, int ty, int x, int y, int *sub) const
1191 {
1192 assert(x >= 0 && y >= 0);
1193
1194 int new_sub;
1195 Point tile_xy = PixelToTile(x, y, &new_sub, false);
1196 tx -= tile_xy.x;
1197 ty -= tile_xy.y;
1198
1199 Point scroll;
1200 if (new_sub == 0) {
1201 *sub = 0;
1202 scroll.x = (tx + this->zoom) * TILE_SIZE;
1203 scroll.y = (ty - this->zoom) * TILE_SIZE;
1204 } else {
1205 *sub = 4 - new_sub;
1206 scroll.x = (tx + 2 * this->zoom) * TILE_SIZE;
1207 scroll.y = (ty - 2 * this->zoom) * TILE_SIZE;
1208 }
1209 return scroll;
1210 }
1211
1218 void SetZoomLevel(ZoomLevelChange change, const Point *zoom_pt)
1219 {
1220 static const int zoomlevels[] = {1, 2, 4, 6, 8}; // Available zoom levels. Bigger number means more zoom-out (further away).
1221 static const int MIN_ZOOM_INDEX = 0;
1222 static const int MAX_ZOOM_INDEX = lengthof(zoomlevels) - 1;
1223
1224 int new_index, cur_index, sub;
1225 Point tile;
1226 switch (change) {
1228 cur_index = - 1; // Definitely different from new_index.
1229 new_index = MIN_ZOOM_INDEX;
1230 tile.x = tile.y = 0;
1231 break;
1232
1235 for (cur_index = MIN_ZOOM_INDEX; cur_index <= MAX_ZOOM_INDEX; cur_index++) {
1236 if (this->zoom == zoomlevels[cur_index]) break;
1237 }
1238 assert(cur_index <= MAX_ZOOM_INDEX);
1239
1240 tile = this->PixelToTile(zoom_pt->x, zoom_pt->y, &sub);
1241 new_index = Clamp(cur_index + ((change == ZoomLevelChange::ZoomIn) ? -1 : 1), MIN_ZOOM_INDEX, MAX_ZOOM_INDEX);
1242 break;
1243
1244 default: NOT_REACHED();
1245 }
1246
1247 if (new_index != cur_index) {
1248 this->zoom = zoomlevels[new_index];
1249 if (cur_index >= 0) {
1250 Point new_tile = this->PixelToTile(zoom_pt->x, zoom_pt->y, &sub);
1251 this->SetNewScroll(this->scroll_x + (tile.x - new_tile.x) * TILE_SIZE,
1252 this->scroll_y + (tile.y - new_tile.y) * TILE_SIZE, sub);
1253 } else if (this->map_type == SmallMapType::LinkStats) {
1254 this->overlay->SetDirty();
1255 }
1256 this->SetWidgetDisabledState(WID_SM_ZOOM_IN, this->zoom == zoomlevels[MIN_ZOOM_INDEX]);
1257 this->SetWidgetDisabledState(WID_SM_ZOOM_OUT, this->zoom == zoomlevels[MAX_ZOOM_INDEX]);
1258 this->SetDirty();
1259 }
1260 }
1261
1266 {
1267 CargoTypes cargo_mask{};
1268 for (int i = 0; i != _smallmap_cargo_count; ++i) {
1269 if (_legend_linkstats[i].show_on_map) cargo_mask.Set(_legend_linkstats[i].cargo_type);
1270 }
1271 this->overlay->SetCargoMask(cargo_mask);
1272 }
1273
1278 {
1279 StringID legend_tooltip;
1280 StringID enable_all_tooltip;
1281 StringID disable_all_tooltip;
1282 int industry_names_select_plane;
1283 int select_buttons_plane;
1284 switch (this->map_type) {
1286 legend_tooltip = STR_SMALLMAP_TOOLTIP_INDUSTRY_SELECTION;
1287 enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_INDUSTRIES;
1288 disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_INDUSTRIES;
1289 industry_names_select_plane = 0;
1290 select_buttons_plane = 0;
1291 break;
1292
1294 legend_tooltip = STR_SMALLMAP_TOOLTIP_COMPANY_SELECTION;
1295 enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_COMPANIES;
1296 disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_COMPANIES;
1297 industry_names_select_plane = SZSP_NONE;
1298 select_buttons_plane = 0;
1299 break;
1300
1302 legend_tooltip = STR_SMALLMAP_TOOLTIP_CARGO_SELECTION;
1303 enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_CARGOS;
1304 disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_CARGOS;
1305 industry_names_select_plane = SZSP_NONE;
1306 select_buttons_plane = 0;
1307 break;
1308
1309 default:
1310 legend_tooltip = STR_NULL;
1311 enable_all_tooltip = STR_NULL;
1312 disable_all_tooltip = STR_NULL;
1313 industry_names_select_plane = SZSP_NONE;
1314 select_buttons_plane = 1;
1315 break;
1316 }
1317
1318 this->GetWidget<NWidgetCore>(WID_SM_LEGEND)->SetToolTip(legend_tooltip);
1319 this->GetWidget<NWidgetCore>(WID_SM_ENABLE_ALL)->SetStringTip(STR_SMALLMAP_ENABLE_ALL, enable_all_tooltip);
1320 this->GetWidget<NWidgetCore>(WID_SM_DISABLE_ALL)->SetStringTip(STR_SMALLMAP_DISABLE_ALL, disable_all_tooltip);
1321 this->GetWidget<NWidgetStacked>(WID_SM_SHOW_IND_NAMES_SEL)->SetDisplayedPlane(industry_names_select_plane);
1322 this->GetWidget<NWidgetStacked>(WID_SM_SELECT_BUTTONS)->SetDisplayedPlane(select_buttons_plane);
1323 }
1324
1330 uint32_t GetTileColours(const TileArea &ta) const
1331 {
1332 int importance = 0;
1333 TileIndex tile = INVALID_TILE; // Position of the most important tile.
1334 TileType et = TileType::Void; // Effective tile type at that position.
1335
1336 for (TileIndex ti : ta) {
1337 TileType ttype = GetTileType(ti);
1338
1339 switch (ttype) {
1342
1343 switch (tt) {
1344 case TransportType::Rail: ttype = TileType::Railway; break;
1345 case TransportType::Road: ttype = TileType::Road; break;
1346 default: ttype = TileType::Water; break;
1347 }
1348 break;
1349 }
1350
1351 case TileType::Industry:
1352 /* Special handling of industries while in "Industries" smallmap view. */
1353 if (this->map_type == SmallMapType::Industries) {
1354 /* If industry is allowed to be seen, use its colour on the map.
1355 * This has the highest priority above any value in _tiletype_importance. */
1356 IndustryType type = Industry::GetByTile(ti)->type;
1357 if (_legend_from_industries[_industry_to_list_pos[type]].show_on_map) {
1358 if (type == _smallmap_industry_highlight) {
1359 if (_smallmap_industry_highlight_state) return MKCOLOUR_XXXX(PC_WHITE);
1360 } else {
1361 return GetIndustrySpec(type)->map_colour.p * 0x01010101;
1362 }
1363 }
1364 /* Otherwise make it disappear */
1366 }
1367 break;
1368
1369 default:
1370 break;
1371 }
1372
1373 if (_tiletype_importance[ttype] > importance) {
1374 importance = _tiletype_importance[ttype];
1375 tile = ti;
1376 et = ttype;
1377 }
1378 }
1379
1380 switch (this->map_type) {
1382 return GetSmallMapContoursPixels(tile, et);
1383
1385 return GetSmallMapVehiclesPixels(tile, et);
1386
1388 return GetSmallMapIndustriesPixels(tile, et);
1389
1391 return GetSmallMapLinkStatsPixels(tile, et);
1392
1394 return GetSmallMapRoutesPixels(tile, et);
1395
1397 return GetSmallMapVegetationPixels(tile, et);
1398
1401
1402 default: NOT_REACHED();
1403 }
1404 }
1405
1412 {
1414 uint line = (pt.y - wi->pos_y - WidgetDimensions::scaled.framerect.top) / GetCharacterHeight(FontSize::Small);
1415 uint columns = this->GetNumberColumnsLegend(wi->current_x);
1416 uint number_of_rows = this->GetNumberRowsLegend(columns);
1417 if (line >= number_of_rows) return -1;
1418
1419 bool rtl = _current_text_dir == TD_RTL;
1420 int x = pt.x - wi->pos_x;
1421 if (rtl) x = wi->current_x - x;
1422 uint column = (x - WidgetDimensions::scaled.framerect.left) / this->column_width;
1423
1424 return (column * number_of_rows) + line;
1425 }
1426
1429 {
1430 if (this->map_type == SmallMapType::LinkStats) {
1431 CompanyMask company_mask = this->GetOverlayCompanyMask();
1432 if (this->overlay->GetCompanyMask() != company_mask) {
1433 this->overlay->SetCompanyMask(company_mask);
1434 } else {
1435 this->overlay->SetDirty();
1436 }
1437 }
1438 }
1439
1441 void Blink()
1442 {
1443 if (_smallmap_industry_highlight == IT_INVALID) return;
1444
1446
1447 this->UpdateLinks();
1448 this->SetDirty();
1449 }
1450
1453 {
1454 if (_smallmap_industry_highlight != IT_INVALID) return;
1455
1456 this->UpdateLinks();
1457 this->SetDirty();
1458 }
1459
1460public:
1461 friend class NWidgetSmallmapDisplay;
1462
1463 SmallMapWindow(WindowDesc &desc, int window_number) : Window(desc)
1464 {
1465 _smallmap_industry_highlight = IT_INVALID;
1466 this->overlay = std::make_unique<LinkGraphOverlay>(this, WID_SM_MAP, CargoTypes{}, this->GetOverlayCompanyMask(), 1);
1467 this->CreateNestedTree();
1469
1471
1473
1476
1477 this->SetupWidgetData();
1479
1480 this->SetZoomLevel(ZoomLevelChange::Init, nullptr);
1482 this->SetOverlayCargoMask();
1483 }
1484
1489 {
1490 const Viewport &vp = *GetMainWindow()->viewport;
1491 Point viewport_center = InverseRemapCoords2(vp.virtual_left + vp.virtual_width / 2, vp.virtual_top + vp.virtual_height / 2);
1492
1493 int sub;
1494 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1495 Point sxy = this->ComputeScroll(viewport_center.x / (int)TILE_SIZE, viewport_center.y / (int)TILE_SIZE,
1496 std::max(0, (int)wid->current_x / 2 - 2), wid->current_y / 2, &sub);
1497 this->SetNewScroll(sxy.x, sxy.y, sub);
1498 this->SetDirty();
1499 }
1500
1507 {
1508 TileIndex tile = st->spread.GetCentreTile();
1509 Point ret = this->RemapTile(TileX(tile), TileY(tile));
1510
1511 /* Same magic 3 as in DrawVehicles; that's where I got it from.
1512 * No idea what it is, but without it the result looks bad.
1513 */
1514 ret.x -= 3 + this->subscroll;
1515 return ret;
1516 }
1517
1518 void Close([[maybe_unused]] int data) override
1519 {
1520 this->BreakIndustryChainLink();
1521 this->Window::Close();
1522 }
1523
1524 std::string GetWidgetString(WidgetID widget, StringID stringid) const override
1525 {
1526 switch (widget) {
1527 case WID_SM_CAPTION:
1528 return GetString(STR_SMALLMAP_CAPTION, STR_SMALLMAP_TYPE_CONTOURS + to_underlying(this->map_type));
1529
1530 default:
1531 return this->Window::GetWidgetString(widget, stringid);
1532 }
1533 }
1534
1535 void OnInit() override
1536 {
1537 uint min_width = 0;
1538 this->min_number_of_columns = INDUSTRY_MIN_NUMBER_OF_COLUMNS;
1539 this->min_number_of_fixed_rows = lengthof(_linkstat_colours_in_legenda);
1541 uint height = 0;
1542 uint num_columns = 1;
1543 for (const LegendAndColour &tbl : _legend_table[i]) {
1544 if (tbl.end) break;
1545 std::string str;
1546 if (i == SmallMapType::Industries) {
1547 str = GetString(STR_SMALLMAP_INDUSTRY, tbl.legend, IndustryPool::MAX_SIZE);
1548 } else if (i == SmallMapType::LinkStats) {
1549 str = GetString(STR_SMALLMAP_LINKSTATS, tbl.legend);
1550 } else if (i == SmallMapType::Owners) {
1551 if (tbl.company != CompanyID::Invalid()) {
1552 if (!Company::IsValidID(tbl.company)) {
1553 /* Rebuild the owner legend. */
1555 this->OnInit();
1556 return;
1557 }
1558 /* Non-fixed legend entries for the owner view. */
1559 str = GetString(STR_SMALLMAP_COMPANY, tbl.company);
1560 } else {
1561 str = GetString(tbl.legend);
1562 }
1563 } else {
1564 if (tbl.col_break) {
1565 this->min_number_of_fixed_rows = std::max(this->min_number_of_fixed_rows, height);
1566 height = 0;
1567 num_columns++;
1568 }
1569 height++;
1570 if (i == SmallMapType::Contour) {
1571 str = GetString(tbl.legend, tbl.height * TILE_HEIGHT_STEP);
1572 } else {
1573 str = GetString(tbl.legend);
1574 }
1575 }
1576 min_width = std::max(GetStringBoundingBox(str).width, min_width);
1577 }
1578 this->min_number_of_fixed_rows = std::max(this->min_number_of_fixed_rows, height);
1579 this->min_number_of_columns = std::max(this->min_number_of_columns, num_columns);
1580 }
1581
1582 /* Width of the legend blob. */
1583 this->legend_width = GetCharacterHeight(FontSize::Small) * 9 / 6;
1584
1585 /* The width of a column is the minimum width of all texts + the size of the blob + some spacing */
1586 this->column_width = min_width + WidgetDimensions::scaled.hsep_normal + this->legend_width + WidgetDimensions::scaled.framerect.Horizontal();
1587
1588 /* Cached string widths of industry names in the smallmap. Calculation is deferred to DrawIndustryNames(). */
1589 std::fill(std::begin(_industry_to_name_string_width), std::end(_industry_to_name_string_width), 0);
1590 }
1591
1592 void OnPaint() override
1593 {
1594 if (this->map_type == SmallMapType::Owners) {
1595 for (const LegendAndColour &tbl : _legend_table[this->map_type]) {
1596 if (tbl.end) break;
1597 if (tbl.company != CompanyID::Invalid() && !Company::IsValidID(tbl.company)) {
1598 /* Rebuild the owner legend. */
1600 this->InvalidateData(1);
1601 break;
1602 }
1603 }
1604 }
1605
1606 this->DrawWidgets();
1607 }
1608
1616 void DrawLegend(const Rect &text, const Rect &icon, bool highlight, std::string_view string) const
1617 {
1618 if (!highlight) {
1619 /* Simply draw the string, not the black border of the legend colour.
1620 * This will enforce the idea of the disabled item */
1621 DrawString(text, string, TextColour::Grey);
1622 } else {
1623 DrawString(text, string, TextColour::Black);
1624 GfxFillRect(icon, PC_BLACK); // Outer border of the legend colour
1625 }
1626 }
1627
1628 void DrawWidget(const Rect &r, WidgetID widget) const override
1629 {
1630 switch (widget) {
1631 case WID_SM_MAP: {
1632 Rect ir = r.Shrink(WidgetDimensions::scaled.bevel);
1633 DrawPixelInfo new_dpi;
1634 if (!FillDrawPixelInfo(&new_dpi, ir)) return;
1635 this->DrawSmallMap(&new_dpi);
1636 break;
1637 }
1638
1639 case WID_SM_LEGEND: {
1640 uint columns = this->GetNumberColumnsLegend(r.Width());
1641 uint number_of_rows = this->GetNumberRowsLegend(columns);
1642 bool rtl = _current_text_dir == TD_RTL;
1643 uint i = 0; // Row counter for industry legend.
1644 uint row_height = GetCharacterHeight(FontSize::Small);
1645 int padding = ScaleGUITrad(1);
1646
1647 Rect origin = r.WithWidth(this->column_width, rtl).Shrink(WidgetDimensions::scaled.framerect).WithHeight(row_height);
1648 Rect text = origin.Indent(this->legend_width + WidgetDimensions::scaled.hsep_normal, rtl);
1649 Rect icon = origin.WithWidth(this->legend_width, rtl).Shrink(0, padding, 0, 0);
1650
1651 for (const LegendAndColour &tbl : _legend_table[this->map_type]) {
1652 if (tbl.end) break;
1653 if (tbl.col_break || ((this->map_type == SmallMapType::Industries || this->map_type == SmallMapType::Owners || this->map_type == SmallMapType::LinkStats) && i++ >= number_of_rows)) {
1654 /* Column break needed, continue at top, COLUMN_WIDTH pixels
1655 * (one "row") to the right. */
1656 int x = rtl ? -(int)this->column_width : this->column_width;
1657 int y = origin.top - text.top;
1658 text = text.Translate(x, y);
1659 icon = icon.Translate(x, y);
1660 i = 1;
1661 }
1662
1663 PixelColour legend_colour = tbl.colour;
1664
1665 switch (this->map_type) {
1667 /* Industry name must be formatted, since it's not in tiny font in the specs.
1668 * So, draw with a parameter and use the STR_SMALLMAP_INDUSTRY string, which is tiny font */
1669 if (tbl.show_on_map && tbl.industry_type == _smallmap_industry_highlight) {
1671 }
1672 this->DrawLegend(text, icon, tbl.show_on_map, GetString(STR_SMALLMAP_INDUSTRY, tbl.legend, Industry::GetIndustryTypeCount(tbl.industry_type)));
1673 break;
1674
1676 this->DrawLegend(text, icon, tbl.show_on_map, GetString(STR_SMALLMAP_LINKSTATS, tbl.legend));
1677 break;
1678
1680 if (tbl.company != CompanyID::Invalid()) {
1681 this->DrawLegend(text, icon, tbl.show_on_map, GetString(STR_SMALLMAP_COMPANY, tbl.company));
1682 break;
1683 }
1684 [[fallthrough]];
1685
1686 default:
1687 /* Anything that is not an industry or a company is using normal process */
1688 GfxFillRect(icon, PC_BLACK);
1689 if (this->map_type == SmallMapType::Contour) {
1690 DrawString(text, GetString(tbl.legend, tbl.height * TILE_HEIGHT_STEP));
1691 } else {
1692 DrawString(text, tbl.legend);
1693 }
1694 break;
1695 }
1696 GfxFillRect(icon.Shrink(WidgetDimensions::scaled.bevel), legend_colour); // Legend colour
1697
1698 text = text.Translate(0, row_height);
1699 icon = icon.Translate(0, row_height);
1700 }
1701 }
1702 }
1703 }
1704
1705 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
1706 {
1707 switch (widget) {
1708 case WID_SM_MAP: { // Map window
1709 if (click_count > 0) this->mouse_capture_widget = widget;
1710
1711 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1712 Window *w = GetMainWindow();
1713 int sub;
1714 pt = this->PixelToTile(pt.x - wid->pos_x, pt.y - wid->pos_y, &sub);
1715 ScrollWindowTo(this->scroll_x + pt.x * TILE_SIZE, this->scroll_y + pt.y * TILE_SIZE, -1, w);
1716
1717 this->SetDirty();
1718 break;
1719 }
1720
1721 case WID_SM_ZOOM_IN:
1722 case WID_SM_ZOOM_OUT: {
1723 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1724 Point zoom_pt = { (int)wid->current_x / 2, (int)wid->current_y / 2};
1726 SndClickBeep();
1727 break;
1728 }
1729
1730 case WID_SM_CONTOUR: // Show land contours
1731 case WID_SM_VEHICLES: // Show vehicles
1732 case WID_SM_INDUSTRIES: // Show industries
1733 case WID_SM_LINKSTATS: // Show route map
1734 case WID_SM_ROUTES: // Show transport routes
1735 case WID_SM_VEGETATION: // Show vegetation
1736 case WID_SM_OWNERS: // Show land owners
1737 this->SwitchMapType(static_cast<SmallMapType>(widget - WID_SM_CONTOUR));
1738 SndClickBeep();
1739 break;
1740
1741 case WID_SM_CENTERMAP: // Center the smallmap again
1744 break;
1745
1746 case WID_SM_TOGGLETOWNNAME: // Toggle town names
1747 this->show_towns = !this->show_towns;
1748 this->SetWidgetLoweredState(WID_SM_TOGGLETOWNNAME, this->show_towns);
1749
1750 this->SetDirty();
1751 SndClickBeep();
1752 break;
1753
1754 case WID_SM_SHOW_IND_NAMES: // Toggle industry names
1755 this->show_ind_names = !this->show_ind_names;
1756 this->SetWidgetLoweredState(WID_SM_SHOW_IND_NAMES, this->show_ind_names);
1757
1758 this->SetDirty();
1759 SndClickBeep();
1760 break;
1761
1762 case WID_SM_LEGEND: // Legend
1763 if (this->map_type == SmallMapType::Industries || this->map_type == SmallMapType::LinkStats || this->map_type == SmallMapType::Owners) {
1764 int click_pos = this->GetPositionOnLegend(pt);
1765 if (click_pos < 0) break;
1766
1767 /* If industry type small map*/
1768 if (this->map_type == SmallMapType::Industries) {
1769 /* If click on industries label, find right industry type and enable/disable it. */
1770 if (click_pos < _smallmap_industry_count) {
1772 }
1773 } else if (this->map_type == SmallMapType::LinkStats) {
1774 if (click_pos < _smallmap_cargo_count) {
1776 this->SetOverlayCargoMask();
1777 }
1778 } else if (this->map_type == SmallMapType::Owners) {
1779 if (click_pos < _smallmap_company_count) {
1780 this->SelectLegendItem(click_pos, _legend_land_owners, _smallmap_company_count, NUM_NO_COMPANY_ENTRIES);
1781 }
1782 }
1783 this->SetDirty();
1784 }
1785 break;
1786
1787 case WID_SM_ENABLE_ALL:
1788 case WID_SM_DISABLE_ALL: {
1789 LegendAndColour *tbl = nullptr;
1790 switch (this->map_type) {
1793 this->BreakIndustryChainLink();
1794 break;
1796 tbl = &(_legend_land_owners[NUM_NO_COMPANY_ENTRIES]);
1797 break;
1799 tbl = _legend_linkstats;
1800 break;
1801 default:
1802 NOT_REACHED();
1803 }
1804 for (;!tbl->end && tbl->legend != STR_LINKGRAPH_LEGEND_UNUSED; ++tbl) {
1805 tbl->show_on_map = (widget == WID_SM_ENABLE_ALL);
1806 }
1807 if (this->map_type == SmallMapType::LinkStats) this->SetOverlayCargoMask();
1808 this->SetDirty();
1809 break;
1810 }
1811
1812 case WID_SM_SHOW_HEIGHT: // Enable/disable showing of heightmap.
1815 this->SetDirty();
1816 break;
1817 }
1818 }
1819
1828 void OnInvalidateData(int data = 0, bool gui_scope = true) override
1829 {
1830 if (!gui_scope) return;
1831
1832 switch (data) {
1833 case 1:
1834 /* The owner legend has already been rebuilt. */
1835 this->ReInit();
1836 break;
1837
1838 case 0: {
1839 extern std::bitset<NUM_INDUSTRYTYPES> _displayed_industries;
1841
1842 for (int i = 0; i != _smallmap_industry_count; i++) {
1843 _legend_from_industries[i].show_on_map = _displayed_industries.test(_legend_from_industries[i].industry_type);
1844 }
1845 break;
1846 }
1847
1848 case 2:
1850 break;
1851
1852 default: NOT_REACHED();
1853 }
1854 this->SetDirty();
1855 }
1856
1857 bool OnRightClick(Point, WidgetID widget) override
1858 {
1859 if (widget != WID_SM_MAP || _scrolling_viewport) return false;
1860
1861 _scrolling_viewport = true;
1862 return true;
1863 }
1864
1865 void OnMouseWheel(int wheel, WidgetID widget) override
1866 {
1867 if (widget != WID_SM_MAP) return;
1868 if (_settings_client.gui.scrollwheel_scrolling != ScrollWheelScrolling::Off) {
1869 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1870 int cursor_x = _cursor.pos.x - this->left - wid->pos_x;
1871 int cursor_y = _cursor.pos.y - this->top - wid->pos_y;
1872 Point pt = {cursor_x, cursor_y};
1874 }
1875 }
1876
1877 void OnScroll(Point delta) override
1878 {
1879 if (_settings_client.gui.scroll_mode == ViewportScrollMode::ViewportRMBFixed || _settings_client.gui.scroll_mode == ViewportScrollMode::MapRMBFixed) _cursor.fix_at = true;
1880
1881 /* While tile is at (delta.x, delta.y)? */
1882 int sub;
1883 Point pt = this->PixelToTile(delta.x, delta.y, &sub);
1884 this->SetNewScroll(this->scroll_x + pt.x * TILE_SIZE, this->scroll_y + pt.y * TILE_SIZE, sub);
1885
1886 this->SetDirty();
1887 }
1888
1889 void OnMouseOver([[maybe_unused]] Point pt, WidgetID widget) override
1890 {
1891 IndustryType new_highlight = IT_INVALID;
1892 if (widget == WID_SM_LEGEND && this->map_type == SmallMapType::Industries) {
1893 int industry_pos = GetPositionOnLegend(pt);
1894 if (industry_pos >= 0 && industry_pos < _smallmap_industry_count) {
1895 new_highlight = _legend_from_industries[industry_pos].industry_type;
1896 }
1897 }
1898 if (new_highlight != _smallmap_industry_highlight) {
1899 _smallmap_industry_highlight = new_highlight;
1901 this->SetDirty();
1902 }
1903 }
1904
1905};
1906
1908bool SmallMapWindow::show_towns = true;
1911
1916class NWidgetSmallmapButtons : public NWidgetContainer {
1918 static constexpr std::tuple<WidgetID, WidgetType, SpriteID, StringID> buttons[] = {
1919 {WID_SM_ZOOM_IN, WWT_PUSHIMGBTN, SPR_IMG_ZOOMIN, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_IN},
1920 {WID_SM_ZOOM_OUT, WWT_PUSHIMGBTN, SPR_IMG_ZOOMOUT, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_OUT},
1921 {WID_SM_CENTERMAP, WWT_PUSHIMGBTN, SPR_IMG_SMALLMAP, STR_SMALLMAP_CENTER_TOOLTIP},
1922 {WID_SM_TOGGLETOWNNAME, WWT_IMGBTN, SPR_IMG_TOWN, STR_SMALLMAP_TOOLTIP_TOGGLE_TOWN_NAMES_ON_OFF},
1923 {INVALID_WIDGET, WWT_PANEL, SPR_EMPTY, STR_NULL}, // Resizable spacer between top and bottom halves.
1924 {WID_SM_BLANK, WWT_IMGBTN, SPR_EMPTY, STR_NULL}, // Optional spacer in 2-column layout only.
1925 {WID_SM_LINKSTATS, WWT_IMGBTN, SPR_IMG_CARGOFLOW, STR_SMALLMAP_TOOLTIP_SHOW_LINK_STATS_ON_MAP},
1926 {WID_SM_ROUTES, WWT_IMGBTN, SPR_IMG_SHOW_ROUTES, STR_SMALLMAP_TOOLTIP_SHOW_TRANSPORT_ROUTES_ON},
1927 {WID_SM_VEGETATION, WWT_IMGBTN, SPR_IMG_PLANTTREES, STR_SMALLMAP_TOOLTIP_SHOW_VEGETATION_ON_MAP},
1928 {WID_SM_OWNERS, WWT_IMGBTN, SPR_IMG_COMPANY_GENERAL, STR_SMALLMAP_TOOLTIP_SHOW_LAND_OWNERS_ON_MAP},
1929 {WID_SM_CONTOUR, WWT_IMGBTN, SPR_IMG_SHOW_COUNTOURS, STR_SMALLMAP_TOOLTIP_SHOW_LAND_CONTOURS_ON_MAP},
1930 {WID_SM_VEHICLES, WWT_IMGBTN, SPR_IMG_SHOW_VEHICLES, STR_SMALLMAP_TOOLTIP_SHOW_VEHICLES_ON_MAP},
1931 {WID_SM_INDUSTRIES, WWT_IMGBTN, SPR_IMG_INDUSTRY, STR_SMALLMAP_TOOLTIP_SHOW_INDUSTRIES_ON_MAP},
1932 };
1933
1936
1937public:
1942 {
1943 for (const auto &[widget, tp, sprite, string] : buttons) {
1944 if (widget == INVALID_WIDGET) {
1945 auto leaf = std::make_unique<NWidgetBackground>(WWT_PANEL, Colours::Brown, widget);
1946 this->Add(std::move(leaf));
1947 } else {
1948 auto leaf = std::make_unique<NWidgetLeaf>(tp, Colours::Brown, widget, WidgetData{.sprite = sprite}, string);
1949 leaf->SetToolbarMinimalSize(1);
1950 this->Add(std::move(leaf));
1951 }
1952 }
1953 }
1954
1955 void SetupSmallestSize(Window *w) override
1956 {
1957 this->button.width = 0;
1958 this->button.height = 0;
1959
1960 this->num_buttons = 0;
1961 for (const auto &child_wid : this->children) {
1962 child_wid->SetupSmallestSize(w);
1963 this->button.width = std::max(this->button.width, child_wid->smallest_y + child_wid->padding.Vertical());
1964 this->button.height = std::max(this->button.height, child_wid->smallest_x + child_wid->padding.Horizontal());
1965 WidgetID index = child_wid->GetIndex();
1966 if (index != INVALID_WIDGET && index != WID_SM_BLANK) ++this->num_buttons;
1967 }
1968
1969 this->smallest_x = this->button.width * 2;
1970 this->smallest_y = this->button.height * CeilDiv(this->num_buttons, 2);
1971 this->fill_x = 0;
1972 this->fill_y = 1;
1973 this->resize_x = 0;
1974 this->resize_y = 1;
1975 }
1976
1977 std::pair<uint, uint> GetPreferredSizeForSize(uint given_width, uint given_height) override
1978 {
1979 if (given_height >= this->num_buttons * this->button.height) return {this->button.width, given_height};
1980 return {given_width, given_height};
1981 }
1982
1983 void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override
1984 {
1985 assert(given_width >= this->button.width && given_height >= this->smallest_y);
1986
1987 this->pos_x = x;
1988 this->pos_y = y;
1989 this->current_x = given_width;
1990 this->current_y = given_height;
1991
1992 uint child_x = 0;
1993 uint child_y = 0;
1994 uint remaining = CeilDiv(this->num_buttons, this->smallest_x == this->current_x ? 2 : 1);
1995
1996 for (auto &child : this->children) {
1997 if (child->type == WWT_PANEL) {
1998 /* Spacer widget needs to fill the remaining space. */
1999 uint height = given_height - child_y - (remaining * this->button.height);
2000 child->AssignSizePosition(sizing, x, y + child_y, given_width, height, rtl);
2001 child_y += height;
2002 } else if (given_width == this->button.width && child->GetIndex() == WID_SM_BLANK) {
2003 /* Blank button only appears in 2-column layout. */
2004 child->AssignSizePosition(sizing, x + child_x, y + child_y, 0, 0, rtl);
2005 } else {
2006 child->AssignSizePosition(sizing, x + child_x, y + child_y, child->smallest_x, child->smallest_y, rtl);
2007 child_x += child->current_x;
2008 if (child_x >= given_width) {
2009 child_x = 0;
2010 child_y += child->current_y;
2011 --remaining;
2012 }
2013 }
2014 }
2015 }
2016
2021 static std::unique_ptr<NWidgetBase> Make()
2022 {
2023 return std::make_unique<NWidgetSmallmapButtons>();
2024 }
2025};
2026
2035class NWidgetSmallmapDisplay : public NWidgetContainer {
2037public:
2038 NWidgetSmallmapDisplay() : NWidgetContainer(NWID_CUSTOM) {}
2039
2040 void SetupSmallestSize(Window *w) override
2041 {
2042 assert(this->children.size() == 2);
2043 NWidgetBase *display = this->children.front().get();
2044 NWidgetBase *bar = this->children.back().get();
2045
2046 display->SetupSmallestSize(w);
2047 bar->SetupSmallestSize(w);
2048
2049 this->smallmap_window = dynamic_cast<SmallMapWindow *>(w);
2050 assert(this->smallmap_window != nullptr);
2051 this->smallest_x = std::max(display->smallest_x, bar->smallest_x + smallmap_window->GetMinLegendWidth());
2052 this->smallest_y = display->smallest_y + std::max(bar->smallest_y, smallmap_window->GetLegendHeight(smallmap_window->min_number_of_columns));
2053 this->fill_x = std::max(display->fill_x, bar->fill_x);
2054 this->fill_y = (display->fill_y == 0 && bar->fill_y == 0) ? 0 : std::min(display->fill_y, bar->fill_y);
2055 this->resize_x = std::max(display->resize_x, bar->resize_x);
2056 this->resize_y = std::min(display->resize_y, bar->resize_y);
2057 this->ApplyAspectRatio();
2058 }
2059
2060 void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override
2061 {
2062 this->pos_x = x;
2063 this->pos_y = y;
2064 this->current_x = given_width;
2065 this->current_y = given_height;
2066
2067 assert(this->children.size() == 2);
2068 NWidgetBase *display = this->children.front().get();
2069 NWidgetBase *bar = this->children.back().get();
2070
2071 if (sizing == SizingType::Smallest) {
2072 this->smallest_x = given_width;
2073 this->smallest_y = given_height;
2074 /* Make display and bar exactly equal to their minimal size. */
2075 display->AssignSizePosition(SizingType::Smallest, x, y, display->smallest_x, display->smallest_y, rtl);
2076 bar->AssignSizePosition(SizingType::Smallest, x, y + display->smallest_y, bar->smallest_x, bar->smallest_y, rtl);
2077 }
2078
2079 uint bar_height = std::max(bar->smallest_y, this->smallmap_window->GetLegendHeight(this->smallmap_window->GetNumberColumnsLegend(given_width - bar->smallest_x)));
2080 uint display_height = given_height - bar_height;
2081 display->AssignSizePosition(SizingType::Resize, x, y, given_width, display_height, rtl);
2082 bar->AssignSizePosition(SizingType::Resize, x, y + display_height, given_width, bar_height, rtl);
2083 }
2084};
2085
2095
2097static constexpr std::initializer_list<NWidgetPart> _nested_smallmap_bar = {
2099 EndContainer(),
2100};
2101
2102static std::unique_ptr<NWidgetBase> SmallMapDisplay()
2103{
2104 std::unique_ptr<NWidgetBase> map_display = std::make_unique<NWidgetSmallmapDisplay>();
2105
2106 map_display = MakeNWidgets(_nested_smallmap_display, std::move(map_display));
2107 map_display = MakeNWidgets(_nested_smallmap_bar, std::move(map_display));
2108 return map_display;
2109}
2110
2111static constexpr std::initializer_list<NWidgetPart> _nested_smallmap_widgets = {
2118 EndContainer(),
2119 NWidgetFunction(SmallMapDisplay), // Smallmap display and legend bar + image buttons.
2120 /* Bottom button row and resize box. */
2126 NWidget(WWT_TEXTBTN, Colours::Brown, WID_SM_SHOW_HEIGHT), SetStringTip(STR_SMALLMAP_SHOW_HEIGHT, STR_SMALLMAP_TOOLTIP_SHOW_HEIGHT),
2127
2128 /* 'show industry names' button and container. Only shown for the industry map type. */
2130 NWidget(WWT_TEXTBTN, Colours::Brown, WID_SM_SHOW_IND_NAMES), SetStringTip(STR_SMALLMAP_SHOW_INDUSTRY_NAMES, STR_SMALLMAP_TOOLTIP_SHOW_INDUSTRY_NAMES),
2131 EndContainer(),
2132
2134 EndContainer(),
2135 EndContainer(),
2137 EndContainer(),
2138 EndContainer(),
2140 EndContainer(),
2141};
2142
2145 WindowPosition::Automatic, "smallmap", 0, 0,
2146 WindowClass::SmallMap, WindowClass::None,
2147 {},
2148 _nested_smallmap_widgets
2149);
2150
2158
2167bool ScrollMainWindowTo(int x, int y, int z, bool instant)
2168{
2169 bool res = ScrollWindowTo(x, y, z, GetMainWindow(), instant);
2170
2171 /* If a user scrolls to a tile (via what way what so ever) and already is on
2172 * that tile (e.g.: pressed twice), move the smallmap to that location,
2173 * so you directly see where you are on the smallmap. */
2174
2175 if (res) return res;
2176
2177 SmallMapWindow *w = dynamic_cast<SmallMapWindow*>(FindWindowById(WindowClass::SmallMap, 0));
2178 if (w != nullptr) w->SmallMapCenterOnCurrentPos();
2179
2180 return res;
2181}
2182
2190{
2191 return static_cast<const SmallMapWindow *>(w)->GetStationMiddle(st);
2192}
Class for backupping variables and making sure they are restored later.
static constexpr CargoType INVALID_CARGO
Invalid marker for cargoes.
Definition cargo_type.h:83
EnumBitSet< CargoType, uint64_t > CargoTypes
Bitset of CargoType elements.
Definition cargo_type.h:115
static constexpr CargoType NUM_CARGO
Maximum number of cargo types in a game.
Definition cargo_type.h:77
CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:22
std::vector< const CargoSpec * > _sorted_cargo_specs
Cargo specifications sorted alphabetically by name.
constexpr Timpl & Set()
Set all bits.
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition factory.hpp:139
How all blitters should look like.
Definition base.hpp:28
virtual void * MoveTo(void *video, int x, int y)=0
Move the destination pointer the requested amount x and y, keeping in mind any pitch and bpp of the r...
virtual void SetPixel(void *video, int x, int y, PixelColour colour)=0
Draw a pixel with a given colour on the video-buffer.
Iterate a range of enum values.
An interval timer will fire every interval, and will continue to fire until it is deleted.
Definition timer.h:76
static const PixelColour LINK_COLOURS[][12]
Colours for the various "load" states of links.
Baseclass for nested widgets.
uint resize_x
Horizontal resize step (0 means not resizable).
uint fill_x
Horizontal fill stepsize (from initial size, 0 means not resizable).
virtual void SetupSmallestSize(Window *w)=0
Compute smallest size needed by the widget.
uint smallest_x
Smallest horizontal size of the widget in a filled window.
uint current_x
Current horizontal size (after resizing).
int pos_y
Vertical position of top-left corner of the widget in the window.
int pos_x
Horizontal position of top-left corner of the widget in the window.
virtual void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl)=0
Assign size and position to the widget.
uint smallest_y
Smallest vertical size of the widget in a filled window.
const WidgetID index
Index of the nested widget (INVALID_WIDGET means 'not used').
uint fill_y
Vertical fill stepsize (from initial size, 0 means not resizable).
uint resize_y
Vertical resize step (0 means not resizable).
uint current_y
Current vertical size (after resizing).
void Add(std::unique_ptr< NWidgetBase > &&wid)
Append widget wid to container.
Definition widget.cpp:1328
std::vector< std::unique_ptr< NWidgetBase > > children
Child widgets in container.
static std::unique_ptr< NWidgetBase > Make()
Create NWidgetSMallmapButtons widget.
uint num_buttons
The number of buttons excluding spacers.
static constexpr std::tuple< WidgetID, WidgetType, SpriteID, StringID > buttons[]
Smallmap button definitions.
std::pair< uint, uint > GetPreferredSizeForSize(uint given_width, uint given_height) override
Get a widget's preferred size for a given size.
void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override
Assign size and position to the widget.
Dimension button
The size of a button.
NWidgetSmallmapButtons()
Construct a new NWidgetSmallmapButtons object.
void SetupSmallestSize(Window *w) override
Compute smallest size needed by the widget.
Custom container class for displaying smallmap with a vertically resizing legend panel.
void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override
Assign size and position to the widget.
void SetupSmallestSize(Window *w) override
Compute smallest size needed by the widget.
const SmallMapWindow * smallmap_window
Window manager instance.
void ClampToMap()
Clamp the tile area to map borders.
TileIndex GetCentreTile() const
Get the centre tile.
PixelColour map_colour
Colour on mini-map.
Definition road.h:133
Class managing the smallmap window.
void SmallMapCenterOnCurrentPos()
Center the small map on the current center of the viewport.
void SetOverlayCargoMask()
Set the link graph overlay cargo mask from the legend.
void Close(int data) override
Hide the window and all its child windows, and mark them for a later deletion.
static SmallMapType map_type
Currently displayed legends.
void DrawLegend(const Rect &text, const Rect &icon, bool highlight, std::string_view string) const
Draw a string legend.
uint GetNumberColumnsLegend(uint width) const
Return number of columns that can be displayed in width pixels.
uint column_width
Width of a column in the WID_SM_LEGEND widget.
uint min_number_of_fixed_rows
Minimal number of rows in the legends for the fixed layouts only (all except SmallMapType::Industries...
void SelectLegendItem(int click_pos, LegendAndColour *legend, int end_legend_item, int begin_legend_item=0)
Select and toggle a legend item.
void SwitchMapType(SmallMapType map_type)
Select a new map type.
const IntervalTimer< TimerWindow > blink_interval
Blink the industries (if selected) on a regular interval.
void DrawSmallMap(DrawPixelInfo *dpi) const
Draws the small map.
Point ComputeScroll(int tx, int ty, int x, int y, int *sub) const
Compute base parameters of the smallmap such that tile (tx, ty) starts at pixel (x,...
Point GetStationMiddle(const Station *st) const
Get the center of the given station as point on the screen in the smallmap window.
void ForceRefresh()
Force a full refresh of the map.
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
uint GetMinLegendWidth() const
Compute minimal required width of the legends.
static bool show_towns
Display town names in the smallmap.
Point PixelToTile(int px, int py, int *sub, bool add_sub=true) const
Determine the tile relative to the base tile of the smallmap, and the pixel position at that tile for...
void Blink()
Blink the industries (if hover over an industry).
int32_t scroll_y
Vertical world coordinate of the base tile left of the top-left corner of the smallmap display.
int32_t scroll_x
Horizontal world coordinate of the base tile left of the top-left corner of the smallmap display.
void DrawIndustryNames(const DrawPixelInfo *dpi, const int vertical_padding) const
Adds industry names to the smallmap.
static void DrawVertMapIndicator(int x, int y, int y2)
Draws vertical part of map indicator.
uint GetLegendHeight(uint num_columns) const
Compute height given a number of columns.
uint min_number_of_columns
Minimal number of columns in legends.
int32_t subscroll
Number of pixels (0..3) between the right end of the base tile and the pixel at the top-left corner o...
CompanyMask GetOverlayCompanyMask() const
Get a bitmask for company links to be displayed.
uint32_t GetTileColours(const TileArea &ta) const
Decide which colours to show to the user for a group of tiles.
std::string GetWidgetString(WidgetID widget, StringID stringid) const override
Get the raw string for a widget.
void DrawTowns(const DrawPixelInfo *dpi, const int vertical_padding) const
Adds town names to the smallmap.
void OnPaint() override
The window must be repainted.
void SetZoomLevel(ZoomLevelChange change, const Point *zoom_pt)
Initialize or change the zoom level.
int GetPositionOnLegend(Point pt)
Determines the mouse position on the legend.
void OnMouseWheel(int wheel, WidgetID widget) override
The mouse wheel has been turned.
void OnMouseOver(Point pt, WidgetID widget) override
The mouse is currently moving over the window or has just moved outside of the window.
ZoomLevelChange
Available kinds of zoomlevel changes.
@ Init
Initialize zoom level.
void SetupWidgetData()
Function to set up widgets depending on the information being shown on the smallmap.
static bool show_ind_names
Display industry names in the smallmap.
void RebuildColourIndexIfNecessary()
Rebuilds the colour indices used for fast access to the smallmap contour colours based on the heightl...
void DrawSmallMapColumn(void *dst, uint xc, uint yc, int pitch, int reps, int start_pos, int end_pos, Blitter *blitter) const
Draws one column of tiles of the small map in a certain mode onto the screen buffer,...
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
static int map_height_limit
Currently used/cached map height limit.
Point RemapTile(int tile_x, int tile_y) const
Remap tile to location on this smallmap.
static void DrawHorizMapIndicator(int x, int x2, int y)
Draws horizontal part of map indicator.
void OnInit() override
Notification that the nested widget tree gets initialized.
void UpdateLinks()
Update all the links on the map.
static const uint INDUSTRY_MIN_NUMBER_OF_COLUMNS
Minimal number of columns in the WID_SM_LEGEND widget for the SmallMapType::Industries legend.
void OnScroll(Point delta) override
Handle the request for (viewport) scrolling.
void DrawWidget(const Rect &r, WidgetID widget) const override
Draw the contents of a nested widget.
uint legend_width
Width of legend 'blob'.
int zoom
Zoom level. Bigger number means more zoom-out (further away).
void DrawMapIndicators() const
Adds map indicators to the smallmap.
bool OnRightClick(Point, WidgetID widget) override
A click with the right mouse button has been made on the window.
void SetNewScroll(int sx, int sy, int sub)
Set new scroll_x, scroll_y, and subscroll values after limiting them such that the center of the smal...
uint GetNumberRowsLegend(uint columns) const
Get the number of rows in the legend from the number of columns.
const IntervalTimer< TimerWindow > refresh_interval
Update the whole map on a regular interval.
void DrawVehicles(const DrawPixelInfo *dpi, Blitter *blitter) const
Adds vehicles to the smallmap.
static void BreakIndustryChainLink()
Notify the industry chain window to stop sending newly selected industries.
A sort-of mixin that implements 'at(pos)' and 'operator[](pos)' only for a specific type.
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition window_gui.h:30
Map accessors for 'clear' tiles.
bool IsClearGround(Tile t, ClearGround ct)
Set the type of clear tile.
Definition clear_map.h:65
@ Grass
Plain grass with dirt transition (0-3).
Definition clear_map.h:22
ClearGround GetClearGround(Tile t)
Get the type of clear tile.
Definition clear_map.h:52
bool IsSnowTile(Tile t)
Test if a tile is covered with snow.
Definition clear_map.h:40
uint GetClearDensity(Tile t)
Get the density of a non-field clear tile.
Definition clear_map.h:77
Definition of stuff that is very close to a company, like the company struct itself.
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Functions related to companies.
static constexpr Owner OWNER_TOWN
A town owns the tile, or a town is expanding.
static constexpr Owner OWNER_NONE
The tile has no ownership.
static constexpr Owner OWNER_WATER
The tile/execution is done by "water".
Function to handling different endian machines.
Type (helpers) for enums.
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
Definition enum_type.hpp:21
EnumClassIndexContainer< std::array< T, to_underlying(N)>, Index > EnumIndexArray
A typedef for EnumClassIndexContainer using std::array as the backing container type.
Factory to 'query' all available blitters.
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition fontcache.cpp:88
@ Start
Align to the start, LTR/RTL aware.
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition gfx.cpp:894
void GfxFillRect(int left, int top, int right, int bottom, const std::variant< PixelColour, PaletteID > &colour, FillRectMode mode)
Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen.
Definition gfx.cpp:111
int DrawString(int left, int right, int top, std::string_view str, ExtendedTextColour colour, Alignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
Definition gfx.cpp:663
bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height)
Set up a clipping area for only drawing into a certain area.
Definition gfx.cpp:1567
bool IsCtrlPressed()
Test if the ctrl key is pressed.
Definition gfx_func.h:82
@ Small
Index of the small font in the font tables.
Definition gfx_type.h:250
@ Invalid
Invalid marker.
Definition gfx_type.h:302
@ Brown
Brown.
Definition gfx_type.h:298
@ White
White colour.
Definition gfx_type.h:330
@ Grey
Grey colour.
Definition gfx_type.h:332
@ Black
Black colour.
Definition gfx_type.h:334
constexpr NWidgetPart NWidgetFunction(NWidgetFunctionType *func_ptr)
Obtain a nested widget (sub)tree from an external source.
constexpr NWidgetPart SetFill(uint16_t fill_x, uint16_t fill_y)
Widget part function for setting filling.
constexpr NWidgetPart SetPadding(uint8_t top, uint8_t right, uint8_t bottom, uint8_t left)
Widget part function for setting additional space around a widget.
constexpr NWidgetPart SetStringTip(StringID string, StringID tip={})
Widget part function for setting the string and tooltip.
std::unique_ptr< NWidgetBase > MakeNWidgets(std::span< const NWidgetPart > nwid_parts, std::unique_ptr< NWidgetBase > &&container)
Construct a nested widget tree from an array of parts.
Definition widget.cpp:3431
constexpr NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
constexpr NWidgetPart NWidget(WidgetType tp, Colours col, WidgetID idx=INVALID_WIDGET)
Widget part function for starting a new 'real' widget.
constexpr NWidgetPart SetResize(int16_t dx, int16_t dy)
Widget part function for setting the resize step.
void SetDirty() const
Mark entire window as dirty (in need of re-paint).
Definition window.cpp:1013
The colour tables for heightmaps.
static const uint32_t _violet_map_heights[]
Height map colours for the violet colour scheme, ordered by height.
static const uint32_t _dark_green_map_heights[]
Height map colours for the dark green colour scheme, ordered by height.
static const uint32_t _green_map_heights[]
Height map colours for the green colour scheme, ordered by height.
Base of all industries.
bool IsTileForestIndustry(TileIndex tile)
Check whether the tile is a forest.
const IndustrySpec * GetIndustrySpec(IndustryType thistype)
Accessor for array _industry_specs.
std::array< IndustryType, NUM_INDUSTRYTYPES > _sorted_industry_types
Industry types sorted by name.
std::bitset< NUM_INDUSTRYTYPES > _displayed_industries
Communication from the industry chain window to the smallmap window about what industries to display.
static const IndustryType NUM_INDUSTRYTYPES
total number of industry types, new and old; limited to 240 because we need some special ids like IT_...
Point InverseRemapCoords2(int x, int y, bool clamp_to_map, bool *clamped)
Map 2D viewport or smallmap coordinate to 3D world or tile coordinate.
Functions related to OTTD's landscape.
Point InverseRemapCoords(int x, int y)
Map 2D viewport or smallmap coordinate to 3D world or tile coordinate.
Definition landscape.h:111
@ Arctic
Landscape with snow levels.
Declaration of linkgraph overlay GUI.
#define Point
Macro that prevents name conflicts between included headers.
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition map_func.h:376
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:429
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:419
constexpr bool IsInsideMM(const size_t x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
constexpr uint CeilDiv(uint a, uint b)
Computes ceil(a / b) for non-negative a and b.
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
PixelColour GetColourGradient(Colours colour, Shade shade)
Get colour gradient palette index.
Definition palette.cpp:393
static constexpr PixelColour PC_TREES
Green palette colour for trees.
@ Light
Light colour shade.
static constexpr PixelColour PC_VERY_LIGHT_YELLOW
Almost-white yellow palette colour.
static constexpr PixelColour PC_ROUGH_LAND
Dark green palette colour for rough land.
static constexpr PixelColour PC_YELLOW
Yellow palette colour.
static constexpr PixelColour PC_GREEN
Green palette colour.
static constexpr PixelColour PC_FIELDS
Light brown palette colour for fields.
static constexpr PixelColour PC_DARK_RED
Dark red palette colour.
static constexpr PixelColour PC_WATER
Dark blue palette colour for water.
static constexpr PixelColour PC_ORANGE
Orange palette colour.
static constexpr PixelColour PC_GRASS_LAND
Dark green palette colour for grass land.
static constexpr PixelColour PC_BARE_LAND
Brown palette colour for bare land.
static constexpr PixelColour PC_GREY
Grey palette colour.
static constexpr PixelColour PC_RED
Red palette colour.
static constexpr PixelColour PC_DARK_GREY
Dark grey palette colour.
static constexpr PixelColour PC_BLACK
Black palette colour.
static constexpr PixelColour PC_RAINFOREST
Pale green palette colour for rainforest.
static constexpr PixelColour PC_WHITE
White palette colour.
static constexpr PixelColour PC_VERY_DARK_BROWN
Almost-black brown palette colour.
static constexpr PixelColour PC_LIGHT_BLUE
Light blue palette colour.
const RailTypeInfo * GetRailTypeInfo(RailType railtype)
Returns a pointer to the Railtype information for a given railtype.
Definition rail.h:320
RailType GetRailType(Tile t)
Gets the rail type of the given tile.
Definition rail_map.h:115
const RoadTypeInfo * GetRoadTypeInfo(RoadType roadtype)
Returns a pointer to the Roadtype information for a given roadtype.
Definition road.h:217
RoadType GetRoadTypeRoad(Tile t)
Get the road type for RoadTramType being RoadTramType::Road.
Definition road_map.h:152
RoadType GetRoadTypeTram(Tile t)
Get the road type for RoadTramType being RoadTramType::Tram.
Definition road_map.h:163
Owner GetRoadOwner(Tile t, RoadTramType rtt)
Get the owner of a specific road type.
Definition road_map.h:244
bool HasRoadTypeRoad(Tile t)
Check if a tile has a road type when RoadTramType is RoadTramType::Road.
Definition road_map.h:200
@ INVALID_ROADTYPE
flag for invalid roadtype
Definition road_type.h:29
@ Tram
Tram type.
Definition road_type.h:40
@ Road
Road type.
Definition road_type.h:39
A number of safeguards to prevent using unsafe methods.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:61
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
@ MapRMBFixed
Map moves with mouse movement on holding right mouse button, cursor position is fixed.
@ ViewportRMBFixed
Viewport moves with mouse movement on holding right mouse button, cursor position is fixed.
@ Off
Scroll wheel has no effect.
bool IsSlopeWithOneCornerRaised(Slope s)
Tests if a specific slope has exactly one corner raised.
Definition slope_func.h:98
void ShowSmallMap()
Show the smallmap window.
static const EnumIndexArray< AndOr, TileType, TileType::MaxSize > _smallmap_contours_andor
Colour masks for "Contour" and "Routes" modes.
static uint32_t GetSmallMapLinkStatsPixels(TileIndex tile, TileType t)
Return the colour a tile would be displayed with in the small map in mode "link stats".
static uint32_t GetSmallMapContoursPixels(TileIndex tile, TileType t)
Return the colour a tile would be displayed with in the small map in mode "Contour".
void BuildLandLegend()
(Re)build the colour tables for the legends.
static uint32_t GetSmallMapVehiclesPixels(TileIndex tile, TileType t)
Return the colour a tile would be displayed with in the small map in mode "Vehicles".
static LegendAndColour _legend_from_industries[NUM_INDUSTRYTYPES+1]
Allow room for all industries, plus a terminator entry This is required in order to have the industry...
static const EnumIndexArray< std::span< const LegendAndColour >, SmallMapType, SmallMapType::End > _legend_table
Legend to use for each SmallMapType.
static constexpr VehicleTypeIndexArray< PixelColour, VehicleType::End > _vehicle_type_colours
Vehicle colours in SmallMapType::Vehicles mode.
static const uint8_t _linkstat_colours_in_legenda[]
Link stat colours shown in legenda.
static uint _industry_to_list_pos[NUM_INDUSTRYTYPES]
For connecting industry type to position in industries list(small map legend).
static const EnumIndexArray< AndOr, TileType, TileType::MaxSize > _smallmap_vehicles_andor
Colour masks for "Vehicles", "Industry", and "Vegetation" modes.
static IndustryType _smallmap_industry_highlight
Highlight a specific industry type.
void BuildOwnerLegend()
Completes the array for the owned property legend.
static const int NUM_NO_COMPANY_ENTRIES
Number of entries in the owner legend that are not companies.
static int _smallmap_cargo_count
Number of cargos in the link stats legend.
uint32_t GetSmallMapOwnerPixels(TileIndex tile, TileType t, IncludeHeightmap include_heightmap)
Return the colour a tile would be displayed with in the small map in mode "Owner".
static bool IsDryCoast(TileIndex tile)
Test if a water tile is a dry coast tile, i.e.
static int _smallmap_company_count
Number of entries in the owner legend.
static SmallMapColourScheme _heightmap_schemes[]
Available colour schemes for height maps.
static constexpr std::initializer_list< NWidgetPart > _nested_smallmap_display
Widget parts of the smallmap display and image buttons.
static uint32_t GetSmallMapVegetationPixels(TileIndex tile, TileType t)
Return the colour a tile would be displayed with in the smallmap in mode "Vegetation".
static constexpr std::initializer_list< NWidgetPart > _nested_smallmap_bar
Widget parts of the smallmap legend bar.
static uint32_t GetSmallMapIndustriesPixels(TileIndex tile, TileType t)
Return the colour a tile would be displayed with in the small map in mode "Industries".
static LegendAndColour _legend_land_contours[]
Legend text giving the colours to look for on the minimap.
static bool _smallmap_show_heightmap
Show heightmap in industry and owner mode of smallmap window.
SmallMapType
Types of legends in the WID_SM_LEGEND widget.
@ Contour
Contour legend.
@ Vegetation
Vegetation legend.
@ LinkStats
LinkStats legend.
@ Owners
Owners legend.
@ Vehicles
Vehicles legend.
@ End
End marker.
@ Routes
Routes legend.
@ Industries
Industries legend.
static int _smallmap_industry_count
Number of used industries.
static bool _smallmap_industry_highlight_state
State of highlight blinking.
void BuildLinkStatsLegend()
Populate legend table for the link stat view.
static uint32_t GetSmallMapRoutesPixels(TileIndex tile, TileType t)
Return the colour a tile would be displayed with in the small map in mode "Routes".
static const AndOr & GetMaskPixelsForTileType(TileIndex tile, TileType t, const EnumIndexArray< AndOr, TileType, TileType::MaxSize > &table)
Get the mask pixel group for a tile from the given lookup table, taking account of coast tiles.
static uint16_t _industry_to_name_string_width[NUM_INDUSTRYTYPES]
The string bounding box width for each industry type in the smallmap.
static constexpr EnumIndexArray< uint32_t, ClearGround, ClearGround::MaxSize > _vegetation_clear_bits
Lookup table of minimap colours to use for each ClearGround type.
static const EnumIndexArray< uint8_t, TileType, TileType::MaxSize > _tiletype_importance
Mapping of tile type to importance of the tile (higher number means more interesting to show).
bool ScrollMainWindowTo(int x, int y, int z, bool instant)
Scrolls the main window to given coordinates.
static TypedIndexContainer< std::array< uint32_t, MAX_COMPANIES >, CompanyID > _company_to_list_pos
For connecting company ID to position in owner list (small map legend).
void BuildIndustriesLegend()
Fills an array for the industries legends.
static WindowDesc _smallmap_desc(WindowPosition::Automatic, "smallmap", 0, 0, WindowClass::SmallMap, WindowClass::None, {}, _nested_smallmap_widgets)
Window definition for the smallmap window.
static LegendAndColour _legend_linkstats[NUM_CARGO+lengthof(_linkstat_colours_in_legenda)+1]
Legend entries for the link stats view.
static uint32_t ApplyMask(uint32_t colour, const AndOr &mask)
Mix colours for a smallmap pixel group.
Point GetSmallMapStationMiddle(const Window *w, const Station *st)
Determine the middle of a station in the smallmap window.
Smallmap GUI functions.
IncludeHeightmap
Enum for how to include the heightmap pixels/colours in small map related functions.
@ Always
Always include the heightmap.
@ IfEnabled
Only include the heightmap if its enabled in the gui by the player.
Types related to the smallmap widgets.
@ WID_SM_OWNERS
Button to select the owners view.
@ WID_SM_VEHICLES
Button to select the vehicles view.
@ WID_SM_MAP_BORDER
Border around the smallmap.
@ WID_SM_CENTERMAP
Button to move smallmap center to main window center.
@ WID_SM_VEGETATION
Button to select the vegetation view.
@ WID_SM_TOGGLETOWNNAME
Toggle button to display town names.
@ WID_SM_CONTOUR
Button to select the contour view (height map).
@ WID_SM_ENABLE_ALL
Button to enable display of all legend entries.
@ WID_SM_SHOW_HEIGHT
Show heightmap toggle button.
@ WID_SM_ZOOM_OUT
Button to zoom out one step.
@ WID_SM_LEGEND
Bottom panel to display smallmap legends.
@ WID_SM_ZOOM_IN
Button to zoom in one step.
@ WID_SM_MAP
Panel containing the smallmap.
@ WID_SM_SHOW_IND_NAMES
Show industry names toggle button.
@ WID_SM_SELECT_BUTTONS
Selection widget for the buttons present in some smallmap modes.
@ WID_SM_BLANK
Empty button as placeholder.
@ WID_SM_LINKSTATS
Button to select the link stats view.
@ WID_SM_SHOW_IND_NAMES_SEL
Container for the 'show industry names' button, which can be hidden.
@ WID_SM_ROUTES
Button to select the routes view.
@ WID_SM_INDUSTRIES
Button to select the industries view.
@ WID_SM_CAPTION
Caption of the window.
@ WID_SM_DISABLE_ALL
Button to disable display of all legend entries.
void SndClickBeep()
Play a beep sound for a click event if enabled in settings.
Definition sound.cpp:252
Functions related to sound.
static const SpriteID SPR_IMG_PLANTTREES
Definition sprites.h:1260
static const SpriteID SPR_IMG_COMPANY_GENERAL
Definition sprites.h:1253
static const SpriteID SPR_IMG_INDUSTRY
Definition sprites.h:1259
static const SpriteID SPR_IMG_SHOW_ROUTES
Definition sprites.h:1258
static const SpriteID SPR_IMG_SHOW_VEHICLES
Definition sprites.h:1257
static const SpriteID SPR_IMG_ZOOMIN
Definition sprites.h:1265
static const SpriteID SPR_IMG_SHOW_COUNTOURS
Definition sprites.h:1256
static const SpriteID SPR_IMG_ZOOMOUT
Definition sprites.h:1266
static const SpriteID SPR_IMG_TOWN
Definition sprites.h:1249
static const SpriteID SPR_EMPTY
Empty (transparent blue) sprite.
Definition sprites.h:46
static const SpriteID SPR_IMG_SMALLMAP
Definition sprites.h:1248
Maps accessors for stations.
StationType GetStationType(Tile t)
Get the station type of this tile.
Definition station_map.h:42
@ Dock
Ship port.
@ Rail
Railways/train station.
@ Bus
Road stop for busses.
@ Truck
Road stop for trucks.
@ Airport
Airports and heliports, excluding the ones on oil rigs.
Definition of base types and functions in a cross-platform compatible way.
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:261
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:423
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition strings.cpp:55
Functions related to OTTD's strings.
StrongType::Typedef< uint32_t, struct StringIDTag, StrongType::Compare, StrongType::Integer > StringID
Numeric value that represents a string, independent of the selected language.
@ TD_RTL
Text is written right-to-left by default.
Class to backup a specific variable and restore it upon destruction of this object to prevent stack v...
TileArea spread
NOSAVE: Station spread tile area.
Specification of a cargo type.
Definition cargotype.h:76
CargoType Index() const
Determines index of this cargospec.
Definition cargotype.h:110
PixelColour legend_colour
Colour in legends and graphs.
Definition cargotype.h:79
StringID name
Name of this type of cargo.
Definition cargotype.h:93
T y
Y coordinate.
T x
X coordinate.
Dimensions (a width and height) of a rectangle in 2D.
value_type height
The height of the rectangle.
value_type width
The width of the rectangle.
Data about how and where to blit pixels.
Definition gfx_type.h:157
Defines the data structure for constructing industry.
StringID name
Displayed name of the industry.
bool enabled
entity still available (by default true).newgrf can disable it, though
PixelColour map_colour
colour used for the small map
Defines the internal data of a functional industry.
Definition industry.h:64
IndustryType type
type of industry.
Definition industry.h:117
static uint16_t GetIndustryTypeCount(IndustryType type)
Get the count of industries for this type.
Definition industry.h:267
static Industry * GetByTile(TileIndex tile)
Get the industry of the given tile.
Definition industry.h:253
Structure for holding relevant data for legends in small map.
PixelColour colour
Colour of the item on the map.
uint8_t height
Height in tiles. Only valid for height legend entries.
StringID legend
String corresponding to the coloured item.
bool show_on_map
For filtering industries, if true, industry is shown on the map in colour.
CompanyID company
Company to display. Only valid for company entries of the owner legend.
bool end
This is the end of the list.
bool col_break
Perform a column break and go further at the next column.
CargoType cargo_type
Type of cargo. Only valid for cargo entries.
IndustryType industry_type
Type of industry. Only valid for industry entries.
static uint MaxY()
Gets the maximum Y coordinate within the map, including TileType::Void.
Definition map_func.h:298
static uint MaxX()
Gets the maximum X coordinate within the map, including TileType::Void.
Definition map_func.h:289
Colour for pixel/line drawing.
Definition gfx_type.h:307
uint8_t p
Palette index.
Definition gfx_type.h:308
static Pool::IterateWrapper< Company > Iterate(size_t from=0)
static constexpr size_t MAX_SIZE
Specification of a rectangle with absolute coordinates of all edges.
Rect WithWidth(int width, bool end) const
Copy Rect and set its width.
int Width() const
Get width of Rect.
Rect Shrink(int s) const
Copy and shrink Rect by s pixels.
Rect WithHeight(int height, bool end=false) const
Copy Rect and set its height.
Rect Indent(int indent, bool end) const
Copy Rect and indent it from its position.
Rect Translate(int x, int y) const
Copy and translate Rect by x,y pixels.
Colour scheme of the smallmap.
std::array< uint32_t, MAX_TILE_HEIGHT+1 > height_colours
Cached colours for each level in a map.
uint32_t default_colour
Default colour of the land.
std::span< const uint32_t > height_colours_base
Base table for determining the colours.
Station data structure.
Town data structure.
Definition town.h:64
Vehicle data structure.
Data structure for viewport, display of a part of the world.
int virtual_top
Virtual top coordinate.
int virtual_left
Virtual left coordinate.
int virtual_width
width << zoom
int virtual_height
height << zoom
Container with the data associated to a single widget.
High level window description.
Definition window_gui.h:171
Data structure for an opened window.
Definition window_gui.h:272
void ReInit(int rx=0, int ry=0, bool reposition=false)
Re-initialize a window, and optionally change its size.
Definition window.cpp:1025
virtual void Close(int data=0)
Hide the window and all its child windows, and mark them for a later deletion.
Definition window.cpp:1150
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
Definition window.cpp:1855
void DrawWidgets() const
Paint all widgets of a window.
Definition widget.cpp:792
void InvalidateData(int data=0, bool gui_scope=true)
Mark this window's data as invalid (in need of re-computing).
Definition window.cpp:3317
void RaiseWidget(WidgetID widget_index)
Marks a widget as raised.
Definition window_gui.h:468
std::unique_ptr< ViewportData > viewport
Pointer to viewport data, if present.
Definition window_gui.h:317
virtual std::string GetWidgetString(WidgetID widget, StringID stringid) const
Get the raw string for a widget.
Definition window.cpp:513
WidgetID mouse_capture_widget
ID of current mouse capture widget (e.g. dragged scrollbar). INVALID_WIDGET if no widget has mouse ca...
Definition window_gui.h:325
void CreateNestedTree()
Perform the first part of the initialization of a nested widget tree.
Definition window.cpp:1845
void SetWidgetLoweredState(WidgetID widget_index, bool lowered_stat)
Sets the lowered/raised status of a widget.
Definition window_gui.h:440
int left
x position of left edge of the window
Definition window_gui.h:308
int top
y position of top edge of the window
Definition window_gui.h:309
Window(WindowDesc &desc)
Empty constructor, initialization has been moved to InitNested() called from the constructor of the d...
Definition window.cpp:1879
const NWID * GetWidget(WidgetID widnum) const
Get the nested widget with number widnum from the nested widget tree.
void LowerWidget(WidgetID widget_index)
Marks a widget as lowered.
Definition window_gui.h:459
void HandleButtonClick(WidgetID widget)
Do all things to make a button look clicked and mark it to be unclicked in a few ticks.
Definition window.cpp:604
void SetWidgetDisabledState(WidgetID widget_index, bool disab_stat)
Sets the enabled/disabled status of a widget.
Definition window_gui.h:380
int height
Height of the window (number of pixels down in y direction).
Definition window_gui.h:311
int width
width of the window (number of pixels to the right in x direction)
Definition window_gui.h:310
WindowNumber window_number
Window number within the window class.
Definition window_gui.h:301
static uint TileHeight(Tile tile)
Returns the height of a tile.
Definition tile_map.h:29
Owner GetTileOwner(Tile tile)
Returns the owner of a tile.
Definition tile_map.h:178
TropicZone GetTropicZone(Tile tile)
Get the tropic zone.
Definition tile_map.h:238
Slope GetTileSlope(TileIndex tile)
Return the slope of a given tile inside the map.
Definition tile_map.h:279
static TileType GetTileType(Tile tile)
Get the tiletype of a given tile.
Definition tile_map.h:96
static constexpr uint MAX_TILE_HEIGHT
Maximum allowed tile height.
Definition tile_type.h:24
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
@ Rainforest
Rainforest tile.
Definition tile_type.h:84
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:100
static constexpr uint TILE_SIZE
Tile size in world coordinates.
Definition tile_type.h:15
TileType
The different types of tiles.
Definition tile_type.h:48
@ TunnelBridge
Tunnel entry/exit and bridge heads.
Definition tile_type.h:58
@ Water
Water tile.
Definition tile_type.h:55
@ Station
A tile of a station or airport.
Definition tile_type.h:54
@ Industry
Part of an industry.
Definition tile_type.h:57
@ Railway
A tile with railway.
Definition tile_type.h:50
@ Void
Invisible tiles at the SW and SE border.
Definition tile_type.h:56
@ Trees
Tile with one or more trees.
Definition tile_type.h:53
@ House
A house by a town.
Definition tile_type.h:52
@ Road
A tile with road and/or tram tracks.
Definition tile_type.h:51
@ Clear
A tile without any structures, i.e. grass, rocks, farm fields etc.
Definition tile_type.h:49
OrthogonalTileArea TileArea
Shorthand for the much more common orthogonal tile area.
Definition of Interval and OneShot timers.
Definition of the Window system.
static constexpr std::chrono::milliseconds TIMER_BLINK_INTERVAL
Interval used by blinking interface elements.
Base of the town class.
TransportType
Available types of transport.
@ Rail
Transport by train.
@ Road
Transport by road vehicle.
Map accessors for tree tiles.
TreeGround GetTreeGround(Tile t)
Returns the groundtype for tree tiles.
Definition tree_map.h:102
@ SnowOrDesert
Snow or desert, depending on landscape.
Definition tree_map.h:55
@ RoughSnow
A snow tile that is rough underneath.
Definition tree_map.h:57
Functions that have tunnels and bridges in common.
TransportType GetTunnelBridgeTransportType(Tile t)
Tunnel: Get the transport type of the tunnel (road or rail) Bridge: Get the transport type of the bri...
Base class for all vehicles.
@ Effect
Effect vehicle type (smoke, explosions, sparks, bubbles).
EnumIndexArray< T, VehicleType, Tend > VehicleTypeIndexArray
Array with VehicleType as index.
bool ScrollWindowTo(int x, int y, int z, Window *w, bool instant)
Scrolls the viewport in a window to a given location.
Functions related to (drawing on) viewports.
static const int TILE_HEIGHT_STEP
One Z unit tile height difference is displayed as 50m.
bool IsTileOnWater(Tile t)
Tests if the tile was built on water.
Definition water_map.h:140
bool IsCoastTile(Tile t)
Is it a coast tile.
Definition water_map.h:216
static RectPadding ScaleGUITrad(const RectPadding &r)
Scale a RectPadding to GUI zoom level.
Definition widget.cpp:49
@ WWT_PUSHTXTBTN
Normal push-button (no toggle button) with text caption.
@ WWT_INSET
Pressed (inset) panel, most commonly used as combo box text area.
Definition widget_type.h:40
@ WWT_IMGBTN
(Toggle) Button with image
Definition widget_type.h:41
@ NWID_CUSTOM
General Custom widget.
Definition widget_type.h:77
@ WWT_PUSHIMGBTN
Normal push-button (no toggle button) with image caption.
@ NWID_HORIZONTAL
Horizontal container.
Definition widget_type.h:66
@ WWT_TEXTBTN
(Toggle) Button with text
Definition widget_type.h:44
@ WWT_PANEL
Simple depressed panel.
Definition widget_type.h:39
@ WWT_STICKYBOX
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX).
Definition widget_type.h:57
@ WWT_SHADEBOX
Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX).
Definition widget_type.h:55
@ WWT_CAPTION
Window caption (window title between closebox and stickybox).
Definition widget_type.h:52
@ NWID_VERTICAL
Vertical container.
Definition widget_type.h:68
@ WWT_CLOSEBOX
Close box (at top-left of a window).
Definition widget_type.h:60
@ WWT_RESIZEBOX
Resize box (normally at bottom-right of a window).
Definition widget_type.h:59
@ WWT_DEFSIZEBOX
Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX).
Definition widget_type.h:56
@ NWID_SELECTION
Stacked widgets, only one visible at a time (eg in a panel with tabs).
Definition widget_type.h:71
@ SZSP_NONE
Display plane with zero size in both directions (none filling and resizing).
SizingType
Different forms of sizing nested widgets, using NWidgetBase::AssignSizePosition().
@ Resize
Resize the nested widget tree.
@ Smallest
Initialize nested widget tree to smallest size. Also updates current_x and current_y.
Window * GetMainWindow()
Get the main window, i.e.
Definition window.cpp:1228
bool _scrolling_viewport
A viewport is being scrolled with the mouse.
Definition window.cpp:88
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
Definition window.cpp:1199
void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope)
Mark window data of all windows of a given class as invalid (in need of re-computing) Note that by de...
Definition window.cpp:3395
Window functions not directly related to making/drawing windows.
Twindow * AllocateWindowDescFront(WindowDesc &desc, WindowNumber window_number, Targs... extra_arguments)
Open a new window.
@ Automatic
Find a place automatically.
Definition window_gui.h:145
int WidgetID
Widget ID.
Definition window_type.h:21
static constexpr WidgetID INVALID_WIDGET
An invalid widget index.
Definition window_type.h:24
Functions related to zooming.