00001
00002
00003
00004
00005
00006
00007
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
00027 const uint8 LinkGraphOverlay::LINK_COLOURS[] = {
00028 0x0f, 0xd1, 0xd0, 0x57,
00029 0x55, 0x53, 0xbf, 0xbd,
00030 0xba, 0xb9, 0xb7, 0xb5
00031 };
00032
00037 void LinkGraphOverlay::GetWidgetDpi(DrawPixelInfo *dpi) const
00038 {
00039 const NWidgetBase *wi = this->window->GetWidget<NWidgetBase>(this->widget_id);
00040 dpi->left = dpi->top = 0;
00041 dpi->width = wi->current_x;
00042 dpi->height = wi->current_y;
00043 }
00044
00048 void LinkGraphOverlay::RebuildCache()
00049 {
00050 this->cached_links.clear();
00051 this->cached_stations.clear();
00052
00053 DrawPixelInfo dpi;
00054 this->GetWidgetDpi(&dpi);
00055
00056 const Station *sta;
00057 FOR_ALL_STATIONS(sta) {
00058
00059 if (sta->owner != INVALID_COMPANY && !HasBit(this->company_mask, sta->owner)) continue;
00060 if (sta->rect.IsEmpty()) continue;
00061
00062 Point pta = this->GetStationMiddle(sta);
00063
00064 StationID from = sta->index;
00065 StationLinkMap &seen_links = this->cached_links[from];
00066
00067 uint supply = 0;
00068 CargoID c;
00069 FOR_EACH_SET_CARGO_ID(c, this->cargo_mask) {
00070 if (!CargoSpec::Get(c)->IsValid()) continue;
00071
00072 supply += sta->goods[c].supply;
00073 const LinkStatMap &links = sta->goods[c].link_stats;
00074 for (LinkStatMap::const_iterator i = links.begin(); i != links.end(); ++i) {
00075 StationID to = i->first;
00076 if (seen_links.find(to) != seen_links.end()) continue;
00077
00078 if (!Station::IsValidID(to)) continue;
00079 const Station *stb = Station::Get(to);
00080 if (stb->owner != INVALID_COMPANY && !HasBit(this->company_mask, stb->owner)) continue;
00081 if (stb->rect.IsEmpty()) continue;
00082
00083 if (!this->IsLinkVisible(pta, this->GetStationMiddle(stb), &dpi)) continue;
00084
00085 this->AddLinks(sta, stb);
00086 this->AddLinks(stb, sta);
00087 seen_links[to];
00088 }
00089 }
00090 if (this->IsPointVisible(pta, &dpi)) {
00091 this->cached_stations.push_back(std::make_pair(from, supply));
00092 }
00093 }
00094 }
00095
00103 inline bool LinkGraphOverlay::IsPointVisible(Point pt, const DrawPixelInfo *dpi, int padding) const
00104 {
00105 return pt.x > dpi->left - padding && pt.y > dpi->top - padding &&
00106 pt.x < dpi->left + dpi->width + padding &&
00107 pt.y < dpi->top + dpi->height + padding;
00108 }
00109
00118 inline bool LinkGraphOverlay::IsLinkVisible(Point pta, Point ptb, const DrawPixelInfo *dpi, int padding) const
00119 {
00120 return !((pta.x < dpi->left - padding && ptb.x < dpi->left - padding) ||
00121 (pta.y < dpi->top - padding && ptb.y < dpi->top - padding) ||
00122 (pta.x > dpi->left + dpi->width + padding &&
00123 ptb.x > dpi->left + dpi->width + padding) ||
00124 (pta.y > dpi->top + dpi->height + padding &&
00125 ptb.y > dpi->top + dpi->height + padding));
00126 }
00127
00133 void LinkGraphOverlay::AddLinks(const Station *from, const Station *to)
00134 {
00135 CargoID c;
00136 FOR_EACH_SET_CARGO_ID(c, this->cargo_mask) {
00137 if (!CargoSpec::Get(c)->IsValid()) continue;
00138 const GoodsEntry &ge = from->goods[c];
00139 uint sum_flows = ge.GetSumFlowVia(to->index);
00140 const LinkStatMap &ls_map = ge.link_stats;
00141 LinkStatMap::const_iterator i = ls_map.find(to->index);
00142 if (i != ls_map.end()) {
00143 const LinkStat &link_stat = i->second;
00144 this->AddStats(link_stat, sum_flows, this->cached_links[from->index][to->index]);
00145 }
00146 }
00147 }
00148
00155 void LinkGraphOverlay::AddStats(const LinkStat &orig_link, uint new_plan, LinkProperties &cargo)
00156 {
00157 uint new_cap = orig_link.Capacity();
00158 uint new_usg = orig_link.Usage();
00159
00160
00161 if (cargo.capacity == 0 ||
00162 max(cargo.usage, cargo.planned) * 32 / (cargo.capacity + 1) < max(new_usg, new_plan) * 32 / (new_cap + 1)) {
00163 cargo.capacity = new_cap;
00164 cargo.usage = new_usg;
00165 cargo.planned = new_plan;
00166 }
00167 }
00168
00173 void LinkGraphOverlay::Draw(const DrawPixelInfo *dpi) const
00174 {
00175 this->DrawLinks(dpi);
00176 this->DrawStationDots(dpi);
00177 }
00178
00183 void LinkGraphOverlay::DrawLinks(const DrawPixelInfo *dpi) const
00184 {
00185 for (LinkMap::const_iterator i(this->cached_links.begin()); i != this->cached_links.end(); ++i) {
00186 if (!Station::IsValidID(i->first)) continue;
00187 Point pta = this->GetStationMiddle(Station::Get(i->first));
00188 for (StationLinkMap::const_iterator j(i->second.begin()); j != i->second.end(); ++j) {
00189 if (!Station::IsValidID(j->first)) continue;
00190 Point ptb = this->GetStationMiddle(Station::Get(j->first));
00191 if (!this->IsLinkVisible(pta, ptb, dpi, this->scale + 2)) continue;
00192 this->DrawContent(pta, ptb, j->second);
00193 }
00194 }
00195 }
00196
00203 void LinkGraphOverlay::DrawContent(Point pta, Point ptb, const LinkProperties &cargo) const
00204 {
00205 int offset_y = (pta.x < ptb.x ? 1 : -1) * this->scale;
00206 int offset_x = (pta.y > ptb.y ? 1 : -1) * this->scale;
00207
00208 uint usage_or_plan = min(cargo.capacity * 2 + 1, max(cargo.usage, cargo.planned));
00209 int colour = LinkGraphOverlay::LINK_COLOURS[usage_or_plan * lengthof(LinkGraphOverlay::LINK_COLOURS) / (cargo.capacity * 2 + 2)];
00210
00211 GfxDrawLine(pta.x + offset_x, pta.y, ptb.x + offset_x, ptb.y, colour, scale);
00212 GfxDrawLine(pta.x, pta.y + offset_y, ptb.x, ptb.y + offset_y, colour, scale);
00213
00214 GfxDrawLine(pta.x, pta.y, ptb.x, ptb.y, _colour_gradient[COLOUR_GREY][1], this->scale);
00215 }
00216
00221 void LinkGraphOverlay::DrawStationDots(const DrawPixelInfo *dpi) const
00222 {
00223 for (StationSupplyList::const_iterator i(this->cached_stations.begin()); i != this->cached_stations.end(); ++i) {
00224 const Station *st = Station::GetIfValid(i->first);
00225 if (st == NULL) continue;
00226 Point pt = this->GetStationMiddle(st);
00227 if (!this->IsPointVisible(pt, dpi, 3 * this->scale)) continue;
00228
00229 uint r = this->scale * 2 + this->scale * 2 * min(200, i->second) / 200;
00230
00231 LinkGraphOverlay::DrawVertex(pt.x, pt.y, r,
00232 _colour_gradient[Company::Get(st->owner)->colour][5],
00233 _colour_gradient[COLOUR_GREY][1]);
00234 }
00235 }
00236
00245 void LinkGraphOverlay::DrawVertex(int x, int y, int size, int colour, int border_colour)
00246 {
00247 size--;
00248 int w1 = size / 2;
00249 int w2 = size / 2 + size % 2;
00250
00251 GfxFillRect(x - w1, y - w1, x + w2, y + w2, colour);
00252
00253 w1++;
00254 w2++;
00255 GfxDrawLine(x - w1, y - w1, x + w2, y - w1, border_colour);
00256 GfxDrawLine(x - w1, y + w2, x + w2, y + w2, border_colour);
00257 GfxDrawLine(x - w1, y - w1, x - w1, y + w2, border_colour);
00258 GfxDrawLine(x + w2, y - w1, x + w2, y + w2, border_colour);
00259 }
00260
00266 Point LinkGraphOverlay::GetStationMiddle(const Station *st) const {
00267 if (this->window->viewport != NULL) {
00268 return GetViewportStationMiddle(this->window->viewport, st);
00269 } else {
00270
00271 return static_cast<const SmallMapWindow *>(this->window)->GetStationMiddle(st);
00272 }
00273 }
00274
00279 void LinkGraphOverlay::SetCargoMask(uint32 cargo_mask)
00280 {
00281 this->cargo_mask = cargo_mask;
00282 this->RebuildCache();
00283 this->window->GetWidget<NWidgetBase>(this->widget_id)->SetDirty(this->window);
00284 }
00285
00290 void LinkGraphOverlay::SetCompanyMask(uint32 company_mask)
00291 {
00292 this->company_mask = company_mask;
00293 this->RebuildCache();
00294 this->window->GetWidget<NWidgetBase>(this->widget_id)->SetDirty(this->window);
00295 }
00296
00298 NWidgetBase *MakeCompanyButtonRowsLinkGraphGUI(int *biggest_index)
00299 {
00300 return MakeCompanyButtonRows(biggest_index, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST, 3, STR_LINKGRAPH_LEGEND_SELECT_COMPANIES);
00301 }
00302
00303 NWidgetBase *MakeSaturationLegendLinkGraphGUI(int *biggest_index)
00304 {
00305 NWidgetVertical *panel = new NWidgetVertical();
00306 for (uint i = 0; i < lengthof(LinkGraphOverlay::LINK_COLOURS); ++i) {
00307 NWidgetBackground * wid = new NWidgetBackground(WWT_PANEL, COLOUR_DARK_GREEN, i + WID_LGL_SATURATION_FIRST);
00308 wid->SetMinimalSize(50, FONT_HEIGHT_SMALL);
00309 wid->SetFill(0, 1);
00310 wid->SetResize(0, 1);
00311 panel->Add(wid);
00312 }
00313 *biggest_index = WID_LGL_SATURATION_LAST;
00314 return panel;
00315 }
00316
00317 NWidgetBase *MakeCargoesLegendLinkGraphGUI(int *biggest_index)
00318 {
00319 static const uint ENTRIES_PER_ROW = CeilDiv(NUM_CARGO, 5);
00320 NWidgetVertical *panel = new NWidgetVertical();
00321 NWidgetHorizontal *row = NULL;
00322 for (uint i = 0; i < NUM_CARGO; ++i) {
00323 if (i % ENTRIES_PER_ROW == 0) {
00324 if (row) panel->Add(row);
00325 row = new NWidgetHorizontal();
00326 }
00327 NWidgetBackground * wid = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, i + WID_LGL_CARGO_FIRST);
00328 wid->SetMinimalSize(25, FONT_HEIGHT_SMALL);
00329 wid->SetFill(0, 1);
00330 wid->SetResize(0, 1);
00331 row->Add(wid);
00332 }
00333 panel->Add(row);
00334 *biggest_index = WID_LGL_CARGO_LAST;
00335 return panel;
00336 }
00337
00338
00339 static const NWidgetPart _nested_linkgraph_legend_widgets[] = {
00340 NWidget(NWID_HORIZONTAL),
00341 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
00342 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_LGL_CAPTION), SetDataTip(STR_LINKGRAPH_LEGEND_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00343 NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
00344 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
00345 EndContainer(),
00346 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
00347 NWidget(NWID_HORIZONTAL),
00348 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_SATURATION),
00349 SetPadding(WD_FRAMERECT_TOP, 0, WD_FRAMERECT_BOTTOM, WD_CAPTIONTEXT_LEFT),
00350 SetMinimalSize(50, 100),
00351 NWidgetFunction(MakeSaturationLegendLinkGraphGUI),
00352 EndContainer(),
00353 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_COMPANIES),
00354 SetPadding(WD_FRAMERECT_TOP, 0, WD_FRAMERECT_BOTTOM, WD_CAPTIONTEXT_LEFT),
00355 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
00356 SetMinimalSize(100, 100),
00357 NWidgetFunction(MakeCompanyButtonRowsLinkGraphGUI),
00358 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_COMPANIES_ALL), SetDataTip(STR_LINKGRAPH_LEGEND_ALL, STR_NULL),
00359 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_COMPANIES_NONE), SetDataTip(STR_LINKGRAPH_LEGEND_NONE, STR_NULL),
00360 EndContainer(),
00361 EndContainer(),
00362 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_CARGOES),
00363 SetPadding(WD_FRAMERECT_TOP, WD_FRAMERECT_RIGHT, WD_FRAMERECT_BOTTOM, WD_CAPTIONTEXT_LEFT),
00364 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
00365 SetMinimalSize(150, 100),
00366 NWidgetFunction(MakeCargoesLegendLinkGraphGUI),
00367 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_CARGOES_ALL), SetDataTip(STR_LINKGRAPH_LEGEND_ALL, STR_NULL),
00368 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_CARGOES_NONE), SetDataTip(STR_LINKGRAPH_LEGEND_NONE, STR_NULL),
00369 EndContainer(),
00370 EndContainer(),
00371 EndContainer(),
00372 EndContainer()
00373 };
00374
00375 static const WindowDesc _linkgraph_legend_desc(
00376 WDP_MANUAL, 300, 314,
00377 WC_LINKGRAPH_LEGEND, WC_NONE,
00378 WDF_UNCLICK_BUTTONS,
00379 _nested_linkgraph_legend_widgets, lengthof(_nested_linkgraph_legend_widgets)
00380 );
00381
00385 void ShowLinkGraphLegend()
00386 {
00387 AllocateWindowDescFront<LinkGraphLegendWindow>(&_linkgraph_legend_desc, 0);
00388 }
00389
00390 LinkGraphLegendWindow::LinkGraphLegendWindow(const WindowDesc *desc, int window_number)
00391 {
00392 this->InitNested(desc, window_number);
00393 this->InvalidateData(0);
00394 this->SetOverlay(FindWindowById(WC_MAIN_WINDOW, 0)->viewport->overlay);
00395 }
00396
00401 void LinkGraphLegendWindow::SetOverlay(LinkGraphOverlay *overlay) {
00402 this->overlay = overlay;
00403 uint32 companies = this->overlay->GetCompanyMask();
00404 for (uint c = 0; c < MAX_COMPANIES; c++) {
00405 if (!this->IsWidgetDisabled(WID_LGL_COMPANY_FIRST + c)) {
00406 this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST + c, HasBit(companies, c));
00407 }
00408 }
00409 uint32 cargoes = this->overlay->GetCargoMask();
00410 for (uint c = 0; c < NUM_CARGO; c++) {
00411 if (!this->IsWidgetDisabled(WID_LGL_CARGO_FIRST + c)) {
00412 this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST + c, HasBit(cargoes, c));
00413 }
00414 }
00415 }
00416
00417 void LinkGraphLegendWindow::DrawWidget(const Rect &r, int widget) const
00418 {
00419 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(widget);
00420 if (IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) {
00421 if (this->IsWidgetDisabled(widget)) return;
00422 CompanyID cid = (CompanyID)(widget - WID_LGL_COMPANY_FIRST);
00423 Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
00424 DrawCompanyIcon(cid, (r.left + r.right - sprite_size.width) / 2, (r.top + r.bottom - sprite_size.height) / 2);
00425 return;
00426 }
00427 if (IsInsideMM(widget, WID_LGL_SATURATION_FIRST, WID_LGL_SATURATION_LAST + 1)) {
00428 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, LinkGraphOverlay::LINK_COLOURS[widget - WID_LGL_SATURATION_FIRST]);
00429 if (widget == WID_LGL_SATURATION_FIRST) {
00430 DrawString(wid->pos_x, wid->current_x + wid->pos_x, wid->pos_y, STR_LINKGRAPH_LEGEND_UNUSED, TC_FROMSTRING, SA_HOR_CENTER);
00431 } else if (widget == WID_LGL_SATURATION_LAST) {
00432 DrawString(wid->pos_x, wid->current_x + wid->pos_x, wid->pos_y, STR_LINKGRAPH_LEGEND_OVERLOADED, TC_FROMSTRING, SA_HOR_CENTER);
00433 } else if (widget == (WID_LGL_SATURATION_LAST + WID_LGL_SATURATION_FIRST) / 2) {
00434 DrawString(wid->pos_x, wid->current_x + wid->pos_x, wid->pos_y, STR_LINKGRAPH_LEGEND_SATURATED, TC_FROMSTRING, SA_HOR_CENTER);
00435 }
00436 }
00437 if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
00438 if (this->IsWidgetDisabled(widget)) return;
00439 CargoSpec *cargo = CargoSpec::Get(widget - WID_LGL_CARGO_FIRST);
00440 GfxFillRect(r.left + 2, r.top + 2, r.right - 2, r.bottom - 2, cargo->legend_colour);
00441 DrawString(wid->pos_x, wid->current_x + wid->pos_x, wid->pos_y + 2, cargo->abbrev, TC_BLACK, SA_HOR_CENTER);
00442 }
00443 }
00444
00448 void LinkGraphLegendWindow::UpdateOverlayCompanies()
00449 {
00450 uint32 mask = 0;
00451 for (uint c = 0; c < MAX_COMPANIES; c++) {
00452 if (this->IsWidgetDisabled(c + WID_LGL_COMPANY_FIRST)) continue;
00453 if (!this->IsWidgetLowered(c + WID_LGL_COMPANY_FIRST)) continue;
00454 SetBit(mask, c);
00455 }
00456 this->overlay->SetCompanyMask(mask);
00457 }
00458
00462 void LinkGraphLegendWindow::UpdateOverlayCargoes()
00463 {
00464 uint32 mask = 0;
00465 for (uint c = 0; c < NUM_CARGO; c++) {
00466 if (this->IsWidgetDisabled(c + WID_LGL_CARGO_FIRST)) continue;
00467 if (!this->IsWidgetLowered(c + WID_LGL_CARGO_FIRST)) continue;
00468 SetBit(mask, c);
00469 }
00470 this->overlay->SetCargoMask(mask);
00471 }
00472
00473 void LinkGraphLegendWindow::OnClick(Point pt, int widget, int click_count)
00474 {
00475
00476 if (IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) {
00477
00478 if (!this->IsWidgetDisabled(widget)) {
00479 this->ToggleWidgetLoweredState(widget);
00480 this->UpdateOverlayCompanies();
00481 }
00482 } else if (widget == WID_LGL_COMPANIES_ALL || widget == WID_LGL_COMPANIES_NONE) {
00483 for (uint c = 0; c < MAX_COMPANIES; c++) {
00484 if (this->IsWidgetDisabled(c + WID_LGL_COMPANY_FIRST)) continue;
00485 this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST + c, widget == WID_LGL_COMPANIES_ALL);
00486 }
00487 this->UpdateOverlayCompanies();
00488 this->SetDirty();
00489 } else if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
00490
00491 if (!this->IsWidgetDisabled(widget)) {
00492 this->ToggleWidgetLoweredState(widget);
00493 this->UpdateOverlayCargoes();
00494
00495 }
00496 } else if (widget == WID_LGL_CARGOES_ALL || widget == WID_LGL_CARGOES_NONE) {
00497 for (uint c = 0; c < NUM_CARGO; c++) {
00498 if (this->IsWidgetDisabled(c + WID_LGL_CARGO_FIRST)) continue;
00499 this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST + c, widget == WID_LGL_CARGOES_ALL);
00500 }
00501 this->UpdateOverlayCargoes();
00502 }
00503 this->SetDirty();
00504 }
00505
00511 void LinkGraphLegendWindow::OnInvalidateData(int data, bool gui_scope)
00512 {
00513
00514 for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
00515 this->SetWidgetDisabledState(i + WID_LGL_COMPANY_FIRST, !Company::IsValidID(i));
00516 }
00517 for (CargoID i = 0; i < NUM_CARGO; i++) {
00518 this->SetWidgetDisabledState(i + WID_LGL_CARGO_FIRST, !CargoSpec::Get(i)->IsValid());
00519 }
00520 }