Skip to content

Commit

Permalink
.NET: Draw REFramework.NET UI within REFramework itself
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Apr 8, 2024
1 parent f0c4b0e commit 7c40a5d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
36 changes: 23 additions & 13 deletions csharp-api/REFrameworkNET/PluginManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,10 @@ namespace REFrameworkNET {

// Must be set up before we load anything as it sets up the LoadLibraryExW hook for cimgui
auto imgui_callback_c = System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(s_imgui_callback_delegate).ToPointer();
REFrameworkNET::API::GetNativeImplementation()->param()->functions->on_imgui_frame((::REFOnImGuiFrameCb)imgui_callback_c);
auto imgui_draw_ui_callback_c = System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(s_imgui_draw_ui_callback_delegate).ToPointer();
auto api_fns = REFrameworkNET::API::GetNativeImplementation()->param()->functions;
api_fns->on_imgui_frame((::REFOnImGuiFrameCb)imgui_callback_c);
api_fns->on_imgui_draw_ui((::REFOnImGuiFrameCb)imgui_draw_ui_callback_c);

s_dependencies = LoadDependencies(); // Pre-loads DLLs in the dependencies folder before loading the plugins

Expand Down Expand Up @@ -575,18 +578,6 @@ namespace REFrameworkNET {
ImGuiNET::ImGui::SetCurrentContext(context);
ImGuiNET::ImGui::SetAllocatorFunctions(mallocFn, freeFn, user_data);*/

// Draw our REFramework.NET menu which has buttons like reload scripts
if (ImGuiNET::ImGui::Begin("REFramework.NET")) {
if (ImGuiNET::ImGui::Button("Unload Scripts")) {
PluginManager::UnloadDynamicAssemblies();
}

if (ImGuiNET::ImGui::Button("Reload Scripts")) {
PluginManager::UnloadDynamicAssemblies();
PluginManager::LoadPlugins_FromSourceCode(0, s_dependencies);
}
}

try {
Callbacks::ImGuiRender::TriggerPre();
} catch (System::Exception^ e) {
Expand All @@ -607,4 +598,23 @@ namespace REFrameworkNET {
REFrameworkNET::API::LogError("Unknown exception caught while triggering ImGuiRender::Post");
}
}

void PluginManager::ImGuiDrawUICallback(::REFImGuiFrameCbData* data) {
// Draw our REFramework.NET menu which has buttons like reload scripts
ImGuiNET::ImGui::PushID("REFramework.NET");
if (ImGuiNET::ImGui::CollapsingHeader("REFramework.NET")) {
if (ImGuiNET::ImGui::Button("Reload Scripts")) {
PluginManager::UnloadDynamicAssemblies();
PluginManager::LoadPlugins_FromSourceCode(0, s_dependencies);
}

ImGuiNET::ImGui::SameLine();

if (ImGuiNET::ImGui::Button("Unload Scripts")) {
PluginManager::UnloadDynamicAssemblies();
}
}

ImGuiNET::ImGui::PopID();
}
}
7 changes: 7 additions & 0 deletions csharp-api/REFrameworkNET/PluginManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@ private ref class PluginManager
static bool LoadPlugins_FromSourceCode(uintptr_t param_raw, System::Collections::Generic::List<System::Reflection::Assembly^>^ deps);
static void UnloadDynamicAssemblies();

// This one is outside of a window context
static void ImGuiCallback(::REFImGuiFrameCbData* data);
delegate void ImGuiCallbackDelegate(::REFImGuiFrameCbData* data);

// This one is in the middle of drawing REFramework's UI
static void ImGuiDrawUICallback(::REFImGuiFrameCbData* data);
delegate void ImGuiDrawUICallbackDelegate(::REFImGuiFrameCbData* data);

static ImGuiCallbackDelegate^ s_imgui_callback_delegate{gcnew ImGuiCallbackDelegate(&ImGuiCallback)};
static ImGuiDrawUICallbackDelegate^ s_imgui_draw_ui_callback_delegate{gcnew ImGuiDrawUICallbackDelegate(&ImGuiDrawUICallback)};

static System::Collections::Generic::List<System::Reflection::Assembly^>^ s_loaded_assemblies{gcnew System::Collections::Generic::List<System::Reflection::Assembly^>()};
static System::Collections::Generic::List<System::Reflection::Assembly^>^ s_dynamic_assemblies{gcnew System::Collections::Generic::List<System::Reflection::Assembly^>()};
Expand All @@ -63,6 +69,7 @@ private ref class PluginManager
static void OnSourceScriptsChanged(System::Object^ sender, System::IO::FileSystemEventArgs^ e);
static void BeginRendering();
delegate void BeginRenderingDelegate();

static BeginRenderingDelegate^ s_begin_rendering_delegate{gcnew BeginRenderingDelegate(&BeginRendering)};
};
}

0 comments on commit 7c40a5d

Please sign in to comment.