Changeset - r20060:09b552cfa41d
[Not reviewed]
master
0 2 0
rubidium - 11 years ago 2013-02-17 20:38:35
rubidium@openttd.org
(svn r25018) -Codechange: Move SmallMapWindow declaration to header (fonsinchen)
2 files changed with 171 insertions and 127 deletions:
0 comments (0 inline, 0 general)
src/smallmap_gui.cpp
Show inline comments
 
@@ -5,38 +5,35 @@
 
 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
 
 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 
 */
 

	
 
/** @file smallmap_gui.cpp GUI that shows a small map of the world with metadata like owner or height. */
 

	
 
#include "stdafx.h"
 
#include "clear_map.h"
 
#include "industry.h"
 
#include "station_map.h"
 
#include "landscape.h"
 
#include "window_gui.h"
 
#include "tree_map.h"
 
#include "viewport_func.h"
 
#include "town.h"
 
#include "blitter/factory.hpp"
 
#include "tunnelbridge_map.h"
 
#include "strings_func.h"
 
#include "core/endian_func.hpp"
 
#include "vehicle_base.h"
 
#include "sound_func.h"
 
#include "window_func.h"
 
#include "company_base.h"
 

	
 
#include "widgets/smallmap_widget.h"
 
#include "smallmap_gui.h"
 

	
 
#include "table/strings.h"
 

	
 
static int _smallmap_industry_count; ///< Number of used industries
 
static int _smallmap_company_count;  ///< Number of entries in the owner legend.
 

	
 
static const int NUM_NO_COMPANY_ENTRIES = 4; ///< Number of entries in the owner legend that are not companies.
 

	
 
static const uint8 PC_ROUGH_LAND      = 0x52; ///< Dark green palette colour for rough land.
 
static const uint8 PC_GRASS_LAND      = 0x54; ///< Dark green palette colour for grass land.
 
static const uint8 PC_BARE_LAND       = 0x37; ///< Brown palette colour for bare land.
 
static const uint8 PC_FIELDS          = 0x25; ///< Light brown palette colour for fields.
 
@@ -55,36 +52,24 @@ static const uint8 PC_WATER           = 
 
/** Macro used for forcing a rebuild of the owner legend the first time it is used. */
 
#define MOEND() {0, 0, INVALID_INDUSTRYTYPE, 0, OWNER_NONE, true, true, false}
 

	
 
/** Macro for end of list marker in arrays of LegendAndColour */
 
#define MKEND() {0, STR_NULL, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, true, false}
 

	
 
/**
 
 * Macro for break marker in arrays of LegendAndColour.
 
 * It will have valid data, though
 
 */
 
#define MS(a, b) {a, b, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, false, true}
 

	
 
/** Structure for holding relevant data for legends in small map */
 
struct LegendAndColour {
 
	uint8 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 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.
 
};
 

	
 
/** Legend text giving the colours to look for on the minimap */
 
static LegendAndColour _legend_land_contours[] = {
 
	/* The colours for the following values are set at BuildLandLegend() based on each colour scheme. */
 
	MC(0),
 
	MC(4),
 
	MC(8),
 
	MC(12),
 
	MC(14),
 

	
 
	MS(PC_BLACK,           STR_SMALLMAP_LEGENDA_ROADS),
 
	MK(PC_GREY,            STR_SMALLMAP_LEGENDA_RAILROADS),
 
	MK(PC_LIGHT_BLUE,      STR_SMALLMAP_LEGENDA_STATIONS_AIRPORTS_DOCKS),
 
@@ -560,99 +545,63 @@ static inline uint32 GetSmallMapOwnerPix
 
		return MKCOLOUR_XXXX(PC_DARK_RED);
 
	}
 

	
 
	return MKCOLOUR_XXXX(_legend_land_owners[_company_to_list_pos[o]].colour);
 
}
 

	
 
/** Vehicle colours in #SMT_VEHICLES mode. Indexed by #VehicleTypeByte. */
 
static const byte _vehicle_type_colours[6] = {
 
	PC_RED, PC_YELLOW, PC_LIGHT_BLUE, PC_WHITE, PC_BLACK, PC_RED
 
};
 

	
 

	
 
/** Class managing the smallmap window. */
 
class SmallMapWindow : public Window {
 
	/** Types of legends in the #WID_SM_LEGEND widget. */
 
	enum SmallMapType {
 
		SMT_CONTOUR,
 
		SMT_VEHICLES,
 
