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 
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 != INVALID_COMPANY && !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 
00074       supply += sta->goods[c].supply;
00075       const LinkStatMap &links = sta->goods[c].link_stats;
00076       for (LinkStatMap::const_iterator i = links.begin(); i != links.end(); ++i) {
00077         StationID to = i->first;
00078         if (seen_links.find(to) != seen_links.end()) continue;
00079 
00080         if (!Station::IsValidID(to)) continue;
00081         const Station *stb = Station::Get(to);
00082         if (stb->owner != INVALID_COMPANY && !HasBit(this->company_mask, stb->owner)) continue;
00083         if (stb->rect.IsEmpty()) continue;
00084 
00085         if (!this->IsLinkVisible(pta, this->GetStationMiddle(stb), &dpi)) continue;
00086 
00087         this->AddLinks(sta, stb);
00088         this->AddLinks(stb, sta);
00089         seen_links[to]; // make sure it is created and marked as seen
00090       }
00091     }
00092     if (this->IsPointVisible(pta, &dpi)) {
00093       this->cached_stations.push_back(std::make_pair(from, supply));
00094     }
00095   }
00096 }
00097 
00105 inline bool LinkGraphOverlay::IsPointVisible(Point pt, const DrawPixelInfo *dpi, int padding) const
00106 {
00107   return pt.x > dpi->left - padding && pt.y > dpi->top - padding &&
00108       pt.x < dpi->left + dpi->width + padding &&
00109       pt.y < dpi->top + dpi->height + padding;
00110 }
00111 
00120 inline bool LinkGraphOverlay::IsLinkVisible(Point pta, Point ptb, const DrawPixelInfo *dpi, int padding) const
00121 {
00122   return !((pta.x < dpi->left - padding && ptb.x < dpi->left - padding) ||
00123       (pta.y < dpi->top - padding && ptb.y < dpi->top - padding) ||
00124       (pta.x > dpi->left + dpi->width + padding &&
00125           ptb.x > dpi->left + dpi->width + padding) ||
00126       (pta.y > dpi->top + dpi->height + padding &&
00127           ptb.y > dpi->top + dpi->height + padding));
00128 }
00129 
00135 void LinkGraphOverlay::AddLinks(const Station *from, const Station *to)
00136 {
00137   CargoID c;
00138   FOR_EACH_SET_CARGO_ID(c, this->cargo_mask) {
00139     if (!CargoSpec::Get(c)->IsValid()) continue;
00140     const GoodsEntry &ge = from->goods[c];
00141     uint sum_flows = ge.GetSumFlowVia(to->index);
00142     const LinkStatMap &ls_map = ge.link_stats;
00143     LinkStatMap::const_iterator i = ls_map.find(to->index);
00144     if (i != ls_map.end()) {
00145       const LinkStat &link_stat = i->second;
00146       this->AddStats(link_stat, sum_flows, this->cached_links[from->index][to->index]);
00147     }
00148   }
00149 }
00150 
00157 /* static */ void LinkGraphOverlay::AddStats(const LinkStat &orig_link, uint new_plan, LinkProperties &cargo)
00158 {
00159   uint new_cap = orig_link.Capacity();
00160   uint new_usg = orig_link.Usage();
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[Company::Get(st->owner)->colour][5],
00235         _colour_gradient[COLOUR_GREY][1]);
00236   }
00237 }
00238 
00247 /* static */ void LinkGraphOverlay::DrawVertex(int x, int y, int size, int colour, int border_colour)
00248 {
00249   size--;
00250   int w1 = size / 2;
00251   int w2 = size / 2 + size % 2;
00252 
00253   GfxFillRect(x - w1, y - w1, x + w2, y + w2, colour);
00254 
00255   w1++;
00256   w2++;
00257   GfxDrawLine(x - w1, y - w1, x + w2, y - w1, border_colour);
00258   GfxDrawLine(x - w1, y + w2, x + w2, y + w2, border_colour);
00259   GfxDrawLine(x - w1, y - w1, x - w1, y + w2, border_colour);
00260   GfxDrawLine(x + w2, y - w1, x + w2, y + w2, border_colour);
00261 }
00262 
00268 Point LinkGraphOverlay::GetStationMiddle(const Station *st) const {
00269   if (this->window->viewport != NULL) {
00270     return GetViewportStationMiddle(this->window->viewport, st);
00271   } else {
00272     /* assume this is a smallmap */
00273     return static_cast<const SmallMapWindow *>(this->window)->GetStationMiddle(st);
00274   }
00275 }
00276 
00281 void LinkGraphOverlay::SetCargoMask(uint32 cargo_mask)
00282 {
00283   this->cargo_mask = cargo_mask;
00284   this->RebuildCache();
00285   this->window->GetWidget<NWidgetBase>(this->widget_id)->SetDirty(this->window);
00286 }
00287 
00292 void LinkGraphOverlay::SetCompanyMask(uint32 company_mask)
00293 {
00294   this->company_mask = company_mask;
00295   this->RebuildCache();
00296   this->window->GetWidget<NWidgetBase>(this->widget_id)->SetDirty(this->window);
00297 }
00298 
00300 NWidgetBase *MakeCompanyButtonRowsLinkGraphGUI(int *biggest_index)
00301 {
00302   return MakeCompanyButtonRows(biggest_index, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST, 3, STR_LINKGRAPH_LEGEND_SELECT_COMPANIES);
00303 }
00304 
00305 NWidgetBase *MakeSaturationLegendLinkGraphGUI(int *biggest_index)
00306 {
00307   NWidgetVertical *panel = new NWidgetVertical();
00308   for (uint i = 0; i < lengthof(LinkGraphOverlay::LINK_COLOURS); ++i) {
00309     NWidgetBackground * wid = new NWidgetBackground(WWT_PANEL, COLOUR_DARK_GREEN, i + WID_LGL_SATURATION_FIRST);
00310     wid->SetMinimalSize(50, FONT_HEIGHT_SMALL);
00311     wid->SetFill(0, 1);
00312     wid->SetResize(0, 1);
00313     panel->Add(wid);
00314   }
00315   *biggest_index = WID_LGL_SATURATION_LAST;
00316   return panel;
00317 }
00318 
00319 NWidgetBase *MakeCargoesLegendLinkGraphGUI(int *biggest_index)
00320 {
00321   static const uint ENTRIES_PER_ROW = CeilDiv(NUM_CARGO, 5);
00322   NWidgetVertical *panel = new NWidgetVertical();
00323   NWidgetHorizontal *row = NULL;
00324   for (uint i = 0; i < NUM_CARGO; ++i) {
00325     if (i % ENTRIES_PER_ROW == 0) {
00326       if (row) panel->Add(row);
00327       row = new NWidgetHorizontal();
00328     }
00329     NWidgetBackground * wid = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, i + WID_LGL_CARGO_FIRST);
00330     wid->SetMinimalSize(25, FONT_HEIGHT_SMALL);
00331     wid->SetFill(0, 1);
00332     wid->SetResize(0, 1);
00333     row->Add(wid);
00334   }
00335   panel->Add(row);
00336   *biggest_index = WID_LGL_CARGO_LAST;
00337   return panel;
00338 }
00339 
00340 
00341 static const NWidgetPart _nested_linkgraph_legend_widgets[] = {
00342   NWidget(NWID_HORIZONTAL),
00343     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
00344     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_LGL_CAPTION), SetDataTip(STR_LINKGRAPH_LEGEND_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00345     NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
00346     NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
00347   EndContainer(),
00348   NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
00349     NWidget(NWID_HORIZONTAL),
00350       NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_SATURATION),
00351         SetPadding(WD_FRAMERECT_TOP, 0, WD_FRAMERECT_BOTTOM, WD_CAPTIONTEXT_LEFT),
00352         SetMinimalSize(50, 100),
00353         NWidgetFunction(MakeSaturationLegendLinkGraphGUI),
00354       EndContainer(),
00355       NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_COMPANIES),
00356         SetPadding(WD_FRAMERECT_TOP, 0, WD_FRAMERECT_BOTTOM, WD_CAPTIONTEXT_LEFT),
00357         NWidget(NWID_VERTICAL, NC_EQUALSIZE),
00358           SetMinimalSize(100, 100),
00359           NWidgetFunction(MakeCompanyButtonRowsLinkGraphGUI),
00360           NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_COMPANIES_ALL), SetDataTip(STR_LINKGRAPH_LEGEND_ALL, STR_NULL),
00361           NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_COMPANIES_NONE), SetDataTip(STR_LINKGRAPH_LEGEND_NONE, STR_NULL),
00362         EndContainer(),
00363       EndContainer(),
00364       NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_CARGOES),
00365         SetPadding(WD_FRAMERECT_TOP, WD_FRAMERECT_RIGHT, WD_FRAMERECT_BOTTOM, WD_CAPTIONTEXT_LEFT),
00366         NWidget(NWID_VERTICAL, NC_EQUALSIZE),
00367           SetMinimalSize(150, 100),
00368           NWidgetFunction(MakeCargoesLegendLinkGraphGUI),
00369           NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_CARGOES_ALL), SetDataTip(STR_LINKGRAPH_LEGEND_ALL, STR_NULL),
00370           NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_CARGOES_NONE), SetDataTip(STR_LINKGRAPH_LEGEND_NONE, STR_NULL),
00371         EndContainer(),
00372       EndContainer(),
00373     EndContainer(),
00374   EndContainer()
00375 };
00376 
00377 assert_compile(WID_LGL_SATURATION_LAST - WID_LGL_SATURATION_FIRST ==
00378     lengthof(LinkGraphOverlay::LINK_COLOURS) - 1);
00379 
00380 static const WindowDesc _linkgraph_legend_desc(
00381   WDP_MANUAL, 300, 314,
00382   WC_LINKGRAPH_LEGEND, WC_NONE,
00383   0,
00384   _nested_linkgraph_legend_widgets, lengthof(_nested_linkgraph_legend_widgets)
00385 );
00386 
00390 void ShowLinkGraphLegend()
00391 {
00392   AllocateWindowDescFront<LinkGraphLegendWindow>(&_linkgraph_legend_desc, 0);
00393 }
00394 
00395 LinkGraphLegendWindow::LinkGraphLegendWindow(const WindowDesc *desc, int window_number)
00396 {
00397   this->InitNested(desc, window_number);
00398   this->InvalidateData(0);
00399   this->SetOverlay(FindWindowById(WC_MAIN_WINDOW, 0)->viewport->overlay);
00400 }
00401 
00406 void LinkGraphLegendWindow::SetOverlay(LinkGraphOverlay *overlay) {
00407   this->overlay = overlay;
00408   uint32 companies = this->overlay->GetCompanyMask();
00409   for (uint c = 0; c < MAX_COMPANIES; c++) {
00410     if (!this->IsWidgetDisabled(WID_LGL_COMPANY_FIRST + c)) {
00411       this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST + c, HasBit(companies, c));
00412     }
00413   }
00414   uint32 cargoes = this->overlay->GetCargoMask();
00415   for (uint c = 0; c < NUM_CARGO; c++) {
00416     if (!this->IsWidgetDisabled(WID_LGL_CARGO_FIRST + c)) {
00417       this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST + c, HasBit(cargoes, c));
00418     }
00419   }
00420 }
00421 
00422 void LinkGraphLegendWindow::DrawWidget(const Rect &r, int widget) const
00423 {
00424   const NWidgetBase *wid = this->GetWidget<NWidgetBase>(widget);
00425   if (IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) {
00426     if (this->IsWidgetDisabled(widget)) return;
00427     CompanyID cid = (CompanyID)(widget - WID_LGL_COMPANY_FIRST);
00428     Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
00429     DrawCompanyIcon(cid, (r.left + r.right - sprite_size.width) / 2, (r.top + r.bottom - sprite_size.height) / 2);
00430     return;
00431   }
00432   if (IsInsideMM(widget, WID_LGL_SATURATION_FIRST, WID_LGL_SATURATION_LAST + 1)) {
00433     GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, LinkGraphOverlay::LINK_COLOURS[widget - WID_LGL_SATURATION_FIRST]);
00434     if (widget == WID_LGL_SATURATION_FIRST) {
00435       DrawString(wid->pos_x, wid->current_x + wid->pos_x, wid->pos_y, STR_LINKGRAPH_LEGEND_UNUSED, TC_FROMSTRING, SA_HOR_CENTER);
00436     } else if (widget == WID_LGL_SATURATION_LAST) {
00437       DrawString(wid->pos_x, wid->current_x + wid->pos_x, wid->pos_y, STR_LINKGRAPH_LEGEND_OVERLOADED, TC_FROMSTRING, SA_HOR_CENTER);
00438     } else if (widget == (WID_LGL_SATURATION_LAST + WID_LGL_SATURATION_FIRST) / 2) {
00439       DrawString(wid->pos_x, wid->current_x + wid->pos_x, wid->pos_y, STR_LINKGRAPH_LEGEND_SATURATED, TC_FROMSTRING, SA_HOR_CENTER);
00440     }
00441   }
00442   if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
00443     if (this->IsWidgetDisabled(widget)) return;
00444     CargoSpec *cargo = CargoSpec::Get(widget - WID_LGL_CARGO_FIRST);
00445     GfxFillRect(r.left + 2, r.top + 2, r.right - 2, r.bottom - 2, cargo->legend_colour);
00446     DrawString(wid->pos_x, wid->current_x + wid->pos_x, wid->pos_y + 2, cargo->abbrev, TC_BLACK, SA_HOR_CENTER);
00447   }
00448 }
00449 
00453 void LinkGraphLegendWindow::UpdateOverlayCompanies()
00454 {
00455   uint32 mask = 0;
00456   for (uint c = 0; c < MAX_COMPANIES; c++) {
00457     if (this->IsWidgetDisabled(c + WID_LGL_COMPANY_FIRST)) continue;
00458     if (!this->IsWidgetLowered(c + WID_LGL_COMPANY_FIRST)) continue;
00459     SetBit(mask, c);
00460   }
00461   this->overlay->SetCompanyMask(mask);
00462 }
00463 
00467 void LinkGraphLegendWindow::UpdateOverlayCargoes()
00468 {
00469   uint32 mask = 0;
00470   for (uint c = 0; c < NUM_CARGO; c++) {
00471     if (this->IsWidgetDisabled(c + WID_LGL_CARGO_FIRST)) continue;
00472     if (!this->IsWidgetLowered(c + WID_LGL_CARGO_FIRST)) continue;
00473     SetBit(mask, c);
00474   }
00475   this->overlay->SetCargoMask(mask);
00476 }
00477 
00478 void LinkGraphLegendWindow::OnClick(Point pt, int widget, int click_count)
00479 {
00480   /* Check which button is clicked */
00481   if (IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) {
00482     if (!this->IsWidgetDisabled(widget)) {
00483       this->ToggleWidgetLoweredState(widget);
00484       this->UpdateOverlayCompanies();
00485     }
00486   } else if (widget == WID_LGL_COMPANIES_ALL || widget == WID_LGL_COMPANIES_NONE) {
00487     for (uint c = 0; c < MAX_COMPANIES; c++) {
00488       if (this->IsWidgetDisabled(c + WID_LGL_COMPANY_FIRST)) continue;
00489       this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST + c, widget == WID_LGL_COMPANIES_ALL);
00490     }
00491     this->UpdateOverlayCompanies();
00492     this->SetDirty();
00493   } else if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
00494     if (!this->IsWidgetDisabled(widget)) {
00495       this->ToggleWidgetLoweredState(widget);
00496       this->UpdateOverlayCargoes();
00497     }
00498   } else if (widget == WID_LGL_CARGOES_ALL || widget == WID_LGL_CARGOES_NONE) {
00499     for (uint c = 0; c < NUM_CARGO; c++) {
00500       if (this->IsWidgetDisabled(c + WID_LGL_CARGO_FIRST)) continue;
00501       this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST + c, widget == WID_LGL_CARGOES_ALL);
00502     }
00503     this->UpdateOverlayCargoes();
00504   }
00505   this->SetDirty();
00506 }
00507 
00513 void LinkGraphLegendWindow::OnInvalidateData(int data, bool gui_scope)
00514 {
00515   /* Disable the companies who are not active */
00516   for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
00517     this->SetWidgetDisabledState(i + WID_LGL_COMPANY_FIRST, !Company::IsValidID(i));
00518   }
00519   for (CargoID i = 0; i < NUM_CARGO; i++) {
00520     this->SetWidgetDisabledState(i + WID_LGL_CARGO_FIRST, !CargoSpec::Get(i)->IsValid());
00521   }
00522 }