OpenTTD Source  1.11.0-beta2
linkgraph_gui.cpp
Go to the documentation of this file.
1 /*
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/>.
6  */
7 
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"
20 
21 #include "table/strings.h"
22 
23 #include "../safeguards.h"
24 
29 const uint8 LinkGraphOverlay::LINK_COLOURS[] = {
30  0x0f, 0xd1, 0xd0, 0x57,
31  0x55, 0x53, 0xbf, 0xbd,
32  0xba, 0xb9, 0xb7, 0xb5
33 };
34 
40 {
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;
45 }
46 
51 {
52  this->cached_links.clear();
53  this->cached_stations.clear();
54  if (this->company_mask == 0) return;
55 
56  DrawPixelInfo dpi;
57  this->GetWidgetDpi(&dpi);
58 
59  for (const Station *sta : Station::Iterate()) {
60  if (sta->rect.IsEmpty()) continue;
61 
62  Point pta = this->GetStationMiddle(sta);
63 
64  StationID from = sta->index;
65  StationLinkMap &seen_links = this->cached_links[from];
66 
67  uint supply = 0;
68  CargoID c;
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);
73 
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();
78  assert(from != to);
79  if (!Station::IsValidID(to) || seen_links.find(to) != seen_links.end()) {
80  continue;
81  }
82  const Station *stb = Station::Get(to);
83  assert(sta != stb);
84 
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;
88 
89  if (!this->IsLinkVisible(pta, this->GetStationMiddle(stb), &dpi)) continue;
90 
91  this->AddLinks(sta, stb);
92  seen_links[to]; // make sure it is created and marked as seen
93  }
94  }
95  if (this->IsPointVisible(pta, &dpi)) {
96  this->cached_stations.push_back(std::make_pair(from, supply));
97  }
98  }
99 }
100 
108 inline bool LinkGraphOverlay::IsPointVisible(Point pt, const DrawPixelInfo *dpi, int padding) const
109 {
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;
113 }
114 
123 inline bool LinkGraphOverlay::IsLinkVisible(Point pta, Point ptb, const DrawPixelInfo *dpi, int padding) const
124 {
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;
129 
130  /*
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
133  */
134 
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
140 
141  int x0 = pta.x;
142  int y0 = pta.y;
143  int x1 = ptb.x;
144  int y1 = ptb.y;
145 
146  auto out_code = [&](int x, int y) -> uint8 {
147  uint8 out = INSIDE;
148  if (x < left) {
149  out |= LEFT;
150  } else if (x > right) {
151  out |= RIGHT;
152  }
153  if (y < top) {
154  out |= TOP;
155  } else if (y > bottom) {
156  out |= BOTTOM;
157  }
158  return out;
159  };
160 
161  uint8 c0 = out_code(x0, y0);
162  uint8 c1 = out_code(x1, y1);
163 
164  while (true) {
165  if (c0 == 0 || c1 == 0) return true;
166  if ((c0 & c1) != 0) return false;
167 
168  if (c0 & TOP) { // point 0 is above the clip window
169  x0 = x0 + (int)(((int64) (x1 - x0)) * ((int64) (top - y0)) / ((int64) (y1 - y0)));
170  y0 = top;
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)));
173  y0 = bottom;
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)));
176  x0 = right;
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)));
179  x0 = left;
180  }
181 
182  c0 = out_code(x0, y0);
183  }
184 
185  NOT_REACHED();
186 }
187 
193 void LinkGraphOverlay::AddLinks(const Station *from, const Station *to)
194 {
195  CargoID c;
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) {
201  continue;
202  }
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]);
209  }
210  }
211 }
212 
223 /* static */ void LinkGraphOverlay::AddStats(uint new_cap, uint new_usg, uint new_plan, bool new_shared, LinkProperties &cargo)
224 {
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;
231  }
232  if (new_shared) cargo.shared = true;
233 }
234 
240 {
241  if (this->dirty) {
242  this->RebuildCache();
243  this->dirty = false;
244  }
245  this->DrawLinks(dpi);
246  this->DrawStationDots(dpi);
247 }
248 
254 {
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);
263  }
264  }
265 }
266 
274 {
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;
278 
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);
285  } else {
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);
288  }
289 
290  GfxDrawLine(pta.x, pta.y, ptb.x, ptb.y, _colour_gradient[COLOUR_GREY][1], this->scale);
291 }
292 
298 {
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;
304 
305  uint r = this->scale * 2 + this->scale * 2 * std::min(200U, i->second) / 200;
306 
307  LinkGraphOverlay::DrawVertex(pt.x, pt.y, r,
309  (Colours)Company::Get(st->owner)->colour : COLOUR_GREY][5],
310  _colour_gradient[COLOUR_GREY][1]);
311  }
312 }
313 
322 /* static */ void LinkGraphOverlay::DrawVertex(int x, int y, int size, int colour, int border_colour)
323 {
324  size--;
325  int w1 = size / 2;
326  int w2 = size / 2 + size % 2;
327 
328  GfxFillRect(x - w1, y - w1, x + w2, y + w2, colour);
329 
330  w1++;
331  w2++;
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);
336 }
337 
344 {
345  if (this->window->viewport != nullptr) {
346  return GetViewportStationMiddle(this->window->viewport, st);
347  } else {
348  /* assume this is a smallmap */
349  return static_cast<const SmallMapWindow *>(this->window)->GetStationMiddle(st);
350  }
351 }
352 
357 void LinkGraphOverlay::SetCargoMask(CargoTypes cargo_mask)
358 {
359  this->cargo_mask = cargo_mask;
360  this->RebuildCache();
361  this->window->GetWidget<NWidgetBase>(this->widget_id)->SetDirty(this->window);
362 }
363 
368 void LinkGraphOverlay::SetCompanyMask(uint32 company_mask)
369 {
370  this->company_mask = company_mask;
371  this->RebuildCache();
372  this->window->GetWidget<NWidgetBase>(this->widget_id)->SetDirty(this->window);
373 }
374 
377 {
378  return MakeCompanyButtonRows(biggest_index, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST, 3, STR_NULL);
379 }
380 
381 NWidgetBase *MakeSaturationLegendLinkGraphGUI(int *biggest_index)
382 {
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);
387  wid->SetFill(1, 1);
388  wid->SetResize(0, 0);
389  panel->Add(wid);
390  }
391  *biggest_index = WID_LGL_SATURATION_LAST;
392  return panel;
393 }
394 
395 NWidgetBase *MakeCargoesLegendLinkGraphGUI(int *biggest_index)
396 {
397  static const uint ENTRIES_PER_ROW = CeilDiv(NUM_CARGO, 5);
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);
404  }
405  NWidgetBackground * wid = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, i + WID_LGL_CARGO_FIRST);
407  wid->SetFill(1, 1);
408  wid->SetResize(0, 0);
409  row->Add(wid);
410  }
411  /* Fill up last row */
412  for (uint i = 0; i < 4 - (NUM_CARGO - 1) % 5; ++i) {
414  spc->SetFill(1, 1);
415  spc->SetResize(0, 0);
416  row->Add(spc);
417  }
418  panel->Add(row);
419  *biggest_index = WID_LGL_CARGO_LAST;
420  return panel;
421 }
422 
423 
424 static const NWidgetPart _nested_linkgraph_legend_widgets[] = {
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),
430  EndContainer(),
431  NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
433  NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_SATURATION),
435  NWidgetFunction(MakeSaturationLegendLinkGraphGUI),
436  EndContainer(),
437  NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_COMPANIES),
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),
443  EndContainer(),
444  EndContainer(),
445  NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_CARGOES),
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),
451  EndContainer(),
452  EndContainer(),
453  EndContainer(),
454  EndContainer()
455 };
456 
457 static_assert(WID_LGL_SATURATION_LAST - WID_LGL_SATURATION_FIRST ==
459 
460 static WindowDesc _linkgraph_legend_desc(
461  WDP_AUTO, "toolbar_linkgraph", 0, 0,
463  0,
464  _nested_linkgraph_legend_widgets, lengthof(_nested_linkgraph_legend_widgets)
465 );
466 
471 {
472  AllocateWindowDescFront<LinkGraphLegendWindow>(&_linkgraph_legend_desc, 0);
473 }
474 
475 LinkGraphLegendWindow::LinkGraphLegendWindow(WindowDesc *desc, int window_number) : Window(desc)
476 {
477  this->InitNested(window_number);
478  this->InvalidateData(0);
479  this->SetOverlay(FindWindowById(WC_MAIN_WINDOW, 0)->viewport->overlay);
480 }
481 
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));
492  }
493  }
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));
498  }
499  }
500 }
501 
502 void LinkGraphLegendWindow::UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
503 {
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;
512  }
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);
518  }
519  }
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);
527  }
528  }
529 }
530 
531 void LinkGraphLegendWindow::DrawWidget(const Rect &r, int widget) const
532 {
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);
538  }
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;
548  }
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);
550  }
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);
556  }
557 }
558 
559 bool LinkGraphLegendWindow::OnTooltip(Point pt, int widget, TooltipCloseCondition close_cond)
560 {
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);
564  } else {
565  uint64 params[2];
566  CompanyID cid = (CompanyID)(widget - WID_LGL_COMPANY_FIRST);
567  params[0] = STR_LINKGRAPH_LEGEND_SELECT_COMPANIES;
568  params[1] = cid;
569  GuiShowTooltips(this, STR_LINKGRAPH_LEGEND_COMPANY_TOOLTIP, 2, params, close_cond);
570  }
571  return true;
572  }
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);
576  uint64 params[1];
577  params[0] = cargo->name;
578  GuiShowTooltips(this, STR_BLACK_STRING, 1, params, close_cond);
579  return true;
580  }
581  return false;
582 }
583 
588 {
589  uint32 mask = 0;
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;
593  SetBit(mask, c);
594  }
595  this->overlay->SetCompanyMask(mask);
596 }
597 
602 {
603  CargoTypes mask = 0;
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;
607  SetBit(mask, c);
608  }
609  this->overlay->SetCargoMask(mask);
610 }
611 
612 void LinkGraphLegendWindow::OnClick(Point pt, int widget, int click_count)
613 {
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();
619  }
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);
624  }
625  this->UpdateOverlayCompanies();
626  this->SetDirty();
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();
631  }
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);
636  }
637  this->UpdateOverlayCargoes();
638  }
639  this->SetDirty();
640 }
641 
647 void LinkGraphLegendWindow::OnInvalidateData(int data, bool gui_scope)
648 {
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));
652  }
653  for (CargoID i = 0; i < NUM_CARGO; i++) {
654  this->SetWidgetDisabledState(i + WID_LGL_CARGO_FIRST, !CargoSpec::Get(i)->IsValid());
655  }
656 }
LinkGraphOverlay::company_mask
uint32 company_mask
Bitmask of companies to be displayed.
Definition: linkgraph_gui.h:74
WD_FRAMERECT_TOP
@ WD_FRAMERECT_TOP
Offset at top to draw the frame rectangular area.
Definition: window_gui.h:62
LinkGraphOverlay::LINK_COLOURS
static const uint8 LINK_COLOURS[]
Colours for the various "load" states of links.
Definition: linkgraph_gui.h:43
Station::goods
GoodsEntry goods[NUM_CARGO]
Goods at this station.
Definition: station_base.h:479
LinkGraphLegendWindow::UpdateWidgetSize
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.
Definition: linkgraph_gui.cpp:502
LinkGraph::EdgeWrapper::Usage
uint Usage() const
Get edge's usage.
Definition: linkgraph.h:98
NWidgetFunction
static NWidgetPart NWidgetFunction(NWidgetFunctionType *func_ptr)
Obtain a nested widget (sub)tree from an external source.
Definition: widget_type.h:1145
LinkGraphOverlay::window
const Window * window
Window to be drawn into.
Definition: linkgraph_gui.h:71
Pool::PoolItem<&_link_graph_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:329
FlowStatMap::GetFlowVia
uint GetFlowVia(StationID via) const
Get the sum of flows via a specific station from this FlowStatMap.
Definition: station_cmd.cpp:4722
LinkGraph
A connected component of a link graph.
Definition: linkgraph.h:39
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:27
WWT_STICKYBOX
@ WWT_STICKYBOX
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
Definition: widget_type.h:64
SetPadding
static NWidgetPart SetPadding(uint8 top, uint8 right, uint8 bottom, uint8 left)
Widget part function for setting additional space around a widget.
Definition: widget_type.h:1045
NWidgetContainer::Add
void Add(NWidgetBase *wid)
Append widget wid to container.
Definition: widget.cpp:951
GetContrastColour
TextColour GetContrastColour(uint8 background, uint8 threshold)
Determine a contrasty text colour for a coloured background.
Definition: gfx.cpp:1262
LinkGraphLegendWindow::UpdateOverlayCompanies
void UpdateOverlayCompanies()
Update the overlay with the new company selection.
Definition: linkgraph_gui.cpp:587
WWT_CAPTION
@ WWT_CAPTION
Window caption (window title between closebox and stickybox)
Definition: widget_type.h:59
Station
Station data structure.
Definition: station_base.h:450
LinkGraphOverlay::RebuildCache
void RebuildCache()
Rebuild the cache and recalculate which links and stations to be shown.
Definition: linkgraph_gui.cpp:50
Window::viewport
ViewportData * viewport
Pointer to viewport data, if present.
Definition: window_gui.h:326
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:227
CargoSpec::Get
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo ID.
Definition: cargotype.h:117
NWID_HORIZONTAL
@ NWID_HORIZONTAL
Horizontal container.
Definition: widget_type.h:73
LinkGraph::ConstNode
Constant node class.
Definition: linkgraph.h:338
maxdim
Dimension maxdim(const Dimension &d1, const Dimension &d2)
Compute bounding box of both dimensions.
Definition: geometry_func.cpp:22
FindWindowById
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
Definition: window.cpp:1133
LinkGraphLegendWindow::OnInvalidateData
void OnInvalidateData(int data=0, bool gui_scope=true) override
Invalidate the data of this window if the cargoes or companies have changed.
Definition: linkgraph_gui.cpp:647
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
NWidgetSpacer
Spacer widget.
Definition: widget_type.h:528
LinkGraphLegendWindow::OnTooltip
bool OnTooltip(Point pt, int widget, TooltipCloseCondition close_cond) override
Event to display a custom tooltip.
Definition: linkgraph_gui.cpp:559
LinkGraphOverlay::GetCargoMask
CargoTypes GetCargoMask()
Get a bitmask of the currently shown cargoes.
Definition: linkgraph_gui.h:65
SpecializedStation< Station, false >::Get
static Station * Get(size_t index)
Gets station with given index.
Definition: base_station_base.h:219
DrawString
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.
Definition: gfx.cpp:640
LinkGraphLegendWindow::UpdateOverlayCargoes
void UpdateOverlayCargoes()
Update the overlay with the new cargo selection.
Definition: linkgraph_gui.cpp:601
CargoSpec
Specification of a cargo type.
Definition: cargotype.h:55
SpecializedStation< Station, false >::IsValidID
static bool IsValidID(size_t index)
Tests whether given index is a valid index for station of this type.
Definition: base_station_base.h:210
VehicleSettings::road_side
byte road_side
the side of the road vehicles drive on
Definition: settings_type.h:467
SA_HOR_CENTER
@ SA_HOR_CENTER
Horizontally center the text.
Definition: gfx_func.h:97
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
BaseStation::owner
Owner owner
The owner of this station.
Definition: base_station_base.h:62
_colour_gradient
byte _colour_gradient[COLOUR_END][8]
All 16 colour gradients 8 colours per gradient from darkest (0) to lightest (7)
Definition: gfx.cpp:52
NWidgetPart
Partial widget specification to allow NWidgets to be written nested.
Definition: widget_type.h:909
SetDataTip
static NWidgetPart SetDataTip(uint32 data, StringID tip)
Widget part function for setting the data and tooltip.
Definition: widget_type.h:1013
SmallMapWindow
Class managing the smallmap window.
Definition: smallmap_gui.h:43
GetStringBoundingBox
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition: gfx.cpp:842
LinkGraph::EdgeWrapper
Wrapper for an edge (const or not) allowing retrieval, but no modification.
Definition: linkgraph.h:76
LinkGraphOverlay::SetCompanyMask
void SetCompanyMask(uint32 company_mask)
Set a new company mask and rebuild the cache.
Definition: linkgraph_gui.cpp:368
SpecializedStation< Station, false >::Iterate
static Pool::IterateWrapper< Station > Iterate(size_t from=0)
Returns an iterable ensemble of all valid stations of type T.
Definition: base_station_base.h:270
IsInsideMM
static bool IsInsideMM(const T x, const size_t min, const size_t max)
Checks if a value is in an interval.
Definition: math_func.hpp:204
WindowDesc
High level window description.
Definition: window_gui.h:166
COMPANY_FIRST
@ COMPANY_FIRST
First company, same as owner.
Definition: company_type.h:22
LinkGraphOverlay::cached_links
LinkMap cached_links
Cache for links to reduce recalculation.
Definition: linkgraph_gui.h:75
NC_EQUALSIZE
@ NC_EQUALSIZE
Value of the NCB_EQUALSIZE flag.
Definition: widget_type.h:428
Window::GetWidget
const NWID * GetWidget(uint widnum) const
Get the nested widget with number widnum from the nested widget tree.
Definition: window_gui.h:844
LinkGraphOverlay::cached_stations
StationSupplyList cached_stations
Cache for stations to be drawn.
Definition: linkgraph_gui.h:76
LinkGraphOverlay
Handles drawing of links into some window.
Definition: linkgraph_gui.h:37
WDP_AUTO
@ WDP_AUTO
Find a place automatically.
Definition: window_gui.h:154
MakeCompanyButtonRows
NWidgetBase * MakeCompanyButtonRows(int *biggest_index, int widget_first, int widget_last, int max_length, StringID button_tooltip)
Make a number of rows with button-like graphics, for enabling/disabling each company.
Definition: widget.cpp:2871
GuiShowTooltips
void GuiShowTooltips(Window *parent, StringID str, uint paramcount, const uint64 params[], TooltipCloseCondition close_tooltip)
Shows a tooltip.
Definition: misc_gui.cpp:781
LinkGraphOverlay::SetCargoMask
void SetCargoMask(CargoTypes cargo_mask)
Set a new cargo mask and rebuild the cache.
Definition: linkgraph_gui.cpp:357
LinkGraphOverlay::AddLinks
void AddLinks(const Station *sta, const Station *stb)
Add all "interesting" links between the given stations to the cache.
Definition: linkgraph_gui.cpp:193
BaseStation::rect
StationRect rect
NOSAVE: Station spread out rectangle maintained by StationRect::xxx() functions.
Definition: base_station_base.h:76
LinkGraphLegendWindow::SetOverlay
void SetOverlay(LinkGraphOverlay *overlay)
Set the overlay belonging to this menu and import its company/cargo settings.
Definition: linkgraph_gui.cpp:486
NWidgetResizeBase::SetResize
void SetResize(uint resize_x, uint resize_y)
Set resize step of the widget.
Definition: widget.cpp:848
Window::SetDirty
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition: window.cpp:984
WD_FRAMERECT_LEFT
@ WD_FRAMERECT_LEFT
Offset at left to draw the frame rectangular area.
Definition: window_gui.h:60
GoodsEntry::node
NodeID node
ID of node in link graph referring to this goods entry.
Definition: station_base.h:258
WD_FRAMERECT_RIGHT
@ WD_FRAMERECT_RIGHT
Offset at right to draw the frame rectangular area.
Definition: window_gui.h:61
WD_FRAMERECT_BOTTOM
@ WD_FRAMERECT_BOTTOM
Offset at bottom to draw the frame rectangular area.
Definition: window_gui.h:63
WWT_PUSHTXTBTN
@ WWT_PUSHTXTBTN
Normal push-button (no toggle button) with text caption.
Definition: widget_type.h:102
NWidgetBase
Baseclass for nested widgets.
Definition: widget_type.h:124
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:80
Window::SetWidgetDisabledState
void SetWidgetDisabledState(byte widget_index, bool disab_stat)
Sets the enabled/disabled status of a widget.
Definition: window_gui.h:392
MAX_COMPANIES
@ MAX_COMPANIES
Maximum number of companies.
Definition: company_type.h:23
LinkGraphOverlay::IsLinkVisible
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.
Definition: linkgraph_gui.cpp:123
LinkGraphOverlay::AddStats
static void AddStats(uint new_cap, uint new_usg, uint new_flow, bool new_shared, LinkProperties &cargo)
Add information from a given pair of link stat and flow stat to the given link properties.
Definition: linkgraph_gui.cpp:223
LinkGraph::EdgeWrapper::Capacity
uint Capacity() const
Get edge's capacity.
Definition: linkgraph.h:92
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
GfxFillRect
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.
Definition: gfx.cpp:114
GoodsEntry::link_graph
LinkGraphID link_graph
Link graph this station belongs to.
Definition: station_base.h:257
WC_LINKGRAPH_LEGEND
@ WC_LINKGRAPH_LEGEND
Linkgraph legend; Window numbers:
Definition: window_type.h:674
NWidgetBase::current_y
uint current_y
Current vertical size (after resizing).
Definition: widget_type.h:173
WC_NONE
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Definition: window_type.h:38
NWID_VERTICAL
@ NWID_VERTICAL
Vertical container.
Definition: widget_type.h:75
NWidgetResizeBase::SetFill
void SetFill(uint fill_x, uint fill_y)
Set the filling of the widget from initial size.
Definition: widget.cpp:837
FONT_HEIGHT_SMALL
#define FONT_HEIGHT_SMALL
Height of characters in the small (FS_SMALL) font.
Definition: gfx_func.h:176
DrawCompanyIcon
void DrawCompanyIcon(CompanyID c, int x, int y)
Draw the icon of a company.
Definition: company_cmd.cpp:142
GetSpriteSize
Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
Get the size of a sprite.
Definition: gfx.cpp:913
LinkGraphOverlay::scale
uint scale
Width of link lines.
Definition: linkgraph_gui.h:77
WWT_CLOSEBOX
@ WWT_CLOSEBOX
Close box (at top-left of a window)
Definition: widget_type.h:67
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
GoodsEntry
Stores station stats for a single cargo.
Definition: station_base.h:170
EndContainer
static NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
Definition: widget_type.h:998
LinkGraphOverlay::dirty
bool dirty
Set if overlay should be rebuilt.
Definition: linkgraph_gui.h:78
LinkGraphOverlay::DrawStationDots
void DrawStationDots(const DrawPixelInfo *dpi) const
Draw dots for stations into the smallmap.
Definition: linkgraph_gui.cpp:297
NWidgetHorizontal
Horizontal container.
Definition: widget_type.h:453
LinkGraph::cargo
CargoID cargo
Cargo of this component's link graph.
Definition: linkgraph.h:532
GoodsEntry::flows
FlowStatMap flows
Planned flows through this station.
Definition: station_base.h:259
MakeCompanyButtonRowsLinkGraphGUI
NWidgetBase * MakeCompanyButtonRowsLinkGraphGUI(int *biggest_index)
Make a number of rows with buttons for each company for the linkgraph legend window.
Definition: linkgraph_gui.cpp:376
NWidget
static NWidgetPart NWidget(WidgetType tp, Colours col, int16 idx=-1)
Widget part function for starting a new 'real' widget.
Definition: widget_type.h:1113
LinkGraphLegendWindow::DrawWidget
void DrawWidget(const Rect &r, int widget) const override
Draw the contents of a nested widget.
Definition: linkgraph_gui.cpp:531
LinkGraphOverlay::Draw
void Draw(const DrawPixelInfo *dpi)
Draw the linkgraph overlay or some part of it, in the area given.
Definition: linkgraph_gui.cpp:239
LinkGraphOverlay::GetStationMiddle
Point GetStationMiddle(const Station *st) const
Determine the middle of a station in the current window.
Definition: linkgraph_gui.cpp:343
WWT_PANEL
@ WWT_PANEL
Simple depressed panel.
Definition: widget_type.h:48
LinkGraph::ConstEdgeIterator
An iterator for const edges.
Definition: linkgraph.h:308
LinkGraphLegendWindow::OnClick
void OnClick(Point pt, int widget, int click_count) override
A click with the left mouse button has been made on the window.
Definition: linkgraph_gui.cpp:612
OWNER_NONE
@ OWNER_NONE
The tile has no ownership.
Definition: company_type.h:25
Window::IsWidgetLowered
bool IsWidgetLowered(byte widget_index) const
Gets the lowered state of a widget.
Definition: window_gui.h:493
NUM_CARGO
@ NUM_CARGO
Maximal number of cargo types in a game.
Definition: cargo_type.h:64
LinkGraphOverlay::IsPointVisible
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.
Definition: linkgraph_gui.cpp:108
NWidgetVertical
Vertical container.
Definition: widget_type.h:476
WC_MAIN_WINDOW
@ WC_MAIN_WINDOW
Main window; Window numbers:
Definition: window_type.h:44
ShowLinkGraphLegend
void ShowLinkGraphLegend()
Open a link graph legend window.
Definition: linkgraph_gui.cpp:470
abs
static T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition: math_func.hpp:21
LinkGraphOverlay::GetWidgetDpi
void GetWidgetDpi(DrawPixelInfo *dpi) const
Get a DPI for the widget we will be drawing to.
Definition: linkgraph_gui.cpp:39
LinkGraphOverlay::cargo_mask
CargoTypes cargo_mask
Bitmask of cargos to be displayed.
Definition: linkgraph_gui.h:73
LinkGraphOverlay::SetDirty
void SetDirty()
Mark the linkgraph dirty to be rebuilt next time Draw() is called.
Definition: linkgraph_gui.h:62
Window::ToggleWidgetLoweredState
void ToggleWidgetLoweredState(byte widget_index)
Invert the lowered/raised status of a widget.
Definition: window_gui.h:463
SetBit
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
Definition: bitmath_func.hpp:121
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:367
SpecializedStation< Station, false >::GetIfValid
static Station * GetIfValid(size_t index)
Returns station if the index is a valid index for this station type.
Definition: base_station_base.h:228
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:20
LinkGraphOverlay::GetCompanyMask
uint32 GetCompanyMask()
Get a bitmask of the currently shown companies.
Definition: linkgraph_gui.h:68
LinkGraphOverlay::DrawContent
void DrawContent(Point pta, Point ptb, const LinkProperties &cargo) const
Draw one specific link.
Definition: linkgraph_gui.cpp:273
CeilDiv
static uint CeilDiv(uint a, uint b)
Computes ceil(a / b) for non-negative a and b.
Definition: math_func.hpp:254
LinkGraphOverlay::widget_id
const uint widget_id
ID of Widget in Window to be drawn to.
Definition: linkgraph_gui.h:72
GameSettings::vehicle
VehicleSettings vehicle
options for vehicles
Definition: settings_type.h:558
Window
Data structure for an opened window.
Definition: window_gui.h:276
WD_CAPTIONTEXT_LEFT
@ WD_CAPTIONTEXT_LEFT
Offset of the caption text at the left.
Definition: window_gui.h:125
Pool::PoolItem<&_link_graph_pool >::IsValidID
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:318
LinkGraphOverlay::DrawLinks
void DrawLinks(const DrawPixelInfo *dpi) const
Draw the cached links or part of them into the given area.
Definition: linkgraph_gui.cpp:253
NWidgetBackground
Nested widget with a child.
Definition: widget_type.h:544
Window::IsWidgetDisabled
bool IsWidgetDisabled(byte widget_index) const
Gets the enabled/disabled status of a widget.
Definition: window_gui.h:421
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:47
LinkGraph::ConstNode::End
ConstEdgeIterator End() const
Get an iterator pointing beyond the end of the edges array.
Definition: linkgraph.h:367
LinkGraphOverlay::DrawVertex
static void DrawVertex(int x, int y, int size, int colour, int border_colour)
Draw a square symbolizing a producer of cargo.
Definition: linkgraph_gui.cpp:322
NWidgetBase::current_x
uint current_x
Current horizontal size (after resizing).
Definition: widget_type.h:172
NWidgetResizeBase::SetMinimalSize
void SetMinimalSize(uint min_x, uint min_y)
Set minimal size of the widget.
Definition: widget.cpp:815
Window::SetWidgetLoweredState
void SetWidgetLoweredState(byte widget_index, bool lowered_stat)
Sets the lowered/raised status of a widget.
Definition: window_gui.h:453
LinkGraph::ConstNode::Begin
ConstEdgeIterator Begin() const
Get an iterator pointing to the start of the edges array.
Definition: linkgraph.h:361
LinkGraph::Monthly
uint Monthly(uint base) const
Scale a value to its monthly equivalent, based on last compression.
Definition: linkgraph.h:517
DrawPixelInfo
Data about how and where to blit pixels.
Definition: gfx_type.h:155
LinkGraph::NodeWrapper::Supply
uint Supply() const
Get supply of wrapped node.
Definition: linkgraph.h:146
LinkProperties
Properties of a link between two stations.
Definition: linkgraph_gui.h:24
WWT_SHADEBOX
@ WWT_SHADEBOX
Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX)
Definition: widget_type.h:62