Example usage for com.badlogic.gdx.scenes.scene2d.ui TextField getCursorPosition

List of usage examples for com.badlogic.gdx.scenes.scene2d.ui TextField getCursorPosition

Introduction

In this page you can find the example usage for com.badlogic.gdx.scenes.scene2d.ui TextField getCursorPosition.

Prototype

public int getCursorPosition() 

Source Link

Usage

From source file:com.ray3k.skincomposer.Spinner.java

License:Open Source License

private void addWidgets() {
    buttonMinus = new Button(style.buttonMinusStyle);
    buttonPlus = new Button(style.buttonPlusStyle);
    textField = new TextField("", style.textFieldStyle) {
        @Override//from  w  ww  .j  av  a2s .  c om
        public void next(boolean up) {
            if (up) {
                if (transversalPrevious != null) {
                    getStage().setKeyboardFocus(transversalPrevious);
                    if (transversalPrevious instanceof TextField) {
                        ((TextField) transversalPrevious).selectAll();
                    }
                } else {
                    super.next(up);
                }
            } else {
                if (transversalNext != null) {
                    getStage().setKeyboardFocus(transversalNext);
                    if (transversalNext instanceof TextField) {
                        ((TextField) transversalNext).selectAll();
                    }
                } else {
                    super.next(up);
                }
            }
        }

    };

    textField.setAlignment(Align.center);

    textField.addListener(new InputListener() {
        @Override
        public boolean keyDown(InputEvent event, int keycode) {
            if (keycode == Keys.UP) {
                addValue();
                fire(new ChangeListener.ChangeEvent());
            } else if (keycode == Keys.DOWN) {
                subtractValue();
                fire(new ChangeListener.ChangeEvent());
            }
            return false;
        }
    });

    textField.setTextFieldFilter((TextField textField1, char c) -> {
        boolean returnValue = false;
        if ((c >= 48 && c <= 57) || c == 45 || (!rounding && c == 46)) {
            String text = textField1.getText();
            if (textField1.getCursorPosition() <= text.length()) {
                text = text.substring(0, textField1.getCursorPosition());
                text += c + textField1.getText().substring(textField1.getCursorPosition());
            }
            if (text.matches("-?\\d*\\.?\\d*")) {
                returnValue = true;
            }
        }
        return returnValue;
    });
    updateText();

    if (null != orientation)
        switch (orientation) {
        case HORIZONTAL:
            add(buttonMinus);
            add(textField).prefWidth(35.0f).minWidth(35.0f).growX();
            add(buttonPlus);
            break;
        case HORIZONTAL_FLIPPED:
            add(buttonPlus);
            add(textField).prefWidth(35.0f).minWidth(35.0f).growX();
            add(buttonMinus);
            break;
        case VERTICAL:
            add(buttonPlus);
            row();
            add(textField).prefWidth(35.0f).minWidth(35.0f).growX();
            row();
            add(buttonMinus);
            break;
        case VERTICAL_FLIPPED:
            add(buttonMinus);
            row();
            add(textField).prefWidth(35.0f).minWidth(35.0f).growX();
            row();
            add(buttonPlus);
            break;
        case RIGHT_STACK: {
            add(textField).prefWidth(35.0f).minWidth(35.0f).growX();

            VerticalGroup group = new VerticalGroup();
            add(group);

            group.addActor(buttonPlus);
            group.addActor(buttonMinus);
            break;
        }
        case LEFT_STACK: {
            VerticalGroup group = new VerticalGroup();
            add(group);

            group.addActor(buttonPlus);
            group.addActor(buttonMinus);

            add(textField).prefWidth(35.0f).minWidth(35.0f).growX();
            break;
        }
        default:
            break;
        }

    buttonMinus.addListener(new ChangeListener() {
        @Override
        public void changed(ChangeListener.ChangeEvent event, Actor actor) {
            subtractValue();
        }
    });

    buttonPlus.addListener(new ChangeListener() {
        @Override
        public void changed(ChangeListener.ChangeEvent event, Actor actor) {
            addValue();
        }
    });

    textField.addListener(new ChangeListener() {
        @Override
        public void changed(ChangeListener.ChangeEvent event, Actor actor) {
            Spinner parent = (Spinner) actor;
            String text = textField.getText();

            if (text.matches("-?\\d+\\.?\\d*")) {
                double value = Double.parseDouble(text);
                parent.value = BigDecimal.valueOf(value);
            } else {
                parent.value = BigDecimal.valueOf(0);
            }
        }
    });

    textField.addListener(new FocusListener() {
        @Override
        public void keyboardFocusChanged(FocusListener.FocusEvent event, Actor actor, boolean focused) {
            Spinner parent = (Spinner) textField.getParent();
            if (usingMinimum && parent.value.doubleValue() < parent.minimum) {
                parent.value = BigDecimal.valueOf(parent.minimum);
            }
            if (usingMaximum && parent.value.doubleValue() > parent.maximum) {
                parent.value = BigDecimal.valueOf(parent.maximum);
            }
            parent.updateText();
        }

    });

    final Spinner spinner = this;
    addCaptureListener(new ChangeListener() {
        @Override
        public void changed(ChangeListener.ChangeEvent event, Actor actor) {
            event.setTarget(spinner);
        }
    });
}

From source file:se.theodor.quiz.TeamsEditingPanel.java

License:Apache License

private static void removeLetters(TextField tf) {
    int cursorPosition = tf.getCursorPosition();
    String input = tf.getText();//ww  w  .j  a v a2s  .c  o  m
    char[] allowed = "0123456789".toCharArray();
    char[] charArray = input.toString().toCharArray();
    StringBuilder result = new StringBuilder();
    for (char c : charArray) {
        for (char a : allowed) {
            if (c == a) {
                result.append(a);
            }
        }
    }
    tf.setText(result.toString());
    tf.setCursorPosition(cursorPosition);
}

From source file:se.theodor.quiz.TeamsPanel.java

License:Apache License

private void removeLetters(TextField tf) {
    int cursorPosition = tf.getCursorPosition();
    String input = tf.getText();/*  ww w  .  ja  va  2  s.c  o m*/
    char[] allowed = "0123456789".toCharArray();
    char[] charArray = input.toString().toCharArray();
    StringBuilder result = new StringBuilder();
    for (char c : charArray) {
        for (char a : allowed) {
            if (c == a) {
                result.append(a);
            }
        }
    }
    tf.setText(result.toString());
    tf.setCursorPosition(cursorPosition);
}