From b48ace0cee3fa3be6ab95d906e5cb8dbe447a66f Mon Sep 17 00:00:00 2001 From: drdev Date: Sat, 22 Feb 2014 21:07:44 +0000 Subject: [PATCH] Flesh out FLabel for mobile more --- forge-m-base/src/forge/Forge.java | 61 ++++++++- forge-m-base/src/forge/assets/FImage.java | 3 + forge-m-base/src/forge/assets/FSkinImage.java | 9 ++ forge-m-base/src/forge/toolbox/FButton.java | 4 +- forge-m-base/src/forge/toolbox/FLabel.java | 119 +++++++++++++++--- 5 files changed, 176 insertions(+), 20 deletions(-) diff --git a/forge-m-base/src/forge/Forge.java b/forge-m-base/src/forge/Forge.java index fe2093c1ab8..7c5da0129d1 100644 --- a/forge-m-base/src/forge/Forge.java +++ b/forge-m-base/src/forge/Forge.java @@ -247,20 +247,73 @@ public class Forge implements ApplicationListener { 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) { 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 + boolean needBlending = (color.a != 0); + if (needBlending) { //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(); + 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(); } @@ -277,7 +330,7 @@ public class Forge implements ApplicationListener { 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(); font.setColor(skinColor.getColor()); if (wrap) { @@ -288,7 +341,7 @@ public class Forge implements ApplicationListener { else if (h == 0) { 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 { float textHeight = font.getMultiLineBounds(text).height; @@ -298,7 +351,7 @@ public class Forge implements ApplicationListener { else if (h == 0) { 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); } } diff --git a/forge-m-base/src/forge/assets/FImage.java b/forge-m-base/src/forge/assets/FImage.java index 30e8985f137..f9cf5c0a391 100644 --- a/forge-m-base/src/forge/assets/FImage.java +++ b/forge-m-base/src/forge/assets/FImage.java @@ -1,8 +1,11 @@ package forge.assets; +import com.badlogic.gdx.math.Vector2; + import forge.Forge.Graphics; public interface FImage { + Vector2 getSize(); void draw(Graphics g, float x, float y); void draw(Graphics g, float x, float y, float w, float h); } diff --git a/forge-m-base/src/forge/assets/FSkinImage.java b/forge-m-base/src/forge/assets/FSkinImage.java index 7464fc42155..7223a260f27 100644 --- a/forge-m-base/src/forge/assets/FSkinImage.java +++ b/forge-m-base/src/forge/assets/FSkinImage.java @@ -1,5 +1,8 @@ package forge.assets; +import com.badlogic.gdx.graphics.g2d.TextureRegion; +import com.badlogic.gdx.math.Vector2; + import forge.Forge.Graphics; /** Properties of various components that make up the skin. @@ -280,6 +283,12 @@ public enum FSkinImage implements FImage { return sourceFile; } + @Override + public Vector2 getSize() { + TextureRegion tr = FSkin.getImages().get(this); + return new Vector2(tr.getRegionWidth(), tr.getRegionHeight()); + } + @Override public void draw(Graphics g, float x, float y) { g.drawImage(FSkin.getImages().get(this), x, y); diff --git a/forge-m-base/src/forge/toolbox/FButton.java b/forge-m-base/src/forge/toolbox/FButton.java index 24308dbfa0d..f08cdff746f 100644 --- a/forge-m-base/src/forge/toolbox/FButton.java +++ b/forge-m-base/src/forge/toolbox/FButton.java @@ -1,5 +1,7 @@ package forge.toolbox; +import com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment; + import forge.Forge.Graphics; import forge.assets.FSkinColor; 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(imgR, w - h, 0, h, h); 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); } } } diff --git a/forge-m-base/src/forge/toolbox/FLabel.java b/forge-m-base/src/forge/toolbox/FLabel.java index bb025dda859..3aa2649b615 100644 --- a/forge-m-base/src/forge/toolbox/FLabel.java +++ b/forge-m-base/src/forge/toolbox/FLabel.java @@ -4,6 +4,7 @@ import com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment; import com.badlogic.gdx.math.Vector2; import forge.Forge.Graphics; +import forge.assets.FImage; import forge.assets.FSkinColor; import forge.assets.FSkinColor.Colors; import forge.assets.FSkinFont; @@ -12,9 +13,9 @@ import forge.assets.FSkinImage; public class FLabel extends FDisplayObject { public static class Builder { //========== Default values for FLabel are set here. - private double bldIconScaleFactor = 0.8; + private float bldIconScaleFactor = 0.8f; private int bldFontSize = 14; - private HAlignment bldFontAlign = HAlignment.LEFT; + private HAlignment bldTextAlignX = HAlignment.LEFT; private HAlignment bldIconAlignX = HAlignment.LEFT; private Vector2 bldIconInsets = new Vector2(0, 0); @@ -26,7 +27,7 @@ public class FLabel extends FDisplayObject { private boolean bldEnabled = true; private String bldText; - private FSkinImage bldIcon; + private FImage bldIcon; private Runnable bldCommand; public FLabel build() { return new FLabel(this); } @@ -34,7 +35,7 @@ public class FLabel extends FDisplayObject { // Begin builder methods. public Builder text(final String s0) { this.bldText = s0; 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() { opaque(true); 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 enabled(final boolean b0) { this.bldEnabled = 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() { iconInBackground(true); 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 l30 = clrMain.stepColor(30); - private double iconScaleFactor; + private float iconScaleFactor; private FSkinFont font; - private HAlignment fontAlign, iconAlignX; + private HAlignment textAlignX, iconAlignX; private Vector2 iconInsets; - private boolean selectable, selected, opaque, iconInBackground, iconScaleAuto, enabled; + private boolean selectable, selected, opaque, iconInBackground, iconScaleAuto, pressed; private String text; - private FSkinImage icon; + private FImage icon; private Runnable command; // Call this using FLabel.Builder()... protected FLabel(final Builder b0) { iconScaleFactor = b0.bldIconScaleFactor; font = FSkinFont.get(b0.bldFontSize); - fontAlign = b0.bldFontAlign; + textAlignX = b0.bldTextAlignX; iconAlignX = b0.bldIconAlignX; iconInsets = b0.bldIconInsets; selectable = b0.bldSelectable; @@ -90,16 +91,52 @@ public class FLabel extends FDisplayObject { opaque = b0.bldOpaque; iconInBackground = b0.bldIconInBackground; iconScaleAuto = b0.bldIconScaleAuto; - setEnabled(b0.bldEnabled); + text = b0.bldText != null ? b0.bldText : ""; + icon = b0.bldIcon; + command = b0.bldCommand; + setEnabled(b0.bldEnabled); } public boolean getSelected() { - return this.selected; + return selected; } 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) { boolean handled = false; if (selectable) { @@ -115,7 +152,59 @@ public class FLabel extends FDisplayObject { @Override 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); + } + } }