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/>.
8 /** @file linkgraph_gui.cpp Implementation of linkgraph overlay GUI. */
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 "../smallmap_gui.h"
18 #include "../core/geometry_func.hpp"
19 #include "../widgets/link_graph_legend_widget.h"
21 #include "table/strings.h"
23 #include "../safeguards.h"
26 * Colours for the various "load" states of links. Ordered from "unused" to
29 const uint8
LinkGraphOverlay::LINK_COLOURS
[] = {
30 0x0f, 0xd1, 0xd0, 0x57,
31 0x55, 0x53, 0xbf, 0xbd,
32 0xba, 0xb9, 0xb7, 0xb5
36 * Get a DPI for the widget we will be drawing to.
37 * @param dpi DrawPixelInfo to fill with the desired dimensions.
39 void LinkGraphOverlay::GetWidgetDpi(DrawPixelInfo
*dpi
) const
41 const NWidgetBase
*wi
= this->window
->GetWidget
<NWidgetBase
>(this->widget_id
);
42 dpi
->left
= dpi
->top
= 0;
43 dpi
->width
= wi
->current_x
;
44 dpi
->height
= wi
->current_y
;
48 * Rebuild the cache and recalculate which links and stations to be shown.
50 void LinkGraphOverlay::RebuildCache()
52 this->cached_links
.clear();
53 this->cached_stations
.clear();
54 if (this->company_mask
== 0) return;
57 this->GetWidgetDpi(&dpi
);
59 for (const Station
*sta
: Station::Iterate()) {
60 if (sta
->rect
.IsEmpty()) continue;
62 Point pta
= this->GetStationMiddle(sta
);
64 StationID from
= sta
->index
;
65 StationLinkMap
&seen_links
= this->cached_links
[from
];
68 for (CargoID c
: SetCargoBitIterator(this->cargo_mask
)) {
69 if (!CargoSpec::Get(c
)->IsValid()) continue;
70 if (!LinkGraph::IsValidID(sta
->goods
[c
].link_graph
)) continue;
71 const LinkGraph
&lg
= *LinkGraph::Get(sta
->goods
[c
].link_graph
);
73 ConstNode from_node
= lg
[sta
->goods
[c
].node
];
74 supply
+= lg
.Monthly(from_node
.Supply());
75 for (ConstEdgeIterator i
= from_node
.Begin(); i
!= from_node
.End(); ++i
) {
76 StationID to
= lg
[i
->first
].Station();
78 if (!Station::IsValidID(to
) || seen_links
.find(to
) != seen_links
.end()) {
81 const Station
*stb
= Station::Get(to
);
84 /* Show links between stations of selected companies or "neutral" ones like oilrigs. */
85 if (stb
->owner
!= OWNER_NONE
&& sta
->owner
!= OWNER_NONE
&& !HasBit(this->company_mask
, stb
->owner
)) continue;
86 if (stb
->rect
.IsEmpty()) continue;
88 if (!this->IsLinkVisible(pta
, this->GetStationMiddle(stb
), &dpi
)) continue;
90 this->AddLinks(sta
, stb
);
91 seen_links
[to
]; // make sure it is created and marked as seen
94 if (this->IsPointVisible(pta
, &dpi
)) {
95 this->cached_stations
.push_back(std::make_pair(from
, supply
));
101 * Determine if a certain point is inside the given DPI, with some lee way.
102 * @param pt Point we are looking for.
103 * @param dpi Visible area.
104 * @param padding Extent of the point.
105 * @return If the point or any of its 'extent' is inside the dpi.
107 inline bool LinkGraphOverlay::IsPointVisible(Point pt
, const DrawPixelInfo
*dpi
, int padding
) const
109 return pt
.x
> dpi
->left
- padding
&& pt
.y
> dpi
->top
- padding
&&
110 pt
.x
< dpi
->left
+ dpi
->width
+ padding
&&
111 pt
.y
< dpi
->top
+ dpi
->height
+ padding
;
115 * Determine if a certain link crosses through the area given by the dpi with some lee way.
116 * @param pta First end of the link.
117 * @param ptb Second end of the link.
118 * @param dpi Visible area.
119 * @param padding Width or thickness of the link.
120 * @return If the link or any of its "thickness" is visible. This may return false positives.
122 inline bool LinkGraphOverlay::IsLinkVisible(Point pta
, Point ptb
, const DrawPixelInfo
*dpi
, int padding
) const
124 const int left
= dpi
->left
- padding
;
125 const int right
= dpi
->left
+ dpi
->width
+ padding
;
126 const int top
= dpi
->top
- padding
;
127 const int bottom
= dpi
->top
+ dpi
->height
+ padding
;
130 * This method is an implementation of the Cohen-Sutherland line-clipping algorithm.
131 * See: https://en.wikipedia.org/wiki/Cohen%E2%80%93Sutherland_algorithm
134 const uint8 INSIDE
= 0; // 0000
135 const uint8 LEFT
= 1; // 0001
136 const uint8 RIGHT
= 2; // 0010
137 const uint8 BOTTOM
= 4; // 0100
138 const uint8 TOP
= 8; // 1000
145 auto out_code
= [&](int x
, int y
) -> uint8
{
149 } else if (x
> right
) {
154 } else if (y
> bottom
) {
160 uint8 c0
= out_code(x0
, y0
);
161 uint8 c1
= out_code(x1
, y1
);
164 if (c0
== 0 || c1
== 0) return true;
165 if ((c0
& c1
) != 0) return false;
167 if (c0
& TOP
) { // point 0 is above the clip window
168 x0
= x0
+ (int)(((int64
) (x1
- x0
)) * ((int64
) (top
- y0
)) / ((int64
) (y1
- y0
)));
170 } else if (c0
& BOTTOM
) { // point 0 is below the clip window
171 x0
= x0
+ (int)(((int64
) (x1
- x0
)) * ((int64
) (bottom
- y0
)) / ((int64
) (y1
- y0
)));
173 } else if (c0
& RIGHT
) { // point 0 is to the right of clip window
174 y0
= y0
+ (int)(((int64
) (y1
- y0
)) * ((int64
) (right
- x0
)) / ((int64
) (x1
- x0
)));
176 } else if (c0
& LEFT
) { // point 0 is to the left of clip window
177 y0
= y0
+ (int)(((int64
) (y1
- y0
)) * ((int64
) (left
- x0
)) / ((int64
) (x1
- x0
)));
181 c0
= out_code(x0
, y0
);
188 * Add all "interesting" links between the given stations to the cache.
189 * @param from The source station.
190 * @param to The destination station.
192 void LinkGraphOverlay::AddLinks(const Station
*from
, const Station
*to
)
194 for (CargoID c
: SetCargoBitIterator(this->cargo_mask
)) {
195 if (!CargoSpec::Get(c
)->IsValid()) continue;
196 const GoodsEntry
&ge
= from
->goods
[c
];
197 if (!LinkGraph::IsValidID(ge
.link_graph
) ||
198 ge
.link_graph
!= to
->goods
[c
].link_graph
) {
201 const LinkGraph
&lg
= *LinkGraph::Get(ge
.link_graph
);
202 ConstEdge edge
= lg
[ge
.node
][to
->goods
[c
].node
];
203 if (edge
.Capacity() > 0) {
204 this->AddStats(lg
.Monthly(edge
.Capacity()), lg
.Monthly(edge
.Usage()),
205 ge
.flows
.GetFlowVia(to
->index
), from
->owner
== OWNER_NONE
|| to
->owner
== OWNER_NONE
,
206 this->cached_links
[from
->index
][to
->index
]);
212 * Add information from a given pair of link stat and flow stat to the given
213 * link properties. The shown usage or plan is always the maximum of all link
215 * @param new_cap Capacity of the new link.
216 * @param new_usg Usage of the new link.
217 * @param new_plan Planned flow for the new link.
218 * @param new_shared If the new link is shared.
219 * @param cargo LinkProperties to write the information to.
221 /* static */ void LinkGraphOverlay::AddStats(uint new_cap
, uint new_usg
, uint new_plan
, bool new_shared
, LinkProperties
&cargo
)
223 /* multiply the numbers by 32 in order to avoid comparing to 0 too often. */
224 if (cargo
.capacity
== 0 ||
225 std::max(cargo
.usage
, cargo
.planned
) * 32 / (cargo
.capacity
+ 1) < std::max(new_usg
, new_plan
) * 32 / (new_cap
+ 1)) {
226 cargo
.capacity
= new_cap
;
227 cargo
.usage
= new_usg
;
228 cargo
.planned
= new_plan
;
230 if (new_shared
) cargo
.shared
= true;
234 * Draw the linkgraph overlay or some part of it, in the area given.
235 * @param dpi Area to be drawn to.
237 void LinkGraphOverlay::Draw(const DrawPixelInfo
*dpi
)
240 this->RebuildCache();
243 this->DrawLinks(dpi
);
244 this->DrawStationDots(dpi
);
248 * Draw the cached links or part of them into the given area.
249 * @param dpi Area to be drawn to.
251 void LinkGraphOverlay::DrawLinks(const DrawPixelInfo
*dpi
) const
253 for (LinkMap::const_iterator
i(this->cached_links
.begin()); i
!= this->cached_links
.end(); ++i
) {
254 if (!Station::IsValidID(i
->first
)) continue;
255 Point pta
= this->GetStationMiddle(Station::Get(i
->first
));
256 for (StationLinkMap::const_iterator
j(i
->second
.begin()); j
!= i
->second
.end(); ++j
) {
257 if (!Station::IsValidID(j
->first
)) continue;
258 Point ptb
= this->GetStationMiddle(Station::Get(j
->first
));
259 if (!this->IsLinkVisible(pta
, ptb
, dpi
, this->scale
+ 2)) continue;
260 this->DrawContent(pta
, ptb
, j
->second
);
266 * Draw one specific link.
267 * @param pta Source of the link.
268 * @param ptb Destination of the link.
269 * @param cargo Properties of the link.
271 void LinkGraphOverlay::DrawContent(Point pta
, Point ptb
, const LinkProperties
&cargo
) const
273 uint usage_or_plan
= std::min(cargo
.capacity
* 2 + 1, std::max(cargo
.usage
, cargo
.planned
));
274 int colour
= LinkGraphOverlay::LINK_COLOURS
[usage_or_plan
* lengthof(LinkGraphOverlay::LINK_COLOURS
) / (cargo
.capacity
* 2 + 2)];
275 int dash
= cargo
.shared
? this->scale
* 4 : 0;
277 /* Move line a bit 90° against its dominant direction to prevent it from
278 * being hidden below the grey line. */
279 int side
= _settings_game
.vehicle
.road_side
? 1 : -1;
280 if (abs(pta
.x
- ptb
.x
) < abs(pta
.y
- ptb
.y
)) {
281 int offset_x
= (pta
.y
> ptb
.y
? 1 : -1) * side
* this->scale
;
282 GfxDrawLine(pta
.x
+ offset_x
, pta
.y
, ptb
.x
+ offset_x
, ptb
.y
, colour
, this->scale
, dash
);
284 int offset_y
= (pta
.x
< ptb
.x
? 1 : -1) * side
* this->scale
;
285 GfxDrawLine(pta
.x
, pta
.y
+ offset_y
, ptb
.x
, ptb
.y
+ offset_y
, colour
, this->scale
, dash
);
288 GfxDrawLine(pta
.x
, pta
.y
, ptb
.x
, ptb
.y
, _colour_gradient
[COLOUR_GREY
][1], this->scale
);
292 * Draw dots for stations into the smallmap. The dots' sizes are determined by the amount of
293 * cargo produced there, their colours by the type of cargo produced.
295 void LinkGraphOverlay::DrawStationDots(const DrawPixelInfo
*dpi
) const
297 for (StationSupplyList::const_iterator
i(this->cached_stations
.begin()); i
!= this->cached_stations
.end(); ++i
) {
298 const Station
*st
= Station::GetIfValid(i
->first
);
299 if (st
== nullptr) continue;
300 Point pt
= this->GetStationMiddle(st
);
301 if (!this->IsPointVisible(pt
, dpi
, 3 * this->scale
)) continue;
303 uint r
= this->scale
* 2 + this->scale
* 2 * std::min(200U, i
->second
) / 200;
305 LinkGraphOverlay::DrawVertex(pt
.x
, pt
.y
, r
,
306 _colour_gradient
[st
->owner
!= OWNER_NONE
?
307 (Colours
)Company::Get(st
->owner
)->colour
: COLOUR_GREY
][5],
308 _colour_gradient
[COLOUR_GREY
][1]);
313 * Draw a square symbolizing a producer of cargo.
314 * @param x X coordinate of the middle of the vertex.
315 * @param y Y coordinate of the middle of the vertex.
316 * @param size Y and y extend of the vertex.
317 * @param colour Colour with which the vertex will be filled.
318 * @param border_colour Colour for the border of the vertex.
320 /* static */ void LinkGraphOverlay::DrawVertex(int x
, int y
, int size
, int colour
, int border_colour
)
324 int w2
= size
/ 2 + size
% 2;
326 GfxFillRect(x
- w1
, y
- w1
, x
+ w2
, y
+ w2
, colour
);
330 GfxDrawLine(x
- w1
, y
- w1
, x
+ w2
, y
- w1
, border_colour
);
331 GfxDrawLine(x
- w1
, y
+ w2
, x
+ w2
, y
+ w2
, border_colour
);
332 GfxDrawLine(x
- w1
, y
- w1
, x
- w1
, y
+ w2
, border_colour
);
333 GfxDrawLine(x
+ w2
, y
- w1
, x
+ w2
, y
+ w2
, border_colour
);
337 * Determine the middle of a station in the current window.
338 * @param st The station we're looking for.
339 * @return Middle point of the station in the current window.
341 Point
LinkGraphOverlay::GetStationMiddle(const Station
*st
) const
343 if (this->window
->viewport
!= nullptr) {
344 return GetViewportStationMiddle(this->window
->viewport
, st
);
346 /* assume this is a smallmap */
347 return static_cast<const SmallMapWindow
*>(this->window
)->GetStationMiddle(st
);
352 * Set a new cargo mask and rebuild the cache.
353 * @param cargo_mask New cargo mask.
355 void LinkGraphOverlay::SetCargoMask(CargoTypes cargo_mask
)
357 this->cargo_mask
= cargo_mask
;
358 this->RebuildCache();
359 this->window
->GetWidget
<NWidgetBase
>(this->widget_id
)->SetDirty(this->window
);
363 * Set a new company mask and rebuild the cache.
364 * @param company_mask New company mask.
366 void LinkGraphOverlay::SetCompanyMask(uint32 company_mask
)
368 this->company_mask
= company_mask
;
369 this->RebuildCache();
370 this->window
->GetWidget
<NWidgetBase
>(this->widget_id
)->SetDirty(this->window
);
373 /** Make a number of rows with buttons for each company for the linkgraph legend window. */
374 NWidgetBase
*MakeCompanyButtonRowsLinkGraphGUI(int *biggest_index
)
376 return MakeCompanyButtonRows(biggest_index
, WID_LGL_COMPANY_FIRST
, WID_LGL_COMPANY_LAST
, COLOUR_GREY
, 3, STR_NULL
);
379 NWidgetBase
*MakeSaturationLegendLinkGraphGUI(int *biggest_index
)
381 NWidgetVertical
*panel
= new NWidgetVertical(NC_EQUALSIZE
);
382 for (uint i
= 0; i
< lengthof(LinkGraphOverlay::LINK_COLOURS
); ++i
) {
383 NWidgetBackground
* wid
= new NWidgetBackground(WWT_PANEL
, COLOUR_DARK_GREEN
, i
+ WID_LGL_SATURATION_FIRST
);
384 wid
->SetMinimalSize(50, 0);
385 wid
->SetMinimalTextLines(1, 0, FS_SMALL
);
387 wid
->SetResize(0, 0);
390 *biggest_index
= WID_LGL_SATURATION_LAST
;
394 NWidgetBase
*MakeCargoesLegendLinkGraphGUI(int *biggest_index
)
396 static const uint ENTRIES_PER_ROW
= CeilDiv(NUM_CARGO
, 5);
397 NWidgetVertical
*panel
= new NWidgetVertical(NC_EQUALSIZE
);
398 NWidgetHorizontal
*row
= nullptr;
399 for (uint i
= 0; i
< NUM_CARGO
; ++i
) {
400 if (i
% ENTRIES_PER_ROW
== 0) {
401 if (row
) panel
->Add(row
);
402 row
= new NWidgetHorizontal(NC_EQUALSIZE
);
404 NWidgetBackground
* wid
= new NWidgetBackground(WWT_PANEL
, COLOUR_GREY
, i
+ WID_LGL_CARGO_FIRST
);
405 wid
->SetMinimalSize(25, 0);
406 wid
->SetMinimalTextLines(1, 0, FS_SMALL
);
408 wid
->SetResize(0, 0);
411 /* Fill up last row */
412 for (uint i
= 0; i
< 4 - (NUM_CARGO
- 1) % 5; ++i
) {
413 NWidgetSpacer
*spc
= new NWidgetSpacer(25, 0);
414 spc
->SetMinimalTextLines(1, 0, FS_SMALL
);
416 spc
->SetResize(0, 0);
420 *biggest_index
= WID_LGL_CARGO_LAST
;
425 static const NWidgetPart _nested_linkgraph_legend_widgets
[] = {
426 NWidget(NWID_HORIZONTAL
),
427 NWidget(WWT_CLOSEBOX
, COLOUR_DARK_GREEN
),
428 NWidget(WWT_CAPTION
, COLOUR_DARK_GREEN
, WID_LGL_CAPTION
), SetDataTip(STR_LINKGRAPH_LEGEND_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
429 NWidget(WWT_SHADEBOX
, COLOUR_DARK_GREEN
),
430 NWidget(WWT_STICKYBOX
, COLOUR_DARK_GREEN
),
432 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
),
433 NWidget(NWID_HORIZONTAL
),
434 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_LGL_SATURATION
),
435 SetPadding(WD_FRAMERECT_TOP
, 0, WD_FRAMERECT_BOTTOM
, WD_CAPTIONTEXT_LEFT
),
436 NWidgetFunction(MakeSaturationLegendLinkGraphGUI
),
438 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_LGL_COMPANIES
),
439 SetPadding(WD_FRAMERECT_TOP
, 0, WD_FRAMERECT_BOTTOM
, WD_CAPTIONTEXT_LEFT
),
440 NWidget(NWID_VERTICAL
, NC_EQUALSIZE
),
441 NWidgetFunction(MakeCompanyButtonRowsLinkGraphGUI
),
442 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_LGL_COMPANIES_ALL
), SetDataTip(STR_LINKGRAPH_LEGEND_ALL
, STR_NULL
),
443 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_LGL_COMPANIES_NONE
), SetDataTip(STR_LINKGRAPH_LEGEND_NONE
, STR_NULL
),
446 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_LGL_CARGOES
),
447 SetPadding(WD_FRAMERECT_TOP
, WD_FRAMERECT_RIGHT
, WD_FRAMERECT_BOTTOM
, WD_CAPTIONTEXT_LEFT
),
448 NWidget(NWID_VERTICAL
, NC_EQUALSIZE
),
449 NWidgetFunction(MakeCargoesLegendLinkGraphGUI
),
450 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_LGL_CARGOES_ALL
), SetDataTip(STR_LINKGRAPH_LEGEND_ALL
, STR_NULL
),
451 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_LGL_CARGOES_NONE
), SetDataTip(STR_LINKGRAPH_LEGEND_NONE
, STR_NULL
),
458 static_assert(WID_LGL_SATURATION_LAST
- WID_LGL_SATURATION_FIRST
==
459 lengthof(LinkGraphOverlay::LINK_COLOURS
) - 1);
461 static WindowDesc
_linkgraph_legend_desc(
462 WDP_AUTO
, "toolbar_linkgraph", 0, 0,
463 WC_LINKGRAPH_LEGEND
, WC_NONE
,
465 _nested_linkgraph_legend_widgets
, lengthof(_nested_linkgraph_legend_widgets
)
469 * Open a link graph legend window.
471 void ShowLinkGraphLegend()
473 AllocateWindowDescFront
<LinkGraphLegendWindow
>(&_linkgraph_legend_desc
, 0);
476 LinkGraphLegendWindow::LinkGraphLegendWindow(WindowDesc
*desc
, int window_number
) : Window(desc
)
478 this->InitNested(window_number
);
479 this->InvalidateData(0);
480 this->SetOverlay(FindWindowById(WC_MAIN_WINDOW
, 0)->viewport
->overlay
);
484 * Set the overlay belonging to this menu and import its company/cargo settings.
485 * @param overlay New overlay for this menu.
487 void LinkGraphLegendWindow::SetOverlay(LinkGraphOverlay
*overlay
) {
488 this->overlay
= overlay
;
489 uint32 companies
= this->overlay
->GetCompanyMask();
490 for (uint c
= 0; c
< MAX_COMPANIES
; c
++) {
491 if (!this->IsWidgetDisabled(WID_LGL_COMPANY_FIRST
+ c
)) {
492 this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST
+ c
, HasBit(companies
, c
));
495 CargoTypes cargoes
= this->overlay
->GetCargoMask();
496 for (uint c
= 0; c
< NUM_CARGO
; c
++) {
497 if (!this->IsWidgetDisabled(WID_LGL_CARGO_FIRST
+ c
)) {
498 this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST
+ c
, HasBit(cargoes
, c
));
503 void LinkGraphLegendWindow::UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
505 if (IsInsideMM(widget
, WID_LGL_SATURATION_FIRST
, WID_LGL_SATURATION_LAST
+ 1)) {
506 StringID str
= STR_NULL
;
507 if (widget
== WID_LGL_SATURATION_FIRST
) {
508 str
= STR_LINKGRAPH_LEGEND_UNUSED
;
509 } else if (widget
== WID_LGL_SATURATION_LAST
) {
510 str
= STR_LINKGRAPH_LEGEND_OVERLOADED
;
511 } else if (widget
== (WID_LGL_SATURATION_LAST
+ WID_LGL_SATURATION_FIRST
) / 2) {
512 str
= STR_LINKGRAPH_LEGEND_SATURATED
;
514 if (str
!= STR_NULL
) {
515 Dimension dim
= GetStringBoundingBox(str
);
516 dim
.width
+= WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
517 dim
.height
+= WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
518 *size
= maxdim(*size
, dim
);
521 if (IsInsideMM(widget
, WID_LGL_CARGO_FIRST
, WID_LGL_CARGO_LAST
+ 1)) {
522 CargoSpec
*cargo
= CargoSpec::Get(widget
- WID_LGL_CARGO_FIRST
);
523 if (cargo
->IsValid()) {
524 Dimension dim
= GetStringBoundingBox(cargo
->abbrev
);
525 dim
.width
+= WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
526 dim
.height
+= WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
527 *size
= maxdim(*size
, dim
);
532 void LinkGraphLegendWindow::DrawWidget(const Rect
&r
, int widget
) const
534 if (IsInsideMM(widget
, WID_LGL_COMPANY_FIRST
, WID_LGL_COMPANY_LAST
+ 1)) {
535 if (this->IsWidgetDisabled(widget
)) return;
536 CompanyID cid
= (CompanyID
)(widget
- WID_LGL_COMPANY_FIRST
);
537 Dimension sprite_size
= GetSpriteSize(SPR_COMPANY_ICON
);
538 DrawCompanyIcon(cid
, (r
.left
+ r
.right
+ 1 - sprite_size
.width
) / 2, (r
.top
+ r
.bottom
+ 1 - sprite_size
.height
) / 2);
540 if (IsInsideMM(widget
, WID_LGL_SATURATION_FIRST
, WID_LGL_SATURATION_LAST
+ 1)) {
541 GfxFillRect(r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.bottom
- 1, LinkGraphOverlay::LINK_COLOURS
[widget
- WID_LGL_SATURATION_FIRST
]);
542 StringID str
= STR_NULL
;
543 if (widget
== WID_LGL_SATURATION_FIRST
) {
544 str
= STR_LINKGRAPH_LEGEND_UNUSED
;
545 } else if (widget
== WID_LGL_SATURATION_LAST
) {
546 str
= STR_LINKGRAPH_LEGEND_OVERLOADED
;
547 } else if (widget
== (WID_LGL_SATURATION_LAST
+ WID_LGL_SATURATION_FIRST
) / 2) {
548 str
= STR_LINKGRAPH_LEGEND_SATURATED
;
550 if (str
!= STR_NULL
) DrawString(r
.left
, r
.right
, (r
.top
+ r
.bottom
+ 1 - FONT_HEIGHT_SMALL
) / 2, str
, TC_FROMSTRING
, SA_HOR_CENTER
);
552 if (IsInsideMM(widget
, WID_LGL_CARGO_FIRST
, WID_LGL_CARGO_LAST
+ 1)) {
553 if (this->IsWidgetDisabled(widget
)) return;
554 CargoSpec
*cargo
= CargoSpec::Get(widget
- WID_LGL_CARGO_FIRST
);
555 GfxFillRect(r
.left
+ 2, r
.top
+ 2, r
.right
- 2, r
.bottom
- 2, cargo
->legend_colour
);
556 DrawString(r
.left
, r
.right
, (r
.top
+ r
.bottom
+ 1 - FONT_HEIGHT_SMALL
) / 2, cargo
->abbrev
, GetContrastColour(cargo
->legend_colour
, 73), SA_HOR_CENTER
);
560 bool LinkGraphLegendWindow::OnTooltip(Point pt
, int widget
, TooltipCloseCondition close_cond
)
562 if (IsInsideMM(widget
, WID_LGL_COMPANY_FIRST
, WID_LGL_COMPANY_LAST
+ 1)) {
563 if (this->IsWidgetDisabled(widget
)) {
564 GuiShowTooltips(this, STR_LINKGRAPH_LEGEND_SELECT_COMPANIES
, 0, nullptr, close_cond
);
567 CompanyID cid
= (CompanyID
)(widget
- WID_LGL_COMPANY_FIRST
);
568 params
[0] = STR_LINKGRAPH_LEGEND_SELECT_COMPANIES
;
570 GuiShowTooltips(this, STR_LINKGRAPH_LEGEND_COMPANY_TOOLTIP
, 2, params
, close_cond
);
574 if (IsInsideMM(widget
, WID_LGL_CARGO_FIRST
, WID_LGL_CARGO_LAST
+ 1)) {
575 if (this->IsWidgetDisabled(widget
)) return false;
576 CargoSpec
*cargo
= CargoSpec::Get(widget
- WID_LGL_CARGO_FIRST
);
578 params
[0] = cargo
->name
;
579 GuiShowTooltips(this, STR_BLACK_STRING
, 1, params
, close_cond
);
586 * Update the overlay with the new company selection.
588 void LinkGraphLegendWindow::UpdateOverlayCompanies()
591 for (uint c
= 0; c
< MAX_COMPANIES
; c
++) {
592 if (this->IsWidgetDisabled(c
+ WID_LGL_COMPANY_FIRST
)) continue;
593 if (!this->IsWidgetLowered(c
+ WID_LGL_COMPANY_FIRST
)) continue;
596 this->overlay
->SetCompanyMask(mask
);
600 * Update the overlay with the new cargo selection.
602 void LinkGraphLegendWindow::UpdateOverlayCargoes()
605 for (uint c
= 0; c
< NUM_CARGO
; c
++) {
606 if (this->IsWidgetDisabled(c
+ WID_LGL_CARGO_FIRST
)) continue;
607 if (!this->IsWidgetLowered(c
+ WID_LGL_CARGO_FIRST
)) continue;
610 this->overlay
->SetCargoMask(mask
);
613 void LinkGraphLegendWindow::OnClick(Point pt
, int widget
, int click_count
)
615 /* Check which button is clicked */
616 if (IsInsideMM(widget
, WID_LGL_COMPANY_FIRST
, WID_LGL_COMPANY_LAST
+ 1)) {
617 if (!this->IsWidgetDisabled(widget
)) {
618 this->ToggleWidgetLoweredState(widget
);
619 this->UpdateOverlayCompanies();
621 } else if (widget
== WID_LGL_COMPANIES_ALL
|| widget
== WID_LGL_COMPANIES_NONE
) {
622 for (uint c
= 0; c
< MAX_COMPANIES
; c
++) {
623 if (this->IsWidgetDisabled(c
+ WID_LGL_COMPANY_FIRST
)) continue;
624 this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST
+ c
, widget
== WID_LGL_COMPANIES_ALL
);
626 this->UpdateOverlayCompanies();
628 } else if (IsInsideMM(widget
, WID_LGL_CARGO_FIRST
, WID_LGL_CARGO_LAST
+ 1)) {
629 if (!this->IsWidgetDisabled(widget
)) {
630 this->ToggleWidgetLoweredState(widget
);
631 this->UpdateOverlayCargoes();
633 } else if (widget
== WID_LGL_CARGOES_ALL
|| widget
== WID_LGL_CARGOES_NONE
) {
634 for (uint c
= 0; c
< NUM_CARGO
; c
++) {
635 if (this->IsWidgetDisabled(c
+ WID_LGL_CARGO_FIRST
)) continue;
636 this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST
+ c
, widget
== WID_LGL_CARGOES_ALL
);
638 this->UpdateOverlayCargoes();
644 * Invalidate the data of this window if the cargoes or companies have changed.
645 * @param data ignored
646 * @param gui_scope ignored
648 void LinkGraphLegendWindow::OnInvalidateData(int data
, bool gui_scope
)
650 /* Disable the companies who are not active */
651 for (CompanyID i
= COMPANY_FIRST
; i
< MAX_COMPANIES
; i
++) {
652 this->SetWidgetDisabledState(i
+ WID_LGL_COMPANY_FIRST
, !Company::IsValidID(i
));
654 for (CargoID i
= 0; i
< NUM_CARGO
; i
++) {
655 this->SetWidgetDisabledState(i
+ WID_LGL_CARGO_FIRST
, !CargoSpec::Get(i
)->IsValid());