OpenTTD Source  20240917-master-g9ab0a47812
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 
24  LinkProperties() : cargo(INVALID_CARGO), capacity(0), usage(0), planned(0), shared(false) {}
25 
27  uint Usage() const { return std::max(this->usage, this->planned); }
28 
30  uint capacity;
31  uint usage;
32  uint planned;
33  uint32_t time;
34  bool shared;
35 };
36 
42 public:
43  typedef std::map<StationID, LinkProperties> StationLinkMap;
44  typedef std::map<StationID, StationLinkMap> LinkMap;
45  typedef std::vector<std::pair<StationID, uint> > StationSupplyList;
46 
47  static const uint8_t LINK_COLOURS[][12];
48 
57  LinkGraphOverlay(Window *w, WidgetID wid, CargoTypes cargo_mask, CompanyMask company_mask, uint scale) :
59  {}
60 
61  void Draw(const DrawPixelInfo *dpi);
62  void SetCargoMask(CargoTypes cargo_mask);
63  void SetCompanyMask(CompanyMask company_mask);
64 
65  bool ShowTooltip(Point pt, TooltipCloseCondition close_cond);
66 
68  void SetDirty() { this->dirty = true; }
69 
71  CargoTypes GetCargoMask() { return this->cargo_mask; }
72 
74  CompanyMask GetCompanyMask() { return this->company_mask; }
75 
76 protected:
79  CargoTypes cargo_mask;
80  CompanyMask company_mask;
81  LinkMap cached_links;
82  StationSupplyList cached_stations;
83  uint scale;
84  bool dirty;
85 
86  Point GetStationMiddle(const Station *st) const;
87 
88  void AddLinks(const Station *sta, const Station *stb);
89  void DrawLinks(const DrawPixelInfo *dpi) const;
90  void DrawStationDots(const DrawPixelInfo *dpi) const;
91  void DrawContent(Point pta, Point ptb, const LinkProperties &cargo) const;
92  bool IsLinkVisible(Point pta, Point ptb, const DrawPixelInfo *dpi, int padding = 0) const;
93  bool IsPointVisible(Point pt, const DrawPixelInfo *dpi, int padding = 0) const;
94  void GetWidgetDpi(DrawPixelInfo *dpi) const;
95  void RebuildCache();
96 
97  static void AddStats(CargoID new_cargo, uint new_cap, uint new_usg, uint new_flow, uint32_t time, bool new_shared, LinkProperties &cargo);
98  static void DrawVertex(int x, int y, int size, int colour, int border_colour);
99 };
100 
101 void ShowLinkGraphLegend();
102 
107 public:
109  void SetOverlay(std::shared_ptr<LinkGraphOverlay> overlay);
110 
111  void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override;
112  void DrawWidget(const Rect &r, WidgetID widget) const override;
113  bool OnTooltip([[maybe_unused]] Point pt, WidgetID widget, TooltipCloseCondition close_cond) override;
114  void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override;
115  void OnInvalidateData(int data = 0, bool gui_scope = true) override;
116 
117 private:
118  std::shared_ptr<LinkGraphOverlay> overlay;
119  size_t num_cargo;
120 
121  void UpdateOverlayCompanies();
122  void UpdateOverlayCargoes();
123 };
124 
125 #endif /* LINKGRAPH_GUI_H */
LinkGraphLegendWindow::SetOverlay
void SetOverlay(std::shared_ptr< LinkGraphOverlay > overlay)
Set the overlay belonging to this menu and import its company/cargo settings.
Definition: linkgraph_gui.cpp:567
LinkGraphOverlay::SetCompanyMask
void SetCompanyMask(CompanyMask company_mask)
Set a new company mask and rebuild the cache.
Definition: linkgraph_gui.cpp:446
linkgraph_base.h
LinkGraphLegendWindow
Menu window to select cargoes and companies to show in a link graph overlay.
Definition: linkgraph_gui.h:106
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:30
LinkProperties::cargo
CargoID cargo
Cargo type of the link.
Definition: linkgraph_gui.h:29
LinkGraphLegendWindow::UpdateOverlayCompanies
void UpdateOverlayCompanies()
Update the overlay with the new company selection.
Definition: linkgraph_gui.cpp:663
Station
Station data structure.
Definition: station_base.h:439
LinkGraphOverlay::RebuildCache
void RebuildCache()
Rebuild the cache and recalculate which links and stations to be shown.
Definition: linkgraph_gui.cpp:71
LinkGraphOverlay::widget_id
const WidgetID widget_id
ID of Widget in Window to be drawn to.
Definition: linkgraph_gui.h:78
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:719
LinkGraphOverlay::GetCargoMask
CargoTypes GetCargoMask()
Get a bitmask of the currently shown cargoes.
Definition: linkgraph_gui.h:71
LinkGraphLegendWindow::UpdateOverlayCargoes
void UpdateOverlayCargoes()
Update the overlay with the new cargo selection.
Definition: linkgraph_gui.cpp:677
LinkGraphOverlay::GetCompanyMask
CompanyMask GetCompanyMask()
Get a bitmask of the currently shown companies.
Definition: linkgraph_gui.h:74
LinkGraphOverlay::window
Window * window
Window to be drawn into.
Definition: linkgraph_gui.h:77
WindowDesc
High level window description.
Definition: window_gui.h:162
WidgetID
int WidgetID
Widget ID.
Definition: window_type.h:18
LinkGraphOverlay::cached_links
LinkMap cached_links
Cache for links to reduce recalculation.
Definition: linkgraph_gui.h:81
LinkGraphOverlay::cached_stations
StationSupplyList cached_stations
Cache for stations to be drawn.
Definition: linkgraph_gui.h:82
LinkGraphOverlay
Handles drawing of links into some window.
Definition: linkgraph_gui.h:41
Window::resize
ResizeInfo resize
Resize information.
Definition: window_gui.h:317
LinkGraphOverlay::SetCargoMask
void SetCargoMask(CargoTypes cargo_mask)
Set a new cargo mask and rebuild the cache.
Definition: linkgraph_gui.cpp:435
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:213
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:143
LinkProperties::capacity
uint capacity
Capacity of the link.
Definition: linkgraph_gui.h:30
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
LinkGraphOverlay::company_mask
CompanyMask company_mask
Bitmask of companies to be displayed.
Definition: linkgraph_gui.h:80
Window::window_number
WindowNumber window_number
Window number within the window class.
Definition: window_gui.h:305
LinkGraphOverlay::scale
uint scale
Width of link lines.
Definition: linkgraph_gui.h:83
LinkGraphOverlay::dirty
bool dirty
Set if overlay should be rebuilt.
Definition: linkgraph_gui.h:84
LinkGraphOverlay::DrawStationDots
void DrawStationDots(const DrawPixelInfo *dpi) const
Draw dots for stations into the smallmap.
Definition: linkgraph_gui.cpp:322
LinkProperties::planned
uint planned
Planned usage of the link.
Definition: linkgraph_gui.h:32
LinkProperties::time
uint32_t time
Travel time of the link.
Definition: linkgraph_gui.h:33
LinkGraphOverlay::Draw
void Draw(const DrawPixelInfo *dpi)
Draw the linkgraph overlay or some part of it, in the area given.
Definition: linkgraph_gui.cpp:262
LinkGraphOverlay::GetStationMiddle
Point GetStationMiddle(const Station *st) const
Determine the middle of a station in the current window.
Definition: linkgraph_gui.cpp:421
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:128
CargoID
uint8_t CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
LinkProperties::usage
uint usage
Actual usage of the link.
Definition: linkgraph_gui.h:31
LinkProperties::shared
bool shared
If this is a shared link to be drawn dashed.
Definition: linkgraph_gui.h:34
LinkGraphOverlay::GetWidgetDpi
void GetWidgetDpi(DrawPixelInfo *dpi) const
Get a DPI for the widget we will be drawing to.
Definition: linkgraph_gui.cpp:60
LinkGraphOverlay::cargo_mask
CargoTypes cargo_mask
Bitmask of cargos to be displayed.
Definition: linkgraph_gui.h:79
LinkGraphOverlay::SetDirty
void SetDirty()
Mark the linkgraph dirty to be rebuilt next time Draw() is called.
Definition: linkgraph_gui.h:68
LinkGraphOverlay::DrawContent
void DrawContent(Point pta, Point ptb, const LinkProperties &cargo) const
Draw one specific link.
Definition: linkgraph_gui.cpp:297
Window
Data structure for an opened window.
Definition: window_gui.h:276
LinkGraphOverlay::DrawLinks
void DrawLinks(const DrawPixelInfo *dpi) const
Draw the cached links or part of them into the given area.
Definition: linkgraph_gui.cpp:276
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:75
LinkGraphOverlay::LINK_COLOURS
static const uint8_t LINK_COLOURS[][12]
Colours for the various "load" states of links.
Definition: linkgraph_gui.h:47
LinkGraphOverlay::AddStats
static void AddStats(CargoID new_cargo, uint new_cap, uint new_usg, uint new_flow, uint32_t 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:244
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:348
LinkProperties::Usage
uint Usage() const
Return the usage of the link to display.
Definition: linkgraph_gui.h:27
ShowLinkGraphLegend
void ShowLinkGraphLegend()
Open a link graph legend window.
Definition: linkgraph_gui.cpp:549
LinkGraphOverlay::LinkGraphOverlay
LinkGraphOverlay(Window *w, WidgetID wid, CargoTypes cargo_mask, CompanyMask company_mask, uint scale)
Create a link graph overlay for the specified window.
Definition: linkgraph_gui.h:57
DrawPixelInfo
Data about how and where to blit pixels.
Definition: gfx_type.h:157
LinkProperties
Monthly statistics for a link between two stations.
Definition: linkgraph_gui.h:23