Support rendering text

This commit is contained in:
drdev
2014-02-20 06:31:40 +00:00
parent 4f8babf927
commit 04d336f674
11 changed files with 170 additions and 67 deletions

3
.gitattributes vendored
View File

@@ -15983,12 +15983,14 @@ forge-m-android/src/com/forge/Main.java -text
forge-m-base/.classpath -text
forge-m-base/.project -text
forge-m-base/.settings/org.eclipse.jdt.core.prefs -text
forge-m-base/libs/gdx-freetype.jar -text
forge-m-base/libs/gdx-sources.jar -text
forge-m-base/libs/gdx.jar -text
forge-m-base/src/forge/FScreen.java -text
forge-m-base/src/forge/Forge.java -text
forge-m-base/src/forge/assets/FSkin.java -text
forge-m-base/src/forge/assets/FSkinColor.java -text
forge-m-base/src/forge/assets/FSkinFont.java -text
forge-m-base/src/forge/assets/FSkinImage.java -text
forge-m-base/src/forge/screens/home/HomeScreen.java -text
forge-m-base/src/forge/toolbox/FButton.java -text
@@ -15999,6 +16001,7 @@ forge-m-desktop/.project -text
forge-m-desktop/.settings/org.eclipse.jdt.core.prefs -text
forge-m-desktop/libs/gdx-backend-lwjgl-natives.jar -text
forge-m-desktop/libs/gdx-backend-lwjgl.jar -text
forge-m-desktop/libs/gdx-freetype-natives.jar -text
forge-m-desktop/libs/gdx-natives.jar -text
forge-m-desktop/src/forge/view/Main.java -text
forge-net/.classpath -text

View File

@@ -9,5 +9,6 @@
<classpathentry kind="lib" path="/forge-m-base/libs/gdx.jar" sourcepath="/forge-m-base/libs/gdx-sources.jar"/>
<classpathentry combineaccessrules="false" exported="true" kind="src" path="/forge-m-base"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="lib" path="/forge-m-base/libs/gdx-freetype.jar"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>

View File

@@ -3,5 +3,6 @@
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry exported="true" kind="lib" path="libs/gdx.jar" sourcepath="libs/gdx-sources.jar"/>
<classpathentry kind="lib" path="libs/gdx-freetype.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

Binary file not shown.

View File

@@ -5,10 +5,14 @@ import java.util.Stack;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment;
import forge.assets.FSkin;
import forge.assets.FSkinColor;
import forge.assets.FSkinFont;
import forge.assets.FSkinImage;
import forge.screens.home.HomeScreen;
@@ -93,6 +97,31 @@ public class Forge extends Game {
batch.draw(image, adjustX(x), adjustY(y, h), w, h);
}
public void drawText(String text, FSkinFont skinFont, FSkinColor skinColor, float x, float y, float w, float h, boolean wrap, boolean centerHorizontally, boolean centerVertically) {
BitmapFont font = skinFont.getFont();
font.setColor(skinColor.getColor());
if (wrap) {
float textHeight = font.getWrappedBounds(text, w).height;
if (h > textHeight && centerVertically) {
y += (h - textHeight) / 2;
}
else if (h == 0) {
h = textHeight;
}
font.drawWrapped(batch, text, adjustX(x), adjustY(y, h), w, centerHorizontally ? HAlignment.CENTER : HAlignment.LEFT);
}
else {
float textHeight = font.getMultiLineBounds(text).height;
if (h > textHeight && centerVertically) {
y += (h - textHeight) / 2;
}
else if (h == 0) {
h = textHeight;
}
font.drawMultiLine(batch, text, adjustX(x), adjustY(y, 0), w, centerHorizontally ? HAlignment.CENTER : HAlignment.LEFT);
}
}
private float adjustX(float x) {
return x + offsetX;
}

View File

