Flesh out FLabel for mobile more

This commit is contained in:
drdev
2014-02-22 21:07:44 +00:00
parent 739462e4fa
commit b48ace0cee
5 changed files with 176 additions and 20 deletions

View File

@@ -247,20 +247,73 @@ public class Forge implements ApplicationListener {
bounds = parentBounds; bounds = parentBounds;
} }
public void drawRect(FSkinColor skinColor, float x, float y, float w, float h) {
drawRect(skinColor.getColor(), x, y, w, h);
}
public void drawRect(Color color, float x, float y, float w, float h) {
batch.end(); //must pause batch while rendering shapes
shapeRenderer.begin(ShapeType.Line);
shapeRenderer.setColor(color);
shapeRenderer.rect(x, y, w, h);
shapeRenderer.end();
batch.begin();
}
public void fillRect(FSkinColor skinColor, float x, float y, float w, float h) { public void fillRect(FSkinColor skinColor, float x, float y, float w, float h) {
fillRect(skinColor.getColor(), x, y, w, h); fillRect(skinColor.getColor(), x, y, w, h);
} }
public void fillRect(Color color, float x, float y, float w, float h) { public void fillRect(Color color, float x, float y, float w, float h) {
batch.end(); //must pause batch while rendering shapes batch.end(); //must pause batch while rendering shapes
if (color.a != 0) { //enable blending so alpha colored shapes work properly boolean needBlending = (color.a != 0);
if (needBlending) { //enable blending so alpha colored shapes work properly
Gdx.gl.glEnable(GL20.GL_BLEND); Gdx.gl.glEnable(GL20.GL_BLEND);
} }
shapeRenderer.begin(ShapeType.Filled); shapeRenderer.begin(ShapeType.Filled);
shapeRenderer.setColor(color); shapeRenderer.setColor(color);
shapeRenderer.rect(x, y, w, h); shapeRenderer.rect(x, y, w, h);
shapeRenderer.end(); shapeRenderer.end();
if (needBlending) {
Gdx.gl.glDisable(GL20.GL_BLEND);
}
batch.begin();
}
public void fillGradientRect(FSkinColor skinColor1, FSkinColor skinColor2, boolean vertical, float x, float y, float w, float h) {
fillGradientRect(skinColor1.getColor(), skinColor2.getColor(), vertical, x, y, w, h);
}
public void fillGradientRect(FSkinColor skinColor1, Color color2, boolean vertical, float x, float y, float w, float h) {
fillGradientRect(skinColor1.getColor(), color2, vertical, x, y, w, h);
}
public void fillGradientRect(Color color1, FSkinColor skinColor2, boolean vertical, float x, float y, float w, float h) {
fillGradientRect(color1, skinColor2.getColor(), vertical, x, y, w, h);
}
public void fillGradientRect(Color color1, Color color2, boolean vertical, float x, float y, float w, float h) {
batch.end(); //must pause batch while rendering shapes
boolean needBlending = (color1.a != 0 || color2.a != 0);
if (needBlending) { //enable blending so alpha colored shapes work properly
Gdx.gl.glEnable(GL20.GL_BLEND);
}
Color topLeftColor = color1;
Color topRightColor = vertical ? color1 : color2;
Color bottomLeftColor = vertical ? color2 : color1;
Color bottomRightColor = color2;
shapeRenderer.begin(ShapeType.Filled);
shapeRenderer.rect(x, y, w, h, bottomLeftColor, bottomRightColor, topRightColor, topLeftColor);
shapeRenderer.end();
if (needBlending) {
Gdx.gl.glDisable(GL20.GL_BLEND);
}
batch.begin(); batch.begin();
} }
@@ -277,7 +330,7 @@ public class Forge implements ApplicationListener {
batch.draw(image, adjustX(x), adjustY(y, h), w, h); 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) { public void drawText(String text, FSkinFont skinFont, FSkinColor skinColor, float x, float y, float w, float h, boolean wrap, HAlignment horzAlignment, boolean centerVertically) {
BitmapFont font = skinFont.getFont(); BitmapFont font = skinFont.getFont();
font.setColor(skinColor.getColor()); font.setColor(skinColor.getColor());
if (wrap) { if (wrap) {
@@ -288,7 +341,7 @@ public class Forge implements ApplicationListener {
else if (h == 0) { else if (h == 0) {
h = textHeight; h = textHeight;
} }
font.drawWrapped(batch, text, adjustX(x), adjustY(y, h), w, centerHorizontally ? HAlignment.CENTER : HAlignment.LEFT); font.drawWrapped(batch, text, adjustX(x), adjustY(y, h), w, horzAlignment);
} }
else { else {
float textHeight = font.getMultiLineBounds(text).height; float textHeight = font.getMultiLineBounds(text).height;
@@ -298,7 +351,7 @@ public class Forge implements ApplicationListener {
else if (h == 0) { else if (h == 0) {
h = textHeight; h = textHeight;
} }
font.drawMultiLine(batch, text, adjustX(x), adjustY(y, 0), w, centerHorizontally ? HAlignment.CENTER : HAlignment.LEFT); font.drawMultiLine(batch, text, adjustX(x), adjustY(y, 0), w, horzAlignment);
} }
} }

View File

@@ -1,8 +1,11 @@
package forge.assets; package forge.assets;
import com.badlogic.gdx.math.Vector2;
import forge.Forge.Graphics; import forge.Forge.Graphics;
public interface FImage { public interface FImage {
Vector2 getSize();
void draw(Graphics g, float x, float y); void draw(Graphics g, float x, float y);
void draw(Graphics g, float x, float y, float w, float h); void draw(Graphics g, float x, float y, float w, float h);
} }

View File

@@ -1,5 +1,8 @@
package forge.assets; package forge.assets;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import forge.Forge.Graphics; import forge.Forge.Graphics;
/** Properties of various components that make up the skin. /** Properties of various components that make up the skin.
@@ -280,6 +283,12 @@ public enum FSkinImage implements FImage {
return sourceFile; return sourceFile;
} }
@Override
public Vector2 getSize() {
TextureRegion tr = FSkin.getImages().get(this);
return new Vector2(tr.getRegionWidth(), tr.getRegionHeight());
}
@Override @Override
public void draw(Graphics g, float x, float y) { public void draw(Graphics g, float x, float y) {
g.drawImage(FSkin.getImages().get(this), x, y); g.drawImage(FSkin.getImages().get(this), x, y);

View File

@@ -1,5 +1,7 @@
package forge.toolbox; package forge.toolbox;
import com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment;
import forge.Forge.Graphics; import forge.Forge.Graphics;
import forge.assets.FSkinColor; import forge.assets.FSkinColor;
import forge.assets.FSkinColor.Colors; import forge.assets.FSkinColor.Colors;
@@ -119,7 +121,7 @@ public class FButton extends FDisplayObject {
g.drawImage(imgM, h, 0, w - (2 * h), h); g.drawImage(imgM, h, 0, w - (2 * h), h);
g.drawImage(imgR, w - h, 0, h, h); g.drawImage(imgR, w - h, 0, h, h);
if (!caption.isEmpty()) { if (!caption.isEmpty()) {
g.drawText(caption, font, foreColor, insetX, 0, w - 2 * insetX, h, false, true, true); g.drawText(caption, font, foreColor, insetX, 0, w - 2 * insetX, h, false, HAlignment.CENTER, true);
} }
} }
} }

View File

@@ -4,6 +4,7 @@ import com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import forge.Forge.Graphics; import forge.Forge.Graphics;
import forge.assets.FImage;
import forge.assets.FSkinColor; import forge.assets.FSkinColor;
import forge.assets.FSkinColor.Colors; import forge.assets.FSkinColor.Colors;
import forge.assets.FSkinFont; import forge.assets.FSkinFont;
@@ -12,9 +13,9 @@ import forge.assets.FSkinImage;
public class FLabel extends FDisplayObject { public class FLabel extends FDisplayObject {
public static class Builder { public static class Builder {
//========== Default values for FLabel are set here. //========== Default values for FLabel are set here.
private double bldIconScaleFactor = 0.8; private float bldIconScaleFactor = 0.8f;
private int bldFontSize = 14; private int bldFontSize = 14;
private HAlignment bldFontAlign = HAlignment.LEFT; private HAlignment bldTextAlignX = HAlignment.LEFT;
private HAlignment bldIconAlignX = HAlignment.LEFT; private HAlignment bldIconAlignX = HAlignment.LEFT;
private Vector2 bldIconInsets = new Vector2(0, 0); private Vector2 bldIconInsets = new Vector2(0, 0);
@@ -26,7 +27,7 @@ public class FLabel extends FDisplayObject {
private boolean bldEnabled = true; private boolean bldEnabled = true;
private String bldText; private String bldText;
private FSkinImage bldIcon; private FImage bldIcon;
private Runnable bldCommand; private Runnable bldCommand;
public FLabel build() { return new FLabel(this); } public FLabel build() { return new FLabel(this); }
@@ -34,7 +35,7 @@ public class FLabel extends FDisplayObject {
// Begin builder methods. // Begin builder methods.
public Builder text(final String s0) { this.bldText = s0; return this; } public Builder text(final String s0) { this.bldText = s0; return this; }
public Builder icon(final FSkinImage i0) { this.bldIcon = i0; return this; } public Builder icon(final FSkinImage i0) { this.bldIcon = i0; return this; }
public Builder fontAlign(final HAlignment a0) { this.bldFontAlign = a0; return this; } public Builder fontAlign(final HAlignment a0) { this.bldTextAlignX = a0; return this; }
public Builder opaque(final boolean b0) { this.bldOpaque = b0; return this; } public Builder opaque(final boolean b0) { this.bldOpaque = b0; return this; }
public Builder opaque() { opaque(true); return this; } public Builder opaque() { opaque(true); return this; }
public Builder selectable(final boolean b0) { this.bldSelectable = b0; return this; } public Builder selectable(final boolean b0) { this.bldSelectable = b0; return this; }
@@ -45,7 +46,7 @@ public class FLabel extends FDisplayObject {
public Builder fontSize(final int i0) { this.bldFontSize = i0; return this; } public Builder fontSize(final int i0) { this.bldFontSize = i0; return this; }
public Builder enabled(final boolean b0) { this.bldEnabled = b0; return this; } public Builder enabled(final boolean b0) { this.bldEnabled = b0; return this; }
public Builder iconScaleAuto(final boolean b0) { this.bldIconScaleAuto = b0; return this; } public Builder iconScaleAuto(final boolean b0) { this.bldIconScaleAuto = b0; return this; }
public Builder iconScaleFactor(final double d0) { this.bldIconScaleFactor = d0; return this; } public Builder iconScaleFactor(final float f0) { this.bldIconScaleFactor = f0; return this; }
public Builder iconInBackground(final boolean b0) { this.bldIconInBackground = b0; return this; } public Builder iconInBackground(final boolean b0) { this.bldIconInBackground = b0; return this; }
public Builder iconInBackground() { iconInBackground(true); return this; } public Builder iconInBackground() { iconInBackground(true); return this; }
public Builder iconAlignX(final HAlignment a0) { this.bldIconAlignX = a0; return this; } public Builder iconAlignX(final HAlignment a0) { this.bldIconAlignX = a0; return this; }
@@ -68,21 +69,21 @@ public class FLabel extends FDisplayObject {
private static final FSkinColor l20 = clrMain.stepColor(20); private static final FSkinColor l20 = clrMain.stepColor(20);
private static final FSkinColor l30 = clrMain.stepColor(30); private static final FSkinColor l30 = clrMain.stepColor(30);
private double iconScaleFactor; private float iconScaleFactor;
private FSkinFont font; private FSkinFont font;
private HAlignment fontAlign, iconAlignX; private HAlignment textAlignX, iconAlignX;
private Vector2 iconInsets; private Vector2 iconInsets;
private boolean selectable, selected, opaque, iconInBackground, iconScaleAuto, enabled; private boolean selectable, selected, opaque, iconInBackground, iconScaleAuto, pressed;
private String text; private String text;
private FSkinImage icon; private FImage icon;
private Runnable command; private Runnable command;
// Call this using FLabel.Builder()... // Call this using FLabel.Builder()...
protected FLabel(final Builder b0) { protected FLabel(final Builder b0) {
iconScaleFactor = b0.bldIconScaleFactor; iconScaleFactor = b0.bldIconScaleFactor;
font = FSkinFont.get(b0.bldFontSize); font = FSkinFont.get(b0.bldFontSize);
fontAlign = b0.bldFontAlign; textAlignX = b0.bldTextAlignX;
iconAlignX = b0.bldIconAlignX; iconAlignX = b0.bldIconAlignX;
iconInsets = b0.bldIconInsets; iconInsets = b0.bldIconInsets;
selectable = b0.bldSelectable; selectable = b0.bldSelectable;
@@ -90,16 +91,52 @@ public class FLabel extends FDisplayObject {
opaque = b0.bldOpaque; opaque = b0.bldOpaque;
iconInBackground = b0.bldIconInBackground; iconInBackground = b0.bldIconInBackground;
iconScaleAuto = b0.bldIconScaleAuto; iconScaleAuto = b0.bldIconScaleAuto;
setEnabled(b0.bldEnabled); text = b0.bldText != null ? b0.bldText : "";
icon = b0.bldIcon;
command = b0.bldCommand;
setEnabled(b0.bldEnabled);
} }
public boolean getSelected() { public boolean getSelected() {
return this.selected; return selected;
} }
public void setSelected(final boolean b0) { public void setSelected(final boolean b0) {
this.selected = b0; selected = b0;
} }
public String getText() {
return text;
}
public void setText(final String text0) {
text = text0;
}
public FImage getIcon() {
return icon;
}
public void setIcon(final FImage icon0) {
icon = icon0;
}
@Override
public boolean touchDown(float x, float y) {
if (opaque || selectable) {
pressed = true;
return true;
}
return false;
}
@Override
public boolean touchUp(float x, float y) {
if (pressed) {
pressed = false;
return true;
}
return false;
}
@Override
public boolean tap(float x, float y, int count) { public boolean tap(float x, float y, int count) {
boolean handled = false; boolean handled = false;
if (selectable) { if (selectable) {
@@ -115,7 +152,59 @@ public class FLabel extends FDisplayObject {
@Override @Override
public void draw(Graphics g) { public void draw(Graphics g) {
// TODO Auto-generated method stub float w = getWidth();
float h = getHeight();
if (pressed) {
g.fillGradientRect(d50, d10, true, 0, 0, w - 1, h - 1);
g.drawRect(d50, 0, 0, w - 2, h - 2);
g.drawRect(d10, 1, 1, w - 4, h - 4);
}
else if (selected && (opaque || selectable)) {
g.fillGradientRect(d30, l10, true, 0, 0, w - 1, h - 1);
g.drawRect(d30, 0, 0, w - 2, h - 2);
g.drawRect(l10, 1, 1, w - 4, h - 4);
}
else if (opaque) {
g.fillGradientRect(d10, l20, true, 0, 0, w - 1, h - 1);
g.drawRect(d50, 0, 0, w - 2, h - 2);
g.drawRect(l10, 1, 1, w - 4, h - 4);
}
else if (selectable) {
g.drawRect(l10, 0, 0, w - 2, h - 2);
g.drawRect(l30, 1, 1, w - 4, h - 4);
}
drawContent(g, w, h, pressed);
} }
protected void drawContent(Graphics g, float w, float h, final boolean pressed) {
if (icon != null) {
float x = iconInsets.x;
float y = iconInsets.y;
Vector2 iconSize = icon.getSize();
float iconWidth = iconSize.x;
float iconHeight = iconSize.y;
float aspectRatio = iconWidth / iconHeight;
if (iconInBackground || iconScaleAuto) {
iconHeight = h * iconScaleFactor;
iconWidth = iconHeight * aspectRatio;
}
if (iconInBackground || text.isEmpty()) {
x = iconAlignX == HAlignment.CENTER
? (int) ((w - iconWidth) / 2 + iconInsets.x)
: (int) iconInsets.x;
y = ((h - iconHeight) / 2) + iconInsets.y;
}
else {
x = 0; //TODO: calculation these
y = 0;
}
g.drawImage(icon, x, y, iconWidth, iconHeight);
}
else if (!text.isEmpty()) { //TODO: consider insets for text
g.drawText(text, font, clrText, 0, 0, w, h, false, textAlignX, true);
}
}
} }