Advertisement
Jnk1296

Text Box

Aug 1st, 2013
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.76 KB | None | 0 0
  1. package net.risenphoenix.jnk.StarField.Popups.Elements;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.FontMetrics;
  6. import java.awt.Graphics;
  7. import java.awt.event.KeyEvent;
  8. import java.util.ArrayList;
  9.  
  10. import net.risenphoenix.jnk.StarField.INIT;
  11. import net.risenphoenix.jnk.StarField.Popups.Elements.Handler;
  12. import net.risenphoenix.jnk.StarField.Popups.Popup;
  13.  
  14. public class TextBox implements SFPopupElement{
  15.    
  16.     int width;
  17.     int height;
  18.    
  19.     int xAnchor = -1;
  20.     int yAnchor = -1;
  21.    
  22.     int drawX;
  23.     int drawY;
  24.    
  25.     FontMetrics fm;
  26.     Popup container = null;
  27.    
  28.     Handler inputHandler;
  29.    
  30.     private boolean[] buttons = new boolean[76]; // 76 Characters mapped to console
  31.    
  32.     private ArrayList<String> bodyLines = new ArrayList<String>();
  33.    
  34.     private StringBuilder sb = new StringBuilder();
  35.    
  36.     private final int cursorDelay = 200;
  37.     int blinkTicks = 1;
  38.     boolean showCursor = false;
  39.    
  40.     private final int MARGIN = 10;
  41.    
  42.     public TextBox(int width, int height, int xAnchor, int yAnchor, Popup container, Handler handler) {
  43.         this.width = width;
  44.         this.height = height;
  45.         this.xAnchor = xAnchor;
  46.         this.yAnchor = yAnchor;
  47.        
  48.         this.container = container;
  49.        
  50.         this.inputHandler = handler;
  51.     }
  52.    
  53.     @Override
  54.     public void drawElement(Graphics g) {
  55.         /* Element Anchors
  56.          * ==============================================================================
  57.          * The textbox element uses it's anchors in three separate ways. The first way
  58.          * in which they are used is in a manner which does not use them at all. If the
  59.          * anchors are given a value which is less than or equal to zero, then the
  60.          * textbox element will use null-reference positioning, and place itself center-
  61.          * screen.
  62.          *
  63.          * If the anchors are given positive values and a valid container is passed
  64.          * to the constructor, then the textbox element will take the X and Y position
  65.          * of the container, and add the values of the anchors to those positions,
  66.          * thus basing it's XY position off of the container.
  67.          * EX: (If Container XY is (300, 250), and xAnchor is 50, and yAnchor is 125,
  68.          *      then the XY position of the text box will be (350, 375).)
  69.          *  
  70.          * If the anchors are given positive values and the container is specified as
  71.          * null, then the textbox element will use absolute positioning, and use the
  72.          * X and Y anchors as coordinates for it's top left corner.
  73.          * ==============================================================================
  74.          */
  75.        
  76.         // Setting X-Anchor
  77.         if (this.xAnchor < 0) {
  78.             this.drawX = (INIT.getFrameSize().width / 2) - (this.width / 2);
  79.         } else if (this.container != null){
  80.             this.drawX = container.getXPos() + xAnchor;
  81.         } else {
  82.             this.drawX = xAnchor;
  83.         }
  84.        
  85.         // Setting Y-Anchor
  86.         if (this.yAnchor < 0) {
  87.             this.drawY = (INIT.getFrameSize().height / 2) - (this.height / 2) - 20;
  88.         } else if (container != null){
  89.             this.drawY = container.getYPos() + yAnchor;
  90.         } else {
  91.             this.drawY = yAnchor;
  92.         }
  93.        
  94.         // Setting off-white color of textbox
  95.         g.setColor(new Color(255,255,255,128));
  96.         g.fillRect(drawX, drawY, this.width, this.height);
  97.        
  98.         // Drawing textbox outline
  99.         g.setColor(Color.BLACK);
  100.         g.drawRect(drawX, drawY, this.width, this.height);
  101.        
  102.         // Setting Font
  103.         g.setFont(new Font("Century Gothic", Font.PLAIN, 14));
  104.        
  105.         // Acquire FontMetrics
  106.         this.fm = g.getFontMetrics();
  107.         int yPos = drawY + 20;
  108.  
  109.         // Draw text to textbox
  110.         for(String s:bodyLines) {
  111.             g.drawString(s, (INIT.getFrameSize().width / 2) - ((width / 2) - MARGIN), yPos);
  112.             yPos += 20;
  113.         }
  114.        
  115.         // Text Cursor Logic
  116.         if (blinkTicks < 1000) {
  117.             blinkTicks++;
  118.         } else {
  119.             blinkTicks = 1;
  120.         }
  121.        
  122.         if (blinkTicks % cursorDelay == 0) {
  123.             if (showCursor) {
  124.                 showCursor = false;
  125.             } else {
  126.                 showCursor = true;
  127.             }
  128.         }
  129.        
  130.         // Draw Text Cursor to screen
  131.         if (showCursor) {
  132.             int xPosition;
  133.            
  134.             if (sb.toString().length() > 0) {
  135.                 xPosition = fm.stringWidth(bodyLines.get(bodyLines.size() - 1)) + drawX + MARGIN;
  136.             } else {
  137.                 xPosition = drawX + MARGIN;
  138.             }
  139.            
  140.             g.fillRect(xPosition, yPos -32, 2, 14);
  141.         }
  142.     }
  143.    
  144.     public void keyHandler(KeyEvent e, boolean pressed) {
  145.         // Obtain Unique Key Code Identifier
  146.         int code = getKeyCode(e.getKeyChar());
  147.        
  148.         // If text has reached the size constraints of the textbox, then do not append input, else append input to textbox body
  149.         if ((bodyLines.size() >= ((this.height - MARGIN) / 20)) && (fm.stringWidth(bodyLines.get(bodyLines.size() - 1)) >= (this.width - (MARGIN * 2) - 20))) {
  150.             // Do Nothing
  151.         } else {
  152.             if (code != -1 && pressed && !buttons[code]) {
  153.                 sb.append(e.getKeyChar());
  154.                 buttons[code] = pressed;
  155.             } else if (code != -1 && !pressed && buttons[code]) {
  156.                 buttons[code] = pressed;
  157.             }
  158.         }
  159.        
  160.         // If the delete key is pressed, then erase all text in the textbox
  161.         if (e.getKeyCode() == KeyEvent.VK_DELETE) {
  162.             this.sb = new StringBuilder();
  163.         }
  164.        
  165.         // If backspace is pressed... (should be rather obvious :P)
  166.         if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE && pressed && !buttons[72]) {
  167.             if (sb.toString().length() > 0) {
  168.                 this.sb.deleteCharAt(sb.toString().length() - 1);
  169.             }
  170.             buttons[72] = pressed;
  171.         } else if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE && !pressed && buttons[72]) {
  172.             buttons[72] = pressed;
  173.         }
  174.        
  175.         // If Escape is pressed, perform the function defined in the handler specified by the constructor.
  176.         if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
  177.             sb = new StringBuilder();
  178.             inputHandler.escape();
  179.             return;
  180.         }
  181.        
  182.         // If Enter is pressed, perform the function defined in the handler specified by the constructor.
  183.         if (e.getKeyCode() == KeyEvent.VK_ENTER) {
  184.             if (sb.toString().length() > 0) {
  185.                 inputHandler.enter(sb.toString());
  186.                 sb = new StringBuilder();
  187.                 return;
  188.             }
  189.         }
  190.        
  191.         wrapInput(this.fm);
  192.     }
  193.    
  194.     // Wraps Text for text box display
  195.     public void wrapInput(FontMetrics fm) {
  196.         bodyLines.clear();
  197.         StringBuilder line = new StringBuilder();
  198.  
  199.         for(int i = 0; i < sb.toString().length(); i++) {
  200.             if (fm.stringWidth(line.toString() + sb.toString().charAt(i)) < (this.width - (MARGIN * 2))) {
  201.                 line.append(sb.charAt(i));
  202.             } else {
  203.                 line.append(sb.charAt(i));
  204.                 StringBuilder toCarry = new StringBuilder();
  205.                 for (int position = line.toString().length() - 1; position > 0; position -= 1) {
  206.                     if (line.toString().charAt(position) != ' ') {
  207.                         toCarry.append(line.toString().charAt(position));
  208.                         line.deleteCharAt(position);
  209.                     } else {
  210.                         toCarry.reverse();
  211.                         bodyLines.add(line.toString());
  212.                         line = new StringBuilder();
  213.                         line.append(toCarry.toString());
  214.                         break;
  215.                     }
  216.                 }
  217.             }
  218.         }
  219.        
  220.         bodyLines.add(line.toString());
  221.     }
  222.    
  223.     // Key Registry for textbox
  224.     public int getKeyCode(char character) {
  225.         int button = -1;
  226.        
  227.         // Lower-Case
  228.         if (character == 'a') button = 0;
  229.         if (character == 'b') button = 1;
  230.         if (character == 'c') button = 2;
  231.         if (character == 'd') button = 3;
  232.         if (character == 'e') button = 4;
  233.         if (character == 'f') button = 5;
  234.         if (character == 'g') button = 6;
  235.         if (character == 'h') button = 7;
  236.         if (character == 'i') button = 8;
  237.         if (character == 'j') button = 9;
  238.         if (character == 'k') button = 10;
  239.         if (character == 'l') button = 11;
  240.         if (character == 'm') button = 12;
  241.         if (character == 'n') button = 13;
  242.         if (character == 'o') button = 14;
  243.         if (character == 'p') button = 15;
  244.         if (character == 'q') button = 16;
  245.         if (character == 'r') button = 17;
  246.         if (character == 's') button = 18;
  247.         if (character == 't') button = 19;
  248.         if (character == 'u') button = 20;
  249.         if (character == 'v') button = 21;
  250.         if (character == 'w') button = 22;
  251.         if (character == 'x') button = 23;
  252.         if (character == 'y') button = 24;
  253.         if (character == 'z') button = 25;
  254.        
  255.         // Upper-Case
  256.         if (character == 'A') button = 26;
  257.         if (character == 'B') button = 27;
  258.         if (character == 'C') button = 28;
  259.         if (character == 'D') button = 29;
  260.         if (character == 'E') button = 30;
  261.         if (character == 'F') button = 31;
  262.         if (character == 'G') button = 32;
  263.         if (character == 'H') button = 33;
  264.         if (character == 'I') button = 34;
  265.         if (character == 'J') button = 35;
  266.         if (character == 'K') button = 36;
  267.         if (character == 'L') button = 37;
  268.         if (character == 'M') button = 38;
  269.         if (character == 'N') button = 39;
  270.         if (character == 'O') button = 40;
  271.         if (character == 'P') button = 41;
  272.         if (character == 'Q') button = 42;
  273.         if (character == 'R') button = 43;
  274.         if (character == 'S') button = 44;
  275.         if (character == 'T') button = 45;
  276.         if (character == 'U') button = 45;
  277.         if (character == 'V') button = 47;
  278.         if (character == 'W') button = 48;
  279.         if (character == 'X') button = 49;
  280.         if (character == 'Y') button = 50;
  281.         if (character == 'Z') button = 51;
  282.        
  283.         // Numeric Set
  284.         if (character == '0') button = 51;
  285.         if (character == '1') button = 52;
  286.         if (character == '2') button = 53;
  287.         if (character == '3') button = 54;
  288.         if (character == '4') button = 55;
  289.         if (character == '5') button = 56;
  290.         if (character == '6') button = 57;
  291.         if (character == '7') button = 58;
  292.         if (character == '8') button = 59;
  293.         if (character == '9') button = 60;
  294.        
  295.         // Special Set
  296.         if (character == ':') button = 61;
  297.         if (character == '=') button = 62;
  298.         if (character == '_') button = 63;
  299.         if (character == '-') button = 64;
  300.         if (character == '.') button = 65;
  301.         if (character == ',') button = 66;
  302.         if (character == '?') button = 67;
  303.         if (character == '/') button = 68;
  304.         if (character == ' ') button = 69;
  305.         if (character == '+') button = 70;
  306.         if (character == '!') button = 71;
  307.        
  308.         /* Value 72 assigned to Backspace Key*/
  309.        
  310.         // Special Set 2
  311.         if (character == '(') button = 73;
  312.         if (character == ')') button = 74;
  313.         if (character == '\'') button = 75;
  314.         return button;
  315.     }
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement