1 4 package demo.sharededitor.models; 5 6 import java.awt.Color ; 7 import java.awt.Font ; 8 import java.awt.Graphics2D ; 9 import java.awt.Image ; 10 import java.awt.Rectangle ; 11 import java.awt.Shape ; 12 import java.awt.font.FontRenderContext ; 13 import java.awt.font.TextAttribute ; 14 import java.awt.font.TextLayout ; 15 import java.awt.geom.AffineTransform ; 16 import java.awt.geom.Rectangle2D ; 17 import java.text.AttributedCharacterIterator ; 18 import java.text.AttributedString ; 19 20 import demo.sharededitor.ui.IFontable; 21 import demo.sharededitor.ui.ITexturable; 22 23 public class Text extends BaseObject implements IFontable, ITexturable 24 { 25 private Shape shape = null; 26 27 protected Shape getShape() 28 { 29 return shape; 30 } 31 32 protected Shape [] getAnchors() 33 { 34 return new Shape [0]; 35 } 36 37 public boolean isAt(int x, int y) 38 { 39 if (shape == null) 40 return false; 41 42 Shape bounds = new Rectangle2D.Double (shape.getBounds().x - 5, 43 shape.getBounds().y - 5, 44 shape.getBounds().width + 5, 45 shape.getBounds().height + 5); 46 return bounds.contains(x, y); 47 } 48 49 public synchronized void move(int dx, int dy) 50 { 51 this.x += dx; 52 this.y += dy; 53 notifyListeners(this); 54 } 55 56 public synchronized void resize(int x, int y) 57 { 58 } 60 61 public synchronized void setTexture(Image image) 62 { 63 super.setTexture(image); 64 notifyListeners(this); 65 } 66 67 private String fontName; 68 69 private int fontSize; 70 71 public synchronized void setFontInfo(String name, int size, String text) 72 { 73 this.text = text; 74 this.fontName = name; 75 this.fontSize = size; 76 notifyListeners(this); 77 } 78 79 public synchronized void setFontName(String name) 80 { 81 this.fontName = name; 82 notifyListeners(this); 83 } 84 85 public synchronized void setFontSize(int size) 86 { 87 this.fontSize = size; 88 notifyListeners(this); 89 } 90 91 private String text; 92 93 public synchronized void setText(String text) 94 { 95 this.text = text; 96 notifyListeners(this); 97 } 98 99 public String getText() 100 { 101 return this.text; 102 } 103 104 public synchronized void appendToText(char c) 105 { 106 if (!this.isInverted) 107 this.text += c; 108 else 109 { 110 this.text = c + ""; 111 this.isInverted = false; 112 } 113 notifyListeners(this); 114 } 115 116 private int x, y; 117 118 private synchronized void renderText(FontRenderContext frc, boolean showCursor) 119 { 120 String text = this.text; 121 if (showCursor || (text.length() == 0)) 122 { 123 if (text.length() > 0) text = this.isInverted ? text : text + "|"; 124 else text = "_"; 125 } 126 127 AttributedString as = new AttributedString (text); 128 as.addAttribute(TextAttribute.FONT, new Font ( 129 this.fontName, 130 Font.PLAIN, 131 this.fontSize)); 132 AttributedCharacterIterator aci = as.getIterator(); 133 TextLayout tl = new TextLayout (aci, frc); 134 this.shape = tl.getOutline(AffineTransform.getTranslateInstance(x, y)); 135 } 136 137 public void draw(Graphics2D g, boolean showAnchors) 138 { 139 renderText(g.getFontRenderContext(), showAnchors); 140 super.draw(g, showAnchors); 141 if (showAnchors) 142 { 143 Rectangle bounds = shape.getBounds(); 144 Shape border = new Rectangle2D.Double (bounds.x - 5, bounds.y - 5, bounds.width + 5, bounds.height + 5); 145 if (this.isInverted) 146 { 147 g.setXORMode(Color.LIGHT_GRAY); 148 g.fillRect(bounds.x - 5, bounds.y - 5, bounds.width + 5, bounds.height + 5); 149 g.setPaintMode(); 150 } 151 g.setColor(Color.LIGHT_GRAY); 152 g.draw(border); 153 } 154 } 155 156 private boolean isInverted; 157 158 public synchronized void selectAction(boolean flag) 159 { 160 if (!this.isInverted) 161 return; 162 this.isInverted = false; 163 } 164 165 public synchronized void alternateSelectAction(boolean flag) 166 { 167 if (this.isInverted) 168 return; 169 this.isInverted = true; 170 } 171 172 public Text() 173 { 174 this.isInverted = false; 175 this.text = ""; 176 } 177 } 178 | Popular Tags |