Fixed list of struct drag and drop, added context menu to remove list entries

This commit is contained in:
Simon Lübeß
2023-12-01 23:14:19 +01:00
parent df446188b6
commit 270e34b983
+42 -8
View File
@@ -405,7 +405,11 @@ internal class EntityEditor
// The buttons should always be visible -> save whether the node is open
// If we have no instance, the user shouldn't be able to open the list
bool listOpen = ImGui.TreeNodeEx(fieldName, myList == null ? ImGuiTreeNodeFlags.Leaf : ImGuiTreeNodeFlags.None);
bool listOpen = ImGui.TreeNodeEx(fieldName,
myList == null ? ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.NoTreePushOnOpen : ImGuiTreeNodeFlags.Framed);
if (myList == null)
listOpen = false;
var addButtonWidth = ImGui.CalcTextSize("+").X + 2 * ImGui.GetStyle().FramePadding.X;
var removeButtonWidth = ImGui.CalcTextSize("-").X + 2 * ImGui.GetStyle().FramePadding.X;
@@ -441,11 +445,15 @@ internal class EntityEditor
ImGui.EndDisabled();
if (listOpen)
{
Debug.Assert(myList != null, "Opened tree node even though it should have been a leaf!");
if (listOpen && myList != null)
{
void DropTarget(int insertIndex, string tooltip)
/// Returns true if something was dropped into the droptarget
bool DropTarget(int insertIndex, string tooltip)
{
bool dropped = false;
if (ImGui.BeginDragDropTarget())
{
ImGui.SetTooltip(tooltip);
@@ -471,20 +479,28 @@ internal class EntityEditor
// If the original index is before the insert index, the insert index will be moved after removing the element
// thus we have to subtract one.
myList.Insert(insertIndex - 1, item);
dropped = true;
}
}
}
ImGui.EndDragDropTarget();
}
return dropped;
}
// Drop at index 0
ImGui.Separator();
DropTarget(0, "Drop before Element 0.");
for (int i = 0; i < myList.Count; i++)
bool orderChanged = false;
for (int i = 0, id = 0; i < myList.Count; i++, id++)
{
ImGui.PushID(id);
object element = myList[i];
// A Bullet point to grab the element
@@ -503,20 +519,38 @@ internal class EntityEditor
ImGui.EndDragDropSource();
}
DropTarget(i, $"Drop before Element {i}.");
orderChanged |= DropTarget(i, $"Drop before Element {i}.");
if (ImGui.BeginPopupContextWindow($"ListElementContext{i}"))
{
if (ImGui.MenuItem($"Remove Element {i}"))
{
myList.RemoveAt(i);
i--;
ImGui.EndPopup();
ImGui.PopID();
continue;
}
ImGui.EndPopup();
}
ImGui.SameLine();
object newValue = ShowFieldEditor(element, element.GetType(), $"Element {i}");
if (newValue != DidNotChange)
// Don't apply changes, when the order changed
if (newValue != DidNotChange && !orderChanged)
{
myList[i] = newValue;
}
// Drop after current element
ImGui.Separator();
DropTarget(i + 1, $"Drop after Element {i}.");
orderChanged |= DropTarget(i + 1, $"Drop after Element {i}.");
ImGui.PopID();
}
ImGui.TreePop();