4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file station_gui.cpp The GUI for stations. */
14 #include "core/pointer.h"
16 #include "textbuf_gui.h"
17 #include "company_func.h"
18 #include "command_func.h"
19 #include "vehicle_gui.h"
20 #include "cargotype.h"
21 #include "station_gui.h"
22 #include "strings_func.h"
24 #include "window_func.h"
25 #include "viewport_func.h"
26 #include "widgets/dropdown_func.h"
27 #include "station_base.h"
28 #include "waypoint_base.h"
29 #include "tilehighlight_func.h"
30 #include "company_base.h"
31 #include "sortlist_type.h"
32 #include "core/geometry_func.hpp"
33 #include "vehiclelist.h"
35 #include "linkgraph/linkgraph.h"
36 #include "station_func.h"
37 #include "zoom_func.h"
39 #include "widgets/station_widget.h"
41 #include "table/strings.h"
51 * Calculates and draws the accepted and supplied cargo around the selected tile(s)
52 * @param dpi area to draw on
53 * @param left x position where the string is to be drawn
54 * @param right the right most position to draw on
55 * @param top y position where the string is to be drawn
56 * @param rad radius around selected tile(s) to be searched
57 * @param sct which type of cargo is to be displayed (passengers/non-passengers)
58 * @return Returns the y value below the strings that were drawn
60 int DrawStationCoverageAreaText (BlitArea
*dpi
, int left
, int right
, int top
,
61 int rad
, StationCoverageType sct
)
63 uint32 accept_mask
= 0;
64 uint32 supply_mask
= 0;
66 if (_thd
.drawstyle
== HT_RECT
) {
67 TileIndex tile
= TileVirtXY (_thd
.pos
.x
, _thd
.pos
.y
);
68 if (tile
< MapSize()) {
69 TileArea
ta (tile
, _thd
.size
.x
/ TILE_SIZE
, _thd
.size
.y
/ TILE_SIZE
);
70 CargoArray accept_cargoes
= GetAreaAcceptance (ta
, rad
);
71 CargoArray supply_cargoes
= GetAreaProduction (ta
, rad
);
73 for (CargoID i
= 0; i
< NUM_CARGO
; i
++) {
75 case SCT_PASSENGERS_ONLY
:
76 if (!IsCargoInClass (i
, CC_PASSENGERS
)) continue;
78 case SCT_NON_PASSENGERS_ONLY
:
79 if (IsCargoInClass (i
, CC_PASSENGERS
)) continue;
82 default: NOT_REACHED();
84 if (accept_cargoes
[i
] >= 8) SetBit(accept_mask
, i
);
85 if (supply_cargoes
[i
] >= 1) SetBit(supply_mask
, i
);
90 SetDParam (0, accept_mask
);
91 top
= DrawStringMultiLine (dpi
, left
, right
, top
, INT32_MAX
, STR_STATION_BUILD_ACCEPTS_CARGO
) + WD_PAR_VSEP_NORMAL
;
92 SetDParam (0, supply_mask
);
93 top
= DrawStringMultiLine (dpi
, left
, right
, top
, INT32_MAX
, STR_STATION_BUILD_SUPPLIES_CARGO
) + WD_PAR_VSEP_NORMAL
;
98 * Check whether we need to redraw the station coverage text.
99 * If it is needed actually make the window for redrawing.
100 * @param w the window to check.
102 void CheckRedrawStationCoverage(const Window
*w
)
104 if (_thd
.dirty
& 1) {
111 * Draw small boxes of cargo amount and ratings data at the given
112 * coordinates. If amount exceeds 576 units, it is shown 'full', same
113 * goes for the rating: at above 90% orso (224) it is also 'full'
115 * @param dpi area to draw on
116 * @param left left most coordinate to draw the box at
117 * @param right right most coordinate to draw the box at
118 * @param y coordinate to draw the box at
119 * @param type Cargo type
120 * @param amount Cargo amount
121 * @param rating ratings data for that particular cargo
123 * @note Each cargo-bar is 16 pixels wide and 6 pixels high
124 * @note Each rating 14 pixels wide and 1 pixel high and is 1 pixel below the cargo-bar
126 static void StationsWndShowStationRating (BlitArea
*dpi
,
127 int left
, int right
, int y
, CargoID type
, uint amount
, byte rating
)
129 static const uint units_full
= 576; ///< number of units to show station as 'full'
130 static const uint rating_full
= 224; ///< rating needed so it is shown as 'full'
132 const CargoSpec
*cs
= CargoSpec::Get(type
);
133 if (!cs
->IsValid()) return;
135 int colour
= cs
->rating_colour
;
136 TextColour tc
= GetContrastColour(colour
);
137 uint w
= (minu(amount
, units_full
) + 5) / 36;
139 int height
= GetCharacterHeight(FS_SMALL
);
141 /* Draw total cargo (limited) on station (fits into 16 pixels) */
142 if (w
!= 0) GfxFillRect (dpi
, left
, y
, left
+ w
- 1, y
+ height
, colour
);
144 /* Draw a one pixel-wide bar of additional cargo meter, useful
145 * for stations with only a small amount (<=30) */
147 uint rest
= amount
/ 5;
150 GfxFillRect (dpi
, w
, y
+ height
- rest
, w
, y
+ height
, colour
);
154 DrawString (dpi
, left
+ 1, right
, y
, cs
->abbrev
, tc
);
156 /* Draw green/red ratings bar (fits into 14 pixels) */
158 GfxFillRect (dpi
, left
+ 1, y
, left
+ 14, y
, PC_RED
);
159 rating
= minu(rating
, rating_full
) / 16;
160 if (rating
!= 0) GfxFillRect (dpi
, left
+ 1, y
, left
+ rating
, y
, PC_GREEN
);
163 typedef GUIList
<const Station
*> GUIStationList
;
166 * The list of stations per company.
168 class CompanyStationsWindow
: public Window
171 /* Runtime saved values */
172 static Listing last_sorting
;
173 static byte facilities
; // types of stations of interest
174 static bool include_empty
; // whether we should include stations without waiting cargo
175 static const uint32 cargo_filter_max
;
176 static uint32 cargo_filter
; // bitmap of cargo types to include
177 static const Station
*last_station
;
179 /* Constants for sorting stations */
180 static const StringID sorter_names
[];
181 static GUIStationList::SortFunction
* const sorter_funcs
[];
183 GUIStationList stations
;
187 * (Re)Build station list
189 * @param owner company whose stations are to be in list
191 void BuildStationsList(const Owner owner
)
193 if (!this->stations
.NeedRebuild()) return;
195 DEBUG(misc
, 3, "Building station list for company %d", owner
);
197 this->stations
.Clear();
200 FOR_ALL_STATIONS(st
) {
201 if (st
->owner
== owner
|| (st
->owner
== OWNER_NONE
&& HasStationInUse(st
->index
, true, owner
))) {
202 if (this->facilities
& st
->facilities
) { // only stations with selected facilities
203 int num_waiting_cargo
= 0;
204 for (CargoID j
= 0; j
< NUM_CARGO
; j
++) {
205 if (st
->goods
[j
].HasRating()) {
206 num_waiting_cargo
++; // count number of waiting cargo
207 if (HasBit(this->cargo_filter
, j
)) {
208 *this->stations
.Append() = st
;
213 /* stations without waiting cargo */
214 if (num_waiting_cargo
== 0 && this->include_empty
) {
215 *this->stations
.Append() = st
;
221 this->stations
.Compact();
222 this->stations
.RebuildDone();
224 this->vscroll
->SetCount(this->stations
.Length()); // Update the scrollbar
227 /** Sort stations by their name */
228 static int CDECL
StationNameSorter(const Station
* const *a
, const Station
* const *b
)
230 static char buf_cache
[64];
233 SetDParam(0, (*a
)->index
);
234 GetString (buf
, STR_STATION_NAME
);
236 if (*b
!= last_station
) {
238 SetDParam(0, (*b
)->index
);
239 GetString (buf_cache
, STR_STATION_NAME
);
242 int r
= strnatcmp(buf
, buf_cache
); // Sort by name (natural sorting).
243 if (r
== 0) return (*a
)->index
- (*b
)->index
;
247 /** Sort stations by their type */
248 static int CDECL
StationTypeSorter(const Station
* const *a
, const Station
* const *b
)
250 return (*a
)->facilities
- (*b
)->facilities
;
253 /** Sort stations by their waiting cargo */
254 static int CDECL
StationWaitingTotalSorter(const Station
* const *a
, const Station
* const *b
)
259 FOR_EACH_SET_CARGO_ID(j
, cargo_filter
) {
260 diff
+= (*a
)->goods
[j
].cargo
.TotalCount() - (*b
)->goods
[j
].cargo
.TotalCount();
266 /** Sort stations by their available waiting cargo */
267 static int CDECL
StationWaitingAvailableSorter(const Station
* const *a
, const Station
* const *b
)
272 FOR_EACH_SET_CARGO_ID(j
, cargo_filter
) {
273 diff
+= (*a
)->goods
[j
].cargo
.AvailableCount() - (*b
)->goods
[j
].cargo
.AvailableCount();
279 /** Sort stations by their rating */
280 static int CDECL
StationRatingMaxSorter(const Station
* const *a
, const Station
* const *b
)
286 FOR_EACH_SET_CARGO_ID(j
, cargo_filter
) {
287 if ((*a
)->goods
[j
].HasRating()) maxr1
= max(maxr1
, (*a
)->goods
[j
].rating
);
288 if ((*b
)->goods
[j
].HasRating()) maxr2
= max(maxr2
, (*b
)->goods
[j
].rating
);
291 return maxr1
- maxr2
;
294 /** Sort stations by their rating */
295 static int CDECL
StationRatingMinSorter(const Station
* const *a
, const Station
* const *b
)
300 for (CargoID j
= 0; j
< NUM_CARGO
; j
++) {
301 if (!HasBit(cargo_filter
, j
)) continue;
302 if ((*a
)->goods
[j
].HasRating()) minr1
= min(minr1
, (*a
)->goods
[j
].rating
);
303 if ((*b
)->goods
[j
].HasRating()) minr2
= min(minr2
, (*b
)->goods
[j
].rating
);
306 return -(minr1
- minr2
);
309 /** Sort the stations list */
310 void SortStationsList()
312 if (!this->stations
.Sort()) return;
314 /* Reset name sorter sort cache */
315 this->last_station
= NULL
;
317 /* Set the modified widget dirty */
318 this->SetWidgetDirty(WID_STL_LIST
);
322 CompanyStationsWindow (const WindowDesc
*desc
, WindowNumber window_number
) :
323 Window (desc
), stations(), vscroll (NULL
)
325 this->stations
.SetListing(this->last_sorting
);
326 this->stations
.SetSortFuncs(this->sorter_funcs
);
327 this->stations
.ForceRebuild();
328 this->stations
.NeedResort();
329 this->SortStationsList();
331 this->CreateNestedTree();
332 this->vscroll
= this->GetScrollbar(WID_STL_SCROLLBAR
);
333 this->InitNested(window_number
);
334 this->owner
= (Owner
)this->window_number
;
337 FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs
) {
338 if (!HasBit(this->cargo_filter
, cs
->Index())) continue;
339 this->LowerWidget(WID_STL_CARGOSTART
+ index
);
342 if (this->cargo_filter
== this->cargo_filter_max
) this->cargo_filter
= _cargo_mask
;
344 for (uint i
= 0; i
< 5; i
++) {
345 if (HasBit(this->facilities
, i
)) this->LowerWidget(i
+ WID_STL_TRAIN
);
347 this->SetWidgetLoweredState(WID_STL_NOCARGOWAITING
, this->include_empty
);
349 this->GetWidget
<NWidgetCore
>(WID_STL_SORTDROPBTN
)->widget_data
= this->sorter_names
[this->stations
.SortType()];
352 void OnDelete (void) FINAL_OVERRIDE
354 this->last_sorting
= this->stations
.GetListing();
357 virtual void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
360 case WID_STL_SORTBY
: {
361 Dimension d
= GetStringBoundingBox(this->GetWidget
<NWidgetCore
>(widget
)->widget_data
);
362 d
.width
+= padding
.width
+ Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
363 d
.height
+= padding
.height
;
364 *size
= maxdim(*size
, d
);
368 case WID_STL_SORTDROPBTN
: {
369 Dimension d
= {0, 0};
370 for (int i
= 0; this->sorter_names
[i
] != INVALID_STRING_ID
; i
++) {
371 d
= maxdim(d
, GetStringBoundingBox(this->sorter_names
[i
]));
373 d
.width
+= padding
.width
;
374 d
.height
+= padding
.height
;
375 *size
= maxdim(*size
, d
);
380 resize
->height
= FONT_HEIGHT_NORMAL
;
381 size
->height
= WD_FRAMERECT_TOP
+ 5 * resize
->height
+ WD_FRAMERECT_BOTTOM
;
387 case WID_STL_AIRPLANE
:
389 size
->height
= max
<uint
>(FONT_HEIGHT_SMALL
, 10) + padding
.height
;
392 case WID_STL_CARGOALL
:
393 case WID_STL_FACILALL
:
394 case WID_STL_NOCARGOWAITING
: {
395 Dimension d
= GetStringBoundingBox(widget
== WID_STL_NOCARGOWAITING
? STR_ABBREV_NONE
: STR_ABBREV_ALL
);
396 d
.width
+= padding
.width
+ 2;
397 d
.height
+= padding
.height
;
398 *size
= maxdim(*size
, d
);
403 if (widget
>= WID_STL_CARGOSTART
) {
404 Dimension d
= GetStringBoundingBox(_sorted_cargo_specs
[widget
- WID_STL_CARGOSTART
]->abbrev
);
405 d
.width
+= padding
.width
+ 2;
406 d
.height
+= padding
.height
;
407 *size
= maxdim(*size
, d
);
413 void OnPaint (BlitArea
*dpi
) OVERRIDE
415 this->BuildStationsList((Owner
)this->window_number
);
416 this->SortStationsList();
418 this->DrawWidgets (dpi
);
421 void DrawWidget (BlitArea
*dpi
, const Rect
&r
, int widget
) const OVERRIDE
425 /* draw arrow pointing up/down for ascending/descending sorting */
426 this->DrawSortButtonState (dpi
, WID_STL_SORTBY
, this->stations
.IsDescSortOrder() ? SBS_DOWN
: SBS_UP
);
430 bool rtl
= _current_text_dir
== TD_RTL
;
431 int max
= min(this->vscroll
->GetPosition() + this->vscroll
->GetCapacity(), this->stations
.Length());
432 int y
= r
.top
+ WD_FRAMERECT_TOP
;
433 for (int i
= this->vscroll
->GetPosition(); i
< max
; ++i
) { // do until max number of stations of owner
434 const Station
*st
= this->stations
[i
];
435 assert(st
->xy
!= INVALID_TILE
);
437 /* Do not do the complex check HasStationInUse here, it may be even false
438 * when the order had been removed and the station list hasn't been removed yet */
439 assert(st
->owner
== owner
|| st
->owner
== OWNER_NONE
);
441 SetDParam(0, st
->index
);
442 SetDParam(1, st
->facilities
);
443 int x
= DrawString (dpi
, r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
, STR_STATION_LIST_STATION
);
446 /* show cargo waiting and station ratings */
447 for (uint j
= 0; j
< _sorted_standard_cargo_specs_size
; j
++) {
448 CargoID cid
= _sorted_cargo_specs
[j
]->Index();
449 if (st
->goods
[cid
].cargo
.TotalCount() > 0) {
450 /* For RTL we work in exactly the opposite direction. So
451 * decrement the space needed first, then draw to the left
452 * instead of drawing to the left and then incrementing
456 if (x
< r
.left
+ WD_FRAMERECT_LEFT
) break;
458 StationsWndShowStationRating (dpi
, x
, x
+ 16, y
, cid
, st
->goods
[cid
].cargo
.TotalCount(), st
->goods
[cid
].rating
);
461 if (x
> r
.right
- WD_FRAMERECT_RIGHT
) break;
465 y
+= FONT_HEIGHT_NORMAL
;
468 if (this->vscroll
->GetCount() == 0) { // company has no stations
469 DrawString (dpi
, r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
, STR_STATION_LIST_NONE
);
475 case WID_STL_NOCARGOWAITING
:
476 DrawString (dpi
, r
.left
+ 1, r
.right
+ 1, r
.top
+ 1, STR_ABBREV_NONE
, TC_BLACK
, SA_HOR_CENTER
);
479 case WID_STL_CARGOALL
:
480 DrawString (dpi
, r
.left
+ 1, r
.right
+ 1, r
.top
+ 1, STR_ABBREV_ALL
, TC_BLACK
, SA_HOR_CENTER
);
483 case WID_STL_FACILALL
:
484 DrawString (dpi
, r
.left
+ 1, r
.right
+ 1, r
.top
+ 1, STR_ABBREV_ALL
, TC_BLACK
, SA_HOR_CENTER
);
488 if (widget
>= WID_STL_CARGOSTART
) {
489 const CargoSpec
*cs
= _sorted_cargo_specs
[widget
- WID_STL_CARGOSTART
];
490 GfxFillRect (dpi
, r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.bottom
- 1, cs
->rating_colour
);
491 TextColour tc
= GetContrastColour(cs
->rating_colour
);
492 DrawString (dpi
, r
.left
+ 1, r
.right
+ 1, r
.top
+ 1, cs
->abbrev
, tc
, SA_HOR_CENTER
);
498 virtual void SetStringParameters(int widget
) const
500 if (widget
== WID_STL_CAPTION
) {
501 SetDParam(0, this->window_number
);
502 SetDParam(1, this->vscroll
->GetCount());
506 virtual void OnClick(Point pt
, int widget
, int click_count
)
510 uint id_v
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_STL_LIST
, 0, FONT_HEIGHT_NORMAL
);
511 if (id_v
>= this->stations
.Length()) return; // click out of list bound
513 const Station
*st
= this->stations
[id_v
];
514 /* do not check HasStationInUse - it is slow and may be invalid */
515 assert(st
->owner
== (Owner
)this->window_number
|| st
->owner
== OWNER_NONE
);
518 ShowExtraViewPortWindow(st
->xy
);
520 ScrollMainWindowToTile(st
->xy
);
528 case WID_STL_AIRPLANE
:
531 ToggleBit(this->facilities
, widget
- WID_STL_TRAIN
);
532 this->ToggleWidgetLoweredState(widget
);
535 FOR_EACH_SET_BIT(i
, this->facilities
) {
536 this->RaiseWidget(i
+ WID_STL_TRAIN
);
538 this->facilities
= 1 << (widget
- WID_STL_TRAIN
);
539 this->LowerWidget(widget
);
541 this->stations
.ForceRebuild();
545 case WID_STL_FACILALL
:
546 for (uint i
= WID_STL_TRAIN
; i
<= WID_STL_SHIP
; i
++) {
547 this->LowerWidget(i
);
550 this->facilities
= FACIL_TRAIN
| FACIL_TRUCK_STOP
| FACIL_BUS_STOP
| FACIL_AIRPORT
| FACIL_DOCK
;
551 this->stations
.ForceRebuild();
555 case WID_STL_CARGOALL
: {
556 for (uint i
= 0; i
< _sorted_standard_cargo_specs_size
; i
++) {
557 this->LowerWidget(WID_STL_CARGOSTART
+ i
);
559 this->LowerWidget(WID_STL_NOCARGOWAITING
);
561 this->cargo_filter
= _cargo_mask
;
562 this->include_empty
= true;
563 this->stations
.ForceRebuild();
568 case WID_STL_SORTBY
: // flip sorting method asc/desc
569 this->stations
.ToggleSortOrder();
573 case WID_STL_SORTDROPBTN
: // select sorting criteria dropdown menu
574 ShowDropDownMenu(this, this->sorter_names
, this->stations
.SortType(), WID_STL_SORTDROPBTN
, 0, 0);
577 case WID_STL_NOCARGOWAITING
:
579 this->include_empty
= !this->include_empty
;
580 this->ToggleWidgetLoweredState(WID_STL_NOCARGOWAITING
);
582 for (uint i
= 0; i
< _sorted_standard_cargo_specs_size
; i
++) {
583 this->RaiseWidget(WID_STL_CARGOSTART
+ i
);
586 this->cargo_filter
= 0;
587 this->include_empty
= true;
589 this->LowerWidget(WID_STL_NOCARGOWAITING
);
591 this->stations
.ForceRebuild();
596 if (widget
>= WID_STL_CARGOSTART
) { // change cargo_filter
597 /* Determine the selected cargo type */
598 const CargoSpec
*cs
= _sorted_cargo_specs
[widget
- WID_STL_CARGOSTART
];
601 ToggleBit(this->cargo_filter
, cs
->Index());
602 this->ToggleWidgetLoweredState(widget
);
604 for (uint i
= 0; i
< _sorted_standard_cargo_specs_size
; i
++) {
605 this->RaiseWidget(WID_STL_CARGOSTART
+ i
);
607 this->RaiseWidget(WID_STL_NOCARGOWAITING
);
609 this->cargo_filter
= 0;
610 this->include_empty
= false;
612 SetBit(this->cargo_filter
, cs
->Index());
613 this->LowerWidget(widget
);
615 this->stations
.ForceRebuild();
622 virtual void OnDropdownSelect(int widget
, int index
)
624 if (this->stations
.SortType() != index
) {
625 this->stations
.SetSortType(index
);
627 /* Display the current sort variant */
628 this->GetWidget
<NWidgetCore
>(WID_STL_SORTDROPBTN
)->widget_data
= this->sorter_names
[this->stations
.SortType()];
634 virtual void OnTick()
636 if (_pause_mode
!= PM_UNPAUSED
) return;
637 if (this->stations
.NeedResort()) {
638 DEBUG(misc
, 3, "Periodic rebuild station list company %d", this->window_number
);
643 virtual void OnResize()
645 this->vscroll
->SetCapacityFromWidget(this, WID_STL_LIST
, WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
);
649 * Some data on this window has become invalid.
650 * @param data Information about the changed data.
651 * @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.
653 virtual void OnInvalidateData(int data
= 0, bool gui_scope
= true)
656 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
657 this->stations
.ForceRebuild();
659 this->stations
.ForceResort();
664 Listing
CompanyStationsWindow::last_sorting
= {false, 0};
665 byte
CompanyStationsWindow::facilities
= FACIL_TRAIN
| FACIL_TRUCK_STOP
| FACIL_BUS_STOP
| FACIL_AIRPORT
| FACIL_DOCK
;
666 bool CompanyStationsWindow::include_empty
= true;
667 const uint32
CompanyStationsWindow::cargo_filter_max
= UINT32_MAX
;
668 uint32
CompanyStationsWindow::cargo_filter
= UINT32_MAX
;
669 const Station
*CompanyStationsWindow::last_station
= NULL
;
671 /* Availible station sorting functions */
672 GUIStationList::SortFunction
* const CompanyStationsWindow::sorter_funcs
[] = {
675 &StationWaitingTotalSorter
,
676 &StationWaitingAvailableSorter
,
677 &StationRatingMaxSorter
,
678 &StationRatingMinSorter
681 /* Names of the sorting functions */
682 const StringID
CompanyStationsWindow::sorter_names
[] = {
684 STR_SORT_BY_FACILITY
,
685 STR_SORT_BY_WAITING_TOTAL
,
686 STR_SORT_BY_WAITING_AVAILABLE
,
687 STR_SORT_BY_RATING_MAX
,
688 STR_SORT_BY_RATING_MIN
,
693 * Make a horizontal row of cargo buttons, starting at widget #WID_STL_CARGOSTART.
694 * @param biggest_index Pointer to store biggest used widget number of the buttons.
695 * @return Horizontal row.
697 static NWidgetBase
*CargoWidgets(int *biggest_index
)
699 NWidgetHorizontal
*container
= new NWidgetHorizontal();
701 for (uint i
= 0; i
< _sorted_standard_cargo_specs_size
; i
++) {
702 NWidgetBackground
*panel
= new NWidgetBackground(WWT_PANEL
, COLOUR_GREY
, WID_STL_CARGOSTART
+ i
);
703 panel
->SetMinimalSize(14, 11);
704 panel
->SetResize(0, 0);
705 panel
->SetFill(0, 1);
706 panel
->SetDataTip(0, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE
);
707 container
->Add(panel
);
709 *biggest_index
= WID_STL_CARGOSTART
+ _sorted_standard_cargo_specs_size
;
713 static const NWidgetPart _nested_company_stations_widgets
[] = {
714 NWidget(NWID_HORIZONTAL
),
715 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
716 NWidget(WWT_CAPTION
, COLOUR_GREY
, WID_STL_CAPTION
), SetDataTip(STR_STATION_LIST_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
717 NWidget(WWT_SHADEBOX
, COLOUR_GREY
),
718 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
719 NWidget(WWT_STICKYBOX
, COLOUR_GREY
),
721 NWidget(NWID_HORIZONTAL
),
722 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_STL_TRAIN
), SetMinimalSize(14, 11), SetDataTip(STR_TRAIN
, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE
), SetFill(0, 1),
723 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_STL_TRUCK
), SetMinimalSize(14, 11), SetDataTip(STR_LORRY
, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE
), SetFill(0, 1),
724 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_STL_BUS
), SetMinimalSize(14, 11), SetDataTip(STR_BUS
, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE
), SetFill(0, 1),
725 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_STL_SHIP
), SetMinimalSize(14, 11), SetDataTip(STR_SHIP
, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE
), SetFill(0, 1),
726 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_STL_AIRPLANE
), SetMinimalSize(14, 11), SetDataTip(STR_PLANE
, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE
), SetFill(0, 1),
727 NWidget(WWT_PUSHBTN
, COLOUR_GREY
, WID_STL_FACILALL
), SetMinimalSize(14, 11), SetDataTip(0x0, STR_STATION_LIST_SELECT_ALL_FACILITIES
), SetFill(0, 1),
728 NWidget(WWT_PANEL
, COLOUR_GREY
), SetMinimalSize(5, 11), SetFill(0, 1), EndContainer(),
729 NWidgetFunction(CargoWidgets
),
730 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_STL_NOCARGOWAITING
), SetMinimalSize(14, 11), SetDataTip(0x0, STR_STATION_LIST_NO_WAITING_CARGO
), SetFill(0, 1), EndContainer(),
731 NWidget(WWT_PUSHBTN
, COLOUR_GREY
, WID_STL_CARGOALL
), SetMinimalSize(14, 11), SetDataTip(0x0, STR_STATION_LIST_SELECT_ALL_TYPES
), SetFill(0, 1),
732 NWidget(WWT_PANEL
, COLOUR_GREY
), SetDataTip(0x0, STR_NULL
), SetResize(1, 0), SetFill(1, 1), EndContainer(),
734 NWidget(NWID_HORIZONTAL
),
735 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_STL_SORTBY
), SetMinimalSize(81, 12), SetDataTip(STR_BUTTON_SORT_BY
, STR_TOOLTIP_SORT_ORDER
),
736 NWidget(WWT_DROPDOWN
, COLOUR_GREY
, WID_STL_SORTDROPBTN
), SetMinimalSize(163, 12), SetDataTip(STR_SORT_BY_NAME
, STR_TOOLTIP_SORT_CRITERIA
), // widget_data gets overwritten.
737 NWidget(WWT_PANEL
, COLOUR_GREY
), SetDataTip(0x0, STR_NULL
), SetResize(1, 0), SetFill(1, 1), EndContainer(),
739 NWidget(NWID_HORIZONTAL
),
740 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(),
741 NWidget(NWID_VERTICAL
),
742 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_STL_SCROLLBAR
),
743 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
),
748 static WindowDesc::Prefs
_company_stations_prefs ("list_stations");
750 static const WindowDesc
_company_stations_desc(
752 WC_STATION_LIST
, WC_NONE
,
754 _nested_company_stations_widgets
, lengthof(_nested_company_stations_widgets
),
755 &_company_stations_prefs
759 * Opens window with list of company's stations
761 * @param company whose stations' list show
763 void ShowCompanyStations(CompanyID company
)
765 if (!Company::IsValidID(company
)) return;
767 AllocateWindowDescFront
<CompanyStationsWindow
>(&_company_stations_desc
, company
);
770 static const NWidgetPart _nested_station_view_widgets
[] = {
771 NWidget(NWID_HORIZONTAL
),
772 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
773 NWidget(WWT_CAPTION
, COLOUR_GREY
, WID_SV_CAPTION
), SetDataTip(STR_STATION_VIEW_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
774 NWidget(WWT_SHADEBOX
, COLOUR_GREY
),
775 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
776 NWidget(WWT_STICKYBOX
, COLOUR_GREY
),
778 NWidget(NWID_HORIZONTAL
),
779 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SV_SORT_ORDER
), SetMinimalSize(81, 12), SetFill(1, 1), SetDataTip(STR_BUTTON_SORT_BY
, STR_TOOLTIP_SORT_ORDER
),
780 NWidget(WWT_DROPDOWN
, COLOUR_GREY
, WID_SV_SORT_BY
), SetMinimalSize(168, 12), SetResize(1, 0), SetFill(0, 1), SetDataTip(0x0, STR_TOOLTIP_SORT_CRITERIA
),
782 NWidget(NWID_HORIZONTAL
),
783 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_SV_GROUP
), SetMinimalSize(81, 12), SetFill(1, 1), SetDataTip(STR_STATION_VIEW_GROUP
, 0x0),
784 NWidget(WWT_DROPDOWN
, COLOUR_GREY
, WID_SV_GROUP_BY
), SetMinimalSize(168, 12), SetResize(1, 0), SetFill(0, 1), SetDataTip(0x0, STR_TOOLTIP_GROUP_ORDER
),
786 NWidget(NWID_HORIZONTAL
),
787 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_SV_WAITING
), SetMinimalSize(237, 44), SetResize(1, 10), SetScrollbar(WID_SV_SCROLLBAR
), EndContainer(),
788 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_SV_SCROLLBAR
),
790 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_SV_ACCEPT_RATING_LIST
), SetMinimalSize(249, 23), SetResize(1, 0), EndContainer(),
791 NWidget(NWID_HORIZONTAL
),
792 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
793 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SV_LOCATION
), SetMinimalSize(45, 12), SetResize(1, 0), SetFill(1, 1),
794 SetDataTip(STR_BUTTON_LOCATION
, STR_STATION_VIEW_CENTER_TOOLTIP
),
795 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SV_ACCEPTS_RATINGS
), SetMinimalSize(46, 12), SetResize(1, 0), SetFill(1, 1),
796 SetDataTip(STR_STATION_VIEW_RATINGS_BUTTON
, STR_STATION_VIEW_RATINGS_TOOLTIP
),
797 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SV_RENAME
), SetMinimalSize(45, 12), SetResize(1, 0), SetFill(1, 1),
798 SetDataTip(STR_BUTTON_RENAME
, STR_STATION_VIEW_RENAME_TOOLTIP
),
800 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_SV_CLOSE_AIRPORT
), SetMinimalSize(45, 12), SetResize(1, 0), SetFill(1, 1),
801 SetDataTip(STR_STATION_VIEW_CLOSE_AIRPORT
, STR_STATION_VIEW_CLOSE_AIRPORT_TOOLTIP
),
802 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SV_TRAINS
), SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_TRAIN
, STR_STATION_VIEW_SCHEDULED_TRAINS_TOOLTIP
),
803 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SV_ROADVEHS
), SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_LORRY
, STR_STATION_VIEW_SCHEDULED_ROAD_VEHICLES_TOOLTIP
),
804 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SV_SHIPS
), SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_SHIP
, STR_STATION_VIEW_SCHEDULED_SHIPS_TOOLTIP
),
805 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SV_PLANES
), SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_PLANE
, STR_STATION_VIEW_SCHEDULED_AIRCRAFT_TOOLTIP
),
806 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
),
811 * Draws icons of waiting cargo in the StationView window
813 * @param i type of cargo
814 * @param waiting number of waiting units
815 * @param dpi area to draw on
816 * @param left left most coordinate to draw on
817 * @param right right most coordinate to draw on
818 * @param y y coordinate
819 * @param width the width of the view
821 static void DrawCargoIcons (CargoID i
, uint waiting
, BlitArea
*dpi
,
822 int left
, int right
, int y
)
824 int width
= ScaleGUITrad(10);
825 uint num
= min((waiting
+ (width
/ 2)) / width
, (right
- left
) / width
); // maximum is width / 10 icons so it won't overflow
826 if (num
== 0) return;
828 SpriteID sprite
= CargoSpec::Get(i
)->GetCargoIcon();
830 int x
= _current_text_dir
== TD_RTL
? left
: right
- num
* width
;
832 DrawSprite (dpi
, sprite
, PAL_NONE
, x
, y
);
843 ST_COUNT
, ///< by amount of cargo
844 ST_STATION_STRING
, ///< by station name
845 ST_STATION_ID
, ///< by station id
849 /** A node in the tree of cached destinations for a cargo type in a station. */
850 struct CargoDestNode
{
851 typedef std::map
<StationID
, CargoDestNode
> map
;
852 typedef map::const_iterator iterator
;
854 CargoDestNode
*const parent
; ///< the parent of this entry
855 uint count
; ///< amount of cargo for this node and children
856 map children
; ///< children of this node
858 CargoDestNode (CargoDestNode
*p
= NULL
)
859 : parent(p
), count(0), children()
865 this->children
.clear();
868 iterator
begin (void) const
870 return this->children
.begin();
873 iterator
end (void) const
875 return this->children
.end();
878 const CargoDestNode
*find (StationID id
) const
880 iterator
iter (this->children
.find (id
));
881 return (iter
!= end()) ? &iter
->second
: NULL
;
884 CargoDestNode
*insert (StationID id
);
886 void update (uint count
);
888 void estimate (CargoID cargo
, StationID source
, StationID next
,
892 /** Find or insert a child node of the current node. */
893 CargoDestNode
*CargoDestNode::insert (StationID id
)
895 const std::pair
<map::iterator
, map::iterator
> range
896 (this->children
.equal_range (id
));
898 if (range
.first
!= range
.second
) return &range
.first
->second
;
900 map::iterator
iter (this->children
.insert (range
.first
,
901 std::make_pair (id
, CargoDestNode (this))));
903 return &iter
->second
;
907 * Update the count for this node and propagate the change uptree.
908 * @param count The amount to be added to this node.
910 void CargoDestNode::update (uint count
)
912 CargoDestNode
*n
= this;
920 * Estimate the amounts of cargo per final destination for a given cargo,
921 * source station and next hop.
922 * @param cargo ID of the cargo to estimate destinations for.
923 * @param source Source station of the given batch of cargo.
924 * @param next Intermediate hop to start the calculation at ("next hop").
925 * @param count Size of the batch of cargo.
927 void CargoDestNode::estimate (CargoID cargo
, StationID source
,
928 StationID next
, uint count
)
930 if (!Station::IsValidID(next
) || !Station::IsValidID(source
)) {
931 this->insert (INVALID_STATION
)->update (count
);
935 std::map
<StationID
, uint
> tmp
;
938 const FlowStatMap
&flowmap
= Station::Get(next
)->goods
[cargo
].flows
;
939 FlowStatMap::const_iterator map_it
= flowmap
.find(source
);
940 if (map_it
!= flowmap
.end()) {
941 const FlowStat::SharesMap
*shares
= map_it
->second
.GetShares();
942 uint32 prev_count
= 0;
943 for (FlowStat::SharesMap::const_iterator i
= shares
->begin(); i
!= shares
->end(); ++i
) {
944 uint add
= i
->first
- prev_count
;
945 tmp
[i
->second
] += add
;
947 prev_count
= i
->first
;
951 if (tmp_count
== 0) {
952 this->insert (INVALID_STATION
)->update (count
);
956 uint sum_estimated
= 0;
957 while (sum_estimated
< count
) {
958 for (std::map
<StationID
, uint
>::iterator i
= tmp
.begin(); i
!= tmp
.end() && sum_estimated
< count
; ++i
) {
959 uint estimate
= DivideApprox (i
->second
* count
, tmp_count
);
960 if (estimate
== 0) estimate
= 1;
962 sum_estimated
+= estimate
;
963 if (sum_estimated
> count
) {
964 estimate
-= sum_estimated
- count
;
965 sum_estimated
= count
;
966 if (estimate
== 0) break;
969 if (i
->first
== next
) {
970 this->insert(next
)->update(estimate
);
972 this->estimate (cargo
, source
, i
->first
, estimate
);
979 * Rebuild the cache for estimated destinations which is used to quickly show the "destination" entries
980 * even if we actually don't know the destination of a certain packet from just looking at it.
981 * @param dest CargoDestNode to save the results in.
982 * @param st Station to recalculate the cache for.
983 * @param i Cargo to recalculate the cache for.
985 static void RecalcDestinations (CargoDestNode
*dest
, const Station
*st
, CargoID i
)
989 const FlowStatMap
&flows
= st
->goods
[i
].flows
;
990 for (FlowStatMap::const_iterator it
= flows
.begin(); it
!= flows
.end(); ++it
) {
991 StationID from
= it
->first
;
992 CargoDestNode
*source_entry
= dest
->insert (from
);
993 const FlowStat::SharesMap
*shares
= it
->second
.GetShares();
994 uint32 prev_count
= 0;
995 for (FlowStat::SharesMap::const_iterator flow_it
= shares
->begin(); flow_it
!= shares
->end(); ++flow_it
) {
996 StationID via
= flow_it
->second
;
997 CargoDestNode
*via_entry
= source_entry
->insert (via
);
998 if (via
== st
->index
) {
999 via_entry
->insert(via
)->update (flow_it
->first
- prev_count
);
1001 via_entry
->estimate (i
, from
, via
, flow_it
->first
- prev_count
);
1003 prev_count
= flow_it
->first
;
1009 struct expanded_map
: std::map
<StationID
, expanded_map
> { };
1011 class CargoNodeEntry
{
1013 typedef std::map
<StationID
, ttd_unique_ptr
<CargoNodeEntry
> > map
;
1015 CargoNodeEntry
*const parent
; ///< The parent of this entry.
1016 const StationID station
; ///< Station this entry is for.
1017 expanded_map
*const expanded
; ///< Map of expanded nodes, or NULL if this node is not expanded itself.
1018 uint count
; ///< Total amount of cargo under this node.
1019 map children
; ///< Children of this node, per station.
1022 CargoNodeEntry (StationID id
, expanded_map
*expanded
, CargoNodeEntry
*p
= NULL
)
1023 : parent(p
), station(id
), expanded(expanded
), count(0)
1028 typedef map::iterator iterator
;
1029 typedef map::const_iterator const_iterator
;
1031 /** Get the parent of this node. */
1032 const CargoNodeEntry
*get_parent (void) const
1034 return this->parent
;
1037 /** Get the station of this node. */
1038 StationID
get_station (void) const
1040 return this->station
;
1043 /** Get the expanded map of this node. */
1044 expanded_map
*get_expanded (void) const
1046 return this->expanded
;
1049 /** Get the total amount of cargo under this node. */
1050 uint
get_count (void) const
1055 /** Check if this node has no children. */
1056 bool empty (void) const
1058 return this->children
.empty();
1061 /** Check if there is a single child with the given id. */
1062 bool has_single_child (StationID station
) const
1064 return (this->children
.size() == 1) &&
1065 (this->children
.begin()->first
== station
);
1068 CargoNodeEntry
*insert (StationID station
, expanded_map
*expanded
);
1070 void update (uint count
);
1072 typedef std::vector
<const CargoNodeEntry
*> vector
;
1073 vector
sort (CargoSortType type
, SortOrder order
) const;
1076 /** Find or insert a child node of the current node. */
1077 CargoNodeEntry
*CargoNodeEntry::insert (StationID station
, expanded_map
*expanded
)
1079 const std::pair
<map::iterator
, map::iterator
> range
1080 (this->children
.equal_range (station
));
1082 if (range
.first
!= range
.second
) {
1083 CargoNodeEntry
*n
= range
.first
->second
.get();
1084 assert (n
->expanded
== expanded
);
1088 CargoNodeEntry
*n
= new CargoNodeEntry (station
, expanded
, this);
1089 map::iterator
iter (this->children
.insert (range
.first
,
1090 std::make_pair (station
, ttd_unique_ptr
<CargoNodeEntry
>(n
))));
1092 return iter
->second
.get();
1096 * Update the count for this node and propagate the change uptree.
1097 * @param count The amount to be added to this node.
1099 void CargoNodeEntry::update (uint count
)
1101 CargoNodeEntry
*n
= this;
1105 } while (n
!= NULL
);
1108 /** Compare two numbers in the given order. */
1110 static inline bool sort_id (Tid st1
, Tid st2
, SortOrder order
)
1112 return (order
== SO_ASCENDING
) ? st1
< st2
: st2
< st1
;
1115 /** Compare two stations, as given by their id, by their name. */
1116 static bool sort_station (StationID st1
, StationID st2
, SortOrder order
)
1118 static char buf1
[MAX_LENGTH_STATION_NAME_CHARS
];
1119 static char buf2
[MAX_LENGTH_STATION_NAME_CHARS
];
1121 if (!Station::IsValidID(st1
)) {
1122 return Station::IsValidID(st2
) ? (order
== SO_ASCENDING
) : sort_id (st1
, st2
, order
);
1123 } else if (!Station::IsValidID(st2
)) {
1124 return order
== SO_DESCENDING
;
1128 GetString (buf1
, STR_STATION_NAME
);
1130 GetString (buf2
, STR_STATION_NAME
);
1132 int res
= strnatcmp(buf1
, buf2
); // Sort by name (natural sorting).
1133 return (res
== 0) ? sort_id (st1
, st2
, order
) :
1134 (order
== SO_ASCENDING
) ? (res
< 0) : (res
> 0);
1137 struct CargoNodeSorter
{
1138 const CargoSortType type
;
1139 const SortOrder order
;
1141 CargoNodeSorter (CargoSortType t
, SortOrder o
) : type(t
), order(o
)
1145 bool operator() (const CargoNodeEntry
*a
, const CargoNodeEntry
*b
);
1148 bool CargoNodeSorter::operator() (const CargoNodeEntry
*a
,
1149 const CargoNodeEntry
*b
)
1151 switch (this->type
) {
1153 uint ca
= a
->get_count();
1154 uint cb
= b
->get_count();
1156 return (this->order
== SO_ASCENDING
) ?
1157 (ca
< cb
) : (cb
< ca
);
1159 } /* fall through */
1160 case ST_STATION_STRING
:
1161 return sort_station (a
->get_station(),
1162 b
->get_station(), this->order
);
1164 return sort_id (a
->get_station(), b
->get_station(),
1171 /** Sort the children into a vector. */
1172 inline CargoNodeEntry::vector
CargoNodeEntry::sort (CargoSortType type
,
1173 SortOrder order
) const
1176 v
.reserve (this->children
.size());
1178 map::const_iterator iter
= this->children
.begin();
1179 while (iter
!= this->children
.end()) {
1180 v
.push_back ((iter
++)->second
.get());
1183 std::sort (v
.begin(), v
.end(), CargoNodeSorter (type
, order
));
1188 class CargoRootEntry
: public CargoNodeEntry
{
1190 bool transfers
; ///< If there are transfers for this cargo.
1191 uint reserved
; ///< Reserved amount of cargo.
1194 CargoRootEntry (StationID station
, expanded_map
*expanded
)
1195 : CargoNodeEntry (station
, expanded
), reserved (0)
1199 /** Set the transfers state. */
1200 void set_transfers (bool value
) { this->transfers
= value
; }
1202 /** Get the transfers state. */
1203 bool get_transfers (void) const { return this->transfers
; }
1205 /** Update the reserved count. */
1206 void update_reserved (uint count
)
1208 this->reserved
+= count
;
1209 this->update (count
);
1212 /** Get the reserved count. */
1213 uint
get_reserved (void) const
1215 return this->reserved
;
1221 * The StationView window
1223 struct StationViewWindow
: public Window
{
1225 * A row being displayed in the cargo view (as opposed to being "hidden" behind a plus sign).
1228 RowDisplay(expanded_map
*f
, StationID n
) : filter(f
), next_station(n
) {}
1229 RowDisplay(CargoID n
) : filter(NULL
), next_cargo(n
) {}
1232 * Parent of the cargo entry belonging to the row.
1234 expanded_map
*filter
;
1237 * ID of the station belonging to the entry actually displayed if it's to/from/via.
1239 StationID next_station
;
1242 * ID of the cargo belonging to the entry actually displayed if it's cargo.
1248 typedef std::vector
<RowDisplay
> CargoDataVector
;
1250 static const int NUM_COLUMNS
= 3; ///< Number of extra "columns" in the cargo view: from, via, to
1253 * Type of data invalidation.
1256 INV_FLOWS
= 0x100, ///< The planned flows have been recalculated and everything has to be updated.
1257 INV_CARGO
= 0x200 ///< Some cargo has been added or removed.
1261 * Type of grouping used in each of the "columns".
1264 GR_SOURCE
, ///< Group by source of cargo ("from").
1265 GR_NEXT
, ///< Group by next station ("via").
1266 GR_DESTINATION
, ///< Group by estimated final destination ("to").
1270 * Display mode of the cargo view.
1273 MODE_WAITING
, ///< Show cargo waiting at the station.
1274 MODE_PLANNED
///< Show cargo planned to pass through the station.
1277 uint expand_shrink_width
; ///< The width allocated to the expand/shrink 'button'
1278 int rating_lines
; ///< Number of lines in the cargo ratings view.
1279 int accepts_lines
; ///< Number of lines in the accepted cargo view.
1282 /** Height of the #WID_SV_ACCEPT_RATING_LIST widget for different views. */
1283 enum AcceptListHeight
{
1284 ALH_RATING
= 13, ///< Height of the cargo ratings view.
1285 ALH_ACCEPTS
= 3, ///< Height of the accepted cargo view.
1288 static const StringID _sort_names
[]; ///< Names of the sorting options in the dropdown.
1289 static const StringID _group_names
[]; ///< Names of the grouping options in the dropdown.
1291 static const Grouping arrangements
[6][NUM_COLUMNS
]; ///< Possible grouping arrangements.
1294 * Sort types of the different 'columns'.
1295 * In fact only ST_COUNT and ST_STATION_STRING are active and you can only
1296 * sort all the columns in the same way. The other options haven't been
1297 * included in the GUI due to lack of space.
1299 CargoSortType sorting
;
1301 /** Sort order (ascending/descending) for the 'columns'. */
1302 SortOrder sort_order
;
1304 int scroll_to_row
; ///< If set, scroll the main viewport to the station pointed to by this row.
1305 int grouping_index
; ///< Currently selected entry in the grouping drop down.
1306 Mode current_mode
; ///< Currently selected display mode of cargo view.
1307 const Grouping
*groupings
; ///< Grouping modes for the different columns.
1309 CargoDestNode cached_destinations
[NUM_CARGO
]; ///< Cache for the flows passing through this station.
1310 std::bitset
<NUM_CARGO
> cached_destinations_valid
; ///< Bitset of up-to-date cached_destinations entries
1312 std::bitset
<NUM_CARGO
> expanded_cargoes
; ///< Bitset of expanded cargo rows.
1313 expanded_map expanded_rows
[NUM_CARGO
]; ///< Parent entry of currently expanded rows.
1315 CargoDataVector displayed_rows
; ///< Parent entry of currently displayed rows (including collapsed ones).
1317 StationViewWindow (const WindowDesc
*desc
, WindowNumber window_number
) :
1318 Window (desc
), expand_shrink_width (0),
1319 rating_lines (ALH_RATING
), accepts_lines (ALH_ACCEPTS
),
1321 sorting (ST_COUNT
), sort_order (SO_DESCENDING
),
1322 scroll_to_row (INT_MAX
), grouping_index (0),
1323 current_mode (MODE_WAITING
), groupings (NULL
),
1324 cached_destinations(), cached_destinations_valid(),
1325 expanded_cargoes(), expanded_rows(), displayed_rows()
1327 this->CreateNestedTree();
1328 this->vscroll
= this->GetScrollbar(WID_SV_SCROLLBAR
);
1329 /* Nested widget tree creation is done in two steps to ensure that this->GetWidget<NWidgetCore>(WID_SV_ACCEPTS_RATINGS) exists in UpdateWidgetSize(). */
1330 this->InitNested(window_number
);
1332 this->SelectGroupBy(_settings_client
.gui
.station_gui_group_order
);
1333 this->SelectSortBy(_settings_client
.gui
.station_gui_sort_by
);
1334 this->SelectSortOrder((SortOrder
)_settings_client
.gui
.station_gui_sort_order
);
1335 this->owner
= Station::Get(window_number
)->owner
;
1338 void OnDelete (void) FINAL_OVERRIDE
1340 DeleteWindowById(WC_TRAINS_LIST
, VehicleListIdentifier(VL_STATION_LIST
, VEH_TRAIN
, this->owner
, this->window_number
).Pack(), false);
1341 DeleteWindowById(WC_ROADVEH_LIST
, VehicleListIdentifier(VL_STATION_LIST
, VEH_ROAD
, this->owner
, this->window_number
).Pack(), false);
1342 DeleteWindowById(WC_SHIPS_LIST
, VehicleListIdentifier(VL_STATION_LIST
, VEH_SHIP
, this->owner
, this->window_number
).Pack(), false);
1343 DeleteWindowById(WC_AIRCRAFT_LIST
, VehicleListIdentifier(VL_STATION_LIST
, VEH_AIRCRAFT
, this->owner
, this->window_number
).Pack(), false);
1347 * Show a certain cargo entry characterized by source/next/dest station, cargo ID and amount of cargo at the
1348 * right place in the cargo view. I.e. update as many rows as are expanded following that characterization.
1349 * @param data Root entry of the tree.
1350 * @param cargo Cargo ID of the entry to be shown.
1351 * @param source Source station of the entry to be shown.
1352 * @param next Next station the cargo to be shown will visit.
1353 * @param dest Final destination of the cargo to be shown.
1354 * @param count Amount of cargo to be shown.
1356 void ShowCargo (CargoRootEntry
*root
, CargoID cargo
, StationID source
, StationID next
, StationID dest
, uint count
)
1358 if (count
== 0) return;
1360 root
->set_transfers (source
!= this->window_number
);
1361 CargoNodeEntry
*data
= root
;
1363 if (this->expanded_cargoes
.test (cargo
)) {
1364 if (_settings_game
.linkgraph
.GetDistributionType(cargo
) != DT_MANUAL
) {
1365 for (int i
= 0; i
< NUM_COLUMNS
; ++i
) {
1367 switch (groupings
[i
]) {
1368 default: NOT_REACHED();
1375 case GR_DESTINATION
:
1379 expanded_map
*expand
= data
->get_expanded();
1380 expanded_map::iterator iter
= expand
->find (s
);
1381 if (iter
!= expand
->end()) {
1382 data
= data
->insert (s
, &iter
->second
);
1384 data
= data
->insert (s
, NULL
);
1389 if (source
!= this->window_number
) {
1390 data
= data
->insert (source
, NULL
);
1395 data
->update (count
);
1398 virtual void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
1401 case WID_SV_WAITING
:
1402 resize
->height
= FONT_HEIGHT_NORMAL
;
1403 size
->height
= WD_FRAMERECT_TOP
+ 4 * resize
->height
+ WD_FRAMERECT_BOTTOM
;
1404 this->expand_shrink_width
= max(GetStringBoundingBox("-").width
, GetStringBoundingBox("+").width
) + WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
1407 case WID_SV_ACCEPT_RATING_LIST
:
1408 size
->height
= WD_FRAMERECT_TOP
+ ((this->GetWidget
<NWidgetCore
>(WID_SV_ACCEPTS_RATINGS
)->widget_data
== STR_STATION_VIEW_RATINGS_BUTTON
) ? this->accepts_lines
: this->rating_lines
) * FONT_HEIGHT_NORMAL
+ WD_FRAMERECT_BOTTOM
;
1411 case WID_SV_CLOSE_AIRPORT
:
1412 if (!(Station::Get(this->window_number
)->facilities
& FACIL_AIRPORT
)) {
1413 /* Hide 'Close Airport' button if no airport present. */
1422 void OnPaint (BlitArea
*dpi
) OVERRIDE
1424 const Station
*st
= Station::Get(this->window_number
);
1426 /* disable some buttons */
1427 this->SetWidgetDisabledState(WID_SV_RENAME
, st
->owner
!= _local_company
);
1428 this->SetWidgetDisabledState(WID_SV_TRAINS
, !(st
->facilities
& FACIL_TRAIN
));
1429 this->SetWidgetDisabledState(WID_SV_ROADVEHS
, !(st
->facilities
& FACIL_TRUCK_STOP
) && !(st
->facilities
& FACIL_BUS_STOP
));
1430 this->SetWidgetDisabledState(WID_SV_SHIPS
, !(st
->facilities
& FACIL_DOCK
));
1431 this->SetWidgetDisabledState(WID_SV_PLANES
, !(st
->facilities
& FACIL_AIRPORT
));
1432 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
1433 this->SetWidgetLoweredState(WID_SV_CLOSE_AIRPORT
, (st
->facilities
& FACIL_AIRPORT
) && (st
->airport
.flags
& AIRPORT_CLOSED_block
) != 0);
1435 this->DrawWidgets (dpi
);
1437 if (!this->IsShaded()) {
1438 /* Draw 'accepted cargo' or 'cargo ratings'. */
1439 const NWidgetBase
*wid
= this->GetWidget
<NWidgetBase
>(WID_SV_ACCEPT_RATING_LIST
);
1440 const Rect r
= {(int)wid
->pos_x
, (int)wid
->pos_y
, (int)(wid
->pos_x
+ wid
->current_x
- 1), (int)(wid
->pos_y
+ wid
->current_y
- 1)};
1441 if (this->GetWidget
<NWidgetCore
>(WID_SV_ACCEPTS_RATINGS
)->widget_data
== STR_STATION_VIEW_RATINGS_BUTTON
) {
1442 int lines
= this->DrawAcceptedCargo (dpi
, r
);
1443 if (lines
> this->accepts_lines
) { // Resize the widget, and perform re-initialization of the window.
1444 this->accepts_lines
= lines
;
1449 int lines
= this->DrawCargoRatings (dpi
, r
);
1450 if (lines
> this->rating_lines
) { // Resize the widget, and perform re-initialization of the window.
1451 this->rating_lines
= lines
;
1457 /* Draw arrow pointing up/down for ascending/descending sorting */
1458 this->DrawSortButtonState (dpi
, WID_SV_SORT_ORDER
, sort_order
== SO_ASCENDING
? SBS_UP
: SBS_DOWN
);
1460 int pos
= this->vscroll
->GetPosition();
1462 int maxrows
= this->vscroll
->GetCapacity();
1464 displayed_rows
.clear();
1466 /* Draw waiting cargo. */
1467 NWidgetBase
*nwi
= this->GetWidget
<NWidgetBase
>(WID_SV_WAITING
);
1468 Rect waiting_rect
= { (int)nwi
->pos_x
, (int)nwi
->pos_y
, (int)(nwi
->pos_x
+ nwi
->current_x
- 1), (int)(nwi
->pos_y
+ nwi
->current_y
- 1)};
1470 for (CargoID i
= 0; i
< NUM_CARGO
; i
++) {
1471 if (!this->cached_destinations_valid
.test (i
)) {
1472 this->cached_destinations_valid
.set (i
);
1473 RecalcDestinations (&this->cached_destinations
[i
], st
, i
);
1476 CargoRootEntry
cargo (this->window_number
, &this->expanded_rows
[i
]);
1477 if (this->current_mode
== MODE_WAITING
) {
1478 this->BuildCargoList (i
, st
->goods
[i
].cargo
, &cargo
);
1480 this->BuildFlowList (i
, st
->goods
[i
].flows
, &cargo
);
1483 if (cargo
.get_count() > 0) {
1484 pos
= this->DrawCargoEntry (&cargo
, i
, dpi
, waiting_rect
, pos
, maxrows
);
1487 this->vscroll
->SetCount (this->vscroll
->GetPosition() - pos
); // update scrollbar
1489 scroll_to_row
= INT_MAX
;
1493 virtual void SetStringParameters(int widget
) const
1495 const Station
*st
= Station::Get(this->window_number
);
1496 SetDParam(0, st
->index
);
1497 SetDParam(1, st
->facilities
);
1501 * Build up the cargo view for PLANNED mode and a specific cargo.
1502 * @param i Cargo to show.
1503 * @param flows The current station's flows for that cargo.
1504 * @param cargo The CargoRootEntry to save the results in.
1506 void BuildFlowList (CargoID i
, const FlowStatMap
&flows
, CargoRootEntry
*cargo
)
1508 const CargoDestNode
*source_dest
= &this->cached_destinations
[i
];
1509 for (FlowStatMap::const_iterator it
= flows
.begin(); it
!= flows
.end(); ++it
) {
1510 StationID from
= it
->first
;
1511 const CargoDestNode
*source_entry
= source_dest
->find (from
);
1512 const FlowStat::SharesMap
*shares
= it
->second
.GetShares();
1513 for (FlowStat::SharesMap::const_iterator flow_it
= shares
->begin(); flow_it
!= shares
->end(); ++flow_it
) {
1514 const CargoDestNode
*via_entry
= source_entry
->find (flow_it
->second
);
1515 for (CargoDestNode::iterator dest_it
= via_entry
->begin(); dest_it
!= via_entry
->end(); ++dest_it
) {
1516 ShowCargo (cargo
, i
, from
, flow_it
->second
, dest_it
->first
, dest_it
->second
.count
);
1523 * Build up the cargo view for WAITING mode and a specific cargo.
1524 * @param i Cargo to show.
1525 * @param packets The current station's cargo list for that cargo.
1526 * @param cargo The CargoRootEntry to save the result in.
1528 void BuildCargoList (CargoID i
, const StationCargoList
&packets
, CargoRootEntry
*cargo
)
1530 const CargoDestNode
*source_dest
= &this->cached_destinations
[i
];
1531 for (StationCargoList::ConstIterator it
= packets
.Packets()->begin(); it
!= packets
.Packets()->end(); it
++) {
1532 const CargoPacket
*cp
= *it
;
1533 StationID next
= it
.GetKey();
1535 const CargoDestNode
*source_entry
= source_dest
->find (cp
->SourceStation());
1536 if (source_entry
== NULL
) {
1537 this->ShowCargo(cargo
, i
, cp
->SourceStation(), next
, INVALID_STATION
, cp
->Count());
1541 const CargoDestNode
*via_entry
= source_entry
->find (next
);
1542 if (via_entry
== NULL
) {
1543 this->ShowCargo(cargo
, i
, cp
->SourceStation(), next
, INVALID_STATION
, cp
->Count());
1547 for (CargoDestNode::iterator dest_it
= via_entry
->begin(); dest_it
!= via_entry
->end(); ++dest_it
) {
1548 uint val
= DivideApprox (cp
->Count() * dest_it
->second
.count
, via_entry
->count
);
1549 this->ShowCargo (cargo
, i
, cp
->SourceStation(), next
, dest_it
->first
, val
);
1553 uint reserved
= packets
.ReservedCount();
1554 if (reserved
!= 0) {
1555 if (this->expanded_cargoes
.test (i
)) {
1556 cargo
->set_transfers (true);
1557 cargo
->update_reserved (reserved
);
1559 cargo
->update (reserved
);
1565 * Select the correct string for an entry referring to the specified station.
1566 * @param station Station the entry is showing cargo for.
1567 * @param here String to be shown if the entry refers to the same station as this station GUI belongs to.
1568 * @param other_station String to be shown if the entry refers to a specific other station.
1569 * @param any String to be shown if the entry refers to "any station".
1570 * @return One of the three given strings, depending on what station the entry refers to.
1572 StringID
GetEntryString(StationID station
, StringID here
, StringID other_station
, StringID any
)
1574 if (station
== this->window_number
) {
1576 } else if (station
== INVALID_STATION
) {
1579 SetDParam(2, station
);
1580 return other_station
;
1585 * Determine if we need to show the special "non-stop" string.
1586 * @param cd Entry we are going to show.
1587 * @param station Station the entry refers to.
1588 * @param column The "column" the entry will be shown in.
1589 * @return either STR_STATION_VIEW_VIA or STR_STATION_VIEW_NONSTOP.
1591 StringID
SearchNonStop (const CargoNodeEntry
*cd
, StationID station
, int column
)
1593 const CargoNodeEntry
*parent
= cd
->get_parent();
1594 for (int i
= column
; i
> 0; --i
) {
1595 if (this->groupings
[i
- 1] == GR_DESTINATION
) {
1596 if (parent
->get_station() == station
) {
1597 return STR_STATION_VIEW_NONSTOP
;
1599 return STR_STATION_VIEW_VIA
;
1602 parent
= parent
->get_parent();
1605 if (this->groupings
[column
+ 1] == GR_DESTINATION
) {
1606 if (cd
->has_single_child (station
)) {
1607 return STR_STATION_VIEW_NONSTOP
;
1609 return STR_STATION_VIEW_VIA
;
1613 return STR_STATION_VIEW_VIA
;
1617 * Draw the cargo string for an entry in the station GUI.
1618 * @param dpi Area to draw on.
1619 * @param r Screen rectangle to draw into.
1620 * @param y Vertical position to draw at.
1621 * @param indent Extra indentation for the string.
1622 * @param sym Symbol to draw at the end of the line, if not null.
1623 * @param str String to draw.
1625 void DrawCargoString (BlitArea
*dpi
, const Rect
&r
, int y
, int indent
,
1626 const char *sym
, StringID str
)
1628 bool rtl
= _current_text_dir
== TD_RTL
;
1630 int text_left
= rtl
? r
.left
+ this->expand_shrink_width
: r
.left
+ WD_FRAMERECT_LEFT
+ indent
* this->expand_shrink_width
;
1631 int text_right
= rtl
? r
.right
- WD_FRAMERECT_LEFT
- indent
* this->expand_shrink_width
: r
.right
- this->expand_shrink_width
;
1632 DrawString (dpi
, text_left
, text_right
, y
, str
);
1635 int sym_left
= rtl
? r
.left
+ WD_FRAMERECT_LEFT
: r
.right
- this->expand_shrink_width
+ WD_FRAMERECT_LEFT
;
1636 int sym_right
= rtl
? r
.left
+ this->expand_shrink_width
- WD_FRAMERECT_RIGHT
: r
.right
- WD_FRAMERECT_RIGHT
;
1637 DrawString (dpi
, sym_left
, sym_right
, y
, sym
, TC_YELLOW
);
1642 * Draw the given cargo entries in the station GUI.
1643 * @param entry Root entry for all cargo to be drawn.
1644 * @param dpi Area to draw on.
1645 * @param r Screen rectangle to draw into.
1646 * @param pos Current row to be drawn to (counted down from 0 to -maxrows, same as vscroll->GetPosition()).
1647 * @param maxrows Maximum row to be drawn.
1648 * @param column Current "column" being drawn.
1649 * @param cargo Current cargo being drawn.
1650 * @return row (in "pos" counting) after the one we have last drawn to.
1652 int DrawEntries (const CargoNodeEntry
*entry
, BlitArea
*dpi
, const Rect
&r
,
1653 int pos
, int maxrows
, int column
, CargoID cargo
)
1655 assert (entry
->empty() || (entry
->get_expanded() != NULL
));
1657 typedef CargoNodeEntry::vector vector
;
1658 vector v
= entry
->sort (this->sorting
, this->sort_order
);
1660 for (vector::const_iterator i
= v
.begin(); i
!= v
.end(); ++i
) {
1661 const CargoNodeEntry
*cd
= *i
;
1663 bool auto_distributed
= _settings_game
.linkgraph
.GetDistributionType(cargo
) != DT_MANUAL
;
1664 assert (auto_distributed
|| (column
== 0));
1666 if (pos
> -maxrows
&& pos
<= 0) {
1667 StringID str
= STR_EMPTY
;
1668 int y
= r
.top
+ WD_FRAMERECT_TOP
- pos
* FONT_HEIGHT_NORMAL
;
1669 SetDParam(0, cargo
);
1670 SetDParam(1, cd
->get_count());
1672 Grouping grouping
= auto_distributed
? this->groupings
[column
] : GR_SOURCE
;
1673 StationID station
= cd
->get_station();
1677 str
= this->GetEntryString(station
, STR_STATION_VIEW_FROM_HERE
, STR_STATION_VIEW_FROM
, STR_STATION_VIEW_FROM_ANY
);
1680 str
= this->GetEntryString(station
, STR_STATION_VIEW_VIA_HERE
, STR_STATION_VIEW_VIA
, STR_STATION_VIEW_VIA_ANY
);
1681 if (str
== STR_STATION_VIEW_VIA
) str
= this->SearchNonStop(cd
, station
, column
);
1683 case GR_DESTINATION
:
1684 str
= this->GetEntryString(station
, STR_STATION_VIEW_TO_HERE
, STR_STATION_VIEW_TO
, STR_STATION_VIEW_TO_ANY
);
1689 if (pos
== -this->scroll_to_row
&& Station::IsValidID(station
)) {
1690 ScrollMainWindowToTile(Station::Get(station
)->xy
);
1693 const char *sym
= NULL
;
1694 if (column
< NUM_COLUMNS
- 1) {
1697 } else if (auto_distributed
) {
1702 this->DrawCargoString (dpi
, r
, y
, column
+ 1, sym
, str
);
1704 expanded_map
*expand
= entry
->get_expanded();
1705 assert (expand
!= NULL
);
1706 this->displayed_rows
.push_back (RowDisplay (expand
, station
));
1709 if (auto_distributed
) {
1710 pos
= this->DrawEntries (cd
, dpi
, r
, pos
, maxrows
, column
+ 1, cargo
);
1717 * Draw the given cargo entry in the station GUI.
1718 * @param cd Cargo entry to be drawn.
1719 * @param cargo Cargo type for this entry.
1720 * @param dpi Area to draw on.
1721 * @param r Screen rectangle to draw into.
1722 * @param pos Current row to be drawn to (counted down from 0 to -maxrows, same as vscroll->GetPosition()).
1723 * @param maxrows Maximum row to be drawn.
1724 * @return row (in "pos" counting) after the one we have last drawn to.
1726 int DrawCargoEntry (const CargoRootEntry
*cd
, CargoID cargo
,
1727 BlitArea
*dpi
, const Rect
&r
, int pos
, int maxrows
)
1729 bool auto_distributed
= _settings_game
.linkgraph
.GetDistributionType(cargo
) != DT_MANUAL
;
1731 if (pos
> -maxrows
&& pos
<= 0) {
1732 int y
= r
.top
+ WD_FRAMERECT_TOP
- pos
* FONT_HEIGHT_NORMAL
;
1733 SetDParam(0, cargo
);
1734 SetDParam(1, cd
->get_count());
1735 StringID str
= STR_STATION_VIEW_WAITING_CARGO
;
1736 DrawCargoIcons (cargo
, cd
->get_count(), dpi
, r
.left
+ WD_FRAMERECT_LEFT
+ this->expand_shrink_width
, r
.right
- WD_FRAMERECT_RIGHT
- this->expand_shrink_width
, y
);
1738 const char *sym
= NULL
;
1739 if (!cd
->empty() || (cd
->get_reserved() > 0)) {
1741 } else if (auto_distributed
) {
1744 /* Only draw '+' if there is something to be shown. */
1745 const StationCargoList
&list
= Station::Get(this->window_number
)->goods
[cargo
].cargo
;
1746 if (list
.ReservedCount() > 0 || cd
->get_transfers()) {
1751 this->DrawCargoString (dpi
, r
, y
, 0, sym
, str
);
1753 this->displayed_rows
.push_back (RowDisplay (cargo
));
1756 pos
= this->DrawEntries (cd
, dpi
, r
, pos
- 1, maxrows
, 0, cargo
);
1758 if (cd
->get_reserved() != 0) {
1759 if (pos
> -maxrows
&& pos
<= 0) {
1760 int y
= r
.top
+ WD_FRAMERECT_TOP
- pos
* FONT_HEIGHT_NORMAL
;
1761 SetDParam (0, cargo
);
1762 SetDParam (1, cd
->get_reserved());
1763 this->DrawCargoString (dpi
, r
, y
, 1, NULL
, STR_STATION_VIEW_RESERVED
);
1764 this->displayed_rows
.push_back (RowDisplay (INVALID_CARGO
));
1773 * Draw accepted cargo in the #WID_SV_ACCEPT_RATING_LIST widget.
1774 * @param dpi Area to draw on.
1775 * @param r Rectangle of the widget.
1776 * @return Number of lines needed for drawing the accepted cargo.
1778 int DrawAcceptedCargo (BlitArea
*dpi
, const Rect
&r
) const
1780 const Station
*st
= Station::Get(this->window_number
);
1782 uint32 cargo_mask
= 0;
1783 for (CargoID i
= 0; i
< NUM_CARGO
; i
++) {
1784 if (HasBit(st
->goods
[i
].status
, GoodsEntry::GES_ACCEPTANCE
)) SetBit(cargo_mask
, i
);
1786 SetDParam(0, cargo_mask
);
1787 int bottom
= DrawStringMultiLine (dpi
, r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, r
.top
+ WD_FRAMERECT_TOP
, INT32_MAX
, STR_STATION_VIEW_ACCEPTS_CARGO
);
1788 return CeilDiv(bottom
- r
.top
- WD_FRAMERECT_TOP
, FONT_HEIGHT_NORMAL
);
1792 * Draw cargo ratings in the #WID_SV_ACCEPT_RATING_LIST widget.
1793 * @param dpi Area to draw on.
1794 * @param r Rectangle of the widget.
1795 * @return Number of lines needed for drawing the cargo ratings.
1797 int DrawCargoRatings (BlitArea
*dpi
, const Rect
&r
) const
1799 const Station
*st
= Station::Get(this->window_number
);
1800 int y
= r
.top
+ WD_FRAMERECT_TOP
;
1802 if (st
->town
->exclusive_counter
> 0) {
1803 SetDParam(0, st
->town
->exclusivity
);
1804 y
= DrawStringMultiLine (dpi
, r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
, r
.bottom
, st
->town
->exclusivity
== st
->owner
? STR_STATION_VIEW_EXCLUSIVE_RIGHTS_SELF
: STR_STATION_VIEW_EXCLUSIVE_RIGHTS_COMPANY
);
1805 y
+= WD_PAR_VSEP_WIDE
;
1808 DrawString (dpi
, r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
, STR_STATION_VIEW_SUPPLY_RATINGS_TITLE
);
1809 y
+= FONT_HEIGHT_NORMAL
;
1811 const CargoSpec
*cs
;
1812 FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs
) {
1813 const GoodsEntry
*ge
= &st
->goods
[cs
->Index()];
1814 if (!ge
->HasRating()) continue;
1816 const LinkGraph
*lg
= LinkGraph::GetIfValid(ge
->link_graph
);
1817 SetDParam(0, cs
->name
);
1818 SetDParam(1, lg
!= NULL
? lg
->Monthly((*lg
)[ge
->node
]->Supply()) : 0);
1819 SetDParam(2, STR_CARGO_RATING_APPALLING
+ (ge
->rating
>> 5));
1820 SetDParam(3, ToPercent8(ge
->rating
));
1821 DrawString (dpi
, r
.left
+ WD_FRAMERECT_LEFT
+ 6, r
.right
- WD_FRAMERECT_RIGHT
- 6, y
, STR_STATION_VIEW_CARGO_SUPPLY_RATING
);
1822 y
+= FONT_HEIGHT_NORMAL
;
1824 return CeilDiv(y
- r
.top
- WD_FRAMERECT_TOP
, FONT_HEIGHT_NORMAL
);
1828 * Handle a click on a specific row in the cargo view.
1829 * @param row Row being clicked.
1831 void HandleCargoWaitingClick(int row
)
1833 if (row
< 0 || (uint
)row
>= this->displayed_rows
.size()) return;
1834 if (_ctrl_pressed
) {
1835 this->scroll_to_row
= row
;
1837 RowDisplay
&display
= this->displayed_rows
[row
];
1838 expanded_map
*filter
= display
.filter
;
1839 if (filter
!= NULL
) {
1840 StationID next
= display
.next_station
;
1841 expanded_map::iterator iter
= filter
->find (next
);
1842 if (iter
!= filter
->end()) {
1843 filter
->erase (iter
);
1847 } else if (display
.next_cargo
!= INVALID_CARGO
) {
1848 this->expanded_cargoes
.flip (display
.next_cargo
);
1851 this->SetWidgetDirty(WID_SV_WAITING
);
1854 virtual void OnClick(Point pt
, int widget
, int click_count
)
1857 case WID_SV_WAITING
:
1858 this->HandleCargoWaitingClick(this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_SV_WAITING
, WD_FRAMERECT_TOP
, FONT_HEIGHT_NORMAL
) - this->vscroll
->GetPosition());
1861 case WID_SV_LOCATION
:
1862 if (_ctrl_pressed
) {
1863 ShowExtraViewPortWindow(Station::Get(this->window_number
)->xy
);
1865 ScrollMainWindowToTile(Station::Get(this->window_number
)->xy
);
1869 case WID_SV_ACCEPTS_RATINGS
: {
1870 /* Swap between 'accepts' and 'ratings' view. */
1872 NWidgetCore
*nwi
= this->GetWidget
<NWidgetCore
>(WID_SV_ACCEPTS_RATINGS
);
1873 if (this->GetWidget
<NWidgetCore
>(WID_SV_ACCEPTS_RATINGS
)->widget_data
== STR_STATION_VIEW_RATINGS_BUTTON
) {
1874 nwi
->SetDataTip(STR_STATION_VIEW_ACCEPTS_BUTTON
, STR_STATION_VIEW_ACCEPTS_TOOLTIP
); // Switch to accepts view.
1875 height_change
= this->rating_lines
- this->accepts_lines
;
1877 nwi
->SetDataTip(STR_STATION_VIEW_RATINGS_BUTTON
, STR_STATION_VIEW_RATINGS_TOOLTIP
); // Switch to ratings view.
1878 height_change
= this->accepts_lines
- this->rating_lines
;
1880 this->ReInit(0, height_change
* FONT_HEIGHT_NORMAL
);
1885 SetDParam(0, this->window_number
);
1886 ShowQueryString(STR_STATION_NAME
, STR_STATION_VIEW_RENAME_STATION_CAPTION
, MAX_LENGTH_STATION_NAME_CHARS
,
1887 this, CS_ALPHANUMERAL
, QSF_ENABLE_DEFAULT
| QSF_LEN_IN_CHARS
);
1890 case WID_SV_CLOSE_AIRPORT
:
1891 DoCommandP(0, this->window_number
, 0, CMD_OPEN_CLOSE_AIRPORT
);
1894 case WID_SV_TRAINS
: // Show list of scheduled trains to this station
1895 case WID_SV_ROADVEHS
: // Show list of scheduled road-vehicles to this station
1896 case WID_SV_SHIPS
: // Show list of scheduled ships to this station
1897 case WID_SV_PLANES
: { // Show list of scheduled aircraft to this station
1898 Owner owner
= Station::Get(this->window_number
)->owner
;
1899 ShowVehicleListWindow(owner
, (VehicleType
)(widget
- WID_SV_TRAINS
), (StationID
)this->window_number
);
1903 case WID_SV_SORT_BY
: {
1904 /* The initial selection is composed of current mode and
1905 * sorting criteria for columns 1, 2, and 3. Column 0 is always
1906 * sorted by cargo ID. The others can theoretically be sorted
1907 * by different things but there is no UI for that. */
1908 ShowDropDownMenu(this, _sort_names
,
1909 this->current_mode
* 2 + (this->sorting
== ST_COUNT
? 1 : 0),
1910 WID_SV_SORT_BY
, 0, 0);
1914 case WID_SV_GROUP_BY
: {
1915 ShowDropDownMenu(this, _group_names
, this->grouping_index
, WID_SV_GROUP_BY
, 0, 0);
1919 case WID_SV_SORT_ORDER
: { // flip sorting method asc/desc
1920 this->SelectSortOrder (this->sort_order
== SO_ASCENDING
? SO_DESCENDING
: SO_ASCENDING
);
1922 this->LowerWidget(WID_SV_SORT_ORDER
);
1929 * Select a new sort order for the cargo view.
1930 * @param order New sort order.
1932 void SelectSortOrder(SortOrder order
)
1934 this->sort_order
= order
;
1935 _settings_client
.gui
.station_gui_sort_order
= order
;
1940 * Select a new sort criterium for the cargo view.
1941 * @param index Row being selected in the sort criteria drop down.
1943 void SelectSortBy(int index
)
1945 _settings_client
.gui
.station_gui_sort_by
= index
;
1946 switch (_sort_names
[index
]) {
1947 case STR_STATION_VIEW_WAITING_STATION
:
1948 this->current_mode
= MODE_WAITING
;
1949 this->sorting
= ST_STATION_STRING
;
1951 case STR_STATION_VIEW_WAITING_AMOUNT
:
1952 this->current_mode
= MODE_WAITING
;
1953 this->sorting
= ST_COUNT
;
1955 case STR_STATION_VIEW_PLANNED_STATION
:
1956 this->current_mode
= MODE_PLANNED
;
1957 this->sorting
= ST_STATION_STRING
;
1959 case STR_STATION_VIEW_PLANNED_AMOUNT
:
1960 this->current_mode
= MODE_PLANNED
;
1961 this->sorting
= ST_COUNT
;
1966 /* Display the current sort variant */
1967 this->GetWidget
<NWidgetCore
>(WID_SV_SORT_BY
)->widget_data
= _sort_names
[index
];
1972 * Select a new grouping mode for the cargo view.
1973 * @param index Row being selected in the grouping drop down.
1975 void SelectGroupBy(int index
)
1977 this->grouping_index
= index
;
1978 _settings_client
.gui
.station_gui_group_order
= index
;
1979 this->GetWidget
<NWidgetCore
>(WID_SV_GROUP_BY
)->widget_data
= _group_names
[index
];
1980 this->groupings
= arrangements
[index
];
1984 virtual void OnDropdownSelect(int widget
, int index
)
1986 if (widget
== WID_SV_SORT_BY
) {
1987 this->SelectSortBy(index
);
1989 this->SelectGroupBy(index
);
1993 virtual void OnQueryTextFinished(char *str
)
1995 if (str
== NULL
) return;
1997 DoCommandP(0, this->window_number
, 0, CMD_RENAME_STATION
, str
);
2000 virtual void OnResize()
2002 this->vscroll
->SetCapacityFromWidget(this, WID_SV_WAITING
, WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
);
2006 * Some data on this window has become invalid. Invalidate the cache for the given cargo if necessary.
2007 * @param data Information about the changed data. If it's a valid cargo ID, invalidate the cargo data.
2008 * @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.
2010 virtual void OnInvalidateData(int data
= 0, bool gui_scope
= true)
2013 if (data
>= 0 && data
< NUM_CARGO
) {
2014 this->cached_destinations_valid
.reset (data
);
2022 const StringID
StationViewWindow::_sort_names
[] = {
2023 STR_STATION_VIEW_WAITING_STATION
,
2024 STR_STATION_VIEW_WAITING_AMOUNT
,
2025 STR_STATION_VIEW_PLANNED_STATION
,
2026 STR_STATION_VIEW_PLANNED_AMOUNT
,
2030 const StringID
StationViewWindow::_group_names
[] = {
2031 STR_STATION_VIEW_GROUP_S_V_D
,
2032 STR_STATION_VIEW_GROUP_S_D_V
,
2033 STR_STATION_VIEW_GROUP_V_S_D
,
2034 STR_STATION_VIEW_GROUP_V_D_S
,
2035 STR_STATION_VIEW_GROUP_D_S_V
,
2036 STR_STATION_VIEW_GROUP_D_V_S
,
2040 const StationViewWindow::Grouping
StationViewWindow::arrangements
[6][NUM_COLUMNS
] = {
2041 { GR_SOURCE
, GR_NEXT
, GR_DESTINATION
}, // S_V_D
2042 { GR_SOURCE
, GR_DESTINATION
, GR_NEXT
}, // S_D_V
2043 { GR_NEXT
, GR_SOURCE
, GR_DESTINATION
}, // V_S_D
2044 { GR_NEXT
, GR_DESTINATION
, GR_SOURCE
}, // V_D_S
2045 { GR_DESTINATION
, GR_SOURCE
, GR_NEXT
}, // D_S_V
2046 { GR_DESTINATION
, GR_NEXT
, GR_SOURCE
}, // D_V_S
2049 assert_compile (lengthof(StationViewWindow::_group_names
) == lengthof(StationViewWindow::arrangements
) + 1);
2051 static WindowDesc::Prefs
_station_view_prefs ("view_station");
2053 static const WindowDesc
_station_view_desc(
2055 WC_STATION_VIEW
, WC_NONE
,
2057 _nested_station_view_widgets
, lengthof(_nested_station_view_widgets
),
2058 &_station_view_prefs
2062 * Opens StationViewWindow for given station
2064 * @param station station which window should be opened
2066 void ShowStationViewWindow(StationID station
)
2068 AllocateWindowDescFront
<StationViewWindow
>(&_station_view_desc
, station
);
2072 * Find a station of the given type in the given area.
2073 * @param ta Base tile area of the to-be-built station
2074 * @param waypoint Look for a waypoint, else a station
2075 * @return Whether a station was found
2077 static bool FindStationInArea (const TileArea
&ta
, bool waypoint
)
2079 TILE_AREA_LOOP(t
, ta
) {
2080 if (IsStationTile(t
)) {
2081 BaseStation
*bst
= BaseStation::GetByTile(t
);
2082 if (bst
->IsWaypoint() == waypoint
) return true;
2090 * Circulate around the to-be-built station to find stations we could join.
2091 * Make sure that only stations are returned where joining wouldn't exceed
2092 * station spread and are our own station.
2093 * @param ta Base tile area of the to-be-built station
2094 * @param distant_join Search for adjacent stations (false) or stations fully
2095 * within station spread
2096 * @param waypoint Look for a waypoint, else a station
2098 static void FindStationsNearby (std::vector
<StationID
> *list
, const TileArea
&ta
, bool distant_join
, bool waypoint
)
2100 /* Look for deleted stations */
2101 typedef std::multimap
<TileIndex
, StationID
> deleted_map
;
2102 deleted_map deleted
;
2103 const BaseStation
*st
;
2104 FOR_ALL_BASE_STATIONS(st
) {
2105 if (st
->IsWaypoint() == waypoint
&& !st
->IsInUse() && st
->owner
== _local_company
2106 /* Include only within station spread */
2107 && DistanceMax (ta
.tile
, st
->xy
) < _settings_game
.station
.station_spread
2108 && DistanceMax (TILE_ADDXY(ta
.tile
, ta
.w
- 1, ta
.h
- 1), st
->xy
) < _settings_game
.station
.station_spread
) {
2109 if (ta
.Contains (st
->xy
)) {
2110 /* Add the station directly if it falls
2111 * into the covered area. */
2112 list
->push_back (st
->index
);
2114 /* Otherwise, store it for later. */
2115 deleted
.insert (std::make_pair (st
->xy
, st
->index
));
2120 /* Only search tiles where we have a chance to stay within the station spread.
2121 * The complete check needs to be done in the callback as we don't know the
2122 * extent of the found station, yet. */
2123 uint min_dim
= min (ta
.w
, ta
.h
);
2124 if (min_dim
>= _settings_game
.station
.station_spread
) return;
2126 /* Keep a set of stations already checked. */
2127 std::set
<StationID
> seen
;
2128 CircularTileIterator
iter (ta
,
2129 distant_join
? _settings_game
.station
.station_spread
- min_dim
: 1);
2130 for (TileIndex tile
= iter
; tile
!= INVALID_TILE
; tile
= ++iter
) {
2131 /* First check if there were deleted stations here */
2132 const std::pair
<deleted_map::iterator
, deleted_map::iterator
> range
= deleted
.equal_range (tile
);
2133 for (deleted_map::const_iterator i
= range
.first
; i
!= range
.second
; i
++) {
2134 list
->push_back (i
->second
);
2136 deleted
.erase (range
.first
, range
.second
);
2138 /* Check if own station and if we stay within station spread */
2139 if (!IsStationTile(tile
)) continue;
2141 StationID sid
= GetStationIndex(tile
);
2142 BaseStation
*st
= BaseStation::Get(sid
);
2144 /* This station is (likely) a waypoint */
2145 if (st
->IsWaypoint() != waypoint
) continue;
2147 if (st
->owner
!= _local_company
) continue;
2149 if (seen
.insert(sid
).second
) {
2151 test
.Add (st
->rect
);
2152 if (test
.w
<= _settings_game
.station
.station_spread
2153 && test
.h
<= _settings_game
.station
.station_spread
) {
2154 list
->push_back (sid
);
2160 static const NWidgetPart _nested_select_station_widgets
[] = {
2161 NWidget(NWID_HORIZONTAL
),
2162 NWidget(WWT_CLOSEBOX
, COLOUR_DARK_GREEN
),
2163 NWidget(WWT_CAPTION
, COLOUR_DARK_GREEN
, WID_JS_CAPTION
), SetDataTip(STR_JOIN_STATION_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
2164 NWidget(WWT_DEFSIZEBOX
, COLOUR_DARK_GREEN
),
2166 NWidget(NWID_HORIZONTAL
),
2167 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_JS_PANEL
), SetResize(1, 0), SetScrollbar(WID_JS_SCROLLBAR
), EndContainer(),
2168 NWidget(NWID_VERTICAL
),
2169 NWidget(NWID_VSCROLLBAR
, COLOUR_DARK_GREEN
, WID_JS_SCROLLBAR
),
2170 NWidget(WWT_RESIZEBOX
, COLOUR_DARK_GREEN
),
2176 * Window for selecting stations/waypoints to (distant) join to.
2178 struct SelectStationWindow
: Window
{
2179 Command select_station_cmd
; ///< Command to build new station
2180 const TileArea area
; ///< Location of new station
2181 const bool waypoint
; ///< Select waypoints, else stations
2182 std::vector
<StationID
> list
; ///< List of nearby stations
2185 SelectStationWindow (const WindowDesc
*desc
, const Command
&cmd
, const TileArea
&ta
, bool waypoint
, const std::vector
<StationID
> &list
) :
2187 select_station_cmd(cmd
),
2193 this->CreateNestedTree();
2194 this->vscroll
= this->GetScrollbar(WID_JS_SCROLLBAR
);
2195 this->GetWidget
<NWidgetCore
>(WID_JS_CAPTION
)->widget_data
= waypoint
? STR_JOIN_WAYPOINT_CAPTION
: STR_JOIN_STATION_CAPTION
;
2196 this->InitNested(0);
2197 this->OnInvalidateData(0);
2200 virtual void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
2202 if (widget
!= WID_JS_PANEL
) return;
2204 /* Determine the widest string */
2205 Dimension d
= GetStringBoundingBox(this->waypoint
? STR_JOIN_WAYPOINT_CREATE_SPLITTED_WAYPOINT
: STR_JOIN_STATION_CREATE_SPLITTED_STATION
);
2206 for (uint i
= 0; i
< this->list
.size(); i
++) {
2207 const BaseStation
*st
= BaseStation::Get(this->list
[i
]);
2208 SetDParam(0, st
->index
);
2209 SetDParam(1, st
->facilities
);
2210 d
= maxdim(d
, GetStringBoundingBox(this->waypoint
? STR_STATION_LIST_WAYPOINT
: STR_STATION_LIST_STATION
));
2213 resize
->height
= d
.height
;
2215 d
.width
+= WD_FRAMERECT_RIGHT
+ WD_FRAMERECT_LEFT
;
2216 d
.height
+= WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
2220 void DrawWidget (BlitArea
*dpi
, const Rect
&r
, int widget
) const OVERRIDE
2222 if (widget
!= WID_JS_PANEL
) return;
2224 uint y
= r
.top
+ WD_FRAMERECT_TOP
;
2225 if (this->vscroll
->GetPosition() == 0) {
2226 DrawString (dpi
, r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
, this->waypoint
? STR_JOIN_WAYPOINT_CREATE_SPLITTED_WAYPOINT
: STR_JOIN_STATION_CREATE_SPLITTED_STATION
);
2227 y
+= this->resize
.step_height
;
2230 for (uint i
= max
<uint
>(1, this->vscroll
->GetPosition()); i
<= this->list
.size(); ++i
, y
+= this->resize
.step_height
) {
2231 /* Don't draw anything if it extends past the end of the window. */
2232 if (i
- this->vscroll
->GetPosition() >= this->vscroll
->GetCapacity()) break;
2234 const BaseStation
*st
= BaseStation::Get(this->list
[i
- 1]);
2235 SetDParam(0, st
->index
);
2236 SetDParam(1, st
->facilities
);
2237 DrawString (dpi
, r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
, this->waypoint
? STR_STATION_LIST_WAYPOINT
: STR_STATION_LIST_STATION
);
2241 virtual void OnClick(Point pt
, int widget
, int click_count
)
2243 if (widget
!= WID_JS_PANEL
) return;
2245 uint st_index
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_JS_PANEL
, WD_FRAMERECT_TOP
);
2246 if (st_index
> this->list
.size()) return;
2248 /* Insert station to be joined into stored command */
2249 SB(this->select_station_cmd
.p2
, 16, 16,
2250 (st_index
> 0) ? this->list
[st_index
- 1] : INVALID_STATION
);
2252 /* Execute stored Command */
2253 this->select_station_cmd
.execp();
2255 /* Close Window; this might cause double frees! */
2256 DeleteWindowById(WC_SELECT_STATION
, 0);
2259 virtual void OnTick()
2261 if (_thd
.dirty
& 2) {
2267 virtual void OnResize()
2269 this->vscroll
->SetCapacityFromWidget(this, WID_JS_PANEL
, WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
);
2273 * Some data on this window has become invalid.
2274 * @param data Information about the changed data.
2275 * @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.
2277 virtual void OnInvalidateData(int data
= 0, bool gui_scope
= true)
2279 if (!gui_scope
) return;
2283 if (!FindStationInArea (this->area
, this->waypoint
)) {
2284 FindStationsNearby (&this->list
, this->area
, _settings_game
.station
.distant_join_stations
, this->waypoint
);
2287 this->vscroll
->SetCount (this->list
.size() + 1);
2292 static WindowDesc::Prefs
_select_station_prefs ("build_station_join");
2294 static const WindowDesc
_select_station_desc(
2296 WC_SELECT_STATION
, WC_NONE
,
2298 _nested_select_station_widgets
, lengthof(_nested_select_station_widgets
),
2299 &_select_station_prefs
2304 * Show the station selection window when needed. If not, build the station.
2305 * @param cmd Command to build the station.
2306 * @param ta Area to build the station in
2307 * @param waypoint Look for waypoints, else stations
2309 void ShowSelectBaseStationIfNeeded (Command
*cmd
, const TileArea
&ta
, bool waypoint
)
2311 /* If a window is already opened and we didn't ctrl-click,
2312 * return true (i.e. just flash the old window) */
2313 Window
*selection_window
= FindWindowById(WC_SELECT_STATION
, 0);
2314 if (selection_window
!= NULL
) {
2315 /* Abort current distant-join and start new one */
2316 selection_window
->Delete();
2319 /* Only show the popup if we press ctrl and we can build there. */
2320 if (_ctrl_pressed
&& cmd
->exec(CommandFlagsToDCFlags(GetCommandFlags(cmd
->cmd
))).Succeeded()
2321 /* Test for adjacent station or station below selection.
2322 * If adjacent-stations is disabled and we are building
2323 * next to a station, do not show the selection window
2324 * but join the other station immediately. */
2325 && !FindStationInArea (ta
, waypoint
)) {
2326 std::vector
<StationID
> list
;
2327 FindStationsNearby (&list
, ta
, false, waypoint
);
2328 if (list
.size() == 0 ? _settings_game
.station
.distant_join_stations
: _settings_game
.station
.adjacent_stations
) {
2329 if (!_settings_client
.gui
.persistent_buildingtools
) ResetPointerMode();
2330 new SelectStationWindow (&_select_station_desc
, *cmd
, ta
, waypoint
, list
);