I have a need to know (and be able to discriminate) when an InputText widget has it's value changed, and also when enter is pressed.
The need is for along the lines of allowing the user to select an item among a list by typing it in full and pressing enter, but also filter that list as the user types in.
Currently you get one or the other by passing the ImGuiInputTextFlags_EnterReturnsTrue flag.
We have all the information needed by the end of InputTextEx, it's just a matter of choosing which to return because we only return a single bool.
Getting that information through other ways using the api or looking at the internal state directly looks very convoluted, if feasible at all.
So would this be a good case for changing the API to return more information, or do you have a better alternative in mind?
be able to discriminate
I believe you can do this via passing ImGuiInputTextFlags_ReadOnly
have a need to know when an InputText widget has it's value changed
If it's me I'd make a copy of the original char* and do a strcmp check each time when any key is pressed.
Have you considered using ImGuiTextEditCallback? I see a lot of useful information inside.
https://github.com/ocornut/imgui/blob/5942c0814387fce9fa2e5cc681b58bf60877a42e/imgui.h#L1417-L1443
I believe you can do this via passing ImGuiInputTextFlags_ReadOnly
What I mean is I need to be able to tell value changed from enter_pressed, in other words a solution that adds another ImGuiInputTextFlag that simply makes the return value be 'value_changed | enter_pressed' would not be enough. ImGuiInputTextFlags_ReadOnly does not help me, the widget needs to be editable.
If it's me I'd make a copy of the original char* and do a strcmp check each time when, say, any key is pressed.
That's inefficient, I was hoping a solution where I wouln't need to manage an extra copy of the string.
Also EnterReturnsTrue makes it so the user buffer is only updated when enter is pressed, I'd need to look at imgui's internal edit buffer.
Have you considered using ImGuiTextEditCallback? I see a lot of useful information inside.
Yes, but it's not easy to tell from the callback when the value has been changed, it's not simple and I'm not sure it's feasible to do without keeping an extra copy around and comparing every time.
Maybe a bettter patch for imgui here would be a new CallbackData field that has the 'value_changed' information.
Sorry for misunderstanding your requirement.
Maybe a bettter patch for imgui here would be a new CallbackData field that has the 'value_changed' information.
This looks like the best solution, which is backward-compatible (modifying the return type will be a breaking change).
@mizvekov
I have merged a small internal change from another branch which should make the "value_changed" data available in the internal state. I've been trying to regroup some of those state into a place that can be queried but some of them are particularly tricky to deal with either because their computation is costly (and so we wouldn't want to do it on every item unless request) either they can't be relied on if we also rely on clipping items.
At any rate, the particular information you need here is cheap to store/query so I added it now.
While it is not exposed just yet, you may create the function in your own helper file (without touching imgui.cpp)
namespace ImGui
{
IMGUI_API bool ImGui::IsItemValueChanged();
}
#include "imgui_internal.h"
bool ImGui::IsItemValueChanged()
{
ImGuiContext& g = *GImGui;
return (g.CurrentWindow->DC.LastItemStatusFlags & ImGuiItemStatusFlags_ValueChanged) != 0;
}
I think this should solve your problem, let me know!
I will eventually expose this function in the official API as a followup to the work done in #820, #956, #1875 but I need to verify that it'll work properly in more tricky use cases.
This has been now added as IsItemEdited().
Solution worked for me, thanks!
Most helpful comment
@mizvekov
I have merged a small internal change from another branch which should make the "value_changed" data available in the internal state. I've been trying to regroup some of those state into a place that can be queried but some of them are particularly tricky to deal with either because their computation is costly (and so we wouldn't want to do it on every item unless request) either they can't be relied on if we also rely on clipping items.
At any rate, the particular information you need here is cheap to store/query so I added it now.
While it is not exposed just yet, you may create the function in your own helper file (without touching imgui.cpp)
I think this should solve your problem, let me know!
I will eventually expose this function in the official API as a followup to the work done in #820, #956, #1875 but I need to verify that it'll work properly in more tricky use cases.