From eba4deb674669968a53b8f5fa55268e3b62c2f8c Mon Sep 17 00:00:00 2001 From: galaxyoyo Date: Tue, 13 Jan 2015 18:37:58 +0100 Subject: [PATCH] =?UTF-8?q?Pour=20une=20meilleure=20utilisation,=20les=20s?= =?UTF-8?q?prites=20sont=20d=C3=A9sormais=20export=C3=A9s=20pour=20permett?= =?UTF-8?q?re=20au=20joueur=20de=20cr=C3=A9er=20ses=20propres=20sprites?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/editor/sprites/SpriteRegister.java | 107 +++++++++++++----- .../galaxyoyo/unknown/client/main/Main.java | 2 + .../galaxyoyo/unknown/editor/EditorFrame.java | 7 +- 3 files changed, 85 insertions(+), 31 deletions(-) diff --git a/src/main/java/galaxyoyo/unknown/api/editor/sprites/SpriteRegister.java b/src/main/java/galaxyoyo/unknown/api/editor/sprites/SpriteRegister.java index 149985a..f8c0e52 100644 --- a/src/main/java/galaxyoyo/unknown/api/editor/sprites/SpriteRegister.java +++ b/src/main/java/galaxyoyo/unknown/api/editor/sprites/SpriteRegister.java @@ -1,10 +1,15 @@ package galaxyoyo.unknown.api.editor.sprites; import java.awt.image.BufferedImage; +import java.io.BufferedInputStream; import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStream; import java.io.InputStreamReader; +import java.net.URISyntaxException; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -21,6 +26,38 @@ public class SpriteRegister private static Map>> nameToCoords; private static Map sprites = new HashMap(); + public static void unpack() + { + try + { + File dir = new File(SpriteRegister.class.getResource("/assets").toURI()); + unpackDir(dir); + } + catch (URISyntaxException | IOException e) + { + e.printStackTrace(); + } + } + + private static void unpackDir(File dir) throws IOException + { + for (File f : dir.listFiles()) + { + if (f.isDirectory()) + { + unpackDir(f); + continue; + } + + String path = f.getAbsolutePath().substring(f.getAbsolutePath().indexOf(File.separatorChar + "assets") + 1); + File local = new File(path); + local.getParentFile().mkdirs(); + if (local.exists()) + local.delete(); + Files.copy(Paths.get(f.toURI()), Paths.get(local.toURI())); + } + } + @SuppressWarnings("unchecked") public static void refreshAllSprites() { @@ -29,40 +66,54 @@ public class SpriteRegister return; } - try + File assetsDir = new File("assets"); + List assets = new ArrayList(); + + for (File dir : assetsDir.listFiles()) { - BufferedReader br = new BufferedReader(new InputStreamReader(SpriteRegister.class.getResourceAsStream("/assets/unknown/textures/sprites/sprites.json"))); - nameToCoords = new Gson().fromJson(br, Map.class); - br.close(); - - for (String key : nameToCoords.keySet()) + assets.add(dir.getName()); + } + + for (String asset : assets) + { + try { - try + File f = new File(assetsDir.getAbsolutePath() + "/" + asset + "/textures/sprites"); + f.mkdirs(); + BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(f, "sprites.json")))); + nameToCoords = new Gson().fromJson(br, Map.class); + br.close(); + + for (String key : nameToCoords.keySet()) { - InputStream is = SpriteRegister.class.getResourceAsStream("/assets/unknown/textures/sprites/" + key + ".png"); - BufferedImage img = ImageIO.read(is); - Category cat = Category.create(key, new ArrayList(nameToCoords.keySet()).indexOf(key), new ArrayList()); - - for (List list : nameToCoords.get(key)) + try { - int x = list.get(0).intValue(); - int y = list.get(1).intValue(); - BufferedImage child = img.getSubimage(x, y, 16, 16); - new Sprite(child, cat, nameToCoords.get(key).indexOf(list)); + + BufferedInputStream is = new BufferedInputStream(new FileInputStream(new File(f, key + ".png"))); + BufferedImage img = ImageIO.read(is); + Category cat = Category.create(key, new ArrayList(nameToCoords.keySet()).indexOf(key), new ArrayList()); + + for (List list : nameToCoords.get(key)) + { + int x = list.get(0).intValue(); + int y = list.get(1).intValue(); + BufferedImage child = img.getSubimage(x, y, 16, 16); + new Sprite(child, cat, nameToCoords.get(key).indexOf(list)); + } + + sprites.put(key, cat); + } + catch (Throwable t) + { + LogManager.getLogger("SpriteRegister").fatal("Erreur lors de la lecture du sprite '" + key + "'", t); + continue; } - - sprites.put(key, cat); - } - catch (Throwable t) - { - LogManager.getLogger("SpriteRegister").fatal("Erreur lors de la lecture du sprite '" + key + "'", t); - continue; } } - } - catch (IOException e) - { - e.printStackTrace(); + catch (IOException e) + { + e.printStackTrace(); + } } } diff --git a/src/main/java/galaxyoyo/unknown/client/main/Main.java b/src/main/java/galaxyoyo/unknown/client/main/Main.java index d273474..73ab639 100644 --- a/src/main/java/galaxyoyo/unknown/client/main/Main.java +++ b/src/main/java/galaxyoyo/unknown/client/main/Main.java @@ -110,6 +110,8 @@ public class Main } } + SpriteRegister.unpack(); + SpriteRegister.refreshAllSprites(); if (set.has(edit)) diff --git a/src/main/java/galaxyoyo/unknown/editor/EditorFrame.java b/src/main/java/galaxyoyo/unknown/editor/EditorFrame.java index 3b4276c..374bacd 100644 --- a/src/main/java/galaxyoyo/unknown/editor/EditorFrame.java +++ b/src/main/java/galaxyoyo/unknown/editor/EditorFrame.java @@ -20,6 +20,7 @@ import java.awt.event.KeyEvent; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.awt.image.BufferedImage; +import java.io.File; import javax.swing.ButtonGroup; import javax.swing.ImageIcon; @@ -158,9 +159,9 @@ public class EditorFrame extends JFrame implements ChangeListener, ActionListene scroll2.getHorizontalScrollBar().setMaximum(0); scroll3.getHorizontalScrollBar().setMaximum(0); - resources.addTab("", new ImageIcon(getClass().getResource("/assets/unknown/textures/layer 1.png")), scroll1); - resources.addTab("", new ImageIcon(getClass().getResource("/assets/unknown/textures/layer 2.png")), scroll2); - resources.addTab("", new ImageIcon(getClass().getResource("/assets/unknown/textures/layer 3.png")), scroll3); + resources.addTab("", new ImageIcon(new File("assets/unknown/textures/layer 1.png").getAbsolutePath()), scroll1); + resources.addTab("", new ImageIcon(new File("assets/unknown/textures/layer 2.png").getAbsolutePath()), scroll2); + resources.addTab("", new ImageIcon(new File("assets/unknown/textures/layer 3.png").getAbsolutePath()), scroll3); resources.addChangeListener(this); resources.setBackgroundAt(0, Color.white); resources.setBackgroundAt(1, Color.white);