1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-21 05:53:02 +01:00

Add plugin API for shortcuts

This commit is contained in:
Ted John
2020-12-16 00:15:20 +00:00
parent 9964df5335
commit afc548c5a7
8 changed files with 171 additions and 3 deletions

View File

@@ -11,14 +11,50 @@
# include "CustomMenu.h"
# include <openrct2-ui/input/ShortcutManager.h>
# include <openrct2/Input.h>
# include <openrct2/world/Map.h>
# include <openrct2/world/Sprite.h>
using namespace OpenRCT2;
using namespace OpenRCT2::Ui;
namespace OpenRCT2::Scripting
{
std::optional<CustomTool> ActiveCustomTool;
std::vector<CustomToolbarMenuItem> CustomMenuItems;
std::vector<CustomShortcut> CustomShortcuts;
CustomShortcut::CustomShortcut(
std::shared_ptr<Plugin> owner, std::string_view id, std::string_view text, const std::vector<std::string>& bindings,
DukValue callback)
: Owner(owner)
, Id(id)
, Text(text)
, Bindings(bindings)
, Callback(callback)
{
auto& shortcutManager = GetShortcutManager();
RegisteredShortcut registeredShortcut(Id, Text, [this]() { Invoke(); });
for (const auto& binding : bindings)
{
registeredShortcut.Default.emplace_back(binding);
}
shortcutManager.RegisterShortcut(std::move(registeredShortcut));
}
CustomShortcut::~CustomShortcut()
{
auto& shortcutManager = GetShortcutManager();
shortcutManager.RemoveShortcut(Id);
}
void CustomShortcut::Invoke() const
{
auto& scriptEngine = GetContext()->GetScriptEngine();
scriptEngine.ExecutePluginCall(Owner, Callback, {}, false);
}
static constexpr std::array<std::string_view, EnumValue(CursorID::Count)> CursorNames = {
"arrow", "blank", "up_arrow", "up_down_arrow", "hand_point", "zzz", "diagonal_arrows",
@@ -89,6 +125,19 @@ namespace OpenRCT2::Scripting
it++;
}
}
auto& shortcuts = CustomShortcuts;
for (auto it = shortcuts.begin(); it != shortcuts.end();)
{
if (it->Owner == owner)
{
it = shortcuts.erase(it);
}
else
{
it++;
}
}
}
void InitialiseCustomMenuItems(ScriptEngine& scriptEngine)