OpenTTD Source 20260311-master-g511d3794ce
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
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"
24#include "dropdown_func.h"
25#include "station_base.h"
26#include "waypoint_base.h"
27#include "tilehighlight_func.h"
28#include "company_base.h"
29#include "sortlist_type.h"
31#include "vehiclelist.h"
32#include "town.h"
33#include "linkgraph/linkgraph.h"
34#include "zoom_func.h"
35#include "station_cmd.h"
36
38#include "widgets/misc_widget.h"
39
40#include "table/strings.h"
41
43
44#include "safeguards.h"
45
47{
48 using StationType = Station;
49
50 static bool IsValidID(StationID id) { return Station::IsValidID(id); }
51 static bool IsValidBaseStation(const BaseStation *st) { return Station::IsExpected(st); }
52 static bool IsAcceptableWaypointTile(TileIndex) { return false; }
53 static constexpr bool IsWaypoint() { return false; }
54};
55
56template <bool ROAD, TileType TILE_TYPE>
58{
59 using StationType = Waypoint;
60
61 static bool IsValidID(StationID id) { return Waypoint::IsValidID(id) && HasBit(Waypoint::Get(id)->waypoint_flags, WPF_ROAD) == ROAD; }
62 static bool IsValidBaseStation(const BaseStation *st) { return Waypoint::IsExpected(st) && HasBit(Waypoint::From(st)->waypoint_flags, WPF_ROAD) == ROAD; }
63 static bool IsAcceptableWaypointTile(TileIndex tile) { return IsTileType(tile, TILE_TYPE); }
64 static constexpr bool IsWaypoint() { return true; }
65};
66using RailWaypointTypeFilter = GenericWaypointTypeFilter<false, TileType::Railway>;
67using RoadWaypointTypeFilter = GenericWaypointTypeFilter<true, TileType::Road>;
68
77int DrawStationCoverageAreaText(const Rect &r, StationCoverageType sct, int rad, bool supplies)
78{
79 TileIndex tile = TileVirtXY(_thd.pos.x, _thd.pos.y);
80 CargoTypes cargo_mask = 0;
81 if (_thd.drawstyle == HT_RECT && tile < Map::Size()) {
82 CargoArray cargoes;
83 if (supplies) {
84 cargoes = GetProductionAroundTiles(tile, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE, rad);
85 } else {
86 cargoes = GetAcceptanceAroundTiles(tile, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE, rad).first;
87 }
88
89 /* Convert cargo counts to a set of cargo bits, and draw the result. */
90 for (CargoType cargo = 0; cargo < NUM_CARGO; ++cargo) {
91 switch (sct) {
92 case SCT_PASSENGERS_ONLY: if (!IsCargoInClass(cargo, CargoClass::Passengers)) continue; break;
93 case SCT_NON_PASSENGERS_ONLY: if (IsCargoInClass(cargo, CargoClass::Passengers)) continue; break;
94 case SCT_ALL: break;
95 default: NOT_REACHED();
96 }
97 if (cargoes[cargo] >= (supplies ? 1U : 8U)) SetBit(cargo_mask, cargo);
98 }
99 }
100 return DrawStringMultiLine(r, GetString(supplies ? STR_STATION_BUILD_SUPPLIES_CARGO : STR_STATION_BUILD_ACCEPTS_CARGO, cargo_mask));
101}
102
107template <typename T>
109{
110 /* With distant join we don't know which station will be selected, so don't show any */
111 if (_ctrl_pressed) {
112 SetViewportCatchmentSpecializedStation<typename T::StationType>(nullptr, true);
113 return;
114 }
115
116 /* Tile area for TileHighlightData */
117 TileArea location(TileVirtXY(_thd.pos.x, _thd.pos.y), _thd.size.x / TILE_SIZE - 1, _thd.size.y / TILE_SIZE - 1);
118
119 /* If the current tile is already a station, then it must be the nearest station. */
120 if (IsTileType(location.tile, TileType::Station) && GetTileOwner(location.tile) == _local_company) {
121 typename T::StationType *st = T::StationType::GetByTile(location.tile);
122 if (st != nullptr && T::IsValidBaseStation(st)) {
123 SetViewportCatchmentSpecializedStation<typename T::StationType>(st, true);
124 return;
125 }
126 }
127
128 /* Extended area by one tile */
129 uint x = TileX(location.tile);
130 uint y = TileY(location.tile);
131
132 /* Waypoints can only be built on existing rail/road tiles, so don't extend area if not highlighting a rail tile. */
133 int max_c = T::IsWaypoint() && !T::IsAcceptableWaypointTile(location.tile) ? 0 : 1;
134 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)));
135
136 typename T::StationType *adjacent = nullptr;
137
138 /* Direct loop instead of ForAllStationsAroundTiles as we are not interested in catchment area */
139 for (TileIndex tile : ta) {
141 typename T::StationType *st = T::StationType::GetByTile(tile);
142 if (st == nullptr || !T::IsValidBaseStation(st)) continue;
143 if (adjacent != nullptr && st != adjacent) {
144 /* Multiple nearby, distant join is required. */
145 adjacent = nullptr;
146 break;
147 }
148 adjacent = st;
149 }
150 }
151 SetViewportCatchmentSpecializedStation<typename T::StationType>(adjacent, true);
152}
153
160{
161 /* Test if ctrl state changed */
162 static bool _last_ctrl_pressed;
163 if (_ctrl_pressed != _last_ctrl_pressed) {
164 _thd.dirty = 0xff;
165 _last_ctrl_pressed = _ctrl_pressed;
166 }
167
168 if (_thd.dirty & 1) {
169 _thd.dirty &= ~1;
170 w->SetDirty();
171
172 if (_settings_client.gui.station_show_coverage && _thd.drawstyle == HT_RECT) {
174 }
175 }
176}
177
178template <typename T>
179void CheckRedrawWaypointCoverage()
180{
181 /* Test if ctrl state changed */
182 static bool _last_ctrl_pressed;
183 if (_ctrl_pressed != _last_ctrl_pressed) {
184 _thd.dirty = 0xff;
185 _last_ctrl_pressed = _ctrl_pressed;
186 }
187
188 if (_thd.dirty & 1) {
189 _thd.dirty &= ~1;
190
191 if (_thd.drawstyle == HT_RECT) {
193 }
194 }
195}
196
197void CheckRedrawRailWaypointCoverage(const Window *)
198{
199 CheckRedrawWaypointCoverage<RailWaypointTypeFilter>();
200}
201
202void CheckRedrawRoadWaypointCoverage(const Window *)
203{
204 CheckRedrawWaypointCoverage<RoadWaypointTypeFilter>();
205}
206
219static void StationsWndShowStationRating(int left, int right, int y, CargoType cargo, uint amount, uint8_t rating)
220{
221 static const uint units_full = 576;
222 static const uint rating_full = 224;
223
224 const CargoSpec *cs = CargoSpec::Get(cargo);
225 if (!cs->IsValid()) return;
226
227 int padding = ScaleGUITrad(1);
228 int width = right - left;
229 PixelColour colour = cs->rating_colour;
230 TextColour tc = GetContrastColour(colour);
231 uint w = std::min(amount + 5, units_full) * width / units_full;
232
233 int height = GetCharacterHeight(FS_SMALL) + padding - 1;
234
235 if (amount > 30) {
236 /* Draw total cargo (limited) on station */
237 GfxFillRect(left, y, left + w - 1, y + height, colour);
238 } else {
239 /* Draw a (scaled) one pixel-wide bar of additional cargo meter, useful
240 * for stations with only a small amount (<=30) */
241 uint rest = ScaleGUITrad(amount) / 5;
242 if (rest != 0) {
243 GfxFillRect(left, y + height - rest, left + padding - 1, y + height, colour);
244 }
245 }
246
247 DrawString(left + padding, right, y, cs->abbrev, tc, SA_CENTER, false, FS_SMALL);
248
249 /* Draw green/red ratings bar (fits under the waiting bar) */
250 y += height + padding + 1;
251 GfxFillRect(left + padding, y, right - padding - 1, y + padding - 1, PC_RED);
252 w = std::min<uint>(rating, rating_full) * (width - padding - padding) / rating_full;
253 if (w != 0) GfxFillRect(left + padding, y, left + w - 1, y + padding - 1, PC_GREEN);
254}
255
257
261class CompanyStationsWindow : public Window
262{
263protected:
265 struct FilterState {
266 Listing last_sorting;
267 StationFacilities facilities;
269 CargoTypes cargoes;
270 };
271
272 static inline FilterState initial_state = {
273 {false, 0},
275 true,
276 ALL_CARGOTYPES,
277 };
278
280 static inline const StringID sorter_names[] = {
281 STR_SORT_BY_NAME,
282 STR_SORT_BY_FACILITY,
283 STR_SORT_BY_WAITING_TOTAL,
284 STR_SORT_BY_WAITING_AVAILABLE,
285 STR_SORT_BY_RATING_MAX,
286 STR_SORT_BY_RATING_MIN,
287 };
288 static const std::initializer_list<GUIStationList::SortFunction * const> sorter_funcs;
289
290 FilterState filter{};
291 GUIStationList stations{filter.cargoes};
292 Scrollbar *vscroll = nullptr;
293 uint rating_width = 0;
294 bool filter_expanded = false;
295 std::array<uint16_t, NUM_CARGO> stations_per_cargo_type{};
297
303 void BuildStationsList(const Owner owner)
304 {
305 if (!this->stations.NeedRebuild()) return;
306
307 Debug(misc, 3, "Building station list for company {}", owner);
308
309 this->stations.clear();
310 this->stations_per_cargo_type.fill(0);
311 this->stations_per_cargo_type_no_rating = 0;
312
313 for (const Station *st : Station::Iterate()) {
314 if (this->filter.facilities.Any(st->facilities)) { // only stations with selected facilities
315 if (st->owner == owner || (st->owner == OWNER_NONE && HasStationInUse(st->index, true, owner))) {
316 bool has_rating = false;
317 /* Add to the station/cargo counts. */
318 for (CargoType cargo = 0; cargo < NUM_CARGO; ++cargo) {
319 if (st->goods[cargo].HasRating()) this->stations_per_cargo_type[cargo]++;
320 }
321 for (CargoType cargo = 0; cargo < NUM_CARGO; ++cargo) {
322 if (st->goods[cargo].HasRating()) {
323 has_rating = true;
324 if (HasBit(this->filter.cargoes, cargo)) {
325 this->stations.push_back(st);
326 break;
327 }
328 }
329 }
330 /* Stations with no cargo rating. */
331 if (!has_rating) {
332 if (this->filter.include_no_rating) this->stations.push_back(st);
333 this->stations_per_cargo_type_no_rating++;
334 }
335 }
336 }
337 }
338
339 this->stations.RebuildDone();
340
341 this->vscroll->SetCount(this->stations.size()); // Update the scrollbar
342 }
343
345 static bool StationNameSorter(const Station * const &a, const Station * const &b, [[maybe_unused]] const CargoTypes &filter)
346 {
347 int r = StrNaturalCompare(a->GetCachedName(), b->GetCachedName()); // Sort by name (natural sorting).
348 if (r == 0) return a->index < b->index;
349 return r < 0;
350 }
351
353 static bool StationTypeSorter(const Station * const &a, const Station * const &b, [[maybe_unused]] const CargoTypes &filter)
354 {
355 return a->facilities < b->facilities;
356 }
357
359 static bool StationWaitingTotalSorter(const Station * const &a, const Station * const &b, const CargoTypes &filter)
360 {
361 int diff = 0;
362
363 for (CargoType cargo : SetCargoBitIterator(filter)) {
364 diff += a->goods[cargo].TotalCount() - b->goods[cargo].TotalCount();
365 }
366
367 return diff < 0;
368 }
369
371 static bool StationWaitingAvailableSorter(const Station * const &a, const Station * const &b, const CargoTypes &filter)
372 {
373 int diff = 0;
374
375 for (CargoType cargo : SetCargoBitIterator(filter)) {
376 diff += a->goods[cargo].AvailableCount() - b->goods[cargo].AvailableCount();
377 }
378
379 return diff < 0;
380 }
381
383 static bool StationRatingMaxSorter(const Station * const &a, const Station * const &b, const CargoTypes &filter)
384 {
385 uint8_t maxr1 = 0;
386 uint8_t maxr2 = 0;
387
388 for (CargoType cargo : SetCargoBitIterator(filter)) {
389 if (a->goods[cargo].HasRating()) maxr1 = std::max(maxr1, a->goods[cargo].rating);
390 if (b->goods[cargo].HasRating()) maxr2 = std::max(maxr2, b->goods[cargo].rating);
391 }
392
393 return maxr1 < maxr2;
394 }
395
397 static bool StationRatingMinSorter(const Station * const &a, const Station * const &b, const CargoTypes &filter)
398 {
399 uint8_t minr1 = 255;
400 uint8_t minr2 = 255;
401
402 for (CargoType cargo : SetCargoBitIterator(filter)) {
403 if (a->goods[cargo].HasRating()) minr1 = std::min(minr1, a->goods[cargo].rating);
404 if (b->goods[cargo].HasRating()) minr2 = std::min(minr2, b->goods[cargo].rating);
405 }
406
407 return minr1 > minr2;
408 }
409
412 {
413 if (!this->stations.Sort()) return;
414
415 /* Set the modified widget dirty */
417 }
418
419public:
421 {
422 /* Load initial filter state. */
423 this->filter = CompanyStationsWindow::initial_state;
424 if (this->filter.cargoes == ALL_CARGOTYPES) this->filter.cargoes = _cargo_mask;
425
426 this->stations.SetListing(this->filter.last_sorting);
427 this->stations.SetSortFuncs(CompanyStationsWindow::sorter_funcs);
428 this->stations.ForceRebuild();
429 this->stations.NeedResort();
430 this->SortStationsList();
431
432 this->CreateNestedTree();
433 this->vscroll = this->GetScrollbar(WID_STL_SCROLLBAR);
434 this->FinishInitNested(window_number);
435 this->owner = this->window_number;
436
437 if (this->filter.cargoes == ALL_CARGOTYPES) this->filter.cargoes = _cargo_mask;
438
439 for (uint i = 0; i < 5; i++) {
440 if (HasBit(this->filter.facilities.base(), i)) this->LowerWidget(i + WID_STL_TRAIN);
441 }
442
443 this->GetWidget<NWidgetCore>(WID_STL_SORTDROPBTN)->SetString(CompanyStationsWindow::sorter_names[this->stations.SortType()]);
444 }
445
448 {
449 /* Save filter state. */
450 this->filter.last_sorting = this->stations.GetListing();
451 CompanyStationsWindow::initial_state = this->filter;
452 }
453
454 void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override
455 {
456 switch (widget) {
457 case WID_STL_SORTBY: {
459 d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
460 d.height += padding.height;
461 size = maxdim(size, d);
462 break;
463 }
464
465 case WID_STL_SORTDROPBTN: {
467 d.width += padding.width;
468 d.height += padding.height;
469 size = maxdim(size, d);
470 break;
471 }
472
473 case WID_STL_LIST:
474 fill.height = resize.height = std::max(GetCharacterHeight(FS_NORMAL), GetCharacterHeight(FS_SMALL) + ScaleGUITrad(3));
475 size.height = padding.height + 5 * resize.height;
476
477 /* Determine appropriate width for mini station rating graph */
478 this->rating_width = 0;
479 for (const CargoSpec *cs : _sorted_standard_cargo_specs) {
480 this->rating_width = std::max(this->rating_width, GetStringBoundingBox(cs->abbrev, FS_SMALL).width);
481 }
482 /* Approximately match original 16 pixel wide rating bars by multiplying string width by 1.6 */
483 this->rating_width = this->rating_width * 16 / 10;
484 break;
485 }
486 }
487
488 void OnPaint() override
489 {
490 this->BuildStationsList(this->window_number);
491 this->SortStationsList();
492
493 this->DrawWidgets();
494 }
495
496 void DrawWidget(const Rect &r, WidgetID widget) const override
497 {
498 switch (widget) {
499 case WID_STL_SORTBY:
500 /* draw arrow pointing up/down for ascending/descending sorting */
502 break;
503
504 case WID_STL_LIST: {
505 bool rtl = _current_text_dir == TD_RTL;
506 Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
507 uint line_height = this->GetWidget<NWidgetBase>(widget)->resize_y;
508 /* Spacing between station name and first rating graph. */
509 int text_spacing = WidgetDimensions::scaled.hsep_wide;
510 /* Spacing between additional rating graphs. */
511 int rating_spacing = WidgetDimensions::scaled.hsep_normal;
512
513 auto [first, last] = this->vscroll->GetVisibleRangeIterators(this->stations);
514 for (auto it = first; it != last; ++it) {
515 const Station *st = *it;
516 assert(st->xy != INVALID_TILE);
517
518 /* Do not do the complex check HasStationInUse here, it may be even false
519 * when the order had been removed and the station list hasn't been removed yet */
520 assert(st->owner == owner || st->owner == OWNER_NONE);
521
522 int x = DrawString(tr.left, tr.right, tr.top + (line_height - GetCharacterHeight(FS_NORMAL)) / 2, GetString(STR_STATION_LIST_STATION, st->index, st->facilities));
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].TotalCount(), 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 std::string GetWidgetString(WidgetID widget, StringID stringid) const override
557 {
558 if (widget == WID_STL_CAPTION) {
559 return GetString(STR_STATION_LIST_CAPTION, this->window_number, this->vscroll->GetCount());
560 }
561
562 if (widget == WID_STL_CARGODROPDOWN) {
563 if (this->filter.cargoes == 0) return GetString(this->filter.include_no_rating ? STR_STATION_LIST_CARGO_FILTER_ONLY_NO_RATING : STR_STATION_LIST_CARGO_FILTER_NO_CARGO_TYPES);
564 if (this->filter.cargoes == _cargo_mask) return GetString(this->filter.include_no_rating ? STR_STATION_LIST_CARGO_FILTER_ALL_AND_NO_RATING : STR_CARGO_TYPE_FILTER_ALL);
565 if (CountBits(this->filter.cargoes) == 1 && !this->filter.include_no_rating) return GetString(CargoSpec::Get(FindFirstBit(this->filter.cargoes))->name);
566 return GetString(STR_STATION_LIST_CARGO_FILTER_MULTIPLE);
567 }
568
569 return this->Window::GetWidgetString(widget, stringid);
570 }
571
572 DropDownList BuildCargoDropDownList(bool expanded) const
573 {
574 /* Define a custom item consisting of check mark, count string, icon and name string. */
576
577 DropDownList list;
578 list.push_back(MakeDropDownListStringItem(STR_STATION_LIST_CARGO_FILTER_SELECT_ALL, CargoFilterCriteria::CF_SELECT_ALL));
579 list.push_back(MakeDropDownListDividerItem());
580
581 bool any_hidden = false;
582
583 uint16_t count = this->stations_per_cargo_type_no_rating;
584 if (count == 0 && !expanded) {
585 any_hidden = true;
586 } else {
587 list.push_back(std::make_unique<DropDownString<DropDownListCheckedItem, FS_SMALL, true>>(fmt::format("{}", count), 0, this->filter.include_no_rating, GetString(STR_STATION_LIST_CARGO_FILTER_NO_RATING), CargoFilterCriteria::CF_NO_RATING, false, count == 0));
588 }
589
590 Dimension d = GetLargestCargoIconSize();
591 for (const CargoSpec *cs : _sorted_cargo_specs) {
592 count = this->stations_per_cargo_type[cs->Index()];
593 if (count == 0 && !expanded) {
594 any_hidden = true;
595 } else {
596 list.push_back(std::make_unique<DropDownListCargoItem>(HasBit(this->filter.cargoes, cs->Index()), fmt::format("{}", count), d, cs->GetCargoIcon(), PAL_NONE, GetString(cs->name), cs->Index(), false, count == 0));
597 }
598 }
599
600 if (!expanded && any_hidden) {
601 if (list.size() > 2) list.push_back(MakeDropDownListDividerItem());
602 list.push_back(MakeDropDownListStringItem(STR_STATION_LIST_CARGO_FILTER_EXPAND, CargoFilterCriteria::CF_EXPAND_LIST));
603 }
604
605 return list;
606 }
607
608 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
609 {
610 switch (widget) {
611 case WID_STL_LIST: {
612 auto it = this->vscroll->GetScrolledItemFromWidget(this->stations, pt.y, this, WID_STL_LIST, WidgetDimensions::scaled.framerect.top);
613 if (it == this->stations.end()) return; // click out of list bound
614
615 const Station *st = *it;
616 /* do not check HasStationInUse - it is slow and may be invalid */
617 assert(st->owner == this->window_number || st->owner == OWNER_NONE);
618
619 if (_ctrl_pressed) {
621 } else {
623 }
624 break;
625 }
626
627 case WID_STL_TRAIN:
628 case WID_STL_TRUCK:
629 case WID_STL_BUS:
630 case WID_STL_AIRPLANE:
631 case WID_STL_SHIP:
632 if (_ctrl_pressed) {
633 this->filter.facilities.Flip(static_cast<StationFacility>(widget - WID_STL_TRAIN));
634 this->ToggleWidgetLoweredState(widget);
635 } else {
636 for (StationFacility i : this->filter.facilities) {
638 }
639 this->filter.facilities = {};
640 this->filter.facilities.Set(static_cast<StationFacility>(widget - WID_STL_TRAIN));
641 this->LowerWidget(widget);
642 }
643 this->stations.ForceRebuild();
644 this->SetDirty();
645 break;
646
647 case WID_STL_FACILALL:
648 for (WidgetID i = WID_STL_TRAIN; i <= WID_STL_SHIP; i++) {
649 this->LowerWidget(i);
650 }
651
653 this->stations.ForceRebuild();
654 this->SetDirty();
655 break;
656
657 case WID_STL_SORTBY: // flip sorting method asc/desc
658 this->stations.ToggleSortOrder();
659 this->SetDirty();
660 break;
661
662 case WID_STL_SORTDROPBTN: // select sorting criteria dropdown menu
664 break;
665
667 static std::string cargo_filter;
668 this->filter_expanded = false;
669 ShowDropDownList(this, this->BuildCargoDropDownList(this->filter_expanded), -1, widget, 0, {DropDownOption::Persist, DropDownOption::Filterable}, &cargo_filter);
670 break;
671 }
672 }
673 }
674
675 void OnDropdownSelect(WidgetID widget, int index, int) override
676 {
677 if (widget == WID_STL_SORTDROPBTN) {
678 if (this->stations.SortType() != index) {
679 this->stations.SetSortType(index);
680
681 /* Display the current sort variant */
683
684 this->SetDirty();
685 }
686 }
687
688 if (widget == WID_STL_CARGODROPDOWN) {
689 FilterState oldstate = this->filter;
690
691 if (index >= 0 && index < NUM_CARGO) {
692 if (_ctrl_pressed) {
693 ToggleBit(this->filter.cargoes, index);
694 } else {
695 this->filter.cargoes = 1ULL << index;
696 this->filter.include_no_rating = false;
697 }
698 } else if (index == CargoFilterCriteria::CF_NO_RATING) {
699 if (_ctrl_pressed) {
700 this->filter.include_no_rating = !this->filter.include_no_rating;
701 } else {
702 this->filter.include_no_rating = true;
703 this->filter.cargoes = 0;
704 }
705 } else if (index == CargoFilterCriteria::CF_SELECT_ALL) {
706 this->filter.cargoes = _cargo_mask;
707 this->filter.include_no_rating = true;
708 } else if (index == CargoFilterCriteria::CF_EXPAND_LIST) {
709 this->filter_expanded = true;
710 ReplaceDropDownList(this, this->BuildCargoDropDownList(this->filter_expanded));
711 return;
712 }
713
714 if (oldstate.cargoes != this->filter.cargoes || oldstate.include_no_rating != this->filter.include_no_rating) {
715 this->stations.ForceRebuild();
716 this->SetDirty();
717
718 /* Only refresh the list if it's changed. */
719 if (_ctrl_pressed) ReplaceDropDownList(this, this->BuildCargoDropDownList(this->filter_expanded));
720 }
721
722 /* Always close the list if ctrl is not pressed. */
724 }
725 }
726
727 void OnGameTick() override
728 {
729 if (this->stations.NeedResort()) {
730 Debug(misc, 3, "Periodic rebuild station list company {}", static_cast<int>(this->window_number));
731 this->SetDirty();
732 }
733 }
734
735 void OnResize() override
736 {
737 this->vscroll->SetCapacityFromWidget(this, WID_STL_LIST, WidgetDimensions::scaled.framerect.Vertical());
738 }
739
745 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
746 {
747 if (data == 0) {
748 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
749 this->stations.ForceRebuild();
750 } else {
751 this->stations.ForceResort();
752 }
753 }
754};
755
756/* Available station sorting functions */
757const std::initializer_list<GUIStationList::SortFunction * const> CompanyStationsWindow::sorter_funcs = {
764};
765
766static constexpr std::initializer_list<NWidgetPart> _nested_company_stations_widgets = {
768 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
769 NWidget(WWT_CAPTION, COLOUR_GREY, WID_STL_CAPTION),
770 NWidget(WWT_SHADEBOX, COLOUR_GREY),
771 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
772 NWidget(WWT_STICKYBOX, COLOUR_GREY),
773 EndContainer(),
775 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),
776 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),
777 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),
778 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),
779 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),
780 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),
781 NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(5, 0), SetFill(0, 1), EndContainer(),
782 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_STL_CARGODROPDOWN), SetFill(1, 0), SetToolTip(STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE_TOOLTIP),
783 NWidget(WWT_PANEL, COLOUR_GREY), SetResize(1, 0), SetFill(1, 1), EndContainer(),
784 EndContainer(),
786 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_STL_SORTBY), SetMinimalSize(81, 12), SetStringTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
787 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_STL_SORTDROPBTN), SetMinimalSize(163, 12), SetStringTip(STR_SORT_BY_NAME, STR_TOOLTIP_SORT_CRITERIA), // widget_data gets overwritten.
788 NWidget(WWT_PANEL, COLOUR_GREY), SetResize(1, 0), SetFill(1, 1), EndContainer(),
789 EndContainer(),
791 NWidget(WWT_PANEL, COLOUR_GREY, WID_STL_LIST), SetMinimalSize(346, 125), SetResize(1, 10), SetToolTip(STR_STATION_LIST_TOOLTIP), SetScrollbar(WID_STL_SCROLLBAR), EndContainer(),
794 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
795 EndContainer(),
796 EndContainer(),
797};
798
799static WindowDesc _company_stations_desc(
800 WDP_AUTO, "list_stations", 358, 162,
802 {},
803 _nested_company_stations_widgets
804);
805
811void ShowCompanyStations(CompanyID company)
812{
813 if (!Company::IsValidID(company)) return;
814
815 AllocateWindowDescFront<CompanyStationsWindow>(_company_stations_desc, company);
816}
817
818static constexpr std::initializer_list<NWidgetPart> _nested_station_view_widgets = {
820 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
821 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_SV_RENAME), SetAspect(WidgetDimensions::ASPECT_RENAME), SetSpriteTip(SPR_RENAME, STR_STATION_VIEW_EDIT_TOOLTIP),
822 NWidget(WWT_CAPTION, COLOUR_GREY, WID_SV_CAPTION),
823 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_SV_LOCATION), SetAspect(WidgetDimensions::ASPECT_LOCATION), SetSpriteTip(SPR_GOTO_LOCATION, STR_STATION_VIEW_CENTER_TOOLTIP),
824 NWidget(WWT_SHADEBOX, COLOUR_GREY),
825 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
826 NWidget(WWT_STICKYBOX, COLOUR_GREY),
827 EndContainer(),
829 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SV_GROUP), SetMinimalSize(81, 12), SetFill(1, 1), SetStringTip(STR_STATION_VIEW_GROUP),
830 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_SV_GROUP_BY), SetMinimalSize(168, 12), SetResize(1, 0), SetFill(0, 1), SetToolTip(STR_TOOLTIP_GROUP_ORDER),
831 EndContainer(),
833 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_SORT_ORDER), SetMinimalSize(81, 12), SetFill(1, 1), SetStringTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
834 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_SV_SORT_BY), SetMinimalSize(168, 12), SetResize(1, 0), SetFill(0, 1), SetToolTip(STR_TOOLTIP_SORT_CRITERIA),
835 EndContainer(),
839 EndContainer(),
842 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_ACCEPTS_RATINGS), SetMinimalSize(46, 12), SetResize(1, 0), SetFill(1, 1),
843 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 EndContainer(),
848 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SV_CATCHMENT), SetMinimalSize(45, 12), SetResize(1, 0), SetFill(1, 1), SetStringTip(STR_BUTTON_CATCHMENT, STR_TOOLTIP_CATCHMENT),
849 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),
850 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),
851 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),
852 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),
853 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
854 EndContainer(),
855};
856
857enum SortOrder : uint8_t {
858 SO_DESCENDING,
859 SO_ASCENDING
860};
861
862class CargoDataEntry;
863
872
873class CargoSorter {
874public:
875 using is_transparent = void;
876 CargoSorter(CargoSortType t = CargoSortType::StationID, SortOrder o = SO_ASCENDING) : type(t), order(o) {}
877 CargoSortType GetSortType() {return this->type;}
878 bool operator()(const CargoDataEntry &cd1, const CargoDataEntry &cd2) const;
879 bool operator()(const CargoDataEntry &cd1, const std::unique_ptr<CargoDataEntry> &cd2) const { return this->operator()(cd1, *cd2); }
880 bool operator()(const std::unique_ptr<CargoDataEntry> &cd1, const CargoDataEntry &cd2) const { return this->operator()(*cd1, cd2); }
881 bool operator()(const std::unique_ptr<CargoDataEntry> &cd1, const std::unique_ptr<CargoDataEntry> &cd2) const { return this->operator()(*cd1, *cd2); }
882
883private:
884 CargoSortType type;
885 SortOrder order;
886
887 template <class Tid>
888 bool SortId(Tid st1, Tid st2) const;
889 bool SortCount(const CargoDataEntry &cd1, const CargoDataEntry &cd2) const;
890 bool SortStation(StationID st1, StationID st2) const;
891};
892
893typedef std::set<std::unique_ptr<CargoDataEntry>, CargoSorter> CargoDataSet;
894
900class CargoDataEntry {
901public:
902 CargoDataEntry();
904
910 CargoDataEntry &InsertOrRetrieve(StationID station)
911 {
912 return this->InsertOrRetrieve<StationID>(station);
913 }
914
921 {
922 return this->InsertOrRetrieve<CargoType>(cargo);
923 }
924
925 void Update(uint count);
926
931 void Remove(StationID station)
932 {
933 CargoDataEntry t(station);
934 this->Remove(t);
935 }
936
942 {
943 CargoDataEntry t(cargo);
944 this->Remove(t);
945 }
946
952 CargoDataEntry *Retrieve(StationID station) const
953 {
954 CargoDataEntry t(station);
955 return this->Retrieve(this->children->find(t));
956 }
957
963 CargoDataEntry *Retrieve(CargoType cargo) const
964 {
965 CargoDataEntry t(cargo);
966 return this->Retrieve(this->children->find(t));
967 }
968
969 void Resort(CargoSortType type, SortOrder order);
970
975 StationID GetStation() const { return this->station; }
976
981 CargoType GetCargo() const { return this->cargo; }
982
987 uint GetCount() const { return this->count; }
988
993 CargoDataEntry *GetParent() const { return this->parent; }
994
999 uint GetNumChildren() const { return this->num_children; }
1000
1005 CargoDataSet::iterator Begin() const { return this->children->begin(); }
1006
1011 CargoDataSet::iterator End() const { return this->children->end(); }
1012
1017 bool HasTransfers() const { return this->transfers; }
1018
1023 void SetTransfers(bool value) { this->transfers = value; }
1024
1025 void Clear();
1026
1029 CargoDataEntry(StationID station);
1031
1032private:
1033 CargoDataEntry *Retrieve(CargoDataSet::iterator i) const;
1034
1035 template <class Tid>
1037
1038 void Remove(CargoDataEntry &entry);
1039 void IncrementSize();
1040
1041 CargoDataEntry *parent;
1042 const union {
1043 StationID station;
1044 struct {
1047 };
1048 };
1050 uint count;
1051 std::unique_ptr<CargoDataSet> children;
1052};
1053
1054CargoDataEntry::CargoDataEntry() :
1055 parent(nullptr),
1056 station(StationID::Invalid()),
1057 num_children(0),
1058 count(0),
1059 children(std::make_unique<CargoDataSet>(CargoSorter(CargoSortType::CargoType)))
1060{}
1061
1062CargoDataEntry::CargoDataEntry(CargoType cargo, uint count, CargoDataEntry *parent) :
1063 parent(parent),
1064 cargo(cargo),
1065 num_children(0),
1066 count(count),
1067 children(std::make_unique<CargoDataSet>())
1068{}
1069
1070CargoDataEntry::CargoDataEntry(StationID station, uint count, CargoDataEntry *parent) :
1071 parent(parent),
1072 station(station),
1073 num_children(0),
1074 count(count),
1075 children(std::make_unique<CargoDataSet>())
1076{}
1077
1078CargoDataEntry::CargoDataEntry(StationID station) :
1079 parent(nullptr),
1080 station(station),
1081 num_children(0),
1082 count(0),
1083 children(nullptr)
1084{}
1085
1086CargoDataEntry::CargoDataEntry(CargoType cargo) :
1087 parent(nullptr),
1088 cargo(cargo),
1089 num_children(0),
1090 count(0),
1091 children(nullptr)
1092{}
1093
1096{
1097 this->Clear();
1098}
1099
1104{
1105 if (this->children != nullptr) this->children->clear();
1106 if (this->parent != nullptr) this->parent->count -= this->count;
1107 this->count = 0;
1108 this->num_children = 0;
1109}
1110
1117void CargoDataEntry::Remove(CargoDataEntry &entry)
1118{
1119 CargoDataSet::iterator i = this->children->find(entry);
1120 if (i != this->children->end()) this->children->erase(i);
1121}
1122
1129template <class Tid>
1130CargoDataEntry &CargoDataEntry::InsertOrRetrieve(Tid child_id)
1131{
1132 CargoDataEntry tmp(child_id);
1133 CargoDataSet::iterator i = this->children->find(tmp);
1134 if (i == this->children->end()) {
1135 IncrementSize();
1136 return **(this->children->insert(std::make_unique<CargoDataEntry>(child_id, 0, this)).first);
1137 } else {
1138 assert(this->children->value_comp().GetSortType() != CargoSortType::Count);
1139 return **i;
1140 }
1141}
1142
1149{
1150 this->count += count;
1151 if (this->parent != nullptr) this->parent->Update(count);
1152}
1153
1158{
1159 ++this->num_children;
1160 if (this->parent != nullptr) this->parent->IncrementSize();
1161}
1162
1163void CargoDataEntry::Resort(CargoSortType type, SortOrder order)
1164{
1165 auto new_children = std::make_unique<CargoDataSet>(CargoSorter(type, order));
1166 new_children->merge(*this->children);
1167 this->children = std::move(new_children);
1168}
1169
1170CargoDataEntry *CargoDataEntry::Retrieve(CargoDataSet::iterator i) const
1171{
1172 if (i == this->children->end()) {
1173 return nullptr;
1174 } else {
1175 assert(this->children->value_comp().GetSortType() != CargoSortType::Count);
1176 return i->get();
1177 }
1178}
1179
1180bool CargoSorter::operator()(const CargoDataEntry &cd1, const CargoDataEntry &cd2) const
1181{
1182 switch (this->type) {
1184 return this->SortId<StationID>(cd1.GetStation(), cd2.GetStation());
1186 return this->SortId<CargoType>(cd1.GetCargo(), cd2.GetCargo());
1188 return this->SortCount(cd1, cd2);
1190 return this->SortStation(cd1.GetStation(), cd2.GetStation());
1191 default:
1192 NOT_REACHED();
1193 }
1194}
1195
1196template <class Tid>
1197bool CargoSorter::SortId(Tid st1, Tid st2) const
1198{
1199 return (this->order == SO_ASCENDING) ? st1 < st2 : st2 < st1;
1200}
1201
1202bool CargoSorter::SortCount(const CargoDataEntry &cd1, const CargoDataEntry &cd2) const
1203{
1204 uint c1 = cd1.GetCount();
1205 uint c2 = cd2.GetCount();
1206 if (c1 == c2) {
1207 return this->SortStation(cd1.GetStation(), cd2.GetStation());
1208 } else if (this->order == SO_ASCENDING) {
1209 return c1 < c2;
1210 } else {
1211 return c2 < c1;
1212 }
1213}
1214
1215bool CargoSorter::SortStation(StationID st1, StationID st2) const
1216{
1217 if (!Station::IsValidID(st1)) {
1218 return Station::IsValidID(st2) ? this->order == SO_ASCENDING : this->SortId(st1, st2);
1219 } else if (!Station::IsValidID(st2)) {
1220 return order == SO_DESCENDING;
1221 }
1222
1223 int res = StrNaturalCompare(Station::Get(st1)->GetCachedName(), Station::Get(st2)->GetCachedName()); // Sort by name (natural sorting).
1224 if (res == 0) {
1225 return this->SortId(st1, st2);
1226 } else {
1227 return (this->order == SO_ASCENDING) ? res < 0 : res > 0;
1228 }
1229}
1230
1234struct StationViewWindow : public Window {
1238 struct RowDisplay {
1239 RowDisplay(CargoDataEntry *f, StationID n) : filter(f), next_station(n) {}
1240 RowDisplay(CargoDataEntry *f, CargoType n) : filter(f), next_cargo(n) {}
1241
1246 union {
1250 StationID next_station;
1251
1256 };
1257 };
1258
1259 typedef std::vector<RowDisplay> CargoDataVector;
1260
1261 static const int NUM_COLUMNS = 4;
1262
1266 enum Invalidation : uint16_t {
1267 INV_FLOWS = 0x100,
1268 INV_CARGO = 0x200
1269 };
1270
1280
1284 enum Mode : uint8_t {
1287 };
1288
1292 int line_height = 0;
1294 Scrollbar *vscroll = nullptr;
1295
1296 /* Height of the #WID_SV_ACCEPT_RATING_LIST widget for different views. */
1297 static constexpr uint RATING_LINES = 13;
1298 static constexpr uint ACCEPTS_LINES = 3;
1299
1301 static inline const StringID sort_names[] = {
1302 STR_STATION_VIEW_WAITING_STATION,
1303 STR_STATION_VIEW_WAITING_AMOUNT,
1304 STR_STATION_VIEW_PLANNED_STATION,
1305 STR_STATION_VIEW_PLANNED_AMOUNT,
1306 };
1307
1308 static inline const StringID group_names[] = {
1309 STR_STATION_VIEW_GROUP_S_V_D,
1310 STR_STATION_VIEW_GROUP_S_D_V,
1311 STR_STATION_VIEW_GROUP_V_S_D,
1312 STR_STATION_VIEW_GROUP_V_D_S,
1313 STR_STATION_VIEW_GROUP_D_S_V,
1314 STR_STATION_VIEW_GROUP_D_V_S,
1315 };
1316
1323 std::array<CargoSortType, NUM_COLUMNS> sortings{};
1324
1326 std::array<SortOrder, NUM_COLUMNS> sort_orders{};
1327
1328 int scroll_to_row = INT_MAX;
1331 std::array<Grouping, NUM_COLUMNS> groupings;
1332
1335 CargoDataVector displayed_rows{};
1336
1338 {
1339 this->CreateNestedTree();
1341 this->vscroll = this->GetScrollbar(WID_SV_SCROLLBAR);
1342 /* Nested widget tree creation is done in two steps to ensure that this->GetWidget<NWidgetCore>(WID_SV_ACCEPTS_RATINGS) exists in UpdateWidgetSize(). */
1343 this->FinishInitNested(window_number);
1344
1345 this->groupings[0] = GR_CARGO;
1346 this->sortings[0] = CargoSortType::AsGrouping;
1347 this->SelectGroupBy(_settings_client.gui.station_gui_group_order);
1348 this->SelectSortBy(_settings_client.gui.station_gui_sort_by);
1349 this->sort_orders[0] = SO_ASCENDING;
1350 this->SelectSortOrder((SortOrder)_settings_client.gui.station_gui_sort_order);
1351 this->owner = Station::Get(window_number)->owner;
1352 }
1353
1354 void OnInit() override
1355 {
1356 this->cargo_icon_size = GetLargestCargoIconSize();
1357 this->line_height = std::max<int>(GetCharacterHeight(FS_NORMAL), this->cargo_icon_size.height);
1358 this->expand_shrink_width = std::max(GetCharacterWidth(FS_NORMAL, '-'), GetCharacterWidth(FS_NORMAL, '+'));
1359 }
1360
1361 void Close([[maybe_unused]] int data = 0) override
1362 {
1363 CloseWindowById(WC_TRAINS_LIST, VehicleListIdentifier(VL_STATION_LIST, VEH_TRAIN, this->owner, this->window_number).ToWindowNumber(), false);
1364 CloseWindowById(WC_ROADVEH_LIST, VehicleListIdentifier(VL_STATION_LIST, VEH_ROAD, this->owner, this->window_number).ToWindowNumber(), false);
1365 CloseWindowById(WC_SHIPS_LIST, VehicleListIdentifier(VL_STATION_LIST, VEH_SHIP, this->owner, this->window_number).ToWindowNumber(), false);
1366 CloseWindowById(WC_AIRCRAFT_LIST, VehicleListIdentifier(VL_STATION_LIST, VEH_AIRCRAFT, this->owner, this->window_number).ToWindowNumber(), false);
1367
1368 SetViewportCatchmentStation(Station::Get(this->window_number), false);
1369 this->Window::Close();
1370 }
1371
1382 void ShowCargo(CargoDataEntry *data, CargoType cargo, StationID source, StationID next, StationID dest, uint count)
1383 {
1384 if (count == 0) return;
1385 bool auto_distributed = _settings_game.linkgraph.GetDistributionType(cargo) != DistributionType::Manual;
1386 const CargoDataEntry *expand = &this->expanded_rows;
1387 for (int i = 0; i < NUM_COLUMNS && expand != nullptr; ++i) {
1388 switch (groupings[i]) {
1389 case GR_CARGO:
1390 assert(i == 0);
1391 data = &data->InsertOrRetrieve(cargo);
1392 data->SetTransfers(source != this->window_number);
1393 expand = expand->Retrieve(cargo);
1394 break;
1395 case GR_SOURCE:
1396 if (auto_distributed || source != this->window_number) {
1397 data = &data->InsertOrRetrieve(source);
1398 expand = expand->Retrieve(source);
1399 }
1400 break;
1401 case GR_NEXT:
1402 if (auto_distributed) {
1403 data = &data->InsertOrRetrieve(next);
1404 expand = expand->Retrieve(next);
1405 }
1406 break;
1407 case GR_DESTINATION:
1408 if (auto_distributed) {
1409 data = &data->InsertOrRetrieve(dest);
1410 expand = expand->Retrieve(dest);
1411 }
1412 break;
1413 }
1414 }
1415 data->Update(count);
1416 }
1417
1418 void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override
1419 {
1420 switch (widget) {
1421 case WID_SV_WAITING:
1422 fill.height = resize.height = this->line_height;
1423 size.height = 4 * resize.height + padding.height;
1424 break;
1425
1427 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;
1428 break;
1429 }
1430 }
1431
1432 void OnPaint() override
1433 {
1434 const Station *st = Station::Get(this->window_number);
1435 CargoDataEntry cargo;
1436 BuildCargoList(&cargo, st);
1437
1438 this->vscroll->SetCount(cargo.GetNumChildren()); // update scrollbar
1439
1440 /* disable some buttons */
1446 this->SetWidgetDisabledState(WID_SV_CLOSE_AIRPORT, !st->facilities.Test(StationFacility::Airport) || st->owner != _local_company || st->owner == OWNER_NONE); // Also consider SE, where _local_company == OWNER_NONE
1448
1452
1453 this->DrawWidgets();
1454
1455 if (!this->IsShaded()) {
1456 /* Draw 'accepted cargo' or 'cargo ratings'. */
1458 const Rect r = wid->GetCurrentRect();
1459 if (this->GetWidget<NWidgetCore>(WID_SV_ACCEPTS_RATINGS)->GetString() == STR_STATION_VIEW_RATINGS_BUTTON) {
1460 int lines = this->DrawAcceptedCargo(r);
1461 if (lines > this->accepts_lines) { // Resize the widget, and perform re-initialization of the window.
1462 this->accepts_lines = lines;
1463 this->ReInit();
1464 return;
1465 }
1466 } else {
1467 int lines = this->DrawCargoRatings(r);
1468 if (lines > this->rating_lines) { // Resize the widget, and perform re-initialization of the window.
1469 this->rating_lines = lines;
1470 this->ReInit();
1471 return;
1472 }
1473 }
1474
1475 /* Draw arrow pointing up/down for ascending/descending sorting */
1476 this->DrawSortButtonState(WID_SV_SORT_ORDER, sort_orders[1] == SO_ASCENDING ? SBS_UP : SBS_DOWN);
1477
1478 int pos = this->vscroll->GetPosition();
1479
1480 int maxrows = this->vscroll->GetCapacity();
1481
1482 displayed_rows.clear();
1483
1484 /* Draw waiting cargo. */
1486 Rect waiting_rect = nwi->GetCurrentRect().Shrink(WidgetDimensions::scaled.framerect);
1487 this->DrawEntries(cargo, waiting_rect, pos, maxrows, 0);
1488 scroll_to_row = INT_MAX;
1489 }
1490 }
1491
1492 std::string GetWidgetString(WidgetID widget, StringID stringid) const override
1493 {
1494 if (widget == WID_SV_CAPTION) {
1495 const Station *st = Station::Get(this->window_number);
1496 return GetString(STR_STATION_VIEW_CAPTION, st->index, st->facilities);
1497 }
1498
1499 return this->Window::GetWidgetString(widget, stringid);
1500 }
1501
1508 {
1509 const Station *st = Station::Get(this->window_number);
1510 CargoDataEntry &entry = cached_destinations.InsertOrRetrieve(cargo);
1511 entry.Clear();
1512
1513 if (!st->goods[cargo].HasData()) return;
1514
1515 for (const auto &it : st->goods[cargo].GetData().flows) {
1516 StationID from = it.first;
1517 CargoDataEntry &source_entry = entry.InsertOrRetrieve(from);
1518 uint32_t prev_count = 0;
1519 for (const auto &flow_it : *it.second.GetShares()) {
1520 StationID via = flow_it.second;
1521 CargoDataEntry &via_entry = source_entry.InsertOrRetrieve(via);
1522 if (via == this->window_number) {
1523 via_entry.InsertOrRetrieve(via).Update(flow_it.first - prev_count);
1524 } else {
1525 EstimateDestinations(cargo, from, via, flow_it.first - prev_count, via_entry);
1526 }
1527 prev_count = flow_it.first;
1528 }
1529 }
1530 }
1531
1541 void EstimateDestinations(CargoType cargo, StationID source, StationID next, uint count, CargoDataEntry &dest)
1542 {
1543 if (Station::IsValidID(next) && Station::IsValidID(source)) {
1544 GoodsEntry &ge = Station::Get(next)->goods[cargo];
1545 if (!ge.HasData()) return;
1546
1547 CargoDataEntry tmp;
1548 const FlowStatMap &flowmap = ge.GetData().flows;
1549 FlowStatMap::const_iterator map_it = flowmap.find(source);
1550 if (map_it != flowmap.end()) {
1551 const FlowStat::SharesMap *shares = map_it->second.GetShares();
1552 uint32_t prev_count = 0;
1553 for (FlowStat::SharesMap::const_iterator i = shares->begin(); i != shares->end(); ++i) {
1554 tmp.InsertOrRetrieve(i->second).Update(i->first - prev_count);
1555 prev_count = i->first;
1556 }
1557 }
1558
1559 if (tmp.GetCount() == 0) {
1560 dest.InsertOrRetrieve(StationID::Invalid()).Update(count);
1561 } else {
1562 uint sum_estimated = 0;
1563 while (sum_estimated < count) {
1564 for (CargoDataSet::iterator i = tmp.Begin(); i != tmp.End() && sum_estimated < count; ++i) {
1565 CargoDataEntry &child = **i;
1566 uint estimate = DivideApprox(child.GetCount() * count, tmp.GetCount());
1567 if (estimate == 0) estimate = 1;
1568
1569 sum_estimated += estimate;
1570 if (sum_estimated > count) {
1571 estimate -= sum_estimated - count;
1572 sum_estimated = count;
1573 }
1574
1575 if (estimate > 0) {
1576 if (child.GetStation() == next) {
1577 dest.InsertOrRetrieve(next).Update(estimate);
1578 } else {
1579 EstimateDestinations(cargo, source, child.GetStation(), estimate, dest);
1580 }
1581 }
1582 }
1583
1584 }
1585 }
1586 } else {
1587 dest.InsertOrRetrieve(StationID::Invalid()).Update(count);
1588 }
1589 }
1590
1597 void BuildFlowList(CargoType cargo, const FlowStatMap &flows, CargoDataEntry *entry)
1598 {
1599 const CargoDataEntry *source_dest = this->cached_destinations.Retrieve(cargo);
1600 for (FlowStatMap::const_iterator it = flows.begin(); it != flows.end(); ++it) {
1601 StationID from = it->first;
1602 const CargoDataEntry *source_entry = source_dest->Retrieve(from);
1603 const FlowStat::SharesMap *shares = it->second.GetShares();
1604 for (FlowStat::SharesMap::const_iterator flow_it = shares->begin(); flow_it != shares->end(); ++flow_it) {
1605 const CargoDataEntry *via_entry = source_entry->Retrieve(flow_it->second);
1606 for (CargoDataSet::iterator dest_it = via_entry->Begin(); dest_it != via_entry->End(); ++dest_it) {
1607 CargoDataEntry &dest_entry = **dest_it;
1608 ShowCargo(entry, cargo, from, flow_it->second, dest_entry.GetStation(), dest_entry.GetCount());
1609 }
1610 }
1611 }
1612 }
1613
1620 void BuildCargoList(CargoType cargo, const StationCargoList &packets, CargoDataEntry *entry)
1621 {
1622 const CargoDataEntry *source_dest = this->cached_destinations.Retrieve(cargo);
1623 for (StationCargoList::ConstIterator it = packets.Packets()->begin(); it != packets.Packets()->end(); it++) {
1624 const CargoPacket *cp = *it;
1625 StationID next = it.GetKey();
1626
1627 const CargoDataEntry *source_entry = source_dest->Retrieve(cp->GetFirstStation());
1628 if (source_entry == nullptr) {
1629 this->ShowCargo(entry, cargo, cp->GetFirstStation(), next, StationID::Invalid(), cp->Count());
1630 continue;
1631 }
1632
1633 const CargoDataEntry *via_entry = source_entry->Retrieve(next);
1634 if (via_entry == nullptr) {
1635 this->ShowCargo(entry, cargo, cp->GetFirstStation(), next, StationID::Invalid(), cp->Count());
1636 continue;
1637 }
1638
1639 uint remaining = cp->Count();
1640 for (CargoDataSet::iterator dest_it = via_entry->Begin(); dest_it != via_entry->End();) {
1641 CargoDataEntry &dest_entry = **dest_it;
1642
1643 /* Advance iterator here instead of in the for statement to test whether this is the last entry */
1644 ++dest_it;
1645
1646 uint val;
1647 if (dest_it == via_entry->End()) {
1648 /* Allocate all remaining waiting cargo to the last destination to avoid
1649 * waiting cargo being "lost", and the displayed total waiting cargo
1650 * not matching GoodsEntry::TotalCount() */
1651 val = remaining;
1652 } else {
1653 val = std::min<uint>(remaining, DivideApprox(cp->Count() * dest_entry.GetCount(), via_entry->GetCount()));
1654 remaining -= val;
1655 }
1656 this->ShowCargo(entry, cargo, cp->GetFirstStation(), next, dest_entry.GetStation(), val);
1657 }
1658 }
1659 this->ShowCargo(entry, cargo, NEW_STATION, NEW_STATION, NEW_STATION, packets.ReservedCount());
1660 }
1661
1667 void BuildCargoList(CargoDataEntry *entry, const Station *st)
1668 {
1669 for (CargoType cargo = 0; cargo < NUM_CARGO; ++cargo) {
1670
1671 if (this->cached_destinations.Retrieve(cargo) == nullptr) {
1672 this->RecalcDestinations(cargo);
1673 }
1674
1675 const GoodsEntry &ge = st->goods[cargo];
1676 if (!ge.HasData()) continue;
1677
1678 if (this->current_mode == MODE_WAITING) {
1679 this->BuildCargoList(cargo, ge.GetData().cargo, entry);
1680 } else {
1681 this->BuildFlowList(cargo, ge.GetData().flows, entry);
1682 }
1683 }
1684 }
1685
1691 {
1692 std::list<StationID> stations;
1693 const CargoDataEntry *parent = entry.GetParent();
1694 if (parent->GetParent() == nullptr) {
1695 this->displayed_rows.push_back(RowDisplay(&this->expanded_rows, entry.GetCargo()));
1696 return;
1697 }
1698
1699 StationID next = entry.GetStation();
1700 while (parent->GetParent()->GetParent() != nullptr) {
1701 stations.push_back(parent->GetStation());
1702 parent = parent->GetParent();
1703 }
1704
1705 CargoType cargo = parent->GetCargo();
1706 CargoDataEntry *filter = this->expanded_rows.Retrieve(cargo);
1707 while (!stations.empty()) {
1708 filter = filter->Retrieve(stations.back());
1709 stations.pop_back();
1710 }
1711
1712 this->displayed_rows.push_back(RowDisplay(filter, next));
1713 }
1714
1723 StringID GetEntryString(StationID station, StringID here, StringID other_station, StringID any) const
1724 {
1725 if (station == this->window_number) {
1726 return here;
1727 } else if (station == StationID::Invalid()) {
1728 return any;
1729 } else if (station == NEW_STATION) {
1730 return STR_STATION_VIEW_RESERVED;
1731 } else {
1732 return other_station;
1733 }
1734 }
1735
1736 StringID GetGroupingString(Grouping grouping, StationID station) const
1737 {
1738 switch (grouping) {
1739 case GR_SOURCE: return this->GetEntryString(station, STR_STATION_VIEW_FROM_HERE, STR_STATION_VIEW_FROM, STR_STATION_VIEW_FROM_ANY);
1740 case GR_NEXT: return this->GetEntryString(station, STR_STATION_VIEW_VIA_HERE, STR_STATION_VIEW_VIA, STR_STATION_VIEW_VIA_ANY);
1741 case GR_DESTINATION: return this->GetEntryString(station, STR_STATION_VIEW_TO_HERE, STR_STATION_VIEW_TO, STR_STATION_VIEW_TO_ANY);
1742 default: NOT_REACHED();
1743 }
1744 }
1745
1753 StringID SearchNonStop(CargoDataEntry &cd, StationID station, int column)
1754 {
1755 assert(column < NUM_COLUMNS);
1757 for (int i = column - 1; i > 0; --i) {
1758 if (this->groupings[i] == GR_DESTINATION) {
1759 if (parent->GetStation() == station) {
1760 return STR_STATION_VIEW_NONSTOP;
1761 } else {
1762 return STR_STATION_VIEW_VIA;
1763 }
1764 }
1765 parent = parent->GetParent();
1766 }
1767
1768 if (column < NUM_COLUMNS - 1 && this->groupings[column + 1] == GR_DESTINATION) {
1769 CargoDataSet::iterator begin = cd.Begin();
1770 CargoDataSet::iterator end = cd.End();
1771 if (begin != end && ++(cd.Begin()) == end && (*(begin))->GetStation() == station) {
1772 return STR_STATION_VIEW_NONSTOP;
1773 } else {
1774 return STR_STATION_VIEW_VIA;
1775 }
1776 }
1777
1778 return STR_STATION_VIEW_VIA;
1779 }
1780
1787 void DrawCargoIcons(CargoType cargo, uint waiting, const Rect &r) const
1788 {
1789 int width = ScaleSpriteTrad(10);
1790 uint num = std::min<uint>((waiting + (width / 2)) / width, r.Width() / width); // maximum is width / 10 icons so it won't overflow
1791 if (num == 0) return;
1792
1793 SpriteID sprite = CargoSpec::Get(cargo)->GetCargoIcon();
1794
1795 int x = _current_text_dir == TD_RTL ? r.left : r.right - num * width;
1796 int y = CentreBounds(r.top, r.bottom, this->cargo_icon_size.height);
1797 do {
1798 DrawSprite(sprite, PAL_NONE, x, y);
1799 x += width;
1800 } while (--num);
1801 }
1802
1813 int DrawEntries(CargoDataEntry &entry, const Rect &r, int pos, int maxrows, int column, CargoType cargo = INVALID_CARGO)
1814 {
1815 assert(column < NUM_COLUMNS);
1816 if (this->sortings[column] == CargoSortType::AsGrouping) {
1817 if (this->groupings[column] != GR_CARGO) {
1818 entry.Resort(CargoSortType::StationString, this->sort_orders[column]);
1819 }
1820 } else {
1821 entry.Resort(CargoSortType::Count, this->sort_orders[column]);
1822 }
1823 int text_y_offset = (this->line_height - GetCharacterHeight(FS_NORMAL)) / 2;
1824 for (CargoDataSet::iterator i = entry.Begin(); i != entry.End(); ++i) {
1825 CargoDataEntry &cd = **i;
1826
1827 Grouping grouping = this->groupings[column];
1828 if (grouping == GR_CARGO) cargo = cd.GetCargo();
1829 bool auto_distributed = _settings_game.linkgraph.GetDistributionType(cargo) != DistributionType::Manual;
1830
1831 if (pos > -maxrows && pos <= 0) {
1832 StringID str = STR_EMPTY;
1833 StationID station = StationID::Invalid();
1834 int y = r.top - pos * this->line_height;
1835 if (this->groupings[column] == GR_CARGO) {
1836 str = STR_STATION_VIEW_WAITING_CARGO;
1837 this->DrawCargoIcons(cd.GetCargo(), cd.GetCount(), Rect(r.left + this->expand_shrink_width, y, r.right - this->expand_shrink_width, y + this->line_height - 1));
1838 } else {
1839 if (!auto_distributed) grouping = GR_SOURCE;
1840 station = cd.GetStation();
1841 str = this->GetGroupingString(grouping, station);
1842 if (grouping == GR_NEXT && str == STR_STATION_VIEW_VIA) str = this->SearchNonStop(cd, station, column);
1843
1844 if (pos == -this->scroll_to_row && Station::IsValidID(station)) {
1846 }
1847 }
1848
1849 bool rtl = _current_text_dir == TD_RTL;
1850 Rect text = r.Indent(column * WidgetDimensions::scaled.hsep_indent, rtl).Indent(this->expand_shrink_width, !rtl);
1851 Rect shrink = r.WithWidth(this->expand_shrink_width, !rtl);
1852
1853 DrawString(text.left, text.right, y + text_y_offset, GetString(str, cargo, cd.GetCount(), station));
1854
1855 if (column < NUM_COLUMNS - 1) {
1856 std::string_view sym;
1857 if (cd.GetNumChildren() > 0) {
1858 sym = "-";
1859 } else if (auto_distributed && str != STR_STATION_VIEW_RESERVED) {
1860 sym = "+";
1861 } else {
1862 /* Only draw '+' if there is something to be shown. */
1863 const GoodsEntry &ge = Station::Get(this->window_number)->goods[cargo];
1864 if (ge.HasData()) {
1865 const StationCargoList &cargo_list = ge.GetData().cargo;
1866 if (grouping == GR_CARGO && (cargo_list.ReservedCount() > 0 || cd.HasTransfers())) {
1867 sym = "+";
1868 }
1869 }
1870 }
1871 if (!sym.empty()) DrawString(shrink.left, shrink.right, y + text_y_offset, sym, TC_YELLOW);
1872 }
1873 this->SetDisplayedRow(cd);
1874 }
1875 --pos;
1876 if ((auto_distributed || column == 0) && column < NUM_COLUMNS - 1) {
1877 pos = this->DrawEntries(cd, r, pos, maxrows, column + 1, cargo);
1878 }
1879 }
1880 return pos;
1881 }
1882
1888 int DrawAcceptedCargo(const Rect &r) const
1889 {
1890 const Station *st = Station::Get(this->window_number);
1891 Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
1892
1893 int bottom = DrawStringMultiLine(tr.left, tr.right, tr.top, INT32_MAX, GetString(STR_STATION_VIEW_ACCEPTS_CARGO, GetAcceptanceMask(st)));
1894 return CeilDiv(bottom - r.top - WidgetDimensions::scaled.framerect.top, GetCharacterHeight(FS_NORMAL));
1895 }
1896
1902 int DrawCargoRatings(const Rect &r) const
1903 {
1904 const Station *st = Station::Get(this->window_number);
1905 bool rtl = _current_text_dir == TD_RTL;
1906 Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
1907
1908 if (st->town->exclusive_counter > 0) {
1909 tr.top = DrawStringMultiLine(tr, GetString(st->town->exclusivity == st->owner ? STR_STATION_VIEW_EXCLUSIVE_RIGHTS_SELF : STR_STATION_VIEW_EXCLUSIVE_RIGHTS_COMPANY, st->town->exclusivity));
1910 tr.top += WidgetDimensions::scaled.vsep_wide;
1911 }
1912
1913 DrawString(tr, TimerGameEconomy::UsingWallclockUnits() ? STR_STATION_VIEW_SUPPLY_RATINGS_TITLE_MINUTE : STR_STATION_VIEW_SUPPLY_RATINGS_TITLE_MONTH);
1914 tr.top += GetCharacterHeight(FS_NORMAL);
1915
1916 for (const CargoSpec *cs : _sorted_standard_cargo_specs) {
1917 const GoodsEntry *ge = &st->goods[cs->Index()];
1918 if (!ge->HasRating()) continue;
1919
1921 DrawString(tr.Indent(WidgetDimensions::scaled.hsep_indent, rtl),
1922 GetString(STR_STATION_VIEW_CARGO_SUPPLY_RATING,
1923 cs->name,
1924 lg != nullptr ? lg->Monthly((*lg)[ge->node].supply) : 0,
1925 STR_CARGO_RATING_APPALLING + (ge->rating >> 5),
1926 ToPercent8(ge->rating)));
1927 tr.top += GetCharacterHeight(FS_NORMAL);
1928 }
1929 return CeilDiv(tr.top - r.top - WidgetDimensions::scaled.framerect.top, GetCharacterHeight(FS_NORMAL));
1930 }
1931
1937 template <class Tid>
1939 {
1940 if (filter->Retrieve(next) != nullptr) {
1941 filter->Remove(next);
1942 } else {
1943 filter->InsertOrRetrieve(next);
1944 }
1945 }
1946
1952 {
1953 if (row < 0 || (uint)row >= this->displayed_rows.size()) return;
1954 if (_ctrl_pressed) {
1955 this->scroll_to_row = row;
1956 } else {
1957 RowDisplay &display = this->displayed_rows[row];
1958 if (display.filter == &this->expanded_rows) {
1960 } else {
1962 }
1963 }
1966 }
1967
1968 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
1969 {
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
1980 if (w != nullptr && this->IsWidgetLowered(WID_SV_CATCHMENT)) {
1983 }
1984 break;
1985
1986 case WID_SV_LOCATION:
1987 if (_ctrl_pressed) {
1988 ShowExtraViewportWindow(Station::Get(this->window_number)->xy);
1989 } else {
1990 ScrollMainWindowToTile(Station::Get(this->window_number)->xy);
1991 }
1992 break;
1993
1995 /* Swap between 'accepts' and 'ratings' view. */
1996 int height_change;
1998 if (this->GetWidget<NWidgetCore>(WID_SV_ACCEPTS_RATINGS)->GetString() == STR_STATION_VIEW_RATINGS_BUTTON) {
1999 nwi->SetStringTip(STR_STATION_VIEW_ACCEPTS_BUTTON, STR_STATION_VIEW_ACCEPTS_TOOLTIP); // Switch to accepts view.
2000 height_change = this->rating_lines - this->accepts_lines;
2001 } else {
2002 nwi->SetStringTip(STR_STATION_VIEW_RATINGS_BUTTON, STR_STATION_VIEW_RATINGS_TOOLTIP); // Switch to ratings view.
2003 height_change = this->accepts_lines - this->rating_lines;
2004 }
2005 this->ReInit(0, height_change * GetCharacterHeight(FS_NORMAL));
2006 break;
2007 }
2008
2009 case WID_SV_RENAME:
2010 ShowQueryString(GetString(STR_STATION_NAME, this->window_number), STR_STATION_VIEW_EDIT_STATION_SIGN, MAX_LENGTH_STATION_NAME_CHARS,
2012 break;
2013
2015 Command<Commands::OpenCloseAirport>::Post(this->window_number);
2016 break;
2017
2018 case WID_SV_TRAINS: // Show list of scheduled trains to this station
2019 case WID_SV_ROADVEHS: // Show list of scheduled road-vehicles to this station
2020 case WID_SV_SHIPS: // Show list of scheduled ships to this station
2021 case WID_SV_PLANES: { // Show list of scheduled aircraft to this station
2022 Owner owner = Station::Get(this->window_number)->owner;
2023 ShowVehicleListWindow(owner, (VehicleType)(widget - WID_SV_TRAINS), static_cast<StationID>(this->window_number));
2024 break;
2025 }
2026
2027 case WID_SV_SORT_BY: {
2028 /* The initial selection is composed of current mode and
2029 * sorting criteria for columns 1, 2, and 3. Column 0 is always
2030 * sorted by cargo type. The others can theoretically be sorted
2031 * by different things but there is no UI for that. */
2033 this->current_mode * 2 + (this->sortings[1] == CargoSortType::Count ? 1 : 0),
2034 WID_SV_SORT_BY, 0, 0);
2035 break;
2036 }
2037
2038 case WID_SV_GROUP_BY: {
2039 ShowDropDownMenu(this, StationViewWindow::group_names, this->grouping_index, WID_SV_GROUP_BY, 0, 0);
2040 break;
2041 }
2042
2043 case WID_SV_SORT_ORDER: { // flip sorting method asc/desc
2044 this->SelectSortOrder(this->sort_orders[1] == SO_ASCENDING ? SO_DESCENDING : SO_ASCENDING);
2045 this->SetTimeout();
2047 break;
2048 }
2049 }
2050 }
2051
2056 void SelectSortOrder(SortOrder order)
2057 {
2058 this->sort_orders[1] = this->sort_orders[2] = this->sort_orders[3] = order;
2059 _settings_client.gui.station_gui_sort_order = this->sort_orders[1];
2060 this->SetDirty();
2061 }
2062
2067 void SelectSortBy(int index)
2068 {
2069 _settings_client.gui.station_gui_sort_by = index;
2070 switch (StationViewWindow::sort_names[index]) {
2071 case STR_STATION_VIEW_WAITING_STATION:
2072 this->current_mode = MODE_WAITING;
2073 this->sortings[1] = this->sortings[2] = this->sortings[3] = CargoSortType::AsGrouping;
2074 break;
2075 case STR_STATION_VIEW_WAITING_AMOUNT:
2076 this->current_mode = MODE_WAITING;
2077 this->sortings[1] = this->sortings[2] = this->sortings[3] = CargoSortType::Count;
2078 break;
2079 case STR_STATION_VIEW_PLANNED_STATION:
2080 this->current_mode = MODE_PLANNED;
2081 this->sortings[1] = this->sortings[2] = this->sortings[3] = CargoSortType::AsGrouping;
2082 break;
2083 case STR_STATION_VIEW_PLANNED_AMOUNT:
2084 this->current_mode = MODE_PLANNED;
2085 this->sortings[1] = this->sortings[2] = this->sortings[3] = CargoSortType::Count;
2086 break;
2087 default:
2088 NOT_REACHED();
2089 }
2090 /* Display the current sort variant */
2092 this->SetDirty();
2093 }
2094
2099 void SelectGroupBy(int index)
2100 {
2101 this->grouping_index = index;
2102 _settings_client.gui.station_gui_group_order = index;
2104 switch (StationViewWindow::group_names[index]) {
2105 case STR_STATION_VIEW_GROUP_S_V_D:
2106 this->groupings[1] = GR_SOURCE;
2107 this->groupings[2] = GR_NEXT;
2108 this->groupings[3] = GR_DESTINATION;
2109 break;
2110 case STR_STATION_VIEW_GROUP_S_D_V:
2111 this->groupings[1] = GR_SOURCE;
2112 this->groupings[2] = GR_DESTINATION;
2113 this->groupings[3] = GR_NEXT;
2114 break;
2115 case STR_STATION_VIEW_GROUP_V_S_D:
2116 this->groupings[1] = GR_NEXT;
2117 this->groupings[2] = GR_SOURCE;
2118 this->groupings[3] = GR_DESTINATION;
2119 break;
2120 case STR_STATION_VIEW_GROUP_V_D_S:
2121 this->groupings[1] = GR_NEXT;
2122 this->groupings[2] = GR_DESTINATION;
2123 this->groupings[3] = GR_SOURCE;
2124 break;
2125 case STR_STATION_VIEW_GROUP_D_S_V:
2126 this->groupings[1] = GR_DESTINATION;
2127 this->groupings[2] = GR_SOURCE;
2128 this->groupings[3] = GR_NEXT;
2129 break;
2130 case STR_STATION_VIEW_GROUP_D_V_S:
2131 this->groupings[1] = GR_DESTINATION;
2132 this->groupings[2] = GR_NEXT;
2133 this->groupings[3] = GR_SOURCE;
2134 break;
2135 }
2136 this->SetDirty();
2137 }
2138
2139 void OnDropdownSelect(WidgetID widget, int index, int) override
2140 {
2141 if (widget == WID_SV_SORT_BY) {
2142 this->SelectSortBy(index);
2143 } else {
2144 this->SelectGroupBy(index);
2145 }
2146 }
2147
2148 void OnQueryTextFinished(std::optional<std::string> str) override
2149 {
2150 if (!str.has_value()) return;
2151
2152 Command<Commands::RenameStation>::Post(STR_ERROR_CAN_T_RENAME_STATION, this->window_number, *str);
2153 }
2154
2155 void OnResize() override
2156 {
2157 this->vscroll->SetCapacityFromWidget(this, WID_SV_WAITING, WidgetDimensions::scaled.framerect.Vertical());
2158 }
2159
2165 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
2166 {
2167 if (gui_scope) {
2168 if (data >= 0 && data < NUM_CARGO) {
2169 this->cached_destinations.Remove((CargoType)data);
2170 } else {
2171 this->ReInit();
2172 }
2173 }
2174 }
2175};
2176
2177static WindowDesc _station_view_desc(
2178 WDP_AUTO, "view_station", 249, 117,
2180 {},
2181 _nested_station_view_widgets
2182);
2183
2189void ShowStationViewWindow(StationID station)
2190{
2191 AllocateWindowDescFront<StationViewWindow>(_station_view_desc, station);
2192}
2193
2199
2200static std::vector<TileAndStation> _deleted_stations_nearby;
2201static std::vector<StationID> _stations_nearby_list;
2202
2210template <class T>
2211static void AddNearbyStation(TileIndex tile, TileArea *ctx)
2212{
2213 /* First check if there were deleted stations here */
2214 for (auto it = _deleted_stations_nearby.begin(); it != _deleted_stations_nearby.end(); /* nothing */) {
2215 if (it->tile == tile) {
2216 _stations_nearby_list.push_back(it->station);
2217 it = _deleted_stations_nearby.erase(it);
2218 } else {
2219 ++it;
2220 }
2221 }
2222
2223 /* Check if own station and if we stay within station spread */
2224 if (!IsTileType(tile, TileType::Station)) return;
2225
2226 StationID sid = GetStationIndex(tile);
2227
2228 /* This station is (likely) a waypoint */
2229 if (!T::IsValidID(sid)) return;
2230
2231 BaseStation *st = BaseStation::Get(sid);
2232 if (st->owner != _local_company || std::ranges::find(_stations_nearby_list, sid) != _stations_nearby_list.end()) return;
2233
2234 if (st->rect.BeforeAddRect(ctx->tile, ctx->w, ctx->h, StationRect::ADD_TEST).Succeeded()) {
2235 _stations_nearby_list.push_back(sid);
2236 }
2237}
2238
2248template <class T>
2249static void 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 /* Look for deleted stations */
2258 for (const BaseStation *st : BaseStation::Iterate()) {
2259 if (T::IsValidBaseStation(st) && !st->IsInUse() && st->owner == _local_company) {
2260 /* Include only within station spread (yes, it is strictly less than) */
2261 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) {
2262 _deleted_stations_nearby.emplace_back(st->xy, st->index);
2263
2264 /* Add the station when it's within where we're going to build */
2265 if (IsInsideBS(TileX(st->xy), TileX(ctx.tile), ctx.w) &&
2266 IsInsideBS(TileY(st->xy), TileY(ctx.tile), ctx.h)) {
2267 AddNearbyStation<T>(st->xy, &ctx);
2268 }
2269 }
2270 }
2271 }
2272
2273 /* Add stations that are within station tile area. Stations do not have to occupy all tiles */
2274 for (auto t : ta) {
2275 AddNearbyStation<T>(t, &ctx);
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;
2282 uint max_dist = distant_join ? _settings_game.station.station_spread - std::min(ta.w, ta.h) : 1;
2283
2284 for (auto tile : SpiralTileSequence(TileAddByDir(ctx.tile, DIR_N), max_dist, ta.w, ta.h)) {
2285 AddNearbyStation<T>(tile, &ctx);
2286 }
2287}
2288
2289static constexpr std::initializer_list<NWidgetPart> _nested_select_station_widgets = {
2291 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
2292 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_JS_CAPTION), SetStringTip(STR_JOIN_STATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2293 NWidget(WWT_DEFSIZEBOX, COLOUR_DARK_GREEN),
2294 EndContainer(),
2298 NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_JS_SCROLLBAR),
2299 NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
2300 EndContainer(),
2301 EndContainer(),
2302};
2303
2308template <class T>
2309struct SelectStationWindow : Window {
2310 StationPickerCmdProc select_station_proc{};
2312 Scrollbar *vscroll = nullptr;
2313
2314 SelectStationWindow(WindowDesc &desc, TileArea ta, StationPickerCmdProc&& proc) :
2315 Window(desc),
2316 select_station_proc(std::move(proc)),
2317 area(ta)
2318 {
2319 this->CreateNestedTree();
2320 this->vscroll = this->GetScrollbar(WID_JS_SCROLLBAR);
2321 this->GetWidget<NWidgetCore>(WID_JS_CAPTION)->SetString(T::IsWaypoint() ? STR_JOIN_WAYPOINT_CAPTION : STR_JOIN_STATION_CAPTION);
2322 this->FinishInitNested(0);
2323 this->OnInvalidateData(0);
2324
2325 _thd.freeze = true;
2326 }
2327
2328 void Close([[maybe_unused]] int data = 0) override
2329 {
2330 SetViewportCatchmentSpecializedStation<typename T::StationType>(nullptr, true);
2331
2332 _thd.freeze = false;
2333 this->Window::Close();
2334 }
2335
2336 void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override
2337 {
2338 if (widget != WID_JS_PANEL) return;
2339
2340 /* Determine the widest string */
2341 Dimension d = GetStringBoundingBox(T::IsWaypoint() ? STR_JOIN_WAYPOINT_CREATE_SPLIT_WAYPOINT : STR_JOIN_STATION_CREATE_SPLIT_STATION);
2342 for (const auto &station : _stations_nearby_list) {
2343 if (station == NEW_STATION) continue;
2344 const BaseStation *st = BaseStation::Get(station);
2345 d = maxdim(d, GetStringBoundingBox(T::IsWaypoint()
2346 ? GetString(STR_STATION_LIST_WAYPOINT, st->index)
2347 : GetString(STR_STATION_LIST_STATION, st->index, st->facilities)));
2348 }
2349
2350 fill.height = resize.height = d.height;
2351 d.height *= 5;
2352 d.width += padding.width;
2353 d.height += padding.height;
2354 size = d;
2355 }
2356
2357 void DrawWidget(const Rect &r, WidgetID widget) const override
2358 {
2359 if (widget != WID_JS_PANEL) return;
2360
2361 Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
2362 auto [first, last] = this->vscroll->GetVisibleRangeIterators(_stations_nearby_list);
2363 for (auto it = first; it != last; ++it, tr.top += this->resize.step_height) {
2364 if (*it == NEW_STATION) {
2365 DrawString(tr, T::IsWaypoint() ? STR_JOIN_WAYPOINT_CREATE_SPLIT_WAYPOINT : STR_JOIN_STATION_CREATE_SPLIT_STATION);
2366 } else {
2367 const BaseStation *st = BaseStation::Get(*it);
2368 DrawString(tr, T::IsWaypoint()
2369 ? GetString(STR_STATION_LIST_WAYPOINT, st->index)
2370 : GetString(STR_STATION_LIST_STATION, st->index, st->facilities));
2371 }
2372 }
2373
2374 }
2375
2376 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
2377 {
2378 if (widget != WID_JS_PANEL) return;
2379
2380 auto it = this->vscroll->GetScrolledItemFromWidget(_stations_nearby_list, pt.y, this, WID_JS_PANEL, WidgetDimensions::scaled.framerect.top);
2381 if (it == _stations_nearby_list.end()) return;
2382
2383 /* Execute stored Command */
2384 this->select_station_proc(false, *it);
2385
2386 /* Close Window; this might cause double frees! */
2388 }
2389
2390 void OnRealtimeTick([[maybe_unused]] uint delta_ms) override
2391 {
2392 if (_thd.dirty & 2) {
2393 _thd.dirty &= ~2;
2394 this->SetDirty();
2395 }
2396 }
2397
2398 void OnResize() override
2399 {
2400 this->vscroll->SetCapacityFromWidget(this, WID_JS_PANEL, WidgetDimensions::scaled.framerect.Vertical());
2401 }
2402
2408 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
2409 {
2410 if (!gui_scope) return;
2411 FindStationsNearby<T>(this->area, true);
2412 this->vscroll->SetCount(_stations_nearby_list.size());
2413 this->SetDirty();
2414 }
2415
2416 void OnMouseOver([[maybe_unused]] Point pt, WidgetID widget) override
2417 {
2418 if (widget != WID_JS_PANEL) {
2419 SetViewportCatchmentSpecializedStation<typename T::StationType>(nullptr, true);
2420 return;
2421 }
2422
2423 /* Show coverage area of station under cursor */
2424 auto it = this->vscroll->GetScrolledItemFromWidget(_stations_nearby_list, pt.y, this, WID_JS_PANEL, WidgetDimensions::scaled.framerect.top);
2425 const typename T::StationType *st = it == _stations_nearby_list.end() || *it == NEW_STATION ? nullptr : T::StationType::Get(*it);
2426 SetViewportCatchmentSpecializedStation<typename T::StationType>(st, true);
2427 }
2428};
2429
2430static WindowDesc _select_station_desc(
2431 WDP_AUTO, "build_station_join", 200, 180,
2434 _nested_select_station_widgets
2435);
2436
2437
2443static bool StationJoinerNeeded(const StationPickerCmdProc &proc)
2444{
2445 /* Only show selection if distant join is enabled in the settings */
2446 if (!_settings_game.station.distant_join_stations) return false;
2447
2448 /* If a window is already opened and we didn't ctrl-click,
2449 * return true (i.e. just flash the old window) */
2450 Window *selection_window = FindWindowById(WC_SELECT_STATION, 0);
2451 if (selection_window != nullptr) {
2452 /* Abort current distant-join and start new one */
2453 selection_window->Close();
2455 }
2456
2457 /* only show the popup, if we press ctrl */
2458 if (!_ctrl_pressed) return false;
2459
2460 /* Now check if we could build there */
2461 return proc(true, StationID::Invalid());
2462}
2463
2470template <class T>
2471void ShowSelectBaseStationIfNeeded(TileArea ta, StationPickerCmdProc&& proc)
2472{
2473 if (StationJoinerNeeded(proc)) {
2474 if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
2475 FindStationsNearby<T>(ta, false);
2476 new SelectStationWindow<T>(_select_station_desc, ta, std::move(proc));
2477 } else {
2478 proc(false, StationID::Invalid());
2479 }
2480}
2481
2487void ShowSelectStationIfNeeded(TileArea ta, StationPickerCmdProc proc)
2488{
2490}
2491
2497void ShowSelectRailWaypointIfNeeded(TileArea ta, StationPickerCmdProc proc)
2498{
2500}
2501
2507void ShowSelectRoadWaypointIfNeeded(TileArea ta, StationPickerCmdProc proc)
2508{
2510}
@ AirportClosed
Dummy block for indicating a closed airport.
Definition airport.h:130
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 bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
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:21
static const CargoType NUM_CARGO
Maximum number of cargo types in a game.
Definition cargo_type.h:73
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:31
Types/functions related to cargoes.
@ Passengers
Passengers.
Definition cargotype.h:50
bool IsCargoInClass(CargoType cargo, CargoClasses cc)
Does cargo c have cargo class cc?
Definition cargotype.h:235
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr bool None() const
Test if none of the values are set.
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.
~CargoDataEntry()
Remove ourselves from our parent.
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.
CargoDataEntry & InsertOrRetrieve(StationID station)
Insert a new child or retrieve an existing child using a station ID as ID.
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.
std::unique_ptr< CargoDataSet > children
the children of this entry.
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.
const Tcont * Packets() const
Returns a pointer to the cargo packet list (so you can iterate over it etc).
StationCargoPacketMap::const_iterator ConstIterator
bool Succeeded() const
Did this command succeed?
The list of stations per company.
std::array< uint16_t, NUM_CARGO > stations_per_cargo_type
Number of stations with a rating for each cargo type.
static bool StationNameSorter(const Station *const &a, const Station *const &b, const CargoTypes &filter)
Sort stations by their name.
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.
static bool StationWaitingAvailableSorter(const Station *const &a, const Station *const &b, const CargoTypes &filter)
Sort stations by their available waiting cargo.
void SortStationsList()
Sort the stations list.
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
static const StringID sorter_names[]
Strings describing how stations are sorted.
void OnDropdownSelect(WidgetID widget, int index, int) override
A dropdown option associated to this window has been selected.
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
static bool StationRatingMaxSorter(const Station *const &a, const Station *const &b, const CargoTypes &filter)
Sort stations by their rating.
std::string GetWidgetString(WidgetID widget, StringID stringid) const override
Get the raw string for a widget.
void OnPaint() override
The window must be repainted.
static bool StationWaitingTotalSorter(const Station *const &a, const Station *const &b, const CargoTypes &filter)
Sort stations by their waiting cargo.
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 &filter)
Sort stations by their type.
void OnGameTick() override
Called once per (game) tick.
~CompanyStationsWindow() override
Save the last sorting state.
void BuildStationsList(const Owner owner)
(Re)Build station list
static const std::initializer_list< GUIStationList::SortFunction *const > sorter_funcs
Functions to sort stations.
uint16_t stations_per_cargo_type_no_rating
Number of stations without a rating.
static bool StationRatingMinSorter(const Station *const &a, const Station *const &b, const CargoTypes &filter)
Sort stations by their rating.
Drop down checkmark 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.
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.
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
uint Monthly(uint base) const
Scale a value to its monthly equivalent, based on last compression.
Definition linkgraph.h:253
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:1167
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:2430
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:2504
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.
Generate TileIndices around a center tile or tile area, with increasing distance.
CargoList that is used for stations.
uint ReservedCount() const
Returns sum of cargo reserved for loading onto vehicles.
static bool UsingWallclockUnits(bool newgame=false)
Check if we are using wallclock units.
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition window_gui.h:30
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.
static constexpr Owner OWNER_NONE
The tile has no ownership.
Functions related to debugging.
#define Debug(category, level, format_string,...)
Output 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, DropDownOptions options, std::string *const persistent_filter_text)
Show a dropdown menu window near a widget of the parent window.
Definition dropdown.cpp:627
std::unique_ptr< DropDownListItem > MakeDropDownListDividerItem()
Creates new DropDownListDividerItem.
Definition dropdown.cpp:36
std::unique_ptr< DropDownListItem > MakeDropDownListStringItem(StringID str, int value, bool masked, bool shaded)
Creates new DropDownListStringItem.
Definition dropdown.cpp:49
void ShowDropDownList(Window *w, DropDownList &&list, int selected, WidgetID button, uint width, DropDownOptions options, std::string *const persistent_filter_text)
Show a drop down list.
Definition dropdown.cpp:585
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.
@ Persist
Set if this dropdown should stay open after an option is selected.
@ Filterable
Set if the dropdown is filterable.
@ Invalid
Invalid base price.
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
Definition enum_type.hpp:21
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition fontcache.cpp:87
Dimension maxdim(const Dimension &d1, const Dimension &d2)
Compute bounding box of both dimensions.
Geometry functions.
int CentreBounds(int min, int max, int size)
Determine where to position a centred object.
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition gfx.cpp:900
Dimension GetStringListBoundingBox(std::span< const StringID > list, FontSize fontsize)
Get maximum dimension of a list of strings.
Definition gfx.cpp:939
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:669
bool _ctrl_pressed
Is Ctrl pressed?
Definition gfx.cpp:39
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:1038
uint8_t GetCharacterWidth(FontSize size, char32_t key)
Return width of character glyph.
Definition gfx.cpp:1278
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:788
void GfxFillRect(int left, int top, int right, int bottom, const std::variant< PixelColour, PaletteID > &colour, FillRectMode mode)
Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen.
Definition gfx.cpp:116
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:250
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:249
@ SA_CENTER
Center both horizontally and vertically.
Definition gfx_type.h:398
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:307
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 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 NWidget(WidgetType tp, Colours col, WidgetID idx=INVALID_WIDGET)
Widget part function for starting a new 'real' widget.
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:980
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.
@ Manual
Manual distribution. No link graph calculations are run.
#define Rect
Macro that prevents name conflicts between included headers.
#define Point
Macro that prevents name conflicts between included headers.
uint DistanceMax(TileIndex t0, TileIndex t1)
Gets the biggest distance component (x or y) between the two given tiles.
Definition map.cpp:201
TileIndex TileAddXY(TileIndex tile, int x, int y)
Adds a given offset to a tile.
Definition map_func.h:474
static TileIndex TileVirtXY(uint x, uint y)
Get a tile from the virtual XY-coordinate.
Definition map_func.h:407
TileIndex TileAddByDir(TileIndex tile, Direction dir)
Adds a Direction to a tile.
Definition map_func.h:603
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition map_func.h:376
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:429
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:419
int DivideApprox(int a, int b)
Deterministic approximate division.
Definition math_func.cpp:22
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(std::string_view str, StringID caption, uint maxsize, Window *parent, CharSetFilter afilter, QueryStringFlags flags)
Show a query popup window with a textbox in it.
Types related to the misc widgets.
@ WID_QS_MOVE
Move button.
Definition misc_widget.h:39
static constexpr CargoType CF_EXPAND_LIST
Expand list to show all items (station list).
Definition cargo_type.h:100
static constexpr CargoType CF_NO_RATING
Show items with no rating (station list).
Definition cargo_type.h:98
static constexpr CargoType CF_SELECT_ALL
Select all items (station list).
Definition cargo_type.h:99
TextColour GetContrastColour(PixelColour background, uint8_t threshold)
Determine a contrasty text colour for a coloured background.
Definition palette.cpp:366
static constexpr PixelColour PC_GREEN
Green palette colour.
static constexpr PixelColour 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:61
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
Base types for having sorted lists in GUIs.
Base classes/functions for stations.
std::pair< CargoArray, CargoTypes > GetAcceptanceAroundTiles(TileIndex center_tile, int w, int h, int rad)
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.
static void StationsWndShowStationRating(int left, int right, int y, CargoType cargo, uint amount, uint8_t rating)
Draw small boxes of cargo amount and ratings data at the given coordinates.
void ShowSelectStationIfNeeded(TileArea ta, StationPickerCmdProc proc)
Show the station selection window when needed.
CargoSortType
Ways of sorting cargo in the UI.
@ 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.
void FindStationsAroundSelection()
Find stations adjacent to the current tile highlight area, so that existing coverage area can be draw...
static bool StationJoinerNeeded(const StationPickerCmdProc &proc)
Check whether we need to show the station selection window.
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.
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 void FindStationsNearby(TileArea ta, bool distant_join)
Circulate around the to-be-built station to find stations we could join.
static void AddNearbyStation(TileIndex tile, TileArea *ctx)
Add station on this tile to _stations_nearby_list if it's fully within the station spread.
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
StationFacility
The facilities a station might be having.
@ Dock
Station with a dock.
@ TruckStop
Station with truck stops.
@ Train
Station with train station.
@ Airport
Station with an airport.
@ BusStop
Station with bus stops.
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_JS_CAPTION
Caption of the window.
@ WID_JS_PANEL
Main panel.
@ WID_JS_SCROLLBAR
Scrollbar of the panel.
@ 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_SV_CLOSE_AIRPORT_SEL
Container for 'close airport' button, which can be hidden.
@ 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:429
Functions related to low-level strings.
@ CS_ALPHANUMERAL
Both numeric and alphabetic and spaces and stuff.
Definition string_type.h:25
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:424
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.
AirportBlocks blocks
stores which blocks on the airport are taken. was 16 bit earlier on, then 32
Base class for all station-ish types.
TileIndex xy
Base tile of the station.
StationFacilities facilities
The facilities that this station has.
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.
Class for storing amounts of cargo.
Definition cargo_type.h:115
Container for cargo from the same location and time.
Definition cargopacket.h:41
uint16_t Count() const
Gets the number of 'items' in this packet.
StationID GetFirstStation() const
Gets the ID of the station where the cargo was loaded for the first time.
Specification of a cargo type.
Definition cargotype.h:74
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo type.
Definition cargotype.h:138
StringID abbrev
Two letter abbreviation for this cargo type.
Definition cargotype.h:95
SpriteID GetCargoIcon() const
Get sprite for showing cargo of this type.
bool IsValid() const
Tests for validity of this cargospec.
Definition cargotype.h:118
CargoTypes cargoes
bitmap of cargo types to include
StationFacilities facilities
types of stations of interest
bool include_no_rating
Whether we should include stations with no cargo rating.
Dimensions (a width and height) of a rectangle in 2D.
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?
NodeID node
ID of node in link graph referring to this goods entry.
const GoodsEntryData & GetData() const
Get optional cargo packet/flow data.
LinkGraphID link_graph
Link graph this station belongs to.
uint8_t rating
Station rating for this cargo.
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 TileType::Void.
Definition map_func.h:298
static uint MaxX()
Gets the maximum X coordinate within the map, including TileType::Void.
Definition map_func.h:289
static uint Size()
Get the size of the map.
Definition map_func.h:280
uint16_t w
The width of the area.
TileIndex tile
The base tile of the area.
uint16_t h
The height of the area.
Colour for pixel/line drawing.
Definition gfx_type.h:405
static Pool::IterateWrapper< BaseStation > Iterate(size_t from=0)
static BaseStation * Get(auto index)
static LinkGraph * GetIfValid(auto index)
Specification of a rectangle with absolute coordinates of all edges.
Rect WithWidth(int width, bool end) const
Copy Rect and set its width.
int Width() const
Get width of Rect.
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.
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.
static bool IsExpected(const BaseStation *st)
static Pool::IterateWrapper< Station > Iterate(size_t from=0)
static Waypoint * Get(auto index)
static Waypoint * From(BaseStation *st)
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 HandleCargoWaitingClick(int row)
Handle a click on a specific row in the cargo view.
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 OnQueryTextFinished(std::optional< std::string > str) override
The query window opened from this window has closed.
void OnDropdownSelect(WidgetID widget, int index, int) override
A dropdown option associated to this window has been selected.
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.
std::array< Grouping, NUM_COLUMNS > groupings
Grouping modes for the different columns.
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 SelectSortBy(int index)
Select a new sort criterium for the cargo view.
void OnResize() override
Called after the window got resized.
StringID SearchNonStop(CargoDataEntry &cd, StationID station, int column)
Determine if we need to show the special "non-stop" string.
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 BuildCargoList(CargoType cargo, const StationCargoList &packets, CargoDataEntry *entry)
Build up the cargo view for WAITING mode and a specific cargo.
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 OnInit() override
Notification that the nested widget tree gets initialized.
void RecalcDestinations(CargoType cargo)
Rebuild the cache for estimated destinations which is used to quickly show the "destination" entries ...
int line_height
Height of a cargo line.
Dimension cargo_icon_size
Size of largest cargo icon.
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.
void BuildCargoList(CargoDataEntry *entry, const Station *st)
Build up the cargo view for all cargoes.
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.
StringID GetEntryString(StationID station, StringID here, StringID other_station, StringID any) const
Select the correct string for an entry referring to the specified station.
std::array< CargoSortType, NUM_COLUMNS > sortings
Sort types of the different 'columns'.
void SetDisplayedRow(const CargoDataEntry &entry)
Mark a specific row, characterized by its CargoDataEntry, as expanded.
std::string GetWidgetString(WidgetID widget, StringID stringid) const override
Get the raw string for a widget.
std::array< SortOrder, NUM_COLUMNS > sort_orders
Sort order (ascending/descending) for the 'columns'.
uint expand_shrink_width
The width allocated to the expand/shrink 'button'.
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.
void DrawCargoIcons(CargoType cargo, uint waiting, const Rect &r) const
Draw icons of waiting cargo.
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.
void BuildFlowList(CargoType cargo, const FlowStatMap &flows, CargoDataEntry *entry)
Build up the cargo view for PLANNED mode and a specific cargo.
void SelectGroupBy(int index)
Select a new grouping mode for the cargo view.
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
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.
std::array< GoodsEntry, NUM_CARGO > goods
Goods at this station.
Airport airport
Tile area the airport covers.
Struct containing TileIndex and StationID.
TileIndex tile
TileIndex.
StationID station
StationID.
CompanyID exclusivity
which company has exclusivity
Definition town.h:86
uint8_t exclusive_counter
months till the exclusivity expires
Definition town.h:87
The information about a vehicle list.
Definition vehiclelist.h:32
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:274
void ReInit(int rx=0, int ry=0, bool reposition=false)
Re-initialize a window, and optionally change its size.
Definition window.cpp:992
virtual void Close(int data=0)
Hide the window and all its child windows, and mark them for a later deletion.
Definition window.cpp:1117
static int SortButtonWidth()
Get width of up/down arrow of sort button state.
Definition widget.cpp:817
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
Definition window.cpp:1822
void DrawWidgets() const
Paint all widgets of a window.
Definition widget.cpp:768
Window * parent
Parent window.
Definition window_gui.h:329
void RaiseWidget(WidgetID widget_index)
Marks a widget as raised.
Definition window_gui.h:470
void SetWidgetDirty(WidgetID widget_index) const
Invalidate a widget, i.e.
Definition window.cpp:570
virtual std::string GetWidgetString(WidgetID widget, StringID stringid) const
Get the raw string for a widget.
Definition window.cpp:518
void DrawSortButtonState(WidgetID widget, SortButtonState state) const
Draw a sort button's up or down arrow symbol.
Definition widget.cpp:800
void CloseChildWindows(WindowClass wc=WC_INVALID) const
Close all children a window might have in a head-recursive manner.
Definition window.cpp:1089
ResizeInfo resize
Resize information.
Definition window_gui.h:315
void CreateNestedTree()
Perform the first part of the initialization of a nested widget tree.
Definition window.cpp:1812
WindowClass window_class
Window class.
Definition window_gui.h:302
bool IsWidgetLowered(WidgetID widget_index) const
Gets the lowered state of a widget.
Definition window_gui.h:492
Owner owner
The owner of the content shown in this window. Company colour is acquired from this variable.
Definition window_gui.h:317
void SetWidgetLoweredState(WidgetID widget_index, bool lowered_stat)
Sets the lowered/raised status of a widget.
Definition window_gui.h:442
bool IsShaded() const
Is window shaded currently?
Definition window_gui.h:563
void SetTimeout()
Set the timeout flag of the window and initiate the timer.
Definition window_gui.h:356
Window(WindowDesc &desc)
Empty constructor, initialization has been moved to InitNested() called from the constructor of the d...
Definition window.cpp:1845
const NWID * GetWidget(WidgetID widnum) const
Get the nested widget with number widnum from the nested widget tree.
Definition window_gui.h:990
void LowerWidget(WidgetID widget_index)
Marks a widget as lowered.
Definition window_gui.h:461
const Scrollbar * GetScrollbar(WidgetID widnum) const
Return the Scrollbar to a widget index.
Definition window.cpp:327
void SetWidgetDisabledState(WidgetID widget_index, bool disab_stat)
Sets the enabled/disabled status of a widget.
Definition window_gui.h:382
int width
width of the window (number of pixels to the right in x direction)
Definition window_gui.h:312
void ToggleWidgetLoweredState(WidgetID widget_index)
Invert the lowered/raised status of a widget.
Definition window_gui.h:451
WindowNumber window_number
Window number within the window class.
Definition window_gui.h:303
Stuff related to the text buffer GUI.
@ EnableMove
enable the 'Move' button
Definition textbuf_gui.h:22
@ EnableDefault
enable the 'Default' button ("\0" is returned)
Definition textbuf_gui.h:20
@ LengthIsInChars
the length of the string is counted in characters
Definition textbuf_gui.h:21
static bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
Owner GetTileOwner(Tile tile)
Returns the owner of a tile.
Definition tile_map.h:178
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:100
static constexpr uint TILE_SIZE
Tile size in world coordinates.
Definition tile_type.h:15
@ Station
A tile of a station or airport.
Definition tile_type.h:54
OrthogonalTileArea TileArea
Shorthand for the much more common orthogonal tile area.
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.
@ VL_STATION_LIST
Index is the station.
Definition vehiclelist.h:25
bool ScrollMainWindowToTile(TileIndex tile, bool instant)
Scrolls the viewport of the main window to a given location.
void SetViewportStationRect(const Station *st, bool sel)
Select or deselect station for rectangle area highlight.
void SetViewportCatchmentStation(const Station *st, bool sel)
Select or deselect station for coverage area highlight.
void SetViewportWaypointRect(const Waypoint *wp, bool sel)
Select or deselect waypoint for rectangle 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:49
@ 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:44
@ WWT_PANEL
Simple depressed panel.
Definition widget_type.h:39
@ 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
@ NWID_SELECTION
Stacked widgets, only one visible at a time (eg in a panel with tabs).
Definition widget_type.h:71
@ SZSP_NONE
Display plane with zero size in both directions (none filling and resizing).
@ EqualSize
Containers should keep all their (resizing) children equally large.
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:1209
Window * FindWindowByClass(WindowClass cls)
Find any window by its class.
Definition window.cpp:1181
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
Definition window.cpp:1166
Window functions not directly related to making/drawing windows.
@ Construction
This window is used for construction; close it whenever changing company.
Definition window_gui.h:153
Twindow * AllocateWindowDescFront(WindowDesc &desc, WindowNumber window_number, Targs... extra_arguments)
Open a new window.
@ 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:144
int WidgetID
Widget ID.
Definition window_type.h:20
@ WC_WAYPOINT_VIEW
Waypoint view; Window numbers:
@ 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:50
@ 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_QUERY_STRING
Query string window; 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