From faf59598e565c262452d8a2823ff5ecda74cac96 Mon Sep 17 00:00:00 2001 From: Ted John Date: Mon, 4 May 2020 00:05:56 +0100 Subject: [PATCH] Start working on exposing ListViewWidget --- src/openrct2-ui/scripting/ScWidget.hpp | 100 ++++++++++++++++----- src/openrct2-ui/scripting/ScWindow.hpp | 15 ++-- src/openrct2-ui/scripting/UiExtensions.cpp | 2 + 3 files changed, 90 insertions(+), 27 deletions(-) diff --git a/src/openrct2-ui/scripting/ScWidget.hpp b/src/openrct2-ui/scripting/ScWidget.hpp index ee112cfbed..daeee14751 100644 --- a/src/openrct2-ui/scripting/ScWidget.hpp +++ b/src/openrct2-ui/scripting/ScWidget.hpp @@ -26,7 +26,7 @@ namespace OpenRCT2::Scripting { class ScWidget { - private: + protected: rct_windowclass _class{}; rct_windownumber _number{}; rct_widgetindex _widgetIndex{}; @@ -39,6 +39,8 @@ namespace OpenRCT2::Scripting { } + static DukValue ToDuk(duk_context* ctx, rct_window* w, rct_widgetindex widgetIndex); + private: std::string type_get() const { @@ -190,24 +192,6 @@ namespace OpenRCT2::Scripting } } - bool isChecked_get() const - { - auto w = GetWindow(); - if (w != nullptr) - { - return widget_is_pressed(w, _widgetIndex); - } - return false; - } - void isChecked_set(bool value) - { - auto w = GetWindow(); - if (w != nullptr) - { - widget_set_checkbox_value(w, _widgetIndex, value ? 1 : 0); - } - } - uint32_t image_get() const { if (IsCustomWindow()) @@ -278,11 +262,10 @@ namespace OpenRCT2::Scripting // 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::isChecked_get, &ScWidget::isChecked_set, "isChecked"); dukglue_register_property(ctx, &ScWidget::viewport_get, nullptr, "viewport"); } - private: + protected: rct_window* GetWindow() const { if (_class == WC_MAIN_WINDOW) @@ -316,6 +299,81 @@ namespace OpenRCT2::Scripting widget_invalidate_by_number(_class, _number, _widgetIndex); } }; + + class ScCheckBoxWidget : public ScWidget + { + public: + ScCheckBoxWidget(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, &ScCheckBoxWidget::isChecked_get, &ScCheckBoxWidget::isChecked_set, "isChecked"); + } + + private: + bool isChecked_get() const + { + auto w = GetWindow(); + if (w != nullptr) + { + return widget_is_pressed(w, _widgetIndex); + } + return false; + } + void isChecked_set(bool value) + { + auto w = GetWindow(); + if (w != nullptr) + { + widget_set_checkbox_value(w, _widgetIndex, value ? 1 : 0); + } + } + }; + + class ScListViewWidget : public ScWidget + { + public: + ScListViewWidget(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, &ScListViewWidget::isStriped_get, &ScListViewWidget::isStriped_set, "isStriped"); + } + + private: + bool isStriped_get() const + { + } + + void isStriped_set(bool value) + { + } + }; + + inline DukValue ScWidget::ToDuk(duk_context* ctx, rct_window* w, rct_widgetindex widgetIndex) + { + const auto& widget = w->widgets[widgetIndex]; + auto c = w->classification; + auto n = w->number; + switch (widget.type) + { + case WWT_CHECKBOX: + return GetObjectAsDukValue(ctx, std::make_shared(c, n, widgetIndex)); + case WWT_SCROLL: + return GetObjectAsDukValue(ctx, std::make_shared(c, n, widgetIndex)); + default: + return GetObjectAsDukValue(ctx, std::make_shared(c, n, widgetIndex)); + } + } + } // namespace OpenRCT2::Scripting #endif diff --git a/src/openrct2-ui/scripting/ScWindow.hpp b/src/openrct2-ui/scripting/ScWindow.hpp index 1efab2166c..1f2dd412db 100644 --- a/src/openrct2-ui/scripting/ScWindow.hpp +++ b/src/openrct2-ui/scripting/ScWindow.hpp @@ -177,16 +177,18 @@ namespace OpenRCT2::Scripting return (flags & (WF_STICK_TO_BACK | WF_STICK_TO_FRONT)) != 0; } - std::vector> widgets_get() const + std::vector widgets_get() const { - std::vector> result; + auto ctx = GetContext()->GetScriptEngine().GetContext(); + + std::vector result; auto w = GetWindow(); if (w != nullptr) { rct_widgetindex widgetIndex = 0; for (auto widget = w->widgets; widget->type != WWT_LAST; widget++) { - result.push_back(std::make_shared(_class, _number, widgetIndex)); + result.push_back(ScWidget::ToDuk(ctx, w, widgetIndex)); widgetIndex++; } } @@ -257,18 +259,19 @@ namespace OpenRCT2::Scripting } } - std::shared_ptr findWidget(std::string name) const + DukValue findWidget(std::string name) const { + auto ctx = GetContext()->GetScriptEngine().GetContext(); auto w = GetWindow(); if (w != nullptr) { auto widgetIndex = FindWidgetIndexByName(w, name); if (widgetIndex) { - return std::make_shared(_class, _number, *widgetIndex); + return ScWidget::ToDuk(ctx, w, *widgetIndex); } } - return {}; + return GetObjectAsDukValue(ctx, nullptr); } void bringToFront() diff --git a/src/openrct2-ui/scripting/UiExtensions.cpp b/src/openrct2-ui/scripting/UiExtensions.cpp index 54a00df453..446412c8ad 100644 --- a/src/openrct2-ui/scripting/UiExtensions.cpp +++ b/src/openrct2-ui/scripting/UiExtensions.cpp @@ -32,6 +32,8 @@ void UiScriptExtensions::Extend(ScriptEngine& scriptEngine) ScUi::Register(ctx); ScViewport::Register(ctx); ScWidget::Register(ctx); + ScCheckBoxWidget::Register(ctx); + ScListViewWidget::Register(ctx); ScWindow::Register(ctx); InitialiseCustomMenuItems(scriptEngine);