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