Fix theme change not being updated everywhere

This commit is contained in:
Lonami Exo 2017-02-08 18:29:28 +01:00
parent da08fb0420
commit 6d2501b022
7 changed files with 47 additions and 49 deletions

View file

@ -3,6 +3,7 @@ package io.github.lonamiwebs.klooni;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.NinePatch; import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton; import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
@ -26,7 +27,7 @@ public class Theme {
public Color currentScore; public Color currentScore;
public Color highScore; public Color highScore;
private Color bandColor; public Color bandColor;
private Color[] cells; private Color[] cells;
private Color[] buttons; private Color[] buttons;
@ -146,11 +147,7 @@ public class Theme {
} }
public Color getCellColor(int colorIndex) { public Color getCellColor(int colorIndex) {
return cells[colorIndex]; return colorIndex < 0 ? emptyCell : cells[colorIndex];
}
public Color getBandColor() {
return bandColor;
} }
public void glClearBackground() { public void glClearBackground() {
@ -164,6 +161,20 @@ public class Theme {
//endregion //endregion
//region Styling utilities
// A 1x1 blank pixel map to be tinted and used in multiple places
public static Texture getBlankTexture() {
final Pixmap pixmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
pixmap.setColor(Color.WHITE);
pixmap.fill();
final Texture result = new Texture(pixmap);
pixmap.dispose();
return result;
}
//endregion
//region Disposal //region Disposal
void dispose() { void dispose() {

View file

@ -1,6 +1,5 @@
package io.github.lonamiwebs.klooni.actors; package io.github.lonamiwebs.klooni.actors;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Rectangle;
@ -10,6 +9,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.utils.Align; import com.badlogic.gdx.utils.Align;
import io.github.lonamiwebs.klooni.Klooni; import io.github.lonamiwebs.klooni.Klooni;
import io.github.lonamiwebs.klooni.Theme;
import io.github.lonamiwebs.klooni.game.BaseScorer; import io.github.lonamiwebs.klooni.game.BaseScorer;
import io.github.lonamiwebs.klooni.game.GameLayout; import io.github.lonamiwebs.klooni.game.GameLayout;
@ -33,13 +33,7 @@ public class Band extends Actor {
public Band(final Klooni game, final GameLayout layout, final BaseScorer scorer) { public Band(final Klooni game, final GameLayout layout, final BaseScorer scorer) {
this.scorer = scorer; this.scorer = scorer;
bandTexture = Theme.getBlankTexture();
// A 1x1 pixel map will be enough since the band texture will then be expanded
Pixmap pixmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
pixmap.setColor(Klooni.theme.getBandColor());
pixmap.fill();
bandTexture = new Texture(pixmap);
pixmap.dispose();
Label.LabelStyle labelStyle = new Label.LabelStyle(); Label.LabelStyle labelStyle = new Label.LabelStyle();
labelStyle.font = game.skin.getFont("font"); labelStyle.font = game.skin.getFont("font");
@ -66,6 +60,7 @@ public class Band extends Actor {
// TODO For some strange reason, the texture coordinates and label coordinates are different // TODO For some strange reason, the texture coordinates and label coordinates are different
Vector2 pos = localToStageCoordinates(new Vector2(x, y)); Vector2 pos = localToStageCoordinates(new Vector2(x, y));
batch.setColor(Klooni.theme.bandColor);
batch.draw(bandTexture, pos.x, pos.y, getWidth(), getHeight()); batch.draw(bandTexture, pos.x, pos.y, getWidth(), getHeight());
scoreLabel.setBounds(x + scoreBounds.x, y + scoreBounds.y, scoreBounds.width, scoreBounds.height); scoreLabel.setBounds(x + scoreBounds.x, y + scoreBounds.y, scoreBounds.width, scoreBounds.height);

View file

@ -1,7 +1,6 @@
package io.github.lonamiwebs.klooni.actors; package io.github.lonamiwebs.klooni.actors;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Rectangle;
@ -41,14 +40,7 @@ public class ThemeCard extends Actor {
public ThemeCard(final Klooni game, final GameLayout layout, final Theme theme) { public ThemeCard(final Klooni game, final GameLayout layout, final Theme theme) {
this.theme = theme; this.theme = theme;
background = Theme.getBlankTexture();
// A 1x1 pixel map will be enough, the background texture will then be stretched accordingly
// 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();
Label.LabelStyle labelStyle = new Label.LabelStyle(); Label.LabelStyle labelStyle = new Label.LabelStyle();
labelStyle.font = game.skin.getFont("font_small"); labelStyle.font = game.skin.getFont("font_small");
@ -75,7 +67,7 @@ public class ThemeCard extends Actor {
public void draw(Batch batch, float parentAlpha) { public void draw(Batch batch, float parentAlpha) {
final float x = getX(), y = getY(); final float x = getX(), y = getY();
batch.setColor(Color.WHITE); batch.setColor(theme.background);
batch.draw(background, x, y, getWidth(), getHeight()); batch.draw(background, x, y, getWidth(), getHeight());
// Do not draw on the borders (0,0 offset to add some padding), colors used: // Do not draw on the borders (0,0 offset to add some padding), colors used:
// 0 7 7 // 0 7 7

View file

@ -36,11 +36,9 @@ public abstract class BaseScorer {
labelStyle.font = game.skin.getFont("font"); labelStyle.font = game.skin.getFont("font");
leftLabel = new Label("0", labelStyle); leftLabel = new Label("0", labelStyle);
leftLabel.setColor(Klooni.theme.currentScore);
leftLabel.setAlignment(Align.right); leftLabel.setAlignment(Align.right);
highScoreLabel = new Label(Integer.toString(highScore), labelStyle); highScoreLabel = new Label(Integer.toString(highScore), labelStyle);
highScoreLabel.setColor(Klooni.theme.highScore);
layout.update(this); layout.update(this);
} }
@ -90,7 +88,10 @@ public abstract class BaseScorer {
batch.setColor(cupColor); batch.setColor(cupColor);
batch.draw(cupTexture, cupArea.x, cupArea.y, cupArea.width, cupArea.height); batch.draw(cupTexture, cupArea.x, cupArea.y, cupArea.width, cupArea.height);
leftLabel.setColor(Klooni.theme.currentScore);
leftLabel.draw(batch, 1f); leftLabel.draw(batch, 1f);
highScoreLabel.setColor(Klooni.theme.highScore);
highScoreLabel.draw(batch, 1f); highScoreLabel.draw(batch, 1f);
} }

View file

@ -80,13 +80,10 @@ public class Board {
return false; return false;
lastPutPiecePos.set(piece.calculateGravityCenter()); lastPutPiecePos.set(piece.calculateGravityCenter());
for (int i = 0; i < piece.cellRows; ++i) { for (int i = 0; i < piece.cellRows; ++i)
for (int j = 0; j < piece.cellCols; ++j) { for (int j = 0; j < piece.cellCols; ++j)
if (piece.filled(i, j)) { if (piece.filled(i, j))
cells[y+i][x+j].set(piece.color); cells[y+i][x+j].set(piece.colorIndex);
}
}
}
return true; return true;
} }

View file

@ -15,12 +15,14 @@ public class Cell {
//region Members //region Members
private boolean empty; // Negative index indicates that the cell is empty
private Color color; private int colorIndex;
private Vector2 pos; private Vector2 pos;
private float size; private float size;
// No need to store the vanish color as a color index since
// this is something quick and shouldn't really affect the UX
private Color vanishColor; private Color vanishColor;
private float vanishSize; private float vanishSize;
private float vanishElapsed; private float vanishElapsed;
@ -34,7 +36,7 @@ public class Cell {
pos = new Vector2(x, y); pos = new Vector2(x, y);
size = cellSize; size = cellSize;
empty = true; colorIndex = -1;
vanishElapsed = Float.POSITIVE_INFINITY; vanishElapsed = Float.POSITIVE_INFINITY;
} }
@ -42,15 +44,14 @@ public class Cell {
//region Package local methods //region Package local methods
// Sets the cell to be non-empty and of the specified color // Sets the cell to be non-empty and of the specified color index
void set(Color c) { void set(int ci) {
empty = false; colorIndex = ci;
color = c;
} }
void draw(SpriteBatch batch) { void draw(SpriteBatch batch) {
// Always query the color to the theme, because it might have changed // Always query the color to the theme, because it might have changed
draw(empty ? Klooni.theme.emptyCell : color, batch, pos.x, pos.y, size); draw(Klooni.theme.getCellColor(colorIndex), batch, pos.x, pos.y, size);
// Draw the previous vanishing cell // Draw the previous vanishing cell
if (vanishElapsed <= vanishLifetime) { if (vanishElapsed <= vanishLifetime) {
@ -71,13 +72,13 @@ public class Cell {
// in this case, a piece was put. The closer it was put, the faster // in this case, a piece was put. The closer it was put, the faster
// this piece will vanish. This immediately marks the piece as empty. // this piece will vanish. This immediately marks the piece as empty.
void vanish(Vector2 vanishFrom) { void vanish(Vector2 vanishFrom) {
if (empty) // We cannot vanish twice if (isEmpty()) // We cannot vanish twice
return; return;
empty = true;
vanishSize = size; vanishSize = size;
vanishColor = color.cpy(); vanishColor = Klooni.theme.getCellColor(colorIndex).cpy();
vanishLifetime = 1f; vanishLifetime = 1f;
colorIndex = -1;
// The vanish distance is this measure (distance² + size³ * 20% size) // The vanish distance is this measure (distance² + size³ * 20% size)
// because it seems good enough. The more the distance, the more the // because it seems good enough. The more the distance, the more the
@ -92,7 +93,7 @@ public class Cell {
} }
boolean isEmpty() { boolean isEmpty() {
return empty; return colorIndex < 0;
} }
//endregion //endregion

View file

@ -16,7 +16,7 @@ public class Piece {
//region Members //region Members
final Vector2 pos; final Vector2 pos;
final Color color; final int colorIndex;
final int cellCols, cellRows; final int cellCols, cellRows;
private boolean shape[][]; private boolean shape[][];
@ -34,7 +34,7 @@ public class Piece {
// colorIndex represents a random index that will be used // colorIndex represents a random index that will be used
// to determine the color of this piece when drawn on the screen. // to determine the color of this piece when drawn on the screen.
private Piece(int cols, int rows, boolean swapSize, int colorIndex) { private Piece(int cols, int rows, boolean swapSize, int colorIndex) {
color = Klooni.theme.getCellColor(colorIndex); this.colorIndex = colorIndex;
pos = new Vector2(); pos = new Vector2();
cellCols = swapSize ? rows : cols; cellCols = swapSize ? rows : cols;
@ -49,7 +49,7 @@ public class Piece {
// L-shaped constructor // L-shaped constructor
private Piece(int lSize, int rotateCount, int colorIndex) { private Piece(int lSize, int rotateCount, int colorIndex) {
color = Klooni.theme.getCellColor(colorIndex); this.colorIndex = colorIndex;
pos = new Vector2(); pos = new Vector2();
cellCols = cellRows = lSize; cellCols = cellRows = lSize;
@ -113,10 +113,11 @@ public class Piece {
//region Package local methods //region Package local methods
void draw(SpriteBatch batch) { void draw(SpriteBatch batch) {
final Color c = Klooni.theme.getCellColor(colorIndex);
for (int i = 0; i < cellRows; ++i) for (int i = 0; i < cellRows; ++i)
for (int j = 0; j < cellCols; ++j) for (int j = 0; j < cellCols; ++j)
if (shape[i][j]) if (shape[i][j])
Cell.draw(color, batch, pos.x + j * cellSize, pos.y + i * cellSize, cellSize); Cell.draw(c, batch, pos.x + j * cellSize, pos.y + i * cellSize, cellSize);
} }
// Calculates the rectangle of the piece with screen coordinates // Calculates the rectangle of the piece with screen coordinates