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

InputTextCallbacks #541

Closed
Aborres opened this issue Mar 2, 2016 · 10 comments
Closed

InputTextCallbacks #541

Aborres opened this issue Mar 2, 2016 · 10 comments

Comments

@Aborres
Copy link

Aborres commented Mar 2, 2016

Using ImGuiInputTextFlags_CallbackAlways, data->EventKey and data->EventChar are never updated. But if you use ImGuiInputTextFlags_CallbackCharFilter data->EventChar is updated but data->Buf is NULL instead of your text buffer. That gives you few control over the buffer.
Would be also good have your filled size?

@ocornut
Copy link
Owner

ocornut commented Mar 2, 2016

This is correct. The callback are for different events and processed in different spots, but if you pass both flags ImGuiInputTextFlags_CallbackCharFilter|ImGuiInputTextFlags_CallbackAlways you should get both events. You may receive multiple character events back and you can identify event given the EventFlag member.

Arguably CallbackAlways may be badly named here.

Does that solve your problem? What are you trying to do precisely?

Would be also good have your filled size?

You mean the text-length? I will look into adding that.

ocornut added a commit that referenced this issue Mar 2, 2016
…user to maintain it. Zero-ing structure properly before use. (#541)
@ocornut
Copy link
Owner

ocornut commented Mar 2, 2016

I have added a BufTextLen field to spare the user from doing a strlen(). I am also now requesting the user to maintain this field if the buffer is modified. The provided helpers DeleteChars/InsertChars maintain this field and I have added a debug assert when returning from the callback that checks if the value is correct. Removing extraneous calls of strlen() has been useful with dealing with large blurbs of texts.

@ocornut
Copy link
Owner

ocornut commented Mar 2, 2016

Your original post imply that you might want to modify the text buffer manually on a regular character input, which you can't at the moment (but you could if you recorded the key event and then did the processing when the following event comes, with the knowledge that characters events come before the Always event). It is something you really need, and if so could you describe what you need it for?

(I could refactor the code so that Key events allow raw buffer modification but that'll be more work)

@ocornut ocornut removed the bug label Mar 2, 2016
@Aborres
Copy link
Author

Aborres commented Mar 3, 2016

Yesterday I was trying all the possible configurations to test it and this is what I found:
If you use something like:

ImGuiInputTextFlags tflags = 0;
tflags |= ImGuiInputTextFlags_AlwaysInsertMode;
tflags |= ImGuiInputTextFlags_CallbackCharFilter;
tflags |= ImGuiInputTextFlags_EnterReturnsTrue;
tflags |= ImGuiInputTextFlags_AllowTabInput;

In my callback I was checking for:

if  (data->EventKey == ImGuiKey_Enter)  {
...
}

But that event key is never update and is always 0 which is tab on your enum. data->EventChar actually works but the data->Buff is NULL.

So I tried for ImGuiInputTextFlags_CallbackAlways and I found there that neither of data->EventChar and data->EventKey were updated, but I was recieving the correct buffer.

I'm currently working on a small text editor for my game engine to edit some shaders and scripts that is why I asked you. I could change that part by my self If you don't mind.
Some advice to start? :)

@ocornut
Copy link
Owner

ocornut commented Mar 3, 2016

Enter isn't a "character" it is a key.

What are you trying to do exactly? Its hard to help you without knowing that. Why do you need those callbacks?

With ImGuiInputTextFlags_EnterReturnsTrue the InputText function will return true as a convenience when enter is pressed.

On 3 Mar 2016, at 10:58, Aborres notifications@github.com wrote:

Yesterday I was trying all the possible configurations to test it and this is what I found:
If you use something like:

ImGuiInputTextFlags tflags = 0;
tflags |= ImGuiInputTextFlags_AlwaysInsertMode;
tflags |= ImGuiInputTextFlags_CallbackCharFilter;
tflags |= ImGuiInputTextFlags_EnterReturnsTrue;
tflags |= ImGuiInputTextFlags_AllowTabInput;
In my callback I was checking for:

if (data->EventKey == ImGuiKey_Enter) {
...
}
But that event key is never update and is always 0 which is tab on your enum. data->EventChar actually works but the data->Buff is NULL.

So I tried for ImGuiInputTextFlags_CallbackAlways and I found there that neither of data->EventChar and data->EventKey were updated, but I was recieving the correct buffer.

I'm currently working on a small text editor for my game engine to edit some shaders and scripts that is why I asked you. I could change that part by my self If you don't mind.
Some advice to start? :)


Reply to this email directly or view it on GitHub.

@Aborres
Copy link
Author

Aborres commented Mar 3, 2016

yep and I started using it as a Key but as I said before, the EventKey never seems to be updated so I changed the Always for the CharFilter and the EventKey for the EventChar. I want some controll over the text every frame to controll what is happening for my text editor.

For example, controll when tab is pressed and replace /t to a X number of spaces, controll over the vertical size of the text to create line numbers, etc.

@ocornut
Copy link
Owner

ocornut commented Mar 3, 2016

Which field are set by which event is documented in the declaration for the ImGuiTextEditCallbackData structure. So EventChar is set by the CharFilter event and EventKey is set by the Completion/History events (it is actually misdocumented as set for Always which may be the cause of your prejudice here, will fix).

For example, controll when tab is pressed and replace /t to a X number of spaces,

So you are right this would require the CharFilter event to allow modifying the text buffer, so you could use InsertChars() and filter the tab out. So could POSSIBLY rely on catching both CharFilter and Always event, record the CharFilter tab character, filter it and store a boolean, then in the Always event submit the new character. It'll be a bit weird but it should work.

Not sure when I can do this extra development to allow CharFilter event to modify the text buffer, this is rather low priority.

controll over the vertical size of the text to create line numbers

Not sure what that entails exactly.

@Aborres
Copy link
Author

Aborres commented Mar 3, 2016

Ok I have just tried it and seems to work but when the callback is called by the always event, the EventFLag is not updated (is equal to 0) but don't mind it works, quite strange and inefficient but works!

Thank you very much for your time and your support!

ocornut added a commit that referenced this issue Mar 4, 2016
@ocornut
Copy link
Owner

ocornut commented Mar 4, 2016

I have made it that the Always event set EventFlag to ImGuiInputTextFlags_CallbackAlways.

The reason this is a little tricky to finish it (if anyone is inclined into looking at it) is that we provide a UTF-8 buffer and UTF-8 positions to the user. This requires conversions back and forth from internally used wchar and can be quite costly for large text. Adding the extra cost to CharFilter would have an cost on editing large amount of text.

Those conversions are generally making the InputText() cost more complex and costly than it should be, and InputText() is already probably the worse and most brittle piece of code in ImGui so simplifications are very desirable.

The plan is to first modify stb_textedit.h to fully supports UTF-8 which I have roughly outlined there nothings/stb#188

@ocornut
Copy link
Owner

ocornut commented Apr 2, 2016

Closing this. I've added note in the todo list that we could allow CharFilter to modify the buffer, and that would be done I manage to remove all the UTF8-wchar conversion as stated above.

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

2 participants