OpenTTD Source  20240919-master-gdf0233f4c2
build_vehicle_gui.cpp File Reference
#include "stdafx.h"
#include "engine_base.h"
#include "engine_func.h"
#include "station_base.h"
#include "network/network.h"
#include "articulated_vehicles.h"
#include "textbuf_gui.h"
#include "command_func.h"
#include "company_func.h"
#include "vehicle_gui.h"
#include "newgrf_engine.h"
#include "newgrf_text.h"
#include "group.h"
#include "string_func.h"
#include "strings_func.h"
#include "window_func.h"
#include "timer/timer_game_calendar.h"
#include "vehicle_func.h"
#include "dropdown_type.h"
#include "dropdown_func.h"
#include "engine_gui.h"
#include "cargotype.h"
#include "core/geometry_func.hpp"
#include "autoreplace_func.h"
#include "engine_cmd.h"
#include "train_cmd.h"
#include "vehicle_cmd.h"
#include "zoom_func.h"
#include "querystring_gui.h"
#include "stringfilter_type.h"
#include "hotkeys.h"
#include "widgets/build_vehicle_widget.h"
#include "table/strings.h"
#include "safeguards.h"

Go to the source code of this file.

Data Structures

struct  BuildVehicleWindow
 GUI for building vehicles. More...
 

Enumerations

enum  BuildVehicleHotkeys { BVHK_FOCUS_FILTER_BOX }
 Enum referring to the Hotkeys in the build vehicle window. More...
 

Functions

uint GetEngineListHeight (VehicleType type)
 Get the height of a single 'entry' in the engine lists. More...
 
static bool EngineNumberSorter (const GUIEngineListItem &a, const GUIEngineListItem &b)
 Determines order of engines by engineID. More...
 
static bool EngineIntroDateSorter (const GUIEngineListItem &a, const GUIEngineListItem &b)
 Determines order of engines by introduction date. More...
 
static bool EngineNameSorter (const GUIEngineListItem &a, const GUIEngineListItem &b)
 Determines order of engines by name. More...
 
static bool EngineReliabilitySorter (const GUIEngineListItem &a, const GUIEngineListItem &b)
 Determines order of engines by reliability. More...
 
static bool EngineCostSorter (const GUIEngineListItem &a, const GUIEngineListItem &b)
 Determines order of engines by purchase cost. More...
 
static bool EngineSpeedSorter (const GUIEngineListItem &a, const GUIEngineListItem &b)
 Determines order of engines by speed. More...
 
static bool EnginePowerSorter (const GUIEngineListItem &a, const GUIEngineListItem &b)
 Determines order of engines by power. More...
 
static bool EngineTractiveEffortSorter (const GUIEngineListItem &a, const GUIEngineListItem &b)
 Determines order of engines by tractive effort. More...
 
static bool EngineRunningCostSorter (const GUIEngineListItem &a, const GUIEngineListItem &b)
 Determines order of engines by running costs. More...
 
static bool EnginePowerVsRunningCostSorter (const GUIEngineListItem &a, const GUIEngineListItem &b)
 Determines order of engines by running costs. More...
 
static bool TrainEngineCapacitySorter (const GUIEngineListItem &a, const GUIEngineListItem &b)
 Determines order of train engines by capacity. More...
 
static bool TrainEnginesThenWagonsSorter (const GUIEngineListItem &a, const GUIEngineListItem &b)
 Determines order of train engines by engine / wagon. More...
 
static bool RoadVehEngineCapacitySorter (const GUIEngineListItem &a, const GUIEngineListItem &b)
 Determines order of road vehicles by capacity. More...
 
static bool ShipEngineCapacitySorter (const GUIEngineListItem &a, const GUIEngineListItem &b)
 Determines order of ships by capacity. More...
 
static bool AircraftEngineCargoSorter (const GUIEngineListItem &a, const GUIEngineListItem &b)
 Determines order of aircraft by cargo. More...
 
static bool AircraftRangeSorter (const GUIEngineListItem &a, const GUIEngineListItem &b)
 Determines order of aircraft by range. More...
 
static bool CargoAndEngineFilter (const GUIEngineListItem *item, const CargoID cid)
 Filters vehicles by cargo and engine (in case of rail vehicle).
 