@@ -1,6 +1,5 @@
package forge.assets;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@@ -9,6 +8,7 @@ import java.util.Map;
import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
@@ -19,17 +19,14 @@ public class FSkin {
private static final String
FILE_SKINS_DIR = "skins/",
FILE_AVATAR_SPRITE = "sprite_avatars.png",
FILE_FONT = "font1.ttf",
DEFAULT_DIR = FILE_SKINS_DIR + "default/";
private static final Map<FSkinImage, TextureRegion> images = new HashMap<FSkinImage, TextureRegion>();
private static final Map<Integer, TextureRegion> avatars = new HashMap<Integer, TextureRegion>();
private static ArrayList<String> allSkins;
private static int currentSkinIndex;
private static String preferredDir;
private static String preferredName;
private static int defaultFontSize = 12;
private static boolean loaded = false;
public static void changeSkin(final String skinName) {
@@ -68,8 +65,6 @@ public class FSkin {
images.clear();
currentSkinIndex = allSkins.indexOf(skinName);
// Non-default (preferred) skin name and dir.
preferredName = skinName.toLowerCase().replace(' ', '_');
preferredDir = FILE_SKINS_DIR + preferredName + "/";
@@ -93,10 +88,10 @@ public class FSkin {
images.put(FSkinImage.BG_SPLASH, new TextureRegion(texture, image.getX(), image.getY(),
image.getWidth(w), image.getHeight(h)));
/*UIManager.put("ProgressBar.background", FSkin.getColorFromPixel(img.getRGB(25, h - 75)));
UIManager.put("ProgressBar.selectionBackground", FSkin.getColorFromPixel(img.getRGB(75, h - 75)));
UIManager.put("ProgressBar.foreground", FSkin.getColorFromPixel(img.getRGB(25, h - 25)));
UIManager.put("ProgressBar.selectionForeground", FSkin.getColorFromPixel(img.getRGB(75, h - 25)));
/*UIManager.put("ProgressBar.background", new Color(img.getRGB(25, h - 75)));
UIManager.put("ProgressBar.selectionBackground", new Color(img.getRGB(75, h - 75)));
UIManager.put("ProgressBar.foreground", new Color(img.getRGB(25, h - 25)));
UIManager.put("ProgressBar.selectionForeground", new Color(img.getRGB(75, h - 25)));
UIManager.put("ProgressBar.border", new LineBorder(Color.BLACK, 0));*/
}
catch (final Exception e) {
@@ -167,7 +162,7 @@ public class FSkin {
//update colors
for (final FSkinColor.Colors c : FSkinColor.Colors.values()) {
c.setColor(FSkin.getColorFromPixel(preferredIcons.getPixel(c.getX(), c.getY())));
c.setColor(new Color(preferredIcons.getPixel(c.getX(), c.getY())));
}
//add images besides splash background
@@ -199,8 +194,8 @@ public class FSkin {
for (int j = 0; j < ph; j += 100) {
for (int i = 0; i < pw; i += 100) {
if (i == 0 && j == 0) { continue; }
pxTest = FSkin.getColorFromPixel(pxPreferredAvatars.getPixel(i + 50, j + 50));
if (pxTest.getAlpha() == 0) { continue; }
pxTest = new Color(pxPreferredAvatars.getPixel(i + 50, j + 50));
if (pxTest.a == 0) { continue; }
FSkin.avatars.put(counter++, new TextureRegion(txPreferredAvatars, i, j, 100, 100));
}
}
@@ -213,8 +208,8 @@ public class FSkin {
for (int j = 0; j < ah; j += 100) {
for (int i = 0; i < aw; i += 100) {
if (i == 0 && j == 0) { continue; }
pxTest = FSkin.getColorFromPixel(pxDefaultAvatars.getPixel(i + 50, j + 50));
if (pxTest.getAlpha() == 0) { continue; }
pxTest = new Color(pxDefaultAvatars.getPixel(i + 50, j + 50));
if (pxTest.a == 0) { continue; }
FSkin.avatars.put(counter++, new TextureRegion(txDefaultAvatars, i, j, 100, 100));
}
}
@@ -232,11 +227,13 @@ public class FSkin {
e.printStackTrace();
}
// Initialize fonts
//SkinFont.setBaseFont(GuiUtils.newFont(FILE_SKINS_DIR + preferredName + "/" + FILE_FONT));
// Update fonts if needed
if (!onInit) {
FSkinFont.updateAll();
}
// Run through enums and load their coords.
//Colors.updateAll();
FSkinColor.updateAll();
// Images loaded; can start UI init.
//FView.SINGLETON_INSTANCE.setSplashProgessBarMessage("Creating display components.");
@@ -283,11 +280,8 @@ public class FSkin {
private static TextureRegion loadTextureRegion(FSkinImage image, Map<String, Texture> textures, Map<String, Pixmap> pixmaps) {
String filename = image.getSourceFile().getFilename();
String preferredFile = preferredDir + filename;
Texture texture = null;
if (textures.containsKey(preferredFile)) {
texture = textures.get(preferredFile);
}
else {
Texture texture = textures.get(preferredFile);
if (texture == null) {
FileHandle file = Gdx.files.internal(preferredFile);
if (file.exists()) {
try {
@@ -307,9 +301,11 @@ public class FSkin {
int w0 = image.getWidth(fullWidth);
int h0 = image.getHeight(fullHeight);
if (pixmaps.containsKey(preferredFile)) {
Pixmap pixmap = pixmaps.get(preferredFile);
Pixmap pixmap = pixmaps.get(preferredFile);
if (pixmap == null) { //return region for preferred file if no pixmap
return new TextureRegion(texture, x0, y0, w0, h0);
}
else {
// Test if requested sub-image in inside bounds of preferred sprite.
// (Height and width of preferred sprite were set in loadFontAndImages.)
if (x0 + w0 <= fullWidth && y0 + h0 <= fullHeight) {
@@ -321,38 +317,33 @@ public class FSkin {
// Center
x = (x0 + w0 / 2);
y = (y0 + h0 / 2);
c = FSkin.getColorFromPixel(pixmap.getPixel(x, y));
if (c.getAlpha() != 0) { return new TextureRegion(texture, x0, y0, w0, h0); }
c = new Color(pixmap.getPixel(x, y));
if (c.a != 0) { return new TextureRegion(texture, x0, y0, w0, h0); }
x += 2;
y += 2;
c = FSkin.getColorFromPixel(pixmap.getPixel(x, y));
if (c.getAlpha() != 0) { return new TextureRegion(texture, x0, y0, w0, h0); }
c = new Color(pixmap.getPixel(x, y));
if (c.a != 0) { return new TextureRegion(texture, x0, y0, w0, h0); }
x -= 4;
c = FSkin.getColorFromPixel(pixmap.getPixel(x, y));
if (c.getAlpha() != 0) { return new TextureRegion(texture, x0, y0, w0, h0); }
c = new Color(pixmap.getPixel(x, y));
if (c.a != 0) { return new TextureRegion(texture, x0, y0, w0, h0); }
y -= 4;
c = FSkin.getColorFromPixel(pixmap.getPixel(x, y));
if (c.getAlpha() != 0) { return new TextureRegion(texture, x0, y0, w0, h0); }
c = new Color(pixmap.getPixel(x, y));
if (c.a != 0) { return new TextureRegion(texture, x0, y0, w0, h0); }
x += 4;
c = FSkin.getColorFromPixel(pixmap.getPixel(x, y));
if (c.getAlpha() != 0) { return new TextureRegion(texture, x0, y0, w0, h0); }
c = new Color(pixmap.getPixel(x, y));
if (c.a != 0) { return new TextureRegion(texture, x0, y0, w0, h0); }
}
}
else {
return new TextureRegion(texture, x0, y0, w0, h0);
}
}
//use default file if can't use preferred file
String defaultFile = DEFAULT_DIR + filename;
if (textures.containsKey(defaultFile)) {
texture = textures.get(defaultFile);
}
else {
texture = textures.get(defaultFile);
if (texture == null) {
FileHandle file = Gdx.files.internal(defaultFile);
if (file.exists()) {
try {
@@ -380,6 +371,15 @@ public class FSkin {
return FSkin.preferredName;
}
/**
* Gets the directory.
*
* @return Path of directory for the current skin.
*/
public static String getDir() {
return FSkin.preferredDir;
}
/**
* Gets the skins.
*
@@ -423,13 +423,4 @@ public class FSkin {
}
public static boolean isLoaded() { return loaded; }
private static Color getColorFromPixel(final int pixel) {
int r, g, b, a;
a = (pixel >> 24) & 0x000000ff;
r = (pixel >> 16) & 0x000000ff;
g = (pixel >> 8) & 0x000000ff;
b = (pixel) & 0x000000ff;
return new Color(r, g, b, a);
}
}

View File

@@ -1,8 +1,9 @@
package forge.assets;
import java.awt.Color;
import java.util.HashMap;
import com.badlogic.gdx.graphics.Color;
public class FSkinColor {
public enum Colors {
CLR_THEME (70, 10),
@@ -120,12 +121,12 @@ public class FSkinColor {
if (this.brightnessDelta != NO_BRIGHTNESS_DELTA) {
if (this.brightnessDelta < 0) {
for (int i = 0; i > this.brightnessDelta; i--) {
this.color = this.color.darker();
this.color = FSkinColor.stepColor(this.color, 10);
}
}
else {
for (int i = 0; i < this.brightnessDelta; i++) {
this.color = this.color.brighter();
this.color = FSkinColor.stepColor(this.color, -10);
}
}
}
@@ -149,9 +150,9 @@ public class FSkinColor {
* @return {@link java.awt.Color}
*/
public static Color stepColor(Color clr0, int step) {
int r = clr0.getRed();
int g = clr0.getGreen();
int b = clr0.getBlue();
float r = clr0.r;
float g = clr0.g;
float b = clr0.b;
// Darker
if (step < 0) {
@@ -165,7 +166,7 @@ public class FSkinColor {
b = ((b + step < 255) ? b + step : 255);
}
return new Color(r, g, b);
return new Color(r, g, b, 0);
}
/** Returns RGB components of a color, with a new
@@ -176,7 +177,7 @@ public class FSkinColor {
* @return {@link java.awt.Color}
*/
public static Color alphaColor(Color clr0, int alpha) {
return new Color(clr0.getRed(), clr0.getGreen(), clr0.getBlue(), alpha);
return new Color(clr0.r, clr0.g, clr0.b, alpha);
}
/**
@@ -184,9 +185,9 @@ public class FSkinColor {
*/
public static boolean isColorBright(Color c) {
int v = (int)Math.sqrt(
c.getRed() * c.getRed() * 0.241 +
c.getGreen() * c.getGreen() * 0.691 +
c.getBlue() * c.getBlue() * 0.068);
c.r * c.r * 0.241 +
c.g * c.g * 0.691 +
c.b * c.b * 0.068);
return v >= 130;
}

View File

@@ -0,0 +1,74 @@
package forge.assets;
import java.util.HashMap;
import java.util.Map;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
public class FSkinFont {
private static final String TTF_FILE = "font1.ttf";
private static final int defaultFontSize = 12;
private static final Map<Integer, FSkinFont> fonts = new HashMap<Integer, FSkinFont>();
public static FSkinFont get() {
return get(defaultFontSize);
}
public static FSkinFont get(final int size0) {
FSkinFont skinFont = fonts.get(size0);
if (skinFont == null) {
skinFont = new FSkinFont(size0);
fonts.put(size0, skinFont);
}
return skinFont;
}
private final int size;
private BitmapFont font;
private FSkinFont(final int size0) {
this.size = size0;
this.updateFont();
}
public int getSize() {
return this.size;
}
public BitmapFont getFont() {
return font;
}
private void updateFont() {
String dir = FSkin.getDir();
String fntFilename = "font" + this.size;
//attempt to use existing .fnt and .png files
FileHandle fntFile = Gdx.files.internal(dir + fntFilename + ".fnt");
if (fntFile.exists()) {
FileHandle pngFile = Gdx.files.internal(dir + fntFilename + ".png");
if (pngFile.exists()) {
font = new BitmapFont(fntFile, pngFile, false);
return;
}
}
//generate .fnt and .png files from .ttf if needed
FileHandle ttfFile = Gdx.files.internal(dir + TTF_FILE);
if (ttfFile.exists()) {
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(ttfFile);
font = generator.generateFont(this.size);
//TODO: Save font to .fnt and .png files for faster loading
generator.dispose();
}
}
public static void updateAll() {
for (FSkinFont skinFont : fonts.values()) {
skinFont.updateFont();
}
}
}

View File

@@ -1,10 +1,9 @@
package forge.toolbox;
import java.awt.AlphaComposite;
import forge.Forge.Graphics;
import forge.assets.FSkinColor;
import forge.assets.FSkinColor.Colors;
import forge.assets.FSkinFont;
import forge.assets.FSkinImage;
public class FButton extends FDisplayObject {
@@ -13,9 +12,9 @@ public class FButton extends FDisplayObject {
private FSkinImage imgL, imgM, imgR;
private String caption;
private FSkinFont font;
private boolean enabled = true;
private boolean toggled = false;
private final AlphaComposite disabledComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.25f);
/**
* Instantiates a new FButton.
@@ -26,7 +25,7 @@ public class FButton extends FDisplayObject {
public FButton(final String caption0) {
caption = caption0;
//setFont(FSkin.getBoldFont(14));
font = FSkinFont.get(14);
resetImg();
}
@@ -93,5 +92,8 @@ public class FButton extends FDisplayObject {
g.drawImage(imgL, 0, 0, h, h);
g.drawImage(imgM, h, 0, w - (2 * h), h);
g.drawImage(imgR, w - h, 0, h, h);
if (!caption.isEmpty()) {
g.drawText(caption, font, foreColor, insetX, 0, w - 2 * insetX, h, false, true, true);
}
}
}

View File

@@ -7,5 +7,6 @@
<classpathentry kind="lib" path="libs/gdx-backend-lwjgl.jar"/>
<classpathentry kind="lib" path="libs/gdx-natives.jar"/>
<classpathentry combineaccessrules="false" kind="src" path="/forge-m-base"/>
<classpathentry kind="lib" path="libs/gdx-freetype-natives.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

Binary file not shown.