Skip to content

Commit 554c653

Browse files
committed
Implemented imgui callbacks
1 parent 25f7aee commit 554c653

3 files changed

Lines changed: 54 additions & 1 deletion

File tree

src/port/api/ui.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,27 @@
66
#include "port/ui/MenuTypes.h"
77
// Include any mod-specific header that holds your maps (e.g. Rando::StaticData)
88

9+
#include <vector>
10+
#include <algorithm>
11+
12+
static std::vector<C_GuiDrawFunc> gGuiDrawCallbacks;
13+
14+
void C_RunGuiDrawCallbacks() {
15+
for (auto& cb : gGuiDrawCallbacks) {
16+
cb();
17+
}
18+
}
19+
20+
extern "C" void C_AddGuiDraw(C_GuiDrawFunc cb) {
21+
if (cb) gGuiDrawCallbacks.push_back(cb);
22+
}
23+
24+
extern "C" void C_RemoveGuiDraw(C_GuiDrawFunc cb) {
25+
gGuiDrawCallbacks.erase(
26+
std::remove(gGuiDrawCallbacks.begin(), gGuiDrawCallbacks.end(), cb),
27+
gGuiDrawCallbacks.end());
28+
}
29+
930
// Helper function to map C Enums to C++ Enums safely
1031
static UIWidgets::ComponentAlignments MapAlign(C_ComponentAlign align) {
1132
if (align == C_ALIGN_RIGHT)

src/port/api/ui.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,13 @@ extern_s void C_AddSidebarEntry(const char* label, int column);
120120
extern_s void C_AddWidget(const char* sidebar, int column, const char* label, C_WidgetConfig* config);
121121

122122
// void C_RemoveMenuEntry(const char* entryName);
123-
extern_s void C_RemoveSidebarEntry(const char* sidebarName);
123+
extern_s void C_RemoveSidebarEntry(const char* sidebarName);
124+
125+
// ---------------------------------------------------------
126+
// 5. Per-frame ImGui draw callbacks
127+
// Registered functions are called once per ImGui frame,
128+
// inside NewFrame … Render, safe to call igBegin/igEnd.
129+
// ---------------------------------------------------------
130+
typedef void (*C_GuiDrawFunc)(void);
131+
extern_s void C_AddGuiDraw(C_GuiDrawFunc callback);
132+
extern_s void C_RemoveGuiDraw(C_GuiDrawFunc callback);

src/port/ui/GhostshipGui.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#include <spdlog/spdlog.h>
44
#include <imgui.h>
55
#include <imgui_internal.h>
6+
#include "port/api/ui.h"
7+
8+
void C_RunGuiDrawCallbacks();
69

710
#ifdef __APPLE__
811
#include <fast/backends/gfx_metal.h>
@@ -20,6 +23,22 @@
2023
#include <ship/window/gui/EventDebuggerWindow.h>
2124
#include <libultraship/window/gui/GfxDebuggerWindow.h>
2225

26+
// Invisible host window that fires C mod ImGui callbacks each frame.
27+
// Overrides Draw() so it never calls ImGui::Begin()/End() itself; the C
28+
// callbacks are free to open any number of their own igBegin/igEnd windows.
29+
class CGuiCallbackWindow : public Ship::GuiWindow {
30+
public:
31+
using GuiWindow::GuiWindow;
32+
void InitElement() override {}
33+
void UpdateElement() override {}
34+
void DrawElement() override {}
35+
void Draw() override {
36+
if (IsVisible()) {
37+
C_RunGuiDrawCallbacks();
38+
}
39+
}
40+
};
41+
2342
namespace GhostshipGui {
2443
// MARK: - Delegates
2544
std::shared_ptr<Ship::GuiWindow> mInputEditorWindow;
@@ -78,6 +97,10 @@ void SetupGuiElements() {
7897
mObjectViewer = std::make_shared<ObjectViewer>(CVAR_WINDOW("ObjectViewer"), "Object Viewer##Dev", ImVec2(820, 630));
7998
gui->AddGuiWindow(mObjectViewer);
8099

100+
auto cGuiWindow = std::make_shared<CGuiCallbackWindow>("gWindows.CGuiCallbacks", "##cguicallbacks");
101+
cGuiWindow->Show();
102+
gui->AddGuiWindow(cGuiWindow);
103+
81104
mGhostshipMenu = std::make_shared<GhostshipMenu>(CVAR_WINDOW("Menu"), "Settings Menu");
82105
gui->SetMenu(mGhostshipMenu);
83106

0 commit comments

Comments
 (0)