From 802a31cdb08b31839b8404d3f4c71e4098a048c3 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Tue, 27 Jan 2026 22:29:29 +0000 Subject: [PATCH] Change #13915: Share one window for engine preview pop ups. (#14703) If more than one engine preview occurs at the same time, previous/next buttons allow the player to switch between engines. --- src/engine_gui.cpp | 190 ++++++++++++++++++++++++++++++------ src/lang/english.txt | 8 ++ src/widgets/engine_widget.h | 4 + src/window_gui.h | 1 + 4 files changed, 175 insertions(+), 28 deletions(-) diff --git a/src/engine_gui.cpp b/src/engine_gui.cpp index dcf3955a96..ea5c8224f6 100644 --- a/src/engine_gui.cpp +++ b/src/engine_gui.cpp @@ -8,6 +8,7 @@ /** @file engine_gui.cpp GUI to show engine related information. */ #include "stdafx.h" +#include "dropdown_func.h" #include "window_gui.h" #include "engine_base.h" #include "command_func.h" @@ -55,7 +56,7 @@ StringID GetEngineCategoryName(EngineID engine) static constexpr std::initializer_list _nested_engine_preview_widgets = { NWidget(NWID_HORIZONTAL), NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE), - NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetStringTip(STR_ENGINE_PREVIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS), + NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE, WID_EP_CAPTION), SetToolTip(STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS), EndContainer(), NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE), NWidget(NWID_VERTICAL), SetPIP(0, WidgetDimensions::unscaled.vsep_wide, 0), SetPadding(WidgetDimensions::unscaled.modalpopup), @@ -66,49 +67,105 @@ static constexpr std::initializer_list _nested_engine_preview_widge EndContainer(), EndContainer(), EndContainer(), + NWidget(NWID_HORIZONTAL), + NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_EP_PREV), SetStringTip(STR_ENGINE_PREVIEW_PREVIOUS, STR_ENGINE_PREVIEW_PREVIOUS_TOOLTIP), SetFill(1, 0), + NWidget(WWT_DROPDOWN, COLOUR_LIGHT_BLUE, WID_EP_LIST), SetToolTip(STR_ENGINE_PREVIEW_ENGINE_LIST_TOOLTIP), SetFill(1, 0), + NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_EP_NEXT), SetStringTip(STR_ENGINE_PREVIEW_NEXT, STR_ENGINE_PREVIEW_NEXT_TOOLTIP), SetFill(1, 0), + EndContainer(), }; struct EnginePreviewWindow : Window { - int vehicle_space = 0; // The space to show the vehicle image + int vehicle_space = 0; ///< The space to show the vehicle image + size_t selected_index = 0; ///< The currently displayed index in the list of engines. + std::vector engines; ///< List of engine IDs to display preview news for. - EnginePreviewWindow(WindowDesc &desc, WindowNumber window_number) : Window(desc) + /** + * Construct a new Engine Preview window. + * @param desc Window description. + * @param engine Initial engine to display. + */ + EnginePreviewWindow(WindowDesc &desc, EngineID engine) : Window(desc) { - this->InitNested(window_number); + this->engines.push_back(engine); + + this->InitNested(); /* There is no way to recover the window; so disallow closure via DEL; unless SHIFT+DEL */ this->flags.Set(WindowFlag::Sticky); } + std::string GetWidgetString(WidgetID widget, StringID stringid) const override + { + if (widget == WID_EP_CAPTION) { + if (this->engines.size() <= 1) return GetString(STR_ENGINE_PREVIEW_CAPTION); + return GetString(STR_ENGINE_PREVIEW_CAPTION_COUNT, this->selected_index + 1, this->engines.size()); + } + + if (widget == WID_EP_LIST) { + return this->selected_index < this->engines.size() ? GetString(STR_ENGINE_PREVIEW_ENGINE_LIST, this->selected_index + 1, this->engines[this->selected_index]) : GetString(STR_INVALID_VEHICLE); + } + + return this->Window::GetWidgetString(widget, stringid); + } + void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override { - if (widget != WID_EP_QUESTION) return; + switch (widget) { + case WID_EP_QUESTION: { + /* Get size of engine sprite, on loan from depot_gui.cpp */ + EngineImageType image_type = EIT_PREVIEW; - /* Get size of engine sprite, on loan from depot_gui.cpp */ - EngineID engine = static_cast(this->window_number); - EngineImageType image_type = EIT_PREVIEW; - uint x, y; - int x_offs, y_offs; + /* First determine required the horizontal size. */ + this->vehicle_space = ScaleSpriteTrad(40); + for (const EngineID &engine : this->engines) { + uint x, y; + int x_offs, y_offs; - const Engine *e = Engine::Get(engine); - switch (e->type) { - default: NOT_REACHED(); - case VEH_TRAIN: GetTrainSpriteSize( engine, x, y, x_offs, y_offs, image_type); break; - case VEH_ROAD: GetRoadVehSpriteSize( engine, x, y, x_offs, y_offs, image_type); break; - case VEH_SHIP: GetShipSpriteSize( engine, x, y, x_offs, y_offs, image_type); break; - case VEH_AIRCRAFT: GetAircraftSpriteSize(engine, x, y, x_offs, y_offs, image_type); break; + const Engine *e = Engine::Get(engine); + switch (e->type) { + default: NOT_REACHED(); + case VEH_TRAIN: GetTrainSpriteSize( engine, x, y, x_offs, y_offs, image_type); break; + case VEH_ROAD: GetRoadVehSpriteSize( engine, x, y, x_offs, y_offs, image_type); break; + case VEH_SHIP: GetShipSpriteSize( engine, x, y, x_offs, y_offs, image_type); break; + case VEH_AIRCRAFT: GetAircraftSpriteSize(engine, x, y, x_offs, y_offs, image_type); break; + } + + this->vehicle_space = std::max(this->vehicle_space, y - y_offs); + size.width = std::max(size.width, x + std::abs(x_offs)); + } + + /* Then account for the description of each vehicle. */ + int height = 0; + for (const EngineID &engine : this->engines) { + int title_height = GetStringHeight(GetString(STR_ENGINE_PREVIEW_MESSAGE, GetEngineCategoryName(engine)), size.width); + int body_height = GetStringHeight(GetEngineInfoString(engine), size.width); + height = std::max(height, title_height + WidgetDimensions::scaled.vsep_wide + GetCharacterHeight(FS_NORMAL) + this->vehicle_space + body_height); + } + + size.height = height; + break; + } + + case WID_EP_LIST: { + size.width = 0; + int index = 0; + for (const EngineID &engine : this->engines) { + size.width = std::max(size.width, GetStringBoundingBox(GetString(STR_ENGINE_PREVIEW_ENGINE_LIST, index + 1, PackEngineNameDParam(engine, EngineNameContext::PreviewNews))).width); + ++index; + } + size.width += padding.width; + break; + } } - this->vehicle_space = std::max(ScaleSpriteTrad(40), y - y_offs); - - size.width = std::max(size.width, x + std::abs(x_offs)); - size.height = GetStringHeight(GetString(STR_ENGINE_PREVIEW_MESSAGE, GetEngineCategoryName(engine)), size.width) + WidgetDimensions::scaled.vsep_wide + GetCharacterHeight(FS_NORMAL) + this->vehicle_space; - size.height += GetStringHeight(GetEngineInfoString(engine), size.width); } void DrawWidget(const Rect &r, WidgetID widget) const override { if (widget != WID_EP_QUESTION) return; - EngineID engine = static_cast(this->window_number); + if (this->selected_index >= this->engines.size()) return; + + EngineID engine = this->engines[selected_index]; int y = DrawStringMultiLine(r, GetString(STR_ENGINE_PREVIEW_MESSAGE, GetEngineCategoryName(engine)), TC_FROMSTRING, SA_HOR_CENTER | SA_TOP) + WidgetDimensions::scaled.vsep_wide; DrawString(r.left, r.right, y, GetString(STR_ENGINE_NAME, PackEngineNameDParam(engine, EngineNameContext::PreviewNews)), TC_BLACK, SA_HOR_CENTER); @@ -124,20 +181,92 @@ struct EnginePreviewWindow : Window { { switch (widget) { case WID_EP_YES: - Command::Post(static_cast(this->window_number)); + if (this->selected_index < this->engines.size()) { + Command::Post(this->engines[this->selected_index]); + } [[fallthrough]]; + case WID_EP_NO: - if (!_shift_pressed) this->Close(); + if (!_shift_pressed) { + this->engines.erase(this->engines.begin() + this->selected_index); + this->InvalidateData(); + } + break; + + case WID_EP_PREV: + this->selected_index = (this->selected_index + this->engines.size() - 1) % this->engines.size(); + this->SetDirty(); + break; + + case WID_EP_NEXT: + this->selected_index = (this->selected_index + 1) % this->engines.size(); + this->SetDirty(); + break; + + case WID_EP_LIST: + ShowDropDownList(this, this->BuildDropdownList(), static_cast(this->selected_index), widget); break; } } + void OnDropdownSelect(WidgetID widget, int index, int) override + { + if (widget != WID_EP_LIST) return; + this->selected_index = index % this->engines.size(); + this->SetDirty(); + } + + /** + * Build the dropdown list of new engines. + * @return The dropdown list. + */ + DropDownList BuildDropdownList() + { + DropDownList list; + + int index = 0; + for (const EngineID &engine : this->engines) { + list.push_back(MakeDropDownListStringItem(GetString(STR_ENGINE_PREVIEW_ENGINE_LIST, index + 1, PackEngineNameDParam(engine, EngineNameContext::PreviewNews)), index, false, false)); + ++index; + } + + return list; + } + void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override { if (!gui_scope) return; - EngineID engine = static_cast(this->window_number); - if (Engine::Get(engine)->preview_company != _local_company) this->Close(); + /* Remove engines that are no longer eligible for preview. */ + for (auto it = this->engines.begin(); it != this->engines.end(); /* nothing */) { + if (Engine::Get(*it)->preview_company != _local_company) { + it = this->engines.erase(it); + } else { + ++it; + } + } + + /* If no engines are remaining, close the window. */ + if (this->engines.empty()) this->Close(); + + /* Ensure selection is valid. */ + if (this->selected_index >= this->engines.size()) this->selected_index = this->engines.size() - 1; + + this->SetWidgetsDisabledState(this->engines.size() <= 1, WID_EP_PREV, WID_EP_LIST, WID_EP_NEXT); + } + + /** + * Adds another engine to the engine preview window. + * @param engine Engine ID to add. + */ + void AddEngineToPreview(EngineID engine) + { + if (std::ranges::find_if(this->engines, [engine](const EngineID &e) { return e == engine; }) != std::end(this->engines)) return; + + this->engines.push_back(engine); + + this->InvalidateData(); + this->ReInit(); } }; @@ -151,7 +280,12 @@ static WindowDesc _engine_preview_desc( void ShowEnginePreviewWindow(EngineID engine) { - AllocateWindowDescFront(_engine_preview_desc, engine); + EnginePreviewWindow *w = dynamic_cast(FindWindowByClass(WC_ENGINE_PREVIEW)); + if (w == nullptr) { + new EnginePreviewWindow(_engine_preview_desc, engine); + } else { + w->AddEngineToPreview(engine); + } } /** diff --git a/src/lang/english.txt b/src/lang/english.txt index 41f22bda19..57544b773e 100644 --- a/src/lang/english.txt +++ b/src/lang/english.txt @@ -4390,8 +4390,16 @@ STR_DEPOT_SELL_CONFIRMATION_TEXT :{YELLOW}You are # Engine preview window STR_ENGINE_PREVIEW_CAPTION :{WHITE}Message from vehicle manufacturer +STR_ENGINE_PREVIEW_CAPTION_COUNT :{WHITE}Message from vehicle manufacturer ({COMMA} of {COMMA}) STR_ENGINE_PREVIEW_MESSAGE :{GOLD}We have just designed a new {STRING} - would you be interested in a year's exclusive use of this vehicle, so we can see how it performs before making it universally available? +STR_ENGINE_PREVIEW_PREVIOUS :Previous +STR_ENGINE_PREVIEW_PREVIOUS_TOOLTIP :Click to step to the previous new vehicle +STR_ENGINE_PREVIEW_NEXT :Next +STR_ENGINE_PREVIEW_NEXT_TOOLTIP :Click to step to the next new vehicle +STR_ENGINE_PREVIEW_ENGINE_LIST :{COMMA}: {ENGINE} +STR_ENGINE_PREVIEW_ENGINE_LIST_TOOLTIP :Click to jump to a new vehicle + STR_ENGINE_PREVIEW_RAILROAD_LOCOMOTIVE :railway locomotive STR_ENGINE_PREVIEW_ELRAIL_LOCOMOTIVE :electrified railway locomotive STR_ENGINE_PREVIEW_MONORAIL_LOCOMOTIVE :monorail locomotive diff --git a/src/widgets/engine_widget.h b/src/widgets/engine_widget.h index 0f5b347768..f4e5ad5c3f 100644 --- a/src/widgets/engine_widget.h +++ b/src/widgets/engine_widget.h @@ -12,9 +12,13 @@ /** Widgets of the #EnginePreviewWindow class. */ enum EnginePreviewWidgets : WidgetID { + WID_EP_CAPTION, ///< The caption for the question. WID_EP_QUESTION, ///< The container for the question. WID_EP_NO, ///< No button. WID_EP_YES, ///< Yes button. + WID_EP_PREV, ///< Previous button. + WID_EP_LIST, ///< Dropdown menu to jump to entries. + WID_EP_NEXT, ///< Next button. }; #endif /* WIDGETS_ENGINE_WIDGET_H */ diff --git a/src/window_gui.h b/src/window_gui.h index 934df38052..20481360ee 100644 --- a/src/window_gui.h +++ b/src/window_gui.h @@ -766,6 +766,7 @@ public: * A dropdown option associated to this window has been selected. * @param widget the widget (button) that the dropdown is associated with. * @param index the element in the dropdown that is selected. + * @param click_result dropdown element specific result data. */ virtual void OnDropdownSelect([[maybe_unused]] WidgetID widget, [[maybe_unused]] int index, [[maybe_unused]] int click_result) {}