-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Drag and Drop helper "reorder drop target" api #2823
base: master
Are you sure you want to change the base?
Conversation
int prev_i = items.index_from_ptr(items.find(color)); | ||
items.find_erase(color); | ||
items.insert(items.begin() + i + (prev_i > i ? 1 : 0), color); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: I'd aim to move the block code above in the loop, maybe start the loop with -1 or end with size()+1 and skip displaying the item but always display the reorder target.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice is the demo also implemented the other form of dragging (which doesn't use the drag and drop api)
imgui.h
Outdated
ImGuiDragDropFlags_AcceptPeekOnly = ImGuiDragDropFlags_AcceptBeforeDelivery | ImGuiDragDropFlags_AcceptNoDrawDefaultRect // For peeking ahead and inspecting the payload before delivery. | ||
ImGuiDragDropFlags_AcceptPeekOnly = ImGuiDragDropFlags_AcceptBeforeDelivery | ImGuiDragDropFlags_AcceptNoDrawDefaultRect, // For peeking ahead and inspecting the payload before delivery. | ||
ImGuiDragDropFlags_AcceptNoBorderPadding = 1 << 13, // Disable extra padding of border rendered around drop destination. | ||
ImGuiDragDropFlags_ReorderHorizontal = 1 << 20, // List that is to be reordered is horizontal. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1 << 20
should be 1 << 14
imgui.h
Outdated
@@ -897,7 +898,9 @@ enum ImGuiDragDropFlags_ | |||
ImGuiDragDropFlags_AcceptBeforeDelivery = 1 << 10, // AcceptDragDropPayload() will returns true even before the mouse button is released. You can then call IsDelivery() to test if the payload needs to be delivered. | |||
ImGuiDragDropFlags_AcceptNoDrawDefaultRect = 1 << 11, // Do not draw the default highlight rectangle when hovering over target. | |||
ImGuiDragDropFlags_AcceptNoPreviewTooltip = 1 << 12, // Request hiding the BeginDragDropSource tooltip from the BeginDragDropTarget site. | |||
ImGuiDragDropFlags_AcceptPeekOnly = ImGuiDragDropFlags_AcceptBeforeDelivery | ImGuiDragDropFlags_AcceptNoDrawDefaultRect // For peeking ahead and inspecting the payload before delivery. | |||
ImGuiDragDropFlags_AcceptPeekOnly = ImGuiDragDropFlags_AcceptBeforeDelivery | ImGuiDragDropFlags_AcceptNoDrawDefaultRect, // For peeking ahead and inspecting the payload before delivery. | |||
ImGuiDragDropFlags_AcceptNoBorderPadding = 1 << 13, // Disable extra padding of border rendered around drop destination. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent naming between _AcceptNoBorderPadding
vs style.AcceptDropPadding
?
I think Border
could be renamed, is a reference to how we draw this drop target but we could imagine it would became a solid thin rectangle, so we should focus on semantic e.g. _AcceptNoPadding
And style.DragDropAcceptPadding
.
But I'm not sure we need this style variable at all for now unless you have a clear use for it.
bb.Max.x += GetContentRegionAvail().x; | ||
bb.Min.y -= g.Style.ItemSpacing.y; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I understand the logic there, why is g.FontSize
used? Is that assuming a certain size for items?
For trees with indentation we'll also want way for the drop target so cover whole width (like ImGuiTreeNodeFlags_SpanFullWidth
) so maybe we need more use of this api before designing the thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is
g.FontSize
used
I intended to get a line height here. Is there a better way? Admittedly font sizes are bit confusing to me, lots of different variables that affect font size.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why a "line height" for a separator between item? Shouldn't the separator be very thin (roughly ~style.ItemSpacing.y?).
Text-only line are g.FontSize
high, most lines with framed widgets are g.FontSize + style.FramePadding.y * 2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I think you want to use window->DC.PrevLineSize.y
here?
Thank you @rokups. |
In-code comments are awesome, please keep doing that 👍 |
Hello Rokas, (1) (2) (3)
|
I have now merged the "simple reordering" part of the demo, the one not actually using the Drag and Drop api, so your PR will conflict, should be an easy rebase+fix+force push hopefully! |
Hi ocornut and rokups. This PR looks really nice. Do you have an idea when it will be committed? I tried to replace the 3 modified files: imgui.cpp, imgui.h and imgui_demo.cpp to use drag and drop in my personal project with imgui v1.74, but I get errors with imgui_draw.cpp: "Out-of-line definition of 'CalcCustomRectUV' does not match any declaration in 'ImFontAtlas' ". Thanks in advance, Jeroen |
Hey @AnimatorJeroen. This is more of a proof of concept. I am not sure it is a right way to do reordering. No idea if we are going to rework it and merge it. I rebased this branch on to master without any issues and no changes were required. Maybe you could provide full error messages? |
Hi Rokups, sorry for the late reply. I use v 1.75 WIP now but I guess I am doing something wrong here. When I replace imgui.cpp, imgui.h and imgui_demo.cpp with your modified versions, I get 17 errors. Thank you! 1>imgui.cpp |
You should not replace files like that. You should either use all files from my branch or apply changes from this PR manually. Replacing half of library with older versions leads to incompatibilities as you can see. |
2798e0b
to
b834a10
Compare
0c1e5bd
to
bb6a60b
Compare
I updated first post detailing rework pushed to the branch of this PR. Rework is quite different from initially proposed code so please read first post again. Previous code has been archived in another branch. People following #143 may find this code useful. |
8b83e0a
to
d735066
Compare
b3b85d8
to
0755767
Compare
c817acb
to
8d39063
Compare
This PR implements a convenience API for reordering of items we discussed in #143.
In order to achieve good visuals i addedImGuiDragDropFlags_AcceptNoBorderPadding
flag which disables expansion of rectangle marking drop target. I took a liberty to add a style variable as well. In my own project i simply commended out hardcoded padding as overall it looked better. I usedItemSpacing
for constructing rectangle for reorders, however at times it can be quite hard to place items on. Not sure what to do about it.https://user-images.githubusercontent.com/19151258/66028917-ca69b300-e506-11e9-984e-432cead8d197.gifI reworked reorder API and it no longer reflects initial description. New API:
And usage (error checking is ignored for brevity):
New API closely mimics behavior of "Drag to reorder items (simple)" in the demo, however it also solves several important issues:
From the point of view of UI user new API is much more intuitive and comfortable to use. We no longer must precisely aim at a spot between two items that is couple pixels high. Instead reorder happens as soon as mouse cursor reaches another reorderable item.
Complicated reordering as displayed in this gif is no longer possible, and that is a good thing. A sensible approach to achieve both reordering and reparenting would be mixing DragAndDrop and Reorder APIs. Reorderable item may show a button for reordering and may use a DragAndDrop API for reparenting.
Old code is archived in https://github.com/rokups/imgui/tree/reorder-api-dnd