		SMT_INDUSTRY,
 
		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 const uint LEGEND_BLOB_WIDTH = 8;              ///< Width of the coloured blob in front of a line text in the #WID_SM_LEGEND widget.
 
	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_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.
 

	
 
	int32 scroll_x;  ///< Horizontal world coordinate of the base tile left of the top-left corner of the smallmap display.
 
	int32 scroll_y;  ///< Vertical world coordinate of the base tile left of the top-left corner of the smallmap display.
 
	int32 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).
 

	
 
	static const uint FORCE_REFRESH_PERIOD = 0x1F; ///< map is redrawn after that many ticks
 
	static const uint BLINK_PERIOD         = 0x0F; ///< highlight blinking interval
 
	uint8 refresh; ///< refresh counter, zeroed every FORCE_REFRESH_PERIOD ticks
 

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

	
 
	/**
 
	 * 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 RemapTile(int tile_x, int tile_y) const
 
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;
 

	
 
		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 sub[out] 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 PixelToTile(int px, int py, int *sub, bool add_sub = true) const
 
inline Point SmallMapWindow::PixelToTile(int px, int py, int *sub, bool add_sub) 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;
 

	
 
		if (py & 1) { // Odd number of rows, handle the 2 pixel shift.
 
			if (px < 2) {
 
				pt.x += this->zoom;
 
				px += 2;
 
@@ -666,25 +615,25 @@ class SmallMapWindow : public Window {
 
		return pt;
 
	}
 

	
 
	/**
 
	 * 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 sub [out] Value of #subscroll needed.
 
	 * @return #scroll_x, #scroll_y values.
 
	 */
 
	Point ComputeScroll(int tx, int ty, int x, int y, int *sub)
 
Point SmallMapWindow::ComputeScroll(int tx, int ty, int x, int y, int *sub)
 
	{
 
		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;
 
@@ -694,25 +643,25 @@ class SmallMapWindow : public Window {
 
			scroll.x = (tx + 2 * this->zoom) * TILE_SIZE;
 
			scroll.y = (ty - 2 * this->zoom) * TILE_SIZE;
 
		}
 
		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)
 
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;
 

	
 
		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;
 
				break;
 
@@ -740,25 +689,25 @@ class SmallMapWindow : public Window {
 
			}
 
			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();
 
		}
 
	}
 

	
 
	/**
 
	 * 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 GetTileColours(const TileArea &ta) const
 
inline uint32 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.
 

	
 
		TILE_AREA_LOOP(ti, ta) {
 
			TileType ttype = GetEffectiveTileType(ti);
 
			if (_tiletype_importance[ttype] > importance) {
 
				importance = _tiletype_importance[ttype];
 
				tile = ti;
 
				et = ttype;
 
			}
 
@@ -791,25 +740,25 @@ class SmallMapWindow : public Window {
 
	 * 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 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 >= MapMaxX() || yc >= MapMaxY()) 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;
 

	
 
@@ -831,25 +780,25 @@ class SmallMapWindow : public Window {
 
				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 DrawVehicles(const DrawPixelInfo *dpi, Blitter *blitter) const
 
void SmallMapWindow::DrawVehicles(const DrawPixelInfo *dpi, Blitter *blitter) const
 
	{
 
		const Vehicle *v;
 
		FOR_ALL_VEHICLES(v) {
 
			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 / TILE_SIZE, v->y_pos / TILE_SIZE);
 

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

	
 
@@ -870,73 +819,49 @@ class SmallMapWindow : public Window {
 
			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
 
void SmallMapWindow::DrawTowns(const DrawPixelInfo *dpi) const
 
	{
 
		const Town *t;
 
		FOR_ALL_TOWNS(t) {
 
			/* 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);
 
			}
 
		}
 
	}
 

	
 
	/**
 
	 * 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);
 
	}
 

	
 
	/**
 
	 * Adds map indicators to the smallmap.
 
	 */
 
	void DrawMapIndicators() const
 
