ControlP5 smart Textfields
in
Contributed Library Questions
•
3 years ago
I'm using controlP5 for the ui of an application I'm working on.
It is a nice and great library.
But using a bunch of Textfields I realized that its behavior is a bit odd, considering the standard ui textfield behaviors.
Textfiled should select all its text content on get focus, to allow to start typing and replacing with the new content.
Then it should allow to submit the new content on focus lost (this is useful to avoid confirm btn in Processing sketches, which are not regular web form).
Here a snippet to achive the above behavior.
Maybe there's a better solution o even better it could be included in the Library.
When using this snippet, clicking on a Textfield, its content will be deleted, so you'll be allowed to start typing inside.
When that Textfield will lost the focus, the new content will be submit, while if the content is still empty, no changes will be made.
- Textfield currentFocusedTf;
- String currentHoldValue = "";
- void mouseReleased()
- {
- ControllerInterface[] ctrl = controlP5.getControllerList();
- currentFocusedTf = null;
- for(int i=0; i<ctrl.length; ++i)
- {
- ControllerInterface ct = ctrl[i];
- if(ct instanceof Textfield)
- {
- Textfield tf = (Textfield) ct;
- if( tf.isFocus())
- {
- currentFocusedTf = tf;
- currentHoldValue = tf.getText();
- tf.setText("");
- return;
- }
- }
- }
- }
- void mousePressed()
- {
- if(currentFocusedTf != null)
- {
- if(currentFocusedTf.getText().length()<=0)
- {
- currentFocusedTf.setText(currentHoldValue);
- }else{
- currentFocusedTf.submit();
- }
- }
- }
1