diff --git a/src/api/java/galaxyoyo/unknown/api/editor/EditorAPI.java b/src/api/java/galaxyoyo/unknown/api/editor/EditorAPI.java index a9093ba..078f97d 100644 --- a/src/api/java/galaxyoyo/unknown/api/editor/EditorAPI.java +++ b/src/api/java/galaxyoyo/unknown/api/editor/EditorAPI.java @@ -1,5 +1,7 @@ package galaxyoyo.unknown.api.editor; +import galaxyoyo.unknown.editor.Editor; + import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; @@ -78,7 +80,7 @@ public class EditorAPI } } - public static void open() + public static Editor open() { JFileChooser jfc = createJFC(); File file = null; @@ -90,17 +92,16 @@ public class EditorAPI System.out.println(file); } - open(file); + return open(file); } - public static void open(File f) + public static Editor open(File f) { - System.out.println(f); + byte[] bytes = null; try { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f)); - byte[] bytes = new byte[(int) f.length()]; - System.out.println(bis); + bytes = new byte[(int) f.length()]; while (bis.read(bytes) != -1); for (byte b : bytes) { @@ -112,9 +113,12 @@ public class EditorAPI { e.printStackTrace(); } + + return open(bytes); } - public static void open(byte[] bytes) + public static Editor open(byte[] bytes) { + return new Editor(bytes); } } diff --git a/src/api/java/galaxyoyo/unknown/api/editor/sprites/Sprite.java b/src/api/java/galaxyoyo/unknown/api/editor/sprites/Sprite.java new file mode 100644 index 0000000..b30471f --- /dev/null +++ b/src/api/java/galaxyoyo/unknown/api/editor/sprites/Sprite.java @@ -0,0 +1,18 @@ +package galaxyoyo.unknown.api.editor.sprites; + +import java.awt.image.BufferedImage; + +public class Sprite +{ + private final BufferedImage img; + + public Sprite(BufferedImage img) + { + this.img = img; + } + + public BufferedImage getImage() + { + return this.img; + } +} diff --git a/src/api/java/galaxyoyo/unknown/api/editor/sprites/SpriteRegister.java b/src/api/java/galaxyoyo/unknown/api/editor/sprites/SpriteRegister.java new file mode 100644 index 0000000..ad2c44a --- /dev/null +++ b/src/api/java/galaxyoyo/unknown/api/editor/sprites/SpriteRegister.java @@ -0,0 +1,77 @@ +package galaxyoyo.unknown.api.editor.sprites; + +import java.awt.image.BufferedImage; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.imageio.ImageIO; + +import org.apache.logging.log4j.LogManager; + +import com.google.gson.Gson; + +public class SpriteRegister +{ + private static Map>> nameToCoords; + private static Map> sprites = new HashMap>(); + + @SuppressWarnings("unchecked") + public static void refreshAllSprites() + { + try + { + BufferedReader br = new BufferedReader(new InputStreamReader(SpriteRegister.class.getResourceAsStream("/assets/unknown/textures/sprites/sprites.json"))); + nameToCoords = new Gson().fromJson(br, Map.class); + System.out.println(nameToCoords); + br.close(); + + for (String key : nameToCoords.keySet()) + { + try + { + InputStream is = SpriteRegister.class.getResourceAsStream("/assets/unknown/textures/sprites/" + key + ".png"); + BufferedImage img = ImageIO.read(is); + List lSprites = new ArrayList(); + + for (List list : nameToCoords.get(key)) + { + int x = list.get(0).intValue(); + int y = list.get(1).intValue(); + int width = list.get(2).intValue() * 16; + int height = list.get(3).intValue() * 16; + BufferedImage child = img.getSubimage(x, y, width, height); + Sprite sprite = new Sprite(child); + lSprites.add(sprite); + } + + sprites.put(key, lSprites); + } + catch (Throwable t) + { + LogManager.getLogger("SpriteRegister").fatal("Erreur lors de la lecture du sprite '" + key + "'", t); + continue; + } + } + } + catch (IOException e) + { + e.printStackTrace(); + } + } + + public static List getCategory(String name) + { + return sprites.get(name); + } + + public static List getCategory(int index) + { + return getCategory(new ArrayList(sprites.keySet()).get(index)); + } +} diff --git a/src/main/java/galaxyoyo/unknown/client/main/Main.java b/src/main/java/galaxyoyo/unknown/client/main/Main.java index 02274ab..c2a75c6 100644 --- a/src/main/java/galaxyoyo/unknown/client/main/Main.java +++ b/src/main/java/galaxyoyo/unknown/client/main/Main.java @@ -4,6 +4,7 @@ package galaxyoyo.unknown.client.main; import galaxyoyo.unknown.api.editor.EditorAPI; +import galaxyoyo.unknown.api.editor.sprites.SpriteRegister; import galaxyoyo.unknown.frame.MainFrame; import java.awt.Color; @@ -103,6 +104,8 @@ public class Main } } + SpriteRegister.refreshAllSprites(); + if (set.has(edit)) { launchEditMode(); diff --git a/src/main/java/galaxyoyo/unknown/editor/Editor.java b/src/main/java/galaxyoyo/unknown/editor/Editor.java index e2c8b44..af925d0 100644 --- a/src/main/java/galaxyoyo/unknown/editor/Editor.java +++ b/src/main/java/galaxyoyo/unknown/editor/Editor.java @@ -1,14 +1,22 @@ package galaxyoyo.unknown.editor; +import java.util.ArrayList; +import java.util.List; + +import galaxyoyo.unknown.api.editor.sprites.Sprite; + public class Editor { private final EditorFrame frame; private byte[] bytes; + private List> sprites = new ArrayList>(); public Editor(byte[] bytes) { - frame = new EditorFrame(); + frame = new EditorFrame(this); this.bytes = bytes; + + getFrame().setVisible(true); } public EditorFrame getFrame() @@ -25,4 +33,9 @@ public class Editor { this.bytes = bytes; } + + public Sprite getSprite(int x, int y) + { + return sprites.get(x).get(y); + } } diff --git a/src/main/java/galaxyoyo/unknown/editor/EditorFrame.java b/src/main/java/galaxyoyo/unknown/editor/EditorFrame.java index eb6f848..6f8311f 100644 --- a/src/main/java/galaxyoyo/unknown/editor/EditorFrame.java +++ b/src/main/java/galaxyoyo/unknown/editor/EditorFrame.java @@ -5,8 +5,12 @@ import javax.swing.JFrame; public class EditorFrame extends JFrame { private static final long serialVersionUID = -2705122356101556462L; + + @SuppressWarnings("unused") + private final Editor editor; - public EditorFrame() + public EditorFrame(Editor editor) { + this.editor = editor; } } diff --git a/src/main/resources/assets/unknown/textures/sprites/gazon.png b/src/main/resources/assets/unknown/textures/sprites/gazon.png new file mode 100644 index 0000000..a5b3c1d Binary files /dev/null and b/src/main/resources/assets/unknown/textures/sprites/gazon.png differ diff --git a/src/main/resources/assets/unknown/textures/sprites/sprites.json b/src/main/resources/assets/unknown/textures/sprites/sprites.json new file mode 100644 index 0000000..ea92c86 --- /dev/null +++ b/src/main/resources/assets/unknown/textures/sprites/sprites.json @@ -0,0 +1,11 @@ +{ + "gazon": [ + [0,15,1,5], + [0,144,1,3], + [16,144,2,2], + [32,208,6,2], + [128,144,1,2], + [160,156,3,2], + [160,32,1,3] + ] +} diff --git a/src/main/resources/assets/unknown/textures/tiles/gazon.png b/src/main/resources/assets/unknown/textures/tiles/gazon.png deleted file mode 100644 index 8d95b91..0000000 Binary files a/src/main/resources/assets/unknown/textures/tiles/gazon.png and /dev/null differ