static uint GetCargoWeight (const CargoArray &cap, VehicleType vtype)
 
static int DrawCargoCapacityInfo (int left, int right, int y, TestedEngineDetails &te, bool refittable)
 
static int DrawRailWagonPurchaseInfo (int left, int right, int y, EngineID engine_number, const RailVehicleInfo *rvi, TestedEngineDetails &te)
 
static int DrawRailEnginePurchaseInfo (int left, int right, int y, EngineID engine_number, const RailVehicleInfo *rvi, TestedEngineDetails &te)
 
static int DrawRoadVehPurchaseInfo (int left, int right, int y, EngineID engine_number, TestedEngineDetails &te)
 
static int DrawShipPurchaseInfo (int left, int right, int y, EngineID engine_number, bool refittable, TestedEngineDetails &te)
 
static int DrawAircraftPurchaseInfo (int left, int right, int y, EngineID engine_number, bool refittable, TestedEngineDetails &te)
 Draw aircraft specific details in the buy window. More...
 
static std::optional< std::string > GetNewGRFAdditionalText (EngineID engine)
 Try to get the NewGRF engine additional text callback as an optional std::string. More...
 
static uint ShowAdditionalText (int left, int right, int y, EngineID engine)
 Display additional text from NewGRF in the purchase information window. More...
 
int DrawVehiclePurchaseInfo (int left, int right, int y, EngineID engine_number, TestedEngineDetails &te)
 Draw the purchase info details of a vehicle at a given location. More...
 
void DrawEngineList (VehicleType type, const Rect &r, const GUIEngineList &eng_list, const Scrollbar &sb, EngineID selected_id, bool show_count, GroupID selected_group)
 Engine drawing loop. More...
 
void DisplayVehicleSortDropDown (Window *w, VehicleType vehicle_type, int selected, WidgetID button)
 Display the dropdown for the vehicle sort criteria. More...
 
void GUIEngineListAddChildren (GUIEngineList &dst, const GUIEngineList &src, EngineID parent, uint8_t indent)
 Add children to GUI engine list to build a hierarchical tree. More...
 
void ShowBuildVehicleWindow (TileIndex tile, VehicleType type)
 

Variables

static constexpr NWidgetPart _nested_build_vehicle_widgets []
 
bool _engine_sort_direction
 false = descending, true = ascending.
 
uint8_t _engine_sort_last_criteria [] = {0, 0, 0, 0}
 Last set sort criteria, for each vehicle type.
 
bool _engine_sort_last_order [] = {false, false, false, false}
 Last set direction of the sort order, for each vehicle type.
 
bool _engine_sort_show_hidden_engines [] = {false, false, false, false}
 Last set 'show hidden engines' setting for each vehicle type.
 
static CargoID _engine_sort_last_cargo_criteria [] = {CargoFilterCriteria::CF_ANY, CargoFilterCriteria::CF_ANY, CargoFilterCriteria::CF_ANY, CargoFilterCriteria::CF_ANY}
 Last set filter criteria, for each vehicle type.
 
static EngineID _last_engine [2] = { INVALID_ENGINE, INVALID_ENGINE }
 
EngList_SortTypeFunction *const _engine_sort_functions [][11]
 Sort functions for the vehicle sort criteria, for each vehicle type.
 
const std::initializer_list< const StringID_engine_sort_listing []
 Dropdown menu strings for the vehicle sort criteria.
 
static GUIEngineList::FilterFunction *const _engine_filter_funcs []
 
static WindowDesc _build_vehicle_desc (WDP_AUTO, "build_vehicle", 240, 268, WC_BUILD_VEHICLE, WC_NONE, WDF_CONSTRUCTION, _nested_build_vehicle_widgets, &BuildVehicleWindow::hotkeys)
 

Detailed Description

GUI for building vehicles.

Definition in file build_vehicle_gui.cpp.

Enumeration Type Documentation

◆ BuildVehicleHotkeys

Enum referring to the Hotkeys in the build vehicle window.

Enumerator
BVHK_FOCUS_FILTER_BOX 

Focus the edit box for editing the filter string.

Definition at line 1162 of file build_vehicle_gui.cpp.

Function Documentation

◆ AircraftEngineCargoSorter()

static bool AircraftEngineCargoSorter ( const GUIEngineListItem a,
const GUIEngineListItem b 
)
static

Determines order of aircraft by cargo.

Parameters
afirst engine to compare
bsecond engine to compare
Returns
for descending order: returns true if a < b. Vice versa for ascending order

Definition at line 396 of file build_vehicle_gui.cpp.

References GUIEngineListItem::engine_id, and Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Get().

◆ AircraftRangeSorter()

static bool AircraftRangeSorter ( const GUIEngineListItem a,
const GUIEngineListItem b 
)
static

Determines order of aircraft by range.

Parameters
afirst engine to compare
bsecond engine to compare
Returns
for descending order: returns true if a < b. Vice versa for ascending order

Definition at line 424 of file build_vehicle_gui.cpp.

◆ DisplayVehicleSortDropDown()

void DisplayVehicleSortDropDown ( Window w,
VehicleType  vehicle_type,
int  selected,
WidgetID  button 
)

Display the dropdown for the vehicle sort criteria.

Parameters
wParent window (holds the dropdown button).
vehicle_typeVehicle type being sorted.
selectedCurrently selected sort criterium.
buttonWidget button.

Definition at line 1107 of file build_vehicle_gui.cpp.

References _settings_game, VehicleSettings::roadveh_acceleration_model, VEH_ROAD, and GameSettings::vehicle.

◆ DrawAircraftPurchaseInfo()

static int DrawAircraftPurchaseInfo ( int  left,
int  right,
int  y,
EngineID  engine_number,
bool  refittable,
TestedEngineDetails te 
)
static

Draw aircraft specific details in the buy window.

Parameters
leftLeft edge of the window to draw in.
rightRight edge of the window to draw in.
yTop of the area to draw in.
engine_numberEngine to display.
refittableIf set, the aircraft can be refitted.
Returns
Bottom of the used area.

Definition at line 799 of file build_vehicle_gui.cpp.

◆ DrawEngineList()

void DrawEngineList ( VehicleType  type,
const Rect r,
const GUIEngineList eng_list,
const Scrollbar sb,
EngineID  selected_id,
bool  show_count,
GroupID  selected_group 
)

Engine drawing loop.

Parameters
typeType of vehicle (VEH_*)
rThe Rect of the list
eng_listWhat engines to draw
sbScrollbar of list.
selected_idwhat engine to highlight as selected, if any
show_countWhether to show the amount of engines or not
selected_groupthe group to list the engines of

Definition at line 1003 of file build_vehicle_gui.cpp.

References _current_text_dir, EIT_PURCHASE, VehicleCellSize::extend_left, VehicleCellSize::extend_right, GetEngineListHeight(), GetScaledSpriteSize(), GetVehicleImageCellSize(), Scrollbar::GetVisibleRangeIterators(), and TD_RTL.

◆ DrawVehiclePurchaseInfo()

int DrawVehiclePurchaseInfo ( int  left,
int  right,
int  y,
EngineID  engine_number,
TestedEngineDetails te 
)

Draw the purchase info details of a vehicle at a given location.

Parameters
left,right,ylocation where to draw the info
engine_numberthe engine of which to draw the info of
Returns
y after drawing all the text

Definition at line 917 of file build_vehicle_gui.cpp.

Referenced by ReplaceVehicleWindow::OnPaint(), and BuildVehicleWindow::OnPaint().

◆ EngineCostSorter()

static bool EngineCostSorter ( const GUIEngineListItem a,
const GUIEngineListItem b 
)
static

Determines order of engines by purchase cost.

Parameters
afirst engine to compare
bsecond engine to compare
Returns
for descending order: returns true if a < b. Vice versa for ascending order

Definition at line 189 of file build_vehicle_gui.cpp.

References _engine_sort_direction, GUIEngineListItem::engine_id, EngineNumberSorter(), and Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Get().

◆ EngineIntroDateSorter()

static bool EngineIntroDateSorter ( const GUIEngineListItem a,
const GUIEngineListItem b 
)
static

Determines order of engines by introduction date.

