OpenTTD
linkgraph_gui.cpp
Go to the documentation of this file.
1 /* $Id: linkgraph_gui.cpp 26891 2014-09-21 16:19:52Z fonsinchen $ */
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  return !((pta.x < dpi->left - padding && ptb.x < dpi->left - padding) ||
129  (pta.y < dpi->top - padding && ptb.y < dpi->top - padding) ||
130  (pta.x > dpi->left + dpi->width + padding &&
131  ptb.x > dpi->left + dpi->width + padding) ||
132  (pta.y > dpi->top + dpi->height + padding &&
133  ptb.y > dpi->top + dpi->height + padding));
134 }
135 
141 void LinkGraphOverlay::AddLinks(const Station *from, const Station *to)
142 {
143  CargoID c;
144  FOR_EACH_SET_CARGO_ID(c, this->cargo_mask) {
145  if (!CargoSpec::Get(c)->IsValid()) continue;
146  const GoodsEntry &ge = from->goods[c];
147  if (!LinkGraph::IsValidID(ge.link_graph) ||
148  ge.link_graph != to->goods[c].link_graph) {
149  continue;
150  }
151  const LinkGraph &lg = *LinkGraph::Get(ge.link_graph);
152  ConstEdge edge = lg[ge.node][to->goods[c].node];
153  if (edge.Capacity() > 0) {
154  this->AddStats(lg.Monthly(edge.Capacity()), lg.Monthly(edge.Usage()),
155  ge.flows.GetFlowVia(to->index), from->owner == OWNER_NONE || to->owner == OWNER_NONE,
156  this->cached_links[from->index][to->index]);
157  }
158  }
159 }
160 
171 /* static */ void LinkGraphOverlay::AddStats(uint new_cap, uint new_usg, uint new_plan, bool new_shared, LinkProperties &cargo)
172 {
173  /* multiply the numbers by 32 in order to avoid comparing to 0 too often. */
174  if (cargo.capacity == 0 ||
175  max(cargo.usage, cargo.planned) * 32 / (cargo.capacity + 1) < max(new_usg, new_plan) * 32 / (new_cap + 1)) {
176  cargo.capacity = new_cap;
177  cargo.usage = new_usg;
178  cargo.planned = new_plan;
179  }
180  if (new_shared) cargo.shared = true;
181 }
182 
187 void LinkGraphOverlay::Draw(const DrawPixelInfo *dpi) const
188 {
189  this->DrawLinks(dpi);
190  this->DrawStationDots(dpi);
191 }
192 
198 {
199  for (LinkMap::const_iterator i(this->cached_links.begin()); i != this->cached_links.end(); ++i) {
200  if (!Station::IsValidID(i->first)) continue;
201  Point pta = this->GetStationMiddle(Station::Get(i->first));
202  for (StationLinkMap::const_iterator j(i->second.begin()); j != i->second.end(); ++j) {
203  if (!Station::IsValidID(j->first)) continue;
204  Point ptb = this->GetStationMiddle(Station::Get(j->first));
205  if (!this->IsLinkVisible(pta, ptb, dpi, this->scale + 2)) continue;
206  this->DrawContent(pta, ptb, j->second);
207  }
208  }
209 }
210 
218 {
219  uint usage_or_plan = min(cargo.capacity * 2 + 1, max(cargo.usage, cargo.planned));
220  int colour = LinkGraphOverlay::LINK_COLOURS[usage_or_plan * lengthof(LinkGraphOverlay::LINK_COLOURS) / (cargo.capacity * 2 + 2)];
221  int dash = cargo.shared ? this->scale * 4 : 0;
222 
223  /* Move line a bit 90° against its dominant direction to prevent it from
224  * being hidden below the grey line. */
225  int side = _settings_game.vehicle.road_side ? 1 : -1;
226  if (abs(pta.x - ptb.x) < abs(pta.y - ptb.y)) {
227  int offset_x = (pta.y > ptb.y ? 1 : -1) * side * this->scale;
228  GfxDrawLine(pta.x + offset_x, pta.y, ptb.x + offset_x, ptb.y, colour, this->scale, dash);
229  } else {
230  int offset_y = (pta.x < ptb.x ? 1 : -1) * side * this->scale;
231  GfxDrawLine(pta.x, pta.y + offset_y, ptb.x, ptb.y + offset_y, colour, this->scale, dash);
232  }
233 
234  GfxDrawLine(pta.x, pta.y, ptb.x, ptb.y, _colour_gradient[COLOUR_GREY][1], this->scale);
235 }
236 
242 {
243  for (StationSupplyList::const_iterator i(this->cached_stations.begin()); i != this->cached_stations.end(); ++i) {
244  const Station *st = Station::GetIfValid(i->first);
245  if (st == NULL) continue;
246  Point pt = this->GetStationMiddle(st);
247  if (!this->IsPointVisible(pt, dpi, 3 * this->scale)) continue;
248 
249  uint r = this->scale * 2 + this->scale * 2 * min(200, i->second) / 200;
250 
251  LinkGraphOverlay::DrawVertex(pt.x, pt.y, r,
253  (Colours)Company::Get(st->owner)->colour : COLOUR_GREY][5],
254  _colour_gradient[COLOUR_GREY][1]);
255  }
256 }
257 
266 /* static */ void LinkGraphOverlay::DrawVertex(int x, int y, int size, int colour, int border_colour)
267 {
268  size--;
269  int w1 = size / 2;
270  int w2 = size / 2 + size % 2;
271 
272  GfxFillRect(x - w1, y - w1, x + w2, y + w2, colour);
273 
274  w1++;
275  w2++;
276  GfxDrawLine(x - w1, y - w1, x + w2, y - w1, border_colour);
277  GfxDrawLine(x - w1, y + w2, x + w2, y + w2, border_colour);
278  GfxDrawLine(x - w1, y - w1, x - w1, y + w2, border_colour);
279  GfxDrawLine(x + w2, y - w1, x + w2, y + w2, border_colour);
280 }
281 
288 {
289  if (this->window->viewport != NULL) {
290  return GetViewportStationMiddle(this->window->viewport, st);
291  } else {
292  /* assume this is a smallmap */
293  return static_cast<const SmallMapWindow *>(this->window)->GetStationMiddle(st);
294  }
295 }
296 
301 void LinkGraphOverlay::SetCargoMask(uint32 cargo_mask)
302 {
303  this->cargo_mask = cargo_mask;
304  this->RebuildCache();
305  this->window->GetWidget<NWidgetBase>(this->widget_id)->SetDirty(this->window);
306 }
307 
312 void LinkGraphOverlay::SetCompanyMask(uint32 company_mask)
313 {
314  this->company_mask = company_mask;
315  this->RebuildCache();
316  this->window->GetWidget<NWidgetBase>(this->widget_id)->SetDirty(this->window);
317 }
318 
321 {
322  return MakeCompanyButtonRows(biggest_index, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST, 3, STR_LINKGRAPH_LEGEND_SELECT_COMPANIES);
323 }
324 
325 NWidgetBase *MakeSaturationLegendLinkGraphGUI(int *biggest_index)
326 {
328  for (uint i = 0; i < lengthof(LinkGraphOverlay::LINK_COLOURS); ++i) {
329  NWidgetBackground * wid = new NWidgetBackground(WWT_PANEL, COLOUR_DARK_GREEN, i + WID_LGL_SATURATION_FIRST);
331  wid->SetFill(1, 1);
332  wid->SetResize(0, 0);
333  panel->Add(wid);
334  }
335  *biggest_index = WID_LGL_SATURATION_LAST;
336  return panel;
337 }
338 
339 NWidgetBase *MakeCargoesLegendLinkGraphGUI(int *biggest_index)
340 {
341  static const uint ENTRIES_PER_ROW = CeilDiv(NUM_CARGO, 5);
343  NWidgetHorizontal *row = NULL;
344  for (uint i = 0; i < NUM_CARGO; ++i) {
345  if (i % ENTRIES_PER_ROW == 0) {
346  if (row) panel->Add(row);
347  row = new NWidgetHorizontal(NC_EQUALSIZE);
348  }
349  NWidgetBackground * wid = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, i + WID_LGL_CARGO_FIRST);
351  wid->SetFill(1, 1);
352  wid->SetResize(0, 0);
353  row->Add(wid);
354  }
355  /* Fill up last row */
356  for (uint i = 0; i < 4 - (NUM_CARGO - 1) % 5; ++i) {
358  spc->SetFill(1, 1);
359  spc->SetResize(0, 0);
360  row->Add(spc);
361  }
362  panel->Add(row);
363  *biggest_index = WID_LGL_CARGO_LAST;
364  return panel;
365 }
366 
367 
368 static const NWidgetPart _nested_linkgraph_legend_widgets[] = {
370  NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
371  NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_LGL_CAPTION), SetDataTip(STR_LINKGRAPH_LEGEND_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
372  NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
373  NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
374  EndContainer(),
375  NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
377  NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_SATURATION),
379  NWidgetFunction(MakeSaturationLegendLinkGraphGUI),
380  EndContainer(),
381  NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_COMPANIES),
385  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_COMPANIES_ALL), SetDataTip(STR_LINKGRAPH_LEGEND_ALL, STR_NULL),
386  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_COMPANIES_NONE), SetDataTip(STR_LINKGRAPH_LEGEND_NONE, STR_NULL),
387  EndContainer(),
388  EndContainer(),
389  NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_CARGOES),
392  NWidgetFunction(MakeCargoesLegendLinkGraphGUI),
393  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_CARGOES_ALL), SetDataTip(STR_LINKGRAPH_LEGEND_ALL, STR_NULL),
394  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_CARGOES_NONE), SetDataTip(STR_LINKGRAPH_LEGEND_NONE, STR_NULL),
395  EndContainer(),
396  EndContainer(),
397  EndContainer(),
398  EndContainer()
399 };
400 
401 assert_compile(WID_LGL_SATURATION_LAST - WID_LGL_SATURATION_FIRST ==
403 
404 static WindowDesc _linkgraph_legend_desc(
405  WDP_AUTO, "toolbar_linkgraph", 0, 0,
407  0,
408  _nested_linkgraph_legend_widgets, lengthof(_nested_linkgraph_legend_widgets)
409 );
410 
415 {
416  AllocateWindowDescFront<LinkGraphLegendWindow>(&_linkgraph_legend_desc, 0);
417 }
418 
419 LinkGraphLegendWindow::LinkGraphLegendWindow(WindowDesc *desc, int window_number) : Window(desc)
420 {
421  this->InitNested(window_number);
422  this->InvalidateData(0);
423  this->SetOverlay(FindWindowById(WC_MAIN_WINDOW, 0)->viewport->overlay);
424 }
425 
431  this->overlay = overlay;
432  uint32 companies = this->overlay->GetCompanyMask();
433  for (uint c = 0; c < MAX_COMPANIES; c++) {
434  if (!this->IsWidgetDisabled(WID_LGL_COMPANY_FIRST + c)) {
435  this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST + c, HasBit(companies, c));
436  }
437  }
438  uint32 cargoes = this->overlay->GetCargoMask();
439  for (uint c = 0; c < NUM_CARGO; c++) {
440  if (!this->IsWidgetDisabled(WID_LGL_CARGO_FIRST + c)) {
441  this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST + c, HasBit(cargoes, c));
442  }
443  }
444 }
445 
446 void LinkGraphLegendWindow::UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
447 {
448  if (IsInsideMM(widget, WID_LGL_SATURATION_FIRST, WID_LGL_SATURATION_LAST + 1)) {
449  StringID str = STR_NULL;
450  if (widget == WID_LGL_SATURATION_FIRST) {
451  str = STR_LINKGRAPH_LEGEND_UNUSED;
452  } else if (widget == WID_LGL_SATURATION_LAST) {
453  str = STR_LINKGRAPH_LEGEND_OVERLOADED;
454  } else if (widget == (WID_LGL_SATURATION_LAST + WID_LGL_SATURATION_FIRST) / 2) {
455  str = STR_LINKGRAPH_LEGEND_SATURATED;
456  }
457  if (str != STR_NULL) {
458  Dimension dim = GetStringBoundingBox(str);
459  dim.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
460  dim.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
461  *size = maxdim(*size, dim);
462  }
463  }
464  if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
465  CargoSpec *cargo = CargoSpec::Get(widget - WID_LGL_CARGO_FIRST);
466  if (cargo->IsValid()) {
467  Dimension dim = GetStringBoundingBox(cargo->abbrev);
468  dim.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
469  dim.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
470  *size = maxdim(*size, dim);
471  }
472  }
473 }
474 
475 void LinkGraphLegendWindow::DrawWidget(const Rect &r, int widget) const
476 {
477  if (IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) {
478  if (this->IsWidgetDisabled(widget)) return;
479  CompanyID cid = (CompanyID)(widget - WID_LGL_COMPANY_FIRST);
480  Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
481  DrawCompanyIcon(cid, (r.left + r.right + 1 - sprite_size.width) / 2, (r.top + r.bottom + 1 - sprite_size.height) / 2);
482  }
483  if (IsInsideMM(widget, WID_LGL_SATURATION_FIRST, WID_LGL_SATURATION_LAST + 1)) {
484  GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, LinkGraphOverlay::LINK_COLOURS[widget - WID_LGL_SATURATION_FIRST]);
485  StringID str = STR_NULL;
486  if (widget == WID_LGL_SATURATION_FIRST) {
487  str = STR_LINKGRAPH_LEGEND_UNUSED;
488  } else if (widget == WID_LGL_SATURATION_LAST) {
489  str = STR_LINKGRAPH_LEGEND_OVERLOADED;
490  } else if (widget == (WID_LGL_SATURATION_LAST + WID_LGL_SATURATION_FIRST) / 2) {
491  str = STR_LINKGRAPH_LEGEND_SATURATED;
492  }
493  if (str != STR_NULL) DrawString(r.left, r.right, (r.top + r.bottom + 1 - FONT_HEIGHT_SMALL) / 2, str, TC_FROMSTRING, SA_HOR_CENTER);
494  }
495  if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
496  if (this->IsWidgetDisabled(widget)) return;
497  CargoSpec *cargo = CargoSpec::Get(widget - WID_LGL_CARGO_FIRST);
498  GfxFillRect(r.left + 2, r.top + 2, r.right - 2, r.bottom - 2, cargo->legend_colour);
499  DrawString(r.left, r.right, (r.top + r.bottom + 1 - FONT_HEIGHT_SMALL) / 2, cargo->abbrev, TC_BLACK, SA_HOR_CENTER);
500  }
501 }
502 
507 {
508  uint32 mask = 0;
509  for (uint c = 0; c < MAX_COMPANIES; c++) {
510  if (this->IsWidgetDisabled(c + WID_LGL_COMPANY_FIRST)) continue;
511  if (!this->IsWidgetLowered(c + WID_LGL_COMPANY_FIRST)) continue;
512  SetBit(mask, c);
513  }
514  this->overlay->SetCompanyMask(mask);
515 }
516 
521 {
522  uint32 mask = 0;
523  for (uint c = 0; c < NUM_CARGO; c++) {
524  if (this->IsWidgetDisabled(c + WID_LGL_CARGO_FIRST)) continue;
525  if (!this->IsWidgetLowered(c + WID_LGL_CARGO_FIRST)) continue;
526  SetBit(mask, c);
527  }
528  this->overlay->SetCargoMask(mask);
529 }
530 
531 void LinkGraphLegendWindow::OnClick(Point pt, int widget, int click_count)
532 {
533  /* Check which button is clicked */
534  if (IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) {
535  if (!this->IsWidgetDisabled(widget)) {
536  this->ToggleWidgetLoweredState(widget);
537  this->UpdateOverlayCompanies();
538  }
539  } else if (widget == WID_LGL_COMPANIES_ALL || widget == WID_LGL_COMPANIES_NONE) {
540  for (uint c = 0; c < MAX_COMPANIES; c++) {
541  if (this->IsWidgetDisabled(c + WID_LGL_COMPANY_FIRST)) continue;
542  this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST + c, widget == WID_LGL_COMPANIES_ALL);
543  }
544  this->UpdateOverlayCompanies();
545  this->SetDirty();
546  } else if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
547  if (!this->IsWidgetDisabled(widget)) {
548  this->ToggleWidgetLoweredState(widget);
549  this->UpdateOverlayCargoes();
550  }
551  } else if (widget == WID_LGL_CARGOES_ALL || widget == WID_LGL_CARGOES_NONE) {
552  for (uint c = 0; c < NUM_CARGO; c++) {
553  if (this->IsWidgetDisabled(c + WID_LGL_CARGO_FIRST)) continue;
554  this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST + c, widget == WID_LGL_CARGOES_ALL);
555  }
556  this->UpdateOverlayCargoes();
557  }
558  this->SetDirty();
559 }
560 
566 void LinkGraphLegendWindow::OnInvalidateData(int data, bool gui_scope)
567 {
568  /* Disable the companies who are not active */
569  for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
570  this->SetWidgetDisabledState(i + WID_LGL_COMPANY_FIRST, !Company::IsValidID(i));
571  }
572  for (CargoID i = 0; i < NUM_CARGO; i++) {
573  this->SetWidgetDisabledState(i + WID_LGL_CARGO_FIRST, !CargoSpec::Get(i)->IsValid());
574  }
575 }