Start working on exposing ListViewWidget

This commit is contained in:
Ted John 2020-05-04 00:05:56 +01:00
parent 45a1184b0e
commit faf59598e5
3 changed files with 90 additions and 27 deletions

View File

@ -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<ScWidget, ScCheckBoxWidget>(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<ScWidget, ScListViewWidget>(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<ScCheckBoxWidget>(c, n, widgetIndex));
case WWT_SCROLL:
return GetObjectAsDukValue(ctx, std::make_shared<ScListViewWidget>(c, n, widgetIndex));
default:
return GetObjectAsDukValue(ctx, std::make_shared<ScWidget>(c, n, widgetIndex));
}
}
} // namespace OpenRCT2::Scripting
#endif

View File

@ -177,16 +177,18 @@ namespace OpenRCT2::Scripting
return (flags & (WF_STICK_TO_BACK | WF_STICK_TO_FRONT)) != 0;
}
std::vector<std::shared_ptr<ScWidget>> widgets_get() const
std::vector<DukValue> widgets_get() const
{
std::vector<std::shared_ptr<ScWidget>> result;
auto ctx = GetContext()->GetScriptEngine().GetContext();
std::vector<DukValue> 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<ScWidget>(_class, _number, widgetIndex));
result.push_back(ScWidget::ToDuk(ctx, w, widgetIndex));
widgetIndex++;
}
}
@ -257,18 +259,19 @@ namespace OpenRCT2::Scripting
}
}
std::shared_ptr<ScWidget> 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<ScWidget>(_class, _number, *widgetIndex);
return ScWidget::ToDuk(ctx, w, *widgetIndex);
}
}
return {};
return GetObjectAsDukValue<ScWidget>(ctx, nullptr);
}
void bringToFront()

View File

@ -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);