diff options
Diffstat (limited to 'src/com/fourisland/fourpuzzle/window/TextRenderer.java')
| -rw-r--r-- | src/com/fourisland/fourpuzzle/window/TextRenderer.java | 306 |
1 files changed, 306 insertions, 0 deletions
| diff --git a/src/com/fourisland/fourpuzzle/window/TextRenderer.java b/src/com/fourisland/fourpuzzle/window/TextRenderer.java new file mode 100644 index 0000000..f114c4d --- /dev/null +++ b/src/com/fourisland/fourpuzzle/window/TextRenderer.java | |||
| @@ -0,0 +1,306 @@ | |||
| 1 | /* | ||
| 2 | * To change this template, choose Tools | Templates | ||
| 3 | * and open the template in the editor. | ||
| 4 | */ | ||
| 5 | |||
| 6 | package com.fourisland.fourpuzzle.window; | ||
| 7 | |||
| 8 | import com.fourisland.fourpuzzle.Display; | ||
| 9 | import com.fourisland.fourpuzzle.Game; | ||
| 10 | import com.fourisland.fourpuzzle.util.PauseTimer; | ||
| 11 | import java.awt.Graphics2D; | ||
| 12 | import java.awt.Rectangle; | ||
| 13 | import java.awt.TexturePaint; | ||
| 14 | import java.awt.image.BufferedImage; | ||
| 15 | import java.util.ArrayList; | ||
| 16 | import java.util.HashMap; | ||
| 17 | import java.util.List; | ||
| 18 | import java.util.Map; | ||
| 19 | import java.util.Vector; | ||
| 20 | |||
| 21 | /** | ||
| 22 | * | ||
| 23 | * @author hatkirby | ||
| 24 | */ | ||
| 25 | public class TextRenderer { | ||
| 26 | |||
| 27 | private static final int SPACER = 4; | ||
| 28 | |||
| 29 | private boolean initalized = false; | ||
| 30 | private int width; | ||
| 31 | private List<LineOfText> messages = new ArrayList<LineOfText>(); | ||
| 32 | |||
| 33 | public TextRenderer(int width) | ||
| 34 | { | ||
| 35 | this.width = width; | ||
| 36 | } | ||
| 37 | |||
| 38 | private boolean useEscapes = false; | ||
| 39 | public void setEscapes(boolean use) | ||
| 40 | { | ||
| 41 | if (initalized == true) | ||
| 42 | { | ||
| 43 | throw new IllegalStateException("Already initalized"); | ||
| 44 | } | ||
| 45 | |||
| 46 | useEscapes = use; | ||
| 47 | } | ||
| 48 | |||
| 49 | private int indent = 0; | ||
| 50 | public void setIndent(int indent) | ||
| 51 | { | ||
| 52 | if (initalized == true) | ||
| 53 | { | ||
| 54 | throw new IllegalStateException("Already initalized"); | ||
| 55 | } | ||
| 56 | |||
| 57 | this.indent = indent; | ||
| 58 | } | ||
| 59 | |||
| 60 | public void initalizeText(String message) | ||
| 61 | { | ||
| 62 | if (initalized == true) | ||
| 63 | { | ||
| 64 | throw new IllegalStateException("Already initalized"); | ||
| 65 | } | ||
| 66 | |||
| 67 | int length = width - SPACER - indent; | ||
| 68 | int len = 0; | ||
| 69 | LineOfText temp = new LineOfText(message); | ||
| 70 | |||
| 71 | if (useEscapes) | ||
| 72 | { | ||
| 73 | for (int i=0; i<temp.length(); i++) | ||
| 74 | { | ||
| 75 | if (temp.toString().substring(i, i+1).equals("\\")) | ||
| 76 | { | ||
| 77 | for (MessageEscape escape : MessageEscape.values()) | ||
| 78 | { | ||
| 79 | if (escape.match(temp.toString().substring(i))) | ||
| 80 | { | ||
| 81 | temp.addEscape(i, escape.getMatch(temp.toString().substring(i))); | ||
| 82 | temp = new LineOfText(escape.removeEscape(temp.toString()), temp.escapes); | ||
| 83 | |||
| 84 | break; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | temp = new LineOfText(MessageEscape.removeEscapes(temp.toString()), temp.escapes); | ||
| 91 | } | ||
| 92 | |||
| 93 | while (!temp.toString().isEmpty()) | ||
| 94 | { | ||
| 95 | while ((Display.getFontMetrics().stringWidth(temp.toString().substring(0, len)) < length) && (len < temp.length())) | ||
| 96 | { | ||
| 97 | len++; | ||
| 98 | } | ||
| 99 | |||
| 100 | if (len != temp.toString().length()) | ||
| 101 | { | ||
| 102 | while ((!temp.toString().substring(len, len+1).equals(" ")) && (len > 0)) | ||
| 103 | { | ||
| 104 | len--; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | messages.add(temp.part(0, len)); | ||
| 109 | |||
| 110 | if (len != temp.length()) | ||
| 111 | { | ||
| 112 | temp = temp.part(len+1, temp.length()); | ||
| 113 | } else { | ||
| 114 | break; | ||
| 115 | } | ||
| 116 | |||
| 117 | len = 0; | ||
| 118 | } | ||
| 119 | |||
| 120 | initalized = true; | ||
| 121 | } | ||
| 122 | |||
| 123 | int upTo = Integer.MIN_VALUE; | ||
| 124 | int num = 0; | ||
| 125 | int lastStart = Integer.MIN_VALUE+1; | ||
| 126 | PauseTimer pt = new PauseTimer(0); | ||
| 127 | public BufferedImage render(int start) | ||
| 128 | { | ||
| 129 | if (initalized == false) | ||
| 130 | { | ||
| 131 | throw new IllegalStateException("TextRenderer must be initalized prior to rendering"); | ||
| 132 | } | ||
| 133 | |||
| 134 | if (lastStart != start) | ||
| 135 | { | ||
| 136 | num = 0; | ||
| 137 | for (int i=start;i<Math.min(messages.size(),start+4);i++) | ||
| 138 | { | ||
| 139 | num += messages.get(i).length(); | ||
| 140 | } | ||
| 141 | |||
| 142 | lastStart = start; | ||
| 143 | upTo = 0; | ||
| 144 | } | ||
| 145 | |||
| 146 | BufferedImage temp = Display.createCanvas(width, Game.HEIGHT); | ||
| 147 | Graphics2D g = temp.createGraphics(); | ||
| 148 | Display.setFont(g); | ||
| 149 | |||
| 150 | int currentColor = 0; | ||
| 151 | |||
| 152 | int fh = g.getFontMetrics().getHeight(); | ||
| 153 | int ty = fh-(SPACER/2); | ||
| 154 | int toPrint = upTo; | ||
| 155 | int end = Math.min(messages.size(), start+4); | ||
| 156 | |||
| 157 | for (int i=start;i<end;i++) | ||
| 158 | { | ||
| 159 | LineOfText message = messages.get(i); | ||
| 160 | int tx = indent; | ||
| 161 | g.setPaint(new TexturePaint(SystemGraphic.getTextColor(currentColor), new Rectangle(0, ty, width, fh))); | ||
| 162 | |||
| 163 | for (int j=0;j<Math.min(toPrint, message.length());j++) | ||
| 164 | { | ||
| 165 | boolean breakout = false; | ||
| 166 | for (MessageEscapePair escape : message.getEscapes(j)) | ||
| 167 | { | ||
| 168 | switch (escape.getMessageEscape()) | ||
| 169 | { | ||
| 170 | case Color: currentColor = Integer.decode(escape.getMatchResult().group(1)); | ||
| 171 | g.setPaint(new TexturePaint(SystemGraphic.getTextColor(currentColor), new Rectangle(0, ty, width, fh))); | ||
| 172 | |||
| 173 | break; | ||
| 174 | |||
| 175 | case Pause: pt.setTimer(10); | ||
| 176 | message.getEscapes(j).remove(escape); | ||
| 177 | |||
| 178 | breakout = true; | ||
| 179 | break; | ||
| 180 | } | ||
| 181 | |||
| 182 | if (breakout) | ||
| 183 | { | ||
| 184 | break; | ||
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | g.drawString(message.toString().substring(j, j+1), tx, ty); | ||
| 189 | |||
| 190 | tx+=(g.getFontMetrics().stringWidth(message.toString().substring(j, j+1))); | ||
| 191 | } | ||
| 192 | |||
| 193 | ty+=(SPACER+fh); | ||
| 194 | |||
| 195 | toPrint -= Math.min(toPrint, message.length()); | ||
| 196 | } | ||
| 197 | |||
| 198 | if (pt.isElapsed() && (upTo < num)) | ||
| 199 | { | ||
| 200 | upTo+=3; | ||
| 201 | } | ||
| 202 | |||
| 203 | return temp; | ||
| 204 | } | ||
| 205 | |||
| 206 | public int numLines() | ||
| 207 | { | ||
| 208 | return messages.size(); | ||
| 209 | } | ||
| 210 | |||
| 211 | public boolean isCascadingDone() | ||
| 212 | { | ||
| 213 | return (upTo >= num); | ||
| 214 | } | ||
| 215 | |||
| 216 | } | ||
| 217 | |||
| 218 | class LineOfText implements CharSequence | ||
| 219 | { | ||
| 220 | String line; | ||
| 221 | Map<Integer, Vector<MessageEscapePair>> escapes = new HashMap<Integer, Vector<MessageEscapePair>>(); | ||
| 222 | public LineOfText(String line) | ||
| 223 | { | ||
| 224 | this.line = line; | ||
| 225 | } | ||
| 226 | |||
| 227 | public LineOfText(String line, Map<Integer, Vector<MessageEscapePair>> escapes) | ||
| 228 | { | ||
| 229 | this.line = line; | ||
| 230 | |||
| 231 | for (int i=0; i<line.length(); i++) | ||
| 232 | { | ||
| 233 | if (escapes.containsKey(i)) | ||
| 234 | { | ||
| 235 | for (MessageEscapePair escape : escapes.get(i)) | ||
| 236 | { | ||
| 237 | addEscape(i, escape); | ||
| 238 | } | ||
| 239 | } | ||
| 240 | } | ||
| 241 | } | ||
| 242 | |||
| 243 | public void addEscape(int index, MessageEscapePair escape) | ||
| 244 | { | ||
| 245 | if (!escapes.containsKey(index)) | ||
| 246 | { | ||
| 247 | escapes.put(index, new Vector<MessageEscapePair>()); | ||
| 248 | } | ||
| 249 | |||
| 250 | escapes.get(index).add(escape); | ||
| 251 | } | ||
| 252 | |||
| 253 | public Vector<MessageEscapePair> getEscapes(int index) | ||
| 254 | { | ||
| 255 | if (!escapes.containsKey(index)) | ||
| 256 | { | ||
| 257 | return new Vector<MessageEscapePair>(); | ||
| 258 | } else { | ||
| 259 | return escapes.get(index); | ||
| 260 | } | ||
| 261 | } | ||
| 262 | |||
| 263 | public LineOfText part(int start, int end) | ||
| 264 | { | ||
| 265 | LineOfText temp = new LineOfText(line.substring(start, end)); | ||
| 266 | |||
| 267 | for (int i=start; i<end; i++) | ||
| 268 | { | ||
| 269 | if (escapes.containsKey(i)) | ||
| 270 | { | ||
| 271 | for (MessageEscapePair escape : escapes.get(i)) | ||
| 272 | { | ||
| 273 | temp.addEscape(i, escape); | ||
| 274 | } | ||
| 275 | } | ||
| 276 | } | ||
| 277 | |||
| 278 | return temp; | ||
| 279 | } | ||
| 280 | |||
| 281 | @Override | ||
| 282 | public String toString() | ||
| 283 | { | ||
| 284 | return line; | ||
| 285 | } | ||
| 286 | |||
| 287 | public void setString(String message) | ||
| 288 | { | ||
| 289 | this.line = message; | ||
| 290 | } | ||
| 291 | |||
| 292 | public int length() | ||
| 293 | { | ||
| 294 | return line.length(); | ||
| 295 | } | ||
| 296 | |||
| 297 | public char charAt(int index) | ||
| 298 | { | ||
| 299 | return line.charAt(index); | ||
| 300 | } | ||
| 301 | |||
| 302 | public CharSequence subSequence(int start, int end) | ||
| 303 | { | ||
| 304 | return line.subSequence(start, end); | ||
| 305 | } | ||
| 306 | } \ No newline at end of file | ||
