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
];
69 FOR_EACH_SET_CARGO_ID(c
, this->cargo_mask
) {
70 if (!CargoSpec::Get(c
)->IsValid()) continue;
71 if (!LinkGraph::IsValidID(sta
->goods
[c
].link_graph
)) continue;
72 const LinkGraph
&lg
= *LinkGraph::Get(sta
->goods
[c
].link_graph
);
74 ConstNode from_node
= lg
[sta
->goods
[c
].node
];
75 supply
+= lg
.Monthly(from_node
.Supply());
76 for (ConstEdgeIterator i
= from_node
.Begin(); i
!= from_node
.End(); ++i
) {
77 StationID to
= lg
[i
->first
].Station();
79 if (!Station::IsValidID(to
) || seen_links
.find(to
) != seen_links
.end()) {
82 const Station
*stb
= Station::Get(to
);
85 /* Show links between stations of selected companies or "neutral" ones like oilrigs. */
86 if (stb
->owner
!= OWNER_NONE
&& sta
->owner
!= OWNER_NONE
&& !HasBit(this->company_mask
, stb
->owner
)) continue;
87 if (stb
->rect
.IsEmpty()) continue;
89 if (!this->IsLinkVisible(pta
, this->GetStationMiddle(stb
), &dpi
)) continue;
91 this->AddLinks(sta
, stb
);
92 seen_links
[to
]; // make sure it is created and marked as seen
95 if (this->IsPointVisible(pta
, &dpi
)) {
96 this->cached_stations
.push_back(std::make_pair(from
, supply
));
102 * Determine if a certain point is inside the given DPI, with some lee way.
103 * @param pt Point we are looking for.
104 * @param dpi Visible area.
105 * @param padding Extent of the point.
106 * @return If the point or any of its 'extent' is inside the dpi.
108 inline bool LinkGraphOverlay::IsPointVisible(Point pt
, const DrawPixelInfo
*dpi
, int padding
) const
110 return pt
.x
> dpi
->left
- padding
&& pt
.y
> dpi
->top
- padding
&&
111 pt
.x
< dpi
->left
+ dpi
->width
+ padding
&&
112 pt
.y
< dpi
->top
+ dpi
->height
+ padding
;
116 * Determine if a certain link crosses through the area given by the dpi with some lee way.
117 * @param pta First end of the link.
118 * @param ptb Second end of the link.
119 * @param dpi Visible area.
120 * @param padding Width or thickness of the link.
121 * @return If the link or any of its "thickness" is visible. This may return false positives.
123 inline bool LinkGraphOverlay::IsLinkVisible(Point pta
, Point ptb
, const DrawPixelInfo
*dpi
, int padding
) const
125 const int left
= dpi
->left
- padding
;
126 const int right
= dpi
->left
+ dpi
->width
+ padding
;
127 const int top
= dpi
->top
- padding
;
128 const int bottom
= dpi
->top
+ dpi
->height
+ padding
;
131 * This method is an implementation of the Cohen-Sutherland line-clipping algorithm.
132 * See: https://en.wikipedia.org/wiki/Cohen%E2%80%93Sutherland_algorithm
135 const uint8 INSIDE
= 0; // 0000
136 const uint8 LEFT
= 1; // 0001
137 const uint8 RIGHT
= 2; // 0010
138 const uint8 BOTTOM
= 4; // 0100
139 const uint8 TOP
= 8; // 1000
146 auto out_code
= [&](int x
, int y
) -> uint8
{
150 } else if (x
> right
) {
155 } else if (y
> bottom
) {
161 uint8 c0
= out_code(x0
, y0
);
162 uint8 c1
= out_code(x1
, y1
);
165 if (c0
== 0 || c1
== 0) return true;
166 if ((c0
& c1
) != 0) return false;
168 if (c0
& TOP
) { // point 0 is above the clip window
169 x0
= x0
+ (int)(((int64
) (x1
- x0
)) * ((int64
) (top
- y0
)) / ((int64
) (y1
- y0
)));
171 } else if (c0
& BOTTOM
) { // point 0 is below the clip window
172 x0
= x0
+ (int)(((int64
) (x1
- x0
)) * ((int64
) (bottom
- y0
)) / ((int64
) (y1
- y0
)));
174 } else if (c0
& RIGHT
) { // point 0 is to the right of clip window
175 y0
= y0
+ (int)(((int64
) (y1
- y0
)) * ((int64
) (right
- x0
)) / ((int64
) (x1
- x0
)));
177 } else if (c0
& LEFT
) { // point 0 is to the left of clip window
178 y0
= y0
+ (int)(((int64
) (y1
- y0
)) * ((int64
) (left
- x0
)) / ((int64
) (x1
- x0
)));
182 c0
= out_code(x0
, y0
);
189 * Add all "interesting" links between the given stations to the cache.
190 * @param from The source station.
191 * @param to The destination station.
193 void LinkGraphOverlay::AddLinks(const Station
*from
, const Station
*to
)
196 FOR_EACH_SET_CARGO_ID(c
, this->cargo_mask
) {
197 if (!CargoSpec::Get(c
)->IsValid()) continue;
198 const GoodsEntry
&ge
= from
->goods
[c
];
199 if (!LinkGraph::IsValidID(ge
.link_graph
) ||
200 ge
.link_graph
!= to
->goods
[c
].link_graph
) {
203 const LinkGraph
&lg
= *LinkGraph::Get(ge
.link_graph
);
204 ConstEdge edge
= lg
[ge
.node
][to
->goods
[c
].node
];
205 if (edge
.Capacity() > 0) {
206 this->AddStats(lg
.Monthly(edge
.Capacity()), lg
.Monthly(edge
.Usage()),
207 ge
.flows
.GetFlowVia(to
->index
), from
->owner
== OWNER_NONE
|| to
->owner
== OWNER_NONE
,
208 this->cached_links
[from
->index
][to
->index
]);
214 * Add information from a given pair of link stat and flow stat to the given
215 * link properties. The shown usage or plan is always the maximum of all link
217 * @param new_cap Capacity of the new link.
218 * @param new_usg Usage of the new link.
219 * @param new_plan Planned flow for the new link.
220 * @param new_shared If the new link is shared.
221 * @param cargo LinkProperties to write the information to.
223 /* static */ void LinkGraphOverlay::AddStats(uint new_cap
, uint new_usg
, uint new_plan
, bool new_shared
, LinkProperties
&cargo
)
225 /* multiply the numbers by 32 in order to avoid comparing to 0 too often. */
226 if (cargo
.capacity
== 0 ||
227 std::max(cargo
.usage
, cargo
.planned
) * 32 / (cargo
.capacity
+ 1) < std::max(new_usg
, new_plan
) * 32 / (new_cap
+ 1)) {
228 cargo
.capacity
= new_cap
;
229 cargo
.usage
= new_usg
;
230 cargo
.planned
= new_plan
;
232 if (new_shared
) cargo
.shared
= true;
236 * Draw the linkgraph overlay or some part of it, in the area given.
237 * @param dpi Area to be drawn to.
239 void LinkGraphOverlay::Draw(const DrawPixelInfo
*dpi
)
242 this->RebuildCache();
245 this->DrawLinks(dpi
);
246 this->DrawStationDots(dpi
);
250 * Draw the cached links or part of them into the given area.
251 * @param dpi Area to be drawn to.
253 void LinkGraphOverlay::DrawLinks(const DrawPixelInfo
*dpi
) const
255 for (LinkMap::const_iterator
i(this->cached_links
.begin()); i
!= this->cached_links
.end(); ++i
) {
256 if (!Station::IsValidID(i
->first
)) continue;
257 Point pta
= this->GetStationMiddle(Station::Get(i
->first
));
258 for (StationLinkMap::const_iterator
j(i
->second
.begin()); j
!= i
->second
.end(); ++j
) {
259 if (!Station::IsValidID(j
->first
)) continue;
260 Point ptb
= this->GetStationMiddle(Station::Get(j
->first
));
261 if (!this->IsLinkVisible(pta
, ptb
, dpi
, this->scale
+ 2)) continue;
262 this->DrawContent(pta
, ptb
, j
->second
);
268 * Draw one specific link.
269 * @param pta Source of the link.
270 * @param ptb Destination of the link.
271 * @param cargo Properties of the link.
273 void LinkGraphOverlay::DrawContent(Point pta
, Point ptb
, const LinkProperties
&cargo
) const
275 uint usage_or_plan
= std::min(cargo
.capacity
* 2 + 1, std::max(cargo
.usage
, cargo
.planned
));
276 int colour
= LinkGraphOverlay::LINK_COLOURS
[usage_or_plan
* lengthof(LinkGraphOverlay::LINK_COLOURS
) / (cargo
.capacity
* 2 + 2)];
277 int dash
= cargo
.shared
? this->scale
* 4 : 0;
279 /* Move line a bit 90° against its dominant direction to prevent it from
280 * being hidden below the grey line. */
281 int side
= _settings_game
.vehicle
.road_side
? 1 : -1;
282 if (abs(pta
.x
- ptb
.x
) < abs(pta
.y
- ptb
.y
)) {
283 int offset_x
= (pta
.y
> ptb
.y
? 1 : -1) * side
* this->scale
;
284 GfxDrawLine(pta
.x
+ offset_x
, pta
.y
, ptb
.x
+ offset_x
, ptb
.y
, colour
, this->scale
, dash
);
286 int offset_y
= (pta
.x
< ptb
.x
? 1 : -1) * side
* this->scale
;
287 GfxDrawLine(pta
.x
, pta
.y
+ offset_y
, ptb
.x
, ptb
.y
+ offset_y
, colour
, this->scale
, dash
);
290 GfxDrawLine(pta
.x
, pta
.y
, ptb
.x
, ptb
.y
, _colour_gradient
[COLOUR_GREY
][1], this->scale
);
294 * Draw dots for stations into the smallmap. The dots' sizes are determined by the amount of
295 * cargo produced there, their colours by the type of cargo produced.
297 void LinkGraphOverlay::DrawStationDots(const DrawPixelInfo
*dpi
) const
299 for (StationSupplyList::const_iterator
i(this->cached_stations
.begin()); i
!= this->cached_stations
.end(); ++i
) {
300 const Station
*st
= Station::GetIfValid(i
->first
);
301 if (st
== nullptr) continue;
302 Point pt
= this->GetStationMiddle(st
);
303 if (!this->IsPointVisible(pt
, dpi
, 3 * this->scale
)) continue;
305 uint r
= this->scale
* 2 + this->scale
* 2 * std::min(200U, i
->second
) / 200;
307 LinkGraphOverlay::DrawVertex(pt
.x
, pt
.y
, r
,
308 _colour_gradient
[st
->owner
!= OWNER_NONE
?
309 (Colours
)Company::Get(st
->owner
)->colour
: COLOUR_GREY
][5],
310 _colour_gradient
[COLOUR_GREY
][1]);
315 * Draw a square symbolizing a producer of cargo.
316 * @param x X coordinate of the middle of the vertex.
317 * @param y Y coordinate of the middle of the vertex.
318 * @param size Y and y extend of the vertex.
319 * @param colour Colour with which the vertex will be filled.
320 * @param border_colour Colour for the border of the vertex.
322 /* static */ void LinkGraphOverlay::DrawVertex(int x
, int y
, int size
, int colour
, int border_colour
)
326 int w2
= size
/ 2 + size
% 2;
328 GfxFillRect(x
- w1
, y
- w1
, x
+ w2
, y
+ w2
, colour
);
332 GfxDrawLine(x
- w1
, y
- w1
, x
+ w2
, y
- w1
, border_colour
);
333 GfxDrawLine(x
- w1
, y
+ w2
, x
+ w2
, y
+ w2
, border_colour
);
334 GfxDrawLine(x
- w1
, y
- w1
, x
- w1
, y
+ w2
, border_colour
);
335 GfxDrawLine(x
+ w2
, y
- w1
, x
+ w2
, y
+ w2
, border_colour
);
339 * Determine the middle of a station in the current window.
340 * @param st The station we're looking for.
341 * @return Middle point of the station in the current window.
343 Point
LinkGraphOverlay::GetStationMiddle(const Station
*st
) const
345 if (this->window
->viewport
!= nullptr) {
346 return GetViewportStationMiddle(this->window
->viewport
, st
);
348 /* assume this is a smallmap */
349 return static_cast<const SmallMapWindow
*>(this->window
)->GetStationMiddle(st
);
354 * Set a new cargo mask and rebuild the cache.
355 * @param cargo_mask New cargo mask.
357 void LinkGraphOverlay::SetCargoMask(CargoTypes cargo_mask
)
359 this->cargo_mask
= cargo_mask
;
360 this->RebuildCache();
361 this->window
->GetWidget
<NWidgetBase
>(this->widget_id
)->SetDirty(this->window
);
365 * Set a new company mask and rebuild the cache.
366 * @param company_mask New company mask.
368 void LinkGraphOverlay::SetCompanyMask(uint32 company_mask
)
370 this->company_mask
= company_mask
;
371 this->RebuildCache();
372 this->window
->GetWidget
<NWidgetBase
>(this->widget_id
)->SetDirty(this->window
);
375 /** Make a number of rows with buttons for each company for the linkgraph legend window. */
376 NWidgetBase
*MakeCompanyButtonRowsLinkGraphGUI(int *biggest_index
)
378 return MakeCompanyButtonRows(biggest_index
, WID_LGL_COMPANY_FIRST
, WID_LGL_COMPANY_LAST
, COLOUR_GREY
, 3, STR_NULL
);
381 NWidgetBase
*MakeSaturationLegendLinkGraphGUI(int *biggest_index
)
383 NWidgetVertical
*panel
= new NWidgetVertical(NC_EQUALSIZE
);
384 for (uint i
= 0; i
< lengthof(LinkGraphOverlay::LINK_COLOURS
); ++i
) {
385 NWidgetBackground
* wid
= new NWidgetBackground(WWT_PANEL
, COLOUR_DARK_GREEN
, i
+ WID_LGL_SATURATION_FIRST
);
386 wid
->SetMinimalSize(50, FONT_HEIGHT_SMALL
);
388 wid
->SetResize(0, 0);
391 *biggest_index
= WID_LGL_SATURATION_LAST
;
395 NWidgetBase
*MakeCargoesLegendLinkGraphGUI(int *biggest_index
)
397 static const uint ENTRIES_PER_ROW
= CeilDiv(NUM_CARGO
, 5);
398 NWidgetVertical
*panel
= new NWidgetVertical(NC_EQUALSIZE
);
399 NWidgetHorizontal
*row
= nullptr;
400 for (uint i
= 0; i
< NUM_CARGO
; ++i
) {
401 if (i
% ENTRIES_PER_ROW
== 0) {
402 if (row
) panel
->Add(row
);
403 row
= new NWidgetHorizontal(NC_EQUALSIZE
);
405 NWidgetBackground
* wid
= new NWidgetBackground(WWT_PANEL
, COLOUR_GREY
, i
+ WID_LGL_CARGO_FIRST
);
406 wid
->SetMinimalSize(25, FONT_HEIGHT_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, FONT_HEIGHT_SMALL
);
415 spc
->SetResize(0, 0);
419 *biggest_index
= WID_LGL_CARGO_LAST
;
424 static const NWidgetPart _nested_linkgraph_legend_widgets
[] = {
425 NWidget(NWID_HORIZONTAL
),
426 NWidget(WWT_CLOSEBOX
, COLOUR_DARK_GREEN
),
427 NWidget(WWT_CAPTION
, COLOUR_DARK_GREEN
, WID_LGL_CAPTION
), SetDataTip(STR_LINKGRAPH_LEGEND_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
428 NWidget(WWT_SHADEBOX
, COLOUR_DARK_GREEN
),
429 NWidget(WWT_STICKYBOX
, COLOUR_DARK_GREEN
),
431 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
),
432 NWidget(NWID_HORIZONTAL
),
433 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_LGL_SATURATION
),
434 SetPadding(WD_FRAMERECT_TOP
, 0, WD_FRAMERECT_BOTTOM
, WD_CAPTIONTEXT_LEFT
),
435 NWidgetFunction(MakeSaturationLegendLinkGraphGUI
),
437 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_LGL_COMPANIES
),
438 SetPadding(WD_FRAMERECT_TOP
, 0, WD_FRAMERECT_BOTTOM
, WD_CAPTIONTEXT_LEFT
),
439 NWidget(NWID_VERTICAL
, NC_EQUALSIZE
),
440 NWidgetFunction(MakeCompanyButtonRowsLinkGraphGUI
),
441 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_LGL_COMPANIES_ALL
), SetDataTip(STR_LINKGRAPH_LEGEND_ALL
, STR_NULL
),
442 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_LGL_COMPANIES_NONE
), SetDataTip(STR_LINKGRAPH_LEGEND_NONE
, STR_NULL
),
445 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_LGL_CARGOES
),
446 SetPadding(WD_FRAMERECT_TOP
, WD_FRAMERECT_RIGHT
, WD_FRAMERECT_BOTTOM
, WD_CAPTIONTEXT_LEFT
),
447 NWidget(NWID_VERTICAL
, NC_EQUALSIZE
),
448 NWidgetFunction(MakeCargoesLegendLinkGraphGUI
),
449 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_LGL_CARGOES_ALL
), SetDataTip(STR_LINKGRAPH_LEGEND_ALL
, STR_NULL
),
450 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_LGL_CARGOES_NONE
), SetDataTip(STR_LINKGRAPH_LEGEND_NONE
, STR_NULL
),
457 static_assert(WID_LGL_SATURATION_LAST
- WID_LGL_SATURATION_FIRST
==
458 lengthof(LinkGraphOverlay::LINK_COLOURS
) - 1);
460 static WindowDesc
_linkgraph_legend_desc(
461 WDP_AUTO
, "toolbar_linkgraph", 0, 0,
462 WC_LINKGRAPH_LEGEND
, WC_NONE
,
464 _nested_linkgraph_legend_widgets
, lengthof(_nested_linkgraph_legend_widgets
)
468 * Open a link graph legend window.
470 void ShowLinkGraphLegend()
472 AllocateWindowDescFront
<LinkGraphLegendWindow
>(&_linkgraph_legend_desc
, 0);
475 LinkGraphLegendWindow::LinkGraphLegendWindow(WindowDesc
*desc
, int window_number
) : Window(desc
)
477 this->InitNested(window_number
);
478 this->InvalidateData(0);
479 this->SetOverlay(FindWindowById(WC_MAIN_WINDOW
, 0)->viewport
->overlay
);
483 * Set the overlay belonging to this menu and import its company/cargo settings.
484 * @param overlay New overlay for this menu.
486 void LinkGraphLegendWindow::SetOverlay(LinkGraphOverlay
*overlay
) {
487 this->overlay
= overlay
;
488 uint32 companies
= this->overlay
->GetCompanyMask();
489 for (uint c
= 0; c
< MAX_COMPANIES
; c
++) {
490 if (!this->IsWidgetDisabled(WID_LGL_COMPANY_FIRST
+ c
)) {
491 this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST
+ c
, HasBit(companies
, c
));
494 CargoTypes cargoes
= this->overlay
->GetCargoMask();
495 for (uint c
= 0; c
< NUM_CARGO
; c
++) {
496 if (!this->IsWidgetDisabled(WID_LGL_CARGO_FIRST
+ c
)) {
497 this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST
+ c
, HasBit(cargoes
, c
));
502 void LinkGraphLegendWindow::UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
504 if (IsInsideMM(widget
, WID_LGL_SATURATION_FIRST
, WID_LGL_SATURATION_LAST
+ 1)) {
505 StringID str
= STR_NULL
;
506 if (widget
== WID_LGL_SATURATION_FIRST
) {
507 str
= STR_LINKGRAPH_LEGEND_UNUSED
;
508 } else if (widget
== WID_LGL_SATURATION_LAST
) {
509 str
= STR_LINKGRAPH_LEGEND_OVERLOADED
;
510 } else if (widget
== (WID_LGL_SATURATION_LAST
+ WID_LGL_SATURATION_FIRST
) / 2) {
511 str
= STR_LINKGRAPH_LEGEND_SATURATED
;
513 if (str
!= STR_NULL
) {
514 Dimension dim
= GetStringBoundingBox(str
);
515 dim
.width
+= WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
516 dim
.height
+= WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
517 *size
= maxdim(*size
, dim
);
520 if (IsInsideMM(widget
, WID_LGL_CARGO_FIRST
, WID_LGL_CARGO_LAST
+ 1)) {
521 CargoSpec
*cargo
= CargoSpec::Get(widget
- WID_LGL_CARGO_FIRST
);
522 if (cargo
->IsValid()) {
523 Dimension dim
= GetStringBoundingBox(cargo
->abbrev
);
524 dim
.width
+= WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
525 dim
.height
+= WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
526 *size
= maxdim(*size
, dim
);
531 void LinkGraphLegendWindow::DrawWidget(const Rect
&r
, int widget
) const
533 if (IsInsideMM(widget
, WID_LGL_COMPANY_FIRST
, WID_LGL_COMPANY_LAST
+ 1)) {
534 if (this->IsWidgetDisabled(widget
)) return;
535 CompanyID cid
= (CompanyID
)(widget
- WID_LGL_COMPANY_FIRST
);
536 Dimension sprite_size
= GetSpriteSize(SPR_COMPANY_ICON
);
537 DrawCompanyIcon(cid
, (r
.left
+ r
.right
+ 1 - sprite_size
.width
) / 2, (r
.top
+ r
.bottom
+ 1 - sprite_size
.height
) / 2);
539 if (IsInsideMM(widget
, WID_LGL_SATURATION_FIRST
, WID_LGL_SATURATION_LAST
+ 1)) {
540 GfxFillRect(r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.bottom
- 1, LinkGraphOverlay::LINK_COLOURS
[widget
- WID_LGL_SATURATION_FIRST
]);
541 StringID str
= STR_NULL
;
542 if (widget
== WID_LGL_SATURATION_FIRST
) {
543 str
= STR_LINKGRAPH_LEGEND_UNUSED
;
544 } else if (widget
== WID_LGL_SATURATION_LAST
) {
545 str
= STR_LINKGRAPH_LEGEND_OVERLOADED
;
546 } else if (widget
== (WID_LGL_SATURATION_LAST
+ WID_LGL_SATURATION_FIRST
) / 2) {
547 str
= STR_LINKGRAPH_LEGEND_SATURATED
;
549 if (str
!= STR_NULL
) DrawString(r
.left
, r
.right
, (r
.top
+ r
.bottom
+ 1 - FONT_HEIGHT_SMALL
) / 2, str
, TC_FROMSTRING
, SA_HOR_CENTER
);
551 if (IsInsideMM(widget
, WID_LGL_CARGO_FIRST
, WID_LGL_CARGO_LAST
+ 1)) {
552 if (this->IsWidgetDisabled(widget
)) return;
553 CargoSpec
*cargo
= CargoSpec::Get(widget
- WID_LGL_CARGO_FIRST
);
554 GfxFillRect(r
.left
+ 2, r
.top
+ 2, r
.right
- 2, r
.bottom
- 2, cargo
->legend_colour
);
555 DrawString(r
.left
, r
.right
, (r
.top
+ r
.bottom
+ 1 - FONT_HEIGHT_SMALL
) / 2, cargo
->abbrev
, GetContrastColour(cargo
->legend_colour
, 73), SA_HOR_CENTER
);
559 bool LinkGraphLegendWindow::OnTooltip(Point pt
, int widget
, TooltipCloseCondition close_cond
)
561 if (IsInsideMM(widget
, WID_LGL_COMPANY_FIRST
, WID_LGL_COMPANY_LAST
+ 1)) {
562 if (this->IsWidgetDisabled(widget
)) {
563 GuiShowTooltips(this, STR_LINKGRAPH_LEGEND_SELECT_COMPANIES
, 0, nullptr, close_cond
);
566 CompanyID cid
= (CompanyID
)(widget
- WID_LGL_COMPANY_FIRST
);
567 params
[0] = STR_LINKGRAPH_LEGEND_SELECT_COMPANIES
;
569 GuiShowTooltips(this, STR_LINKGRAPH_LEGEND_COMPANY_TOOLTIP
, 2, params
, close_cond
);
573 if (IsInsideMM(widget
, WID_LGL_CARGO_FIRST
, WID_LGL_CARGO_LAST
+ 1)) {
574 if (this->IsWidgetDisabled(widget
)) return false;
575 CargoSpec
*cargo
= CargoSpec::Get(widget
- WID_LGL_CARGO_FIRST
);
577 params
[0] = cargo
->name
;
578 GuiShowTooltips(this, STR_BLACK_STRING
, 1, params
, close_cond
);
585 * Update the overlay with the new company selection.
587 void LinkGraphLegendWindow::UpdateOverlayCompanies()
590 for (uint c
= 0; c
< MAX_COMPANIES
; c
++) {
591 if (this->IsWidgetDisabled(c
+ WID_LGL_COMPANY_FIRST
)) continue;
592 if (!this->IsWidgetLowered(c
+ WID_LGL_COMPANY_FIRST
)) continue;
595 this->overlay
->SetCompanyMask(mask
);
599 * Update the overlay with the new cargo selection.
601 void LinkGraphLegendWindow::UpdateOverlayCargoes()
604 for (uint c
= 0; c
< NUM_CARGO
; c
++) {
605 if (this->IsWidgetDisabled(c
+ WID_LGL_CARGO_FIRST
)) continue;
606 if (!this->IsWidgetLowered(c
+ WID_LGL_CARGO_FIRST
)) continue;
609 this->overlay
->SetCargoMask(mask
);
612 void LinkGraphLegendWindow::OnClick(Point pt
, int widget
, int click_count
)
614 /* Check which button is clicked */
615 if (IsInsideMM(widget
, WID_LGL_COMPANY_FIRST
, WID_LGL_COMPANY_LAST
+ 1)) {
616 if (!this->IsWidgetDisabled(widget
)) {
617 this->ToggleWidgetLoweredState(widget
);
618 this->UpdateOverlayCompanies();
620 } else if (widget
== WID_LGL_COMPANIES_ALL
|| widget
== WID_LGL_COMPANIES_NONE
) {
621 for (uint c
= 0; c
< MAX_COMPANIES
; c
++) {
622 if (this->IsWidgetDisabled(c
+ WID_LGL_COMPANY_FIRST
)) continue;
623 this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST
+ c
, widget
== WID_LGL_COMPANIES_ALL
);
625 this->UpdateOverlayCompanies();
627 } else if (IsInsideMM(widget
, WID_LGL_CARGO_FIRST
, WID_LGL_CARGO_LAST
+ 1)) {
628 if (!this->IsWidgetDisabled(widget
)) {
629 this->ToggleWidgetLoweredState(widget
);
630 this->UpdateOverlayCargoes();
632 } else if (widget
== WID_LGL_CARGOES_ALL
|| widget
== WID_LGL_CARGOES_NONE
) {
633 for (uint c
= 0; c
< NUM_CARGO
; c
++) {
634 if (this->IsWidgetDisabled(c
+ WID_LGL_CARGO_FIRST
)) continue;
635 this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST
+ c
, widget
== WID_LGL_CARGOES_ALL
);
637 this->UpdateOverlayCargoes();
643 * Invalidate the data of this window if the cargoes or companies have changed.
644 * @param data ignored
645 * @param gui_scope ignored
647 void LinkGraphLegendWindow::OnInvalidateData(int data
, bool gui_scope
)
649 /* Disable the companies who are not active */
650 for (CompanyID i
= COMPANY_FIRST
; i
< MAX_COMPANIES
; i
++) {
651 this->SetWidgetDisabledState(i
+ WID_LGL_COMPANY_FIRST
, !Company::IsValidID(i
));
653 for (CargoID i
= 0; i
< NUM_CARGO
; i
++) {
654 this->SetWidgetDisabledState(i
+ WID_LGL_CARGO_FIRST
, !CargoSpec::Get(i
)->IsValid());