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 // 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 // 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 addButtonWidth = ImGui.CalcTextSize("+").X + 2 * ImGui.GetStyle().FramePadding.X;
var removeButtonWidth = 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(); ImGui.EndDisabled();
if (listOpen)
if (listOpen && myList != null)
{ {
void DropTarget(int insertIndex, string tooltip) Debug.Assert(myList != null, "Opened tree node even though it should have been a leaf!");
/// Returns true if something was dropped into the droptarget
bool DropTarget(int insertIndex, string tooltip)
{ {
bool dropped = false;
if (ImGui.BeginDragDropTarget()) if (ImGui.BeginDragDropTarget())
{ {
ImGui.SetTooltip(tooltip); 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 // 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. // thus we have to subtract one.
myList.Insert(insertIndex - 1, item); myList.Insert(insertIndex - 1, item);
dropped = true;
} }
} }
} }
ImGui.EndDragDropTarget(); ImGui.EndDragDropTarget();
} }
return dropped;
} }
// Drop at index 0 // Drop at index 0
ImGui.Separator(); ImGui.Separator();
DropTarget(0, "Drop before Element 0."); 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]; object element = myList[i];
// A Bullet point to grab the element // A Bullet point to grab the element
@@ -503,20 +519,38 @@ internal class EntityEditor
ImGui.EndDragDropSource(); 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(); ImGui.SameLine();
object newValue = ShowFieldEditor(element, element.GetType(), $"Element {i}"); 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; myList[i] = newValue;
} }
// Drop after current element // Drop after current element
ImGui.Separator(); ImGui.Separator();
DropTarget(i + 1, $"Drop after Element {i}.");
orderChanged |= DropTarget(i + 1, $"Drop after Element {i}.");
ImGui.PopID();
} }
ImGui.TreePop(); ImGui.TreePop();