OpenTTD
linkgraph_gui.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
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/>.
8  */
9 
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"
22 
23 #include "table/strings.h"
24 
25 #include "../safeguards.h"
26 
31 const uint8 LinkGraphOverlay::LINK_COLOURS[] = {
32  0x0f, 0xd1, 0xd0, 0x57,
33  0x55, 0x53, 0xbf, 0xbd,
34  0xba, 0xb9, 0xb7, 0xb5
35 };
36 
42 {
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;
47 }
48 
53 {
54  this->cached_links.clear();
55  this->cached_stations.clear();
56  if (this->company_mask == 0) return;
57 
58  DrawPixelInfo dpi;
59  this->GetWidgetDpi(&dpi);
60 
61  const Station *sta;
62  FOR_ALL_STATIONS(sta) {
63  if (sta->rect.IsEmpty()) continue;
64 
65  Point pta = this->GetStationMiddle(sta);
66 
67  StationID from = sta->index;
68  StationLinkMap &seen_links = this->cached_links[from];
69 
70  uint supply = 0;
71  CargoID c;
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);
76 
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();
81  assert(from != to);
82  if (!Station::IsValidID(to) || seen_links.find(to) != seen_links.end()) {
83  continue;
84  }
85  const Station *stb = Station::Get(to);
86  assert(sta != stb);
87 
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;
91 
92  if (!this->IsLinkVisible(pta, this->GetStationMiddle(stb), &dpi)) continue;
93 
94  this->AddLinks(sta, stb);
95  seen_links[to]; // make sure it is created and marked as seen
96  }
97  }
98  if (this->IsPointVisible(pta, &dpi)) {
99  this->cached_stations.push_back(std::make_pair(from, supply));
100  }
101  }
102 }
103 
111 inline bool LinkGraphOverlay::IsPointVisible(Point pt, const DrawPixelInfo *dpi, int padding) const
112 {
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;
116 }
117 
126 inline bool LinkGraphOverlay::IsLinkVisible(Point pta, Point ptb, const DrawPixelInfo *dpi, int padding) const
127 {
128  const int left = dpi->left - padding;
129  const int right = dpi->left + dpi->width + padding;
130  const int top = dpi->top - padding;
131  const int bottom = dpi->top + dpi->height + padding;
132 
133  /*
134  * This method is an implementation of the Cohen-Sutherland line-clipping algorithm.
135  * See: https://en.wikipedia.org/wiki/Cohen%E2%80%93Sutherland_algorithm
136  */
137 
138  const uint8 INSIDE = 0; // 0000
139  const uint8 LEFT = 1; // 0001
140  const uint8 RIGHT = 2; // 0010
141  const uint8 BOTTOM = 4; // 0100
142  const uint8 TOP = 8; // 1000
143 
144  int x0 = pta.x;
145  int y0 = pta.y;
146  int x1 = ptb.x;
147  int y1 = ptb.y;
148 
149  auto out_code = [&](int x, int y) -> uint8 {
150  uint8 out = INSIDE;
151  if (x < left) {
152  out |= LEFT;
153  } else if (x > right) {
154  out |= RIGHT;
155  }
156  if (y < top) {
157  out |= TOP;
158  } else if (y > bottom) {
159  out |= BOTTOM;
160  }
161  return out;
162  };
163 
164  uint8 c0 = out_code(x0, y0);
165  uint8 c1 = out_code(x1, y1);
166 
167  while (true) {
168  if (c0 == 0 || c1 == 0) return true;
169  if ((c0 & c1) != 0) return false;
170 
171  if (c0 & TOP) { // point 0 is above the clip window
172  x0 = x0 + (int)(((int64) (x1 - x0)) * ((int64) (top - y0)) / ((int64) (y1 - y0)));
173  y0 = top;
174  } else if (c0 & BOTTOM) { // point 0 is below the clip window
175  x0 = x0 + (int)(((int64) (x1 - x0)) * ((int64) (bottom - y0)) / ((int64) (y1 - y0)));
176  y0 = bottom;
177  } else if (c0 & RIGHT) { // point 0 is to the right of clip window
178  y0 = y0 + (int)(((int64) (y1 - y0)) * ((int64) (right - x0)) / ((int64) (x1 - x0)));
179  x0 = right;
180  } else if (c0 & LEFT) { // point 0 is to the left of clip window
181  y0 = y0 + (int)(((int64) (y1 - y0)) * ((int64) (left - x0)) / ((int64) (x1 - x0)));
182  x0 = left;
183  }
184 
185  c0 = out_code(x0, y0);
186  }
187 
188  NOT_REACHED();
189 }
190 
196 void LinkGraphOverlay::AddLinks(const Station *from, const Station *to)
197 {
198  CargoID c;
199  FOR_EACH_SET_CARGO_ID(c, this->cargo_mask) {
200  if (!CargoSpec::Get(c)->IsValid()) continue;
201  const GoodsEntry &ge = from->goods[c];
202  if (!LinkGraph::IsValidID(ge.link_graph) ||
203  ge.link_graph != to->goods[c].link_graph) {
204  continue;
205  }
206  const LinkGraph &lg = *LinkGraph::Get(ge.link_graph);
207  ConstEdge edge = lg[ge.node][to->goods[c].node];
208  if (edge.Capacity() > 0) {
209  this->AddStats(lg.Monthly(edge.Capacity()), lg.Monthly(edge.Usage()),
210  ge.flows.GetFlowVia(to->index), from->owner == OWNER_NONE || to->owner == OWNER_NONE,
211  this->cached_links[from->index][to->index]);
212  }
213  }
214 }
215 
226 /* static */ void LinkGraphOverlay::AddStats(uint new_cap, uint new_usg, uint new_plan, bool new_shared, LinkProperties &cargo)
227 {
228  /* multiply the numbers by 32 in order to avoid comparing to 0 too often. */
229  if (cargo.capacity == 0 ||
230  max(cargo.usage, cargo.planned) * 32 / (cargo.capacity + 1) < max(new_usg, new_plan) * 32 / (new_cap + 1)) {
231  cargo.capacity = new_cap;
232  cargo.usage = new_usg;
233  cargo.planned = new_plan;
234  }
235  if (new_shared) cargo.shared = true;
236 }
237 
243 {
244  if (this->dirty) {
245  this->RebuildCache();
246  this->dirty = false;
247  }
248  this->DrawLinks(dpi);
249  this->DrawStationDots(dpi);
250 }
251 
257 {
258  for (LinkMap::const_iterator i(this->cached_links.begin()); i != this->cached_links.end(); ++i) {
259  if (!Station::IsValidID(i->first)) continue;
260  Point pta = this->GetStationMiddle(Station::Get(i->first));
261  for (StationLinkMap::const_iterator j(i->second.begin()); j != i->second.end(); ++j) {
262  if (!Station::IsValidID(j->first)) continue;
263  Point ptb = this->GetStationMiddle(Station::Get(j->first));
264  if (!this->IsLinkVisible(pta, ptb, dpi, this->scale + 2)) continue;
265  this->DrawContent(pta, ptb, j->second);
266  }
267  }
268 }
269 
277 {
278  uint usage_or_plan = min(cargo.capacity * 2 + 1, max(cargo.usage, cargo.planned));
279  int colour = LinkGraphOverlay::LINK_COLOURS[usage_or_plan * lengthof(LinkGraphOverlay::LINK_COLOURS) / (cargo.capacity * 2 + 2)];
280  int dash = cargo.shared ? this->scale * 4 : 0;
281 
282  /* Move line a bit 90° against its dominant direction to prevent it from
283  * being hidden below the grey line. */
284  int side = _settings_game.vehicle.road_side ? 1 : -1;
285  if (abs(pta.x - ptb.x) < abs(pta.y - ptb.y)) {
286  int offset_x = (pta.y > ptb.y ? 1 : -1) * side * this->scale;
287  GfxDrawLine(pta.x + offset_x, pta.y, ptb.x + offset_x, ptb.y, colour, this->scale, dash);
288  } else {
289  int offset_y = (pta.x < ptb.x ? 1 : -1) * side * this->scale;
290  GfxDrawLine(pta.x, pta.y + offset_y, ptb.x, ptb.y + offset_y, colour, this->scale, dash);
291  }
292 
293  GfxDrawLine(pta.x, pta.y, ptb.x, ptb.y, _colour_gradient[COLOUR_GREY][1], this->scale);
294 }
295 
301 {
302  for (StationSupplyList::const_iterator i(this->cached_stations.begin()); i != this->cached_stations.end(); ++i) {
303  const Station *st = Station::GetIfValid(i->first);
304  if (st == NULL) continue;
305  Point pt = this->GetStationMiddle(st);
306  if (!this->IsPointVisible(pt, dpi, 3 * this->scale)) continue;
307 
308  uint r = this->scale * 2 + this->scale * 2 * min(200, i->second) / 200;
309 
310  LinkGraphOverlay::DrawVertex(pt.x, pt.y, r,
312  (Colours)Company::Get(st->owner)->colour : COLOUR_GREY][5],
313  _colour_gradient[COLOUR_GREY][1]);
314  }
315 }
316 
325 /* static */ void LinkGraphOverlay::DrawVertex(int x, int y, int size, int colour, int border_colour)
326 {
327  size--;
328  int w1 = size / 2;
329  int w2 = size / 2 + size % 2;
330 
331  GfxFillRect(x - w1, y - w1, x + w2, y + w2, colour);
332 
333  w1++;
334  w2++;
335  GfxDrawLine(x - w1, y - w1, x + w2, y - w1, border_colour);
336  GfxDrawLine(x - w1, y + w2, x + w2, y + w2, border_colour);
337  GfxDrawLine(x - w1, y - w1, x - w1, y + w2, border_colour);
338  GfxDrawLine(x + w2, y - w1, x + w2, y + w2, border_colour);
339 }
340 
347 {
348  if (this->window->viewport != NULL) {
349  return GetViewportStationMiddle(this->window->viewport, st);
350  } else {
351  /* assume this is a smallmap */
352  return static_cast<const SmallMapWindow *>(this->window)->GetStationMiddle(st);
353  }
354 }
355 
361 {
362  this->cargo_mask = cargo_mask;
363  this->RebuildCache();
364  this->window->GetWidget<NWidgetBase>(this->widget_id)->SetDirty(this->window);
365 }
366 
372 {
373  this->company_mask = company_mask;
374  this->RebuildCache();
375  this->window->GetWidget<NWidgetBase>(this->widget_id)->SetDirty(this->window);
376 }
377 
380 {
381  return MakeCompanyButtonRows(biggest_index, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST, 3, STR_NULL);
382 }
383 
384 NWidgetBase *MakeSaturationLegendLinkGraphGUI(int *biggest_index)
385 {
387  for (uint i = 0; i < lengthof(LinkGraphOverlay::LINK_COLOURS); ++i) {
388  NWidgetBackground * wid = new NWidgetBackground(WWT_PANEL, COLOUR_DARK_GREEN, i + WID_LGL_SATURATION_FIRST);
390  wid->SetFill(1, 1);
391  wid->SetResize(0, 0);
392  panel->Add(wid);
393  }
394  *biggest_index = WID_LGL_SATURATION_LAST;
395  return panel;
396 }
397 
398 NWidgetBase *MakeCargoesLegendLinkGraphGUI(int *biggest_index)
399 {
400  static const uint ENTRIES_PER_ROW = CeilDiv(NUM_CARGO, 5);
402  NWidgetHorizontal *row = NULL;
403  for (uint i = 0; i < NUM_CARGO; ++i) {
404  if (i % ENTRIES_PER_ROW == 0) {
405  if (row) panel->Add(row);
406  row = new NWidgetHorizontal(NC_EQUALSIZE);
407  }
408  NWidgetBackground * wid = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, i + WID_LGL_CARGO_FIRST);
410  wid->SetFill(1, 1);
411  wid->SetResize(0, 0);
412  row->Add(wid);
413  }
414  /* Fill up last row */
415  for (uint i = 0; i < 4 - (NUM_CARGO - 1) % 5; ++i) {
417  spc->SetFill(1, 1);
418  spc->SetResize(0, 0);
419  row->Add(spc);
420  }
421  panel->Add(row);
422  *biggest_index = WID_LGL_CARGO_LAST;
423  return panel;
424 }
425 
426 
427 static const NWidgetPart _nested_linkgraph_legend_widgets[] = {
429  NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
430  NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_LGL_CAPTION), SetDataTip(STR_LINKGRAPH_LEGEND_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
431  NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
432  NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
433  EndContainer(),
434  NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
436  NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_SATURATION),
438  NWidgetFunction(MakeSaturationLegendLinkGraphGUI),
439  EndContainer(),
440  NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_COMPANIES),
444  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_COMPANIES_ALL), SetDataTip(STR_LINKGRAPH_LEGEND_ALL, STR_NULL),
445  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_COMPANIES_NONE), SetDataTip(STR_LINKGRAPH_LEGEND_NONE, STR_NULL),
446  EndContainer(),
447  EndContainer(),
448  NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_CARGOES),
451  NWidgetFunction(MakeCargoesLegendLinkGraphGUI),
452  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_CARGOES_ALL), SetDataTip(STR_LINKGRAPH_LEGEND_ALL, STR_NULL),
453  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_CARGOES_NONE), SetDataTip(STR_LINKGRAPH_LEGEND_NONE, STR_NULL),
454  EndContainer(),
455  EndContainer(),
456  EndContainer(),
457  EndContainer()
458 };
459 
460 assert_compile(WID_LGL_SATURATION_LAST - WID_LGL_SATURATION_FIRST ==
462 
463 static WindowDesc _linkgraph_legend_desc(
464  WDP_AUTO, "toolbar_linkgraph", 0, 0,
466  0,
467  _nested_linkgraph_legend_widgets, lengthof(_nested_linkgraph_legend_widgets)
468 );
469 
474 {
475  AllocateWindowDescFront<LinkGraphLegendWindow>(&_linkgraph_legend_desc, 0);
476 }
477 
478 LinkGraphLegendWindow::LinkGraphLegendWindow(WindowDesc *desc, int window_number) : Window(desc)
479 {
480  this->InitNested(window_number);
481  this->InvalidateData(0);
482  this->SetOverlay(FindWindowById(WC_MAIN_WINDOW, 0)->viewport->overlay);
483 }
484 
490  this->overlay = overlay;
491  uint32 companies = this->overlay->GetCompanyMask();
492  for (uint c = 0; c < MAX_COMPANIES; c++) {
493  if (!this->IsWidgetDisabled(WID_LGL_COMPANY_FIRST + c)) {
494  this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST + c, HasBit(companies, c));
495  }
496  }
497  CargoTypes cargoes = this->overlay->GetCargoMask();
498  for (uint c = 0; c < NUM_CARGO; c++) {
499  if (!this->IsWidgetDisabled(WID_LGL_CARGO_FIRST + c)) {
500  this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST + c, HasBit(cargoes, c));
501  }
502  }
503 }
504 
505 void LinkGraphLegendWindow::UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
506 {
507  if (IsInsideMM(widget, WID_LGL_SATURATION_FIRST, WID_LGL_SATURATION_LAST + 1)) {
508  StringID str = STR_NULL;
509  if (widget == WID_LGL_SATURATION_FIRST) {
510  str = STR_LINKGRAPH_LEGEND_UNUSED;
511  } else if (widget == WID_LGL_SATURATION_LAST) {
512  str = STR_LINKGRAPH_LEGEND_OVERLOADED;
513  } else if (widget == (WID_LGL_SATURATION_LAST + WID_LGL_SATURATION_FIRST) / 2) {
514  str = STR_LINKGRAPH_LEGEND_SATURATED;
515  }
516  if (str != STR_NULL) {
517  Dimension dim = GetStringBoundingBox(str);
518  dim.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
519  dim.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
520  *size = maxdim(*size, dim);
521  }
522  }
523  if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
524  CargoSpec *cargo = CargoSpec::Get(widget - WID_LGL_CARGO_FIRST);
525  if (cargo->IsValid()) {
526  Dimension dim = GetStringBoundingBox(cargo->abbrev);
527  dim.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
528  dim.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
529  *size = maxdim(*size, dim);
530  }
531  }
532 }
533 
534 void LinkGraphLegendWindow::DrawWidget(const Rect &r, int widget) const
535 {
536  if (IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) {
537  if (this->IsWidgetDisabled(widget)) return;
538  CompanyID cid = (CompanyID)(widget - WID_LGL_COMPANY_FIRST);
539  Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
540  DrawCompanyIcon(cid, (r.left + r.right + 1 - sprite_size.width) / 2, (r.top + r.bottom + 1 - sprite_size.height) / 2);
541  }
542  if (IsInsideMM(widget, WID_LGL_SATURATION_FIRST, WID_LGL_SATURATION_LAST + 1)) {
543  GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, LinkGraphOverlay::LINK_COLOURS[widget - WID_LGL_SATURATION_FIRST]);
544  StringID str = STR_NULL;
545  if (widget == WID_LGL_SATURATION_FIRST) {
546  str = STR_LINKGRAPH_LEGEND_UNUSED;
547  } else if (widget == WID_LGL_SATURATION_LAST) {
548  str = STR_LINKGRAPH_LEGEND_OVERLOADED;
549  } else if (widget == (WID_LGL_SATURATION_LAST + WID_LGL_SATURATION_FIRST) / 2) {
550  str = STR_LINKGRAPH_LEGEND_SATURATED;
551  }
552  if (str != STR_NULL) DrawString(r.left, r.right, (r.top + r.bottom + 1 - FONT_HEIGHT_SMALL) / 2, str, TC_FROMSTRING, SA_HOR_CENTER);
553  }
554  if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
555  if (this->IsWidgetDisabled(widget)) return;
556  CargoSpec *cargo = CargoSpec::Get(widget - WID_LGL_CARGO_FIRST);
557  GfxFillRect(r.left + 2, r.top + 2, r.right - 2, r.bottom - 2, cargo->legend_colour);
558  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  }
560 }
561 
562 bool LinkGraphLegendWindow::OnTooltip(Point pt, int widget, TooltipCloseCondition close_cond)
563 {
564  if (IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) {
565  if (this->IsWidgetDisabled(widget)) {
566  GuiShowTooltips(this, STR_LINKGRAPH_LEGEND_SELECT_COMPANIES, 0, NULL, close_cond);
567  } else {
568  uint64 params[2];
569  CompanyID cid = (CompanyID)(widget - WID_LGL_COMPANY_FIRST);
570  params[0] = STR_LINKGRAPH_LEGEND_SELECT_COMPANIES;
571  params[1] = cid;
572  GuiShowTooltips(this, STR_LINKGRAPH_LEGEND_COMPANY_TOOLTIP, 2, params, close_cond);
573  }
574  return true;
575  }
576  if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
577  if (this->IsWidgetDisabled(widget)) return false;
578  CargoSpec *cargo = CargoSpec::Get(widget - WID_LGL_CARGO_FIRST);
579  uint64 params[1];
580  params[0] = cargo->name;
581  GuiShowTooltips(this, STR_BLACK_STRING, 1, params, close_cond);
582  return true;
583  }
584  return false;
585 }
586 
591 {
592  uint32 mask = 0;
593  for (uint c = 0; c < MAX_COMPANIES; c++) {
594  if (this->IsWidgetDisabled(c + WID_LGL_COMPANY_FIRST)) continue;
595  if (!this->IsWidgetLowered(c + WID_LGL_COMPANY_FIRST)) continue;
596  SetBit(mask, c);
597  }
598  this->overlay->SetCompanyMask(mask);
599 }
600 
605 {
606  CargoTypes mask = 0;
607  for (uint c = 0; c < NUM_CARGO; c++) {
608  if (this->IsWidgetDisabled(c + WID_LGL_CARGO_FIRST)) continue;
609  if (!this->IsWidgetLowered(c + WID_LGL_CARGO_FIRST)) continue;
610  SetBit(mask, c);
611  }
612  this->overlay->SetCargoMask(mask);
613 }
614 
615 void LinkGraphLegendWindow::OnClick(Point pt, int widget, int click_count)
616 {
617  /* Check which button is clicked */
618  if (IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) {
619  if (!this->IsWidgetDisabled(widget)) {
620  this->ToggleWidgetLoweredState(widget);
621  this->UpdateOverlayCompanies();
622  }
623  } else if (widget == WID_LGL_COMPANIES_ALL || widget == WID_LGL_COMPANIES_NONE) {
624  for (uint c = 0; c < MAX_COMPANIES; c++) {
625  if (this->IsWidgetDisabled(c + WID_LGL_COMPANY_FIRST)) continue;
626  this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST + c, widget == WID_LGL_COMPANIES_ALL);
627  }
628  this->UpdateOverlayCompanies();
629  this->SetDirty();
630  } else if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
631  if (!this->IsWidgetDisabled(widget)) {
632  this->ToggleWidgetLoweredState(widget);
633  this->UpdateOverlayCargoes();
634  }
635  } else if (widget == WID_LGL_CARGOES_ALL || widget == WID_LGL_CARGOES_NONE) {
636  for (uint c = 0; c < NUM_CARGO; c++) {
637  if (this->IsWidgetDisabled(c + WID_LGL_CARGO_FIRST)) continue;
638  this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST + c, widget == WID_LGL_CARGOES_ALL);
639  }
640  this->UpdateOverlayCargoes();
641  }
642  this->SetDirty();
643 }
644 
650 void LinkGraphLegendWindow::OnInvalidateData(int data, bool gui_scope)
651 {
652  /* Disable the companies who are not active */
653  for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
654  this->SetWidgetDisabledState(i + WID_LGL_COMPANY_FIRST, !Company::IsValidID(i));
655  }
656  for (CargoID i = 0; i < NUM_CARGO; i++) {
657  this->SetWidgetDisabledState(i + WID_LGL_CARGO_FIRST, !CargoSpec::Get(i)->IsValid());
658  }
659 }
Constant node class.
Definition: linkgraph.h:339
VehicleSettings vehicle
options for vehicles
Properties of a link between two stations.
Definition: linkgraph_gui.h:26
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:77
Data about how and where to blit pixels.
Definition: gfx_type.h:156
Horizontally center the text.
Definition: gfx_func.h:99
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:113
void SetOverlay(LinkGraphOverlay *overlay)
Set the overlay belonging to this menu and import its company/cargo settings.
static NWidgetPart NWidgetFunction(NWidgetFunctionType *func_ptr)
Obtain a nested widget (sub)tree from an external source.
Definition: widget_type.h:1146
LinkMap cached_links
Cache for links to reduce recalculation.
Definition: linkgraph_gui.h:77
uint32 GetCompanyMask()
Get a bitmask of the currently shown companies.
Definition: linkgraph_gui.h:70
High level window description.
Definition: window_gui.h:168
virtual void OnClick(Point pt, int widget, int click_count)
A click with the left mouse button has been made on the window.
void SetMinimalSize(uint min_x, uint min_y)
Set minimal size of the widget.
Definition: widget.cpp:817
uint usage
Actual usage of the link.
Definition: linkgraph_gui.h:30
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:246
uint Supply() const
Get supply of wrapped node.
Definition: linkgraph.h:147
static bool IsInsideMM(const T x, const uint min, const uint max)
Checks if a value is in an interval.
Definition: math_func.hpp:266
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.
Offset at top to draw the frame rectangular area.
Definition: window_gui.h:64
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
Horizontal container.
Definition: widget_type.h:75
void SetCargoMask(CargoTypes cargo_mask)
Set a new cargo mask and rebuild the cache.
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
Definition: window.cpp:1114
byte _colour_gradient[COLOUR_END][8]
All 16 colour gradients 8 colours per gradient from darkest (0) to lightest (7)
Definition: gfx.cpp:53
Maximal number of cargo types in a game.
Definition: cargo_type.h:66
Specification of a cargo type.
Definition: cargotype.h:56
void GuiShowTooltips(Window *parent, StringID str, uint paramcount, const uint64 params[], TooltipCloseCondition close_tooltip)
Shows a tooltip.
Definition: misc_gui.cpp:741
uint Usage() const
Get edge&#39;s usage.
Definition: linkgraph.h:99
static const uint8 LINK_COLOURS[]
Colours for the various "load" states of links.
Definition: linkgraph_gui.h:45
void DrawCompanyIcon(CompanyID c, int x, int y)
Draw the icon of a company.
StationRect rect
NOSAVE: Station spread out rectangle maintained by StationRect::xxx() functions.
uint GetFlowVia(StationID via) const
Get the sum of flows via a specific station from this FlowStatMap.
Stores station stats for a single cargo.
Definition: station_base.h:170
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
Close box (at top-left of a window)
Definition: widget_type.h:69
StringID abbrev
Two letter abbreviation for this cargo type.
Definition: cargotype.h:75
CargoID cargo
Cargo of this component&#39;s link graph.
Definition: linkgraph.h:533
Spacer widget.
Definition: widget_type.h:529
uint scale
Width of link lines.
Definition: linkgraph_gui.h:79
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:26
void RebuildCache()
Rebuild the cache and recalculate which links and stations to be shown.
GoodsEntry goods[NUM_CARGO]
Goods at this station.
Definition: station_base.h:472
StringID name
Name of this type of cargo.
Definition: cargotype.h:71
void DrawStationDots(const DrawPixelInfo *dpi) const
Draw dots for stations into the smallmap.
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:2864
Partial widget specification to allow NWidgets to be written nested.
Definition: widget_type.h:910
A connected component of a link graph.
Definition: linkgraph.h:40
void Draw(const DrawPixelInfo *dpi)
Draw the linkgraph overlay or some part of it, in the area given.
Data structure for an opened window.
Definition: window_gui.h:278
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:1046
Main window; Window numbers:
Definition: window_type.h:46
void Add(NWidgetBase *wid)
Append widget wid to container.
Definition: widget.cpp:944
#define FONT_HEIGHT_SMALL
Height of characters in the small (FS_SMALL) font.
Definition: gfx_func.h:177
LinkGraphID link_graph
Link graph this station belongs to.
Definition: station_base.h:257
Class managing the smallmap window.
Definition: smallmap_gui.h:45
void UpdateOverlayCargoes()
Update the overlay with the new cargo selection.
byte road_side
the side of the road vehicles drive on
void GetWidgetDpi(DrawPixelInfo *dpi) const
Get a DPI for the widget we will be drawing to.
uint current_y
Current vertical size (after resizing).
Definition: widget_type.h:175
static NWidgetPart SetDataTip(uint32 data, StringID tip)
Widget part function for setting the data and tooltip.
Definition: widget_type.h:1014
void SetFill(uint fill_x, uint fill_y)
Set the filling of the widget from initial size.
Definition: widget.cpp:839
void SetCompanyMask(uint32 company_mask)
Set a new company mask and rebuild the cache.
Linkgraph legend; Window numbers:
Definition: window_type.h:676
uint Monthly(uint base) const
Scale a value to its monthly equivalent, based on last compression.
Definition: linkgraph.h:518
Normal push-button (no toggle button) with text caption.
Definition: widget_type.h:104
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.
Vertical container.
Definition: widget_type.h:477
First company, same as owner.
Definition: company_type.h:24
Simple depressed panel.
Definition: widget_type.h:50
static uint CeilDiv(uint a, uint b)
Computes ceil(a / b) for non-negative a and b.
Definition: math_func.hpp:316
NodeID node
ID of node in link graph referring to this goods entry.
Definition: station_base.h:258
bool shared
If this is a shared link to be drawn dashed.
Definition: linkgraph_gui.h:32
static NWidgetPart NWidget(WidgetType tp, Colours col, int16 idx=-1)
Widget part function for starting a new &#39;real&#39; widget.
Definition: widget_type.h:1114
The tile has no ownership.
Definition: company_type.h:27
Offset at bottom to draw the frame rectangular area.
Definition: window_gui.h:65
Baseclass for nested widgets.
Definition: widget_type.h:126
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:500
Offset of the caption text at the left.
Definition: window_gui.h:127
An iterator for const edges.
Definition: linkgraph.h:309
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:42
Horizontal container.
Definition: widget_type.h:454
virtual void DrawWidget(const Rect &r, int widget) const
Draw the contents of a nested widget.
const uint widget_id
ID of Widget in Window to be drawn to.
Definition: linkgraph_gui.h:74
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:18
Wrapper for an edge (const or not) allowing retrieval, but no modification.
Definition: linkgraph.h:77
Maximum number of companies.
Definition: company_type.h:25
FlowStatMap flows
Planned flows through this station.
Definition: station_base.h:259
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition: gfx.cpp:700
void AddLinks(const Station *sta, const Station *stb)
Add all "interesting" links between the given stations to the cache.
OwnerByte owner
The owner of this station.
static void DrawVertex(int x, int y, int size, int colour, int border_colour)
Draw a square symbolizing a producer of cargo.
Dimension maxdim(const Dimension &d1, const Dimension &d2)
Compute bounding box of both dimensions.
No window, redirects to WC_MAIN_WINDOW.
Definition: window_type.h:40
bool dirty
Set if overlay should be rebuilt.
Definition: linkgraph_gui.h:80
void SetResize(uint resize_x, uint resize_y)
Set resize step of the widget.
Definition: widget.cpp:850
virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
Update size and resize step of a widget in the window.
Window caption (window title between closebox and stickybox)
Definition: widget_type.h:61
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo ID.
Definition: cargotype.h:118
TextColour GetContrastColour(uint8 background, uint8 threshold)
Determine a contrasty text colour for a coloured background.
Definition: gfx.cpp:1118
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...
uint32 company_mask
Bitmask of companies to be displayed.
Definition: linkgraph_gui.h:76
Vertical container.
Definition: widget_type.h:77
static T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition: math_func.hpp:83
void ShowLinkGraphLegend()
Open a link graph legend window.
static NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME, WWT_INSET, or WWT_PANEL).
Definition: widget_type.h:999
virtual void OnInvalidateData(int data=0, bool gui_scope=true)
Invalidate the data of this window if the cargoes or companies have changed.
uint current_x
Current horizontal size (after resizing).
Definition: widget_type.h:174
ConstEdgeIterator End() const
Get an iterator pointing beyond the end of the edges array.
Definition: linkgraph.h:368
const Window * window
Window to be drawn into.
Definition: linkgraph_gui.h:73
CargoTypes cargo_mask
Bitmask of cargos to be displayed.
Definition: linkgraph_gui.h:75
static bool IsValidID(size_t index)
Tests whether given index is a valid index for station of this type.
Coordinates of a point in 2D.
Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
Get the size of a sprite.
Definition: gfx.cpp:769
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-NULL) Titem.
Definition: pool_type.hpp:235
virtual bool OnTooltip(Point pt, int widget, TooltipCloseCondition close_cond)
Event to display a custom tooltip.
void DrawContent(Point pta, Point ptb, const LinkProperties &cargo) const
Draw one specific link.
Offset at right to draw the frame rectangular area.
Definition: window_gui.h:63
uint capacity
Capacity of the link.
Definition: linkgraph_gui.h:29
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
Definition: widget_type.h:66
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
bool IsValid() const
Tests for validity of this cargospec.
Definition: cargotype.h:99
Specification of a rectangle with absolute coordinates of all edges.
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
Owner
Enum for all companies/owners.
Definition: company_type.h:20
void SetDirty()
Mark the linkgraph dirty to be rebuilt next time Draw() is called.
Definition: linkgraph_gui.h:64
void DrawLinks(const DrawPixelInfo *dpi) const
Draw the cached links or part of them into the given area.
Find a place automatically.
Definition: window_gui.h:156
ViewportData * viewport
Pointer to viewport data, if present.
Definition: window_gui.h:328
ConstEdgeIterator Begin() const
Get an iterator pointing to the start of the edges array.
Definition: linkgraph.h:362
uint Capacity() const
Get edge&#39;s capacity.
Definition: linkgraph.h:93
static Station * Get(size_t index)
Gets station with given index.
const NWID * GetWidget(uint widnum) const
Get the nested widget with number widnum from the nested widget tree.
Definition: window_gui.h:847
CargoTypes GetCargoMask()
Get a bitmask of the currently shown cargoes.
Definition: linkgraph_gui.h:67
NWidgetBase * MakeCompanyButtonRowsLinkGraphGUI(int *biggest_index)
Make a number of rows with buttons for each company for the linkgraph legend window.
Dimensions (a width and height) of a rectangle in 2D.
Value of the NCB_EQUALSIZE flag.
Definition: widget_type.h:429
Offset at left to draw the frame rectangular area.
Definition: window_gui.h:62
Station data structure.
Definition: station_base.h:446
Nested widget with a child.
Definition: widget_type.h:545
Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX)
Definition: widget_type.h:64
void UpdateOverlayCompanies()
Update the overlay with the new company selection.
Point GetStationMiddle(const Station *st) const
Determine the middle of a station in the current window.
Handles drawing of links into some window.
Definition: linkgraph_gui.h:39
uint planned
Planned usage of the link.
Definition: linkgraph_gui.h:31
StationSupplyList cached_stations
Cache for stations to be drawn.
Definition: linkgraph_gui.h:78
static Station * GetIfValid(size_t index)
Returns station if the index is a valid index for this station type.