diff --git a/distribution/openrct2.d.ts b/distribution/openrct2.d.ts index 02fee35be1..a78b2ce1b5 100644 --- a/distribution/openrct2.d.ts +++ b/distribution/openrct2.d.ts @@ -1377,6 +1377,7 @@ declare global { */ border?: boolean; image: number; + isPressed: boolean; text: string; onClick: () => void; } diff --git a/src/openrct2-ui/scripting/CustomWindow.cpp b/src/openrct2-ui/scripting/CustomWindow.cpp index 4a1c2b66fb..6b00b7fc91 100644 --- a/src/openrct2-ui/scripting/CustomWindow.cpp +++ b/src/openrct2-ui/scripting/CustomWindow.cpp @@ -112,6 +112,7 @@ namespace OpenRCT2::Ui::Windows int32_t SelectedIndex{}; bool IsChecked{}; bool IsDisabled{}; + bool IsPressed{}; bool HasBorder{}; bool ShowColumnHeaders{}; bool IsStriped{}; @@ -149,16 +150,13 @@ namespace OpenRCT2::Ui::Windows result.Text = ProcessString(desc["text"]); result.HasBorder = true; } + result.IsPressed = AsOrDefault(desc["isPressed"], false); result.OnClick = desc["onClick"]; } else if (result.Type == "checkbox") { result.Text = ProcessString(desc["text"]); - auto dukIsChecked = desc["isChecked"]; - if (dukIsChecked.type() == DukValue::Type::BOOLEAN) - { - result.IsChecked = dukIsChecked.as_bool(); - } + result.IsChecked = AsOrDefault(desc["isChecked"], false); result.OnChange = desc["onChange"]; } else if (result.Type == "dropdown") @@ -821,6 +819,10 @@ namespace OpenRCT2::Ui::Windows widget.string = const_cast(desc.Text.c_str()); widget.flags |= WIDGET_FLAGS::TEXT_IS_STRING; } + if (desc.IsPressed) + { + widget.flags |= WIDGET_FLAGS::IS_PRESSED; + } widgetList.push_back(widget); } else if (desc.Type == "checkbox") diff --git a/src/openrct2-ui/scripting/ScWidget.hpp b/src/openrct2-ui/scripting/ScWidget.hpp index 9fe2ae4889..4b40796208 100644 --- a/src/openrct2-ui/scripting/ScWidget.hpp +++ b/src/openrct2-ui/scripting/ScWidget.hpp @@ -212,27 +212,6 @@ namespace OpenRCT2::Scripting } } - uint32_t image_get() const - { - if (IsCustomWindow()) - { - auto widget = GetWidget(); - if (widget != nullptr && widget->type == WWT_FLATBTN) - { - return widget->image; - } - } - return 0; - } - void image_set(uint32_t value) - { - auto widget = GetWidget(); - if (widget != nullptr && widget->type == WWT_FLATBTN) - { - widget->image = value; - } - } - std::string text_get() const { if (IsCustomWindow()) @@ -281,7 +260,6 @@ namespace OpenRCT2::Scripting dukglue_register_property(ctx, &ScWidget::isDisabled_get, &ScWidget::isDisabled_set, "isDisabled"); // No so common - dukglue_register_property(ctx, &ScWidget::image_get, &ScWidget::image_set, "image"); dukglue_register_property(ctx, &ScWidget::text_get, &ScWidget::text_set, "text"); dukglue_register_property(ctx, &ScWidget::viewport_get, nullptr, "viewport"); } @@ -321,6 +299,84 @@ namespace OpenRCT2::Scripting } }; + class ScButtonWidget : public ScWidget + { + public: + ScButtonWidget(rct_windowclass c, rct_windownumber n, rct_widgetindex widgetIndex) + : ScWidget(c, n, widgetIndex) + { + } + + static void Register(duk_context* ctx) + { + dukglue_set_base_class(ctx); + dukglue_register_property(ctx, &ScButtonWidget::border_get, &ScButtonWidget::border_set, "border"); + dukglue_register_property(ctx, &ScButtonWidget::isPressed_get, &ScButtonWidget::isPressed_set, "isPressed"); + dukglue_register_property(ctx, &ScButtonWidget::image_get, &ScButtonWidget::image_set, "image"); + } + + private: + bool border_get() const + { + auto widget = GetWidget(); + if (widget != nullptr) + { + return widget->type == WWT_IMGBTN; + } + return false; + } + void border_set(bool value) + { + auto widget = GetWidget(); + if (widget != nullptr && (widget->type == WWT_FLATBTN || widget->type == WWT_IMGBTN)) + { + if (value) + widget->type = WWT_IMGBTN; + else + widget->type = WWT_FLATBTN; + Invalidate(); + } + } + + bool isPressed_get() const + { + auto w = GetWindow(); + if (w != nullptr) + { + return widget_is_pressed(w, _widgetIndex); + } + return false; + } + void isPressed_set(bool value) + { + auto w = GetWindow(); + if (w != nullptr) + { + widget_set_checkbox_value(w, _widgetIndex, value ? 1 : 0); + Invalidate(); + } + } + + uint32_t image_get() const + { + auto widget = GetWidget(); + if (widget != nullptr && widget->type == WWT_FLATBTN) + { + return widget->image; + } + return 0; + } + void image_set(uint32_t value) + { + auto widget = GetWidget(); + if (widget != nullptr && widget->type == WWT_FLATBTN) + { + widget->image = value; + Invalidate(); + } + } + }; + class ScCheckBoxWidget : public ScWidget { public: @@ -351,6 +407,7 @@ namespace OpenRCT2::Scripting if (w != nullptr) { widget_set_checkbox_value(w, _widgetIndex, value ? 1 : 0); + Invalidate(); } } }; @@ -608,6 +665,10 @@ namespace OpenRCT2::Scripting auto n = w->number; switch (widget.type) { + case WWT_BUTTON: + case WWT_FLATBTN: + case WWT_IMGBTN: + return GetObjectAsDukValue(ctx, std::make_shared(c, n, widgetIndex)); case WWT_CHECKBOX: return GetObjectAsDukValue(ctx, std::make_shared(c, n, widgetIndex)); case WWT_DROPDOWN: diff --git a/src/openrct2-ui/scripting/UiExtensions.cpp b/src/openrct2-ui/scripting/UiExtensions.cpp index 6c7ffcfdd1..fed8810bc7 100644 --- a/src/openrct2-ui/scripting/UiExtensions.cpp +++ b/src/openrct2-ui/scripting/UiExtensions.cpp @@ -32,6 +32,7 @@ void UiScriptExtensions::Extend(ScriptEngine& scriptEngine) ScUi::Register(ctx); ScViewport::Register(ctx); ScWidget::Register(ctx); + ScButtonWidget::Register(ctx); ScCheckBoxWidget::Register(ctx); ScDropdownWidget::Register(ctx); ScListViewWidget::Register(ctx);