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 
242 void LinkGraphOverlay::Draw(const DrawPixelInfo *dpi) const
243 {
244  this->DrawLinks(dpi);
245  this->DrawStationDots(dpi);
246 }
247 
253 {
254  for (LinkMap::const_iterator i(this->cached_links.begin()); i != this->cached_links.end(); ++i) {
255  if (!Station::IsValidID(i->first)) continue;
256  Point pta = this->GetStationMiddle(Station::Get(i->first));
257  for (StationLinkMap::const_iterator j(i->second.begin()); j != i->second.end(); ++j) {
258  if (!Station::IsValidID(j->first)) continue;
259  Point ptb = this->GetStationMiddle(Station::Get(j->first));
260  if (!this->IsLinkVisible(pta, ptb, dpi, this->scale + 2)) continue;
261  this->DrawContent(pta, ptb, j->second);
262  }
263  }
264 }
265 
273 {
274  uint usage_or_plan = min(cargo.capacity * 2 + 1, max(cargo.usage, cargo.planned));
275  int colour = LinkGraphOverlay::LINK_COLOURS[usage_or_plan * lengthof(LinkGraphOverlay::LINK_COLOURS) / (cargo.capacity * 2 + 2)];
276  int dash = cargo.shared ? this->scale * 4 : 0;
277 
278  /* Move line a bit 90° against its dominant direction to prevent it from
279  * being hidden below the grey line. */
280  int side = _settings_game.vehicle.road_side ? 1 : -1;
281  if (abs(pta.x - ptb.x) < abs(pta.y - ptb.y)) {
282  int offset_x = (pta.y > ptb.y ? 1 : -1) * side * this->scale;
283  GfxDrawLine(pta.x + offset_x, pta.y, ptb.x + offset_x, ptb.y, colour, this->scale, dash);
284  } else {
285  int offset_y = (pta.x < ptb.x ? 1 : -1) * side * this->scale;
286  GfxDrawLine(pta.x, pta.y + offset_y, ptb.x, ptb.y + offset_y, colour, this->scale, dash);
287  }
288 
289  GfxDrawLine(pta.x, pta.y, ptb.x, ptb.y, _colour_gradient[COLOUR_GREY][1], this->scale);
290 }
291 
297 {
298  for (StationSupplyList::const_iterator i(this->cached_stations.begin()); i != this->cached_stations.end(); ++i) {
299  const Station *st = Station::GetIfValid(i->first);
300  if (st == NULL) continue;
301  Point pt = this->GetStationMiddle(st);
302  if (!this->IsPointVisible(pt, dpi, 3 * this->scale)) continue;
303 
304  uint r = this->scale * 2 + this->scale * 2 * min(200, i->second) / 200;
305 
306  LinkGraphOverlay::DrawVertex(pt.x, pt.y, r,
308  (Colours)Company::Get(st->owner)->colour : COLOUR_GREY][5],
309  _colour_gradient[COLOUR_GREY][1]);
310  }
311 }
312 
321 /* static */ void LinkGraphOverlay::DrawVertex(int x, int y, int size, int colour, int border_colour)
322 {
323  size--;
324  int w1 = size / 2;
325  int w2 = size / 2 + size % 2;
326 
327  GfxFillRect(x - w1, y - w1, x + w2, y + w2, colour);
328 
329  w1++;
330  w2++;
331  GfxDrawLine(x - w1, y - w1, x + w2, y - w1, border_colour);
332  GfxDrawLine(x - w1, y + w2, x + w2, y + w2, border_colour);
333  GfxDrawLine(x - w1, y - w1, x - w1, y + w2, border_colour);
334  GfxDrawLine(x + w2, y - w1, x + w2, y + w2, border_colour);
335 }
336 
343 {
344  if (this->window->viewport != NULL) {
345  return GetViewportStationMiddle(this->window->viewport, st);
346  } else {
347  /* assume this is a smallmap */
348  return static_cast<const SmallMapWindow *>(this->window)->GetStationMiddle(st);
349  }
350 }
351 
357 {
358  this->cargo_mask = cargo_mask;
359  this->RebuildCache();
360  this->window->GetWidget<NWidgetBase>(this->widget_id)->SetDirty(this->window);
361 }
362 
368 {
369  this->company_mask = company_mask;
370  this->RebuildCache();
371  this->window->GetWidget<NWidgetBase>(this->widget_id)->SetDirty(this->window);
372 }
373 
376 {
377  return MakeCompanyButtonRows(biggest_index, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST, 3, STR_NULL);
378 }
379 
380 NWidgetBase *MakeSaturationLegendLinkGraphGUI(int *biggest_index)
381 {
383  for (uint i = 0; i < lengthof(LinkGraphOverlay::LINK_COLOURS); ++i) {
384  NWidgetBackground * wid = new NWidgetBackground(WWT_PANEL, COLOUR_DARK_GREEN, i + WID_LGL_SATURATION_FIRST);
386  wid->SetFill(1, 1);
387  wid->SetResize(0, 0);
388  panel->Add(wid);
389  }
390  *biggest_index = WID_LGL_SATURATION_LAST;
391  return panel;
392 }
393 
394 NWidgetBase *MakeCargoesLegendLinkGraphGUI(int *biggest_index)
395 {
396  static const uint ENTRIES_PER_ROW = CeilDiv(NUM_CARGO, 5);
398  NWidgetHorizontal *row = NULL;
399  for (uint i = 0; i < NUM_CARGO; ++i) {
400  if (i % ENTRIES_PER_ROW == 0) {
401  if (row) panel->Add(row);
402  row = new NWidgetHorizontal(NC_EQUALSIZE);
403  }
404  NWidgetBackground * wid = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, i + WID_LGL_CARGO_FIRST);
406  wid->SetFill(1, 1);
407  wid->SetResize(0, 0);
408  row->Add(wid);
409  }
410  /* Fill up last row */
411  for (uint i = 0; i < 4 - (NUM_CARGO - 1) % 5; ++i) {
413  spc->SetFill(1, 1);
414  spc->SetResize(0, 0);
415  row->Add(spc);
416  }
417  panel->Add(row);
418  *biggest_index = WID_LGL_CARGO_LAST;
419  return panel;
420 }
421 
422 
423 static const NWidgetPart _nested_linkgraph_legend_widgets[] = {
425  NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
426  NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_LGL_CAPTION), SetDataTip(STR_LINKGRAPH_LEGEND_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
427  NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
428  NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
429  EndContainer(),
430  NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
432  NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_SATURATION),
434  NWidgetFunction(MakeSaturationLegendLinkGraphGUI),
435  EndContainer(),
436  NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_COMPANIES),
440  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_COMPANIES_ALL), SetDataTip(STR_LINKGRAPH_LEGEND_ALL, STR_NULL),
441  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_COMPANIES_NONE), SetDataTip(STR_LINKGRAPH_LEGEND_NONE, STR_NULL),
442  EndContainer(),
443  EndContainer(),
444  NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_CARGOES),
447  NWidgetFunction(MakeCargoesLegendLinkGraphGUI),
448  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_CARGOES_ALL), SetDataTip(STR_LINKGRAPH_LEGEND_ALL, STR_NULL),
449  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_CARGOES_NONE), SetDataTip(STR_LINKGRAPH_LEGEND_NONE, STR_NULL),
450  EndContainer(),
451  EndContainer(),
452  EndContainer(),
453  EndContainer()
454 };
455 
456 assert_compile(WID_LGL_SATURATION_LAST - WID_LGL_SATURATION_FIRST ==
458 
459 static WindowDesc _linkgraph_legend_desc(
460  WDP_AUTO, "toolbar_linkgraph", 0, 0,
462  0,
463  _nested_linkgraph_legend_widgets, lengthof(_nested_linkgraph_legend_widgets)
464 );
465 
470 {
471  AllocateWindowDescFront<LinkGraphLegendWindow>(&_linkgraph_legend_desc, 0);
472 }
473 
474 LinkGraphLegendWindow::LinkGraphLegendWindow(WindowDesc *desc, int window_number) : Window(desc)
475 {
476  this->InitNested(window_number);
477  this->InvalidateData(0);
478  this->SetOverlay(FindWindowById(WC_MAIN_WINDOW, 0)->viewport->overlay);
479 }
480 
486  this->overlay = overlay;
487  uint32 companies = this->overlay->GetCompanyMask();
488  for (uint c = 0; c < MAX_COMPANIES; c++) {
489  if (!this->IsWidgetDisabled(WID_LGL_COMPANY_FIRST + c)) {
490  this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST + c, HasBit(companies, c));
491  }
492  }
493  CargoTypes cargoes = this->overlay->GetCargoMask();
494  for (uint c = 0; c < NUM_CARGO; c++) {
495  if (!this->IsWidgetDisabled(WID_LGL_CARGO_FIRST + c)) {
496  this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST + c, HasBit(cargoes, c));
497  }
498  }
499 }
500 
501 void LinkGraphLegendWindow::UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
502 {
503  if (IsInsideMM(widget, WID_LGL_SATURATION_FIRST, WID_LGL_SATURATION_LAST + 1)) {
504  StringID str = STR_NULL;
505  if (widget == WID_LGL_SATURATION_FIRST) {
506  str = STR_LINKGRAPH_LEGEND_UNUSED;
507  } else if (widget == WID_LGL_SATURATION_LAST) {
508  str = STR_LINKGRAPH_LEGEND_OVERLOADED;
509  } else if (widget == (WID_LGL_SATURATION_LAST + WID_LGL_SATURATION_FIRST) / 2) {
510  str = STR_LINKGRAPH_LEGEND_SATURATED;
511  }
512  if (str != STR_NULL) {
513  Dimension dim = GetStringBoundingBox(str);
514  dim.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
515  dim.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
516  *size = maxdim(*size, dim);
517  }
518  }
519  if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
520  CargoSpec *cargo = CargoSpec::Get(widget - WID_LGL_CARGO_FIRST);
521  if (cargo->IsValid()) {
522  Dimension dim = GetStringBoundingBox(cargo->abbrev);
523  dim.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
524  dim.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
525  *size = maxdim(*size, dim);
526  }
527  }
528 }
529 
530 void LinkGraphLegendWindow::DrawWidget(const Rect &r, int widget) const
531 {
532  if (IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) {
533  if (this->IsWidgetDisabled(widget)) return;
534  CompanyID cid = (CompanyID)(widget - WID_LGL_COMPANY_FIRST);
535  Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
536  DrawCompanyIcon(cid, (r.left + r.right + 1 - sprite_size.width) / 2, (r.top + r.bottom + 1 - sprite_size.height) / 2);
537  }
538  if (IsInsideMM(widget, WID_LGL_SATURATION_FIRST, WID_LGL_SATURATION_LAST + 1)) {
539  GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, LinkGraphOverlay::LINK_COLOURS[widget - WID_LGL_SATURATION_FIRST]);
540  StringID str = STR_NULL;
541  if (widget == WID_LGL_SATURATION_FIRST) {
542  str = STR_LINKGRAPH_LEGEND_UNUSED;
543  } else if (widget == WID_LGL_SATURATION_LAST) {
544  str = STR_LINKGRAPH_LEGEND_OVERLOADED;
545  } else if (widget == (WID_LGL_SATURATION_LAST + WID_LGL_SATURATION_FIRST) / 2) {
546  str = STR_LINKGRAPH_LEGEND_SATURATED;
547  }
548  if (str != STR_NULL) DrawString(r.left, r.right, (r.top + r.bottom + 1 - FONT_HEIGHT_SMALL) / 2, str, TC_FROMSTRING, SA_HOR_CENTER);
549  }
550  if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
551  if (this->IsWidgetDisabled(widget)) return;
552  CargoSpec *cargo = CargoSpec::Get(widget - WID_LGL_CARGO_FIRST);
553  GfxFillRect(r.left + 2, r.top + 2, r.right - 2, r.bottom - 2, cargo->legend_colour);
554  DrawString(r.left, r.right, (r.top + r.bottom + 1 - FONT_HEIGHT_SMALL) / 2, cargo->abbrev, GetContrastColour(cargo->legend_colour, 73), SA_HOR_CENTER);
555  }
556 }
557 
558 bool LinkGraphLegendWindow::OnHoverCommon(Point pt, int widget, TooltipCloseCondition close_cond)
559 {
560  if (IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) {
561  if (this->IsWidgetDisabled(widget)) {
562  GuiShowTooltips(this, STR_LINKGRAPH_LEGEND_SELECT_COMPANIES, 0, NULL, close_cond);
563  } else {
564  uint64 params[2];
565  CompanyID cid = (CompanyID)(widget - WID_LGL_COMPANY_FIRST);
566  params[0] = STR_LINKGRAPH_LEGEND_SELECT_COMPANIES;
567  params[1] = cid;
568  GuiShowTooltips(this, STR_LINKGRAPH_LEGEND_COMPANY_TOOLTIP, 2, params, close_cond);
569  }
570  return true;
571  }
572  if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
573  if (this->IsWidgetDisabled(widget)) return false;
574  CargoSpec *cargo = CargoSpec::Get(widget - WID_LGL_CARGO_FIRST);
575  uint64 params[1];
576  params[0] = cargo->name;
577  GuiShowTooltips(this, STR_BLACK_STRING, 1, params, close_cond);
578  return true;
579  }
580  return false;
581 }
582 
584 {
585  this->OnHoverCommon(pt, widget, TCC_HOVER);
586 }
587 
589 {
591  return this->OnHoverCommon(pt, widget, TCC_RIGHT_CLICK);
592  }
593  return false;
594 }
595 
600 {
601  uint32 mask = 0;
602  for (uint c = 0; c < MAX_COMPANIES; c++) {
603  if (this->IsWidgetDisabled(c + WID_LGL_COMPANY_FIRST)) continue;
604  if (!this->IsWidgetLowered(c + WID_LGL_COMPANY_FIRST)) continue;
605  SetBit(mask, c);
606  }
607  this->overlay->SetCompanyMask(mask);
608 }
609 
614 {
615  CargoTypes mask = 0;
616  for (uint c = 0; c < NUM_CARGO; c++) {
617  if (this->IsWidgetDisabled(c + WID_LGL_CARGO_FIRST)) continue;
618  if (!this->IsWidgetLowered(c + WID_LGL_CARGO_FIRST)) continue;
619  SetBit(mask, c);
620  }
621  this->overlay->SetCargoMask(mask);
622 }
623 
624 void LinkGraphLegendWindow::OnClick(Point pt, int widget, int click_count)
625 {
626  /* Check which button is clicked */
627  if (IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) {
628  if (!this->IsWidgetDisabled(widget)) {
629  this->ToggleWidgetLoweredState(widget);
630  this->UpdateOverlayCompanies();
631  }
632  } else if (widget == WID_LGL_COMPANIES_ALL || widget == WID_LGL_COMPANIES_NONE) {
633  for (uint c = 0; c < MAX_COMPANIES; c++) {
634  if (this->IsWidgetDisabled(c + WID_LGL_COMPANY_FIRST)) continue;
635  this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST + c, widget == WID_LGL_COMPANIES_ALL);
636  }
637  this->UpdateOverlayCompanies();
638  this->SetDirty();
639  } else if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
640  if (!this->IsWidgetDisabled(widget)) {
641  this->ToggleWidgetLoweredState(widget);
642  this->UpdateOverlayCargoes();
643  }
644  } else if (widget == WID_LGL_CARGOES_ALL || widget == WID_LGL_CARGOES_NONE) {
645  for (uint c = 0; c < NUM_CARGO; c++) {
646  if (this->IsWidgetDisabled(c + WID_LGL_CARGO_FIRST)) continue;
647  this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST + c, widget == WID_LGL_CARGOES_ALL);
648  }
649  this->UpdateOverlayCargoes();
650  }
651  this->SetDirty();
652 }
653 
659 void LinkGraphLegendWindow::OnInvalidateData(int data, bool gui_scope)
660 {
661  /* Disable the companies who are not active */
662  for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
663  this->SetWidgetDisabledState(i + WID_LGL_COMPANY_FIRST, !Company::IsValidID(i));
664  }
665  for (CargoID i = 0; i < NUM_CARGO; i++) {
666  this->SetWidgetDisabledState(i + WID_LGL_CARGO_FIRST, !CargoSpec::Get(i)->IsValid());
667  }
668 }
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:112
virtual void OnHover(Point pt, int widget)
The mouse is hovering over a widget in the window, perform an action for it, like opening a custom to...
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:75
uint32 GetCompanyMask()
Get a bitmask of the currently shown companies.
Definition: linkgraph_gui.h:68
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
uint16 hover_delay_ms
time required to activate a hover event, in milliseconds
Definition: settings_type.h:95
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:1111
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
void Draw(const DrawPixelInfo *dpi) const
Draw the linkgraph overlay or some part of it, in the area given.
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:77
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
Data structure for an opened window.
Definition: window_gui.h:271
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
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:76
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:499
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:72
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:699
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
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.
GUISettings gui
settings related to the GUI
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:1117
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:74
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
virtual bool OnRightClick(Point pt, int widget)
A click with the right mouse button has been made on the window.
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:71
CargoTypes cargo_mask
Bitmask of cargos to be displayed.
Definition: linkgraph_gui.h:73
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:768
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-NULL) Titem.
Definition: pool_type.hpp:235
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 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:321
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:832
CargoTypes GetCargoMask()
Get a bitmask of the currently shown cargoes.
Definition: linkgraph_gui.h:65
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:76
static Station * GetIfValid(size_t index)
Returns station if the index is a valid index for this station type.