2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file station_gui.cpp The GUI for stations. */
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 "widgets/dropdown_func.h"
24 #include "station_base.h"
25 #include "waypoint_base.h"
26 #include "tilehighlight_func.h"
27 #include "company_base.h"
28 #include "sortlist_type.h"
29 #include "core/geometry_func.hpp"
30 #include "vehiclelist.h"
32 #include "linkgraph/linkgraph.h"
33 #include "zoom_func.h"
34 #include "station_cmd.h"
36 #include "widgets/station_widget.h"
38 #include "table/strings.h"
40 #include "safeguards.h"
43 * Calculates and draws the accepted or supplied cargo around the selected tile(s)
44 * @param left x position where the string is to be drawn
45 * @param right the right most position to draw on
46 * @param top y position where the string is to be drawn
47 * @param sct which type of cargo is to be displayed (passengers/non-passengers)
48 * @param rad radius around selected tile(s) to be searched
49 * @param supplies if supplied cargoes should be drawn, else accepted cargoes
50 * @return Returns the y value below the string that was drawn
52 int DrawStationCoverageAreaText(int left
, int right
, int top
, StationCoverageType sct
, int rad
, bool supplies
)
54 TileIndex tile
= TileVirtXY(_thd
.pos
.x
, _thd
.pos
.y
);
55 CargoTypes cargo_mask
= 0;
56 if (_thd
.drawstyle
== HT_RECT
&& tile
< Map::Size()) {
59 cargoes
= GetProductionAroundTiles(tile
, _thd
.size
.x
/ TILE_SIZE
, _thd
.size
.y
/ TILE_SIZE
, rad
);
61 cargoes
= GetAcceptanceAroundTiles(tile
, _thd
.size
.x
/ TILE_SIZE
, _thd
.size
.y
/ TILE_SIZE
, rad
);
64 /* Convert cargo counts to a set of cargo bits, and draw the result. */
65 for (CargoID i
= 0; i
< NUM_CARGO
; i
++) {
67 case SCT_PASSENGERS_ONLY
: if (!IsCargoInClass(i
, CC_PASSENGERS
)) continue; break;
68 case SCT_NON_PASSENGERS_ONLY
: if (IsCargoInClass(i
, CC_PASSENGERS
)) continue; break;
70 default: NOT_REACHED();
72 if (cargoes
[i
] >= (supplies
? 1U : 8U)) SetBit(cargo_mask
, i
);
75 SetDParam(0, cargo_mask
);
76 return DrawStringMultiLine(left
, right
, top
, INT32_MAX
, supplies
? STR_STATION_BUILD_SUPPLIES_CARGO
: STR_STATION_BUILD_ACCEPTS_CARGO
);
80 * Find stations adjacent to the current tile highlight area, so that existing coverage
84 void FindStationsAroundSelection()
86 /* With distant join we don't know which station will be selected, so don't show any */
88 SetViewportCatchmentSpecializedStation
<T
>(nullptr, true);
92 /* Tile area for TileHighlightData */
93 TileArea
location(TileVirtXY(_thd
.pos
.x
, _thd
.pos
.y
), _thd
.size
.x
/ TILE_SIZE
- 1, _thd
.size
.y
/ TILE_SIZE
- 1);
95 /* Extended area by one tile */
96 uint x
= TileX(location
.tile
);
97 uint y
= TileY(location
.tile
);
99 /* Waypoints can only be built on existing rail tiles, so don't extend area if not highlighting a rail tile. */
100 int max_c
= T::EXPECTED_FACIL
== FACIL_WAYPOINT
&& !IsTileType(location
.tile
, MP_RAILWAY
) ? 0 : 1;
101 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
)));
103 T
*adjacent
= nullptr;
105 /* Direct loop instead of ForAllStationsAroundTiles as we are not interested in catchment area */
106 for (TileIndex tile
: ta
) {
107 if (IsTileType(tile
, MP_STATION
) && GetTileOwner(tile
) == _local_company
) {
108 T
*st
= T::GetByTile(tile
);
109 if (st
== nullptr) continue;
110 if (adjacent
!= nullptr && st
!= adjacent
) {
111 /* Multiple nearby, distant join is required. */
118 SetViewportCatchmentSpecializedStation
<T
>(adjacent
, true);
122 * Check whether we need to redraw the station coverage text.
123 * If it is needed actually make the window for redrawing.
124 * @param w the window to check.
126 void CheckRedrawStationCoverage(const Window
*w
)
128 /* Test if ctrl state changed */
129 static bool _last_ctrl_pressed
;
130 if (_ctrl_pressed
!= _last_ctrl_pressed
) {
132 _last_ctrl_pressed
= _ctrl_pressed
;
135 if (_thd
.dirty
& 1) {
139 if (_settings_client
.gui
.station_show_coverage
&& _thd
.drawstyle
== HT_RECT
) {
140 FindStationsAroundSelection
<Station
>();
145 void CheckRedrawWaypointCoverage(const Window
*)
147 /* Test if ctrl state changed */
148 static bool _last_ctrl_pressed
;
149 if (_ctrl_pressed
!= _last_ctrl_pressed
) {
151 _last_ctrl_pressed
= _ctrl_pressed
;
154 if (_thd
.dirty
& 1) {
157 if (_thd
.drawstyle
== HT_RECT
) {
158 FindStationsAroundSelection
<Waypoint
>();
164 * Draw small boxes of cargo amount and ratings data at the given
165 * coordinates. If amount exceeds 576 units, it is shown 'full', same
166 * goes for the rating: at above 90% orso (224) it is also 'full'
168 * @param left left most coordinate to draw the box at
169 * @param right right most coordinate to draw the box at
170 * @param y coordinate to draw the box at
171 * @param type Cargo type
172 * @param amount Cargo amount
173 * @param rating ratings data for that particular cargo
175 static void StationsWndShowStationRating(int left
, int right
, int y
, CargoID type
, uint amount
, byte rating
)
177 static const uint units_full
= 576; ///< number of units to show station as 'full'
178 static const uint rating_full
= 224; ///< rating needed so it is shown as 'full'
180 const CargoSpec
*cs
= CargoSpec::Get(type
);
181 if (!cs
->IsValid()) return;
183 int padding
= ScaleGUITrad(1);
184 int width
= right
- left
;
185 int colour
= cs
->rating_colour
;
186 TextColour tc
= GetContrastColour(colour
);
187 uint w
= std::min(amount
+ 5, units_full
) * width
/ units_full
;
189 int height
= GetCharacterHeight(FS_SMALL
) + padding
- 1;
192 /* Draw total cargo (limited) on station */
193 GfxFillRect(left
, y
, left
+ w
- 1, y
+ height
, colour
);
195 /* Draw a (scaled) one pixel-wide bar of additional cargo meter, useful
196 * for stations with only a small amount (<=30) */
197 uint rest
= ScaleGUITrad(amount
) / 5;
199 GfxFillRect(left
, y
+ height
- rest
, left
+ padding
- 1, y
+ height
, colour
);
203 DrawString(left
+ padding
, right
, y
, cs
->abbrev
, tc
, SA_CENTER
, false, FS_SMALL
);
205 /* Draw green/red ratings bar (fits under the waiting bar) */
206 y
+= height
+ padding
+ 1;
207 GfxFillRect(left
+ padding
, y
, right
- padding
- 1, y
+ padding
- 1, PC_RED
);
208 w
= std::min
<uint
>(rating
, rating_full
) * (width
- padding
- padding
) / rating_full
;
209 if (w
!= 0) GfxFillRect(left
+ padding
, y
, left
+ w
- 1, y
+ padding
- 1, PC_GREEN
);
212 typedef GUIList
<const Station
*, const CargoTypes
&> GUIStationList
;
215 * The list of stations per company.
217 class CompanyStationsWindow
: public Window
220 /* Runtime saved values */
222 Listing last_sorting
;
223 byte facilities
; ///< types of stations of interest
224 bool include_no_rating
; ///< Whether we should include stations with no cargo rating.
225 CargoTypes cargoes
; ///< bitmap of cargo types to include
228 static inline FilterState initial_state
= {
230 FACIL_TRAIN
| FACIL_TRUCK_STOP
| FACIL_BUS_STOP
| FACIL_AIRPORT
| FACIL_DOCK
,
235 /* Constants for sorting stations */
236 static const StringID sorter_names
[];
237 static GUIStationList::SortFunction
* const sorter_funcs
[];
240 GUIStationList stations
{filter
.cargoes
};
243 bool filter_expanded
;
244 std::array
<uint16_t, NUM_CARGO
> stations_per_cargo_type
; ///< Number of stations with a rating for each cargo type.
245 uint16_t stations_per_cargo_type_no_rating
; ///< Number of stations without a rating.
248 * (Re)Build station list
250 * @param owner company whose stations are to be in list
252 void BuildStationsList(const Owner owner
)
254 if (!this->stations
.NeedRebuild()) return;
256 Debug(misc
, 3, "Building station list for company {}", owner
);
258 this->stations
.clear();
259 this->stations_per_cargo_type
.fill(0);
260 this->stations_per_cargo_type_no_rating
= 0;
262 for (const Station
*st
: Station::Iterate()) {
263 if (st
->owner
== owner
|| (st
->owner
== OWNER_NONE
&& HasStationInUse(st
->index
, true, owner
))) {
264 if (this->filter
.facilities
& st
->facilities
) { // only stations with selected facilities
265 bool has_rating
= false;
266 /* Add to the station/cargo counts. */
267 for (CargoID j
= 0; j
< NUM_CARGO
; j
++) {
268 if (st
->goods
[j
].HasRating()) this->stations_per_cargo_type
[j
]++;
270 for (CargoID j
= 0; j
< NUM_CARGO
; j
++) {
271 if (st
->goods
[j
].HasRating()) {
273 if (HasBit(this->filter
.cargoes
, j
)) {
274 this->stations
.push_back(st
);
279 /* Stations with no cargo rating. */
281 if (this->filter
.include_no_rating
) this->stations
.push_back(st
);
282 this->stations_per_cargo_type_no_rating
++;
288 this->stations
.shrink_to_fit();
289 this->stations
.RebuildDone();
291 this->vscroll
->SetCount(this->stations
.size()); // Update the scrollbar
294 /** Sort stations by their name */
295 static bool StationNameSorter(const Station
* const &a
, const Station
* const &b
, const CargoTypes
&)
297 int r
= StrNaturalCompare(a
->GetCachedName(), b
->GetCachedName()); // Sort by name (natural sorting).
298 if (r
== 0) return a
->index
< b
->index
;
302 /** Sort stations by their type */
303 static bool StationTypeSorter(const Station
* const &a
, const Station
* const &b
, const CargoTypes
&)
305 return a
->facilities
< b
->facilities
;
308 /** Sort stations by their waiting cargo */
309 static bool StationWaitingTotalSorter(const Station
* const &a
, const Station
* const &b
, const CargoTypes
&cargo_filter
)
313 for (CargoID j
: SetCargoBitIterator(cargo_filter
)) {
314 diff
+= a
->goods
[j
].cargo
.TotalCount() - b
->goods
[j
].cargo
.TotalCount();
320 /** Sort stations by their available waiting cargo */
321 static bool StationWaitingAvailableSorter(const Station
* const &a
, const Station
* const &b
, const CargoTypes
&cargo_filter
)
325 for (CargoID j
: SetCargoBitIterator(cargo_filter
)) {
326 diff
+= a
->goods
[j
].cargo
.AvailableCount() - b
->goods
[j
].cargo
.AvailableCount();
332 /** Sort stations by their rating */
333 static bool StationRatingMaxSorter(const Station
* const &a
, const Station
* const &b
, const CargoTypes
&cargo_filter
)
338 for (CargoID j
: SetCargoBitIterator(cargo_filter
)) {
339 if (a
->goods
[j
].HasRating()) maxr1
= std::max(maxr1
, a
->goods
[j
].rating
);
340 if (b
->goods
[j
].HasRating()) maxr2
= std::max(maxr2
, b
->goods
[j
].rating
);
343 return maxr1
< maxr2
;
346 /** Sort stations by their rating */
347 static bool StationRatingMinSorter(const Station
* const &a
, const Station
* const &b
, const CargoTypes
&cargo_filter
)
352 for (CargoID j
: SetCargoBitIterator(cargo_filter
)) {
353 if (a
->goods
[j
].HasRating()) minr1
= std::min(minr1
, a
->goods
[j
].rating
);
354 if (b
->goods
[j
].HasRating()) minr2
= std::min(minr2
, b
->goods
[j
].rating
);
357 return minr1
> minr2
;
360 /** Sort the stations list */
361 void SortStationsList()
363 if (!this->stations
.Sort()) return;
365 /* Set the modified widget dirty */
366 this->SetWidgetDirty(WID_STL_LIST
);
370 CompanyStationsWindow(WindowDesc
*desc
, WindowNumber window_number
) : Window(desc
)
372 /* Load initial filter state. */
373 this->filter
= CompanyStationsWindow::initial_state
;
374 if (this->filter
.cargoes
== ALL_CARGOTYPES
) this->filter
.cargoes
= _cargo_mask
;
376 this->stations
.SetListing(this->filter
.last_sorting
);
377 this->stations
.SetSortFuncs(CompanyStationsWindow::sorter_funcs
);
378 this->stations
.ForceRebuild();
379 this->stations
.NeedResort();
380 this->SortStationsList();
382 this->CreateNestedTree();
383 this->vscroll
= this->GetScrollbar(WID_STL_SCROLLBAR
);
384 this->FinishInitNested(window_number
);
385 this->owner
= (Owner
)this->window_number
;
387 if (this->filter
.cargoes
== ALL_CARGOTYPES
) this->filter
.cargoes
= _cargo_mask
;
389 for (uint i
= 0; i
< 5; i
++) {
390 if (HasBit(this->filter
.facilities
, i
)) this->LowerWidget(i
+ WID_STL_TRAIN
);
393 this->GetWidget
<NWidgetCore
>(WID_STL_SORTDROPBTN
)->widget_data
= this->sorter_names
[this->stations
.SortType()];
396 ~CompanyStationsWindow()
398 /* Save filter state. */
399 this->filter
.last_sorting
= this->stations
.GetListing();
400 CompanyStationsWindow::initial_state
= this->filter
;
403 void UpdateWidgetSize(WidgetID widget
, Dimension
*size
, [[maybe_unused
]] const Dimension
&padding
, [[maybe_unused
]] Dimension
*fill
, [[maybe_unused
]] Dimension
*resize
) override
406 case WID_STL_SORTBY
: {
407 Dimension d
= GetStringBoundingBox(this->GetWidget
<NWidgetCore
>(widget
)->widget_data
);
408 d
.width
+= padding
.width
+ Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
409 d
.height
+= padding
.height
;
410 *size
= maxdim(*size
, d
);
414 case WID_STL_SORTDROPBTN
: {
415 Dimension d
= {0, 0};
416 for (int i
= 0; CompanyStationsWindow::sorter_names
[i
] != INVALID_STRING_ID
; i
++) {
417 d
= maxdim(d
, GetStringBoundingBox(CompanyStationsWindow::sorter_names
[i
]));
419 d
.width
+= padding
.width
;
420 d
.height
+= padding
.height
;
421 *size
= maxdim(*size
, d
);
426 resize
->height
= std::max(GetCharacterHeight(FS_NORMAL
), GetCharacterHeight(FS_SMALL
) + ScaleGUITrad(3));
427 size
->height
= padding
.height
+ 5 * resize
->height
;
429 /* Determine appropriate width for mini station rating graph */
430 this->rating_width
= 0;
431 for (const CargoSpec
*cs
: _sorted_standard_cargo_specs
) {
432 this->rating_width
= std::max(this->rating_width
, GetStringBoundingBox(cs
->abbrev
, FS_SMALL
).width
);
434 /* Approximately match original 16 pixel wide rating bars by multiplying string width by 1.6 */
435 this->rating_width
= this->rating_width
* 16 / 10;
440 void OnPaint() override
442 this->BuildStationsList((Owner
)this->window_number
);
443 this->SortStationsList();
448 void DrawWidget(const Rect
&r
, WidgetID widget
) const override
452 /* draw arrow pointing up/down for ascending/descending sorting */
453 this->DrawSortButtonState(WID_STL_SORTBY
, this->stations
.IsDescSortOrder() ? SBS_DOWN
: SBS_UP
);
457 bool rtl
= _current_text_dir
== TD_RTL
;
458 size_t max
= std::min
<size_t>(this->vscroll
->GetPosition() + this->vscroll
->GetCapacity(), this->stations
.size());
459 Rect tr
= r
.Shrink(WidgetDimensions::scaled
.framerect
);
460 uint line_height
= this->GetWidget
<NWidgetBase
>(widget
)->resize_y
;
461 /* Spacing between station name and first rating graph. */
462 int text_spacing
= WidgetDimensions::scaled
.hsep_wide
;
463 /* Spacing between additional rating graphs. */
464 int rating_spacing
= WidgetDimensions::scaled
.hsep_normal
;
466 for (size_t i
= this->vscroll
->GetPosition(); i
< max
; ++i
) { // do until max number of stations of owner
467 const Station
*st
= this->stations
[i
];
468 assert(st
->xy
!= INVALID_TILE
);
470 /* Do not do the complex check HasStationInUse here, it may be even false
471 * when the order had been removed and the station list hasn't been removed yet */
472 assert(st
->owner
== owner
|| st
->owner
== OWNER_NONE
);
474 SetDParam(0, st
->index
);
475 SetDParam(1, st
->facilities
);
476 int x
= DrawString(tr
.left
, tr
.right
, tr
.top
+ (line_height
- GetCharacterHeight(FS_NORMAL
)) / 2, STR_STATION_LIST_STATION
);
477 x
+= rtl
? -text_spacing
: text_spacing
;
479 /* show cargo waiting and station ratings */
480 for (const CargoSpec
*cs
: _sorted_standard_cargo_specs
) {
481 CargoID cid
= cs
->Index();
482 if (st
->goods
[cid
].HasRating()) {
483 /* For RTL we work in exactly the opposite direction. So
484 * decrement the space needed first, then draw to the left
485 * instead of drawing to the left and then incrementing
488 x
-= rating_width
+ rating_spacing
;
489 if (x
< tr
.left
) break;
491 StationsWndShowStationRating(x
, x
+ rating_width
, tr
.top
, cid
, st
->goods
[cid
].cargo
.TotalCount(), st
->goods
[cid
].rating
);
493 x
+= rating_width
+ rating_spacing
;
494 if (x
> tr
.right
) break;
498 tr
.top
+= line_height
;
501 if (this->vscroll
->GetCount() == 0) { // company has no stations
502 DrawString(tr
.left
, tr
.right
, tr
.top
+ (line_height
- GetCharacterHeight(FS_NORMAL
)) / 2, STR_STATION_LIST_NONE
);
510 void SetStringParameters(WidgetID widget
) const override
512 if (widget
== WID_STL_CAPTION
) {
513 SetDParam(0, this->window_number
);
514 SetDParam(1, this->vscroll
->GetCount());
517 if (widget
== WID_STL_CARGODROPDOWN
) {
518 if (this->filter
.cargoes
== 0) {
519 SetDParam(0, this->filter
.include_no_rating
? STR_STATION_LIST_CARGO_FILTER_ONLY_NO_RATING
: STR_STATION_LIST_CARGO_FILTER_NO_CARGO_TYPES
);
520 } else if (this->filter
.cargoes
== _cargo_mask
) {
521 SetDParam(0, this->filter
.include_no_rating
? STR_STATION_LIST_CARGO_FILTER_ALL_AND_NO_RATING
: STR_CARGO_TYPE_FILTER_ALL
);
522 } else if (CountBits(this->filter
.cargoes
) == 1 && !this->filter
.include_no_rating
) {
523 SetDParam(0, CargoSpec::Get(FindFirstBit(this->filter
.cargoes
))->name
);
525 SetDParam(0, STR_STATION_LIST_CARGO_FILTER_MULTIPLE
);
530 DropDownList
BuildCargoDropDownList(bool expanded
) const
532 /* Define a custom item consisting of check mark, count string, icon and name string. */
533 using DropDownListCargoItem
= DropDownCheck
<DropDownString
<DropDownListIconItem
, FS_SMALL
, true>>;
536 list
.push_back(std::make_unique
<DropDownListStringItem
>(STR_STATION_LIST_CARGO_FILTER_SELECT_ALL
, CargoFilterCriteria::CF_SELECT_ALL
));
537 list
.push_back(std::make_unique
<DropDownListDividerItem
>(-1));
539 bool any_hidden
= false;
541 uint16_t count
= this->stations_per_cargo_type_no_rating
;
542 if (count
== 0 && !expanded
) {
545 list
.push_back(std::make_unique
<DropDownString
<DropDownListCheckedItem
, FS_SMALL
, true>>(fmt::format("{}", count
), this->filter
.include_no_rating
, STR_STATION_LIST_CARGO_FILTER_NO_RATING
, CargoFilterCriteria::CF_NO_RATING
, false, count
== 0));
548 Dimension d
= GetLargestCargoIconSize();
549 for (const CargoSpec
*cs
: _sorted_cargo_specs
) {
550 count
= this->stations_per_cargo_type
[cs
->Index()];
551 if (count
== 0 && !expanded
) {
554 list
.push_back(std::make_unique
<DropDownListCargoItem
>(HasBit(this->filter
.cargoes
, cs
->Index()), fmt::format("{}", count
), d
, cs
->GetCargoIcon(), PAL_NONE
, cs
->name
, cs
->Index(), false, count
== 0));
558 if (!expanded
&& any_hidden
) {
559 if (list
.size() > 2) list
.push_back(std::make_unique
<DropDownListDividerItem
>(-1));
560 list
.push_back(std::make_unique
<DropDownListStringItem
>(STR_STATION_LIST_CARGO_FILTER_EXPAND
, CargoFilterCriteria::CF_EXPAND_LIST
));
566 void OnClick([[maybe_unused
]] Point pt
, WidgetID widget
, [[maybe_unused
]] int click_count
) override
570 auto it
= this->vscroll
->GetScrolledItemFromWidget(this->stations
, pt
.y
, this, WID_STL_LIST
);
571 if (it
== this->stations
.end()) return; // click out of list bound
573 const Station
*st
= *it
;
574 /* do not check HasStationInUse - it is slow and may be invalid */
575 assert(st
->owner
== (Owner
)this->window_number
|| st
->owner
== OWNER_NONE
);
578 ShowExtraViewportWindow(st
->xy
);
580 ScrollMainWindowToTile(st
->xy
);
588 case WID_STL_AIRPLANE
:
591 ToggleBit(this->filter
.facilities
, widget
- WID_STL_TRAIN
);
592 this->ToggleWidgetLoweredState(widget
);
594 for (uint i
: SetBitIterator(this->filter
.facilities
)) {
595 this->RaiseWidget(i
+ WID_STL_TRAIN
);
597 this->filter
.facilities
= 1 << (widget
- WID_STL_TRAIN
);
598 this->LowerWidget(widget
);
600 this->stations
.ForceRebuild();
604 case WID_STL_FACILALL
:
605 for (WidgetID i
= WID_STL_TRAIN
; i
<= WID_STL_SHIP
; i
++) {
606 this->LowerWidget(i
);
609 this->filter
.facilities
= FACIL_TRAIN
| FACIL_TRUCK_STOP
| FACIL_BUS_STOP
| FACIL_AIRPORT
| FACIL_DOCK
;
610 this->stations
.ForceRebuild();
614 case WID_STL_SORTBY
: // flip sorting method asc/desc
615 this->stations
.ToggleSortOrder();
619 case WID_STL_SORTDROPBTN
: // select sorting criteria dropdown menu
620 ShowDropDownMenu(this, this->sorter_names
, this->stations
.SortType(), WID_STL_SORTDROPBTN
, 0, 0);
623 case WID_STL_CARGODROPDOWN
:
624 this->filter_expanded
= false;
625 ShowDropDownList(this, this->BuildCargoDropDownList(this->filter_expanded
), -1, widget
, 0, false, true);
630 void OnDropdownSelect(int widget
, int index
) override
632 if (widget
== WID_STL_SORTDROPBTN
) {
633 if (this->stations
.SortType() != index
) {
634 this->stations
.SetSortType(index
);
636 /* Display the current sort variant */
637 this->GetWidget
<NWidgetCore
>(WID_STL_SORTDROPBTN
)->widget_data
= this->sorter_names
[this->stations
.SortType()];
643 if (widget
== WID_STL_CARGODROPDOWN
) {
644 FilterState oldstate
= this->filter
;
646 if (index
>= 0 && index
< NUM_CARGO
) {
648 ToggleBit(this->filter
.cargoes
, index
);
650 this->filter
.cargoes
= 1ULL << index
;
651 this->filter
.include_no_rating
= false;
653 } else if (index
== CargoFilterCriteria::CF_NO_RATING
) {
655 this->filter
.include_no_rating
= !this->filter
.include_no_rating
;
657 this->filter
.include_no_rating
= true;
658 this->filter
.cargoes
= 0;
660 } else if (index
== CargoFilterCriteria::CF_SELECT_ALL
) {
661 this->filter
.cargoes
= _cargo_mask
;
662 this->filter
.include_no_rating
= true;
663 } else if (index
== CargoFilterCriteria::CF_EXPAND_LIST
) {
664 this->filter_expanded
= true;
665 ReplaceDropDownList(this, this->BuildCargoDropDownList(this->filter_expanded
));
669 if (oldstate
.cargoes
!= this->filter
.cargoes
|| oldstate
.include_no_rating
!= this->filter
.include_no_rating
) {
670 this->stations
.ForceRebuild();
673 /* Only refresh the list if it's changed. */
674 if (_ctrl_pressed
) ReplaceDropDownList(this, this->BuildCargoDropDownList(this->filter_expanded
));
677 /* Always close the list if ctrl is not pressed. */
678 if (!_ctrl_pressed
) this->CloseChildWindows(WC_DROPDOWN_MENU
);
682 void OnGameTick() override
684 if (this->stations
.NeedResort()) {
685 Debug(misc
, 3, "Periodic rebuild station list company {}", this->window_number
);
690 void OnResize() override
692 this->vscroll
->SetCapacityFromWidget(this, WID_STL_LIST
, WidgetDimensions::scaled
.framerect
.Vertical());
696 * Some data on this window has become invalid.
697 * @param data Information about the changed data.
698 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
700 void OnInvalidateData([[maybe_unused
]] int data
= 0, [[maybe_unused
]] bool gui_scope
= true) override
703 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
704 this->stations
.ForceRebuild();
706 this->stations
.ForceResort();
711 /* Available station sorting functions */
712 GUIStationList::SortFunction
* const CompanyStationsWindow::sorter_funcs
[] = {
715 &StationWaitingTotalSorter
,
716 &StationWaitingAvailableSorter
,
717 &StationRatingMaxSorter
,
718 &StationRatingMinSorter
721 /* Names of the sorting functions */
722 const StringID
CompanyStationsWindow::sorter_names
[] = {
724 STR_SORT_BY_FACILITY
,
725 STR_SORT_BY_WAITING_TOTAL
,
726 STR_SORT_BY_WAITING_AVAILABLE
,
727 STR_SORT_BY_RATING_MAX
,
728 STR_SORT_BY_RATING_MIN
,
732 static constexpr NWidgetPart _nested_company_stations_widgets
[] = {
733 NWidget(NWID_HORIZONTAL
),
734 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
735 NWidget(WWT_CAPTION
, COLOUR_GREY
, WID_STL_CAPTION
), SetDataTip(STR_STATION_LIST_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
736 NWidget(WWT_SHADEBOX
, COLOUR_GREY
),
737 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
738 NWidget(WWT_STICKYBOX
, COLOUR_GREY
),
740 NWidget(NWID_HORIZONTAL
),
741 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_STL_TRAIN
), SetMinimalSize(14, 0), SetDataTip(STR_TRAIN
, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE
), SetFill(0, 1),
742 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_STL_TRUCK
), SetMinimalSize(14, 0), SetDataTip(STR_LORRY
, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE
), SetFill(0, 1),
743 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_STL_BUS
), SetMinimalSize(14, 0), SetDataTip(STR_BUS
, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE
), SetFill(0, 1),
744 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_STL_SHIP
), SetMinimalSize(14, 0), SetDataTip(STR_SHIP
, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE
), SetFill(0, 1),
745 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_STL_AIRPLANE
), SetMinimalSize(14, 0), SetDataTip(STR_PLANE
, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE
), SetFill(0, 1),
746 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_STL_FACILALL
), SetMinimalSize(14, 0), SetDataTip(STR_ABBREV_ALL
, STR_STATION_LIST_SELECT_ALL_FACILITIES
), SetTextStyle(TC_BLACK
, FS_SMALL
), SetFill(0, 1),
747 NWidget(WWT_PANEL
, COLOUR_GREY
), SetMinimalSize(5, 0), SetFill(0, 1), EndContainer(),
748 NWidget(WWT_DROPDOWN
, COLOUR_GREY
, WID_STL_CARGODROPDOWN
), SetFill(1, 0), SetDataTip(STR_JUST_STRING
, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE
),
749 NWidget(WWT_PANEL
, COLOUR_GREY
), SetResize(1, 0), SetFill(1, 1), EndContainer(),
751 NWidget(NWID_HORIZONTAL
),
752 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_STL_SORTBY
), SetMinimalSize(81, 12), SetDataTip(STR_BUTTON_SORT_BY
, STR_TOOLTIP_SORT_ORDER
),
753 NWidget(WWT_DROPDOWN
, COLOUR_GREY
, WID_STL_SORTDROPBTN
), SetMinimalSize(163, 12), SetDataTip(STR_SORT_BY_NAME
, STR_TOOLTIP_SORT_CRITERIA
), // widget_data gets overwritten.
754 NWidget(WWT_PANEL
, COLOUR_GREY
), SetResize(1, 0), SetFill(1, 1), EndContainer(),
756 NWidget(NWID_HORIZONTAL
),
757 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_STL_LIST
), SetMinimalSize(346, 125), SetResize(1, 10), SetDataTip(0x0, STR_STATION_LIST_TOOLTIP
), SetScrollbar(WID_STL_SCROLLBAR
), EndContainer(),
758 NWidget(NWID_VERTICAL
),
759 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_STL_SCROLLBAR
),
760 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
),
765 static WindowDesc
_company_stations_desc(__FILE__
, __LINE__
,
766 WDP_AUTO
, "list_stations", 358, 162,
767 WC_STATION_LIST
, WC_NONE
,
769 std::begin(_nested_company_stations_widgets
), std::end(_nested_company_stations_widgets
)
773 * Opens window with list of company's stations
775 * @param company whose stations' list show
777 void ShowCompanyStations(CompanyID company
)
779 if (!Company::IsValidID(company
)) return;
781 AllocateWindowDescFront
<CompanyStationsWindow
>(&_company_stations_desc
, company
);
784 static constexpr NWidgetPart _nested_station_view_widgets
[] = {
785 NWidget(NWID_HORIZONTAL
),
786 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
787 NWidget(WWT_PUSHIMGBTN
, COLOUR_GREY
, WID_SV_RENAME
), SetMinimalSize(12, 14), SetDataTip(SPR_RENAME
, STR_STATION_VIEW_RENAME_TOOLTIP
),
788 NWidget(WWT_CAPTION
, COLOUR_GREY
, WID_SV_CAPTION
), SetDataTip(STR_STATION_VIEW_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
789 NWidget(WWT_PUSHIMGBTN
, COLOUR_GREY
, WID_SV_LOCATION
), SetMinimalSize(12, 14), SetDataTip(SPR_GOTO_LOCATION
, STR_STATION_VIEW_CENTER_TOOLTIP
),
790 NWidget(WWT_SHADEBOX
, COLOUR_GREY
),
791 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
792 NWidget(WWT_STICKYBOX
, COLOUR_GREY
),
794 NWidget(NWID_HORIZONTAL
),
795 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_SV_GROUP
), SetMinimalSize(81, 12), SetFill(1, 1), SetDataTip(STR_STATION_VIEW_GROUP
, 0x0),
796 NWidget(WWT_DROPDOWN
, COLOUR_GREY
, WID_SV_GROUP_BY
), SetMinimalSize(168, 12), SetResize(1, 0), SetFill(0, 1), SetDataTip(0x0, STR_TOOLTIP_GROUP_ORDER
),
798 NWidget(NWID_HORIZONTAL
),
799 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SV_SORT_ORDER
), SetMinimalSize(81, 12), SetFill(1, 1), SetDataTip(STR_BUTTON_SORT_BY
, STR_TOOLTIP_SORT_ORDER
),
800 NWidget(WWT_DROPDOWN
, COLOUR_GREY
, WID_SV_SORT_BY
), SetMinimalSize(168, 12), SetResize(1, 0), SetFill(0, 1), SetDataTip(0x0, STR_TOOLTIP_SORT_CRITERIA
),
802 NWidget(NWID_HORIZONTAL
),
803 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_SV_WAITING
), SetMinimalSize(237, 44), SetResize(1, 10), SetScrollbar(WID_SV_SCROLLBAR
), EndContainer(),
804 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_SV_SCROLLBAR
),
806 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_SV_ACCEPT_RATING_LIST
), SetMinimalSize(249, 23), SetResize(1, 0), EndContainer(),
807 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
808 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SV_ACCEPTS_RATINGS
), SetMinimalSize(46, 12), SetResize(1, 0), SetFill(1, 1),
809 SetDataTip(STR_STATION_VIEW_RATINGS_BUTTON
, STR_STATION_VIEW_RATINGS_TOOLTIP
),
810 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_SV_CLOSE_AIRPORT
), SetMinimalSize(45, 12), SetResize(1, 0), SetFill(1, 1),
811 SetDataTip(STR_STATION_VIEW_CLOSE_AIRPORT
, STR_STATION_VIEW_CLOSE_AIRPORT_TOOLTIP
),
812 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_SV_CATCHMENT
), SetMinimalSize(45, 12), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_BUTTON_CATCHMENT
, STR_TOOLTIP_CATCHMENT
),
813 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SV_TRAINS
), SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_TRAIN
, STR_STATION_VIEW_SCHEDULED_TRAINS_TOOLTIP
),
814 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SV_ROADVEHS
), SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_LORRY
, STR_STATION_VIEW_SCHEDULED_ROAD_VEHICLES_TOOLTIP
),
815 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SV_SHIPS
), SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_SHIP
, STR_STATION_VIEW_SCHEDULED_SHIPS_TOOLTIP
),
816 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SV_PLANES
), SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_PLANE
, STR_STATION_VIEW_SCHEDULED_AIRCRAFT_TOOLTIP
),
817 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
),
822 * Draws icons of waiting cargo in the StationView window
824 * @param i type of cargo
825 * @param waiting number of waiting units
826 * @param left left most coordinate to draw on
827 * @param right right most coordinate to draw on
828 * @param y y coordinate
830 static void DrawCargoIcons(CargoID i
, uint waiting
, int left
, int right
, int y
)
832 int width
= ScaleSpriteTrad(10);
833 uint num
= std::min
<uint
>((waiting
+ (width
/ 2)) / width
, (right
- left
) / width
); // maximum is width / 10 icons so it won't overflow
834 if (num
== 0) return;
836 SpriteID sprite
= CargoSpec::Get(i
)->GetCargoIcon();
838 int x
= _current_text_dir
== TD_RTL
? left
: right
- num
* width
;
840 DrawSprite(sprite
, PAL_NONE
, x
, y
);
850 class CargoDataEntry
;
852 enum class CargoSortType
: byte
{
853 AsGrouping
, ///< by the same principle the entries are being grouped
854 Count
, ///< by amount of cargo
855 StationString
, ///< by station name
856 StationID
, ///< by station id
857 CargoID
, ///< by cargo id
862 CargoSorter(CargoSortType t
= CargoSortType::StationID
, SortOrder o
= SO_ASCENDING
) : type(t
), order(o
) {}
863 CargoSortType
GetSortType() {return this->type
;}
864 bool operator()(const CargoDataEntry
*cd1
, const CargoDataEntry
*cd2
) const;
871 bool SortId(Tid st1
, Tid st2
) const;
872 bool SortCount(const CargoDataEntry
*cd1
, const CargoDataEntry
*cd2
) const;
873 bool SortStation (StationID st1
, StationID st2
) const;
876 typedef std::set
<CargoDataEntry
*, CargoSorter
> CargoDataSet
;
879 * A cargo data entry representing one possible row in the station view window's
880 * top part. Cargo data entries form a tree where each entry can have several
881 * children. Parents keep track of the sums of their childrens' cargo counts.
883 class CargoDataEntry
{
889 * Insert a new child or retrieve an existing child using a station ID as ID.
890 * @param station ID of the station for which an entry shall be created or retrieved
891 * @return a child entry associated with the given station.
893 CargoDataEntry
*InsertOrRetrieve(StationID station
)
895 return this->InsertOrRetrieve
<StationID
>(station
);
899 * Insert a new child or retrieve an existing child using a cargo ID as ID.
900 * @param cargo ID of the cargo for which an entry shall be created or retrieved
901 * @return a child entry associated with the given cargo.
903 CargoDataEntry
*InsertOrRetrieve(CargoID cargo
)
905 return this->InsertOrRetrieve
<CargoID
>(cargo
);
908 void Update(uint count
);
911 * Remove a child associated with the given station.
912 * @param station ID of the station for which the child should be removed.
914 void Remove(StationID station
)
916 CargoDataEntry
t(station
);
921 * Remove a child associated with the given cargo.
922 * @param cargo ID of the cargo for which the child should be removed.
924 void Remove(CargoID cargo
)
926 CargoDataEntry
t(cargo
);
931 * Retrieve a child for the given station. Return nullptr if it doesn't exist.
932 * @param station ID of the station the child we're looking for is associated with.
933 * @return a child entry for the given station or nullptr.
935 CargoDataEntry
*Retrieve(StationID station
) const
937 CargoDataEntry
t(station
);
938 return this->Retrieve(this->children
->find(&t
));
942 * Retrieve a child for the given cargo. Return nullptr if it doesn't exist.
943 * @param cargo ID of the cargo the child we're looking for is associated with.
944 * @return a child entry for the given cargo or nullptr.
946 CargoDataEntry
*Retrieve(CargoID cargo
) const
948 CargoDataEntry
t(cargo
);
949 return this->Retrieve(this->children
->find(&t
));
952 void Resort(CargoSortType type
, SortOrder order
);
955 * Get the station ID for this entry.
957 StationID
GetStation() const { return this->station
; }
960 * Get the cargo ID for this entry.
962 CargoID
GetCargo() const { return this->cargo
; }
965 * Get the cargo count for this entry.
967 uint
GetCount() const { return this->count
; }
970 * Get the parent entry for this entry.
972 CargoDataEntry
*GetParent() const { return this->parent
; }
975 * Get the number of children for this entry.
977 uint
GetNumChildren() const { return this->num_children
; }
980 * Get an iterator pointing to the begin of the set of children.
982 CargoDataSet::iterator
Begin() const { return this->children
->begin(); }
985 * Get an iterator pointing to the end of the set of children.
987 CargoDataSet::iterator
End() const { return this->children
->end(); }
990 * Has this entry transfers.
992 bool HasTransfers() const { return this->transfers
; }
995 * Set the transfers state.
997 void SetTransfers(bool value
) { this->transfers
= value
; }
1002 CargoDataEntry(StationID st
, uint c
, CargoDataEntry
*p
);
1003 CargoDataEntry(CargoID car
, uint c
, CargoDataEntry
*p
);
1004 CargoDataEntry(StationID st
);
1005 CargoDataEntry(CargoID car
);
1007 CargoDataEntry
*Retrieve(CargoDataSet::iterator i
) const;
1010 CargoDataEntry
*InsertOrRetrieve(Tid s
);
1012 void Remove(CargoDataEntry
*comp
);
1013 void IncrementSize();
1015 CargoDataEntry
*parent
; ///< the parent of this entry.
1017 StationID station
; ///< ID of the station this entry is associated with.
1019 CargoID cargo
; ///< ID of the cargo this entry is associated with.
1020 bool transfers
; ///< If there are transfers for this cargo.
1023 uint num_children
; ///< the number of subentries belonging to this entry.
1024 uint count
; ///< sum of counts of all children or amount of cargo for this entry.
1025 CargoDataSet
*children
; ///< the children of this entry.
1028 CargoDataEntry::CargoDataEntry() :
1030 station(INVALID_STATION
),
1033 children(new CargoDataSet(CargoSorter(CargoSortType::CargoID
)))
1036 CargoDataEntry::CargoDataEntry(CargoID cargo
, uint count
, CargoDataEntry
*parent
) :
1041 children(new CargoDataSet
)
1044 CargoDataEntry::CargoDataEntry(StationID station
, uint count
, CargoDataEntry
*parent
) :
1049 children(new CargoDataSet
)
1052 CargoDataEntry::CargoDataEntry(StationID station
) :
1060 CargoDataEntry::CargoDataEntry(CargoID cargo
) :
1068 CargoDataEntry::~CargoDataEntry()
1071 delete this->children
;
1075 * Delete all subentries, reset count and num_children and adapt parent's count.
1077 void CargoDataEntry::Clear()
1079 if (this->children
!= nullptr) {
1080 for (auto &it
: *this->children
) {
1084 this->children
->clear();
1086 if (this->parent
!= nullptr) this->parent
->count
-= this->count
;
1088 this->num_children
= 0;
1092 * Remove a subentry from this one and delete it.
1093 * @param child the entry to be removed. This may also be a synthetic entry
1094 * which only contains the ID of the entry to be removed. In this case child is
1097 void CargoDataEntry::Remove(CargoDataEntry
*child
)
1099 CargoDataSet::iterator i
= this->children
->find(child
);
1100 if (i
!= this->children
->end()) {
1102 this->children
->erase(i
);
1107 * Retrieve a subentry or insert it if it doesn't exist, yet.
1108 * @tparam ID type of ID: either StationID or CargoID
1109 * @param child_id ID of the child to be inserted or retrieved.
1110 * @return the new or retrieved subentry
1113 CargoDataEntry
*CargoDataEntry::InsertOrRetrieve(Tid child_id
)
1115 CargoDataEntry
tmp(child_id
);
1116 CargoDataSet::iterator i
= this->children
->find(&tmp
);
1117 if (i
== this->children
->end()) {
1119 return *(this->children
->insert(new CargoDataEntry(child_id
, 0, this)).first
);
1121 CargoDataEntry
*ret
= *i
;
1122 assert(this->children
->value_comp().GetSortType() != CargoSortType::Count
);
1128 * Update the count for this entry and propagate the change to the parent entry
1130 * @param count the amount to be added to this entry
1132 void CargoDataEntry::Update(uint count
)
1134 this->count
+= count
;
1135 if (this->parent
!= nullptr) this->parent
->Update(count
);
1141 void CargoDataEntry::IncrementSize()
1143 ++this->num_children
;
1144 if (this->parent
!= nullptr) this->parent
->IncrementSize();
1147 void CargoDataEntry::Resort(CargoSortType type
, SortOrder order
)
1149 CargoDataSet
*new_subs
= new CargoDataSet(this->children
->begin(), this->children
->end(), CargoSorter(type
, order
));
1150 delete this->children
;
1151 this->children
= new_subs
;
1154 CargoDataEntry
*CargoDataEntry::Retrieve(CargoDataSet::iterator i
) const
1156 if (i
== this->children
->end()) {
1159 assert(this->children
->value_comp().GetSortType() != CargoSortType::Count
);
1164 bool CargoSorter::operator()(const CargoDataEntry
*cd1
, const CargoDataEntry
*cd2
) const
1166 switch (this->type
) {
1167 case CargoSortType::StationID
:
1168 return this->SortId
<StationID
>(cd1
->GetStation(), cd2
->GetStation());
1169 case CargoSortType::CargoID
:
1170 return this->SortId
<CargoID
>(cd1
->GetCargo(), cd2
->GetCargo());
1171 case CargoSortType::Count
:
1172 return this->SortCount(cd1
, cd2
);
1173 case CargoSortType::StationString
:
1174 return this->SortStation(cd1
->GetStation(), cd2
->GetStation());
1181 bool CargoSorter::SortId(Tid st1
, Tid st2
) const
1183 return (this->order
== SO_ASCENDING
) ? st1
< st2
: st2
< st1
;
1186 bool CargoSorter::SortCount(const CargoDataEntry
*cd1
, const CargoDataEntry
*cd2
) const
1188 uint c1
= cd1
->GetCount();
1189 uint c2
= cd2
->GetCount();
1191 return this->SortStation(cd1
->GetStation(), cd2
->GetStation());
1192 } else if (this->order
== SO_ASCENDING
) {
1199 bool CargoSorter::SortStation(StationID st1
, StationID st2
) const
1201 if (!Station::IsValidID(st1
)) {
1202 return Station::IsValidID(st2
) ? this->order
== SO_ASCENDING
: this->SortId(st1
, st2
);
1203 } else if (!Station::IsValidID(st2
)) {
1204 return order
== SO_DESCENDING
;
1207 int res
= StrNaturalCompare(Station::Get(st1
)->GetCachedName(), Station::Get(st2
)->GetCachedName()); // Sort by name (natural sorting).
1209 return this->SortId(st1
, st2
);
1211 return (this->order
== SO_ASCENDING
) ? res
< 0 : res
> 0;
1216 * The StationView window
1218 struct StationViewWindow
: public Window
{
1220 * A row being displayed in the cargo view (as opposed to being "hidden" behind a plus sign).
1223 RowDisplay(CargoDataEntry
*f
, StationID n
) : filter(f
), next_station(n
) {}
1224 RowDisplay(CargoDataEntry
*f
, CargoID n
) : filter(f
), next_cargo(n
) {}
1227 * Parent of the cargo entry belonging to the row.
1229 CargoDataEntry
*filter
;
1232 * ID of the station belonging to the entry actually displayed if it's to/from/via.
1234 StationID next_station
;
1237 * ID of the cargo belonging to the entry actually displayed if it's cargo.
1243 typedef std::vector
<RowDisplay
> CargoDataVector
;
1245 static const int NUM_COLUMNS
= 4; ///< Number of "columns" in the cargo view: cargo, from, via, to
1248 * Type of data invalidation.
1251 INV_FLOWS
= 0x100, ///< The planned flows have been recalculated and everything has to be updated.
1252 INV_CARGO
= 0x200 ///< Some cargo has been added or removed.
1256 * Type of grouping used in each of the "columns".
1259 GR_SOURCE
, ///< Group by source of cargo ("from").
1260 GR_NEXT
, ///< Group by next station ("via").
1261 GR_DESTINATION
, ///< Group by estimated final destination ("to").
1262 GR_CARGO
, ///< Group by cargo type.
1266 * Display mode of the cargo view.
1269 MODE_WAITING
, ///< Show cargo waiting at the station.
1270 MODE_PLANNED
///< Show cargo planned to pass through the station.
1273 uint expand_shrink_width
; ///< The width allocated to the expand/shrink 'button'
1274 int rating_lines
; ///< Number of lines in the cargo ratings view.
1275 int accepts_lines
; ///< Number of lines in the accepted cargo view.
1278 /** Height of the #WID_SV_ACCEPT_RATING_LIST widget for different views. */
1279 enum AcceptListHeight
{
1280 ALH_RATING
= 13, ///< Height of the cargo ratings view.
1281 ALH_ACCEPTS
= 3, ///< Height of the accepted cargo view.
1284 static const StringID _sort_names
[]; ///< Names of the sorting options in the dropdown.
1285 static const StringID _group_names
[]; ///< Names of the grouping options in the dropdown.
1288 * Sort types of the different 'columns'.
1289 * In fact only CargoSortType::Count and CargoSortType::AsGrouping are active and you can only
1290 * sort all the columns in the same way. The other options haven't been
1291 * included in the GUI due to lack of space.
1293 CargoSortType sortings
[NUM_COLUMNS
];
1295 /** Sort order (ascending/descending) for the 'columns'. */
1296 SortOrder sort_orders
[NUM_COLUMNS
];
1298 int scroll_to_row
; ///< If set, scroll the main viewport to the station pointed to by this row.
1299 int grouping_index
; ///< Currently selected entry in the grouping drop down.
1300 Mode current_mode
; ///< Currently selected display mode of cargo view.
1301 Grouping groupings
[NUM_COLUMNS
]; ///< Grouping modes for the different columns.
1303 CargoDataEntry expanded_rows
; ///< Parent entry of currently expanded rows.
1304 CargoDataEntry cached_destinations
; ///< Cache for the flows passing through this station.
1305 CargoDataVector displayed_rows
; ///< Parent entry of currently displayed rows (including collapsed ones).
1307 StationViewWindow(WindowDesc
*desc
, WindowNumber window_number
) : Window(desc
),
1308 scroll_to_row(INT_MAX
), grouping_index(0)
1310 this->rating_lines
= ALH_RATING
;
1311 this->accepts_lines
= ALH_ACCEPTS
;
1313 this->CreateNestedTree();
1314 this->vscroll
= this->GetScrollbar(WID_SV_SCROLLBAR
);
1315 /* Nested widget tree creation is done in two steps to ensure that this->GetWidget<NWidgetCore>(WID_SV_ACCEPTS_RATINGS) exists in UpdateWidgetSize(). */
1316 this->FinishInitNested(window_number
);
1318 this->groupings
[0] = GR_CARGO
;
1319 this->sortings
[0] = CargoSortType::AsGrouping
;
1320 this->SelectGroupBy(_settings_client
.gui
.station_gui_group_order
);
1321 this->SelectSortBy(_settings_client
.gui
.station_gui_sort_by
);
1322 this->sort_orders
[0] = SO_ASCENDING
;
1323 this->SelectSortOrder((SortOrder
)_settings_client
.gui
.station_gui_sort_order
);
1324 this->owner
= Station::Get(window_number
)->owner
;
1327 void Close([[maybe_unused
]] int data
= 0) override
1329 CloseWindowById(WC_TRAINS_LIST
, VehicleListIdentifier(VL_STATION_LIST
, VEH_TRAIN
, this->owner
, this->window_number
).Pack(), false);
1330 CloseWindowById(WC_ROADVEH_LIST
, VehicleListIdentifier(VL_STATION_LIST
, VEH_ROAD
, this->owner
, this->window_number
).Pack(), false);
1331 CloseWindowById(WC_SHIPS_LIST
, VehicleListIdentifier(VL_STATION_LIST
, VEH_SHIP
, this->owner
, this->window_number
).Pack(), false);
1332 CloseWindowById(WC_AIRCRAFT_LIST
, VehicleListIdentifier(VL_STATION_LIST
, VEH_AIRCRAFT
, this->owner
, this->window_number
).Pack(), false);
1334 SetViewportCatchmentStation(Station::Get(this->window_number
), false);
1335 this->Window::Close();
1339 * Show a certain cargo entry characterized by source/next/dest station, cargo ID and amount of cargo at the
1340 * right place in the cargo view. I.e. update as many rows as are expanded following that characterization.
1341 * @param data Root entry of the tree.
1342 * @param cargo Cargo ID of the entry to be shown.
1343 * @param source Source station of the entry to be shown.
1344 * @param next Next station the cargo to be shown will visit.
1345 * @param dest Final destination of the cargo to be shown.
1346 * @param count Amount of cargo to be shown.
1348 void ShowCargo(CargoDataEntry
*data
, CargoID cargo
, StationID source
, StationID next
, StationID dest
, uint count
)
1350 if (count
== 0) return;
1351 bool auto_distributed
= _settings_game
.linkgraph
.GetDistributionType(cargo
) != DT_MANUAL
;
1352 const CargoDataEntry
*expand
= &this->expanded_rows
;
1353 for (int i
= 0; i
< NUM_COLUMNS
&& expand
!= nullptr; ++i
) {
1354 switch (groupings
[i
]) {
1357 data
= data
->InsertOrRetrieve(cargo
);
1358 data
->SetTransfers(source
!= this->window_number
);
1359 expand
= expand
->Retrieve(cargo
);
1362 if (auto_distributed
|| source
!= this->window_number
) {
1363 data
= data
->InsertOrRetrieve(source
);
1364 expand
= expand
->Retrieve(source
);
1368 if (auto_distributed
) {
1369 data
= data
->InsertOrRetrieve(next
);
1370 expand
= expand
->Retrieve(next
);
1373 case GR_DESTINATION
:
1374 if (auto_distributed
) {
1375 data
= data
->InsertOrRetrieve(dest
);
1376 expand
= expand
->Retrieve(dest
);
1381 data
->Update(count
);
1384 void UpdateWidgetSize(WidgetID widget
, Dimension
*size
, [[maybe_unused
]] const Dimension
&padding
, [[maybe_unused
]] Dimension
*fill
, [[maybe_unused
]] Dimension
*resize
) override
1387 case WID_SV_WAITING
:
1388 resize
->height
= GetCharacterHeight(FS_NORMAL
);
1389 size
->height
= 4 * resize
->height
+ padding
.height
;
1390 this->expand_shrink_width
= std::max(GetStringBoundingBox("-").width
, GetStringBoundingBox("+").width
);
1393 case WID_SV_ACCEPT_RATING_LIST
:
1394 size
->height
= ((this->GetWidget
<NWidgetCore
>(WID_SV_ACCEPTS_RATINGS
)->widget_data
== STR_STATION_VIEW_RATINGS_BUTTON
) ? this->accepts_lines
: this->rating_lines
) * GetCharacterHeight(FS_NORMAL
) + padding
.height
;
1397 case WID_SV_CLOSE_AIRPORT
:
1398 if (!(Station::Get(this->window_number
)->facilities
& FACIL_AIRPORT
)) {
1399 /* Hide 'Close Airport' button if no airport present. */
1408 void OnPaint() override
1410 const Station
*st
= Station::Get(this->window_number
);
1411 CargoDataEntry cargo
;
1412 BuildCargoList(&cargo
, st
);
1414 this->vscroll
->SetCount(cargo
.GetNumChildren()); // update scrollbar
1416 /* disable some buttons */
1417 this->SetWidgetDisabledState(WID_SV_RENAME
, st
->owner
!= _local_company
);
1418 this->SetWidgetDisabledState(WID_SV_TRAINS
, !(st
->facilities
& FACIL_TRAIN
));
1419 this->SetWidgetDisabledState(WID_SV_ROADVEHS
, !(st
->facilities
& FACIL_TRUCK_STOP
) && !(st
->facilities
& FACIL_BUS_STOP
));
1420 this->SetWidgetDisabledState(WID_SV_SHIPS
, !(st
->facilities
& FACIL_DOCK
));
1421 this->SetWidgetDisabledState(WID_SV_PLANES
, !(st
->facilities
& FACIL_AIRPORT
));
1422 this->SetWidgetDisabledState(WID_SV_CLOSE_AIRPORT
, !(st
->facilities
& FACIL_AIRPORT
) || st
->owner
!= _local_company
|| st
->owner
== OWNER_NONE
); // Also consider SE, where _local_company == OWNER_NONE
1423 this->SetWidgetLoweredState(WID_SV_CLOSE_AIRPORT
, (st
->facilities
& FACIL_AIRPORT
) && (st
->airport
.flags
& AIRPORT_CLOSED_block
) != 0);
1425 extern const Station
*_viewport_highlight_station
;
1426 this->SetWidgetDisabledState(WID_SV_CATCHMENT
, st
->facilities
== FACIL_NONE
);
1427 this->SetWidgetLoweredState(WID_SV_CATCHMENT
, _viewport_highlight_station
== st
);
1429 this->DrawWidgets();
1431 if (!this->IsShaded()) {
1432 /* Draw 'accepted cargo' or 'cargo ratings'. */
1433 const NWidgetBase
*wid
= this->GetWidget
<NWidgetBase
>(WID_SV_ACCEPT_RATING_LIST
);
1434 const Rect r
= wid
->GetCurrentRect();
1435 if (this->GetWidget
<NWidgetCore
>(WID_SV_ACCEPTS_RATINGS
)->widget_data
== STR_STATION_VIEW_RATINGS_BUTTON
) {
1436 int lines
= this->DrawAcceptedCargo(r
);
1437 if (lines
> this->accepts_lines
) { // Resize the widget, and perform re-initialization of the window.
1438 this->accepts_lines
= lines
;
1443 int lines
= this->DrawCargoRatings(r
);
1444 if (lines
> this->rating_lines
) { // Resize the widget, and perform re-initialization of the window.
1445 this->rating_lines
= lines
;
1451 /* Draw arrow pointing up/down for ascending/descending sorting */
1452 this->DrawSortButtonState(WID_SV_SORT_ORDER
, sort_orders
[1] == SO_ASCENDING
? SBS_UP
: SBS_DOWN
);
1454 int pos
= this->vscroll
->GetPosition();
1456 int maxrows
= this->vscroll
->GetCapacity();
1458 displayed_rows
.clear();
1460 /* Draw waiting cargo. */
1461 NWidgetBase
*nwi
= this->GetWidget
<NWidgetBase
>(WID_SV_WAITING
);
1462 Rect waiting_rect
= nwi
->GetCurrentRect().Shrink(WidgetDimensions::scaled
.framerect
);
1463 this->DrawEntries(&cargo
, waiting_rect
, pos
, maxrows
, 0);
1464 scroll_to_row
= INT_MAX
;
1468 void SetStringParameters(WidgetID widget
) const override
1470 if (widget
== WID_SV_CAPTION
) {
1471 const Station
*st
= Station::Get(this->window_number
);
1472 SetDParam(0, st
->index
);
1473 SetDParam(1, st
->facilities
);
1478 * Rebuild the cache for estimated destinations which is used to quickly show the "destination" entries
1479 * even if we actually don't know the destination of a certain packet from just looking at it.
1480 * @param i Cargo to recalculate the cache for.
1482 void RecalcDestinations(CargoID i
)
1484 const Station
*st
= Station::Get(this->window_number
);
1485 CargoDataEntry
*cargo_entry
= cached_destinations
.InsertOrRetrieve(i
);
1486 cargo_entry
->Clear();
1488 for (const auto &it
: st
->goods
[i
].flows
) {
1489 StationID from
= it
.first
;
1490 CargoDataEntry
*source_entry
= cargo_entry
->InsertOrRetrieve(from
);
1491 uint32_t prev_count
= 0;
1492 for (const auto &flow_it
: *it
.second
.GetShares()) {
1493 StationID via
= flow_it
.second
;
1494 CargoDataEntry
*via_entry
= source_entry
->InsertOrRetrieve(via
);
1495 if (via
== this->window_number
) {
1496 via_entry
->InsertOrRetrieve(via
)->Update(flow_it
.first
- prev_count
);
1498 EstimateDestinations(i
, from
, via
, flow_it
.first
- prev_count
, via_entry
);
1500 prev_count
= flow_it
.first
;
1506 * Estimate the amounts of cargo per final destination for a given cargo, source station and next hop and
1507 * save the result as children of the given CargoDataEntry.
1508 * @param cargo ID of the cargo to estimate destinations for.
1509 * @param source Source station of the given batch of cargo.
1510 * @param next Intermediate hop to start the calculation at ("next hop").
1511 * @param count Size of the batch of cargo.
1512 * @param dest CargoDataEntry to save the results in.
1514 void EstimateDestinations(CargoID cargo
, StationID source
, StationID next
, uint count
, CargoDataEntry
*dest
)
1516 if (Station::IsValidID(next
) && Station::IsValidID(source
)) {
1518 const FlowStatMap
&flowmap
= Station::Get(next
)->goods
[cargo
].flows
;
1519 FlowStatMap::const_iterator map_it
= flowmap
.find(source
);
1520 if (map_it
!= flowmap
.end()) {
1521 const FlowStat::SharesMap
*shares
= map_it
->second
.GetShares();
1522 uint32_t prev_count
= 0;
1523 for (FlowStat::SharesMap::const_iterator i
= shares
->begin(); i
!= shares
->end(); ++i
) {
1524 tmp
.InsertOrRetrieve(i
->second
)->Update(i
->first
- prev_count
);
1525 prev_count
= i
->first
;
1529 if (tmp
.GetCount() == 0) {
1530 dest
->InsertOrRetrieve(INVALID_STATION
)->Update(count
);
1532 uint sum_estimated
= 0;
1533 while (sum_estimated
< count
) {
1534 for (CargoDataSet::iterator i
= tmp
.Begin(); i
!= tmp
.End() && sum_estimated
< count
; ++i
) {
1535 CargoDataEntry
*child
= *i
;
1536 uint estimate
= DivideApprox(child
->GetCount() * count
, tmp
.GetCount());
1537 if (estimate
== 0) estimate
= 1;
1539 sum_estimated
+= estimate
;
1540 if (sum_estimated
> count
) {
1541 estimate
-= sum_estimated
- count
;
1542 sum_estimated
= count
;
1546 if (child
->GetStation() == next
) {
1547 dest
->InsertOrRetrieve(next
)->Update(estimate
);
1549 EstimateDestinations(cargo
, source
, child
->GetStation(), estimate
, dest
);
1557 dest
->InsertOrRetrieve(INVALID_STATION
)->Update(count
);
1562 * Build up the cargo view for PLANNED mode and a specific cargo.
1563 * @param i Cargo to show.
1564 * @param flows The current station's flows for that cargo.
1565 * @param cargo The CargoDataEntry to save the results in.
1567 void BuildFlowList(CargoID i
, const FlowStatMap
&flows
, CargoDataEntry
*cargo
)
1569 const CargoDataEntry
*source_dest
= this->cached_destinations
.Retrieve(i
);
1570 for (FlowStatMap::const_iterator it
= flows
.begin(); it
!= flows
.end(); ++it
) {
1571 StationID from
= it
->first
;
1572 const CargoDataEntry
*source_entry
= source_dest
->Retrieve(from
);
1573 const FlowStat::SharesMap
*shares
= it
->second
.GetShares();
1574 for (FlowStat::SharesMap::const_iterator flow_it
= shares
->begin(); flow_it
!= shares
->end(); ++flow_it
) {
1575 const CargoDataEntry
*via_entry
= source_entry
->Retrieve(flow_it
->second
);
1576 for (CargoDataSet::iterator dest_it
= via_entry
->Begin(); dest_it
!= via_entry
->End(); ++dest_it
) {
1577 CargoDataEntry
*dest_entry
= *dest_it
;
1578 ShowCargo(cargo
, i
, from
, flow_it
->second
, dest_entry
->GetStation(), dest_entry
->GetCount());
1585 * Build up the cargo view for WAITING mode and a specific cargo.
1586 * @param i Cargo to show.
1587 * @param packets The current station's cargo list for that cargo.
1588 * @param cargo The CargoDataEntry to save the result in.
1590 void BuildCargoList(CargoID i
, const StationCargoList
&packets
, CargoDataEntry
*cargo
)
1592 const CargoDataEntry
*source_dest
= this->cached_destinations
.Retrieve(i
);
1593 for (StationCargoList::ConstIterator it
= packets
.Packets()->begin(); it
!= packets
.Packets()->end(); it
++) {
1594 const CargoPacket
*cp
= *it
;
1595 StationID next
= it
.GetKey();
1597 const CargoDataEntry
*source_entry
= source_dest
->Retrieve(cp
->GetFirstStation());
1598 if (source_entry
== nullptr) {
1599 this->ShowCargo(cargo
, i
, cp
->GetFirstStation(), next
, INVALID_STATION
, cp
->Count());
1603 const CargoDataEntry
*via_entry
= source_entry
->Retrieve(next
);
1604 if (via_entry
== nullptr) {
1605 this->ShowCargo(cargo
, i
, cp
->GetFirstStation(), next
, INVALID_STATION
, cp
->Count());
1609 uint remaining
= cp
->Count();
1610 for (CargoDataSet::iterator dest_it
= via_entry
->Begin(); dest_it
!= via_entry
->End();) {
1611 CargoDataEntry
*dest_entry
= *dest_it
;
1613 /* Advance iterator here instead of in the for statement to test whether this is the last entry */
1617 if (dest_it
== via_entry
->End()) {
1618 /* Allocate all remaining waiting cargo to the last destination to avoid
1619 * waiting cargo being "lost", and the displayed total waiting cargo
1620 * not matching GoodsEntry::TotalCount() */
1623 val
= std::min
<uint
>(remaining
, DivideApprox(cp
->Count() * dest_entry
->GetCount(), via_entry
->GetCount()));
1626 this->ShowCargo(cargo
, i
, cp
->GetFirstStation(), next
, dest_entry
->GetStation(), val
);
1629 this->ShowCargo(cargo
, i
, NEW_STATION
, NEW_STATION
, NEW_STATION
, packets
.ReservedCount());
1633 * Build up the cargo view for all cargoes.
1634 * @param cargo The root cargo entry to save all results in.
1635 * @param st The station to calculate the cargo view from.
1637 void BuildCargoList(CargoDataEntry
*cargo
, const Station
*st
)
1639 for (CargoID i
= 0; i
< NUM_CARGO
; i
++) {
1641 if (this->cached_destinations
.Retrieve(i
) == nullptr) {
1642 this->RecalcDestinations(i
);
1645 if (this->current_mode
== MODE_WAITING
) {
1646 this->BuildCargoList(i
, st
->goods
[i
].cargo
, cargo
);
1648 this->BuildFlowList(i
, st
->goods
[i
].flows
, cargo
);
1654 * Mark a specific row, characterized by its CargoDataEntry, as expanded.
1655 * @param data The row to be marked as expanded.
1657 void SetDisplayedRow(const CargoDataEntry
*data
)
1659 std::list
<StationID
> stations
;
1660 const CargoDataEntry
*parent
= data
->GetParent();
1661 if (parent
->GetParent() == nullptr) {
1662 this->displayed_rows
.push_back(RowDisplay(&this->expanded_rows
, data
->GetCargo()));
1666 StationID next
= data
->GetStation();
1667 while (parent
->GetParent()->GetParent() != nullptr) {
1668 stations
.push_back(parent
->GetStation());
1669 parent
= parent
->GetParent();
1672 CargoID cargo
= parent
->GetCargo();
1673 CargoDataEntry
*filter
= this->expanded_rows
.Retrieve(cargo
);
1674 while (!stations
.empty()) {
1675 filter
= filter
->Retrieve(stations
.back());
1676 stations
.pop_back();
1679 this->displayed_rows
.push_back(RowDisplay(filter
, next
));
1683 * Select the correct string for an entry referring to the specified station.
1684 * @param station Station the entry is showing cargo for.
1685 * @param here String to be shown if the entry refers to the same station as this station GUI belongs to.
1686 * @param other_station String to be shown if the entry refers to a specific other station.
1687 * @param any String to be shown if the entry refers to "any station".
1688 * @return One of the three given strings or STR_STATION_VIEW_RESERVED, depending on what station the entry refers to.
1690 StringID
GetEntryString(StationID station
, StringID here
, StringID other_station
, StringID any
)
1692 if (station
== this->window_number
) {
1694 } else if (station
== INVALID_STATION
) {
1696 } else if (station
== NEW_STATION
) {
1697 return STR_STATION_VIEW_RESERVED
;
1699 SetDParam(2, station
);
1700 return other_station
;
1705 * Determine if we need to show the special "non-stop" string.
1706 * @param cd Entry we are going to show.
1707 * @param station Station the entry refers to.
1708 * @param column The "column" the entry will be shown in.
1709 * @return either STR_STATION_VIEW_VIA or STR_STATION_VIEW_NONSTOP.
1711 StringID
SearchNonStop(CargoDataEntry
*cd
, StationID station
, int column
)
1713 CargoDataEntry
*parent
= cd
->GetParent();
1714 for (int i
= column
- 1; i
> 0; --i
) {
1715 if (this->groupings
[i
] == GR_DESTINATION
) {
1716 if (parent
->GetStation() == station
) {
1717 return STR_STATION_VIEW_NONSTOP
;
1719 return STR_STATION_VIEW_VIA
;
1722 parent
= parent
->GetParent();
1725 if (this->groupings
[column
+ 1] == GR_DESTINATION
) {
1726 CargoDataSet::iterator begin
= cd
->Begin();
1727 CargoDataSet::iterator end
= cd
->End();
1728 if (begin
!= end
&& ++(cd
->Begin()) == end
&& (*(begin
))->GetStation() == station
) {
1729 return STR_STATION_VIEW_NONSTOP
;
1731 return STR_STATION_VIEW_VIA
;
1735 return STR_STATION_VIEW_VIA
;
1739 * Draw the given cargo entries in the station GUI.
1740 * @param entry Root entry for all cargo to be drawn.
1741 * @param r Screen rectangle to draw into.
1742 * @param pos Current row to be drawn to (counted down from 0 to -maxrows, same as vscroll->GetPosition()).
1743 * @param maxrows Maximum row to be drawn.
1744 * @param column Current "column" being drawn.
1745 * @param cargo Current cargo being drawn (if cargo column has been passed).
1746 * @return row (in "pos" counting) after the one we have last drawn to.
1748 int DrawEntries(CargoDataEntry
*entry
, const Rect
&r
, int pos
, int maxrows
, int column
, CargoID cargo
= INVALID_CARGO
)
1750 if (this->sortings
[column
] == CargoSortType::AsGrouping
) {
1751 if (this->groupings
[column
] != GR_CARGO
) {
1752 entry
->Resort(CargoSortType::StationString
, this->sort_orders
[column
]);
1755 entry
->Resort(CargoSortType::Count
, this->sort_orders
[column
]);
1757 for (CargoDataSet::iterator i
= entry
->Begin(); i
!= entry
->End(); ++i
) {
1758 CargoDataEntry
*cd
= *i
;
1760 Grouping grouping
= this->groupings
[column
];
1761 if (grouping
== GR_CARGO
) cargo
= cd
->GetCargo();
1762 bool auto_distributed
= _settings_game
.linkgraph
.GetDistributionType(cargo
) != DT_MANUAL
;
1764 if (pos
> -maxrows
&& pos
<= 0) {
1765 StringID str
= STR_EMPTY
;
1766 int y
= r
.top
- pos
* GetCharacterHeight(FS_NORMAL
);
1767 SetDParam(0, cargo
);
1768 SetDParam(1, cd
->GetCount());
1770 if (this->groupings
[column
] == GR_CARGO
) {
1771 str
= STR_STATION_VIEW_WAITING_CARGO
;
1772 DrawCargoIcons(cd
->GetCargo(), cd
->GetCount(), r
.left
+ this->expand_shrink_width
, r
.right
- this->expand_shrink_width
, y
);
1774 if (!auto_distributed
) grouping
= GR_SOURCE
;
1775 StationID station
= cd
->GetStation();
1779 str
= this->GetEntryString(station
, STR_STATION_VIEW_FROM_HERE
, STR_STATION_VIEW_FROM
, STR_STATION_VIEW_FROM_ANY
);
1782 str
= this->GetEntryString(station
, STR_STATION_VIEW_VIA_HERE
, STR_STATION_VIEW_VIA
, STR_STATION_VIEW_VIA_ANY
);
1783 if (str
== STR_STATION_VIEW_VIA
) str
= this->SearchNonStop(cd
, station
, column
);
1785 case GR_DESTINATION
:
1786 str
= this->GetEntryString(station
, STR_STATION_VIEW_TO_HERE
, STR_STATION_VIEW_TO
, STR_STATION_VIEW_TO_ANY
);
1791 if (pos
== -this->scroll_to_row
&& Station::IsValidID(station
)) {
1792 ScrollMainWindowToTile(Station::Get(station
)->xy
);
1796 bool rtl
= _current_text_dir
== TD_RTL
;
1797 Rect text
= r
.Indent(column
* WidgetDimensions::scaled
.hsep_indent
, rtl
).Indent(this->expand_shrink_width
, !rtl
);
1798 Rect shrink
= r
.WithWidth(this->expand_shrink_width
, !rtl
);
1800 DrawString(text
.left
, text
.right
, y
, str
);
1802 if (column
< NUM_COLUMNS
- 1) {
1803 const char *sym
= nullptr;
1804 if (cd
->GetNumChildren() > 0) {
1806 } else if (auto_distributed
&& str
!= STR_STATION_VIEW_RESERVED
) {
1809 /* Only draw '+' if there is something to be shown. */
1810 const StationCargoList
&list
= Station::Get(this->window_number
)->goods
[cargo
].cargo
;
1811 if (grouping
== GR_CARGO
&& (list
.ReservedCount() > 0 || cd
->HasTransfers())) {
1815 if (sym
!= nullptr) DrawString(shrink
.left
, shrink
.right
, y
, sym
, TC_YELLOW
);
1817 this->SetDisplayedRow(cd
);
1820 if (auto_distributed
|| column
== 0) {
1821 pos
= this->DrawEntries(cd
, r
, pos
, maxrows
, column
+ 1, cargo
);
1828 * Draw accepted cargo in the #WID_SV_ACCEPT_RATING_LIST widget.
1829 * @param r Rectangle of the widget.
1830 * @return Number of lines needed for drawing the accepted cargo.
1832 int DrawAcceptedCargo(const Rect
&r
) const
1834 const Station
*st
= Station::Get(this->window_number
);
1835 Rect tr
= r
.Shrink(WidgetDimensions::scaled
.framerect
);
1837 SetDParam(0, GetAcceptanceMask(st
));
1838 int bottom
= DrawStringMultiLine(tr
.left
, tr
.right
, tr
.top
, INT32_MAX
, STR_STATION_VIEW_ACCEPTS_CARGO
);
1839 return CeilDiv(bottom
- r
.top
- WidgetDimensions::scaled
.framerect
.top
, GetCharacterHeight(FS_NORMAL
));
1843 * Draw cargo ratings in the #WID_SV_ACCEPT_RATING_LIST widget.
1844 * @param r Rectangle of the widget.
1845 * @return Number of lines needed for drawing the cargo ratings.
1847 int DrawCargoRatings(const Rect
&r
) const
1849 const Station
*st
= Station::Get(this->window_number
);
1850 bool rtl
= _current_text_dir
== TD_RTL
;
1851 Rect tr
= r
.Shrink(WidgetDimensions::scaled
.framerect
);
1853 if (st
->town
->exclusive_counter
> 0) {
1854 SetDParam(0, st
->town
->exclusivity
);
1855 tr
.top
= DrawStringMultiLine(tr
, st
->town
->exclusivity
== st
->owner
? STR_STATION_VIEW_EXCLUSIVE_RIGHTS_SELF
: STR_STATION_VIEW_EXCLUSIVE_RIGHTS_COMPANY
);
1856 tr
.top
+= WidgetDimensions::scaled
.vsep_wide
;
1859 DrawString(tr
, TimerGameEconomy::UsingWallclockUnits() ? STR_STATION_VIEW_SUPPLY_RATINGS_TITLE_MINUTE
: STR_STATION_VIEW_SUPPLY_RATINGS_TITLE_MONTH
);
1860 tr
.top
+= GetCharacterHeight(FS_NORMAL
);
1862 for (const CargoSpec
*cs
: _sorted_standard_cargo_specs
) {
1863 const GoodsEntry
*ge
= &st
->goods
[cs
->Index()];
1864 if (!ge
->HasRating()) continue;
1866 const LinkGraph
*lg
= LinkGraph::GetIfValid(ge
->link_graph
);
1867 SetDParam(0, cs
->name
);
1868 SetDParam(1, lg
!= nullptr ? lg
->Monthly((*lg
)[ge
->node
].supply
) : 0);
1869 SetDParam(2, STR_CARGO_RATING_APPALLING
+ (ge
->rating
>> 5));
1870 SetDParam(3, ToPercent8(ge
->rating
));
1871 DrawString(tr
.Indent(WidgetDimensions::scaled
.hsep_indent
, rtl
), STR_STATION_VIEW_CARGO_SUPPLY_RATING
);
1872 tr
.top
+= GetCharacterHeight(FS_NORMAL
);
1874 return CeilDiv(tr
.top
- r
.top
- WidgetDimensions::scaled
.framerect
.top
, GetCharacterHeight(FS_NORMAL
));
1878 * Expand or collapse a specific row.
1879 * @param filter Parent of the row.
1880 * @param next ID pointing to the row.
1883 void HandleCargoWaitingClick(CargoDataEntry
*filter
, Tid next
)
1885 if (filter
->Retrieve(next
) != nullptr) {
1886 filter
->Remove(next
);
1888 filter
->InsertOrRetrieve(next
);
1893 * Handle a click on a specific row in the cargo view.
1894 * @param row Row being clicked.
1896 void HandleCargoWaitingClick(int row
)
1898 if (row
< 0 || (uint
)row
>= this->displayed_rows
.size()) return;
1899 if (_ctrl_pressed
) {
1900 this->scroll_to_row
= row
;
1902 RowDisplay
&display
= this->displayed_rows
[row
];
1903 if (display
.filter
== &this->expanded_rows
) {
1904 this->HandleCargoWaitingClick
<CargoID
>(display
.filter
, display
.next_cargo
);
1906 this->HandleCargoWaitingClick
<StationID
>(display
.filter
, display
.next_station
);
1909 this->SetWidgetDirty(WID_SV_WAITING
);
1912 void OnClick([[maybe_unused
]] Point pt
, WidgetID widget
, [[maybe_unused
]] int click_count
) override
1915 case WID_SV_WAITING
:
1916 this->HandleCargoWaitingClick(this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_SV_WAITING
, WidgetDimensions::scaled
.framerect
.top
) - this->vscroll
->GetPosition());
1919 case WID_SV_CATCHMENT
:
1920 SetViewportCatchmentStation(Station::Get(this->window_number
), !this->IsWidgetLowered(WID_SV_CATCHMENT
));
1923 case WID_SV_LOCATION
:
1924 if (_ctrl_pressed
) {
1925 ShowExtraViewportWindow(Station::Get(this->window_number
)->xy
);
1927 ScrollMainWindowToTile(Station::Get(this->window_number
)->xy
);
1931 case WID_SV_ACCEPTS_RATINGS
: {
1932 /* Swap between 'accepts' and 'ratings' view. */
1934 NWidgetCore
*nwi
= this->GetWidget
<NWidgetCore
>(WID_SV_ACCEPTS_RATINGS
);
1935 if (this->GetWidget
<NWidgetCore
>(WID_SV_ACCEPTS_RATINGS
)->widget_data
== STR_STATION_VIEW_RATINGS_BUTTON
) {
1936 nwi
->SetDataTip(STR_STATION_VIEW_ACCEPTS_BUTTON
, STR_STATION_VIEW_ACCEPTS_TOOLTIP
); // Switch to accepts view.
1937 height_change
= this->rating_lines
- this->accepts_lines
;
1939 nwi
->SetDataTip(STR_STATION_VIEW_RATINGS_BUTTON
, STR_STATION_VIEW_RATINGS_TOOLTIP
); // Switch to ratings view.
1940 height_change
= this->accepts_lines
- this->rating_lines
;
1942 this->ReInit(0, height_change
* GetCharacterHeight(FS_NORMAL
));
1947 SetDParam(0, this->window_number
);
1948 ShowQueryString(STR_STATION_NAME
, STR_STATION_VIEW_RENAME_STATION_CAPTION
, MAX_LENGTH_STATION_NAME_CHARS
,
1949 this, CS_ALPHANUMERAL
, QSF_ENABLE_DEFAULT
| QSF_LEN_IN_CHARS
);
1952 case WID_SV_CLOSE_AIRPORT
:
1953 Command
<CMD_OPEN_CLOSE_AIRPORT
>::Post(this->window_number
);
1956 case WID_SV_TRAINS
: // Show list of scheduled trains to this station
1957 case WID_SV_ROADVEHS
: // Show list of scheduled road-vehicles to this station
1958 case WID_SV_SHIPS
: // Show list of scheduled ships to this station
1959 case WID_SV_PLANES
: { // Show list of scheduled aircraft to this station
1960 Owner owner
= Station::Get(this->window_number
)->owner
;
1961 ShowVehicleListWindow(owner
, (VehicleType
)(widget
- WID_SV_TRAINS
), (StationID
)this->window_number
);
1965 case WID_SV_SORT_BY
: {
1966 /* The initial selection is composed of current mode and
1967 * sorting criteria for columns 1, 2, and 3. Column 0 is always
1968 * sorted by cargo ID. The others can theoretically be sorted
1969 * by different things but there is no UI for that. */
1970 ShowDropDownMenu(this, _sort_names
,
1971 this->current_mode
* 2 + (this->sortings
[1] == CargoSortType::Count
? 1 : 0),
1972 WID_SV_SORT_BY
, 0, 0);
1976 case WID_SV_GROUP_BY
: {
1977 ShowDropDownMenu(this, _group_names
, this->grouping_index
, WID_SV_GROUP_BY
, 0, 0);
1981 case WID_SV_SORT_ORDER
: { // flip sorting method asc/desc
1982 this->SelectSortOrder(this->sort_orders
[1] == SO_ASCENDING
? SO_DESCENDING
: SO_ASCENDING
);
1984 this->LowerWidget(WID_SV_SORT_ORDER
);
1991 * Select a new sort order for the cargo view.
1992 * @param order New sort order.
1994 void SelectSortOrder(SortOrder order
)
1996 this->sort_orders
[1] = this->sort_orders
[2] = this->sort_orders
[3] = order
;
1997 _settings_client
.gui
.station_gui_sort_order
= this->sort_orders
[1];
2002 * Select a new sort criterium for the cargo view.
2003 * @param index Row being selected in the sort criteria drop down.
2005 void SelectSortBy(int index
)
2007 _settings_client
.gui
.station_gui_sort_by
= index
;
2008 switch (_sort_names
[index
]) {
2009 case STR_STATION_VIEW_WAITING_STATION
:
2010 this->current_mode
= MODE_WAITING
;
2011 this->sortings
[1] = this->sortings
[2] = this->sortings
[3] = CargoSortType::AsGrouping
;
2013 case STR_STATION_VIEW_WAITING_AMOUNT
:
2014 this->current_mode
= MODE_WAITING
;
2015 this->sortings
[1] = this->sortings
[2] = this->sortings
[3] = CargoSortType::Count
;
2017 case STR_STATION_VIEW_PLANNED_STATION
:
2018 this->current_mode
= MODE_PLANNED
;
2019 this->sortings
[1] = this->sortings
[2] = this->sortings
[3] = CargoSortType::AsGrouping
;
2021 case STR_STATION_VIEW_PLANNED_AMOUNT
:
2022 this->current_mode
= MODE_PLANNED
;
2023 this->sortings
[1] = this->sortings
[2] = this->sortings
[3] = CargoSortType::Count
;
2028 /* Display the current sort variant */
2029 this->GetWidget
<NWidgetCore
>(WID_SV_SORT_BY
)->widget_data
= _sort_names
[index
];
2034 * Select a new grouping mode for the cargo view.
2035 * @param index Row being selected in the grouping drop down.
2037 void SelectGroupBy(int index
)
2039 this->grouping_index
= index
;
2040 _settings_client
.gui
.station_gui_group_order
= index
;
2041 this->GetWidget
<NWidgetCore
>(WID_SV_GROUP_BY
)->widget_data
= _group_names
[index
];
2042 switch (_group_names
[index
]) {
2043 case STR_STATION_VIEW_GROUP_S_V_D
:
2044 this->groupings
[1] = GR_SOURCE
;
2045 this->groupings
[2] = GR_NEXT
;
2046 this->groupings
[3] = GR_DESTINATION
;
2048 case STR_STATION_VIEW_GROUP_S_D_V
:
2049 this->groupings
[1] = GR_SOURCE
;
2050 this->groupings
[2] = GR_DESTINATION
;
2051 this->groupings
[3] = GR_NEXT
;
2053 case STR_STATION_VIEW_GROUP_V_S_D
:
2054 this->groupings
[1] = GR_NEXT
;
2055 this->groupings
[2] = GR_SOURCE
;
2056 this->groupings
[3] = GR_DESTINATION
;
2058 case STR_STATION_VIEW_GROUP_V_D_S
:
2059 this->groupings
[1] = GR_NEXT
;
2060 this->groupings
[2] = GR_DESTINATION
;
2061 this->groupings
[3] = GR_SOURCE
;
2063 case STR_STATION_VIEW_GROUP_D_S_V
:
2064 this->groupings
[1] = GR_DESTINATION
;
2065 this->groupings
[2] = GR_SOURCE
;
2066 this->groupings
[3] = GR_NEXT
;
2068 case STR_STATION_VIEW_GROUP_D_V_S
:
2069 this->groupings
[1] = GR_DESTINATION
;
2070 this->groupings
[2] = GR_NEXT
;
2071 this->groupings
[3] = GR_SOURCE
;
2077 void OnDropdownSelect(WidgetID widget
, int index
) override
2079 if (widget
== WID_SV_SORT_BY
) {
2080 this->SelectSortBy(index
);
2082 this->SelectGroupBy(index
);
2086 void OnQueryTextFinished(char *str
) override
2088 if (str
== nullptr) return;
2090 Command
<CMD_RENAME_STATION
>::Post(STR_ERROR_CAN_T_RENAME_STATION
, this->window_number
, str
);
2093 void OnResize() override
2095 this->vscroll
->SetCapacityFromWidget(this, WID_SV_WAITING
, WidgetDimensions::scaled
.framerect
.Vertical());
2099 * Some data on this window has become invalid. Invalidate the cache for the given cargo if necessary.
2100 * @param data Information about the changed data. If it's a valid cargo ID, invalidate the cargo data.
2101 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
2103 void OnInvalidateData([[maybe_unused
]] int data
= 0, [[maybe_unused
]] bool gui_scope
= true) override
2106 if (data
>= 0 && data
< NUM_CARGO
) {
2107 this->cached_destinations
.Remove((CargoID
)data
);
2115 const StringID
StationViewWindow::_sort_names
[] = {
2116 STR_STATION_VIEW_WAITING_STATION
,
2117 STR_STATION_VIEW_WAITING_AMOUNT
,
2118 STR_STATION_VIEW_PLANNED_STATION
,
2119 STR_STATION_VIEW_PLANNED_AMOUNT
,
2123 const StringID
StationViewWindow::_group_names
[] = {
2124 STR_STATION_VIEW_GROUP_S_V_D
,
2125 STR_STATION_VIEW_GROUP_S_D_V
,
2126 STR_STATION_VIEW_GROUP_V_S_D
,
2127 STR_STATION_VIEW_GROUP_V_D_S
,
2128 STR_STATION_VIEW_GROUP_D_S_V
,
2129 STR_STATION_VIEW_GROUP_D_V_S
,
2133 static WindowDesc
_station_view_desc(__FILE__
, __LINE__
,
2134 WDP_AUTO
, "view_station", 249, 117,
2135 WC_STATION_VIEW
, WC_NONE
,
2137 std::begin(_nested_station_view_widgets
), std::end(_nested_station_view_widgets
)
2141 * Opens StationViewWindow for given station
2143 * @param station station which window should be opened
2145 void ShowStationViewWindow(StationID station
)
2147 AllocateWindowDescFront
<StationViewWindow
>(&_station_view_desc
, station
);
2150 /** Struct containing TileIndex and StationID */
2151 struct TileAndStation
{
2152 TileIndex tile
; ///< TileIndex
2153 StationID station
; ///< StationID
2156 static std::vector
<TileAndStation
> _deleted_stations_nearby
;
2157 static std::vector
<StationID
> _stations_nearby_list
;
2160 * Add station on this tile to _stations_nearby_list if it's fully within the
2162 * @param tile Tile just being checked
2163 * @param user_data Pointer to TileArea context
2164 * @tparam T the type of station to look for
2167 static bool AddNearbyStation(TileIndex tile
, void *user_data
)
2169 TileArea
*ctx
= (TileArea
*)user_data
;
2171 /* First check if there were deleted stations here */
2172 for (auto it
= _deleted_stations_nearby
.begin(); it
!= _deleted_stations_nearby
.end(); /* nothing */) {
2173 if (it
->tile
== tile
) {
2174 _stations_nearby_list
.push_back(it
->station
);
2175 it
= _deleted_stations_nearby
.erase(it
);
2181 /* Check if own station and if we stay within station spread */
2182 if (!IsTileType(tile
, MP_STATION
)) return false;
2184 StationID sid
= GetStationIndex(tile
);
2186 /* This station is (likely) a waypoint */
2187 if (!T::IsValidID(sid
)) return false;
2189 T
*st
= T::Get(sid
);
2190 if (st
->owner
!= _local_company
|| std::find(_stations_nearby_list
.begin(), _stations_nearby_list
.end(), sid
) != _stations_nearby_list
.end()) return false;
2192 if (st
->rect
.BeforeAddRect(ctx
->tile
, ctx
->w
, ctx
->h
, StationRect::ADD_TEST
).Succeeded()) {
2193 _stations_nearby_list
.push_back(sid
);
2196 return false; // We want to include *all* nearby stations
2200 * Circulate around the to-be-built station to find stations we could join.
2201 * Make sure that only stations are returned where joining wouldn't exceed
2202 * station spread and are our own station.
2203 * @param ta Base tile area of the to-be-built station
2204 * @param distant_join Search for adjacent stations (false) or stations fully
2205 * within station spread
2206 * @tparam T the type of station to look for
2209 static const T
*FindStationsNearby(TileArea ta
, bool distant_join
)
2213 _stations_nearby_list
.clear();
2214 _stations_nearby_list
.push_back(NEW_STATION
);
2215 _deleted_stations_nearby
.clear();
2217 /* Check the inside, to return, if we sit on another station */
2218 for (TileIndex t
: ta
) {
2219 if (t
< Map::Size() && IsTileType(t
, MP_STATION
) && T::IsValidID(GetStationIndex(t
))) return T::GetByTile(t
);
2222 /* Look for deleted stations */
2223 for (const BaseStation
*st
: BaseStation::Iterate()) {
2224 if (T::IsExpected(st
) && !st
->IsInUse() && st
->owner
== _local_company
) {
2225 /* Include only within station spread (yes, it is strictly less than) */
2226 if (std::max(DistanceMax(ta
.tile
, st
->xy
), DistanceMax(TILE_ADDXY(ta
.tile
, ta
.w
- 1, ta
.h
- 1), st
->xy
)) < _settings_game
.station
.station_spread
) {
2227 _deleted_stations_nearby
.push_back({st
->xy
, st
->index
});
2229 /* Add the station when it's within where we're going to build */
2230 if (IsInsideBS(TileX(st
->xy
), TileX(ctx
.tile
), ctx
.w
) &&
2231 IsInsideBS(TileY(st
->xy
), TileY(ctx
.tile
), ctx
.h
)) {
2232 AddNearbyStation
<T
>(st
->xy
, &ctx
);
2238 /* Only search tiles where we have a chance to stay within the station spread.
2239 * The complete check needs to be done in the callback as we don't know the
2240 * extent of the found station, yet. */
2241 if (distant_join
&& std::min(ta
.w
, ta
.h
) >= _settings_game
.station
.station_spread
) return nullptr;
2242 uint max_dist
= distant_join
? _settings_game
.station
.station_spread
- std::min(ta
.w
, ta
.h
) : 1;
2244 TileIndex tile
= TileAddByDir(ctx
.tile
, DIR_N
);
2245 CircularTileSearch(&tile
, max_dist
, ta
.w
, ta
.h
, AddNearbyStation
<T
>, &ctx
);
2250 static constexpr NWidgetPart _nested_select_station_widgets
[] = {
2251 NWidget(NWID_HORIZONTAL
),
2252 NWidget(WWT_CLOSEBOX
, COLOUR_DARK_GREEN
),
2253 NWidget(WWT_CAPTION
, COLOUR_DARK_GREEN
, WID_JS_CAPTION
), SetDataTip(STR_JOIN_STATION_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
2254 NWidget(WWT_DEFSIZEBOX
, COLOUR_DARK_GREEN
),
2256 NWidget(NWID_HORIZONTAL
),
2257 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_JS_PANEL
), SetResize(1, 0), SetScrollbar(WID_JS_SCROLLBAR
), EndContainer(),
2258 NWidget(NWID_VERTICAL
),
2259 NWidget(NWID_VSCROLLBAR
, COLOUR_DARK_GREEN
, WID_JS_SCROLLBAR
),
2260 NWidget(WWT_RESIZEBOX
, COLOUR_DARK_GREEN
),
2266 * Window for selecting stations/waypoints to (distant) join to.
2267 * @tparam T The type of station to join with
2270 struct SelectStationWindow
: Window
{
2271 StationPickerCmdProc select_station_proc
;
2272 TileArea area
; ///< Location of new station
2275 SelectStationWindow(WindowDesc
*desc
, TileArea ta
, StationPickerCmdProc
&& proc
) :
2277 select_station_proc(std::move(proc
)),
2280 this->CreateNestedTree();
2281 this->vscroll
= this->GetScrollbar(WID_JS_SCROLLBAR
);
2282 this->GetWidget
<NWidgetCore
>(WID_JS_CAPTION
)->widget_data
= T::EXPECTED_FACIL
== FACIL_WAYPOINT
? STR_JOIN_WAYPOINT_CAPTION
: STR_JOIN_STATION_CAPTION
;
2283 this->FinishInitNested(0);
2284 this->OnInvalidateData(0);
2289 void Close([[maybe_unused
]] int data
= 0) override
2291 SetViewportCatchmentSpecializedStation
<T
>(nullptr, true);
2293 _thd
.freeze
= false;
2294 this->Window::Close();
2297 void UpdateWidgetSize(WidgetID widget
, Dimension
*size
, [[maybe_unused
]] const Dimension
&padding
, [[maybe_unused
]] Dimension
*fill
, [[maybe_unused
]] Dimension
*resize
) override
2299 if (widget
!= WID_JS_PANEL
) return;
2301 /* Determine the widest string */
2302 Dimension d
= GetStringBoundingBox(T::EXPECTED_FACIL
== FACIL_WAYPOINT
? STR_JOIN_WAYPOINT_CREATE_SPLITTED_WAYPOINT
: STR_JOIN_STATION_CREATE_SPLITTED_STATION
);
2303 for (const auto &station
: _stations_nearby_list
) {
2304 if (station
== NEW_STATION
) continue;
2305 const T
*st
= T::Get(station
);
2306 SetDParam(0, st
->index
);
2307 SetDParam(1, st
->facilities
);
2308 d
= maxdim(d
, GetStringBoundingBox(T::EXPECTED_FACIL
== FACIL_WAYPOINT
? STR_STATION_LIST_WAYPOINT
: STR_STATION_LIST_STATION
));
2311 resize
->height
= d
.height
;
2313 d
.width
+= padding
.width
;
2314 d
.height
+= padding
.height
;
2318 void DrawWidget(const Rect
&r
, WidgetID widget
) const override
2320 if (widget
!= WID_JS_PANEL
) return;
2322 Rect tr
= r
.Shrink(WidgetDimensions::scaled
.framerect
);
2323 for (uint i
= this->vscroll
->GetPosition(); i
< _stations_nearby_list
.size(); ++i
, tr
.top
+= this->resize
.step_height
) {
2324 if (_stations_nearby_list
[i
] == NEW_STATION
) {
2325 DrawString(tr
, T::EXPECTED_FACIL
== FACIL_WAYPOINT
? STR_JOIN_WAYPOINT_CREATE_SPLITTED_WAYPOINT
: STR_JOIN_STATION_CREATE_SPLITTED_STATION
);
2327 const T
*st
= T::Get(_stations_nearby_list
[i
]);
2328 SetDParam(0, st
->index
);
2329 SetDParam(1, st
->facilities
);
2330 DrawString(tr
, T::EXPECTED_FACIL
== FACIL_WAYPOINT
? STR_STATION_LIST_WAYPOINT
: STR_STATION_LIST_STATION
);
2336 void OnClick([[maybe_unused
]] Point pt
, WidgetID widget
, [[maybe_unused
]] int click_count
) override
2338 if (widget
!= WID_JS_PANEL
) return;
2340 auto it
= this->vscroll
->GetScrolledItemFromWidget(_stations_nearby_list
, pt
.y
, this, WID_JS_PANEL
, WidgetDimensions::scaled
.framerect
.top
);
2341 if (it
== _stations_nearby_list
.end()) return;
2343 /* Execute stored Command */
2344 this->select_station_proc(false, *it
);
2346 /* Close Window; this might cause double frees! */
2347 CloseWindowById(WC_SELECT_STATION
, 0);
2350 void OnRealtimeTick([[maybe_unused
]] uint delta_ms
) override
2352 if (_thd
.dirty
& 2) {
2358 void OnResize() override
2360 this->vscroll
->SetCapacityFromWidget(this, WID_JS_PANEL
, WidgetDimensions::scaled
.framerect
.Vertical());
2364 * Some data on this window has become invalid.
2365 * @param data Information about the changed data.
2366 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
2368 void OnInvalidateData([[maybe_unused
]] int data
= 0, [[maybe_unused
]] bool gui_scope
= true) override
2370 if (!gui_scope
) return;
2371 FindStationsNearby
<T
>(this->area
, true);
2372 this->vscroll
->SetCount(_stations_nearby_list
.size());
2376 void OnMouseOver([[maybe_unused
]] Point pt
, WidgetID widget
) override
2378 if (widget
!= WID_JS_PANEL
) {
2379 SetViewportCatchmentSpecializedStation
<T
>(nullptr, true);
2383 /* Show coverage area of station under cursor */
2384 auto it
= this->vscroll
->GetScrolledItemFromWidget(_stations_nearby_list
, pt
.y
, this, WID_JS_PANEL
, WidgetDimensions::scaled
.framerect
.top
);
2385 const T
*st
= it
== _stations_nearby_list
.end() || *it
== NEW_STATION
? nullptr : T::Get(*it
);
2386 SetViewportCatchmentSpecializedStation
<T
>(st
, true);
2390 static WindowDesc
_select_station_desc(__FILE__
, __LINE__
,
2391 WDP_AUTO
, "build_station_join", 200, 180,
2392 WC_SELECT_STATION
, WC_NONE
,
2394 std::begin(_nested_select_station_widgets
), std::end(_nested_select_station_widgets
)
2399 * Check whether we need to show the station selection window.
2400 * @param cmd Command to build the station.
2401 * @param ta Tile area of the to-be-built station
2402 * @tparam T the type of station
2403 * @return whether we need to show the station selection window.
2406 static bool StationJoinerNeeded(TileArea ta
, const StationPickerCmdProc
&proc
)
2408 /* Only show selection if distant join is enabled in the settings */
2409 if (!_settings_game
.station
.distant_join_stations
) return false;
2411 /* If a window is already opened and we didn't ctrl-click,
2412 * return true (i.e. just flash the old window) */
2413 Window
*selection_window
= FindWindowById(WC_SELECT_STATION
, 0);
2414 if (selection_window
!= nullptr) {
2415 /* Abort current distant-join and start new one */
2416 selection_window
->Close();
2417 UpdateTileSelection();
2420 /* only show the popup, if we press ctrl */
2421 if (!_ctrl_pressed
) return false;
2423 /* Now check if we could build there */
2424 if (!proc(true, INVALID_STATION
)) return false;
2426 /* Test for adjacent station or station below selection.
2427 * If adjacent-stations is disabled and we are building next to a station, do not show the selection window.
2428 * but join the other station immediately. */
2429 const T
*st
= FindStationsNearby
<T
>(ta
, false);
2430 return st
== nullptr && (_settings_game
.station
.adjacent_stations
|| std::any_of(std::begin(_stations_nearby_list
), std::end(_stations_nearby_list
), [](StationID s
) { return s
!= NEW_STATION
; }));
2434 * Show the station selection window when needed. If not, build the station.
2435 * @param cmd Command to build the station.
2436 * @param ta Area to build the station in
2437 * @tparam the class to find stations for
2440 void ShowSelectBaseStationIfNeeded(TileArea ta
, StationPickerCmdProc
&& proc
)
2442 if (StationJoinerNeeded
<T
>(ta
, proc
)) {
2443 if (!_settings_client
.gui
.persistent_buildingtools
) ResetObjectToPlace();
2444 new SelectStationWindow
<T
>(&_select_station_desc
, ta
, std::move(proc
));
2446 proc(false, INVALID_STATION
);
2451 * Show the station selection window when needed. If not, build the station.
2452 * @param ta Area to build the station in
2453 * @param proc Function called to execute the build command.
2455 void ShowSelectStationIfNeeded(TileArea ta
, StationPickerCmdProc proc
)
2457 ShowSelectBaseStationIfNeeded
<Station
>(ta
, std::move(proc
));
2461 * Show the waypoint selection window when needed. If not, build the waypoint.
2462 * @param ta Area to build the waypoint in
2463 * @param proc Function called to execute the build command.
2465 void ShowSelectWaypointIfNeeded(TileArea ta
, StationPickerCmdProc proc
)
2467 ShowSelectBaseStationIfNeeded
<Waypoint
>(ta
, std::move(proc
));