2020-02-11 21:26:05 +00:00
|
|
|
/*****************************************************************************
|
2026-01-05 14:42:59 +01:00
|
|
|
* Copyright (c) 2014-2026 OpenRCT2 developers
|
2020-02-11 21:26:05 +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.
|
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
2020-04-24 15:58:01 +01:00
|
|
|
#ifdef ENABLE_SCRIPTING
|
2020-02-23 12:55:48 +00:00
|
|
|
|
2024-11-29 12:04:56 +09:00
|
|
|
#include "CustomMenu.h"
|
2020-02-11 21:26:05 +00:00
|
|
|
|
2024-11-29 12:04:56 +09:00
|
|
|
#include <openrct2-ui/UiContext.h>
|
|
|
|
|
#include <openrct2-ui/input/ShortcutManager.h>
|
2025-09-29 02:05:00 +10:00
|
|
|
#include <openrct2/core/EnumMap.hpp>
|
2026-08-11 22:16:10 +01:00
|
|
|
#include <openrct2/interface/Viewport.h>
|
2025-09-29 02:05:00 +10:00
|
|
|
#include <openrct2/scripting/ScriptUtil.hpp>
|
2025-01-08 21:12:11 +01:00
|
|
|
#include <openrct2/ui/WindowManager.h>
|
2025-09-04 21:57:41 +02:00
|
|
|
#include <openrct2/world/Map.h>
|
2020-05-01 21:32:41 +01:00
|
|
|
|
2020-12-16 00:15:20 +00:00
|
|
|
using namespace OpenRCT2;
|
|
|
|
|
using namespace OpenRCT2::Ui;
|
|
|
|
|
|
2020-02-11 21:26:05 +00:00
|
|
|
namespace OpenRCT2::Scripting
|
|
|
|
|
{
|
2020-05-01 21:32:41 +01:00
|
|
|
std::optional<CustomTool> ActiveCustomTool;
|
2020-02-11 21:26:05 +00:00
|
|
|
std::vector<CustomToolbarMenuItem> CustomMenuItems;
|
2021-02-23 22:18:00 +00:00
|
|
|
std::vector<std::unique_ptr<CustomShortcut>> CustomShortcuts;
|
2020-12-16 00:15:20 +00:00
|
|
|
|
|
|
|
|
CustomShortcut::CustomShortcut(
|
|
|
|
|
std::shared_ptr<Plugin> owner, std::string_view id, std::string_view text, const std::vector<std::string>& bindings,
|
2025-09-29 02:05:00 +10:00
|
|
|
JSCallback callback)
|
2020-12-16 00:15:20 +00:00
|
|
|
: Owner(owner)
|
|
|
|
|
, Id(id)
|
|
|
|
|
, Text(text)
|
|
|
|
|
, Bindings(bindings)
|
|
|
|
|
, Callback(callback)
|
|
|
|
|
{
|
|
|
|
|
auto& shortcutManager = GetShortcutManager();
|
|
|
|
|
|
|
|
|
|
RegisteredShortcut registeredShortcut(Id, Text, [this]() { Invoke(); });
|
|
|
|
|
for (const auto& binding : bindings)
|
|
|
|
|
{
|
2025-09-30 22:57:43 +02:00
|
|
|
registeredShortcut.standard.emplace_back(binding);
|
2020-12-16 00:15:20 +00:00
|
|
|
}
|
2025-09-30 22:57:43 +02:00
|
|
|
registeredShortcut.current = registeredShortcut.standard;
|
2025-09-30 23:05:23 +02:00
|
|
|
shortcutManager.registerShortcut(std::move(registeredShortcut));
|
|
|
|
|
shortcutManager.loadUserBindings();
|
2020-12-16 00:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CustomShortcut::~CustomShortcut()
|
|
|
|
|
{
|
|
|
|
|
auto& shortcutManager = GetShortcutManager();
|
2025-09-30 23:05:23 +02:00
|
|
|
shortcutManager.removeShortcut(Id);
|
2020-12-16 00:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CustomShortcut::Invoke() const
|
|
|
|
|
{
|
|
|
|
|
auto& scriptEngine = GetContext()->GetScriptEngine();
|
2025-09-29 02:05:00 +10:00
|
|
|
scriptEngine.ExecutePluginCall(Owner, Callback.callback, {}, false);
|
2020-12-16 00:15:20 +00:00
|
|
|
}
|
2020-02-11 21:26:05 +00:00
|
|
|
|
2026-08-06 21:29:51 +02:00
|
|
|
static constexpr std::array<std::string_view, EnumValue(CursorID::count)> kCursorNames = {
|
2020-05-02 11:38:33 +01:00
|
|
|
"arrow", "blank", "up_arrow", "up_down_arrow", "hand_point", "zzz", "diagonal_arrows",
|
|
|
|
|
"picker", "tree_down", "fountain_down", "statue_down", "bench_down", "cross_hair", "bin_down",
|
|
|
|
|
"lamppost_down", "fence_down", "flower_down", "path_down", "dig_down", "water_down", "house_down",
|
|
|
|
|
"volcano_down", "walk_down", "paint_down", "entrance_down", "hand_open", "hand_closed",
|
|
|
|
|
};
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue CursorIDToJSValue(JSContext* ctx, CursorID id)
|
2020-05-02 11:38:33 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
auto idVal = EnumValue(id);
|
2026-08-06 21:29:51 +02:00
|
|
|
if (idVal < kCursorNames.size())
|
2020-05-02 11:38:33 +01:00
|
|
|
{
|
2026-08-06 21:29:51 +02:00
|
|
|
return JSFromStdString(ctx, kCursorNames[idVal]);
|
2020-05-02 11:38:33 +01:00
|
|
|
}
|
2025-09-29 02:05:00 +10:00
|
|
|
return JS_UNDEFINED;
|
2020-05-02 11:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
static CursorID CursorJSValToID(JSContext* ctx, JSValue value)
|
2020-05-02 11:38:33 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
if (JS_IsString(value))
|
2020-05-02 11:38:33 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
std::string valueStr = JSToStdString(ctx, value);
|
2026-08-05 23:07:04 +02:00
|
|
|
for (uint8_t i = 0; i < EnumValue(CursorID::count); i++)
|
2020-05-02 11:38:33 +01:00
|
|
|
{
|
2026-08-06 21:29:51 +02:00
|
|
|
if (kCursorNames[i] == valueStr)
|
2025-09-29 02:05:00 +10:00
|
|
|
{
|
|
|
|
|
return static_cast<CursorID>(i);
|
|
|
|
|
}
|
2020-05-02 11:38:33 +01:00
|
|
|
}
|
|
|
|
|
}
|
2026-08-05 23:07:04 +02:00
|
|
|
return CursorID::arrow;
|
2025-09-29 02:05:00 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const EnumMap<ViewportInteractionItem> ToolFilterMap{
|
|
|
|
|
{ "terrain", ViewportInteractionItem::terrain },
|
|
|
|
|
{ "entity", ViewportInteractionItem::entity },
|
|
|
|
|
{ "ride", ViewportInteractionItem::ride },
|
|
|
|
|
{ "water", ViewportInteractionItem::water },
|
|
|
|
|
{ "scenery", ViewportInteractionItem::scenery },
|
|
|
|
|
{ "footpath", ViewportInteractionItem::footpath },
|
|
|
|
|
{ "footpath_item", ViewportInteractionItem::pathAddition },
|
|
|
|
|
{ "park_entrance", ViewportInteractionItem::parkEntrance },
|
|
|
|
|
{ "wall", ViewportInteractionItem::wall },
|
|
|
|
|
{ "large_scenery", ViewportInteractionItem::largeScenery },
|
|
|
|
|
{ "label", ViewportInteractionItem::label },
|
|
|
|
|
{ "banner", ViewportInteractionItem::banner },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static ViewportInteractionItem FilterJSValToEnum(JSContext* ctx, JSValue value)
|
|
|
|
|
{
|
|
|
|
|
if (JS_IsString(value))
|
|
|
|
|
{
|
|
|
|
|
if (auto val = ToolFilterMap.TryGet(JSToStdString(ctx, value)); val.has_value())
|
|
|
|
|
{
|
|
|
|
|
return val.value();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ViewportInteractionItem::none;
|
2020-05-02 11:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
2020-05-01 21:32:41 +01:00
|
|
|
static void RemoveMenuItemsAndTool(std::shared_ptr<Plugin> owner)
|
2020-02-11 21:26:05 +00:00
|
|
|
{
|
2020-05-01 21:32:41 +01:00
|
|
|
if (ActiveCustomTool)
|
|
|
|
|
{
|
|
|
|
|
if (ActiveCustomTool->Owner == owner)
|
|
|
|
|
{
|
2023-01-16 21:13:42 +01:00
|
|
|
ToolCancel();
|
2020-05-01 21:32:41 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-11 21:26:05 +00:00
|
|
|
auto& menuItems = CustomMenuItems;
|
|
|
|
|
for (auto it = menuItems.begin(); it != menuItems.end();)
|
|
|
|
|
{
|
|
|
|
|
if (it->Owner == owner)
|
|
|
|
|
{
|
|
|
|
|
it = menuItems.erase(it);
|
|
|
|
|
}
|
2020-12-16 00:15:20 +00:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
it++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto& shortcuts = CustomShortcuts;
|
|
|
|
|
for (auto it = shortcuts.begin(); it != shortcuts.end();)
|
|
|
|
|
{
|
2021-02-23 22:18:00 +00:00
|
|
|
if ((*it)->Owner == owner)
|
2020-12-16 00:15:20 +00:00
|
|
|
{
|
|
|
|
|
it = shortcuts.erase(it);
|
|
|
|
|
}
|
2020-02-11 21:26:05 +00:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
it++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void InitialiseCustomMenuItems(ScriptEngine& scriptEngine)
|
|
|
|
|
{
|
2020-05-01 21:32:41 +01:00
|
|
|
scriptEngine.SubscribeToPluginStoppedEvent(
|
|
|
|
|
[](std::shared_ptr<Plugin> plugin) -> void { RemoveMenuItemsAndTool(plugin); });
|
2020-02-11 21:26:05 +00:00
|
|
|
}
|
2020-05-01 21:32:41 +01:00
|
|
|
|
|
|
|
|
void CustomTool::OnUpdate(const ScreenCoordsXY& screenCoords)
|
|
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
InvokeEventHandler(onMove.callback, screenCoords);
|
2020-05-01 21:32:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CustomTool::OnDown(const ScreenCoordsXY& screenCoords)
|
|
|
|
|
{
|
|
|
|
|
MouseDown = true;
|
2025-09-29 02:05:00 +10:00
|
|
|
InvokeEventHandler(onDown.callback, screenCoords);
|
2020-05-01 21:32:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CustomTool::OnDrag(const ScreenCoordsXY& screenCoords)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CustomTool::Start()
|
|
|
|
|
{
|
|
|
|
|
auto& scriptEngine = GetContext()->GetScriptEngine();
|
2025-09-29 02:05:00 +10:00
|
|
|
scriptEngine.ExecutePluginCall(Owner, onStart.callback, {}, false);
|
2020-05-01 21:32:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CustomTool::OnUp(const ScreenCoordsXY& screenCoords)
|
|
|
|
|
{
|
|
|
|
|
MouseDown = false;
|
2025-09-29 02:05:00 +10:00
|
|
|
InvokeEventHandler(onUp.callback, screenCoords);
|
2020-05-01 21:32:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CustomTool::OnAbort()
|
|
|
|
|
{
|
|
|
|
|
auto& scriptEngine = GetContext()->GetScriptEngine();
|
2025-09-29 02:05:00 +10:00
|
|
|
scriptEngine.ExecutePluginCall(Owner, onFinish.callback, {}, false);
|
2020-05-01 21:32:41 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
void CustomTool::InvokeEventHandler(JSValue handler, const ScreenCoordsXY& screenCoords)
|
2020-05-01 21:32:41 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JSContext* ctx = Owner->GetContext();
|
|
|
|
|
if (JS_IsFunction(ctx, handler))
|
2020-05-01 21:32:41 +01:00
|
|
|
{
|
2023-01-16 21:14:50 +01:00
|
|
|
auto info = GetMapCoordinatesFromPos(screenCoords, Filter);
|
2020-05-01 21:32:41 +01:00
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue obj = JS_NewObject(ctx);
|
|
|
|
|
JS_SetPropertyStr(ctx, obj, "isDown", JS_NewBool(ctx, MouseDown));
|
|
|
|
|
JS_SetPropertyStr(ctx, obj, "screenCoords", ToJSValue(ctx, screenCoords));
|
|
|
|
|
JS_SetPropertyStr(ctx, obj, "mapCoords", ToJSValue(ctx, info.Loc));
|
2020-05-01 21:32:41 +01:00
|
|
|
|
2025-09-15 23:34:29 +02:00
|
|
|
if (info.interactionType == ViewportInteractionItem::entity && info.Entity != nullptr)
|
2020-05-01 21:32:41 +01:00
|
|
|
{
|
2026-05-17 13:21:59 +02:00
|
|
|
JS_SetPropertyStr(ctx, obj, "entityId", JS_NewInt32(ctx, info.Entity->id.ToUnderlying()));
|
2020-05-01 21:32:41 +01:00
|
|
|
}
|
2020-08-02 08:32:59 +01:00
|
|
|
else if (info.Element != nullptr)
|
2020-05-01 21:32:41 +01:00
|
|
|
{
|
2022-10-11 19:39:24 +01:00
|
|
|
auto el = MapGetFirstElementAt(info.Loc);
|
2020-05-01 21:32:41 +01:00
|
|
|
if (el != nullptr)
|
|
|
|
|
{
|
2026-01-15 11:28:14 -03:00
|
|
|
int32_t index = 0;
|
2020-05-01 21:32:41 +01:00
|
|
|
do
|
|
|
|
|
{
|
2020-08-02 08:32:59 +01:00
|
|
|
if (el == info.Element)
|
2020-05-01 21:32:41 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_SetPropertyStr(ctx, obj, "tileElementIndex", JS_NewInt32(ctx, index));
|
2020-05-01 21:32:41 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
index++;
|
2026-05-17 15:21:16 +02:00
|
|
|
} while (!(el++)->isLastForTile());
|
2020-05-01 21:32:41 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto& scriptEngine = GetContext()->GetScriptEngine();
|
2025-09-29 02:05:00 +10:00
|
|
|
scriptEngine.ExecutePluginCall(Owner, handler, { obj }, false);
|
2020-05-01 21:32:41 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
[[nodiscard]] JSValue InitialiseCustomTool(ScriptEngine& scriptEngine, JSContext* ctx, JSValue value)
|
2020-05-01 21:32:41 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
std::shared_ptr<Plugin> currentPlugin = scriptEngine.GetExecInfo().GetCurrentPlugin();
|
|
|
|
|
|
|
|
|
|
if (JS_IsObject(value))
|
2020-05-01 21:32:41 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue idVal = JS_GetPropertyStr(ctx, value, "id");
|
|
|
|
|
if (!JS_IsString(idVal))
|
2020-05-01 21:32:41 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_FreeValue(ctx, idVal);
|
|
|
|
|
JS_ThrowTypeError(ctx, "Invalid id. Expected string");
|
|
|
|
|
return JS_EXCEPTION;
|
|
|
|
|
}
|
2021-02-10 01:06:18 +00:00
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
CustomTool customTool;
|
|
|
|
|
customTool.Owner = currentPlugin;
|
2021-02-10 01:06:18 +00:00
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
customTool.Id = JSToStdString(ctx, idVal);
|
|
|
|
|
JS_FreeValue(ctx, idVal);
|
2020-05-01 21:32:41 +01:00
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue cursorVal = JS_GetPropertyStr(ctx, value, "cursor");
|
|
|
|
|
customTool.Cursor = CursorJSValToID(ctx, cursorVal);
|
|
|
|
|
JS_FreeValue(ctx, cursorVal);
|
|
|
|
|
|
|
|
|
|
JSValue filter = JS_GetPropertyStr(ctx, value, "filter");
|
|
|
|
|
if (JS_IsArray(filter))
|
|
|
|
|
{
|
|
|
|
|
customTool.Filter = 0;
|
|
|
|
|
int64_t len = -1;
|
|
|
|
|
JS_GetLength(ctx, filter, &len);
|
|
|
|
|
for (int64_t i = 0; i < len; i++)
|
2020-05-01 21:32:41 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue curFilter = JS_GetPropertyInt64(ctx, filter, i);
|
|
|
|
|
auto elem = FilterJSValToEnum(ctx, curFilter);
|
|
|
|
|
customTool.Filter |= static_cast<uint32_t>(EnumToFlag(elem));
|
|
|
|
|
JS_FreeValue(ctx, curFilter);
|
2020-05-01 21:32:41 +01:00
|
|
|
}
|
|
|
|
|
}
|
2025-09-29 02:05:00 +10:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
customTool.Filter = kViewportInteractionItemAll;
|
|
|
|
|
}
|
|
|
|
|
JS_FreeValue(ctx, filter);
|
|
|
|
|
|
|
|
|
|
customTool.onStart = JSToCallback(ctx, value, "onStart");
|
|
|
|
|
customTool.onDown = JSToCallback(ctx, value, "onDown");
|
|
|
|
|
customTool.onMove = JSToCallback(ctx, value, "onMove");
|
|
|
|
|
customTool.onUp = JSToCallback(ctx, value, "onUp");
|
|
|
|
|
customTool.onFinish = JSToCallback(ctx, value, "onFinish");
|
|
|
|
|
|
|
|
|
|
auto* windowMgr = GetWindowManager();
|
|
|
|
|
auto toolbarWindow = windowMgr->FindByClass(WindowClass::topToolbar);
|
|
|
|
|
if (toolbarWindow != nullptr)
|
|
|
|
|
{
|
|
|
|
|
// Use a widget that does not exist on top toolbar but also make sure it isn't
|
|
|
|
|
// kWidgetIndexNull, as that prevents abort from being called.
|
|
|
|
|
// TODO: refactor this to not leech on the top toolbar.
|
|
|
|
|
WidgetIndex widgetIndex = 0xFFFE;
|
|
|
|
|
ToolCancel();
|
|
|
|
|
ToolSet(*toolbarWindow, widgetIndex, static_cast<Tool>(customTool.Cursor));
|
|
|
|
|
ActiveCustomTool = std::move(customTool);
|
|
|
|
|
ActiveCustomTool->Start();
|
|
|
|
|
}
|
2020-05-01 21:32:41 +01:00
|
|
|
}
|
2025-09-29 02:05:00 +10:00
|
|
|
return JS_UNDEFINED;
|
2020-05-01 21:32:41 +01:00
|
|
|
}
|
2020-02-11 21:26:05 +00:00
|
|
|
} // namespace OpenRCT2::Scripting
|
2020-02-23 12:55:48 +00:00
|
|
|
|
|
|
|
|
#endif
|