Add plugin API for setting button isPressed (#11851)

This commit is contained in:
Ted John 2020-06-02 03:05:24 +01:00 committed by GitHub
parent 1b6899a954
commit 0ca42f453c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 92 additions and 27 deletions

View file

@ -1377,6 +1377,7 @@ declare global {
*/
border?: boolean;
image: number;
isPressed: boolean;
text: string;
onClick: () => void;
}

View file

@ -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<utf8*>(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")

View file

@ -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<ScWidget, ScButtonWidget>(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<ScButtonWidget>(c, n, widgetIndex));
case WWT_CHECKBOX:
return GetObjectAsDukValue(ctx, std::make_shared<ScCheckBoxWidget>(c, n, widgetIndex));
case WWT_DROPDOWN:

View file

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