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"
38 #include "network/network.h"
40 #include "widgets/station_widget.h"
42 #include "table/strings.h"
52 * Calculates and draws the accepted and supplied cargo around the selected tile(s)
53 * @param dpi area to draw on
54 * @param left x position where the string is to be drawn
55 * @param right the right most position to draw on
56 * @param top y position where the string is to be drawn
57 * @param rad radius around selected tile(s) to be searched
58 * @param sct which type of cargo is to be displayed (passengers/non-passengers)
59 * @return Returns the y value below the strings that were drawn
61 int DrawStationCoverageAreaText (BlitArea
*dpi
, int left
, int right
, int top
,
62 int rad
, StationCoverageType sct
)
64 uint32 accept_mask
= 0;
65 uint32 supply_mask
= 0;
67 if (_thd
.drawstyle
== HT_RECT
) {
68 TileIndex tile
= TileVirtXY (_thd
.pos
.x
, _thd
.pos
.y
);
69 if (tile
< MapSize()) {
70 TileArea
ta (tile
, _thd
.size
.x
/ TILE_SIZE
, _thd
.size
.y
/ TILE_SIZE
);
71 CargoArray accept_cargoes
= GetAreaAcceptance (ta
, rad
);
72 CargoArray supply_cargoes
= GetAreaProduction (ta
, rad
);
74 for (CargoID i
= 0; i
< NUM_CARGO
; i
++) {
76 case SCT_PASSENGERS_ONLY
:
77 if (!IsCargoInClass (i
, CC_PASSENGERS
)) continue;
79 case SCT_NON_PASSENGERS_ONLY
:
80 if (IsCargoInClass (i
, CC_PASSENGERS
)) continue;
83 default: NOT_REACHED();
85 if (accept_cargoes
[i
] >= 8) SetBit(accept_mask
, i
);
86 if (supply_cargoes
[i
] >= 1) SetBit(supply_mask
, i
);
91 SetDParam (0, accept_mask
);
92 top
= DrawStringMultiLine (dpi
, left
, right
, top
, INT32_MAX
, STR_STATION_BUILD_ACCEPTS_CARGO
) + WD_PAR_VSEP_NORMAL
;
93 SetDParam (0, supply_mask
);
94 top
= DrawStringMultiLine (dpi
, left
, right
, top
, INT32_MAX
, STR_STATION_BUILD_SUPPLIES_CARGO
) + WD_PAR_VSEP_NORMAL
;
99 * Check whether we need to redraw the station coverage text.
100 * If it is needed actually make the window for redrawing.
101 * @param w the window to check.
103 void CheckRedrawStationCoverage(const Window
*w
)
105 if (_thd
.dirty
& 1) {
112 * Draw small boxes of cargo amount and ratings data at the given
113 * coordinates. If amount exceeds 576 units, it is shown 'full', same
114 * goes for the rating: at above 90% orso (224) it is also 'full'
116 * @param dpi area to draw on
117 * @param left left most coordinate to draw the box at
118 * @param right right most coordinate to draw the box at
119 * @param y coordinate to draw the box at
120 * @param type Cargo type
121 * @param amount Cargo amount
122 * @param rating ratings data for that particular cargo
124 * @note Each cargo-bar is 16 pixels wide and 6 pixels high
125 * @note Each rating 14 pixels wide and 1 pixel high and is 1 pixel below the cargo-bar
127 static void StationsWndShowStationRating (BlitArea
*dpi
,
128 int left
, int right
, int y
, CargoID type
, uint amount
, byte rating
)
130 static const uint units_full
= 576; ///< number of units to show station as 'full'
131 static const uint rating_full
= 224; ///< rating needed so it is shown as 'full'
133 const CargoSpec
*cs
= CargoSpec::Get(type
);
134 if (!cs
->IsValid()) return;
136 int colour
= cs
->rating_colour
;
137 TextColour tc
= GetContrastColour(colour
);
138 uint w
= (minu(amount
, units_full
) + 5) / 36;
140 int height
= GetCharacterHeight(FS_SMALL
);
142 /* Draw total cargo (limited) on station (fits into 16 pixels) */
143 if (w
!= 0) GfxFillRect (dpi
, left
, y
, left
+ w
- 1, y
+ height
, colour
);
145 /* Draw a one pixel-wide bar of additional cargo meter, useful
146 * for stations with only a small amount (<=30) */
148 uint rest
= amount
/ 5;
151 GfxFillRect (dpi
, w
, y
+ height
- rest
, w
, y
+ height
, colour
);
155 DrawString (dpi
, left
+ 1, right
, y
, cs
->abbrev
, tc
);
157 /* Draw green/red ratings bar (fits into 14 pixels) */
159 GfxFillRect (dpi
, left
+ 1, y
, left
+ 14, y
, PC_RED
);
160 rating
= minu(rating
, rating_full
) / 16;
161 if (rating
!= 0) GfxFillRect (dpi
, left
+ 1, y
, left
+ rating
, y
, PC_GREEN
);
164 typedef GUIList
<const Station
*> GUIStationList
;
167 * The list of stations per company.
169 class CompanyStationsWindow
: public Window
172 /* Runtime saved values */
173 static Listing last_sorting
;
174 static byte facilities
; // types of stations of interest
175 static bool include_empty
; // whether we should include stations without waiting cargo
176 static const uint32 cargo_filter_max
;
177 static uint32 cargo_filter
; // bitmap of cargo types to include
178 static const Station
*last_station
;
180 /* Constants for sorting stations */
181 static const StringID sorter_names
[];
182 static GUIStationList::SortFunction
* const sorter_funcs
[];
184 GUIStationList stations
;
188 * (Re)Build station list
190 * @param owner company whose stations are to be in list
192 void BuildStationsList(const Owner owner
)
194 if (!this->stations
.NeedRebuild()) return;
196 DEBUG(misc
, 3, "Building station list for company %d", owner
);
198 this->stations
.Clear();
201 FOR_ALL_STATIONS(st
) {
202 if (st
->owner
== owner
|| (st
->owner
== OWNER_NONE
&& HasStationInUse(st
->index
, true, owner
))) {
203 if (this->facilities
& st
->facilities
) { // only stations with selected facilities
204 int num_waiting_cargo
= 0;
205 for (CargoID j
= 0; j
< NUM_CARGO
; j
++) {
206 if (st
->goods
[j
].HasRating()) {
207 num_waiting_cargo
++; // count number of waiting cargo
208 if (HasBit(this->cargo_filter
, j
)) {
209 *this->stations
.Append() = st
;
214 /* stations without waiting cargo */
215 if (num_waiting_cargo
== 0 && this->include_empty
) {
216 *this->stations
.Append() = st
;
222 this->stations
.Compact();
223 this->stations
.RebuildDone();
225 this->vscroll
->SetCount(this->stations
.Length()); // Update the scrollbar
228 /** Sort stations by their name */
229 static int CDECL
StationNameSorter(const Station
* const *a
, const Station
* const *b
)
231 static char buf_cache
[64];
234 SetDParam(0, (*a
)->index
);
235 GetString (buf
, STR_STATION_NAME
);
237 if (*b
!= last_station
) {
239 SetDParam(0, (*b
)->index
);
240 GetString (buf_cache
, STR_STATION_NAME
);
243 int r
= strnatcmp(buf
, buf_cache
); // Sort by name (natural sorting).
244 if (r
== 0) return (*a
)->index
- (*b
)->index
;
248 /** Sort stations by their type */
249 static int CDECL
StationTypeSorter(const Station
* const *a
, const Station
* const *b
)
251 return (*a
)->facilities
- (*b
)->facilities
;
254 /** Sort stations by their waiting cargo */
255 static int CDECL
StationWaitingTotalSorter(const Station
* const *a
, const Station
* const *b
)
260 FOR_EACH_SET_CARGO_ID(j
, cargo_filter
) {
261 diff
+= (*a
)->goods
[j
].cargo
.TotalCount() - (*b
)->goods
[j
].cargo
.TotalCount();
267 /** Sort stations by their available waiting cargo */
268 static int CDECL
StationWaitingAvailableSorter(const Station
* const *a
, const Station
* const *b
)
273 FOR_EACH_SET_CARGO_ID(j
, cargo_filter
) {
274 diff
+= (*a
)->goods
[j
].cargo
.AvailableCount() - (*b
)->goods
[j
].cargo
.AvailableCount();
280 /** Sort stations by their rating */
281 static int CDECL
StationRatingMaxSorter(const Station
* const *a
, const Station
* const *b
)
287 FOR_EACH_SET_CARGO_ID(j
, cargo_filter
) {
288 if ((*a
)->goods
[j
].HasRating()) maxr1
= max(maxr1
, (*a
)->goods
[j
].rating
);
289 if ((*b
)->goods
[j
].HasRating()) maxr2
= max(maxr2
, (*b
)->goods
[j
].rating
);
292 return maxr1
- maxr2
;
295 /** Sort stations by their rating */
296 static int CDECL
StationRatingMinSorter(const Station
* const *a
, const Station
* const *b
)
301 for (CargoID j
= 0; j
< NUM_CARGO
; j
++) {
302 if (!HasBit(cargo_filter
, j
)) continue;
303 if ((*a
)->goods
[j
].HasRating()) minr1
= min(minr1
, (*a
)->goods
[j
].rating
);
304 if ((*b
)->goods
[j
].HasRating()) minr2
= min(minr2
, (*b
)->goods
[j
].rating
);
307 return -(minr1
- minr2
);
310 /** Sort the stations list */
311 void SortStationsList()
313 if (!this->stations
.Sort()) return;
315 /* Reset name sorter sort cache */
316 this->last_station
= NULL
;
318 /* Set the modified widget dirty */
319 this->SetWidgetDirty(WID_STL_LIST
);
323 CompanyStationsWindow (const WindowDesc
*desc
, WindowNumber window_number
) :
324 Window (desc
), stations(), vscroll (NULL
)
326 this->stations
.SetListing(this->last_sorting
);
327 this->stations
.SetSortFuncs(this->sorter_funcs
);
328 this->stations
.ForceRebuild();
329 this->stations
.NeedResort();
330 this->SortStationsList();
332 this->CreateNestedTree();
333 this->vscroll
= this->GetScrollbar(WID_STL_SCROLLBAR
);
334 this->InitNested(window_number
);
335 this->owner
= (Owner
)this->window_number
;
338 FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs
) {
339 if (!HasBit(this->cargo_filter
, cs
->Index())) continue;
340 this->LowerWidget(WID_STL_CARGOSTART
+ index
);
343 if (this->cargo_filter
== this->cargo_filter_max
) this->cargo_filter
= _cargo_mask
;
345 for (uint i
= 0; i
< 5; i
++) {
346 if (HasBit(this->facilities
, i
)) this->LowerWidget(i
+ WID_STL_TRAIN
);
348 this->SetWidgetLoweredState(WID_STL_NOCARGOWAITING
, this->include_empty
);
350 this->GetWidget
<NWidgetCore
>(WID_STL_SORTDROPBTN
)->widget_data
= this->sorter_names
[this->stations
.SortType()];
353 void OnDelete (void) FINAL_OVERRIDE
355 this->last_sorting
= this->stations
.GetListing();
358 virtual void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
361 case WID_STL_SORTBY
: {
362 Dimension d
= GetStringBoundingBox(this->GetWidget
<NWidgetCore
>(widget
)->widget_data
);
363 d
.width
+= padding
.width
+ Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
364 d
.height
+= padding
.height
;
365 *size
= maxdim(*size
, d
);
369 case WID_STL_SORTDROPBTN
: {
370 Dimension d
= {0, 0};
371 for (int i
= 0; this->sorter_names
[i
] != INVALID_STRING_ID
; i
++) {
372 d
= maxdim(d
, GetStringBoundingBox(this->sorter_names
[i
]));
374 d
.width
+= padding
.width
;
375 d
.height
+= padding
.height
;
376 *size
= maxdim(*size
, d
);
381 resize
->height
= FONT_HEIGHT_NORMAL
;
382 size
->height
= WD_FRAMERECT_TOP
+ 5 * resize
->height
+ WD_FRAMERECT_BOTTOM
;
388 case WID_STL_AIRPLANE
:
390 size
->height
= max
<uint
>(FONT_HEIGHT_SMALL
, 10) + padding
.height
;
393 case WID_STL_CARGOALL
:
394 case WID_STL_FACILALL
:
395 case WID_STL_NOCARGOWAITING
: {
396 Dimension d
= GetStringBoundingBox(widget
== WID_STL_NOCARGOWAITING
? STR_ABBREV_NONE
: STR_ABBREV_ALL
);
397 d
.width
+= padding
.width
+ 2;
398 d
.height
+= padding
.height
;
399 *size
= maxdim(*size
, d
);
404 if (widget
>= WID_STL_CARGOSTART
) {
405 Dimension d
= GetStringBoundingBox(_sorted_cargo_specs
[widget
- WID_STL_CARGOSTART
]->abbrev
);
406 d
.width
+= padding
.width
+ 2;
407 d
.height
+= padding
.height
;
408 *size
= maxdim(*size
, d
);
414 void OnPaint (BlitArea
*dpi
) OVERRIDE
416 this->BuildStationsList((Owner
)this->window_number
);
417 this->SortStationsList();
419 this->DrawWidgets (dpi
);
422 void DrawWidget (BlitArea
*dpi
, const Rect
&r
, int widget
) const OVERRIDE
426 /* draw arrow pointing up/down for ascending/descending sorting */
427 this->DrawSortButtonState (dpi
, WID_STL_SORTBY
, this->stations
.IsDescSortOrder() ? SBS_DOWN
: SBS_UP
);
431 bool rtl
= _current_text_dir
== TD_RTL
;
432 int max
= min(this->vscroll
->GetPosition() + this->vscroll
->GetCapacity(), this->stations
.Length());
433 int y
= r
.top
+ WD_FRAMERECT_TOP
;
434 for (int i
= this->vscroll
->GetPosition(); i
< max
; ++i
) { // do until max number of stations of owner
435 const Station
*st
= this->stations
[i
];
436 assert(st
->xy
!= INVALID_TILE
);
438 /* Do not do the complex check HasStationInUse here, it may be even false
439 * when the order had been removed and the station list hasn't been removed yet */
440 assert(st
->owner
== owner
|| st
->owner
== OWNER_NONE
);
442 SetDParam(0, st
->index
);
443 SetDParam(1, st
->facilities
);
444 int x
= DrawString (dpi
, r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
, STR_STATION_LIST_STATION
);
447 /* show cargo waiting and station ratings */
448 for (uint j
= 0; j
< _sorted_standard_cargo_specs_size
; j
++) {
449 CargoID cid
= _sorted_cargo_specs
[j
]->Index();
450 if (st
->goods
[cid
].cargo
.TotalCount() > 0) {
451 /* For RTL we work in exactly the opposite direction. So
452 * decrement the space needed first, then draw to the left
453 * instead of drawing to the left and then incrementing
457 if (x
< r
.left
+ WD_FRAMERECT_LEFT
) break;
459 StationsWndShowStationRating (dpi
, x
, x
+ 16, y
, cid
, st
->goods
[cid
].cargo
.TotalCount(), st
->goods
[cid
].rating
);
462 if (x
> r
.right
- WD_FRAMERECT_RIGHT
) break;
466 y
+= FONT_HEIGHT_NORMAL
;
469 if (this->vscroll
->GetCount() == 0) { // company has no stations
470 DrawString (dpi
, r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
, STR_STATION_LIST_NONE
);
476 case WID_STL_NOCARGOWAITING
:
477 DrawString (dpi
, r
.left
+ 1, r
.right
+ 1, r
.top
+ 1, STR_ABBREV_NONE
, TC_BLACK
, SA_HOR_CENTER
);
480 case WID_STL_CARGOALL
:
481 DrawString (dpi
, r
.left
+ 1, r
.right
+ 1, r
.top
+ 1, STR_ABBREV_ALL
, TC_BLACK
, SA_HOR_CENTER
);
484 case WID_STL_FACILALL
:
485 DrawString (dpi
, r
.left
+ 1, r
.right
+ 1, r
.top
+ 1, STR_ABBREV_ALL
, TC_BLACK
, SA_HOR_CENTER
);
489 if (widget
>= WID_STL_CARGOSTART
) {
490 const CargoSpec
*cs
= _sorted_cargo_specs
[widget
- WID_STL_CARGOSTART
];
491 GfxFillRect (dpi
, r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.bottom
- 1, cs
->rating_colour
);
492 TextColour tc
= GetContrastColour(cs
->rating_colour
);
493 DrawString (dpi
, r
.left
+ 1, r
.right
+ 1, r
.top
+ 1, cs
->abbrev
, tc
, SA_HOR_CENTER
);
499 virtual void SetStringParameters(int widget
) const
501 if (widget
== WID_STL_CAPTION
) {
502 SetDParam(0, this->window_number
);
503 SetDParam(1, this->vscroll
->GetCount());
507 virtual void OnClick(Point pt
, int widget
, int click_count
)
511 uint id_v
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_STL_LIST
, 0, FONT_HEIGHT_NORMAL
);
512 if (id_v
>= this->stations
.Length()) return; // click out of list bound
514 const Station
*st
= this->stations
[id_v
];
515 /* do not check HasStationInUse - it is slow and may be invalid */
516 assert(st
->owner
== (Owner
)this->window_number
|| st
->owner
== OWNER_NONE
);
519 ShowExtraViewPortWindow(st
->xy
);
521 ScrollMainWindowToTile(st
->xy
);
529 case WID_STL_AIRPLANE
:
532 ToggleBit(this->facilities
, widget
- WID_STL_TRAIN
);
533 this->ToggleWidgetLoweredState(widget
);
536 FOR_EACH_SET_BIT(i
, this->facilities
) {
537 this->RaiseWidget(i
+ WID_STL_TRAIN
);
539 this->facilities
= 1 << (widget
- WID_STL_TRAIN
);
540 this->LowerWidget(widget
);
542 this->stations
.ForceRebuild();
546 case WID_STL_FACILALL
:
547 for (uint i
= WID_STL_TRAIN
; i
<= WID_STL_SHIP
; i
++) {
548 this->LowerWidget(i
);
551 this->facilities
= FACIL_TRAIN
| FACIL_TRUCK_STOP
| FACIL_BUS_STOP
| FACIL_AIRPORT
| FACIL_DOCK
;
552 this->stations
.ForceRebuild();
556 case WID_STL_CARGOALL
: {
557 for (uint i
= 0; i
< _sorted_standard_cargo_specs_size
; i
++) {
558 this->LowerWidget(WID_STL_CARGOSTART
+ i
);
560 this->LowerWidget(WID_STL_NOCARGOWAITING
);
562 this->cargo_filter
= _cargo_mask
;
563 this->include_empty
= true;
564 this->stations
.ForceRebuild();
569 case WID_STL_SORTBY
: // flip sorting method asc/desc
570 this->stations
.ToggleSortOrder();
574 case WID_STL_SORTDROPBTN
: // select sorting criteria dropdown menu
575 ShowDropDownMenu(this, this->sorter_names
, this->stations
.SortType(), WID_STL_SORTDROPBTN
, 0, 0);
578 case WID_STL_NOCARGOWAITING
:
580 this->include_empty
= !this->include_empty
;
581 this->ToggleWidgetLoweredState(WID_STL_NOCARGOWAITING
);
583 for (uint i
= 0; i
< _sorted_standard_cargo_specs_size
; i
++) {
584 this->RaiseWidget(WID_STL_CARGOSTART
+ i
);
587 this->cargo_filter
= 0;
588 this->include_empty
= true;
590 this->LowerWidget(WID_STL_NOCARGOWAITING
);
592 this->stations
.ForceRebuild();
597 if (widget
>= WID_STL_CARGOSTART
) { // change cargo_filter
598 /* Determine the selected cargo type */
599 const CargoSpec
*cs
= _sorted_cargo_specs
[widget
- WID_STL_CARGOSTART
];
602 ToggleBit(this->cargo_filter
, cs
->Index());
603 this->ToggleWidgetLoweredState(widget
);
605 for (uint i
= 0; i
< _sorted_standard_cargo_specs_size
; i
++) {
606 this->RaiseWidget(WID_STL_CARGOSTART
+ i
);
608 this->RaiseWidget(WID_STL_NOCARGOWAITING
);
610 this->cargo_filter
= 0;
611 this->include_empty
= false;
613 SetBit(this->cargo_filter
, cs
->Index());
614 this->LowerWidget(widget
);
616 this->stations
.ForceRebuild();
623 virtual void OnDropdownSelect(int widget
, int index
)
625 if (this->stations
.SortType() != index
) {
626 this->stations
.SetSortType(index
);
628 /* Display the current sort variant */
629 this->GetWidget
<NWidgetCore
>(WID_STL_SORTDROPBTN
)->widget_data
= this->sorter_names
[this->stations
.SortType()];
635 virtual void OnTick()
637 if (_pause_mode
!= PM_UNPAUSED
) return;
638 if (this->stations
.NeedResort()) {
639 DEBUG(misc
, 3, "Periodic rebuild station list company %d", this->window_number
);
644 virtual void OnResize()
646 this->vscroll
->SetCapacityFromWidget(this, WID_STL_LIST
, WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
);
650 * Some data on this window has become invalid.
651 * @param data Information about the changed data.
652 * @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.
654 virtual void OnInvalidateData(int data
= 0, bool gui_scope
= true)
657 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
658 this->stations
.ForceRebuild();
660 this->stations
.ForceResort();
665 Listing
CompanyStationsWindow::last_sorting
= {false, 0};
666 byte
CompanyStationsWindow::facilities
= FACIL_TRAIN
| FACIL_TRUCK_STOP
| FACIL_BUS_STOP
| FACIL_AIRPORT
| FACIL_DOCK
;
667 bool CompanyStationsWindow::include_empty
= true;
668 const uint32
CompanyStationsWindow::cargo_filter_max
= UINT32_MAX
;
669 uint32
CompanyStationsWindow::cargo_filter
= UINT32_MAX
;
670 const Station
*CompanyStationsWindow::last_station
= NULL
;
672 /* Availible station sorting functions */
673 GUIStationList::SortFunction
* const CompanyStationsWindow::sorter_funcs
[] = {
676 &StationWaitingTotalSorter
,
677 &StationWaitingAvailableSorter
,
678 &StationRatingMaxSorter
,
679 &StationRatingMinSorter
682 /* Names of the sorting functions */
683 const StringID
CompanyStationsWindow::sorter_names
[] = {
685 STR_SORT_BY_FACILITY
,
686 STR_SORT_BY_WAITING_TOTAL
,
687 STR_SORT_BY_WAITING_AVAILABLE
,
688 STR_SORT_BY_RATING_MAX
,
689 STR_SORT_BY_RATING_MIN
,
694 * Make a horizontal row of cargo buttons, starting at widget #WID_STL_CARGOSTART.
695 * @param biggest_index Pointer to store biggest used widget number of the buttons.
696 * @return Horizontal row.
698 static NWidgetBase
*CargoWidgets(int *biggest_index
)
700 NWidgetHorizontal
*container
= new NWidgetHorizontal();
702 for (uint i
= 0; i
< _sorted_standard_cargo_specs_size
; i
++) {
703 NWidgetBackground
*panel
= new NWidgetBackground(WWT_PANEL
, COLOUR_GREY
, WID_STL_CARGOSTART
+ i
);
704 panel
->SetMinimalSize(14, 11);
705 panel
->SetResize(0, 0);
706 panel
->SetFill(0, 1);
707 panel
->SetDataTip(0, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE
);
708 container
->Add(panel
);
710 *biggest_index
= WID_STL_CARGOSTART
+ _sorted_standard_cargo_specs_size
;
714 static const NWidgetPart _nested_company_stations_widgets
[] = {
715 NWidget(NWID_HORIZONTAL
),
716 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
717 NWidget(WWT_CAPTION
, COLOUR_GREY
, WID_STL_CAPTION
), SetDataTip(STR_STATION_LIST_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
718 NWidget(WWT_SHADEBOX
, COLOUR_GREY
),
719 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
720 NWidget(WWT_STICKYBOX
, COLOUR_GREY
),
722 NWidget(NWID_HORIZONTAL
),
723 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),
724 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),
725 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),
726 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),
727 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),
728 NWidget(WWT_PUSHBTN
, COLOUR_GREY
, WID_STL_FACILALL
), SetMinimalSize(14, 11), SetDataTip(0x0, STR_STATION_LIST_SELECT_ALL_FACILITIES
), SetFill(0, 1),
729 NWidget(WWT_PANEL
, COLOUR_GREY
), SetMinimalSize(5, 11), SetFill(0, 1), EndContainer(),
730 NWidgetFunction(CargoWidgets
),
731 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_STL_NOCARGOWAITING
), SetMinimalSize(14, 11), SetDataTip(0x0, STR_STATION_LIST_NO_WAITING_CARGO
), SetFill(0, 1), EndContainer(),
732 NWidget(WWT_PUSHBTN
, COLOUR_GREY
, WID_STL_CARGOALL
), SetMinimalSize(14, 11), SetDataTip(0x0, STR_STATION_LIST_SELECT_ALL_TYPES
), SetFill(0, 1),
733 NWidget(WWT_PANEL
, COLOUR_GREY
), SetDataTip(0x0, STR_NULL
), SetResize(1, 0), SetFill(1, 1), EndContainer(),
735 NWidget(NWID_HORIZONTAL
),
736 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_STL_SORTBY
), SetMinimalSize(81, 12), SetDataTip(STR_BUTTON_SORT_BY
, STR_TOOLTIP_SORT_ORDER
),
737 NWidget(WWT_DROPDOWN
, COLOUR_GREY
, WID_STL_SORTDROPBTN
), SetMinimalSize(163, 12), SetDataTip(STR_SORT_BY_NAME
, STR_TOOLTIP_SORT_CRITERIA
), // widget_data gets overwritten.
738 NWidget(WWT_PANEL
, COLOUR_GREY
), SetDataTip(0x0, STR_NULL
), SetResize(1, 0), SetFill(1, 1), EndContainer(),
740 NWidget(NWID_HORIZONTAL
),
741 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(),
742 NWidget(NWID_VERTICAL
),
743 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_STL_SCROLLBAR
),
744 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
),
749 static WindowDesc::Prefs
_company_stations_prefs ("list_stations");
751 static const WindowDesc
_company_stations_desc(
753 WC_STATION_LIST
, WC_NONE
,
755 _nested_company_stations_widgets
, lengthof(_nested_company_stations_widgets
),
756 &_company_stations_prefs
760 * Opens window with list of company's stations
762 * @param company whose stations' list show
764 void ShowCompanyStations(CompanyID company
)
766 if (!Company::IsValidID(company
)) return;
768 AllocateWindowDescFront
<CompanyStationsWindow
>(&_company_stations_desc
, company
);
771 static const NWidgetPart _nested_station_view_widgets
[] = {
772 NWidget(NWID_HORIZONTAL
),
773 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
774 NWidget(WWT_CAPTION
, COLOUR_GREY
, WID_SV_CAPTION
), SetDataTip(STR_STATION_VIEW_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
775 NWidget(WWT_SHADEBOX
, COLOUR_GREY
),
776 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
777 NWidget(WWT_STICKYBOX
, COLOUR_GREY
),
779 NWidget(NWID_HORIZONTAL
),
780 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SV_SORT_ORDER
), SetMinimalSize(81, 12), SetFill(1, 1), SetDataTip(STR_BUTTON_SORT_BY
, STR_TOOLTIP_SORT_ORDER
),
781 NWidget(WWT_DROPDOWN
, COLOUR_GREY
, WID_SV_SORT_BY
), SetMinimalSize(168, 12), SetResize(1, 0), SetFill(0, 1), SetDataTip(0x0, STR_TOOLTIP_SORT_CRITERIA
),
783 NWidget(NWID_HORIZONTAL
),
784 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_SV_GROUP
), SetMinimalSize(81, 12), SetFill(1, 1), SetDataTip(STR_STATION_VIEW_GROUP
, 0x0),
785 NWidget(WWT_DROPDOWN
, COLOUR_GREY
, WID_SV_GROUP_BY
), SetMinimalSize(168, 12), SetResize(1, 0), SetFill(0, 1), SetDataTip(0x0, STR_TOOLTIP_GROUP_ORDER
),
787 NWidget(NWID_HORIZONTAL
),
788 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_SV_WAITING
), SetMinimalSize(237, 44), SetResize(1, 10), SetScrollbar(WID_SV_SCROLLBAR
), EndContainer(),
789 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_SV_SCROLLBAR
),
791 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_SV_ACCEPT_RATING_LIST
), SetMinimalSize(249, 23), SetResize(1, 0), EndContainer(),
792 NWidget(NWID_HORIZONTAL
),
793 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
794 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SV_LOCATION
), SetMinimalSize(45, 12), SetResize(1, 0), SetFill(1, 1),
795 SetDataTip(STR_BUTTON_LOCATION
, STR_STATION_VIEW_CENTER_TOOLTIP
),
796 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SV_ACCEPTS_RATINGS
), SetMinimalSize(46, 12), SetResize(1, 0), SetFill(1, 1),
797 SetDataTip(STR_STATION_VIEW_RATINGS_BUTTON
, STR_STATION_VIEW_RATINGS_TOOLTIP
),
798 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SV_RENAME
), SetMinimalSize(45, 12), SetResize(1, 0), SetFill(1, 1),
799 SetDataTip(STR_BUTTON_RENAME
, STR_STATION_VIEW_RENAME_TOOLTIP
),
801 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_SV_CLOSE_AIRPORT
), SetMinimalSize(45, 12), SetResize(1, 0), SetFill(1, 1),
802 SetDataTip(STR_STATION_VIEW_CLOSE_AIRPORT
, STR_STATION_VIEW_CLOSE_AIRPORT_TOOLTIP
),
803 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SV_TRAINS
), SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_TRAIN
, STR_STATION_VIEW_SCHEDULED_TRAINS_TOOLTIP
),
804 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SV_ROADVEHS
), SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_LORRY
, STR_STATION_VIEW_SCHEDULED_ROAD_VEHICLES_TOOLTIP
),
805 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SV_SHIPS
), SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_SHIP
, STR_STATION_VIEW_SCHEDULED_SHIPS_TOOLTIP
),
806 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SV_PLANES
), SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_PLANE
, STR_STATION_VIEW_SCHEDULED_AIRCRAFT_TOOLTIP
),
807 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
),
812 * Draws icons of waiting cargo in the StationView window
814 * @param i type of cargo
815 * @param waiting number of waiting units
816 * @param dpi area to draw on
817 * @param left left most coordinate to draw on
818 * @param right right most coordinate to draw on
819 * @param y y coordinate
820 * @param width the width of the view
822 static void DrawCargoIcons (CargoID i
, uint waiting
, BlitArea
*dpi
,
823 int left
, int right
, int y
)
825 int width
= ScaleGUITrad(10);
826 uint num
= min((waiting
+ (width
/ 2)) / width
, (right
- left
) / width
); // maximum is width / 10 icons so it won't overflow
827 if (num
== 0) return;
829 SpriteID sprite
= CargoSpec::Get(i
)->GetCargoIcon();
831 int x
= _current_text_dir
== TD_RTL
? left
: right
- num
* width
;
833 DrawSprite (dpi
, sprite
, PAL_NONE
, x
, y
);
844 ST_COUNT
, ///< by amount of cargo
845 ST_STATION_STRING
, ///< by station name
846 ST_STATION_ID
, ///< by station id
850 /** A node in the tree of cached destinations for a cargo type in a station. */
851 struct CargoDestNode
{
852 typedef std::map
<StationID
, CargoDestNode
> map
;
853 typedef map::const_iterator iterator
;
855 CargoDestNode
*const parent
; ///< the parent of this entry
856 uint count
; ///< amount of cargo for this node and children
857 map children
; ///< children of this node
859 CargoDestNode (CargoDestNode
*p
= NULL
)
860 : parent(p
), count(0), children()
866 this->children
.clear();
869 iterator
begin (void) const
871 return this->children
.begin();
874 iterator
end (void) const
876 return this->children
.end();
879 const CargoDestNode
*find (StationID id
) const
881 iterator
iter (this->children
.find (id
));
882 return (iter
!= end()) ? &iter
->second
: NULL
;
885 CargoDestNode
*insert (StationID id
);
887 void update (uint count
);
889 void estimate (CargoID cargo
, StationID source
, StationID next
,
893 /** Find or insert a child node of the current node. */
894 CargoDestNode
*CargoDestNode::insert (StationID id
)
896 const std::pair
<map::iterator
, map::iterator
> range
897 (this->children
.equal_range (id
));
899 if (range
.first
!= range
.second
) return &range
.first
->second
;
901 map::iterator
iter (this->children
.insert (range
.first
,
902 std::make_pair (id
, CargoDestNode (this))));
904 return &iter
->second
;
908 * Update the count for this node and propagate the change uptree.
909 * @param count The amount to be added to this node.
911 void CargoDestNode::update (uint count
)
913 CargoDestNode
*n
= this;
921 * Estimate the amounts of cargo per final destination for a given cargo,
922 * source station and next hop.
923 * @param cargo ID of the cargo to estimate destinations for.
924 * @param source Source station of the given batch of cargo.
925 * @param next Intermediate hop to start the calculation at ("next hop").
926 * @param count Size of the batch of cargo.
928 void CargoDestNode::estimate (CargoID cargo
, StationID source
,
929 StationID next
, uint count
)
931 if (!Station::IsValidID(next
) || !Station::IsValidID(source
)) {
932 this->insert (INVALID_STATION
)->update (count
);
936 std::map
<StationID
, uint
> tmp
;
939 const FlowStatMap
&flowmap
= Station::Get(next
)->goods
[cargo
].flows
;
940 FlowStatMap::const_iterator map_it
= flowmap
.find(source
);
941 if (map_it
!= flowmap
.end()) {
942 const FlowStat::SharesMap
*shares
= map_it
->second
.GetShares();
943 uint32 prev_count
= 0;
944 for (FlowStat::SharesMap::const_iterator i
= shares
->begin(); i
!= shares
->end(); ++i
) {
945 uint add
= i
->first
- prev_count
;
946 tmp
[i
->second
] += add
;
948 prev_count
= i
->first
;
952 if (tmp_count
== 0) {
953 this->insert (INVALID_STATION
)->update (count
);
957 uint sum_estimated
= 0;
958 while (sum_estimated
< count
) {
959 for (std::map
<StationID
, uint
>::iterator i
= tmp
.begin(); i
!= tmp
.end() && sum_estimated
< count
; ++i
) {
960 uint estimate
= DivideApprox (i
->second
* count
, tmp_count
);
961 if (estimate
== 0) estimate
= 1;
963 sum_estimated
+= estimate
;
964 if (sum_estimated
> count
) {
965 estimate
-= sum_estimated
- count
;
966 sum_estimated
= count
;
967 if (estimate
== 0) break;
970 if (i
->first
== next
) {
971 this->insert(next
)->update(estimate
);
973 this->estimate (cargo
, source
, i
->first
, estimate
);
980 * Rebuild the cache for estimated destinations which is used to quickly show the "destination" entries
981 * even if we actually don't know the destination of a certain packet from just looking at it.
982 * @param dest CargoDestNode to save the results in.
983 * @param st Station to recalculate the cache for.
984 * @param i Cargo to recalculate the cache for.
986 static void RecalcDestinations (CargoDestNode
*dest
, const Station
*st
, CargoID i
)
990 const FlowStatMap
&flows
= st
->goods
[i
].flows
;
991 for (FlowStatMap::const_iterator it
= flows
.begin(); it
!= flows
.end(); ++it
) {
992 StationID from
= it
->first
;
993 CargoDestNode
*source_entry
= dest
->insert (from
);
994 const FlowStat::SharesMap
*shares
= it
->second
.GetShares();
995 uint32 prev_count
= 0;
996 for (FlowStat::SharesMap::const_iterator flow_it
= shares
->begin(); flow_it
!= shares
->end(); ++flow_it
) {
997 StationID via
= flow_it
->second
;
998 CargoDestNode
*via_entry
= source_entry
->insert (via
);
999 if (via
== st
->index
) {
1000 via_entry
->insert(via
)->update (flow_it
->first
- prev_count
);
1002 via_entry
->estimate (i
, from
, via
, flow_it
->first
- prev_count
);
1004 prev_count
= flow_it
->first
;
1010 struct expanded_map
: std::map
<StationID
, expanded_map
> { };
1012 class CargoNodeEntry
{
1014 typedef std::map
<StationID
, ttd_unique_ptr
<CargoNodeEntry
> > map
;
1016 CargoNodeEntry
*const parent
; ///< The parent of this entry.
1017 const StationID station
; ///< Station this entry is for.
1018 expanded_map
*const expanded
; ///< Map of expanded nodes, or NULL if this node is not expanded itself.
1019 uint count
; ///< Total amount of cargo under this node.
1020 map children
; ///< Children of this node, per station.
1023 CargoNodeEntry (StationID id
, expanded_map
*expanded
, CargoNodeEntry
*p
= NULL
)
1024 : parent(p
), station(id
), expanded(expanded
), count(0)
1029 typedef map::iterator iterator
;
1030 typedef map::const_iterator const_iterator
;
1032 /** Get the parent of this node. */
1033 const CargoNodeEntry
*get_parent (void) const
1035 return this->parent
;
1038 /** Get the station of this node. */
1039 StationID
get_station (void) const
1041 return this->station
;
1044 /** Get the expanded map of this node. */
1045 expanded_map
*get_expanded (void) const
1047 return this->expanded
;
1050 /** Get the total amount of cargo under this node. */
1051 uint
get_count (void) const
1056 /** Check if this node has no children. */
1057 bool empty (void) const
1059 return this->children
.empty();
1062 /** Check if there is a single child with the given id. */
1063 bool has_single_child (StationID station
) const
1065 return (this->children
.size() == 1) &&
1066 (this->children
.begin()->first
== station
);
1069 CargoNodeEntry
*insert (StationID station
, expanded_map
*expanded
);
1071 void update (uint count
);
1073 typedef std::vector
<const CargoNodeEntry
*> vector
;
1074 vector
sort (CargoSortType type
, SortOrder order
) const;
1077 /** Find or insert a child node of the current node. */
1078 CargoNodeEntry
*CargoNodeEntry::insert (StationID station
, expanded_map
*expanded
)
1080 const std::pair
<map::iterator
, map::iterator
> range
1081 (this->children
.equal_range (station
));
1083 if (range
.first
!= range
.second
) {
1084 CargoNodeEntry
*n
= range
.first
->second
.get();
1085 assert (n
->expanded
== expanded
);
1089 CargoNodeEntry
*n
= new CargoNodeEntry (station
, expanded
, this);
1090 map::iterator
iter (this->children
.insert (range
.first
,
1091 std::make_pair (station
, ttd_unique_ptr
<CargoNodeEntry
>(n
))));
1093 return iter
->second
.get();
1097 * Update the count for this node and propagate the change uptree.
1098 * @param count The amount to be added to this node.
1100 void CargoNodeEntry::update (uint count
)
1102 CargoNodeEntry
*n
= this;
1106 } while (n
!= NULL
);
1109 /** Compare two numbers in the given order. */
1111 static inline bool sort_id (Tid st1
, Tid st2
, SortOrder order
)
1113 return (order
== SO_ASCENDING
) ? st1
< st2
: st2
< st1
;
1116 /** Compare two stations, as given by their id, by their name. */
1117 static bool sort_station (StationID st1
, StationID st2
, SortOrder order
)
1119 static char buf1
[MAX_LENGTH_STATION_NAME_CHARS
];
1120 static char buf2
[MAX_LENGTH_STATION_NAME_CHARS
];
1122 if (!Station::IsValidID(st1
)) {
1123 return Station::IsValidID(st2
) ? (order
== SO_ASCENDING
) : sort_id (st1
, st2
, order
);
1124 } else if (!Station::IsValidID(st2
)) {
1125 return order
== SO_DESCENDING
;
1129 GetString (buf1
, STR_STATION_NAME
);
1131 GetString (buf2
, STR_STATION_NAME
);
1133 int res
= strnatcmp(buf1
, buf2
); // Sort by name (natural sorting).
1134 return (res
== 0) ? sort_id (st1
, st2
, order
) :
1135 (order
== SO_ASCENDING
) ? (res
< 0) : (res
> 0);
1138 struct CargoNodeSorter
{
1139 const CargoSortType type
;
1140 const SortOrder order
;
1142 CargoNodeSorter (CargoSortType t
, SortOrder o
) : type(t
), order(o
)
1146 bool operator() (const CargoNodeEntry
*a
, const CargoNodeEntry
*b
);
1149 bool CargoNodeSorter::operator() (const CargoNodeEntry
*a
,
1150 const CargoNodeEntry
*b
)
1152 switch (this->type
) {
1154 uint ca
= a
->get_count();
1155 uint cb
= b
->get_count();
1157 return (this->order
== SO_ASCENDING
) ?
1158 (ca
< cb
) : (cb
< ca
);
1160 } /* fall through */
1161 case ST_STATION_STRING
:
1162 return sort_station (a
->get_station(),
1163 b
->get_station(), this->order
);
1165 return sort_id (a
->get_station(), b
->get_station(),
1172 /** Sort the children into a vector. */
1173 inline CargoNodeEntry::vector
CargoNodeEntry::sort (CargoSortType type
,
1174 SortOrder order
) const
1177 v
.reserve (this->children
.size());
1179 map::const_iterator iter
= this->children
.begin();
1180 while (iter
!= this->children
.end()) {
1181 v
.push_back ((iter
++)->second
.get());
1184 std::sort (v
.begin(), v
.end(), CargoNodeSorter (type
, order
));
1189 class CargoRootEntry
: public CargoNodeEntry
{
1191 bool transfers
; ///< If there are transfers for this cargo.
1192 uint reserved
; ///< Reserved amount of cargo.
1195 CargoRootEntry (StationID station
, expanded_map
*expanded
)
1196 : CargoNodeEntry (station
, expanded
), reserved (0)
1200 /** Set the transfers state. */
1201 void set_transfers (bool value
) { this->transfers
= value
; }
1203 /** Get the transfers state. */
1204 bool get_transfers (void) const { return this->transfers
; }
1206 /** Update the reserved count. */
1207 void update_reserved (uint count
)
1209 this->reserved
+= count
;
1210 this->update (count
);
1213 /** Get the reserved count. */
1214 uint
get_reserved (void) const
1216 return this->reserved
;
1222 * The StationView window
1224 struct StationViewWindow
: public Window
{
1226 * A row being displayed in the cargo view (as opposed to being "hidden" behind a plus sign).
1229 RowDisplay(expanded_map
*f
, StationID n
) : filter(f
), next_station(n
) {}
1230 RowDisplay(CargoID n
) : filter(NULL
), next_cargo(n
) {}
1233 * Parent of the cargo entry belonging to the row.
1235 expanded_map
*filter
;
1238 * ID of the station belonging to the entry actually displayed if it's to/from/via.
1240 StationID next_station
;
1243 * ID of the cargo belonging to the entry actually displayed if it's cargo.
1249 typedef std::vector
<RowDisplay
> CargoDataVector
;
1251 static const int NUM_COLUMNS
= 3; ///< Number of extra "columns" in the cargo view: from, via, to
1254 * Type of data invalidation.
1257 INV_FLOWS
= 0x100, ///< The planned flows have been recalculated and everything has to be updated.
1258 INV_CARGO
= 0x200 ///< Some cargo has been added or removed.
1262 * Type of grouping used in each of the "columns".
1265 GR_SOURCE
, ///< Group by source of cargo ("from").
1266 GR_NEXT
, ///< Group by next station ("via").
1267 GR_DESTINATION
, ///< Group by estimated final destination ("to").
1271 * Display mode of the cargo view.
1274 MODE_WAITING
, ///< Show cargo waiting at the station.
1275 MODE_PLANNED
///< Show cargo planned to pass through the station.
1278 uint expand_shrink_width
; ///< The width allocated to the expand/shrink 'button'
1279 int rating_lines
; ///< Number of lines in the cargo ratings view.
1280 int accepts_lines
; ///< Number of lines in the accepted cargo view.
1283 /** Height of the #WID_SV_ACCEPT_RATING_LIST widget for different views. */
1284 enum AcceptListHeight
{
1285 ALH_RATING
= 13, ///< Height of the cargo ratings view.
1286 ALH_ACCEPTS
= 3, ///< Height of the accepted cargo view.
1289 static const StringID _sort_names
[]; ///< Names of the sorting options in the dropdown.
1290 static const StringID _group_names
[]; ///< Names of the grouping options in the dropdown.
1292 static const Grouping arrangements
[6][NUM_COLUMNS
]; ///< Possible grouping arrangements.
1295 * Sort types of the different 'columns'.
1296 * In fact only ST_COUNT and ST_STATION_STRING are active and you can only
1297 * sort all the columns in the same way. The other options haven't been
1298 * included in the GUI due to lack of space.
1300 CargoSortType sorting
;
1302 /** Sort order (ascending/descending) for the 'columns'. */
1303 SortOrder sort_order
;
1305 int scroll_to_row
; ///< If set, scroll the main viewport to the station pointed to by this row.
1306 int grouping_index
; ///< Currently selected entry in the grouping drop down.
1307 Mode current_mode
; ///< Currently selected display mode of cargo view.
1308 const Grouping
*groupings
; ///< Grouping modes for the different columns.
1310 CargoDestNode cached_destinations
[NUM_CARGO
]; ///< Cache for the flows passing through this station.
1311 std::bitset
<NUM_CARGO
> cached_destinations_valid
; ///< Bitset of up-to-date cached_destinations entries
1313 std::bitset
<NUM_CARGO
> expanded_cargoes
; ///< Bitset of expanded cargo rows.
1314 expanded_map expanded_rows
[NUM_CARGO
]; ///< Parent entry of currently expanded rows.
1316 CargoDataVector displayed_rows
; ///< Parent entry of currently displayed rows (including collapsed ones).
1318 StationViewWindow (const WindowDesc
*desc
, WindowNumber window_number
) :
1319 Window (desc
), expand_shrink_width (0),
1320 rating_lines (ALH_RATING
), accepts_lines (ALH_ACCEPTS
),
1322 sorting (ST_COUNT
), sort_order (SO_DESCENDING
),
1323 scroll_to_row (INT_MAX
), grouping_index (0),
1324 current_mode (MODE_WAITING
), groupings (NULL
),
1325 cached_destinations(), cached_destinations_valid(),
1326 expanded_cargoes(), expanded_rows(), displayed_rows()
1328 this->CreateNestedTree();
1329 this->vscroll
= this->GetScrollbar(WID_SV_SCROLLBAR
);
1330 /* Nested widget tree creation is done in two steps to ensure that this->GetWidget<NWidgetCore>(WID_SV_ACCEPTS_RATINGS) exists in UpdateWidgetSize(). */
1331 this->InitNested(window_number
);
1333 this->SelectGroupBy(_settings_client
.gui
.station_gui_group_order
);
1334 this->SelectSortBy(_settings_client
.gui
.station_gui_sort_by
);
1335 this->SelectSortOrder((SortOrder
)_settings_client
.gui
.station_gui_sort_order
);
1336 this->owner
= Station::Get(window_number
)->owner
;
1339 void OnDelete (void) FINAL_OVERRIDE
1341 DeleteWindowById(WC_TRAINS_LIST
, VehicleListIdentifier(VL_STATION_LIST
, VEH_TRAIN
, this->owner
, this->window_number
).Pack(), false);
1342 DeleteWindowById(WC_ROADVEH_LIST
, VehicleListIdentifier(VL_STATION_LIST
, VEH_ROAD
, this->owner
, this->window_number
).Pack(), false);
1343 DeleteWindowById(WC_SHIPS_LIST
, VehicleListIdentifier(VL_STATION_LIST
, VEH_SHIP
, this->owner
, this->window_number
).Pack(), false);
1344 DeleteWindowById(WC_AIRCRAFT_LIST
, VehicleListIdentifier(VL_STATION_LIST
, VEH_AIRCRAFT
, this->owner
, this->window_number
).Pack(), false);
1348 * Show a certain cargo entry characterized by source/next/dest station, cargo ID and amount of cargo at the
1349 * right place in the cargo view. I.e. update as many rows as are expanded following that characterization.
1350 * @param data Root entry of the tree.
1351 * @param cargo Cargo ID of the entry to be shown.
1352 * @param source Source station of the entry to be shown.
1353 * @param next Next station the cargo to be shown will visit.
1354 * @param dest Final destination of the cargo to be shown.
1355 * @param count Amount of cargo to be shown.
1357 void ShowCargo (CargoRootEntry
*root
, CargoID cargo
, StationID source
, StationID next
, StationID dest
, uint count
)
1359 if (count
== 0) return;
1361 root
->set_transfers (source
!= this->window_number
);
1362 CargoNodeEntry
*data
= root
;
1364 if (this->expanded_cargoes
.test (cargo
)) {
1365 if (_settings_game
.linkgraph
.GetDistributionType(cargo
) != DT_MANUAL
) {
1366 for (int i
= 0; i
< NUM_COLUMNS
; ++i
) {
1368 switch (groupings
[i
]) {
1369 default: NOT_REACHED();
1376 case GR_DESTINATION
:
1380 expanded_map
*expand
= data
->get_expanded();
1381 expanded_map::iterator iter
= expand
->find (s
);
1382 if (iter
!= expand
->end()) {
1383 data
= data
->insert (s
, &iter
->second
);
1385 data
= data
->insert (s
, NULL
);
1390 if (source
!= this->window_number
) {
1391 data
= data
->insert (source
, NULL
);
1396 data
->update (count
);
1399 virtual void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
1402 case WID_SV_WAITING
:
1403 resize
->height
= FONT_HEIGHT_NORMAL
;
1404 size
->height
= WD_FRAMERECT_TOP
+ 4 * resize
->height
+ WD_FRAMERECT_BOTTOM
;
1405 this->expand_shrink_width
= max(GetStringBoundingBox("-").width
, GetStringBoundingBox("+").width
) + WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
1408 case WID_SV_ACCEPT_RATING_LIST
:
1409 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
;
1412 case WID_SV_CLOSE_AIRPORT
: {
1413 /* Hide 'Close Airport' button if no airport present, and in oilrigs. */
1414 const Station
*st
= Station::Get (this->window_number
);
1415 if (!(st
->facilities
& FACIL_AIRPORT
) || (st
->owner
== OWNER_NONE
)) {
1425 void OnPaint (BlitArea
*dpi
) OVERRIDE
1427 const Station
*st
= Station::Get(this->window_number
);
1429 /* disable some buttons */
1430 this->SetWidgetDisabledState (WID_SV_RENAME
,
1431 (st
->owner
!= _local_company
) && ((st
->owner
!= OWNER_NONE
) || _networking
));
1432 this->SetWidgetDisabledState(WID_SV_TRAINS
, !(st
->facilities
& FACIL_TRAIN
));
1433 this->SetWidgetDisabledState(WID_SV_ROADVEHS
, !(st
->facilities
& FACIL_TRUCK_STOP
) && !(st
->facilities
& FACIL_BUS_STOP
));
1434 this->SetWidgetDisabledState(WID_SV_SHIPS
, !(st
->facilities
& FACIL_DOCK
));
1435 this->SetWidgetDisabledState(WID_SV_PLANES
, !(st
->facilities
& FACIL_AIRPORT
));
1436 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
1437 this->SetWidgetLoweredState(WID_SV_CLOSE_AIRPORT
, (st
->facilities
& FACIL_AIRPORT
) && (st
->airport
.flags
& AIRPORT_CLOSED_block
) != 0);
1439 this->DrawWidgets (dpi
);
1441 if (!this->IsShaded()) {
1442 /* Draw 'accepted cargo' or 'cargo ratings'. */
1443 const NWidgetBase
*wid
= this->GetWidget
<NWidgetBase
>(WID_SV_ACCEPT_RATING_LIST
);
1444 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)};
1445 if (this->GetWidget
<NWidgetCore
>(WID_SV_ACCEPTS_RATINGS
)->widget_data
== STR_STATION_VIEW_RATINGS_BUTTON
) {
1446 int lines
= this->DrawAcceptedCargo (dpi
, r
);
1447 if (lines
> this->accepts_lines
) { // Resize the widget, and perform re-initialization of the window.
1448 this->accepts_lines
= lines
;
1453 int lines
= this->DrawCargoRatings (dpi
, r
);
1454 if (lines
> this->rating_lines
) { // Resize the widget, and perform re-initialization of the window.
1455 this->rating_lines
= lines
;
1461 /* Draw arrow pointing up/down for ascending/descending sorting */
1462 this->DrawSortButtonState (dpi
, WID_SV_SORT_ORDER
, sort_order
== SO_ASCENDING
? SBS_UP
: SBS_DOWN
);
1464 int pos
= this->vscroll
->GetPosition();
1466 int maxrows
= this->vscroll
->GetCapacity();
1468 displayed_rows
.clear();
1470 /* Draw waiting cargo. */
1471 NWidgetBase
*nwi
= this->GetWidget
<NWidgetBase
>(WID_SV_WAITING
);
1472 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)};
1474 for (CargoID i
= 0; i
< NUM_CARGO
; i
++) {
1475 if (!this->cached_destinations_valid
.test (i
)) {
1476 this->cached_destinations_valid
.set (i
);
1477 RecalcDestinations (&this->cached_destinations
[i
], st
, i
);
1480 CargoRootEntry
cargo (this->window_number
, &this->expanded_rows
[i
]);
1481 if (this->current_mode
== MODE_WAITING
) {
1482 this->BuildCargoList (i
, st
->goods
[i
].cargo
, &cargo
);
1484 this->BuildFlowList (i
, st
->goods
[i
].flows
, &cargo
);
1487 if (cargo
.get_count() > 0) {
1488 pos
= this->DrawCargoEntry (&cargo
, i
, dpi
, waiting_rect
, pos
, maxrows
);
1491 this->vscroll
->SetCount (this->vscroll
->GetPosition() - pos
); // update scrollbar
1493 scroll_to_row
= INT_MAX
;
1497 virtual void SetStringParameters(int widget
) const
1499 const Station
*st
= Station::Get(this->window_number
);
1500 SetDParam(0, st
->index
);
1501 SetDParam(1, st
->facilities
);
1505 * Build up the cargo view for PLANNED mode and a specific cargo.
1506 * @param i Cargo to show.
1507 * @param flows The current station's flows for that cargo.
1508 * @param cargo The CargoRootEntry to save the results in.
1510 void BuildFlowList (CargoID i
, const FlowStatMap
&flows
, CargoRootEntry
*cargo
)
1512 const CargoDestNode
*source_dest
= &this->cached_destinations
[i
];
1513 for (FlowStatMap::const_iterator it
= flows
.begin(); it
!= flows
.end(); ++it
) {
1514 StationID from
= it
->first
;
1515 const CargoDestNode
*source_entry
= source_dest
->find (from
);
1516 const FlowStat::SharesMap
*shares
= it
->second
.GetShares();
1517 for (FlowStat::SharesMap::const_iterator flow_it
= shares
->begin(); flow_it
!= shares
->end(); ++flow_it
) {
1518 const CargoDestNode
*via_entry
= source_entry
->find (flow_it
->second
);
1519 for (CargoDestNode::iterator dest_it
= via_entry
->begin(); dest_it
!= via_entry
->end(); ++dest_it
) {
1520 ShowCargo (cargo
, i
, from
, flow_it
->second
, dest_it
->first
, dest_it
->second
.count
);
1527 * Build up the cargo view for WAITING mode and a specific cargo.
1528 * @param i Cargo to show.
1529 * @param packets The current station's cargo list for that cargo.
1530 * @param cargo The CargoRootEntry to save the result in.
1532 void BuildCargoList (CargoID i
, const StationCargoList
&packets
, CargoRootEntry
*cargo
)
1534 const CargoDestNode
*source_dest
= &this->cached_destinations
[i
];
1535 for (StationCargoList::ConstIterator it
= packets
.Packets()->begin(); it
!= packets
.Packets()->end(); it
++) {
1536 const CargoPacket
*cp
= *it
;
1537 StationID next
= it
.GetKey();
1539 const CargoDestNode
*source_entry
= source_dest
->find (cp
->SourceStation());
1540 if (source_entry
== NULL
) {
1541 this->ShowCargo(cargo
, i
, cp
->SourceStation(), next
, INVALID_STATION
, cp
->Count());
1545 const CargoDestNode
*via_entry
= source_entry
->find (next
);
1546 if (via_entry
== NULL
) {
1547 this->ShowCargo(cargo
, i
, cp
->SourceStation(), next
, INVALID_STATION
, cp
->Count());
1551 for (CargoDestNode::iterator dest_it
= via_entry
->begin(); dest_it
!= via_entry
->end(); ++dest_it
) {
1552 uint val
= DivideApprox (cp
->Count() * dest_it
->second
.count
, via_entry
->count
);
1553 this->ShowCargo (cargo
, i
, cp
->SourceStation(), next
, dest_it
->first
, val
);
1557 uint reserved
= packets
.ReservedCount();
1558 if (reserved
!= 0) {
1559 if (this->expanded_cargoes
.test (i
)) {
1560 cargo
->set_transfers (true);
1561 cargo
->update_reserved (reserved
);
1563 cargo
->update (reserved
);
1569 * Select the correct string for an entry referring to the specified station.
1570 * @param station Station the entry is showing cargo for.
1571 * @param here String to be shown if the entry refers to the same station as this station GUI belongs to.
1572 * @param other_station String to be shown if the entry refers to a specific other station.
1573 * @param any String to be shown if the entry refers to "any station".
1574 * @return One of the three given strings, depending on what station the entry refers to.
1576 StringID
GetEntryString(StationID station
, StringID here
, StringID other_station
, StringID any
)
1578 if (station
== this->window_number
) {
1580 } else if (station
== INVALID_STATION
) {
1583 SetDParam(2, station
);
1584 return other_station
;
1589 * Determine if we need to show the special "non-stop" string.
1590 * @param cd Entry we are going to show.
1591 * @param station Station the entry refers to.
1592 * @param column The "column" the entry will be shown in.
1593 * @return either STR_STATION_VIEW_VIA or STR_STATION_VIEW_NONSTOP.
1595 StringID
SearchNonStop (const CargoNodeEntry
*cd
, StationID station
, int column
)
1597 const CargoNodeEntry
*parent
= cd
->get_parent();
1598 for (int i
= column
; i
> 0; --i
) {
1599 if (this->groupings
[i
- 1] == GR_DESTINATION
) {
1600 if (parent
->get_station() == station
) {
1601 return STR_STATION_VIEW_NONSTOP
;
1603 return STR_STATION_VIEW_VIA
;
1606 parent
= parent
->get_parent();
1609 if (this->groupings
[column
+ 1] == GR_DESTINATION
) {
1610 if (cd
->has_single_child (station
)) {
1611 return STR_STATION_VIEW_NONSTOP
;
1613 return STR_STATION_VIEW_VIA
;
1617 return STR_STATION_VIEW_VIA
;
1621 * Draw the cargo string for an entry in the station GUI.
1622 * @param dpi Area to draw on.
1623 * @param r Screen rectangle to draw into.
1624 * @param y Vertical position to draw at.
1625 * @param indent Extra indentation for the string.
1626 * @param sym Symbol to draw at the end of the line, if not null.
1627 * @param str String to draw.
1629 void DrawCargoString (BlitArea
*dpi
, const Rect
&r
, int y
, int indent
,
1630 const char *sym
, StringID str
)
1632 bool rtl
= _current_text_dir
== TD_RTL
;
1634 int text_left
= rtl
? r
.left
+ this->expand_shrink_width
: r
.left
+ WD_FRAMERECT_LEFT
+ indent
* this->expand_shrink_width
;
1635 int text_right
= rtl
? r
.right
- WD_FRAMERECT_LEFT
- indent
* this->expand_shrink_width
: r
.right
- this->expand_shrink_width
;
1636 DrawString (dpi
, text_left
, text_right
, y
, str
);
1639 int sym_left
= rtl
? r
.left
+ WD_FRAMERECT_LEFT
: r
.right
- this->expand_shrink_width
+ WD_FRAMERECT_LEFT
;
1640 int sym_right
= rtl
? r
.left
+ this->expand_shrink_width
- WD_FRAMERECT_RIGHT
: r
.right
- WD_FRAMERECT_RIGHT
;
1641 DrawString (dpi
, sym_left
, sym_right
, y
, sym
, TC_YELLOW
);
1646 * Draw the given cargo entries in the station GUI.
1647 * @param entry Root entry for all cargo to be drawn.
1648 * @param dpi Area to draw on.
1649 * @param r Screen rectangle to draw into.
1650 * @param pos Current row to be drawn to (counted down from 0 to -maxrows, same as vscroll->GetPosition()).
1651 * @param maxrows Maximum row to be drawn.
1652 * @param column Current "column" being drawn.
1653 * @param cargo Current cargo being drawn.
1654 * @return row (in "pos" counting) after the one we have last drawn to.
1656 int DrawEntries (const CargoNodeEntry
*entry
, BlitArea
*dpi
, const Rect
&r
,
1657 int pos
, int maxrows
, int column
, CargoID cargo
)
1659 assert (entry
->empty() || (entry
->get_expanded() != NULL
));
1661 typedef CargoNodeEntry::vector vector
;
1662 vector v
= entry
->sort (this->sorting
, this->sort_order
);
1664 for (vector::const_iterator i
= v
.begin(); i
!= v
.end(); ++i
) {
1665 const CargoNodeEntry
*cd
= *i
;
1667 bool auto_distributed
= _settings_game
.linkgraph
.GetDistributionType(cargo
) != DT_MANUAL
;
1668 assert (auto_distributed
|| (column
== 0));
1670 if (pos
> -maxrows
&& pos
<= 0) {
1671 StringID str
= STR_EMPTY
;
1672 int y
= r
.top
+ WD_FRAMERECT_TOP
- pos
* FONT_HEIGHT_NORMAL
;
1673 SetDParam(0, cargo
);
1674 SetDParam(1, cd
->get_count());
1676 Grouping grouping
= auto_distributed
? this->groupings
[column
] : GR_SOURCE
;
1677 StationID station
= cd
->get_station();
1681 str
= this->GetEntryString(station
, STR_STATION_VIEW_FROM_HERE
, STR_STATION_VIEW_FROM
, STR_STATION_VIEW_FROM_ANY
);
1684 str
= this->GetEntryString(station
, STR_STATION_VIEW_VIA_HERE
, STR_STATION_VIEW_VIA
, STR_STATION_VIEW_VIA_ANY
);
1685 if (str
== STR_STATION_VIEW_VIA
) str
= this->SearchNonStop(cd
, station
, column
);
1687 case GR_DESTINATION
:
1688 str
= this->GetEntryString(station
, STR_STATION_VIEW_TO_HERE
, STR_STATION_VIEW_TO
, STR_STATION_VIEW_TO_ANY
);
1693 if (pos
== -this->scroll_to_row
&& Station::IsValidID(station
)) {
1694 ScrollMainWindowToTile(Station::Get(station
)->xy
);
1697 const char *sym
= NULL
;
1698 if (column
< NUM_COLUMNS
- 1) {
1701 } else if (auto_distributed
) {
1706 this->DrawCargoString (dpi
, r
, y
, column
+ 1, sym
, str
);
1708 expanded_map
*expand
= entry
->get_expanded();
1709 assert (expand
!= NULL
);
1710 this->displayed_rows
.push_back (RowDisplay (expand
, station
));
1713 if (auto_distributed
) {
1714 pos
= this->DrawEntries (cd
, dpi
, r
, pos
, maxrows
, column
+ 1, cargo
);
1721 * Draw the given cargo entry in the station GUI.
1722 * @param cd Cargo entry to be drawn.
1723 * @param cargo Cargo type for this entry.
1724 * @param dpi Area to draw on.
1725 * @param r Screen rectangle to draw into.
1726 * @param pos Current row to be drawn to (counted down from 0 to -maxrows, same as vscroll->GetPosition()).
1727 * @param maxrows Maximum row to be drawn.
1728 * @return row (in "pos" counting) after the one we have last drawn to.
1730 int DrawCargoEntry (const CargoRootEntry
*cd
, CargoID cargo
,
1731 BlitArea
*dpi
, const Rect
&r
, int pos
, int maxrows
)
1733 bool auto_distributed
= _settings_game
.linkgraph
.GetDistributionType(cargo
) != DT_MANUAL
;
1735 if (pos
> -maxrows
&& pos
<= 0) {
1736 int y
= r
.top
+ WD_FRAMERECT_TOP
- pos
* FONT_HEIGHT_NORMAL
;
1737 SetDParam(0, cargo
);
1738 SetDParam(1, cd
->get_count());
1739 StringID str
= STR_STATION_VIEW_WAITING_CARGO
;
1740 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
);
1742 const char *sym
= NULL
;
1743 if (!cd
->empty() || (cd
->get_reserved() > 0)) {
1745 } else if (auto_distributed
) {
1748 /* Only draw '+' if there is something to be shown. */
1749 const StationCargoList
&list
= Station::Get(this->window_number
)->goods
[cargo
].cargo
;
1750 if (list
.ReservedCount() > 0 || cd
->get_transfers()) {
1755 this->DrawCargoString (dpi
, r
, y
, 0, sym
, str
);
1757 this->displayed_rows
.push_back (RowDisplay (cargo
));
1760 pos
= this->DrawEntries (cd
, dpi
, r
, pos
- 1, maxrows
, 0, cargo
);
1762 if (cd
->get_reserved() != 0) {
1763 if (pos
> -maxrows
&& pos
<= 0) {
1764 int y
= r
.top
+ WD_FRAMERECT_TOP
- pos
* FONT_HEIGHT_NORMAL
;
1765 SetDParam (0, cargo
);
1766 SetDParam (1, cd
->get_reserved());
1767 this->DrawCargoString (dpi
, r
, y
, 1, NULL
, STR_STATION_VIEW_RESERVED
);
1768 this->displayed_rows
.push_back (RowDisplay (INVALID_CARGO
));
1777 * Draw accepted cargo in the #WID_SV_ACCEPT_RATING_LIST widget.
1778 * @param dpi Area to draw on.
1779 * @param r Rectangle of the widget.
1780 * @return Number of lines needed for drawing the accepted cargo.
1782 int DrawAcceptedCargo (BlitArea
*dpi
, const Rect
&r
) const
1784 const Station
*st
= Station::Get(this->window_number
);
1786 uint32 cargo_mask
= 0;
1787 for (CargoID i
= 0; i
< NUM_CARGO
; i
++) {
1788 if (HasBit(st
->goods
[i
].status
, GoodsEntry::GES_ACCEPTANCE
)) SetBit(cargo_mask
, i
);
1790 SetDParam(0, cargo_mask
);
1791 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
);
1792 return CeilDiv(bottom
- r
.top
- WD_FRAMERECT_TOP
, FONT_HEIGHT_NORMAL
);
1796 * Draw cargo ratings in the #WID_SV_ACCEPT_RATING_LIST widget.
1797 * @param dpi Area to draw on.
1798 * @param r Rectangle of the widget.
1799 * @return Number of lines needed for drawing the cargo ratings.
1801 int DrawCargoRatings (BlitArea
*dpi
, const Rect
&r
) const
1803 const Station
*st
= Station::Get(this->window_number
);
1804 int y
= r
.top
+ WD_FRAMERECT_TOP
;
1806 if (st
->town
->exclusive_counter
> 0) {
1807 SetDParam(0, st
->town
->exclusivity
);
1808 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
);
1809 y
+= WD_PAR_VSEP_WIDE
;
1812 DrawString (dpi
, r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
, STR_STATION_VIEW_SUPPLY_RATINGS_TITLE
);
1813 y
+= FONT_HEIGHT_NORMAL
;
1815 const CargoSpec
*cs
;
1816 FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs
) {
1817 const GoodsEntry
*ge
= &st
->goods
[cs
->Index()];
1818 if (!ge
->HasRating()) continue;
1820 const LinkGraph
*lg
= LinkGraph::GetIfValid(ge
->link_graph
);
1821 SetDParam(0, cs
->name
);
1822 SetDParam(1, lg
!= NULL
? lg
->Monthly((*lg
)[ge
->node
]->Supply()) : 0);
1823 SetDParam(2, STR_CARGO_RATING_APPALLING
+ (ge
->rating
>> 5));
1824 SetDParam(3, ToPercent8(ge
->rating
));
1825 DrawString (dpi
, r
.left
+ WD_FRAMERECT_LEFT
+ 6, r
.right
- WD_FRAMERECT_RIGHT
- 6, y
, STR_STATION_VIEW_CARGO_SUPPLY_RATING
);
1826 y
+= FONT_HEIGHT_NORMAL
;
1828 return CeilDiv(y
- r
.top
- WD_FRAMERECT_TOP
, FONT_HEIGHT_NORMAL
);
1832 * Handle a click on a specific row in the cargo view.
1833 * @param row Row being clicked.
1835 void HandleCargoWaitingClick(int row
)
1837 if (row
< 0 || (uint
)row
>= this->displayed_rows
.size()) return;
1838 if (_ctrl_pressed
) {
1839 this->scroll_to_row
= row
;
1841 RowDisplay
&display
= this->displayed_rows
[row
];
1842 expanded_map
*filter
= display
.filter
;
1843 if (filter
!= NULL
) {
1844 StationID next
= display
.next_station
;
1845 expanded_map::iterator iter
= filter
->find (next
);
1846 if (iter
!= filter
->end()) {
1847 filter
->erase (iter
);
1851 } else if (display
.next_cargo
!= INVALID_CARGO
) {
1852 this->expanded_cargoes
.flip (display
.next_cargo
);
1855 this->SetWidgetDirty(WID_SV_WAITING
);
1858 virtual void OnClick(Point pt
, int widget
, int click_count
)
1861 case WID_SV_WAITING
:
1862 this->HandleCargoWaitingClick(this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_SV_WAITING
, WD_FRAMERECT_TOP
, FONT_HEIGHT_NORMAL
) - this->vscroll
->GetPosition());
1865 case WID_SV_LOCATION
:
1866 if (_ctrl_pressed
) {
1867 ShowExtraViewPortWindow(Station::Get(this->window_number
)->xy
);
1869 ScrollMainWindowToTile(Station::Get(this->window_number
)->xy
);
1873 case WID_SV_ACCEPTS_RATINGS
: {
1874 /* Swap between 'accepts' and 'ratings' view. */
1876 NWidgetCore
*nwi
= this->GetWidget
<NWidgetCore
>(WID_SV_ACCEPTS_RATINGS
);
1877 if (this->GetWidget
<NWidgetCore
>(WID_SV_ACCEPTS_RATINGS
)->widget_data
== STR_STATION_VIEW_RATINGS_BUTTON
) {
1878 nwi
->SetDataTip(STR_STATION_VIEW_ACCEPTS_BUTTON
, STR_STATION_VIEW_ACCEPTS_TOOLTIP
); // Switch to accepts view.
1879 height_change
= this->rating_lines
- this->accepts_lines
;
1881 nwi
->SetDataTip(STR_STATION_VIEW_RATINGS_BUTTON
, STR_STATION_VIEW_RATINGS_TOOLTIP
); // Switch to ratings view.
1882 height_change
= this->accepts_lines
- this->rating_lines
;
1884 this->ReInit(0, height_change
* FONT_HEIGHT_NORMAL
);
1889 SetDParam(0, this->window_number
);
1890 ShowQueryString(STR_STATION_NAME
, STR_STATION_VIEW_RENAME_STATION_CAPTION
, MAX_LENGTH_STATION_NAME_CHARS
,
1891 this, CS_ALPHANUMERAL
, QSF_ENABLE_DEFAULT
| QSF_LEN_IN_CHARS
);
1894 case WID_SV_CLOSE_AIRPORT
:
1895 DoCommandP(0, this->window_number
, 0, CMD_OPEN_CLOSE_AIRPORT
);
1898 case WID_SV_TRAINS
: // Show list of scheduled trains to this station
1899 case WID_SV_ROADVEHS
: // Show list of scheduled road-vehicles to this station
1900 case WID_SV_SHIPS
: // Show list of scheduled ships to this station
1901 case WID_SV_PLANES
: { // Show list of scheduled aircraft to this station
1902 Owner owner
= Station::Get(this->window_number
)->owner
;
1903 ShowVehicleListWindow(owner
, (VehicleType
)(widget
- WID_SV_TRAINS
), (StationID
)this->window_number
);
1907 case WID_SV_SORT_BY
: {
1908 /* The initial selection is composed of current mode and
1909 * sorting criteria for columns 1, 2, and 3. Column 0 is always
1910 * sorted by cargo ID. The others can theoretically be sorted
1911 * by different things but there is no UI for that. */
1912 ShowDropDownMenu(this, _sort_names
,
1913 this->current_mode
* 2 + (this->sorting
== ST_COUNT
? 1 : 0),
1914 WID_SV_SORT_BY
, 0, 0);
1918 case WID_SV_GROUP_BY
: {
1919 ShowDropDownMenu(this, _group_names
, this->grouping_index
, WID_SV_GROUP_BY
, 0, 0);
1923 case WID_SV_SORT_ORDER
: { // flip sorting method asc/desc
1924 this->SelectSortOrder (this->sort_order
== SO_ASCENDING
? SO_DESCENDING
: SO_ASCENDING
);
1926 this->LowerWidget(WID_SV_SORT_ORDER
);
1933 * Select a new sort order for the cargo view.
1934 * @param order New sort order.
1936 void SelectSortOrder(SortOrder order
)
1938 this->sort_order
= order
;
1939 _settings_client
.gui
.station_gui_sort_order
= order
;
1944 * Select a new sort criterium for the cargo view.
1945 * @param index Row being selected in the sort criteria drop down.
1947 void SelectSortBy(int index
)
1949 _settings_client
.gui
.station_gui_sort_by
= index
;
1950 switch (_sort_names
[index
]) {
1951 case STR_STATION_VIEW_WAITING_STATION
:
1952 this->current_mode
= MODE_WAITING
;
1953 this->sorting
= ST_STATION_STRING
;
1955 case STR_STATION_VIEW_WAITING_AMOUNT
:
1956 this->current_mode
= MODE_WAITING
;
1957 this->sorting
= ST_COUNT
;
1959 case STR_STATION_VIEW_PLANNED_STATION
:
1960 this->current_mode
= MODE_PLANNED
;
1961 this->sorting
= ST_STATION_STRING
;
1963 case STR_STATION_VIEW_PLANNED_AMOUNT
:
1964 this->current_mode
= MODE_PLANNED
;
1965 this->sorting
= ST_COUNT
;
1970 /* Display the current sort variant */
1971 this->GetWidget
<NWidgetCore
>(WID_SV_SORT_BY
)->widget_data
= _sort_names
[index
];
1976 * Select a new grouping mode for the cargo view.
1977 * @param index Row being selected in the grouping drop down.
1979 void SelectGroupBy(int index
)
1981 this->grouping_index
= index
;
1982 _settings_client
.gui
.station_gui_group_order
= index
;
1983 this->GetWidget
<NWidgetCore
>(WID_SV_GROUP_BY
)->widget_data
= _group_names
[index
];
1984 this->groupings
= arrangements
[index
];
1988 virtual void OnDropdownSelect(int widget
, int index
)
1990 if (widget
== WID_SV_SORT_BY
) {
1991 this->SelectSortBy(index
);
1993 this->SelectGroupBy(index
);
1997 virtual void OnQueryTextFinished(char *str
)
1999 if (str
== NULL
) return;
2001 DoCommandP(0, this->window_number
, 0, CMD_RENAME_STATION
, str
);
2004 virtual void OnResize()
2006 this->vscroll
->SetCapacityFromWidget(this, WID_SV_WAITING
, WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
);
2010 * Some data on this window has become invalid. Invalidate the cache for the given cargo if necessary.
2011 * @param data Information about the changed data. If it's a valid cargo ID, invalidate the cargo data.
2012 * @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.
2014 virtual void OnInvalidateData(int data
= 0, bool gui_scope
= true)
2017 if (data
>= 0 && data
< NUM_CARGO
) {
2018 this->cached_destinations_valid
.reset (data
);
2026 const StringID
StationViewWindow::_sort_names
[] = {
2027 STR_STATION_VIEW_WAITING_STATION
,
2028 STR_STATION_VIEW_WAITING_AMOUNT
,
2029 STR_STATION_VIEW_PLANNED_STATION
,
2030 STR_STATION_VIEW_PLANNED_AMOUNT
,
2034 const StringID
StationViewWindow::_group_names
[] = {
2035 STR_STATION_VIEW_GROUP_S_V_D
,
2036 STR_STATION_VIEW_GROUP_S_D_V
,
2037 STR_STATION_VIEW_GROUP_V_S_D
,
2038 STR_STATION_VIEW_GROUP_V_D_S
,
2039 STR_STATION_VIEW_GROUP_D_S_V
,
2040 STR_STATION_VIEW_GROUP_D_V_S
,
2044 const StationViewWindow::Grouping
StationViewWindow::arrangements
[6][NUM_COLUMNS
] = {
2045 { GR_SOURCE
, GR_NEXT
, GR_DESTINATION
}, // S_V_D
2046 { GR_SOURCE
, GR_DESTINATION
, GR_NEXT
}, // S_D_V
2047 { GR_NEXT
, GR_SOURCE
, GR_DESTINATION
}, // V_S_D
2048 { GR_NEXT
, GR_DESTINATION
, GR_SOURCE
}, // V_D_S
2049 { GR_DESTINATION
, GR_SOURCE
, GR_NEXT
}, // D_S_V
2050 { GR_DESTINATION
, GR_NEXT
, GR_SOURCE
}, // D_V_S
2053 assert_compile (lengthof(StationViewWindow::_group_names
) == lengthof(StationViewWindow::arrangements
) + 1);
2055 static WindowDesc::Prefs
_station_view_prefs ("view_station");
2057 static const WindowDesc
_station_view_desc(
2059 WC_STATION_VIEW
, WC_NONE
,
2061 _nested_station_view_widgets
, lengthof(_nested_station_view_widgets
),
2062 &_station_view_prefs
2066 * Opens StationViewWindow for given station
2068 * @param station station which window should be opened
2070 void ShowStationViewWindow(StationID station
)
2072 AllocateWindowDescFront
<StationViewWindow
>(&_station_view_desc
, station
);
2076 * Find a station of the given type in the given area.
2077 * @param ta Base tile area of the to-be-built station
2078 * @param waypoint Look for a waypoint, else a station
2079 * @return Whether a station was found
2081 static bool FindStationInArea (const TileArea
&ta
, bool waypoint
)
2083 TILE_AREA_LOOP(t
, ta
) {
2084 if (IsStationTile(t
)) {
2085 BaseStation
*bst
= BaseStation::GetByTile(t
);
2086 if (bst
->IsWaypoint() == waypoint
) return true;
2094 * Circulate around the to-be-built station to find stations we could join.
2095 * Make sure that only stations are returned where joining wouldn't exceed
2096 * station spread and are our own station.
2097 * @param ta Base tile area of the to-be-built station
2098 * @param distant_join Search for adjacent stations (false) or stations fully
2099 * within station spread
2100 * @param waypoint Look for a waypoint, else a station
2102 static void FindStationsNearby (std::vector
<StationID
> *list
, const TileArea
&ta
, bool distant_join
, bool waypoint
)
2104 /* Look for deleted stations */
2105 typedef std::multimap
<TileIndex
, StationID
> deleted_map
;
2106 deleted_map deleted
;
2107 const BaseStation
*st
;
2108 FOR_ALL_BASE_STATIONS(st
) {
2109 if (st
->IsWaypoint() == waypoint
&& !st
->IsInUse() && st
->owner
== _local_company
2110 /* Include only within station spread */
2111 && DistanceMax (ta
.tile
, st
->xy
) < _settings_game
.station
.station_spread
2112 && DistanceMax (TILE_ADDXY(ta
.tile
, ta
.w
- 1, ta
.h
- 1), st
->xy
) < _settings_game
.station
.station_spread
) {
2113 if (ta
.Contains (st
->xy
)) {
2114 /* Add the station directly if it falls
2115 * into the covered area. */
2116 list
->push_back (st
->index
);
2118 /* Otherwise, store it for later. */
2119 deleted
.insert (std::make_pair (st
->xy
, st
->index
));
2124 /* Only search tiles where we have a chance to stay within the station spread.
2125 * The complete check needs to be done in the callback as we don't know the
2126 * extent of the found station, yet. */
2127 uint min_dim
= min (ta
.w
, ta
.h
);
2128 if (min_dim
>= _settings_game
.station
.station_spread
) return;
2130 /* Keep a set of stations already checked. */
2131 std::set
<StationID
> seen
;
2132 CircularTileIterator
iter (ta
,
2133 distant_join
? _settings_game
.station
.station_spread
- min_dim
: 1);
2134 for (TileIndex tile
= iter
; tile
!= INVALID_TILE
; tile
= ++iter
) {
2135 /* First check if there were deleted stations here */
2136 const std::pair
<deleted_map::iterator
, deleted_map::iterator
> range
= deleted
.equal_range (tile
);
2137 for (deleted_map::const_iterator i
= range
.first
; i
!= range
.second
; i
++) {
2138 list
->push_back (i
->second
);
2140 deleted
.erase (range
.first
, range
.second
);
2142 /* Check if own station and if we stay within station spread */
2143 if (!IsStationTile(tile
)) continue;
2145 StationID sid
= GetStationIndex(tile
);
2146 BaseStation
*st
= BaseStation::Get(sid
);
2148 /* This station is (likely) a waypoint */
2149 if (st
->IsWaypoint() != waypoint
) continue;
2151 if (st
->owner
!= _local_company
) continue;
2153 if (seen
.insert(sid
).second
) {
2155 test
.Add (st
->rect
);
2156 if (test
.w
<= _settings_game
.station
.station_spread
2157 && test
.h
<= _settings_game
.station
.station_spread
) {
2158 list
->push_back (sid
);
2164 static const NWidgetPart _nested_select_station_widgets
[] = {
2165 NWidget(NWID_HORIZONTAL
),
2166 NWidget(WWT_CLOSEBOX
, COLOUR_DARK_GREEN
),
2167 NWidget(WWT_CAPTION
, COLOUR_DARK_GREEN
, WID_JS_CAPTION
), SetDataTip(STR_JOIN_STATION_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
2168 NWidget(WWT_DEFSIZEBOX
, COLOUR_DARK_GREEN
),
2170 NWidget(NWID_HORIZONTAL
),
2171 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_JS_PANEL
), SetResize(1, 0), SetScrollbar(WID_JS_SCROLLBAR
), EndContainer(),
2172 NWidget(NWID_VERTICAL
),
2173 NWidget(NWID_VSCROLLBAR
, COLOUR_DARK_GREEN
, WID_JS_SCROLLBAR
),
2174 NWidget(WWT_RESIZEBOX
, COLOUR_DARK_GREEN
),
2180 * Window for selecting stations/waypoints to (distant) join to.
2182 struct SelectStationWindow
: Window
{
2183 Command select_station_cmd
; ///< Command to build new station
2184 const TileArea area
; ///< Location of new station
2185 const bool waypoint
; ///< Select waypoints, else stations
2186 std::vector
<StationID
> list
; ///< List of nearby stations
2189 SelectStationWindow (const WindowDesc
*desc
, const Command
&cmd
, const TileArea
&ta
, bool waypoint
, const std::vector
<StationID
> &list
) :
2191 select_station_cmd(cmd
),
2197 this->CreateNestedTree();
2198 this->vscroll
= this->GetScrollbar(WID_JS_SCROLLBAR
);
2199 this->GetWidget
<NWidgetCore
>(WID_JS_CAPTION
)->widget_data
= waypoint
? STR_JOIN_WAYPOINT_CAPTION
: STR_JOIN_STATION_CAPTION
;
2200 this->InitNested(0);
2201 this->OnInvalidateData(0);
2204 virtual void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
2206 if (widget
!= WID_JS_PANEL
) return;
2208 /* Determine the widest string */
2209 Dimension d
= GetStringBoundingBox(this->waypoint
? STR_JOIN_WAYPOINT_CREATE_SPLITTED_WAYPOINT
: STR_JOIN_STATION_CREATE_SPLITTED_STATION
);
2210 for (uint i
= 0; i
< this->list
.size(); i
++) {
2211 const BaseStation
*st
= BaseStation::Get(this->list
[i
]);
2212 SetDParam(0, st
->index
);
2213 SetDParam(1, st
->facilities
);
2214 d
= maxdim(d
, GetStringBoundingBox(this->waypoint
? STR_STATION_LIST_WAYPOINT
: STR_STATION_LIST_STATION
));
2217 resize
->height
= d
.height
;
2219 d
.width
+= WD_FRAMERECT_RIGHT
+ WD_FRAMERECT_LEFT
;
2220 d
.height
+= WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
2224 void DrawWidget (BlitArea
*dpi
, const Rect
&r
, int widget
) const OVERRIDE
2226 if (widget
!= WID_JS_PANEL
) return;
2228 uint y
= r
.top
+ WD_FRAMERECT_TOP
;
2229 if (this->vscroll
->GetPosition() == 0) {
2230 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
);
2231 y
+= this->resize
.step_height
;
2234 for (uint i
= max
<uint
>(1, this->vscroll
->GetPosition()); i
<= this->list
.size(); ++i
, y
+= this->resize
.step_height
) {
2235 /* Don't draw anything if it extends past the end of the window. */
2236 if (i
- this->vscroll
->GetPosition() >= this->vscroll
->GetCapacity()) break;
2238 const BaseStation
*st
= BaseStation::Get(this->list
[i
- 1]);
2239 SetDParam(0, st
->index
);
2240 SetDParam(1, st
->facilities
);
2241 DrawString (dpi
, r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
, this->waypoint
? STR_STATION_LIST_WAYPOINT
: STR_STATION_LIST_STATION
);
2245 virtual void OnClick(Point pt
, int widget
, int click_count
)
2247 if (widget
!= WID_JS_PANEL
) return;
2249 uint st_index
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_JS_PANEL
, WD_FRAMERECT_TOP
);
2250 if (st_index
> this->list
.size()) return;
2252 /* Insert station to be joined into stored command */
2253 SB(this->select_station_cmd
.p2
, 16, 16,
2254 (st_index
> 0) ? this->list
[st_index
- 1] : INVALID_STATION
);
2256 /* Execute stored Command */
2257 this->select_station_cmd
.execp();
2259 /* Close Window; this might cause double frees! */
2260 DeleteWindowById(WC_SELECT_STATION
, 0);
2263 virtual void OnTick()
2265 if (_thd
.dirty
& 2) {
2271 virtual void OnResize()
2273 this->vscroll
->SetCapacityFromWidget(this, WID_JS_PANEL
, WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
);
2277 * Some data on this window has become invalid.
2278 * @param data Information about the changed data.
2279 * @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.
2281 virtual void OnInvalidateData(int data
= 0, bool gui_scope
= true)
2283 if (!gui_scope
) return;
2287 if (!FindStationInArea (this->area
, this->waypoint
)) {
2288 FindStationsNearby (&this->list
, this->area
, _settings_game
.station
.distant_join_stations
, this->waypoint
);
2291 this->vscroll
->SetCount (this->list
.size() + 1);
2296 static WindowDesc::Prefs
_select_station_prefs ("build_station_join");
2298 static const WindowDesc
_select_station_desc(
2300 WC_SELECT_STATION
, WC_NONE
,
2302 _nested_select_station_widgets
, lengthof(_nested_select_station_widgets
),
2303 &_select_station_prefs
2308 * Show the station selection window when needed. If not, build the station.
2309 * @param cmd Command to build the station.
2310 * @param ta Area to build the station in
2311 * @param waypoint Look for waypoints, else stations
2313 void ShowSelectBaseStationIfNeeded (Command
*cmd
, const TileArea
&ta
, bool waypoint
)
2315 /* If a window is already opened and we didn't ctrl-click,
2316 * return true (i.e. just flash the old window) */
2317 Window
*selection_window
= FindWindowById(WC_SELECT_STATION
, 0);
2318 if (selection_window
!= NULL
) {
2319 /* Abort current distant-join and start new one */
2320 selection_window
->Delete();
2323 /* Only show the popup if we press ctrl and we can build there. */
2324 if (_ctrl_pressed
&& cmd
->exec(CommandFlagsToDCFlags(GetCommandFlags(cmd
->cmd
))).Succeeded()
2325 /* Test for adjacent station or station below selection.
2326 * If adjacent-stations is disabled and we are building
2327 * next to a station, do not show the selection window
2328 * but join the other station immediately. */
2329 && !FindStationInArea (ta
, waypoint
)) {
2330 std::vector
<StationID
> list
;
2331 FindStationsNearby (&list
, ta
, false, waypoint
);
2332 if (list
.size() == 0 ? _settings_game
.station
.distant_join_stations
: _settings_game
.station
.adjacent_stations
) {
2333 if (!_settings_client
.gui
.persistent_buildingtools
) ResetPointerMode();
2334 new SelectStationWindow (&_select_station_desc
, *cmd
, ta
, waypoint
, list
);