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
00026 const uint8 LinkGraphOverlay::LINK_COLOURS[] = {
00027 0x0f, 0xd1, 0xd0, 0x57,
00028 0x55, 0x53, 0xbf, 0xbd,
00029 0xba, 0xb9, 0xb7, 0xb5
00030 };
00031
00036 void LinkGraphOverlay::GetWidgetDpi(DrawPixelInfo *dpi) const
00037 {
00038 const NWidgetBase *wi = this->window->GetWidget<NWidgetBase>(this->widget_id);
00039 dpi->left = dpi->top = 0;
00040 dpi->width = wi->current_x;
00041 dpi->height = wi->current_y;
00042 }
00043
00047 void LinkGraphOverlay::RebuildCache()
00048 {
00049 this->cached_links.clear();
00050 this->cached_stations.clear();
00051
00052 DrawPixelInfo dpi;
00053 this->GetWidgetDpi(&dpi);
00054
00055 const Station *sta;
00056 FOR_ALL_STATIONS(sta) {
00057
00058 if (sta->owner != INVALID_COMPANY && !HasBit(this->company_mask, sta->owner)) continue;
00059 if (sta->rect.IsEmpty()) continue;
00060
00061 Point pta = this->GetStationMiddle(sta);
00062
00063 StationID from = sta->index;
00064 StationLinkMap &seen_links = this->cached_links[from];
00065
00066 uint supply = 0;
00067 CargoID c;
00068 FOR_EACH_SET_CARGO_ID(c, this->cargo_mask) {
00069 if (!CargoSpec::Get(c)->IsValid()) continue;
00070
00071 supply += sta->goods[c].supply;
00072 const LinkStatMap &links = sta->goods[c].link_stats;
00073 for (LinkStatMap::const_iterator i = links.begin(); i != links.end(); ++i) {
00074 StationID to = i->first;
00075 if (seen_links.find(to) != seen_links.end()) continue;
00076
00077 if (!Station::IsValidID(to)) continue;
00078 const Station *stb = Station::Get(to);
00079 if (stb->owner != INVALID_COMPANY && !HasBit(this->company_mask, stb->owner)) continue;
00080 if (stb->rect.IsEmpty()) continue;
00081
00082 if (!this->IsLinkVisible(pta, this->GetStationMiddle(stb), &dpi)) continue;
00083
00084 this->AddLinks(sta, stb);
00085 this->AddLinks(stb, sta);
00086 seen_links[to];
00087 }
00088 }
00089 if (this->IsPointVisible(pta, &dpi)) {
00090 this->cached_stations.push_back(std::make_pair(from, supply));
00091 }
00092 }
00093 }
00094
00102 FORCEINLINE bool LinkGraphOverlay::IsPointVisible(Point pt, const DrawPixelInfo *dpi, int padding) const
00103 {
00104 return pt.x > dpi->left - padding && pt.y > dpi->top - padding &&
00105 pt.x < dpi->left + dpi->width + padding &&
00106 pt.y < dpi->top + dpi->height + padding;
00107 }
00108
00117 FORCEINLINE bool LinkGraphOverlay::IsLinkVisible(Point pta, Point ptb, const DrawPixelInfo *dpi, int padding) const
00118 {
00119 return !((pta.x < dpi->left - padding && ptb.x < dpi->left - padding) ||
00120 (pta.y < dpi->top - padding && ptb.y < dpi->top - padding) ||
00121 (pta.x > dpi->left + dpi->width + padding &&
00122 ptb.x > dpi->left + dpi->width + padding) ||
00123 (pta.y > dpi->top + dpi->height + padding &&
00124 ptb.y > dpi->top + dpi->height + padding));
00125 }
00126
00132 void LinkGraphOverlay::AddLinks(const Station *from, const Station *to)
00133 {
00134 CargoID c;
00135 FOR_EACH_SET_CARGO_ID(c, this->cargo_mask) {
00136 if (!CargoSpec::Get(c)->IsValid()) continue;
00137 const GoodsEntry &ge = from->goods[c];
00138 FlowStat sum_flows = ge.GetSumFlowVia(to->index);
00139 const LinkStatMap &ls_map = ge.link_stats;
00140 LinkStatMap::const_iterator i = ls_map.find(to->index);
00141 if (i != ls_map.end()) {
00142 const LinkStat &link_stat = i->second;
00143 this->AddStats(link_stat, sum_flows, this->cached_links[from->index][to->index]);
00144 }
00145 }
00146 }
00147
00154 void LinkGraphOverlay::AddStats(const LinkStat &orig_link, const FlowStat &orig_flow, LinkProperties &cargo)
00155 {
00156 uint new_cap = orig_link.Capacity();
00157 uint new_usg = orig_link.Usage();
00158 uint new_plan = orig_flow.Planned();
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
00297 enum LinkGraphLegendWindowWidgets {
00298 LGL_CAPTION,
00299 LGL_SATURATION,
00300 LGL_SATURATION_FIRST,
00301 LGL_SATURATION_LAST = LGL_SATURATION_FIRST + lengthof(LinkGraphOverlay::LINK_COLOURS) - 1,
00302 LGL_COMPANIES,
00303 LGL_COMPANY_FIRST,
00304 LGL_COMPANY_LAST = LGL_COMPANY_FIRST + MAX_COMPANIES - 1,
00305 LGL_COMPANIES_ALL,
00306 LGL_COMPANIES_NONE,
00307 LGL_CARGOES,
00308 LGL_CARGO_FIRST,
00309 LGL_CARGO_LAST = LGL_CARGO_FIRST + NUM_CARGO - 1,
00310 LGL_CARGOES_ALL,
00311 LGL_CARGOES_NONE,s
00312 };
00313
00315 NWidgetBase *MakeCompanyButtonRowsLinkGraphGUI(int *biggest_index)
00316 {
00317 return MakeCompanyButtonRows(biggest_index, LGL_COMPANY_FIRST, LGL_COMPANY_LAST, 3, STR_LINKGRAPH_LEGEND_SELECT_COMPANIES);
00318 }
00319
00320 NWidgetBase *MakeSaturationLegendLinkGraphGUI(int *biggest_index)
00321 {
00322 NWidgetVertical *panel = new NWidgetVertical();
00323 for (uint i = 0; i < lengthof(LinkGraphOverlay::LINK_COLOURS); ++i) {
00324 NWidgetBackground * wid = new NWidgetBackground(WWT_PANEL, COLOUR_DARK_GREEN, i + LGL_SATURATION_FIRST);
00325 wid->SetMinimalSize(50, FONT_HEIGHT_SMALL);
00326 wid->SetFill(0, 1);
00327 wid->SetResize(0, 1);
00328 panel->Add(wid);
00329 }
00330 *biggest_index = LGL_SATURATION_LAST;
00331 return panel;
00332 }
00333
00334 NWidgetBase *MakeCargoesLegendLinkGraphGUI(int *biggest_index)
00335 {
00336 static const uint ENTRIES_PER_ROW = CeilDiv(NUM_CARGO, 5);
00337 NWidgetVertical *panel = new NWidgetVertical();
00338 NWidgetHorizontal *row = NULL;
00339 for (uint i = 0; i < NUM_CARGO; ++i) {
00340 if (i % ENTRIES_PER_ROW == 0) {
00341 if (row) panel->Add(row);
00342 row = new NWidgetHorizontal();
00343 }
00344 NWidgetBackground * wid = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, i + LGL_CARGO_FIRST);
00345 wid->SetMinimalSize(25, FONT_HEIGHT_SMALL);
00346 wid->SetFill(0, 1);
00347 wid->SetResize(0, 1);
00348 row->Add(wid);
00349 }
00350 panel->Add(row);
00351 *biggest_index = LGL_CARGO_LAST;
00352 return panel;
00353 }
00354
00355
00356 static const NWidgetPart _nested_linkgraph_legend_widgets[] = {
00357 NWidget(NWID_HORIZONTAL),
00358 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
00359 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, LGL_CAPTION), SetDataTip(STR_LINKGRAPH_LEGEND_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00360 NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
00361 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
00362 EndContainer(),
00363 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
00364 NWidget(NWID_HORIZONTAL),
00365 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, LGL_SATURATION),
00366 SetPadding(WD_FRAMERECT_TOP, 0, WD_FRAMERECT_BOTTOM, WD_CAPTIONTEXT_LEFT),
00367 SetMinimalSize(50, 100),
00368 NWidgetFunction(MakeSaturationLegendLinkGraphGUI),
00369 EndContainer(),
00370 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, LGL_COMPANIES),
00371 SetPadding(WD_FRAMERECT_TOP, 0, WD_FRAMERECT_BOTTOM, WD_CAPTIONTEXT_LEFT),
00372 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
00373 SetMinimalSize(100, 100),
00374 NWidgetFunction(MakeCompanyButtonRowsLinkGraphGUI),
00375 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, LGL_COMPANIES_ALL), SetDataTip(STR_LINKGRAPH_LEGEND_ALL, STR_NULL),
00376 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, LGL_COMPANIES_NONE), SetDataTip(STR_LINKGRAPH_LEGEND_NONE, STR_NULL),
00377 EndContainer(),
00378 EndContainer(),
00379 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, LGL_CARGOES),
00380 SetPadding(WD_FRAMERECT_TOP, WD_FRAMERECT_RIGHT, WD_FRAMERECT_BOTTOM, WD_CAPTIONTEXT_LEFT),
00381 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
00382 SetMinimalSize(150, 100),
00383 NWidgetFunction(MakeCargoesLegendLinkGraphGUI),
00384 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, LGL_CARGOES_ALL), SetDataTip(STR_LINKGRAPH_LEGEND_ALL, STR_NULL),
00385 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, LGL_CARGOES_NONE), SetDataTip(STR_LINKGRAPH_LEGEND_NONE, STR_NULL),
00386 EndContainer(),
00387 EndContainer(),
00388 EndContainer(),
00389 EndContainer()
00390 };
00391
00392 static const WindowDesc _linkgraph_legend_desc(
00393 WDP_MANUAL, 300, 314,
00394 WC_LINKGRAPH_LEGEND, WC_NONE,
00395 WDF_UNCLICK_BUTTONS,
00396 _nested_linkgraph_legend_widgets, lengthof(_nested_linkgraph_legend_widgets)
00397 );
00398
00402 void ShowLinkGraphLegend()
00403 {
00404 AllocateWindowDescFront<LinkGraphLegendWindow>(&_linkgraph_legend_desc, 0);
00405 }
00406
00407 LinkGraphLegendWindow::LinkGraphLegendWindow(const WindowDesc *desc, int window_number)
00408 {
00409 this->InitNested(desc, window_number);
00410 this->InvalidateData(0);
00411 this->SetOverlay(FindWindowById(WC_MAIN_WINDOW, 0)->viewport->overlay);
00412 }
00413
00418 void LinkGraphLegendWindow::SetOverlay(LinkGraphOverlay *overlay) {
00419 this->overlay = overlay;
00420 uint32 companies = this->overlay->GetCompanyMask();
00421 for (uint c = 0; c < MAX_COMPANIES; c++) {
00422 if (!this->IsWidgetDisabled(LGL_COMPANY_FIRST + c)) {
00423 this->SetWidgetLoweredState(LGL_COMPANY_FIRST + c, HasBit(companies, c));
00424 }
00425 }
00426 uint32 cargoes = this->overlay->GetCargoMask();
00427 for (uint c = 0; c < NUM_CARGO; c++) {
00428 if (!this->IsWidgetDisabled(LGL_CARGO_FIRST + c)) {
00429 this->SetWidgetLoweredState(LGL_CARGO_FIRST + c, HasBit(cargoes, c));
00430 }
00431 }
00432 }
00433
00434 void LinkGraphLegendWindow::DrawWidget(const Rect &r, int widget) const
00435 {
00436 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(widget);
00437 if (IsInsideMM(widget, LGL_COMPANY_FIRST, LGL_COMPANY_LAST + 1)) {
00438 if (this->IsWidgetDisabled(widget)) return;
00439 CompanyID cid = (CompanyID)(widget - LGL_COMPANY_FIRST);
00440 Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
00441 DrawCompanyIcon(cid, (r.left + r.right - sprite_size.width) / 2, (r.top + r.bottom - sprite_size.height) / 2);
00442 return;
00443 }
00444 if (IsInsideMM(widget, LGL_SATURATION_FIRST, LGL_SATURATION_LAST + 1)) {
00445 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, LinkGraphOverlay::LINK_COLOURS[widget - LGL_SATURATION_FIRST]);
00446 if (widget == LGL_SATURATION_FIRST) {
00447 DrawString(wid->pos_x, wid->current_x + wid->pos_x, wid->pos_y, STR_LINKGRAPH_LEGEND_UNUSED, TC_FROMSTRING, SA_HOR_CENTER);
00448 } else if (widget == LGL_SATURATION_LAST) {
00449 DrawString(wid->pos_x, wid->current_x + wid->pos_x, wid->pos_y, STR_LINKGRAPH_LEGEND_OVERLOADED, TC_FROMSTRING, SA_HOR_CENTER);
00450 } else if (widget == (LGL_SATURATION_LAST + LGL_SATURATION_FIRST) / 2) {
00451 DrawString(wid->pos_x, wid->current_x + wid->pos_x, wid->pos_y, STR_LINKGRAPH_LEGEND_SATURATED, TC_FROMSTRING, SA_HOR_CENTER);
00452 }
00453 }
00454 if (IsInsideMM(widget, LGL_CARGO_FIRST, LGL_CARGO_LAST + 1)) {
00455 if (this->IsWidgetDisabled(widget)) return;
00456 CargoSpec *cargo = CargoSpec::Get(widget - LGL_CARGO_FIRST);
00457 GfxFillRect(r.left + 2, r.top + 2, r.right - 2, r.bottom - 2, cargo->legend_colour);
00458 DrawString(wid->pos_x, wid->current_x + wid->pos_x, wid->pos_y + 2, cargo->abbrev, TC_BLACK, SA_HOR_CENTER);
00459 }
00460 }
00461
00465 void LinkGraphLegendWindow::UpdateOverlayCompanies()
00466 {
00467 uint32 mask = 0;
00468 for (uint c = 0; c < MAX_COMPANIES; c++) {
00469 if (this->IsWidgetDisabled(c + LGL_COMPANY_FIRST)) continue;
00470 if (!this->IsWidgetLowered(c + LGL_COMPANY_FIRST)) continue;
00471 SetBit(mask, c);
00472 }
00473 this->overlay->SetCompanyMask(mask);
00474 }
00475
00479 void LinkGraphLegendWindow::UpdateOverlayCargoes()
00480 {
00481 uint32 mask = 0;
00482 for (uint c = 0; c < NUM_CARGO; c++) {
00483 if (this->IsWidgetDisabled(c + LGL_CARGO_FIRST)) continue;
00484 if (!this->IsWidgetLowered(c + LGL_CARGO_FIRST)) continue;
00485 SetBit(mask, c);
00486 }
00487 this->overlay->SetCargoMask(mask);
00488 }
00489
00490 void LinkGraphLegendWindow::OnClick(Point pt, int widget, int click_count)
00491 {
00492
00493 if (IsInsideMM(widget, LGL_COMPANY_FIRST, LGL_COMPANY_LAST + 1)) {
00494
00495 if (!this->IsWidgetDisabled(widget)) {
00496 this->ToggleWidgetLoweredState(widget);
00497 this->UpdateOverlayCompanies();
00498 }
00499 } else if (widget == LGL_COMPANIES_ALL || widget == LGL_COMPANIES_NONE) {
00500 for (uint c = 0; c < MAX_COMPANIES; c++) {
00501 if (this->IsWidgetDisabled(c + LGL_COMPANY_FIRST)) continue;
00502 this->SetWidgetLoweredState(LGL_COMPANY_FIRST + c, widget == LGL_COMPANIES_ALL);
00503 }
00504 this->UpdateOverlayCompanies();
00505 this->SetDirty();
00506 } else if (IsInsideMM(widget, LGL_CARGO_FIRST, LGL_CARGO_LAST + 1)) {
00507
00508 if (!this->IsWidgetDisabled(widget)) {
00509 this->ToggleWidgetLoweredState(widget);
00510 this->UpdateOverlayCargoes();
00511
00512 }
00513 } else if (widget == LGL_CARGOES_ALL || widget == LGL_CARGOES_NONE) {
00514 for (uint c = 0; c < NUM_CARGO; c++) {
00515 if (this->IsWidgetDisabled(c + LGL_CARGO_FIRST)) continue;
00516 this->SetWidgetLoweredState(LGL_CARGO_FIRST + c, widget == LGL_CARGOES_ALL);
00517 }
00518 this->UpdateOverlayCargoes();
00519 }
00520 this->SetDirty();
00521 }
00522
00528 void LinkGraphLegendWindow::OnInvalidateData(int data, bool gui_scope)
00529 {
00530
00531 for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
00532 this->SetWidgetDisabledState(i + LGL_COMPANY_FIRST, !Company::IsValidID(i));
00533 }
00534 for (CargoID i = 0; i < NUM_CARGO; i++) {
00535 this->SetWidgetDisabledState(i + LGL_CARGO_FIRST, !CargoSpec::Get(i)->IsValid());
00536 }
00537 }