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 uint 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, uint new_plan, LinkProperties &cargo)
00155 {
00156 uint new_cap = orig_link.Capacity();
00157 uint new_usg = orig_link.Usage();
00158
00159
00160 if (cargo.capacity == 0 ||
00161 max(cargo.usage, cargo.planned) * 32 / (cargo.capacity + 1) < max(new_usg, new_plan) * 32 / (new_cap + 1)) {
00162 cargo.capacity = new_cap;
00163 cargo.usage = new_usg;
00164 cargo.planned = new_plan;
00165 }
00166 }
00167
00172 void LinkGraphOverlay::Draw(const DrawPixelInfo *dpi) const
00173 {
00174 this->DrawLinks(dpi);
00175 this->DrawStationDots(dpi);
00176 }
00177
00182 void LinkGraphOverlay::DrawLinks(const DrawPixelInfo *dpi) const
00183 {
00184 for (LinkMap::const_iterator i(this->cached_links.begin()); i != this->cached_links.end(); ++i) {
00185 if (!Station::IsValidID(i->first)) continue;
00186 Point pta = this->GetStationMiddle(Station::Get(i->first));
00187 for (StationLinkMap::const_iterator j(i->second.begin()); j != i->second.end(); ++j) {
00188 if (!Station::IsValidID(j->first)) continue;
00189 Point ptb = this->GetStationMiddle(Station::Get(j->first));
00190 if (!this->IsLinkVisible(pta, ptb, dpi, this->scale + 2)) continue;
00191 this->DrawContent(pta, ptb, j->second);
00192 }
00193 }
00194 }
00195
00202 void LinkGraphOverlay::DrawContent(Point pta, Point ptb, const LinkProperties &cargo) const
00203 {
00204 int offset_y = (pta.x < ptb.x ? 1 : -1) * this->scale;
00205 int offset_x = (pta.y > ptb.y ? 1 : -1) * this->scale;
00206
00207 uint usage_or_plan = min(cargo.capacity * 2 + 1, max(cargo.usage, cargo.planned));
00208 int colour = LinkGraphOverlay::LINK_COLOURS[usage_or_plan * lengthof(LinkGraphOverlay::LINK_COLOURS) / (cargo.capacity * 2 + 2)];
00209
00210 GfxDrawLine(pta.x + offset_x, pta.y, ptb.x + offset_x, ptb.y, colour, scale);
00211 GfxDrawLine(pta.x, pta.y + offset_y, ptb.x, ptb.y + offset_y, colour, scale);
00212
00213 GfxDrawLine(pta.x, pta.y, ptb.x, ptb.y, _colour_gradient[COLOUR_GREY][1], this->scale);
00214 }
00215
00220 void LinkGraphOverlay::DrawStationDots(const DrawPixelInfo *dpi) const
00221 {
00222 for (StationSupplyList::const_iterator i(this->cached_stations.begin()); i != this->cached_stations.end(); ++i) {
00223 const Station *st = Station::GetIfValid(i->first);
00224 if (st == NULL) continue;
00225 Point pt = this->GetStationMiddle(st);
00226 if (!this->IsPointVisible(pt, dpi, 3 * this->scale)) continue;
00227
00228 uint r = this->scale * 2 + this->scale * 2 * min(200, i->second) / 200;
00229
00230 LinkGraphOverlay::DrawVertex(pt.x, pt.y, r,
00231 _colour_gradient[Company::Get(st->owner)->colour][5],
00232 _colour_gradient[COLOUR_GREY][1]);
00233 }
00234 }
00235
00244 void LinkGraphOverlay::DrawVertex(int x, int y, int size, int colour, int border_colour)
00245 {
00246 size--;
00247 int w1 = size / 2;
00248 int w2 = size / 2 + size % 2;
00249
00250 GfxFillRect(x - w1, y - w1, x + w2, y + w2, colour);
00251
00252 w1++;
00253 w2++;
00254 GfxDrawLine(x - w1, y - w1, x + w2, y - w1, border_colour);
00255 GfxDrawLine(x - w1, y + w2, x + w2, y + w2, border_colour);
00256 GfxDrawLine(x - w1, y - w1, x - w1, y + w2, border_colour);
00257 GfxDrawLine(x + w2, y - w1, x + w2, y + w2, border_colour);
00258 }
00259
00265 Point LinkGraphOverlay::GetStationMiddle(const Station *st) const {
00266 if (this->window->viewport != NULL) {
00267 return GetViewportStationMiddle(this->window->viewport, st);
00268 } else {
00269
00270 return static_cast<const SmallMapWindow *>(this->window)->GetStationMiddle(st);
00271 }
00272 }
00273
00278 void LinkGraphOverlay::SetCargoMask(uint32 cargo_mask)
00279 {
00280 this->cargo_mask = cargo_mask;
00281 this->RebuildCache();
00282 this->window->GetWidget<NWidgetBase>(this->widget_id)->SetDirty(this->window);
00283 }
00284
00289 void LinkGraphOverlay::SetCompanyMask(uint32 company_mask)
00290 {
00291 this->company_mask = company_mask;
00292 this->RebuildCache();
00293 this->window->GetWidget<NWidgetBase>(this->widget_id)->SetDirty(this->window);
00294 }
00295
00296 enum LinkGraphLegendWindowWidgets {
00297 LGL_CAPTION,
00298 LGL_SATURATION,
00299 LGL_SATURATION_FIRST,
00300 LGL_SATURATION_LAST = LGL_SATURATION_FIRST + lengthof(LinkGraphOverlay::LINK_COLOURS) - 1,
00301 LGL_COMPANIES,
00302 LGL_COMPANY_FIRST,
00303 LGL_COMPANY_LAST = LGL_COMPANY_FIRST + MAX_COMPANIES - 1,
00304 LGL_COMPANIES_ALL,
00305 LGL_COMPANIES_NONE,
00306 LGL_CARGOES,
00307 LGL_CARGO_FIRST,
00308 LGL_CARGO_LAST = LGL_CARGO_FIRST + NUM_CARGO - 1,
00309 LGL_CARGOES_ALL,
00310 LGL_CARGOES_NONE,s
00311 };
00312
00314 NWidgetBase *MakeCompanyButtonRowsLinkGraphGUI(int *biggest_index)
00315 {
00316 return MakeCompanyButtonRows(biggest_index, LGL_COMPANY_FIRST, LGL_COMPANY_LAST, 3, STR_LINKGRAPH_LEGEND_SELECT_COMPANIES);
00317 }
00318
00319 NWidgetBase *MakeSaturationLegendLinkGraphGUI(int *biggest_index)
00320 {
00321 NWidgetVertical *panel = new NWidgetVertical();
00322 for (uint i = 0; i < lengthof(LinkGraphOverlay::LINK_COLOURS); ++i) {
00323 NWidgetBackground * wid = new NWidgetBackground(WWT_PANEL, COLOUR_DARK_GREEN, i + LGL_SATURATION_FIRST);
00324 wid->SetMinimalSize(50, FONT_HEIGHT_SMALL);
00325 wid->SetFill(0, 1);
00326 wid->SetResize(0, 1);
00327 panel->Add(wid);
00328 }
00329 *biggest_index = LGL_SATURATION_LAST;
00330 return panel;
00331 }
00332
00333 NWidgetBase *MakeCargoesLegendLinkGraphGUI(int *biggest_index)
00334 {
00335 static const uint ENTRIES_PER_ROW = CeilDiv(NUM_CARGO, 5);
00336 NWidgetVertical *panel = new NWidgetVertical();
00337 NWidgetHorizontal *row = NULL;
00338 for (uint i = 0; i < NUM_CARGO; ++i) {
00339 if (i % ENTRIES_PER_ROW == 0) {
00340 if (row) panel->Add(row);
00341 row = new NWidgetHorizontal();
00342 }
00343 NWidgetBackground * wid = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, i + LGL_CARGO_FIRST);
00344 wid->SetMinimalSize(25, FONT_HEIGHT_SMALL);
00345 wid->SetFill(0, 1);
00346 wid->SetResize(0, 1);
00347 row->Add(wid);
00348 }
00349 panel->Add(row);
00350 *biggest_index = LGL_CARGO_LAST;
00351 return panel;
00352 }
00353
00354
00355 static const NWidgetPart _nested_linkgraph_legend_widgets[] = {
00356 NWidget(NWID_HORIZONTAL),
00357 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
00358 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, LGL_CAPTION), SetDataTip(STR_LINKGRAPH_LEGEND_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00359 NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
00360 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
00361 EndContainer(),
00362 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
00363 NWidget(NWID_HORIZONTAL),
00364 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, LGL_SATURATION),
00365 SetPadding(WD_FRAMERECT_TOP, 0, WD_FRAMERECT_BOTTOM, WD_CAPTIONTEXT_LEFT),
00366 SetMinimalSize(50, 100),
00367 NWidgetFunction(MakeSaturationLegendLinkGraphGUI),
00368 EndContainer(),
00369 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, LGL_COMPANIES),
00370 SetPadding(WD_FRAMERECT_TOP, 0, WD_FRAMERECT_BOTTOM, WD_CAPTIONTEXT_LEFT),
00371 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
00372 SetMinimalSize(100, 100),
00373 NWidgetFunction(MakeCompanyButtonRowsLinkGraphGUI),
00374 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, LGL_COMPANIES_ALL), SetDataTip(STR_LINKGRAPH_LEGEND_ALL, STR_NULL),
00375 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, LGL_COMPANIES_NONE), SetDataTip(STR_LINKGRAPH_LEGEND_NONE, STR_NULL),
00376 EndContainer(),
00377 EndContainer(),
00378 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, LGL_CARGOES),
00379 SetPadding(WD_FRAMERECT_TOP, WD_FRAMERECT_RIGHT, WD_FRAMERECT_BOTTOM, WD_CAPTIONTEXT_LEFT),
00380 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
00381 SetMinimalSize(150, 100),
00382 NWidgetFunction(MakeCargoesLegendLinkGraphGUI),
00383 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, LGL_CARGOES_ALL), SetDataTip(STR_LINKGRAPH_LEGEND_ALL, STR_NULL),
00384 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, LGL_CARGOES_NONE), SetDataTip(STR_LINKGRAPH_LEGEND_NONE, STR_NULL),
00385 EndContainer(),
00386 EndContainer(),
00387 EndContainer(),
00388 EndContainer()
00389 };
00390
00391 static const WindowDesc _linkgraph_legend_desc(
00392 WDP_MANUAL, 300, 314,
00393 WC_LINKGRAPH_LEGEND, WC_NONE,
00394 WDF_UNCLICK_BUTTONS,
00395 _nested_linkgraph_legend_widgets, lengthof(_nested_linkgraph_legend_widgets)
00396 );
00397
00401 void ShowLinkGraphLegend()
00402 {
00403 AllocateWindowDescFront<LinkGraphLegendWindow>(&_linkgraph_legend_desc, 0);
00404 }
00405
00406 LinkGraphLegendWindow::LinkGraphLegendWindow(const WindowDesc *desc, int window_number)
00407 {
00408 this->InitNested(desc, window_number);
00409 this->InvalidateData(0);
00410 this->SetOverlay(FindWindowById(WC_MAIN_WINDOW, 0)->viewport->overlay);
00411 }
00412
00417 void LinkGraphLegendWindow::SetOverlay(LinkGraphOverlay *overlay) {
00418 this->overlay = overlay;
00419 uint32 companies = this->overlay->GetCompanyMask();
00420 for (uint c = 0; c < MAX_COMPANIES; c++) {
00421 if (!this->IsWidgetDisabled(LGL_COMPANY_FIRST + c)) {
00422 this->SetWidgetLoweredState(LGL_COMPANY_FIRST + c, HasBit(companies, c));
00423 }
00424 }
00425 uint32 cargoes = this->overlay->GetCargoMask();
00426 for (uint c = 0; c < NUM_CARGO; c++) {
00427 if (!this->IsWidgetDisabled(LGL_CARGO_FIRST + c)) {
00428 this->SetWidgetLoweredState(LGL_CARGO_FIRST + c, HasBit(cargoes, c));
00429 }
00430 }
00431 }
00432
00433 void LinkGraphLegendWindow::DrawWidget(const Rect &r, int widget) const
00434 {
00435 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(widget);
00436 if (IsInsideMM(widget, LGL_COMPANY_FIRST, LGL_COMPANY_LAST + 1)) {
00437 if (this->IsWidgetDisabled(widget)) return;
00438 CompanyID cid = (CompanyID)(widget - LGL_COMPANY_FIRST);
00439 Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
00440 DrawCompanyIcon(cid, (r.left + r.right - sprite_size.width) / 2, (r.top + r.bottom - sprite_size.height) / 2);
00441 return;
00442 }
00443 if (IsInsideMM(widget, LGL_SATURATION_FIRST, LGL_SATURATION_LAST + 1)) {
00444 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, LinkGraphOverlay::LINK_COLOURS[widget - LGL_SATURATION_FIRST]);
00445 if (widget == LGL_SATURATION_FIRST) {
00446 DrawString(wid->pos_x, wid->current_x + wid->pos_x, wid->pos_y, STR_LINKGRAPH_LEGEND_UNUSED, TC_FROMSTRING, SA_HOR_CENTER);
00447 } else if (widget == LGL_SATURATION_LAST) {
00448 DrawString(wid->pos_x, wid->current_x + wid->pos_x, wid->pos_y, STR_LINKGRAPH_LEGEND_OVERLOADED, TC_FROMSTRING, SA_HOR_CENTER);
00449 } else if (widget == (LGL_SATURATION_LAST + LGL_SATURATION_FIRST) / 2) {
00450 DrawString(wid->pos_x, wid->current_x + wid->pos_x, wid->pos_y, STR_LINKGRAPH_LEGEND_SATURATED, TC_FROMSTRING, SA_HOR_CENTER);
00451 }
00452 }
00453 if (IsInsideMM(widget, LGL_CARGO_FIRST, LGL_CARGO_LAST + 1)) {
00454 if (this->IsWidgetDisabled(widget)) return;
00455 CargoSpec *cargo = CargoSpec::Get(widget - LGL_CARGO_FIRST);
00456 GfxFillRect(r.left + 2, r.top + 2, r.right - 2, r.bottom - 2, cargo->legend_colour);
00457 DrawString(wid->pos_x, wid->current_x + wid->pos_x, wid->pos_y + 2, cargo->abbrev, TC_BLACK, SA_HOR_CENTER);
00458 }
00459 }
00460
00464 void LinkGraphLegendWindow::UpdateOverlayCompanies()
00465 {
00466 uint32 mask = 0;
00467 for (uint c = 0; c < MAX_COMPANIES; c++) {
00468 if (this->IsWidgetDisabled(c + LGL_COMPANY_FIRST)) continue;
00469 if (!this->IsWidgetLowered(c + LGL_COMPANY_FIRST)) continue;
00470 SetBit(mask, c);
00471 }
00472 this->overlay->SetCompanyMask(mask);
00473 }
00474
00478 void LinkGraphLegendWindow::UpdateOverlayCargoes()
00479 {
00480 uint32 mask = 0;
00481 for (uint c = 0; c < NUM_CARGO; c++) {
00482 if (this->IsWidgetDisabled(c + LGL_CARGO_FIRST)) continue;
00483 if (!this->IsWidgetLowered(c + LGL_CARGO_FIRST)) continue;
00484 SetBit(mask, c);
00485 }
00486 this->overlay->SetCargoMask(mask);
00487 }
00488
00489 void LinkGraphLegendWindow::OnClick(Point pt, int widget, int click_count)
00490 {
00491
00492 if (IsInsideMM(widget, LGL_COMPANY_FIRST, LGL_COMPANY_LAST + 1)) {
00493
00494 if (!this->IsWidgetDisabled(widget)) {
00495 this->ToggleWidgetLoweredState(widget);
00496 this->UpdateOverlayCompanies();
00497 }
00498 } else if (widget == LGL_COMPANIES_ALL || widget == LGL_COMPANIES_NONE) {
00499 for (uint c = 0; c < MAX_COMPANIES; c++) {
00500 if (this->IsWidgetDisabled(c + LGL_COMPANY_FIRST)) continue;
00501 this->SetWidgetLoweredState(LGL_COMPANY_FIRST + c, widget == LGL_COMPANIES_ALL);
00502 }
00503 this->UpdateOverlayCompanies();
00504 this->SetDirty();
00505 } else if (IsInsideMM(widget, LGL_CARGO_FIRST, LGL_CARGO_LAST + 1)) {
00506
00507 if (!this->IsWidgetDisabled(widget)) {
00508 this->ToggleWidgetLoweredState(widget);
00509 this->UpdateOverlayCargoes();
00510
00511 }
00512 } else if (widget == LGL_CARGOES_ALL || widget == LGL_CARGOES_NONE) {
00513 for (uint c = 0; c < NUM_CARGO; c++) {
00514 if (this->IsWidgetDisabled(c + LGL_CARGO_FIRST)) continue;
00515 this->SetWidgetLoweredState(LGL_CARGO_FIRST + c, widget == LGL_CARGOES_ALL);
00516 }
00517 this->UpdateOverlayCargoes();
00518 }
00519 this->SetDirty();
00520 }
00521
00527 void LinkGraphLegendWindow::OnInvalidateData(int data, bool gui_scope)
00528 {
00529
00530 for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
00531 this->SetWidgetDisabledState(i + LGL_COMPANY_FIRST, !Company::IsValidID(i));
00532 }
00533 for (CargoID i = 0; i < NUM_CARGO; i++) {
00534 this->SetWidgetDisabledState(i + LGL_CARGO_FIRST, !CargoSpec::Get(i)->IsValid());
00535 }
00536 }