You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Finaling event from various widgets (sliders etc.) for Undo/Redo systems #1875
Hi!
Thanks for the great imgui! I really made big leaps in no time with this fantastic library. I'm implementing an editor for my game but i'm failing to grab the right events for my undo/redo stack.
If i do
if(ImGui::SliderFloat())
i get every value change which makes my stack a mess. However i want only the value that was changed after the slider was released.
Is there a functionality or an event for that? I tried to hook manually by waiting for ImGui::GetIO().WantCaptureMouse to become false again but this is not set immediately but only when i move to another item (and does not resemble release states). Same goes for the TextInput, i don't want each character but only if the change was completed.
Is there a way to do that intuitively or what would be best practice?
Thanks!
TL;DR: how to grab slider input changes immediately after sliding is finished? need it for undo.
The problem with this approach is highlighted in #956, it currently fails with multi-component widgets (can be fixed) and it's particularly tricker to handle for e.g. the color edit popup. Working on finding a solution.
Some cases may be unsatisfactory: e.g. if you use InputFloat() and the user presses the +/- buttons several times. Depending on how your Undo system may work you may detect no-ops, or merge events in the undo stack. You might also use GetItemID() from imgui_internal.h if querying an unique ID for the item can help you merging modifications.
Let me know how it works for you.
(*) I am still not very happy with them, but considering the wide amount of widget they support I find it tricky to find a non-ambiguous name. Suggestions welcome, I can still rename the functions while they are fresh and not in a tagged release. EDITIsItemDeactivatedAfterChange was eventually renamed to IsItemDeactivatedAfterEdit.
This section of the Demo window can let you understand how it works:
changed the title [-]Events from Input Sliders (for Undo/Redo)[/-][+]Finally event from various widgets (sliders etc.) for Undo/Redo systems[/+]on Jun 13, 2018
changed the title [-]Finally event from various widgets (sliders etc.) for Undo/Redo systems[/-][+]Finaling event from various widgets (sliders etc.) for Undo/Redo systems[/+]on Jun 13, 2018
We need a pair of API to record undo: BeginChange - e.g. drag begins. In this case we can record the orignal old value. EndChange - e.g. drag ends. As the new final value got, we can record the new and old values for the undo.
Edit: the IsItemDeactivatedAfterChange API only acts as the EndChange phase, so it's hard to track the old value, since for sliders, value is always changing.
I deal with this by just merging actions together. Whenever an action is pushed onto the undo-stack the topmost action on the stack checks if it can merge the action, if it can then it merges and discards the added action. Same field edits are on a time threshold before they are classified as new actions.
Code for one case (just to demonstrate how infantile those blocks are):
Actually, we can properly handle undo recording by following code. Tested for DragFloat, DragFloat2 and DragFloat3.
// Current editing valuestaticfloat value = 0.0f;
// Old value, used for store value before editingstaticfloat oldValue = 0.0f;
if (ImGui::DragFloat("Drag float", &value))
{
// Here we apply change to editing target
}
if (ImGui::IsItemActive() && !IsItemActiveLastFrame())
{
// Here we record old value when item is actived for the first time.
oldValue = value;
}
if (ImGui::IsItemDeactivatedAfterChange())
{
// Here we can save undo (to undo stack or something). Triggered once just after user stops editing. // RecordUndo(oldValue, value);
}
@superwangkai Interestingly, every time in the past I have implemented an Undo system it didn't need the "old value" to function but I can see why yours would need it.
I am not sure how your hypothetical BeginChange/EndChange api would work, and how it would scale to most complex widgets.
Open to more changes if needed but I'm not sure what to do at the moment.
@ocornut To my understanding, old value and new value are store as a pair recored for a particular undo action. When we want to undo, we apply the old value to the target; when we redo, we apply the new value back to the target.
if (ImGui::IsItemActive() && !IsItemActiveLastFrame()) acts as the BeginChange phase
while if (ImGui::IsItemDeactivatedAfterChange()) acts as the EndChange phase. These two phases are just hypothetical as you mentioned, please don't be too serious about them.
Edit: Plus, we don't want value changes between these two phases, so we can get a clean, single piece of undo action.
Please inspire me if I'm wrong or there is something I missed.
For ColorEdit*, there will be two undo actions recorded and I need to study more to find the reason. However, for InputText/Drag*, AFAIK, it works great.
@superwangkai, not really a hack - IIRC that's what IBM laid down with CUA decades ago. Ends up being necessary for a lot of things anyways. Particularly things that have long-running consequences (ie. trigger a distance field recalc, baking, etc) or are prone to fine but meaningless editing (gizmos/manipulators), without merging actions just positioning something in a scene floods an undo-stack with garbage with all of the move -> change-view -> move -> change-view -> repeat ad-f.
Edit: but yeah, start/end would still be handy for stuff.
Yes, for DragFloat, DragFloat2, DragFloat3, DragFloat4, InputText, DragInt, DragInt2, DragInt3, DragInt4 and also widgets of the ColorEdit except the color wheel, I'm happy with current behavior. But I think it is more natural for ColorEdit to merge users mouse moves on the wheel and store one undo record after user closed the picker. (Edit: To me, for the mouse moves on the color wheel, they are more like adjustments before user finally submitting the update. So, I prefer to store only one undo record for this color editing.)
To make my expression better... Just imagine the following bad scenario -
You suddenly want to adjust some color in your cool editor. So you open this color picker, and then you click and move around for some times on the color wheel, then you are happy with the result and close the color picker.
Then you regret, so you press Ctrl+Z, but you are so confused, since there are many color editing records. You just don't know which one is the original color...
For a standard float widget, when the users release they mouse after dragging or finished entering some number by return key, we should regard the operation as an intended editing. It seems to be the best we can do.
For a color picker, if we regard every movement on the open picker as a valid editing, it will be a mess. But if we choose user's closing picker as a confirmation, we get the same clean result as a float widget.
Additionally, we have to select multiple times to get a valid color on the picker, e.g. first hue, then alpha... and you get a color you want.
Yeah, I understand now. It looks like ideally IsItemActivated should happen when you click on color and open picking widget, and IsItemDeactivatedAfterEditIsItemDeactivated should happen when you click on some other widget (which closes the picking widget).
I don't have my testing code now, however, generally, current behavior is that IsItemDeactivatedAfterEdit happens after you clicked on the widgets of ColorEdit or moved your mouse on the color wheel.
I noticed another small issue with IsItemDeactivatedAfterEdit. If you have a single item (i.e. InputInt) in a window and try to tab out, since the active control doesn't change, it won't trigger a deactivate.
I also noticed small problem with IsItemDeactivatedAfterEdit when used after slider widget.
IsItemDeactivatedAfterEdit returns true in frame when there is mouse up (so io.MouseDown[0] is false) but in that frame slider do not calculate value, so out parameter (e.g. float* v of SliderFloat) is default value.
That means that if I want to apply value on IsItemDeactivatedAfterEdit, nothing changes.
Possible fix would be removing pointed line in SliderBehaviorT
When you drag a color into another it writes into a different item without that written item being ever activated.
As a result IsItemDeactivatedAfterEdit() never returns true there:
While #4714 (after upcoming fix) would not suffer from this, a drag and drop target would.
Closing this ancient issue.
Many issues have been fixed with IsItemDeactivated(), IsItemDeactivatedAfterEdit() over the years. (#5184, #5904, #6766, #8303, #8004, #8149, #4714 and more).
I am not ruling this that there might still be issues, but the reality is that this particular topic here has too many messages going in many directions and it's hard to make sense of it consider many changes have been made since then.
If you or anyone has further issues implementing an undo/redo system please open a new issue. Thank you!
Activity
ocornut commentedon Jun 12, 2018
This is what you could use:
(from #820)
I've been meaning to add it but couldn't think of a function name (naming is the hardest thing) :)
EDIT IsItemReleased() would be nice but it is ambiguous because we have
IsItemActive()andIsItemClicked().ocornut commentedon Jun 12, 2018
Here's a better name,
IsItemDeactivated()The problem with this approach is highlighted in #956, it currently fails with multi-component widgets (can be fixed) and it's particularly tricker to handle for e.g. the color edit popup. Working on finding a solution.
Added IsItemDeactivated() to query if the last item was active previo…
Added IsItemDeactivatedAfterChange() if the last item was active prev…
ocornut commentedon Jun 12, 2018
After agonizing a over the naming convention (*) I think I've got a decent solution for this long-standing issue.
(How to get a slider or input field to only return results once the user stops editing? #820, Widgets returning true only when they change value? #956, Finaling event from various widgets (sliders etc.) for Undo/Redo systems #1875)
Some cases may be unsatisfactory: e.g. if you use InputFloat() and the user presses the +/- buttons several times. Depending on how your Undo system may work you may detect no-ops, or merge events in the undo stack. You might also use
GetItemID()fromimgui_internal.hif querying an unique ID for the item can help you merging modifications.Let me know how it works for you.
(*) I am still not very happy with them, but considering the wide amount of widget they support I find it tricky to find a non-ambiguous name. Suggestions welcome, I can still rename the functions while they are fresh and not in a tagged release.
EDIT
IsItemDeactivatedAfterChangewas eventually renamed toIsItemDeactivatedAfterEdit.This section of the Demo window can let you understand how it works:
[-]Events from Input Sliders (for Undo/Redo)[/-][+]Finally event from various widgets (sliders etc.) for Undo/Redo systems[/+][-]Finally event from various widgets (sliders etc.) for Undo/Redo systems[/-][+]Finaling event from various widgets (sliders etc.) for Undo/Redo systems[/+]devkaiwang commentedon Jun 19, 2018
We need a pair of API to record undo:
BeginChange- e.g. drag begins. In this case we can record the orignal old value.EndChange- e.g. drag ends. As the new final value got, we can record the new and old values for the undo.Edit: the
IsItemDeactivatedAfterChangeAPI only acts as theEndChangephase, so it's hard to track the old value, since for sliders, value is always changing.JSandusky commentedon Jun 20, 2018
I deal with this by just merging actions together. Whenever an action is pushed onto the undo-stack the topmost action on the stack checks if it can merge the action, if it can then it merges and discards the added action. Same field edits are on a time threshold before they are classified as new actions.
Code for one case (just to demonstrate how infantile those blocks are):
devkaiwang commentedon Jun 20, 2018
@JSandusky It's a smart hack and we need something clean :)
devkaiwang commentedon Jun 20, 2018
Actually, we can properly handle undo recording by following code. Tested for DragFloat, DragFloat2 and DragFloat3.
ocornut commentedon Jun 20, 2018
@superwangkai Interestingly, every time in the past I have implemented an Undo system it didn't need the "old value" to function but I can see why yours would need it.
I am not sure how your hypothetical
BeginChange/EndChangeapi would work, and how it would scale to most complex widgets.Open to more changes if needed but I'm not sure what to do at the moment.
devkaiwang commentedon Jun 20, 2018
@ocornut To my understanding, old value and new value are store as a pair recored for a particular undo action. When we want to undo, we apply the old value to the target; when we redo, we apply the new value back to the target.
if (ImGui::IsItemActive() && !IsItemActiveLastFrame())acts as theBeginChangephasewhile
if (ImGui::IsItemDeactivatedAfterChange())acts as theEndChangephase. These two phases are just hypothetical as you mentioned, please don't be too serious about them.Edit: Plus, we don't want value changes between these two phases, so we can get a clean, single piece of undo action.
Please inspire me if I'm wrong or there is something I missed.
devkaiwang commentedon Jun 20, 2018
For ColorEdit*, there will be two undo actions recorded and I need to study more to find the reason. However, for InputText/Drag*, AFAIK, it works great.
JSandusky commentedon Jun 20, 2018
@superwangkai, not really a hack - IIRC that's what IBM laid down with CUA decades ago. Ends up being necessary for a lot of things anyways. Particularly things that have long-running consequences (ie. trigger a distance field recalc, baking, etc) or are prone to fine but meaningless editing (gizmos/manipulators), without merging actions just positioning something in a scene floods an undo-stack with garbage with all of the
move -> change-view -> move -> change-view -> repeat ad-f.Edit: but yeah, start/end would still be handy for stuff.
27 remaining items
eliasdaler commentedon May 31, 2019
Are you talking about double-clicking on a slider and then editing the value via keyboard input?
devkaiwang commentedon May 31, 2019
@ocornut
I hope I could express better...
Yes, for
DragFloat, DragFloat2, DragFloat3, DragFloat4, InputText, DragInt, DragInt2, DragInt3, DragInt4and also widgets of theColorEditexcept the color wheel, I'm happy with current behavior. But I think it is more natural forColorEditto merge users mouse moves on the wheel and store one undo record after user closed the picker. (Edit: To me, for the mouse moves on the color wheel, they are more like adjustments before user finally submitting the update. So, I prefer to store only one undo record for this color editing.)@eliasdaler
I just met some issue with
ColorEdit4, and should with as well asColorEdit3devkaiwang commentedon May 31, 2019
To make my expression better... Just imagine the following bad scenario -
You suddenly want to adjust some color in your cool editor. So you open this color picker, and then you click and move around for some times on the color wheel, then you are happy with the result and close the color picker.
Then you regret, so you press Ctrl+Z, but you are so confused, since there are many color editing records. You just don't know which one is the original color...
ocornut commentedon May 31, 2019
I understand what you mean but wouldn't you have the exact same multiple CTRL+Z issue by editing a standard float multiple time?
(I however acknowledge your request, I just don't know what's the best code and design to achieve it nor when I will be able to look at it.)
devkaiwang commentedon May 31, 2019
For a standard float widget, when the users release they mouse after dragging or finished entering some number by
returnkey, we should regard the operation as an intended editing. It seems to be the best we can do.For a color picker, if we regard every movement on the open picker as a valid editing, it will be a mess. But if we choose user's closing picker as a confirmation, we get the same clean result as a float widget.
Additionally, we have to select multiple times to get a valid color on the picker, e.g. first hue, then alpha... and you get a color you want.
eliasdaler commentedon May 31, 2019
Yeah, I understand now. It looks like ideally
IsItemActivatedshould happen when you click on color and open picking widget, andIsItemDeactivatedAfterEditIsItemDeactivatedshould happen when you click on some other widget (which closes the picking widget).What is the current behaviour?
devkaiwang commentedon Jun 3, 2019
@eliasdaler
I don't have my testing code now, however, generally, current behavior is that
IsItemDeactivatedAfterEdithappens after you clicked on the widgets ofColorEditor moved your mouse on the color wheel.Misc: Made Button(), ColorButton() not trigger an "edited" event lead…
kudaba commentedon Dec 17, 2019
I noticed another small issue with
IsItemDeactivatedAfterEdit. If you have a single item (i.e.InputInt) in a window and try to tab out, since the active control doesn't change, it won't trigger a deactivate.JakubLukas commentedon Oct 5, 2021
I also noticed small problem with IsItemDeactivatedAfterEdit when used after slider widget.
IsItemDeactivatedAfterEdit returns true in frame when there is mouse up (so io.MouseDown[0] is false) but in that frame slider do not calculate value, so out parameter (e.g. float* v of SliderFloat) is default value.
That means that if I want to apply value on IsItemDeactivatedAfterEdit, nothing changes.
Possible fix would be removing pointed line in
SliderBehaviorTocornut commentedon Nov 24, 2021
When investigating and working on a solution for #4714 I came up with an issue:
{ static ImVec4 color0(1.0f, 0.0f, 0.0f, 1.0f); static ImVec4 color1(0.0f, 1.0f, 0.0f, 1.0f); static int edited_ret_frame = -1; static int edited_ret_field = 0; static int edited_query_frame = -1; static int edited_query_field = 0; static int deactivated_frame = -1; static int deactivated_field = 0; if (ImGui::ColorEdit4("color0", &color0.x)) { edited_ret_frame = ImGui::GetFrameCount(); edited_ret_field = 0; } if (ImGui::IsItemEdited()) { edited_query_frame = ImGui::GetFrameCount(); edited_query_field = 0; } if (ImGui::IsItemDeactivatedAfterEdit()) { deactivated_frame = ImGui::GetFrameCount(); deactivated_field = 0; } if (ImGui::ColorEdit4("color1", &color1.x)) { edited_ret_frame = ImGui::GetFrameCount(); edited_ret_field = 1; } if (ImGui::IsItemEdited()) { edited_query_frame = ImGui::GetFrameCount(); edited_query_field = 1; } if (ImGui::IsItemDeactivatedAfterEdit()) { deactivated_frame = ImGui::GetFrameCount(); deactivated_field = 1; } ImGui::Text("Edited (ret) frame %d, field %d", edited_ret_frame, edited_ret_field); ImGui::Text("Edited (query) frame %d, field %d", edited_query_frame, edited_query_field); ImGui::Text("Deactivated frame %d, field %d", deactivated_frame, deactivated_field); }When you drag a color into another it writes into a different item without that written item being ever activated.
As a result
IsItemDeactivatedAfterEdit()never returns true there:While #4714 (after upcoming fix) would not suffer from this, a drag and drop target would.
ocornut commentedon Jan 20, 2025
Closing this ancient issue.
Many issues have been fixed with IsItemDeactivated(), IsItemDeactivatedAfterEdit() over the years. (#5184, #5904, #6766, #8303, #8004, #8149, #4714 and more).
I am not ruling this that there might still be issues, but the reality is that this particular topic here has too many messages going in many directions and it's hard to make sense of it consider many changes have been made since then.
If you or anyone has further issues implementing an undo/redo system please open a new issue. Thank you!
Added ImGuiItemFlags_LiveEdit flag, and much-awaited support for disa…
Split ImGuiItemFlags_LiveEdit into ImGuiItemFlags_LiveEditText, ImGui…