OpenTTD Source 20250205-master-gfd85ab1e2c
station_gui.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10#include "stdafx.h"
11#include "debug.h"
12#include "gui.h"
13#include "textbuf_gui.h"
14#include "company_func.h"
15#include "command_func.h"
16#include "vehicle_gui.h"
17#include "cargotype.h"
18#include "station_gui.h"
19#include "strings_func.h"
20#include "string_func.h"
21#include "window_func.h"
22#include "viewport_func.h"
23#include "dropdown_type.h"
25#include "dropdown_func.h"
26#include "station_base.h"
27#include "waypoint_base.h"
28#include "tilehighlight_func.h"
29#include "company_base.h"
30#include "sortlist_type.h"
32#include "vehiclelist.h"
33#include "town.h"
34#include "linkgraph/linkgraph.h"
35#include "zoom_func.h"
36#include "station_cmd.h"
37
39
40#include "table/strings.h"
41
42#include "safeguards.h"
43
45{
46 using StationType = Station;
47
48 static bool IsValidID(StationID id) { return Station::IsValidID(id); }
49 static bool IsValidBaseStation(const BaseStation *st) { return Station::IsExpected(st); }
50 static bool IsAcceptableWaypointTile(TileIndex) { return false; }
51 static constexpr bool IsWaypoint() { return false; }
52};
53
54template <bool ROAD, TileType TILE_TYPE>
56{
57 using StationType = Waypoint;
58
59 static bool IsValidID(StationID id) { return Waypoint::IsValidID(id) && HasBit(Waypoint::Get(id)->waypoint_flags, WPF_ROAD) == ROAD; }
60 static bool IsValidBaseStation(const BaseStation *st) { return Waypoint::IsExpected(st) && HasBit(Waypoint::From(st)->waypoint_flags, WPF_ROAD) == ROAD; }
61 static bool IsAcceptableWaypointTile(TileIndex tile) { return IsTileType(tile, TILE_TYPE); }
62 static constexpr bool IsWaypoint() { return true; }
63};
66
75int DrawStationCoverageAreaText(const Rect &r, StationCoverageType sct, int rad, bool supplies)
76{
77 TileIndex tile = TileVirtXY(_thd.pos.x, _thd.pos.y);
78 CargoTypes cargo_mask = 0;
79 if (_thd.drawstyle == HT_RECT && tile < Map::Size()) {
80 CargoArray cargoes;
81 if (supplies) {
82 cargoes = GetProductionAroundTiles(tile, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE, rad);
83 } else {
84 cargoes = GetAcceptanceAroundTiles(tile, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE, rad);
85 }
86
87 /* Convert cargo counts to a set of cargo bits, and draw the result. */
88 for (CargoType i = 0; i < NUM_CARGO; i++) {
89 switch (sct) {
90 case SCT_PASSENGERS_ONLY: if (!IsCargoInClass(i, CC_PASSENGERS)) continue; break;
91 case SCT_NON_PASSENGERS_ONLY: if (IsCargoInClass(i, CC_PASSENGERS)) continue; break;
92 case SCT_ALL: break;
93 default: NOT_REACHED();
94 }
95 if (cargoes[i] >= (supplies ? 1U : 8U)) SetBit(cargo_mask, i);
96 }
97 }
98 SetDParam(0, cargo_mask);
99 return DrawStringMultiLine(r, supplies ? STR_STATION_BUILD_SUPPLIES_CARGO : STR_STATION_BUILD_ACCEPTS_CARGO);
100}
101
106template <typename T>
108{
109 /* With distant join we don't know which station will be selected, so don't show any */
110 if (_ctrl_pressed) {
111 SetViewportCatchmentSpecializedStation<typename T::StationType>(nullptr, true);
112 return;
113 }
114
115 /* Tile area for TileHighlightData */
116 TileArea location(TileVirtXY(_thd.pos.x, _thd.pos.y), _thd.size.x / TILE_SIZE - 1, _thd.size.y / TILE_SIZE - 1);
117
118 /* If the current tile is already a station, then it must be the nearest station. */
119 if (IsTileType(location.tile, MP_STATION) && GetTileOwner(location.tile) == _local_company) {
120 typename T::StationType *st = T::StationType::GetByTile(location.tile);
121 if (st != nullptr && T::IsValidBaseStation(st)) {
122 SetViewportCatchmentSpecializedStation<typename T::StationType>(st, true);
123 return;
124 }
125 }
126
127 /* Extended area by one tile */
128 uint x = TileX(location.tile);
129 uint y = TileY(location.tile);
130
131 /* Waypoints can only be built on existing rail/road tiles, so don't extend area if not highlighting a rail tile. */
132 int max_c = T::IsWaypoint() && !T::IsAcceptableWaypointTile(location.tile) ? 0 : 1;
133 TileArea ta(TileXY(std::max<int>(0, x - max_c), std::max<int>(0, y - max_c)), TileXY(std::min<int>(Map::MaxX(), x + location.w + max_c), std::min<int>(Map::MaxY(), y + location.h + max_c)));
134
135 typename T::StationType *adjacent = nullptr;
136
137 /* Direct loop instead of ForAllStationsAroundTiles as we are not interested in catchment area */
138 for (TileIndex tile : ta) {
139 if (IsTileType(tile, MP_STATION) && GetTileOwner(tile) == _local_company) {
140 typename T::StationType *st = T::StationType::GetByTile(tile);
141 if (st == nullptr || !T::IsValidBaseStation(st)) continue;
142 if (adjacent != nullptr && st != adjacent) {
143 /* Multiple nearby, distant join is required. */
144 adjacent = nullptr;
145 break;
146 }
147 adjacent = st;
148 }
149 }
150 SetViewportCatchmentSpecializedStation<typename T::StationType>(adjacent, true);
151}
152
159{
160 /* Test if ctrl state changed */
161 static bool _last_ctrl_pressed;
162 if (_ctrl_pressed != _last_ctrl_pressed) {
163 _thd.dirty = 0xff;
164 _last_ctrl_pressed = _ctrl_pressed;
165 }
166
167 if (_thd.dirty & 1) {
168 _thd.dirty &= ~1;
169 w->SetDirty();
170
172 FindStationsAroundSelection<StationTypeFilter>();
173 }
174 }
175}
176
177template <typename T>
178void CheckRedrawWaypointCoverage()
179{
180 /* Test if ctrl state changed */
181 static bool _last_ctrl_pressed;
182 if (_ctrl_pressed != _last_ctrl_pressed) {
183 _thd.dirty = 0xff;
184 _last_ctrl_pressed = _ctrl_pressed;
185 }
186
187 if (_thd.dirty & 1) {
188 _thd.dirty &= ~1;
189
190 if (_thd.drawstyle == HT_RECT) {
191 FindStationsAroundSelection<T>();
192 }
193 }
194}
195
196void CheckRedrawRailWaypointCoverage(const Window *)
197{
198 CheckRedrawWaypointCoverage<RailWaypointTypeFilter>();
199}
200
201void CheckRedrawRoadWaypointCoverage(const Window *)
202{
203 CheckRedrawWaypointCoverage<RoadWaypointTypeFilter>();
204}
205
218static void StationsWndShowStationRating(int left, int right, int y, CargoType type, uint amount, uint8_t rating)
219{
220 static const uint units_full = 576;
221 static const uint rating_full = 224;
222
223 const CargoSpec *cs = CargoSpec::Get(type);
224 if (!cs->IsValid()) return;
225
226 int padding = ScaleGUITrad(1);
227 int width = right - left;
228 int colour = cs->rating_colour;
229 TextColour tc = GetContrastColour(colour);
230 uint w = std::min(amount + 5, units_full) * width / units_full;
231
232 int height = GetCharacterHeight(FS_SMALL) + padding - 1;
233
234 if (amount > 30) {
235 /* Draw total cargo (limited) on station */
236 GfxFillRect(left, y, left + w - 1, y + height, colour);
237 } else {
238 /* Draw a (scaled) one pixel-wide bar of additional cargo meter, useful
239 * for stations with only a small amount (<=30) */
240 uint rest = ScaleGUITrad(amount) / 5;
241 if (rest != 0) {
242 GfxFillRect(left, y + height - rest, left + padding - 1, y + height, colour);
243 }
244 }
245
246 DrawString(left + padding, right, y, cs->abbrev, tc, SA_CENTER, false, FS_SMALL);
247
248 /* Draw green/red ratings bar (fits under the waiting bar) */
249 y += height + padding + 1;
250 GfxFillRect(left + padding, y, right - padding - 1, y + padding - 1, PC_RED);
251 w = std::min<uint>(rating, rating_full) * (width - padding - padding) / rating_full;
252 if (w != 0) GfxFillRect(left + padding, y, left + w - 1, y + padding - 1, PC_GREEN);
253}
254
256
261{
262protected:
263 /* Runtime saved values */
264 struct FilterState {
265 Listing last_sorting;
268 CargoTypes cargoes;
269 };
270
271 static inline FilterState initial_state = {
272 {false, 0},
274 true,
275 ALL_CARGOTYPES,
276 };
277
278 /* Constants for sorting stations */
279 static inline const StringID sorter_names[] = {
286 };
287 static const std::initializer_list<GUIStationList::SortFunction * const> sorter_funcs;
288
289 FilterState filter;
290 GUIStationList stations{filter.cargoes};
291 Scrollbar *vscroll;
292 uint rating_width;
293 bool filter_expanded;
294 std::array<uint16_t, NUM_CARGO> stations_per_cargo_type;
296
303 {
304 if (!this->stations.NeedRebuild()) return;
305
306 Debug(misc, 3, "Building station list for company {}", owner);
307
308 this->stations.clear();
309 this->stations_per_cargo_type.fill(0);
310 this->stations_per_cargo_type_no_rating = 0;
311
312 for (const Station *st : Station::Iterate()) {
313 if ((this->filter.facilities & st->facilities) != 0) { // only stations with selected facilities
314 if (st->owner == owner || (st->owner == OWNER_NONE && HasStationInUse(st->index, true, owner))) {
315 bool has_rating = false;
316 /* Add to the station/cargo counts. */
317 for (CargoType j = 0; j < NUM_CARGO; j++) {
318 if (st->goods[j].HasRating()) this->stations_per_cargo_type[j]++;
319 }
320 for (CargoType j = 0; j < NUM_CARGO; j++) {
321 if (st->goods[j].HasRating()) {
322 has_rating = true;
323 if (HasBit(this->filter.cargoes, j)) {
324 this->stations.push_back(st);
325 break;
326 }
327 }
328 }
329 /* Stations with no cargo rating. */
330 if (!has_rating) {
331 if (this->filter.include_no_rating) this->stations.push_back(st);
332 this->stations_per_cargo_type_no_rating++;
333 }
334 }
335 }
336 }
337
338 this->stations.RebuildDone();
339
340 this->vscroll->SetCount(this->stations.size()); // Update the scrollbar
341 }
342
344 static bool StationNameSorter(const Station * const &a, const Station * const &b, const CargoTypes &)
345 {
346 int r = StrNaturalCompare(a->GetCachedName(), b->GetCachedName()); // Sort by name (natural sorting).
347 if (r == 0) return a->index < b->index;
348 return r < 0;
349 }
350
352 static bool StationTypeSorter(const Station * const &a, const Station * const &b, const CargoTypes &)
353 {
354 return a->facilities < b->facilities;
355 }
356
358 static bool StationWaitingTotalSorter(const Station * const &a, const Station * const &b, const CargoTypes &cargo_filter)
359 {
360 int diff = 0;
361
363 diff += (a->goods[j].HasData() ? a->goods[j].GetData().cargo.TotalCount() : 0) - (b->goods[j].HasData() ? b->goods[j].GetData().cargo.TotalCount() : 0);
364 }
365
366 return diff < 0;
367 }
368
370 static bool StationWaitingAvailableSorter(const Station * const &a, const Station * const &b, const CargoTypes &cargo_filter)
371 {
372 int diff = 0;
373
375 diff += (a->goods[j].HasData() ? a->goods[j].GetData().cargo.AvailableCount() : 0) - (b->goods[j].HasData() ? b->goods[j].GetData().cargo.AvailableCount() : 0);
376 }
377
378 return diff < 0;
379 }
380
382 static bool StationRatingMaxSorter(const Station * const &a, const Station * const &b, const CargoTypes &cargo_filter)
383 {
384 uint8_t maxr1 = 0;
385 uint8_t maxr2 = 0;
386
388 if (a->goods[j].HasRating()) maxr1 = std::max(maxr1, a->goods[j].rating);
389 if (b->goods[j].HasRating()) maxr2 = std::max(maxr2, b->goods[j].rating);
390 }
391
392 return maxr1 < maxr2;
393 }
394
396 static bool StationRatingMinSorter(const Station * const &a, const Station * const &b, const CargoTypes &cargo_filter)
397 {
398 uint8_t minr1 = 255;
399 uint8_t minr2 = 255;
400
402 if (a->goods[j].HasRating()) minr1 = std::min(minr1, a->goods[j].rating);
403 if (b->goods[j].HasRating()) minr2 = std::min(minr2, b->goods[j].rating);
404 }
405
406 return minr1 > minr2;
407 }
408
411 {
412 if (!this->stations.Sort()) return;
413
414 /* Set the modified widget dirty */
416 }
417
418public:
420 {
421 /* Load initial filter state. */
422 this->filter = CompanyStationsWindow::initial_state;
423 if (this->filter.cargoes == ALL_CARGOTYPES) this->filter.cargoes = _cargo_mask;
424
425 this->stations.SetListing(this->filter.last_sorting);
426 this->stations.SetSortFuncs(CompanyStationsWindow::sorter_funcs);
427 this->stations.ForceRebuild();
428 this->stations.NeedResort();
429 this->SortStationsList();
430
431 this->CreateNestedTree();
432 this->vscroll = this->GetScrollbar(WID_STL_SCROLLBAR);
433 this->FinishInitNested(window_number);
434 this->owner = this->window_number;
435
436 if (this->filter.cargoes == ALL_CARGOTYPES) this->filter.cargoes = _cargo_mask;
437
438 for (uint i = 0; i < 5; i++) {
439 if (HasBit(this->filter.facilities, i)) this->LowerWidget(i + WID_STL_TRAIN);
440 }
441
442 this->GetWidget<NWidgetCore>(WID_STL_SORTDROPBTN)->SetString(CompanyStationsWindow::sorter_names[this->stations.SortType()]);
443 }
444
446 {
447 /* Save filter state. */
448 this->filter.last_sorting = this->stations.GetListing();
449 CompanyStationsWindow::initial_state = this->filter;
450 }
451
453 {
454 switch (widget) {
455 case WID_STL_SORTBY: {
457 d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
458 d.height += padding.height;
459 size = maxdim(size, d);
460 break;
461 }
462
463 case WID_STL_SORTDROPBTN: {
464 Dimension d = GetStringListBoundingBox(CompanyStationsWindow::sorter_names);
465 d.width += padding.width;
466 d.height += padding.height;
467 size = maxdim(size, d);
468 break;
469 }
470
471 case WID_STL_LIST:
473 size.height = padding.height + 5 * resize.height;
474
475 /* Determine appropriate width for mini station rating graph */
476 this->rating_width = 0;
477 for (const CargoSpec *cs : _sorted_standard_cargo_specs) {
478 this->rating_width = std::max(this->rating_width, GetStringBoundingBox(cs->abbrev, FS_SMALL).width);
479 }
480 /* Approximately match original 16 pixel wide rating bars by multiplying string width by 1.6 */
481 this->rating_width = this->rating_width * 16 / 10;
482 break;
483 }
484 }
485
486 void OnPaint() override
487 {
488 this->BuildStationsList(this->window_number);
489 this->SortStationsList();
490
491 this->DrawWidgets();
492 }
493
494 void DrawWidget(const Rect &r, WidgetID widget) const override
495 {
496 switch (widget) {
497 case WID_STL_SORTBY:
498 /* draw arrow pointing up/down for ascending/descending sorting */
500 break;
501
502 case WID_STL_LIST: {
503 bool rtl = _current_text_dir == TD_RTL;
504 Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
505 uint line_height = this->GetWidget<NWidgetBase>(widget)->resize_y;
506 /* Spacing between station name and first rating graph. */
508 /* Spacing between additional rating graphs. */
510
511 auto [first, last] = this->vscroll->GetVisibleRangeIterators(this->stations);
512 for (auto it = first; it != last; ++it) {
513 const Station *st = *it;
514 assert(st->xy != INVALID_TILE);
515
516 /* Do not do the complex check HasStationInUse here, it may be even false
517 * when the order had been removed and the station list hasn't been removed yet */
518 assert(st->owner == owner || st->owner == OWNER_NONE);
519
520 SetDParam(0, st->index);
521 SetDParam(1, st->facilities);
522 int x = DrawString(tr.left, tr.right, tr.top + (line_height - GetCharacterHeight(FS_NORMAL)) / 2, STR_STATION_LIST_STATION);
523 x += rtl ? -text_spacing : text_spacing;
524
525 /* show cargo waiting and station ratings */
526 for (const CargoSpec *cs : _sorted_standard_cargo_specs) {
527 CargoType cargo_type = cs->Index();
528 if (st->goods[cargo_type].HasRating()) {
529 /* For RTL we work in exactly the opposite direction. So
530 * decrement the space needed first, then draw to the left
531 * instead of drawing to the left and then incrementing
532 * the space. */
533 if (rtl) {
534 x -= rating_width + rating_spacing;
535 if (x < tr.left) break;
536 }
537 StationsWndShowStationRating(x, x + rating_width, tr.top, cargo_type, st->goods[cargo_type].HasData() ? st->goods[cargo_type].GetData().cargo.TotalCount() : 0, st->goods[cargo_type].rating);
538 if (!rtl) {
539 x += rating_width + rating_spacing;
540 if (x > tr.right) break;
541 }
542 }
543 }
544 tr.top += line_height;
545 }
546
547 if (this->vscroll->GetCount() == 0) { // company has no stations
548 DrawString(tr.left, tr.right, tr.top + (line_height - GetCharacterHeight(FS_NORMAL)) / 2, STR_STATION_LIST_NONE);
549 return;
550 }
551 break;
552 }
553 }
554 }
555
556 void SetStringParameters(WidgetID widget) const override
557 {
558 if (widget == WID_STL_CAPTION) {
559 SetDParam(0, this->window_number);
560 SetDParam(1, this->vscroll->GetCount());
561 }
562
563 if (widget == WID_STL_CARGODROPDOWN) {
564 if (this->filter.cargoes == 0) {
566 } else if (this->filter.cargoes == _cargo_mask) {
568 } else if (CountBits(this->filter.cargoes) == 1 && !this->filter.include_no_rating) {
569 SetDParam(0, CargoSpec::Get(FindFirstBit(this->filter.cargoes))->name);
570 } else {
572 }
573 }
574 }
575
576 DropDownList BuildCargoDropDownList(bool expanded) const
577 {
578 /* Define a custom item consisting of check mark, count string, icon and name string. */
580
581 DropDownList list;
582 list.push_back(MakeDropDownListStringItem(STR_STATION_LIST_CARGO_FILTER_SELECT_ALL, CargoFilterCriteria::CF_SELECT_ALL));
583 list.push_back(MakeDropDownListDividerItem());
584
585 bool any_hidden = false;
586
588 if (count == 0 && !expanded) {
589 any_hidden = true;
590 } else {
591 list.push_back(std::make_unique<DropDownString<DropDownListCheckedItem, FS_SMALL, true>>(fmt::format("{}", count), 0, this->filter.include_no_rating, STR_STATION_LIST_CARGO_FILTER_NO_RATING, CargoFilterCriteria::CF_NO_RATING, false, count == 0));
592 }
593
595 for (const CargoSpec *cs : _sorted_cargo_specs) {
596 count = this->stations_per_cargo_type[cs->Index()];
597 if (count == 0 && !expanded) {
598 any_hidden = true;
599 } else {
600 list.push_back(std::make_unique<DropDownListCargoItem>(HasBit(this->filter.cargoes, cs->Index()), fmt::format("{}", count), d, cs->GetCargoIcon(), PAL_NONE, cs->name, cs->Index(), false, count == 0));
601 }
602 }
603
604 if (!expanded && any_hidden) {
605 if (list.size() > 2) list.push_back(MakeDropDownListDividerItem());
606 list.push_back(MakeDropDownListStringItem(STR_STATION_LIST_CARGO_FILTER_EXPAND, CargoFilterCriteria::CF_EXPAND_LIST));
607 }
608
609 return list;
610 }
611
612 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
613 {
614 switch (widget) {
615 case WID_STL_LIST: {
616 auto it = this->vscroll->GetScrolledItemFromWidget(this->stations, pt.y, this, WID_STL_LIST, WidgetDimensions::scaled.framerect.top);
617 if (it == this->stations.end()) return; // click out of list bound
618
619 const Station *st = *it;
620 /* do not check HasStationInUse - it is slow and may be invalid */
621 assert(st->owner == this->window_number || st->owner == OWNER_NONE);
622
623 if (_ctrl_pressed) {
625 } else {
627 }
628 break;
629 }
630
631 case WID_STL_TRAIN:
632 case WID_STL_TRUCK:
633 case WID_STL_BUS:
634 case WID_STL_AIRPLANE:
635 case WID_STL_SHIP:
636 if (_ctrl_pressed) {
637 ToggleBit(this->filter.facilities, widget - WID_STL_TRAIN);
638 this->ToggleWidgetLoweredState(widget);
639 } else {
640 for (uint i : SetBitIterator(this->filter.facilities)) {
641 this->RaiseWidget(i + WID_STL_TRAIN);
642 }
643 this->filter.facilities = 1 << (widget - WID_STL_TRAIN);
644 this->LowerWidget(widget);
645 }
646 this->stations.ForceRebuild();
647 this->SetDirty();
648 break;
649
650 case WID_STL_FACILALL:
651 for (WidgetID i = WID_STL_TRAIN; i <= WID_STL_SHIP; i++) {
652 this->LowerWidget(i);
653 }
654
655 this->filter.facilities = FACIL_TRAIN | FACIL_TRUCK_STOP | FACIL_BUS_STOP | FACIL_AIRPORT | FACIL_DOCK;
656 this->stations.ForceRebuild();
657 this->SetDirty();
658 break;
659
660 case WID_STL_SORTBY: // flip sorting method asc/desc
661 this->stations.ToggleSortOrder();
662 this->SetDirty();
663 break;
664
665 case WID_STL_SORTDROPBTN: // select sorting criteria dropdown menu
666 ShowDropDownMenu(this, CompanyStationsWindow::sorter_names, this->stations.SortType(), WID_STL_SORTDROPBTN, 0, 0);
667 break;
668
670 this->filter_expanded = false;
671 ShowDropDownList(this, this->BuildCargoDropDownList(this->filter_expanded), -1, widget, 0, false, true);
672 break;
673 }
674 }
675
676 void OnDropdownSelect(int widget, int index) override
677 {
678 if (widget == WID_STL_SORTDROPBTN) {
679 if (this->stations.SortType() != index) {
680 this->stations.SetSortType(index);
681
682 /* Display the current sort variant */
683 this->GetWidget<NWidgetCore>(WID_STL_SORTDROPBTN)->SetString(CompanyStationsWindow::sorter_names[this->stations.SortType()]);
684
685 this->SetDirty();
686 }
687 }
688
689 if (widget == WID_STL_CARGODROPDOWN) {
690 FilterState oldstate = this->filter;
691
692 if (index >= 0 && index < NUM_CARGO) {
693 if (_ctrl_pressed) {
694 ToggleBit(this->filter.cargoes, index);
695 } else {
696 this->filter.cargoes = 1ULL << index;
697 this->filter.include_no_rating = false;
698 }
699 } else if (index == CargoFilterCriteria::CF_NO_RATING) {
700 if (_ctrl_pressed) {
701 this->filter.include_no_rating = !this->filter.include_no_rating;
702 } else {
703 this->filter.include_no_rating = true;
704 this->filter.cargoes = 0;
705 }
706 } else if (index == CargoFilterCriteria::CF_SELECT_ALL) {
707 this->filter.cargoes = _cargo_mask;
708 this->filter.include_no_rating = true;
709 } else if (index == CargoFilterCriteria::CF_EXPAND_LIST) {
710 this->filter_expanded = true;
711 ReplaceDropDownList(this, this->BuildCargoDropDownList(this->filter_expanded));
712 return;
713 }
714
715 if (oldstate.cargoes != this->filter.cargoes || oldstate.include_no_rating != this->filter.include_no_rating) {
716 this->stations.ForceRebuild();
717 this->SetDirty();
718
719 /* Only refresh the list if it's changed. */
720 if (_ctrl_pressed) ReplaceDropDownList(this, this->BuildCargoDropDownList(this->filter_expanded));
721 }
722
723 /* Always close the list if ctrl is not pressed. */
725 }
726 }
727
728 void OnGameTick() override
729 {
730 if (this->stations.NeedResort()) {
731 Debug(misc, 3, "Periodic rebuild station list company {}", static_cast<int>(this->window_number));
732 this->SetDirty();
733 }
734 }
735
736 void OnResize() override
737 {
738 this->vscroll->SetCapacityFromWidget(this, WID_STL_LIST, WidgetDimensions::scaled.framerect.Vertical());
739 }
740
746 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
747 {
748 if (data == 0) {
749 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
750 this->stations.ForceRebuild();
751 } else {
752 this->stations.ForceResort();
753 }
754 }
755};
756
757/* Available station sorting functions */
758const std::initializer_list<GUIStationList::SortFunction * const> CompanyStationsWindow::sorter_funcs = {
759 &StationNameSorter,
760 &StationTypeSorter,
761 &StationWaitingTotalSorter,
762 &StationWaitingAvailableSorter,
763 &StationRatingMaxSorter,
764 &StationRatingMinSorter
765};
766
767static constexpr NWidgetPart _nested_company_stations_widgets[] = {
769 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
770 NWidget(WWT_CAPTION, COLOUR_GREY, WID_STL_CAPTION), SetStringTip(STR_STATION_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
771 NWidget(WWT_SHADEBOX, COLOUR_GREY),
772 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
773 NWidget(WWT_STICKYBOX, COLOUR_GREY),
774 EndContainer(),
776 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_STL_TRAIN), SetAspect(WidgetDimensions::ASPECT_VEHICLE_ICON), SetStringTip(STR_TRAIN, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE_TOOLTIP), SetFill(0, 1),
777 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_STL_TRUCK), SetAspect(WidgetDimensions::ASPECT_VEHICLE_ICON), SetStringTip(STR_LORRY, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE_TOOLTIP), SetFill(0, 1),
778 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_STL_BUS), SetAspect(WidgetDimensions::ASPECT_VEHICLE_ICON), SetStringTip(STR_BUS, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE_TOOLTIP), SetFill(0, 1),
779 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_STL_SHIP), SetAspect(WidgetDimensions::ASPECT_VEHICLE_ICON), SetStringTip(STR_SHIP, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE_TOOLTIP), SetFill(0, 1),
780 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_STL_AIRPLANE), SetAspect(WidgetDimensions::ASPECT_VEHICLE_ICON), SetStringTip(STR_PLANE, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE_TOOLTIP), SetFill(0, 1),
781 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_STL_FACILALL), SetAspect(WidgetDimensions::ASPECT_VEHICLE_ICON), SetStringTip(STR_ABBREV_ALL, STR_STATION_LIST_SELECT_ALL_FACILITIES_TOOLTIP), SetTextStyle(TC_BLACK, FS_SMALL), SetFill(0, 1),
782 NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(5, 0), SetFill(0, 1), EndContainer(),
783 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_STL_CARGODROPDOWN), SetFill(1, 0), SetStringTip(STR_JUST_STRING, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE_TOOLTIP),
784 NWidget(WWT_PANEL, COLOUR_GREY), SetResize(1, 0), SetFill(1, 1), EndContainer(),
785 EndContainer(),
787 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_STL_SORTBY), SetMinimalSize(81, 12), SetStringTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
788 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_STL_SORTDROPBTN), SetMinimalSize(163, 12), SetStringTip(STR_SORT_BY_NAME, STR_TOOLTIP_SORT_CRITERIA), // widget_data gets overwritten.
789 NWidget(WWT_PANEL, COLOUR_GREY), SetResize(1, 0), SetFill(1, 1), EndContainer(),
790 EndContainer(),
792 NWidget(WWT_PANEL, COLOUR_GREY, WID_STL_LIST), SetMinimalSize(346, 125), SetResize(1, 10), SetToolTip(STR_STATION_LIST_TOOLTIP), SetScrollbar(WID_STL_SCROLLBAR), EndContainer(),
795 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
796 EndContainer(),
797 EndContainer(),
798};
799
800static WindowDesc _company_stations_desc(
801 WDP_AUTO, "list_stations", 358, 162,
803 {},
804 _nested_company_stations_widgets
805);
806
813{
814 if (!Company::IsValidID(company)) return;
815
816 AllocateWindowDescFront<CompanyStationsWindow>(_company_stations_desc, company);
817}
818
819static constexpr NWidgetPart _nested_station_view_widgets[] = {
821 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
822 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_SV_RENAME), SetAspect(WidgetDimensions::ASPECT_RENAME), SetSpriteTip(SPR_RENAME, STR_STATION_VIEW_RENAME_TOOLTIP),
823 NWidget(WWT_CAPTION, COLOUR_GREY, WID_SV_CAPTION), SetStringTip(STR_STATION_VIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
824 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_SV_LOCATION), SetAspect(WidgetDimensions::ASPECT_LOCATION), SetSpriteTip(SPR_GOTO_LOCATION, STR_STATION_VIEW_CENTER_TOOLTIP),
825 NWidget(WWT_SHADEBOX, COLOUR_GREY),
826 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
827 NWidget(WWT_STICKYBOX, COLOUR_GREY),
828 EndContainer(),
830 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SV_GROUP), SetMinimalSize(81, 12), SetFill(1, 1), SetStringTip(STR_STATION_VIEW_GROUP),
831 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_SV_GROUP_BY), SetMinimalSize(168, 12), SetResize(1, 0), SetFill(0, 1), SetToolTip(STR_TOOLTIP_GROUP_ORDER),
832 EndContainer(),
834 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_SORT_ORDER), SetMinimalSize(81, 12), SetFill(1, 1), SetStringTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
835 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_SV_SORT_BY), SetMinimalSize(168, 12), SetResize(1, 0), SetFill(0, 1), SetToolTip(STR_TOOLTIP_SORT_CRITERIA),
836 EndContainer(),
840 EndContainer(),
843 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_ACCEPTS_RATINGS), SetMinimalSize(46, 12), SetResize(1, 0), SetFill(1, 1),
844 SetStringTip(STR_STATION_VIEW_RATINGS_BUTTON, STR_STATION_VIEW_RATINGS_TOOLTIP),
845 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SV_CLOSE_AIRPORT), SetMinimalSize(45, 12), SetResize(1, 0), SetFill(1, 1),
846 SetStringTip(STR_STATION_VIEW_CLOSE_AIRPORT, STR_STATION_VIEW_CLOSE_AIRPORT_TOOLTIP),
847 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SV_CATCHMENT), SetMinimalSize(45, 12), SetResize(1, 0), SetFill(1, 1), SetStringTip(STR_BUTTON_CATCHMENT, STR_TOOLTIP_CATCHMENT),
848 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_TRAINS), SetAspect(WidgetDimensions::ASPECT_VEHICLE_ICON), SetFill(0, 1), SetStringTip(STR_TRAIN, STR_STATION_VIEW_SCHEDULED_TRAINS_TOOLTIP),
849 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_ROADVEHS), SetAspect(WidgetDimensions::ASPECT_VEHICLE_ICON), SetFill(0, 1), SetStringTip(STR_LORRY, STR_STATION_VIEW_SCHEDULED_ROAD_VEHICLES_TOOLTIP),
850 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_SHIPS), SetAspect(WidgetDimensions::ASPECT_VEHICLE_ICON), SetFill(0, 1), SetStringTip(STR_SHIP, STR_STATION_VIEW_SCHEDULED_SHIPS_TOOLTIP),
851 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_PLANES), SetAspect(WidgetDimensions::ASPECT_VEHICLE_ICON), SetFill(0, 1), SetStringTip(STR_PLANE, STR_STATION_VIEW_SCHEDULED_AIRCRAFT_TOOLTIP),
852 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
853 EndContainer(),
854};
855
865static void DrawCargoIcons(CargoType i, uint waiting, int left, int right, int y)
866{
867 int width = ScaleSpriteTrad(10);
868 uint num = std::min<uint>((waiting + (width / 2)) / width, (right - left) / width); // maximum is width / 10 icons so it won't overflow
869 if (num == 0) return;
870
871 SpriteID sprite = CargoSpec::Get(i)->GetCargoIcon();
872
873 int x = _current_text_dir == TD_RTL ? left : right - num * width;
874 do {
875 DrawSprite(sprite, PAL_NONE, x, y);
876 x += width;
877 } while (--num);
878}
879
880enum SortOrder : uint8_t {
881 SO_DESCENDING,
882 SO_ASCENDING
883};
884
885class CargoDataEntry;
886
887enum class CargoSortType : uint8_t {
888 AsGrouping,
889 Count,
891 StationID,
892 CargoType,
893};
894
896public:
897 CargoSorter(CargoSortType t = CargoSortType::StationID, SortOrder o = SO_ASCENDING) : type(t), order(o) {}
898 CargoSortType GetSortType() {return this->type;}
899 bool operator()(const CargoDataEntry *cd1, const CargoDataEntry *cd2) const;
900
901private:
902 CargoSortType type;
903 SortOrder order;
904
905 template <class Tid>
906 bool SortId(Tid st1, Tid st2) const;
907 bool SortCount(const CargoDataEntry *cd1, const CargoDataEntry *cd2) const;
908 bool SortStation (StationID st1, StationID st2) const;
909};
910
911typedef std::set<CargoDataEntry *, CargoSorter> CargoDataSet;
912
919public:
922
929 {
930 return this->InsertOrRetrieve<StationID>(station);
931 }
932
939 {
940 return this->InsertOrRetrieve<CargoType>(cargo);
941 }
942
943 void Update(uint count);
944
949 void Remove(StationID station)
950 {
952 this->Remove(&t);
953 }
954
960 {
962 this->Remove(&t);
963 }
964
971 {
973 return this->Retrieve(this->children->find(&t));
974 }
975
982 {
984 return this->Retrieve(this->children->find(&t));
985 }
986
987 void Resort(CargoSortType type, SortOrder order);
988
992 StationID GetStation() const { return this->station; }
993
997 CargoType GetCargo() const { return this->cargo; }
998
1002 uint GetCount() const { return this->count; }
1003
1007 CargoDataEntry *GetParent() const { return this->parent; }
1008
1012 uint GetNumChildren() const { return this->num_children; }
1013
1017 CargoDataSet::iterator Begin() const { return this->children->begin(); }
1018
1022 CargoDataSet::iterator End() const { return this->children->end(); }
1023
1027 bool HasTransfers() const { return this->transfers; }
1028
1032 void SetTransfers(bool value) { this->transfers = value; }
1033
1034 void Clear();
1035private:
1036
1037 CargoDataEntry(StationID st, uint c, CargoDataEntry *p);
1038 CargoDataEntry(CargoType car, uint c, CargoDataEntry *p);
1039 CargoDataEntry(StationID st);
1041
1042 CargoDataEntry *Retrieve(CargoDataSet::iterator i) const;
1043
1044 template <class Tid>
1046
1047 void Remove(CargoDataEntry *comp);
1048 void IncrementSize();
1049
1051 const union {
1052 StationID station;
1053 struct {
1056 };
1057 };
1059 uint count;
1060 CargoDataSet *children;
1061};
1062
1063CargoDataEntry::CargoDataEntry() :
1064 parent(nullptr),
1065 station(INVALID_STATION),
1066 num_children(0),
1067 count(0),
1068 children(new CargoDataSet(CargoSorter(CargoSortType::CargoType)))
1069{}
1070
1071CargoDataEntry::CargoDataEntry(CargoType cargo, uint count, CargoDataEntry *parent) :
1072 parent(parent),
1073 cargo(cargo),
1074 num_children(0),
1075 count(count),
1076 children(new CargoDataSet)
1077{}
1078
1079CargoDataEntry::CargoDataEntry(StationID station, uint count, CargoDataEntry *parent) :
1080 parent(parent),
1081 station(station),
1082 num_children(0),
1083 count(count),
1084 children(new CargoDataSet)
1085{}
1086
1087CargoDataEntry::CargoDataEntry(StationID station) :
1088 parent(nullptr),
1089 station(station),
1090 num_children(0),
1091 count(0),
1092 children(nullptr)
1093{}
1094
1095CargoDataEntry::CargoDataEntry(CargoType cargo) :
1096 parent(nullptr),
1097 cargo(cargo),
1098 num_children(0),
1099 count(0),
1100 children(nullptr)
1101{}
1102
1103CargoDataEntry::~CargoDataEntry()
1104{
1105 this->Clear();
1106 delete this->children;
1107}
1108
1113{
1114 if (this->children != nullptr) {
1115 for (auto &it : *this->children) {
1116 assert(it != this);
1117 delete it;
1118 }
1119 this->children->clear();
1120 }
1121 if (this->parent != nullptr) this->parent->count -= this->count;
1122 this->count = 0;
1123 this->num_children = 0;
1124}
1125
1133{
1134 CargoDataSet::iterator i = this->children->find(child);
1135 if (i != this->children->end()) {
1136 delete *i;
1137 this->children->erase(i);
1138 }
1139}
1140
1147template <class Tid>
1149{
1150 CargoDataEntry tmp(child_id);
1151 CargoDataSet::iterator i = this->children->find(&tmp);
1152 if (i == this->children->end()) {
1153 IncrementSize();
1154 return *(this->children->insert(new CargoDataEntry(child_id, 0, this)).first);
1155 } else {
1156 CargoDataEntry *ret = *i;
1157 assert(this->children->value_comp().GetSortType() != CargoSortType::Count);
1158 return ret;
1159 }
1160}
1161
1168{
1169 this->count += count;
1170 if (this->parent != nullptr) this->parent->Update(count);
1171}
1172
1177{
1178 ++this->num_children;
1179 if (this->parent != nullptr) this->parent->IncrementSize();
1180}
1181
1182void CargoDataEntry::Resort(CargoSortType type, SortOrder order)
1183{
1184 CargoDataSet *new_subs = new CargoDataSet(this->children->begin(), this->children->end(), CargoSorter(type, order));
1185 delete this->children;
1186 this->children = new_subs;
1187}
1188
1189CargoDataEntry *CargoDataEntry::Retrieve(CargoDataSet::iterator i) const
1190{
1191 if (i == this->children->end()) {
1192 return nullptr;
1193 } else {
1194 assert(this->children->value_comp().GetSortType() != CargoSortType::Count);
1195 return *i;
1196 }
1197}
1198
1199bool CargoSorter::operator()(const CargoDataEntry *cd1, const CargoDataEntry *cd2) const
1200{
1201 switch (this->type) {
1203 return this->SortId<StationID>(cd1->GetStation(), cd2->GetStation());
1205 return this->SortId<CargoType>(cd1->GetCargo(), cd2->GetCargo());
1207 return this->SortCount(cd1, cd2);
1209 return this->SortStation(cd1->GetStation(), cd2->GetStation());
1210 default:
1211 NOT_REACHED();
1212 }
1213}
1214
1215template <class Tid>
1216bool CargoSorter::SortId(Tid st1, Tid st2) const
1217{
1218 return (this->order == SO_ASCENDING) ? st1 < st2 : st2 < st1;
1219}
1220
1221bool CargoSorter::SortCount(const CargoDataEntry *cd1, const CargoDataEntry *cd2) const
1222{
1223 uint c1 = cd1->GetCount();
1224 uint c2 = cd2->GetCount();
1225 if (c1 == c2) {
1226 return this->SortStation(cd1->GetStation(), cd2->GetStation());
1227 } else if (this->order == SO_ASCENDING) {
1228 return c1 < c2;
1229 } else {
1230 return c2 < c1;
1231 }
1232}
1233
1234bool CargoSorter::SortStation(StationID st1, StationID st2) const
1235{
1236 if (!Station::IsValidID(st1)) {
1237 return Station::IsValidID(st2) ? this->order == SO_ASCENDING : this->SortId(st1, st2);
1238 } else if (!Station::IsValidID(st2)) {
1239 return order == SO_DESCENDING;
1240 }
1241
1242 int res = StrNaturalCompare(Station::Get(st1)->GetCachedName(), Station::Get(st2)->GetCachedName()); // Sort by name (natural sorting).
1243 if (res == 0) {
1244 return this->SortId(st1, st2);
1245 } else {
1246 return (this->order == SO_ASCENDING) ? res < 0 : res > 0;
1247 }
1248}
1249
1253struct StationViewWindow : public Window {
1257 struct RowDisplay {
1258 RowDisplay(CargoDataEntry *f, StationID n) : filter(f), next_station(n) {}
1260
1265 union {
1269 StationID next_station;
1270
1275 };
1276 };
1277
1278 typedef std::vector<RowDisplay> CargoDataVector;
1279
1280 static const int NUM_COLUMNS = 4;
1281
1286 INV_FLOWS = 0x100,
1287 INV_CARGO = 0x200
1289
1299
1307
1311 Scrollbar *vscroll;
1312
1313 /* Height of the #WID_SV_ACCEPT_RATING_LIST widget for different views. */
1314 static constexpr uint RATING_LINES = 13;
1315 static constexpr uint ACCEPTS_LINES = 3;
1316
1333
1341
1344
1349
1352 CargoDataVector displayed_rows;
1353
1356 {
1357 this->rating_lines = RATING_LINES;
1358 this->accepts_lines = ACCEPTS_LINES;
1359
1360 this->CreateNestedTree();
1361 this->vscroll = this->GetScrollbar(WID_SV_SCROLLBAR);
1362 /* Nested widget tree creation is done in two steps to ensure that this->GetWidget<NWidgetCore>(WID_SV_ACCEPTS_RATINGS) exists in UpdateWidgetSize(). */
1363 this->FinishInitNested(window_number);
1364
1365 this->groupings[0] = GR_CARGO;
1366 this->sortings[0] = CargoSortType::AsGrouping;
1369 this->sort_orders[0] = SO_ASCENDING;
1371 this->owner = Station::Get(window_number)->owner;
1372 }
1373
1374 void Close([[maybe_unused]] int data = 0) override
1375 {
1376 CloseWindowById(WC_TRAINS_LIST, VehicleListIdentifier(VL_STATION_LIST, VEH_TRAIN, this->owner, this->window_number).ToWindowNumber(), false);
1377 CloseWindowById(WC_ROADVEH_LIST, VehicleListIdentifier(VL_STATION_LIST, VEH_ROAD, this->owner, this->window_number).ToWindowNumber(), false);
1378 CloseWindowById(WC_SHIPS_LIST, VehicleListIdentifier(VL_STATION_LIST, VEH_SHIP, this->owner, this->window_number).ToWindowNumber(), false);
1379 CloseWindowById(WC_AIRCRAFT_LIST, VehicleListIdentifier(VL_STATION_LIST, VEH_AIRCRAFT, this->owner, this->window_number).ToWindowNumber(), false);
1380
1381 SetViewportCatchmentStation(Station::Get(this->window_number), false);
1382 this->Window::Close();
1383 }
1384
1395 void ShowCargo(CargoDataEntry *data, CargoType cargo, StationID source, StationID next, StationID dest, uint count)
1396 {
1397 if (count == 0) return;
1398 bool auto_distributed = _settings_game.linkgraph.GetDistributionType(cargo) != DT_MANUAL;
1399 const CargoDataEntry *expand = &this->expanded_rows;
1400 for (int i = 0; i < NUM_COLUMNS && expand != nullptr; ++i) {
1401 switch (groupings[i]) {
1402 case GR_CARGO:
1403 assert(i == 0);
1404 data = data->InsertOrRetrieve(cargo);
1405 data->SetTransfers(source != this->window_number);
1406 expand = expand->Retrieve(cargo);
1407 break;
1408 case GR_SOURCE:
1409 if (auto_distributed || source != this->window_number) {
1410 data = data->InsertOrRetrieve(source);
1411 expand = expand->Retrieve(source);
1412 }
1413 break;
1414 case GR_NEXT:
1415 if (auto_distributed) {
1416 data = data->InsertOrRetrieve(next);
1417 expand = expand->Retrieve(next);
1418 }
1419 break;
1420 case GR_DESTINATION:
1421 if (auto_distributed) {
1422 data = data->InsertOrRetrieve(dest);
1423 expand = expand->Retrieve(dest);
1424 }
1425 break;
1426 }
1427 }
1428 data->Update(count);
1429 }
1430
1432 {
1433 switch (widget) {
1434 case WID_SV_WAITING:
1436 size.height = 4 * resize.height + padding.height;
1437 this->expand_shrink_width = std::max(GetStringBoundingBox("-").width, GetStringBoundingBox("+").width);
1438 break;
1439
1441 size.height = ((this->GetWidget<NWidgetCore>(WID_SV_ACCEPTS_RATINGS)->GetString() == STR_STATION_VIEW_RATINGS_BUTTON) ? this->accepts_lines : this->rating_lines) * GetCharacterHeight(FS_NORMAL) + padding.height;
1442 break;
1443
1445 if (!(Station::Get(this->window_number)->facilities & FACIL_AIRPORT)) {
1446 /* Hide 'Close Airport' button if no airport present. */
1447 size.width = 0;
1448 resize.width = 0;
1449 fill.width = 0;
1450 }
1451 break;
1452 }
1453 }
1454
1455 void OnPaint() override
1456 {
1457 const Station *st = Station::Get(this->window_number);
1458 CargoDataEntry cargo;
1459 BuildCargoList(&cargo, st);
1460
1461 this->vscroll->SetCount(cargo.GetNumChildren()); // update scrollbar
1462
1463 /* disable some buttons */
1469 this->SetWidgetDisabledState(WID_SV_CLOSE_AIRPORT, !(st->facilities & FACIL_AIRPORT) || st->owner != _local_company || st->owner == OWNER_NONE); // Also consider SE, where _local_company == OWNER_NONE
1471
1474 this->SetWidgetLoweredState(WID_SV_CATCHMENT, _viewport_highlight_station == st);
1475
1476 this->DrawWidgets();
1477
1478 if (!this->IsShaded()) {
1479 /* Draw 'accepted cargo' or 'cargo ratings'. */
1481 const Rect r = wid->GetCurrentRect();
1483 int lines = this->DrawAcceptedCargo(r);
1484 if (lines > this->accepts_lines) { // Resize the widget, and perform re-initialization of the window.
1485 this->accepts_lines = lines;
1486 this->ReInit();
1487 return;
1488 }
1489 } else {
1490 int lines = this->DrawCargoRatings(r);
1491 if (lines > this->rating_lines) { // Resize the widget, and perform re-initialization of the window.
1492 this->rating_lines = lines;
1493 this->ReInit();
1494 return;
1495 }
1496 }
1497
1498 /* Draw arrow pointing up/down for ascending/descending sorting */
1499 this->DrawSortButtonState(WID_SV_SORT_ORDER, sort_orders[1] == SO_ASCENDING ? SBS_UP : SBS_DOWN);
1500
1501 int pos = this->vscroll->GetPosition();
1502
1503 int maxrows = this->vscroll->GetCapacity();
1504
1505 displayed_rows.clear();
1506
1507 /* Draw waiting cargo. */
1509 Rect waiting_rect = nwi->GetCurrentRect().Shrink(WidgetDimensions::scaled.framerect);
1510 this->DrawEntries(&cargo, waiting_rect, pos, maxrows, 0);
1512 }
1513 }
1514
1515 void SetStringParameters(WidgetID widget) const override
1516 {
1517 if (widget == WID_SV_CAPTION) {
1518 const Station *st = Station::Get(this->window_number);
1519 SetDParam(0, st->index);
1520 SetDParam(1, st->facilities);
1521 }
1522 }
1523
1530 {
1531 const Station *st = Station::Get(this->window_number);
1533 cargo_entry->Clear();
1534
1535 if (!st->goods[i].HasData()) return;
1536
1537 for (const auto &it : st->goods[i].GetData().flows) {
1538 StationID from = it.first;
1540 uint32_t prev_count = 0;
1541 for (const auto &flow_it : *it.second.GetShares()) {
1542 StationID via = flow_it.second;
1544 if (via == this->window_number) {
1546 } else {
1547 EstimateDestinations(i, from, via, flow_it.first - prev_count, via_entry);
1548 }
1549 prev_count = flow_it.first;
1550 }
1551 }
1552 }
1553
1563 void EstimateDestinations(CargoType cargo, StationID source, StationID next, uint count, CargoDataEntry *dest)
1564 {
1565 if (Station::IsValidID(next) && Station::IsValidID(source)) {
1566 GoodsEntry &ge = Station::Get(next)->goods[cargo];
1567 if (!ge.HasData()) return;
1568
1570 const FlowStatMap &flowmap = ge.GetData().flows;
1571 FlowStatMap::const_iterator map_it = flowmap.find(source);
1572 if (map_it != flowmap.end()) {
1573 const FlowStat::SharesMap *shares = map_it->second.GetShares();
1574 uint32_t prev_count = 0;
1575 for (FlowStat::SharesMap::const_iterator i = shares->begin(); i != shares->end(); ++i) {
1576 tmp.InsertOrRetrieve(i->second)->Update(i->first - prev_count);
1577 prev_count = i->first;
1578 }
1579 }
1580
1581 if (tmp.GetCount() == 0) {
1582 dest->InsertOrRetrieve(INVALID_STATION)->Update(count);
1583 } else {
1584 uint sum_estimated = 0;
1585 while (sum_estimated < count) {
1586 for (CargoDataSet::iterator i = tmp.Begin(); i != tmp.End() && sum_estimated < count; ++i) {
1587 CargoDataEntry *child = *i;
1588 uint estimate = DivideApprox(child->GetCount() * count, tmp.GetCount());
1589 if (estimate == 0) estimate = 1;
1590
1591 sum_estimated += estimate;
1592 if (sum_estimated > count) {
1593 estimate -= sum_estimated - count;
1594 sum_estimated = count;
1595 }
1596
1597 if (estimate > 0) {
1598 if (child->GetStation() == next) {
1599 dest->InsertOrRetrieve(next)->Update(estimate);
1600 } else {
1601 EstimateDestinations(cargo, source, child->GetStation(), estimate, dest);
1602 }
1603 }
1604 }
1605
1606 }
1607 }
1608 } else {
1609 dest->InsertOrRetrieve(INVALID_STATION)->Update(count);
1610 }
1611 }
1612
1620 {
1621 const CargoDataEntry *source_dest = this->cached_destinations.Retrieve(i);
1622 for (FlowStatMap::const_iterator it = flows.begin(); it != flows.end(); ++it) {
1623 StationID from = it->first;
1625 const FlowStat::SharesMap *shares = it->second.GetShares();
1626 for (FlowStat::SharesMap::const_iterator flow_it = shares->begin(); flow_it != shares->end(); ++flow_it) {
1628 for (CargoDataSet::iterator dest_it = via_entry->Begin(); dest_it != via_entry->End(); ++dest_it) {
1630 ShowCargo(cargo, i, from, flow_it->second, dest_entry->GetStation(), dest_entry->GetCount());
1631 }
1632 }
1633 }
1634 }
1635
1643 {
1644 const CargoDataEntry *source_dest = this->cached_destinations.Retrieve(i);
1645 for (StationCargoList::ConstIterator it = packets.Packets()->begin(); it != packets.Packets()->end(); it++) {
1646 const CargoPacket *cp = *it;
1647 StationID next = it.GetKey();
1648
1649 const CargoDataEntry *source_entry = source_dest->Retrieve(cp->GetFirstStation());
1650 if (source_entry == nullptr) {
1651 this->ShowCargo(cargo, i, cp->GetFirstStation(), next, INVALID_STATION, cp->Count());
1652 continue;
1653 }
1654
1656 if (via_entry == nullptr) {
1657 this->ShowCargo(cargo, i, cp->GetFirstStation(), next, INVALID_STATION, cp->Count());
1658 continue;
1659 }
1660
1661 uint remaining = cp->Count();
1662 for (CargoDataSet::iterator dest_it = via_entry->Begin(); dest_it != via_entry->End();) {
1664
1665 /* Advance iterator here instead of in the for statement to test whether this is the last entry */
1666 ++dest_it;
1667
1668 uint val;
1669 if (dest_it == via_entry->End()) {
1670 /* Allocate all remaining waiting cargo to the last destination to avoid
1671 * waiting cargo being "lost", and the displayed total waiting cargo
1672 * not matching GoodsEntry::TotalCount() */
1673 val = remaining;
1674 } else {
1675 val = std::min<uint>(remaining, DivideApprox(cp->Count() * dest_entry->GetCount(), via_entry->GetCount()));
1676 remaining -= val;
1677 }
1678 this->ShowCargo(cargo, i, cp->GetFirstStation(), next, dest_entry->GetStation(), val);
1679 }
1680 }
1681 this->ShowCargo(cargo, i, NEW_STATION, NEW_STATION, NEW_STATION, packets.ReservedCount());
1682 }
1683
1689 void BuildCargoList(CargoDataEntry *cargo, const Station *st)
1690 {
1691 for (CargoType i = 0; i < NUM_CARGO; i++) {
1692
1693 if (this->cached_destinations.Retrieve(i) == nullptr) {
1694 this->RecalcDestinations(i);
1695 }
1696
1697 const GoodsEntry &ge = st->goods[i];
1698 if (!ge.HasData()) continue;
1699
1700 if (this->current_mode == MODE_WAITING) {
1701 this->BuildCargoList(i, ge.GetData().cargo, cargo);
1702 } else {
1703 this->BuildFlowList(i, ge.GetData().flows, cargo);
1704 }
1705 }
1706 }
1707
1713 {
1714 std::list<StationID> stations;
1715 const CargoDataEntry *parent = data->GetParent();
1716 if (parent->GetParent() == nullptr) {
1717 this->displayed_rows.push_back(RowDisplay(&this->expanded_rows, data->GetCargo()));
1718 return;
1719 }
1720
1721 StationID next = data->GetStation();
1722 while (parent->GetParent()->GetParent() != nullptr) {
1723 stations.push_back(parent->GetStation());
1724 parent = parent->GetParent();
1725 }
1726
1727 CargoType cargo = parent->GetCargo();
1728 CargoDataEntry *filter = this->expanded_rows.Retrieve(cargo);
1729 while (!stations.empty()) {
1730 filter = filter->Retrieve(stations.back());
1731 stations.pop_back();
1732 }
1733
1734 this->displayed_rows.push_back(RowDisplay(filter, next));
1735 }
1736
1746 {
1747 if (station == this->window_number) {
1748 return here;
1749 } else if (station == INVALID_STATION) {
1750 return any;
1751 } else if (station == NEW_STATION) {
1753 } else {
1754 SetDParam(2, station);
1755 return other_station;
1756 }
1757 }
1758
1767 {
1768 CargoDataEntry *parent = cd->GetParent();
1769 for (int i = column - 1; i > 0; --i) {
1770 if (this->groupings[i] == GR_DESTINATION) {
1771 if (parent->GetStation() == station) {
1773 } else {
1774 return STR_STATION_VIEW_VIA;
1775 }
1776 }
1777 parent = parent->GetParent();
1778 }
1779
1780 if (this->groupings[column + 1] == GR_DESTINATION) {
1781 CargoDataSet::iterator begin = cd->Begin();
1782 CargoDataSet::iterator end = cd->End();
1783 if (begin != end && ++(cd->Begin()) == end && (*(begin))->GetStation() == station) {
1785 } else {
1786 return STR_STATION_VIEW_VIA;
1787 }
1788 }
1789
1790 return STR_STATION_VIEW_VIA;
1791 }
1792
1803 int DrawEntries(CargoDataEntry *entry, const Rect &r, int pos, int maxrows, int column, CargoType cargo = INVALID_CARGO)
1804 {
1805 if (this->sortings[column] == CargoSortType::AsGrouping) {
1806 if (this->groupings[column] != GR_CARGO) {
1807 entry->Resort(CargoSortType::StationString, this->sort_orders[column]);
1808 }
1809 } else {
1810 entry->Resort(CargoSortType::Count, this->sort_orders[column]);
1811 }
1812 for (CargoDataSet::iterator i = entry->Begin(); i != entry->End(); ++i) {
1813 CargoDataEntry *cd = *i;
1814
1815 Grouping grouping = this->groupings[column];
1816 if (grouping == GR_CARGO) cargo = cd->GetCargo();
1817 bool auto_distributed = _settings_game.linkgraph.GetDistributionType(cargo) != DT_MANUAL;
1818
1819 if (pos > -maxrows && pos <= 0) {
1820 StringID str = STR_EMPTY;
1821 int y = r.top - pos * GetCharacterHeight(FS_NORMAL);
1822 SetDParam(0, cargo);
1823 SetDParam(1, cd->GetCount());
1824
1825 if (this->groupings[column] == GR_CARGO) {
1827 DrawCargoIcons(cd->GetCargo(), cd->GetCount(), r.left + this->expand_shrink_width, r.right - this->expand_shrink_width, y);
1828 } else {
1829 if (!auto_distributed) grouping = GR_SOURCE;
1830 StationID station = cd->GetStation();
1831
1832 switch (grouping) {
1833 case GR_SOURCE:
1835 break;
1836 case GR_NEXT:
1838 if (str == STR_STATION_VIEW_VIA) str = this->SearchNonStop(cd, station, column);
1839 break;
1840 case GR_DESTINATION:
1842 break;
1843 default:
1844 NOT_REACHED();
1845 }
1846 if (pos == -this->scroll_to_row && Station::IsValidID(station)) {
1848 }
1849 }
1850
1851 bool rtl = _current_text_dir == TD_RTL;
1852 Rect text = r.Indent(column * WidgetDimensions::scaled.hsep_indent, rtl).Indent(this->expand_shrink_width, !rtl);
1853 Rect shrink = r.WithWidth(this->expand_shrink_width, !rtl);
1854
1855 DrawString(text.left, text.right, y, str);
1856
1857 if (column < NUM_COLUMNS - 1) {
1858 const char *sym = nullptr;
1859 if (cd->GetNumChildren() > 0) {
1860 sym = "-";
1861 } else if (auto_distributed && str != STR_STATION_VIEW_RESERVED) {
1862 sym = "+";
1863 } else {
1864 /* Only draw '+' if there is something to be shown. */
1865 const GoodsEntry &ge = Station::Get(this->window_number)->goods[cargo];
1866 if (ge.HasData()) {
1867 const StationCargoList &cargo_list = ge.GetData().cargo;
1868 if (grouping == GR_CARGO && (cargo_list.ReservedCount() > 0 || cd->HasTransfers())) {
1869 sym = "+";
1870 }
1871 }
1872 }
1873 if (sym != nullptr) DrawString(shrink.left, shrink.right, y, sym, TC_YELLOW);
1874 }
1875 this->SetDisplayedRow(cd);
1876 }
1877 --pos;
1878 if (auto_distributed || column == 0) {
1879 pos = this->DrawEntries(cd, r, pos, maxrows, column + 1, cargo);
1880 }
1881 }
1882 return pos;
1883 }
1884
1890 int DrawAcceptedCargo(const Rect &r) const
1891 {
1892 const Station *st = Station::Get(this->window_number);
1893 Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
1894
1896 int bottom = DrawStringMultiLine(tr.left, tr.right, tr.top, INT32_MAX, STR_STATION_VIEW_ACCEPTS_CARGO);
1898 }
1899
1905 int DrawCargoRatings(const Rect &r) const
1906 {
1907 const Station *st = Station::Get(this->window_number);
1908 bool rtl = _current_text_dir == TD_RTL;
1909 Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
1910
1911 if (st->town->exclusive_counter > 0) {
1912 SetDParam(0, st->town->exclusivity);
1915 }
1916
1919
1920 for (const CargoSpec *cs : _sorted_standard_cargo_specs) {
1921 const GoodsEntry *ge = &st->goods[cs->Index()];
1922 if (!ge->HasRating()) continue;
1923
1925 SetDParam(0, cs->name);
1926 SetDParam(1, lg != nullptr ? lg->Monthly((*lg)[ge->node].supply) : 0);
1928 SetDParam(3, ToPercent8(ge->rating));
1931 }
1933 }
1934
1940 template <class Tid>
1942 {
1943 if (filter->Retrieve(next) != nullptr) {
1944 filter->Remove(next);
1945 } else {
1946 filter->InsertOrRetrieve(next);
1947 }
1948 }
1949
1955 {
1956 if (row < 0 || (uint)row >= this->displayed_rows.size()) return;
1957 if (_ctrl_pressed) {
1958 this->scroll_to_row = row;
1959 } else {
1960 RowDisplay &display = this->displayed_rows[row];
1961 if (display.filter == &this->expanded_rows) {
1963 } else {
1965 }
1966 }
1968 }
1969
1970 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
1971 {
1972 switch (widget) {
1973 case WID_SV_WAITING:
1974 this->HandleCargoWaitingClick(this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SV_WAITING, WidgetDimensions::scaled.framerect.top) - this->vscroll->GetPosition());
1975 break;
1976
1977 case WID_SV_CATCHMENT:
1979 break;
1980
1981 case WID_SV_LOCATION:
1982 if (_ctrl_pressed) {
1983 ShowExtraViewportWindow(Station::Get(this->window_number)->xy);
1984 } else {
1985 ScrollMainWindowToTile(Station::Get(this->window_number)->xy);
1986 }
1987 break;
1988
1990 /* Swap between 'accepts' and 'ratings' view. */
1991 int height_change;
1995 height_change = this->rating_lines - this->accepts_lines;
1996 } else {
1998 height_change = this->accepts_lines - this->rating_lines;
1999 }
2000 this->ReInit(0, height_change * GetCharacterHeight(FS_NORMAL));
2001 break;
2002 }
2003
2004 case WID_SV_RENAME:
2005 SetDParam(0, this->window_number);
2008 break;
2009
2011 Command<CMD_OPEN_CLOSE_AIRPORT>::Post(this->window_number);
2012 break;
2013
2014 case WID_SV_TRAINS: // Show list of scheduled trains to this station
2015 case WID_SV_ROADVEHS: // Show list of scheduled road-vehicles to this station
2016 case WID_SV_SHIPS: // Show list of scheduled ships to this station
2017 case WID_SV_PLANES: { // Show list of scheduled aircraft to this station
2018 Owner owner = Station::Get(this->window_number)->owner;
2019 ShowVehicleListWindow(owner, (VehicleType)(widget - WID_SV_TRAINS), static_cast<StationID>(this->window_number));
2020 break;
2021 }
2022
2023 case WID_SV_SORT_BY: {
2024 /* The initial selection is composed of current mode and
2025 * sorting criteria for columns 1, 2, and 3. Column 0 is always
2026 * sorted by cargo type. The others can theoretically be sorted
2027 * by different things but there is no UI for that. */
2029 this->current_mode * 2 + (this->sortings[1] == CargoSortType::Count ? 1 : 0),
2030 WID_SV_SORT_BY, 0, 0);
2031 break;
2032 }
2033
2034 case WID_SV_GROUP_BY: {
2035 ShowDropDownMenu(this, StationViewWindow::group_names, this->grouping_index, WID_SV_GROUP_BY, 0, 0);
2036 break;
2037 }
2038
2039 case WID_SV_SORT_ORDER: { // flip sorting method asc/desc
2040 this->SelectSortOrder(this->sort_orders[1] == SO_ASCENDING ? SO_DESCENDING : SO_ASCENDING);
2041 this->SetTimeout();
2043 break;
2044 }
2045 }
2046 }
2047
2052 void SelectSortOrder(SortOrder order)
2053 {
2054 this->sort_orders[1] = this->sort_orders[2] = this->sort_orders[3] = order;
2055 _settings_client.gui.station_gui_sort_order = this->sort_orders[1];
2056 this->SetDirty();
2057 }
2058
2063 void SelectSortBy(int index)
2064 {
2066 switch (StationViewWindow::sort_names[index]) {
2068 this->current_mode = MODE_WAITING;
2069 this->sortings[1] = this->sortings[2] = this->sortings[3] = CargoSortType::AsGrouping;
2070 break;
2072 this->current_mode = MODE_WAITING;
2073 this->sortings[1] = this->sortings[2] = this->sortings[3] = CargoSortType::Count;
2074 break;
2076 this->current_mode = MODE_PLANNED;
2077 this->sortings[1] = this->sortings[2] = this->sortings[3] = CargoSortType::AsGrouping;
2078 break;
2080 this->current_mode = MODE_PLANNED;
2081 this->sortings[1] = this->sortings[2] = this->sortings[3] = CargoSortType::Count;
2082 break;
2083 default:
2084 NOT_REACHED();
2085 }
2086 /* Display the current sort variant */
2088 this->SetDirty();
2089 }
2090
2095 void SelectGroupBy(int index)
2096 {
2097 this->grouping_index = index;
2100 switch (StationViewWindow::group_names[index]) {
2102 this->groupings[1] = GR_SOURCE;
2103 this->groupings[2] = GR_NEXT;
2104 this->groupings[3] = GR_DESTINATION;
2105 break;
2107 this->groupings[1] = GR_SOURCE;
2108 this->groupings[2] = GR_DESTINATION;
2109 this->groupings[3] = GR_NEXT;
2110 break;
2112 this->groupings[1] = GR_NEXT;
2113 this->groupings[2] = GR_SOURCE;
2114 this->groupings[3] = GR_DESTINATION;
2115 break;
2117 this->groupings[1] = GR_NEXT;
2118 this->groupings[2] = GR_DESTINATION;
2119 this->groupings[3] = GR_SOURCE;
2120 break;
2122 this->groupings[1] = GR_DESTINATION;
2123 this->groupings[2] = GR_SOURCE;
2124 this->groupings[3] = GR_NEXT;
2125 break;
2127 this->groupings[1] = GR_DESTINATION;
2128 this->groupings[2] = GR_NEXT;
2129 this->groupings[3] = GR_SOURCE;
2130 break;
2131 }
2132 this->SetDirty();
2133 }
2134
2135 void OnDropdownSelect(WidgetID widget, int index) override
2136 {
2137 if (widget == WID_SV_SORT_BY) {
2138 this->SelectSortBy(index);
2139 } else {
2140 this->SelectGroupBy(index);
2141 }
2142 }
2143
2144 void OnQueryTextFinished(std::optional<std::string> str) override
2145 {
2146 if (!str.has_value()) return;
2147
2149 }
2150
2151 void OnResize() override
2152 {
2153 this->vscroll->SetCapacityFromWidget(this, WID_SV_WAITING, WidgetDimensions::scaled.framerect.Vertical());
2154 }
2155
2161 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
2162 {
2163 if (gui_scope) {
2164 if (data >= 0 && data < NUM_CARGO) {
2165 this->cached_destinations.Remove((CargoType)data);
2166 } else {
2167 this->ReInit();
2168 }
2169 }
2170 }
2171};
2172
2173static WindowDesc _station_view_desc(
2174 WDP_AUTO, "view_station", 249, 117,
2176 {},
2177 _nested_station_view_widgets
2178);
2179
2185void ShowStationViewWindow(StationID station)
2186{
2187 AllocateWindowDescFront<StationViewWindow>(_station_view_desc, station);
2188}
2189
2195
2196static std::vector<TileAndStation> _deleted_stations_nearby;
2197static std::vector<StationID> _stations_nearby_list;
2198
2206template <class T>
2207static bool AddNearbyStation(TileIndex tile, void *user_data)
2208{
2209 TileArea *ctx = (TileArea *)user_data;
2210
2211 /* First check if there were deleted stations here */
2212 for (auto it = _deleted_stations_nearby.begin(); it != _deleted_stations_nearby.end(); /* nothing */) {
2213 if (it->tile == tile) {
2214 _stations_nearby_list.push_back(it->station);
2215 it = _deleted_stations_nearby.erase(it);
2216 } else {
2217 ++it;
2218 }
2219 }
2220
2221 /* Check if own station and if we stay within station spread */
2222 if (!IsTileType(tile, MP_STATION)) return false;
2223
2224 StationID sid = GetStationIndex(tile);
2225
2226 /* This station is (likely) a waypoint */
2227 if (!T::IsValidID(sid)) return false;
2228
2229 BaseStation *st = BaseStation::Get(sid);
2230 if (st->owner != _local_company || std::ranges::find(_stations_nearby_list, sid) != _stations_nearby_list.end()) return false;
2231
2232 if (st->rect.BeforeAddRect(ctx->tile, ctx->w, ctx->h, StationRect::ADD_TEST).Succeeded()) {
2233 _stations_nearby_list.push_back(sid);
2234 }
2235
2236 return false; // We want to include *all* nearby stations
2237}
2238
2248template <class T>
2249static const BaseStation *FindStationsNearby(TileArea ta, bool distant_join)
2250{
2251 TileArea ctx = ta;
2252
2253 _stations_nearby_list.clear();
2254 _stations_nearby_list.push_back(NEW_STATION);
2255 _deleted_stations_nearby.clear();
2256
2257 /* Check the inside, to return, if we sit on another station */
2258 for (TileIndex t : ta) {
2259 if (t < Map::Size() && IsTileType(t, MP_STATION) && T::IsValidID(GetStationIndex(t))) return BaseStation::GetByTile(t);
2260 }
2261
2262 /* Look for deleted stations */
2263 for (const BaseStation *st : BaseStation::Iterate()) {
2264 if (T::IsValidBaseStation(st) && !st->IsInUse() && st->owner == _local_company) {
2265 /* Include only within station spread (yes, it is strictly less than) */
2266 if (std::max(DistanceMax(ta.tile, st->xy), DistanceMax(TileAddXY(ta.tile, ta.w - 1, ta.h - 1), st->xy)) < _settings_game.station.station_spread) {
2267 _deleted_stations_nearby.push_back({st->xy, st->index});
2268
2269 /* Add the station when it's within where we're going to build */
2270 if (IsInsideBS(TileX(st->xy), TileX(ctx.tile), ctx.w) &&
2271 IsInsideBS(TileY(st->xy), TileY(ctx.tile), ctx.h)) {
2272 AddNearbyStation<T>(st->xy, &ctx);
2273 }
2274 }
2275 }
2276 }
2277
2278 /* Only search tiles where we have a chance to stay within the station spread.
2279 * The complete check needs to be done in the callback as we don't know the
2280 * extent of the found station, yet. */
2281 if (distant_join && std::min(ta.w, ta.h) >= _settings_game.station.station_spread) return nullptr;
2282 uint max_dist = distant_join ? _settings_game.station.station_spread - std::min(ta.w, ta.h) : 1;
2283
2284 TileIndex tile = TileAddByDir(ctx.tile, DIR_N);
2285 CircularTileSearch(&tile, max_dist, ta.w, ta.h, AddNearbyStation<T>, &ctx);
2286
2287 return nullptr;
2288}
2289
2290static constexpr NWidgetPart _nested_select_station_widgets[] = {
2292 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
2293 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_JS_CAPTION), SetStringTip(STR_JOIN_STATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2294 NWidget(WWT_DEFSIZEBOX, COLOUR_DARK_GREEN),
2295 EndContainer(),
2297 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_JS_PANEL), SetResize(1, 0), SetScrollbar(WID_JS_SCROLLBAR), EndContainer(),
2299 NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_JS_SCROLLBAR),
2300 NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
2301 EndContainer(),
2302 EndContainer(),
2303};
2304
2309template <class T>
2311 StationPickerCmdProc select_station_proc;
2313 Scrollbar *vscroll;
2314
2315 SelectStationWindow(WindowDesc &desc, TileArea ta, StationPickerCmdProc&& proc) :
2316 Window(desc),
2317 select_station_proc(std::move(proc)),
2318 area(ta)
2319 {
2320 this->CreateNestedTree();
2321 this->vscroll = this->GetScrollbar(WID_JS_SCROLLBAR);
2322 this->GetWidget<NWidgetCore>(WID_JS_CAPTION)->SetString(T::IsWaypoint() ? STR_JOIN_WAYPOINT_CAPTION : STR_JOIN_STATION_CAPTION);
2323 this->FinishInitNested(0);
2324 this->OnInvalidateData(0);
2325
2326 _thd.freeze = true;
2327 }
2328
2329 void Close([[maybe_unused]] int data = 0) override
2330 {
2332
2333 _thd.freeze = false;
2334 this->Window::Close();
2335 }
2336
2338 {
2339 if (widget != WID_JS_PANEL) return;
2340
2341 /* Determine the widest string */
2343 for (const auto &station : _stations_nearby_list) {
2344 if (station == NEW_STATION) continue;
2345 const BaseStation *st = BaseStation::Get(station);
2346 SetDParam(0, st->index);
2347 SetDParam(1, st->facilities);
2349 }
2350
2351 resize.height = d.height;
2352 d.height *= 5;
2353 d.width += padding.width;
2354 d.height += padding.height;
2355 size = d;
2356 }
2357
2358 void DrawWidget(const Rect &r, WidgetID widget) const override
2359 {
2360 if (widget != WID_JS_PANEL) return;
2361
2362 Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
2363 auto [first, last] = this->vscroll->GetVisibleRangeIterators(_stations_nearby_list);
2364 for (auto it = first; it != last; ++it, tr.top += this->resize.step_height) {
2365 if (*it == NEW_STATION) {
2367 } else {
2368 const BaseStation *st = BaseStation::Get(*it);
2369 SetDParam(0, st->index);
2370 SetDParam(1, st->facilities);
2372 }
2373 }
2374
2375 }
2376
2377 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
2378 {
2379 if (widget != WID_JS_PANEL) return;
2380
2381 auto it = this->vscroll->GetScrolledItemFromWidget(_stations_nearby_list, pt.y, this, WID_JS_PANEL, WidgetDimensions::scaled.framerect.top);
2382 if (it == _stations_nearby_list.end()) return;
2383
2384 /* Execute stored Command */
2385 this->select_station_proc(false, *it);
2386
2387 /* Close Window; this might cause double frees! */
2389 }
2390
2391 void OnRealtimeTick([[maybe_unused]] uint delta_ms) override
2392 {
2393 if (_thd.dirty & 2) {
2394 _thd.dirty &= ~2;
2395 this->SetDirty();
2396 }
2397 }
2398
2399 void OnResize() override
2400 {
2401 this->vscroll->SetCapacityFromWidget(this, WID_JS_PANEL, WidgetDimensions::scaled.framerect.Vertical());
2402 }
2403
2409 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
2410 {
2411 if (!gui_scope) return;
2412 FindStationsNearby<T>(this->area, true);
2413 this->vscroll->SetCount(_stations_nearby_list.size());
2414 this->SetDirty();
2415 }
2416
2417 void OnMouseOver([[maybe_unused]] Point pt, WidgetID widget) override
2418 {
2419 if (widget != WID_JS_PANEL) {
2421 return;
2422 }
2423
2424 /* Show coverage area of station under cursor */
2425 auto it = this->vscroll->GetScrolledItemFromWidget(_stations_nearby_list, pt.y, this, WID_JS_PANEL, WidgetDimensions::scaled.framerect.top);
2426 const typename T::StationType *st = it == _stations_nearby_list.end() || *it == NEW_STATION ? nullptr : T::StationType::Get(*it);
2428 }
2429};
2430
2431static WindowDesc _select_station_desc(
2432 WDP_AUTO, "build_station_join", 200, 180,
2435 _nested_select_station_widgets
2436);
2437
2438
2446template <class T>
2447static bool StationJoinerNeeded(TileArea ta, const StationPickerCmdProc &proc)
2448{
2449 /* Only show selection if distant join is enabled in the settings */
2450 if (!_settings_game.station.distant_join_stations) return false;
2451
2452 /* If a window is already opened and we didn't ctrl-click,
2453 * return true (i.e. just flash the old window) */
2454 Window *selection_window = FindWindowById(WC_SELECT_STATION, 0);
2455 if (selection_window != nullptr) {
2456 /* Abort current distant-join and start new one */
2457 selection_window->Close();
2459 }
2460
2461 /* only show the popup, if we press ctrl */
2462 if (!_ctrl_pressed) return false;
2463
2464 /* Now check if we could build there */
2465 if (!proc(true, INVALID_STATION)) return false;
2466
2467 return FindStationsNearby<T>(ta, false) == nullptr;
2468}
2469
2476template <class T>
2477void ShowSelectBaseStationIfNeeded(TileArea ta, StationPickerCmdProc&& proc)
2478{
2479 if (StationJoinerNeeded<T>(ta, proc)) {
2481 new SelectStationWindow<T>(_select_station_desc, ta, std::move(proc));
2482 } else {
2483 proc(false, INVALID_STATION);
2484 }
2485}
2486
2492void ShowSelectStationIfNeeded(TileArea ta, StationPickerCmdProc proc)
2493{
2494 ShowSelectBaseStationIfNeeded<StationTypeFilter>(ta, std::move(proc));
2495}
2496
2502void ShowSelectRailWaypointIfNeeded(TileArea ta, StationPickerCmdProc proc)
2503{
2504 ShowSelectBaseStationIfNeeded<RailWaypointTypeFilter>(ta, std::move(proc));
2505}
2506
2512void ShowSelectRoadWaypointIfNeeded(TileArea ta, StationPickerCmdProc proc)
2513{
2514 ShowSelectBaseStationIfNeeded<RoadWaypointTypeFilter>(ta, std::move(proc));
2515}
static const uint64_t AIRPORT_CLOSED_block
Dummy block for indicating a closed airport.
Definition airport.h:128
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
constexpr uint8_t FindFirstBit(T x)
Search the first set bit in a value.
constexpr uint CountBits(T value)
Counts the number of set bits in a variable.
constexpr T ToggleBit(T &x, const uint8_t y)
Toggles a bit in a variable.
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:22
static const CargoType NUM_CARGO
Maximum number of cargo types in a game.
Definition cargo_type.h:74
Dimension GetLargestCargoIconSize()
Get dimensions of largest cargo icon.
std::span< const CargoSpec * > _sorted_standard_cargo_specs
Standard cargo specifications sorted alphabetically by name.
std::vector< const CargoSpec * > _sorted_cargo_specs
Cargo specifications sorted alphabetically by name.
CargoTypes _cargo_mask
Bitmask of cargo types available.
Definition cargotype.cpp:33
Types/functions related to cargoes.
bool IsCargoInClass(CargoType c, CargoClass cc)
Does cargo c have cargo class cc?
Definition cargotype.h:240
@ CC_PASSENGERS
Passengers.
Definition cargotype.h:51
A cargo data entry representing one possible row in the station view window's top part.
uint count
sum of counts of all children or amount of cargo for this entry.
void Clear()
Delete all subentries, reset count and num_children and adapt parent's count.
StationID GetStation() const
Get the station ID for this entry.
void IncrementSize()
Increment.
void SetTransfers(bool value)
Set the transfers state.
CargoType GetCargo() const
Get the cargo type for this entry.
uint num_children
the number of subentries belonging to this entry.
void Remove(CargoType cargo)
Remove a child associated with the given cargo.
CargoDataEntry * Retrieve(StationID station) const
Retrieve a child for the given station.
uint GetCount() const
Get the cargo count for this entry.
CargoDataEntry * GetParent() const
Get the parent entry for this entry.
StationID station
ID of the station this entry is associated with.
CargoType cargo
ID of the cargo this entry is associated with.
CargoDataSet * children
the children of this entry.
CargoDataEntry * InsertOrRetrieve(CargoType cargo)
Insert a new child or retrieve an existing child using a cargo type as ID.
CargoDataSet::iterator End() const
Get an iterator pointing to the end of the set of children.
void Update(uint count)
Update the count for this entry and propagate the change to the parent entry if there is one.
bool transfers
If there are transfers for this cargo.
CargoDataSet::iterator Begin() const
Get an iterator pointing to the begin of the set of children.
bool HasTransfers() const
Has this entry transfers.
CargoDataEntry * Retrieve(CargoType cargo) const
Retrieve a child for the given cargo.
CargoDataEntry * parent
the parent of this entry.
void Remove(StationID station)
Remove a child associated with the given station.
uint GetNumChildren() const
Get the number of children for this entry.
CargoDataEntry * InsertOrRetrieve(StationID station)
Insert a new child or retrieve an existing child using a station ID as ID.
const Tcont * Packets() const
Returns a pointer to the cargo packet list (so you can iterate over it etc).
Tcont::const_iterator ConstIterator
The const iterator for our container.
bool Succeeded() const
Did this command succeed?
The list of stations per company.
static bool StationRatingMaxSorter(const Station *const &a, const Station *const &b, const CargoTypes &cargo_filter)
Sort stations by their rating.
std::array< uint16_t, NUM_CARGO > stations_per_cargo_type
Number of stations with a rating for each cargo type.
void UpdateWidgetSize(WidgetID widget, Dimension &size, const Dimension &padding, Dimension &fill, Dimension &resize) override
Update size and resize step of a widget in the window.
void OnResize() override
Called after the window got resized.
void SortStationsList()
Sort the stations list.
static bool StationRatingMinSorter(const Station *const &a, const Station *const &b, const CargoTypes &cargo_filter)
Sort stations by their rating.
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
void OnDropdownSelect(int widget, int index) override
A dropdown option associated to this window has been selected.
static bool StationWaitingAvailableSorter(const Station *const &a, const Station *const &b, const CargoTypes &cargo_filter)
Sort stations by their available waiting cargo.
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
void OnPaint() override
The window must be repainted.
static bool StationNameSorter(const Station *const &a, const Station *const &b, const CargoTypes &)
Sort stations by their name.
void DrawWidget(const Rect &r, WidgetID widget) const override
Draw the contents of a nested widget.
static bool StationTypeSorter(const Station *const &a, const Station *const &b, const CargoTypes &)
Sort stations by their type.
void OnGameTick() override
Called once per (game) tick.
static bool StationWaitingTotalSorter(const Station *const &a, const Station *const &b, const CargoTypes &cargo_filter)
Sort stations by their waiting cargo.
void BuildStationsList(const Owner owner)
(Re)Build station list
uint16_t stations_per_cargo_type_no_rating
Number of stations without a rating.
void SetStringParameters(WidgetID widget) const override
Initialize string parameters for a widget.
Drop down checkmark component.
Drop down string component.
Flow descriptions by origin stations.
List template of 'things' T to sort in a GUI.
void RebuildDone()
Notify the sortlist that the rebuild is done.
void SetListing(Listing l)
Import sort conditions.
bool IsDescSortOrder() const
Check if the sort order is descending.
void ToggleSortOrder()
Toggle the sort order Since that is the worst condition for the sort function reverse the list here.
bool NeedRebuild() const
Check if a rebuild is needed.
void ForceRebuild()
Force that a rebuild is needed.
bool Sort(Comp compare)
Sort the list.
void ForceResort()
Force a resort next Sort call Reset the resort timer if used too.
uint8_t SortType() const
Get the sorttype of the list.
Listing GetListing() const
Export current sort conditions.
void SetSortFuncs(std::span< SortFunction *const > n_funcs)
Hand the sort function pointers to the GUIList.
bool NeedResort()
Check if a resort is needed next loop If used the resort timer will decrease every call till 0.
void SetSortType(uint8_t n_type)
Set the sorttype of the list.
A connected component of a link graph.
Definition linkgraph.h:37
Baseclass for nested widgets.
Base class for a 'real' widget.
void SetStringTip(StringID string, StringID tool_tip)
Set string and tool tip of the nested widget.
Definition widget.cpp:1139
Scrollbar data structure.
size_type GetCapacity() const
Gets the number of visible elements of the scrollbar.
void SetCount(size_t num)
Sets the number of elements in the list.
auto GetScrolledItemFromWidget(Tcontainer &container, int clickpos, const Window *const w, WidgetID widget, int padding=0, int line_height=-1) const
Return an iterator pointing to the element of a scrolled widget that a user clicked in.
size_type GetScrolledRowFromWidget(int clickpos, const Window *const w, WidgetID widget, int padding=0, int line_height=-1) const
Compute the row of a scrolled widget that a user clicked in.
Definition widget.cpp:2459
void SetCapacityFromWidget(Window *w, WidgetID widget, int padding=0)
Set capacity of visible elements from the size and resize properties of a widget.
Definition widget.cpp:2533
size_type GetCount() const
Gets the number of elements in the list.
auto GetVisibleRangeIterators(Tcontainer &container) const
Get a pair of iterators for the range of visible elements in a container.
size_type GetPosition() const
Gets the position of the first visible element in the list.
CargoList that is used for stations.
uint TotalCount() const
Returns total count of cargo at the station, including cargo which is already reserved for loading.
uint ReservedCount() const
Returns sum of cargo reserved for loading onto vehicles.
uint AvailableCount() const
Returns sum of cargo still available for loading at the sation.
static bool UsingWallclockUnits(bool newgame=false)
Check if we are using wallclock units.
RectPadding framerect
Standard padding inside many panels.
Definition window_gui.h:40
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition window_gui.h:28
int vsep_wide
Wide vertical spacing.
Definition window_gui.h:60
int hsep_wide
Wide horizontal spacing.
Definition window_gui.h:62
int hsep_normal
Normal horizontal spacing.
Definition window_gui.h:61
int hsep_indent
Width of identation for tree layouts.
Definition window_gui.h:63
Functions related to commands.
Definition of stuff that is very close to a company, like the company struct itself.
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Functions related to companies.
Owner
Enum for all companies/owners.
@ OWNER_NONE
The tile has no ownership.
int DivideApprox(int a, int b)
Deterministic approximate division.
Definition math_func.cpp:22
Functions related to debugging.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition debug.h:37
@ DIR_N
North.
void ShowDropDownMenu(Window *w, std::span< const StringID > strings, int selected, WidgetID button, uint32_t disabled_mask, uint32_t hidden_mask, uint width)
Show a dropdown menu window near a widget of the parent window.
Definition dropdown.cpp:441
void ShowDropDownList(Window *w, DropDownList &&list, int selected, WidgetID button, uint width, bool instant_close, bool persist)
Show a drop down list.
Definition dropdown.cpp:404
Common drop down list components.
Functions related to the drop down widget.
Types related to the drop down widget.
std::vector< std::unique_ptr< const DropDownListItem > > DropDownList
A drop down list is a collection of drop down list items.
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition fontcache.cpp:77
Dimension maxdim(const Dimension &d1, const Dimension &d2)
Compute bounding box of both dimensions.
Geometry functions.
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition gfx.cpp:851
Dimension GetStringListBoundingBox(std::span< const StringID > list, FontSize fontsize)
Get maximum dimension of a list of strings.
Definition gfx.cpp:889
int DrawString(int left, int right, int top, std::string_view str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
Definition gfx.cpp:657
bool _ctrl_pressed
Is Ctrl pressed?
Definition gfx.cpp:38
void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode)
Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen.
Definition gfx.cpp:114
void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
Draw a sprite, not in a viewport.
Definition gfx.cpp:988
int DrawStringMultiLine(int left, int right, int top, int bottom, std::string_view str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly over multiple lines.
Definition gfx.cpp:774
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
@ FS_SMALL
Index of the small font in the font tables.
Definition gfx_type.h:244
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:243
@ SA_CENTER
Center both horizontally and vertically.
Definition gfx_type.h:385
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:294
constexpr NWidgetPart SetFill(uint16_t fill_x, uint16_t fill_y)
Widget part function for setting filling.
constexpr NWidgetPart SetSpriteTip(SpriteID sprite, StringID tip={})
Widget part function for setting the sprite and tooltip.
constexpr NWidgetPart SetScrollbar(WidgetID index)
Attach a scrollbar to a widget.
constexpr NWidgetPart SetStringTip(StringID string, StringID tip={})
Widget part function for setting the string and tooltip.
constexpr NWidgetPart SetAspect(float ratio, AspectFlags flags=AspectFlag::ResizeX)
Widget part function for setting the aspect ratio.
constexpr NWidgetPart SetTextStyle(TextColour colour, FontSize size=FS_NORMAL)
Widget part function for setting the text style.
constexpr NWidgetPart SetMinimalSize(int16_t x, int16_t y)
Widget part function for setting the minimal size.
constexpr NWidgetPart NWidget(WidgetType tp, Colours col, WidgetID idx=-1)
Widget part function for starting a new 'real' widget.
constexpr NWidgetPart SetToolTip(StringID tip)
Widget part function for setting tooltip and clearing the widget data.
constexpr NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
constexpr NWidgetPart SetResize(int16_t dx, int16_t dy)
Widget part function for setting the resize step.
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition window.cpp:937
GUI functions that shouldn't be here.
void ShowExtraViewportWindow(TileIndex tile=INVALID_TILE)
Show a new Extra Viewport window.
Declaration of link graph classes used for cargo distribution.
@ DT_MANUAL
Manual distribution. No link graph calculations are run.
bool CircularTileSearch(TileIndex *tile, uint size, TestTileOnSearchProc proc, void *user_data)
Function performing a search around a center tile and going outward, thus in circle.
Definition map.cpp:244
uint DistanceMax(TileIndex t0, TileIndex t1)
Gets the biggest distance component (x or y) between the two given tiles.
Definition map.cpp:175
TileIndex TileAddXY(TileIndex tile, int x, int y)
Adds a given offset to a tile.
Definition map_func.h:470
TileIndex TileAddByDir(TileIndex tile, Direction dir)
Adds a Direction to a tile.
Definition map_func.h:599
static debug_inline TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition map_func.h:373
static debug_inline uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:425
static debug_inline uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:415
static debug_inline TileIndex TileVirtXY(uint x, uint y)
Get a tile from the virtual XY-coordinate.
Definition map_func.h:404
constexpr bool IsInsideBS(const T x, const size_t base, const size_t size)
Checks if a value is between a window started at some base point.
constexpr uint CeilDiv(uint a, uint b)
Computes ceil(a / b) for non-negative a and b.
constexpr uint ToPercent8(uint i)
Converts a "fract" value 0..255 to "percent" value 0..100.
void ShowQueryString(StringID str, StringID caption, uint maxsize, Window *parent, CharSetFilter afilter, QueryStringFlags flags)
Show a query popup window with a textbox in it.
static constexpr CargoType CF_EXPAND_LIST
Expand list to show all items (station list)
Definition cargo_type.h:101
static constexpr CargoType CF_NO_RATING
Show items with no rating (station list)
Definition cargo_type.h:99
static constexpr CargoType CF_SELECT_ALL
Select all items (station list)
Definition cargo_type.h:100
TextColour GetContrastColour(uint8_t background, uint8_t threshold)
Determine a contrasty text colour for a coloured background.
Definition palette.cpp:360
static const uint8_t PC_GREEN
Green palette colour.
static const uint8_t PC_RED
Red palette colour.
A number of safeguards to prevent using unsafe methods.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:57
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:56
Base types for having sorted lists in GUIs.
Base classes/functions for stations.
CargoArray GetAcceptanceAroundTiles(TileIndex center_tile, int w, int h, int rad, CargoTypes *always_accepted)
Get the acceptance of cargoes around the tile in 1/8.
bool HasStationInUse(StationID station, bool include_company, CompanyID company)
Tests whether the company's vehicles have this station in orders.
CargoTypes GetAcceptanceMask(const Station *st)
Get a mask of the cargo types that the station accepts.
CargoArray GetProductionAroundTiles(TileIndex north_tile, int w, int h, int rad)
Get the cargo types being produced around the tile (in a rectangle).
Command definitions related to stations.
void ShowSelectStationIfNeeded(TileArea ta, StationPickerCmdProc proc)
Show the station selection window when needed.
static bool AddNearbyStation(TileIndex tile, void *user_data)
Add station on this tile to _stations_nearby_list if it's fully within the station spread.
CargoSortType
@ StationID
by station id
@ CargoType
by cargo type
@ AsGrouping
by the same principle the entries are being grouped
@ StationString
by station name
@ Count
by amount of cargo
void ShowSelectRailWaypointIfNeeded(TileArea ta, StationPickerCmdProc proc)
Show the rail waypoint selection window when needed.
static void DrawCargoIcons(CargoType i, uint waiting, int left, int right, int y)
Draws icons of waiting cargo in the StationView window.
static void StationsWndShowStationRating(int left, int right, int y, CargoType type, uint amount, uint8_t rating)
Draw small boxes of cargo amount and ratings data at the given coordinates.
void FindStationsAroundSelection()
Find stations adjacent to the current tile highlight area, so that existing coverage area can be draw...
int DrawStationCoverageAreaText(const Rect &r, StationCoverageType sct, int rad, bool supplies)
Calculates and draws the accepted or supplied cargo around the selected tile(s)
void ShowStationViewWindow(StationID station)
Opens StationViewWindow for given station.
static const BaseStation * FindStationsNearby(TileArea ta, bool distant_join)
Circulate around the to-be-built station to find stations we could join.
void CheckRedrawStationCoverage(const Window *w)
Check whether we need to redraw the station coverage text.
void ShowCompanyStations(CompanyID company)
Opens window with list of company's stations.
void ShowSelectBaseStationIfNeeded(TileArea ta, StationPickerCmdProc &&proc)
Show the station selection window when needed.
static bool StationJoinerNeeded(TileArea ta, const StationPickerCmdProc &proc)
Check whether we need to show the station selection window.
void ShowSelectRoadWaypointIfNeeded(TileArea ta, StationPickerCmdProc proc)
Show the road waypoint selection window when needed.
Contains enums and function declarations connected with stations GUI.
StationCoverageType
Types of cargo to display for station coverage.
Definition station_gui.h:21
@ SCT_NON_PASSENGERS_ONLY
Draw all non-passenger class cargoes.
Definition station_gui.h:23
@ SCT_PASSENGERS_ONLY
Draw only passenger class cargoes.
Definition station_gui.h:22
@ SCT_ALL
Draw all cargoes.
Definition station_gui.h:24
StationID GetStationIndex(Tile t)
Get StationID from a tile.
Definition station_map.h:28
@ FACIL_DOCK
Station with a dock.
@ FACIL_BUS_STOP
Station with bus stops.
@ FACIL_AIRPORT
Station with an airport.
@ FACIL_TRUCK_STOP
Station with truck stops.
@ FACIL_TRAIN
Station with train station.
@ FACIL_NONE
The station has no facilities at all.
static const uint MAX_LENGTH_STATION_NAME_CHARS
The maximum length of a station name in characters including '\0'.
Types related to the station widgets.
@ WID_SV_CLOSE_AIRPORT
'Close airport' button.
@ WID_SV_SORT_ORDER
'Sort order' button
@ WID_SV_CATCHMENT
Toggle catchment area highlight.
@ WID_SV_ROADVEHS
List of scheduled road vehs button.
@ WID_SV_SCROLLBAR
Scrollbar.
@ WID_SV_CAPTION
Caption of the window.
@ WID_SV_GROUP
label for "group by"
@ WID_SV_RENAME
'Rename' button.
@ WID_SV_SORT_BY
'Sort by' button
@ WID_SV_GROUP_BY
'Group by' button
@ WID_SV_PLANES
List of scheduled planes button.
@ WID_SV_ACCEPT_RATING_LIST
List of accepted cargoes / rating of cargoes.
@ WID_SV_WAITING
List of waiting cargo.
@ WID_SV_SHIPS
List of scheduled ships button.
@ WID_SV_LOCATION
'Location' button.
@ WID_SV_TRAINS
List of scheduled trains button.
@ WID_SV_ACCEPTS_RATINGS
'Accepts' / 'Ratings' button.
@ WID_STL_CAPTION
Caption of the window.
@ WID_STL_TRUCK
'TRUCK' button - list only facilities where is a truck stop.
@ WID_STL_SCROLLBAR
Scrollbar next to the main panel.
@ WID_STL_SORTDROPBTN
Dropdown button.
@ WID_STL_SORTBY
'Sort by' button - reverse sort direction.
@ WID_STL_TRAIN
'TRAIN' button - list only facilities where is a railroad station.
@ WID_STL_BUS
'BUS' button - list only facilities where is a bus stop.
@ WID_STL_FACILALL
'ALL' button - list all facilities.
@ WID_STL_LIST
The main panel, list of stations.
@ WID_STL_SHIP
'SHIP' button - list only facilities where is a dock.
@ WID_STL_CARGODROPDOWN
Cargo type dropdown list.
@ WID_STL_AIRPLANE
'AIRPLANE' button - list only facilities where is an airport.
Definition of base types and functions in a cross-platform compatible way.
int StrNaturalCompare(std::string_view s1, std::string_view s2, bool ignore_garbage_at_front)
Compares two strings using case insensitive natural sort.
Definition string.cpp:589
Functions related to low-level strings.
@ CS_ALPHANUMERAL
Both numeric and alphabetic and spaces and stuff.
Definition string_type.h:25
void SetDParam(size_t n, uint64_t v)
Set a string parameter v at index n in the global string parameter array.
Definition strings.cpp:104
std::string GetString(StringID string)
Resolve the given StringID into a std::string with all the associated DParam lookups and formatting.
Definition strings.cpp:332
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition strings.cpp:56
Functions related to OTTD's strings.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
@ TD_RTL
Text is written right-to-left by default.
uint64_t flags
stores which blocks on the airport are taken. was 16 bit earlier on, then 32
Base class for all station-ish types.
StationFacility facilities
The facilities that this station has.
TileIndex xy
Base tile of the station.
Owner owner
The owner of this station.
StationRect rect
NOSAVE: Station spread out rectangle maintained by StationRect::xxx() functions.
Town * town
The town this station is associated with.
static BaseStation * GetByTile(TileIndex tile)
Get the base station belonging to a specific tile.
Class for storing amounts of cargo.
Definition cargo_type.h:112
Container for cargo from the same location and time.
Definition cargopacket.h:40
Specification of a cargo type.
Definition cargotype.h:77
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo type.
Definition cargotype.h:140
StringID abbrev
Two letter abbreviation for this cargo type.
Definition cargotype.h:98
SpriteID GetCargoIcon() const
Get sprite for showing cargo of this type.
StringID name
Name of this type of cargo.
Definition cargotype.h:94
bool IsValid() const
Tests for validity of this cargospec.
Definition cargotype.h:121
GUISettings gui
settings related to the GUI
CargoTypes cargoes
bitmap of cargo types to include
bool include_no_rating
Whether we should include stations with no cargo rating.
uint8_t facilities
types of stations of interest
Dimensions (a width and height) of a rectangle in 2D.
bool persistent_buildingtools
keep the building tools active after usage
uint8_t station_gui_group_order
the order of grouping cargo entries in the station gui
uint8_t station_gui_sort_order
the sort order of entries in the station gui - ascending or descending
bool station_show_coverage
whether to highlight coverage area
uint8_t station_gui_sort_by
sort cargo entries in the station gui by station name or amount
StationSettings station
settings related to station management
LinkGraphSettings linkgraph
settings for link graph calculations
FlowStatMap flows
Planned flows through this station.
StationCargoList cargo
The cargo packets of cargo waiting in this station.
Stores station stats for a single cargo.
bool HasRating() const
Does this cargo have a rating at this station?
debug_inline const GoodsEntryData & GetData() const
Get optional cargo packet/flow data.
NodeID node
ID of node in link graph referring to this goods entry.
LinkGraphID link_graph
Link graph this station belongs to.
uint8_t rating
Station rating for this cargo.
debug_inline bool HasData() const
Test if this goods entry has optional cargo packet/flow data.
Data structure describing how to show the list (what sort direction and criteria).
static uint MaxY()
Gets the maximum Y coordinate within the map, including MP_VOID.
Definition map_func.h:306
static debug_inline uint Size()
Get the size of the map.
Definition map_func.h:288
static debug_inline uint MaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
Definition map_func.h:297
Partial widget specification to allow NWidgets to be written nested.
Represents the covered area of e.g.
uint16_t w
The width of the area.
TileIndex tile
The base tile of the area.
uint16_t h
The height of the area.
Coordinates of a point in 2D.
Tindex index
Index of this pool item.
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
static Titem * Get(size_t index)
Returns Titem with given index.
Specification of a rectangle with absolute coordinates of all edges.
Rect WithWidth(int width, bool end) const
Copy Rect and set its width.
Rect Shrink(int s) const
Copy and shrink Rect by s pixels.
Rect Indent(int indent, bool end) const
Copy Rect and indent it from its position.
uint step_height
Step-size of height resize changes.
Definition window_gui.h:213
Window for selecting stations/waypoints to (distant) join to.
void DrawWidget(const Rect &r, WidgetID widget) const override
Draw the contents of a nested widget.
void OnRealtimeTick(uint delta_ms) override
Called periodically.
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
void OnResize() override
Called after the window got resized.
void Close(int data=0) override
Hide the window and all its child windows, and mark them for a later deletion.
void OnMouseOver(Point pt, WidgetID widget) override
The mouse is currently moving over the window or has just moved outside of the window.
TileArea area
Location of new station.
void UpdateWidgetSize(WidgetID widget, Dimension &size, const Dimension &padding, Dimension &fill, Dimension &resize) override
Update size and resize step of a widget in the window.
Iterable ensemble of each set bit in a value.
static bool IsExpected(const BaseStation *st)
Helper for checking whether the given station is of this type.
static Pool::IterateWrapper< Station > Iterate(size_t from=0)
Returns an iterable ensemble of all valid stations of type T.
static Waypoint * Get(size_t index)
Gets station with given index.
static bool IsValidID(size_t index)
Tests whether given index is a valid index for station of this type.
static Waypoint * From(BaseStation *st)
Converts a BaseStation to SpecializedStation with type checking.
uint8_t station_spread
amount a station may spread
bool distant_join_stations
allow to join non-adjacent stations
A row being displayed in the cargo view (as opposed to being "hidden" behind a plus sign).
StationID next_station
ID of the station belonging to the entry actually displayed if it's to/from/via.
CargoDataEntry * filter
Parent of the cargo entry belonging to the row.
CargoType next_cargo
ID of the cargo belonging to the entry actually displayed if it's cargo.
The StationView window.
void EstimateDestinations(CargoType cargo, StationID source, StationID next, uint count, CargoDataEntry *dest)
Estimate the amounts of cargo per final destination for a given cargo, source station and next hop an...
void HandleCargoWaitingClick(int row)
Handle a click on a specific row in the cargo view.
void OnQueryTextFinished(std::optional< std::string > str) override
The query window opened from this window has closed.
Mode
Display mode of the cargo view.
@ MODE_PLANNED
Show cargo planned to pass through the station.
@ MODE_WAITING
Show cargo waiting at the station.
void SetStringParameters(WidgetID widget) const override
Initialize string parameters for a widget.
void SelectSortBy(int index)
Select a new sort criterium for the cargo view.
void OnResize() override
Called after the window got resized.
void Close(int data=0) override
Hide the window and all its child windows, and mark them for a later deletion.
Grouping
Type of grouping used in each of the "columns".
@ GR_SOURCE
Group by source of cargo ("from").
@ GR_NEXT
Group by next station ("via").
@ GR_CARGO
Group by cargo type.
@ GR_DESTINATION
Group by estimated final destination ("to").
void RecalcDestinations(CargoType i)
Rebuild the cache for estimated destinations which is used to quickly show the "destination" entries ...
int DrawCargoRatings(const Rect &r) const
Draw cargo ratings in the WID_SV_ACCEPT_RATING_LIST widget.
static const int NUM_COLUMNS
Number of "columns" in the cargo view: cargo, from, via, to.
void SetDisplayedRow(const CargoDataEntry *data)
Mark a specific row, characterized by its CargoDataEntry, as expanded.
StringID SearchNonStop(CargoDataEntry *cd, StationID station, int column)
Determine if we need to show the special "non-stop" string.
void BuildCargoList(CargoType i, const StationCargoList &packets, CargoDataEntry *cargo)
Build up the cargo view for WAITING mode and a specific cargo.
Invalidation
Type of data invalidation.
@ INV_FLOWS
The planned flows have been recalculated and everything has to be updated.
@ INV_CARGO
Some cargo has been added or removed.
CargoSortType sortings[NUM_COLUMNS]
Sort types of the different 'columns'.
void SelectSortOrder(SortOrder order)
Select a new sort order for the cargo view.
CargoDataEntry expanded_rows
Parent entry of currently expanded rows.
void OnPaint() override
The window must be repainted.
static constexpr uint RATING_LINES
Height in lines of the cargo ratings view.
CargoDataVector displayed_rows
Parent entry of currently displayed rows (including collapsed ones).
static const StringID group_names[]
Names of the grouping options in the dropdown.
int grouping_index
Currently selected entry in the grouping drop down.
void UpdateWidgetSize(WidgetID widget, Dimension &size, const Dimension &padding, Dimension &fill, Dimension &resize) override
Update size and resize step of a widget in the window.
static constexpr uint ACCEPTS_LINES
Height in lines of the accepted cargo view.
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
void BuildFlowList(CargoType i, const FlowStatMap &flows, CargoDataEntry *cargo)
Build up the cargo view for PLANNED mode and a specific cargo.
void BuildCargoList(CargoDataEntry *cargo, const Station *st)
Build up the cargo view for all cargoes.
uint expand_shrink_width
The width allocated to the expand/shrink 'button'.
int DrawEntries(CargoDataEntry *entry, const Rect &r, int pos, int maxrows, int column, CargoType cargo=INVALID_CARGO)
Draw the given cargo entries in the station GUI.
void HandleCargoWaitingClick(CargoDataEntry *filter, Tid next)
Expand or collapse a specific row.
int rating_lines
Number of lines in the cargo ratings view.
static const StringID sort_names[]
Names of the sorting options in the dropdown.
CargoDataEntry cached_destinations
Cache for the flows passing through this station.
int scroll_to_row
If set, scroll the main viewport to the station pointed to by this row.
Mode current_mode
Currently selected display mode of cargo view.
int accepts_lines
Number of lines in the accepted cargo view.
StringID GetEntryString(StationID station, StringID here, StringID other_station, StringID any)
Select the correct string for an entry referring to the specified station.
SortOrder sort_orders[NUM_COLUMNS]
Sort order (ascending/descending) for the 'columns'.
void SelectGroupBy(int index)
Select a new grouping mode for the cargo view.
Grouping groupings[NUM_COLUMNS]
Grouping modes for the different columns.
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
void OnDropdownSelect(WidgetID widget, int index) override
A dropdown option associated to this window has been selected.
int DrawAcceptedCargo(const Rect &r) const
Draw accepted cargo in the WID_SV_ACCEPT_RATING_LIST widget.
void ShowCargo(CargoDataEntry *data, CargoType cargo, StationID source, StationID next, StationID dest, uint count)
Show a certain cargo entry characterized by source/next/dest station, cargo type and amount of cargo ...
Station data structure.
GoodsEntry goods[NUM_CARGO]
Goods at this station.
Airport airport
Tile area the airport covers.
Struct containing TileIndex and StationID.
TileIndex tile
TileIndex.
StationID station
StationID.
HighLightStyle drawstyle
Lower bits 0-3 are reserved for detailed highlight information.
uint8_t dirty
Whether the build station window needs to redraw due to the changed selection.
Point size
Size, in tile "units", of the white/red selection area.
Point pos
Location, in tile "units", of the northern tile of the selected area.
bool freeze
Freeze highlight in place.
CompanyID exclusivity
which company has exclusivity
Definition town.h:73
uint8_t exclusive_counter
months till the exclusivity expires
Definition town.h:74
The information about a vehicle list.
Definition vehiclelist.h:29
Representation of a waypoint.
High level window description.
Definition window_gui.h:168
Number to differentiate different windows of the same class.
Data structure for an opened window.
Definition window_gui.h:272
void ReInit(int rx=0, int ry=0, bool reposition=false)
Re-initialize a window, and optionally change its size.
Definition window.cpp:949
virtual void Close(int data=0)
Hide the window and all its child windows, and mark them for a later deletion.
Definition window.cpp:1044
static int SortButtonWidth()
Get width of up/down arrow of sort button state.
Definition widget.cpp:780
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
Definition window.cpp:1730
void DrawWidgets() const
Paint all widgets of a window.
Definition widget.cpp:731
Window * parent
Parent window.
Definition window_gui.h:327
void RaiseWidget(WidgetID widget_index)
Marks a widget as raised.
Definition window_gui.h:468
void SetWidgetDirty(WidgetID widget_index) const
Invalidate a widget, i.e.
Definition window.cpp:548
void DrawSortButtonState(WidgetID widget, SortButtonState state) const
Draw a sort button's up or down arrow symbol.
Definition widget.cpp:763
void CloseChildWindows(WindowClass wc=WC_INVALID) const
Close all children a window might have in a head-recursive manner.
Definition window.cpp:1032
ResizeInfo resize
Resize information.
Definition window_gui.h:313
void CreateNestedTree()
Perform the first part of the initialization of a nested widget tree.
Definition window.cpp:1720
bool IsWidgetLowered(WidgetID widget_index) const
Gets the lowered state of a widget.
Definition window_gui.h:490
Owner owner
The owner of the content shown in this window. Company colour is acquired from this variable.
Definition window_gui.h:315
void SetWidgetLoweredState(WidgetID widget_index, bool lowered_stat)
Sets the lowered/raised status of a widget.
Definition window_gui.h:440
bool IsShaded() const
Is window shaded currently?
Definition window_gui.h:556
void SetTimeout()
Set the timeout flag of the window and initiate the timer.
Definition window_gui.h:354
const NWID * GetWidget(WidgetID widnum) const
Get the nested widget with number widnum from the nested widget tree.
Definition window_gui.h:970
void LowerWidget(WidgetID widget_index)
Marks a widget as lowered.
Definition window_gui.h:459
const Scrollbar * GetScrollbar(WidgetID widnum) const
Return the Scrollbar to a widget index.
Definition window.cpp:311
void SetWidgetDisabledState(WidgetID widget_index, bool disab_stat)
Sets the enabled/disabled status of a widget.
Definition window_gui.h:380
int width
width of the window (number of pixels to the right in x direction)
Definition window_gui.h:310
void ToggleWidgetLoweredState(WidgetID widget_index)
Invert the lowered/raised status of a widget.
Definition window_gui.h:449
WindowNumber window_number
Window number within the window class.
Definition window_gui.h:301
Stuff related to the text buffer GUI.
@ QSF_ENABLE_DEFAULT
enable the 'Default' button ("\0" is returned)
Definition textbuf_gui.h:21
@ QSF_LEN_IN_CHARS
the length of the string is counted in characters
Definition textbuf_gui.h:22
Owner GetTileOwner(Tile tile)
Returns the owner of a tile.
Definition tile_map.h:178
static debug_inline bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
static const uint TILE_SIZE
Tile size in world coordinates.
Definition tile_type.h:15
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:95
@ MP_STATION
A tile of a station.
Definition tile_type.h:53
Functions related to tile highlights.
void ResetObjectToPlace()
Reset the cursor and mouse mode handling back to default (normal cursor, only clicking in windows).
void UpdateTileSelection()
Updates tile highlighting for all cases.
@ HT_RECT
rectangle (stations, depots, ...)
Base of the town class.
Functions related to the vehicle's GUIs.
VehicleType
Available vehicle types.
@ VEH_ROAD
Road vehicle type.
@ VEH_AIRCRAFT
Aircraft vehicle type.
@ VEH_SHIP
Ship vehicle type.
@ VEH_TRAIN
Train vehicle type.
Functions and type for generating vehicle lists.
bool ScrollMainWindowToTile(TileIndex tile, bool instant)
Scrolls the viewport of the main window to a given location.
void SetViewportCatchmentStation(const Station *st, bool sel)
Select or deselect station for coverage area highlight.
const Station * _viewport_highlight_station
Currently selected station for coverage area highlight.
Functions related to (drawing on) viewports.
Base of waypoints.
@ WPF_ROAD
This is a road waypoint.
static RectPadding ScaleGUITrad(const RectPadding &r)
Scale a RectPadding to GUI zoom level.
Definition widget.cpp:35
@ WWT_PUSHTXTBTN
Normal push-button (no toggle button) with text caption.
@ WWT_PUSHIMGBTN
Normal push-button (no toggle button) with image caption.
@ NWID_HORIZONTAL
Horizontal container.
Definition widget_type.h:66
@ WWT_TEXTBTN
(Toggle) Button with text
Definition widget_type.h:46
@ WWT_PANEL
Simple depressed panel.
Definition widget_type.h:41
@ WWT_STICKYBOX
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
Definition widget_type.h:57
@ WWT_SHADEBOX
Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX)
Definition widget_type.h:55
@ WWT_CAPTION
Window caption (window title between closebox and stickybox)
Definition widget_type.h:52
@ NWID_VSCROLLBAR
Vertical scrollbar.
Definition widget_type.h:76
@ NWID_VERTICAL
Vertical container.
Definition widget_type.h:68
@ WWT_CLOSEBOX
Close box (at top-left of a window)
Definition widget_type.h:60
@ WWT_RESIZEBOX
Resize box (normally at bottom-right of a window)
Definition widget_type.h:59
@ WWT_DEFSIZEBOX
Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX)
Definition widget_type.h:56
@ WWT_DROPDOWN
Drop down list.
Definition widget_type.h:61
@ NC_EQUALSIZE
Value of the NCB_EQUALSIZE flag.
void CloseWindowById(WindowClass cls, WindowNumber number, bool force, int data)
Close a window by its class and window number (if it is open).
Definition window.cpp:1137
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
Definition window.cpp:1095
Window functions not directly related to making/drawing windows.
@ Construction
This window is used for construction; close it whenever changing company.
@ SBS_DOWN
Sort ascending.
Definition window_gui.h:219
@ SBS_UP
Sort descending.
Definition window_gui.h:220
@ WDP_AUTO
Find a place automatically.
Definition window_gui.h:145
int WidgetID
Widget ID.
Definition window_type.h:20
@ WC_STATION_LIST
Station list; Window numbers:
@ WC_ROADVEH_LIST
Road vehicle list; Window numbers:
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Definition window_type.h:47
@ WC_SELECT_STATION
Select station (when joining stations); Window numbers:
@ WC_SHIPS_LIST
Ships list; Window numbers:
@ WC_STATION_VIEW
Station view; Window numbers:
@ WC_TRAINS_LIST
Trains list; Window numbers:
@ WC_DROPDOWN_MENU
Drop down menu; Window numbers:
@ WC_AIRCRAFT_LIST
Aircraft list; Window numbers:
Functions related to zooming.
int ScaleSpriteTrad(int value)
Scale traditional pixel dimensions to GUI zoom level, for drawing sprites.
Definition zoom_func.h:107