Give up on the ShapeRenderer (Band and ThemeCard)

This commit is contained in:
Lonami Exo 2017-01-30 20:34:05 +01:00
parent d8825b0f27
commit 6cff9d5968
6 changed files with 39 additions and 41 deletions

View file

@ -15,7 +15,7 @@ public class Theme {
private String displayName;
private String name;
private int price;
private Color background;
public Color background;
private Color[] cells;
private Color[] buttons;

View file

@ -2,10 +2,12 @@ package io.github.lonamiwebs.klooni.actors;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.utils.Align;
@ -17,8 +19,7 @@ import io.github.lonamiwebs.klooni.game.Scorer;
public class Band extends Actor {
private final Scorer scorer;
private final ShapeRenderer shapeRenderer; // To draw the horizontal "Band"
private final Color bandColor;
private final Texture bandTexture;
public final Rectangle scoreBounds;
public final Rectangle infoBounds;
@ -26,10 +27,14 @@ public class Band extends Actor {
public final Label infoLabel;
public final Label scoreLabel;
public Band(final GameLayout layout, final Scorer aScorer, final Color aBandColor) {
public Band(final GameLayout layout, final Scorer aScorer, final Color bandColor) {
scorer = aScorer;
bandColor = aBandColor;
shapeRenderer = new ShapeRenderer(20); // Only 20 vertex are required, maybe less
Pixmap pixmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
pixmap.setColor(bandColor);
pixmap.fill();
bandTexture = new Texture(pixmap);
pixmap.dispose();
Label.LabelStyle scoreStyle = new Label.LabelStyle();
scoreStyle.font = new BitmapFont(Gdx.files.internal("font/geosans-light.fnt"));
@ -50,20 +55,13 @@ public class Band extends Actor {
@Override
public void draw(Batch batch, float parentAlpha) {
// We need to end (thus flush) the batch or things will mess up!
batch.end();
float w = getWidth();
float h = getHeight();
// TODO This is not the best way to apply the transformation, but, oh well
float x = getParent().getX();
float y = getParent().getY();
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(bandColor);
shapeRenderer.rect(x + getX(), y + getY(), w, h);
shapeRenderer.end();
batch.begin();
// TODO For some strange reason, the texture coordinates and label coordinates are different
Vector2 pos = localToStageCoordinates(new Vector2(x, y));
batch.draw(bandTexture, pos.x, pos.y, getWidth(), getHeight());
scoreLabel.setBounds(x + scoreBounds.x, y + scoreBounds.y, scoreBounds.width, scoreBounds.height);
scoreLabel.setText(Integer.toString(scorer.getCurrentScore()));

View file

@ -1,10 +1,10 @@
package io.github.lonamiwebs.klooni.actors;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import io.github.lonamiwebs.klooni.Theme;
@ -13,34 +13,30 @@ import io.github.lonamiwebs.klooni.game.GameLayout;
public class ThemeCard extends Actor {
private final ShapeRenderer shapeRenderer;
public final Theme theme;
private final Texture background;
public ThemeCard(final GameLayout layout, final Theme theme) {
shapeRenderer = new ShapeRenderer(20);
// TODO We could also use white color and then batch.setColor(theme.background)
Pixmap pixmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
pixmap.setColor(theme.background);
pixmap.fill();
background = new Texture(pixmap);
pixmap.dispose();
this.theme = theme;
layout.update(this);
setWidth(Gdx.graphics.getWidth());
setScaleX(200);
}
@Override
public void draw(Batch batch, float parentAlpha) {
batch.end();
final float x = getX(), y = getY();
Vector2 pos = localToStageCoordinates(new Vector2(x, y));
// TODO Something is wrong with this code, shape renderer fills one yes one no if multiple themes
Gdx.gl.glEnable(GL20.GL_BLEND);
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(0.5f, 0.5f, 0.5f, 0.3f);
shapeRenderer.rect(pos.x, pos.y, getWidth(), getHeight());
shapeRenderer.end();
Gdx.gl.glDisable(GL20.GL_BLEND);
batch.begin();
batch.setColor(Color.WHITE);
batch.draw(background, x, y, getWidth(), getHeight());
// Consider 5 cells on the available size (1/5 height each)
// Do not draw on the borders to add some padding, colors used:
// 0 7 7

View file

@ -92,6 +92,6 @@ public class GameLayout {
}
public void update(ThemeCard card) {
card.setSize(availableWidth, logoHeight);
card.setSize(availableWidth - marginWidth, scoreHeight);
}
}

View file

@ -102,7 +102,7 @@ public class CustomizeScreen implements Screen {
}
themesGroup.space(8);
table.add(new ScrollPane(themesGroup)).expandY();
table.add(new ScrollPane(themesGroup)).expand().fill();
}
@Override

View file

@ -27,12 +27,16 @@ public class PauseMenuStage extends Stage {
private boolean shown;
private boolean hiding;
private final ShapeRenderer shapeRenderer;
private final Band band;
private final Scorer scorer;
public PauseMenuStage(final GameLayout layout, final Klooni game, final Scorer aScorer) {
scorer = aScorer;
shapeRenderer = new ShapeRenderer(20); // 20 vertex seems to be enough for a rectangle
Table table = new Table();
table.setFillParent(true);
addActor(table);
@ -133,8 +137,8 @@ public class PauseMenuStage extends Stage {
@Override
public void draw() {
// Draw an overlay rectangle with not all the opacity
// This is the only place where ShapeRenderer is OK because the batch hasn't started
if (shown) {
ShapeRenderer shapeRenderer = new ShapeRenderer(20);
Gdx.gl.glEnable(GL20.GL_BLEND);
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(1f, 1f, 1f, 0.3f);