Changeset - r28071:710a5282a32a
[Not reviewed]
master
0 2 0
Peter Nelson - 8 months ago 2023-10-31 01:32:40
peter1138@openttd.org
Revert 6b68956: Move declaration of SmallMapWindow out of header file.

This split needlessly complicates `SmallMapWindow` for the sake of one method (no longer) used by `LinkGraphOverlay`.
2 files changed with 1178 insertions and 1225 deletions:
0 comments (0 inline, 0 general)
src/smallmap_gui.cpp
Show inline comments
 
@@ -36,6 +36,18 @@ static int _smallmap_industry_count; ///
 
static int _smallmap_company_count;  ///< Number of entries in the owner legend.
 
static int _smallmap_cargo_count;    ///< Number of cargos in the link stats legend.
 

	
 
/** Structure for holding relevant data for legends in small map */
 
struct LegendAndColour {
 
	uint8_t colour;            ///< Colour of the item on the map.
 
	StringID legend;           ///< String corresponding to the coloured item.
 
	IndustryType type;         ///< Type of industry. Only valid for industry entries.
 
	uint8_t height;            ///< Height in tiles. Only valid for height legend entries.
 
	CompanyID company;         ///< Company to display. Only valid for company entries of the owner legend.
 
	bool show_on_map;          ///< For filtering industries, if \c true, industry is shown on the map in colour.
 
	bool end;                  ///< This is the end of the list.
 
	bool col_break;            ///< Perform a column break and go further at the next column.
 
};
 

	
 
/** Link stat colours shown in legenda. */
 
static uint8_t _linkstat_colours_in_legenda[] = {0, 1, 3, 5, 7, 9, 11};
 

	
 
@@ -594,1113 +606,1229 @@ static const byte _vehicle_type_colours[
 
	PC_RED, PC_YELLOW, PC_LIGHT_BLUE, PC_WHITE, PC_BLACK, PC_RED
 
};
 

	
 
/** Class managing the smallmap window. */
 
class SmallMapWindow : public Window {
 
protected:
 
	/** Types of legends in the #WID_SM_LEGEND widget. */
 
	enum SmallMapType {
 
		SMT_CONTOUR,
 
		SMT_VEHICLES,
 
		SMT_INDUSTRY,
 
		SMT_LINKSTATS,
 
		SMT_ROUTES,
 
		SMT_VEGETATION,
 
		SMT_OWNER,
 
	};
 

	
 
/** Notify the industry chain window to stop sending newly selected industries. */
 
/* static */ void SmallMapWindow::BreakIndustryChainLink()
 
{
 
	InvalidateWindowClassesData(WC_INDUSTRY_CARGOES, NUM_INDUSTRYTYPES);
 
}
 
	/** Available kinds of zoomlevel changes. */
 
	enum ZoomLevelChange {
 
		ZLC_INITIALIZE, ///< Initialize zoom level.
 
		ZLC_ZOOM_OUT,   ///< Zoom out.
 
		ZLC_ZOOM_IN,    ///< Zoom in.
 
	};
 

	
 
	static SmallMapType map_type; ///< Currently displayed legends.
 
	static bool show_towns;       ///< Display town names in the smallmap.
 
	static int map_height_limit;  ///< Currently used/cached map height limit.
 

	
 
	static const uint INDUSTRY_MIN_NUMBER_OF_COLUMNS = 2; ///< Minimal number of columns in the #WID_SM_LEGEND widget for the #SMT_INDUSTRY legend.
 

	
 
	uint min_number_of_columns;    ///< Minimal number of columns in legends.
 
	uint min_number_of_fixed_rows; ///< Minimal number of rows in the legends for the fixed layouts only (all except #SMT_INDUSTRY).
 
	uint column_width;             ///< Width of a column in the #WID_SM_LEGEND widget.
 
	uint legend_width;             ///< Width of legend 'blob'.
 

	
 
	int32_t scroll_x;  ///< Horizontal world coordinate of the base tile left of the top-left corner of the smallmap display.
 
	int32_t scroll_y;  ///< Vertical world coordinate of the base tile left of the top-left corner of the smallmap display.
 
	int32_t subscroll; ///< Number of pixels (0..3) between the right end of the base tile and the pixel at the top-left corner of the smallmap display.
 
	int zoom;        ///< Zoom level. Bigger number means more zoom-out (further away).
 

	
 
	LinkGraphOverlay *overlay;
 

	
 
	/** Notify the industry chain window to stop sending newly selected industries. */
 
	static void BreakIndustryChainLink()
 
	{
 
		InvalidateWindowClassesData(WC_INDUSTRY_CARGOES, NUM_INDUSTRYTYPES);
 
	}
 

	
 
	static inline Point SmallmapRemapCoords(int x, int y)
 
	{
 
		Point pt;
 
		pt.x = (y - x) * 2;
 
		pt.y = y + x;
 
		return pt;
 
	}
 

	
 
	/**
 
	 * Draws vertical part of map indicator
 
	 * @param x X coord of left/right border of main viewport
 
	 * @param y Y coord of top border of main viewport
 
	 * @param y2 Y coord of bottom border of main viewport
 
	 */
 
	static inline void DrawVertMapIndicator(int x, int y, int y2)
 
	{
 
		GfxFillRect(x, y,      x, y + 3, PC_VERY_LIGHT_YELLOW);
 
		GfxFillRect(x, y2 - 3, x, y2,    PC_VERY_LIGHT_YELLOW);
 
	}
 

	
 
	/**
 
	 * Draws horizontal part of map indicator
 
	 * @param x X coord of left border of main viewport
 
	 * @param x2 X coord of right border of main viewport
 
	 * @param y Y coord of top/bottom border of main viewport
 
	 */
 
	static inline void DrawHorizMapIndicator(int x, int x2, int y)
 
	{
 
		GfxFillRect(x,      y, x + 3, y, PC_VERY_LIGHT_YELLOW);
 
		GfxFillRect(x2 - 3, y, x2,    y, PC_VERY_LIGHT_YELLOW);
 
	}
 

	
 
inline Point SmallMapWindow::SmallmapRemapCoords(int x, int y) const
 
{
 
	Point pt;
 
	pt.x = (y - x) * 2;
 
	pt.y = y + x;
 
	return pt;
 
}
 
	/**
 
	 * Compute minimal required width of the legends.
 
	 * @return Minimally needed width for displaying the smallmap legends in pixels.
 
	 */
 
	inline uint GetMinLegendWidth() const
 
	{
 
		return WidgetDimensions::scaled.framerect.left + this->min_number_of_columns * this->column_width;
 
	}
 

	
 
	/**
 
	 * Return number of columns that can be displayed in \a width pixels.
 
	 * @return Number of columns to display.
 
	 */
 
	inline uint GetNumberColumnsLegend(uint width) const
 
	{
 
		return width / this->column_width;
 
	}
 

	
 
	/**
 
	 * Compute height given a number of columns.
 
	 * @param num_columns Number of columns.
 
	 * @return Needed height for displaying the smallmap legends in pixels.
 
	 */
 
	inline uint GetLegendHeight(uint num_columns) const
 
	{
 
		return WidgetDimensions::scaled.framerect.Vertical() +
 
				this->GetNumberRowsLegend(num_columns) * FONT_HEIGHT_SMALL;
 
	}
 

	
 
	/**
 
	 * Get a bitmask for company links to be displayed. Usually this will be
 
	 * the _local_company. Spectators get to see all companies' links.
 
	 * @return Company mask.
 
	 */
 
	inline CompanyMask GetOverlayCompanyMask() const
 
	{
 
		return Company::IsValidID(_local_company) ? 1U << _local_company : MAX_UVALUE(CompanyMask);
 
	}
 

	
 
	/** Blink the industries (if selected) on a regular interval. */
 
	IntervalTimer<TimerWindow> blink_interval = {std::chrono::milliseconds(450), [this](auto) {
 
		Blink();
 
	}};
 

	
 
/**
 
 * Remap tile to location on this smallmap.
 
 * @param tile_x X coordinate of the tile.
 
 * @param tile_y Y coordinate of the tile.
 
 * @return Position to draw on.
 
 */
 
inline Point SmallMapWindow::RemapTile(int tile_x, int tile_y) const
 
{
 
	int x_offset = tile_x - this->scroll_x / (int)TILE_SIZE;
 
	int y_offset = tile_y - this->scroll_y / (int)TILE_SIZE;
 
	/** Update the whole map on a regular interval. */
 
	IntervalTimer<TimerWindow> refresh_interval = {std::chrono::milliseconds(930), [this](auto) {
 
		ForceRefresh();
 
	}};
 

	
 
	/**
 
	 * Rebuilds the colour indices used for fast access to the smallmap contour colours based on the heightlevel.
 
	 */
 
	void RebuildColourIndexIfNecessary()
 
	{
 
		/* Rebuild colour indices if necessary. */
 
		if (SmallMapWindow::map_height_limit == _settings_game.construction.map_height_limit) return;
 

	
 
		for (uint n = 0; n < lengthof(_heightmap_schemes); n++) {
 
			/* The heights go from 0 up to and including maximum. */
 
			int heights = _settings_game.construction.map_height_limit + 1;
 
			_heightmap_schemes[n].height_colours = ReallocT<uint32_t>(_heightmap_schemes[n].height_colours, heights);
 

	
 
			for (int z = 0; z < heights; z++) {
 
				size_t access_index = (_heightmap_schemes[n].colour_count * z) / heights;
 

	
 
	if (this->zoom == 1) return SmallmapRemapCoords(x_offset, y_offset);
 
				/* Choose colour by mapping the range (0..max heightlevel) on the complete colour table. */
 
				_heightmap_schemes[n].height_colours[z] = _heightmap_schemes[n].height_colours_base[access_index];
 
			}
 
		}
 

	
 
		SmallMapWindow::map_height_limit = _settings_game.construction.map_height_limit;
 
		BuildLandLegend();
 
	}
 

	
 
	/**
 
	 * Get the number of rows in the legend from the number of columns. Those
 
	 * are at least min_number_of_fixed_rows and possibly more if there are so
 
	 * many cargoes, industry types or companies that they won't fit in the
 
	 * available space.
 
	 * @param columns Number of columns in the legend.
 
	 * @return Number of rows needed for everything to fit in.
 
	 */
 
	uint GetNumberRowsLegend(uint columns) const
 
	{
 
		/* Reserve one column for link colours */
 
		uint num_rows_linkstats = CeilDiv(_smallmap_cargo_count, columns - 1);
 
		uint num_rows_others = CeilDiv(std::max(_smallmap_industry_count, _smallmap_company_count), columns);
 
		return std::max({this->min_number_of_fixed_rows, num_rows_linkstats, num_rows_others});
 
	}
 

	
 
	/* For negative offsets, round towards -inf. */
 
	if (x_offset < 0) x_offset -= this->zoom - 1;
 
	if (y_offset < 0) y_offset -= this->zoom - 1;
 
	/**
 
	 * Select and toggle a legend item. When CTRL is pressed, disable all other
 
	 * items in the group defined by begin_legend_item and end_legend_item and
 
	 * keep the clicked one enabled even if it was already enabled before. If
 
	 * the other items in the group are all disabled already and CTRL is pressed
 
	 * enable them instead.
 
	 * @param click_pos the index of the item being selected
 
	 * @param legend the legend from which we select
 
	 * @param end_legend_item index one past the last item in the group to be inverted
 
	 * @param begin_legend_item index of the first item in the group to be inverted
 
	 */
 
	void SelectLegendItem(int click_pos, LegendAndColour *legend, int end_legend_item, int begin_legend_item = 0)
 
	{
 
		if (_ctrl_pressed) {
 
			/* Disable all, except the clicked one */
 
			bool changes = false;
 
			for (int i = begin_legend_item; i != end_legend_item; i++) {
 
				bool new_state = (i == click_pos);
 
				if (legend[i].show_on_map != new_state) {
 
					changes = true;
 
					legend[i].show_on_map = new_state;
 
				}
 
			}
 
			if (!changes) {
 
				/* Nothing changed? Then show all (again). */
 
				for (int i = begin_legend_item; i != end_legend_item; i++) {
 
					legend[i].show_on_map = true;
 
				}
 
			}
 
		} else {
 
			legend[click_pos].show_on_map = !legend[click_pos].show_on_map;
 
		}
 

	
 
		if (this->map_type == SMT_INDUSTRY) this->BreakIndustryChainLink();
 
	}
 

	
 
	/**
 
	 * Select a new map type.
 
	 * @param map_type New map type.
 
	 */
 
	void SwitchMapType(SmallMapType map_type)
 
	{
 
		this->RaiseWidget(this->map_type + WID_SM_CONTOUR);
 
		this->map_type = map_type;
 
		this->LowerWidget(this->map_type + WID_SM_CONTOUR);
 

	
 
		this->SetupWidgetData();
 

	
 
		if (map_type == SMT_LINKSTATS) this->overlay->SetDirty();
 
		if (map_type != SMT_INDUSTRY) this->BreakIndustryChainLink();
 
		this->SetDirty();
 
	}
 

	
 
	return SmallmapRemapCoords(x_offset / this->zoom, y_offset / this->zoom);
 
}
 
	/**
 
	 * Set new #scroll_x, #scroll_y, and #subscroll values after limiting them such that the center
 
	 * of the smallmap always contains a part of the map.
 
	 * @param sx  Proposed new #scroll_x
 
	 * @param sy  Proposed new #scroll_y
 
	 * @param sub Proposed new #subscroll
 
	 */
 
	void SetNewScroll(int sx, int sy, int sub)
 
	{
 
		const NWidgetBase *wi = this->GetWidget<NWidgetBase>(WID_SM_MAP);
 
		Point hv = InverseRemapCoords(wi->current_x * ZOOM_LVL_BASE * TILE_SIZE / 2, wi->current_y * ZOOM_LVL_BASE * TILE_SIZE / 2);
 
		hv.x *= this->zoom;
 
		hv.y *= this->zoom;
 

	
 
		if (sx < -hv.x) {
 
			sx = -hv.x;
 
			sub = 0;
 
		}
 
		if (sx > (int)(Map::MaxX() * TILE_SIZE) - hv.x) {
 
			sx = Map::MaxX() * TILE_SIZE - hv.x;
 
			sub = 0;
 
		}
 
		if (sy < -hv.y) {
 
			sy = -hv.y;
 
			sub = 0;
 
		}
 
		if (sy > (int)(Map::MaxY() * TILE_SIZE) - hv.y) {
 
			sy = Map::MaxY() * TILE_SIZE - hv.y;
 
			sub = 0;
 
		}
 

	
 
		this->scroll_x = sx;
 
		this->scroll_y = sy;
 
		this->subscroll = sub;
 
		if (this->map_type == SMT_LINKSTATS) this->overlay->SetDirty();
 
	}
 

	
 
