Go to the documentation of this file.
10 #include "../stdafx.h"
11 #include "../window_gui.h"
12 #include "../window_func.h"
13 #include "../company_base.h"
14 #include "../company_gui.h"
15 #include "../date_func.h"
16 #include "../viewport_func.h"
17 #include "../zoom_func.h"
18 #include "../smallmap_gui.h"
19 #include "../core/geometry_func.hpp"
20 #include "../widgets/link_graph_legend_widget.h"
22 #include "table/strings.h"
24 #include "../safeguards.h"
32 0x0f, 0xd1, 0xd0, 0x57,
33 0x55, 0x53, 0xbf, 0xbd,
34 0xba, 0xb9, 0xb7, 0xb5
37 0x0f, 0xd1, 0xd0, 0x57,
38 0x55, 0x53, 0x96, 0x95,
39 0x94, 0x93, 0x92, 0x91
42 0x0f, 0x0b, 0x09, 0x07,
43 0x05, 0x03, 0xbf, 0xbd,
44 0xba, 0xb9, 0xb7, 0xb5
47 0x0f, 0x0b, 0x0a, 0x09,
48 0x08, 0x07, 0x06, 0x05,
49 0x04, 0x03, 0x02, 0x01
60 dpi->left = dpi->top = 0;
78 if (sta->rect.IsEmpty())
continue;
82 StationID from = sta->index;
91 ConstNode from_node = lg[sta->goods[c].node];
94 StationID to = lg[i->first].Station();
104 if (stb->
rect.IsEmpty())
continue;
127 return pt.x > dpi->left - padding && pt.y > dpi->top - padding &&
128 pt.x < dpi->left + dpi->width + padding &&
129 pt.y < dpi->top + dpi->height + padding;
142 const int left = dpi->left - padding;
143 const int right = dpi->left + dpi->width + padding;
144 const int top = dpi->top - padding;
145 const int bottom = dpi->top + dpi->height + padding;
152 const uint8 INSIDE = 0;
153 const uint8 LEFT = 1;
154 const uint8 RIGHT = 2;
155 const uint8 BOTTOM = 4;
163 auto out_code = [&](
int x,
int y) -> uint8 {
167 }
else if (x > right) {
172 }
else if (y > bottom) {
178 uint8 c0 = out_code(x0, y0);
179 uint8 c1 = out_code(x1, y1);
182 if (c0 == 0 || c1 == 0)
return true;
183 if ((c0 & c1) != 0)
return false;
186 x0 = x0 + (int)(((int64) (x1 - x0)) * ((int64) (top - y0)) / ((int64) (y1 - y0)));
188 }
else if (c0 & BOTTOM) {
189 x0 = x0 + (int)(((int64) (x1 - x0)) * ((int64) (bottom - y0)) / ((int64) (y1 - y0)));
191 }
else if (c0 & RIGHT) {
192 y0 = y0 + (int)(((int64) (y1 - y0)) * ((int64) (right - x0)) / ((int64) (x1 - x0)));
194 }
else if (c0 & LEFT) {
195 y0 = y0 + (int)(((int64) (y1 - y0)) * ((int64) (left - x0)) / ((int64) (x1 - x0)));
199 c0 = out_code(x0, y0);
245 cargo.
Usage() * 32 / (cargo.
capacity + 1) < std::max(new_usg, new_plan) * 32 / (new_cap + 1)) {
246 cargo.
cargo = new_cargo;
248 cargo.
usage = new_usg;
252 if (new_shared) cargo.
shared =
true;
276 for (LinkMap::const_iterator i(this->
cached_links.begin()); i != this->cached_links.end(); ++i) {
279 for (StationLinkMap::const_iterator j(i->second.begin()); j != i->second.end(); ++j) {
282 if (!this->
IsLinkVisible(pta, ptb, dpi, width + 2))
continue;
296 uint usage_or_plan = std::min(cargo.
capacity * 2 + 1, cargo.
Usage());
299 int dash = cargo.
shared ? width * 4 : 0;
304 if (
abs(pta.x - ptb.x) <
abs(pta.y - ptb.y)) {
305 int offset_x = (pta.y > ptb.y ? 1 : -1) * side * width;
306 GfxDrawLine(pta.x + offset_x, pta.y, ptb.x + offset_x, ptb.y, colour, width, dash);
308 int offset_y = (pta.x < ptb.x ? 1 : -1) * side * width;
309 GfxDrawLine(pta.x, pta.y + offset_y, ptb.x, ptb.y + offset_y, colour, width, dash);
312 GfxDrawLine(pta.x, pta.y, ptb.x, ptb.y,
_colour_gradient[COLOUR_GREY][1], width);
322 for (StationSupplyList::const_iterator i(this->
cached_stations.begin()); i != this->cached_stations.end(); ++i) {
324 if (st ==
nullptr)
continue;
328 uint r = width * 2 + width * 2 * std::min(200U, i->second) / 200;
349 int w2 = size / 2 + size % 2;
351 GfxFillRect(x - w1, y - w1, x + w2, y + w2, colour);
355 GfxDrawLine(x - w1, y - w1, x + w2, y - w1, border_colour);
356 GfxDrawLine(x - w1, y + w2, x + w2, y + w2, border_colour);
357 GfxDrawLine(x - w1, y - w1, x - w1, y + w2, border_colour);
358 GfxDrawLine(x + w2, y - w1, x + w2, y + w2, border_colour);
361 bool LinkGraphOverlay::ShowTooltip(
Point pt, TooltipCloseCondition close_cond)
363 for (
auto i(this->
cached_links.crbegin()); i != this->cached_links.crend(); ++i) {
366 for (
auto j(i->second.crbegin()); j != i->second.crend(); ++j) {
368 if (i->first == j->first)
continue;
372 float dist =
std::abs((ptb.x - pta.x) * (pta.y - pt.y) - (pta.x - pt.x) * (ptb.y - pta.y)) /
373 std::sqrt((ptb.x - pta.x) * (ptb.x - pta.x) + (ptb.y - pta.y) * (ptb.y - pta.y));
374 const auto &link = j->second;
375 if (dist <= 4 && link.Usage() > 0 &&
376 pt.x >= std::min(pta.x, ptb.x) &&
377 pt.x <= std::max(pta.x, ptb.x)) {
378 static char buf[1024];
382 uint32 back_time = 0;
385 const auto &back = k->second;
386 back_time = back.time;
387 if (back.Usage() > 0) {
390 SetDParam(2, back.Usage() * 100 / (back.capacity + 1));
391 buf_end = GetString(buf, STR_LINKGRAPH_STATS_TOOLTIP_RETURN_EXTENSION,
lastof(buf));
395 const auto time = link.time ? back_time ? ((link.time + back_time) / 2) : link.time : back_time;
398 buf_end = GetString(buf_end, STR_LINKGRAPH_STATS_TOOLTIP_TIME_EXTENSION,
lastof(buf));
404 SetDParam(4, link.Usage() * 100 / (link.capacity + 1));
455 return MakeCompanyButtonRows(biggest_index, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST, COLOUR_GREY, 3, STR_NULL);
458 NWidgetBase *MakeSaturationLegendLinkGraphGUI(
int *biggest_index)
469 *biggest_index = WID_LGL_SATURATION_LAST;
473 NWidgetBase *MakeCargoesLegendLinkGraphGUI(
int *biggest_index)
479 if (i % ENTRIES_PER_ROW == 0) {
480 if (row) panel->
Add(row);
491 for (uint i = 0; i < 4 - (
NUM_CARGO - 1) % 5; ++i) {
499 *biggest_index = WID_LGL_CARGO_LAST;
504 static const NWidgetPart _nested_linkgraph_legend_widgets[] = {
534 static_assert(WID_LGL_SATURATION_LAST - WID_LGL_SATURATION_FIRST ==
538 WDP_AUTO,
"toolbar_linkgraph", 0, 0,
541 _nested_linkgraph_legend_widgets,
lengthof(_nested_linkgraph_legend_widgets)
549 AllocateWindowDescFront<LinkGraphLegendWindow>(&_linkgraph_legend_desc, 0);
552 LinkGraphLegendWindow::LinkGraphLegendWindow(
WindowDesc *desc,
int window_number) :
Window(desc)
554 this->InitNested(window_number);
555 this->InvalidateData(0);
564 this->overlay = overlay;
581 if (
IsInsideMM(widget, WID_LGL_SATURATION_FIRST, WID_LGL_SATURATION_LAST + 1)) {
583 if (widget == WID_LGL_SATURATION_FIRST) {
584 str = STR_LINKGRAPH_LEGEND_UNUSED;
585 }
else if (widget == WID_LGL_SATURATION_LAST) {
586 str = STR_LINKGRAPH_LEGEND_OVERLOADED;
587 }
else if (widget == (WID_LGL_SATURATION_LAST + WID_LGL_SATURATION_FIRST) / 2) {
588 str = STR_LINKGRAPH_LEGEND_SATURATED;
590 if (str != STR_NULL) {
592 dim.width += padding.width;
593 dim.height += padding.height;
594 *size =
maxdim(*size, dim);
597 if (
IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
601 dim.width += padding.width;
602 dim.height += padding.height;
603 *size =
maxdim(*size, dim);
612 if (
IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) {
618 if (
IsInsideMM(widget, WID_LGL_SATURATION_FIRST, WID_LGL_SATURATION_LAST + 1)) {
622 if (widget == WID_LGL_SATURATION_FIRST) {
623 str = STR_LINKGRAPH_LEGEND_UNUSED;
624 }
else if (widget == WID_LGL_SATURATION_LAST) {
625 str = STR_LINKGRAPH_LEGEND_OVERLOADED;
626 }
else if (widget == (WID_LGL_SATURATION_LAST + WID_LGL_SATURATION_FIRST) / 2) {
627 str = STR_LINKGRAPH_LEGEND_SATURATED;
629 if (str != STR_NULL) {
633 if (
IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
643 if (
IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) {
645 GuiShowTooltips(
this, STR_LINKGRAPH_LEGEND_SELECT_COMPANIES, 0,
nullptr, close_cond);
649 params[0] = STR_LINKGRAPH_LEGEND_SELECT_COMPANIES;
651 GuiShowTooltips(
this, STR_LINKGRAPH_LEGEND_COMPANY_TOOLTIP, 2, params, close_cond);
655 if (
IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
659 params[0] = cargo->
name;
697 if (
IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) {
702 }
else if (widget == WID_LGL_COMPANIES_ALL || widget == WID_LGL_COMPANIES_NONE) {
709 }
else if (
IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
714 }
else if (widget == WID_LGL_CARGOES_ALL || widget == WID_LGL_CARGOES_NONE) {
@ TC_FORCED
Ignore colour changes from strings.
uint32 company_mask
Bitmask of companies to be displayed.
static constexpr bool IsInsideMM(const T x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
GoodsEntry goods[NUM_CARGO]
Goods at this station.
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.
uint Usage() const
Get edge's usage.
static Titem * Get(size_t index)
Returns Titem with given index.
uint GetFlowVia(StationID via) const
Get the sum of flows via a specific station from this FlowStatMap.
A connected component of a link graph.
Dimensions (a width and height) of a rectangle in 2D.
CargoID cargo
Cargo type of the link.
Rect Shrink(int s) const
Copy and shrink Rect by s pixels.
TextColour GetContrastColour(uint8 background, uint8 threshold)
Determine a contrasty text colour for a coloured background.
void UpdateOverlayCompanies()
Update the overlay with the new company selection.
uint32 TravelTime() const
Get edge's average travel time.
void RebuildCache()
Rebuild the cache and recalculate which links and stations to be shown.
ViewportData * viewport
Pointer to viewport data, if present.
Tindex index
Index of this pool item.
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo ID.
Dimension maxdim(const Dimension &d1, const Dimension &d2)
Compute bounding box of both dimensions.
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
void OnInvalidateData(int data=0, bool gui_scope=true) override
Invalidate the data of this window if the cargoes or companies have changed.
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
bool OnTooltip(Point pt, int widget, TooltipCloseCondition close_cond) override
Event to display a custom tooltip.
CargoTypes GetCargoMask()
Get a bitmask of the currently shown cargoes.
static Station * Get(size_t index)
Gets station with given index.
ClientSettings _settings_client
The current settings for this game.
int DrawString(int left, int right, int top, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
void UpdateOverlayCargoes()
Update the overlay with the new cargo selection.
Specification of a cargo type.
static bool IsValidID(size_t index)
Tests whether given index is a valid index for station of this type.
byte road_side
the side of the road vehicles drive on
Owner
Enum for all companies/owners.
Owner owner
The owner of this station.
byte _colour_gradient[COLOUR_END][8]
All 16 colour gradients 8 colours per gradient from darkest (0) to lightest (7)
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Class managing the smallmap window.
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
Return the string dimension in pixels.
Wrapper for an edge (const or not) allowing retrieval, but no modification.
void SetCompanyMask(uint32 company_mask)
Set a new company mask and rebuild the cache.
Window * window
Window to be drawn into.
static Pool::IterateWrapper< Station > Iterate(size_t from=0)
Returns an iterable ensemble of all valid stations of type T.
uint32 time
Travel time of the link.
High level window description.
@ COMPANY_FIRST
First company, same as owner.
LinkMap cached_links
Cache for links to reduce recalculation.
const NWID * GetWidget(uint widnum) const
Get the nested widget with number widnum from the nested widget tree.
StationSupplyList cached_stations
Cache for stations to be drawn.
Handles drawing of links into some window.
static const uint8 LINK_COLOURS[][12]
Colours for the various "load" states of links.
@ WDP_AUTO
Find a place automatically.
Iterable ensemble of each set bit in a value.
void GuiShowTooltips(Window *parent, StringID str, uint paramcount, const uint64 params[], TooltipCloseCondition close_tooltip)
Shows a tooltip.
Rect Translate(int x, int y) const
Copy and translate Rect by x,y pixels.
void SetCargoMask(CargoTypes cargo_mask)
Set a new cargo mask and rebuild the cache.
void AddLinks(const Station *sta, const Station *stb)
Add all "interesting" links between the given stations to the cache.
StationRect rect
NOSAVE: Station spread out rectangle maintained by StationRect::xxx() functions.
void SetOverlay(LinkGraphOverlay *overlay)
Set the overlay belonging to this menu and import its company/cargo settings.
bool IsValid() const
Tests for validity of this cargospec.
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
@ FS_SMALL
Index of the small font in the font tables.
NodeID node
ID of node in link graph referring to this goods entry.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
void SetWidgetDisabledState(byte widget_index, bool disab_stat)
Sets the enabled/disabled status of a widget.
@ MAX_COMPANIES
Maximum number of companies.
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.
uint Capacity() const
Get edge's capacity.
uint capacity
Capacity of the link.
Coordinates of a point in 2D.
void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode)
Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen.
LinkGraphID link_graph
Link graph this station belongs to.
@ WC_LINKGRAPH_LEGEND
Linkgraph legend; Window numbers:
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
@ SA_HOR_CENTER
Horizontally center the text.
#define FONT_HEIGHT_SMALL
Height of characters in the small (FS_SMALL) font.
void DrawCompanyIcon(CompanyID c, int x, int y)
Draw the icon of a company.
Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
Get the size of a sprite.
uint scale
Width of link lines.
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Stores station stats for a single cargo.
bool dirty
Set if overlay should be rebuilt.
void DrawStationDots(const DrawPixelInfo *dpi) const
Draw dots for stations into the smallmap.
uint planned
Planned usage of the link.
FlowStatMap flows
Planned flows through this station.
NWidgetBase * MakeCompanyButtonRowsLinkGraphGUI(int *biggest_index)
Make a number of rows with buttons for each company for the linkgraph legend window.
void DrawWidget(const Rect &r, int widget) const override
Draw the contents of a nested widget.
void Draw(const DrawPixelInfo *dpi)
Draw the linkgraph overlay or some part of it, in the area given.
Point GetStationMiddle(const Station *st) const
Determine the middle of a station in the current window.
An iterator for const edges.
void OnClick(Point pt, int widget, int click_count) override
A click with the left mouse button has been made on the window.
@ OWNER_NONE
The tile has no ownership.
bool IsWidgetLowered(byte widget_index) const
Gets the lowered state of a widget.
@ NUM_CARGO
Maximal number of cargo types in a game.
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.
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.
StringID name
Name of this type of cargo.
@ WC_MAIN_WINDOW
Main window; Window numbers:
uint usage
Actual usage of the link.
bool shared
If this is a shared link to be drawn dashed.
StringID abbrev
Two letter abbreviation for this cargo type.
void ShowLinkGraphLegend()
Open a link graph legend window.
static T abs(const T a)
Returns the absolute value of (scalar) variable.
void GetWidgetDpi(DrawPixelInfo *dpi) const
Get a DPI for the widget we will be drawing to.
CargoTypes cargo_mask
Bitmask of cargos to be displayed.
void SetDirty()
Mark the linkgraph dirty to be rebuilt next time Draw() is called.
static int CenterBounds(int min, int max, int size)
Determine where to draw a centred object inside a widget.
void ToggleWidgetLoweredState(byte widget_index)
Invert the lowered/raised status of a widget.
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
#define lengthof(x)
Return the length of an fixed size array.
static Station * GetIfValid(size_t index)
Returns station if the index is a valid index for this station type.
byte CargoID
Cargo slots to indicate a cargo type within a game.
uint32 GetCompanyMask()
Get a bitmask of the currently shown companies.
uint8 linkgraph_colours
linkgraph overlay colours
void DrawContent(Point pta, Point ptb, const LinkProperties &cargo) const
Draw one specific link.
static uint CeilDiv(uint a, uint b)
Computes ceil(a / b) for non-negative a and b.
const uint widget_id
ID of Widget in Window to be drawn to.
VehicleSettings vehicle
options for vehicles
Data structure for an opened window.
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
void DrawLinks(const DrawPixelInfo *dpi) const
Draw the cached links or part of them into the given area.
bool IsWidgetDisabled(byte widget_index) const
Gets the enabled/disabled status of a widget.
Specification of a rectangle with absolute coordinates of all edges.
ConstEdgeIterator End() const
Get an iterator pointing beyond the end of the edges array.
static void DrawVertex(int x, int y, int size, int colour, int border_colour)
Draw a square symbolizing a producer of cargo.
#define lastof(x)
Get the last element of an fixed size array.
uint Usage() const
Return the usage of the link to display.
void SetWidgetLoweredState(byte widget_index, bool lowered_stat)
Sets the lowered/raised status of a widget.
static const int DAY_TICKS
1 day is 74 ticks; _date_fract used to be uint16 and incremented by 885.
ConstEdgeIterator Begin() const
Get an iterator pointing to the start of the edges array.
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
GUISettings gui
settings related to the GUI
uint Monthly(uint base) const
Scale a value to its monthly equivalent, based on last compression.
Data about how and where to blit pixels.
uint Supply() const
Get supply of wrapped node.
Monthly statistics for a link between two stations.