OpenTTD Source: src/linkgraph/linkgraph_gui.h Source File

OpenTTD Source 12.0-beta1

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
24 struct LinkProperties {
25 LinkProperties() : capacity(0), usage(0), planned(0), shared(false) {}
26
27 uint capacity;
28 uint usage;
29 uint planned;
30 bool shared;
31 };
32
37 class LinkGraphOverlay {
38 public:
39 typedef std::map<StationID, LinkProperties> StationLinkMap;
40 typedef std::map<StationID, StationLinkMap> LinkMap;
41 typedef std::vector<std::pair<StationID, uint> > StationSupplyList;
42
43 static const uint8 LINK_COLOURS[];
44
53 LinkGraphOverlay(const Window *w, uint wid, CargoTypes cargo_mask, uint32 company_mask, uint scale) :
54 window(w), widget_id(wid), cargo_mask(cargo_mask), company_mask(company_mask), scale(scale)
55 {}
56
57 void Draw(const DrawPixelInfo *dpi);
58 void SetCargoMask(CargoTypes cargo_mask);
59 void SetCompanyMask(uint32 company_mask);
60
62 void SetDirty() { this->dirty = true; }
63
65 CargoTypes GetCargoMask() { return this->cargo_mask; }
66
68 uint32 GetCompanyMask() { return this->company_mask; }
69
70 protected:
71 const Window *window;
72 const uint widget_id;
73 CargoTypes cargo_mask;
74 uint32 company_mask;
75 LinkMap cached_links;
76 StationSupplyList cached_stations;
77 uint scale;
78 bool dirty;
79
80 Point GetStationMiddle(const Station *st) const;
81
82 void AddLinks(const Station *sta, const Station *stb);
83 void DrawLinks(const DrawPixelInfo *dpi) const;
84 void DrawStationDots(const DrawPixelInfo *dpi) const;
85 void DrawContent(Point pta, Point ptb, const LinkProperties &cargo) const;
86 bool IsLinkVisible(Point pta, Point ptb, const DrawPixelInfo *dpi, int padding = 0) const;
87 bool IsPointVisible(Point pt, const DrawPixelInfo *dpi, int padding = 0) const;
88 void GetWidgetDpi(DrawPixelInfo *dpi) const;
89 void RebuildCache();
90
91 static void AddStats(uint new_cap, uint new_usg, uint new_flow, bool new_shared, LinkProperties &cargo);
92 static void DrawVertex(int x, int y, int size, int colour, int border_colour);
93 };
94
95 void ShowLinkGraphLegend();
96
100 struct LinkGraphLegendWindow : Window {
101 public:
102 LinkGraphLegendWindow(WindowDesc *desc, int window_number);
103 void SetOverlay(LinkGraphOverlay *overlay);
104
105 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override;
106 void DrawWidget(const Rect &r, int widget) const override;
107 bool OnTooltip(Point pt, int widget, TooltipCloseCondition close_cond) override;
108 void OnClick(Point pt, int widget, int click_count) override;
109 void OnInvalidateData(int data = 0, bool gui_scope = true) override;
110
111 private:
112 LinkGraphOverlay *overlay;
113
114 void UpdateOverlayCompanies();
115 void UpdateOverlayCargoes();
116 };
117
118 #endif /* LINKGRAPH_GUI_H */
LinkGraphOverlay::company_mask
uint32 company_mask
Bitmask of companies to be displayed.
Definition: linkgraph_gui.h:74
LinkGraphOverlay::LINK_COLOURS
static const uint8 LINK_COLOURS[]
Colours for the various "load" states of links.
Definition: linkgraph_gui.h:43
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:503
linkgraph_base.h
LinkGraphOverlay::window
const Window * window
Window to be drawn into.
Definition: linkgraph_gui.h:71
LinkGraphLegendWindow
Menu window to select cargoes and companies to show in a link graph overlay.
Definition: linkgraph_gui.h:100
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:27
LinkGraphLegendWindow::UpdateOverlayCompanies
void UpdateOverlayCompanies()
Update the overlay with the new company selection.
Definition: linkgraph_gui.cpp:588
Station
Station data structure.
Definition: station_base.h:447
LinkGraphOverlay::RebuildCache
void RebuildCache()
Rebuild the cache and recalculate which links and stations to be shown.
Definition: linkgraph_gui.cpp:50
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:648
LinkGraphLegendWindow::OnTooltip
bool OnTooltip(Point pt, int widget, TooltipCloseCondition close_cond) override
Event to display a custom tooltip.
Definition: linkgraph_gui.cpp:560
LinkGraphOverlay::GetCargoMask
CargoTypes GetCargoMask()
Get a bitmask of the currently shown cargoes.
Definition: linkgraph_gui.h:65
LinkGraphLegendWindow::UpdateOverlayCargoes
void UpdateOverlayCargoes()
Update the overlay with the new cargo selection.
Definition: linkgraph_gui.cpp:602
LinkGraphOverlay::SetCompanyMask
void SetCompanyMask(uint32 company_mask)
Set a new company mask and rebuild the cache.
Definition: linkgraph_gui.cpp:366
WindowDesc
High level window description.
Definition: window_gui.h:168
LinkGraphOverlay::cached_links
LinkMap cached_links
Cache for links to reduce recalculation.
Definition: linkgraph_gui.h:75
LinkGraphOverlay::cached_stations
StationSupplyList cached_stations
Cache for stations to be drawn.
Definition: linkgraph_gui.h:76
LinkGraphOverlay
Handles drawing of links into some window.
Definition: linkgraph_gui.h:37
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:355
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:192
LinkGraphLegendWindow::SetOverlay
void SetOverlay(LinkGraphOverlay *overlay)
Set the overlay belonging to this menu and import its company/cargo settings.
Definition: linkgraph_gui.cpp:487
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:122
LinkGraphOverlay::AddStats
static void AddStats(uint new_cap, uint new_usg, uint new_flow, 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:221
LinkProperties::capacity
uint capacity
Capacity of the link.
Definition: linkgraph_gui.h:27
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:307
LinkGraphOverlay::scale
uint scale
Width of link lines.
Definition: linkgraph_gui.h:77
LinkGraphOverlay::dirty
bool dirty
Set if overlay should be rebuilt.
Definition: linkgraph_gui.h:78
LinkGraphOverlay::DrawStationDots
void DrawStationDots(const DrawPixelInfo *dpi) const
Draw dots for stations into the smallmap.
Definition: linkgraph_gui.cpp:295
LinkProperties::planned
uint planned
Planned usage of the link.
Definition: linkgraph_gui.h:29
LinkGraphLegendWindow::DrawWidget
void DrawWidget(const Rect &r, int widget) const override
Draw the contents of a nested widget.
Definition: linkgraph_gui.cpp:532
LinkGraphOverlay::Draw
void Draw(const DrawPixelInfo *dpi)
Draw the linkgraph overlay or some part of it, in the area given.
Definition: linkgraph_gui.cpp:237
LinkGraphOverlay::LinkGraphOverlay
LinkGraphOverlay(const 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:53
LinkGraphOverlay::GetStationMiddle
Point GetStationMiddle(const Station *st) const
Determine the middle of a station in the current window.
Definition: linkgraph_gui.cpp:341
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:613
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:107
LinkProperties::usage
uint usage
Actual usage of the link.
Definition: linkgraph_gui.h:28
LinkProperties::shared
bool shared
If this is a shared link to be drawn dashed.
Definition: linkgraph_gui.h:30
LinkGraphOverlay::GetWidgetDpi
void GetWidgetDpi(DrawPixelInfo *dpi) const
Get a DPI for the widget we will be drawing to.
Definition: linkgraph_gui.cpp:39
LinkGraphOverlay::cargo_mask
CargoTypes cargo_mask
Bitmask of cargos to be displayed.
Definition: linkgraph_gui.h:73
LinkGraphOverlay::SetDirty
void SetDirty()
Mark the linkgraph dirty to be rebuilt next time Draw() is called.
Definition: linkgraph_gui.h:62
LinkGraphOverlay::GetCompanyMask
uint32 GetCompanyMask()
Get a bitmask of the currently shown companies.
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:271
LinkGraphOverlay::widget_id
const uint widget_id
ID of Widget in Window to be drawn to.
Definition: linkgraph_gui.h:72
Window
Data structure for an opened window.
Definition: window_gui.h:279
LinkGraphOverlay::DrawLinks
void DrawLinks(const DrawPixelInfo *dpi) const
Draw the cached links or part of them into the given area.
Definition: linkgraph_gui.cpp:251
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:47
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:320
ShowLinkGraphLegend
void ShowLinkGraphLegend()
Open a link graph legend window.
Definition: linkgraph_gui.cpp:471
DrawPixelInfo
Data about how and where to blit pixels.
Definition: gfx_type.h:155
LinkProperties
Properties of a link between two stations.
Definition: linkgraph_gui.h:24