	/**
 
	 * Adds map indicators to the smallmap.
 
	 */
 
	void DrawMapIndicators() const
 
	{
 
		/* Find main viewport. */
 
		const Viewport *vp = GetMainWindow()->viewport;
 

	
 
		Point upper_left_smallmap_coord  = InverseRemapCoords2(vp->virtual_left, vp->virtual_top);
 
		Point lower_right_smallmap_coord = InverseRemapCoords2(vp->virtual_left + vp->virtual_width - 1, vp->virtual_top + vp->virtual_height - 1);
 

	
 
		Point upper_left = this->RemapTile(upper_left_smallmap_coord.x / (int)TILE_SIZE, upper_left_smallmap_coord.y / (int)TILE_SIZE);
 
		upper_left.x -= this->subscroll;
 

	
 
		Point lower_right = this->RemapTile(lower_right_smallmap_coord.x / (int)TILE_SIZE, lower_right_smallmap_coord.y / (int)TILE_SIZE);
 
		lower_right.x -= this->subscroll;
 

	
 
		SmallMapWindow::DrawVertMapIndicator(upper_left.x, upper_left.y, lower_right.y);
 
		SmallMapWindow::DrawVertMapIndicator(lower_right.x, upper_left.y, lower_right.y);
 

	
 
/**
 
 * Determine the tile relative to the base tile of the smallmap, and the pixel position at
 
 * that tile for a point in the smallmap.
 
 * @param px       Horizontal coordinate of the pixel.
 
 * @param py       Vertical coordinate of the pixel.
 
 * @param[out] sub Pixel position at the tile (0..3).
 
 * @param add_sub  Add current #subscroll to the position.
 
 * @return Tile being displayed at the given position relative to #scroll_x and #scroll_y.
 
 * @note The #subscroll offset is already accounted for.
 
 */
 
inline Point SmallMapWindow::PixelToTile(int px, int py, int *sub, bool add_sub) const
 
{
 
	if (add_sub) px += this->subscroll;  // Total horizontal offset.
 
		SmallMapWindow::DrawHorizMapIndicator(upper_left.x, lower_right.x, upper_left.y);
 
		SmallMapWindow::DrawHorizMapIndicator(upper_left.x, lower_right.x, lower_right.y);
 
	}
 

	
 
	/**
 
	 * Draws one column of tiles of the small map in a certain mode onto the screen buffer, skipping the shifted rows in between.
 
	 *
 
	 * @param dst Pointer to a part of the screen buffer to write to.
 
	 * @param xc The X coordinate of the first tile in the column.
 
	 * @param yc The Y coordinate of the first tile in the column
 
	 * @param pitch Number of pixels to advance in the screen buffer each time a pixel is written.
 
	 * @param reps Number of lines to draw
 
	 * @param start_pos Position of first pixel to draw.
 
	 * @param end_pos Position of last pixel to draw (exclusive).
 
	 * @param blitter current blitter
 
	 * @note If pixel position is below \c 0, skip drawing.
 
	 */
 
	void DrawSmallMapColumn(void *dst, uint xc, uint yc, int pitch, int reps, int start_pos, int end_pos, Blitter *blitter) const
 
	{
 
		void *dst_ptr_abs_end = blitter->MoveTo(_screen.dst_ptr, 0, _screen.height);
 
		uint min_xy = _settings_game.construction.freeform_edges ? 1 : 0;
 

	
 
		do {
 
			/* Check if the tile (xc,yc) is within the map range */
 
			if (xc >= Map::MaxX() || yc >= Map::MaxY()) continue;
 

	
 
			/* Check if the dst pointer points to a pixel inside the screen buffer */
 
			if (dst < _screen.dst_ptr) continue;
 
			if (dst >= dst_ptr_abs_end) continue;
 

	
 
			/* Construct tilearea covered by (xc, yc, xc + this->zoom, yc + this->zoom) such that it is within min_xy limits. */
 
			TileArea ta;
 
			if (min_xy == 1 && (xc == 0 || yc == 0)) {
 
				if (this->zoom == 1) continue; // The tile area is empty, don't draw anything.
 

	
 
				ta = TileArea(TileXY(std::max(min_xy, xc), std::max(min_xy, yc)), this->zoom - (xc == 0), this->zoom - (yc == 0));
 
			} else {
 
				ta = TileArea(TileXY(xc, yc), this->zoom, this->zoom);
 
			}
 
			ta.ClampToMap(); // Clamp to map boundaries (may contain MP_VOID tiles!).
 

	
 
			uint32_t val = this->GetTileColours(ta);
 
			uint8_t *val8 = (uint8_t *)&val;
 
			int idx = std::max(0, -start_pos);
 
			for (int pos = std::max(0, start_pos); pos < end_pos; pos++) {
 
				blitter->SetPixel(dst, idx, 0, val8[idx]);
 
				idx++;
 
			}
 
		/* Switch to next tile in the column */
 
		} while (xc += this->zoom, yc += this->zoom, dst = blitter->MoveTo(dst, pitch, 0), --reps != 0);
 
	}
 

	
 
	/* For each two rows down, add a x and a y tile, and
 
	 * For each four pixels to the right, move a tile to the right. */
 
	Point pt = {((py >> 1) - (px >> 2)) * this->zoom, ((py >> 1) + (px >> 2)) * this->zoom};
 
	px &= 3;
 
	/**
 
	 * Adds vehicles to the smallmap.
 
	 * @param dpi the part of the smallmap to be drawn into
 
	 * @param blitter current blitter
 
	 */
 
	void DrawVehicles(const DrawPixelInfo *dpi, Blitter *blitter) const
 
	{
 
		for (const Vehicle *v : Vehicle::Iterate()) {
 
			if (v->type == VEH_EFFECT) continue;
 
			if (v->vehstatus & (VS_HIDDEN | VS_UNCLICKABLE)) continue;
 

	
 
			/* Remap into flat coordinates. */
 
			Point pt = this->RemapTile(v->x_pos / (int)TILE_SIZE, v->y_pos / (int)TILE_SIZE);
 

	
 
			int y = pt.y - dpi->top;
 
			if (!IsInsideMM(y, 0, dpi->height)) continue; // y is out of bounds.
 

	
 
			bool skip = false; // Default is to draw both pixels.
 
			int x = pt.x - this->subscroll - 3 - dpi->left; // Offset X coordinate.
 
			if (x < 0) {
 
				/* if x+1 is 0, that means we're on the very left edge,
 
				 * and should thus only draw a single pixel */
 
				if (++x != 0) continue;
 
				skip = true;
 
			} else if (x >= dpi->width - 1) {
 
				/* Check if we're at the very right edge, and if so draw only a single pixel */
 
				if (x != dpi->width - 1) continue;
 
				skip = true;
 
			}
 

	
 
	if (py & 1) { // Odd number of rows, handle the 2 pixel shift.
 
		if (px < 2) {
 
			pt.x += this->zoom;
 
			px += 2;
 
		} else {
 
			pt.y += this->zoom;
 
			px -= 2;
 
			/* Calculate pointer to pixel and the colour */
 
			byte colour = (this->map_type == SMT_VEHICLES) ? _vehicle_type_colours[v->type] : PC_WHITE;
 

	
 
			/* And draw either one or two pixels depending on clipping */
 
			blitter->SetPixel(dpi->dst_ptr, x, y, colour);
 
			if (!skip) blitter->SetPixel(dpi->dst_ptr, x + 1, y, colour);
 
		}
 
	}
 

	
 
	/**
 
	 * Adds town names to the smallmap.
 
	 * @param dpi the part of the smallmap to be drawn into
 
	 */
 
	void DrawTowns(const DrawPixelInfo *dpi) const
 
	{
 
		for (const Town *t : Town::Iterate()) {
 
			/* Remap the town coordinate */
 
			Point pt = this->RemapTile(TileX(t->xy), TileY(t->xy));
 
			int x = pt.x - this->subscroll - (t->cache.sign.width_small >> 1);
 
			int y = pt.y;
 

	
 
			/* Check if the town sign is within bounds */
 
			if (x + t->cache.sign.width_small > dpi->left &&
 
					x < dpi->left + dpi->width &&
 
					y + FONT_HEIGHT_SMALL > dpi->top &&
 
					y < dpi->top + dpi->height) {
 
				/* And draw it. */
 
				SetDParam(0, t->index);
 
				DrawString(x, x + t->cache.sign.width_small, y, STR_SMALLMAP_TOWN);
 
			}
 
		}
 
	}
 

	
 
	*sub = px;
 
	return pt;
 
}
 
	/**
 
	 * Draws the small map.
 
	 *
 
	 * Basically, the small map is draw column of pixels by column of pixels. The pixels
 
	 * are drawn directly into the screen buffer. The final map is drawn in multiple passes.
 
	 * The passes are:
 
	 * <ol><li>The colours of tiles in the different modes.</li>
 
	 * <li>Town names (optional)</li></ol>
 
	 *
 
	 * @param dpi pointer to pixel to write onto
 
	 */
 
	void DrawSmallMap(DrawPixelInfo *dpi) const
 
	{
 
		Blitter *blitter = BlitterFactory::GetCurrentBlitter();
 
		AutoRestoreBackup dpi_backup(_cur_dpi, dpi);
 

	
 
		/* Clear it */
 
		GfxFillRect(dpi->left, dpi->top, dpi->left + dpi->width - 1, dpi->top + dpi->height - 1, PC_BLACK);
 

	
 
		/* Which tile is displayed at (dpi->left, dpi->top)? */
 
		int dx;
 
		Point tile = this->PixelToTile(dpi->left, dpi->top, &dx);
 
		int tile_x = this->scroll_x / (int)TILE_SIZE + tile.x;
 
		int tile_y = this->scroll_y / (int)TILE_SIZE + tile.y;
 

	
 
		void *ptr = blitter->MoveTo(dpi->dst_ptr, -dx - 4, 0);
 
		int x = - dx - 4;
 
		int y = 0;
 

	
 
		for (;;) {
 
			/* Distance from left edge */
 
			if (x >= -3) {
 
				if (x >= dpi->width) break; // Exit the loop.
 

	
 
				int end_pos = std::min(dpi->width, x + 4);
 
				int reps = (dpi->height - y + 1) / 2; // Number of lines.
 
				if (reps > 0) {
 
					this->DrawSmallMapColumn(ptr, tile_x, tile_y, dpi->pitch * 2, reps, x, end_pos, blitter);
 
				}
 
			}
 

	
 
			if (y == 0) {
 
				tile_y += this->zoom;
 
				y++;
 
				ptr = blitter->MoveTo(ptr, 0, 1);
 
			} else {
 
				tile_x -= this->zoom;
 
				y--;
 
				ptr = blitter->MoveTo(ptr, 0, -1);
 
			}
 
			ptr = blitter->MoveTo(ptr, 2, 0);
 
			x += 2;
 
		}
 

	
 
		/* Draw vehicles */
 
		if (this->map_type == SMT_CONTOUR || this->map_type == SMT_VEHICLES) this->DrawVehicles(dpi, blitter);
 

	
 
		/* Draw link stat overlay */
 
		if (this->map_type == SMT_LINKSTATS) this->overlay->Draw(dpi);
 

	
 
		/* Draw town names */
 
		if (this->show_towns) this->DrawTowns(dpi);
 

	
 
		/* Draw map indicators */
 
		this->DrawMapIndicators();
 
	}
 

	
 
/**
 
 * Compute base parameters of the smallmap such that tile (\a tx, \a ty) starts at pixel (\a x, \a y).
 
 * @param tx       Tile x coordinate.
 
 * @param ty       Tile y coordinate.
 
 * @param x        Non-negative horizontal position in the display where the tile starts.
 
 * @param y        Non-negative vertical position in the display where the tile starts.
 
 * @param[out] sub Value of #subscroll needed.
 
 * @return #scroll_x, #scroll_y values.
 
 */
 
Point SmallMapWindow::ComputeScroll(int tx, int ty, int x, int y, int *sub)
 
{
 
	assert(x >= 0 && y >= 0);
 
	/**
 
	 * Remap tile to location on this smallmap.
 
	 * @param tile_x X coordinate of the tile.
 
	 * @param tile_y Y coordinate of the tile.
 
	 * @return Position to draw on.
 
	 */
 
	Point RemapTile(int tile_x, int tile_y) const
 
	{
 
		int x_offset = tile_x - this->scroll_x / (int)TILE_SIZE;
 
		int y_offset = tile_y - this->scroll_y / (int)TILE_SIZE;
 

	
 
		if (this->zoom == 1) return SmallmapRemapCoords(x_offset, y_offset);
 

	
 
		/* For negative offsets, round towards -inf. */
 
		if (x_offset < 0) x_offset -= this->zoom - 1;
 
		if (y_offset < 0) y_offset -= this->zoom - 1;
 

	
 
		return SmallmapRemapCoords(x_offset / this->zoom, y_offset / this->zoom);
 
	}
 

	
 
	/**
 
	 * Determine the tile relative to the base tile of the smallmap, and the pixel position at
 
	 * that tile for a point in the smallmap.
 
	 * @param px       Horizontal coordinate of the pixel.
 
	 * @param py       Vertical coordinate of the pixel.
 
	 * @param[out] sub Pixel position at the tile (0..3).
 
	 * @param add_sub  Add current #subscroll to the position.
 
	 * @return Tile being displayed at the given position relative to #scroll_x and #scroll_y.
 
	 * @note The #subscroll offset is already accounted for.
 
	 */
 
	Point PixelToTile(int px, int py, int *sub, bool add_sub = true) const
 
	{
 
		if (add_sub) px += this->subscroll;  // Total horizontal offset.
 

	
 
		/* For each two rows down, add a x and a y tile, and
 
		 * For each four pixels to the right, move a tile to the right. */
 
		Point pt = {((py >> 1) - (px >> 2)) * this->zoom, ((py >> 1) + (px >> 2)) * this->zoom};
 
		px &= 3;
 

	
 
	int new_sub;
 
	Point tile_xy = PixelToTile(x, y, &new_sub, false);
 
	tx -= tile_xy.x;
 
	ty -= tile_xy.y;
 
		if (py & 1) { // Odd number of rows, handle the 2 pixel shift.
 
			if (px < 2) {
 
				pt.x += this->zoom;
 
				px += 2;
 
			} else {
 
				pt.y += this->zoom;
 
				px -= 2;
 
			}
 
		}
 

	
 
		*sub = px;
 
		return pt;
 
	}
 

	
 
	Point scroll;
 
	if (new_sub == 0) {
 
		*sub = 0;
 
		scroll.x = (tx + this->zoom) * TILE_SIZE;
 
		scroll.y = (ty - this->zoom) * TILE_SIZE;
 
	} else {
 
		*sub = 4 - new_sub;
 
		scroll.x = (tx + 2 * this->zoom) * TILE_SIZE;
 
		scroll.y = (ty - 2 * this->zoom) * TILE_SIZE;
 
	/**
 
	 * Compute base parameters of the smallmap such that tile (\a tx, \a ty) starts at pixel (\a x, \a y).
 
	 * @param tx       Tile x coordinate.
 
	 * @param ty       Tile y coordinate.
 
	 * @param x        Non-negative horizontal position in the display where the tile starts.
 
	 * @param y        Non-negative vertical position in the display where the tile starts.
 
	 * @param[out] sub Value of #subscroll needed.
 
	 * @return #scroll_x, #scroll_y values.
 
	 */
 
	Point ComputeScroll(int tx, int ty, int x, int y, int *sub) const
 
	{
 
		assert(x >= 0 && y >= 0);
 

	
 
		int new_sub;
 
		Point tile_xy = PixelToTile(x, y, &new_sub, false);
 
		tx -= tile_xy.x;
 
		ty -= tile_xy.y;
 

	
 
		Point scroll;
 
		if (new_sub == 0) {
 
			*sub = 0;
 
			scroll.x = (tx + this->zoom) * TILE_SIZE;
 
			scroll.y = (ty - this->zoom) * TILE_SIZE;
 
		} else {
 
			*sub = 4 - new_sub;
 
			scroll.x = (tx + 2 * this->zoom) * TILE_SIZE;
 
			scroll.y = (ty - 2 * this->zoom) * TILE_SIZE;
 
		}
 
		return scroll;
 
	}
 
	return scroll;
 
}
 

	
 
	/**
 
	 * Initialize or change the zoom level.
 
	 * @param change  Way to change the zoom level.
 
	 * @param zoom_pt Position to keep fixed while zooming.
 
	 * @pre \c *zoom_pt should contain a point in the smallmap display when zooming in or out.
 
	 */
 
	void SetZoomLevel(ZoomLevelChange change, const Point *zoom_pt)
 
	{
 
		static const int zoomlevels[] = {1, 2, 4, 6, 8}; // Available zoom levels. Bigger number means more zoom-out (further away).
 
		static const int MIN_ZOOM_INDEX = 0;
 
		static const int MAX_ZOOM_INDEX = lengthof(zoomlevels) - 1;
 

	
 
		int new_index, cur_index, sub;
 
		Point tile;
 
		switch (change) {
 
			case ZLC_INITIALIZE:
 
				cur_index = - 1; // Definitely different from new_index.
 
				new_index = MIN_ZOOM_INDEX;
 
				tile.x = tile.y = 0;
 
				break;
 

	
 
			case ZLC_ZOOM_IN:
 
			case ZLC_ZOOM_OUT:
 
				for (cur_index = MIN_ZOOM_INDEX; cur_index <= MAX_ZOOM_INDEX; cur_index++) {
 
					if (this->zoom == zoomlevels[cur_index]) break;
 
				}
 
				assert(cur_index <= MAX_ZOOM_INDEX);
 

	
 
				tile = this->PixelToTile(zoom_pt->x, zoom_pt->y, &sub);
 
				new_index = Clamp(cur_index + ((change == ZLC_ZOOM_IN) ? -1 : 1), MIN_ZOOM_INDEX, MAX_ZOOM_INDEX);
 
				break;
 

	
 
			default: NOT_REACHED();
 
		}
 

	
 
		if (new_index != cur_index) {
 
			this->zoom = zoomlevels[new_index];
 
			if (cur_index >= 0) {
 
				Point new_tile = this->PixelToTile(zoom_pt->x, zoom_pt->y, &sub);
 
				this->SetNewScroll(this->scroll_x + (tile.x - new_tile.x) * TILE_SIZE,
 
						this->scroll_y + (tile.y - new_tile.y) * TILE_SIZE, sub);
 
			} else if (this->map_type == SMT_LINKSTATS) {
 
				this->overlay->SetDirty();
 
			}
 
			this->SetWidgetDisabledState(WID_SM_ZOOM_IN,  this->zoom == zoomlevels[MIN_ZOOM_INDEX]);
 
			this->SetWidgetDisabledState(WID_SM_ZOOM_OUT, this->zoom == zoomlevels[MAX_ZOOM_INDEX]);
 
			this->SetDirty();
 
		}
 
	}
 

	
 
/**
 
 * Initialize or change the zoom level.
 
 * @param change  Way to change the zoom level.
 
 * @param zoom_pt Position to keep fixed while zooming.
 
 * @pre \c *zoom_pt should contain a point in the smallmap display when zooming in or out.
 
 */
 
void SmallMapWindow::SetZoomLevel(ZoomLevelChange change, const Point *zoom_pt)
 
{
 
	static const int zoomlevels[] = {1, 2, 4, 6, 8}; // Available zoom levels. Bigger number means more zoom-out (further away).
 
	static const int MIN_ZOOM_INDEX = 0;
 
	static const int MAX_ZOOM_INDEX = lengthof(zoomlevels) - 1;
 
	/**
 
	 * Set the link graph overlay cargo mask from the legend.
 
	 */
 
	void SetOverlayCargoMask()
 
	{
 
		CargoTypes cargo_mask = 0;
 
		for (int i = 0; i != _smallmap_cargo_count; ++i) {
 
			if (_legend_linkstats[i].show_on_map) SetBit(cargo_mask, _legend_linkstats[i].type);
 
		}
 
		this->overlay->SetCargoMask(cargo_mask);
 
	}
 

	
 
	/**
 
	 * Function to set up widgets depending on the information being shown on the smallmap.
 
	 */
 
	void SetupWidgetData()
 
	{
 
		StringID legend_tooltip;
 
		StringID enable_all_tooltip;
 
		StringID disable_all_tooltip;
 
		int plane;
 
		switch (this->map_type) {
 
			case SMT_INDUSTRY:
 
				legend_tooltip = STR_SMALLMAP_TOOLTIP_INDUSTRY_SELECTION;
 
				enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_INDUSTRIES;
 
				disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_INDUSTRIES;
 
				plane = 0;
 
				break;
 

	
 
			case SMT_OWNER:
 
				legend_tooltip = STR_SMALLMAP_TOOLTIP_COMPANY_SELECTION;
 
				enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_COMPANIES;
 
				disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_COMPANIES;
 
				plane = 0;
 
				break;
 

	
 
			case SMT_LINKSTATS:
 
				legend_tooltip = STR_SMALLMAP_TOOLTIP_CARGO_SELECTION;
 
				enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_CARGOS;
 
				disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_CARGOS;
 
				plane = 0;
 
				break;
 

	
 
			default:
 
				legend_tooltip = STR_NULL;
 
				enable_all_tooltip = STR_NULL;
 
				disable_all_tooltip = STR_NULL;
 
				plane = 1;
 
				break;
 
		}
 

	
 
		this->GetWidget<NWidgetCore>(WID_SM_LEGEND)->SetDataTip(STR_NULL, legend_tooltip);
 
		this->GetWidget<NWidgetCore>(WID_SM_ENABLE_ALL)->SetDataTip(STR_SMALLMAP_ENABLE_ALL, enable_all_tooltip);
 
		this->GetWidget<NWidgetCore>(WID_SM_DISABLE_ALL)->SetDataTip(STR_SMALLMAP_DISABLE_ALL, disable_all_tooltip);
 
		this->GetWidget<NWidgetStacked>(WID_SM_SELECT_BUTTONS)->SetDisplayedPlane(plane);
 
	}
 

	
 
	int new_index, cur_index, sub;
 
	Point tile;
 
	switch (change) {
 
		case ZLC_INITIALIZE:
 
			cur_index = - 1; // Definitely different from new_index.
 
			new_index = MIN_ZOOM_INDEX;
 
			tile.x = tile.y = 0;
 
			break;
 
	/**
 
	 * Decide which colours to show to the user for a group of tiles.
 
	 * @param ta Tile area to investigate.
 
	 * @return Colours to display.
 
	 */
 
	uint32_t GetTileColours(const TileArea &ta) const
 
	{
 
		int importance = 0;
 
		TileIndex tile = INVALID_TILE; // Position of the most important tile.
 
		TileType et = MP_VOID;         // Effective tile type at that position.
 

	
 
		for (TileIndex ti : ta) {
 
			TileType ttype = GetTileType(ti);
 

	
 
			switch (ttype) {
 
				case MP_TUNNELBRIDGE: {
 
					TransportType tt = GetTunnelBridgeTransportType(ti);
 

	
 
					switch (tt) {
 
						case TRANSPORT_RAIL: ttype = MP_RAILWAY; break;
 
						case TRANSPORT_ROAD: ttype = MP_ROAD;    break;
 
						default:             ttype = MP_WATER;   break;
 
					}
 
					break;
 
				}
 

	
 
				case MP_INDUSTRY:
 
					/* Special handling of industries while in "Industries" smallmap view. */
 
					if (this->map_type == SMT_INDUSTRY) {
 
						/* If industry is allowed to be seen, use its colour on the map.
 
						 * This has the highest priority above any value in _tiletype_importance. */
 
						IndustryType type = Industry::GetByTile(ti)->type;
 
						if (_legend_from_industries[_industry_to_list_pos[type]].show_on_map) {
 
							if (type == _smallmap_industry_highlight) {
 
								if (_smallmap_industry_highlight_state) return MKCOLOUR_XXXX(PC_WHITE);
 
							} else {
 
								return GetIndustrySpec(type)->map_colour * 0x01010101;
 
							}
 
						}
 
						/* Otherwise make it disappear */
 
						ttype = IsTileOnWater(ti) ? MP_WATER : MP_CLEAR;
 
					}
 
					break;
 

	
 
				default:
 
					break;
 
			}
 

	
 
			if (_tiletype_importance[ttype] > importance) {
 
				importance = _tiletype_importance[ttype];
 
				tile = ti;
 
				et = ttype;
 
			}
 
		}
 

	
 
		switch (this->map_type) {
 
			case SMT_CONTOUR:
 
				return GetSmallMapContoursPixels(tile, et);
 

	
 
			case SMT_VEHICLES:
 
				return GetSmallMapVehiclesPixels(tile, et);
 

	
 
		case ZLC_ZOOM_IN:
 
		case ZLC_ZOOM_OUT:
 
			for (cur_index = MIN_ZOOM_INDEX; cur_index <= MAX_ZOOM_INDEX; cur_index++) {
 
				if (this->zoom == zoomlevels[cur_index]) break;
 
			case SMT_INDUSTRY:
 
				return GetSmallMapIndustriesPixels(tile, et);
 

	
 
			case SMT_LINKSTATS:
 
				return GetSmallMapLinkStatsPixels(tile, et);
 

	
 
			case SMT_ROUTES:
 
				return GetSmallMapRoutesPixels(tile, et);
 

	
 
			case SMT_VEGETATION:
 
				return GetSmallMapVegetationPixels(tile, et);
 

	
 
			case SMT_OWNER:
 
				return GetSmallMapOwnerPixels(tile, et, IncludeHeightmap::IfEnabled);
 

	
 
			default: NOT_REACHED();
 
		}
 
	}
 

	
 
	/**
 
	 * Determines the mouse position on the legend.
 
	 * @param pt Mouse position.
 
	 * @return Legend item under the mouse.
 
	 */
 
	int GetPositionOnLegend(Point pt)
 
	{
 
		const NWidgetBase *wi = this->GetWidget<NWidgetBase>(WID_SM_LEGEND);
 
		uint line = (pt.y - wi->pos_y - WidgetDimensions::scaled.framerect.top) / FONT_HEIGHT_SMALL;
 
		uint columns = this->GetNumberColumnsLegend(wi->current_x);
 
		uint number_of_rows = this->GetNumberRowsLegend(columns);
 
		if (line >= number_of_rows) return -1;
 

	
 
		bool rtl = _current_text_dir == TD_RTL;
 
		int x = pt.x - wi->pos_x;
 
		if (rtl) x = wi->current_x - x;
 
		uint column = (x - WidgetDimensions::scaled.framerect.left) / this->column_width;
 

	
 
		return (column * number_of_rows) + line;
 
	}
 

	
 
	/** Update all the links on the map. */
 
	void UpdateLinks()
 
	{
 
		if (this->map_type == SMT_LINKSTATS) {
 
			CompanyMask company_mask = this->GetOverlayCompanyMask();
 
			if (this->overlay->GetCompanyMask() != company_mask) {
 
				this->overlay->SetCompanyMask(company_mask);
 
			} else {
 
				this->overlay->SetDirty();
 
			}
 
			assert(cur_index <= MAX_ZOOM_INDEX);
 
		}
 
	}
 

	
 
			tile = this->PixelToTile(zoom_pt->x, zoom_pt->y, &sub);
 
			new_index = Clamp(cur_index + ((change == ZLC_ZOOM_IN) ? -1 : 1), MIN_ZOOM_INDEX, MAX_ZOOM_INDEX);
 
			break;
 
	/** Blink the industries (if hover over an industry). */
 
	void Blink()
 
	{
 
		if (_smallmap_industry_highlight == INVALID_INDUSTRYTYPE) return;
 

	
 
		default: NOT_REACHED();
 
		_smallmap_industry_highlight_state = !_smallmap_industry_highlight_state;
 

	
 
		this->UpdateLinks();
 
		this->SetDirty();
 
	}
 

	
 
	if (new_index != cur_index) {
 
		this->zoom = zoomlevels[new_index];
 
		if (cur_index >= 0) {
 
			Point new_tile = this->PixelToTile(zoom_pt->x, zoom_pt->y, &sub);
 
			this->SetNewScroll(this->scroll_x + (tile.x - new_tile.x) * TILE_SIZE,
 
					this->scroll_y + (tile.y - new_tile.y) * TILE_SIZE, sub);
 
		} else if (this->map_type == SMT_LINKSTATS) {
 
			this->overlay->SetDirty();
 
		}
 
		this->SetWidgetDisabledState(WID_SM_ZOOM_IN,  this->zoom == zoomlevels[MIN_ZOOM_INDEX]);
 
		this->SetWidgetDisabledState(WID_SM_ZOOM_OUT, this->zoom == zoomlevels[MAX_ZOOM_INDEX]);
 
	/** Force a full refresh of the map. */
 
	void ForceRefresh()
 
	{
 
		if (_smallmap_industry_highlight != INVALID_INDUSTRYTYPE) return;
 

	
 
		this->UpdateLinks();
 
		this->SetDirty();
 
	}
 

	
 
public:
 
	friend class NWidgetSmallmapDisplay;
 

	
 
	SmallMapWindow(WindowDesc *desc, int window_number) : Window(desc)
 
	{
 
		_smallmap_industry_highlight = INVALID_INDUSTRYTYPE;
 
		this->overlay = new LinkGraphOverlay(this, WID_SM_MAP, 0, this->GetOverlayCompanyMask(), 1);
 
		this->InitNested(window_number);
 
		this->LowerWidget(this->map_type + WID_SM_CONTOUR);
 

	
 
		this->RebuildColourIndexIfNecessary();
 

	
 
		this->SetWidgetLoweredState(WID_SM_SHOW_HEIGHT, _smallmap_show_heightmap);
 

	
 
		this->SetWidgetLoweredState(WID_SM_TOGGLETOWNNAME, this->show_towns);
 

	
 
		this->SetupWidgetData();
 

	
 
		this->SetZoomLevel(ZLC_INITIALIZE, nullptr);
 
		this->SmallMapCenterOnCurrentPos();
 
		this->SetOverlayCargoMask();
 
	}
 

	
 
	virtual ~SmallMapWindow()
 
	{
 
		delete this->overlay;
 
	}
 

	
 
	/**
 
	 * Center the small map on the current center of the viewport.
 
	 */
 
	void SmallMapCenterOnCurrentPos()
 
	{
 
		const Viewport *vp = GetMainWindow()->viewport;
 
		Point viewport_center = InverseRemapCoords2(vp->virtual_left + vp->virtual_width / 2, vp->virtual_top + vp->virtual_height / 2);
 

	
 
		int sub;
 
		const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
 
		Point sxy = this->ComputeScroll(viewport_center.x / (int)TILE_SIZE, viewport_center.y / (int)TILE_SIZE,
 
				std::max(0, (int)wid->current_x / 2 - 2), wid->current_y / 2, &sub);
 
		this->SetNewScroll(sxy.x, sxy.y, sub);
 
		this->SetDirty();
 
	}
 
}
 

	
 
	/**
 
	 * Get the center of the given station as point on the screen in the smallmap window.
 
	 * @param st Station to find in the smallmap.
 
	 * @return Point with coordinates of the station.
 
	 */
 
	Point GetStationMiddle(const Station *st) const
 
	{
 
		int x = CenterBounds(st->rect.left, st->rect.right, 0);
 
		int y = CenterBounds(st->rect.top, st->rect.bottom, 0);
 
		Point ret = this->RemapTile(x, y);
 

	
 
		/* Same magic 3 as in DrawVehicles; that's where I got it from.
 
		 * No idea what it is, but without it the result looks bad.
 
		 */
 
		ret.x -= 3 + this->subscroll;
 
		return ret;
 
	}
 

	
 
	void Close([[maybe_unused]] int data) override
 
	{
 
		this->BreakIndustryChainLink();
 
		this->Window::Close();
 
	}
 

	
 
	void SetStringParameters(int widget) const override
 
	{
 
		switch (widget) {
 
			case WID_SM_CAPTION:
 
				SetDParam(0, STR_SMALLMAP_TYPE_CONTOURS + this->map_type);
 
				break;
 
		}
 
	}
 

	
 
	void OnInit() override
 
	{
 
		uint min_width = 0;
 
		this->min_number_of_columns = INDUSTRY_MIN_NUMBER_OF_COLUMNS;
 
		this->min_number_of_fixed_rows = lengthof(_linkstat_colours_in_legenda);
 
		for (uint i = 0; i < lengthof(_legend_table); i++) {
 
			uint height = 0;
 
			uint num_columns = 1;
 
			for (const LegendAndColour *tbl = _legend_table[i]; !tbl->end; ++tbl) {
 
				StringID str;
 
				if (i == SMT_INDUSTRY) {
 
					SetDParam(0, tbl->legend);
 
					SetDParam(1, IndustryPool::MAX_SIZE);
 
					str = STR_SMALLMAP_INDUSTRY;
 
				} else if (i == SMT_LINKSTATS) {
 
					SetDParam(0, tbl->legend);
 
					str = STR_SMALLMAP_LINKSTATS;
 
				} else if (i == SMT_OWNER) {
 
					if (tbl->company != INVALID_COMPANY) {
 
						if (!Company::IsValidID(tbl->company)) {
 
							/* Rebuild the owner legend. */
 
							BuildOwnerLegend();
 
							this->OnInit();
 
							return;
 
						}
 
						/* Non-fixed legend entries for the owner view. */
 
						SetDParam(0, tbl->company);
 
						str = STR_SMALLMAP_COMPANY;
 
					} else {
 
						str = tbl->legend;
 
					}
 
				} else {
 
					if (tbl->col_break) {
 
						this->min_number_of_fixed_rows = std::max(this->min_number_of_fixed_rows, height);
 
						height = 0;
 
						num_columns++;
 
					}
 
					height++;
 
					str = tbl->legend;
 
				}
 
				min_width = std::max(GetStringBoundingBox(str).width, min_width);
 
			}
 
			this->min_number_of_fixed_rows = std::max(this->min_number_of_fixed_rows, height);
 
			this->min_number_of_columns = std::max(this->min_number_of_columns, num_columns);
 
		}
 

	
 
		/* Width of the legend blob. */
 
		this->legend_width = (FONT_HEIGHT_SMALL - ScaleGUITrad(1)) * 8 / 5;
 

	
 
		/* The width of a column is the minimum width of all texts + the size of the blob + some spacing */
 
		this->column_width = min_width + WidgetDimensions::scaled.hsep_normal + this->legend_width + WidgetDimensions::scaled.framerect.Horizontal();
 
	}
 

	
 
	void OnPaint() override
 
	{
 
		if (this->map_type == SMT_OWNER) {
 
			for (const LegendAndColour *tbl = _legend_table[this->map_type]; !tbl->end; ++tbl) {
 
				if (tbl->company != INVALID_COMPANY && !Company::IsValidID(tbl->company)) {
 
					/* Rebuild the owner legend. */
 
					BuildOwnerLegend();
 
					this->InvalidateData(1);
 
					break;
 
				}
 
			}
 
		}
 

	
 
		this->DrawWidgets();
 
	}
 

	
 
	void DrawWidget(const Rect &r, int widget) const override
 
	{
 
		switch (widget) {
 
			case WID_SM_MAP: {
 
				Rect ir = r.Shrink(WidgetDimensions::scaled.bevel);
 
				DrawPixelInfo new_dpi;
 
				if (!FillDrawPixelInfo(&new_dpi, ir.left, ir.top, ir.Width(), ir.Height())) return;
 
				this->DrawSmallMap(&new_dpi);
 
				break;
 
			}
 

	
 
			case WID_SM_LEGEND: {
 
				uint columns = this->GetNumberColumnsLegend(r.Width());
 
				uint number_of_rows = this->GetNumberRowsLegend(columns);
 
				bool rtl = _current_text_dir == TD_RTL;
 
				uint i = 0; // Row counter for industry legend.
 
				uint row_height = FONT_HEIGHT_SMALL;
 
				int padding = ScaleGUITrad(1);
 

	
 
				Rect origin = r.WithWidth(this->column_width, rtl).Shrink(WidgetDimensions::scaled.framerect).WithHeight(row_height);
 
				Rect text = origin.Indent(this->legend_width + WidgetDimensions::scaled.hsep_normal, rtl);
 
				Rect icon = origin.WithWidth(this->legend_width, rtl).Shrink(0, padding, 0, 0);
 

	
 
				StringID string = STR_NULL;
 
				switch (this->map_type) {
 
					case SMT_INDUSTRY:
 
						string = STR_SMALLMAP_INDUSTRY;
 
						break;
 
					case SMT_LINKSTATS:
 
						string = STR_SMALLMAP_LINKSTATS;
 
						break;
 
					case SMT_OWNER:
 
						string = STR_SMALLMAP_COMPANY;
 
						break;
 
					default:
 
						break;
 
				}
 

	
 
				for (const LegendAndColour *tbl = _legend_table[this->map_type]; !tbl->end; ++tbl) {
 
					if (tbl->col_break || ((this->map_type == SMT_INDUSTRY || this->map_type == SMT_OWNER || this->map_type == SMT_LINKSTATS) && i++ >= number_of_rows)) {
 
						/* Column break needed, continue at top, COLUMN_WIDTH pixels
 
						 * (one "row") to the right. */
 
						int x = rtl ? -(int)this->column_width : this->column_width;
 
						int y = origin.top - text.top;
 
						text = text.Translate(x, y);
 
						icon = icon.Translate(x, y);
 
						i = 1;
 
					}
 

	
 
					uint8_t legend_colour = tbl->colour;
 

	
 
					switch (this->map_type) {
 
						case SMT_INDUSTRY:
 
							/* Industry name must be formatted, since it's not in tiny font in the specs.
 
							 * So, draw with a parameter and use the STR_SMALLMAP_INDUSTRY string, which is tiny font */
 
							SetDParam(0, tbl->legend);
 
							SetDParam(1, Industry::GetIndustryTypeCount(tbl->type));
 
							if (tbl->show_on_map && tbl->type == _smallmap_industry_highlight) {
 
								legend_colour = _smallmap_industry_highlight_state ? PC_WHITE : PC_BLACK;
 
							}
 
							FALLTHROUGH;
 

	
 
/**
 
 * Decide which colours to show to the user for a group of tiles.
 
 * @param ta Tile area to investigate.
 
 * @return Colours to display.
 
 */
 
inline uint32_t SmallMapWindow::GetTileColours(const TileArea &ta) const
 
{
 
	int importance = 0;
 
	TileIndex tile = INVALID_TILE; // Position of the most important tile.
 
	TileType et = MP_VOID;         // Effective tile type at that position.
 
						case SMT_LINKSTATS:
 
							SetDParam(0, tbl->legend);
 
							FALLTHROUGH;
 

	
 
						case SMT_OWNER:
 
							if (this->map_type != SMT_OWNER || tbl->company != INVALID_COMPANY) {
 
								if (this->map_type == SMT_OWNER) SetDParam(0, tbl->company);
 
								if (!tbl->show_on_map) {
 
									/* Simply draw the string, not the black border of the legend colour.
 
									 * This will enforce the idea of the disabled item */
 
									DrawString(text, string, TC_GREY);
 
								} else {
 
									DrawString(text, string, TC_BLACK);
 
									GfxFillRect(icon, PC_BLACK); // Outer border of the legend colour
 
								}
 
								break;
 
							}
 
							FALLTHROUGH;
 

	
 
						default:
 
							if (this->map_type == SMT_CONTOUR) SetDParam(0, tbl->height * TILE_HEIGHT_STEP);
 
							/* Anything that is not an industry or a company is using normal process */
 
							GfxFillRect(icon, PC_BLACK);
 
							DrawString(text, tbl->legend);
 
							break;
 
					}
 
					GfxFillRect(icon.Shrink(WidgetDimensions::scaled.bevel), legend_colour); // Legend colour
 

	
 
					text = text.Translate(0, row_height);
 
					icon = icon.Translate(0, row_height);
 
				}
 
			}
 
		}
 
	}
 

	
 
	void OnClick([[maybe_unused]] Point pt, int widget, [[maybe_unused]] int click_count) override
 
	{
 
		switch (widget) {
 
			case WID_SM_MAP: { // Map window
 
				if (click_count > 0) this->mouse_capture_widget = widget;
 

	
 
				const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
 
				Window *w = GetMainWindow();
 
				int sub;
 
				pt = this->PixelToTile(pt.x - wid->pos_x, pt.y - wid->pos_y, &sub);
 
				ScrollWindowTo(this->scroll_x + pt.x * TILE_SIZE, this->scroll_y + pt.y * TILE_SIZE, -1, w);
 

	
 
				this->SetDirty();
 
				break;
 
			}
 

	
 
			case WID_SM_ZOOM_IN:
 
			case WID_SM_ZOOM_OUT: {
 
				const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
 
				Point zoom_pt = { (int)wid->current_x / 2, (int)wid->current_y / 2};
 
				this->SetZoomLevel((widget == WID_SM_ZOOM_IN) ? ZLC_ZOOM_IN : ZLC_ZOOM_OUT, &zoom_pt);
 
				if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
 
				break;
 
			}
 

	
 
			case WID_SM_CONTOUR:    // Show land contours
 
			case WID_SM_VEHICLES:   // Show vehicles
 
			case WID_SM_INDUSTRIES: // Show industries
 
			case WID_SM_LINKSTATS:  // Show route map
 
			case WID_SM_ROUTES:     // Show transport routes
 
			case WID_SM_VEGETATION: // Show vegetation
 
			case WID_SM_OWNERS:     // Show land owners
 
				this->SwitchMapType((SmallMapType)(widget - WID_SM_CONTOUR));
 
				if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
 
				break;
 

	
 
			case WID_SM_CENTERMAP: // Center the smallmap again
 
				this->SmallMapCenterOnCurrentPos();
 
				this->HandleButtonClick(WID_SM_CENTERMAP);
 
				if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
 
				break;
 

	
 
			case WID_SM_TOGGLETOWNNAME: // Toggle town names
 
				this->show_towns = !this->show_towns;
 
				this->SetWidgetLoweredState(WID_SM_TOGGLETOWNNAME, this->show_towns);
 

	
 
	for (TileIndex ti : ta) {
 
		TileType ttype = GetTileType(ti);
 
				this->SetDirty();
 
				if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
 
				break;
 

	
 
			case WID_SM_LEGEND: // Legend
 
				if (this->map_type == SMT_INDUSTRY || this->map_type == SMT_LINKSTATS || this->map_type == SMT_OWNER) {
 
					int click_pos = this->GetPositionOnLegend(pt);
 
					if (click_pos < 0) break;
 

	
 
					/* If industry type small map*/
 
					if (this->map_type == SMT_INDUSTRY) {
 
						/* If click on industries label, find right industry type and enable/disable it. */
 
						if (click_pos < _smallmap_industry_count) {
 
							this->SelectLegendItem(click_pos, _legend_from_industries, _smallmap_industry_count);
 
						}
 
					} else if (this->map_type == SMT_LINKSTATS) {
 
						if (click_pos < _smallmap_cargo_count) {
 
							this->SelectLegendItem(click_pos, _legend_linkstats, _smallmap_cargo_count);
 
							this->SetOverlayCargoMask();
 
						}
 
					} else if (this->map_type == SMT_OWNER) {
 
						if (click_pos < _smallmap_company_count) {
 
							this->SelectLegendItem(click_pos, _legend_land_owners, _smallmap_company_count, NUM_NO_COMPANY_ENTRIES);
 
						}
 
					}
 
					this->SetDirty();
 
				}
 
				break;
 

	
 
		switch (ttype) {
 
			case MP_TUNNELBRIDGE: {
 
				TransportType tt = GetTunnelBridgeTransportType(ti);
 
			case WID_SM_ENABLE_ALL:
 
			case WID_SM_DISABLE_ALL: {
 
				LegendAndColour *tbl = nullptr;
 
				switch (this->map_type) {
 
					case SMT_INDUSTRY:
 
						tbl = _legend_from_industries;
 
						this->BreakIndustryChainLink();
 
						break;
 
					case SMT_OWNER:
 
						tbl = &(_legend_land_owners[NUM_NO_COMPANY_ENTRIES]);
 
						break;
 
					case SMT_LINKSTATS:
 
						tbl = _legend_linkstats;
 
						break;
 
					default:
 
						NOT_REACHED();
 
				}
 
				for (;!tbl->end && tbl->legend != STR_LINKGRAPH_LEGEND_UNUSED; ++tbl) {
 
					tbl->show_on_map = (widget == WID_SM_ENABLE_ALL);
 
				}
 
				if (this->map_type == SMT_LINKSTATS) this->SetOverlayCargoMask();
 
				this->SetDirty();
 
				break;
 
			}
 

	
 
				switch (tt) {
 
					case TRANSPORT_RAIL: ttype = MP_RAILWAY; break;
 
					case TRANSPORT_ROAD: ttype = MP_ROAD;    break;
 
					default:             ttype = MP_WATER;   break;
 
			case WID_SM_SHOW_HEIGHT: // Enable/disable showing of heightmap.
 
				_smallmap_show_heightmap = !_smallmap_show_heightmap;
 
				this->SetWidgetLoweredState(WID_SM_SHOW_HEIGHT, _smallmap_show_heightmap);
 
				this->SetDirty();
 
				break;
 
		}
 
	}
 

	
 
	/**
 
	 * Some data on this window has become invalid.
 
	 * @param data Information about the changed data.
 
	 * - data = 0: Displayed industries at the industry chain window have changed.
 
	 * - data = 1: Companies have changed.
 
	 * - data = 2: Cheat changing the maximum heightlevel has been used, rebuild our heightlevel-to-colour index
 
	 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
 
	 */
 
	void OnInvalidateData(int data = 0, bool gui_scope = true) override
 
	{
 
		if (!gui_scope) return;
 

	
 
		switch (data) {
 
			case 1:
 
				/* The owner legend has already been rebuilt. */
 
				this->ReInit();
 
				break;
 

	
 
			case 0: {
 
				extern std::bitset<NUM_INDUSTRYTYPES> _displayed_industries;
 
				if (this->map_type != SMT_INDUSTRY) this->SwitchMapType(SMT_INDUSTRY);
 

	
 
				for (int i = 0; i != _smallmap_industry_count; i++) {
 
					_legend_from_industries[i].show_on_map = _displayed_industries.test(_legend_from_industries[i].type);
 
				}
 
				break;
 
			}
 

	
 
			case MP_INDUSTRY:
 
				/* Special handling of industries while in "Industries" smallmap view. */
 
				if (this->map_type == SMT_INDUSTRY) {
 
					/* If industry is allowed to be seen, use its colour on the map.
 
					 * This has the highest priority above any value in _tiletype_importance. */
 
					IndustryType type = Industry::GetByTile(ti)->type;
 
					if (_legend_from_industries[_industry_to_list_pos[type]].show_on_map) {
 
						if (type == _smallmap_industry_highlight) {
 
							if (_smallmap_industry_highlight_state) return MKCOLOUR_XXXX(PC_WHITE);
 
						} else {
 
							return GetIndustrySpec(type)->map_colour * 0x01010101;
 
						}
 
					}
 
					/* Otherwise make it disappear */
 
					ttype = IsTileOnWater(ti) ? MP_WATER : MP_CLEAR;
 
				}
 
			case 2:
 
				this->RebuildColourIndexIfNecessary();
 
				break;
 

	
 
			default:
 
				break;
 
			default: NOT_REACHED();
 
		}
 

	
 
		if (_tiletype_importance[ttype] > importance) {
 
			importance = _tiletype_importance[ttype];
 
			tile = ti;
 
			et = ttype;
 
		}
 
		this->SetDirty();
 
	}
 

	
 
	switch (this->map_type) {
 
		case SMT_CONTOUR:
 
			return GetSmallMapContoursPixels(tile, et);
 

	
 
		case SMT_VEHICLES:
 
			return GetSmallMapVehiclesPixels(tile, et);
 

	
 
		case SMT_INDUSTRY:
 
			return GetSmallMapIndustriesPixels(tile, et);
 

	
 
		case SMT_LINKSTATS:
 
			return GetSmallMapLinkStatsPixels(tile, et);
 

	
 
		case SMT_ROUTES:
 
			return GetSmallMapRoutesPixels(tile, et);
 

	
 
		case SMT_VEGETATION:
 
			return GetSmallMapVegetationPixels(tile, et);
 

	
 
		case SMT_OWNER:
 
			return GetSmallMapOwnerPixels(tile, et, IncludeHeightmap::IfEnabled);
 

	
 
		default: NOT_REACHED();
 
	}
 
}
 

	
 
/**
 
 * Draws one column of tiles of the small map in a certain mode onto the screen buffer, skipping the shifted rows in between.
 
 *
 
 * @param dst Pointer to a part of the screen buffer to write to.
 
 * @param xc The X coordinate of the first tile in the column.
 
 * @param yc The Y coordinate of the first tile in the column
 
 * @param pitch Number of pixels to advance in the screen buffer each time a pixel is written.
 
 * @param reps Number of lines to draw
 
 * @param start_pos Position of first pixel to draw.
 
 * @param end_pos Position of last pixel to draw (exclusive).
 
 * @param blitter current blitter
 
 * @note If pixel position is below \c 0, skip drawing.
 
 */
 
void SmallMapWindow::DrawSmallMapColumn(void *dst, uint xc, uint yc, int pitch, int reps, int start_pos, int end_pos, Blitter *blitter) const
 
{
 
	void *dst_ptr_abs_end = blitter->MoveTo(_screen.dst_ptr, 0, _screen.height);
 
	uint min_xy = _settings_game.construction.freeform_edges ? 1 : 0;
 

	
 
	do {
 
		/* Check if the tile (xc,yc) is within the map range */
 
		if (xc >= Map::MaxX() || yc >= Map::MaxY()) continue;
 

	
 
		/* Check if the dst pointer points to a pixel inside the screen buffer */
 
		if (dst < _screen.dst_ptr) continue;
 
		if (dst >= dst_ptr_abs_end) continue;
 

	
 
		/* Construct tilearea covered by (xc, yc, xc + this->zoom, yc + this->zoom) such that it is within min_xy limits. */
 
		TileArea ta;
 
		if (min_xy == 1 && (xc == 0 || yc == 0)) {
 
			if (this->zoom == 1) continue; // The tile area is empty, don't draw anything.
 

	
 
			ta = TileArea(TileXY(std::max(min_xy, xc), std::max(min_xy, yc)), this->zoom - (xc == 0), this->zoom - (yc == 0));
 
		} else {
 
			ta = TileArea(TileXY(xc, yc), this->zoom, this->zoom);
 
		}
 
		ta.ClampToMap(); // Clamp to map boundaries (may contain MP_VOID tiles!).
 

	
 
		uint32_t val = this->GetTileColours(ta);
 
		uint8_t *val8 = (uint8_t *)&val;
 
		int idx = std::max(0, -start_pos);
 
		for (int pos = std::max(0, start_pos); pos < end_pos; pos++) {
 
			blitter->SetPixel(dst, idx, 0, val8[idx]);
 
			idx++;
 
		}
 
	/* Switch to next tile in the column */
 
	} while (xc += this->zoom, yc += this->zoom, dst = blitter->MoveTo(dst, pitch, 0), --reps != 0);
 
}
 

	
 
/**
 
 * Adds vehicles to the smallmap.
 
 * @param dpi the part of the smallmap to be drawn into
 
 * @param blitter current blitter
 
 */
 
void SmallMapWindow::DrawVehicles(const DrawPixelInfo *dpi, Blitter *blitter) const
 
{
 
	for (const Vehicle *v : Vehicle::Iterate()) {
 
		if (v->type == VEH_EFFECT) continue;
 
		if (v->vehstatus & (VS_HIDDEN | VS_UNCLICKABLE)) continue;
 

	
 
		/* Remap into flat coordinates. */
 
		Point pt = this->RemapTile(v->x_pos / (int)TILE_SIZE, v->y_pos / (int)TILE_SIZE);
 

	
 
		int y = pt.y - dpi->top;
 
		if (!IsInsideMM(y, 0, dpi->height)) continue; // y is out of bounds.
 

	
 
		bool skip = false; // Default is to draw both pixels.
 
		int x = pt.x - this->subscroll - 3 - dpi->left; // Offset X coordinate.
 
		if (x < 0) {
 
			/* if x+1 is 0, that means we're on the very left edge,
 
			 * and should thus only draw a single pixel */
 
			if (++x != 0) continue;
 
			skip = true;
 
		} else if (x >= dpi->width - 1) {
 
			/* Check if we're at the very right edge, and if so draw only a single pixel */
 
			if (x != dpi->width - 1) continue;
 
			skip = true;
 
		}
 

	
 
		/* Calculate pointer to pixel and the colour */
 
		byte colour = (this->map_type == SMT_VEHICLES) ? _vehicle_type_colours[v->type] : PC_WHITE;
 
	bool OnRightClick(Point, int widget) override
 
	{
 
		if (widget != WID_SM_MAP || _scrolling_viewport) return false;
 

	
 
		/* And draw either one or two pixels depending on clipping */
 
		blitter->SetPixel(dpi->dst_ptr, x, y, colour);
 
		if (!skip) blitter->SetPixel(dpi->dst_ptr, x + 1, y, colour);
 
	}
 
}
 

	
 
/**
 
 * Adds town names to the smallmap.
 
 * @param dpi the part of the smallmap to be drawn into
 
 */
 
void SmallMapWindow::DrawTowns(const DrawPixelInfo *dpi) const
 
{
 
	for (const Town *t : Town::Iterate()) {
 
		/* Remap the town coordinate */
 
		Point pt = this->RemapTile(TileX(t->xy), TileY(t->xy));
 
		int x = pt.x - this->subscroll - (t->cache.sign.width_small >> 1);
 
		int y = pt.y;
 

	
 
		/* Check if the town sign is within bounds */
 
		if (x + t->cache.sign.width_small > dpi->left &&
 
				x < dpi->left + dpi->width &&
 
				y + FONT_HEIGHT_SMALL > dpi->top &&
 
				y < dpi->top + dpi->height) {
 
			/* And draw it. */
 
			SetDParam(0, t->index);
 
			DrawString(x, x + t->cache.sign.width_small, y, STR_SMALLMAP_TOWN);
 
		}
 
	}
 
}
 

	
 
/**
 
 * Adds map indicators to the smallmap.
 
 */
 
void SmallMapWindow::DrawMapIndicators() const
 
{
 
	/* Find main viewport. */
 
	const Viewport *vp = GetMainWindow()->viewport;
 

	
 
	Point upper_left_smallmap_coord  = InverseRemapCoords2(vp->virtual_left, vp->virtual_top);
 
	Point lower_right_smallmap_coord = InverseRemapCoords2(vp->virtual_left + vp->virtual_width - 1, vp->virtual_top + vp->virtual_height - 1);
 

	
 
	Point upper_left = this->RemapTile(upper_left_smallmap_coord.x / (int)TILE_SIZE, upper_left_smallmap_coord.y / (int)TILE_SIZE);
 
	upper_left.x -= this->subscroll;
 

	
 
	Point lower_right = this->RemapTile(lower_right_smallmap_coord.x / (int)TILE_SIZE, lower_right_smallmap_coord.y / (int)TILE_SIZE);
 
	lower_right.x -= this->subscroll;
 

	
 
	SmallMapWindow::DrawVertMapIndicator(upper_left.x, upper_left.y, lower_right.y);
 
	SmallMapWindow::DrawVertMapIndicator(lower_right.x, upper_left.y, lower_right.y);
 

	
 
	SmallMapWindow::DrawHorizMapIndicator(upper_left.x, lower_right.x, upper_left.y);
 
	SmallMapWindow::DrawHorizMapIndicator(upper_left.x, lower_right.x, lower_right.y);
 
}
 

	
 
/**
 
 * Draws the small map.
 
 *
 
 * Basically, the small map is draw column of pixels by column of pixels. The pixels
 
 * are drawn directly into the screen buffer. The final map is drawn in multiple passes.
 
 * The passes are:
 
 * <ol><li>The colours of tiles in the different modes.</li>
 
 * <li>Town names (optional)</li></ol>
 
 *
 
 * @param dpi pointer to pixel to write onto
 
 */
 
void SmallMapWindow::DrawSmallMap(DrawPixelInfo *dpi) const
 
{
 
	Blitter *blitter = BlitterFactory::GetCurrentBlitter();
 
	AutoRestoreBackup dpi_backup(_cur_dpi, dpi);
 

	
 
	/* Clear it */
 
	GfxFillRect(dpi->left, dpi->top, dpi->left + dpi->width - 1, dpi->top + dpi->height - 1, PC_BLACK);
 

	
 
	/* Which tile is displayed at (dpi->left, dpi->top)? */
 
	int dx;
 
	Point tile = this->PixelToTile(dpi->left, dpi->top, &dx);
 
	int tile_x = this->scroll_x / (int)TILE_SIZE + tile.x;
 
	int tile_y = this->scroll_y / (int)TILE_SIZE + tile.y;
 

	
 
	void *ptr = blitter->MoveTo(dpi->dst_ptr, -dx - 4, 0);
 
	int x = - dx - 4;
 
	int y = 0;
 

	
 
	for (;;) {
 
		/* Distance from left edge */
 
		if (x >= -3) {
 
			if (x >= dpi->width) break; // Exit the loop.
 

	
 
			int end_pos = std::min(dpi->width, x + 4);
 
			int reps = (dpi->height - y + 1) / 2; // Number of lines.
 
			if (reps > 0) {
 
				this->DrawSmallMapColumn(ptr, tile_x, tile_y, dpi->pitch * 2, reps, x, end_pos, blitter);
 
			}
 
		}
 

	
 
		if (y == 0) {
 
			tile_y += this->zoom;
 
			y++;
 
			ptr = blitter->MoveTo(ptr, 0, 1);
 
		} else {
 
			tile_x -= this->zoom;
 
			y--;
 
			ptr = blitter->MoveTo(ptr, 0, -1);
 
		}
 
		ptr = blitter->MoveTo(ptr, 2, 0);
 
		x += 2;
 
		_scrolling_viewport = true;
 
		return true;
 
	}
 

	
 
	/* Draw vehicles */
 
	if (this->map_type == SMT_CONTOUR || this->map_type == SMT_VEHICLES) this->DrawVehicles(dpi, blitter);
 

	
 
	/* Draw link stat overlay */
 
	if (this->map_type == SMT_LINKSTATS) this->overlay->Draw(dpi);
 

	
 
	/* Draw town names */
 
	if (this->show_towns) this->DrawTowns(dpi);
 

	
 
	/* Draw map indicators */
 
	this->DrawMapIndicators();
 
}
 

	
 
/**
 
 * Function to set up widgets depending on the information being shown on the smallmap.
 
 */
 
void SmallMapWindow::SetupWidgetData()
 
{
 
	StringID legend_tooltip;
 
	StringID enable_all_tooltip;
 
	StringID disable_all_tooltip;
 
	int plane;
 
	switch (this->map_type) {
 
		case SMT_INDUSTRY:
 
			legend_tooltip = STR_SMALLMAP_TOOLTIP_INDUSTRY_SELECTION;
 
			enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_INDUSTRIES;
 
			disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_INDUSTRIES;
 
			plane = 0;
 
			break;
 

	
 
		case SMT_OWNER:
 
			legend_tooltip = STR_SMALLMAP_TOOLTIP_COMPANY_SELECTION;
 
			enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_COMPANIES;
 
			disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_COMPANIES;
 
			plane = 0;
 
			break;
 

	
 
		case SMT_LINKSTATS:
 
			legend_tooltip = STR_SMALLMAP_TOOLTIP_CARGO_SELECTION;
 
			enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_CARGOS;
 
			disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_CARGOS;
 
			plane = 0;
 
			break;
 

	
 
		default:
 
			legend_tooltip = STR_NULL;
 
			enable_all_tooltip = STR_NULL;
 
			disable_all_tooltip = STR_NULL;
 
			plane = 1;
 
			break;
 
	}
 

	
 
	this->GetWidget<NWidgetCore>(WID_SM_LEGEND)->SetDataTip(STR_NULL, legend_tooltip);
 
	this->GetWidget<NWidgetCore>(WID_SM_ENABLE_ALL)->SetDataTip(STR_SMALLMAP_ENABLE_ALL, enable_all_tooltip);
 
	this->GetWidget<NWidgetCore>(WID_SM_DISABLE_ALL)->SetDataTip(STR_SMALLMAP_DISABLE_ALL, disable_all_tooltip);
 
	this->GetWidget<NWidgetStacked>(WID_SM_SELECT_BUTTONS)->SetDisplayedPlane(plane);
 
}
 

	
 
SmallMapWindow::SmallMapWindow(WindowDesc *desc, int window_number) : Window(desc)
 
{
 
	_smallmap_industry_highlight = INVALID_INDUSTRYTYPE;
 
	this->overlay = new LinkGraphOverlay(this, WID_SM_MAP, 0, this->GetOverlayCompanyMask(), 1);
 
	this->InitNested(window_number);
 
	this->LowerWidget(this->map_type + WID_SM_CONTOUR);
 

	
 
	this->RebuildColourIndexIfNecessary();
 

	
 
	this->SetWidgetLoweredState(WID_SM_SHOW_HEIGHT, _smallmap_show_heightmap);
 

	
 
	this->SetWidgetLoweredState(WID_SM_TOGGLETOWNNAME, this->show_towns);
 

	
 
	this->SetupWidgetData();
 

	
 
	this->SetZoomLevel(ZLC_INITIALIZE, nullptr);
 
	this->SmallMapCenterOnCurrentPos();
 
	this->SetOverlayCargoMask();
 
}
 

	
 
SmallMapWindow::~SmallMapWindow()
 
{
 
	delete this->overlay;
 
}
 

	
 
/* virtual */ void SmallMapWindow::Close([[maybe_unused]] int data)
 
{
 
	this->BreakIndustryChainLink();
 
	this->Window::Close();
 
}
 

	
 
/**
 
 * Rebuilds the colour indices used for fast access to the smallmap contour colours based on the heightlevel.
 
 */
 
void SmallMapWindow::RebuildColourIndexIfNecessary()
 
{
 
	/* Rebuild colour indices if necessary. */
 
	if (SmallMapWindow::map_height_limit == _settings_game.construction.map_height_limit) return;
 

	
 
	for (uint n = 0; n < lengthof(_heightmap_schemes); n++) {
 
		/* The heights go from 0 up to and including maximum. */
 
		int heights = _settings_game.construction.map_height_limit + 1;
 
		_heightmap_schemes[n].height_colours = ReallocT<uint32_t>(_heightmap_schemes[n].height_colours, heights);
 

	
 
		for (int z = 0; z < heights; z++) {
 
			size_t access_index = (_heightmap_schemes[n].colour_count * z) / heights;
 

	
 
			/* Choose colour by mapping the range (0..max heightlevel) on the complete colour table. */
 
			_heightmap_schemes[n].height_colours[z] = _heightmap_schemes[n].height_colours_base[access_index];
 
		}
 
	}
 

	
 
	SmallMapWindow::map_height_limit = _settings_game.construction.map_height_limit;
 
	BuildLandLegend();
 
}
 

	
 
/* virtual */ void SmallMapWindow::SetStringParameters(int widget) const
 
{
 
	switch (widget) {
 
		case WID_SM_CAPTION:
 
			SetDParam(0, STR_SMALLMAP_TYPE_CONTOURS + this->map_type);
 
			break;
 
	}
 
}
 

	
 
/* virtual */ void SmallMapWindow::OnInit()
 
{
 
	uint min_width = 0;
 
	this->min_number_of_columns = INDUSTRY_MIN_NUMBER_OF_COLUMNS;
 
	this->min_number_of_fixed_rows = lengthof(_linkstat_colours_in_legenda);
 
	for (uint i = 0; i < lengthof(_legend_table); i++) {
 
		uint height = 0;
 
		uint num_columns = 1;
 
		for (const LegendAndColour *tbl = _legend_table[i]; !tbl->end; ++tbl) {
 
			StringID str;
 
			if (i == SMT_INDUSTRY) {
 
				SetDParam(0, tbl->legend);
 
				SetDParam(1, IndustryPool::MAX_SIZE);
 
				str = STR_SMALLMAP_INDUSTRY;
 
			} else if (i == SMT_LINKSTATS) {
 
				SetDParam(0, tbl->legend);
 
				str = STR_SMALLMAP_LINKSTATS;
 
			} else if (i == SMT_OWNER) {
 
				if (tbl->company != INVALID_COMPANY) {
 
					if (!Company::IsValidID(tbl->company)) {
 
						/* Rebuild the owner legend. */
 
						BuildOwnerLegend();
 
						this->OnInit();
 
						return;
 
					}
 
					/* Non-fixed legend entries for the owner view. */
 
					SetDParam(0, tbl->company);
 
					str = STR_SMALLMAP_COMPANY;
 
				} else {
 
					str = tbl->legend;
 
				}
 
			} else {
 
				if (tbl->col_break) {
 
					this->min_number_of_fixed_rows = std::max(this->min_number_of_fixed_rows, height);
 
					height = 0;
 
					num_columns++;
 
				}
 
				height++;
 
				str = tbl->legend;
 
			}
 
			min_width = std::max(GetStringBoundingBox(str).width, min_width);
 
		}
 
		this->min_number_of_fixed_rows = std::max(this->min_number_of_fixed_rows, height);
 
		this->min_number_of_columns = std::max(this->min_number_of_columns, num_columns);
 
	}
 

	
 
	/* Width of the legend blob. */
 
	this->legend_width = (FONT_HEIGHT_SMALL - ScaleGUITrad(1)) * 8 / 5;
 

	
 
	/* The width of a column is the minimum width of all texts + the size of the blob + some spacing */
 
	this->column_width = min_width + WidgetDimensions::scaled.hsep_normal + this->legend_width + WidgetDimensions::scaled.framerect.Horizontal();
 
}
 

	
 
/* virtual */ void SmallMapWindow::OnPaint()
 
{
 
	if (this->map_type == SMT_OWNER) {
 
		for (const LegendAndColour *tbl = _legend_table[this->map_type]; !tbl->end; ++tbl) {
 
			if (tbl->company != INVALID_COMPANY && !Company::IsValidID(tbl->company)) {
 
				/* Rebuild the owner legend. */
 
				BuildOwnerLegend();
 
				this->InvalidateData(1);
 
				break;
 
	void OnMouseWheel(int wheel) override
 
	{
 
		if (_settings_client.gui.scrollwheel_scrolling != 2) {
 
			const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
 
			int cursor_x = _cursor.pos.x - this->left - wid->pos_x;
 
			int cursor_y = _cursor.pos.y - this->top  - wid->pos_y;
 
			if (IsInsideMM(cursor_x, 0, wid->current_x) && IsInsideMM(cursor_y, 0, wid->current_y)) {
 
				Point pt = {cursor_x, cursor_y};
 
				this->SetZoomLevel((wheel < 0) ? ZLC_ZOOM_IN : ZLC_ZOOM_OUT, &pt);
 
			}
 
		}
 
	}
 

	
 
	this->DrawWidgets();
 
}
 

	
 
/* virtual */ void SmallMapWindow::DrawWidget(const Rect &r, int widget) const
 
{
 
	switch (widget) {
 
		case WID_SM_MAP: {
 
			Rect ir = r.Shrink(WidgetDimensions::scaled.bevel);
 
			DrawPixelInfo new_dpi;
 
			if (!FillDrawPixelInfo(&new_dpi, ir.left, ir.top, ir.Width(), ir.Height())) return;
 
			this->DrawSmallMap(&new_dpi);
 
			break;
 
		}
 

	
 
		case WID_SM_LEGEND: {
 
			uint columns = this->GetNumberColumnsLegend(r.Width());
 
			uint number_of_rows = this->GetNumberRowsLegend(columns);
 
			bool rtl = _current_text_dir == TD_RTL;
 
			uint i = 0; // Row counter for industry legend.
 
			uint row_height = FONT_HEIGHT_SMALL;
 
			int padding = ScaleGUITrad(1);
 

	
 
			Rect origin = r.WithWidth(this->column_width, rtl).Shrink(WidgetDimensions::scaled.framerect).WithHeight(row_height);
 
			Rect text = origin.Indent(this->legend_width + WidgetDimensions::scaled.hsep_normal, rtl);
 
			Rect icon = origin.WithWidth(this->legend_width, rtl).Shrink(0, padding, 0, 0);
 

	
 
			StringID string = STR_NULL;
 
			switch (this->map_type) {
 
				case SMT_INDUSTRY:
 
					string = STR_SMALLMAP_INDUSTRY;
 
					break;
 
				case SMT_LINKSTATS:
 
					string = STR_SMALLMAP_LINKSTATS;
 
					break;
 
				case SMT_OWNER:
 
					string = STR_SMALLMAP_COMPANY;
 
					break;
 
				default:
 
					break;
 
			}
 

	
 
			for (const LegendAndColour *tbl = _legend_table[this->map_type]; !tbl->end; ++tbl) {
 
				if (tbl->col_break || ((this->map_type == SMT_INDUSTRY || this->map_type == SMT_OWNER || this->map_type == SMT_LINKSTATS) && i++ >= number_of_rows)) {
 
					/* Column break needed, continue at top, COLUMN_WIDTH pixels
 
					 * (one "row") to the right. */
 
					int x = rtl ? -(int)this->column_width : this->column_width;
 
					int y = origin.top - text.top;
 
					text = text.Translate(x, y);
 
					icon = icon.Translate(x, y);
 
					i = 1;
 
				}
 

	
 
				uint8_t legend_colour = tbl->colour;
 

	
 
				switch (this->map_type) {
 
					case SMT_INDUSTRY:
 
						/* Industry name must be formatted, since it's not in tiny font in the specs.
 
						 * So, draw with a parameter and use the STR_SMALLMAP_INDUSTRY string, which is tiny font */
 
						SetDParam(0, tbl->legend);
 
						SetDParam(1, Industry::GetIndustryTypeCount(tbl->type));
 
						if (tbl->show_on_map && tbl->type == _smallmap_industry_highlight) {
 
							legend_colour = _smallmap_industry_highlight_state ? PC_WHITE : PC_BLACK;
 
						}
 
						FALLTHROUGH;
 

	
 
					case SMT_LINKSTATS:
 
						SetDParam(0, tbl->legend);
 
						FALLTHROUGH;
 

	
 
					case SMT_OWNER:
 
						if (this->map_type != SMT_OWNER || tbl->company != INVALID_COMPANY) {
 
							if (this->map_type == SMT_OWNER) SetDParam(0, tbl->company);
 
							if (!tbl->show_on_map) {
 
								/* Simply draw the string, not the black border of the legend colour.
 
								 * This will enforce the idea of the disabled item */
 
								DrawString(text, string, TC_GREY);
 
							} else {
 
								DrawString(text, string, TC_BLACK);
 
								GfxFillRect(icon, PC_BLACK); // Outer border of the legend colour
 
							}
 
							break;
 
						}
 
						FALLTHROUGH;
 

	
 
					default:
 
						if (this->map_type == SMT_CONTOUR) SetDParam(0, tbl->height * TILE_HEIGHT_STEP);
 
						/* Anything that is not an industry or a company is using normal process */
 
						GfxFillRect(icon, PC_BLACK);
 
						DrawString(text, tbl->legend);
 
						break;
 
				}
 
				GfxFillRect(icon.Shrink(WidgetDimensions::scaled.bevel), legend_colour); // Legend colour
 

	
 
				text = text.Translate(0, row_height);
 
				icon = icon.Translate(0, row_height);
 
			}
 
		}
 
	}
 
}
 
	void OnScroll(Point delta) override
 
	{
 
		if (_settings_client.gui.scroll_mode == VSM_VIEWPORT_RMB_FIXED || _settings_client.gui.scroll_mode == VSM_MAP_RMB_FIXED) _cursor.fix_at = true;
 

	
 
/**
 
 * Select a new map type.
 
 * @param map_type New map type.
 
 */
 
void SmallMapWindow::SwitchMapType(SmallMapType map_type)
 
{
 
	this->RaiseWidget(this->map_type + WID_SM_CONTOUR);
 
	this->map_type = map_type;
 
	this->LowerWidget(this->map_type + WID_SM_CONTOUR);
 

	
 
	this->SetupWidgetData();
 

	
 
	if (map_type == SMT_LINKSTATS) this->overlay->SetDirty();
 
	if (map_type != SMT_INDUSTRY) this->BreakIndustryChainLink();
 
	this->SetDirty();
 
}
 

	
 
/**
 
 * Get the number of rows in the legend from the number of columns. Those
 
 * are at least min_number_of_fixed_rows and possibly more if there are so
 
 * many cargoes, industry types or companies that they won't fit in the
 
 * available space.
 
 * @param columns Number of columns in the legend.
 
 * @return Number of rows needed for everything to fit in.
 
 */
 
inline uint SmallMapWindow::GetNumberRowsLegend(uint columns) const
 
{
 
	/* Reserve one column for link colours */
 
	uint num_rows_linkstats = CeilDiv(_smallmap_cargo_count, columns - 1);
 
	uint num_rows_others = CeilDiv(std::max(_smallmap_industry_count, _smallmap_company_count), columns);
 
	return std::max({this->min_number_of_fixed_rows, num_rows_linkstats, num_rows_others});
 
}
 
		/* While tile is at (delta.x, delta.y)? */
 
		int sub;
 
		Point pt = this->PixelToTile(delta.x, delta.y, &sub);
 
		this->SetNewScroll(this->scroll_x + pt.x * TILE_SIZE, this->scroll_y + pt.y * TILE_SIZE, sub);
 

	
 
/**
 
 * Select and toggle a legend item. When CTRL is pressed, disable all other
 
 * items in the group defined by begin_legend_item and end_legend_item and
 
 * keep the clicked one enabled even if it was already enabled before. If
 
 * the other items in the group are all disabled already and CTRL is pressed
 
 * enable them instead.
 
 * @param click_pos the index of the item being selected
 
 * @param legend the legend from which we select
 
 * @param end_legend_item index one past the last item in the group to be inverted
 
 * @param begin_legend_item index of the first item in the group to be inverted
 
 */
 
void SmallMapWindow::SelectLegendItem(int click_pos, LegendAndColour *legend, int end_legend_item, int begin_legend_item)
 
{
 
	if (_ctrl_pressed) {
 
		/* Disable all, except the clicked one */
 
		bool changes = false;
 
		for (int i = begin_legend_item; i != end_legend_item; i++) {
 
			bool new_state = (i == click_pos);
 
			if (legend[i].show_on_map != new_state) {
 
				changes = true;
 
				legend[i].show_on_map = new_state;
 
			}
 
		}
 
		if (!changes) {
 
			/* Nothing changed? Then show all (again). */
 
			for (int i = begin_legend_item; i != end_legend_item; i++) {
 
				legend[i].show_on_map = true;
 
			}
 
		}
 
	} else {
 
		legend[click_pos].show_on_map = !legend[click_pos].show_on_map;
 
	}
 

	
 
	if (this->map_type == SMT_INDUSTRY) this->BreakIndustryChainLink();
 
}
 

	
 
/**
 
 * Set the link graph overlay cargo mask from the legend.
 
 */
 
void SmallMapWindow::SetOverlayCargoMask()
 
{
 
	CargoTypes cargo_mask = 0;
 
	for (int i = 0; i != _smallmap_cargo_count; ++i) {
 
		if (_legend_linkstats[i].show_on_map) SetBit(cargo_mask, _legend_linkstats[i].type);
 
	}
 
	this->overlay->SetCargoMask(cargo_mask);
 
}
 

	
 
/**
 
 * Determines the mouse position on the legend.
 
 * @param pt Mouse position.
 
 * @return Legend item under the mouse.
 
 */
 
int SmallMapWindow::GetPositionOnLegend(Point pt)
 
{
 
	const NWidgetBase *wi = this->GetWidget<NWidgetBase>(WID_SM_LEGEND);
 
	uint line = (pt.y - wi->pos_y - WidgetDimensions::scaled.framerect.top) / FONT_HEIGHT_SMALL;
 
	uint columns = this->GetNumberColumnsLegend(wi->current_x);
 
	uint number_of_rows = this->GetNumberRowsLegend(columns);
 
	if (line >= number_of_rows) return -1;
 

	
 
	bool rtl = _current_text_dir == TD_RTL;
 
	int x = pt.x - wi->pos_x;
 
	if (rtl) x = wi->current_x - x;
 
	uint column = (x - WidgetDimensions::scaled.framerect.left) / this->column_width;
 

	
 
	return (column * number_of_rows) + line;
 
}
 

	
 
/* virtual */ void SmallMapWindow::OnMouseOver([[maybe_unused]] Point pt, int widget)
 
{
 
	IndustryType new_highlight = INVALID_INDUSTRYTYPE;
 
	if (widget == WID_SM_LEGEND && this->map_type == SMT_INDUSTRY) {
 
		int industry_pos = GetPositionOnLegend(pt);
 
		if (industry_pos >= 0 && industry_pos < _smallmap_industry_count) {
 
			new_highlight = _legend_from_industries[industry_pos].type;
 
		}
 
	}
 
	if (new_highlight != _smallmap_industry_highlight) {
 
		_smallmap_industry_highlight = new_highlight;
 
		_smallmap_industry_highlight_state = true;
 
		this->SetDirty();
 
	}
 
}
 

	
 
/* virtual */ void SmallMapWindow::OnClick([[maybe_unused]] Point pt, int widget, [[maybe_unused]] int click_count)
 
{
 
	switch (widget) {
 
		case WID_SM_MAP: { // Map window
 
			if (click_count > 0) this->mouse_capture_widget = widget;
 

	
 
			const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
 
			Window *w = GetMainWindow();
 
			int sub;
 
			pt = this->PixelToTile(pt.x - wid->pos_x, pt.y - wid->pos_y, &sub);
 
			ScrollWindowTo(this->scroll_x + pt.x * TILE_SIZE, this->scroll_y + pt.y * TILE_SIZE, -1, w);
 

	
 
			this->SetDirty();
 
			break;
 
		}
 

	
 
		case WID_SM_ZOOM_IN:
 
		case WID_SM_ZOOM_OUT: {
 
			const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
 
			Point zoom_pt = { (int)wid->current_x / 2, (int)wid->current_y / 2};
 
			this->SetZoomLevel((widget == WID_SM_ZOOM_IN) ? ZLC_ZOOM_IN : ZLC_ZOOM_OUT, &zoom_pt);
 
			if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
 
			break;
 
		}
 

	
 
		case WID_SM_CONTOUR:    // Show land contours
 
		case WID_SM_VEHICLES:   // Show vehicles
 
		case WID_SM_INDUSTRIES: // Show industries
 
		case WID_SM_LINKSTATS:  // Show route map
 
		case WID_SM_ROUTES:     // Show transport routes
 
		case WID_SM_VEGETATION: // Show vegetation
 
		case WID_SM_OWNERS:     // Show land owners
 
			this->SwitchMapType((SmallMapType)(widget - WID_SM_CONTOUR));
 
			if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
 
			break;
 

	
 
		case WID_SM_CENTERMAP: // Center the smallmap again
 
			this->SmallMapCenterOnCurrentPos();
 
			this->HandleButtonClick(WID_SM_CENTERMAP);
 
			if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
 
			break;
 

	
 
		case WID_SM_TOGGLETOWNNAME: // Toggle town names
 
			this->show_towns = !this->show_towns;
 
			this->SetWidgetLoweredState(WID_SM_TOGGLETOWNNAME, this->show_towns);
 

	
 
			this->SetDirty();
 
			if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
 
			break;
 

	
 
		case WID_SM_LEGEND: // Legend
 
			if (this->map_type == SMT_INDUSTRY || this->map_type == SMT_LINKSTATS || this->map_type == SMT_OWNER) {
 
				int click_pos = this->GetPositionOnLegend(pt);
 
				if (click_pos < 0) break;
 

	
 
				/* If industry type small map*/
 
				if (this->map_type == SMT_INDUSTRY) {
 
					/* If click on industries label, find right industry type and enable/disable it. */
 
					if (click_pos < _smallmap_industry_count) {
 
						this->SelectLegendItem(click_pos, _legend_from_industries, _smallmap_industry_count);
 
					}
 
				} else if (this->map_type == SMT_LINKSTATS) {
 
					if (click_pos < _smallmap_cargo_count) {
 
						this->SelectLegendItem(click_pos, _legend_linkstats, _smallmap_cargo_count);
 
						this->SetOverlayCargoMask();
 
					}
 
				} else if (this->map_type == SMT_OWNER) {
 
					if (click_pos < _smallmap_company_count) {
 
						this->SelectLegendItem(click_pos, _legend_land_owners, _smallmap_company_count, NUM_NO_COMPANY_ENTRIES);
 
					}
 
				}
 
				this->SetDirty();
 
	void OnMouseOver([[maybe_unused]] Point pt, int widget) override
 
	{
 
		IndustryType new_highlight = INVALID_INDUSTRYTYPE;
 
		if (widget == WID_SM_LEGEND && this->map_type == SMT_INDUSTRY) {
 
			int industry_pos = GetPositionOnLegend(pt);
 
			if (industry_pos >= 0 && industry_pos < _smallmap_industry_count) {
 
				new_highlight = _legend_from_industries[industry_pos].type;
 
			}
 
			break;
 

	
 
		case WID_SM_ENABLE_ALL:
 
		case WID_SM_DISABLE_ALL: {
 
			LegendAndColour *tbl = nullptr;
 
			switch (this->map_type) {
 
				case SMT_INDUSTRY:
 
					tbl = _legend_from_industries;
 
					this->BreakIndustryChainLink();
 
					break;
 
				case SMT_OWNER:
 
					tbl = &(_legend_land_owners[NUM_NO_COMPANY_ENTRIES]);
 
					break;
 
				case SMT_LINKSTATS:
 
					tbl = _legend_linkstats;
 
					break;
 
				default:
 
					NOT_REACHED();
 
			}
 
			for (;!tbl->end && tbl->legend != STR_LINKGRAPH_LEGEND_UNUSED; ++tbl) {
 
				tbl->show_on_map = (widget == WID_SM_ENABLE_ALL);
 
			}
 
			if (this->map_type == SMT_LINKSTATS) this->SetOverlayCargoMask();
 
			this->SetDirty();
 
			break;
 
		}
 

	
 
		case WID_SM_SHOW_HEIGHT: // Enable/disable showing of heightmap.
 
			_smallmap_show_heightmap = !_smallmap_show_heightmap;
 
			this->SetWidgetLoweredState(WID_SM_SHOW_HEIGHT, _smallmap_show_heightmap);
 
		if (new_highlight != _smallmap_industry_highlight) {
 
			_smallmap_industry_highlight = new_highlight;
 
			_smallmap_industry_highlight_state = true;
 
			this->SetDirty();
 
			break;
 
	}
 
}
 

	
 
/**
 
 * Some data on this window has become invalid.
 
 * @param data Information about the changed data.
 
 * - data = 0: Displayed industries at the industry chain window have changed.
 
 * - data = 1: Companies have changed.
 
 * - data = 2: Cheat changing the maximum heightlevel has been used, rebuild our heightlevel-to-colour index
 
 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
 
 */
 
/* virtual */ void SmallMapWindow::OnInvalidateData(int data, bool gui_scope)
 
{
 
	if (!gui_scope) return;
 

	
 
	switch (data) {
 
		case 1:
 
			/* The owner legend has already been rebuilt. */
 
			this->ReInit();
 
			break;
 

	
 
		case 0: {
 
			extern std::bitset<NUM_INDUSTRYTYPES> _displayed_industries;
 
			if (this->map_type != SMT_INDUSTRY) this->SwitchMapType(SMT_INDUSTRY);
 

	
 
			for (int i = 0; i != _smallmap_industry_count; i++) {
 
				_legend_from_industries[i].show_on_map = _displayed_industries.test(_legend_from_industries[i].type);
 
			}
 
			break;
 
		}
 

	
 
		case 2:
 
			this->RebuildColourIndexIfNecessary();
 
			break;
 

	
 
		default: NOT_REACHED();
 
	}
 
	this->SetDirty();
 
}
 

	
 
/* virtual */ bool SmallMapWindow::OnRightClick([[maybe_unused]] Point pt, int widget)
 
{
 
	if (widget != WID_SM_MAP || _scrolling_viewport) return false;
 

	
 
	_scrolling_viewport = true;
 
	return true;
 
}
 

	
 
/* virtual */ void SmallMapWindow::OnMouseWheel(int wheel)
 
{
 
	if (_settings_client.gui.scrollwheel_scrolling != 2) {
 
		const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
 
		int cursor_x = _cursor.pos.x - this->left - wid->pos_x;
 
		int cursor_y = _cursor.pos.y - this->top  - wid->pos_y;
 
		if (IsInsideMM(cursor_x, 0, wid->current_x) && IsInsideMM(cursor_y, 0, wid->current_y)) {
 
			Point pt = {cursor_x, cursor_y};
 
			this->SetZoomLevel((wheel < 0) ? ZLC_ZOOM_IN : ZLC_ZOOM_OUT, &pt);
 
		}
 
	}
 
}
 

	
 
/** Update all the links on the map. */
 
void SmallMapWindow::UpdateLinks()
 
{
 
	if (this->map_type == SMT_LINKSTATS) {
 
		CompanyMask company_mask = this->GetOverlayCompanyMask();
 
		if (this->overlay->GetCompanyMask() != company_mask) {
 
			this->overlay->SetCompanyMask(company_mask);
 
		} else {
 
			this->overlay->SetDirty();
 
		}
 
	}
 
}
 

	
 
/** Blink the industries (if hover over an industry). */
 
void SmallMapWindow::Blink()
 
{
 
	if (_smallmap_industry_highlight == INVALID_INDUSTRYTYPE) return;
 

	
 
	_smallmap_industry_highlight_state = !_smallmap_industry_highlight_state;
 

	
 
	this->UpdateLinks();
 
	this->SetDirty();
 
}
 

	
 
/** Force a full refresh of the map. */
 
void SmallMapWindow::ForceRefresh()
 
{
 
	if (_smallmap_industry_highlight != INVALID_INDUSTRYTYPE) return;
 

	
 
	this->UpdateLinks();
 
	this->SetDirty();
 
}
 

	
 
/**
 
 * Set new #scroll_x, #scroll_y, and #subscroll values after limiting them such that the center
 
 * of the smallmap always contains a part of the map.
 
 * @param sx  Proposed new #scroll_x
 
 * @param sy  Proposed new #scroll_y
 
 * @param sub Proposed new #subscroll
 
 */
 
void SmallMapWindow::SetNewScroll(int sx, int sy, int sub)
 
{
 
	const NWidgetBase *wi = this->GetWidget<NWidgetBase>(WID_SM_MAP);
 
	Point hv = InverseRemapCoords(wi->current_x * ZOOM_LVL_BASE * TILE_SIZE / 2, wi->current_y * ZOOM_LVL_BASE * TILE_SIZE / 2);
 
	hv.x *= this->zoom;
 
	hv.y *= this->zoom;
 

	
 
	if (sx < -hv.x) {
 
		sx = -hv.x;
 
		sub = 0;
 
	}
 
	if (sx > (int)(Map::MaxX() * TILE_SIZE) - hv.x) {
 
		sx = Map::MaxX() * TILE_SIZE - hv.x;
 
		sub = 0;
 
	}
 
	if (sy < -hv.y) {
 
		sy = -hv.y;
 
		sub = 0;
 
	}
 
	if (sy > (int)(Map::MaxY() * TILE_SIZE) - hv.y) {
 
		sy = Map::MaxY() * TILE_SIZE - hv.y;
 
		sub = 0;
 
	}
 

	
 
	this->scroll_x = sx;
 
	this->scroll_y = sy;
 
	this->subscroll = sub;
 
	if (this->map_type == SMT_LINKSTATS) this->overlay->SetDirty();
 
}
 

	
 
/* virtual */ void SmallMapWindow::OnScroll(Point delta)
 
{
 
	if (_settings_client.gui.scroll_mode == VSM_VIEWPORT_RMB_FIXED || _settings_client.gui.scroll_mode == VSM_MAP_RMB_FIXED) _cursor.fix_at = true;
 

	
 
	/* While tile is at (delta.x, delta.y)? */
 
	int sub;
 
	Point pt = this->PixelToTile(delta.x, delta.y, &sub);
 
	this->SetNewScroll(this->scroll_x + pt.x * TILE_SIZE, this->scroll_y + pt.y * TILE_SIZE, sub);
 

	
 
	this->SetDirty();
 
}
 

	
 
/**
 
 * Center the small map on the current center of the viewport.
 
 */
 
void SmallMapWindow::SmallMapCenterOnCurrentPos()
 
{
 
	const Viewport *vp = GetMainWindow()->viewport;
 
	Point viewport_center = InverseRemapCoords2(vp->virtual_left + vp->virtual_width / 2, vp->virtual_top + vp->virtual_height / 2);
 

	
 
	int sub;
 
	const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
 
	Point sxy = this->ComputeScroll(viewport_center.x / (int)TILE_SIZE, viewport_center.y / (int)TILE_SIZE,
 
			std::max(0, (int)wid->current_x / 2 - 2), wid->current_y / 2, &sub);
 
	this->SetNewScroll(sxy.x, sxy.y, sub);
 
	this->SetDirty();
 
}
 

	
 
/**
 
 * Get the center of the given station as point on the screen in the smallmap window.
 
 * @param st Station to find in the smallmap.
 
 * @return Point with coordinates of the station.
 
 */
 
Point SmallMapWindow::GetStationMiddle(const Station *st) const
 
{
 
	int x = CenterBounds(st->rect.left, st->rect.right, 0);
 
	int y = CenterBounds(st->rect.top, st->rect.bottom, 0);
 
	Point ret = this->RemapTile(x, y);
 

	
 
	/* Same magic 3 as in DrawVehicles; that's where I got it from.
 
	 * No idea what it is, but without it the result looks bad.
 
	 */
 
	ret.x -= 3 + this->subscroll;
 
	return ret;
 
}
 
};
 

	
 
SmallMapWindow::SmallMapType SmallMapWindow::map_type = SMT_CONTOUR;
 
bool SmallMapWindow::show_towns = true;
src/smallmap_gui.h
Show inline comments
 
@@ -37,181 +37,6 @@ enum class IncludeHeightmap {
 

	
 
uint32_t GetSmallMapOwnerPixels(TileIndex tile, TileType t, IncludeHeightmap include_heightmap);
 

	
 
/** Structure for holding relevant data for legends in small map */
 
struct LegendAndColour {
 
	uint8_t colour;              ///< Colour of the item on the map.
 
	StringID legend;           ///< String corresponding to the coloured item.
 
	IndustryType type;         ///< Type of industry. Only valid for industry entries.
 
	uint8_t height;              ///< Height in tiles. Only valid for height legend entries.
 
	CompanyID company;         ///< Company to display. Only valid for company entries of the owner legend.
 
	bool show_on_map;          ///< For filtering industries, if \c true, industry is shown on the map in colour.
 
	bool end;                  ///< This is the end of the list.
 
	bool col_break;            ///< Perform a column break and go further at the next column.
 
};
 

	
 
/** Class managing the smallmap window. */
 
class SmallMapWindow : public Window {
 
protected:
 
	/** Types of legends in the #WID_SM_LEGEND widget. */
 
	enum SmallMapType {
 
		SMT_CONTOUR,
 
		SMT_VEHICLES,
 
		SMT_INDUSTRY,
 
		SMT_LINKSTATS,
 
		SMT_ROUTES,
 
		SMT_VEGETATION,
 
		SMT_OWNER,
 
	};
 

	
 
	/** Available kinds of zoomlevel changes. */
 
	enum ZoomLevelChange {
 
		ZLC_INITIALIZE, ///< Initialize zoom level.
 
		ZLC_ZOOM_OUT,   ///< Zoom out.
 
		ZLC_ZOOM_IN,    ///< Zoom in.
 
	};
 

	
 
	static SmallMapType map_type; ///< Currently displayed legends.
 
	static bool show_towns;       ///< Display town names in the smallmap.
 
	static int map_height_limit;  ///< Currently used/cached map height limit.
 

	
 
	static const uint INDUSTRY_MIN_NUMBER_OF_COLUMNS = 2; ///< Minimal number of columns in the #WID_SM_LEGEND widget for the #SMT_INDUSTRY legend.
 

	
 
	uint min_number_of_columns;    ///< Minimal number of columns in legends.
 
	uint min_number_of_fixed_rows; ///< Minimal number of rows in the legends for the fixed layouts only (all except #SMT_INDUSTRY).
 
	uint column_width;             ///< Width of a column in the #WID_SM_LEGEND widget.
 
	uint legend_width;             ///< Width of legend 'blob'.
 

	
 
	int32_t scroll_x;  ///< Horizontal world coordinate of the base tile left of the top-left corner of the smallmap display.
 
	int32_t scroll_y;  ///< Vertical world coordinate of the base tile left of the top-left corner of the smallmap display.
 
	int32_t subscroll; ///< Number of pixels (0..3) between the right end of the base tile and the pixel at the top-left corner of the smallmap display.
 
	int zoom;        ///< Zoom level. Bigger number means more zoom-out (further away).
 

	
 
	LinkGraphOverlay *overlay;
 

	
 
	static void BreakIndustryChainLink();
 
	Point SmallmapRemapCoords(int x, int y) const;
 

	
 
	/**
 
	 * Draws vertical part of map indicator
 
	 * @param x X coord of left/right border of main viewport
 
	 * @param y Y coord of top border of main viewport
 
	 * @param y2 Y coord of bottom border of main viewport
 
	 */
 
	static inline void DrawVertMapIndicator(int x, int y, int y2)
 
	{
 
		GfxFillRect(x, y,      x, y + 3, PC_VERY_LIGHT_YELLOW);
 
		GfxFillRect(x, y2 - 3, x, y2,    PC_VERY_LIGHT_YELLOW);
 
	}
 

	
 
	/**
 
	 * Draws horizontal part of map indicator
 
	 * @param x X coord of left border of main viewport
 
	 * @param x2 X coord of right border of main viewport
 
	 * @param y Y coord of top/bottom border of main viewport
 
	 */
 
	static inline void DrawHorizMapIndicator(int x, int x2, int y)
 
	{
 
		GfxFillRect(x,      y, x + 3, y, PC_VERY_LIGHT_YELLOW);
 
		GfxFillRect(x2 - 3, y, x2,    y, PC_VERY_LIGHT_YELLOW);
 
	}
 

	
 
	/**
 
	 * Compute minimal required width of the legends.
 
	 * @return Minimally needed width for displaying the smallmap legends in pixels.
 
	 */
 
	inline uint GetMinLegendWidth() const
 
	{
 
		return WidgetDimensions::scaled.framerect.left + this->min_number_of_columns * this->column_width;
 
	}
 

	
 
	/**
 
	 * Return number of columns that can be displayed in \a width pixels.
 
	 * @return Number of columns to display.
 
	 */
 
	inline uint GetNumberColumnsLegend(uint width) const
 
	{
 
		return width / this->column_width;
 
	}
 

	
 
	/**
 
	 * Compute height given a number of columns.
 
	 * @param num_columns Number of columns.
 
	 * @return Needed height for displaying the smallmap legends in pixels.
 
	 */
 
	inline uint GetLegendHeight(uint num_columns) const
 
	{
 
		return WidgetDimensions::scaled.framerect.Vertical() +
 
				this->GetNumberRowsLegend(num_columns) * FONT_HEIGHT_SMALL;
 
	}
 

	
 
	/**
 
	 * Get a bitmask for company links to be displayed. Usually this will be
 
	 * the _local_company. Spectators get to see all companies' links.
 
	 * @return Company mask.
 
	 */
 
	inline CompanyMask GetOverlayCompanyMask() const
 
	{
 
		return Company::IsValidID(_local_company) ? 1U << _local_company : MAX_UVALUE(CompanyMask);
 
	}
 

	
 
	/** Blink the industries (if selected) on a regular interval. */
 
	IntervalTimer<TimerWindow> blink_interval = {std::chrono::milliseconds(450), [this](auto) {
 
		Blink();
 
	}};
 

	
 
	/** Update the whole map on a regular interval. */
 
	IntervalTimer<TimerWindow> refresh_interval = {std::chrono::milliseconds(930), [this](auto) {
 
		ForceRefresh();
 
	}};
 

	
 
	void RebuildColourIndexIfNecessary();
 
	uint GetNumberRowsLegend(uint columns) const;
 
	void SelectLegendItem(int click_pos, LegendAndColour *legend, int end_legend_item, int begin_legend_item = 0);
 
	void SwitchMapType(SmallMapType map_type);
 
	void SetNewScroll(int sx, int sy, int sub);
 

	
 
	void DrawMapIndicators() const;
 
	void DrawSmallMapColumn(void *dst, uint xc, uint yc, int pitch, int reps, int start_pos, int end_pos, Blitter *blitter) const;
 
	void DrawVehicles(const DrawPixelInfo *dpi, Blitter *blitter) const;
 
	void DrawTowns(const DrawPixelInfo *dpi) const;
 
	void DrawSmallMap(DrawPixelInfo *dpi) const;
 

	
 
	Point RemapTile(int tile_x, int tile_y) const;
 
	Point PixelToTile(int px, int py, int *sub, bool add_sub = true) const;
 
	Point ComputeScroll(int tx, int ty, int x, int y, int *sub);
 
	void SetZoomLevel(ZoomLevelChange change, const Point *zoom_pt);
 
	void SetOverlayCargoMask();
 
	void SetupWidgetData();
 
	uint32_t GetTileColours(const TileArea &ta) const;
 

	
 
	int GetPositionOnLegend(Point pt);
 

	
 
	void UpdateLinks();
 
	void Blink();
 
	void ForceRefresh();
 

	
 
public:
 
	friend class NWidgetSmallmapDisplay;
 

	
 
	SmallMapWindow(WindowDesc *desc, int window_number);
 
	virtual ~SmallMapWindow();
 

	
 
	void SmallMapCenterOnCurrentPos();
 
	Point GetStationMiddle(const Station *st) const;
 

	
 
	void Close([[maybe_unused]] int data = 0) override;
 
	void SetStringParameters(int widget) const override;
 
	void OnInit() override;
 
	void OnPaint() override;
 
	void DrawWidget(const Rect &r, int widget) const override;
 
	void OnClick([[maybe_unused]] Point pt, int widget, [[maybe_unused]] int click_count) override;
 
	void OnInvalidateData(int data = 0, bool gui_scope = true) override;
 
	bool OnRightClick(Point pt, int widget) override;
 
	void OnMouseWheel(int wheel) override;
 
	void OnScroll(Point delta) override;
 
	void OnMouseOver([[maybe_unused]] Point pt, int widget) override;
 
};
 

	
 
Point GetSmallMapStationMiddle(const Window *w, const Station *st);
 

	
 
#endif /* SMALLMAP_GUI_H */
0 comments (0 inline, 0 general)