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