/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package transientlibs.preui.objects.gui.elements;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import transientlibs.preui.objects.gui.elements.TextMarker;
import transientlibs.preui.objects.gui.elements.InfoGroup;
import transientlibs.preui.objects.gui.elements.Marker;
import transientlibs.preui.utils.layout.FontHelper;
import transientlibs.preui.utils.layout.HorizontalAlign;
import transientlibs.preui.utils.layout.TextBreak;
import transientlibs.preui.utils.layout.VerticalAlign;
import transientlibs.processors.misc.Detonator;
import java.util.ArrayList;
import java.util.List;
import transientlibs.preui.objects.gui.interfaces.IFont;
import transientlibs.preui.objects.gui.interfaces.IColour;
import transientlibs.preui.objects.gui.interfaces.IMarker;
//import utils.layout.align.HorizontalAlign;
//import utils.textRender;
/**
* Original text wrapping and text alignment algorithm by the void256 of the
* Nifty UI project
*
* @author void
*/
public class TextCapsule {
public static final IColour transientGray = new TransientColour(255, 186, 186, 186);
public int textOffsetX = 0;
public int textOffsetY = 0; //used for calculation starting position
public String text = "";
public String[] textLines;// = new String[0]; //wrapped text
public int lineDistance = 0; //additional space between different lines
public int x = 0;
public int y = 0;
public int xoffsetHack = 0;
//public Font font;
public IFont font;
public IColour colour = null;
public IColour highlightedColour = null;
public HorizontalAlign textHAlign = HorizontalAlign.center; //position inside the element
public VerticalAlign textVAlign = VerticalAlign.center; //position inside the element
//int maxX = 0; //set with setDefaultOffset of textMarker, =MaxTextWidth
public int maxWidth;
public int maxHeight;
public TextMarker parentNode;
public boolean runningText = false;
public StringBuilder[] revealedText;// = new StringBuilder[0];
public String[] outputString;// = new String[0];
public int revealCounter;
public int revealStringCounter;
public boolean fullyRevealed;
public long lastReveal;
public long lastCheck;
//public static final long revealDelay = 20;
public void resetTextReveal() {
// for (StringBuilder r : revealedText) {r.setLength(0);}
//Log.info("Reset reveal");
revealCounter = 0;
revealStringCounter = 0;
fullyRevealed = false;
outputString = new String[textLines.length];
revealedText = new StringBuilder[textLines.length];
for (int x1 = 0; x1 < textLines.length; x1++) {
revealedText[x1] = new StringBuilder();
outputString[x1] = " ";
}
lastReveal = System.currentTimeMillis() - Detonator.INSTANCE.runningTextDelay;
}
public void revealAllSymbols() {
System.arraycopy(textLines, 0, outputString, 0, textLines.length);
revealStringCounter = textLines.length + 1;
checkIfFullCapsuleRevealed();
}
public void checkIfFullCapsuleRevealed() {
if (revealStringCounter >= textLines.length) {
fullyRevealed = true;
TextMarker tryMarker = parentNode.getInGroup().nextElement(parentNode);
runningText = false;
if (tryMarker != null) {
//Log.info("now you run");
tryMarker.textCapsule.runningText = true;
tryMarker.isDrawn = true;
tryMarker.isOnScreen = true;
tryMarker.textCapsule.cut();
((InfoGroup) (parentNode.getInGroup())).currentRunningElement = tryMarker;
}
}
}
public void revealSymbol() {
//Log.info("Awright, string to reveal is: ["+textLines[0]+"]");
revealedText[revealStringCounter].append(textLines[revealStringCounter].charAt(revealCounter));
outputString[revealStringCounter] = revealedText[revealStringCounter].toString();
//Log.info("current textLine length: "+textLines[revealStringCounter].length());
//Log.info("current textLine counter: "+revealCounter);
revealCounter++;
if (revealCounter == textLines[revealStringCounter].length()) {
revealCounter = 0;
revealStringCounter++;
checkIfFullCapsuleRevealed();
}
//if (textLines[revealStringCounter].charAt(revealCounter-1) == ' ') {revealSymbol();}
}
/*
* Rewrap and generate the output text
*/
public void cut() {
//Log.info("do cut");
//Log.info("MaxWidth cut: "+maxWidth);
textLines = wrapText(maxWidth, text.split("\n", -1), font);
//Log.info(textLines[0]);
if (runningText) {
resetTextReveal();
//if (textLines[0].length()>0) {
// revealSymbol();
//} else {fullyRevealed = true;}
}
//Log.info("After wrap: "+textLines[0]);
}
public void draw(SpriteBatch spriteBatch) {
if (runningText) {
if (fullyRevealed == false) {
lastCheck = System.currentTimeMillis();
if (lastCheck - lastReveal > Detonator.INSTANCE.runningTextDelay) {
lastReveal = lastCheck;
revealSymbol();
}
}
//Log.info("render outputstring");
renderLines(spriteBatch, parentNode, outputString, font, parentNode.isHovered());
} else {
renderLines(spriteBatch, parentNode, textLines, font, parentNode.isHovered());
}
}
public void draw() {
if (runningText) {
if (fullyRevealed == false) {
lastCheck = System.currentTimeMillis();
if (lastCheck - lastReveal > Detonator.INSTANCE.runningTextDelay) {
lastReveal = lastCheck;
revealSymbol();
}
}
//Log.info("render outputstring");
renderLines(parentNode, outputString, font, parentNode.isHovered());
} else {
renderLines(parentNode, textLines, font, parentNode.isHovered());
}
}
public void renderLines(final Marker w, String[] lines, IFont font, boolean isHighlighted) {
//Log.info("Awright, string to render is: ["+lines[0]+"]");
int StoredHeight;
int outY;
//int accumulatedHeight;
outY = getStartYWithVerticalAlign(lines.length * font.getHeight(lines[0]), w.getHeight(), textVAlign, textOffsetY); //+y;
for (String line : lines) {
//int yy = w.screenCoords.getIntY() + outY;
int yy = y + outY;
if (Math.abs(xoffsetHack) > 0) {
int fittingOffset = FontHelper.getVisibleCharactersFromStart(font, line, Math.abs(xoffsetHack), 1.0f);
//unneeded?
// String cut = line.substring(0, fittingOffset);
String substring = line.substring(fittingOffset, line.length());
//int xx = w.screenCoords.x + xoffsetHack + font.getWidth(substring);
int xx = x + xoffsetHack + font.getWidth(substring);
renderLine(xx, yy, substring, font, isHighlighted);
//StoredHeight = font.getWidth(substring);
StoredHeight = font.getHeight(substring);
} else {
//Log.info("Line! ["+line+"]");
//int xx = w.screenCoords.x + getStartXWithHorizontalAlign(font.getWidth(line), w.getWidth(), textHAlign);//+x;
int xx = x + getStartXWithHorizontalAlign(font.getWidth(line), w.getWidth(), textHAlign);//+x;
renderLine(xx, yy, line, font, isHighlighted);
StoredHeight = font.getHeight(line) + 2; //2 is added for better readability
}
outY += StoredHeight;
}
Detonator.INSTANCE.lastDrawnY = outY;
}
public void renderLines(SpriteBatch spriteBatch, IMarker w, String[] lines, IFont font, boolean isHighlighted) {
//Log.info("Awright, string to render is: ["+lines[0]+"]");
int StoredHeight;
int outY;
//int accumulatedHeight;
outY = getStartYWithVerticalAlign(lines.length * font.getHeight(lines[0]), w.getHeight(), textVAlign, textOffsetY); //+y;
for (String line : lines) {
int yy = y + outY;
if (Math.abs(xoffsetHack) > 0) {
int fittingOffset = FontHelper.getVisibleCharactersFromStart(font, line, Math.abs(xoffsetHack), 1.0f);
String substring = line.substring(fittingOffset, line.length());
int xx = x + xoffsetHack + font.getWidth(substring);
renderLine(spriteBatch, xx, yy, substring, font, isHighlighted);
StoredHeight = font.getHeight(substring);
} else {
//Log.info("Line! ["+line+"]");
int xx = x + getStartXWithHorizontalAlign(font.getWidth(line), w.getWidth(), textHAlign);//+x;
renderLine(spriteBatch, xx, yy, line, font, isHighlighted);
StoredHeight = font.getHeight(line) + 2 + + lineDistance; //2 is added for better readability
}
outY += StoredHeight;
//outY -= StoredHeight;
}
Detonator.INSTANCE.lastDrawnY = outY;
}
public void render(final TextMarker w) {
if (textLines.length == 0 || (textLines.length == 1 && textLines[0].length() == 0)) {
return;
}
renderLines(w, textLines, w.textCapsule.font, w.isHovered());
}
public void render(final TextMarker w, SpriteBatch spriteBatch) {
if (textLines.length == 0 || (textLines.length == 1 && textLines[0].length() == 0)) {
return;
}
renderLines(spriteBatch, w, textLines, w.textCapsule.font, w.isHovered());
}
private void renderLine(
final int xx,
final int yy,
final String line,
final IFont font, boolean isHighlighted) {
if (!(isHighlighted)) {
font.drawString(xx, yy, line, colour);
} else {
font.drawString(xx, yy, line, highlightedColour);
}
}
/**
* Get start Y for text rendering given the textHeight and the
* elementHeight.
*
* @param textHeight text height
* @param elementHeight element height
* @param verticalAlign verticalAlign
* @return start y for text rendering
*/
public static int getStartYWithVerticalAlign(
final int textHeight,
final int elementHeight,
final VerticalAlign verticalAlign,
final int offsetY) {
if (VerticalAlign.top == verticalAlign) {
return (0 + offsetY);
} else if (VerticalAlign.center == verticalAlign) {
return ((elementHeight - textHeight) / 2) + offsetY;
} else if (VerticalAlign.bottom == verticalAlign) {
return elementHeight - textHeight + offsetY;
} else {
return 0;
}
}
/**
* Get start x for text rendering given the textWidth and the elementWidth.
*
* @param textWidth text width
* @param elementWidth element width
* @param horizontalAlign horizontalAlign
* @return start x for text rendering
*/
public static int getStartXWithHorizontalAlign(
final int textWidth,
final int elementWidth,
final HorizontalAlign horizontalAlign) {
if (HorizontalAlign.left == horizontalAlign) {
return 0;
} else if (HorizontalAlign.center == horizontalAlign) {
return (elementWidth - textWidth) / 2;
} else if (HorizontalAlign.right == horizontalAlign) {
return elementWidth - textWidth;
} else {
return 0;
}
}
public static String[] wrapText(final int width, final String[] textLines, IFont font) {
List< String> lines = new ArrayList< String>();
for (String line : textLines) {
//Log.info("font: "+font.getWidth("11"));
int lineLengthInPixel = font.getWidth(line);
if (lineLengthInPixel > width) {
lines.addAll(new TextBreak(line, width, font).split());
} else {
lines.add(line);
}
}
return lines.toArray(new String[0]);
}
private void renderLine(
SpriteBatch spriteBatch,
final int xx,
final int yy,
final String line,
final IFont font, boolean isHighlighted) {
if (!(isHighlighted)) {
font.drawString(spriteBatch, xx, yy, line, colour);
} else {
font.drawString(spriteBatch, xx, yy, line, highlightedColour);
}
}
public TextCapsule() {
colour = Detonator.INSTANCE.colourFactory.defaultButtonColour;
highlightedColour = colour;
//highlightedColour = Detonator.INSTANCE.colourFactory.defaultHoveredButtonColour;
}
}