Back to project page DolphinOES.
The source code is released under:
Apache License
If you think the Android project DolphinOES listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/******************************************************************************* * Copyright 2014 See AUTHORS file.// www . ja v a 2 s .com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ package com.sidereal.dolphinoes.ui; import java.util.ArrayList; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.math.Vector2; import com.sidereal.dolphinoes.architecture.DolphinOES; import com.sidereal.dolphinoes.architecture.GameObject; import com.sidereal.dolphinoes.architecture.GameScene; /** Abstract object used for drawing text on the screen. Supports customisation * for different font, font colors, scale, transparency; * * @author Claudiu Bele */ public class TextBuilder extends GameObject { // region static /** Gets the Blocks4 font made by Claudiu Bele, which can be already found in * the framework and doesn't require creating additional files. * * @return */ public static BitmapFont getFont(String fontDataPath) { return new BitmapFont(DolphinOES.assets .get(fontDataPath, BitmapFont.class).getData().getFontFile()); } // endregion static // region fields public enum Allign { Left, Center, Right }; public enum Anchor { Top, Middle, Bottom } public static class Paragraph { public String text; public Color color; public Paragraph(String text, Color color) { this.text = text; this.color = color; } } public Color color; public String text; private BitmapFont font; public float alpha; public float scale; public Vector2 bounds; private boolean wrapText; public float lineSpacing; public float windowSize; private Allign allignment; private Anchor anchor; private ArrayList<Paragraph> rawParagraphs; private ArrayList<Paragraph> paraGraphsToWrite; // endregion fields // region Constructors public TextBuilder(GameScene scene, boolean wrap) { this(scene, wrap, DolphinOES.assets.frameworkAssetsFolder + "Blocks.fnt"); } public TextBuilder(GameScene scene, boolean wrap, String fontDataPath) { super(scene); font = getFont(fontDataPath); this.color = Color.WHITE; this.bounds = new Vector2(); rawParagraphs = new ArrayList<TextBuilder.Paragraph>(); paraGraphsToWrite = new ArrayList<TextBuilder.Paragraph>(); setAlpha(1); setScale(1f); pos.setRelative(0, 0, 5); lineSpacing = 5f; windowSize = 600; this.wrapText = wrap; allignment = Allign.Center; anchor = Anchor.Middle; } // endregion // region methods public final ArrayList<Paragraph> wrapText(String data, Color color) { ArrayList<Paragraph> resultingLines = new ArrayList<Paragraph>(); if (!wrapText) { resultingLines.add(new Paragraph(data, color)); return resultingLines; } // splitting the string into lines String[] lines = data.split("\n"); for (int i = 0; i < lines.length; i++) { String[] words = lines[i].split(" "); String currText = ""; for (int j = 0; j < words.length; j++) { // curr text + new word exceeds line limit, cut it if (font.getBounds(currText + words[j]).width > windowSize) { // trim line currText = currText.trim(); if (currText == null) currText = ""; // this is a one word line, if (currText.equals("")) { resultingLines.add(new Paragraph(words[j], color)); } // not a one word line else { resultingLines.add(new Paragraph(currText, color)); currText = ""; j--; } } else { // append curr word to the string currText += words[j] + " "; } } currText = currText.trim(); resultingLines.add(new Paragraph(currText, color)); } bounds.set(windowSize, font.getBounds("X").height * resultingLines.size() + (resultingLines.size()) * lineSpacing); return resultingLines; } // region adding and setting text public final void addText(String data, Color lineColor) { rawParagraphs.add(new Paragraph(data, lineColor)); paraGraphsToWrite.addAll(wrapText(data, lineColor)); if (text == "") text = data; else text += "\n " + data; } public final void addText(ArrayList<String> data, ArrayList<Color> paragraphColors) { for (int i = 0; i < data.size(); i++) { if (paragraphColors == null || paragraphColors.size() != data.size() || paragraphColors.get(i) == null) addText(data.get(i), Color.WHITE); else addText(data.get(i), paragraphColors.get(i)); } } public void setText(String data, Color targetColor) { if (text != null && text.equals(data)) return; text = data; paraGraphsToWrite.clear(); if (wrapText) { paraGraphsToWrite = wrapText(data, targetColor); } else { paraGraphsToWrite.add(new Paragraph(data, targetColor)); } rawParagraphs.clear(); rawParagraphs.add(new Paragraph(data, targetColor)); } // endregion // region getters and setters public void setWindowSize(float size) { this.windowSize = size; paraGraphsToWrite.clear(); for (int i = 0; i < rawParagraphs.size(); i++) { paraGraphsToWrite.addAll(wrapText(rawParagraphs.get(i).text, rawParagraphs.get(i).color)); } } public final void clearText() { paraGraphsToWrite.clear(); rawParagraphs.clear(); } public final void setAllign(Allign allignment) { this.allignment = allignment; } public final void setAnchor(Anchor anchor) { this.anchor = anchor; } public final void setFont(BitmapFont font) { this.font = font; } public final void setFont(String fontPath) { font = getFont(fontPath); } public final BitmapFont getFont() { return font; } public final void setAlpha(float alpha) { if (font.getColor().a == alpha) return; alpha = Math.max(0, Math.min(1, alpha)); this.alpha = alpha; color.a = alpha; font.setColor(color); } public final void setScale(float scale) { this.scale = Math.max(0.1f, Math.min(10, scale)); font.setScale(this.scale); } public final void setColor(Color color) { color.a = alpha; this.color = color; font.setColor(color); } // endregion @Override public final void update() { float newX, newY; if (alpha == 0) return; float currLineOffset = 0; for (int i = 0; i < paraGraphsToWrite.size(); i++) { if (allignment.equals(Allign.Center)) { newX = pos.getX() - font.getBounds(paraGraphsToWrite.get(i).text).width / 2; } else if (allignment.equals(Allign.Left)) { newX = pos.getX(); } else { newX = pos.getX() - font.getBounds(paraGraphsToWrite.get(i).text).width; } if (anchor.equals(Anchor.Top)) { newY = pos.getY(); newY -= currLineOffset; currLineOffset += font.getBounds(paraGraphsToWrite.get(i).text).height; currLineOffset += lineSpacing; } else if (anchor.equals(Anchor.Middle)) { newY = pos.getY() + font.getBounds(paraGraphsToWrite.get(i).text).height / 2; if (paraGraphsToWrite.size() != 0) newY += (((paraGraphsToWrite.size() - 1) / 2f) - i) * (font.getBounds("X").height + lineSpacing); } else { newY = pos.getY() + font.getBounds(paraGraphsToWrite.get(i).text).height / 2; newY += currLineOffset; currLineOffset += font.getBounds(paraGraphsToWrite.get(i).text).height; currLineOffset += lineSpacing; } setColor(paraGraphsToWrite.get(i).color); color.a = alpha; font.draw(gameBatch.spriteBatch, paraGraphsToWrite.get(i).text, (int) newX, (int) newY); } bounds.set(windowSize, font.getBounds("X").height * paraGraphsToWrite.size() + (paraGraphsToWrite.size()) * lineSpacing); } @Override public void dispose() { paraGraphsToWrite = null; rawParagraphs = null; bounds = null; color = null; font = null; } // endregion methods }