Skip to content
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

Can't change underlying number value when InputText has focus #2881

Open
kudaba opened this issue Nov 5, 2019 · 5 comments
Open

Can't change underlying number value when InputText has focus #2881

kudaba opened this issue Nov 5, 2019 · 5 comments

Comments

@kudaba
Copy link
Contributor

kudaba commented Nov 5, 2019

Version: ~1.72b (a116439)
Branch: Docking

Back-ends: imgui_impl_win32.cpp + imgui_impl_dx12.cpp
Operating System: win10

My Issue/Question:

While trying to implement custom interaction with number fields I hit an issue where I need to clear the focus of the control to change the underlying value. To easily repro the issue:

        static float s_value;
        ImGui::InputFloat("Changing Float", &s_value);
        s_value += ImGui::GetIO().DeltaTime;

Causes:

changing_float

Example of controls I was trying to setup:
https://gist.github.com/kudaba/01337c088a0bcef0c2e22a4e846f729c

@ocornut
Copy link
Owner

ocornut commented Nov 5, 2019

Hello,

InputFloat/InputText/etc. need to own the master copy of the data when active for several reasons (not getting into details here but it's not an easy thing to change with the current code).

I don't know what your input scheme is for that widget, but one possible work-around is to set the ImGuiInputTextFlags_ReadOnly flag when using the other form of interaction. This will force InputText to retranslate the source input value into a text buffer (in some edge case this can affect the text cursor).

@kudaba
Copy link
Contributor Author

kudaba commented Nov 5, 2019

I was using that, but it was a bit weird since I had to set that value before, but I was changing the value after since I need to check hover state, etc. I just tried locally adding a new ImGuiInputTextFlags_Dynamic to re-initialize the control if it changes, but floating point precision is making it a bit unstable. I think I'll just have to add a something to ImGuiInputTextState to force it to reset in a nice way. In my case it's for a custom control so that makes sense. I'm not sure if this is a feature we would want to be publicly facing anyway.

@ocornut
Copy link
Owner

ocornut commented Nov 5, 2019

I think it would be good to first add that feature as an imgui_internal.h api (calling a function or poking into the state structure), when we redesign InputText() I would definitively want to take this into account! (along with more important kind-of-opposite change such as #701 !)

@kudaba
Copy link
Contributor Author

kudaba commented Nov 5, 2019

This seems to do the job:
kudaba@53f90d7

Unfortunately I'm not in a good state to make a pr for it.

kudaba added a commit to kudaba/imgui that referenced this issue Nov 11, 2019
…seful in custom controls if the value gets modified while the text input control is in focus. ocornut#2881
kudaba added a commit to kudaba/imgui that referenced this issue Nov 11, 2019
…seful in custom controls if the value gets modified while the text input control is in focus. ocornut#2881
@JPGygax68
Copy link

JPGygax68 commented Apr 3, 2020

I have a related use case. I'm writing a file dialog and am using InputText to let the user type in the name of the next leg of the path. The user can also go back to the parent using backspace when the input field is empty.
But this of course requires re-initializing the text of the input field as the user moves down or up the path chain. I get odd behavior in those cases, and it now occurred to me that this must be due to the internal state of the InputText not being updated after I change the content of its buffer from outside
I'm attaching a screenshot just to illustrate what I'm doing.
Is there a proper way to change the content of the buffer from outside? Or should I try to use the Always callback?

grafik

EDIT: I made this work using the callback interface, though it was a bit convoluted.

ocornut added a commit that referenced this issue Feb 7, 2024
Very highly requested feature (#6962, #5219, #3290, #4627, #5054, #3878, #2881, #1506, #1216, #968).
Also useful for interactive completion/selection popups (#2057, #718)
Based on @kudaba PR. Design for Inputtext V2 should make this obsolete.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants