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
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
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
00075 supply += sta->goods[c].supply;
00076 const LinkStatMap &links = sta->goods[c].link_stats;
00077 for (LinkStatMap::const_iterator i = links.begin(); i != links.end(); ++i) {
00078 StationID to = i->first;
00079 if (seen_links.find(to) != seen_links.end()) continue;
00080
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];
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 const LinkStatMap &ls_map = ge.link_stats;
00144 LinkStatMap::const_iterator i = ls_map.find(to->index);
00145 if (i != ls_map.end()) {
00146 const LinkStat &link_stat = i->second;
00147 this->AddStats(link_stat, sum_flows, this->cached_links[from->index][to->index]);
00148 }
00149 }
00150 }
00151
00158 void LinkGraphOverlay::AddStats(const LinkStat &orig_link, uint new_plan, LinkProperties &cargo)
00159 {
00160 uint new_cap = orig_link.Capacity();
00161 uint new_usg = orig_link.Usage();
00162
00163
00164 if (cargo.capacity == 0 ||
00165 max(cargo.usage, cargo.planned) * 32 / (cargo.capacity + 1) < max(new_usg, new_plan) * 32 / (new_cap + 1)) {
00166 cargo.capacity = new_cap;
00167 cargo.usage = new_usg;
00168 cargo.planned = new_plan;
00169 }
00170 }
00171
00176 void LinkGraphOverlay::Draw(const DrawPixelInfo *dpi) const
00177 {
00178 this->DrawLinks(dpi);
00179 this->DrawStationDots(dpi);
00180 }
00181
00186 void LinkGraphOverlay::DrawLinks(const DrawPixelInfo *dpi) const
00187 {
00188 for (LinkMap::const_iterator i(this->cached_links.begin()); i != this->cached_links.end(); ++i) {
00189 if (!Station::IsValidID(i->first)) continue;
00190 Point pta = this->GetStationMiddle(Station::Get(i->first));
00191 for (StationLinkMap::const_iterator j(i->second.begin()); j != i->second.end(); ++j) {
00192 if (!Station::IsValidID(j->first)) continue;
00193 Point ptb = this->GetStationMiddle(Station::Get(j->first));
00194 if (!this->IsLinkVisible(pta, ptb, dpi, this->scale + 2)) continue;
00195 this->DrawContent(pta, ptb, j->second);
00196 }
00197 }
00198 }
00199
00206 void LinkGraphOverlay::DrawContent(Point pta, Point ptb, const LinkProperties &cargo) const
00207 {
00208 int offset_y = (pta.x < ptb.x ? 1 : -1) * this->scale;
00209 int offset_x = (pta.y > ptb.y ? 1 : -1) * this->scale;
00210
00211 uint usage_or_plan = min(cargo.capacity * 2 + 1, max(cargo.usage, cargo.planned));
00212 int colour = LinkGraphOverlay::LINK_COLOURS[usage_or_plan * lengthof(LinkGraphOverlay::LINK_COLOURS) / (cargo.capacity * 2 + 2)];
00213
00214 GfxDrawLine(pta.x + offset_x, pta.y, ptb.x + offset_x, ptb.y, colour, scale);
00215 GfxDrawLine(pta.x, pta.y + offset_y, ptb.x, ptb.y + offset_y, colour, scale);
00216
00217 GfxDrawLine(pta.x, pta.y, ptb.x, ptb.y, _colour_gradient[COLOUR_GREY][1], this->scale);
00218 }
00219
00224 void LinkGraphOverlay::DrawStationDots(const DrawPixelInfo *dpi) const
00225 {
00226 for (StationSupplyList::const_iterator i(this->cached_stations.begin()); i != this->cached_stations.end(); ++i) {
00227 const Station *st = Station::GetIfValid(i->first);
00228 if (st == NULL) continue;
00229 Point pt = this->GetStationMiddle(st);
00230 if (!this->IsPointVisible(pt, dpi, 3 * this->scale)) continue;
00231
00232 uint r = this->scale * 2 + this->scale * 2 * min(200, i->second) / 200;
00233
00234 LinkGraphOverlay::DrawVertex(pt.x, pt.y, r,
00235 _colour_gradient[st->owner != OWNER_NONE ?
00236 (Colours)Company::Get(st->owner)->colour : COLOUR_GREY][5],
00237 _colour_gradient[COLOUR_GREY][1]);
00238 }
00239 }
00240
00249 void LinkGraphOverlay::DrawVertex(int x, int y, int size, int colour, int border_colour)
00250 {
00251 size--;
00252 int w1 = size / 2;
00253 int w2 = size / 2 + size % 2;
00254
00255 GfxFillRect(x - w1, y - w1, x + w2, y + w2, colour);
00256
00257 w1++;
00258 w2++;
00259 GfxDrawLine(x - w1, y - w1, x + w2, y - w1, border_colour);
00260 GfxDrawLine(x - w1, y + w2, x + w2, y + w2, border_colour);
00261 GfxDrawLine(x - w1, y - w1, x - w1, y + w2, border_colour);
00262 GfxDrawLine(x + w2, y - w1, x + w2, y + w2, border_colour);
00263 }
00264
00270 Point LinkGraphOverlay::GetStationMiddle(const Station *st) const {
00271 if (this->window->viewport != NULL) {
00272 return GetViewportStationMiddle(this->window->viewport, st);
00273 } else {
00274
00275 return static_cast<const SmallMapWindow *>(this->window)->GetStationMiddle(st);
00276 }
00277 }
00278
00283 void LinkGraphOverlay::SetCargoMask(uint32 cargo_mask)
00284 {
00285 this->cargo_mask = cargo_mask;
00286 this->RebuildCache();
00287 this->window->GetWidget<NWidgetBase>(this->widget_id)->SetDirty(this->window);
00288 }
00289
00294 void LinkGraphOverlay::SetCompanyMask(uint32 company_mask)
00295 {
00296 this->company_mask = company_mask;
00297 this->RebuildCache();
00298 this->window->GetWidget<NWidgetBase>(this->widget_id)->SetDirty(this->window);
00299 }
00300
00302 NWidgetBase *MakeCompanyButtonRowsLinkGraphGUI(int *biggest_index)
00303 {
00304 return MakeCompanyButtonRows(biggest_index, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST, 3, STR_LINKGRAPH_LEGEND_SELECT_COMPANIES);
00305 }
00306
00307 NWidgetBase *MakeSaturationLegendLinkGraphGUI(int *biggest_index)
00308 {
00309 NWidgetVertical *panel = new NWidgetVertical();
00310 for (uint i = 0; i < lengthof(LinkGraphOverlay::LINK_COLOURS); ++i) {
00311 NWidgetBackground * wid = new NWidgetBackground(WWT_PANEL, COLOUR_DARK_GREEN, i + WID_LGL_SATURATION_FIRST);
00312 wid->SetMinimalSize(50, FONT_HEIGHT_SMALL);
00313 wid->SetFill(0, 1);
00314 wid->SetResize(0, 1);
00315 panel->Add(wid);
00316 }
00317 *biggest_index = WID_LGL_SATURATION_LAST;
00318 return panel;
00319 }
00320
00321 NWidgetBase *MakeCargoesLegendLinkGraphGUI(int *biggest_index)
00322 {
00323 static const uint ENTRIES_PER_ROW = CeilDiv(NUM_CARGO, 5);
00324 NWidgetVertical *panel = new NWidgetVertical();
00325 NWidgetHorizontal *row = NULL;
00326 for (uint i = 0; i < NUM_CARGO; ++i) {
00327 if (i % ENTRIES_PER_ROW == 0) {
00328 if (row) panel->Add(row);
00329 row = new NWidgetHorizontal();
00330 }
00331 NWidgetBackground * wid = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, i + WID_LGL_CARGO_FIRST);
00332 wid->SetMinimalSize(25, FONT_HEIGHT_SMALL);
00333 wid->SetFill(0, 1);
00334 wid->SetResize(0, 1);
00335 row->Add(wid);
00336 }
00337 panel->Add(row);
00338 *biggest_index = WID_LGL_CARGO_LAST;
00339 return panel;
00340 }
00341
00342
00343 static const NWidgetPart _nested_linkgraph_legend_widgets[] = {
00344 NWidget(NWID_HORIZONTAL),
00345 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
00346 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_LGL_CAPTION), SetDataTip(STR_LINKGRAPH_LEGEND_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00347 NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
00348 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
00349 EndContainer(),
00350 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
00351 NWidget(NWID_HORIZONTAL),
00352 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_SATURATION),
00353 SetPadding(WD_FRAMERECT_TOP, 0, WD_FRAMERECT_BOTTOM, WD_CAPTIONTEXT_LEFT),
00354 SetMinimalSize(50, 100),
00355 NWidgetFunction(MakeSaturationLegendLinkGraphGUI),
00356 EndContainer(),
00357 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_COMPANIES),
00358 SetPadding(WD_FRAMERECT_TOP, 0, WD_FRAMERECT_BOTTOM, WD_CAPTIONTEXT_LEFT),
00359 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
00360 SetMinimalSize(100, 100),
00361 NWidgetFunction(MakeCompanyButtonRowsLinkGraphGUI),
00362 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_COMPANIES_ALL), SetDataTip(STR_LINKGRAPH_LEGEND_ALL, STR_NULL),
00363 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_COMPANIES_NONE), SetDataTip(STR_LINKGRAPH_LEGEND_NONE, STR_NULL),
00364 EndContainer(),
00365 EndContainer(),
00366 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_CARGOES),
00367 SetPadding(WD_FRAMERECT_TOP, WD_FRAMERECT_RIGHT, WD_FRAMERECT_BOTTOM, WD_CAPTIONTEXT_LEFT),
00368 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
00369 SetMinimalSize(150, 100),
00370 NWidgetFunction(MakeCargoesLegendLinkGraphGUI),
00371 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_CARGOES_ALL), SetDataTip(STR_LINKGRAPH_LEGEND_ALL, STR_NULL),
00372 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_CARGOES_NONE), SetDataTip(STR_LINKGRAPH_LEGEND_NONE, STR_NULL),
00373 EndContainer(),
00374 EndContainer(),
00375 EndContainer(),
00376 EndContainer()
00377 };
00378
00379 assert_compile(WID_LGL_SATURATION_LAST - WID_LGL_SATURATION_FIRST ==
00380 lengthof(LinkGraphOverlay::LINK_COLOURS) - 1);
00381
00382 static const WindowDesc _linkgraph_legend_desc(
00383 WDP_MANUAL, 300, 314,
00384 WC_LINKGRAPH_LEGEND, WC_NONE,
00385 0,
00386 _nested_linkgraph_legend_widgets, lengthof(_nested_linkgraph_legend_widgets)
00387 );
00388
00392 void ShowLinkGraphLegend()
00393 {
00394 AllocateWindowDescFront<LinkGraphLegendWindow>(&_linkgraph_legend_desc, 0);
00395 }
00396
00397 LinkGraphLegendWindow::LinkGraphLegendWindow(const WindowDesc *desc, int window_number)
00398 {
00399 this->InitNested(desc, window_number);
00400 this->InvalidateData(0);
00401 this->SetOverlay(FindWindowById(WC_MAIN_WINDOW, 0)->viewport->overlay);
00402 }
00403
00408 void LinkGraphLegendWindow::SetOverlay(LinkGraphOverlay *overlay) {
00409 this->overlay = overlay;
00410 uint32 companies = this->overlay->GetCompanyMask();
00411 for (uint c = 0; c < MAX_COMPANIES; c++) {
00412 if (!this->IsWidgetDisabled(WID_LGL_COMPANY_FIRST + c)) {
00413 this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST + c, HasBit(companies, c));
00414 }
00415 }
00416 uint32 cargoes = this->overlay->GetCargoMask();
00417 for (uint c = 0; c < NUM_CARGO; c++) {
00418 if (!this->IsWidgetDisabled(WID_LGL_CARGO_FIRST + c)) {
00419 this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST + c, HasBit(cargoes, c));
00420 }
00421 }
00422 }
00423
00424 void LinkGraphLegendWindow::DrawWidget(const Rect &r, int widget) const
00425 {
00426 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(widget);
00427 if (IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) {
00428 if (this->IsWidgetDisabled(widget)) return;
00429 CompanyID cid = (CompanyID)(widget - WID_LGL_COMPANY_FIRST);
00430 Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
00431 DrawCompanyIcon(cid, (r.left + r.right - sprite_size.width) / 2, (r.top + r.bottom - sprite_size.height) / 2);
00432 return;
00433 }
00434 if (IsInsideMM(widget, WID_LGL_SATURATION_FIRST, WID_LGL_SATURATION_LAST + 1)) {
00435 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, LinkGraphOverlay::LINK_COLOURS[widget - WID_LGL_SATURATION_FIRST]);
00436 if (widget == WID_LGL_SATURATION_FIRST) {
00437 DrawString(wid->pos_x, wid->current_x + wid->pos_x, wid->pos_y, STR_LINKGRAPH_LEGEND_UNUSED, TC_FROMSTRING, SA_HOR_CENTER);
00438 } else if (widget == WID_LGL_SATURATION_LAST) {
00439 DrawString(wid->pos_x, wid->current_x + wid->pos_x, wid->pos_y, STR_LINKGRAPH_LEGEND_OVERLOADED, TC_FROMSTRING, SA_HOR_CENTER);
00440 } else if (widget == (WID_LGL_SATURATION_LAST + WID_LGL_SATURATION_FIRST) / 2) {
00441 DrawString(wid->pos_x, wid->current_x + wid->pos_x, wid->pos_y, STR_LINKGRAPH_LEGEND_SATURATED, TC_FROMSTRING, SA_HOR_CENTER);
00442 }
00443 }
00444 if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
00445 if (this->IsWidgetDisabled(widget)) return;
00446 CargoSpec *cargo = CargoSpec::Get(widget - WID_LGL_CARGO_FIRST);
00447 GfxFillRect(r.left + 2, r.top + 2, r.right - 2, r.bottom - 2, cargo->legend_colour);
00448 DrawString(wid->pos_x, wid->current_x + wid->pos_x, wid->pos_y + 2, cargo->abbrev, TC_BLACK, SA_HOR_CENTER);
00449 }
00450 }
00451
00455 void LinkGraphLegendWindow::UpdateOverlayCompanies()
00456 {
00457 uint32 mask = 0;
00458 for (uint c = 0; c < MAX_COMPANIES; c++) {
00459 if (this->IsWidgetDisabled(c + WID_LGL_COMPANY_FIRST)) continue;
00460 if (!this->IsWidgetLowered(c + WID_LGL_COMPANY_FIRST)) continue;
00461 SetBit(mask, c);
00462 }
00463 this->overlay->SetCompanyMask(mask);
00464 }
00465
00469 void LinkGraphLegendWindow::UpdateOverlayCargoes()
00470 {
00471 uint32 mask = 0;
00472 for (uint c = 0; c < NUM_CARGO; c++) {
00473 if (this->IsWidgetDisabled(c + WID_LGL_CARGO_FIRST)) continue;
00474 if (!this->IsWidgetLowered(c + WID_LGL_CARGO_FIRST)) continue;
00475 SetBit(mask, c);
00476 }
00477 this->overlay->SetCargoMask(mask);
00478 }
00479
00480 void LinkGraphLegendWindow::OnClick(Point pt, int widget, int click_count)
00481 {
00482
00483 if (IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) {
00484 if (!this->IsWidgetDisabled(widget)) {
00485 this->ToggleWidgetLoweredState(widget);
00486 this->UpdateOverlayCompanies();
00487 }
00488 } else if (widget == WID_LGL_COMPANIES_ALL || widget == WID_LGL_COMPANIES_NONE) {
00489 for (uint c = 0; c < MAX_COMPANIES; c++) {
00490 if (this->IsWidgetDisabled(c + WID_LGL_COMPANY_FIRST)) continue;
00491 this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST + c, widget == WID_LGL_COMPANIES_ALL);
00492 }
00493 this->UpdateOverlayCompanies();
00494 this->SetDirty();
00495 } else if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
00496 if (!this->IsWidgetDisabled(widget)) {
00497 this->ToggleWidgetLoweredState(widget);
00498 this->UpdateOverlayCargoes();
00499 }
00500 } else if (widget == WID_LGL_CARGOES_ALL || widget == WID_LGL_CARGOES_NONE) {
00501 for (uint c = 0; c < NUM_CARGO; c++) {
00502 if (this->IsWidgetDisabled(c + WID_LGL_CARGO_FIRST)) continue;
00503 this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST + c, widget == WID_LGL_CARGOES_ALL);
00504 }
00505 this->UpdateOverlayCargoes();
00506 }
00507 this->SetDirty();
00508 }
00509
00515 void LinkGraphLegendWindow::OnInvalidateData(int data, bool gui_scope)
00516 {
00517
00518 for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
00519 this->SetWidgetDisabledState(i + WID_LGL_COMPANY_FIRST, !Company::IsValidID(i));
00520 }
00521 for (CargoID i = 0; i < NUM_CARGO; i++) {
00522 this->SetWidgetDisabledState(i + WID_LGL_CARGO_FIRST, !CargoSpec::Get(i)->IsValid());
00523 }
00524 }