/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Ocarina2D.TextEngine;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import puppyeyes.engine.Actor;
import puppyeyes.engine.Settings.Input;
import puppyeyes.engine.Sprite;
/**
*
* @author majora
*/
public class Text extends Actor {
private double currentChar;
private int length;
private String word;
public int maxWidth;
private boolean finishedRendering = false;
private int lastSpace = 0;
// Capitals
private Sprite sprA = new Sprite("Resources/Sprites/Text/A.png");
private Sprite sprB = new Sprite("Resources/Sprites/Text/B.png");
private Sprite sprC = new Sprite("Resources/Sprites/Text/C.png");
private Sprite sprD = new Sprite("Resources/Sprites/Text/D.png");
private Sprite sprE = new Sprite("Resources/Sprites/Text/E.png");
private Sprite sprF = new Sprite("Resources/Sprites/Text/F.png");
private Sprite sprG = new Sprite("Resources/Sprites/Text/G.png");
private Sprite sprH = new Sprite("Resources/Sprites/Text/H.png");
private Sprite sprI = new Sprite("Resources/Sprites/Text/I.png");
private Sprite sprJ = new Sprite("Resources/Sprites/Text/J.png");
private Sprite sprK = new Sprite("Resources/Sprites/Text/K.png");
private Sprite sprL = new Sprite("Resources/Sprites/Text/L.png");
private Sprite sprM = new Sprite("Resources/Sprites/Text/M.png");
private Sprite sprN = new Sprite("Resources/Sprites/Text/N.png");
private Sprite sprO = new Sprite("Resources/Sprites/Text/O.png");
private Sprite sprP = new Sprite("Resources/Sprites/Text/P.png");
private Sprite sprQ = new Sprite("Resources/Sprites/Text/Q.png");
private Sprite sprR = new Sprite("Resources/Sprites/Text/R.png");
private Sprite sprS = new Sprite("Resources/Sprites/Text/S.png");
private Sprite sprT = new Sprite("Resources/Sprites/Text/T.png");
private Sprite sprU = new Sprite("Resources/Sprites/Text/U.png");
private Sprite sprV = new Sprite("Resources/Sprites/Text/V.png");
private Sprite sprW = new Sprite("Resources/Sprites/Text/W.png");
private Sprite sprX = new Sprite("Resources/Sprites/Text/X.png");
private Sprite sprY = new Sprite("Resources/Sprites/Text/Y.png");
private Sprite sprZ = new Sprite("Resources/Sprites/Text/Z.png");
// Non
private Sprite spra = new Sprite("Resources/Sprites/Text/a.png");
private Sprite sprb = new Sprite("Resources/Sprites/Text/b.png");
private Sprite sprc = new Sprite("Resources/Sprites/Text/c.png");
private Sprite sprd = new Sprite("Resources/Sprites/Text/d.png");
private Sprite spre = new Sprite("Resources/Sprites/Text/e.png");
private Sprite sprf = new Sprite("Resources/Sprites/Text/f.png");
private Sprite sprg = new Sprite("Resources/Sprites/Text/g.png");
private Sprite sprh = new Sprite("Resources/Sprites/Text/h.png");
private Sprite spri = new Sprite("Resources/Sprites/Text/i.png");
private Sprite sprj = new Sprite("Resources/Sprites/Text/j.png");
private Sprite sprk = new Sprite("Resources/Sprites/Text/k.png");
private Sprite sprl = new Sprite("Resources/Sprites/Text/l.png");
private Sprite sprm = new Sprite("Resources/Sprites/Text/m.png");
private Sprite sprn = new Sprite("Resources/Sprites/Text/n.png");
private Sprite spro = new Sprite("Resources/Sprites/Text/o.png");
private Sprite sprp = new Sprite("Resources/Sprites/Text/p.png");
private Sprite sprq = new Sprite("Resources/Sprites/Text/q.png");
private Sprite sprr = new Sprite("Resources/Sprites/Text/r.png");
private Sprite sprs = new Sprite("Resources/Sprites/Text/s.png");
private Sprite sprt = new Sprite("Resources/Sprites/Text/t.png");
private Sprite spru = new Sprite("Resources/Sprites/Text/u.png");
private Sprite sprv = new Sprite("Resources/Sprites/Text/v.png");
private Sprite sprw = new Sprite("Resources/Sprites/Text/w.png");
private Sprite sprx = new Sprite("Resources/Sprites/Text/x.png");
private Sprite spry = new Sprite("Resources/Sprites/Text/y.png");
private Sprite sprz = new Sprite("Resources/Sprites/Text/z.png");
// Other
private Sprite sprSpace = new Sprite("Resources/Sprites/Text/space.png");
private Sprite sprComma = new Sprite("Resources/Sprites/Text/comma.png");
private Sprite sprFullStop = new Sprite("Resources/Sprites/Text/fullstop.png");
public Text(String word) {
this.word = word;
currentChar = 0;
length = word.length();
draw.setVisible(true);
draw.setLayer(3);
maxWidth = (sprSpace.getWidth()+1) *50;
}
/** 08457 91 91 55.
* Anglian water: 01522 341000
* Southern Electric: 0800 107 3203
*
* Create a word instantly
* @param word
* @param instant set the text to display instantly.
*/
public Text(String word, boolean instant) {
this.word = word;
length = word.length();
if (instant==true) { currentChar = length; }
else { currentChar = 0; }
draw.setVisible(true);
draw.setLayer(3);
maxWidth = (sprSpace.getWidth()+1) *46;
}
/**
* Change the text to the new string.
* @param text the string to set the text to.
*/
public void setText(String text) {
word = text;
length = word.length();
currentChar = length;
}
/**
* Checks if the text has finished rendering.
* @return true if the text has finished rendering, false otherwise.
*/
public boolean isFinished() {
return finishedRendering;
}
/**
* Counts the number of characters to the next space from the position.
* @param position the position to start from.
* @return the number of characters to a space.
*/
private int countToSpace(int position) {
char character = 'q';
int endPosition=position;
while (character != ' ' && endPosition<word.length()) {
character = word.charAt(endPosition);
endPosition++;
}
return endPosition - position;
}
/**
* Counts the number of pixels to the next space from the position.
* @param the position to start from.
* @return the number of pixels to a space.
*/
private int pixelsToSpace(int position) {
char character = 'q';
int xposition=0;
while (character != ' ' && position<word.length()) {
Sprite sprCharacter = charToSprite(word.charAt(position));
character = word.charAt(position);
if (sprCharacter!=null) {
xposition += sprCharacter.getWidth()+1;
} else { return 0; }
position++;
}
return xposition;
}
@Override
public void step() {
if (currentChar< length) {
currentChar++;
} else { finishedRendering = true; }
boolean pressB = Input.keyDown(KeyEvent.VK_F);
if (pressB==true) {
currentChar= length;
}
}
@Override
public void draw(Graphics2D graphics, int x, int y) {
int xposition = 0;
int yposition = 0;
for (int i = 0; i<(int)currentChar; i++) {
Sprite character = charToSprite(word.charAt(i));
if (character!=null) {
if (pixelsToSpace(i)+xposition >= maxWidth) {
xposition = 0; yposition += sprSpace.getHeight()+2;
}
character.drawSprite(graphics,xposition+x,yposition+y);
xposition += character.getWidth()+1;
}
if (xposition >= maxWidth) { xposition = x; yposition += sprSpace.getHeight()+2; }
}
}
@Override
public void destroy() {
}
/**
* Returns the sprite assigned to the given character
* @param character the character to convert.
* @return a Sprite for the character. If the character is not known it returns null.
*/
public Sprite charToSprite(char character) {
switch (character) {
// Capitals
case 'A': return sprA;
case 'B': return sprB;
case 'C': return sprC;
case 'D': return sprD;
case 'E': return sprE;
case 'F': return sprF;
case 'G': return sprG;
case 'H': return sprH;
case 'I': return sprI;
case 'J': return sprJ;
case 'K': return sprK;
case 'L': return sprL;
case 'M': return sprM;
case 'N': return sprN;
case 'O': return sprO;
case 'P': return sprP;
case 'Q': return sprQ;
case 'R': return sprR;
case 'S': return sprS;
case 'T': return sprT;
case 'U': return sprU;
case 'V': return sprV;
case 'W': return sprW;
case 'X': return sprX;
case 'Y': return sprY;
case 'Z': return sprZ;
// Non - capital
case 'a': return spra;
case 'b': return sprb;
case 'c': return sprc;
case 'd': return sprd;
case 'e': return spre;
case 'f': return sprf;
case 'g': return sprg;
case 'h': return sprh;
case 'i': return spri;
case 'j': return sprj;
case 'k': return sprk;
case 'l': return sprl;
case 'm': return sprm;
case 'n': return sprn;
case 'o': return spro;
case 'p': return sprp;
case 'q': return sprq;
case 'r': return sprr;
case 's': return sprs;
case 't': return sprt;
case 'u': return spru;
case 'v': return sprv;
case 'w': return sprw;
case 'x': return sprx;
case 'y': return spry;
case 'z': return sprz;
// Other
case ' ': lastSpace = (int)currentChar; return sprSpace;
case '?': return sprSpace;
case '.': return sprFullStop;
case ',': return sprComma;
}
return null;
}
}