Repository navigation
Expand file tree
/
Copy pathImGuiMultiContextCompositor.cs
More file actions
492 lines (410 loc) · 16 KB
/
Copy pathImGuiMultiContextCompositor.cs
File metadata and controls
492 lines (410 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
using System.Runtime.InteropServices;
namespace ImGui4NET.Club;
/// <summary>
/// Managed port of <c>imgui_club</c>'s Multi-Context Compositor.
/// </summary>
/// <remarks>
/// When several Dear ImGui contexts are displayed at the same time, this class
/// arbitrates between them: it decides which context receives the mouse, which
/// one owns the keyboard, which one may change the cursor shape, and it
/// replicates drag-and-drop payloads across contexts.
/// <para>
/// The C++ original is a struct plus free functions that read
/// <see cref="ImGuiContext"/> internals. This port keeps the same structure and
/// the same call order:
/// </para>
/// <code>
/// mcc.PreNewFrameUpdateAll();
/// ImGui.SetCurrentContext(ctx1);
/// ImGui.NewFrame();
/// mcc.PostNewFrameUpdateOne(ctx1);
/// ...
/// mcc.PostEndFrameUpdateAll();
/// </code>
/// <para>
/// Contexts are stored as raw pointers because <see cref="ImGuiContext"/> is a
/// native struct owned by Dear ImGui. Ordering is pure bookkeeping and never
/// crosses the ABI boundary, so a managed list is used instead of an
/// <c>ImVector</c>.
/// </para>
/// </remarks>
public sealed unsafe class MultiContextCompositor
{
private const int FlagNoMouse = (int)ImGuiConfigFlags.ImGuiConfigFlags_NoMouse;
private const int FlagNoKeyboard = (int)ImGuiConfigFlags.ImGuiConfigFlags_NoKeyboard;
private const int FlagNoCursorChange = (int)ImGuiConfigFlags.ImGuiConfigFlags_NoMouseCursorChange;
private const int ViewportCanHostOtherWindows =
(int)ImGuiViewportFlags.ImGuiViewportFlags_CanHostOtherWindows;
private const int ViewportIsFocused = (int)ImGuiViewportFlags.ImGuiViewportFlags_IsFocused;
private const int DragSourceExtern = (int)ImGuiDragDropFlags.ImGuiDragDropFlags_SourceExtern;
private const int DragPayloadNoCrossContext =
(int)ImGuiDragDropFlags.ImGuiDragDropFlags_PayloadNoCrossContext;
private ImGuiPayload _dragDropPayload;
/// <summary>Registered contexts, in registration order.</summary>
public List<nint> Contexts { get; } = [];
/// <summary>Registered contexts, sorted front (index 0) to back.</summary>
public List<nint> ContextsFrontToBack { get; } = [];
/// <summary>First context, front to back, whose <c>io.WantCaptureMouse</c> is set.</summary>
public nint CtxMouseFirst { get; private set; }
/// <summary>Context owning the hovered secondary viewport, if any.</summary>
public nint CtxMouseExclusive { get; private set; }
/// <summary>Context that requested a non-default cursor shape.</summary>
public nint CtxMouseShape { get; private set; }
/// <summary>Context that owns the keyboard this frame.</summary>
public nint CtxKeyboardExclusive { get; private set; }
/// <summary>Context that started the active drag and drop, if any.</summary>
public nint CtxDragDropSrc { get; private set; }
/// <summary>Context that receives the replicated drag and drop payload.</summary>
public nint CtxDragDropDst { get; private set; }
/// <summary>Deep copy of the active drag and drop payload.</summary>
public ImGuiPayload DragDropPayload => _dragDropPayload;
private bool _payloadAllocated;
// -----------------------------------------------------------------------
// Registration
// -----------------------------------------------------------------------
/// <summary>Registers a context with the compositor.</summary>
public void AddContext(ImGuiContext* context)
{
if (context == null || Contexts.Contains((nint)context))
{
return;
}
Contexts.Add((nint)context);
ContextsFrontToBack.Add((nint)context);
}
/// <summary>Unregisters a context.</summary>
public void RemoveContext(ImGuiContext* context)
{
nint key = (nint)context;
Contexts.RemoveAll(c => c == key);
ContextsFrontToBack.RemoveAll(c => c == key);
if (CtxDragDropSrc == key)
{
CtxDragDropSrc = 0;
}
if (CtxDragDropDst == key)
{
CtxDragDropDst = 0;
}
}
// -----------------------------------------------------------------------
// Frame hooks
// -----------------------------------------------------------------------
/// <summary>
/// Call at a shared sync point before <c>NewFrame</c> runs on any context.
/// </summary>
public void PreNewFrameUpdateAll()
{
FreePayload();
CtxMouseFirst = 0;
CtxMouseExclusive = 0;
CtxMouseShape = 0;
CtxKeyboardExclusive = 0;
CtxDragDropSrc = 0;
CtxDragDropDst = 0;
// PASS 1: decide ownership for this frame.
foreach (nint raw in ContextsFrontToBack)
{
ImGuiContext* ctx = (ImGuiContext*)raw;
if (ctx == null)
{
continue;
}
TrackSecondaryViewportOwners(raw, ctx);
if (CtxMouseFirst == 0 && ctx->IO.WantCaptureMouse != 0)
{
CtxMouseFirst = raw;
}
// Remember the second context as a potential drop target.
if (CtxDragDropDst == 0 && ctx->HoveredWindowBeforeClear != null)
{
CtxDragDropDst = raw;
}
if (CtxMouseShape == 0 && ctx->MouseCursor != (int)ImGuiMouseCursor.ImGuiMouseCursor_Arrow)
{
CtxMouseShape = raw;
}
bool dragActive = ctx->DragDropActive != 0;
bool externalSource = (ctx->DragDropSourceFlags & DragSourceExtern) != 0;
if (CtxDragDropSrc == 0 && dragActive && !externalSource)
{
CtxDragDropSrc = raw;
}
}
// Without a focused secondary viewport the top-most context keeps the keyboard.
if (CtxKeyboardExclusive == 0 && ContextsFrontToBack.Count > 0)
{
CtxKeyboardExclusive = ContextsFrontToBack[0];
}
if (CtxDragDropSrc != 0)
{
CopyPayloadFromSource((ImGuiContext*)CtxDragDropSrc);
}
// No payload means there is nothing to drop anywhere.
if (CtxDragDropDst != 0 && !_payloadAllocated)
{
CtxDragDropDst = 0;
}
// PASS 2: apply the decisions.
bool aboveClaimingContext = true;
foreach (nint raw in ContextsFrontToBack)
{
ImGuiContext* ctx = (ImGuiContext*)raw;
if (ctx == null)
{
continue;
}
ApplyKeyboardFlags(raw, ctx);
ApplyCursorFlags(raw, ctx);
aboveClaimingContext = ApplyMouseFlags(raw, ctx, aboveClaimingContext);
}
if (CtxDragDropSrc != 0 && CtxDragDropDst != 0)
{
PushPayloadToDestination();
}
}
/// <summary>
/// Call right after <c>NewFrame</c> on <paramref name="context"/>.
/// </summary>
public void PostNewFrameUpdateOne(ImGuiContext* context)
{
if (context == null)
{
return;
}
// Clicking inside a context brings it to the front.
if (context->NavWindow != null && AnyMouseClicked())
{
BringContextToFront((nint)context, CtxKeyboardExclusive);
}
}
/// <summary>
/// Call at a shared sync point after <c>EndFrame</c> on every context.
/// </summary>
public void PostEndFrameUpdateAll() => FreePayload();
// -----------------------------------------------------------------------
// Routing helpers
// -----------------------------------------------------------------------
private void TrackSecondaryViewportOwners(nint raw, ImGuiContext* ctx)
{
// A hovered secondary viewport takes exclusive mouse ownership. The
// backend-submitted MouseHoveredViewport is used, not the last valid one.
if (CtxMouseExclusive == 0 && ctx->IO.MouseHoveredViewport != 0)
{
ImGuiViewport* hovered = FindViewport(ctx, ctx->IO.MouseHoveredViewport);
if (hovered != null && (hovered->Flags & ViewportCanHostOtherWindows) == 0)
{
CtxMouseExclusive = raw;
}
}
// A focused secondary viewport takes exclusive keyboard ownership.
if (CtxKeyboardExclusive == 0 && ctx->NavWindow != null)
{
ImGuiViewportP* viewportP = ctx->NavWindow->Viewport;
ImGuiViewport* viewport = (ImGuiViewport*)viewportP;
if (viewport != null
&& (viewport->Flags & ViewportIsFocused) != 0
&& (viewport->Flags & ViewportCanHostOtherWindows) == 0)
{
CtxKeyboardExclusive = raw;
}
}
}
private static ImGuiViewport* FindViewport(ImGuiContext* ctx, uint id)
{
ImVector<IntPtr> viewports = ctx->PlatformIO.Viewports;
for (int i = 0; i < viewports.Size; i++)
{
ImGuiViewport* viewport = (ImGuiViewport*)viewports.Data[i];
if (viewport != null && viewport->ID == id)
{
return viewport;
}
}
return null;
}
private void ApplyKeyboardFlags(nint raw, ImGuiContext* ctx)
{
ImGuiIO* io = &ctx->IO;
if (CtxKeyboardExclusive == raw)
{
io->ConfigFlags &= ~FlagNoKeyboard;
}
else
{
io->ConfigFlags |= FlagNoKeyboard;
}
}
private void ApplyCursorFlags(nint raw, ImGuiContext* ctx)
{
ImGuiIO* io = &ctx->IO;
if (CtxMouseShape == 0 || CtxMouseShape == raw)
{
io->ConfigFlags &= ~FlagNoCursorChange;
}
else
{
io->ConfigFlags |= FlagNoCursorChange;
}
}
/// <summary>
/// Applies the mouse flags and returns whether the next context is still
/// above the one that claimed the mouse.
/// </summary>
private bool ApplyMouseFlags(nint raw, ImGuiContext* ctx, bool aboveClaimingContext)
{
ImGuiIO* io = &ctx->IO;
bool allow;
if (CtxMouseExclusive != 0)
{
// A secondary viewport is hovered: only its owner gets the mouse.
allow = CtxMouseExclusive == raw;
}
else
{
// On a shared viewport everything above the claiming context also
// gets the mouse, so overlapping windows behave as one context.
allow = aboveClaimingContext || CtxDragDropDst == raw;
}
if (allow)
{
io->ConfigFlags &= ~FlagNoMouse;
}
else
{
io->ConfigFlags |= FlagNoMouse;
}
return aboveClaimingContext && raw != CtxMouseFirst;
}
private void BringContextToFront(nint contextKey, nint keepInputsFor)
{
ContextsFrontToBack.RemoveAll(c => c == contextKey);
ContextsFrontToBack.Insert(0, contextKey);
foreach (nint otherKey in ContextsFrontToBack)
{
if (otherKey == 0 || otherKey == contextKey || otherKey == keepInputsFor)
{
continue;
}
ImGuiContext* otherCtx = (ImGuiContext*)otherKey;
ImGuiIO* otherIo = &otherCtx->IO;
ImGuiIO.ClearInputKeys(otherIo);
}
}
// -----------------------------------------------------------------------
// Drag and drop replication
// -----------------------------------------------------------------------
private void CopyPayloadFromSource(ImGuiContext* source)
{
if (source->DragDropActive == 0)
{
return;
}
// A payload flagged PayloadNoCrossContext must not leave its context.
if ((source->DragDropSourceFlags & DragPayloadNoCrossContext) != 0)
{
return;
}
ImGuiPayload* src = &source->DragDropPayload;
if (src->Data == null || src->DataSize <= 0)
{
return;
}
void* copy = ImGui.MemAlloc((nuint)src->DataSize);
if (copy == null)
{
return;
}
Buffer.MemoryCopy(src->Data, copy, src->DataSize, src->DataSize);
_dragDropPayload = *src;
_dragDropPayload.Data = copy;
_payloadAllocated = true;
}
private void PushPayloadToDestination()
{
if (_dragDropPayload.Data == null)
{
return;
}
int flags = DragSourceExtern
| (int)ImGuiDragDropFlags.ImGuiDragDropFlags_SourceNoPreviewTooltip;
if (ImGui.BeginDragDropSource(flags) != 0)
{
string dataType;
fixed (sbyte* dataTypePtr = &_dragDropPayload.DataType.e0)
{
dataType = Marshal.PtrToStringUTF8((nint)dataTypePtr) ?? string.Empty;
}
ImGui.SetDragDropPayload(dataType, _dragDropPayload.Data, (nuint)_dragDropPayload.DataSize);
ImGui.EndDragDropSource();
}
}
private void FreePayload()
{
if (!_payloadAllocated)
{
_dragDropPayload = default;
return;
}
if (_dragDropPayload.Data != null)
{
ImGui.MemFree(_dragDropPayload.Data);
}
_dragDropPayload = default;
_payloadAllocated = false;
}
// -----------------------------------------------------------------------
// Debug overlay
// -----------------------------------------------------------------------
/// <summary>Draws the upstream debug overlay in the current context.</summary>
public void ShowDebugWindow()
{
ImGuiViewport* mainViewport = ImGui.GetMainViewport();
if (mainViewport != null)
{
ImVec2 pos = mainViewport->Pos;
ImVec2 pivot = new() { x = 0f, y = 0f };
ImGui.SetNextWindowPos(in pos, 0, in pivot);
}
ImVec4 windowBg = new() { x = 1f, y = 1f, z = 1f, w = 0.5f };
ImVec4 textCol = new() { x = 0f, y = 0f, z = 0f, w = 1f };
ImGui.PushStyleColor((int)ImGuiCol.ImGuiCol_WindowBg, in windowBg);
ImGui.PushStyleColor((int)ImGuiCol.ImGuiCol_Text, in textCol);
int flags = (int)ImGuiWindowFlags.ImGuiWindowFlags_NoDecoration
| (int)ImGuiWindowFlags.ImGuiWindowFlags_AlwaysAutoResize
| (int)ImGuiWindowFlags.ImGuiWindowFlags_NoFocusOnAppearing
| (int)ImGuiWindowFlags.ImGuiWindowFlags_NoInputs;
ImGui.Begin("Multi-Context Compositor Overlay", null, flags);
ImGui.SeparatorText("Multi-Context Compositor");
ImGui.TextUnformatted($"Front: {NameOf(FrontContext())}");
ImGui.TextUnformatted($"MousePos first: {NameOf(CtxMouseFirst)}");
ImGui.TextUnformatted($"MousePos excl.: {NameOf(CtxMouseExclusive)}");
ImGui.TextUnformatted($"Keyboard excl.: {NameOf(CtxKeyboardExclusive)}");
ImGui.TextUnformatted($"DragDrop src: {NameOf(CtxDragDropSrc)}");
ImGui.TextUnformatted($"DragDrop dst: {NameOf(CtxDragDropDst)}");
ImGui.End();
ImGui.PopStyleColor(2);
}
private nint FrontContext()
=> ContextsFrontToBack.Count > 0 ? ContextsFrontToBack[0] : 0;
private static string NameOf(nint ctxRaw)
{
if (ctxRaw == 0)
{
return string.Empty;
}
ImGuiContext* ctx = (ImGuiContext*)ctxRaw;
// ContextName lives inside the native context, so taking its address
// needs no `fixed`: the whole struct is unmanaged.
sbyte* name = &ctx->ContextName.e0;
return Marshal.PtrToStringUTF8((nint)name) ?? string.Empty;
}
/// <summary>
/// True when any mouse button was pressed this frame. The bindings do not
/// expose <c>IsAnyMouseClicked</c>, so the three buttons are queried.
/// </summary>
private static bool AnyMouseClicked()
=> ImGui.IsMouseClicked((int)ImGuiMouseButton.ImGuiMouseButton_Left) != 0
|| ImGui.IsMouseClicked((int)ImGuiMouseButton.ImGuiMouseButton_Right) != 0
|| ImGui.IsMouseClicked((int)ImGuiMouseButton.ImGuiMouseButton_Middle) != 0;
}