From e27a20f800b12f6dd593ddf73d85ee03228d095b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Severin=20Paul=20H=C3=B6fer?= <84280965+zzril@users.noreply.github.com> Date: Sat, 20 Jan 2024 23:38:49 +0100 Subject: [PATCH] Sort entries in entrance style dropdown --- src/openrct2-ui/windows/Ride.cpp | 106 +++++++++++++++++++------------ 1 file changed, 64 insertions(+), 42 deletions(-) diff --git a/src/openrct2-ui/windows/Ride.cpp b/src/openrct2-ui/windows/Ride.cpp index d7099ad689..c80bbb577c 100644 --- a/src/openrct2-ui/windows/Ride.cpp +++ b/src/openrct2-ui/windows/Ride.cpp @@ -49,6 +49,7 @@ #include #include #include +#include #include #include #include @@ -619,6 +620,14 @@ struct VehicleTypeLabel const char* label_string; }; +// Used for sorting the entrance type dropdown. +struct EntranceTypeLabel +{ + ObjectEntryIndex ObjIndex; + StringId Name; + uint8_t StationStyle; +}; + class RideWindow final : public Window { int16_t _viewIndex; @@ -630,6 +639,7 @@ class RideWindow final : public Window std::vector _vehicleDropdownData; int16_t _vehicleIndex = 0; uint16_t _rideColour = 0; + std::vector _entranceStyleDropdownData; bool _autoScrollGraph = true; public: @@ -1989,6 +1999,48 @@ private: Dropdown::SetChecked(pos, true); } + void PopulateEntranceStyleDropdown() + { + _entranceStyleDropdownData.clear(); + + auto& objManager = GetContext()->GetObjectManager(); + + for (ObjectEntryIndex i = 0; i < MAX_STATION_OBJECTS; i++) + { + auto stationObj = static_cast(objManager.GetLoadedObject(ObjectType::Station, i)); + if (stationObj != nullptr) + { + _entranceStyleDropdownData.push_back( + { i, stationObj->NameStringId, GetStationStyleFromIdentifier(stationObj->GetIdentifier()) }); + } + } + + std::stable_sort( + _entranceStyleDropdownData.begin(), _entranceStyleDropdownData.end(), + [](const EntranceTypeLabel& a, const EntranceTypeLabel& b) { return a.StationStyle < b.StationStyle; }); + } + + void ShowEntranceStyleDropdown() + { + auto dropdownWidget = &widgets[WIDX_ENTRANCE_STYLE_DROPDOWN] - 1; + auto ride = GetRide(rideId); + + PopulateEntranceStyleDropdown(); + + for (size_t i = 0; i < _entranceStyleDropdownData.size(); i++) + { + gDropdownItems[i].Args = _entranceStyleDropdownData[i].Name; + gDropdownItems[i].Format = _entranceStyleDropdownData[i].ObjIndex == ride->entrance_style + ? STR_DROPDOWN_MENU_LABEL_SELECTED + : STR_DROPDOWN_MENU_LABEL; + } + + WindowDropdownShowTextCustomWidth( + { windowPos.x + dropdownWidget->left, windowPos.y + dropdownWidget->top }, dropdownWidget->height() + 1, colours[1], + 0, Dropdown::Flag::StayOpen, _entranceStyleDropdownData.size(), + widgets[WIDX_ENTRANCE_STYLE_DROPDOWN].right - dropdownWidget->left); + } + void MainOnMouseDown(WidgetIndex widgetIndex) { switch (widgetIndex) @@ -4152,29 +4204,8 @@ private: Dropdown::SetChecked(ride->track_colour[colourSchemeIndex].supports, true); break; case WIDX_ENTRANCE_STYLE_DROPDOWN: - { - auto ddIndex = 0; - auto& objManager = GetContext()->GetObjectManager(); - for (i = 0; i < MAX_STATION_OBJECTS; i++) - { - auto stationObj = static_cast(objManager.GetLoadedObject(ObjectType::Station, i)); - if (stationObj != nullptr) - { - gDropdownItems[ddIndex].Format = STR_DROPDOWN_MENU_LABEL; - gDropdownItems[ddIndex].Args = stationObj->NameStringId; - if (ride->entrance_style == i) - { - gDropdownItems[ddIndex].Format = STR_DROPDOWN_MENU_LABEL_SELECTED; - } - ddIndex++; - } - } - - WindowDropdownShowTextCustomWidth( - { windowPos.x + dropdownWidget->left, windowPos.y + dropdownWidget->top }, dropdownWidget->height() + 1, - colours[1], 0, Dropdown::Flag::StayOpen, ddIndex, widgets[widgetIndex].right - dropdownWidget->left); + ShowEntranceStyleDropdown(); break; - } case WIDX_VEHICLE_COLOUR_SCHEME_DROPDOWN: for (i = 0; i < 3; i++) { @@ -4271,28 +4302,19 @@ private: break; case WIDX_ENTRANCE_STYLE_DROPDOWN: { - auto ddIndex = 0; - auto& objManager = GetContext()->GetObjectManager(); - for (auto i = 0; i < MAX_STATION_OBJECTS; i++) + if (static_cast(dropdownIndex) >= _entranceStyleDropdownData.size()) { - auto stationObj = static_cast(objManager.GetLoadedObject(ObjectType::Station, i)); - if (stationObj != nullptr) - { - if (ddIndex == dropdownIndex) - { - auto rideSetAppearanceAction = RideSetAppearanceAction( - rideId, RideSetAppearanceType::EntranceStyle, ddIndex, 0); - rideSetAppearanceAction.SetCallback([ddIndex](const GameAction*, const GameActions::Result* res) { - if (res->Error != GameActions::Status::Ok) - return; - gLastEntranceStyle = ddIndex; - }); - GameActions::Execute(&rideSetAppearanceAction); - break; - } - ddIndex++; - } + break; } + auto objIndex = _entranceStyleDropdownData[dropdownIndex].ObjIndex; + auto rideSetAppearanceAction = RideSetAppearanceAction( + rideId, RideSetAppearanceType::EntranceStyle, objIndex, 0); + rideSetAppearanceAction.SetCallback([objIndex](const GameAction*, const GameActions::Result* res) { + if (res->Error != GameActions::Status::Ok) + return; + gLastEntranceStyle = objIndex; + }); + GameActions::Execute(&rideSetAppearanceAction); break; } case WIDX_VEHICLE_COLOUR_SCHEME_DROPDOWN: