diff --git a/distribution/openrct2.d.ts b/distribution/openrct2.d.ts index bc4c716fef..610c9c4bfa 100644 --- a/distribution/openrct2.d.ts +++ b/distribution/openrct2.d.ts @@ -133,6 +133,9 @@ export interface Window { width: number; height: number; title: string; + isSticky: boolean; + + close(): void; } export interface WindowDesc { @@ -146,8 +149,13 @@ export interface WindowDesc { } export interface Ui { + width: number; + height: number; + windows: number; + + getWindow(id: number): Window; + getWindow(classification: string, id?: number): Window; openWindow(desc: WindowDesc): Window; - closeWindow(window: Window): void; closeWindows(classification: string, id?: number): void; closeAllWindows(): void; } diff --git a/src/openrct2/scripting/Plugin.cpp b/src/openrct2/scripting/Plugin.cpp index d0ea3f1013..ce482f3267 100644 --- a/src/openrct2/scripting/Plugin.cpp +++ b/src/openrct2/scripting/Plugin.cpp @@ -41,7 +41,7 @@ Plugin::~Plugin() void Plugin::Load() { - std::string projectedVariables = "console,context,map,park"; + std::string projectedVariables = "console,context,map,park,ui"; std::string code; { std::ifstream fs(_path); diff --git a/src/openrct2/scripting/ScUi.hpp b/src/openrct2/scripting/ScUi.hpp new file mode 100644 index 0000000000..a503f619e6 --- /dev/null +++ b/src/openrct2/scripting/ScUi.hpp @@ -0,0 +1,132 @@ +#pragma once + +#include +#include +#include +#include "../common.h" +#include "../Context.h" +#include "../interface/Window.h" +#include "../interface/Window_internal.h" +#include "../ui/UiContext.h" +#include "../ui/WindowManager.h" + +namespace OpenRCT2::Scripting +{ + class ScWindow + { + private: + rct_windowclass _class; + rct_windownumber _number; + + public: + ScWindow(rct_windowclass c, rct_windownumber n) + : _class(c), + _number(n) + { + } + + sint32 x_get() { return GetWindow()->x; } + void x_set(sint32 value) + { + auto w = GetWindow(); + window_set_position(w, value, w->y); + } + sint32 y_get() { return GetWindow()->y; } + void y_set(sint32 value) + { + auto w = GetWindow(); + window_set_position(w, w->x, value); + } + sint32 width_get() { return GetWindow()->width; } + sint32 height_get() { return GetWindow()->height; } + bool isSticky_get() + { + auto flags = GetWindow()->flags; + return (flags & (WF_STICK_TO_BACK | WF_STICK_TO_FRONT)) != 0; + } + + static void Register(duk_context * ctx) + { + dukglue_register_property(ctx, &ScWindow::x_get, &ScWindow::x_set, "x"); + dukglue_register_property(ctx, &ScWindow::y_get, &ScWindow::y_set, "y"); + dukglue_register_property(ctx, &ScWindow::width_get, nullptr, "width"); + dukglue_register_property(ctx, &ScWindow::height_get, nullptr, "height"); + dukglue_register_property(ctx, &ScWindow::isSticky_get, nullptr, "isSticky"); + } + + private: + rct_window * GetWindow() const + { + return window_find_by_number(_class, _number); + } + }; + + class ScUi + { + public: + sint32 width_get() { return context_get_width(); } + sint32 height_get() { return context_get_height(); } + sint32 windows_get() + { + return gWindowNextSlot - g_window_list; + } + + std::shared_ptr openWindow(DukValue desc) + { + return nullptr; + } + + void closeWindows(std::string classification, DukValue id) + { + auto cls = GetClassification(classification); + if (cls != WC_NULL) + { + if (id.type() == DukValue::Type::NUMBER) + { + window_close_by_number(cls, id.as_int()); + } + else + { + window_close_by_class(cls); + } + } + } + + void closeAllWindows() + { + window_close_all(); + } + + std::shared_ptr getWindow(sint32 index) + { + auto windowEnd = gWindowNextSlot; + auto i = 0; + for (auto w = g_window_list; w < windowEnd; w++) + { + if (i == index) + { + return std::make_shared(w->classification, w->number); + } + i++; + } + return nullptr; + } + + static void Register(duk_context * ctx) + { + dukglue_register_property(ctx, &ScUi::height_get, nullptr, "height"); + dukglue_register_property(ctx, &ScUi::width_get, nullptr, "width"); + dukglue_register_property(ctx, &ScUi::windows_get, nullptr, "windows"); + dukglue_register_method(ctx, &ScUi::openWindow, "openWindow"); + dukglue_register_method(ctx, &ScUi::closeWindows, "closeWindows"); + dukglue_register_method(ctx, &ScUi::closeAllWindows, "closeAllWindows"); + dukglue_register_method(ctx, &ScUi::getWindow, "getWindow"); + } + + private: + rct_windowclass GetClassification(const std::string& key) + { + return WC_NULL; + } + }; +} diff --git a/src/openrct2/scripting/ScriptEngine.cpp b/src/openrct2/scripting/ScriptEngine.cpp index 82ba255bf3..ed86d8576e 100644 --- a/src/openrct2/scripting/ScriptEngine.cpp +++ b/src/openrct2/scripting/ScriptEngine.cpp @@ -25,6 +25,7 @@ #include "ScPark.hpp" #include "ScTile.hpp" #include "ScThing.hpp" +#include "ScUi.hpp" using namespace OpenRCT2; using namespace OpenRCT2::Scripting; @@ -63,11 +64,14 @@ void ScriptEngine::Initialise() ScTile::Register(ctx); ScTileElement::Register(ctx); ScThing::Register(ctx); + ScUi::Register(ctx); + ScWindow::Register(ctx); dukglue_register_global(ctx, std::make_shared(_console), "console"); dukglue_register_global(ctx, std::make_shared(_execInfo, _hookEngine), "context"); dukglue_register_global(ctx, std::make_shared(ctx), "map"); dukglue_register_global(ctx, std::make_shared(), "park"); + dukglue_register_global(ctx, std::make_shared(), "ui"); LoadPlugins(); StartPlugins();