Implement window colours API

This commit is contained in:
Ted John 2020-02-27 19:59:46 +00:00
parent 5c1fabec56
commit 09c8c6d508
3 changed files with 51 additions and 0 deletions

View File

@ -412,6 +412,7 @@ declare global {
minWidth?: number;
minHeight?: number;
widgets?: Widget[];
colours?: number[];
tabs?: Tab[];
onClose?: () => void;

View File

@ -207,6 +207,7 @@ namespace OpenRCT2::Ui::Windows
std::string Title;
std::optional<int32_t> Id;
std::vector<CustomWidgetDesc> Widgets;
std::vector<colour_t> Colours;
CustomWindowDesc() = default;
@ -238,6 +239,19 @@ namespace OpenRCT2::Ui::Windows
});
}
if (desc["colours"].is_array())
{
auto dukColours = desc["colours"].as_array();
std::transform(dukColours.begin(), dukColours.end(), std::back_inserter(result.Colours), [](const DukValue& w) {
colour_t c = COLOUR_BLACK;
if (w.type() == DukValue::Type::NUMBER)
{
c = static_cast<colour_t>(std::clamp<int32_t>(w.as_int(), COLOUR_BLACK, COLOUR_COUNT - 1));
}
return c;
});
}
return result;
}
@ -315,9 +329,17 @@ namespace OpenRCT2::Ui::Windows
window->number = GetNewWindowNumber();
window->custom_info = new CustomWindowInfo(owner, desc);
window->enabled_widgets = (1 << WIDX_CLOSE);
// Set window colours
window->colours[0] = COLOUR_GREY;
window->colours[1] = COLOUR_GREY;
window->colours[2] = COLOUR_GREY;
auto numColours = std::min(std::size(window->colours), std::size(desc.Colours));
for (size_t i = 0; i < numColours; i++)
{
window->colours[i] = desc.Colours[i];
}
if (desc.IsResizable())
{
window->min_width = desc.MinWidth.value_or(0);

View File

@ -98,6 +98,33 @@ namespace OpenRCT2::Scripting
return result;
}
std::vector<int32_t> colours_get()
{
std::vector<int32_t> result;
auto w = GetWindow();
if (w != nullptr)
{
result.reserve(std::size(w->colours));
for (auto c : w->colours)
{
result.push_back(c);
}
}
return result;
}
void colours_set(std::vector<int32_t> colours)
{
auto w = GetWindow();
if (w != nullptr)
{
for (int32_t i = 0; i < std::size(w->colours); i++)
{
w->colours[i] = i < colours.size() ? std::clamp<int32_t>(colours[i], COLOUR_BLACK, COLOUR_COUNT - 1)
: COLOUR_BLACK;
}
}
}
void close()
{
auto w = GetWindow();
@ -141,6 +168,7 @@ namespace OpenRCT2::Scripting
dukglue_register_property(ctx, &ScWindow::height_get, nullptr, "height");
dukglue_register_property(ctx, &ScWindow::isSticky_get, nullptr, "isSticky");
dukglue_register_property(ctx, &ScWindow::widgets_get, nullptr, "widgets");
dukglue_register_property(ctx, &ScWindow::colours_get, &ScWindow::colours_set, "colours");
dukglue_register_method(ctx, &ScWindow::close, "close");
dukglue_register_method(ctx, &ScWindow::findWidget, "findWidget");