package com.lucasdnd.ags.ui; import java.awt.Toolkit; import java.awt.event.KeyEvent; import org.newdawn.slick.Color; import org.newdawn.slick.GameContainer; import org.newdawn.slick.Graphics; import org.newdawn.slick.Input; import org.newdawn.slick.SlickException; import org.newdawn.slick.state.StateBasedGame; import com.lucasdnd.ags.system.ResourceLoader; public class TextInput extends TextButton { boolean hasFocus; boolean isShiftDown; String label; String text; int maxTextLength; float rectangleMarginX = 140f; float rectangleMarginY = -7f; float innerTextPaddingX = 4f; String cursor = ""; int cursorBlinkTime = 500; int currentCursorBlinkTime = 0; public TextInput(String label, int maxTextLength, float xPos, float yPos, float width, float height) throws SlickException { super(label, xPos, yPos, width, height); this.maxTextLength = maxTextLength; this.width = width; this.height = height; this.label = label; text = ""; isShiftDown = false; } public void update(GameContainer container, StateBasedGame game, int delta, boolean leftMouseClicked, boolean leftMouseDown, int mouseX, int mouseY, Input input) throws SlickException { if (isEnabled) { super.update(container, game, delta, leftMouseClicked, leftMouseDown, mouseX, mouseY); // Cursor blink currentCursorBlinkTime += delta; if (currentCursorBlinkTime >= cursorBlinkTime) { currentCursorBlinkTime = 0; if (cursor.compareTo("|") == 0) { cursor = ""; } else { cursor = "|"; } } // Get keys input String letter = ""; if (input.isKeyPressed(Input.KEY_A)) { letter = "a"; } else if (input.isKeyPressed(Input.KEY_B)) { letter = "b"; } else if (input.isKeyPressed(Input.KEY_C)) { letter = "c"; } else if (input.isKeyPressed(Input.KEY_D)) { letter = "d"; } else if (input.isKeyPressed(Input.KEY_E)) { letter = "e"; } else if (input.isKeyPressed(Input.KEY_F)) { letter = "f"; } else if (input.isKeyPressed(Input.KEY_G)) { letter = "g"; } else if (input.isKeyPressed(Input.KEY_H)) { letter = "h"; } else if (input.isKeyPressed(Input.KEY_I)) { letter = "i"; } else if (input.isKeyPressed(Input.KEY_J)) { letter = "j"; } else if (input.isKeyPressed(Input.KEY_K)) { letter = "k"; } else if (input.isKeyPressed(Input.KEY_L)) { letter = "l"; } else if (input.isKeyPressed(Input.KEY_M)) { letter = "m"; } else if (input.isKeyPressed(Input.KEY_N)) { letter = "n"; } else if (input.isKeyPressed(Input.KEY_O)) { letter = "o"; } else if (input.isKeyPressed(Input.KEY_P)) { letter = "p"; } else if (input.isKeyPressed(Input.KEY_Q)) { letter = "q"; } else if (input.isKeyPressed(Input.KEY_R)) { letter = "r"; } else if (input.isKeyPressed(Input.KEY_S)) { letter = "s"; } else if (input.isKeyPressed(Input.KEY_T)) { letter = "t"; } else if (input.isKeyPressed(Input.KEY_U)) { letter = "u"; } else if (input.isKeyPressed(Input.KEY_V)) { letter = "v"; } else if (input.isKeyPressed(Input.KEY_W)) { letter = "w"; } else if (input.isKeyPressed(Input.KEY_X)) { letter = "x"; } else if (input.isKeyPressed(Input.KEY_Y)) { letter = "y"; } else if (input.isKeyPressed(Input.KEY_Z)) { letter = "z"; } else if (input.isKeyPressed(Input.KEY_0)) { letter = "0"; } else if (input.isKeyPressed(Input.KEY_1)) { letter = "1"; } else if (input.isKeyPressed(Input.KEY_2)) { letter = "2"; } else if (input.isKeyPressed(Input.KEY_3)) { letter = "3"; } else if (input.isKeyPressed(Input.KEY_4)) { letter = "4"; } else if (input.isKeyPressed(Input.KEY_5)) { letter = "5"; } else if (input.isKeyPressed(Input.KEY_6)) { letter = "6"; } else if (input.isKeyPressed(Input.KEY_7)) { letter = "7"; } else if (input.isKeyPressed(Input.KEY_8)) { letter = "8"; } else if (input.isKeyPressed(Input.KEY_9)) { letter = "9"; } else if (input.isKeyPressed(Input.KEY_SPACE)) { letter = " "; } else if (input.isKeyPressed(Input.KEY_BACK)) { cursor = "|"; currentCursorBlinkTime = 0; if (text.length() > 0) { text = text.substring(0, text.length() - 1); } } // Append the letter to the String if (letter.compareTo("") != 0 && text.length() < maxTextLength) { cursor = "|"; currentCursorBlinkTime = 0; boolean isCapsOn = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK); if (input.isKeyDown(Input.KEY_LSHIFT) || isCapsOn) { letter = letter.toUpperCase(); } text += letter; } } } @Override public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException { if (visible) { g.setLineWidth(2f); g.setColor(Color.black); // Draw label ResourceLoader.getInstance().tinyLightGrayFont.drawString(xPos + 1f, yPos + 1f, label); ResourceLoader.getInstance().tinyBlackFont.drawString(xPos, yPos, label); g.drawRoundRect(xPos + rectangleMarginX, yPos + rectangleMarginY, width, height, 2); // Draw inner text ResourceLoader.getInstance().tinyLightGrayFont.drawString(xPos + rectangleMarginX + innerTextPaddingX + 1f, yPos + 1f, text + cursor); ResourceLoader.getInstance().tinyBlackFont.drawString(xPos + rectangleMarginX + innerTextPaddingX, yPos, text + cursor); } } public boolean isHasFocus() { return hasFocus; } public void setHasFocus(boolean hasFocus) { this.hasFocus = hasFocus; } public String getLabel() { return label; } public void setLabel(String label) { this.label = label; } public String getText() { return text; } public void setText(String text) { this.text = text; } }