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 if (!LinkGraph::IsValidID(sta->goods[c].link_graph)) continue;
00075 const LinkGraph &lg = *LinkGraph::Get(sta->goods[c].link_graph);
00076
00077 ConstNode from_node = lg[sta->goods[c].node];
00078 supply += lg.Monthly(from_node.Supply());
00079 for (ConstEdgeIterator i = from_node.Begin(); i != from_node.End(); ++i) {
00080 StationID to = lg[i->first].Station();
00081 assert(from != to);
00082 if (!Station::IsValidID(to) || seen_links.find(to) != seen_links.end()) {
00083 continue;
00084 }
00085 const Station *stb = Station::Get(to);
00086 assert(sta != stb);
00087 if (stb->owner != OWNER_NONE && !HasBit(this->company_mask, stb->owner)) continue;
00088 if (stb->rect.IsEmpty()) continue;
00089
00090 if (!this->IsLinkVisible(pta, this->GetStationMiddle(stb), &dpi)) continue;
00091
00092 this->AddLinks(sta, stb);
00093 seen_links[to];
00094 }
00095 }
00096 if (this->IsPointVisible(pta, &dpi)) {
00097 this->cached_stations.push_back(std::make_pair(from, supply));
00098 }
00099 }
00100 }
00101
00109 inline bool LinkGraphOverlay::IsPointVisible(Point pt, const DrawPixelInfo *dpi, int padding) const
00110 {
00111 return pt.x > dpi->left - padding && pt.y > dpi->top - padding &&
00112 pt.x < dpi->left + dpi->width + padding &&
00113 pt.y < dpi->top + dpi->height + padding;
00114 }
00115
00124 inline bool LinkGraphOverlay::IsLinkVisible(Point pta, Point ptb, const DrawPixelInfo *dpi, int padding) const
00125 {
00126 return !((pta.x < dpi->left - padding && ptb.x < dpi->left - padding) ||
00127 (pta.y < dpi->top - padding && ptb.y < dpi->top - padding) ||
00128 (pta.x > dpi->left + dpi->width + padding &&
00129 ptb.x > dpi->left + dpi->width + padding) ||
00130 (pta.y > dpi->top + dpi->height + padding &&
00131 ptb.y > dpi->top + dpi->height + padding));
00132 }
00133
00139 void LinkGraphOverlay::AddLinks(const Station *from, const Station *to)
00140 {
00141 CargoID c;
00142 FOR_EACH_SET_CARGO_ID(c, this->cargo_mask) {
00143 if (!CargoSpec::Get(c)->IsValid()) continue;
00144 const GoodsEntry &ge = from->goods[c];
00145 if (!LinkGraph::IsValidID(ge.link_graph) ||
00146 ge.link_graph != to->goods[c].link_graph) {
00147 continue;
00148 }
00149 const LinkGraph &lg = *LinkGraph::Get(ge.link_graph);
00150 ConstEdge edge = lg[ge.node][to->goods[c].node];
00151 if (edge.Capacity() > 0) {
00152 this->AddStats(lg.Monthly(edge.Capacity()), lg.Monthly(edge.Usage()),
00153 ge.GetSumFlowVia(to->index), this->cached_links[from->index][to->index]);
00154 }
00155 }
00156 }
00157
00164 void LinkGraphOverlay::AddStats(uint new_cap, uint new_usg, uint new_plan, LinkProperties &cargo)
00165 {
00166
00167 if (cargo.capacity == 0 ||
00168 max(cargo.usage, cargo.planned) * 32 / (cargo.capacity + 1) < max(new_usg, new_plan) * 32 / (new_cap + 1)) {
00169 cargo.capacity = new_cap;
00170 cargo.usage = new_usg;
00171 cargo.planned = new_plan;
00172 }
00173 }
00174
00179 void LinkGraphOverlay::Draw(const DrawPixelInfo *dpi) const
00180 {
00181 this->DrawLinks(dpi);
00182 this->DrawStationDots(dpi);
00183 }
00184
00189 void LinkGraphOverlay::DrawLinks(const DrawPixelInfo *dpi) const
00190 {
00191 for (LinkMap::const_iterator i(this->cached_links.begin()); i != this->cached_links.end(); ++i) {
00192 if (!Station::IsValidID(i->first)) continue;
00193 Point pta = this->GetStationMiddle(Station::Get(i->first));
00194 for (StationLinkMap::const_iterator j(i->second.begin()); j != i->second.end(); ++j) {
00195 if (!Station::IsValidID(j->first)) continue;
00196 Point ptb = this->GetStationMiddle(Station::Get(j->first));
00197 if (!this->IsLinkVisible(pta, ptb, dpi, this->scale + 2)) continue;
00198 this->DrawContent(pta, ptb, j->second);
00199 }
00200 }
00201 }
00202
00209 void LinkGraphOverlay::DrawContent(Point pta, Point ptb, const LinkProperties &cargo) const
00210 {
00211 int offset_y = (pta.x < ptb.x ? 1 : -1) * this->scale;
00212 int offset_x = (pta.y > ptb.y ? 1 : -1) * this->scale;
00213
00214 uint usage_or_plan = min(cargo.capacity * 2 + 1, max(cargo.usage, cargo.planned));
00215 int colour = LinkGraphOverlay::LINK_COLOURS[usage_or_plan * lengthof(LinkGraphOverlay::LINK_COLOURS) / (cargo.capacity * 2 + 2)];
00216
00217 GfxDrawLine(pta.x + offset_x, pta.y, ptb.x + offset_x, ptb.y, colour, scale);
00218 GfxDrawLine(pta.x, pta.y + offset_y, ptb.x, ptb.y + offset_y, colour, scale);
00219
00220 GfxDrawLine(pta.x, pta.y, ptb.x, ptb.y, _colour_gradient[COLOUR_GREY][1], this->scale);
00221 }
00222
00227 void LinkGraphOverlay::DrawStationDots(const DrawPixelInfo *dpi) const
00228 {
00229 for (StationSupplyList::const_iterator i(this->cached_stations.begin()); i != this->cached_stations.end(); ++i) {
00230 const Station *st = Station::GetIfValid(i->first);
00231 if (st == NULL) continue;
00232 Point pt = this->GetStationMiddle(st);
00233 if (!this->IsPointVisible(pt, dpi, 3 * this->scale)) continue;
00234
00235 uint r = this->scale * 2 + this->scale * 2 * min(200, i->second) / 200;
00236
00237 LinkGraphOverlay::DrawVertex(pt.x, pt.y, r,
00238 _colour_gradient[st->owner != OWNER_NONE ?
00239 (Colours)Company::Get(st->owner)->colour : COLOUR_GREY][5],
00240 _colour_gradient[COLOUR_GREY][1]);
00241 }
00242 }
00243
00252 void LinkGraphOverlay::DrawVertex(int x, int y, int size, int colour, int border_colour)
00253 {
00254 size--;
00255 int w1 = size / 2;
00256 int w2 = size / 2 + size % 2;
00257
00258 GfxFillRect(x - w1, y - w1, x + w2, y + w2, colour);
00259
00260 w1++;
00261 w2++;
00262 GfxDrawLine(x - w1, y - w1, x + w2, y - w1, border_colour);
00263 GfxDrawLine(x - w1, y + w2, x + w2, y + w2, border_colour);
00264 GfxDrawLine(x - w1, y - w1, x - w1, y + w2, border_colour);
00265 GfxDrawLine(x + w2, y - w1, x + w2, y + w2, border_colour);
00266 }
00267
00273 Point LinkGraphOverlay::GetStationMiddle(const Station *st) const {
00274 if (this->window->viewport != NULL) {
00275 return GetViewportStationMiddle(this->window->viewport, st);
00276 } else {
00277
00278 return static_cast<const SmallMapWindow *>(this->window)->GetStationMiddle(st);
00279 }
00280 }
00281
00286 void LinkGraphOverlay::SetCargoMask(uint32 cargo_mask)
00287 {
00288 this->cargo_mask = cargo_mask;
00289 this->RebuildCache();
00290 this->window->GetWidget<NWidgetBase>(this->widget_id)->SetDirty(this->window);
00291 }
00292
00297 void LinkGraphOverlay::SetCompanyMask(uint32 company_mask)
00298 {
00299 this->company_mask = company_mask;
00300 this->RebuildCache();
00301 this->window->GetWidget<NWidgetBase>(this->widget_id)->SetDirty(this->window);
00302 }
00303
00305 NWidgetBase *MakeCompanyButtonRowsLinkGraphGUI(int *biggest_index)
00306 {
00307 return MakeCompanyButtonRows(biggest_index, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST, 3, STR_LINKGRAPH_LEGEND_SELECT_COMPANIES);
00308 }
00309
00310 NWidgetBase *MakeSaturationLegendLinkGraphGUI(int *biggest_index)
00311 {
00312 NWidgetVertical *panel = new NWidgetVertical();
00313 for (uint i = 0; i < lengthof(LinkGraphOverlay::LINK_COLOURS); ++i) {
00314 NWidgetBackground * wid = new NWidgetBackground(WWT_PANEL, COLOUR_DARK_GREEN, i + WID_LGL_SATURATION_FIRST);
00315 wid->SetMinimalSize(50, FONT_HEIGHT_SMALL);
00316 wid->SetFill(0, 1);
00317 wid->SetResize(0, 1);
00318 panel->Add(wid);
00319 }
00320 *biggest_index = WID_LGL_SATURATION_LAST;
00321 return panel;
00322 }
00323
00324 NWidgetBase *MakeCargoesLegendLinkGraphGUI(int *biggest_index)
00325 {
00326 static const uint ENTRIES_PER_ROW = CeilDiv(NUM_CARGO, 5);
00327 NWidgetVertical *panel = new NWidgetVertical();
00328 NWidgetHorizontal *row = NULL;
00329 for (uint i = 0; i < NUM_CARGO; ++i) {
00330 if (i % ENTRIES_PER_ROW == 0) {
00331 if (row) panel->Add(row);
00332 row = new NWidgetHorizontal();
00333 }
00334 NWidgetBackground * wid = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, i + WID_LGL_CARGO_FIRST);
00335 wid->SetMinimalSize(25, FONT_HEIGHT_SMALL);
00336 wid->SetFill(0, 1);
00337 wid->SetResize(0, 1);
00338 row->Add(wid);
00339 }
00340 panel->Add(row);
00341 *biggest_index = WID_LGL_CARGO_LAST;
00342 return panel;
00343 }
00344
00345
00346 static const NWidgetPart _nested_linkgraph_legend_widgets[] = {
00347 NWidget(NWID_HORIZONTAL),
00348 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
00349 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_LGL_CAPTION), SetDataTip(STR_LINKGRAPH_LEGEND_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00350 NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
00351 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
00352 EndContainer(),
00353 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
00354 NWidget(NWID_HORIZONTAL),
00355 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_SATURATION),
00356 SetPadding(WD_FRAMERECT_TOP, 0, WD_FRAMERECT_BOTTOM, WD_CAPTIONTEXT_LEFT),
00357 SetMinimalSize(50, 100),
00358 NWidgetFunction(MakeSaturationLegendLinkGraphGUI),
00359 EndContainer(),
00360 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_COMPANIES),
00361 SetPadding(WD_FRAMERECT_TOP, 0, WD_FRAMERECT_BOTTOM, WD_CAPTIONTEXT_LEFT),
00362 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
00363 SetMinimalSize(100, 100),
00364 NWidgetFunction(MakeCompanyButtonRowsLinkGraphGUI),
00365 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_COMPANIES_ALL), SetDataTip(STR_LINKGRAPH_LEGEND_ALL, STR_NULL),
00366 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_COMPANIES_NONE), SetDataTip(STR_LINKGRAPH_LEGEND_NONE, STR_NULL),
00367 EndContainer(),
00368 EndContainer(),
00369 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_CARGOES),
00370 SetPadding(WD_FRAMERECT_TOP, WD_FRAMERECT_RIGHT, WD_FRAMERECT_BOTTOM, WD_CAPTIONTEXT_LEFT),
00371 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
00372 SetMinimalSize(150, 100),
00373 NWidgetFunction(MakeCargoesLegendLinkGraphGUI),
00374 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_CARGOES_ALL), SetDataTip(STR_LINKGRAPH_LEGEND_ALL, STR_NULL),
00375 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_CARGOES_NONE), SetDataTip(STR_LINKGRAPH_LEGEND_NONE, STR_NULL),
00376 EndContainer(),
00377 EndContainer(),
00378 EndContainer(),
00379 EndContainer()
00380 };
00381
00382 assert_compile(WID_LGL_SATURATION_LAST - WID_LGL_SATURATION_FIRST ==
00383 lengthof(LinkGraphOverlay::LINK_COLOURS) - 1);
00384
00385 static const WindowDesc _linkgraph_legend_desc(
00386 WDP_MANUAL, 300, 314,
00387 WC_LINKGRAPH_LEGEND, WC_NONE,
00388 0,
00389 _nested_linkgraph_legend_widgets, lengthof(_nested_linkgraph_legend_widgets)
00390 );
00391
00395 void ShowLinkGraphLegend()
00396 {
00397 AllocateWindowDescFront<LinkGraphLegendWindow>(&_linkgraph_legend_desc, 0);
00398 }
00399
00400 LinkGraphLegendWindow::LinkGraphLegendWindow(const WindowDesc *desc, int window_number)
00401 {
00402 this->InitNested(desc, window_number);
00403 this->InvalidateData(0);
00404 this->SetOverlay(FindWindowById(WC_MAIN_WINDOW, 0)->viewport->overlay);
00405 }
00406
00411 void LinkGraphLegendWindow::SetOverlay(LinkGraphOverlay *overlay) {
00412 this->overlay = overlay;
00413 uint32 companies = this->overlay->GetCompanyMask();
00414 for (uint c = 0; c < MAX_COMPANIES; c++) {
00415 if (!this->IsWidgetDisabled(WID_LGL_COMPANY_FIRST + c)) {
00416 this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST + c, HasBit(companies, c));
00417 }
00418 }
00419 uint32 cargoes = this->overlay->GetCargoMask();
00420 for (uint c = 0; c < NUM_CARGO; c++) {
00421 if (!this->IsWidgetDisabled(WID_LGL_CARGO_FIRST + c)) {
00422 this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST + c, HasBit(cargoes, c));
00423 }
00424 }
00425 }
00426
00427 void LinkGraphLegendWindow::DrawWidget(const Rect &r, int widget) const
00428 {
00429 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(widget);
00430 if (IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) {
00431 if (this->IsWidgetDisabled(widget)) return;
00432 CompanyID cid = (CompanyID)(widget - WID_LGL_COMPANY_FIRST);
00433 Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
00434 DrawCompanyIcon(cid, (r.left + r.right - sprite_size.width) / 2, (r.top + r.bottom - sprite_size.height) / 2);
00435 return;
00436 }
00437 if (IsInsideMM(widget, WID_LGL_SATURATION_FIRST, WID_LGL_SATURATION_LAST + 1)) {
00438 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, LinkGraphOverlay::LINK_COLOURS[widget - WID_LGL_SATURATION_FIRST]);
00439 if (widget == WID_LGL_SATURATION_FIRST) {
00440 DrawString(wid->pos_x, wid->current_x + wid->pos_x, wid->pos_y, STR_LINKGRAPH_LEGEND_UNUSED, TC_FROMSTRING, SA_HOR_CENTER);
00441 } else if (widget == WID_LGL_SATURATION_LAST) {
00442 DrawString(wid->pos_x, wid->current_x + wid->pos_x, wid->pos_y, STR_LINKGRAPH_LEGEND_OVERLOADED, TC_FROMSTRING, SA_HOR_CENTER);
00443 } else if (widget == (WID_LGL_SATURATION_LAST + WID_LGL_SATURATION_FIRST) / 2) {
00444 DrawString(wid->pos_x, wid->current_x + wid->pos_x, wid->pos_y, STR_LINKGRAPH_LEGEND_SATURATED, TC_FROMSTRING, SA_HOR_CENTER);
00445 }
00446 }
00447 if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
00448 if (this->IsWidgetDisabled(widget)) return;
00449 CargoSpec *cargo = CargoSpec::Get(widget - WID_LGL_CARGO_FIRST);
00450 GfxFillRect(r.left + 2, r.top + 2, r.right - 2, r.bottom - 2, cargo->legend_colour);
00451 DrawString(wid->pos_x, wid->current_x + wid->pos_x, wid->pos_y + 2, cargo->abbrev, TC_BLACK, SA_HOR_CENTER);
00452 }
00453 }
00454
00458 void LinkGraphLegendWindow::UpdateOverlayCompanies()
00459 {
00460 uint32 mask = 0;
00461 for (uint c = 0; c < MAX_COMPANIES; c++) {
00462 if (this->IsWidgetDisabled(c + WID_LGL_COMPANY_FIRST)) continue;
00463 if (!this->IsWidgetLowered(c + WID_LGL_COMPANY_FIRST)) continue;
00464 SetBit(mask, c);
00465 }
00466 this->overlay->SetCompanyMask(mask);
00467 }
00468
00472 void LinkGraphLegendWindow::UpdateOverlayCargoes()
00473 {
00474 uint32 mask = 0;
00475 for (uint c = 0; c < NUM_CARGO; c++) {
00476 if (this->IsWidgetDisabled(c + WID_LGL_CARGO_FIRST)) continue;
00477 if (!this->IsWidgetLowered(c + WID_LGL_CARGO_FIRST)) continue;
00478 SetBit(mask, c);
00479 }
00480 this->overlay->SetCargoMask(mask);
00481 }
00482
00483 void LinkGraphLegendWindow::OnClick(Point pt, int widget, int click_count)
00484 {
00485
00486 if (IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) {
00487 if (!this->IsWidgetDisabled(widget)) {
00488 this->ToggleWidgetLoweredState(widget);
00489 this->UpdateOverlayCompanies();
00490 }
00491 } else if (widget == WID_LGL_COMPANIES_ALL || widget == WID_LGL_COMPANIES_NONE) {
00492 for (uint c = 0; c < MAX_COMPANIES; c++) {
00493 if (this->IsWidgetDisabled(c + WID_LGL_COMPANY_FIRST)) continue;
00494 this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST + c, widget == WID_LGL_COMPANIES_ALL);
00495 }
00496 this->UpdateOverlayCompanies();
00497 this->SetDirty();
00498 } else if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
00499 if (!this->IsWidgetDisabled(widget)) {
00500 this->ToggleWidgetLoweredState(widget);
00501 this->UpdateOverlayCargoes();
00502 }
00503 } else if (widget == WID_LGL_CARGOES_ALL || widget == WID_LGL_CARGOES_NONE) {
00504 for (uint c = 0; c < NUM_CARGO; c++) {
00505 if (this->IsWidgetDisabled(c + WID_LGL_CARGO_FIRST)) continue;
00506 this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST + c, widget == WID_LGL_CARGOES_ALL);
00507 }
00508 this->UpdateOverlayCargoes();
00509 }
00510 this->SetDirty();
00511 }
00512
00518 void LinkGraphLegendWindow::OnInvalidateData(int data, bool gui_scope)
00519 {
00520
00521 for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
00522 this->SetWidgetDisabledState(i + WID_LGL_COMPANY_FIRST, !Company::IsValidID(i));
00523 }
00524 for (CargoID i = 0; i < NUM_CARGO; i++) {
00525 this->SetWidgetDisabledState(i + WID_LGL_CARGO_FIRST, !CargoSpec::Get(i)->IsValid());
00526 }
00527 }