Made a TextFieldFilter for proper floating point entry

Any community contributions to libgdx go here! Some may get included in the core API when permission is granted.

Made a TextFieldFilter for proper floating point entry

Postby Suds » Sat Sep 20, 2014 8:07 am

Feel free to use anywhere / distribute / incorporate into libgdx alongside the DigitsOnlyFilter. Under the apache 2 license (seriously, just use it anywhere. Commercial or otherwise. Anyone could have made this.)

This is a simple class that implements the TextFieldFilter interface to enforce correct floating point entry into scene2d text fields. It actually took a few hours trying to catch all the incorrect cases. :D

Code: Select all
package com.suds.tools;

import com.badlogic.gdx.scenes.scene2d.ui.TextField;

/**
 * Org.: DefeatThePurpose Entertainment
 * User: Suds (Scott Drew) <suds@defeatthepurpose.net>
 * Date: 19/09/2014
 * Time: 5:56 PM
 */
public class FloatTextFilter implements TextField.TextFieldFilter {
    @Override
    public boolean acceptChar(TextField textField, char c) {
        if(Character.isDigit(c) && !textField.getText().contains("-")) return true;
        else if(Character.isDigit(c) && textField.getCursorPosition() > 0) return true;
        else if(c == '-' && textField.getText().length() == 0) return true;
        else if(c == '.' && !textField.getText().contains(".") && !textField.getText().contains("-")) return true;
        else if(c == '.' && !textField.getText().contains(".") && textField.getText().contains("-")) return textField.getCursorPosition() > 0;
        else if(c == '-' && !textField.getText().contains("-")) return textField.getCursorPosition() == 0;

        return false;
    }
}


here is a paste link, in case the code blocks on the forum suck for copying from: http://pastie.org/private/frcthqxyb5lasxnspeahqa
Suds
 
Posts: 5
Joined: Thu Aug 07, 2014 9:14 am

Re: Made a TextFieldFilter for proper floating point entry

Postby Tonielro » Sun Sep 21, 2014 4:39 am

that is great. How do we use it? Why not give an example on how to use in our project ?
Tonielro
 
Posts: 62
Joined: Tue Apr 08, 2014 5:39 pm

Re: Made a TextFieldFilter for proper floating point entry

Postby BurningHand » Sun Sep 21, 2014 3:25 pm

The's some complicated logic you have there, the following seems like it should work while being more readable:

Code: Select all
public class FloatTextFieldFilter implements TextFieldFilter {

   @Override
   public boolean acceptChar (TextField textField, char c) {
      if (textField.getCursorPosition() == 0) {
         return acceptStartChar(textField, c);
      }
      String text = textField.getText();
      return isDigit(c) || (c == '.' && text.indexOf('.') == -1);
   }
   private boolean acceptStartChar(TextField textField, char c) {
      String text = textField.getText();
      if (text.isEmpty() || isDigit(text.charAt(0))) {
         return isDigit(c) || c == '.' || c == '-' || c == '+';
      }
      if (text.charAt(0) == '.') {
         return isDigit(c);
      }
      return false;            
   }
   private boolean isDigit(char c) {
      return Character.isDigit(c);
   }

}


Usage:
Code: Select all
TextField text = new TextField("", skin);
text.setTextFieldFilter(new FloatTextFieldFilter());
IRC: nexsoftware / mobidevelop; GitHub: MobiDevelop;
BurningHand
 
Posts: 2812
Joined: Mon Oct 25, 2010 4:35 am


Return to Libgdx Contributions

Who is online

Users browsing this forum: No registered users and 1 guest