Fix support for alpha colors

This commit is contained in:
drdev
2014-02-20 07:21:53 +00:00
parent cafff3f379
commit 0cd306d23b
4 changed files with 56 additions and 35 deletions

View File

@@ -3,10 +3,14 @@ package forge;
import com.badlogic.gdx.Screen;
import forge.Forge.Graphics;
import forge.assets.FSkinColor;
import forge.assets.FSkinImage;
import forge.assets.FSkinColor.Colors;
import forge.toolbox.FDisplayObject;
public abstract class FScreen extends FDisplayObject implements Screen {
private static final FSkinColor clrTheme = FSkinColor.get(Colors.CLR_THEME);
@Override
public final void resize(int width, int height) {
setBounds(0, 0, width, height);
@@ -26,6 +30,7 @@ public abstract class FScreen extends FDisplayObject implements Screen {
protected void drawBackground(Graphics g) {
g.drawImage(FSkinImage.BG_TEXTURE, 0, 0, getWidth(), getHeight());
g.fillRect(clrTheme, 0, 0, getWidth(), getHeight());
}
@Override

View File

@@ -4,11 +4,15 @@ import java.util.Stack;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GL20;
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 com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import forge.assets.FSkin;
import forge.assets.FSkinColor;
@@ -20,6 +24,7 @@ public class Forge extends Game {
private static Forge game;
private static int screenHeight;
private static SpriteBatch batch;
private static ShapeRenderer shapeRenderer;
private static final Stack<FScreen> screens = new Stack<FScreen>();
public Forge() {
@@ -32,7 +37,8 @@ public class Forge extends Game {
@Override
public void create() {
batch = new SpriteBatch();
//Gdx.graphics.setContinuousRendering(false); //save power consumption by disabling continuous rendering
shapeRenderer = new ShapeRenderer();
Gdx.graphics.setContinuousRendering(false); //save power consumption by disabling continuous rendering
FSkin.loadLight("journeyman", true);
FSkin.loadFull(true);
@@ -69,6 +75,7 @@ public class Forge extends Game {
public void dispose () {
super.dispose();
batch.dispose();
shapeRenderer.dispose();
}
public static class Graphics {
@@ -84,6 +91,23 @@ public class Forge extends Game {
offsetY = g.offsetY + y;
}
public void fillRect(FSkinColor skinColor, float x, float y, float w, float h) {
fillRect(skinColor.getColor(), x, y, w, h);
}
public void fillRect(Color color, float x, float y, float w, float h) {
batch.end(); //must pause batch while rendering shapes
if (color.a != 0) { //enable blending so alpha colored shapes work properly
Gdx.gl.glEnable(GL20.GL_BLEND);
}
shapeRenderer.begin(ShapeType.Filled);
shapeRenderer.setColor(color);
shapeRenderer.rect(x, y, w, h);
shapeRenderer.end();
batch.begin();
}
public void drawImage(FSkinImage image, float x, float y) {
drawImage(FSkin.getImages().get(image), x, y);
}

View File

@@ -130,7 +130,6 @@ public class FSkin {
avatars.clear();
final Map<String, Texture> textures = new HashMap<String, Texture>();
final Map<String, Pixmap> pixmaps = new HashMap<String, Pixmap>();
//FView.SINGLETON_INSTANCE.setSplashProgessBarMessage("Processing image sprites: ", 5);
@@ -144,11 +143,9 @@ public class FSkin {
try {
textures.put(f1.path(), new Texture(f1));
Pixmap preferredIcons = new Pixmap(f1);
pixmaps.put(f1.path(), preferredIcons);
//FView.SINGLETON_INSTANCE.incrementSplashProgessBar(++p);
textures.put(f2.path(), new Texture(f2));
pixmaps.put(f2.path(), new Pixmap(f2));
Pixmap preferredIcons = new Pixmap(f2);
//FView.SINGLETON_INSTANCE.incrementSplashProgessBar(++p);
textures.put(f3.path(), new Texture(f3));
//FView.SINGLETON_INSTANCE.incrementSplashProgessBar(++p);
@@ -168,7 +165,7 @@ public class FSkin {
//add images besides splash background
for (FSkinImage image : FSkinImage.values()) {
if (image != FSkinImage.BG_SPLASH) {
TextureRegion textureRegion = loadTextureRegion(image, textures, pixmaps);
TextureRegion textureRegion = loadTextureRegion(image, textures, preferredIcons);
if (textureRegion != null) {
images.put(image, textureRegion);
}
@@ -214,9 +211,7 @@ public class FSkin {
}
}
for (Pixmap pixmap : pixmaps.values()) {
pixmap.dispose();
}
preferredIcons.dispose();
pxDefaultAvatars.dispose();
//FView.SINGLETON_INSTANCE.incrementSplashProgessBar(++p);
@@ -277,8 +272,9 @@ public class FSkin {
addEncodingSymbol("T", GameplayImages.IMG_TAP);*/
}
private static TextureRegion loadTextureRegion(FSkinImage image, Map<String, Texture> textures, Map<String, Pixmap> pixmaps) {
String filename = image.getSourceFile().getFilename();
private static TextureRegion loadTextureRegion(FSkinImage image, Map<String, Texture> textures, Pixmap preferredIcons) {
SourceFile sourceFile = image.getSourceFile();
String filename = sourceFile.getFilename();
String preferredFile = preferredDir + filename;
Texture texture = textures.get(preferredFile);
if (texture == null) {
@@ -301,8 +297,7 @@ public class FSkin {
int w0 = image.getWidth(fullWidth);
int h0 = image.getHeight(fullHeight);
Pixmap pixmap = pixmaps.get(preferredFile);
if (pixmap == null) { //return region for preferred file if no pixmap
if (sourceFile != SourceFile.ICONS) { //just return region for preferred file if not icons file
return new TextureRegion(texture, x0, y0, w0, h0);
}
else {
@@ -317,24 +312,24 @@ public class FSkin {
// Center
x = (x0 + w0 / 2);
y = (y0 + h0 / 2);
c = new Color(pixmap.getPixel(x, y));
c = new Color(preferredIcons.getPixel(x, y));
if (c.a != 0) { return new TextureRegion(texture, x0, y0, w0, h0); }
x += 2;
y += 2;
c = new Color(pixmap.getPixel(x, y));
c = new Color(preferredIcons.getPixel(x, y));
if (c.a != 0) { return new TextureRegion(texture, x0, y0, w0, h0); }
x -= 4;
c = new Color(pixmap.getPixel(x, y));
c = new Color(preferredIcons.getPixel(x, y));
if (c.a != 0) { return new TextureRegion(texture, x0, y0, w0, h0); }
y -= 4;
c = new Color(pixmap.getPixel(x, y));
c = new Color(preferredIcons.getPixel(x, y));
if (c.a != 0) { return new TextureRegion(texture, x0, y0, w0, h0); }
x += 4;
c = new Color(pixmap.getPixel(x, y));
c = new Color(preferredIcons.getPixel(x, y));
if (c.a != 0) { return new TextureRegion(texture, x0, y0, w0, h0); }
}
}

View File

@@ -58,7 +58,7 @@ public class FSkinColor {
private final int brightnessDelta;
private final int step;
private final int contrastStep;
private final int alpha;
private final float alpha;
protected Color color;
public Color getColor() { return color; }
@@ -67,7 +67,7 @@ public class FSkinColor {
private FSkinColor(Colors baseColor0) {
this(baseColor0, NO_BRIGHTNESS_DELTA, NO_STEP, NO_STEP, NO_ALPHA);
}
private FSkinColor(Colors baseColor0, int brightnessDelta0, int step0, int contrastStep0, int alpha0) {
private FSkinColor(Colors baseColor0, int brightnessDelta0, int step0, int contrastStep0, float alpha0) {
this.baseColor = baseColor0;
this.brightnessDelta = brightnessDelta0;
this.step = step0;
@@ -76,7 +76,7 @@ public class FSkinColor {
this.updateColor();
}
private FSkinColor getDerivedColor(int brightnessDelta0, int step0, int contrastStep0, int alpha0) {
private FSkinColor getDerivedColor(int brightnessDelta0, int step0, int contrastStep0, float alpha0) {
String key = this.baseColor.name() + "|" + brightnessDelta0 + "|" + step0 + "|" + contrastStep0 + "|" + alpha0;
FSkinColor derivedColor = derivedColors.get(key);
if (derivedColor == null) {
@@ -112,7 +112,7 @@ public class FSkinColor {
return getContrastColor(255);
}
public FSkinColor alphaColor(int alpha0) {
public FSkinColor alphaColor(float alpha0) {
return getDerivedColor(this.brightnessDelta, this.step, this.contrastStep, alpha0);
}
@@ -150,9 +150,9 @@ public class FSkinColor {
* @return {@link java.awt.Color}
*/
public static Color stepColor(Color clr0, int step) {
float r = clr0.r;
float g = clr0.g;
float b = clr0.b;
float r = clr0.r * 255;
float g = clr0.g * 255;
float b = clr0.b * 255;
// Darker
if (step < 0) {
@@ -166,17 +166,14 @@ public class FSkinColor {
b = ((b + step < 255) ? b + step : 255);
}
return new Color(r, g, b, 0);
return new Color(r / 255, g / 255, b / 255, 0);
}
/** Returns RGB components of a color, with a new
* value for alpha. 0 = transparent, 255 = opaque.
*
* @param clr0 {@link java.awt.Color}
* @param alpha int
* @return {@link java.awt.Color}
/**
* Returns RGB components of a color, with a new
* value for alpha. 0f = transparent, 1f = opaque.
*/
public static Color alphaColor(Color clr0, int alpha) {
public static Color alphaColor(Color clr0, float alpha) {
return new Color(clr0.r, clr0.g, clr0.b, alpha);
}
@@ -184,11 +181,11 @@ public class FSkinColor {
* @see http://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx
*/
public static boolean isColorBright(Color c) {
int v = (int)Math.sqrt(
double v = Math.sqrt(
c.r * c.r * 0.241 +
c.g * c.g * 0.691 +
c.b * c.b * 0.068);
return v >= 130;
return v > 0.5;
}
public static Color getHighContrastColor(Color c) {