From b60e5c17cdfe0b2000260d3fb19db38867ff5e39 Mon Sep 17 00:00:00 2001 From: bjarni Date: Sun, 21 Jan 2007 22:50:43 +0000 Subject: [PATCH] (svn r8331) -Feature: the train and aircraft build windows are now resizable in horizontal direction as well -Codechange: merged train and aircraft build window code --- src/build_vehicle_gui.cpp | 483 ++++++++++++++++++++++++++++++----- src/depot_gui.cpp | 2 +- src/train.h | 2 + src/train_gui.cpp | 518 -------------------------------------- src/vehicle_gui.cpp | 19 +- src/vehicle_gui.h | 5 +- 6 files changed, 431 insertions(+), 598 deletions(-) diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp index a08dbc07b1..c22bbd9a17 100644 --- a/src/build_vehicle_gui.cpp +++ b/src/build_vehicle_gui.cpp @@ -2,6 +2,7 @@ #include "stdafx.h" #include "openttd.h" +#include "train.h" #include "aircraft.h" #include "debug.h" #include "functions.h" @@ -39,23 +40,49 @@ enum BuildVehicleWidgets { static const Widget _build_vehicle_widgets[] = { { WWT_CLOSEBOX, RESIZE_NONE, 14, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW }, - { WWT_CAPTION, RESIZE_NONE, 14, 11, 239, 0, 13, STR_A005_NEW_AIRCRAFT, STR_018C_WINDOW_TITLE_DRAG_THIS }, + { WWT_CAPTION, RESIZE_RIGHT, 14, 11, 227, 0, 13, 0x0, STR_018C_WINDOW_TITLE_DRAG_THIS }, { WWT_PUSHTXTBTN, RESIZE_NONE, 14, 0, 80, 14, 25, STR_SORT_BY, STR_SORT_ORDER_TIP}, - { WWT_PANEL, RESIZE_NONE, 14, 81, 227, 14, 25, 0x0, STR_SORT_CRITERIA_TIP}, - { WWT_TEXTBTN, RESIZE_NONE, 14, 228, 239, 14, 25, STR_0225, STR_SORT_CRITERIA_TIP}, - { WWT_MATRIX, RESIZE_BOTTOM, 14, 0, 227, 26, 121, 0x401, STR_A025_AIRCRAFT_SELECTION_LIST }, - { WWT_SCROLLBAR, RESIZE_BOTTOM, 14, 228, 239, 26, 121, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST }, - { WWT_PANEL, RESIZE_TB, 14, 0, 239, 122, 213, 0x0, STR_NULL }, + { WWT_PANEL, RESIZE_RIGHT, 14, 81, 215, 14, 25, 0x0, STR_SORT_CRITERIA_TIP}, + { WWT_TEXTBTN, RESIZE_LR, 14, 216, 227, 14, 25, STR_0225, STR_SORT_CRITERIA_TIP}, + { WWT_MATRIX, RESIZE_RB, 14, 0, 215, 26, 121, 0x0, STR_NULL }, + { WWT_SCROLLBAR, RESIZE_LRB, 14, 216, 227, 26, 121, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST }, + { WWT_PANEL, RESIZE_RTB, 14, 0, 227, 122, 213, 0x0, STR_NULL }, - { WWT_PUSHTXTBTN, RESIZE_TB, 14, 0, 114, 214, 225, STR_A006_BUILD_AIRCRAFT, STR_A026_BUILD_THE_HIGHLIGHTED_AIRCRAFT }, - { WWT_PUSHTXTBTN, RESIZE_TB, 14, 115, 227, 214, 225, STR_A037_RENAME, STR_A038_RENAME_AIRCRAFT_TYPE }, - { WWT_RESIZEBOX, RESIZE_TB, 14, 228, 239, 214, 225, 0x0, STR_RESIZE_BUTTON }, + { WWT_PUSHTXTBTN, RESIZE_TB, 14, 0, 114, 214, 225, 0x0, STR_NULL }, + { WWT_PUSHTXTBTN, RESIZE_RTB, 14, 115, 215, 214, 225, 0x0, STR_NULL }, + { WWT_RESIZEBOX, RESIZE_LRTB, 14, 216, 227, 214, 225, 0x0, STR_RESIZE_BUTTON }, { WIDGETS_END}, }; +/* Setup widget strings to fit the different types of vehicles */ +static void SetupWindowStrings(const Window *w, byte type) +{ + switch (type) { + case VEH_Train: + w->widget[BUILD_VEHICLE_WIDGET_CAPTION].data = STR_JUST_STRING; + w->widget[BUILD_VEHICLE_WIDGET_LIST].tooltips = STR_8843_TRAIN_VEHICLE_SELECTION; + w->widget[BUILD_VEHICLE_WIDGET_BUILD].data = STR_881F_BUILD_VEHICLE; + w->widget[BUILD_VEHICLE_WIDGET_BUILD].tooltips = STR_8844_BUILD_THE_HIGHLIGHTED_TRAIN; + w->widget[BUILD_VEHICLE_WIDGET_RENAME].data = STR_8820_RENAME; + w->widget[BUILD_VEHICLE_WIDGET_RENAME].tooltips = STR_8845_RENAME_TRAIN_VEHICLE_TYPE; + break; + case VEH_Aircraft: + w->widget[BUILD_VEHICLE_WIDGET_CAPTION].data = STR_A005_NEW_AIRCRAFT; + w->widget[BUILD_VEHICLE_WIDGET_LIST].tooltips = STR_A025_AIRCRAFT_SELECTION_LIST; + w->widget[BUILD_VEHICLE_WIDGET_BUILD].data = STR_A006_BUILD_AIRCRAFT; + w->widget[BUILD_VEHICLE_WIDGET_BUILD].tooltips = STR_A026_BUILD_THE_HIGHLIGHTED_AIRCRAFT; + w->widget[BUILD_VEHICLE_WIDGET_RENAME].data = STR_A037_RENAME; + w->widget[BUILD_VEHICLE_WIDGET_RENAME].tooltips = STR_A038_RENAME_AIRCRAFT_TYPE; + break; + } +} + static bool _internal_sort_order; // descending/ascending -static byte _last_sort_criteria = 0; -static bool _last_sort_order = false; +static byte _last_sort_criteria_train = 0; +static bool _last_sort_order_train = false; + +static byte _last_sort_criteria_aircraft = 0; +static bool _last_sort_order_aircraft = false; static int CDECL EngineNumberSorter(const void *a, const void *b) { @@ -120,6 +147,81 @@ static int CDECL EngineReliabilitySorter(const void *a, const void *b) return _internal_sort_order ? -r : r; } +/* Train sorting functions */ +static int CDECL TrainEngineCostSorter(const void *a, const void *b) +{ + int va = RailVehInfo(*(const EngineID*)a)->base_cost; + int vb = RailVehInfo(*(const EngineID*)b)->base_cost; + int r = va - vb; + + return _internal_sort_order ? -r : r; +} + +static int CDECL TrainEngineSpeedSorter(const void *a, const void *b) +{ + int va = RailVehInfo(*(const EngineID*)a)->max_speed; + int vb = RailVehInfo(*(const EngineID*)b)->max_speed; + int r = va - vb; + + return _internal_sort_order ? -r : r; +} + +static int CDECL TrainEnginePowerSorter(const void *a, const void *b) +{ + const RailVehicleInfo *rvi_a = RailVehInfo(*(const EngineID*)a); + const RailVehicleInfo *rvi_b = RailVehInfo(*(const EngineID*)b); + + int va = rvi_a->power << (rvi_a->flags & RVI_MULTIHEAD ? 1 : 0); + int vb = rvi_b->power << (rvi_b->flags & RVI_MULTIHEAD ? 1 : 0); + int r = va - vb; + + return _internal_sort_order ? -r : r; +} + +static int CDECL TrainEngineRunningCostSorter(const void *a, const void *b) +{ + const RailVehicleInfo *rvi_a = RailVehInfo(*(const EngineID*)a); + const RailVehicleInfo *rvi_b = RailVehInfo(*(const EngineID*)b); + + int va = rvi_a->running_cost_base * _price.running_rail[rvi_a->running_cost_class] * (rvi_a->flags & RVI_MULTIHEAD ? 2 : 1); + int vb = rvi_b->running_cost_base * _price.running_rail[rvi_b->running_cost_class] * (rvi_b->flags & RVI_MULTIHEAD ? 2 : 1); + int r = va - vb; + + return _internal_sort_order ? -r : r; +} + +static int CDECL TrainEnginePowerVsRunningCostSorter(const void *a, const void *b) +{ + const RailVehicleInfo *rvi_a = RailVehInfo(*(const EngineID*)a); + const RailVehicleInfo *rvi_b = RailVehInfo(*(const EngineID*)b); + + /* Here we are using a few tricks to get the right sort. + * We want power/running cost, but since we usually got higher running cost than power and we store the result in an int, + * we will actually calculate cunning cost/power (to make it more than 1). + * Because of this, the return value have to be reversed as well and we return b - a instead of a - b. + * Another thing is that both power and running costs should be doubled for multiheaded engines. + * Since it would be multipling with 2 in both numerator and denumerator, it will even themselves out and we skip checking for multiheaded. */ + int va = (rvi_a->running_cost_base * _price.running_rail[rvi_a->running_cost_class]) / max((uint16)1, rvi_a->power); + int vb = (rvi_b->running_cost_base * _price.running_rail[rvi_b->running_cost_class]) / max((uint16)1, rvi_b->power); + int r = vb - va; + + return _internal_sort_order ? -r : r; +} + +static int CDECL TrainEnginesThenWagonsSorter(const void *a, const void *b) +{ + EngineID va = *(const EngineID*)a; + EngineID vb = *(const EngineID*)b; + int val_a = ((RailVehInfo(va)->flags & RVI_WAGON) != 0) ? 1 : 0; + int val_b = ((RailVehInfo(vb)->flags & RVI_WAGON) != 0) ? 1 : 0; + int r = val_a - val_b; + + /* Use EngineID to sort instead since we want consistent sorting */ + if (r == 0) return EngineNumberSorter(a, b); + + return _internal_sort_order ? -r : r; +} + /* Aircraft sorting functions */ static int CDECL AircraftEngineCostSorter(const void *a, const void *b) @@ -170,6 +272,31 @@ static int CDECL AircraftEngineCargoSorter(const void *a, const void *b) return _internal_sort_order ? -r : r; } +static EngList_SortTypeFunction * const _train_sorter[] = { + &EngineNumberSorter, + &TrainEngineCostSorter, + &TrainEngineSpeedSorter, + &TrainEnginePowerSorter, + &EngineIntroDateSorter, + &EngineNameSorter, + &TrainEngineRunningCostSorter, + &TrainEnginePowerVsRunningCostSorter, + &EngineReliabilitySorter, +}; + +static const StringID _train_sort_listing[] = { + STR_ENGINE_SORT_ENGINE_ID, + STR_ENGINE_SORT_COST, + STR_SORT_BY_MAX_SPEED, + STR_ENGINE_SORT_POWER, + STR_ENGINE_SORT_INTRO_DATE, + STR_SORT_BY_DROPDOWN_NAME, + STR_ENGINE_SORT_RUNNING_COST, + STR_ENGINE_SORT_POWER_VS_RUNNING_COST, + STR_SORT_BY_RELIABILITY, + INVALID_STRING_ID +}; + static EngList_SortTypeFunction * const _aircraft_sorter[] = { &EngineNumberSorter, &AircraftEngineCostSorter, @@ -194,18 +321,73 @@ static const StringID _aircraft_sort_listing[] = { }; -/** -* Draw the purchase info details of an aircraft at a given location. - * @param x,y location where to draw the info - * @param engine_number the engine of which to draw the info of - */ -void DrawAircraftPurchaseInfo(int x, int y, uint w, EngineID engine_number) +/* Draw rail wagon specific details */ +static int DrawVehiclePurchaseInfo(int x, int y, EngineID engine_number, const RailVehicleInfo *rvi) +{ + /* Purchase cost */ + SetDParam(0, (rvi->base_cost * _price.build_railwagon) >> 8); + DrawString(x, y, STR_PURCHASE_INFO_COST, 0); + y += 10; + + /* Wagon weight - (including cargo) */ + SetDParam(0, rvi->weight); + SetDParam(1, (_cargoc.weights[rvi->cargo_type] * rvi->capacity >> 4) + rvi->weight); + DrawString(x, y, STR_PURCHASE_INFO_WEIGHT_CWEIGHT, 0); + y += 10; + + /* Wagon speed limit, displayed if above zero */ + if (rvi->max_speed > 0 && _patches.wagon_speed_limits) { + SetDParam(0, rvi->max_speed); + DrawString(x,y, STR_PURCHASE_INFO_SPEED, 0); + y += 10; + } + return y; +} + +/* Draw locomotive specific details */ +static int DrawVehiclePurchaseInfo(int x, int y, EngineID engine_number, const RailVehicleInfo *rvi, const Engine *e) +{ + int multihead = (rvi->flags&RVI_MULTIHEAD?1:0); + + /* Purchase Cost - Engine weight */ + SetDParam(0, rvi->base_cost * (_price.build_railvehicle >> 3) >> 5); + SetDParam(1, rvi->weight << multihead); + DrawString(x,y, STR_PURCHASE_INFO_COST_WEIGHT, 0); + y += 10; + + /* Max speed - Engine power */ + SetDParam(0, rvi->max_speed); + SetDParam(1, rvi->power << multihead); + DrawString(x,y, STR_PURCHASE_INFO_SPEED_POWER, 0); + y += 10; + + /* Max tractive effort - not applicable if old acceleration or maglev */ + if (_patches.realistic_acceleration && e->railtype != RAILTYPE_MAGLEV) { + SetDParam(0, ((rvi->weight << multihead) * 10 * rvi->tractive_effort) / 256); + DrawString(x, y, STR_PURCHASE_INFO_MAX_TE, 0); + y += 10; + } + + /* Running cost */ + SetDParam(0, (rvi->running_cost_base * _price.running_rail[rvi->running_cost_class] >> 8) << multihead); + DrawString(x,y, STR_PURCHASE_INFO_RUNNINGCOST, 0); + y += 10; + + /* Powered wagons power - Powered wagons extra weight */ + if (rvi->pow_wag_power != 0) { + SetDParam(0, rvi->pow_wag_power); + SetDParam(1, rvi->pow_wag_weight); + DrawString(x,y, STR_PURCHASE_INFO_PWAGPOWER_PWAGWEIGHT, 0); + y += 10; + }; + + return y; +} + +/* Draw aircraft specific details */ +static int DrawVehiclePurchaseInfo(int x, int y, EngineID engine_number, const AircraftVehicleInfo *avi) { - const AircraftVehicleInfo *avi = AircraftVehInfo(engine_number); - const Engine *e = GetEngine(engine_number); CargoID cargo; - YearMonthDay ymd; - ConvertDateToYMD(e->intro_date, &ymd); /* Purchase cost - Max speed */ SetDParam(0, avi->base_cost * (_price.aircraft_base>>3)>>5); @@ -234,20 +416,76 @@ void DrawAircraftPurchaseInfo(int x, int y, uint w, EngineID engine_number) DrawString(x, y, STR_PURCHASE_INFO_RUNNINGCOST, 0); y += 10; - /* Design date - Life length */ - SetDParam(0, ymd.year); - SetDParam(1, e->lifelength); - DrawString(x, y, STR_PURCHASE_INFO_DESIGNED_LIFE, 0); - y += 10; + return y; +} - /* Reliability */ - SetDParam(0, e->reliability * 100 >> 16); - DrawString(x, y, STR_PURCHASE_INFO_RELIABILITY, 0); - y += 10; +/** + * Draw the purchase info details of a vehicle at a given location. + * @param x,y location where to draw the info + * @param w how wide are the text allowed to be (size of widget/window to Draw in) + * @param engine_number the engine of which to draw the info of + */ +void DrawVehiclePurchaseInfo(int x, int y, uint w, EngineID engine_number) +{ + const Engine *e = GetEngine(engine_number); + YearMonthDay ymd; + ConvertDateToYMD(e->intro_date, &ymd); + + switch (e->type) { + case VEH_Train: { + const RailVehicleInfo *rvi = RailVehInfo(engine_number); + + if (rvi->flags & RVI_WAGON) { + y = DrawVehiclePurchaseInfo(x, y, engine_number, rvi); + } else { + y = DrawVehiclePurchaseInfo(x, y, engine_number, rvi, e); + } + + /* Cargo type + capacity, or N/A */ + if (rvi->capacity == 0) { + SetDParam(0, CT_INVALID); + SetDParam(2, STR_EMPTY); + } else { + int multihead = (rvi->flags & RVI_MULTIHEAD ? 1 : 0); + SetDParam(0, rvi->cargo_type); + SetDParam(1, (rvi->capacity * (CountArticulatedParts(engine_number) + 1)) << multihead); + SetDParam(2, STR_9842_REFITTABLE); + } + DrawString(x,y, STR_PURCHASE_INFO_CAPACITY, 0); + y += 10; + } + break; + case VEH_Aircraft: + y = DrawVehiclePurchaseInfo(x, y, engine_number, AircraftVehInfo(engine_number)); + break; + default: NOT_REACHED(); + } + + /* Draw details, that applies to all types except rail wagons */ + if (e->type != VEH_Train || (RailVehInfo(engine_number)->flags & RVI_WAGON == 0)) { + /* Design date - Life length */ + SetDParam(0, ymd.year); + SetDParam(1, e->lifelength); + DrawString(x, y, STR_PURCHASE_INFO_DESIGNED_LIFE, 0); + y += 10; + + /* Reliability */ + SetDParam(0, e->reliability * 100 >> 16); + DrawString(x, y, STR_PURCHASE_INFO_RELIABILITY, 0); + y += 10; + } /* Additional text from NewGRF */ y += ShowAdditionalText(x, y, w, engine_number); - y += ShowRefitOptionsList(x, y, w, engine_number); + switch (e->type) { + case VEH_Train: { + const RailVehicleInfo *rvi = RailVehInfo(engine_number); + if (rvi->capacity > 0) y += ShowRefitOptionsList(x, y, w, engine_number); + } break; + case VEH_Aircraft: + y += ShowRefitOptionsList(x, y, w, engine_number); + break; + } } void DrawAircraftImage(const Vehicle *v, int x, int y, VehicleID selection) @@ -277,6 +515,54 @@ void CcBuildAircraft(bool success, TileIndex tile, uint32 p1, uint32 p2) } } +/* Figure out what train EngineIDs to put in the list */ +static void GenerateBuildTrainList(Window *w) +{ + EngineID eid, sel_id; + int num_engines = 0; + int num_wagons = 0; + buildvehicle_d *bv = &WP(w, buildvehicle_d); + + bv->filter.railtype = (w->window_number == 0) ? RAILTYPE_END : GetRailType(w->window_number); + + EngList_RemoveAll(&bv->eng_list); + + /* Make list of all available train engines and wagons. + * Also check to see if the previously selected engine is still available, + * and if not, reset selection to INVALID_ENGINE. This could be the case + * when engines become obsolete and are removed */ + for (sel_id = INVALID_ENGINE, eid = 0; eid < NUM_TRAIN_ENGINES; eid++) { + const Engine *e = GetEngine(eid); + const RailVehicleInfo *rvi = RailVehInfo(eid); + + if (bv->filter.railtype != RAILTYPE_END && !HasPowerOnRail(e->railtype, bv->filter.railtype)) continue; + if (!IsEngineBuildable(eid, VEH_Train, _local_player)) continue; + + EngList_Add(&bv->eng_list, eid); + if ((rvi->flags & RVI_WAGON) == 0) { + num_engines++; + } else { + num_wagons++; + } + + if (eid == bv->sel_engine) sel_id = eid; + } + + bv->sel_engine = sel_id; + + // make engines first, and then wagons, sorted by ListPositionOfEngine() + _internal_sort_order = false; + EngList_Sort(&bv->eng_list, TrainEnginesThenWagonsSorter); + + // and then sort engines + _internal_sort_order = bv->descending_sort_order; + EngList_SortPartial(&bv->eng_list, _train_sorter[bv->sort_criteria], 0, num_engines); + + // and finally sort wagons + EngList_SortPartial(&bv->eng_list, _train_sorter[bv->sort_criteria], num_engines, num_wagons); +} + +/* Figure out what aircraft EngineIDs to put in the list */ static void GenerateBuildAircraftList(Window *w) { EngineID eid, sel_id; @@ -314,82 +600,122 @@ static void GenerateBuildAircraftList(Window *w) bv->sel_engine = sel_id; } +/* Generate the list of vehicles */ static void GenerateBuildList(Window *w) { buildvehicle_d *bv = &WP(w, buildvehicle_d); switch (bv->vehicle_type) { + case VEH_Train: + GenerateBuildTrainList(w); + break; case VEH_Aircraft: GenerateBuildAircraftList(w); _internal_sort_order = bv->descending_sort_order; EngList_Sort(&bv->eng_list, _aircraft_sorter[bv->sort_criteria]); break; - - default: NOT_REACHED(); } } -static void DrawBuildAircraftWindow(Window *w) +static void DrawBuildVehicleWindow(Window *w) { const buildvehicle_d *bv = &WP(w, buildvehicle_d); SetWindowWidgetDisabledState(w, BUILD_VEHICLE_WIDGET_BUILD, w->window_number == 0); SetVScrollCount(w, EngList_Count(&bv->eng_list)); + SetDParam(0, bv->filter.railtype + STR_881C_NEW_RAIL_VEHICLES); // This should only affect rail vehicles DrawWindowWidgets(w); { int x = 2; int y = 27; EngineID selected_id = bv->sel_engine; - EngineID eid = w->vscroll.pos; + uint16 position = w->vscroll.pos; uint16 max = min(w->vscroll.pos + w->vscroll.cap, EngList_Count(&bv->eng_list)); - for (; eid < max; eid++) { - const EngineID engine = bv->eng_list[eid]; + switch (bv->vehicle_type) { + case VEH_Train: + for (; position < max; position++, y += 14) { + const EngineID engine = bv->eng_list[position]; - DrawString(x + 62, y + 7, GetCustomEngineName(engine), engine == selected_id ? 0xC : 0x10); - DrawAircraftEngine(x + 29, y + 10, engine, GetEnginePalette(engine, _local_player)); - y += 24; + DrawString(x + 59, y + 2, GetCustomEngineName(engine), engine == selected_id ? 0xC : 0x10); + DrawTrainEngine(x + 29, y + 6, engine, GetEnginePalette(engine, _local_player)); + } + break; + case VEH_Aircraft: + for (; position < max; position++, y += 24) { + const EngineID engine = bv->eng_list[position]; + + DrawString(x + 62, y + 7, GetCustomEngineName(engine), engine == selected_id ? 0xC : 0x10); + DrawAircraftEngine(x + 29, y + 10, engine, GetEnginePalette(engine, _local_player)); + } + break; } if (selected_id != INVALID_ENGINE) { const Widget *wi = &w->widget[BUILD_VEHICLE_WIDGET_PANEL]; - DrawAircraftPurchaseInfo(x, wi->top + 1, wi->right - wi->left - 2, selected_id); + DrawVehiclePurchaseInfo(x, wi->top + 1, wi->right - wi->left - 2, selected_id); } } - DrawString(85, 15, _aircraft_sort_listing[bv->sort_criteria], 0x10); - DoDrawString(bv->descending_sort_order ? DOWNARROW : UPARROW, 69, 15, 0x10); + { + StringID str = STR_NULL; + switch (bv->vehicle_type) { + case VEH_Train: str = _train_sort_listing[bv->sort_criteria]; break; + case VEH_Aircraft: str = _aircraft_sort_listing[bv->sort_criteria]; break; + } + + DrawString(85, 15, str, 0x10); + DoDrawString(bv->descending_sort_order ? DOWNARROW : UPARROW, 69, 15, 0x10); + } } -static void BuildAircraftClickEvent(Window *w, WindowEvent *e) +static void BuildVehicleClickEvent(Window *w, WindowEvent *e) { buildvehicle_d *bv = &WP(w, buildvehicle_d); switch (e->we.click.widget) { case BUILD_VEHICLE_WIDGET_SORT_ASSENDING_DESCENDING: bv->descending_sort_order ^= true; - _last_sort_order = bv->descending_sort_order; + switch (bv->vehicle_type) { + case VEH_Train: _last_sort_order_train = bv->descending_sort_order; break; + case VEH_Aircraft: _last_sort_order_aircraft = bv->descending_sort_order; break; + } GenerateBuildList(w); SetWindowDirty(w); break; case BUILD_VEHICLE_WIDGET_LIST: { - uint i = (e->we.click.pt.y - 26) / 24 + w->vscroll.pos; + uint i = (e->we.click.pt.y - 26) / GetVehicleListHeight(bv->vehicle_type) + w->vscroll.pos; uint num_items = EngList_Count(&bv->eng_list); bv->sel_engine = (i < num_items) ? bv->eng_list[i] : INVALID_ENGINE; SetWindowDirty(w); break; } - case BUILD_VEHICLE_WIDGET_SORT_TEXT: case BUILD_VEHICLE_WIDGET_SORT_DROPDOWN:/* Select sorting criteria dropdown menu */ - ShowDropDownMenu(w, _aircraft_sort_listing, bv->sort_criteria, BUILD_VEHICLE_WIDGET_SORT_DROPDOWN, 0, 0); + case BUILD_VEHICLE_WIDGET_SORT_TEXT: case BUILD_VEHICLE_WIDGET_SORT_DROPDOWN: // Select sorting criteria dropdown menu + switch (bv->vehicle_type) { + case VEH_Train: + ShowDropDownMenu(w, _train_sort_listing, bv->sort_criteria, BUILD_VEHICLE_WIDGET_SORT_DROPDOWN, 0, 0); + break; + case VEH_Aircraft: + ShowDropDownMenu(w, _aircraft_sort_listing, bv->sort_criteria, BUILD_VEHICLE_WIDGET_SORT_DROPDOWN, 0, 0); + break; + } return; case BUILD_VEHICLE_WIDGET_BUILD: { EngineID sel_eng = bv->sel_engine; if (sel_eng != INVALID_ENGINE) { - DoCommandP(w->window_number, sel_eng, 0, CcBuildAircraft, CMD_BUILD_AIRCRAFT | CMD_MSG(STR_A008_CAN_T_BUILD_AIRCRAFT)); + switch (bv->vehicle_type) { + case VEH_Train: + DoCommandP(w->window_number, sel_eng, 0, (RailVehInfo(sel_eng)->flags & RVI_WAGON) ? CcBuildWagon : CcBuildLoco, + CMD_BUILD_RAIL_VEHICLE | CMD_MSG(STR_882B_CAN_T_BUILD_RAILROAD_VEHICLE)); + break; + case VEH_Aircraft: + DoCommandP(w->window_number, sel_eng, 0, CcBuildAircraft, CMD_BUILD_AIRCRAFT | CMD_MSG(STR_A008_CAN_T_BUILD_AIRCRAFT)); + break; + } } break; } @@ -397,15 +723,21 @@ static void BuildAircraftClickEvent(Window *w, WindowEvent *e) case BUILD_VEHICLE_WIDGET_RENAME: { EngineID sel_eng = bv->sel_engine; if (sel_eng != INVALID_ENGINE) { + StringID str = STR_NULL; + bv->rename_engine = sel_eng; - ShowQueryString(GetCustomEngineName(sel_eng), STR_A039_RENAME_AIRCRAFT_TYPE, 31, 160, w, CS_ALPHANUMERAL); + switch (bv->vehicle_type) { + case VEH_Train: str = STR_886A_RENAME_TRAIN_VEHICLE_TYPE; break; + case VEH_Aircraft: str = STR_A039_RENAME_AIRCRAFT_TYPE; break; + } + ShowQueryString(GetCustomEngineName(sel_eng), str, 31, 160, w, CS_ALPHANUMERAL); } break; } } } -static void NewAircraftWndProc(Window *w, WindowEvent *e) +static void NewVehicleWndProc(Window *w, WindowEvent *e) { buildvehicle_d *bv = &WP(w, buildvehicle_d); @@ -419,42 +751,53 @@ static void NewAircraftWndProc(Window *w, WindowEvent *e) break; case WE_PAINT: - DrawBuildAircraftWindow(w); + DrawBuildVehicleWindow(w); break; case WE_CLICK: - BuildAircraftClickEvent(w, e); + BuildVehicleClickEvent(w, e); break; case WE_ON_EDIT_TEXT: { if (e->we.edittext.str[0] != '\0') { + StringID str = STR_NULL; _cmd_text = e->we.edittext.str; - DoCommandP(0, bv->rename_engine, 0, NULL, CMD_RENAME_ENGINE | CMD_MSG(STR_A03A_CAN_T_RENAME_AIRCRAFT_TYPE)); + switch (bv->vehicle_type) { + case VEH_Train: str = STR_886B_CAN_T_RENAME_TRAIN_VEHICLE; break; + case VEH_Aircraft: str = STR_A03A_CAN_T_RENAME_AIRCRAFT_TYPE; break; + } + DoCommandP(0, bv->rename_engine, 0, NULL, CMD_RENAME_ENGINE | CMD_MSG(str)); } break; } case WE_DROPDOWN_SELECT: /* we have selected a dropdown item in the list */ if (bv->sort_criteria != e->we.dropdown.index) { - bv->sort_criteria = _last_sort_criteria = e->we.dropdown.index; + bv->sort_criteria = e->we.dropdown.index; + switch (bv->vehicle_type) { + case VEH_Train: _last_sort_criteria_train = bv->sort_criteria; break; + case VEH_Aircraft: _last_sort_criteria_aircraft = bv->sort_criteria; break; + } GenerateBuildList(w); } SetWindowDirty(w); break; case WE_RESIZE: - w->vscroll.cap += e->we.sizing.diff.y / 24; + if (e->we.sizing.diff.y == 0) break; + + w->vscroll.cap += e->we.sizing.diff.y / GetVehicleListHeight(bv->vehicle_type); w->widget[BUILD_VEHICLE_WIDGET_LIST].data = (w->vscroll.cap << 8) + 1; break; } } static const WindowDesc _build_vehicle_desc = { - WDP_AUTO, WDP_AUTO, 240, 226, + WDP_AUTO, WDP_AUTO, 228, 226, WC_BUILD_VEHICLE,0, WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_RESIZABLE, _build_vehicle_widgets, - NewAircraftWndProc + NewVehicleWndProc }; void ShowBuildVehicleWindow(TileIndex tile, byte type) @@ -462,33 +805,47 @@ void ShowBuildVehicleWindow(TileIndex tile, byte type) buildvehicle_d *bv; Window *w; + assert(IsPlayerBuildableVehicleType(type)); + DeleteWindowById(WC_BUILD_VEHICLE, tile); + w = AllocateWindowDescFront(&_build_vehicle_desc, tile); + if (w == NULL) return; w->caption_color = (tile != 0) ? GetTileOwner(tile) : _local_player; w->resize.step_height = GetVehicleListHeight(type); - w->vscroll.cap = 4; + w->vscroll.cap = w->resize.step_height == 24 ? 4 : 8; w->widget[BUILD_VEHICLE_WIDGET_LIST].data = (w->vscroll.cap << 8) + 1; bv = &WP(w, buildvehicle_d); EngList_Create(&bv->eng_list); bv->sel_engine = INVALID_ENGINE; - bv->sort_criteria = _last_sort_criteria; - bv->descending_sort_order = _last_sort_order; bv->vehicle_type = type; switch (type) { + case VEH_Train: + WP(w,buildvehicle_d).filter.railtype = (tile == 0) ? RAILTYPE_END : GetRailType(tile); + ResizeWindow(w, 0, 16); + bv->sort_criteria = _last_sort_criteria_train; + bv->descending_sort_order = _last_sort_order_train; + break; case VEH_Aircraft: { AcceptPlanes acc_planes = (tile == 0) ? ALL : GetAirport(GetStationByTile(tile)->airport_type)->acc_planes; bv->filter.acc_planes = acc_planes; + ResizeWindow(w, 12, 0); + bv->sort_criteria = _last_sort_criteria_aircraft; + bv->descending_sort_order = _last_sort_order_aircraft; break; } - default: NOT_REACHED(); } + SetupWindowStrings(w, type); + + w->resize.width = w->width; + w->resize.height = w->height; GenerateBuildList(w); - /* Select the first plane in the list as default when opening the window */ + /* Select the first engine in the list as default when opening the window */ if (EngList_Count(&bv->eng_list) > 0) bv->sel_engine = bv->eng_list[0]; } diff --git a/src/depot_gui.cpp b/src/depot_gui.cpp index 1bcc9babda..1e21af5147 100644 --- a/src/depot_gui.cpp +++ b/src/depot_gui.cpp @@ -736,9 +736,9 @@ static void DepotWndProc(Window *w, WindowEvent *e) case DEPOT_WIDGET_BUILD: // Build vehicle ResetObjectToPlace(); switch (WP(w, depot_d).type) { - case VEH_Train: ShowBuildTrainWindow(w->window_number); break; case VEH_Road: ShowBuildRoadVehWindow(w->window_number); break; case VEH_Ship: ShowBuildShipWindow(w->window_number); break; + case VEH_Train: case VEH_Aircraft: ShowBuildVehicleWindow(w->window_number, WP(w, depot_d).type); break; diff --git a/src/train.h b/src/train.h index aead2f2ca3..ed7e2a9623 100644 --- a/src/train.h +++ b/src/train.h @@ -220,6 +220,8 @@ void ConnectMultiheadedTrains(void); uint CountArticulatedParts(EngineID engine_type); int CheckTrainInDepot(const Vehicle *v, bool needs_to_be_stopped); +void CcBuildLoco(bool success, TileIndex tile, uint32 p1, uint32 p2); +void CcBuildWagon(bool success, TileIndex tile, uint32 p1, uint32 p2); void CcCloneTrain(bool success, TileIndex tile, uint32 p1, uint32 p2); byte FreightWagonMult(CargoID cargo); diff --git a/src/train_gui.cpp b/src/train_gui.cpp index 606d4981c1..0e7a4dbfad 100644 --- a/src/train_gui.cpp +++ b/src/train_gui.cpp @@ -4,326 +4,17 @@ #include "openttd.h" #include "debug.h" #include "functions.h" -#include "rail_map.h" #include "table/sprites.h" #include "table/strings.h" -#include "map.h" -#include "engine.h" #include "window.h" #include "gui.h" -#include "gfx.h" #include "vehicle.h" #include "viewport.h" -#include "station.h" #include "command.h" -#include "player.h" #include "vehicle_gui.h" #include "depot.h" #include "train.h" #include "newgrf_engine.h" -#include "date.h" -#include "strings.h" - -enum BuildTrainWidgets { - BUILD_TRAIN_WIDGET_CLOSEBOX = 0, - BUILD_TRAIN_WIDGET_CAPTION, - BUILD_TRAIN_WIDGET_SORT_ASCENDING_DESCENDING, - BUILD_TRAIN_WIDGET_SORT_TEXT, - BUILD_TRAIN_WIDGET_SORT_DROPDOWN, - BUILD_TRAIN_WIDGET_LIST, - BUILD_TRAIN_WIDGET_SCROLLBAR, - BUILD_TRAIN_WIDGET_PANEL, - BUILD_TRAIN_WIDGET_BUILD, - BUILD_TRAIN_WIDGET_RENAME, - BUILD_TRAIN_WIDGET_RESIZE, -}; - -static const Widget _new_rail_vehicle_widgets[] = { - { WWT_CLOSEBOX, RESIZE_NONE, 14, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, - { WWT_CAPTION, RESIZE_NONE, 14, 11, 227, 0, 13, STR_JUST_STRING, STR_018C_WINDOW_TITLE_DRAG_THIS}, - { WWT_PUSHTXTBTN, RESIZE_NONE, 14, 0, 80, 14, 25, STR_SORT_BY, STR_SORT_ORDER_TIP}, - { WWT_PANEL, RESIZE_NONE, 14, 81, 215, 14, 25, 0x0, STR_SORT_CRITERIA_TIP}, - { WWT_TEXTBTN, RESIZE_NONE, 14, 216, 227, 14, 25, STR_0225, STR_SORT_CRITERIA_TIP}, - { WWT_MATRIX, RESIZE_BOTTOM, 14, 0, 215, 26, 137, 0x801, STR_8843_TRAIN_VEHICLE_SELECTION}, - { WWT_SCROLLBAR, RESIZE_BOTTOM, 14, 216, 227, 26, 137, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST}, - { WWT_PANEL, RESIZE_TB, 14, 0, 227, 138, 239, 0x0, STR_NULL}, - { WWT_PUSHTXTBTN, RESIZE_TB, 14, 0, 107, 240, 251, STR_881F_BUILD_VEHICLE, STR_8844_BUILD_THE_HIGHLIGHTED_TRAIN}, - { WWT_PUSHTXTBTN, RESIZE_TB, 14, 108, 215, 240, 251, STR_8820_RENAME, STR_8845_RENAME_TRAIN_VEHICLE_TYPE}, - { WWT_RESIZEBOX, RESIZE_TB, 14, 216, 227, 240, 251, 0x0, STR_RESIZE_BUTTON}, - { WIDGETS_END}, -}; - -static bool _internal_sort_order; // false = ascending, true = descending -static byte _last_sort_criteria = 0; -static bool _last_sort_order = false; - -static int CDECL TrainEngineNumberSorter(const void *a, const void *b) -{ - const EngineID va = *(const EngineID*)a; - const EngineID vb = *(const EngineID*)b; - int r = ListPositionOfEngine(va) - ListPositionOfEngine(vb); - - return _internal_sort_order ? -r : r; -} - -static int CDECL TrainEnginesThenWagonsSorter(const void *a, const void *b) -{ - EngineID va = *(const EngineID*)a; - EngineID vb = *(const EngineID*)b; - int val_a = ((RailVehInfo(va)->flags & RVI_WAGON) != 0) ? 1 : 0; - int val_b = ((RailVehInfo(vb)->flags & RVI_WAGON) != 0) ? 1 : 0; - int r = val_a - val_b; - - /* Use EngineID to sort instead since we want consistent sorting */ - if (r == 0) return TrainEngineNumberSorter(a, b); - - return _internal_sort_order ? -r : r; -} - -static int CDECL TrainEngineCostSorter(const void *a, const void *b) -{ - int va = RailVehInfo(*(const EngineID*)a)->base_cost; - int vb = RailVehInfo(*(const EngineID*)b)->base_cost; - int r = va - vb; - - return _internal_sort_order ? -r : r; -} - -static int CDECL TrainEngineSpeedSorter(const void *a, const void *b) -{ - int va = RailVehInfo(*(const EngineID*)a)->max_speed; - int vb = RailVehInfo(*(const EngineID*)b)->max_speed; - int r = va - vb; - - return _internal_sort_order ? -r : r; -} - -static int CDECL TrainEnginePowerSorter(const void *a, const void *b) -{ - const RailVehicleInfo *rvi_a = RailVehInfo(*(const EngineID*)a); - const RailVehicleInfo *rvi_b = RailVehInfo(*(const EngineID*)b); - - int va = rvi_a->power << (rvi_a->flags & RVI_MULTIHEAD ? 1 : 0); - int vb = rvi_b->power << (rvi_b->flags & RVI_MULTIHEAD ? 1 : 0); - int r = va - vb; - - return _internal_sort_order ? -r : r; -} - -static int CDECL TrainEngineIntroDateSorter(const void *a, const void *b) -{ - int va = GetEngine(*(const EngineID*)a)->intro_date; - int vb = GetEngine(*(const EngineID*)b)->intro_date; - int r = va - vb; - - return _internal_sort_order ? -r : r; -} - -static EngineID _last_engine; // cached vehicle to hopefully speed up name-sorting - -static char _bufcache[64]; // used together with _last_vehicle to hopefully speed up stringsorting -static int CDECL TrainEngineNameSorter(const void *a, const void *b) -{ - const EngineID va = *(const EngineID*)a; - const EngineID vb = *(const EngineID*)b; - char buf1[64]; - int r; - - GetString(buf1, GetCustomEngineName(va), lastof(buf1)); - - if (vb != _last_engine) { - _last_engine = vb; - _bufcache[0] = '\0'; - - GetString(_bufcache, GetCustomEngineName(vb), lastof(_bufcache)); - } - - r = strcasecmp(buf1, _bufcache); // sort by name - - return _internal_sort_order ? -r : r; -} - -static int CDECL TrainEngineRunningCostSorter(const void *a, const void *b) -{ - const RailVehicleInfo *rvi_a = RailVehInfo(*(const EngineID*)a); - const RailVehicleInfo *rvi_b = RailVehInfo(*(const EngineID*)b); - - int va = rvi_a->running_cost_base * _price.running_rail[rvi_a->running_cost_class] * (rvi_a->flags & RVI_MULTIHEAD ? 2 : 1); - int vb = rvi_b->running_cost_base * _price.running_rail[rvi_b->running_cost_class] * (rvi_b->flags & RVI_MULTIHEAD ? 2 : 1); - int r = va - vb; - - return _internal_sort_order ? -r : r; -} - -static int CDECL TrainEnginePowerVsRunningCostSorter(const void *a, const void *b) -{ - const RailVehicleInfo *rvi_a = RailVehInfo(*(const EngineID*)a); - const RailVehicleInfo *rvi_b = RailVehInfo(*(const EngineID*)b); - - /* Here we are using a few tricks to get the right sort. - * We want power/running cost, but since we usually got higher running cost than power and we store the result in an int, - * we will actually calculate cunning cost/power (to make it more than 1). - * Because of this, the return value have to be reversed as well and we return b - a instead of a - b. - * Another thing is that both power and running costs should be doubled for multiheaded engines. - * Since it would be multipling with 2 in both numerator and denumerator, it will even themselves out and we skip checking for multiheaded. */ - int va = (rvi_a->running_cost_base * _price.running_rail[rvi_a->running_cost_class]) / max((uint16)1, rvi_a->power); - int vb = (rvi_b->running_cost_base * _price.running_rail[rvi_b->running_cost_class]) / max((uint16)1, rvi_b->power); - int r = vb - va; - - return _internal_sort_order ? -r : r; -} - -static int CDECL TrainEngineReliabilitySorter(const void *a, const void *b) -{ - int va = GetEngine(*(const EngineID*)a)->reliability; - int vb = GetEngine(*(const EngineID*)b)->reliability; - int r = va - vb; - - return _internal_sort_order ? -r : r; -} - -static EngList_SortTypeFunction * const _engine_sorter[] = { - &TrainEngineNumberSorter, - &TrainEngineCostSorter, - &TrainEngineSpeedSorter, - &TrainEnginePowerSorter, - &TrainEngineIntroDateSorter, - &TrainEngineNameSorter, - &TrainEngineRunningCostSorter, - &TrainEnginePowerVsRunningCostSorter, - &TrainEngineReliabilitySorter, -}; - -static const StringID _engine_sort_listing[] = { - STR_ENGINE_SORT_ENGINE_ID, - STR_ENGINE_SORT_COST, - STR_SORT_BY_MAX_SPEED, - STR_ENGINE_SORT_POWER, - STR_ENGINE_SORT_INTRO_DATE, - STR_SORT_BY_DROPDOWN_NAME, - STR_ENGINE_SORT_RUNNING_COST, - STR_ENGINE_SORT_POWER_VS_RUNNING_COST, - STR_SORT_BY_RELIABILITY, - INVALID_STRING_ID -}; - -/** - * Draw the purchase info details of train engine at a given location. - * @param x,y location where to draw the info - * @param engine_number the engine of which to draw the info of - */ -void DrawTrainEnginePurchaseInfo(int x, int y, uint w, EngineID engine_number) -{ - const RailVehicleInfo *rvi = RailVehInfo(engine_number); - const Engine *e = GetEngine(engine_number); - int multihead = (rvi->flags&RVI_MULTIHEAD?1:0); - YearMonthDay ymd; - ConvertDateToYMD(e->intro_date, &ymd); - - /* Purchase Cost - Engine weight */ - SetDParam(0, rvi->base_cost * (_price.build_railvehicle >> 3) >> 5); - SetDParam(1, rvi->weight << multihead); - DrawString(x,y, STR_PURCHASE_INFO_COST_WEIGHT, 0); - y += 10; - - /* Max speed - Engine power */ - SetDParam(0, rvi->max_speed); - SetDParam(1, rvi->power << multihead); - DrawString(x,y, STR_PURCHASE_INFO_SPEED_POWER, 0); - y += 10; - - /* Max tractive effort - not applicable if old acceleration or maglev */ - if (_patches.realistic_acceleration && e->railtype != RAILTYPE_MAGLEV) { - SetDParam(0, ((rvi->weight << multihead) * 10 * rvi->tractive_effort) / 256); - DrawString(x, y, STR_PURCHASE_INFO_MAX_TE, 0); - y += 10; - } - - /* Running cost */ - SetDParam(0, (rvi->running_cost_base * _price.running_rail[rvi->running_cost_class] >> 8) << multihead); - DrawString(x,y, STR_PURCHASE_INFO_RUNNINGCOST, 0); - y += 10; - - /* Powered wagons power - Powered wagons extra weight */ - if (rvi->pow_wag_power != 0) { - SetDParam(0, rvi->pow_wag_power); - SetDParam(1, rvi->pow_wag_weight); - DrawString(x,y, STR_PURCHASE_INFO_PWAGPOWER_PWAGWEIGHT, 0); - y += 10; - }; - - /* Cargo type + capacity, or N/A */ - if (rvi->capacity == 0) { - SetDParam(0, CT_INVALID); - SetDParam(2, STR_EMPTY); - } else { - SetDParam(0, rvi->cargo_type); - SetDParam(1, (rvi->capacity * (CountArticulatedParts(engine_number) + 1)) << multihead); - SetDParam(2, STR_9842_REFITTABLE); - } - DrawString(x,y, STR_PURCHASE_INFO_CAPACITY, 0); - y += 10; - - /* Design date - Life length */ - SetDParam(0, ymd.year); - SetDParam(1, e->lifelength); - DrawString(x,y, STR_PURCHASE_INFO_DESIGNED_LIFE, 0); - y += 10; - - /* Reliability */ - SetDParam(0, e->reliability * 100 >> 16); - DrawString(x,y, STR_PURCHASE_INFO_RELIABILITY, 0); - y += 10; - - /* Additional text from NewGRF */ - y += ShowAdditionalText(x, y, w, engine_number); - if (rvi->capacity > 0) y += ShowRefitOptionsList(x, y, w, engine_number); -} - -/** - * Draw the purchase info details of a train wagon at a given location. - * @param x,y location where to draw the info - * @param engine_number the engine of which to draw the info of - */ -void DrawTrainWagonPurchaseInfo(int x, int y, uint w, EngineID engine_number) -{ - const RailVehicleInfo *rvi = RailVehInfo(engine_number); - bool refittable = (EngInfo(engine_number)->refit_mask != 0); - - /* Purchase cost */ - SetDParam(0, (rvi->base_cost * _price.build_railwagon) >> 8); - DrawString(x, y, STR_PURCHASE_INFO_COST, 0); - y += 10; - - /* Wagon weight - (including cargo) */ - SetDParam(0, rvi->weight); - SetDParam(1, (_cargoc.weights[rvi->cargo_type] * rvi->capacity >> 4) + rvi->weight); - DrawString(x, y, STR_PURCHASE_INFO_WEIGHT_CWEIGHT, 0); - y += 10; - - /* Cargo type + capacity, or N/A */ - if (rvi->capacity == 0) { - SetDParam(0, CT_INVALID); - SetDParam(2, STR_EMPTY); - } else { - SetDParam(0, rvi->cargo_type); - SetDParam(1, rvi->capacity * (CountArticulatedParts(engine_number) + 1)); - SetDParam(2, refittable ? STR_9842_REFITTABLE : STR_EMPTY); - } - DrawString(x, y, STR_PURCHASE_INFO_CAPACITY, 0); - y += 10; - - /* Wagon speed limit, displayed if above zero */ - if (rvi->max_speed > 0 && _patches.wagon_speed_limits) { - SetDParam(0, rvi->max_speed); - DrawString(x,y, STR_PURCHASE_INFO_SPEED, 0); - y += 10; - } - - /* Additional text from NewGRF */ - y += ShowAdditionalText(x, y, w, engine_number); - y += ShowRefitOptionsList(x, y, w, engine_number); -} void CcBuildWagon(bool success, TileIndex tile, uint32 p1, uint32 p2) { @@ -370,215 +61,6 @@ void CcCloneTrain(bool success, TileIndex tile, uint32 p1, uint32 p2) if (success) ShowTrainViewWindow(GetVehicle(_new_vehicle_id)); } -static void engine_drawing_loop(const EngineList *engines, int x, int *y, EngineID sel, EngineID position, int16 show_max) -{ - int count = min(EngList_Count(engines), show_max); - for (; position < count; *y += 14, position++) { - EngineID id = (*engines)[position]; - DrawString(x + 59, *y + 2, GetCustomEngineName(id), sel == id ? 0xC : 0x10); - DrawTrainEngine(x + 29, *y + 6, id, GetEnginePalette(id, _local_player)); - } -} - -static void GenerateBuildList(Window *w) -{ - EngineID eid, sel_id; - int num_engines = 0; - int num_wagons = 0; - buildvehicle_d *bv = &WP(w, buildvehicle_d); - - bv->filter.railtype = (w->window_number == 0) ? RAILTYPE_END : GetRailType(w->window_number); - - EngList_RemoveAll(&bv->eng_list); - - /* Make list of all available train engines and wagons. - * Also check to see if the previously selected engine is still available, - * and if not, reset selection to INVALID_ENGINE. This could be the case - * when engines become obsolete and are removed */ - for (sel_id = INVALID_ENGINE, eid = 0; eid < NUM_TRAIN_ENGINES; eid++) { - const Engine *e = GetEngine(eid); - const RailVehicleInfo *rvi = RailVehInfo(eid); - - if (bv->filter.railtype != RAILTYPE_END && !HasPowerOnRail(e->railtype, bv->filter.railtype)) continue; - if (!IsEngineBuildable(eid, VEH_Train, _local_player)) continue; - - EngList_Add(&bv->eng_list, eid); - if ((rvi->flags & RVI_WAGON) == 0) { - num_engines++; - } else { - num_wagons++; - } - - if (eid == bv->sel_engine) sel_id = eid; - } - - bv->sel_engine = sel_id; - - // make engines first, and then wagons, sorted by ListPositionOfEngine() - _internal_sort_order = false; - EngList_Sort(&bv->eng_list, TrainEnginesThenWagonsSorter); - - // and then sort engines - _internal_sort_order = bv->descending_sort_order; - EngList_SortPartial(&bv->eng_list, _engine_sorter[bv->sort_criteria], 0, num_engines); - - // and finally sort wagons - EngList_SortPartial(&bv->eng_list, _engine_sorter[bv->sort_criteria], num_engines, num_wagons); -} - -static void DrawTrainBuildWindow(Window *w) -{ - const buildvehicle_d *bv = &WP(w, buildvehicle_d); - int x = 1; - int y = 27; - EngineID selected_id = bv->sel_engine; - int max = w->vscroll.pos + w->vscroll.cap; - - SetWindowWidgetDisabledState(w, BUILD_TRAIN_WIDGET_BUILD, w->window_number == 0); // Disable unless we got a depot to build in - - SetVScrollCount(w, EngList_Count(&bv->eng_list)); - SetDParam(0, bv->filter.railtype + STR_881C_NEW_RAIL_VEHICLES); - DrawWindowWidgets(w); - - /* Draw the engines */ - engine_drawing_loop(&bv->eng_list, x, &y, selected_id, w->vscroll.pos, max); - - if (selected_id != INVALID_ENGINE) { - const RailVehicleInfo *rvi = RailVehInfo(selected_id); - const Widget *wi = &w->widget[BUILD_TRAIN_WIDGET_PANEL]; - - if (rvi->flags & RVI_WAGON) { - DrawTrainWagonPurchaseInfo(2, wi->top + 1, wi->right - wi->left - 2, selected_id); - } else { - DrawTrainEnginePurchaseInfo(2, wi->top + 1, wi->right - wi->left - 2, selected_id); - } - } - DrawString(85, 15, _engine_sort_listing[bv->sort_criteria], 0x10); - DoDrawString(bv->descending_sort_order ? DOWNARROW : UPARROW, 69, 15, 0x10); -} - -static void NewRailVehicleWndProc(Window *w, WindowEvent *e) -{ - buildvehicle_d *bv = &WP(w, buildvehicle_d); - switch (e->event) { - case WE_CREATE: - EngList_Create(&bv->eng_list); - bv->sel_engine = INVALID_ENGINE; - bv->sort_criteria = _last_sort_criteria; - bv->descending_sort_order = _last_sort_order; - GenerateBuildList(w); - /* Select the first engine in the list as default when opening the window */ - if (EngList_Count(&bv->eng_list) > 0) bv->sel_engine = bv->eng_list[0]; - break; - - case WE_INVALIDATE_DATA: - GenerateBuildList(w); - break; - - case WE_DESTROY: - EngList_Destroy(&bv->eng_list); - break; - - case WE_PAINT: - DrawTrainBuildWindow(w); - break; - - case WE_CLICK: { - switch (e->we.click.widget) { - case BUILD_TRAIN_WIDGET_SORT_ASCENDING_DESCENDING: - bv->descending_sort_order ^= true; - _last_sort_order = bv->descending_sort_order; - GenerateBuildList(w); - SetWindowDirty(w); - break; - - case BUILD_TRAIN_WIDGET_SORT_TEXT: case BUILD_TRAIN_WIDGET_SORT_DROPDOWN:/* Select sorting criteria dropdown menu */ - ShowDropDownMenu(w, _engine_sort_listing, bv->sort_criteria, BUILD_TRAIN_WIDGET_SORT_DROPDOWN, 0, 0); - return; - - case BUILD_TRAIN_WIDGET_LIST: { - uint i = ((e->we.click.pt.y - 26) / 14) + w->vscroll.pos; - uint num_items = EngList_Count(&bv->eng_list); - bv->sel_engine = (i < num_items) ? bv->eng_list[i] : INVALID_ENGINE; - SetWindowDirty(w); - break; - } - - case BUILD_TRAIN_WIDGET_BUILD: { - EngineID sel_eng = bv->sel_engine; - if (sel_eng != INVALID_ENGINE) - DoCommandP(w->window_number, sel_eng, 0, (RailVehInfo(sel_eng)->flags & RVI_WAGON) ? CcBuildWagon : CcBuildLoco, CMD_BUILD_RAIL_VEHICLE | CMD_MSG(STR_882B_CAN_T_BUILD_RAILROAD_VEHICLE)); - break; - } - - case BUILD_TRAIN_WIDGET_RENAME: { - EngineID sel_eng = bv->sel_engine; - if (sel_eng != INVALID_ENGINE) { - bv->rename_engine = sel_eng; - ShowQueryString(GetCustomEngineName(sel_eng), STR_886A_RENAME_TRAIN_VEHICLE_TYPE, 31, 160, w, CS_ALPHANUMERAL); - } - break; - } - } - } - break; - - case WE_ON_EDIT_TEXT: { - if (e->we.edittext.str[0] != '\0') { - _cmd_text = e->we.edittext.str; - DoCommandP(0, bv->rename_engine, 0, NULL, CMD_RENAME_ENGINE | CMD_MSG(STR_886B_CAN_T_RENAME_TRAIN_VEHICLE)); - } - break; - } - - case WE_DROPDOWN_SELECT: /* we have selected a dropdown item in the list */ - if (bv->sort_criteria != e->we.dropdown.index) { - bv->sort_criteria = _last_sort_criteria = e->we.dropdown.index; - GenerateBuildList(w); - SetWindowDirty(w); - } - break; - - case WE_RESIZE: { - if (e->we.sizing.diff.y == 0) break; - - w->vscroll.cap += e->we.sizing.diff.y / 14; - w->widget[BUILD_TRAIN_WIDGET_LIST].data = (w->vscroll.cap << 8) + 1; - break; - } - } -} - -static const WindowDesc _new_rail_vehicle_desc = { - WDP_AUTO, WDP_AUTO, 228, 252, - WC_BUILD_VEHICLE,0, - WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_RESIZABLE, - _new_rail_vehicle_widgets, - NewRailVehicleWndProc -}; - -void ShowBuildTrainWindow(TileIndex tile) -{ - Window *w; - - DeleteWindowById(WC_BUILD_VEHICLE, tile); - - w = AllocateWindowDescFront(&_new_rail_vehicle_desc, tile); - w->vscroll.cap = 8; - w->widget[BUILD_TRAIN_WIDGET_LIST].data = (w->vscroll.cap << 8) + 1; - - w->resize.step_height = 14; - w->resize.height = w->height - 14 * 4; // Minimum of 4 vehicles in the display - - if (tile != 0) { - w->caption_color = GetTileOwner(tile); - WP(w,buildvehicle_d).filter.railtype = GetRailType(tile); - } else { - w->caption_color = _local_player; - WP(w,buildvehicle_d).filter.railtype = RAILTYPE_END; - } -} - /** * Get the number of pixels for the given wagon length. * @param len Length measured in 1/8ths of a standard wagon. diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index 97f4de633c..9b6c3c7254 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -1045,20 +1045,15 @@ static void DrawEngineArrayInReplaceWindow(Window *w, int x, int y, int x2, int } } -static void DrawVehiclePurchaseInfo(const int x, const int y, uint w, const EngineID engine_number) +static void DrawVehiclePurchaseInfoLocal(const int x, const int y, uint w, const EngineID engine_number) { switch (GetEngine(engine_number)->type) { - case VEH_Train: - if ((RailVehInfo(engine_number)->flags & RVI_WAGON) == 0) { - DrawTrainEnginePurchaseInfo(x, y, w, engine_number); - } else { - DrawTrainWagonPurchaseInfo(x, y, w, engine_number); - } - break; - case VEH_Road: DrawRoadVehPurchaseInfo(x, y, w, engine_number); break; case VEH_Ship: DrawShipPurchaseInfo(x, y, w, engine_number); break; - case VEH_Aircraft: DrawAircraftPurchaseInfo(x, y, w, engine_number); break; + case VEH_Train: + case VEH_Aircraft: + DrawVehiclePurchaseInfo(x, y, w, engine_number); + break; default: NOT_REACHED(); } } @@ -1146,7 +1141,7 @@ static void ReplaceVehicleWndProc(Window *w, WindowEvent *e) for (i = 0 ; i < 2 ; i++) { if (selected_id[i] != INVALID_ENGINE) { const Widget *wi = &w->widget[i == 0 ? 3 : 11]; - DrawVehiclePurchaseInfo(wi->left + 2 , wi->top + 1, wi->right - wi->left - 2, selected_id[i]); + DrawVehiclePurchaseInfoLocal(wi->left + 2 , wi->top + 1, wi->right - wi->left - 2, selected_id[i]); } } } break; // end of paint @@ -1743,9 +1738,9 @@ void PlayerVehWndProc(Window *w, WindowEvent *e) case VLW_WIDGET_NEW_VEHICLES: switch (vl->vehicle_type) { - case VEH_Train: ShowBuildTrainWindow(0); break; case VEH_Road: ShowBuildRoadVehWindow(0); break; case VEH_Ship: ShowBuildShipWindow(0); break; + case VEH_Train: case VEH_Aircraft: ShowBuildVehicleWindow(0, vl->vehicle_type); break; } break; diff --git a/src/vehicle_gui.h b/src/vehicle_gui.h index a3d20e4f7e..77d067db1d 100644 --- a/src/vehicle_gui.h +++ b/src/vehicle_gui.h @@ -32,10 +32,8 @@ static inline bool ValidVLWFlags(uint16 flags) void PlayerVehWndProc(Window *w, WindowEvent *e); -void DrawTrainEnginePurchaseInfo(int x, int y, uint w, EngineID engine_number); -void DrawTrainWagonPurchaseInfo(int x, int y, uint w, EngineID engine_number); +void DrawVehiclePurchaseInfo(int x, int y, uint w, EngineID engine_number); void DrawRoadVehPurchaseInfo(int x, int y, uint w, EngineID engine_number); -void DrawAircraftPurchaseInfo(int x, int y, uint w, EngineID engine_number); void DrawShipPurchaseInfo(int x, int y, uint w, EngineID engine_number); void DrawTrainImage(const Vehicle *v, int x, int y, int count, int skip, VehicleID selection); @@ -43,7 +41,6 @@ void DrawRoadVehImage(const Vehicle *v, int x, int y, VehicleID selection); void DrawShipImage(const Vehicle *v, int x, int y, VehicleID selection); void DrawAircraftImage(const Vehicle *v, int x, int y, VehicleID selection); -void ShowBuildTrainWindow(TileIndex tile); void ShowBuildRoadVehWindow(TileIndex tile); void ShowBuildShipWindow(TileIndex tile); void ShowBuildVehicleWindow(TileIndex tile, byte type);