From 02c8290c293e2857e2e2a8475b2e9371d8debdbb Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Mon, 30 Jan 2017 18:58:54 +0100 Subject: [PATCH] Update button style on theme change --- .../io/github/lonamiwebs/klooni/Klooni.java | 7 +++--- .../io/github/lonamiwebs/klooni/Theme.java | 24 +++++++++++++++---- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/core/src/io/github/lonamiwebs/klooni/Klooni.java b/core/src/io/github/lonamiwebs/klooni/Klooni.java index 5d459b8..92a273b 100644 --- a/core/src/io/github/lonamiwebs/klooni/Klooni.java +++ b/core/src/io/github/lonamiwebs/klooni/Klooni.java @@ -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)); } diff --git a/core/src/io/github/lonamiwebs/klooni/Theme.java b/core/src/io/github/lonamiwebs/klooni/Theme.java index f928b41..7a0c89d 100644 --- a/core/src/io/github/lonamiwebs/klooni/Theme.java +++ b/core/src/io/github/lonamiwebs/klooni/Theme.java @@ -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) {