Parameters
afirst engine to compare
bsecond engine to compare
Returns
for descending order: returns true if a < b. Vice versa for ascending order

Definition at line 123 of file build_vehicle_gui.cpp.

References _engine_sort_direction, GUIEngineListItem::engine_id, EngineNumberSorter(), and Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Get().

◆ EngineNameSorter()

static bool EngineNameSorter ( const GUIEngineListItem a,
const GUIEngineListItem b 
)
static

Determines order of engines by name.

Parameters
afirst engine to compare
bsecond engine to compare
Returns
for descending order: returns true if a < b. Vice versa for ascending order

Definition at line 143 of file build_vehicle_gui.cpp.

References GUIEngineListItem::engine_id.

◆ EngineNumberSorter()

static bool EngineNumberSorter ( const GUIEngineListItem a,
const GUIEngineListItem b 
)
static

◆ EnginePowerSorter()

static bool EnginePowerSorter ( const GUIEngineListItem a,
const GUIEngineListItem b 
)
static

Determines order of engines by power.

Parameters
afirst engine to compare
bsecond engine to compare
Returns
for descending order: returns true if a < b. Vice versa for ascending order

Definition at line 223 of file build_vehicle_gui.cpp.

References _engine_sort_direction, GUIEngineListItem::engine_id, EngineNumberSorter(), and Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Get().

◆ EnginePowerVsRunningCostSorter()

static bool EnginePowerVsRunningCostSorter ( const GUIEngineListItem a,
const GUIEngineListItem b 
)
static

Determines order of engines by running costs.

Parameters
afirst engine to compare
bsecond engine to compare
Returns
for descending order: returns true if a < b. Vice versa for ascending order

Definition at line 274 of file build_vehicle_gui.cpp.

