question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using IntPtr (For example, with SetDragDropPayload)

See original GitHub issue

Hi there! I have a bit of a newbie question to ask since I’m having a hard time finding how people approach this: How should I go about using ImGui.NET methods that take an IntPtr?

For example, ImGui.SetDragDropPayload takes a type string, an IntPtr, and a size. When I ImGui.GetDragDropPayload() I don’t seem to be able to just grab the type that I set (I can only query if it is a certain type)… And I’m not sure how to use IntPtr to represent an object in my game engine.

I feel like there is a fundamental paradigm that I’m not understanding here, as using IntPtr to hold game data seems like a common thing in ImGui.NET. I’ve done some looking into how to represent objects as IntPtr in .Net, but this seems to point me to creating IntPtr for use in WinApi functions and I haven’t been able to get anything working yet or understand how I should be approaching this…

As you can probably tell, I’m also entirely unfamiliar with the C++ Dear ImGui, so this makes things even trickier for me to get my head around. Any thoughts would be appreciated!

Thanks, Allen

Edit:

Here’s an example of how I’m using it right now. As I mentioned, I don’t know how to use the payload propertly, so I’m just using a separate static object to represent what’s currently being dragged. Then checking to see if the payload’s NativePtr is null or not. It seems to work, I think, but is obviously not ideal:

private static unsafe void SubmitTestWindow()
{
    ImGui.Begin("Test");

    string[] items = new string[] { "hello", "world", "how", "are", "you?" };

    for (int i = 0; i < items.Length; i++)
    {
        ImGui.Button(items[i]);
        if (ImGui.BeginDragDropSource())
        {
            ImGui.Text(items[i]);
            ImGui.SetDragDropPayload(typeof(string).FullName, IntPtr.Zero, 0);
            draggedItem = items[i];
            ImGui.EndDragDropSource();
        }
        if (ImGui.BeginDragDropTarget())
        {
            var payload = ImGui.AcceptDragDropPayload(typeof(string).FullName);
            if (payload.NativePtr != null)
            {
                Console.WriteLine("Dropped " + draggedItem + " onto " + items[i]);
                draggedItem = null;
            }
            ImGui.EndDragDropTarget();
        }
    }

    ImGui.End();
}

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Rafiuthcommented, Sep 16, 2021

@Thraka I’m a bit rusty on ImGui, but I think you can just pass the variable address as the data parameter, like in the example below:

string[] dragItems = { "Apple", "Banana", "Orange", "Mango" };
private unsafe void DragDropSample()
{
    for (int i = 0; i < dragItems.Length; i++) {
        ImGui.Button(dragItems[i]);
    
        if (ImGui.BeginDragDropSource()) {
            ImGui.Text(dragItems[i]);
            
            ImGui.SetDragDropPayload("ItemIndex", (IntPtr)(&i), sizeof(int));
            ImGui.EndDragDropSource();
        }
        if (ImGui.BeginDragDropTarget()) {
            var payload = ImGui.AcceptDragDropPayload("ItemIndex");
            if (payload.NativePtr != null) {
                var dataPtr = (int*)payload.Data;
                int srcIndex = dataPtr[0];
                
                var srcItem = dragItems[srcIndex];
                dragItems[srcIndex] = dragItems[i];
                dragItems[i] = srcItem;
            }
            ImGui.EndDragDropTarget();
        }
    }
}
0reactions
zaafarcommented, Jan 10, 2023

I did it in less than 20 lines. (NOTE: this code is inside the BeginTabBar -> for loop.

                    if (ImGui.BeginDragDropSource())
                    {
                        this.ruleIndexToSwap = i;
                        ImGui.SetDragDropPayload("RuleIndex", IntPtr.Zero, 0);
                        ImGui.Text(currRule.Name);
                        ImGui.EndDragDropSource();
                    }

                    if (ImGui.BeginDragDropTarget())
                    {
                        ImGui.AcceptDragDropPayload("RuleIndex");
                        if (ImGui.IsMouseReleased(ImGuiMouseButton.Left))
                        {
                            this.SwapRules(this.ruleIndexToSwap, i);
                        }

                        ImGui.EndDragDropTarget();
                    }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using IntPtr (For example, with SetDragDropPayload) #184
I've done some looking into how to represent objects as IntPtr in .Net, but this seems to point me to creating IntPtr for...
Read more >
How to effectively/properly use IntPtr in ImGui.NET
SetDragDropPayload takes a type string , an IntPtr , and a size unint . When I ImGui. ... Here's an example of how...
Read more >
idisposable - Proper IntPtr use in C# - Stack Overflow
An IntPtr is only a value type which size matches the size of a pointer on the target platform. You need to use...
Read more >
Demo: Added basic Drag and Drop demo. (#143, #1931) · ...
// ColorEdit widgets automatically act as drag source and drag target. // They are using standardized payload strings IMGUI_PAYLOAD_TYPE_COLOR_3F and ...
Read more >
IntPtr.Add(IntPtr, Int32) Method (System)
The following example instantiates an IntPtr object that points to the beginning of a ten-element array, and then calls the Add method to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found