/*
* 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.BitmapFont;
import transientlibs.preui.objects.gui.interfaces.IFont;
import transientlibs.preui.objects.gui.interfaces.IImage;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import java.io.IOException;
import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;
import transientlibs.preui.utils.layout.FontHelper;
import transientlibs.preui.utils.layout.HorizontalAlign;
import transientlibs.preui.utils.layout.VerticalAlign;
import transientlibs.slick2d.util.Log;
import transientlibs.bindedobjects.core.datasets.Action;
import transientlibs.processors.misc.ClickProcessor;
import transientlibs.processors.misc.Detonator;
import transientlibs.preui.objects.gui.misc.ButtonEffect;
/**
*
* @author kibertoad
*/
public class TextMarker extends Marker {
//public int finalTextY;
public int totalTextSize;
//public Fonts fontReference = null;
public IFont font;
public TextCapsule textCapsule = new TextCapsule();
static public TextMarker lastTextMarker;
public TextMarker(String textValue, int setX, int setY) {
super(null, null, setX, setY);
ID = -1;
textCapsule.textHAlign = HorizontalAlign.center;
textCapsule.textVAlign = VerticalAlign.center;
textCapsule.font = Detonator.INSTANCE.fontProvider.getDefaultFont();
font = Detonator.INSTANCE.fontProvider.getDefaultFont();
//textCapsule.highlightedColour = textCapsule.colour;
infoOnHover = true;
textCapsule.text = textValue;
adjustConstraints();
setDefaultOffset();
textCapsule.cut();
}
public TextMarker(IImage setImage, int setX, int setY) {
this(setImage, setImage, setX, setY, -1);
}
public TextMarker(IImage setImage, IImage setHoveredImage, int setx, int sety, int setID) {
super(setImage, setHoveredImage, setx, sety);
//Log.info("NewText Marker");
ID = setID;
textCapsule.textHAlign = HorizontalAlign.center;
textCapsule.textVAlign = VerticalAlign.center;
textCapsule.font = Detonator.INSTANCE.fontProvider.getDefaultFont();
font = Detonator.INSTANCE.fontProvider.getDefaultFont();
//textCapsule.highlightedColour = textCapsule.colour;
infoOnHover = true;
adjustConstraints();
setDefaultOffset();
}
@Override
public void savetoYaml(Writer out) {
try {
StringBuilder outputString = new StringBuilder();
if (!"".equals(textCapsule.text)) {
outputString.append(" - button:");
outputString.append("\n");
outputString.append(" text: ");
outputString.append(textCapsule.text);
} else {
outputString.append(" - image: ");
outputString.append(this.originalImage.getName());
}
outputString.append("\n");
outputString.append(" position: ");
outputString.append(Integer.toString(this.screenCoords.getIntX()));
outputString.append(" ");
outputString.append(Integer.toString(this.screenCoords.getIntY()));
outputString.append("\n");
out.write(outputString.toString());
} catch (IOException ex) {
Logger.getLogger(TextMarker.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void saveToFile(Writer out) {
StringBuilder outputString = new StringBuilder();
outputString.append("element textmarker ");
outputString.append(stringID);
outputString.append(" ");
outputString.append(screenCoords.getIntX());
outputString.append(" ");
outputString.append(screenCoords.getIntY());
outputString.append(" ");
String originalN;
if (originalImage == null) {
Log.error("Looks like some original image is missing, please go check, saving without it");
originalN = "-";
} else {
originalN = originalImage.getName();
}
outputString.append(originalN);
outputString.append(" ");
outputString.append(font.getName());
outputString.append(" ");
outputString.append(imageWidth);
if ((!"".equals(textCapsule.text)) && (!" ".equals(textCapsule.text))) {
outputString.append("\n");
outputString.append("setelementtext ");
outputString.append(stringID);
outputString.append(" ");
outputString.append(textCapsule.text);
}
outputString.append("\n");
outputString.append("\n");
Log.info("String was written: " + outputString.toString());
//Log.info("TransientImage ID: "+imageID);
try {
out.write(outputString.toString());
} catch (IOException ex) {
Logger.getLogger(Marker.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public MarkerType markerID() {
return MarkerType.ButtonMarker;
}
@Override
public String name() {
// if ((selectedImage == null) && (inGroup == null)) {return "TextMarker (no image, no buttongroup)";} else
// if ((selectedImage == null) && (inGroup != null)) {return "TextMarker (no image, in buttongroup)";} else
// {return "TextMarker (with image)";}
return "TextMarker";
}
@Override
public int textHeight() {
return textCapsule.font.getHeight(textCapsule.text);
}
public int textWidth() {
return textCapsule.font.getWidth(textCapsule.text);
}
public void processRelatedAction() {
Action currentAction = (Action) relatedNode;
//Log.info("ggag");
if ((currentAction.reqs.isFulfilled())
&& (currentAction.reqsToActivate.isFulfilled())) {
//Log.info("do eeet");
if (currentAction.eval != null) {
currentAction.eval.processSilently();
}
//Log.info("Size: r "+currentAction.acts.size());
currentAction.acts.process();
}
}
public void calculateTotalHeight() {
int StoredHeight;
totalTextSize = TextCapsule.getStartYWithVerticalAlign(textCapsule.textLines.length * textCapsule.font.getHeight(textCapsule.textLines[0]), getHeight(), textCapsule.textVAlign, textCapsule.textOffsetY); //+y;;
for (String line : textCapsule.textLines) {
if (Math.abs(textCapsule.xoffsetHack) > 0) {
int fittingOffset = FontHelper.getVisibleCharactersFromStart(textCapsule.font, line, Math.abs(textCapsule.xoffsetHack), 1.0f);
String substring = line.substring(fittingOffset, line.length());
StoredHeight = textCapsule.font.getHeight(substring);
} else {
StoredHeight = textCapsule.font.getHeight(line) + 2; //2 is added for better readability
}
totalTextSize += StoredHeight;
}
//totalTextSize = finalTextY;
}
@Override
public boolean isClickable() {
return true;
}
public final void setDefaultOffset() {
textCapsule.parentNode = this;
textCapsule.x = screenCoords.getIntX();
textCapsule.y = screenCoords.getIntY();
//System.out.print("Max X: "+TextBox.maxX);
//System.out.print("TransientImage width: "+ImageWidth);
}
public static int calculateNextYPosition(int YOffset) {
return lastTextMarker.screenCoords.getIntY() + lastTextMarker.getHeight() + YOffset;
}
public static int calculateNextXPosition() {
return lastTextMarker.screenCoords.getIntX();
}
public void setUnlimitedText(String setText) {
textCapsule.text = setText;
countTextSize();
adjustConstraints();
setDefaultOffset();
rewrap();
}
@Override
public void setText(String setText) {
//Log.info("set Text");
textCapsule.text = setText;
adjustConstraints();
setDefaultOffset();
//Log.info("rewrap on setText");
rewrap();
}
@Override
public void setFont(IFont setFont) {
//Log.info("SetFont for text: "+textCapsule.text);
textCapsule.font = setFont;
if (textCapsule.text.length() > 0) {
//Log.info("rewrap on setFont");
rewrap();
}
}
public void rewrap() {
textCapsule.cut();
}
public static void setLastText(String setText) {
lastTextMarker.textCapsule.text = setText;
}
public void isFocusedOnClick() {
addPayload(new ButtonEffect(ButtonEffect.EffectType.FocusedOnClick, this));
}
@Override
public void draw() {
//super.draw ();
//Log.info("draw!");
//Log.info("Drawing marker "+name());
if (getImage() != null) {
if (isFocused) {
focusedImageLink.draw(screenCoords.getIntX(), screenCoords.getIntY());
} else if (!(isHovered())) {
getImage().draw(screenCoords.getIntX(), screenCoords.getIntY());
} else {
getHoveredImage().draw(screenCoords.getIntX(), screenCoords.getIntY());
}
}
if (!"".equals(textCapsule.text)) {
//Log.info("my inside text: "+textCapsule.text);
textCapsule.draw();
}
}
public final void adjustConstraints() {
textCapsule.maxWidth = imageWidth;
}
@Override
public void moveMarker(int toX, int toY) {
textCapsule.x = textCapsule.x + (toX - screenCoords.getIntX());
textCapsule.y = textCapsule.y + (toY - screenCoords.getIntY());
super.moveMarker(toX, toY);
}
@Override
public void moveMarkerByDelta(int byX, int byY) {
textCapsule.x = textCapsule.x + (byX);
textCapsule.y = textCapsule.y + (byY);
super.moveMarkerByDelta(byX, byY);
}
public final void countTextSize() {
imageHeight = textCapsule.font.getHeight(textCapsule.text);
imageWidth = textCapsule.font.getWidth(textCapsule.text);
}
@Override
public void addPayload(ClickProcessor cp) {
super.addPayload(cp);
textCapsule.highlightedColour = Detonator.INSTANCE.colourFactory.defaultHoveredButtonColour;
//textCapsule.highlightedColour = Detonator.INSTANCE.colourFactory.produceColour(org.newdawn.slick.Color.gray);
}
@Override
public void draw(SpriteBatch spriteBatch) {
super.draw(spriteBatch);
if ((isDrawn) && (!"".equals(textCapsule.text))) {
//Log.info("my inside text: "+textCapsule.text);
textCapsule.draw(spriteBatch);
}
}
@Override
public String getText() {
return textCapsule.text;
}
@Override
public BitmapFont getFont() {
return font.getFontInfo().font.font;
}
}