2022-02-24 00:42:54 +00:00
|
|
|
/*****************************************************************************
|
2026-01-05 14:42:59 +01:00
|
|
|
* Copyright (c) 2014-2026 OpenRCT2 developers
|
2022-02-24 00:42:54 +00: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
|
|
|
|
|
|
2024-11-29 12:04:56 +09:00
|
|
|
#include "CustomImages.h"
|
2022-02-24 00:42:54 +00:00
|
|
|
|
2024-11-29 12:04:56 +09:00
|
|
|
#include "ScGraphicsContext.hpp"
|
2022-02-25 20:05:10 +00:00
|
|
|
|
2024-11-29 12:04:56 +09:00
|
|
|
#include <openrct2/Context.h>
|
2026-07-21 16:00:55 +01:00
|
|
|
#include <openrct2/drawing/Drawing.Sprite.h>
|
2024-11-29 12:04:56 +09:00
|
|
|
#include <openrct2/drawing/Image.h>
|
|
|
|
|
#include <openrct2/drawing/ImageImporter.h>
|
2026-07-21 16:00:55 +01:00
|
|
|
#include <openrct2/drawing/NewDrawing.h>
|
2024-11-29 12:04:56 +09:00
|
|
|
#include <openrct2/drawing/X8DrawingEngine.h>
|
|
|
|
|
#include <openrct2/scripting/Plugin.h>
|
2025-09-29 02:05:00 +10:00
|
|
|
#include <thirdparty/base64.hpp>
|
2022-02-25 18:49:52 +00:00
|
|
|
|
2022-02-24 20:49:35 +00:00
|
|
|
using namespace OpenRCT2::Drawing;
|
|
|
|
|
|
2022-02-24 00:42:54 +00:00
|
|
|
namespace OpenRCT2::Scripting
|
|
|
|
|
{
|
|
|
|
|
enum class PixelDataKind
|
|
|
|
|
{
|
2026-08-05 23:32:25 +02:00
|
|
|
unknown,
|
|
|
|
|
raw,
|
|
|
|
|
rle,
|
|
|
|
|
palette,
|
|
|
|
|
png
|
2022-02-24 00:42:54 +00:00
|
|
|
};
|
|
|
|
|
|
2022-02-24 20:49:35 +00:00
|
|
|
enum class PixelDataPaletteKind
|
|
|
|
|
{
|
|
|
|
|
None,
|
|
|
|
|
Keep,
|
|
|
|
|
Closest,
|
|
|
|
|
Dither
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-24 00:42:54 +00:00
|
|
|
struct PixelData
|
|
|
|
|
{
|
|
|
|
|
PixelDataKind Type;
|
|
|
|
|
int32_t Width;
|
|
|
|
|
int32_t Height;
|
|
|
|
|
int32_t Stride;
|
2022-02-24 20:49:35 +00:00
|
|
|
PixelDataPaletteKind Palette;
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue Data;
|
2022-02-24 00:42:54 +00:00
|
|
|
};
|
|
|
|
|
|
2022-02-25 18:49:52 +00:00
|
|
|
struct AllocatedImageList
|
|
|
|
|
{
|
|
|
|
|
std::shared_ptr<Plugin> Owner;
|
|
|
|
|
ImageList Range;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static std::vector<AllocatedImageList> _allocatedImages;
|
|
|
|
|
|
|
|
|
|
static void FreeImages(ImageList range)
|
|
|
|
|
{
|
|
|
|
|
for (ImageIndex i = 0; i < range.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
auto index = range.BaseId + i;
|
2023-01-16 18:52:17 +00:00
|
|
|
auto g1 = GfxGetG1Element(index);
|
2022-02-25 18:49:52 +00:00
|
|
|
if (g1 != nullptr)
|
|
|
|
|
{
|
|
|
|
|
// Free pixel data
|
|
|
|
|
delete[] g1->offset;
|
|
|
|
|
|
|
|
|
|
// Replace slot with empty element
|
2023-01-19 08:16:44 +00:00
|
|
|
G1Element empty{};
|
2023-01-16 18:52:17 +00:00
|
|
|
GfxSetG1Element(index, &empty);
|
2022-02-25 18:49:52 +00:00
|
|
|
}
|
|
|
|
|
}
|
2023-01-16 22:31:34 +00:00
|
|
|
GfxObjectFreeImages(range.BaseId, range.Count);
|
2022-02-25 18:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<ImageList> AllocateCustomImages(const std::shared_ptr<Plugin>& plugin, uint32_t count)
|
|
|
|
|
{
|
2023-01-19 08:16:44 +00:00
|
|
|
std::vector<G1Element> images;
|
2022-02-25 18:49:52 +00:00
|
|
|
images.resize(count);
|
|
|
|
|
|
2023-01-16 22:31:34 +00:00
|
|
|
auto base = GfxObjectAllocateImages(images.data(), count);
|
2025-01-19 16:59:45 +01:00
|
|
|
if (base == kImageIndexUndefined)
|
2022-02-25 18:49:52 +00:00
|
|
|
{
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
auto range = ImageList(base, count);
|
|
|
|
|
|
|
|
|
|
AllocatedImageList item;
|
|
|
|
|
item.Owner = plugin;
|
|
|
|
|
item.Range = range;
|
|
|
|
|
_allocatedImages.push_back(std::move(item));
|
|
|
|
|
return range;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FreeCustomImages(const std::shared_ptr<Plugin>& plugin, ImageList range)
|
|
|
|
|
{
|
|
|
|
|
auto it = std::find_if(
|
|
|
|
|
_allocatedImages.begin(), _allocatedImages.end(),
|
|
|
|
|
[&plugin, range](const AllocatedImageList& item) { return item.Owner == plugin && item.Range == range; });
|
|
|
|
|
if (it == _allocatedImages.end())
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FreeImages(it->Range);
|
|
|
|
|
_allocatedImages.erase(it);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DoesPluginOwnImage(const std::shared_ptr<Plugin>& plugin, ImageIndex index)
|
|
|
|
|
{
|
|
|
|
|
auto it = std::find_if(
|
|
|
|
|
_allocatedImages.begin(), _allocatedImages.end(),
|
|
|
|
|
[&plugin, index](const AllocatedImageList& item) { return item.Owner == plugin && item.Range.Contains(index); });
|
|
|
|
|
return it != _allocatedImages.end();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void FreeCustomImages(const std::shared_ptr<Plugin>& plugin)
|
|
|
|
|
{
|
|
|
|
|
auto it = _allocatedImages.begin();
|
|
|
|
|
while (it != _allocatedImages.end())
|
|
|
|
|
{
|
|
|
|
|
if (it->Owner == plugin)
|
|
|
|
|
{
|
|
|
|
|
FreeImages(it->Range);
|
|
|
|
|
it = _allocatedImages.erase(it);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
it++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void InitialiseCustomImages(ScriptEngine& scriptEngine)
|
|
|
|
|
{
|
|
|
|
|
scriptEngine.SubscribeToPluginStoppedEvent([](std::shared_ptr<Plugin> plugin) -> void { FreeCustomImages(plugin); });
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue JSGetImageInfo(JSContext* ctx, ImageIndex id)
|
2022-02-24 00:42:54 +00:00
|
|
|
{
|
2023-01-16 18:52:17 +00:00
|
|
|
auto* g1 = GfxGetG1Element(id);
|
2022-02-24 00:42:54 +00:00
|
|
|
if (g1 == nullptr)
|
|
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
return JS_UNDEFINED;
|
2022-02-24 00:42:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue obj = JS_NewObject(ctx);
|
|
|
|
|
JS_SetPropertyStr(ctx, obj, "id", JS_NewInt32(ctx, id));
|
|
|
|
|
JS_SetPropertyStr(ctx, obj, "offset", ToJSValue(ctx, ScreenCoordsXY{ g1->xOffset, g1->yOffset }));
|
|
|
|
|
JS_SetPropertyStr(ctx, obj, "width", JS_NewInt32(ctx, g1->width));
|
|
|
|
|
JS_SetPropertyStr(ctx, obj, "height", JS_NewInt32(ctx, g1->height));
|
2022-02-24 00:42:54 +00:00
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_SetPropertyStr(ctx, obj, "hasTransparent", JS_NewBool(ctx, g1->flags.has(G1Flag::hasTransparency)));
|
|
|
|
|
JS_SetPropertyStr(ctx, obj, "isRLE", JS_NewBool(ctx, g1->flags.has(G1Flag::hasRLECompression)));
|
|
|
|
|
JS_SetPropertyStr(ctx, obj, "isPalette", JS_NewBool(ctx, g1->flags.has(G1Flag::isPalette)));
|
|
|
|
|
JS_SetPropertyStr(ctx, obj, "noZoom", JS_NewBool(ctx, g1->flags.has(G1Flag::noZoomDraw)));
|
2022-02-24 00:42:54 +00:00
|
|
|
|
2025-12-27 14:56:15 +01:00
|
|
|
if (g1->flags.has(G1Flag::hasZoomSprite))
|
2022-02-24 00:42:54 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_SetPropertyStr(ctx, obj, "nextZoomId", JS_NewInt32(ctx, id - g1->zoomedOffset));
|
2022-02-24 00:42:54 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_SetPropertyStr(ctx, obj, "nextZoomId", JS_UNDEFINED);
|
2022-02-24 00:42:54 +00:00
|
|
|
}
|
2025-09-29 02:05:00 +10:00
|
|
|
return obj;
|
2022-02-24 00:42:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
static std::string_view GetPixelDataTypeForG1(const G1Element& g1)
|
2022-02-24 00:42:54 +00:00
|
|
|
{
|
2025-12-27 14:56:15 +01:00
|
|
|
if (g1.flags.has(G1Flag::hasRLECompression))
|
2022-02-24 00:42:54 +00:00
|
|
|
return "rle";
|
2025-12-27 14:56:15 +01:00
|
|
|
else if (g1.flags.has(G1Flag::isPalette))
|
2022-02-24 00:42:54 +00:00
|
|
|
return "palette";
|
|
|
|
|
return "raw";
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue JSGetImagePixelData(JSContext* ctx, ImageIndex id)
|
2022-02-24 00:42:54 +00:00
|
|
|
{
|
2023-01-16 18:52:17 +00:00
|
|
|
auto* g1 = GfxGetG1Element(id);
|
2022-02-24 00:42:54 +00:00
|
|
|
if (g1 == nullptr)
|
|
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
return JS_UNDEFINED;
|
2022-02-24 00:42:54 +00:00
|
|
|
}
|
2023-01-16 18:52:17 +00:00
|
|
|
auto dataSize = G1CalculateDataSize(g1);
|
2025-09-29 02:05:00 +10:00
|
|
|
auto type = GetPixelDataTypeForG1(*g1);
|
2022-02-24 00:42:54 +00:00
|
|
|
|
|
|
|
|
// Copy the G1 data to a JS buffer wrapped in a Uint8Array
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue data = JS_NewUint8ArrayCopy(ctx, g1->offset, dataSize);
|
2022-02-24 00:42:54 +00:00
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue obj = JS_NewObject(ctx);
|
|
|
|
|
JS_SetPropertyStr(ctx, obj, "type", JSFromStdString(ctx, type));
|
|
|
|
|
JS_SetPropertyStr(ctx, obj, "width", JS_NewInt32(ctx, g1->width));
|
|
|
|
|
JS_SetPropertyStr(ctx, obj, "height", JS_NewInt32(ctx, g1->height));
|
|
|
|
|
JS_SetPropertyStr(ctx, obj, "data", data);
|
|
|
|
|
return obj;
|
2022-02-24 00:42:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
static std::vector<uint8_t> GetDataFromBufferLikeObject(JSContext* ctx, JSValue data)
|
2022-02-24 00:42:54 +00:00
|
|
|
{
|
2022-02-24 20:49:35 +00:00
|
|
|
std::vector<uint8_t> result;
|
2025-09-29 02:05:00 +10:00
|
|
|
if (JS_IsArray(data))
|
2022-02-24 00:42:54 +00:00
|
|
|
{
|
|
|
|
|
// From array of numbers
|
2025-09-29 02:05:00 +10:00
|
|
|
int64_t arrSz = 0;
|
|
|
|
|
JS_GetLength(ctx, data, &arrSz);
|
|
|
|
|
if (arrSz > 0)
|
2022-02-24 00:42:54 +00:00
|
|
|
{
|
2026-04-01 01:47:26 +11:00
|
|
|
result.reserve(arrSz);
|
2025-09-29 02:05:00 +10:00
|
|
|
JSIterateArray(ctx, data, [&result](JSContext* ctx2, JSValue val) { result.push_back(JSToInt(ctx2, val)); });
|
2022-02-24 00:42:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
2025-09-29 02:05:00 +10:00
|
|
|
else if (JS_IsString(data))
|
2022-02-24 00:42:54 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
std::string str = JSToStdString(ctx, data);
|
|
|
|
|
result = base64::decode_into<std::vector<uint8_t>>(str);
|
2022-02-24 00:42:54 +00:00
|
|
|
}
|
2026-04-02 06:59:39 +02:00
|
|
|
else if (JS_GetTypedArrayType(data) == JSTypedArrayEnum::JS_TYPED_ARRAY_UINT8)
|
2022-02-24 00:42:54 +00:00
|
|
|
{
|
|
|
|
|
// From Uint8Array
|
2025-09-29 02:05:00 +10:00
|
|
|
size_t sz = 0;
|
|
|
|
|
uint8_t* arr = JS_GetUint8Array(ctx, &sz, data);
|
|
|
|
|
if (arr)
|
|
|
|
|
{
|
|
|
|
|
result = std::vector<uint8_t>(arr, arr + sz);
|
|
|
|
|
}
|
2022-02-24 00:42:54 +00:00
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 20:49:35 +00:00
|
|
|
static std::vector<uint8_t> RemovePadding(const std::vector<uint8_t>& srcData, const PixelData& pixelData)
|
2022-02-24 00:42:54 +00:00
|
|
|
{
|
2022-02-24 20:49:35 +00:00
|
|
|
std::vector<uint8_t> unpadded(pixelData.Width * pixelData.Height, 0);
|
|
|
|
|
auto* src = srcData.data();
|
|
|
|
|
auto* dst = unpadded.data();
|
|
|
|
|
for (int32_t y = 0; y < pixelData.Height; y++)
|
2022-02-24 00:42:54 +00:00
|
|
|
{
|
2022-02-24 20:49:35 +00:00
|
|
|
std::memcpy(dst, src, pixelData.Width);
|
|
|
|
|
src += pixelData.Stride;
|
|
|
|
|
dst += pixelData.Width;
|
2022-02-24 00:42:54 +00:00
|
|
|
}
|
2022-02-24 20:49:35 +00:00
|
|
|
return unpadded;
|
|
|
|
|
}
|
2022-02-24 00:42:54 +00:00
|
|
|
|
2024-03-21 18:27:07 +01:00
|
|
|
static ImportMode getImportModeFromPalette(const PixelDataPaletteKind& palette)
|
|
|
|
|
{
|
|
|
|
|
switch (palette)
|
|
|
|
|
{
|
|
|
|
|
case PixelDataPaletteKind::Closest:
|
|
|
|
|
return ImportMode::Closest;
|
|
|
|
|
case PixelDataPaletteKind::Dither:
|
|
|
|
|
return ImportMode::Dithering;
|
|
|
|
|
case PixelDataPaletteKind::None:
|
|
|
|
|
case PixelDataPaletteKind::Keep:
|
|
|
|
|
default:
|
|
|
|
|
return ImportMode::Default;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
static std::vector<uint8_t> GetBufferFromPixelData(JSContext* ctx, PixelData& pixelData)
|
2022-02-24 20:49:35 +00:00
|
|
|
{
|
|
|
|
|
std::vector<uint8_t> imageData;
|
2022-02-24 00:42:54 +00:00
|
|
|
switch (pixelData.Type)
|
|
|
|
|
{
|
2026-08-05 23:32:25 +02:00
|
|
|
case PixelDataKind::raw:
|
2022-02-24 00:42:54 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
auto data = GetDataFromBufferLikeObject(ctx, pixelData.Data);
|
2022-02-24 20:49:35 +00:00
|
|
|
if (pixelData.Stride != pixelData.Width)
|
2022-02-24 00:42:54 +00:00
|
|
|
{
|
2022-02-24 20:49:35 +00:00
|
|
|
// Make sure data is expected size for RemovePadding
|
|
|
|
|
data.resize(pixelData.Stride * pixelData.Height);
|
|
|
|
|
data = RemovePadding(data, pixelData);
|
2022-02-24 00:42:54 +00:00
|
|
|
}
|
2022-02-24 20:49:35 +00:00
|
|
|
|
|
|
|
|
// Make sure data is expected size
|
|
|
|
|
data.resize(pixelData.Width * pixelData.Height);
|
2022-04-02 19:24:01 +01:00
|
|
|
imageData = std::move(data);
|
2022-02-24 00:42:54 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2026-08-05 23:32:25 +02:00
|
|
|
case PixelDataKind::rle:
|
2022-02-24 00:42:54 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
imageData = GetDataFromBufferLikeObject(ctx, pixelData.Data);
|
2022-02-24 00:42:54 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2026-08-05 23:32:25 +02:00
|
|
|
case PixelDataKind::png:
|
2022-02-24 20:49:35 +00:00
|
|
|
{
|
2025-03-26 11:59:12 +01:00
|
|
|
auto imageFormat = pixelData.Palette == PixelDataPaletteKind::Keep ? ImageFormat::png : ImageFormat::png32;
|
2024-03-21 18:27:07 +01:00
|
|
|
auto palette = pixelData.Palette == PixelDataPaletteKind::Keep ? Palette::KeepIndices : Palette::OpenRCT2;
|
|
|
|
|
auto importMode = getImportModeFromPalette(pixelData.Palette);
|
2025-09-29 02:05:00 +10:00
|
|
|
auto pngData = GetDataFromBufferLikeObject(ctx, pixelData.Data);
|
2022-02-24 20:49:35 +00:00
|
|
|
auto image = Imaging::ReadFromBuffer(pngData, imageFormat);
|
2026-01-31 13:25:29 +01:00
|
|
|
constexpr ImportFlags flags = { ImportFlag::rle };
|
2024-08-07 18:42:09 +02:00
|
|
|
ImageImportMeta meta = { { 0, 0 }, palette, flags, importMode };
|
2022-02-24 20:49:35 +00:00
|
|
|
|
|
|
|
|
ImageImporter importer;
|
2024-03-21 18:27:07 +01:00
|
|
|
auto importResult = importer.Import(image, meta);
|
2022-02-24 20:49:35 +00:00
|
|
|
|
2026-08-05 23:32:25 +02:00
|
|
|
pixelData.Type = PixelDataKind::rle;
|
2022-02-24 20:49:35 +00:00
|
|
|
pixelData.Width = importResult.Element.width;
|
|
|
|
|
pixelData.Height = importResult.Element.height;
|
|
|
|
|
|
|
|
|
|
imageData = std::move(importResult.Buffer);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-02-24 00:42:54 +00:00
|
|
|
default:
|
|
|
|
|
throw std::runtime_error("Unsupported pixel data type.");
|
|
|
|
|
}
|
2022-02-24 20:49:35 +00:00
|
|
|
return imageData;
|
2022-02-24 00:42:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
static PixelDataKind PixelDataKindFromJS(const std::string& s)
|
2022-02-24 00:42:54 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
if (s == "raw")
|
2026-08-05 23:32:25 +02:00
|
|
|
return PixelDataKind::raw;
|
2025-09-29 02:05:00 +10:00
|
|
|
if (s == "rle")
|
2026-08-05 23:32:25 +02:00
|
|
|
return PixelDataKind::rle;
|
2025-09-29 02:05:00 +10:00
|
|
|
if (s == "palette")
|
2026-08-05 23:32:25 +02:00
|
|
|
return PixelDataKind::palette;
|
2025-09-29 02:05:00 +10:00
|
|
|
if (s == "png")
|
2026-08-05 23:32:25 +02:00
|
|
|
return PixelDataKind::png;
|
|
|
|
|
return PixelDataKind::unknown;
|
2022-02-24 00:42:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
static PixelDataPaletteKind PixelDataPaletteKindFromJS(const std::string& s)
|
2022-02-24 20:49:35 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
if (s == "keep")
|
|
|
|
|
return PixelDataPaletteKind::Keep;
|
|
|
|
|
if (s == "closest")
|
|
|
|
|
return PixelDataPaletteKind::Closest;
|
|
|
|
|
if (s == "dither")
|
|
|
|
|
return PixelDataPaletteKind::Dither;
|
2022-02-24 20:49:35 +00:00
|
|
|
return PixelDataPaletteKind::None;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
static PixelData GetPixelDataFromJS(JSContext* ctx, JSValue jsPixelData)
|
2022-02-24 00:42:54 +00:00
|
|
|
{
|
|
|
|
|
PixelData pixelData;
|
2025-09-29 02:05:00 +10:00
|
|
|
pixelData.Type = PixelDataKindFromJS(JSToStdString(ctx, jsPixelData, "type"));
|
|
|
|
|
pixelData.Palette = PixelDataPaletteKindFromJS(JSToStdString(ctx, jsPixelData, "palette"));
|
|
|
|
|
pixelData.Width = AsOrDefault(ctx, jsPixelData, "width", static_cast<int32_t>(0));
|
|
|
|
|
pixelData.Height = AsOrDefault(ctx, jsPixelData, "height", static_cast<int32_t>(0));
|
|
|
|
|
pixelData.Stride = AsOrDefault(ctx, jsPixelData, "stride", static_cast<int32_t>(pixelData.Width));
|
|
|
|
|
// Note: this must be JS_FreeValued
|
|
|
|
|
pixelData.Data = JS_GetPropertyStr(ctx, jsPixelData, "data");
|
2022-02-24 00:42:54 +00:00
|
|
|
return pixelData;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 20:49:35 +00:00
|
|
|
static void ReplacePixelDataForImage(ImageIndex id, const PixelData& pixelData, std::vector<uint8_t>&& data)
|
2022-02-24 00:42:54 +00:00
|
|
|
{
|
|
|
|
|
// Setup the g1 element
|
2023-01-19 08:16:44 +00:00
|
|
|
G1Element el{};
|
2023-01-16 18:52:17 +00:00
|
|
|
auto* lastel = GfxGetG1Element(id);
|
2022-02-24 00:42:54 +00:00
|
|
|
if (lastel != nullptr)
|
|
|
|
|
{
|
|
|
|
|
el = *lastel;
|
|
|
|
|
delete[] el.offset;
|
|
|
|
|
}
|
2022-02-24 20:49:35 +00:00
|
|
|
|
|
|
|
|
// Copy data into new unmanaged uint8_t[]
|
|
|
|
|
auto newData = new uint8_t[data.size()];
|
|
|
|
|
std::memcpy(newData, data.data(), data.size());
|
|
|
|
|
|
|
|
|
|
el.offset = newData;
|
2022-02-24 00:42:54 +00:00
|
|
|
el.width = pixelData.Width;
|
|
|
|
|
el.height = pixelData.Height;
|
2025-12-27 14:56:15 +01:00
|
|
|
el.flags = {};
|
2026-08-05 23:32:25 +02:00
|
|
|
if (pixelData.Type == PixelDataKind::rle)
|
2022-02-24 00:42:54 +00:00
|
|
|
{
|
2025-12-27 14:56:15 +01:00
|
|
|
el.flags.set(G1Flag::hasRLECompression);
|
2022-02-24 00:42:54 +00:00
|
|
|
}
|
2023-01-16 18:52:17 +00:00
|
|
|
GfxSetG1Element(id, &el);
|
2023-01-16 22:31:34 +00:00
|
|
|
DrawingEngineInvalidateImage(id);
|
2022-02-24 00:42:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
void JSSetPixelData(JSContext* ctx, ImageIndex id, JSValue jsPixelData)
|
2022-02-24 00:42:54 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
auto pixelData = GetPixelDataFromJS(ctx, jsPixelData);
|
|
|
|
|
auto newData = GetBufferFromPixelData(ctx, pixelData);
|
|
|
|
|
ReplacePixelDataForImage(id, pixelData, std::move(newData));
|
|
|
|
|
JS_FreeValue(ctx, pixelData.Data);
|
2022-02-24 00:42:54 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-23 00:26:26 +11:00
|
|
|
void JSDrawCustomImage(
|
|
|
|
|
JSContext* ctx, ScriptEngine& scriptEngine, ImageIndex id, ScreenSize size, const JSCallback& callback)
|
2022-02-25 20:05:10 +00:00
|
|
|
{
|
|
|
|
|
auto plugin = scriptEngine.GetExecInfo().GetCurrentPlugin();
|
|
|
|
|
|
|
|
|
|
auto drawingEngine = std::make_unique<X8DrawingEngine>(GetContext()->GetUiContext());
|
2025-05-05 01:06:46 +03:00
|
|
|
RenderTarget rt;
|
|
|
|
|
rt.DrawingEngine = drawingEngine.get();
|
|
|
|
|
rt.width = size.width;
|
|
|
|
|
rt.height = size.height;
|
2022-02-25 20:05:10 +00:00
|
|
|
|
|
|
|
|
auto createNewImage = false;
|
2023-01-16 18:52:17 +00:00
|
|
|
auto g1 = GfxGetG1Element(id);
|
2025-12-27 14:56:15 +01:00
|
|
|
if (g1 == nullptr || g1->width != size.width || g1->height != size.height || g1->flags.has(G1Flag::hasRLECompression))
|
2022-02-25 20:05:10 +00:00
|
|
|
{
|
|
|
|
|
createNewImage = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (createNewImage)
|
|
|
|
|
{
|
2022-03-24 23:34:57 +00:00
|
|
|
auto bufferSize = size.width * size.height;
|
2026-02-07 23:41:40 +01:00
|
|
|
rt.bits = new PaletteIndex[bufferSize];
|
2025-05-05 01:06:46 +03:00
|
|
|
std::memset(rt.bits, 0, bufferSize);
|
2022-02-25 20:05:10 +00:00
|
|
|
|
2025-04-24 19:16:21 +03:00
|
|
|
drawingEngine->BeginDraw();
|
|
|
|
|
|
2022-02-25 20:05:10 +00:00
|
|
|
// Draw the original image if we are creating a new one
|
2025-05-05 01:06:46 +03:00
|
|
|
GfxDrawSprite(rt, ImageId(id), { 0, 0 });
|
2025-04-24 19:16:21 +03:00
|
|
|
|
|
|
|
|
drawingEngine->EndDraw();
|
2022-02-25 20:05:10 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2026-02-07 23:41:40 +01:00
|
|
|
rt.bits = reinterpret_cast<PaletteIndex*>(g1->offset);
|
2022-02-25 20:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
if (callback.IsValid())
|
|
|
|
|
{
|
|
|
|
|
drawingEngine->BeginDraw();
|
|
|
|
|
scriptEngine.ExecutePluginCall(plugin, callback.callback, { gScGraphicsContext.New(ctx, rt) }, false);
|
|
|
|
|
drawingEngine->EndDraw();
|
|
|
|
|
}
|
2022-02-25 20:05:10 +00:00
|
|
|
|
|
|
|
|
if (createNewImage)
|
|
|
|
|
{
|
2023-01-19 08:16:44 +00:00
|
|
|
G1Element newg1{};
|
2022-02-25 20:05:10 +00:00
|
|
|
if (g1 != nullptr)
|
|
|
|
|
{
|
|
|
|
|
delete[] g1->offset;
|
|
|
|
|
newg1 = *g1;
|
|
|
|
|
}
|
2026-02-07 23:41:40 +01:00
|
|
|
newg1.offset = reinterpret_cast<uint8_t*>(rt.bits);
|
2022-02-25 20:05:10 +00:00
|
|
|
newg1.width = size.width;
|
|
|
|
|
newg1.height = size.height;
|
2025-12-27 14:56:15 +01:00
|
|
|
newg1.flags = {};
|
2023-01-16 18:52:17 +00:00
|
|
|
GfxSetG1Element(id, &newg1);
|
2022-02-25 20:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-16 22:31:34 +00:00
|
|
|
DrawingEngineInvalidateImage(id);
|
2022-02-25 20:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
2022-02-24 00:42:54 +00:00
|
|
|
} // namespace OpenRCT2::Scripting
|
|
|
|
|
|
|
|
|
|
#endif
|