Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.risenphoenix.jnk.StarField.Popups.Elements;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.event.KeyEvent;
- import java.util.ArrayList;
- import net.risenphoenix.jnk.StarField.INIT;
- import net.risenphoenix.jnk.StarField.Popups.Elements.Handler;
- import net.risenphoenix.jnk.StarField.Popups.Popup;
- public class TextBox implements SFPopupElement{
- int width;
- int height;
- int xAnchor = -1;
- int yAnchor = -1;
- int drawX;
- int drawY;
- FontMetrics fm;
- Popup container = null;
- Handler inputHandler;
- private boolean[] buttons = new boolean[76]; // 76 Characters mapped to console
- private ArrayList<String> bodyLines = new ArrayList<String>();
- private StringBuilder sb = new StringBuilder();
- private final int cursorDelay = 200;
- int blinkTicks = 1;
- boolean showCursor = false;
- private final int MARGIN = 10;
- public TextBox(int width, int height, int xAnchor, int yAnchor, Popup container, Handler handler) {
- this.width = width;
- this.height = height;
- this.xAnchor = xAnchor;
- this.yAnchor = yAnchor;
- this.container = container;
- this.inputHandler = handler;
- }
- @Override
- public void drawElement(Graphics g) {
- /* Element Anchors
- * ==============================================================================
- * The textbox element uses it's anchors in three separate ways. The first way
- * in which they are used is in a manner which does not use them at all. If the
- * anchors are given a value which is less than or equal to zero, then the
- * textbox element will use null-reference positioning, and place itself center-
- * screen.
- *
- * If the anchors are given positive values and a valid container is passed
- * to the constructor, then the textbox element will take the X and Y position
- * of the container, and add the values of the anchors to those positions,
- * thus basing it's XY position off of the container.
- * EX: (If Container XY is (300, 250), and xAnchor is 50, and yAnchor is 125,
- * then the XY position of the text box will be (350, 375).)
- *
- * If the anchors are given positive values and the container is specified as
- * null, then the textbox element will use absolute positioning, and use the
- * X and Y anchors as coordinates for it's top left corner.
- * ==============================================================================
- */
- // Setting X-Anchor
- if (this.xAnchor < 0) {
- this.drawX = (INIT.getFrameSize().width / 2) - (this.width / 2);
- } else if (this.container != null){
- this.drawX = container.getXPos() + xAnchor;
- } else {
- this.drawX = xAnchor;
- }
- // Setting Y-Anchor
- if (this.yAnchor < 0) {
- this.drawY = (INIT.getFrameSize().height / 2) - (this.height / 2) - 20;
- } else if (container != null){
- this.drawY = container.getYPos() + yAnchor;
- } else {
- this.drawY = yAnchor;
- }
- // Setting off-white color of textbox
- g.setColor(new Color(255,255,255,128));
- g.fillRect(drawX, drawY, this.width, this.height);
- // Drawing textbox outline
- g.setColor(Color.BLACK);
- g.drawRect(drawX, drawY, this.width, this.height);
- // Setting Font
- g.setFont(new Font("Century Gothic", Font.PLAIN, 14));
- // Acquire FontMetrics
- this.fm = g.getFontMetrics();
- int yPos = drawY + 20;
- // Draw text to textbox
- for(String s:bodyLines) {
- g.drawString(s, (INIT.getFrameSize().width / 2) - ((width / 2) - MARGIN), yPos);
- yPos += 20;
- }
- // Text Cursor Logic
- if (blinkTicks < 1000) {
- blinkTicks++;
- } else {
- blinkTicks = 1;
- }
- if (blinkTicks % cursorDelay == 0) {
- if (showCursor) {
- showCursor = false;
- } else {
- showCursor = true;
- }
- }
- // Draw Text Cursor to screen
- if (showCursor) {
- int xPosition;
- if (sb.toString().length() > 0) {
- xPosition = fm.stringWidth(bodyLines.get(bodyLines.size() - 1)) + drawX + MARGIN;
- } else {
- xPosition = drawX + MARGIN;
- }
- g.fillRect(xPosition, yPos -32, 2, 14);
- }
- }
- public void keyHandler(KeyEvent e, boolean pressed) {
- // Obtain Unique Key Code Identifier
- int code = getKeyCode(e.getKeyChar());
- // If text has reached the size constraints of the textbox, then do not append input, else append input to textbox body
- if ((bodyLines.size() >= ((this.height - MARGIN) / 20)) && (fm.stringWidth(bodyLines.get(bodyLines.size() - 1)) >= (this.width - (MARGIN * 2) - 20))) {
- // Do Nothing
- } else {
- if (code != -1 && pressed && !buttons[code]) {
- sb.append(e.getKeyChar());
- buttons[code] = pressed;
- } else if (code != -1 && !pressed && buttons[code]) {
- buttons[code] = pressed;
- }
- }
- // If the delete key is pressed, then erase all text in the textbox
- if (e.getKeyCode() == KeyEvent.VK_DELETE) {
- this.sb = new StringBuilder();
- }
- // If backspace is pressed... (should be rather obvious :P)
- if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE && pressed && !buttons[72]) {
- if (sb.toString().length() > 0) {
- this.sb.deleteCharAt(sb.toString().length() - 1);
- }
- buttons[72] = pressed;
- } else if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE && !pressed && buttons[72]) {
- buttons[72] = pressed;
- }
- // If Escape is pressed, perform the function defined in the handler specified by the constructor.
- if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
- sb = new StringBuilder();
- inputHandler.escape();
- return;
- }
- // If Enter is pressed, perform the function defined in the handler specified by the constructor.
- if (e.getKeyCode() == KeyEvent.VK_ENTER) {
- if (sb.toString().length() > 0) {
- inputHandler.enter(sb.toString());
- sb = new StringBuilder();
- return;
- }
- }
- wrapInput(this.fm);
- }
- // Wraps Text for text box display
- public void wrapInput(FontMetrics fm) {
- bodyLines.clear();
- StringBuilder line = new StringBuilder();
- for(int i = 0; i < sb.toString().length(); i++) {
- if (fm.stringWidth(line.toString() + sb.toString().charAt(i)) < (this.width - (MARGIN * 2))) {
- line.append(sb.charAt(i));
- } else {
- line.append(sb.charAt(i));
- StringBuilder toCarry = new StringBuilder();
- for (int position = line.toString().length() - 1; position > 0; position -= 1) {
- if (line.toString().charAt(position) != ' ') {
- toCarry.append(line.toString().charAt(position));
- line.deleteCharAt(position);
- } else {
- toCarry.reverse();
- bodyLines.add(line.toString());
- line = new StringBuilder();
- line.append(toCarry.toString());
- break;
- }
- }
- }
- }
- bodyLines.add(line.toString());
- }
- // Key Registry for textbox
- public int getKeyCode(char character) {
- int button = -1;
- // Lower-Case
- if (character == 'a') button = 0;
- if (character == 'b') button = 1;
- if (character == 'c') button = 2;
- if (character == 'd') button = 3;
- if (character == 'e') button = 4;
- if (character == 'f') button = 5;
- if (character == 'g') button = 6;
- if (character == 'h') button = 7;
- if (character == 'i') button = 8;
- if (character == 'j') button = 9;
- if (character == 'k') button = 10;
- if (character == 'l') button = 11;
- if (character == 'm') button = 12;
- if (character == 'n') button = 13;
- if (character == 'o') button = 14;
- if (character == 'p') button = 15;
- if (character == 'q') button = 16;
- if (character == 'r') button = 17;
- if (character == 's') button = 18;
- if (character == 't') button = 19;
- if (character == 'u') button = 20;
- if (character == 'v') button = 21;
- if (character == 'w') button = 22;
- if (character == 'x') button = 23;
- if (character == 'y') button = 24;
- if (character == 'z') button = 25;
- // Upper-Case
- if (character == 'A') button = 26;
- if (character == 'B') button = 27;
- if (character == 'C') button = 28;
- if (character == 'D') button = 29;
- if (character == 'E') button = 30;
- if (character == 'F') button = 31;
- if (character == 'G') button = 32;
- if (character == 'H') button = 33;
- if (character == 'I') button = 34;
- if (character == 'J') button = 35;
- if (character == 'K') button = 36;
- if (character == 'L') button = 37;
- if (character == 'M') button = 38;
- if (character == 'N') button = 39;
- if (character == 'O') button = 40;
- if (character == 'P') button = 41;
- if (character == 'Q') button = 42;
- if (character == 'R') button = 43;
- if (character == 'S') button = 44;
- if (character == 'T') button = 45;
- if (character == 'U') button = 45;
- if (character == 'V') button = 47;
- if (character == 'W') button = 48;
- if (character == 'X') button = 49;
- if (character == 'Y') button = 50;
- if (character == 'Z') button = 51;
- // Numeric Set
- if (character == '0') button = 51;
- if (character == '1') button = 52;
- if (character == '2') button = 53;
- if (character == '3') button = 54;
- if (character == '4') button = 55;
- if (character == '5') button = 56;
- if (character == '6') button = 57;
- if (character == '7') button = 58;
- if (character == '8') button = 59;
- if (character == '9') button = 60;
- // Special Set
- if (character == ':') button = 61;
- if (character == '=') button = 62;
- if (character == '_') button = 63;
- if (character == '-') button = 64;
- if (character == '.') button = 65;
- if (character == ',') button = 66;
- if (character == '?') button = 67;
- if (character == '/') button = 68;
- if (character == ' ') button = 69;
- if (character == '+') button = 70;
- if (character == '!') button = 71;
- /* Value 72 assigned to Backspace Key*/
- // Special Set 2
- if (character == '(') button = 73;
- if (character == ')') button = 74;
- if (character == '\'') button = 75;
- return button;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement