1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
8 /** @file smallmap_gui.h Smallmap GUI functions. */
9 
10 #ifndef SMALLMAP_GUI_H
11 #define SMALLMAP_GUI_H
12 
13 #include "industry_type.h"
14 #include "company_base.h"
15 #include "window_gui.h"
16 #include "strings_func.h"
17 #include "blitter/factory.hpp"
18 #include "linkgraph/linkgraph_gui.h"
19 #include "widgets/smallmap_widget.h"
20 #include "guitimer_func.h"
21 
22 /* set up the cargos to be displayed in the smallmap's route legend */
23 void BuildLinkStatsLegend();
24 
25 void BuildIndustriesLegend();
26 void ShowSmallMap();
27 void BuildLandLegend();
28 void BuildOwnerLegend();
29 
30 /** Structure for holding relevant data for legends in small map */
31 struct LegendAndColour {
32 	uint8 colour;              ///< Colour of the item on the map.
33 	StringID legend;           ///< String corresponding to the coloured item.
34 	IndustryType type;         ///< Type of industry. Only valid for industry entries.
35 	uint8 height;              ///< Height in tiles. Only valid for height legend entries.
36 	CompanyID company;         ///< Company to display. Only valid for company entries of the owner legend.
37 	bool show_on_map;          ///< For filtering industries, if \c true, industry is shown on the map in colour.
38 	bool end;                  ///< This is the end of the list.
39 	bool col_break;            ///< Perform a column break and go further at the next column.
40 };
41 
42 /** Class managing the smallmap window. */
43 class SmallMapWindow : public Window {
44 protected:
45 	/** Types of legends in the #WID_SM_LEGEND widget. */
46 	enum SmallMapType {
47 		SMT_CONTOUR,
48 		SMT_VEHICLES,
49 		SMT_INDUSTRY,
50 		SMT_LINKSTATS,
51 		SMT_ROUTES,
52 		SMT_VEGETATION,
53 		SMT_OWNER,
54 	};
55 
56 	/** Available kinds of zoomlevel changes. */
57 	enum ZoomLevelChange {
58 		ZLC_INITIALIZE, ///< Initialize zoom level.
59 		ZLC_ZOOM_OUT,   ///< Zoom out.
60 		ZLC_ZOOM_IN,    ///< Zoom in.
61 	};
62 
63 	static SmallMapType map_type; ///< Currently displayed legends.
64 	static bool show_towns;       ///< Display town names in the smallmap.
65 	static int map_height_limit;  ///< Currently used/cached map height limit.
66 
67 	static const uint INDUSTRY_MIN_NUMBER_OF_COLUMNS = 2; ///< Minimal number of columns in the #WID_SM_LEGEND widget for the #SMT_INDUSTRY legend.
68 	static const uint FORCE_REFRESH_PERIOD = 930; ///< map is redrawn after that many milliseconds.
69 	static const uint BLINK_PERIOD         = 450; ///< highlight blinking interval in milliseconds.
70 
71 	uint min_number_of_columns;    ///< Minimal number of columns in legends.
72 	uint min_number_of_fixed_rows; ///< Minimal number of rows in the legends for the fixed layouts only (all except #SMT_INDUSTRY).
73 	uint column_width;             ///< Width of a column in the #WID_SM_LEGEND widget.
74 	uint legend_width;             ///< Width of legend 'blob'.
75 
76 	int32 scroll_x;  ///< Horizontal world coordinate of the base tile left of the top-left corner of the smallmap display.
77 	int32 scroll_y;  ///< Vertical world coordinate of the base tile left of the top-left corner of the smallmap display.
78 	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.
79 	int zoom;        ///< Zoom level. Bigger number means more zoom-out (further away).
80 
81 	GUITimer refresh; ///< Refresh timer.
82 	LinkGraphOverlay *overlay;
83 
84 	static void BreakIndustryChainLink();
85 	Point SmallmapRemapCoords(int x, int y) const;
86 
87 	/**
88 	 * Draws vertical part of map indicator
89 	 * @param x X coord of left/right border of main viewport
90 	 * @param y Y coord of top border of main viewport
91 	 * @param y2 Y coord of bottom border of main viewport
92 	 */
DrawVertMapIndicator(int x,int y,int y2)93 	static inline void DrawVertMapIndicator(int x, int y, int y2)
94 	{
95 		GfxFillRect(x, y,      x, y + 3, PC_VERY_LIGHT_YELLOW);
96 		GfxFillRect(x, y2 - 3, x, y2,    PC_VERY_LIGHT_YELLOW);
97 	}
98 
99 	/**
100 	 * Draws horizontal part of map indicator
101 	 * @param x X coord of left border of main viewport
102 	 * @param x2 X coord of right border of main viewport
103 	 * @param y Y coord of top/bottom border of main viewport
104 	 */
DrawHorizMapIndicator(int x,int x2,int y)105 	static inline void DrawHorizMapIndicator(int x, int x2, int y)
106 	{
107 		GfxFillRect(x,      y, x + 3, y, PC_VERY_LIGHT_YELLOW);
108 		GfxFillRect(x2 - 3, y, x2,    y, PC_VERY_LIGHT_YELLOW);
109 	}
110 
111 	/**
112 	 * Compute minimal required width of the legends.
113 	 * @return Minimally needed width for displaying the smallmap legends in pixels.
114 	 */
GetMinLegendWidth()115 	inline uint GetMinLegendWidth() const
116 	{
117 		return WD_FRAMERECT_LEFT + this->min_number_of_columns * this->column_width;
118 	}
119 
120 	/**
121 	 * Return number of columns that can be displayed in \a width pixels.
122 	 * @return Number of columns to display.
123 	 */
GetNumberColumnsLegend(uint width)124 	inline uint GetNumberColumnsLegend(uint width) const
125 	{
126 		return width / this->column_width;
127 	}
128 
129 	/**
130 	 * Compute height given a number of columns.
131 	 * @param num_columns Number of columns.
132 	 * @return Needed height for displaying the smallmap legends in pixels.
133 	 */
GetLegendHeight(uint num_columns)134 	inline uint GetLegendHeight(uint num_columns) const
135 	{
136 		return WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM +
137 				this->GetNumberRowsLegend(num_columns) * FONT_HEIGHT_SMALL;
138 	}
139 
140 	/**
141 	 * Get a bitmask for company links to be displayed. Usually this will be
142 	 * the _local_company. Spectators get to see all companies' links.
143 	 * @return Company mask.
144 	 */
GetOverlayCompanyMask()145 	inline uint32 GetOverlayCompanyMask() const
146 	{
147 		return Company::IsValidID(_local_company) ? 1U << _local_company : 0xffffffff;
148 	}
149 
150 	void RebuildColourIndexIfNecessary();
151 	uint GetNumberRowsLegend(uint columns) const;
152 	void SelectLegendItem(int click_pos, LegendAndColour *legend, int end_legend_item, int begin_legend_item = 0);
153 	void SwitchMapType(SmallMapType map_type);
154 	void SetNewScroll(int sx, int sy, int sub);
155 
156 	void DrawMapIndicators() const;
157 	void DrawSmallMapColumn(void *dst, uint xc, uint yc, int pitch, int reps, int start_pos, int end_pos, Blitter *blitter) const;
158 	void DrawVehicles(const DrawPixelInfo *dpi, Blitter *blitter) const;
159 	void DrawTowns(const DrawPixelInfo *dpi) const;
160 	void DrawSmallMap(DrawPixelInfo *dpi) const;
161 
162 	Point RemapTile(int tile_x, int tile_y) const;
163 	Point PixelToTile(int px, int py, int *sub, bool add_sub = true) const;
164 	Point ComputeScroll(int tx, int ty, int x, int y, int *sub);
165 	void SetZoomLevel(ZoomLevelChange change, const Point *zoom_pt);
166 	void SetOverlayCargoMask();
167 	void SetupWidgetData();
168 	uint32 GetTileColours(const TileArea &ta) const;
169 
170 	int GetPositionOnLegend(Point pt);
171 
172 public:
173 	friend class NWidgetSmallmapDisplay;
174 
175 	SmallMapWindow(WindowDesc *desc, int window_number);
176 	virtual ~SmallMapWindow();
177 
178 	void SmallMapCenterOnCurrentPos();
179 	Point GetStationMiddle(const Station *st) const;
180 
181 	void Close() override;
182 	void SetStringParameters(int widget) const override;
183 	void OnInit() override;
184 	void OnPaint() override;
185 	void DrawWidget(const Rect &r, int widget) const override;
186 	void OnClick(Point pt, int widget, int click_count) override;
187 	void OnInvalidateData(int data = 0, bool gui_scope = true) override;
188 	bool OnRightClick(Point pt, int widget) override;
189 	void OnMouseWheel(int wheel) override;
190 	void OnRealtimeTick(uint delta_ms) override;
191 	void OnScroll(Point delta) override;
192 	void OnMouseOver(Point pt, int widget) override;
193 };
194 
195 #endif /* SMALLMAP_GUI_H */
196