void SmallMapWindow::DrawMapIndicators() const
 
	{
 
		/* Find main viewport. */
 
		const ViewPort *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
 

	
 
		Point tile = InverseRemapCoords(vp->virtual_left, vp->virtual_top);
 
		Point tl = this->RemapTile(tile.x >> 4, tile.y >> 4);
 
		tl.x -= this->subscroll;
 

	
 
		tile = InverseRemapCoords(vp->virtual_left + vp->virtual_width, vp->virtual_top + vp->virtual_height);
 
		Point br = this->RemapTile(tile.x >> 4, tile.y >> 4);
 
		br.x -= this->subscroll;
 

	
 
@@ -949,25 +874,25 @@ class SmallMapWindow : public Window {
 

	
 
	/**
 
	 * 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
 
void SmallMapWindow::DrawSmallMap(DrawPixelInfo *dpi) const
 
	{
 
		Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
 
		DrawPixelInfo *old_dpi;
 

	
 
		old_dpi = _cur_dpi;
 
		_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;
 
@@ -1010,25 +935,25 @@ class SmallMapWindow : public Window {
 
		/* Draw town names */
 
		if (this->show_towns) this->DrawTowns(dpi);
 

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

	
 
		_cur_dpi = old_dpi;
 
	}
 

	
 
	/**
 
	 * Function to set up widgets depending on the information being shown on the smallmap.
 
	 */
 
	void SetupWidgetData()
 
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;
 
@@ -1045,83 +970,62 @@ class SmallMapWindow : public Window {
 
				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);
 
	}
 

	
 
public:
 
	uint min_number_of_columns;    ///< Minimal number of columns in legends.
 

	
 
	SmallMapWindow(const WindowDesc *desc, int window_number) : Window(), refresh(FORCE_REFRESH_PERIOD)
 
SmallMapWindow::SmallMapWindow(const WindowDesc *desc, int window_number) : Window(), refresh(FORCE_REFRESH_PERIOD)
 
	{
 
		_smallmap_industry_highlight = INVALID_INDUSTRYTYPE;
 
		this->InitNested(desc, window_number);
 
		this->LowerWidget(this->map_type + WID_SM_CONTOUR);
 

	
 
		BuildLandLegend();
 
		this->SetWidgetLoweredState(WID_SM_SHOW_HEIGHT, _smallmap_show_heightmap);
 

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

	
 
		this->SetupWidgetData();
 

	
 
		this->SetZoomLevel(ZLC_INITIALIZE, NULL);
 
		this->SmallMapCenterOnCurrentPos();
 
	}
 

	
 
	/**
 
	 * Compute minimal required width of the legends.
 
	 * @return Minimally needed width for displaying the smallmap legends in pixels.
 
	 */
 
	inline uint GetMinLegendWidth() const
 
	{
 
		return WD_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.
 
	 */
 
	uint GetLegendHeight(uint num_columns) const
 
uint SmallMapWindow::GetLegendHeight(uint num_columns) const
 
	{
 
		uint num_rows = max(this->min_number_of_fixed_rows, CeilDiv(max(_smallmap_company_count, _smallmap_industry_count), num_columns));
 
		return WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + num_rows * FONT_HEIGHT_SMALL;
 
	}
 

	
 
	virtual void SetStringParameters(int widget) const
 
/* 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 OnInit()
 
/* virtual */ void SmallMapWindow::SmallMapWindow::OnInit()
 
	{
 
		uint min_width = 0;
 
		this->min_number_of_columns = INDUSTRY_MIN_NUMBER_OF_COLUMNS;
 
		this->min_number_of_fixed_rows = 0;
 
		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);
 
@@ -1150,41 +1054,41 @@ public:
 
					str = tbl->legend;
 
				}
 
				min_width = max(GetStringBoundingBox(str).width, min_width);
 
			}
 
			this->min_number_of_fixed_rows = max(this->min_number_of_fixed_rows, height);
 
			this->min_number_of_columns = max(this->min_number_of_columns, num_columns);
 
		}
 

	
 
		/* The width of a column is the minimum width of all texts + the size of the blob + some spacing */
 
		this->column_width = min_width + LEGEND_BLOB_WIDTH + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
 
	}
 

	
 
	virtual void OnPaint()
 
