Files
OpenRCT2-Web/src/openrct2-ui/scripting/CustomWindow.cpp
T

1507 lines
56 KiB
C++
Raw Normal View History

2018-03-25 18:23:24 +01:00
/*****************************************************************************
2024-12-31 20:30:04 +01:00
* Copyright (c) 2014-2025 OpenRCT2 developers
2018-03-25 18:23:24 +01:00
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#ifdef ENABLE_SCRIPTING
2018-07-31 17:59:48 +02:00
#include "../UiStringIds.h"
#include "../interface/Dropdown.h"
#include "../interface/Widget.h"
#include "../scripting/ScGraphicsContext.hpp"
#include "../scripting/ScWidget.hpp"
#include "../windows/Windows.h"
#include "CustomListView.h"
#include "ScUi.hpp"
#include "ScWindow.hpp"
2020-02-23 12:55:48 +00:00
#include <limits>
2025-02-13 10:51:46 -05:00
#include <openrct2/SpriteIds.h>
#include <openrct2/config/Config.h>
#include <openrct2/drawing/Drawing.h>
#include <openrct2/interface/ColourWithFlags.h>
#include <openrct2/interface/Window.h>
#include <openrct2/localisation/Formatter.h>
#include <openrct2/scripting/Plugin.h>
#include <optional>
#include <string>
#include <utility>
#include <vector>
2018-03-25 18:23:24 +01:00
using namespace OpenRCT2;
using namespace OpenRCT2::Scripting;
namespace OpenRCT2::Ui::Windows
{
enum CustomWindowWidx
2018-03-25 18:23:24 +01:00
{
WIDX_BACKGROUND,
WIDX_TITLE,
WIDX_CLOSE,
WIDX_CONTENT_PANEL,
2020-04-30 20:32:39 +01:00
WIDX_TAB_0,
2018-03-25 18:23:24 +01:00
};
static auto CustomDefaultWidgets = makeWidgets(
2025-06-10 22:27:50 +02:00
makeWindowShim(STR_STRING, { 50, 50 }),
makeWidget({ 0, 14 }, { 50, 36 }, WidgetType::resize, WindowColour::secondary) // content panel
);
2018-03-25 18:23:24 +01:00
2018-03-25 22:04:36 +01:00
struct CustomWidgetDesc
{
// Properties
std::string Type;
2018-07-31 16:25:50 +02:00
int32_t X{};
int32_t Y{};
int32_t Width{};
int32_t Height{};
std::string Name;
2020-02-27 18:22:32 +00:00
ImageId Image;
2018-03-25 22:04:36 +01:00
std::string Text;
TextAlignment TextAlign{};
2021-02-20 19:40:32 +01:00
colour_t Colour{};
2020-05-01 00:06:58 +01:00
std::string Tooltip;
2018-03-25 23:27:19 +01:00
std::vector<std::string> Items;
2020-05-02 20:11:07 +01:00
std::vector<ListViewItem> ListViewItems;
std::vector<ListViewColumn> ListViewColumns;
2020-05-09 16:06:21 +01:00
ScrollbarType Scrollbars{};
2018-07-31 16:25:50 +02:00
int32_t SelectedIndex{};
2021-01-23 14:28:07 +00:00
int32_t MaxLength{};
std::optional<RowColumn> SelectedCell;
2020-02-15 15:07:10 +00:00
bool IsChecked{};
bool IsDisabled{};
2021-01-23 12:21:18 +00:00
bool IsVisible{};
bool IsPressed{};
2020-04-30 20:32:39 +01:00
bool HasBorder{};
2020-05-02 20:11:07 +01:00
bool ShowColumnHeaders{};
bool IsStriped{};
2020-05-03 17:07:27 +01:00
bool CanSelect{};
2018-03-25 22:04:36 +01:00
// Event handlers
DukValue OnClick;
2018-03-25 23:27:19 +01:00
DukValue OnChange;
DukValue OnDraw;
2020-02-27 19:44:51 +00:00
DukValue OnIncrement;
DukValue OnDecrement;
2020-05-03 16:29:15 +01:00
DukValue OnHighlight;
2018-03-25 22:04:36 +01:00
static CustomWidgetDesc FromDukValue(DukValue desc)
{
CustomWidgetDesc result;
result.Type = desc["type"].as_string();
result.X = desc["x"].as_int();
result.Y = desc["y"].as_int();
result.Width = desc["width"].as_int();
result.Height = desc["height"].as_int();
2020-05-01 00:06:58 +01:00
result.IsDisabled = AsOrDefault(desc["isDisabled"], false);
2021-01-23 12:21:18 +00:00
result.IsVisible = AsOrDefault(desc["isVisible"], true);
2020-05-01 00:06:58 +01:00
result.Name = AsOrDefault(desc["name"], "");
result.Tooltip = AsOrDefault(desc["tooltip"], "");
2018-03-25 23:27:19 +01:00
if (result.Type == "button")
{
2020-02-27 18:22:32 +00:00
auto dukImage = desc["image"];
if (dukImage.type() == DukValue::Type::STRING || dukImage.type() == DukValue::Type::NUMBER)
2020-02-27 18:22:32 +00:00
{
result.Image = ImageId(ImageFromDuk(dukImage));
2020-04-30 20:32:39 +01:00
result.HasBorder = false;
2020-02-27 18:22:32 +00:00
}
else
{
result.Text = ProcessString(desc["text"]);
2020-04-30 20:32:39 +01:00
result.HasBorder = true;
2020-02-27 18:22:32 +00:00
}
result.IsPressed = AsOrDefault(desc["isPressed"], false);
2018-03-25 23:27:19 +01:00
result.OnClick = desc["onClick"];
}
2020-02-15 15:07:10 +00:00
else if (result.Type == "checkbox")
{
result.Text = ProcessString(desc["text"]);
result.IsChecked = AsOrDefault(desc["isChecked"], false);
2020-02-15 15:07:10 +00:00
result.OnChange = desc["onChange"];
}
else if (result.Type == "colourpicker")
{
auto colour = AsOrDefault(desc["colour"], 0);
if (colour < COLOUR_COUNT)
{
result.Colour = colour;
}
result.OnChange = desc["onChange"];
}
else if (result.Type == "custom")
{
result.OnDraw = desc["onDraw"];
}
2018-03-25 23:27:19 +01:00
else if (result.Type == "dropdown")
{
2021-01-25 02:32:18 +00:00
if (desc["items"].is_array())
{
2021-01-25 02:32:18 +00:00
auto dukItems = desc["items"].as_array();
for (const auto& dukItem : dukItems)
{
result.Items.push_back(ProcessString(dukItem));
}
}
2021-01-25 02:32:18 +00:00
result.SelectedIndex = AsOrDefault(desc["selectedIndex"], 0);
2018-03-25 23:27:19 +01:00
result.OnChange = desc["onChange"];
}
2021-01-28 23:51:56 +00:00
else if (result.Type == "groupbox")
2020-02-12 21:56:18 +00:00
{
2020-02-15 15:07:10 +00:00
result.Text = ProcessString(desc["text"]);
2020-02-12 21:56:18 +00:00
}
2021-01-28 23:51:56 +00:00
else if (result.Type == "label")
{
result.Text = ProcessString(desc["text"]);
if (ProcessString(desc["textAlign"]) == "centred")
{
2025-10-16 20:06:18 +02:00
result.TextAlign = TextAlignment::centre;
2021-01-28 23:51:56 +00:00
}
}
2020-05-02 20:11:07 +01:00
else if (result.Type == "listview")
{
2020-05-07 00:02:28 +01:00
result.ListViewColumns = FromDuk<std::vector<ListViewColumn>>(desc["columns"]);
result.ListViewItems = FromDuk<std::vector<ListViewItem>>(desc["items"]);
result.SelectedCell = FromDuk<std::optional<RowColumn>>(desc["selectedCell"]);
2020-05-02 20:11:07 +01:00
result.ShowColumnHeaders = AsOrDefault(desc["showColumnHeaders"], false);
result.IsStriped = AsOrDefault(desc["isStriped"], false);
2020-05-03 16:29:15 +01:00
result.OnClick = desc["onClick"];
result.OnHighlight = desc["onHighlight"];
2020-05-03 17:07:27 +01:00
result.CanSelect = AsOrDefault(desc["canSelect"], false);
2020-05-09 16:06:21 +01:00
if (desc["scrollbars"].type() == DukValue::UNDEFINED)
result.Scrollbars = ScrollbarType::Vertical;
else
result.Scrollbars = FromDuk<ScrollbarType>(desc["scrollbars"]);
2020-05-02 20:11:07 +01:00
}
2020-02-27 19:44:51 +00:00
else if (result.Type == "spinner")
{
result.Text = ProcessString(desc["text"]);
result.OnIncrement = desc["onIncrement"];
result.OnDecrement = desc["onDecrement"];
2021-02-04 00:25:34 +00:00
result.OnClick = desc["onClick"];
2020-02-27 19:44:51 +00:00
}
2021-01-23 14:28:07 +00:00
else if (result.Type == "textbox")
{
result.Text = ProcessString(desc["text"]);
result.MaxLength = AsOrDefault(desc["maxLength"], 32);
result.OnChange = desc["onChange"];
}
2020-05-01 00:06:58 +01:00
result.HasBorder = AsOrDefault(desc["border"], result.HasBorder);
2020-04-30 20:32:39 +01:00
return result;
}
};
struct CustomTabDesc
{
ImageId imageFrameBase;
uint32_t imageFrameCount;
uint32_t imageFrameDuration;
2020-05-01 13:53:28 +01:00
ScreenCoordsXY offset;
2020-04-30 20:32:39 +01:00
std::vector<CustomWidgetDesc> Widgets;
static CustomTabDesc FromDukValue(const DukValue& desc)
{
CustomTabDesc result;
auto dukImage = desc["image"];
if (dukImage.type() == DukValue::Type::STRING || dukImage.type() == DukValue::Type::NUMBER)
2020-04-30 20:32:39 +01:00
{
result.imageFrameBase = ImageId(ImageFromDuk(dukImage));
result.imageFrameCount = 0;
result.imageFrameDuration = 0;
2020-04-30 20:32:39 +01:00
}
else if (dukImage.type() == DukValue::Type::OBJECT)
{
result.imageFrameBase = ImageId(dukImage["frameBase"].as_uint());
2020-04-30 20:32:39 +01:00
result.imageFrameCount = AsOrDefault(dukImage["frameCount"], 0);
result.imageFrameDuration = AsOrDefault(dukImage["frameDuration"], 0);
2020-05-01 13:53:28 +01:00
if (dukImage["primaryColour"].type() == DukValue::Type::NUMBER)
{
result.imageFrameBase = result.imageFrameBase.WithPrimary(dukImage["primaryColour"].as_uint());
if (dukImage["secondaryColour"].type() == DukValue::Type::NUMBER)
{
result.imageFrameBase = result.imageFrameBase.WithSecondary(dukImage["secondaryColour"].as_uint());
if (dukImage["tertiaryColour"].type() == DukValue::Type::NUMBER)
{
result.imageFrameBase = result.imageFrameBase.WithTertiary(dukImage["tertiaryColour"].as_uint());
}
}
}
2020-05-01 13:53:28 +01:00
auto dukCoord = dukImage["offset"];
if (dukCoord.type() == DukValue::Type::OBJECT)
{
result.offset = { AsOrDefault(dukCoord["x"], 0), AsOrDefault(dukCoord["y"], 0) };
2020-05-01 13:53:28 +01:00
}
2020-04-30 20:32:39 +01:00
}
if (desc["widgets"].is_array())
{
auto dukWidgets = desc["widgets"].as_array();
std::transform(dukWidgets.begin(), dukWidgets.end(), std::back_inserter(result.Widgets), [](const DukValue& w) {
return CustomWidgetDesc::FromDukValue(w);
});
}
2018-03-25 22:04:36 +01:00
return result;
}
};
2018-03-25 18:23:24 +01:00
struct CustomWindowDesc
{
std::string Classification;
2018-07-31 16:25:50 +02:00
std::optional<int32_t> X;
std::optional<int32_t> Y;
2025-06-16 23:10:47 +02:00
ScreenSize size{};
2018-07-31 16:25:50 +02:00
std::optional<int32_t> MinWidth;
std::optional<int32_t> MinHeight;
std::optional<int32_t> MaxWidth;
std::optional<int32_t> MaxHeight;
2018-03-25 18:23:24 +01:00
std::string Title;
2018-07-31 16:25:50 +02:00
std::optional<int32_t> Id;
2018-03-25 22:04:36 +01:00
std::vector<CustomWidgetDesc> Widgets;
2024-05-09 02:45:07 +02:00
std::vector<ColourWithFlags> Colours;
2020-04-30 20:32:39 +01:00
std::vector<CustomTabDesc> Tabs;
std::optional<int32_t> TabIndex;
2018-03-25 18:23:24 +01:00
2020-04-26 16:01:03 +01:00
// Event handlers
DukValue OnClose;
2020-04-30 20:32:39 +01:00
DukValue OnUpdate;
DukValue OnTabChange;
2020-04-26 16:01:03 +01:00
2018-03-25 18:23:24 +01:00
CustomWindowDesc() = default;
bool IsResizable() const
{
return MinWidth || MinHeight || MaxWidth || MaxHeight;
}
static CustomWindowDesc FromDukValue(DukValue desc)
{
CustomWindowDesc result;
result.Classification = desc["classification"].as_string();
result.X = GetOptionalInt(desc["x"]);
result.Y = GetOptionalInt(desc["y"]);
2025-06-16 23:10:47 +02:00
result.size.width = desc["width"].as_int();
result.size.height = desc["height"].as_int();
2018-03-25 18:23:24 +01:00
result.MinWidth = GetOptionalInt(desc["minWidth"]);
result.MaxWidth = GetOptionalInt(desc["maxWidth"]);
result.MinHeight = GetOptionalInt(desc["minHeight"]);
result.MaxHeight = GetOptionalInt(desc["maxHeight"]);
2020-10-16 00:13:52 +01:00
result.Title = desc["title"].as_string();
2018-03-25 18:23:24 +01:00
result.Id = GetOptionalInt(desc["id"]);
result.TabIndex = GetOptionalInt(desc["tabIndex"]);
2018-03-25 22:04:36 +01:00
if (desc["widgets"].is_array())
{
auto dukWidgets = desc["widgets"].as_array();
2018-07-31 17:59:48 +02:00
std::transform(dukWidgets.begin(), dukWidgets.end(), std::back_inserter(result.Widgets), [](const DukValue& w) {
return CustomWidgetDesc::FromDukValue(w);
});
2018-03-25 22:04:36 +01:00
}
2020-04-30 20:32:39 +01:00
if (desc["tabs"].is_array())
{
auto dukTabs = desc["tabs"].as_array();
std::transform(dukTabs.begin(), dukTabs.end(), std::back_inserter(result.Tabs), [](const DukValue& w) {
return CustomTabDesc::FromDukValue(w);
});
}
2020-02-27 19:59:46 +00:00
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) {
2024-05-09 02:45:07 +02:00
ColourWithFlags c = { COLOUR_BLACK };
2020-02-27 19:59:46 +00:00
if (w.type() == DukValue::Type::NUMBER)
{
2024-06-07 05:39:22 +10:00
colour_t colour = w.as_uint() & ~kLegacyColourFlagTranslucent;
2025-10-16 23:04:06 +02:00
bool isTranslucent = (w.as_uint() & kLegacyColourFlagTranslucent);
2024-05-09 02:45:07 +02:00
c.colour = std::clamp<colour_t>(colour, COLOUR_BLACK, COLOUR_COUNT - 1);
2025-10-16 23:04:06 +02:00
c.flags.set(ColourFlag::translucent, isTranslucent);
2020-02-27 19:59:46 +00:00
}
return c;
});
}
2020-04-26 16:01:03 +01:00
result.OnClose = desc["onClose"];
2020-04-30 20:32:39 +01:00
result.OnUpdate = desc["onUpdate"];
result.OnTabChange = desc["onTabChange"];
2020-04-26 16:01:03 +01:00
2020-02-22 18:04:27 +00:00
return result;
2018-03-25 18:23:24 +01:00
}
2018-07-31 16:25:50 +02:00
static std::optional<int32_t> GetOptionalInt(DukValue input)
2018-03-25 18:23:24 +01:00
{
2018-07-31 17:59:48 +02:00
return input.type() == DukValue::Type::NUMBER ? std::make_optional(input.as_int()) : std::nullopt;
2018-03-25 18:23:24 +01:00
}
};
class CustomWindowInfo
{
public:
std::shared_ptr<Plugin> Owner;
CustomWindowDesc Desc;
2022-12-24 16:50:29 +01:00
std::vector<Widget> Widgets;
2018-03-25 23:27:19 +01:00
std::vector<size_t> WidgetIndexMap;
std::vector<CustomListView> ListViews;
2018-03-25 18:23:24 +01:00
2020-02-23 15:33:01 +00:00
CustomWindowInfo(std::shared_ptr<Plugin> owner, const CustomWindowDesc& desc)
: Owner(owner)
2018-07-31 17:59:48 +02:00
, Desc(desc)
2018-03-25 18:23:24 +01:00
{
}
CustomWindowInfo(const CustomWindowInfo&) = delete;
2018-03-25 23:27:19 +01:00
const CustomWidgetDesc* GetCustomWidgetDesc(WindowBase* w, size_t widgetIndex) const
2018-03-25 23:27:19 +01:00
{
if (widgetIndex < WidgetIndexMap.size())
{
auto widgetDescIndex = WidgetIndexMap[widgetIndex];
if (widgetDescIndex < Desc.Widgets.size())
{
return &Desc.Widgets[widgetDescIndex];
}
2021-09-15 22:22:15 +02:00
auto page = static_cast<size_t>(w->page);
if (Desc.Tabs.size() > page)
2020-04-30 20:32:39 +01:00
{
2021-09-15 22:22:15 +02:00
auto& widgets = Desc.Tabs[page].Widgets;
auto tabWidgetIndex = widgetDescIndex - Desc.Widgets.size();
if (tabWidgetIndex < widgets.size())
2020-04-30 20:32:39 +01:00
{
return &widgets[tabWidgetIndex];
2020-04-30 20:32:39 +01:00
}
}
2018-03-25 23:27:19 +01:00
}
return nullptr;
}
2020-02-14 23:36:24 +00:00
CustomWidgetDesc* GetCustomWidgetDesc(WindowBase* w, size_t widgetIndex)
2020-02-14 23:36:24 +00:00
{
2020-04-30 20:32:39 +01:00
return const_cast<CustomWidgetDesc*>(std::as_const(*this).GetCustomWidgetDesc(w, widgetIndex));
2020-02-14 23:36:24 +00:00
}
2018-03-25 18:23:24 +01:00
};
2023-07-18 06:24:42 +01:00
class CustomWindow;
static CustomWindowInfo& GetInfo(CustomWindow* w);
2020-04-23 19:31:56 +01:00
static void InvokeEventHandler(const std::shared_ptr<Plugin>& owner, const DukValue& dukHandler);
2018-07-31 17:59:48 +02:00
static void InvokeEventHandler(
2020-04-23 19:31:56 +01:00
const std::shared_ptr<Plugin>& owner, const DukValue& dukHandler, const std::vector<DukValue>& args);
2018-03-25 18:23:24 +01:00
2021-02-24 22:54:53 +00:00
class CustomWindow final : public Window
2018-03-25 18:23:24 +01:00
{
2021-01-31 14:07:21 +00:00
private:
2025-09-17 18:31:20 +02:00
static WindowNumber _nextWindowNumber;
2023-07-18 06:24:42 +01:00
CustomWindowInfo _info;
2018-03-25 18:23:24 +01:00
2021-01-31 14:07:21 +00:00
public:
2023-07-18 06:24:42 +01:00
CustomWindow(std::shared_ptr<Plugin> owner, const CustomWindowDesc& desc)
: _info(owner, desc)
{
}
void onOpen() override
2018-03-25 18:23:24 +01:00
{
2021-01-31 14:07:21 +00:00
number = GetNewWindowNumber();
2018-03-25 18:23:24 +01:00
2021-01-31 14:07:21 +00:00
// Set window tab
page = _info.Desc.TabIndex.value_or(0);
2020-02-27 19:59:46 +00:00
2021-01-31 14:07:21 +00:00
// Set window colours
colours[0] = COLOUR_GREY;
colours[1] = COLOUR_GREY;
colours[2] = COLOUR_GREY;
auto numColours = std::min(std::size(colours), std::size(_info.Desc.Colours));
2021-01-31 14:07:21 +00:00
for (size_t i = 0; i < numColours; i++)
2018-03-25 22:04:36 +01:00
{
colours[i] = _info.Desc.Colours[i];
2021-01-31 14:07:21 +00:00
}
if (_info.Desc.IsResizable())
2021-01-31 14:07:21 +00:00
{
2025-09-17 19:03:29 +02:00
minWidth = _info.Desc.MinWidth.value_or(0);
minHeight = _info.Desc.MinHeight.value_or(0);
maxWidth = _info.Desc.MaxWidth.value_or(kMaxWindowSize.width);
maxHeight = _info.Desc.MaxHeight.value_or(kMaxWindowSize.height);
2021-01-31 14:07:21 +00:00
}
RefreshWidgets();
}
void onClose() override
2021-01-31 14:07:21 +00:00
{
2023-07-18 06:24:42 +01:00
InvokeEventHandler(_info.Owner, _info.Desc.OnClose);
2021-01-31 14:07:21 +00:00
}
void onResize() override
2021-01-31 14:07:21 +00:00
{
2025-09-17 19:03:29 +02:00
if (width < minWidth)
{
invalidate();
2025-09-17 19:03:29 +02:00
width = minWidth;
}
2025-09-17 19:03:29 +02:00
if (height < minHeight)
{
invalidate();
2025-09-17 19:03:29 +02:00
height = minHeight;
}
2021-01-31 14:07:21 +00:00
UpdateViewport();
}
void onUpdate() override
2021-01-31 14:07:21 +00:00
{
2023-07-18 06:24:42 +01:00
if (_info.Desc.Tabs.size() > static_cast<size_t>(page))
2021-01-31 14:07:21 +00:00
{
2023-07-18 06:24:42 +01:00
const auto& tab = _info.Desc.Tabs[page];
2021-01-31 14:07:21 +00:00
if (tab.imageFrameCount != 0)
2020-04-30 20:32:39 +01:00
{
2025-09-17 19:03:29 +02:00
currentFrame++;
if (currentFrame >= tab.imageFrameCount * tab.imageFrameDuration)
2021-01-31 14:07:21 +00:00
{
2025-09-17 19:03:29 +02:00
currentFrame = 0;
2021-01-31 14:07:21 +00:00
}
invalidateWidget(WIDX_TAB_0 + this->page);
2021-01-31 14:07:21 +00:00
}
}
2023-07-18 06:24:42 +01:00
InvokeEventHandler(_info.Owner, _info.Desc.OnUpdate);
2021-01-31 14:07:21 +00:00
// Since the plugin may alter widget positions and sizes during an update event,
// we need to force an update for all list view scrollbars
2022-08-21 18:49:23 +02:00
WidgetIndex widgetIndex = 0;
2025-01-18 14:45:17 +02:00
for (auto& widget : widgets)
2021-01-31 14:07:21 +00:00
{
if (widget.type == WidgetType::scroll)
2021-01-31 14:07:21 +00:00
{
widgetScrollUpdateThumbs(*this, widgetIndex);
2021-01-31 14:07:21 +00:00
}
widgetIndex++;
}
UpdateViewport();
2021-01-31 14:07:21 +00:00
}
void onPrepareDraw() override
2021-01-31 14:07:21 +00:00
{
auto& closeButton = widgets[WIDX_CLOSE];
2025-10-16 23:04:06 +02:00
bool translucent = colours[closeButton.colour].flags.has(ColourFlag::translucent);
2025-10-07 20:14:37 +02:00
if (Config::Get().interface.enlargedUi)
closeButton.string = !translucent ? kCloseBoxStringBlackLarge : kCloseBoxStringWhiteLarge;
else
closeButton.string = !translucent ? kCloseBoxStringBlackNormal : kCloseBoxStringWhiteNormal;
2021-01-31 14:07:21 +00:00
// Having the content panel visible for transparent windows makes the borders darker than they should be
// For now just hide it if there are no tabs and the window is not resizable
2023-07-18 06:24:42 +01:00
auto numTabs = _info.Desc.Tabs.size();
2025-02-24 17:55:33 +01:00
if (canBeResized() || numTabs != 0)
2021-01-31 14:07:21 +00:00
{
2025-06-09 22:29:06 +02:00
widgets[WIDX_CONTENT_PANEL].flags.unset(WidgetFlag::isHidden);
2021-01-31 14:07:21 +00:00
}
else
{
2025-06-09 22:29:06 +02:00
widgets[WIDX_CONTENT_PANEL].flags.set(WidgetFlag::isHidden);
2021-01-31 14:07:21 +00:00
}
SetPressedTab();
2023-07-18 06:24:42 +01:00
const auto& desc = _info.Desc;
2021-01-31 14:07:21 +00:00
auto ft = Formatter::Common();
ft.Add<const char*>(desc.Title.c_str());
size_t scrollIndex = 0;
2025-01-18 14:45:17 +02:00
for (const auto& widget : widgets)
2021-01-31 14:07:21 +00:00
{
if (widget.type == WidgetType::scroll)
2021-01-31 14:07:21 +00:00
{
2023-07-18 06:24:42 +01:00
auto& listView = _info.ListViews[scrollIndex];
2025-11-08 10:27:58 +01:00
auto wwidth = widget.width() - 2;
2025-12-12 18:26:05 +01:00
auto wheight = widget.height() - 2;
2021-01-31 14:07:21 +00:00
if (listView.GetScrollbars() == ScrollbarType::Horizontal
|| listView.GetScrollbars() == ScrollbarType::Both)
{
wheight -= kScrollBarWidth + 1;
2021-01-31 14:07:21 +00:00
}
if (listView.GetScrollbars() == ScrollbarType::Vertical || listView.GetScrollbars() == ScrollbarType::Both)
{
wwidth -= kScrollBarWidth + 1;
2021-01-31 14:07:21 +00:00
}
listView.Resize({ wwidth, wheight });
scrollIndex++;
}
}
}
2025-12-26 12:58:33 +01:00
void onDraw(Drawing::RenderTarget& rt) override
2021-01-31 14:07:21 +00:00
{
WindowDrawWidgets(*this, rt);
DrawTabImages(rt);
2021-01-31 14:07:21 +00:00
if (viewport != nullptr)
{
auto widgetIndex = GetViewportWidgetIndex();
if (widgetIsVisible(*this, widgetIndex.value_or(false)))
2021-01-31 14:07:21 +00:00
{
WindowDrawViewport(rt, *this);
2021-01-31 14:07:21 +00:00
}
}
}
void onDrawWidget(WidgetIndex widgetIndex, RenderTarget& rt) override
{
const auto& widget = widgets[widgetIndex];
2023-07-18 06:24:42 +01:00
const auto widgetDesc = _info.GetCustomWidgetDesc(this, widgetIndex);
if (widgetDesc != nullptr && widgetDesc->Type == "custom")
{
auto& onDraw = widgetDesc->OnDraw;
if (onDraw.is_function())
{
RenderTarget widgetDpi;
if (ClipDrawPixelInfo(
2025-11-08 10:27:58 +01:00
widgetDpi, rt, { windowPos.x + widget.left, windowPos.y + widget.top }, widget.width() - 1,
2025-12-12 18:26:05 +01:00
widget.height() - 1))
{
auto ctx = onDraw.context();
auto dukWidget = ScWidget::ToDukValue(ctx, this, widgetIndex);
auto dukG = GetObjectAsDukValue(ctx, std::make_shared<ScGraphicsContext>(ctx, widgetDpi));
auto& scriptEngine = GetContext()->GetScriptEngine();
2023-07-18 06:24:42 +01:00
scriptEngine.ExecutePluginCall(_info.Owner, widgetDesc->OnDraw, dukWidget, { dukG }, false);
}
}
}
else
{
Window::onDrawWidget(widgetIndex, rt);
}
}
void onMouseUp(WidgetIndex widgetIndex) override
2021-01-31 14:07:21 +00:00
{
switch (widgetIndex)
{
case WIDX_CLOSE:
{
auto* windowMgr = Ui::GetWindowManager();
windowMgr->Close(*this);
2021-01-31 14:07:21 +00:00
break;
}
2021-01-31 14:07:21 +00:00
default:
{
2023-07-18 06:24:42 +01:00
if (widgetIndex >= WIDX_TAB_0
&& widgetIndex < static_cast<WidgetIndex>(WIDX_TAB_0 + _info.Desc.Tabs.size()))
2021-01-31 14:07:21 +00:00
{
ChangeTab(widgetIndex - WIDX_TAB_0);
break;
}
2023-07-18 06:24:42 +01:00
const auto widgetDesc = _info.GetCustomWidgetDesc(this, widgetIndex);
2021-01-31 14:07:21 +00:00
if (widgetDesc != nullptr)
{
if (widgetDesc->Type == "button")
{
2023-07-18 06:24:42 +01:00
InvokeEventHandler(_info.Owner, widgetDesc->OnClick);
2021-01-31 14:07:21 +00:00
}
else if (widgetDesc->Type == "checkbox")
{
auto& widget = widgets[widgetIndex];
2025-06-09 22:29:06 +02:00
widget.flags.flip(WidgetFlag::isPressed);
bool isChecked = widget.flags.has(WidgetFlag::isPressed);
2021-01-31 14:07:21 +00:00
widgetSetCheckboxValue(*this, widgetIndex, isChecked);
2021-01-31 14:07:21 +00:00
std::vector<DukValue> args;
auto ctx = widgetDesc->OnChange.context();
duk_push_boolean(ctx, isChecked);
args.push_back(DukValue::take_from_stack(ctx));
2023-07-18 06:24:42 +01:00
InvokeEventHandler(_info.Owner, widgetDesc->OnChange, args);
2021-01-31 14:07:21 +00:00
}
2021-02-04 00:25:34 +00:00
else if (widgetDesc->Type == "spinner")
{
auto& widget = widgets[widgetIndex];
if (widget.text != STR_NUMERIC_DOWN && widget.text != STR_NUMERIC_UP)
{
2023-07-18 06:24:42 +01:00
InvokeEventHandler(_info.Owner, widgetDesc->OnClick);
2021-02-04 00:25:34 +00:00
}
}
2021-01-31 14:07:21 +00:00
}
2020-04-30 20:32:39 +01:00
break;
}
2021-01-31 14:07:21 +00:00
}
}
2020-04-30 20:32:39 +01:00
void onMouseDown(WidgetIndex widgetIndex) override
2021-01-31 14:07:21 +00:00
{
auto* widget = &widgets[widgetIndex];
2023-07-18 06:24:42 +01:00
const auto widgetDesc = _info.GetCustomWidgetDesc(this, widgetIndex);
2021-01-31 14:07:21 +00:00
if (widgetDesc != nullptr)
{
if (widgetDesc->Type == "colourpicker")
2018-03-25 23:27:19 +01:00
{
2021-01-31 14:07:21 +00:00
WindowDropdownShowColour(this, widget, colours[widget->colour], widgetDesc->Colour);
}
else if (widgetDesc->Type == "dropdown")
{
widget--;
auto selectedIndex = widgetDesc->SelectedIndex;
const auto& items = widgetDesc->Items;
2025-01-19 16:59:45 +01:00
const auto numItems = std::min<size_t>(items.size(), Dropdown::kItemsMaxSize);
2021-01-31 14:07:21 +00:00
for (size_t i = 0; i < numItems; i++)
2018-03-25 23:27:19 +01:00
{
2025-08-23 16:59:48 +02:00
gDropdown.items[i] = Dropdown::MenuLabel(items[i].c_str());
2018-03-25 23:27:19 +01:00
}
2021-01-31 14:07:21 +00:00
WindowDropdownShowTextCustomWidth(
2025-12-12 18:26:05 +01:00
{ windowPos.x + widget->left, windowPos.y + widget->top }, widget->height(), colours[widget->colour], 0,
Dropdown::Flag::StayOpen, numItems, widget->width() - 4);
2025-08-23 17:53:21 +02:00
if (selectedIndex >= 0 && selectedIndex < static_cast<int32_t>(numItems))
gDropdown.items[selectedIndex].setChecked(true);
2021-01-31 14:07:21 +00:00
}
else if (widgetDesc->Type == "spinner")
{
if (widget->text == STR_NUMERIC_DOWN)
2020-02-15 15:07:10 +00:00
{
2023-07-18 06:24:42 +01:00
InvokeEventHandler(_info.Owner, widgetDesc->OnDecrement);
2021-01-31 14:07:21 +00:00
}
else if (widget->text == STR_NUMERIC_UP)
{
2023-07-18 06:24:42 +01:00
InvokeEventHandler(_info.Owner, widgetDesc->OnIncrement);
2020-02-15 15:07:10 +00:00
}
2018-03-25 23:27:19 +01:00
}
2021-01-31 14:07:21 +00:00
else if (widgetDesc->Type == "textbox")
2018-03-25 23:27:19 +01:00
{
2024-03-22 08:53:22 +00:00
WindowStartTextbox(*this, widgetIndex, widgetDesc->Text, widgetDesc->MaxLength + 1);
2020-02-27 19:44:51 +00:00
}
}
2018-03-25 23:27:19 +01:00
}
void onDropdown(WidgetIndex widgetIndex, int32_t dropdownIndex) override
2018-03-25 23:27:19 +01:00
{
2021-01-31 14:07:21 +00:00
if (dropdownIndex == -1)
return;
2018-03-25 23:27:19 +01:00
2023-07-18 06:24:42 +01:00
auto widgetDesc = _info.GetCustomWidgetDesc(this, widgetIndex);
2021-01-31 14:07:21 +00:00
if (widgetDesc != nullptr)
2021-01-23 14:28:07 +00:00
{
2021-01-31 14:07:21 +00:00
if (widgetDesc->Type == "colourpicker")
2020-04-30 20:32:39 +01:00
{
2023-04-22 09:58:13 +01:00
UpdateWidgetColour(this, widgetIndex, ColourDropDownIndexToColour(dropdownIndex));
2020-04-30 20:32:39 +01:00
}
2021-01-31 14:07:21 +00:00
else if (widgetDesc->Type == "dropdown")
2020-05-02 20:11:07 +01:00
{
2021-01-31 14:07:21 +00:00
UpdateWidgetSelectedIndex(this, widgetIndex - 1, dropdownIndex);
2020-05-02 20:11:07 +01:00
}
2021-01-31 14:07:21 +00:00
}
}
void onTextInput(WidgetIndex widgetIndex, std::string_view text) override
2021-01-31 14:07:21 +00:00
{
2023-07-18 06:24:42 +01:00
auto widgetDesc = _info.GetCustomWidgetDesc(this, widgetIndex);
2021-01-31 14:07:21 +00:00
if (widgetDesc != nullptr)
{
if (widgetDesc->Type == "textbox")
2020-05-02 20:11:07 +01:00
{
2021-01-31 14:07:21 +00:00
UpdateWidgetText(this, widgetIndex, text);
std::vector<DukValue> args;
auto ctx = widgetDesc->OnChange.context();
duk_push_lstring(ctx, text.data(), text.size());
args.push_back(DukValue::take_from_stack(ctx));
2023-07-18 06:24:42 +01:00
InvokeEventHandler(_info.Owner, widgetDesc->OnChange, args);
2020-05-02 20:11:07 +01:00
}
}
}
2018-03-25 18:23:24 +01:00
ScreenSize onScrollGetSize(int32_t scrollIndex) override
2020-04-30 20:32:39 +01:00
{
2023-07-18 06:24:42 +01:00
if (scrollIndex < static_cast<int32_t>(_info.ListViews.size()))
2020-04-30 20:32:39 +01:00
{
2023-07-18 06:24:42 +01:00
auto size = _info.ListViews[scrollIndex].GetSize();
2021-01-31 14:07:21 +00:00
return { size.width, size.height };
}
return {};
}
void onScrollMouseDown(int32_t scrollIndex, const ScreenCoordsXY& screenCoords) override
2021-01-31 14:07:21 +00:00
{
2023-07-18 06:24:42 +01:00
if (scrollIndex < static_cast<int32_t>(_info.ListViews.size()))
2021-01-31 14:07:21 +00:00
{
2023-07-18 06:24:42 +01:00
_info.ListViews[scrollIndex].MouseDown(screenCoords);
2021-01-31 14:07:21 +00:00
}
}
void onScrollMouseDrag(int32_t scrollIndex, const ScreenCoordsXY& screenCoords) override
2021-01-31 14:07:21 +00:00
{
2023-07-18 06:24:42 +01:00
if (scrollIndex < static_cast<int32_t>(_info.ListViews.size()))
2021-01-31 14:07:21 +00:00
{
2023-07-18 06:24:42 +01:00
_info.ListViews[scrollIndex].MouseOver(screenCoords, true);
2021-01-31 14:07:21 +00:00
}
}
void onScrollMouseOver(int32_t scrollIndex, const ScreenCoordsXY& screenCoords) override
2021-01-31 14:07:21 +00:00
{
2023-07-18 06:24:42 +01:00
if (scrollIndex < static_cast<int32_t>(_info.ListViews.size()))
2021-01-31 14:07:21 +00:00
{
2023-07-18 06:24:42 +01:00
_info.ListViews[scrollIndex].MouseOver(screenCoords, false);
2021-01-31 14:07:21 +00:00
}
}
void onScrollDraw(int32_t scrollIndex, RenderTarget& rt) override
2021-01-31 14:07:21 +00:00
{
2023-07-18 06:24:42 +01:00
if (scrollIndex < static_cast<int32_t>(_info.ListViews.size()))
2021-01-31 14:07:21 +00:00
{
_info.ListViews[scrollIndex].Paint(this, rt, &scrolls[scrollIndex]);
2021-01-31 14:07:21 +00:00
}
}
void ChangeTab(size_t tabIndex)
{
if (page == static_cast<int16_t>(tabIndex) && !widgets.empty())
return;
page = static_cast<int16_t>(tabIndex);
2025-09-17 19:03:29 +02:00
currentFrame = 0;
RefreshWidgets();
invalidate();
onResize();
onPrepareDraw();
WindowInitScrollWidgets(*this);
invalidate();
2023-07-18 06:24:42 +01:00
InvokeEventHandler(_info.Owner, _info.Desc.OnTabChange);
}
const CustomWindowInfo& getInfo() const
2023-07-18 06:24:42 +01:00
{
return _info;
}
CustomWindowInfo& getInfo()
2023-07-18 06:24:42 +01:00
{
return _info;
}
2021-01-31 14:07:21 +00:00
private:
2022-08-21 18:49:23 +02:00
std::optional<WidgetIndex> GetViewportWidgetIndex()
2021-01-31 14:07:21 +00:00
{
2022-08-21 18:49:23 +02:00
WidgetIndex widgetIndex = 0;
2025-01-18 14:45:17 +02:00
for (auto& widget : widgets)
2021-01-31 14:07:21 +00:00
{
if (widget.type == WidgetType::viewport)
2020-04-30 20:32:39 +01:00
{
2021-01-31 14:07:21 +00:00
return widgetIndex;
2020-04-30 20:32:39 +01:00
}
2021-01-31 14:07:21 +00:00
widgetIndex++;
2020-04-30 20:32:39 +01:00
}
2021-01-31 14:07:21 +00:00
return std::nullopt;
2020-04-30 20:32:39 +01:00
}
2021-01-31 14:07:21 +00:00
void UpdateViewport()
2020-02-15 17:09:41 +00:00
{
2021-01-31 14:07:21 +00:00
auto viewportWidgetIndex = GetViewportWidgetIndex();
if (viewportWidgetIndex)
2020-02-15 17:09:41 +00:00
{
2021-01-31 14:07:21 +00:00
auto viewportWidget = &widgets[*viewportWidgetIndex];
2023-07-18 06:24:42 +01:00
auto widgetInfo = _info.GetCustomWidgetDesc(this, *viewportWidgetIndex);
2021-01-31 14:07:21 +00:00
if (widgetInfo != nullptr)
2020-02-15 17:09:41 +00:00
{
2021-01-31 14:07:21 +00:00
auto left = windowPos.x + viewportWidget->left + 1;
auto top = windowPos.y + viewportWidget->top + 1;
2025-11-08 10:27:58 +01:00
auto wwidth = viewportWidget->width() - 2;
2025-12-12 18:26:05 +01:00
auto wheight = viewportWidget->height() - 2;
2021-01-31 14:07:21 +00:00
if (viewport == nullptr)
{
ViewportCreate(*this, { left, top }, wwidth, wheight, Focus(CoordsXYZ(0, 0, 0)));
2025-09-20 21:46:42 +02:00
flags |= WindowFlag::noScrolling;
invalidate();
2021-01-31 14:07:21 +00:00
}
else
{
if (viewport->pos.x != left || viewport->pos.y != top || viewport->width != wwidth
|| viewport->height != wheight)
{
viewport->pos.x = left;
viewport->pos.y = top;
viewport->width = wwidth;
viewport->height = wheight;
invalidate();
2021-01-31 14:07:21 +00:00
}
}
}
else
{
removeViewport();
}
}
else
{
removeViewport();
2021-01-31 14:07:21 +00:00
}
}
void SetPressedTab()
{
2023-07-18 06:24:42 +01:00
auto numTabs = _info.Desc.Tabs.size();
2021-01-31 14:07:21 +00:00
if (numTabs != 0)
{
for (size_t i = 0; i < numTabs; i++)
{
setWidgetPressed(static_cast<WidgetIndex>(WIDX_TAB_0 + i), false);
2021-01-31 14:07:21 +00:00
}
setWidgetPressed(WIDX_TAB_0 + page, true);
2021-01-31 14:07:21 +00:00
}
}
void DrawTabImages(RenderTarget& rt)
2021-01-31 14:07:21 +00:00
{
2023-07-18 06:24:42 +01:00
const auto& tabs = _info.Desc.Tabs;
2021-01-31 14:07:21 +00:00
size_t tabIndex = 0;
for (const auto& tab : tabs)
{
2022-08-21 18:49:23 +02:00
auto widgetIndex = static_cast<WidgetIndex>(WIDX_TAB_0 + tabIndex);
2021-01-31 14:07:21 +00:00
auto widget = &widgets[widgetIndex];
if (widgetIsVisible(*this, widgetIndex))
2021-01-31 14:07:21 +00:00
{
auto leftTop = windowPos + tab.offset + ScreenCoordsXY{ widget->left, widget->top };
auto image = tab.imageFrameBase;
if (static_cast<size_t>(page) == tabIndex && tab.imageFrameDuration != 0 && tab.imageFrameCount != 0)
{
2025-09-17 19:03:29 +02:00
auto frame = currentFrame / tab.imageFrameDuration;
2021-01-31 14:07:21 +00:00
auto imageOffset = frame % tab.imageFrameCount;
image = image.WithIndex(image.GetIndex() + imageOffset);
}
GfxDrawSprite(rt, image, leftTop);
2021-01-31 14:07:21 +00:00
}
tabIndex++;
}
}
void RefreshWidgets()
{
2023-07-18 06:24:42 +01:00
auto& widgetList = _info.Widgets;
2021-01-31 14:07:21 +00:00
widgetList.clear();
2023-07-18 06:24:42 +01:00
_info.WidgetIndexMap.clear();
_info.ListViews.clear();
2021-01-31 14:07:21 +00:00
// Add default widgets (window shim)
widgetList.insert(widgetList.begin(), std::begin(CustomDefaultWidgets), std::end(CustomDefaultWidgets));
for (size_t i = 0; i < widgetList.size(); i++)
{
2023-07-18 06:24:42 +01:00
_info.WidgetIndexMap.push_back(std::numeric_limits<size_t>::max());
2021-01-31 14:07:21 +00:00
}
// Add window tabs
2023-07-18 06:24:42 +01:00
if (_info.Desc.Tabs.size() != 0)
2021-01-31 14:07:21 +00:00
{
2025-06-16 23:10:47 +02:00
widgetList[WIDX_CONTENT_PANEL].top = kTabBarHeight;
2021-01-31 14:07:21 +00:00
}
2023-07-18 06:24:42 +01:00
for (size_t tabDescIndex = 0; tabDescIndex < _info.Desc.Tabs.size(); tabDescIndex++)
2021-01-31 14:07:21 +00:00
{
2022-12-24 16:50:29 +01:00
Widget widget{};
widget.type = WidgetType::tab;
2021-01-31 14:07:21 +00:00
widget.colour = 1;
widget.left = static_cast<int16_t>(3 + (tabDescIndex * 31));
widget.right = widget.left + 30;
widget.top = 17;
2025-06-16 23:10:47 +02:00
widget.bottom = kTabBarHeight;
2025-09-23 21:09:54 +02:00
widget.image = ImageId(SPR_TAB, FilterPaletteID::paletteNull);
2025-01-19 16:59:45 +01:00
widget.tooltip = kStringIdNone;
2021-01-31 14:07:21 +00:00
widgetList.push_back(widget);
2023-07-18 06:24:42 +01:00
_info.WidgetIndexMap.push_back(std::numeric_limits<size_t>::max());
2021-01-31 14:07:21 +00:00
}
// Add custom widgets
2023-07-18 06:24:42 +01:00
auto totalWidgets = _info.Desc.Widgets.size();
2021-01-31 14:07:21 +00:00
auto tabWidgetsOffset = totalWidgets;
2023-07-18 06:24:42 +01:00
if (_info.Desc.Tabs.size() != 0)
2021-01-31 14:07:21 +00:00
{
2023-07-18 06:24:42 +01:00
totalWidgets += _info.Desc.Tabs[page].Widgets.size();
2021-01-31 14:07:21 +00:00
}
for (size_t widgetDescIndex = 0; widgetDescIndex < totalWidgets; widgetDescIndex++)
{
2023-07-18 06:24:42 +01:00
const auto& widgetDesc = widgetDescIndex < _info.Desc.Widgets.size()
? _info.Desc.Widgets[widgetDescIndex]
: _info.Desc.Tabs[page].Widgets[widgetDescIndex - tabWidgetsOffset];
2021-01-31 14:07:21 +00:00
auto preWidgetSize = widgetList.size();
CreateWidget(widgetList, widgetDesc);
auto numWidgetsAdded = widgetList.size() - preWidgetSize;
for (size_t i = 0; i < numWidgetsAdded; i++)
2021-01-31 14:07:21 +00:00
{
2023-07-18 06:24:42 +01:00
_info.WidgetIndexMap.push_back(widgetDescIndex);
2021-01-31 14:07:21 +00:00
}
if (widgetDesc.Type == "listview")
{
2023-07-18 06:24:42 +01:00
CustomListView listView(this, _info.ListViews.size());
listView.Owner = _info.Owner;
2021-01-31 14:07:21 +00:00
listView.SetScrollbars(widgetDesc.Scrollbars, true);
listView.SetColumns(widgetDesc.ListViewColumns, true);
listView.SetItems(widgetDesc.ListViewItems, true);
listView.SelectedCell = widgetDesc.SelectedCell;
listView.ShowColumnHeaders = widgetDesc.ShowColumnHeaders;
listView.IsStriped = widgetDesc.IsStriped;
listView.OnClick = widgetDesc.OnClick;
listView.OnHighlight = widgetDesc.OnHighlight;
listView.CanSelect = widgetDesc.CanSelect;
2023-07-18 06:24:42 +01:00
_info.ListViews.push_back(std::move(listView));
2021-01-31 14:07:21 +00:00
}
}
setWidgets(widgetList);
2021-01-31 14:07:21 +00:00
WindowInitScrollWidgets(*this);
2021-01-31 14:07:21 +00:00
UpdateViewport();
}
2022-12-24 16:50:29 +01:00
static void CreateWidget(std::vector<Widget>& widgetList, const CustomWidgetDesc& desc)
2021-01-31 14:07:21 +00:00
{
2022-12-24 16:50:29 +01:00
Widget widget{};
2021-01-31 14:07:21 +00:00
widget.colour = 1;
widget.left = desc.X;
widget.top = desc.Y;
widget.right = desc.X + desc.Width - 1;
widget.bottom = desc.Y + desc.Height - 1;
widget.content = std::numeric_limits<uint32_t>::max();
2025-01-19 16:59:45 +01:00
widget.tooltip = kStringIdNone;
widget.sztooltip = const_cast<utf8*>(desc.Tooltip.c_str());
2025-06-09 22:29:06 +02:00
widget.flags.set(WidgetFlag::tooltipIsString);
2021-01-31 14:07:21 +00:00
if (desc.IsDisabled)
2025-06-09 22:29:06 +02:00
widget.flags.set(WidgetFlag::isDisabled);
2021-01-31 14:07:21 +00:00
if (!desc.IsVisible)
2025-06-09 22:29:06 +02:00
widget.flags.set(WidgetFlag::isHidden);
2021-01-31 14:07:21 +00:00
if (desc.Type == "button")
{
if (desc.Image.HasValue())
{
widget.type = desc.HasBorder ? WidgetType::imgBtn : WidgetType::flatBtn;
2022-12-24 17:47:22 +01:00
widget.image = desc.Image;
2020-02-15 17:09:41 +00:00
}
else
{
widget.type = WidgetType::button;
2021-01-31 14:07:21 +00:00
widget.string = const_cast<utf8*>(desc.Text.c_str());
2025-06-09 22:29:06 +02:00
widget.flags.set(WidgetFlag::textIsString);
}
2021-01-31 14:07:21 +00:00
if (desc.IsPressed)
{
2025-06-09 22:29:06 +02:00
widget.flags.set(WidgetFlag::isPressed);
2021-01-31 14:07:21 +00:00
}
widgetList.push_back(widget);
}
else if (desc.Type == "checkbox")
{
widget.type = WidgetType::checkbox;
2021-01-31 14:07:21 +00:00
widget.string = const_cast<utf8*>(desc.Text.c_str());
2025-06-09 22:29:06 +02:00
widget.flags.set(WidgetFlag::textIsString);
2021-01-31 14:07:21 +00:00
if (desc.IsChecked)
{
2025-06-09 22:29:06 +02:00
widget.flags.set(WidgetFlag::isPressed);
2021-01-31 14:07:21 +00:00
}
widgetList.push_back(widget);
}
else if (desc.Type == "colourpicker")
{
widget.type = WidgetType::colourBtn;
widget.image = getColourButtonImage(desc.Colour);
2021-01-31 14:07:21 +00:00
widgetList.push_back(widget);
}
else if (desc.Type == "custom")
{
widget.type = WidgetType::custom;
widgetList.push_back(widget);
}
2021-01-31 14:07:21 +00:00
else if (desc.Type == "dropdown")
{
widget.type = WidgetType::dropdownMenu;
2021-01-31 14:07:21 +00:00
if (desc.SelectedIndex >= 0 && static_cast<size_t>(desc.SelectedIndex) < desc.Items.size())
{
widget.string = const_cast<utf8*>(desc.Items[desc.SelectedIndex].c_str());
}
else
{
widget.string = const_cast<utf8*>("");
}
2025-06-09 22:29:06 +02:00
widget.flags.set(WidgetFlag::textIsString);
2021-01-31 14:07:21 +00:00
widgetList.push_back(widget);
// Add the dropdown button
widget = {};
widget.type = WidgetType::button;
2021-01-31 14:07:21 +00:00
widget.colour = 1;
widget.left = desc.X + desc.Width - 12;
widget.right = desc.X + desc.Width - 2;
widget.top = desc.Y + 1;
widget.bottom = desc.Y + desc.Height - 2;
widget.text = STR_DROPDOWN_GLYPH;
2025-01-19 16:59:45 +01:00
widget.tooltip = kStringIdNone;
2021-01-31 14:07:21 +00:00
if (desc.IsDisabled)
2025-06-09 22:29:06 +02:00
widget.flags.set(WidgetFlag::isDisabled);
2021-01-31 14:07:21 +00:00
widgetList.push_back(widget);
}
else if (desc.Type == "groupbox")
{
widget.type = WidgetType::groupbox;
2021-01-31 14:07:21 +00:00
widget.string = const_cast<utf8*>(desc.Text.c_str());
2025-06-09 22:29:06 +02:00
widget.flags.set(WidgetFlag::textIsString);
2021-01-31 14:07:21 +00:00
widgetList.push_back(widget);
}
else if (desc.Type == "label")
{
widget.type = WidgetType::label;
2021-01-31 14:07:21 +00:00
widget.string = const_cast<utf8*>(desc.Text.c_str());
2025-06-09 22:29:06 +02:00
widget.flags.set(WidgetFlag::textIsString);
2025-10-16 20:06:18 +02:00
if (desc.TextAlign == TextAlignment::centre)
2021-01-31 14:07:21 +00:00
{
widget.type = WidgetType::labelCentred;
2021-01-31 14:07:21 +00:00
}
widgetList.push_back(widget);
}
else if (desc.Type == "listview")
{
widget.type = WidgetType::scroll;
2021-01-31 14:07:21 +00:00
widget.content = 0;
if (desc.Scrollbars == ScrollbarType::Horizontal)
widget.content = SCROLL_HORIZONTAL;
else if (desc.Scrollbars == ScrollbarType::Vertical)
widget.content = SCROLL_VERTICAL;
else if (desc.Scrollbars == ScrollbarType::Both)
widget.content = SCROLL_BOTH;
widgetList.push_back(widget);
}
else if (desc.Type == "spinner")
{
widget.type = WidgetType::spinner;
2021-01-31 14:07:21 +00:00
widget.string = const_cast<utf8*>(desc.Text.c_str());
2025-06-09 22:29:06 +02:00
widget.flags.set(WidgetFlag::textIsString);
2021-01-31 14:07:21 +00:00
widgetList.push_back(widget);
// Add the increment button
2021-01-31 14:07:21 +00:00
widget = {};
widget.type = WidgetType::button;
2021-01-31 14:07:21 +00:00
widget.colour = 1;
widget.left = desc.X + desc.Width - 13;
widget.right = widget.left + 11;
2021-01-31 14:07:21 +00:00
widget.top = desc.Y + 1;
widget.bottom = desc.Y + desc.Height - 2;
widget.text = STR_NUMERIC_UP;
2025-01-19 16:59:45 +01:00
widget.tooltip = kStringIdNone;
2021-01-31 14:07:21 +00:00
if (desc.IsDisabled)
2025-06-09 22:29:06 +02:00
widget.flags.set(WidgetFlag::isDisabled);
widget.flags.set(WidgetFlag::isHoldable);
2021-01-31 14:07:21 +00:00
widgetList.push_back(widget);
// Add the decrement button
widget.left = desc.X + desc.Width - 26;
widget.right = widget.left + 12;
widget.text = STR_NUMERIC_DOWN;
2021-01-31 14:07:21 +00:00
widgetList.push_back(widget);
}
else if (desc.Type == "textbox")
{
widget.type = WidgetType::textBox;
2021-01-31 14:07:21 +00:00
widget.string = const_cast<utf8*>(desc.Text.c_str());
2025-06-09 22:29:06 +02:00
widget.flags.set(WidgetFlag::textIsString);
2021-01-31 14:07:21 +00:00
widgetList.push_back(widget);
}
else if (desc.Type == "viewport")
{
widget.type = WidgetType::viewport;
2025-01-19 16:59:45 +01:00
widget.text = kStringIdNone;
2021-01-31 14:07:21 +00:00
widgetList.push_back(widget);
2020-02-15 17:09:41 +00:00
}
}
2021-01-31 14:07:21 +00:00
2025-09-17 18:31:20 +02:00
static WindowNumber GetNewWindowNumber()
2021-01-31 14:07:21 +00:00
{
auto result = _nextWindowNumber++;
auto* windowMgr = GetWindowManager();
while (windowMgr->FindByNumber(WindowClass::custom, result) != nullptr)
2021-01-31 14:07:21 +00:00
{
result++;
}
return result;
}
};
2025-09-17 18:31:20 +02:00
WindowNumber CustomWindow::_nextWindowNumber;
WindowBase* WindowCustomOpen(std::shared_ptr<Plugin> owner, DukValue dukDesc)
2021-01-31 14:07:21 +00:00
{
auto desc = CustomWindowDesc::FromDukValue(dukDesc);
2025-09-20 21:46:42 +02:00
WindowFlags windowFlags = { WindowFlag::resizable, WindowFlag::transparent };
auto* windowMgr = GetWindowManager();
2021-01-31 14:07:21 +00:00
CustomWindow* window{};
if (desc.X && desc.Y)
{
window = windowMgr->Create<CustomWindow>(
WindowClass::custom, { *desc.X, *desc.Y }, desc.size, windowFlags, owner, desc);
2021-01-31 14:07:21 +00:00
}
else
{
window = windowMgr->Create<CustomWindow>(WindowClass::custom, desc.size, windowFlags, owner, desc);
2021-01-31 14:07:21 +00:00
}
return window;
2018-03-25 18:23:24 +01:00
}
2023-07-18 06:24:42 +01:00
static CustomWindowInfo& GetInfo(CustomWindow* w)
2018-03-25 18:23:24 +01:00
{
return w->getInfo();
2018-03-25 18:23:24 +01:00
}
2020-04-23 19:31:56 +01:00
static void InvokeEventHandler(const std::shared_ptr<Plugin>& owner, const DukValue& dukHandler)
2018-03-25 23:27:19 +01:00
{
std::vector<DukValue> args;
InvokeEventHandler(owner, dukHandler, args);
}
2020-04-23 19:31:56 +01:00
static void InvokeEventHandler(
const std::shared_ptr<Plugin>& owner, const DukValue& dukHandler, const std::vector<DukValue>& args)
2018-03-25 22:04:36 +01:00
{
auto& scriptEngine = GetContext()->GetScriptEngine();
scriptEngine.ExecutePluginCall(owner, dukHandler, args, false);
2018-03-25 22:04:36 +01:00
}
2020-02-14 23:36:24 +00:00
std::string GetWindowTitle(WindowBase* w)
{
if (w->classification == WindowClass::custom)
{
2023-07-18 06:24:42 +01:00
auto& customInfo = GetInfo(static_cast<CustomWindow*>(w));
return customInfo.Desc.Title;
}
return {};
}
void UpdateWindowTitle(WindowBase* w, std::string_view value)
{
if (w->classification == WindowClass::custom)
{
2023-07-18 06:24:42 +01:00
auto& customInfo = GetInfo(static_cast<CustomWindow*>(w));
customInfo.Desc.Title = value;
}
}
void UpdateWindowTab(WindowBase* w, int32_t tabIndex)
{
if (w->classification == WindowClass::custom)
{
2023-07-18 06:24:42 +01:00
auto& customInfo = GetInfo(static_cast<CustomWindow*>(w));
if (tabIndex >= 0 && tabIndex < static_cast<int32_t>(customInfo.Desc.Tabs.size()))
{
static_cast<CustomWindow*>(w)->ChangeTab(tabIndex);
}
}
}
void UpdateWidgetText(WindowBase* w, WidgetIndex widgetIndex, std::string_view value)
2020-02-14 23:36:24 +00:00
{
if (w->classification == WindowClass::custom)
2020-02-14 23:36:24 +00:00
{
2023-07-18 06:24:42 +01:00
auto& customInfo = GetInfo(static_cast<CustomWindow*>(w));
2020-04-30 20:32:39 +01:00
auto customWidgetInfo = customInfo.GetCustomWidgetDesc(w, widgetIndex);
2020-02-14 23:36:24 +00:00
if (customWidgetInfo != nullptr)
{
2020-10-16 00:13:52 +01:00
customWidgetInfo->Text = value;
2020-02-14 23:36:24 +00:00
w->widgets[widgetIndex].string = customWidgetInfo->Text.data();
auto* windowMgr = Ui::GetWindowManager();
windowMgr->InvalidateWidget(*w, widgetIndex);
2020-02-14 23:36:24 +00:00
}
}
}
void UpdateWidgetItems(WindowBase* w, WidgetIndex widgetIndex, const std::vector<std::string>& items)
{
if (w->classification == WindowClass::custom)
{
2023-07-18 06:24:42 +01:00
auto& customInfo = GetInfo(static_cast<CustomWindow*>(w));
auto customWidgetInfo = customInfo.GetCustomWidgetDesc(w, widgetIndex);
if (customWidgetInfo != nullptr)
{
customWidgetInfo->Items = items;
UpdateWidgetSelectedIndex(w, widgetIndex, customWidgetInfo->SelectedIndex);
}
}
}
void UpdateWidgetColour(WindowBase* w, WidgetIndex widgetIndex, colour_t colour)
{
if (w->classification == WindowClass::custom)
{
2023-07-18 06:24:42 +01:00
auto& customInfo = GetInfo(static_cast<CustomWindow*>(w));
auto customWidgetInfo = customInfo.GetCustomWidgetDesc(w, widgetIndex);
if (customWidgetInfo != nullptr)
{
auto& widget = w->widgets[widgetIndex];
auto lastColour = customWidgetInfo->Colour;
if (lastColour != colour && colour < COLOUR_COUNT)
{
customWidgetInfo->Colour = colour;
widget.image = getColourButtonImage(colour);
auto* windowMgr = Ui::GetWindowManager();
windowMgr->InvalidateWidget(*w, widgetIndex);
std::vector<DukValue> args;
auto ctx = customWidgetInfo->OnChange.context();
duk_push_int(ctx, colour);
args.push_back(DukValue::take_from_stack(ctx));
InvokeEventHandler(customInfo.Owner, customWidgetInfo->OnChange, args);
}
}
}
}
void UpdateWidgetSelectedIndex(WindowBase* w, WidgetIndex widgetIndex, int32_t selectedIndex)
{
if (w->classification == WindowClass::custom)
{
2023-07-18 06:24:42 +01:00
auto& customInfo = GetInfo(static_cast<CustomWindow*>(w));
auto customWidgetInfo = customInfo.GetCustomWidgetDesc(w, widgetIndex);
if (customWidgetInfo != nullptr)
{
auto& widget = w->widgets[widgetIndex];
auto lastSelectedIndex = customWidgetInfo->SelectedIndex;
if (customWidgetInfo->Items.size() == 0)
{
selectedIndex = -1;
}
else
{
if (selectedIndex < 0 || static_cast<size_t>(selectedIndex) >= customWidgetInfo->Items.size())
{
selectedIndex = 0;
}
}
if (selectedIndex == -1)
{
widget.string = const_cast<utf8*>("");
}
else
{
widget.string = const_cast<utf8*>(customWidgetInfo->Items[selectedIndex].c_str());
}
customWidgetInfo->SelectedIndex = selectedIndex;
auto* windowMgr = Ui::GetWindowManager();
windowMgr->InvalidateWidget(*w, widgetIndex);
if (lastSelectedIndex != selectedIndex)
{
std::vector<DukValue> args;
auto ctx = customWidgetInfo->OnChange.context();
duk_push_int(ctx, selectedIndex);
args.push_back(DukValue::take_from_stack(ctx));
InvokeEventHandler(customInfo.Owner, customWidgetInfo->OnChange, args);
}
}
}
}
std::vector<std::string> GetWidgetItems(WindowBase* w, WidgetIndex widgetIndex)
{
if (w->classification == WindowClass::custom)
{
2023-07-18 06:24:42 +01:00
auto& customInfo = GetInfo(static_cast<CustomWindow*>(w));
auto customWidgetInfo = customInfo.GetCustomWidgetDesc(w, widgetIndex);
if (customWidgetInfo != nullptr)
{
return customWidgetInfo->Items;
}
}
return {};
}
colour_t GetWidgetColour(WindowBase* w, WidgetIndex widgetIndex)
{
if (w->classification == WindowClass::custom)
{
2023-07-18 06:24:42 +01:00
auto& customInfo = GetInfo(static_cast<CustomWindow*>(w));
auto customWidgetInfo = customInfo.GetCustomWidgetDesc(w, widgetIndex);
if (customWidgetInfo != nullptr)
{
return customWidgetInfo->Colour;
}
}
return COLOUR_BLACK;
}
int32_t GetWidgetSelectedIndex(WindowBase* w, WidgetIndex widgetIndex)
{
if (w->classification == WindowClass::custom)
{
2023-07-18 06:24:42 +01:00
auto& customInfo = GetInfo(static_cast<CustomWindow*>(w));
auto customWidgetInfo = customInfo.GetCustomWidgetDesc(w, widgetIndex);
if (customWidgetInfo != nullptr)
{
return customWidgetInfo->SelectedIndex;
}
}
return -1;
}
WindowBase* FindCustomWindowByClassification(std::string_view classification)
{
2025-09-17 23:16:17 +02:00
for (const auto& w : gWindowList)
{
if (w->classification == WindowClass::custom)
{
2023-07-18 06:24:42 +01:00
const auto& customInfo = GetInfo(static_cast<CustomWindow*>(w.get()));
if (customInfo.Desc.Classification == classification)
{
return w.get();
}
}
}
return nullptr;
}
std::optional<WidgetIndex> FindWidgetIndexByName(WindowBase* w, std::string_view name)
{
if (w->classification == WindowClass::custom)
{
2023-07-18 06:24:42 +01:00
auto& customInfo = GetInfo(static_cast<CustomWindow*>(w));
2020-02-22 18:04:27 +00:00
for (size_t i = 0; i < customInfo.Widgets.size(); i++)
{
2020-04-30 20:32:39 +01:00
auto customWidgetInfo = customInfo.GetCustomWidgetDesc(w, i);
if (customWidgetInfo != nullptr)
{
if (customWidgetInfo->Name == name)
{
2022-08-21 18:49:23 +02:00
return static_cast<WidgetIndex>(i);
}
}
}
}
return std::nullopt;
}
std::string GetWidgetName(WindowBase* w, WidgetIndex widgetIndex)
2020-05-06 22:46:03 +01:00
{
if (w->classification == WindowClass::custom)
2020-05-06 22:46:03 +01:00
{
2023-07-18 06:24:42 +01:00
const auto& customInfo = GetInfo(static_cast<CustomWindow*>(w));
2020-05-06 22:46:03 +01:00
auto customWidgetInfo = customInfo.GetCustomWidgetDesc(w, widgetIndex);
if (customWidgetInfo != nullptr)
{
return customWidgetInfo->Name;
}
}
return {};
}
void SetWidgetName(WindowBase* w, WidgetIndex widgetIndex, std::string_view name)
2020-05-06 22:46:03 +01:00
{
if (w->classification == WindowClass::custom)
2020-05-06 22:46:03 +01:00
{
2023-07-18 06:24:42 +01:00
auto& customInfo = GetInfo(static_cast<CustomWindow*>(w));
2020-05-06 22:46:03 +01:00
auto customWidgetInfo = customInfo.GetCustomWidgetDesc(w, widgetIndex);
if (customWidgetInfo != nullptr)
{
customWidgetInfo->Name = std::string(name);
}
}
}
std::string GetWidgetTooltip(WindowBase* w, WidgetIndex widgetIndex)
{
if (w->classification == WindowClass::custom)
{
2023-07-18 06:24:42 +01:00
const auto& customInfo = GetInfo(static_cast<CustomWindow*>(w));
auto customWidgetInfo = customInfo.GetCustomWidgetDesc(w, widgetIndex);
if (customWidgetInfo != nullptr)
{
return customWidgetInfo->Tooltip;
}
}
return {};
}
void SetWidgetTooltip(WindowBase* w, WidgetIndex widgetIndex, std::string_view tooltip)
{
if (w->classification == WindowClass::custom)
{
2023-07-18 06:24:42 +01:00
auto& customInfo = GetInfo(static_cast<CustomWindow*>(w));
auto customWidgetInfo = customInfo.GetCustomWidgetDesc(w, widgetIndex);
if (customWidgetInfo != nullptr)
{
customWidgetInfo->Tooltip = std::string(tooltip);
}
}
}
CustomListView* GetCustomListView(WindowBase* w, WidgetIndex widgetIndex)
{
if (w->classification == WindowClass::custom)
{
2023-07-18 06:24:42 +01:00
auto& info = GetInfo(static_cast<CustomWindow*>(w));
auto scrollIndex = WindowGetScrollDataIndex(*w, widgetIndex);
if (scrollIndex < static_cast<int32_t>(info.ListViews.size()))
{
return &info.ListViews[scrollIndex];
}
}
return nullptr;
}
int32_t GetWidgetMaxLength(WindowBase* w, WidgetIndex widgetIndex)
2021-01-23 14:28:07 +00:00
{
if (w->classification == WindowClass::custom)
2021-01-23 14:28:07 +00:00
{
2023-07-18 06:24:42 +01:00
auto& customInfo = GetInfo(static_cast<CustomWindow*>(w));
2021-01-23 14:28:07 +00:00
auto customWidgetInfo = customInfo.GetCustomWidgetDesc(w, widgetIndex);
if (customWidgetInfo != nullptr)
{
return customWidgetInfo->MaxLength;
}
}
return 0;
}
void SetWidgetMaxLength(WindowBase* w, WidgetIndex widgetIndex, int32_t value)
2021-01-23 14:28:07 +00:00
{
if (w->classification == WindowClass::custom)
2021-01-23 14:28:07 +00:00
{
2023-07-18 06:24:42 +01:00
auto& customInfo = GetInfo(static_cast<CustomWindow*>(w));
2021-01-23 14:28:07 +00:00
auto customWidgetInfo = customInfo.GetCustomWidgetDesc(w, widgetIndex);
if (customWidgetInfo != nullptr)
{
customWidgetInfo->MaxLength = value;
}
}
}
void CloseWindowsOwnedByPlugin(std::shared_ptr<Plugin> plugin)
{
// Get all the windows that need closing
std::vector<WindowBase*> customWindows;
2025-09-17 23:16:17 +02:00
for (const auto& window : gWindowList)
{
if (window->classification == WindowClass::custom)
{
auto customWindow = reinterpret_cast<CustomWindow*>(window.get());
2023-07-18 06:24:42 +01:00
auto& customInfo = GetInfo(customWindow);
if (customInfo.Owner == plugin)
{
customWindows.push_back(window.get());
}
}
}
for (auto& window : customWindows)
{
auto* windowMgr = Ui::GetWindowManager();
windowMgr->Close(*window);
}
}
2018-07-31 17:59:48 +02:00
} // namespace OpenRCT2::Ui::Windows
2020-02-23 12:55:48 +00:00
#endif