top of page

ImGui Drag and Drop

School - Sep 2019 – Jun 2020

DragAndDrop.gif

The following code is from the editor I made for SoulCaster.

Window

First things first, there needs to be a window to show the prefabs. I set up the window with some parameters for the first use making the window usable out of the gate.

Displaying The Prefabs

Before I start displaying prefabs, I calculate how many I can show on a line. This way, I know when to start a new line and not show too many or not enough on one line. Then for each prefab, I put the texture and name into a group. The group lets ImGui watch both of them as one object, so if the user clicks on either the name or icon, ImGui will tell me rather than just picking one to watch. When a prefab is clicked, I update the propriety view with the prefab's info, so the user can then edit it.

Creating The Payload

When the user clicks and starts to drag, the window showing a preview of the prefab should show up. This can be done with ImGui::BegainDragDropSource. The first thing that needs to be done for the payload is the payload's data needs to be set. Due to my editor's nature, only one payload of this type can exist at a time, so the name is not as important as long as it matches when trying to read the payload later. I also set the data of the payload to be the name of the prefab to be instantiated. Next, I add the texture and name of the prefab to show in the preview window. 

Instantiating The Prefab

Finally, all that is left is to read the payload and instantiate the prefab when the user lets go of the left mouse button. Before I instantiate the prefab, I make sure the user has a valid payload to drop and that they are not trying to drop it on an ImGui window. Once I know that the payload and drop location are valid, I read the data and send a command to instantiate the prefab.

Full Code

​

bottom of page