/* 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;
 
				}
 
			}
 
		}
 

	
 
		this->DrawWidgets();
 
	}
 

	
 
	virtual void DrawWidget(const Rect &r, int widget) const
 
/* virtual */ void SmallMapWindow::DrawWidget(const Rect &r, int widget) const
 
	{
 
		switch (widget) {
 
			case WID_SM_MAP: {
 
				DrawPixelInfo new_dpi;
 
				if (!FillDrawPixelInfo(&new_dpi, r.left + 1, r.top + 1, r.right - r.left - 1, r.bottom - r.top - 1)) return;
 
				this->DrawSmallMap(&new_dpi);
 
				break;
 
			}
 

	
 
			case WID_SM_LEGEND: {
 
				uint columns = this->GetNumberColumnsLegend(r.right - r.left + 1);
 
				uint number_of_rows = max((this->map_type == SMT_INDUSTRY || this->map_type == SMT_OWNER) ? CeilDiv(max(_smallmap_company_count, _smallmap_industry_count), columns) : 0, this->min_number_of_fixed_rows);
 
@@ -1247,74 +1151,74 @@ public:
 
					GfxFillRect(x + blob_left + 1, y + 2, x + blob_right - 1, y + row_height - 2, legend_colour); // Legend colour
 

	
 
					y += row_height;
 
				}
 
			}
 
		}
 
	}
 

	
 
	/**
 
	 * Select a new map type.
 
	 * @param map_type New map type.
 
	 */
 
	void SwitchMapType(SmallMapType 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();
 

	
 
		this->SetDirty();
 
	}
 

	
 
	/**
 
	 * Determines the mouse position on the legend.
 
	 * @param pt Mouse position.
 
	 * @return Legend item under the mouse.
 
	 */
 
	int GetPositionOnLegend(Point pt)
 
int SmallMapWindow::GetPositionOnLegend(Point pt)
 
	{
 
		const NWidgetBase *wi = this->GetWidget<NWidgetBase>(WID_SM_LEGEND);
 
		uint line = (pt.y - wi->pos_y - WD_FRAMERECT_TOP) / FONT_HEIGHT_SMALL;
 
		uint columns = this->GetNumberColumnsLegend(wi->current_x);
 
		uint number_of_rows = max(CeilDiv(max(_smallmap_company_count, _smallmap_industry_count), columns), this->min_number_of_fixed_rows);
 
		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 - WD_FRAMERECT_LEFT) / this->column_width;
 

	
 
		return (column * number_of_rows) + line;
 
	}
 

	
 
	virtual void OnMouseOver(Point pt, int widget)
 
/* virtual */ void SmallMapWindow::OnMouseOver(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;
 
			this->refresh = _smallmap_industry_highlight != INVALID_INDUSTRYTYPE ? BLINK_PERIOD : FORCE_REFRESH_PERIOD;
 
			_smallmap_industry_highlight_state = true;
 
			this->SetDirty();
 
		}
 
	}
 

	
 
	virtual void OnClick(Point pt, int widget, int click_count)
 
/* virtual */ void SmallMapWindow::OnClick(Point pt, int widget, int click_count)
 
	{
 
		/* User clicked something, notify the industry chain window to stop sending newly selected industries. */
 
		InvalidateWindowClassesData(WC_INDUSTRY_CARGOES, NUM_INDUSTRYTYPES);
 

	
 
		switch (widget) {
 
			case WID_SM_MAP: { // Map window
 
				/*
 
				 * XXX: scrolling with the left mouse button is done by subsequently
 
				 * clicking with the left mouse button; clicking once centers the
 
				 * large map at the selected point. So by unclicking the left mouse
 
				 * button here, it gets reclicked during the next inputloop, which
 
				 * would make it look like the mouse is being dragged, while it is
 
@@ -1459,88 +1363,88 @@ public:
 
				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.
 
	 * @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 OnInvalidateData(int data = 0, bool gui_scope = true)
 
/* 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 uint64 _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 = HasBit(_displayed_industries, _legend_from_industries[i].type);
 
				}
 
				break;
 
			}
 

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

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

	
 
		_scrolling_viewport = true;
 
		return true;
 
	}
 

	
 
	virtual void OnMouseWheel(int wheel)
 
/* virtual */ void SmallMapWindow::OnMouseWheel(int wheel)
 
	{
 
		if (_settings_client.gui.scrollwheel_scrolling == 0) {
 
			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);
 
			}
 
		}
 
	}
 

	
 
	virtual void OnTick()
 
/* virtual */ void SmallMapWindow::OnTick()
 
	{
 
		/* Update the window every now and then */
 
		if (--this->refresh != 0) return;
 

	
 
		_smallmap_industry_highlight_state = !_smallmap_industry_highlight_state;
 

	
 
		this->refresh = _smallmap_industry_highlight != INVALID_INDUSTRYTYPE ? BLINK_PERIOD : FORCE_REFRESH_PERIOD;
 
		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 SetNewScroll(int sx, int sy, int sub)
 
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)(MapMaxX() * TILE_SIZE) - hv.x) {
 
			sx = MapMaxX() * TILE_SIZE - hv.x;
 
@@ -1551,48 +1455,47 @@ public:
 
			sub = 0;
 
		}
 
		if (sy > (int)(MapMaxY() * TILE_SIZE) - hv.y) {
 
			sy = MapMaxY() * TILE_SIZE - hv.y;
 
			sub = 0;
 
		}
 

	
 
		this->scroll_x = sx;
 
		this->scroll_y = sy;
 
		this->subscroll = sub;
 
	}
 

	
 
	virtual void OnScroll(Point delta)
 
