linkgraph_gui.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "../stdafx.h"
00013 #include "../window_gui.h"
00014 #include "../window_func.h"
00015 #include "../company_base.h"
00016 #include "../company_gui.h"
00017 #include "../date_func.h"
00018 #include "../viewport_func.h"
00019 #include "../smallmap_gui.h"
00020 #include "../widgets/link_graph_legend_widget.h"
00021 
00022 #include "table/strings.h"
00023 
00028 const uint8 LinkGraphOverlay::LINK_COLOURS[] = {
00029   0x0f, 0xd1, 0xd0, 0x57,
00030   0x55, 0x53, 0xbf, 0xbd,
00031   0xba, 0xb9, 0xb7, 0xb5
00032 };
00033 
00038 void LinkGraphOverlay::GetWidgetDpi(DrawPixelInfo *dpi) const
00039 {
00040   const NWidgetBase *wi = this->window->GetWidget<NWidgetBase>(this->widget_id);
00041   dpi->left = dpi->top = 0;
00042   dpi->width = wi->current_x;
00043   dpi->height = wi->current_y;
00044 }
00045 
00049 void LinkGraphOverlay::RebuildCache()
00050 {
00051   this->cached_links.clear();
00052   this->cached_stations.clear();
00053   if (this->company_mask == 0) return;
00054 
00055   DrawPixelInfo dpi;
00056   this->GetWidgetDpi(&dpi);
00057 
00058   const Station *sta;
00059   FOR_ALL_STATIONS(sta) {
00060     /* Show links between stations of selected companies or "neutral" ones like oilrigs. */
00061     if (sta->owner != OWNER_NONE && !HasBit(this->company_mask, sta->owner)) continue;
00062     if (sta->rect.IsEmpty()) continue;
00063 
00064     Point pta = this->GetStationMiddle(sta);
00065 
00066     StationID from = sta->index;
00067     StationLinkMap &seen_links = this->cached_links[from];
00068 
00069     uint supply = 0;
00070     CargoID c;
00071     FOR_EACH_SET_CARGO_ID(c, this->cargo_mask) {
00072       if (!CargoSpec::Get(c)->IsValid()) continue;
00073       if (!LinkGraph::IsValidID(sta->goods[c].link_graph)) continue;
00074       const LinkGraph &lg = *LinkGraph::Get(sta->goods[c].link_graph);
00075 
00076       ConstNode from_node = lg[sta->goods[c].node];
00077       supply += lg.Monthly(from_node.Supply());
00078       for (ConstEdgeIterator i = from_node.Begin(); i != from_node.End(); ++i) {
00079         StationID to = lg[i->first].Station();
00080         assert(from != to);
00081         if (!Station::IsValidID(to) || seen_links.find(to) != seen_links.end()) {
00082           continue;
00083         }
00084         const Station *stb = Station::Get(to);
00085         assert(sta != stb);
00086         if (stb->owner != OWNER_NONE && !HasBit(this->company_mask, stb->owner)) continue;
00087         if (stb->rect.IsEmpty()) continue;
00088 
00089         if (!this->IsLinkVisible(pta, this->GetStationMiddle(stb), &dpi)) continue;
00090 
00091         this->AddLinks(sta, stb);
00092         seen_links[to]; // make sure it is created and marked as seen
00093       }
00094     }
00095     if (this->IsPointVisible(pta, &dpi)) {
00096       this->cached_stations.push_back(std::make_pair(from, supply));
00097     }
00098   }
00099 }
00100 
00108 inline bool LinkGraphOverlay::IsPointVisible(Point pt, const DrawPixelInfo *dpi, int padding) const
00109 {
00110   return pt.x > dpi->left - padding && pt.y > dpi->top - padding &&
00111       pt.x < dpi->left + dpi->width + padding &&
00112       pt.y < dpi->top + dpi->height + padding;
00113 }
00114 
00123 inline bool LinkGraphOverlay::IsLinkVisible(Point pta, Point ptb, const DrawPixelInfo *dpi, int padding) const
00124 {
00125   return !((pta.x < dpi->left - padding && ptb.x < dpi->left - padding) ||
00126       (pta.y < dpi->top - padding && ptb.y < dpi->top - padding) ||
00127       (pta.x > dpi->left + dpi->width + padding &&
00128           ptb.x > dpi->left + dpi->width + padding) ||
00129       (pta.y > dpi->top + dpi->height + padding &&
00130           ptb.y > dpi->top + dpi->height + padding));
00131 }
00132 
00138 void LinkGraphOverlay::AddLinks(const Station *from, const Station *to)
00139 {
00140   CargoID c;
00141   FOR_EACH_SET_CARGO_ID(c, this->cargo_mask) {
00142     if (!CargoSpec::Get(c)->IsValid()) continue;
00143     const GoodsEntry &ge = from->goods[c];
00144     if (!LinkGraph::IsValidID(ge.link_graph) ||
00145         ge.link_graph != to->goods[c].link_graph) {
00146       continue;
00147     }
00148     const LinkGraph &lg = *LinkGraph::Get(ge.link_graph);
00149     ConstEdge edge = lg[ge.node][to->goods[c].node];
00150     if (edge.Capacity() > 0) {
00151       this->AddStats(lg.Monthly(edge.Capacity()), lg.Monthly(edge.Usage()),
00152           ge.GetSumFlowVia(to->index), this->cached_links[from->index][to->index]);
00153     }
00154   }
00155 }
00156 
00163 /* static */ void LinkGraphOverlay::AddStats(uint new_cap, uint new_usg, uint new_plan, LinkProperties &cargo)
00164 {
00165   /* multiply the numbers by 32 in order to avoid comparing to 0 too often. */
00166   if (cargo.capacity == 0 ||
00167       max(cargo.usage, cargo.planned) * 32 / (cargo.capacity + 1) < max(new_usg, new_plan) * 32 / (new_cap + 1)) {
00168     cargo.capacity = new_cap;
00169     cargo.usage = new_usg;
00170     cargo.planned = new_plan;
00171   }
00172 }
00173 
00178 void LinkGraphOverlay::Draw(const DrawPixelInfo *dpi) const
00179 {
00180   this->DrawLinks(dpi);
00181   this->DrawStationDots(dpi);
00182 }
00183 
00188 void LinkGraphOverlay::DrawLinks(const DrawPixelInfo *dpi) const
00189 {
00190   for (LinkMap::const_iterator i(this->cached_links.begin()); i != this->cached_links.end(); ++i) {
00191     if (!Station::IsValidID(i->first)) continue;
00192     Point pta = this->GetStationMiddle(Station::Get(i->first));
00193     for (StationLinkMap::const_iterator j(i->second.begin()); j != i->second.end(); ++j) {
00194       if (!Station::IsValidID(j->first)) continue;
00195       Point ptb = this->GetStationMiddle(Station::Get(j->first));
00196       if (!this->IsLinkVisible(pta, ptb, dpi, this->scale + 2)) continue;
00197       this->DrawContent(pta, ptb, j->second);
00198     }
00199   }
00200 }
00201 
00208 void LinkGraphOverlay::DrawContent(Point pta, Point ptb, const LinkProperties &cargo) const
00209 {
00210   int offset_y = (pta.x < ptb.x ? 1 : -1) * this->scale;
00211   int offset_x = (pta.y > ptb.y ? 1 : -1) * this->scale;
00212 
00213   uint usage_or_plan = min(cargo.capacity * 2 + 1, max(cargo.usage, cargo.planned));
00214   int colour = LinkGraphOverlay::LINK_COLOURS[usage_or_plan * lengthof(LinkGraphOverlay::LINK_COLOURS) / (cargo.capacity * 2 + 2)];
00215 
00216   GfxDrawLine(pta.x + offset_x, pta.y, ptb.x + offset_x, ptb.y, colour, scale);
00217   GfxDrawLine(pta.x, pta.y + offset_y, ptb.x, ptb.y + offset_y, colour, scale);
00218 
00219   GfxDrawLine(pta.x, pta.y, ptb.x, ptb.y, _colour_gradient[COLOUR_GREY][1], this->scale);
00220 }
00221 
00226 void LinkGraphOverlay::DrawStationDots(const DrawPixelInfo *dpi) const
00227 {
00228   for (StationSupplyList::const_iterator i(this->cached_stations.begin()); i != this->cached_stations.end(); ++i) {
00229     const Station *st = Station::GetIfValid(i->first);
00230     if (st == NULL) continue;
00231     Point pt = this->GetStationMiddle(st);
00232     if (!this->IsPointVisible(pt, dpi, 3 * this->scale)) continue;
00233 
00234     uint r = this->scale * 2 + this->scale * 2 * min(200, i->second) / 200;
00235 
00236     LinkGraphOverlay::DrawVertex(pt.x, pt.y, r,
00237         _colour_gradient[st->owner != OWNER_NONE ?
00238             (Colours)Company::Get(st->owner)->colour : COLOUR_GREY][5],
00239         _colour_gradient[COLOUR_GREY][1]);
00240   }
00241 }
00242 
00251 /* static */ void LinkGraphOverlay::DrawVertex(int x, int y, int size, int colour, int border_colour)
00252 {
00253   size--;
00254   int w1 = size / 2;
00255   int w2 = size / 2 + size % 2;
00256 
00257   GfxFillRect(x - w1, y - w1, x + w2, y + w2, colour);
00258 
00259   w1++;
00260   w2++;
00261   GfxDrawLine(x - w1, y - w1, x + w2, y - w1, border_colour);
00262   GfxDrawLine(x - w1, y + w2, x + w2, y + w2, border_colour);
00263   GfxDrawLine(x - w1, y - w1, x - w1, y + w2, border_colour);
00264   GfxDrawLine(x + w2, y - w1, x + w2, y + w2, border_colour);
00265 }
00266 
00272 Point LinkGraphOverlay::GetStationMiddle(const Station *st) const
00273 {
00274   if (this->window->viewport != NULL) {
00275     return GetViewportStationMiddle(this->window->viewport, st);
00276   } else {
00277     /* assume this is a smallmap */
00278     return static_cast<const SmallMapWindow *>(this->window)->GetStationMiddle(st);
00279   }
00280 }
00281 
00286 void LinkGraphOverlay::SetCargoMask(uint32 cargo_mask)
00287 {
00288   this->cargo_mask = cargo_mask;
00289   this->RebuildCache();
00290   this->window->GetWidget<NWidgetBase>(this->widget_id)->SetDirty(this->window);
00291 }
00292 
00297 void LinkGraphOverlay::SetCompanyMask(uint32 company_mask)
00298 {
00299   this->company_mask = company_mask;
00300   this->RebuildCache();
00301   this->window->GetWidget<NWidgetBase>(this->widget_id)->SetDirty(this->window);
00302 }
00303 
00305 NWidgetBase *MakeCompanyButtonRowsLinkGraphGUI(int *biggest_index)
00306 {
00307   return MakeCompanyButtonRows(biggest_index, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST, 3, STR_LINKGRAPH_LEGEND_SELECT_COMPANIES);
00308 }
00309 
00310 NWidgetBase *MakeSaturationLegendLinkGraphGUI(int *biggest_index)
00311 {
00312   NWidgetVertical *panel = new NWidgetVertical();
00313   for (uint i = 0; i < lengthof(LinkGraphOverlay::LINK_COLOURS); ++i) {
00314     NWidgetBackground * wid = new NWidgetBackground(WWT_PANEL, COLOUR_DARK_GREEN, i + WID_LGL_SATURATION_FIRST);
00315     wid->SetMinimalSize(50, FONT_HEIGHT_SMALL);
00316     wid->SetFill(0, 1);
00317     wid->SetResize(0, 1);
00318     panel->Add(wid);
00319   }
00320   *biggest_index = WID_LGL_SATURATION_LAST;
00321   return panel;
00322 }
00323 
00324 NWidgetBase *MakeCargoesLegendLinkGraphGUI(int *biggest_index)
00325 {
00326   static const uint ENTRIES_PER_ROW = CeilDiv(NUM_CARGO, 5);
00327   NWidgetVertical *panel = new NWidgetVertical();
00328   NWidgetHorizontal *row = NULL;
00329   for (uint i = 0; i < NUM_CARGO; ++i) {
00330     if (i % ENTRIES_PER_ROW == 0) {
00331       if (row) panel->Add(row);
00332       row = new NWidgetHorizontal();
00333     }
00334     NWidgetBackground * wid = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, i + WID_LGL_CARGO_FIRST);
00335     wid->SetMinimalSize(25, FONT_HEIGHT_SMALL);
00336     wid->SetFill(0, 1);
00337     wid->SetResize(0, 1);
00338     row->Add(wid);
00339   }
00340   panel->Add(row);
00341   *biggest_index = WID_LGL_CARGO_LAST;
00342   return panel;
00343 }
00344 
00345 
00346 static const NWidgetPart _nested_linkgraph_legend_widgets[] = {
00347   NWidget(NWID_HORIZONTAL),
00348     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
00349     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_LGL_CAPTION), SetDataTip(STR_LINKGRAPH_LEGEND_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00350     NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
00351     NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
00352   EndContainer(),
00353   NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
00354     NWidget(NWID_HORIZONTAL),
00355       NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_SATURATION),
00356         SetPadding(WD_FRAMERECT_TOP, 0, WD_FRAMERECT_BOTTOM, WD_CAPTIONTEXT_LEFT),
00357         SetMinimalSize(50, 100),
00358         NWidgetFunction(MakeSaturationLegendLinkGraphGUI),
00359       EndContainer(),
00360       NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_COMPANIES),
00361         SetPadding(WD_FRAMERECT_TOP, 0, WD_FRAMERECT_BOTTOM, WD_CAPTIONTEXT_LEFT),
00362         NWidget(NWID_VERTICAL, NC_EQUALSIZE),
00363           SetMinimalSize(100, 100),
00364           NWidgetFunction(MakeCompanyButtonRowsLinkGraphGUI),
00365           NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_COMPANIES_ALL), SetDataTip(STR_LINKGRAPH_LEGEND_ALL, STR_NULL),
00366           NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_COMPANIES_NONE), SetDataTip(STR_LINKGRAPH_LEGEND_NONE, STR_NULL),
00367         EndContainer(),
00368       EndContainer(),
00369       NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_CARGOES),
00370         SetPadding(WD_FRAMERECT_TOP, WD_FRAMERECT_RIGHT, WD_FRAMERECT_BOTTOM, WD_CAPTIONTEXT_LEFT),
00371         NWidget(NWID_VERTICAL, NC_EQUALSIZE),
00372           SetMinimalSize(150, 100),
00373           NWidgetFunction(MakeCargoesLegendLinkGraphGUI),
00374           NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_CARGOES_ALL), SetDataTip(STR_LINKGRAPH_LEGEND_ALL, STR_NULL),
00375           NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_CARGOES_NONE), SetDataTip(STR_LINKGRAPH_LEGEND_NONE, STR_NULL),
00376         EndContainer(),
00377       EndContainer(),
00378     EndContainer(),
00379   EndContainer()
00380 };
00381 
00382 assert_compile(WID_LGL_SATURATION_LAST - WID_LGL_SATURATION_FIRST ==
00383     lengthof(LinkGraphOverlay::LINK_COLOURS) - 1);
00384 
00385 static WindowDesc _linkgraph_legend_desc(
00386   WDP_MANUAL, "toolbar_linkgraph", 300, 314,
00387   WC_LINKGRAPH_LEGEND, WC_NONE,
00388   0,
00389   _nested_linkgraph_legend_widgets, lengthof(_nested_linkgraph_legend_widgets)
00390 );
00391 
00395 void ShowLinkGraphLegend()
00396 {
00397   AllocateWindowDescFront<LinkGraphLegendWindow>(&_linkgraph_legend_desc, 0);
00398 }
00399 
00400 LinkGraphLegendWindow::LinkGraphLegendWindow(WindowDesc *desc, int window_number) : Window(desc)
00401 {
00402   this->InitNested(window_number);
00403   this->InvalidateData(0);
00404   this->SetOverlay(FindWindowById(WC_MAIN_WINDOW, 0)->viewport->overlay);
00405 }
00406 
00411 void LinkGraphLegendWindow::SetOverlay(LinkGraphOverlay *overlay) {
00412   this->overlay = overlay;
00413   uint32 companies = this->overlay->GetCompanyMask();
00414   for (uint c = 0; c < MAX_COMPANIES; c++) {
00415     if (!this->IsWidgetDisabled(WID_LGL_COMPANY_FIRST + c)) {
00416       this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST + c, HasBit(companies, c));
00417     }
00418   }
00419   uint32 cargoes = this->overlay->GetCargoMask();
00420   for (uint c = 0; c < NUM_CARGO; c++) {
00421     if (!this->IsWidgetDisabled(WID_LGL_CARGO_FIRST + c)) {
00422       this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST + c, HasBit(cargoes, c));
00423     }
00424   }
00425 }
00426 
00427 void LinkGraphLegendWindow::DrawWidget(const Rect &r, int widget) const
00428 {
00429   const NWidgetBase *wid = this->GetWidget<NWidgetBase>(widget);
00430   if (IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) {
00431     if (this->IsWidgetDisabled(widget)) return;
00432     CompanyID cid = (CompanyID)(widget - WID_LGL_COMPANY_FIRST);
00433     Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
00434     DrawCompanyIcon(cid, (r.left + r.right - sprite_size.width) / 2, (r.top + r.bottom - sprite_size.height) / 2);
00435     return;
00436   }
00437   if (IsInsideMM(widget, WID_LGL_SATURATION_FIRST, WID_LGL_SATURATION_LAST + 1)) {
00438     GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, LinkGraphOverlay::LINK_COLOURS[widget - WID_LGL_SATURATION_FIRST]);
00439     if (widget == WID_LGL_SATURATION_FIRST) {
00440       DrawString(wid->pos_x, wid->current_x + wid->pos_x, wid->pos_y, STR_LINKGRAPH_LEGEND_UNUSED, TC_FROMSTRING, SA_HOR_CENTER);
00441     } else if (widget == WID_LGL_SATURATION_LAST) {
00442       DrawString(wid->pos_x, wid->current_x + wid->pos_x, wid->pos_y, STR_LINKGRAPH_LEGEND_OVERLOADED, TC_FROMSTRING, SA_HOR_CENTER);
00443     } else if (widget == (WID_LGL_SATURATION_LAST + WID_LGL_SATURATION_FIRST) / 2) {
00444       DrawString(wid->pos_x, wid->current_x + wid->pos_x, wid->pos_y, STR_LINKGRAPH_LEGEND_SATURATED, TC_FROMSTRING, SA_HOR_CENTER);
00445     }
00446   }
00447   if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
00448     if (this->IsWidgetDisabled(widget)) return;
00449     CargoSpec *cargo = CargoSpec::Get(widget - WID_LGL_CARGO_FIRST);
00450     GfxFillRect(r.left + 2, r.top + 2, r.right - 2, r.bottom - 2, cargo->legend_colour);
00451     DrawString(wid->pos_x, wid->current_x + wid->pos_x, wid->pos_y + 2, cargo->abbrev, TC_BLACK, SA_HOR_CENTER);
00452   }
00453 }
00454 
00458 void LinkGraphLegendWindow::UpdateOverlayCompanies()
00459 {
00460   uint32 mask = 0;
00461   for (uint c = 0; c < MAX_COMPANIES; c++) {
00462     if (this->IsWidgetDisabled(c + WID_LGL_COMPANY_FIRST)) continue;
00463     if (!this->IsWidgetLowered(c + WID_LGL_COMPANY_FIRST)) continue;
00464     SetBit(mask, c);
00465   }
00466   this->overlay->SetCompanyMask(mask);
00467 }
00468 
00472 void LinkGraphLegendWindow::UpdateOverlayCargoes()
00473 {
00474   uint32 mask = 0;
00475   for (uint c = 0; c < NUM_CARGO; c++) {
00476     if (this->IsWidgetDisabled(c + WID_LGL_CARGO_FIRST)) continue;
00477     if (!this->IsWidgetLowered(c + WID_LGL_CARGO_FIRST)) continue;
00478     SetBit(mask, c);
00479   }
00480   this->overlay->SetCargoMask(mask);
00481 }
00482 
00483 void LinkGraphLegendWindow::OnClick(Point pt, int widget, int click_count)
00484 {
00485   /* Check which button is clicked */
00486   if (IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) {
00487     if (!this->IsWidgetDisabled(widget)) {
00488       this->ToggleWidgetLoweredState(widget);
00489       this->UpdateOverlayCompanies();
00490     }
00491   } else if (widget == WID_LGL_COMPANIES_ALL || widget == WID_LGL_COMPANIES_NONE) {
00492     for (uint c = 0; c < MAX_COMPANIES; c++) {
00493       if (this->IsWidgetDisabled(c + WID_LGL_COMPANY_FIRST)) continue;
00494       this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST + c, widget == WID_LGL_COMPANIES_ALL);
00495     }
00496     this->UpdateOverlayCompanies();
00497     this->SetDirty();
00498   } else if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
00499     if (!this->IsWidgetDisabled(widget)) {
00500       this->ToggleWidgetLoweredState(widget);
00501       this->UpdateOverlayCargoes();
00502     }
00503   } else if (widget == WID_LGL_CARGOES_ALL || widget == WID_LGL_CARGOES_NONE) {
00504     for (uint c = 0; c < NUM_CARGO; c++) {
00505       if (this->IsWidgetDisabled(c + WID_LGL_CARGO_FIRST)) continue;
00506       this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST + c, widget == WID_LGL_CARGOES_ALL);
00507     }
00508     this->UpdateOverlayCargoes();
00509   }
00510   this->SetDirty();
00511 }
00512 
00518 void LinkGraphLegendWindow::OnInvalidateData(int data, bool gui_scope)
00519 {
00520   /* Disable the companies who are not active */
00521   for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
00522     this->SetWidgetDisabledState(i + WID_LGL_COMPANY_FIRST, !Company::IsValidID(i));
00523   }
00524   for (CargoID i = 0; i < NUM_CARGO; i++) {
00525     this->SetWidgetDisabledState(i + WID_LGL_CARGO_FIRST, !CargoSpec::Get(i)->IsValid());
00526   }
00527 }