Update button style on theme change

This commit is contained in:
Lonami Exo 2017-01-30 18:58:54 +01:00
parent 382a41c06b
commit 02c8290c29
2 changed files with 23 additions and 8 deletions

View file

@ -19,9 +19,6 @@ public class Klooni extends Game {
public void create() {
prefs = Gdx.app.getPreferences("io.github.lonamiwebs.klooni.game");
// Use only one instance for the theme, so anyone using it uses the most up-to-date
theme = Theme.getTheme(prefs.getString("themeName", "default"));
// TODO Better way to have this skin somewhere
// Gotta create that darn .json!
skin = new Skin(Gdx.files.internal("skin/uiskin.json"));
@ -46,6 +43,10 @@ public class Klooni extends Game {
skin.add("web_texture", new Texture(Gdx.files.internal("ui/web.png")));
skin.add("back_texture", new Texture(Gdx.files.internal("ui/back.png")));
// Use only one instance for the theme, so anyone using it uses the most up-to-date
Theme.skin = skin; // Not the best idea
theme = Theme.getTheme(prefs.getString("themeName", "default"));
Gdx.input.setCatchBackKey(true); // To show the pause menu
setScreen(new MainMenuScreen(this));
}

View file

@ -17,7 +17,13 @@ public class Theme {
private Color[] cells;
private Color[] buttons;
private Theme() { }
public static Skin skin;
private ImageButton.ImageButtonStyle[] buttonStyles;
private Theme() {
buttonStyles = new ImageButton.ImageButtonStyle[4];
}
public static Theme[] getThemes() {
FileHandle[] handles = Gdx.files.internal("themes").list();
@ -43,6 +49,10 @@ public class Theme {
}
private Theme update(final FileHandle handle) {
if (skin == null) {
throw new NullPointerException("A Theme.skin must be set before updating any Theme instance");
}
final JsonValue json = new JsonReader().parse(handle.readString());
name = handle.nameWithoutExtension();
@ -57,6 +67,12 @@ public class Theme {
buttons = new Color[buttonColors.size];
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new Color((int)Long.parseLong(buttonColors.getString(i), 16));
if (buttonStyles[i] == null) {
buttonStyles[i] = new ImageButton.ImageButtonStyle();
}
buttonStyles[i].up = skin.newDrawable("button_up", buttons[i]);
buttonStyles[i].down = skin.newDrawable("button_down", buttons[i]);
}
JsonValue cellColors = colors.get("cells");
@ -71,10 +87,8 @@ public class Theme {
// TODO Avoid creating game.skin.newDrawable all the time without disposing
public ImageButton.ImageButtonStyle getStyle(final Skin skin, int button, final String imageName) {
return new ImageButton.ImageButtonStyle(
skin.newDrawable("button_up", buttons[button]),
skin.newDrawable("button_down", buttons[button]),
null, skin.getDrawable(imageName), null, null);
buttonStyles[button].imageUp = skin.getDrawable(imageName);
return buttonStyles[button];
}
public Color getCellColor(int colorIndex) {