References _engine_sort_direction, GUIEngineListItem::engine_id, EngineNumberSorter(), EngineRunningCostSorter(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Get(), Engine::GetPower(), and Engine::GetRunningCost().

◆ EngineReliabilitySorter()

static bool EngineReliabilitySorter ( const GUIEngineListItem a,
const GUIEngineListItem b 
)
static

Determines order of engines by reliability.

Parameters
afirst engine to compare
bsecond engine to compare
Returns
for descending order: returns true if a < b. Vice versa for ascending order

Definition at line 172 of file build_vehicle_gui.cpp.

References _engine_sort_direction, GUIEngineListItem::engine_id, EngineNumberSorter(), and Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Get().

◆ EngineRunningCostSorter()

static bool EngineRunningCostSorter ( const GUIEngineListItem a,
const GUIEngineListItem b 
)
static

Determines order of engines by running costs.

Parameters
afirst engine to compare
bsecond engine to compare
Returns
for descending order: returns true if a < b. Vice versa for ascending order

Definition at line 257 of file build_vehicle_gui.cpp.

References _engine_sort_direction, GUIEngineListItem::engine_id, EngineNumberSorter(), and Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Get().

Referenced by EnginePowerVsRunningCostSorter().

◆ EngineSpeedSorter()

static bool EngineSpeedSorter ( const GUIEngineListItem a,
const GUIEngineListItem b 
)
static

Determines order of engines by speed.

Parameters
afirst engine to compare
bsecond engine to compare
Returns
for descending order: returns true if a < b. Vice versa for ascending order

Definition at line 206 of file build_vehicle_gui.cpp.

References _engine_sort_direction, GUIEngineListItem::engine_id, EngineNumberSorter(), and Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Get().

◆ EngineTractiveEffortSorter()

static bool EngineTractiveEffortSorter ( const GUIEngineListItem a,
const GUIEngineListItem b 
)
static

Determines order of engines by tractive effort.

Parameters
afirst engine to compare
bsecond engine to compare
Returns
for descending order: returns true if a < b. Vice versa for ascending order

Definition at line 240 of file build_vehicle_gui.cpp.

References _engine_sort_direction, GUIEngineListItem::engine_id, EngineNumberSorter(), and Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Get().

◆ GetEngineListHeight()

uint GetEngineListHeight ( VehicleType  type)

Get the height of a single 'entry' in the engine lists.

Parameters
typethe vehicle type to get the height of
Returns
the height for the entry

Definition at line 53 of file build_vehicle_gui.cpp.

References EIT_PURCHASE, FS_NORMAL, GetCharacterHeight(), GetVehicleImageCellSize(), and WidgetDimensions::scaled.

Referenced by DrawEngineList().

◆ GetNewGRFAdditionalText()

static std::optional<std::string> GetNewGRFAdditionalText ( EngineID  engine)
static

Try to get the NewGRF engine additional text callback as an optional std::string.

Parameters
engineThe engine whose additional text to get.
Returns
The std::string if present, otherwise std::nullopt.

Definition at line 860 of file build_vehicle_gui.cpp.

Referenced by BuildVehicleWindow::FilterByText().

◆ GUIEngineListAddChildren()

void GUIEngineListAddChildren ( GUIEngineList dst,
const GUIEngineList src,
EngineID  parent,
uint8_t  indent 
)

Add children to GUI engine list to build a hierarchical tree.

Parameters
dstDestination list.
srcSource list.
parentCurrent tree parent (set by self with recursion).
indentCurrent tree indentation level (set by self with recursion).

Definition at line 1130 of file build_vehicle_gui.cpp.

References Engine::display_last_variant, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Get(), GUIEngineListAddChildren(), HasVariants, INVALID_ENGINE, IsFolded, None, and Shaded.

Referenced by ReplaceVehicleWindow::GenerateReplaceVehList(), and GUIEngineListAddChildren().

◆ RoadVehEngineCapacitySorter()

static bool RoadVehEngineCapacitySorter ( const GUIEngineListItem a,
const GUIEngineListItem b 
)
static

Determines order of road vehicles by capacity.

Parameters
afirst engine to compare
bsecond engine to compare
Returns
for descending order: returns true if a < b. Vice versa for ascending order

Definition at line 355 of file build_vehicle_gui.cpp.

References _engine_sort_direction, GUIEngineListItem::engine_id, EngineNumberSorter(), and GetTotalCapacityOfArticulatedParts().

◆ ShipEngineCapacitySorter()

static bool ShipEngineCapacitySorter ( const GUIEngineListItem a,
const GUIEngineListItem b 
)
static

Determines order of ships by capacity.

Parameters
afirst engine to compare
bsecond engine to compare
Returns
for descending order: returns true if a < b. Vice versa for ascending order

Definition at line 374 of file build_vehicle_gui.cpp.

References _engine_sort_direction, GUIEngineListItem::engine_id, EngineNumberSorter(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Get(), and Engine::GetDisplayDefaultCapacity().

◆ ShowAdditionalText()

static uint ShowAdditionalText ( int  left,
int  right,
int  y,
EngineID  engine 
)
static

Display additional text from NewGRF in the purchase information window.

Parameters
leftLeft border of text bounding box
rightRight border of text bounding box
yTop border of text bounding box
engineEngine to query the additional purchase information for
Returns
Bottom border of text bounding box

Definition at line 885 of file build_vehicle_gui.cpp.

◆ TrainEngineCapacitySorter()

static bool TrainEngineCapacitySorter ( const GUIEngineListItem a,
const GUIEngineListItem b 
)
static

Determines order of train engines by capacity.

Parameters
afirst engine to compare
bsecond engine to compare
Returns
for descending order: returns true if a < b. Vice versa for ascending order

Definition at line 316 of file build_vehicle_gui.cpp.

◆ TrainEnginesThenWagonsSorter()

static bool TrainEnginesThenWagonsSorter ( const GUIEngineListItem a,
const GUIEngineListItem b 
)
static

Determines order of train engines by engine / wagon.

Parameters
afirst engine to compare
bsecond engine to compare
Returns
for descending order: returns true if a < b. Vice versa for ascending order

Definition at line 336 of file build_vehicle_gui.cpp.

Variable Documentation

◆ _engine_filter_funcs

GUIEngineList::FilterFunction* const _engine_filter_funcs[]
static
Initial value:

Definition at line 549 of file build_vehicle_gui.cpp.

CargoAndEngineFilter
static bool CargoAndEngineFilter(const GUIEngineListItem *item, const CargoID cid)
Filters vehicles by cargo and engine (in case of rail vehicle).
Definition: build_vehicle_gui.cpp:537