Merge branch 'development' into master_joker
[openttd-joker.git] / src / smallmap_gui.h
blobc01838ca4c146b673e3f4308b7c3842dbbb1ac9e
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file smallmap_gui.h Smallmap GUI functions. */
12 #ifndef SMALLMAP_GUI_H
13 #define SMALLMAP_GUI_H
15 #include "industry_type.h"
16 #include "company_base.h"
17 #include "window_gui.h"
18 #include "strings_func.h"
19 #include "blitter/factory.hpp"
20 #include "linkgraph/linkgraph_gui.h"
21 #include "widgets/smallmap_widget.h"
23 static const int NUM_NO_COMPANY_ENTRIES = 4; ///< Number of entries in the owner legend that are not companies.
25 /** Mapping of tile type to importance of the tile (higher number means more interesting to show). */
26 static const byte _tiletype_importance[] = {
27 2, // MP_CLEAR
28 8, // MP_RAILWAY
29 7, // MP_ROAD
30 5, // MP_HOUSE
31 2, // MP_TREES
32 9, // MP_STATION
33 2, // MP_WATER
34 1, // MP_VOID
35 6, // MP_INDUSTRY
36 8, // MP_TUNNELBRIDGE
37 2, // MP_OBJECT
41 /* set up the cargos to be displayed in the smallmap's route legend */
42 void BuildLinkStatsLegend();
44 struct TunnelBridgeToMap {
45 TileIndex from_tile;
46 TileIndex to_tile;
48 typedef SmallVector<TunnelBridgeToMap, 64> TunnelBridgeToMapVector;
50 void BuildIndustriesLegend();
51 void ShowSmallMap();
52 void BuildLandLegend();
53 void BuildOwnerLegend();
55 /** Structure for holding relevant data for legends in small map */
56 struct LegendAndColour {
57 uint8 colour; ///< Colour of the item on the map.
58 StringID legend; ///< String corresponding to the coloured item.
59 IndustryType type; ///< Type of industry. Only valid for industry entries.
60 uint8 height; ///< Height in tiles. Only valid for height legend entries.
61 CompanyID company; ///< Company to display. Only valid for company entries of the owner legend.
62 bool show_on_map; ///< For filtering industries, if \c true, industry is shown on the map in colour.
63 bool end; ///< This is the end of the list.
64 bool col_break; ///< Perform a column break and go further at the next column.
67 /** Class managing the smallmap window. */
68 class SmallMapWindow : public Window {
69 protected:
70 /** Types of legends in the #WID_SM_LEGEND widget. */
71 enum SmallMapType {
72 SMT_CONTOUR,
73 SMT_VEHICLES,
74 SMT_INDUSTRY,
75 SMT_LINKSTATS,
76 SMT_ROUTES,
77 SMT_VEGETATION,
78 SMT_OWNER,
81 /** Available kinds of zoomlevel changes. */
82 enum ZoomLevelChange {
83 ZLC_INITIALIZE, ///< Initialize zoom level.
84 ZLC_ZOOM_OUT, ///< Zoom out.
85 ZLC_ZOOM_IN, ///< Zoom in.
88 struct MouseOverTileSearchData {
89 SmallMapType map_type;
90 TileType found_type;
93 static SmallMapType map_type; ///< Currently displayed legends.
94 static bool show_towns; ///< Display town names in the smallmap.
95 static int max_heightlevel; ///< Currently used/cached maximum heightlevel.
97 static const uint LEGEND_BLOB_WIDTH = 8; ///< Width of the coloured blob in front of a line text in the #WID_SM_LEGEND widget.
98 static const uint INDUSTRY_MIN_NUMBER_OF_COLUMNS = 2; ///< Minimal number of columns in the #WID_SM_LEGEND widget for the #SMT_INDUSTRY legend.
99 static const uint FORCE_REFRESH_PERIOD = 0x1F; ///< map is redrawn after that many ticks
100 static const uint BLINK_PERIOD = 0x0F; ///< highlight blinking interval
102 uint min_number_of_columns; ///< Minimal number of columns in legends.
103 uint min_number_of_fixed_rows; ///< Minimal number of rows in the legends for the fixed layouts only (all except #SMT_INDUSTRY).
104 uint column_width; ///< Width of a column in the #WID_SM_LEGEND widget.
106 int32 scroll_x; ///< Horizontal world coordinate of the base tile left of the top-left corner of the smallmap display.
107 int32 scroll_y; ///< Vertical world coordinate of the base tile left of the top-left corner of the smallmap display.
108 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.
109 int zoom; ///< Zoom level. Bigger number means more zoom-out (further away).
111 uint8 refresh; ///< Refresh counter, zeroed every FORCE_REFRESH_PERIOD ticks.
112 LinkGraphOverlay *overlay;
114 static void BreakIndustryChainLink();
115 Point SmallmapRemapCoords(int x, int y) const;
118 * Draws vertical part of map indicator
119 * @param x X coord of left/right border of main viewport
120 * @param y Y coord of top border of main viewport
121 * @param y2 Y coord of bottom border of main viewport
123 static inline void DrawVertMapIndicator(int x, int y, int y2)
125 GfxFillRect(x, y, x, y + 3, PC_VERY_LIGHT_YELLOW);
126 GfxFillRect(x, y2 - 3, x, y2, PC_VERY_LIGHT_YELLOW);
130 * Draws horizontal part of map indicator
131 * @param x X coord of left border of main viewport
132 * @param x2 X coord of right border of main viewport
133 * @param y Y coord of top/bottom border of main viewport
135 static inline void DrawHorizMapIndicator(int x, int x2, int y)
137 GfxFillRect(x, y, x + 3, y, PC_VERY_LIGHT_YELLOW);
138 GfxFillRect(x2 - 3, y, x2, y, PC_VERY_LIGHT_YELLOW);
142 * Compute minimal required width of the legends.
143 * @return Minimally needed width for displaying the smallmap legends in pixels.
145 inline uint GetMinLegendWidth() const
147 return WD_FRAMERECT_LEFT + this->min_number_of_columns * this->column_width;
151 * Return number of columns that can be displayed in \a width pixels.
152 * @return Number of columns to display.
154 inline uint GetNumberColumnsLegend(uint width) const
156 return width / this->column_width;
160 * Compute height given a number of columns.
161 * @param num_columns Number of columns.
162 * @return Needed height for displaying the smallmap legends in pixels.
164 inline uint GetLegendHeight(uint num_columns) const
166 return WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM +
167 this->GetNumberRowsLegend(num_columns) * FONT_HEIGHT_SMALL;
171 * Get a bitmask for company links to be displayed. Usually this will be
172 * the _local_company. Spectators get to see all companies' links.
173 * @return Company mask.
175 inline uint32 GetOverlayCompanyMask() const
177 return Company::IsValidID(_local_company) ? 1U << _local_company : 0xffffffff;
180 uint GetNumberRowsLegend(uint columns) const;
181 void SelectLegendItem(int click_pos, LegendAndColour *legend, int end_legend_item, int begin_legend_item = 0);
182 void SwitchMapType(SmallMapType map_type);
183 void SetNewScroll(int sx, int sy, int sub);
185 void DrawMapIndicators() const;
186 void DrawSmallMapColumn(void *dst, uint xc, uint yc, int pitch, int reps, int start_pos, int end_pos, Blitter *blitter) const;
187 void DrawVehicles(const DrawPixelInfo *dpi, Blitter *blitter) const;
188 void DrawTowns(const DrawPixelInfo *dpi) const;
189 void DrawSmallMap(DrawPixelInfo *dpi, bool draw_indicators = true) const;
191 Point RemapTile(int tile_x, int tile_y) const;
192 Point PixelToTile(int px, int py, int *sub, bool add_sub = true) const;
193 Point ComputeScroll(int tx, int ty, int x, int y, int *sub);
194 void SetZoomLevel(ZoomLevelChange change, const Point *zoom_pt);
195 void SetOverlayCargoMask();
196 void SetupWidgetData();
197 uint32 GetTileColours(const TileArea &ta) const;
199 int GetPositionOnLegend(Point pt);
201 public:
202 friend class NWidgetSmallmapDisplay;
204 SmallMapWindow(WindowDesc *desc, int window_number);
205 virtual ~SmallMapWindow();
207 static bool MouseOverTileSearchProc(TileIndex tile, void *user_data);
209 static void RebuildColourIndexIfNecessary();
211 void SmallMapCenterOnCurrentPos();
212 Point GetStationMiddle(const Station *st) const;
214 virtual void SetStringParameters(int widget) const;
215 virtual void OnInit();
216 virtual void OnPaint();
217 virtual void DrawWidget(const Rect &r, int widget) const;
218 virtual void OnClick(Point pt, int widget, int click_count);
219 virtual void OnInvalidateData(int data = 0, bool gui_scope = true);
220 virtual bool OnRightClick(Point pt, int widget);
221 virtual void OnMouseWheel(int wheel);
222 virtual void OnTick();
223 virtual void OnScroll(Point delta);
224 virtual void OnMouseOver(Point pt, int widget);
225 virtual void OnToolTip(Point pt, int widget, TooltipCloseCondition close_cond);
227 void TakeScreenshot();
228 void ScreenshotCallbackHandler(void *buf, uint y, uint pitch, uint n);
231 #endif /* SMALLMAP_GUI_H */