[9b6a069] | 1 | package gamegui;
|
---|
| 2 |
|
---|
| 3 | import java.awt.*;
|
---|
| 4 | import java.awt.event.*;
|
---|
| 5 | import java.util.*;
|
---|
| 6 |
|
---|
| 7 | public class MultiTextbox extends Textbox {
|
---|
| 8 | ArrayList<String> lstStrings;
|
---|
| 9 | FontMetrics metrics;
|
---|
| 10 | boolean active;
|
---|
| 11 |
|
---|
| 12 | public MultiTextbox(String newName, int newX, int newY, int newWidth, int newHeight, String newLabel, boolean isActive, Font newFont, FontMetrics newMetrics) {
|
---|
| 13 | super(newName, newX, newY, newWidth, newHeight, newLabel, newFont, false);
|
---|
| 14 |
|
---|
| 15 | lstStrings = new ArrayList<String>();
|
---|
| 16 | metrics = newMetrics;
|
---|
| 17 | active = isActive;
|
---|
| 18 |
|
---|
| 19 | splitString();
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | public void append(String str) {
|
---|
| 23 | if(getText().equals(""))
|
---|
| 24 | setText(str);
|
---|
| 25 | else
|
---|
| 26 | setText(getText() + "\n" + str);
|
---|
| 27 |
|
---|
| 28 | splitString();
|
---|
| 29 |
|
---|
| 30 | if(lstStrings.size()*15+6 > getHeight())
|
---|
| 31 | getScrollBar().setSize(getScrollBar().getMaxSize()*getHeight()/(lstStrings.size()*15+6));
|
---|
| 32 | else
|
---|
| 33 | getScrollBar().setSize(getScrollBar().getMaxSize());
|
---|
| 34 |
|
---|
| 35 | getScrollBar().setPosition(getScrollBar().getMaxSize()-getScrollBar().getSize());
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | public void setText(String s) {
|
---|
| 39 | super.setText(s);
|
---|
| 40 |
|
---|
| 41 | splitString();
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | public void clear() {
|
---|
| 45 | super.clear();
|
---|
| 46 | lstStrings = new ArrayList<String>();
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | public boolean handleEvent(MouseEvent e) {
|
---|
| 50 | if(!getScrollBar().handleEvent(e))
|
---|
| 51 | return false;
|
---|
| 52 |
|
---|
| 53 | if(e.getY() < getY()+getWidth()) {
|
---|
| 54 | changeTextStart(-30);
|
---|
| 55 | }else if(getY()+getHeight()-getWidth() < e.getY()) {
|
---|
| 56 | changeTextStart(30);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | return true;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | public void handleEvent(KeyEvent e) {
|
---|
| 63 | if(!active)
|
---|
| 64 | return;
|
---|
| 65 |
|
---|
| 66 | super.handleEvent(e);
|
---|
| 67 |
|
---|
| 68 | splitString();
|
---|
| 69 |
|
---|
| 70 | if(lstStrings.size()*15+6 > getHeight())
|
---|
| 71 | getScrollBar().setSize(getScrollBar().getMaxSize()*getHeight()/(lstStrings.size()*15+6));
|
---|
| 72 | else
|
---|
| 73 | getScrollBar().setSize(getScrollBar().getMaxSize());
|
---|
| 74 |
|
---|
| 75 | getScrollBar().setPosition(getScrollBar().getMaxSize()-getScrollBar().getSize());
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | private void changeTextStart(int increment) {
|
---|
| 79 | setTextStart(getTextStart()+increment);
|
---|
| 80 |
|
---|
| 81 | if(lstStrings.size()*15+6>getHeight() && getTextStart() >= lstStrings.size()*15+6-getHeight()) {
|
---|
| 82 | setTextStart(lstStrings.size()*15+6-getHeight());
|
---|
| 83 | getScrollBar().setPosition(getScrollBar().getMaxSize()-getScrollBar().getSize());
|
---|
| 84 | }else if(getTextStart() < 0 || lstStrings.size()*15+6<=getHeight()) {
|
---|
| 85 | setTextStart(0);
|
---|
| 86 | getScrollBar().setPosition(0);
|
---|
| 87 | }else
|
---|
| 88 | getScrollBar().setPosition(getTextStart()*getScrollBar().getMaxSize()/(lstStrings.size()*15+6));
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | private void splitString() {
|
---|
| 92 | String drawnString = getText();
|
---|
| 93 |
|
---|
| 94 | ArrayList<String> lstTemp = new ArrayList<String>();
|
---|
| 95 | do {
|
---|
| 96 | int x = 0, xReal, lastSpace = -1;
|
---|
| 97 | while(x<drawnString.length() && metrics.stringWidth(drawnString.substring(0, x+1))<=getWidth()-10 && !drawnString.substring(x, x+1).equals("\n")) {
|
---|
| 98 | if(drawnString.charAt(x) == ' ')
|
---|
| 99 | lastSpace = x;
|
---|
| 100 | x++;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | xReal = x;
|
---|
| 104 |
|
---|
| 105 | if(lastSpace > 0 && drawnString.length()>x)
|
---|
| 106 | x = lastSpace+1;
|
---|
| 107 |
|
---|
| 108 | if(drawnString.length()>xReal && drawnString.substring(xReal, xReal+1).equals("\n")) {
|
---|
| 109 | lstTemp.add(drawnString.substring(0, xReal));
|
---|
| 110 | drawnString = drawnString.substring(xReal+1);
|
---|
| 111 | }else {
|
---|
| 112 | lstTemp.add(drawnString.substring(0, x));
|
---|
| 113 | drawnString = drawnString.substring(x);
|
---|
| 114 | }
|
---|
| 115 | }while(metrics.stringWidth(drawnString)>0);
|
---|
| 116 |
|
---|
| 117 | if(lstTemp.size()*15-getHeight()+6 > 0)
|
---|
| 118 | setTextStart(lstTemp.size()*15-getHeight()+6);
|
---|
| 119 | else
|
---|
| 120 | setTextStart(0);
|
---|
| 121 |
|
---|
| 122 | lstStrings = lstTemp;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | public void draw(Graphics g) {
|
---|
| 126 | if(isSelected() && System.currentTimeMillis() - getLastCursorChange() > getBlinkInterval())
|
---|
| 127 | {
|
---|
| 128 | if(getCursorState() == 0)
|
---|
| 129 | setCursorState(1);
|
---|
| 130 | else
|
---|
| 131 | setCursorState(0);
|
---|
| 132 |
|
---|
| 133 | setLastCursorChange(System.currentTimeMillis());
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | srcGraphics.setColor(Color.green);
|
---|
| 137 | srcGraphics.setFont(getFont());
|
---|
| 138 |
|
---|
| 139 | int x;
|
---|
| 140 | for(x=0; x<lstStrings.size(); x++)
|
---|
| 141 | srcGraphics.drawString(lstStrings.get(x), 5, metrics.getHeight()+x*15-getTextStart());
|
---|
| 142 |
|
---|
| 143 | x--;
|
---|
| 144 | if(isSelected() && getCursorState() == 1)
|
---|
| 145 | srcGraphics.drawLine(metrics.stringWidth(lstStrings.get(x))+6, 5+x*15-getTextStart(), metrics.stringWidth(lstStrings.get(x))+6, metrics.getHeight()+x*15-getTextStart());
|
---|
| 146 |
|
---|
| 147 | g.setColor(Color.green);
|
---|
| 148 | g.setFont(getFont());
|
---|
| 149 |
|
---|
| 150 | g.drawImage(source, getX(), getY(), null);
|
---|
| 151 | g.drawString(getLabel(), getX() - metrics.stringWidth(getLabel()) - 10, getY() + (getHeight() + metrics.getHeight())/2 - 2);
|
---|
| 152 |
|
---|
| 153 | g.setColor(Color.red);
|
---|
| 154 | if(!noBorder)
|
---|
| 155 | g.drawRect(getX(), getY(), getWidth(), getHeight());
|
---|
| 156 |
|
---|
| 157 | if(getScrollBar() != null)
|
---|
| 158 | getScrollBar().draw(g);
|
---|
| 159 | }
|
---|
| 160 | }
|
---|