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.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
@ -26,7 +27,7 @@ public class Theme {
public Color currentScore;
public Color highScore;
private Color bandColor;
public Color bandColor;
private Color[] cells;
private Color[] buttons;
@ -146,11 +147,7 @@ public class Theme {
}
public Color getCellColor(int colorIndex) {
return cells[colorIndex];
}
public Color getBandColor() {
return bandColor;
return colorIndex < 0 ? emptyCell : cells[colorIndex];
}
public void glClearBackground() {
@ -164,6 +161,20 @@ public class Theme {
//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
void dispose() {

View file

@ -1,6 +1,5 @@
package io.github.lonamiwebs.klooni.actors;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
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 io.github.lonamiwebs.klooni.Klooni;
import io.github.lonamiwebs.klooni.Theme;
import io.github.lonamiwebs.klooni.game.BaseScorer;
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) {
this.scorer = scorer;
// 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();
bandTexture = Theme.getBlankTexture();
Label.LabelStyle labelStyle = new Label.LabelStyle();
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
Vector2 pos = localToStageCoordinates(new Vector2(x, y));
batch.setColor(Klooni.theme.bandColor);
batch.draw(bandTexture, pos.x, pos.y, getWidth(), getHeight());
scoreLabel.setBounds(x + scoreBounds.x, y + scoreBounds.y, scoreBounds.width, scoreBounds.height);

View file

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

View file

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

View file

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

View file

@ -15,12 +15,14 @@ public class Cell {
//region Members
private boolean empty;
private Color color;
// Negative index indicates that the cell is empty
private int colorIndex;
private Vector2 pos;
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 float vanishSize;
private float vanishElapsed;
@ -34,7 +36,7 @@ public class Cell {
pos = new Vector2(x, y);
size = cellSize;
empty = true;
colorIndex = -1;
vanishElapsed = Float.POSITIVE_INFINITY;
}
@ -42,15 +44,14 @@ public class Cell {
//region Package local methods
// Sets the cell to be non-empty and of the specified color
void set(Color c) {
empty = false;
color = c;
// Sets the cell to be non-empty and of the specified color index
void set(int ci) {
colorIndex = ci;
}
void draw(SpriteBatch batch) {
// 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
if (vanishElapsed <= vanishLifetime) {
@ -71,13 +72,13 @@ public class Cell {
// 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.
void vanish(Vector2 vanishFrom) {
if (empty) // We cannot vanish twice
if (isEmpty()) // We cannot vanish twice
return;
empty = true;
vanishSize = size;
vanishColor = color.cpy();
vanishColor = Klooni.theme.getCellColor(colorIndex).cpy();
vanishLifetime = 1f;
colorIndex = -1;
// The vanish distance is this measure (distance² + size³ * 20% size)
// because it seems good enough. The more the distance, the more the
@ -92,7 +93,7 @@ public class Cell {
}
boolean isEmpty() {
return empty;
return colorIndex < 0;
}
//endregion

View file

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