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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

1484 lines
55 KiB
C++
Raw Normal View History

2018-03-25 18:23:24 +01:00
/*****************************************************************************
2026-01-05 14:42:59 +01:00
* Copyright (c) 2014-2026 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"
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>
2025-09-29 02:05:00 +10:00
#include <openrct2/interface/Viewport.h>
#include <openrct2/interface/WindowTypes.h>
#include <optional>
#include <string>
#include <utility>
#include <vector>
2018-03-25 18:23:24 +01:00
using namespace OpenRCT2;
using namespace OpenRCT2::Drawing;
2018-03-25 18:23:24 +01:00
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(
makeWindowShim(kStringIdNone, { 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{};
2026-01-28 10:06:45 +01:00
Drawing::Colour 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
2025-09-29 02:05:00 +10:00
JSCallback OnClick;
JSCallback OnChange;
JSCallback OnDraw;
JSCallback OnIncrement;
JSCallback OnDecrement;
JSCallback OnHighlight;
2018-03-25 22:04:36 +01:00
2025-09-29 02:05:00 +10:00
static CustomWidgetDesc FromJSValue(JSContext* ctx, JSValue desc)
2018-03-25 22:04:36 +01:00
{
CustomWidgetDesc result;
2025-09-29 02:05:00 +10:00
result.Type = JSToStdString(ctx, desc, "type");
result.X = JSToInt(ctx, desc, "x");
result.Y = JSToInt(ctx, desc, "y");
result.Width = JSToInt(ctx, desc, "width");
result.Height = JSToInt(ctx, desc, "height");
result.IsDisabled = AsOrDefault(ctx, desc, "isDisabled", false);
result.IsVisible = AsOrDefault(ctx, desc, "isVisible", true);
result.Name = AsOrDefault(ctx, desc, "name", "");
result.Tooltip = AsOrDefault(ctx, desc, "tooltip", "");
2018-03-25 23:27:19 +01:00
if (result.Type == "button")
{
2025-09-29 02:05:00 +10:00
JSValue jsImage = JS_GetPropertyStr(ctx, desc, "image");
if (JS_IsString(jsImage) || JS_IsNumber(jsImage))
2020-02-27 18:22:32 +00:00
{
2025-09-29 02:05:00 +10:00
result.Image = ImageId(ImageFromJSValue(ctx, jsImage));
2020-04-30 20:32:39 +01:00
result.HasBorder = false;
2020-02-27 18:22:32 +00:00
}
else
{
2025-09-29 02:05:00 +10:00
result.Text = AsOrDefault(ctx, desc, "text", "");
2020-04-30 20:32:39 +01:00
result.HasBorder = true;
2020-02-27 18:22:32 +00:00
}
2025-09-29 02:05:00 +10:00
JS_FreeValue(ctx, jsImage);
result.IsPressed = AsOrDefault(ctx, desc, "isPressed", false);
result.OnClick = JSToCallback(ctx, desc, "onClick");
2018-03-25 23:27:19 +01:00
}
2020-02-15 15:07:10 +00:00
else if (result.Type == "checkbox")
{
2025-09-29 02:05:00 +10:00
result.Text = AsOrDefault(ctx, desc, "text", "");
result.IsChecked = AsOrDefault(ctx, desc, "isChecked", false);
result.OnChange = JSToCallback(ctx, desc, "onChange");
2020-02-15 15:07:10 +00:00
}
else if (result.Type == "colourpicker")
{
2025-09-29 02:05:00 +10:00
auto colour = AsOrDefault(ctx, desc, "colour", 0);
2026-01-28 10:06:45 +01:00
if (colour < kColourNumTotal)
{
2026-01-28 10:06:45 +01:00
result.Colour = static_cast<Drawing::Colour>(colour);
}
2025-09-29 02:05:00 +10:00
result.OnChange = JSToCallback(ctx, desc, "onChange");
}
else if (result.Type == "custom")
{
2025-09-29 02:05:00 +10:00
result.OnDraw = JSToCallback(ctx, desc, "onDraw");
}
2018-03-25 23:27:19 +01:00
else if (result.Type == "dropdown")
{
2025-09-29 02:05:00 +10:00
JSIterateArray(ctx, desc, "items", [&result](JSContext* ctx2, JSValue val) {
result.Items.push_back(JSToStdString(ctx2, val));
});
result.SelectedIndex = AsOrDefault(ctx, desc, "selectedIndex", 0);
result.OnChange = JSToCallback(ctx, desc, "onChange");
2018-03-25 23:27:19 +01:00
}
2021-01-28 23:51:56 +00:00
else if (result.Type == "groupbox")
2020-02-12 21:56:18 +00:00
{
2025-09-29 02:05:00 +10:00
result.Text = AsOrDefault(ctx, desc, "text", "");
2020-02-12 21:56:18 +00:00
}
2021-01-28 23:51:56 +00:00
else if (result.Type == "label")
{
2025-09-29 02:05:00 +10:00
result.Text = AsOrDefault(ctx, desc, "text", "");
if (JSToStdString(ctx, desc, "textAlign") == "centred")
2021-01-28 23:51:56 +00:00
{
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")
{
2025-09-29 02:05:00 +10:00
JSValue cols = JS_GetPropertyStr(ctx, desc, "columns");
JSValue items = JS_GetPropertyStr(ctx, desc, "items");
JSValue selected = JS_GetPropertyStr(ctx, desc, "selectedCell");
JSValue scrollbars = JS_GetPropertyStr(ctx, desc, "scrollbars");
result.ListViewColumns = ListViewColumnVecFromJS(ctx, cols);
result.ListViewItems = ListViewItemVecFromJS(ctx, items);
result.SelectedCell = RowColumnFromJS(ctx, selected);
result.ShowColumnHeaders = AsOrDefault(ctx, desc, "showColumnHeaders", false);
result.IsStriped = AsOrDefault(ctx, desc, "isStriped", false);
result.OnClick = JSToCallback(ctx, desc, "onClick");
result.OnHighlight = JSToCallback(ctx, desc, "onHighlight");
result.CanSelect = AsOrDefault(ctx, desc, "canSelect", false);
if (JS_IsUndefined(scrollbars))
2026-08-05 23:05:01 +02:00
result.Scrollbars = ScrollbarType::vertical;
2020-05-09 16:06:21 +01:00
else
2025-09-29 02:05:00 +10:00
result.Scrollbars = ScrollbarTypeFromJS(ctx, scrollbars);
JS_FreeValue(ctx, cols);
JS_FreeValue(ctx, items);
JS_FreeValue(ctx, selected);
JS_FreeValue(ctx, scrollbars);
2020-05-02 20:11:07 +01:00
}
2020-02-27 19:44:51 +00:00
else if (result.Type == "spinner")
{
2025-09-29 02:05:00 +10:00
result.Text = AsOrDefault(ctx, desc, "text", "");
result.OnIncrement = JSToCallback(ctx, desc, "onIncrement");
result.OnDecrement = JSToCallback(ctx, desc, "onDecrement");
result.OnClick = JSToCallback(ctx, desc, "onClick");
2020-02-27 19:44:51 +00:00
}
2021-01-23 14:28:07 +00:00
else if (result.Type == "textbox")
{
2025-09-29 02:05:00 +10:00
result.Text = AsOrDefault(ctx, desc, "text", "");
result.MaxLength = AsOrDefault(ctx, desc, "maxLength", 32);
result.OnChange = JSToCallback(ctx, desc, "onChange");
2021-01-23 14:28:07 +00:00
}
2025-09-29 02:05:00 +10:00
result.HasBorder = AsOrDefault(ctx, 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;
2025-09-29 02:05:00 +10:00
static CustomTabDesc FromJSValue(JSContext* ctx, JSValue desc)
2020-04-30 20:32:39 +01:00
{
CustomTabDesc result;
2025-09-29 02:05:00 +10:00
JSValue jsImage = JS_GetPropertyStr(ctx, desc, "image");
if (JS_IsString(jsImage) || JS_IsNumber(jsImage))
2020-04-30 20:32:39 +01:00
{
2025-09-29 02:05:00 +10:00
result.imageFrameBase = ImageId(ImageFromJSValue(ctx, jsImage));
result.imageFrameCount = 0;
result.imageFrameDuration = 0;
2020-04-30 20:32:39 +01:00
}
2025-09-29 02:05:00 +10:00
else if (JS_IsObject(jsImage))
2020-04-30 20:32:39 +01:00
{
2025-09-29 02:05:00 +10:00
result.imageFrameBase = ImageId(JSToInt64(ctx, jsImage, "frameBase"));
result.imageFrameCount = AsOrDefault(ctx, jsImage, "frameCount", 0);
result.imageFrameDuration = AsOrDefault(ctx, jsImage, "frameDuration", 0);
2020-05-01 13:53:28 +01:00
2025-09-29 02:05:00 +10:00
auto primaryCol = JSToOptionalInt64(ctx, jsImage, "primaryColour");
if (primaryCol.has_value())
{
2025-09-29 02:05:00 +10:00
result.imageFrameBase = result.imageFrameBase.WithPrimary(static_cast<Colour>(primaryCol.value()));
2025-09-29 02:05:00 +10:00
auto secondaryCol = JSToOptionalInt64(ctx, jsImage, "secondaryColour");
if (secondaryCol.has_value())
{
2025-09-29 02:05:00 +10:00
result.imageFrameBase = result.imageFrameBase.WithSecondary(static_cast<Colour>(secondaryCol.value()));
2025-09-29 02:05:00 +10:00
auto tertiaryCol = JSToOptionalInt64(ctx, jsImage, "tertiaryColour");
if (tertiaryCol.has_value())
{
2026-01-28 10:06:45 +01:00
result.imageFrameBase = result.imageFrameBase.WithTertiary(
2025-09-29 02:05:00 +10:00
static_cast<Colour>(tertiaryCol.value()));
}
}
}
2025-09-29 02:05:00 +10:00
JSValue jsCoord = JS_GetPropertyStr(ctx, jsImage, "offset");
if (JS_IsObject(jsCoord))
2020-05-01 13:53:28 +01:00
{
2025-09-29 02:05:00 +10:00
result.offset = { AsOrDefault(ctx, jsCoord, "x", 0), AsOrDefault(ctx, jsCoord, "y", 0) };
2020-05-01 13:53:28 +01:00
}
2025-09-29 02:05:00 +10:00
JS_FreeValue(ctx, jsCoord);
2020-04-30 20:32:39 +01:00
}
2025-09-29 02:05:00 +10:00
JS_FreeValue(ctx, jsImage);
JSIterateArray(ctx, desc, "widgets", [&result](JSContext* ctx2, JSValue x) {
result.Widgets.push_back(CustomWidgetDesc::FromJSValue(ctx2, x));
});
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
2025-09-29 02:05:00 +10:00
JSCallback OnClose;
JSCallback OnUpdate;
JSCallback 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;
}
2025-09-29 02:05:00 +10:00
static CustomWindowDesc FromJSValue(JSContext* ctx, JSValue desc)
2018-03-25 18:23:24 +01:00
{
CustomWindowDesc result;
2025-09-29 02:05:00 +10:00
result.Classification = JSToStdString(ctx, desc, "classification");
result.X = JSToOptionalInt(ctx, desc, "x");
result.Y = JSToOptionalInt(ctx, desc, "y");
result.size.width = JSToInt(ctx, desc, "width");
result.size.height = JSToInt(ctx, desc, "height");
result.MinWidth = JSToOptionalInt(ctx, desc, "minWidth");
result.MaxWidth = JSToOptionalInt(ctx, desc, "maxWidth");
result.MinHeight = JSToOptionalInt(ctx, desc, "minHeight");
result.MaxHeight = JSToOptionalInt(ctx, desc, "maxHeight");
result.Title = JSToStdString(ctx, desc, "title");
result.Id = JSToOptionalInt(ctx, desc, "id");
result.TabIndex = JSToOptionalInt(ctx, desc, "tabIndex");
2018-03-25 22:04:36 +01:00
2025-09-29 02:05:00 +10:00
JSIterateArray(ctx, desc, "widgets", [&result](JSContext* ctx2, JSValue val) {
result.Widgets.push_back(CustomWidgetDesc::FromJSValue(ctx2, val));
});
2018-03-25 22:04:36 +01:00
2025-09-29 02:05:00 +10:00
JSIterateArray(ctx, desc, "tabs", [&result](JSContext* ctx2, JSValue x) {
result.Tabs.push_back(CustomTabDesc::FromJSValue(ctx2, x));
});
2020-04-30 20:32:39 +01:00
2025-09-29 02:05:00 +10:00
JSIterateArray(ctx, desc, "colours", [&result](JSContext* ctx2, JSValue x) {
ColourWithFlags c = { Colour::black };
if (JS_IsNumber(x))
{
int32_t xValue = JSToInt(ctx2, x);
uint8_t colour = (xValue & ~kLegacyColourFlagTranslucent) % kColourNumTotal;
bool isTranslucent = (xValue & kLegacyColourFlagTranslucent);
c.colour = static_cast<Colour>(colour);
c.flags.set(ColourFlag::translucent, isTranslucent);
}
result.Colours.push_back(c);
});
2020-02-27 19:59:46 +00:00
2025-09-29 02:05:00 +10:00
result.OnClose = JSToCallback(ctx, desc, "onClose");
result.OnUpdate = JSToCallback(ctx, desc, "onUpdate");
result.OnTabChange = JSToCallback(ctx, 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
}
};
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);
2025-09-29 02:05:00 +10:00
static void InvokeEventHandler(const std::shared_ptr<Plugin>& owner, const JSCallback& jsCallback);
2018-07-31 17:59:48 +02:00
static void InvokeEventHandler(
2025-09-29 02:05:00 +10:00
const std::shared_ptr<Plugin>& owner, const JSCallback& jsCallback, const std::vector<JSValue>& 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
2026-01-28 10:06:45 +01:00
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;
widgets[WIDX_TITLE].setString(desc.Title.c_str());
2021-01-31 14:07:21 +00:00
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;
2026-08-05 23:05:01 +02:00
if (listView.GetScrollbars() == ScrollbarType::horizontal
|| listView.GetScrollbars() == ScrollbarType::both)
2021-01-31 14:07:21 +00:00
{
wheight -= kScrollBarWidth + 1;
2021-01-31 14:07:21 +00:00
}
2026-08-05 23:05:01 +02:00
if (listView.GetScrollbars() == ScrollbarType::vertical || listView.GetScrollbars() == ScrollbarType::both)
2021-01-31 14:07:21 +00:00
{
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;
2025-09-29 02:05:00 +10:00
if (onDraw.IsValid())
{
RenderTarget widgetRT;
if (ClipRenderTarget(
widgetRT, rt, { windowPos.x + widget.left, windowPos.y + widget.top }, widget.width() - 1,
2025-12-12 18:26:05 +01:00
widget.height() - 1))
{
2025-09-29 02:05:00 +10:00
auto ctx = onDraw.context;
auto jsWidget = gScWidget.New(ctx, this, widgetIndex);
auto gfx = gScGraphicsContext.New(ctx, widgetRT);
auto& scriptEngine = GetContext()->GetScriptEngine();
2025-09-29 02:05:00 +10:00
scriptEngine.ExecutePluginCall(_info.Owner, onDraw.callback, jsWidget, { gfx }, false);
JS_FreeValue(ctx, jsWidget);
}
}
}
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 = 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
2025-09-29 02:05:00 +10:00
std::vector<JSValue> args;
args.push_back(JS_NewBool(widgetDesc->OnChange.context, isChecked));
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,
{}, 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);
2025-09-29 02:05:00 +10:00
std::vector<JSValue> args;
std::string textStr(text);
args.push_back(JSFromStdString(widgetDesc->OnChange.context, textStr));
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
if (!_info.Desc.Tabs.empty())
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;
if (!_info.Desc.Tabs.empty())
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;
widget.setString(desc.Text.c_str());
}
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.setString(desc.Items[desc.SelectedIndex].c_str());
2021-01-31 14:07:21 +00:00
}
else
{
widget.setString("");
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;
widget.setString(desc.Text.c_str());
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;
2026-08-05 23:05:01 +02:00
if (desc.Scrollbars == ScrollbarType::horizontal)
2021-01-31 14:07:21 +00:00
widget.content = SCROLL_HORIZONTAL;
2026-08-05 23:05:01 +02:00
else if (desc.Scrollbars == ScrollbarType::vertical)
2021-01-31 14:07:21 +00:00
widget.content = SCROLL_VERTICAL;
2026-08-05 23:05:01 +02:00
else if (desc.Scrollbars == ScrollbarType::both)
2021-01-31 14:07:21 +00:00
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;
widget.setString(desc.Text.c_str());
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;
2025-09-29 02:05:00 +10:00
WindowBase* WindowCustomOpen(JSContext* ctx, std::shared_ptr<Plugin> owner, JSValue descVal)
2021-01-31 14:07:21 +00:00
{
2025-09-29 02:05:00 +10:00
auto desc = CustomWindowDesc::FromJSValue(ctx, descVal);
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
}
2025-09-29 02:05:00 +10:00
static void InvokeEventHandler(const std::shared_ptr<Plugin>& owner, const JSCallback& jsCallback)
2018-03-25 23:27:19 +01:00
{
2025-09-29 02:05:00 +10:00
std::vector<JSValue> args;
InvokeEventHandler(owner, jsCallback, args);
2018-03-25 23:27:19 +01:00
}
2020-04-23 19:31:56 +01:00
static void InvokeEventHandler(
2025-09-29 02:05:00 +10:00
const std::shared_ptr<Plugin>& owner, const JSCallback& jsCallback, const std::vector<JSValue>& args)
2018-03-25 22:04:36 +01:00
{
auto& scriptEngine = GetContext()->GetScriptEngine();
2025-09-29 02:05:00 +10:00
scriptEngine.ExecutePluginCall(owner, jsCallback.callback, 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 = 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);
}
}
}
2026-01-28 10:06:45 +01:00
void UpdateWidgetColour(WindowBase* w, WidgetIndex widgetIndex, Colour 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;
2026-01-28 10:06:45 +01:00
if (lastColour != colour && colourIsValid(colour))
{
customWidgetInfo->Colour = colour;
widget.image = getColourButtonImage(colour);
auto* windowMgr = GetWindowManager();
windowMgr->InvalidateWidget(*w, widgetIndex);
2025-09-29 02:05:00 +10:00
std::vector<JSValue> args;
args.push_back(JS_NewInt32(customWidgetInfo->OnChange.context, EnumValue(colour)));
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.empty())
{
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 = GetWindowManager();
windowMgr->InvalidateWidget(*w, widgetIndex);
if (lastSelectedIndex != selectedIndex)
{
2025-09-29 02:05:00 +10:00
std::vector<JSValue> args;
args.push_back(JS_NewInt32(customWidgetInfo->OnChange.context, selectedIndex));
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 {};
}
2026-01-28 10:06:45 +01:00
Colour 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;
}
}
2026-01-28 10:06:45 +01:00
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 = 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