-
-
Notifications
You must be signed in to change notification settings - Fork 67
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
Add to LineEdit and TextEdit a filter for valid input #7193
Comments
Use a SpinBox node instead of LineEdit if you only want to allow numeric input. Are there many situations where you need a multiline text field to only allow numeric input? |
Note that SpinBox technically also allows non-numeric input, but it's automatically converted to numbers when accepted. The same behavior can be easily coded in LineEdit. Real filtering would allow SpinBoxes to handle it better. |
On the other hand, it would prevent you from writing expressions that use functions such as |
I didn't know that node, but playing arround, I found some issues I don't undertand/I would do in another way:
My desire is to directly restrict what user can type, not applying some function to any text he/she has already written. For example, user can only write up to one "." in some "real number" mode, but none in some "integer number" mode.
Not really, I was generalizing. |
I think this can be implemented as a virtual method: extends LineEdit
func _text_input(character):
if character makes a valid number:
return true
return false something like that. |
@KoBeWi that is one option. Another option (like in Kivy), is that the filter method has to return the |
I would go with @acgc99's solution. The method's signature could be However, you mentioned that text should be added by the validation if it returns a non-empty string. Shouldn't the existing text be modified instead? |
@Calinou , the new character is not added by the validation, it is returned by the validation to the method that inserts the text. This is the example in the case of Kivy with some small modifications:
You redefine the Another issue that you might face is that if you have a numeric input field with |
I ended up doing an add-on. I had to make multiple workarounds to make things work, although by how I still think that Godot base node signals should be changed as commented above. |
A generalized version could be It can validate when text is going to be inserted, deleted, or replaced.
|
If modifications are made, also pasting text or deleting/replacing text by selection should be filtered. |
Note that this proposal could benefit Godot Editor since, for example, |
You can enter an expression that uses letters but returns a float, such as |
Describe the project you are working on
A mobile app where user can track their progress in physical exercises.
Describe the problem or limitation you are having in your project
To introduce record training session info, I use
LineEdit
. The problem is that some info is only numeric, andLineEdit
doesn't allow you to filter user input.Describe the feature / enhancement and how it helps to overcome the problem or limitation
Add to
LineEdit
(andTextEdit
) filter options: allow only integer numbers, real numbers, only text...Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
I used Kivy for Python UIs and I was able to handle this issue. I attach the corresponding documentation. Basically, you have to override the method that passes the new character to be written to the widget.
My proposal is to have a method like in
Kivy
, that gets the new character (and not the new text) and you determine if it is right or not. The workflow would be:""
.If this enhancement will not be used often, can it be worked around with a few lines of script?
I can be solved using regular expressions, see this example.
The
LineEdit
andTextEdit
nodes have the signaltext_changed
, but with this you get the new text, the workflow is:text_changed
is triggered and in the connected function you have to check if the new text is valid.text_changed
).Is there a reason why this should be core and not an add-on in the asset library?
This would also help in games where you have to write a number of coins to trade or something similar; or introducing names (without numbers).
The text was updated successfully, but these errors were encountered: