4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file linkgraph_gui.cpp Implementation of linkgraph overlay GUI. */
12 #include "../stdafx.h"
13 #include "../window_gui.h"
14 #include "../window_func.h"
15 #include "../company_base.h"
16 #include "../company_gui.h"
17 #include "../date_func.h"
18 #include "../viewport_func.h"
19 #include "../smallmap_gui.h"
20 #include "../core/geometry_func.hpp"
21 #include "../widgets/link_graph_legend_widget.h"
23 #include "table/strings.h"
25 #include "../safeguards.h"
28 * Colours for the various "load" states of links. Ordered from "unused" to
31 const uint8
LinkGraphOverlay::LINK_COLOURS
[] = {
32 0x0f, 0xd1, 0xd0, 0x57,
33 0x55, 0x53, 0xbf, 0xbd,
34 0xba, 0xb9, 0xb7, 0xb5
38 * Get a DPI for the widget we will be drawing to.
39 * @param dpi DrawPixelInfo to fill with the desired dimensions.
41 void LinkGraphOverlay::GetWidgetDpi(DrawPixelInfo
*dpi
) const
43 const NWidgetBase
*wi
= this->window
->GetWidget
<NWidgetBase
>(this->widget_id
);
44 dpi
->left
= dpi
->top
= 0;
45 dpi
->width
= wi
->current_x
;
46 dpi
->height
= wi
->current_y
;
50 * Rebuild the cache and recalculate which links and stations to be shown.
52 void LinkGraphOverlay::RebuildCache()
54 this->cached_links
.clear();
55 this->cached_stations
.clear();
56 if (this->company_mask
== 0) return;
59 this->GetWidgetDpi(&dpi
);
62 FOR_ALL_STATIONS(sta
) {
63 if (sta
->rect
.IsEmpty()) continue;
65 Point pta
= this->GetStationMiddle(sta
);
67 StationID from
= sta
->index
;
68 StationLinkMap
&seen_links
= this->cached_links
[from
];
72 FOR_EACH_SET_CARGO_ID(c
, this->cargo_mask
) {
73 if (!CargoSpec::Get(c
)->IsValid()) continue;
74 if (!LinkGraph::IsValidID(sta
->goods
[c
].link_graph
)) continue;
75 const LinkGraph
&lg
= *LinkGraph::Get(sta
->goods
[c
].link_graph
);
77 ConstNode from_node
= lg
[sta
->goods
[c
].node
];
78 supply
+= lg
.Monthly(from_node
.Supply());
79 for (ConstEdgeIterator i
= from_node
.Begin(); i
!= from_node
.End(); ++i
) {
80 StationID to
= lg
[i
->first
].Station();
82 if (!Station::IsValidID(to
) || seen_links
.find(to
) != seen_links
.end()) {
85 const Station
*stb
= Station::Get(to
);
88 /* Show links between stations of selected companies or "neutral" ones like oilrigs. */
89 if (stb
->owner
!= OWNER_NONE
&& sta
->owner
!= OWNER_NONE
&& !HasBit(this->company_mask
, stb
->owner
)) continue;
90 if (stb
->rect
.IsEmpty()) continue;
92 if (!this->IsLinkVisible(pta
, this->GetStationMiddle(stb
), &dpi
)) continue;
94 this->AddLinks(sta
, stb
);
95 seen_links
[to
]; // make sure it is created and marked as seen
98 if (this->IsPointVisible(pta
, &dpi
)) {
99 this->cached_stations
.push_back(std::make_pair(from
, supply
));
105 * Determine if a certain point is inside the given DPI, with some lee way.
106 * @param pt Point we are looking for.
107 * @param dpi Visible area.
108 * @param padding Extent of the point.
109 * @return If the point or any of its 'extent' is inside the dpi.
111 inline bool LinkGraphOverlay::IsPointVisible(Point pt
, const DrawPixelInfo
*dpi
, int padding
) const
113 return pt
.x
> dpi
->left
- padding
&& pt
.y
> dpi
->top
- padding
&&
114 pt
.x
< dpi
->left
+ dpi
->width
+ padding
&&
115 pt
.y
< dpi
->top
+ dpi
->height
+ padding
;
119 * Determine if a certain link crosses through the area given by the dpi with some lee way.
120 * @param pta First end of the link.
121 * @param ptb Second end of the link.
122 * @param dpi Visible area.
123 * @param padding Width or thickness of the link.
124 * @return If the link or any of its "thickness" is visible. This may return false positives.
126 inline bool LinkGraphOverlay::IsLinkVisible(Point pta
, Point ptb
, const DrawPixelInfo
*dpi
, int padding
) const
128 return !((pta
.x
< dpi
->left
- padding
&& ptb
.x
< dpi
->left
- padding
) ||
129 (pta
.y
< dpi
->top
- padding
&& ptb
.y
< dpi
->top
- padding
) ||
130 (pta
.x
> dpi
->left
+ dpi
->width
+ padding
&&
131 ptb
.x
> dpi
->left
+ dpi
->width
+ padding
) ||
132 (pta
.y
> dpi
->top
+ dpi
->height
+ padding
&&
133 ptb
.y
> dpi
->top
+ dpi
->height
+ padding
));
137 * Add all "interesting" links between the given stations to the cache.
138 * @param from The source station.
139 * @param to The destination station.
141 void LinkGraphOverlay::AddLinks(const Station
*from
, const Station
*to
)
144 FOR_EACH_SET_CARGO_ID(c
, this->cargo_mask
) {
145 if (!CargoSpec::Get(c
)->IsValid()) continue;
146 const GoodsEntry
&ge
= from
->goods
[c
];
147 if (!LinkGraph::IsValidID(ge
.link_graph
) ||
148 ge
.link_graph
!= to
->goods
[c
].link_graph
) {
151 const LinkGraph
&lg
= *LinkGraph::Get(ge
.link_graph
);
152 ConstEdge edge
= lg
[ge
.node
][to
->goods
[c
].node
];
153 if (edge
.Capacity() > 0) {
154 this->AddStats(lg
.Monthly(edge
.Capacity()), lg
.Monthly(edge
.Usage()),
155 ge
.flows
.GetFlowVia(to
->index
), from
->owner
== OWNER_NONE
|| to
->owner
== OWNER_NONE
,
156 this->cached_links
[from
->index
][to
->index
]);
162 * Add information from a given pair of link stat and flow stat to the given
163 * link properties. The shown usage or plan is always the maximum of all link
165 * @param new_cap Capacity of the new link.
166 * @param new_usg Usage of the new link.
167 * @param new_plan Planned flow for the new link.
168 * @param new_shared If the new link is shared.
169 * @param cargo LinkProperties to write the information to.
171 /* static */ void LinkGraphOverlay::AddStats(uint new_cap
, uint new_usg
, uint new_plan
, bool new_shared
, LinkProperties
&cargo
)
173 /* multiply the numbers by 32 in order to avoid comparing to 0 too often. */
174 if (cargo
.capacity
== 0 ||
175 max(cargo
.usage
, cargo
.planned
) * 32 / (cargo
.capacity
+ 1) < max(new_usg
, new_plan
) * 32 / (new_cap
+ 1)) {
176 cargo
.capacity
= new_cap
;
177 cargo
.usage
= new_usg
;
178 cargo
.planned
= new_plan
;
180 if (new_shared
) cargo
.shared
= true;
184 * Draw the linkgraph overlay or some part of it, in the area given.
185 * @param dpi Area to be drawn to.
187 void LinkGraphOverlay::Draw(const DrawPixelInfo
*dpi
) const
189 this->DrawLinks(dpi
);
190 this->DrawStationDots(dpi
);
194 * Draw the cached links or part of them into the given area.
195 * @param dpi Area to be drawn to.
197 void LinkGraphOverlay::DrawLinks(const DrawPixelInfo
*dpi
) const
199 for (LinkMap::const_iterator
i(this->cached_links
.begin()); i
!= this->cached_links
.end(); ++i
) {
200 if (!Station::IsValidID(i
->first
)) continue;
201 Point pta
= this->GetStationMiddle(Station::Get(i
->first
));
202 for (StationLinkMap::const_iterator
j(i
->second
.begin()); j
!= i
->second
.end(); ++j
) {
203 if (!Station::IsValidID(j
->first
)) continue;
204 Point ptb
= this->GetStationMiddle(Station::Get(j
->first
));
205 if (!this->IsLinkVisible(pta
, ptb
, dpi
, this->scale
+ 2)) continue;
206 this->DrawContent(pta
, ptb
, j
->second
);
212 * Draw one specific link.
213 * @param pta Source of the link.
214 * @param ptb Destination of the link.
215 * @param cargo Properties of the link.
217 void LinkGraphOverlay::DrawContent(Point pta
, Point ptb
, const LinkProperties
&cargo
) const
219 uint usage_or_plan
= min(cargo
.capacity
* 2 + 1, max(cargo
.usage
, cargo
.planned
));
220 int colour
= LinkGraphOverlay::LINK_COLOURS
[usage_or_plan
* lengthof(LinkGraphOverlay::LINK_COLOURS
) / (cargo
.capacity
* 2 + 2)];
221 int dash
= cargo
.shared
? this->scale
* 4 : 0;
223 /* Move line a bit 90° against its dominant direction to prevent it from
224 * being hidden below the grey line. */
225 int side
= _settings_game
.vehicle
.road_side
? 1 : -1;
226 if (abs(pta
.x
- ptb
.x
) < abs(pta
.y
- ptb
.y
)) {
227 int offset_x
= (pta
.y
> ptb
.y
? 1 : -1) * side
* this->scale
;
228 GfxDrawLine(pta
.x
+ offset_x
, pta
.y
, ptb
.x
+ offset_x
, ptb
.y
, colour
, this->scale
, dash
);
230 int offset_y
= (pta
.x
< ptb
.x
? 1 : -1) * side
* this->scale
;
231 GfxDrawLine(pta
.x
, pta
.y
+ offset_y
, ptb
.x
, ptb
.y
+ offset_y
, colour
, this->scale
, dash
);
234 GfxDrawLine(pta
.x
, pta
.y
, ptb
.x
, ptb
.y
, _colour_gradient
[COLOUR_GREY
][1], this->scale
);
238 * Draw dots for stations into the smallmap. The dots' sizes are determined by the amount of
239 * cargo produced there, their colours by the type of cargo produced.
241 void LinkGraphOverlay::DrawStationDots(const DrawPixelInfo
*dpi
) const
243 for (StationSupplyList::const_iterator
i(this->cached_stations
.begin()); i
!= this->cached_stations
.end(); ++i
) {
244 const Station
*st
= Station::GetIfValid(i
->first
);
245 if (st
== NULL
) continue;
246 Point pt
= this->GetStationMiddle(st
);
247 if (!this->IsPointVisible(pt
, dpi
, 3 * this->scale
)) continue;
249 uint r
= this->scale
* 2 + this->scale
* 2 * min(200, i
->second
) / 200;
251 LinkGraphOverlay::DrawVertex(pt
.x
, pt
.y
, r
,
252 _colour_gradient
[st
->owner
!= OWNER_NONE
?
253 (Colours
)Company::Get(st
->owner
)->colour
: COLOUR_GREY
][5],
254 _colour_gradient
[COLOUR_GREY
][1]);
259 * Draw a square symbolizing a producer of cargo.
260 * @param x X coordinate of the middle of the vertex.
261 * @param y Y coordinate of the middle of the vertex.
262 * @param size Y and y extend of the vertex.
263 * @param colour Colour with which the vertex will be filled.
264 * @param border_colour Colour for the border of the vertex.
266 /* static */ void LinkGraphOverlay::DrawVertex(int x
, int y
, int size
, int colour
, int border_colour
)
270 int w2
= size
/ 2 + size
% 2;
272 GfxFillRect(x
- w1
, y
- w1
, x
+ w2
, y
+ w2
, colour
);
276 GfxDrawLine(x
- w1
, y
- w1
, x
+ w2
, y
- w1
, border_colour
);
277 GfxDrawLine(x
- w1
, y
+ w2
, x
+ w2
, y
+ w2
, border_colour
);
278 GfxDrawLine(x
- w1
, y
- w1
, x
- w1
, y
+ w2
, border_colour
);
279 GfxDrawLine(x
+ w2
, y
- w1
, x
+ w2
, y
+ w2
, border_colour
);
283 * Determine the middle of a station in the current window.
284 * @param st The station we're looking for.
285 * @return Middle point of the station in the current window.
287 Point
LinkGraphOverlay::GetStationMiddle(const Station
*st
) const
289 if (this->window
->viewport
!= NULL
) {
290 return GetViewportStationMiddle(this->window
->viewport
, st
);
292 /* assume this is a smallmap */
293 return static_cast<const SmallMapWindow
*>(this->window
)->GetStationMiddle(st
);
298 * Set a new cargo mask and rebuild the cache.
299 * @param cargo_mask New cargo mask.
301 void LinkGraphOverlay::SetCargoMask(uint32 cargo_mask
)
303 this->cargo_mask
= cargo_mask
;
304 this->RebuildCache();
305 this->window
->GetWidget
<NWidgetBase
>(this->widget_id
)->SetDirty(this->window
);
309 * Set a new company mask and rebuild the cache.
310 * @param company_mask New company mask.
312 void LinkGraphOverlay::SetCompanyMask(uint32 company_mask
)
314 this->company_mask
= company_mask
;
315 this->RebuildCache();
316 this->window
->GetWidget
<NWidgetBase
>(this->widget_id
)->SetDirty(this->window
);
319 /** Make a number of rows with buttons for each company for the linkgraph legend window. */
320 NWidgetBase
*MakeCompanyButtonRowsLinkGraphGUI(int *biggest_index
)
322 return MakeCompanyButtonRows(biggest_index
, WID_LGL_COMPANY_FIRST
, WID_LGL_COMPANY_LAST
, 3, STR_LINKGRAPH_LEGEND_SELECT_COMPANIES
);
325 NWidgetBase
*MakeSaturationLegendLinkGraphGUI(int *biggest_index
)
327 NWidgetVertical
*panel
= new NWidgetVertical(NC_EQUALSIZE
);
328 for (uint i
= 0; i
< lengthof(LinkGraphOverlay::LINK_COLOURS
); ++i
) {
329 NWidgetBackground
* wid
= new NWidgetBackground(WWT_PANEL
, COLOUR_DARK_GREEN
, i
+ WID_LGL_SATURATION_FIRST
);
330 wid
->SetMinimalSize(50, FONT_HEIGHT_SMALL
);
332 wid
->SetResize(0, 0);
335 *biggest_index
= WID_LGL_SATURATION_LAST
;
339 NWidgetBase
*MakeCargoesLegendLinkGraphGUI(int *biggest_index
)
341 static const uint ENTRIES_PER_ROW
= CeilDiv(NUM_CARGO
, 5);
342 NWidgetVertical
*panel
= new NWidgetVertical(NC_EQUALSIZE
);
343 NWidgetHorizontal
*row
= NULL
;
344 for (uint i
= 0; i
< NUM_CARGO
; ++i
) {
345 if (i
% ENTRIES_PER_ROW
== 0) {
346 if (row
) panel
->Add(row
);
347 row
= new NWidgetHorizontal(NC_EQUALSIZE
);
349 NWidgetBackground
* wid
= new NWidgetBackground(WWT_PANEL
, COLOUR_GREY
, i
+ WID_LGL_CARGO_FIRST
);
350 wid
->SetMinimalSize(25, FONT_HEIGHT_SMALL
);
352 wid
->SetResize(0, 0);
355 /* Fill up last row */
356 for (uint i
= 0; i
< 4 - (NUM_CARGO
- 1) % 5; ++i
) {
357 NWidgetSpacer
*spc
= new NWidgetSpacer(25, FONT_HEIGHT_SMALL
);
359 spc
->SetResize(0, 0);
363 *biggest_index
= WID_LGL_CARGO_LAST
;
368 static const NWidgetPart _nested_linkgraph_legend_widgets
[] = {
369 NWidget(NWID_HORIZONTAL
),
370 NWidget(WWT_CLOSEBOX
, COLOUR_DARK_GREEN
),
371 NWidget(WWT_CAPTION
, COLOUR_DARK_GREEN
, WID_LGL_CAPTION
), SetDataTip(STR_LINKGRAPH_LEGEND_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
372 NWidget(WWT_SHADEBOX
, COLOUR_DARK_GREEN
),
373 NWidget(WWT_STICKYBOX
, COLOUR_DARK_GREEN
),
375 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
),
376 NWidget(NWID_HORIZONTAL
),
377 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_LGL_SATURATION
),
378 SetPadding(WD_FRAMERECT_TOP
, 0, WD_FRAMERECT_BOTTOM
, WD_CAPTIONTEXT_LEFT
),
379 NWidgetFunction(MakeSaturationLegendLinkGraphGUI
),
381 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_LGL_COMPANIES
),
382 SetPadding(WD_FRAMERECT_TOP
, 0, WD_FRAMERECT_BOTTOM
, WD_CAPTIONTEXT_LEFT
),
383 NWidget(NWID_VERTICAL
, NC_EQUALSIZE
),
384 NWidgetFunction(MakeCompanyButtonRowsLinkGraphGUI
),
385 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_LGL_COMPANIES_ALL
), SetDataTip(STR_LINKGRAPH_LEGEND_ALL
, STR_NULL
),
386 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_LGL_COMPANIES_NONE
), SetDataTip(STR_LINKGRAPH_LEGEND_NONE
, STR_NULL
),
389 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_LGL_CARGOES
),
390 SetPadding(WD_FRAMERECT_TOP
, WD_FRAMERECT_RIGHT
, WD_FRAMERECT_BOTTOM
, WD_CAPTIONTEXT_LEFT
),
391 NWidget(NWID_VERTICAL
, NC_EQUALSIZE
),
392 NWidgetFunction(MakeCargoesLegendLinkGraphGUI
),
393 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_LGL_CARGOES_ALL
), SetDataTip(STR_LINKGRAPH_LEGEND_ALL
, STR_NULL
),
394 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_LGL_CARGOES_NONE
), SetDataTip(STR_LINKGRAPH_LEGEND_NONE
, STR_NULL
),
401 assert_compile(WID_LGL_SATURATION_LAST
- WID_LGL_SATURATION_FIRST
==
402 lengthof(LinkGraphOverlay::LINK_COLOURS
) - 1);
404 static WindowDesc
_linkgraph_legend_desc(
405 WDP_AUTO
, "toolbar_linkgraph", 0, 0,
406 WC_LINKGRAPH_LEGEND
, WC_NONE
,
408 _nested_linkgraph_legend_widgets
, lengthof(_nested_linkgraph_legend_widgets
)
412 * Open a link graph legend window.
414 void ShowLinkGraphLegend()
416 AllocateWindowDescFront
<LinkGraphLegendWindow
>(&_linkgraph_legend_desc
, 0);
419 LinkGraphLegendWindow::LinkGraphLegendWindow(WindowDesc
*desc
, int window_number
) : Window(desc
)
421 this->InitNested(window_number
);
422 this->InvalidateData(0);
423 this->SetOverlay(FindWindowById(WC_MAIN_WINDOW
, 0)->viewport
->overlay
);
427 * Set the overlay belonging to this menu and import its company/cargo settings.
428 * @params overlay New overlay for this menu.
430 void LinkGraphLegendWindow::SetOverlay(LinkGraphOverlay
*overlay
) {
431 this->overlay
= overlay
;
432 uint32 companies
= this->overlay
->GetCompanyMask();
433 for (uint c
= 0; c
< MAX_COMPANIES
; c
++) {
434 if (!this->IsWidgetDisabled(WID_LGL_COMPANY_FIRST
+ c
)) {
435 this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST
+ c
, HasBit(companies
, c
));
438 uint32 cargoes
= this->overlay
->GetCargoMask();
439 for (uint c
= 0; c
< NUM_CARGO
; c
++) {
440 if (!this->IsWidgetDisabled(WID_LGL_CARGO_FIRST
+ c
)) {
441 this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST
+ c
, HasBit(cargoes
, c
));
446 void LinkGraphLegendWindow::UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
448 if (IsInsideMM(widget
, WID_LGL_SATURATION_FIRST
, WID_LGL_SATURATION_LAST
+ 1)) {
449 StringID str
= STR_NULL
;
450 if (widget
== WID_LGL_SATURATION_FIRST
) {
451 str
= STR_LINKGRAPH_LEGEND_UNUSED
;
452 } else if (widget
== WID_LGL_SATURATION_LAST
) {
453 str
= STR_LINKGRAPH_LEGEND_OVERLOADED
;
454 } else if (widget
== (WID_LGL_SATURATION_LAST
+ WID_LGL_SATURATION_FIRST
) / 2) {
455 str
= STR_LINKGRAPH_LEGEND_SATURATED
;
457 if (str
!= STR_NULL
) {
458 Dimension dim
= GetStringBoundingBox(str
);
459 dim
.width
+= WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
460 dim
.height
+= WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
461 *size
= maxdim(*size
, dim
);
464 if (IsInsideMM(widget
, WID_LGL_CARGO_FIRST
, WID_LGL_CARGO_LAST
+ 1)) {
465 CargoSpec
*cargo
= CargoSpec::Get(widget
- WID_LGL_CARGO_FIRST
);
466 if (cargo
->IsValid()) {
467 Dimension dim
= GetStringBoundingBox(cargo
->abbrev
);
468 dim
.width
+= WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
469 dim
.height
+= WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
470 *size
= maxdim(*size
, dim
);
475 void LinkGraphLegendWindow::DrawWidget(const Rect
&r
, int widget
) const
477 if (IsInsideMM(widget
, WID_LGL_COMPANY_FIRST
, WID_LGL_COMPANY_LAST
+ 1)) {
478 if (this->IsWidgetDisabled(widget
)) return;
479 CompanyID cid
= (CompanyID
)(widget
- WID_LGL_COMPANY_FIRST
);
480 Dimension sprite_size
= GetSpriteSize(SPR_COMPANY_ICON
);
481 DrawCompanyIcon(cid
, (r
.left
+ r
.right
+ 1 - sprite_size
.width
) / 2, (r
.top
+ r
.bottom
+ 1 - sprite_size
.height
) / 2);
483 if (IsInsideMM(widget
, WID_LGL_SATURATION_FIRST
, WID_LGL_SATURATION_LAST
+ 1)) {
484 GfxFillRect(r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.bottom
- 1, LinkGraphOverlay::LINK_COLOURS
[widget
- WID_LGL_SATURATION_FIRST
]);
485 StringID str
= STR_NULL
;
486 if (widget
== WID_LGL_SATURATION_FIRST
) {
487 str
= STR_LINKGRAPH_LEGEND_UNUSED
;
488 } else if (widget
== WID_LGL_SATURATION_LAST
) {
489 str
= STR_LINKGRAPH_LEGEND_OVERLOADED
;
490 } else if (widget
== (WID_LGL_SATURATION_LAST
+ WID_LGL_SATURATION_FIRST
) / 2) {
491 str
= STR_LINKGRAPH_LEGEND_SATURATED
;
493 if (str
!= STR_NULL
) DrawString(r
.left
, r
.right
, (r
.top
+ r
.bottom
+ 1 - FONT_HEIGHT_SMALL
) / 2, str
, TC_FROMSTRING
, SA_HOR_CENTER
);
495 if (IsInsideMM(widget
, WID_LGL_CARGO_FIRST
, WID_LGL_CARGO_LAST
+ 1)) {
496 if (this->IsWidgetDisabled(widget
)) return;
497 CargoSpec
*cargo
= CargoSpec::Get(widget
- WID_LGL_CARGO_FIRST
);
498 GfxFillRect(r
.left
+ 2, r
.top
+ 2, r
.right
- 2, r
.bottom
- 2, cargo
->legend_colour
);
499 DrawString(r
.left
, r
.right
, (r
.top
+ r
.bottom
+ 1 - FONT_HEIGHT_SMALL
) / 2, cargo
->abbrev
, TC_BLACK
, SA_HOR_CENTER
);
504 * Update the overlay with the new company selection.
506 void LinkGraphLegendWindow::UpdateOverlayCompanies()
509 for (uint c
= 0; c
< MAX_COMPANIES
; c
++) {
510 if (this->IsWidgetDisabled(c
+ WID_LGL_COMPANY_FIRST
)) continue;
511 if (!this->IsWidgetLowered(c
+ WID_LGL_COMPANY_FIRST
)) continue;
514 this->overlay
->SetCompanyMask(mask
);
518 * Update the overlay with the new cargo selection.
520 void LinkGraphLegendWindow::UpdateOverlayCargoes()
523 for (uint c
= 0; c
< NUM_CARGO
; c
++) {
524 if (this->IsWidgetDisabled(c
+ WID_LGL_CARGO_FIRST
)) continue;
525 if (!this->IsWidgetLowered(c
+ WID_LGL_CARGO_FIRST
)) continue;
528 this->overlay
->SetCargoMask(mask
);
531 void LinkGraphLegendWindow::OnClick(Point pt
, int widget
, int click_count
)
533 /* Check which button is clicked */
534 if (IsInsideMM(widget
, WID_LGL_COMPANY_FIRST
, WID_LGL_COMPANY_LAST
+ 1)) {
535 if (!this->IsWidgetDisabled(widget
)) {
536 this->ToggleWidgetLoweredState(widget
);
537 this->UpdateOverlayCompanies();
539 } else if (widget
== WID_LGL_COMPANIES_ALL
|| widget
== WID_LGL_COMPANIES_NONE
) {
540 for (uint c
= 0; c
< MAX_COMPANIES
; c
++) {
541 if (this->IsWidgetDisabled(c
+ WID_LGL_COMPANY_FIRST
)) continue;
542 this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST
+ c
, widget
== WID_LGL_COMPANIES_ALL
);
544 this->UpdateOverlayCompanies();
546 } else if (IsInsideMM(widget
, WID_LGL_CARGO_FIRST
, WID_LGL_CARGO_LAST
+ 1)) {
547 if (!this->IsWidgetDisabled(widget
)) {
548 this->ToggleWidgetLoweredState(widget
);
549 this->UpdateOverlayCargoes();
551 } else if (widget
== WID_LGL_CARGOES_ALL
|| widget
== WID_LGL_CARGOES_NONE
) {
552 for (uint c
= 0; c
< NUM_CARGO
; c
++) {
553 if (this->IsWidgetDisabled(c
+ WID_LGL_CARGO_FIRST
)) continue;
554 this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST
+ c
, widget
== WID_LGL_CARGOES_ALL
);
556 this->UpdateOverlayCargoes();
562 * Invalidate the data of this window if the cargoes or companies have changed.
563 * @param data ignored
564 * @param gui_scope ignored
566 void LinkGraphLegendWindow::OnInvalidateData(int data
, bool gui_scope
)
568 /* Disable the companies who are not active */
569 for (CompanyID i
= COMPANY_FIRST
; i
< MAX_COMPANIES
; i
++) {
570 this->SetWidgetDisabledState(i
+ WID_LGL_COMPANY_FIRST
, !Company::IsValidID(i
));
572 for (CargoID i
= 0; i
< NUM_CARGO
; i
++) {
573 this->SetWidgetDisabledState(i
+ WID_LGL_CARGO_FIRST
, !CargoSpec::Get(i
)->IsValid());