OpenTTD Source  13.2.1
linkgraph_gui.h
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
10 #ifndef LINKGRAPH_GUI_H
11 #define LINKGRAPH_GUI_H
12 
13 #include "../company_func.h"
14 #include "../station_base.h"
15 #include "../widget_type.h"
16 #include "../window_gui.h"
17 #include "linkgraph_base.h"
18 #include <map>
19 #include <vector>
20 
26  LinkProperties() : cargo(CT_INVALID), capacity(0), usage(0), planned(0), shared(false) {}
27 
29  uint Usage() const { return std::max(this->usage, this->planned); }
30 
32  uint capacity;
33  uint usage;
34  uint planned;
35  uint32 time;
36  bool shared;
37 };
38 
44 public:
45  typedef std::map<StationID, LinkProperties> StationLinkMap;
46  typedef std::map<StationID, StationLinkMap> LinkMap;
47  typedef std::vector<std::pair<StationID, uint> > StationSupplyList;
48 
49  static const uint8 LINK_COLOURS[][12];
50 
59  LinkGraphOverlay(Window *w, uint wid, CargoTypes cargo_mask, uint32 company_mask, uint scale) :
61  {}
62 
63  void Draw(const DrawPixelInfo *dpi);
64  void SetCargoMask(CargoTypes cargo_mask);
65  void SetCompanyMask(uint32 company_mask);
66 
67  bool ShowTooltip(Point pt, TooltipCloseCondition close_cond);
68 
70  void SetDirty() { this->dirty = true; }
71 
73  CargoTypes GetCargoMask() { return this->cargo_mask; }
74 
76  uint32 GetCompanyMask() { return this->company_mask; }
77 
78 protected:
80  const uint widget_id;
81  CargoTypes cargo_mask;
82  uint32 company_mask;
83  LinkMap cached_links;
84  StationSupplyList cached_stations;
85  uint scale;
86  bool dirty;
87 
88  Point GetStationMiddle(const Station *st) const;
89 
90  void AddLinks(const Station *sta, const Station *stb);
91  void DrawLinks(const DrawPixelInfo *dpi) const;
92  void DrawStationDots(const DrawPixelInfo *dpi) const;
93  void DrawContent(Point pta, Point ptb, const LinkProperties &cargo) const;
94  bool IsLinkVisible(Point pta, Point ptb, const DrawPixelInfo *dpi, int padding = 0) const;
95  bool IsPointVisible(Point pt, const DrawPixelInfo *dpi, int padding = 0) const;
96  void GetWidgetDpi(DrawPixelInfo *dpi) const;
97  void RebuildCache();
98 
99  static void AddStats(CargoID new_cargo, uint new_cap, uint new_usg, uint new_flow, uint32 time, bool new_shared, LinkProperties &cargo);
100  static void DrawVertex(int x, int y, int size, int colour, int border_colour);
101 };
102 
103 void ShowLinkGraphLegend();
104 
109 public:
111  void SetOverlay(LinkGraphOverlay *overlay);
112 
113  void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override;
114  void DrawWidget(const Rect &r, int widget) const override;
115  bool OnTooltip(Point pt, int widget, TooltipCloseCondition close_cond) override;
116  void OnClick(Point pt, int widget, int click_count) override;
117  void OnInvalidateData(int data = 0, bool gui_scope = true) override;
118 
119 private:
120  LinkGraphOverlay *overlay;
121 
122  void UpdateOverlayCompanies();
123  void UpdateOverlayCargoes();
124 };
125 
126 #endif /* LINKGRAPH_GUI_H */
LinkGraphOverlay::company_mask
uint32 company_mask
Bitmask of companies to be displayed.
Definition: linkgraph_gui.h:82
LinkGraphLegendWindow::UpdateWidgetSize
void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
Update size and resize step of a widget in the window.
Definition: linkgraph_gui.cpp:579
linkgraph_base.h
LinkGraphOverlay::LinkGraphOverlay
LinkGraphOverlay(Window *w, uint wid, CargoTypes cargo_mask, uint32 company_mask, uint scale)
Create a link graph overlay for the specified window.
Definition: linkgraph_gui.h:59
LinkGraphLegendWindow
Menu window to select cargoes and companies to show in a link graph overlay.
Definition: linkgraph_gui.h:108
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:27
LinkProperties::cargo
CargoID cargo
Cargo type of the link.
Definition: linkgraph_gui.h:31
LinkGraphLegendWindow::UpdateOverlayCompanies
void UpdateOverlayCompanies()
Update the overlay with the new company selection.
Definition: linkgraph_gui.cpp:669
Station
Station data structure.
Definition: station_base.h:454
LinkGraphOverlay::RebuildCache
void RebuildCache()
Rebuild the cache and recalculate which links and stations to be shown.
Definition: linkgraph_gui.cpp:68
LinkGraphLegendWindow::OnInvalidateData
void OnInvalidateData(int data=0, bool gui_scope=true) override
Invalidate the data of this window if the cargoes or companies have changed.
Definition: linkgraph_gui.cpp:729
LinkGraphLegendWindow::OnTooltip
bool OnTooltip(Point pt, int widget, TooltipCloseCondition close_cond) override
Event to display a custom tooltip.
Definition: linkgraph_gui.cpp:641
LinkGraphOverlay::GetCargoMask
CargoTypes GetCargoMask()
Get a bitmask of the currently shown cargoes.
Definition: linkgraph_gui.h:73
LinkGraphLegendWindow::UpdateOverlayCargoes
void UpdateOverlayCargoes()
Update the overlay with the new cargo selection.
Definition: linkgraph_gui.cpp:683
LinkGraphOverlay::SetCompanyMask
void SetCompanyMask(uint32 company_mask)
Set a new company mask and rebuild the cache.
Definition: linkgraph_gui.cpp:445
LinkGraphOverlay::window
Window * window
Window to be drawn into.
Definition: linkgraph_gui.h:79
LinkProperties::time
uint32 time
Travel time of the link.
Definition: linkgraph_gui.h:35
WindowDesc
High level window description.
Definition: window_gui.h:102
LinkGraphOverlay::cached_links
LinkMap cached_links
Cache for links to reduce recalculation.
Definition: linkgraph_gui.h:83
LinkGraphOverlay::cached_stations
StationSupplyList cached_stations
Cache for stations to be drawn.
Definition: linkgraph_gui.h:84
LinkGraphOverlay
Handles drawing of links into some window.
Definition: linkgraph_gui.h:43
LinkGraphOverlay::LINK_COLOURS
static const uint8 LINK_COLOURS[][12]
Colours for the various "load" states of links.
Definition: linkgraph_gui.h:49
Window::resize
ResizeInfo resize
Resize information.
Definition: window_gui.h:251
LinkGraphOverlay::SetCargoMask
void SetCargoMask(CargoTypes cargo_mask)
Set a new cargo mask and rebuild the cache.
Definition: linkgraph_gui.cpp:434
LinkGraphOverlay::AddLinks
void AddLinks(const Station *sta, const Station *stb)
Add all "interesting" links between the given stations to the cache.
Definition: linkgraph_gui.cpp:210
LinkGraphLegendWindow::SetOverlay
void SetOverlay(LinkGraphOverlay *overlay)
Set the overlay belonging to this menu and import its company/cargo settings.
Definition: linkgraph_gui.cpp:563
LinkGraphOverlay::IsLinkVisible
bool IsLinkVisible(Point pta, Point ptb, const DrawPixelInfo *dpi, int padding=0) const
Determine if a certain link crosses through the area given by the dpi with some lee way.
Definition: linkgraph_gui.cpp:140
LinkProperties::capacity
uint capacity
Capacity of the link.
Definition: linkgraph_gui.h:32
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
Window::window_number
WindowNumber window_number
Window number within the window class.
Definition: window_gui.h:241
LinkGraphOverlay::scale
uint scale
Width of link lines.
Definition: linkgraph_gui.h:85
LinkGraphOverlay::dirty
bool dirty
Set if overlay should be rebuilt.
Definition: linkgraph_gui.h:86
LinkGraphOverlay::DrawStationDots
void DrawStationDots(const DrawPixelInfo *dpi) const
Draw dots for stations into the smallmap.
Definition: linkgraph_gui.cpp:319
LinkProperties::planned
uint planned
Planned usage of the link.
Definition: linkgraph_gui.h:34
LinkGraphLegendWindow::DrawWidget
void DrawWidget(const Rect &r, int widget) const override
Draw the contents of a nested widget.
Definition: linkgraph_gui.cpp:608
LinkGraphOverlay::Draw
void Draw(const DrawPixelInfo *dpi)
Draw the linkgraph overlay or some part of it, in the area given.
Definition: linkgraph_gui.cpp:259
LinkGraphOverlay::GetStationMiddle
Point GetStationMiddle(const Station *st) const
Determine the middle of a station in the current window.
Definition: linkgraph_gui.cpp:420
LinkGraphLegendWindow::OnClick
void OnClick(Point pt, int widget, int click_count) override
A click with the left mouse button has been made on the window.
Definition: linkgraph_gui.cpp:694
LinkGraphOverlay::IsPointVisible
bool IsPointVisible(Point pt, const DrawPixelInfo *dpi, int padding=0) const
Determine if a certain point is inside the given DPI, with some lee way.
Definition: linkgraph_gui.cpp:125
LinkGraphOverlay::AddStats
static void AddStats(CargoID new_cargo, uint new_cap, uint new_usg, uint new_flow, uint32 time, bool new_shared, LinkProperties &cargo)
Add information from a given pair of link stat and flow stat to the given link properties.
Definition: linkgraph_gui.cpp:241
LinkProperties::usage
uint usage
Actual usage of the link.
Definition: linkgraph_gui.h:33
LinkProperties::shared
bool shared
If this is a shared link to be drawn dashed.
Definition: linkgraph_gui.h:36
LinkGraphOverlay::GetWidgetDpi
void GetWidgetDpi(DrawPixelInfo *dpi) const
Get a DPI for the widget we will be drawing to.
Definition: linkgraph_gui.cpp:57
LinkGraphOverlay::cargo_mask
CargoTypes cargo_mask
Bitmask of cargos to be displayed.
Definition: linkgraph_gui.h:81
LinkGraphOverlay::SetDirty
void SetDirty()
Mark the linkgraph dirty to be rebuilt next time Draw() is called.
Definition: linkgraph_gui.h:70
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:20
LinkGraphOverlay::GetCompanyMask
uint32 GetCompanyMask()
Get a bitmask of the currently shown companies.
Definition: linkgraph_gui.h:76
LinkGraphOverlay::DrawContent
void DrawContent(Point pta, Point ptb, const LinkProperties &cargo) const
Draw one specific link.
Definition: linkgraph_gui.cpp:294
LinkGraphOverlay::widget_id
const uint widget_id
ID of Widget in Window to be drawn to.
Definition: linkgraph_gui.h:80
Window
Data structure for an opened window.
Definition: window_gui.h:213
LinkGraphOverlay::DrawLinks
void DrawLinks(const DrawPixelInfo *dpi) const
Draw the cached links or part of them into the given area.
Definition: linkgraph_gui.cpp:273
CT_INVALID
@ CT_INVALID
Invalid cargo type.
Definition: cargo_type.h:69
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:69
LinkGraphOverlay::DrawVertex
static void DrawVertex(int x, int y, int size, int colour, int border_colour)
Draw a square symbolizing a producer of cargo.
Definition: linkgraph_gui.cpp:345
LinkProperties::Usage
uint Usage() const
Return the usage of the link to display.
Definition: linkgraph_gui.h:29
ShowLinkGraphLegend
void ShowLinkGraphLegend()
Open a link graph legend window.
Definition: linkgraph_gui.cpp:547
DrawPixelInfo
Data about how and where to blit pixels.
Definition: gfx_type.h:151
LinkProperties
Monthly statistics for a link between two stations.
Definition: linkgraph_gui.h:25