From e786cb8180853862fae6d6fc9d9e1c252d0d8e1a Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 13 Mar 2024 19:50:05 +0100 Subject: [PATCH] feat: Added option to create menu items from scripts --- plugins/script_loader/source/script_api/v1/ui.cpp | 5 +++++ .../templates/CSharp/ImHexLibrary/UI.cs | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/plugins/script_loader/source/script_api/v1/ui.cpp b/plugins/script_loader/source/script_api/v1/ui.cpp index bc72ba09f..b5106e576 100644 --- a/plugins/script_loader/source/script_api/v1/ui.cpp +++ b/plugins/script_loader/source/script_api/v1/ui.cpp @@ -179,4 +179,9 @@ private: SCRIPT_API(void registerView, const char *icon, const char *name, void *drawFunction) { ContentRegistry::Views::add(icon, name, ScriptView::DrawFunction(drawFunction)); +} + +SCRIPT_API(void addMenuItem, const char *icon, const char *menuName, const char *itemName, void *function) { + using MenuFunction = void(*)(); + ContentRegistry::Interface::addMenuItem({ menuName, itemName }, icon, 9999, Shortcut::None, reinterpret_cast(function)); } \ No newline at end of file diff --git a/plugins/script_loader/templates/CSharp/ImHexLibrary/UI.cs b/plugins/script_loader/templates/CSharp/ImHexLibrary/UI.cs index 77630df77..a12b7ac6c 100644 --- a/plugins/script_loader/templates/CSharp/ImHexLibrary/UI.cs +++ b/plugins/script_loader/templates/CSharp/ImHexLibrary/UI.cs @@ -6,6 +6,7 @@ namespace ImHex public partial class UI { private delegate void DrawContentDelegate(); + private delegate void ActionDelegate(); private static List _registeredDelegates = new(); @@ -27,6 +28,9 @@ namespace ImHex [LibraryImport("ImHex")] private static partial void registerViewV1(byte[] icon, byte[] name, IntPtr drawFunction); + [LibraryImport("ImHex")] + private static partial void addMenuItemV1(byte[] icon, byte[] menuName, byte[] itemName, IntPtr drawFunction); + public static void ShowMessageBox(string message) { showMessageBoxV1(Encoding.UTF8.GetBytes(message)); @@ -89,5 +93,16 @@ namespace ImHex ); } + public static void AddMenuItem(byte[] icon, string menuName, string itemName, Action function) + { + _registeredDelegates.Add(new ActionDelegate(function)); + addMenuItemV1( + icon, + Encoding.UTF8.GetBytes(menuName), + Encoding.UTF8.GetBytes(itemName), + Marshal.GetFunctionPointerForDelegate(_registeredDelegates[^1]) + ); + } + } }