1 /* $Id: linkgraph_gui.cpp 25912 2013-10-23 19:42:17Z fonsinchen $ */
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 "../3rdparty/cpp-btree/btree_map.h"
27 #include "../safeguards.h"
30 * Colours for the various "load" states of links. Ordered from "unused" to
33 const uint8
LinkGraphOverlay::LINK_COLOURS
[] = {
34 0x0f, 0xd1, 0xd0, 0x57,
35 0x55, 0x53, 0xbf, 0xbd,
36 0xba, 0xb9, 0xb7, 0xb5
40 * Get a DPI for the widget we will be drawing to.
41 * @param dpi DrawPixelInfo to fill with the desired dimensions.
43 void LinkGraphOverlay::GetWidgetDpi(DrawPixelInfo
*dpi
) const
45 const NWidgetBase
*wi
= this->window
->GetWidget
<NWidgetBase
>(this->widget_id
);
46 dpi
->left
= dpi
->top
= 0;
47 dpi
->width
= wi
->current_x
;
48 dpi
->height
= wi
->current_y
;
52 * Rebuild the cache and recalculate which links and stations to be shown.
54 void LinkGraphOverlay::RebuildCache()
56 this->cached_links
.clear();
57 this->cached_stations
.clear();
58 if (this->company_mask
== 0) return;
61 this->GetWidgetDpi(&dpi
);
63 struct LinkCacheItem
{
68 btree::btree_map
<std::pair
<StationID
, StationID
>, LinkCacheItem
> link_cache_map
;
70 auto AddLinks
= [&](const Station
*from
, const Station
*to
, Point from_pt
, Point to_pt
) {
71 LinkCacheItem
*item
= nullptr;
73 FOR_EACH_SET_CARGO_ID(c
, this->cargo_mask
) {
74 if (!CargoSpec::Get(c
)->IsValid()) continue;
75 const GoodsEntry
&ge
= from
->goods
[c
];
76 if (!LinkGraph::IsValidID(ge
.link_graph
) ||
77 ge
.link_graph
!= to
->goods
[c
].link_graph
) {
80 const LinkGraph
&lg
= *LinkGraph::Get(ge
.link_graph
);
81 ConstEdge edge
= lg
[ge
.node
][to
->goods
[c
].node
];
82 if (edge
.Capacity() > 0) {
84 item
= &link_cache_map
[std::make_pair(from
->index
, to
->index
)];
85 if (item
->prop
.capacity
== 0) {
86 item
->from_pt
= from_pt
;
90 this->AddStats(lg
.Monthly(edge
.Capacity()), lg
.Monthly(edge
.Usage()),
91 ge
.flows
.GetFlowVia(to
->index
), from
->owner
== OWNER_NONE
|| to
->owner
== OWNER_NONE
,
98 FOR_ALL_STATIONS(sta
) {
99 if (sta
->rect
.IsEmpty()) continue;
101 Point pta
= this->GetStationMiddle(sta
);
103 StationID from
= sta
->index
;
107 FOR_EACH_SET_CARGO_ID(c
, this->cargo_mask
) {
108 if (!CargoSpec::Get(c
)->IsValid()) continue;
109 if (!LinkGraph::IsValidID(sta
->goods
[c
].link_graph
)) continue;
110 const LinkGraph
&lg
= *LinkGraph::Get(sta
->goods
[c
].link_graph
);
112 ConstNode from_node
= lg
[sta
->goods
[c
].node
];
113 supply
+= lg
.Monthly(from_node
.Supply());
114 for (ConstEdgeIterator i
= from_node
.Begin(); i
!= from_node
.End(); ++i
) {
115 StationID to
= lg
[i
->first
].Station();
117 if (!Station::IsValidID(to
) || link_cache_map
.count(std::make_pair(from
, to
))) {
120 const Station
*stb
= Station::Get(to
);
123 /* Show links between stations of selected companies or "neutral" ones like oilrigs. */
124 if (stb
->owner
!= OWNER_NONE
&& sta
->owner
!= OWNER_NONE
&& !HasBit(this->company_mask
, stb
->owner
)) continue;
125 if (stb
->rect
.IsEmpty()) continue;
127 Point ptb
= this->GetStationMiddle(stb
);
129 if (!this->IsLinkVisible(pta
, ptb
, &dpi
)) continue;
131 AddLinks(sta
, stb
, pta
, ptb
);
134 if (this->IsPointVisible(pta
, &dpi
)) {
135 this->cached_stations
.push_back({ from
, supply
, pta
});
139 this->cached_links
.reserve(link_cache_map
.size());
140 for (auto &iter
: link_cache_map
) {
141 this->cached_links
.push_back({ iter
.first
.first
, iter
.first
.second
, iter
.second
.from_pt
, iter
.second
.to_pt
, iter
.second
.prop
});
146 * Determine if a certain point is inside the given DPI, with some lee way.
147 * @param pt Point we are looking for.
148 * @param dpi Visible area.
149 * @param padding Extent of the point.
150 * @return If the point or any of its 'extent' is inside the dpi.
152 inline bool LinkGraphOverlay::IsPointVisible(Point pt
, const DrawPixelInfo
*dpi
, int padding
) const
154 return pt
.x
> dpi
->left
- padding
&& pt
.y
> dpi
->top
- padding
&&
155 pt
.x
< dpi
->left
+ dpi
->width
+ padding
&&
156 pt
.y
< dpi
->top
+ dpi
->height
+ padding
;
160 * Determine if a certain link crosses through the area given by the dpi with some lee way.
161 * @param pta First end of the link.
162 * @param ptb Second end of the link.
163 * @param dpi Visible area.
164 * @param padding Width or thickness of the link.
165 * @return If the link or any of its "thickness" is visible. This may return false positives.
167 inline bool LinkGraphOverlay::IsLinkVisible(Point pta
, Point ptb
, const DrawPixelInfo
*dpi
, int padding
) const
169 const int left
= dpi
->left
- padding
;
170 const int right
= dpi
->left
+ dpi
->width
+ padding
;
171 const int top
= dpi
->top
- padding
;
172 const int bottom
= dpi
->top
+ dpi
->height
+ padding
;
174 // Cut-down Cohen–Sutherland algorithm
176 const unsigned char INSIDE
= 0; // 0000
177 const unsigned char LEFT
= 1; // 0001
178 const unsigned char RIGHT
= 2; // 0010
179 const unsigned char BOTTOM
= 4; // 0100
180 const unsigned char TOP
= 8; // 1000
187 auto out_code
= [&](int x
, int y
) -> unsigned char {
188 unsigned char out
= INSIDE
;
191 } else if (x
> right
) {
196 } else if (y
> bottom
) {
202 unsigned char c0
= out_code(x0
, y0
);
203 unsigned char c1
= out_code(x1
, y1
);
206 if (c0
== 0 || c1
== 0) return true;
207 if ((c0
& c1
) != 0) return false;
209 if (c0
& TOP
) { // point 0 is above the clip window
210 x0
= x0
+ (int)(((int64
) (x1
- x0
)) * ((int64
) (top
- y0
)) / ((int64
) (y1
- y0
)));
212 } else if (c0
& BOTTOM
) { // point 0 is below the clip window
213 x0
= x0
+ (int)(((int64
) (x1
- x0
)) * ((int64
) (bottom
- y0
)) / ((int64
) (y1
- y0
)));
215 } else if (c0
& RIGHT
) { // point 0 is to the right of clip window
216 y0
= y0
+ (int)(((int64
) (y1
- y0
)) * ((int64
) (right
- x0
)) / ((int64
) (x1
- x0
)));
218 } else if (c0
& LEFT
) { // point 0 is to the left of clip window
219 y0
= y0
+ (int)(((int64
) (y1
- y0
)) * ((int64
) (left
- x0
)) / ((int64
) (x1
- x0
)));
223 c0
= out_code(x0
, y0
);
230 * Add information from a given pair of link stat and flow stat to the given
231 * link properties. The shown usage or plan is always the maximum of all link
233 * @param new_cap Capacity of the new link.
234 * @param new_usg Usage of the new link.
235 * @param new_plan Planned flow for the new link.
236 * @param new_shared If the new link is shared.
237 * @param cargo LinkProperties to write the information to.
239 /* static */ void LinkGraphOverlay::AddStats(uint new_cap
, uint new_usg
, uint new_plan
, bool new_shared
, LinkProperties
&cargo
)
241 /* multiply the numbers by 32 in order to avoid comparing to 0 too often. */
242 if (cargo
.capacity
== 0 ||
243 max(cargo
.usage
, cargo
.planned
) * 32 / (cargo
.capacity
+ 1) < max(new_usg
, new_plan
) * 32 / (new_cap
+ 1)) {
244 cargo
.capacity
= new_cap
;
245 cargo
.usage
= new_usg
;
246 cargo
.planned
= new_plan
;
248 if (new_shared
) cargo
.shared
= true;
251 void LinkGraphOverlay::RefreshDrawCache()
253 for (StationSupplyList::iterator
i(this->cached_stations
.begin()); i
!= this->cached_stations
.end(); ++i
) {
254 const Station
*st
= Station::GetIfValid(i
->id
);
255 if (st
== NULL
) continue;
257 i
->pt
= this->GetStationMiddle(st
);
259 for (LinkList::iterator
i(this->cached_links
.begin()); i
!= this->cached_links
.end(); ++i
) {
260 const Station
*sta
= Station::GetIfValid(i
->from_id
);
261 if (sta
== NULL
) continue;
262 const Station
*stb
= Station::GetIfValid(i
->to_id
);
263 if (stb
== NULL
) continue;
265 i
->from_pt
= this->GetStationMiddle(sta
);
266 i
->to_pt
= this->GetStationMiddle(stb
);
271 * Draw the linkgraph overlay or some part of it, in the area given.
272 * @param dpi Area to be drawn to.
274 void LinkGraphOverlay::Draw(const DrawPixelInfo
*dpi
)
276 if (this->last_update_number
!= GetWindowUpdateNumber()) {
277 this->last_update_number
= GetWindowUpdateNumber();
278 this->RefreshDrawCache();
281 this->DrawLinks(dpi
);
282 this->DrawStationDots(dpi
);
286 * Draw the cached links or part of them into the given area.
287 * @param dpi Area to be drawn to.
289 void LinkGraphOverlay::DrawLinks(const DrawPixelInfo
*dpi
) const
291 for (LinkList::const_iterator
i(this->cached_links
.begin()); i
!= this->cached_links
.end(); ++i
) {
292 if (!this->IsLinkVisible(i
->from_pt
, i
->to_pt
, dpi
, this->scale
+ 2)) continue;
293 if (!Station::IsValidID(i
->from_id
)) continue;
294 if (!Station::IsValidID(i
->to_id
)) continue;
295 this->DrawContent(i
->from_pt
, i
->to_pt
, i
->prop
);
300 * Draw one specific link.
301 * @param pta Source of the link.
302 * @param ptb Destination of the link.
303 * @param cargo Properties of the link.
305 void LinkGraphOverlay::DrawContent(Point pta
, Point ptb
, const LinkProperties
&cargo
) const
307 uint usage_or_plan
= min(cargo
.capacity
* 2 + 1, max(cargo
.usage
, cargo
.planned
));
308 int colour
= LinkGraphOverlay::LINK_COLOURS
[usage_or_plan
* lengthof(LinkGraphOverlay::LINK_COLOURS
) / (cargo
.capacity
* 2 + 2)];
309 int dash
= cargo
.shared
? this->scale
* 4 : 0;
311 /* Move line a bit 90° against its dominant direction to prevent it from
312 * being hidden below the grey line. */
313 int side
= _settings_game
.vehicle
.road_side
? 1 : -1;
314 if (abs(pta
.x
- ptb
.x
) < abs(pta
.y
- ptb
.y
)) {
315 int offset_x
= (pta
.y
> ptb
.y
? 1 : -1) * side
* this->scale
;
316 GfxDrawLine(pta
.x
+ offset_x
, pta
.y
, ptb
.x
+ offset_x
, ptb
.y
, colour
, this->scale
, dash
);
318 int offset_y
= (pta
.x
< ptb
.x
? 1 : -1) * side
* this->scale
;
319 GfxDrawLine(pta
.x
, pta
.y
+ offset_y
, ptb
.x
, ptb
.y
+ offset_y
, colour
, this->scale
, dash
);
322 GfxDrawLine(pta
.x
, pta
.y
, ptb
.x
, ptb
.y
, _colour_gradient
[COLOUR_GREY
][1], this->scale
);
326 * Draw dots for stations into the smallmap. The dots' sizes are determined by the amount of
327 * cargo produced there, their colours by the type of cargo produced.
329 void LinkGraphOverlay::DrawStationDots(const DrawPixelInfo
*dpi
) const
331 for (StationSupplyList::const_iterator
i(this->cached_stations
.begin()); i
!= this->cached_stations
.end(); ++i
) {
332 const Point
&pt
= i
->pt
;
333 if (!this->IsPointVisible(pt
, dpi
, 3 * this->scale
)) continue;
335 const Station
*st
= Station::GetIfValid(i
->id
);
336 if (st
== NULL
) continue;
338 uint r
= this->scale
* 2 + this->scale
* 2 * min(200, i
->quantity
) / 200;
340 LinkGraphOverlay::DrawVertex(pt
.x
, pt
.y
, r
,
341 _colour_gradient
[st
->owner
!= OWNER_NONE
?
342 (Colours
)Company::Get(st
->owner
)->colour
: COLOUR_GREY
][5],
343 _colour_gradient
[COLOUR_GREY
][1]);
348 * Draw a square symbolizing a producer of cargo.
349 * @param x X coordinate of the middle of the vertex.
350 * @param y Y coordinate of the middle of the vertex.
351 * @param size Y and y extend of the vertex.
352 * @param colour Colour with which the vertex will be filled.
353 * @param border_colour Colour for the border of the vertex.
355 /* static */ void LinkGraphOverlay::DrawVertex(int x
, int y
, int size
, int colour
, int border_colour
)
359 int w2
= size
/ 2 + size
% 2;
361 GfxFillRect(x
- w1
, y
- w1
, x
+ w2
, y
+ w2
, colour
);
365 GfxDrawLine(x
- w1
, y
- w1
, x
+ w2
, y
- w1
, border_colour
);
366 GfxDrawLine(x
- w1
, y
+ w2
, x
+ w2
, y
+ w2
, border_colour
);
367 GfxDrawLine(x
- w1
, y
- w1
, x
- w1
, y
+ w2
, border_colour
);
368 GfxDrawLine(x
+ w2
, y
- w1
, x
+ w2
, y
+ w2
, border_colour
);
372 * Determine the middle of a station in the current window.
373 * @param st The station we're looking for.
374 * @return Middle point of the station in the current window.
376 Point
LinkGraphOverlay::GetStationMiddle(const Station
*st
) const
378 if (this->window
->viewport
!= NULL
) {
379 return GetViewportStationMiddle(this->window
->viewport
, st
);
381 /* assume this is a smallmap */
382 return static_cast<const SmallMapWindow
*>(this->window
)->GetStationMiddle(st
);
387 * Set a new cargo mask and rebuild the cache.
388 * @param cargo_mask New cargo mask.
390 void LinkGraphOverlay::SetCargoMask(uint32 cargo_mask
)
392 this->cargo_mask
= cargo_mask
;
393 this->RebuildCache();
394 this->window
->GetWidget
<NWidgetBase
>(this->widget_id
)->SetDirty(this->window
);
398 * Set a new company mask and rebuild the cache.
399 * @param company_mask New company mask.
401 void LinkGraphOverlay::SetCompanyMask(uint32 company_mask
)
403 this->company_mask
= company_mask
;
404 this->RebuildCache();
405 this->window
->GetWidget
<NWidgetBase
>(this->widget_id
)->SetDirty(this->window
);
408 /** Make a number of rows with buttons for each company for the linkgraph legend window. */
409 NWidgetBase
*MakeCompanyButtonRowsLinkGraphGUI(int *biggest_index
)
411 return MakeCompanyButtonRows(biggest_index
, WID_LGL_COMPANY_FIRST
, WID_LGL_COMPANY_LAST
, 3, STR_LINKGRAPH_LEGEND_SELECT_COMPANIES
);
414 NWidgetBase
*MakeSaturationLegendLinkGraphGUI(int *biggest_index
)
416 NWidgetVertical
*panel
= new NWidgetVertical(NC_EQUALSIZE
);
417 for (uint i
= 0; i
< lengthof(LinkGraphOverlay::LINK_COLOURS
); ++i
) {
418 NWidgetBackground
* wid
= new NWidgetBackground(WWT_PANEL
, COLOUR_DARK_GREEN
, i
+ WID_LGL_SATURATION_FIRST
);
419 wid
->SetMinimalSize(50, FONT_HEIGHT_SMALL
);
421 wid
->SetResize(0, 0);
424 *biggest_index
= WID_LGL_SATURATION_LAST
;
428 NWidgetBase
*MakeCargoesLegendLinkGraphGUI(int *biggest_index
)
430 static const uint ENTRIES_PER_ROW
= CeilDiv(NUM_CARGO
, 5);
431 NWidgetVertical
*panel
= new NWidgetVertical(NC_EQUALSIZE
);
432 NWidgetHorizontal
*row
= NULL
;
433 for (uint i
= 0; i
< NUM_CARGO
; ++i
) {
434 if (i
% ENTRIES_PER_ROW
== 0) {
435 if (row
) panel
->Add(row
);
436 row
= new NWidgetHorizontal(NC_EQUALSIZE
);
438 NWidgetBackground
* wid
= new NWidgetBackground(WWT_PANEL
, COLOUR_GREY
, i
+ WID_LGL_CARGO_FIRST
);
439 wid
->SetMinimalSize(25, FONT_HEIGHT_SMALL
);
441 wid
->SetResize(0, 0);
444 /* Fill up last row */
445 for (uint i
= 0; i
< 4 - (NUM_CARGO
- 1) % 5; ++i
) {
446 NWidgetSpacer
*spc
= new NWidgetSpacer(25, FONT_HEIGHT_SMALL
);
448 spc
->SetResize(0, 0);
452 *biggest_index
= WID_LGL_CARGO_LAST
;
457 static const NWidgetPart _nested_linkgraph_legend_widgets
[] = {
458 NWidget(NWID_HORIZONTAL
),
459 NWidget(WWT_CLOSEBOX
, COLOUR_DARK_GREEN
),
460 NWidget(WWT_CAPTION
, COLOUR_DARK_GREEN
, WID_LGL_CAPTION
), SetDataTip(STR_LINKGRAPH_LEGEND_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
461 NWidget(WWT_SHADEBOX
, COLOUR_DARK_GREEN
),
462 NWidget(WWT_STICKYBOX
, COLOUR_DARK_GREEN
),
464 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
),
465 NWidget(NWID_HORIZONTAL
),
466 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_LGL_SATURATION
),
467 SetPadding(WD_FRAMERECT_TOP
, 0, WD_FRAMERECT_BOTTOM
, WD_CAPTIONTEXT_LEFT
),
468 NWidgetFunction(MakeSaturationLegendLinkGraphGUI
),
470 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_LGL_COMPANIES
),
471 SetPadding(WD_FRAMERECT_TOP
, 0, WD_FRAMERECT_BOTTOM
, WD_CAPTIONTEXT_LEFT
),
472 NWidget(NWID_VERTICAL
, NC_EQUALSIZE
),
473 NWidgetFunction(MakeCompanyButtonRowsLinkGraphGUI
),
474 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_LGL_COMPANIES_ALL
), SetDataTip(STR_LINKGRAPH_LEGEND_ALL
, STR_NULL
),
475 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_LGL_COMPANIES_NONE
), SetDataTip(STR_LINKGRAPH_LEGEND_NONE
, STR_NULL
),
478 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_LGL_CARGOES
),
479 SetPadding(WD_FRAMERECT_TOP
, WD_FRAMERECT_RIGHT
, WD_FRAMERECT_BOTTOM
, WD_CAPTIONTEXT_LEFT
),
480 NWidget(NWID_VERTICAL
, NC_EQUALSIZE
),
481 NWidgetFunction(MakeCargoesLegendLinkGraphGUI
),
482 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_LGL_CARGOES_ALL
), SetDataTip(STR_LINKGRAPH_LEGEND_ALL
, STR_NULL
),
483 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_LGL_CARGOES_NONE
), SetDataTip(STR_LINKGRAPH_LEGEND_NONE
, STR_NULL
),
490 assert_compile(WID_LGL_SATURATION_LAST
- WID_LGL_SATURATION_FIRST
==
491 lengthof(LinkGraphOverlay::LINK_COLOURS
) - 1);
493 static WindowDesc
_linkgraph_legend_desc(
494 WDP_AUTO
, "toolbar_linkgraph", 0, 0,
495 WC_LINKGRAPH_LEGEND
, WC_NONE
,
497 _nested_linkgraph_legend_widgets
, lengthof(_nested_linkgraph_legend_widgets
)
501 * Open a link graph legend window.
503 void ShowLinkGraphLegend()
505 AllocateWindowDescFront
<LinkGraphLegendWindow
>(&_linkgraph_legend_desc
, 0);
508 LinkGraphLegendWindow::LinkGraphLegendWindow(WindowDesc
*desc
, int window_number
) : Window(desc
)
510 this->InitNested(window_number
);
511 this->InvalidateData(0);
512 this->SetOverlay(FindWindowById(WC_MAIN_WINDOW
, 0)->viewport
->overlay
);
516 * Set the overlay belonging to this menu and import its company/cargo settings.
517 * @params overlay New overlay for this menu.
519 void LinkGraphLegendWindow::SetOverlay(LinkGraphOverlay
*overlay
) {
520 this->overlay
= overlay
;
521 uint32 companies
= this->overlay
->GetCompanyMask();
522 for (uint c
= 0; c
< MAX_COMPANIES
; c
++) {
523 if (!this->IsWidgetDisabled(WID_LGL_COMPANY_FIRST
+ c
)) {
524 this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST
+ c
, HasBit(companies
, c
));
527 uint32 cargoes
= this->overlay
->GetCargoMask();
528 for (uint c
= 0; c
< NUM_CARGO
; c
++) {
529 if (!this->IsWidgetDisabled(WID_LGL_CARGO_FIRST
+ c
)) {
530 this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST
+ c
, HasBit(cargoes
, c
));
535 void LinkGraphLegendWindow::UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
537 if (IsInsideMM(widget
, WID_LGL_SATURATION_FIRST
, WID_LGL_SATURATION_LAST
+ 1)) {
538 StringID str
= STR_NULL
;
539 if (widget
== WID_LGL_SATURATION_FIRST
) {
540 str
= STR_LINKGRAPH_LEGEND_UNUSED
;
541 } else if (widget
== WID_LGL_SATURATION_LAST
) {
542 str
= STR_LINKGRAPH_LEGEND_OVERLOADED
;
543 } else if (widget
== (WID_LGL_SATURATION_LAST
+ WID_LGL_SATURATION_FIRST
) / 2) {
544 str
= STR_LINKGRAPH_LEGEND_SATURATED
;
546 if (str
!= STR_NULL
) {
547 Dimension dim
= GetStringBoundingBox(str
);
548 dim
.width
+= WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
549 dim
.height
+= WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
550 *size
= maxdim(*size
, dim
);
553 if (IsInsideMM(widget
, WID_LGL_CARGO_FIRST
, WID_LGL_CARGO_LAST
+ 1)) {
554 CargoSpec
*cargo
= CargoSpec::Get(widget
- WID_LGL_CARGO_FIRST
);
555 if (cargo
->IsValid()) {
556 Dimension dim
= GetStringBoundingBox(cargo
->abbrev
);
557 dim
.width
+= WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
558 dim
.height
+= WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
559 *size
= maxdim(*size
, dim
);
564 void LinkGraphLegendWindow::DrawWidget(const Rect
&r
, int widget
) const
566 if (IsInsideMM(widget
, WID_LGL_COMPANY_FIRST
, WID_LGL_COMPANY_LAST
+ 1)) {
567 if (this->IsWidgetDisabled(widget
)) return;
568 CompanyID cid
= (CompanyID
)(widget
- WID_LGL_COMPANY_FIRST
);
569 Dimension sprite_size
= GetSpriteSize(SPR_COMPANY_ICON
);
570 DrawCompanyIcon(cid
, (r
.left
+ r
.right
+ 1 - sprite_size
.width
) / 2, (r
.top
+ r
.bottom
+ 1 - sprite_size
.height
) / 2);
572 if (IsInsideMM(widget
, WID_LGL_SATURATION_FIRST
, WID_LGL_SATURATION_LAST
+ 1)) {
573 GfxFillRect(r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.bottom
- 1, LinkGraphOverlay::LINK_COLOURS
[widget
- WID_LGL_SATURATION_FIRST
]);
574 StringID str
= STR_NULL
;
575 if (widget
== WID_LGL_SATURATION_FIRST
) {
576 str
= STR_LINKGRAPH_LEGEND_UNUSED
;
577 } else if (widget
== WID_LGL_SATURATION_LAST
) {
578 str
= STR_LINKGRAPH_LEGEND_OVERLOADED
;
579 } else if (widget
== (WID_LGL_SATURATION_LAST
+ WID_LGL_SATURATION_FIRST
) / 2) {
580 str
= STR_LINKGRAPH_LEGEND_SATURATED
;
582 if (str
!= STR_NULL
) DrawString(r
.left
, r
.right
, (r
.top
+ r
.bottom
+ 1 - FONT_HEIGHT_SMALL
) / 2, str
, TC_FROMSTRING
, SA_HOR_CENTER
);
584 if (IsInsideMM(widget
, WID_LGL_CARGO_FIRST
, WID_LGL_CARGO_LAST
+ 1)) {
585 if (this->IsWidgetDisabled(widget
)) return;
586 CargoSpec
*cargo
= CargoSpec::Get(widget
- WID_LGL_CARGO_FIRST
);
587 GfxFillRect(r
.left
+ 2, r
.top
+ 2, r
.right
- 2, r
.bottom
- 2, cargo
->legend_colour
);
588 DrawString(r
.left
, r
.right
, (r
.top
+ r
.bottom
+ 1 - FONT_HEIGHT_SMALL
) / 2, cargo
->abbrev
, GetContrastColour(cargo
->legend_colour
, 42), SA_HOR_CENTER
);
593 * Update the overlay with the new company selection.
595 void LinkGraphLegendWindow::UpdateOverlayCompanies()
598 for (uint c
= 0; c
< MAX_COMPANIES
; c
++) {
599 if (this->IsWidgetDisabled(c
+ WID_LGL_COMPANY_FIRST
)) continue;
600 if (!this->IsWidgetLowered(c
+ WID_LGL_COMPANY_FIRST
)) continue;
603 this->overlay
->SetCompanyMask(mask
);
607 * Update the overlay with the new cargo selection.
609 void LinkGraphLegendWindow::UpdateOverlayCargoes()
612 for (uint c
= 0; c
< NUM_CARGO
; c
++) {
613 if (this->IsWidgetDisabled(c
+ WID_LGL_CARGO_FIRST
)) continue;
614 if (!this->IsWidgetLowered(c
+ WID_LGL_CARGO_FIRST
)) continue;
617 this->overlay
->SetCargoMask(mask
);
620 void LinkGraphLegendWindow::OnClick(Point pt
, int widget
, int click_count
)
622 /* Check which button is clicked */
623 if (IsInsideMM(widget
, WID_LGL_COMPANY_FIRST
, WID_LGL_COMPANY_LAST
+ 1)) {
624 if (!this->IsWidgetDisabled(widget
)) {
625 this->ToggleWidgetLoweredState(widget
);
626 this->UpdateOverlayCompanies();
628 } else if (widget
== WID_LGL_COMPANIES_ALL
|| widget
== WID_LGL_COMPANIES_NONE
) {
629 for (uint c
= 0; c
< MAX_COMPANIES
; c
++) {
630 if (this->IsWidgetDisabled(c
+ WID_LGL_COMPANY_FIRST
)) continue;
631 this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST
+ c
, widget
== WID_LGL_COMPANIES_ALL
);
633 this->UpdateOverlayCompanies();
635 } else if (IsInsideMM(widget
, WID_LGL_CARGO_FIRST
, WID_LGL_CARGO_LAST
+ 1)) {
636 if (!this->IsWidgetDisabled(widget
)) {
637 this->ToggleWidgetLoweredState(widget
);
638 this->UpdateOverlayCargoes();
640 } else if (widget
== WID_LGL_CARGOES_ALL
|| widget
== WID_LGL_CARGOES_NONE
) {
641 for (uint c
= 0; c
< NUM_CARGO
; c
++) {
642 if (this->IsWidgetDisabled(c
+ WID_LGL_CARGO_FIRST
)) continue;
643 this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST
+ c
, widget
== WID_LGL_CARGOES_ALL
);
645 this->UpdateOverlayCargoes();
651 * Invalidate the data of this window if the cargoes or companies have changed.
652 * @param data ignored
653 * @param gui_scope ignored
655 void LinkGraphLegendWindow::OnInvalidateData(int data
, bool gui_scope
)
657 /* Disable the companies who are not active */
658 for (CompanyID i
= COMPANY_FIRST
; i
< MAX_COMPANIES
; i
++) {
659 this->SetWidgetDisabledState(i
+ WID_LGL_COMPANY_FIRST
, !Company::IsValidID(i
));
661 for (CargoID i
= 0; i
< NUM_CARGO
; i
++) {
662 this->SetWidgetDisabledState(i
+ WID_LGL_CARGO_FIRST
, !CargoSpec::Get(i
)->IsValid());