InputBox class as promised

This is the InputBox class I was talking about @t0neg0d.

It might not be as fully featured as you want, so feel free to do it the way you want.

[java]
public abstract class InputBox extends Window {

private Form form;
private ButtonAdapter btnOk;
private ButtonAdapter btnCancel;
protected TextField textInput;

/**
 * Creates a new instance of the InputBox control
 *
 * @param screen The screen control the Element is to be added to
 * @param position A Vector2f containing the x/y position of the Element
 */
public InputBox(ElementManager screen, Vector2f position) {
    this(screen, UIDUtil.getUID(), position,
            screen.getStyle("Window").getVector2f("defaultSize"),
            screen.getStyle("Window").getVector4f("resizeBorders"),
            screen.getStyle("Window").getString("defaultImg"));
}

/**
 * Creates a new instance of the InputBox control
 *
 * @param screen The screen control the Element is to be added to
 * @param position A Vector2f containing the x/y position of the Element
 * @param dimensions A Vector2f containing the width/height dimensions
 * of the Element
 */
public InputBox(ElementManager screen, Vector2f position, Vector2f dimensions) {
    this(screen, UIDUtil.getUID(), position, dimensions,
            screen.getStyle("Window").getVector4f("resizeBorders"),
            screen.getStyle("Window").getString("defaultImg"));
}

/**
 * Creates a new instance of the InputBox control
 *
 * @param screen The screen control the Element is to be added to
 * @param position A Vector2f containing the x/y position of the Element
 * @param dimensions A Vector2f containing the width/height dimensions
 * of the Element
 * @param resizeBorders A Vector4f containg the border information used
 * when resizing the default image (x = N, y = W, z = E, w = S)
 * @param defaultImg The default image to use for the InputBox window
 */
public InputBox(ElementManager screen, Vector2f position, Vector2f dimensions, Vector4f resizeBorders, String defaultImg) {
    this(screen, UIDUtil.getUID(), position, dimensions, resizeBorders, defaultImg);
}

/**
 * Creates a new instance of the InputBox control
 *
 * @param screen The screen control the Element is to be added to
 * @param UID A unique String identifier for the Element
 * @param position A Vector2f containing the x/y position of the Element
 */
public InputBox(ElementManager screen, String UID, Vector2f position) {
    this(screen, UID, position,
            screen.getStyle("Window").getVector2f("defaultSize"),
            screen.getStyle("Window").getVector4f("resizeBorders"),
            screen.getStyle("Window").getString("defaultImg"));
}

/**
 * Creates a new instance of the InputBox control
 *
 * @param screen The screen control the Element is to be added to
 * @param UID A unique String identifier for the Element
 * @param position A Vector2f containing the x/y position of the Element
 * @param dimensions A Vector2f containing the width/height dimensions
 * of the Element
 */
public InputBox(ElementManager screen, String UID, Vector2f position, Vector2f dimensions) {
    this(screen, UID, position, dimensions,
            screen.getStyle("Window").getVector4f("resizeBorders"),
            screen.getStyle("Window").getString("defaultImg"));
}

/**
 * Creates a new instance of the InputBox control
 *
 * @param screen The screen control the Element is to be added to
 * @param UID A unique String identifier for the Element
 * @param position A Vector2f containing the x/y position of the Element
 * @param dimensions A Vector2f containing the width/height dimensions
 * of the Element
 * @param resizeBorders A Vector4f containg the border information used
 * when resizing the default image (x = N, y = W, z = E, w = S)
 * @param defaultImg The default image to use for the InputBox window
 */
public InputBox(ElementManager screen, String UID, Vector2f position, Vector2f dimensions, Vector4f resizeBorders, String defaultImg) {
    super(screen, UID, position, dimensions, resizeBorders, defaultImg);

    form = new Form(screen);
    Vector4f indents = screen.getStyle("Window").getVector4f("contentIndents");

    btnOk = new ButtonAdapter(screen, UID + ":btnOk",
            new Vector2f(
            getWidth() - screen.getStyle("Button").getVector2f("defaultSize").x - indents.x,
            getHeight() - screen.getStyle("Button").getVector2f("defaultSize").y - indents.y)) {

        @Override
        public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
            onButtonOkPressed(evt, toggled);
        }
    };

    btnOk.setText("Ok");
    btnOk.setDockS(true);
    btnOk.setDockE(true);
    addChild(btnOk);
    form.addFormElement(btnOk);

    btnCancel = new ButtonAdapter(screen, UID + ":btnCancel",
            new Vector2f(
            indents.x,
            getHeight() - screen.getStyle("Button").getVector2f("defaultSize").y - indents.w)) {

        @Override
        public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
            onButtonCancelPressed(evt, toggled);
        }
    };

    btnCancel.setText("Cancel");
    btnCancel.setDockS(true);
    btnCancel.setDockW(true);
    addChild(btnCancel);
    form.addFormElement(btnCancel);

    textInput = new TextField(screen, UID + ":txtField",
            new Vector2f(indents.x, getDragBarHeight() + indents.y),
            new Vector2f(getWidth() - 25, screen.getStyle("TextField").getVector2f("defaultSize").y));
    textInput.setDockS(true);
    textInput.setDockW(true);
    addChild(textInput);
    form.addFormElement(textInput);
}

public void onButtonCancelPressed(MouseButtonEvent evt, boolean toggled) {
    this.hide();
}

public void onButtonOkPressed(MouseButtonEvent evt, boolean toggled) {
}

@Override
public void setText(String text) {
    setInputDefaultText(text);
}

public void setInputDefaultText(String text) {
    textInput.setText(text);
}

public String getInputText() {
    return textInput.getText();
}

public abstract boolean isInputValid();

}
[/java]

1 Like

Nothing to see here now.

@madjack offtopic - just put /edit in the url to edit the first post.

1 Like

Thanks @jayfella

@madjack This is going to be added to the library and will be located in the same package with Alert and Dialog. I may extend Dialog and reposition the input IF a message is added as well. Just options in case someone needs it.

1 Like