/* virtual */ void SmallMapWindow::OnScroll(Point delta)
 
	{
 
		_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();
 
	}
 

	
 
	void SmallMapCenterOnCurrentPos()
 
void SmallMapWindow::SmallMapCenterOnCurrentPos()
 
	{
 
		const ViewPort *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
 
		Point pt = InverseRemapCoords(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(pt.x / TILE_SIZE, pt.y / TILE_SIZE, max(0, (int)wid->current_x / 2 - 2), wid->current_y / 2, &sub);
 
		this->SetNewScroll(sxy.x, sxy.y, sub);
 
		this->SetDirty();
 
	}
 
};
 

	
 
SmallMapWindow::SmallMapType SmallMapWindow::map_type = SMT_CONTOUR;
 
bool SmallMapWindow::show_towns = true;
 

	
 
/**
 
 * Custom container class for displaying smallmap with a vertically resizing legend panel.
 
 * The legend panel has a smallest height that depends on its width. Standard containers cannot handle this case.
 
 *
 
 * @note The container assumes it has two children, the first is the display, the second is the bar with legends and selection image buttons.
 
 *       Both children should be both horizontally and vertically resizable and horizontally fillable.
 
 *       The bar should have a minimal size with a zero-size legends display. Child padding is not supported.
 
 */
src/smallmap_gui.h
Show inline comments
 
@@ -3,18 +3,159 @@
 
/*
 
 * This file is part of OpenTTD.
 
 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
 
 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 
 */
 

	
 
/** @file smallmap_gui.h Smallmap GUI functions. */
 

	
 
#ifndef SMALLMAP_GUI_H
 
#define SMALLMAP_GUI_H
 

	
 
#include "industry_type.h"
 
#include "window_gui.h"
 
#include "strings_func.h"
 
#include "blitter/factory.hpp"
 
#include "widgets/smallmap_widget.h"
 

	
 
void BuildIndustriesLegend();
 
void ShowSmallMap();
 
void BuildLandLegend();
 
void BuildOwnerLegend();
 

	
 
/** Structure for holding relevant data for legends in small map */
 
struct LegendAndColour {
 
	uint8 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 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_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 const uint LEGEND_BLOB_WIDTH = 8;              ///< Width of the coloured blob in front of a line text in the #WID_SM_LEGEND widget.
 
	static const uint INDUSTRY_MIN_NUMBER_OF_COLUMNS = 2; ///< Minimal number of columns in the #WID_SM_LEGEND widget for the #SMT_INDUSTRY legend.
 
	static const uint FORCE_REFRESH_PERIOD = 0x1F; ///< map is redrawn after that many ticks
 
	static const uint BLINK_PERIOD         = 0x0F; ///< highlight blinking interval
 

	
 
	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.
 

	
 
	int32 scroll_x;  ///< Horizontal world coordinate of the base tile left of the top-left corner of the smallmap display.
 
	int32 scroll_y;  ///< Vertical world coordinate of the base tile left of the top-left corner of the smallmap display.
 
	int32 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).
 

	
 
	uint8 refresh;   ///< Refresh counter, zeroed every FORCE_REFRESH_PERIOD ticks.
 

	
 
	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);
 
	}
 

	
 
	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 SetupWidgetData();
 
	uint32 GetTileColours(const TileArea &ta) const;
 

	
 
	int GetPositionOnLegend(Point pt);
 

	
 

	
 
public:
 

	
 
	uint min_number_of_columns;    ///< Minimal number of columns in legends.
 

	
 
	SmallMapWindow(const WindowDesc *desc, int window_number);
 

	
 
	/**
 
	 * Compute minimal required width of the legends.
 
	 * @return Minimally needed width for displaying the smallmap legends in pixels.
 
	 */
 
	inline uint GetMinLegendWidth() const
 
	{
 
		return WD_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;
 
	}
 

	
 
	uint GetLegendHeight(uint num_columns) const;
 

	
 
	void SwitchMapType(SmallMapType map_type);
 
	void SetNewScroll(int sx, int sy, int sub);
 
	void SmallMapCenterOnCurrentPos();
 

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

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