From d1905acb8eb2a05cfc0e61fd95a9211b96dcee80 Mon Sep 17 00:00:00 2001 From: galaxyoyo Date: Thu, 8 Jan 2015 18:20:59 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=C3=A9=20quelques=20choses=20pour=20l'?= =?UTF-8?q?=C3=A9diteur?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../galaxyoyo/unknown/api/editor/Case.java | 48 ++++++++ .../unknown/api/editor/EditorAPI.java | 107 ++++++++++++------ .../galaxyoyo/unknown/api/editor/RawCase.java | 46 ++++++++ .../galaxyoyo/unknown/api/editor/RawMap.java | 20 ++++ .../unknown/api/editor/RawSprite.java | 22 ++++ .../unknown/api/editor/sprites/Sprite.java | 2 + .../galaxyoyo/unknown/client/main/Main.java | 16 +-- .../java/galaxyoyo/unknown/editor/Editor.java | 41 ------- .../galaxyoyo/unknown/editor/EditorFrame.java | 60 +++++++++- .../java/galaxyoyo/unknown/editor/Map.java | 53 +++++++++ 10 files changed, 326 insertions(+), 89 deletions(-) create mode 100644 src/main/java/galaxyoyo/unknown/api/editor/Case.java create mode 100644 src/main/java/galaxyoyo/unknown/api/editor/RawCase.java create mode 100644 src/main/java/galaxyoyo/unknown/api/editor/RawMap.java create mode 100644 src/main/java/galaxyoyo/unknown/api/editor/RawSprite.java delete mode 100644 src/main/java/galaxyoyo/unknown/editor/Editor.java create mode 100644 src/main/java/galaxyoyo/unknown/editor/Map.java diff --git a/src/main/java/galaxyoyo/unknown/api/editor/Case.java b/src/main/java/galaxyoyo/unknown/api/editor/Case.java new file mode 100644 index 0000000..6aec52d --- /dev/null +++ b/src/main/java/galaxyoyo/unknown/api/editor/Case.java @@ -0,0 +1,48 @@ +package galaxyoyo.unknown.api.editor; + +import galaxyoyo.unknown.api.editor.sprites.Sprite; + +public class Case +{ + private int x; + private int y; + private Sprite couche1; + private Sprite couche2; + private Sprite couche3; + + public int getPosX() + { + return x; + } + + public int getPosY() + { + return y; + } + + public Sprite getCoucheOne() + { + return couche1; + } + + public Sprite getCoucheTwo() + { + return couche2; + } + + public Sprite getCoucheThree() + { + return couche3; + } + + public static Case create(int posX, int posY, Sprite couche1, Sprite couche2, Sprite couche3) + { + Case c = new Case(); + c.x = posX; + c.y = posY; + c.couche1 = couche1; + c.couche2 = couche2; + c.couche3 = couche3; + return c; + } +} diff --git a/src/main/java/galaxyoyo/unknown/api/editor/EditorAPI.java b/src/main/java/galaxyoyo/unknown/api/editor/EditorAPI.java index afc4b91..f652587 100644 --- a/src/main/java/galaxyoyo/unknown/api/editor/EditorAPI.java +++ b/src/main/java/galaxyoyo/unknown/api/editor/EditorAPI.java @@ -1,12 +1,12 @@ package galaxyoyo.unknown.api.editor; -import galaxyoyo.unknown.editor.Editor; +import galaxyoyo.unknown.editor.Map; -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; +import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -14,29 +14,38 @@ import java.util.List; import javax.swing.JFileChooser; import javax.swing.filechooser.FileNameExtensionFilter; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + public class EditorAPI { - public static Byte[] toBytes(int width, int height) + public static RawMap toRawMap(int width, int height) { - List bytes = new ArrayList(); + List cases = new ArrayList(); for (int y = 1; y < height; y += 16) { for (int x = 1; x < width; x += 16) { - bytes.add((byte) 0); - bytes.add((byte) 0); - bytes.add((byte) (Byte.MIN_VALUE / 2)); + RawCase c = RawCase.create(x / 16, y / 16, RawSprite.BLANK, RawSprite.BLANK, RawSprite.BLANK); + cases.add(c); } - bytes.remove(bytes.lastIndexOf((byte) (Byte.MIN_VALUE / 2))); - bytes.add(Byte.MIN_VALUE); } - bytes.remove(bytes.lastIndexOf(Byte.MIN_VALUE)); - - return bytes.toArray(new Byte[0]); + return RawMap.create(cases); } + private static Gson createGson() + { + GsonBuilder builder = new GsonBuilder(); + + builder.enableComplexMapKeySerialization(); + builder.serializeNulls(); + builder.setPrettyPrinting(); + + return builder.create(); + } + public static JFileChooser createJFC() { JFileChooser jfc = new JFileChooser(); @@ -51,7 +60,7 @@ public class EditorAPI return jfc; } - public static void saveAs(byte ... bytes) + public static void saveAs(RawMap map) { JFileChooser jfc = createJFC(); File file = null; @@ -66,16 +75,32 @@ public class EditorAPI file = new File(file.getParentFile(), file.getName() + ".gworld"); } - save(file, bytes); + save(file, map); } - public static void save(File file, byte ... bytes) + public static void save(File file, RawMap map) { + String json = createGson().toJson(map); + String crypted = ""; + + for (char c : json.toCharArray()) + { + char ch = c; + + if (c != '\n') + { + ch = (char) (c * 2); + } + + crypted += ch; + } + try { - BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)); - bos.write(bytes); - bos.close(); + file.createNewFile(); + BufferedWriter bw = new BufferedWriter(new FileWriter(file)); + bw.write(crypted); + bw.close(); } catch (IOException ex) { @@ -83,7 +108,7 @@ public class EditorAPI } } - public static Editor open() + public static Map open() { JFileChooser jfc = createJFC(); File file = null; @@ -98,30 +123,46 @@ public class EditorAPI return open(file); } - public static Editor open(File f) + public static Map open(File f) { - byte[] bytes = null; + String json = null; try { - BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f)); - bytes = new byte[(int) f.length()]; - while (bis.read(bytes) != -1); - for (byte b : bytes) + BufferedReader br = new BufferedReader(new FileReader(f)); + String text = ""; + String ln; + while ((ln = br.readLine()) != null) { - System.err.println(b); + text += ln + "\n"; + } + br.close(); + + json = ""; + + for (char c : text.toCharArray()) + { + char ch = c; + + if (c != '\n') + { + ch = (char) (c / 2); + } + + json += ch; } - bis.close(); } catch (IOException e) { e.printStackTrace(); } - return open(bytes); + RawMap rm = createGson().fromJson(json, RawMap.class); + + return open(rm); } - public static Editor open(byte[] bytes) + public static Map open(RawMap map) { - return new Editor(bytes); + return new Map(map); } } diff --git a/src/main/java/galaxyoyo/unknown/api/editor/RawCase.java b/src/main/java/galaxyoyo/unknown/api/editor/RawCase.java new file mode 100644 index 0000000..94caeeb --- /dev/null +++ b/src/main/java/galaxyoyo/unknown/api/editor/RawCase.java @@ -0,0 +1,46 @@ +package galaxyoyo.unknown.api.editor; + +public class RawCase +{ + private int x; + private int y; + private RawSprite couche1; + private RawSprite couche2; + private RawSprite couche3; + + public int getPosX() + { + return x; + } + + public int getPosY() + { + return y; + } + + public RawSprite getCoucheOne() + { + return couche1; + } + + public RawSprite getCoucheTwo() + { + return couche2; + } + + public RawSprite getCoucheThree() + { + return couche3; + } + + public static RawCase create(int posX, int posY, RawSprite couche1, RawSprite couche2, RawSprite couche3) + { + RawCase c = new RawCase(); + c.x = posX; + c.y = posY; + c.couche1 = couche1; + c.couche2 = couche2; + c.couche3 = couche3; + return c; + } +} diff --git a/src/main/java/galaxyoyo/unknown/api/editor/RawMap.java b/src/main/java/galaxyoyo/unknown/api/editor/RawMap.java new file mode 100644 index 0000000..a01f397 --- /dev/null +++ b/src/main/java/galaxyoyo/unknown/api/editor/RawMap.java @@ -0,0 +1,20 @@ +package galaxyoyo.unknown.api.editor; + +import java.util.List; + +public class RawMap +{ + private List cases; + + public List getCases() + { + return cases; + } + + public static RawMap create(List cases) + { + RawMap rm = new RawMap(); + rm.cases = cases; + return rm; + } +} diff --git a/src/main/java/galaxyoyo/unknown/api/editor/RawSprite.java b/src/main/java/galaxyoyo/unknown/api/editor/RawSprite.java new file mode 100644 index 0000000..a882c60 --- /dev/null +++ b/src/main/java/galaxyoyo/unknown/api/editor/RawSprite.java @@ -0,0 +1,22 @@ +package galaxyoyo.unknown.api.editor; + +import com.google.gson.annotations.Expose; + +public class RawSprite +{ + private int primaryIndex = 0; + private int secondaryIndex = 0; + + @Expose(serialize = false, deserialize = false) + public static final RawSprite BLANK = new RawSprite(); + + public int getPrimaryIndex() + { + return primaryIndex; + } + + public int getSecondaryIndex() + { + return secondaryIndex; + } +} diff --git a/src/main/java/galaxyoyo/unknown/api/editor/sprites/Sprite.java b/src/main/java/galaxyoyo/unknown/api/editor/sprites/Sprite.java index b30471f..33ce4dc 100644 --- a/src/main/java/galaxyoyo/unknown/api/editor/sprites/Sprite.java +++ b/src/main/java/galaxyoyo/unknown/api/editor/sprites/Sprite.java @@ -4,6 +4,8 @@ import java.awt.image.BufferedImage; public class Sprite { + public static final Sprite BLANK = new Sprite(new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB)); + private final BufferedImage img; public Sprite(BufferedImage img) diff --git a/src/main/java/galaxyoyo/unknown/client/main/Main.java b/src/main/java/galaxyoyo/unknown/client/main/Main.java index c2a75c6..3f9cd4b 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.RawMap; import galaxyoyo.unknown.api.editor.sprites.SpriteRegister; import galaxyoyo.unknown.frame.MainFrame; @@ -68,8 +69,6 @@ public class Main LOGGER.addAppender(file); LOGGER.setLevel(Level.INFO); - - OptionParser parser = new OptionParser(); OptionSpec edit = parser.accepts("edit", "Lancer l'\u00e9diteur de monde").withOptionalArg(); @@ -188,18 +187,11 @@ public class Main g.drawLine(0, y, width, y); } - Byte[] Bytes = EditorAPI.toBytes(baseWidth, baseHeight); + RawMap rm = EditorAPI.toRawMap(baseWidth, baseHeight); - byte[] bytes = new byte[Bytes.length]; + EditorAPI.saveAs(rm); - for (int i = 0; i < Bytes.length; ++i) - { - bytes[i] = Bytes[i]; - } - - EditorAPI.saveAs(bytes); - - EditorAPI.open(bytes); + EditorAPI.open(rm); try { diff --git a/src/main/java/galaxyoyo/unknown/editor/Editor.java b/src/main/java/galaxyoyo/unknown/editor/Editor.java deleted file mode 100644 index af925d0..0000000 --- a/src/main/java/galaxyoyo/unknown/editor/Editor.java +++ /dev/null @@ -1,41 +0,0 @@ -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(this); - this.bytes = bytes; - - getFrame().setVisible(true); - } - - public EditorFrame getFrame() - { - return frame; - } - - public byte[] getBytes() - { - return bytes; - } - - public void setBytes(byte[] bytes) - { - 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 6f8311f..263457a 100644 --- a/src/main/java/galaxyoyo/unknown/editor/EditorFrame.java +++ b/src/main/java/galaxyoyo/unknown/editor/EditorFrame.java @@ -1,16 +1,70 @@ package galaxyoyo.unknown.editor; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; + import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.JTabbedPane; public class EditorFrame extends JFrame { private static final long serialVersionUID = -2705122356101556462L; @SuppressWarnings("unused") - private final Editor editor; + private final Map map; + + private final JPanel content = new JPanel(); + + private final JTabbedPane tabs = new JTabbedPane(JTabbedPane.BOTTOM); + private final JPanel tabEvents = new JPanel(); + private final JPanel tabColl = new JPanel(); + private final JPanel mapPanel = new JPanel(); + private final JTabbedPane resources = new JTabbedPane(); + private final JPanel couche1 = new JPanel(); + private final JPanel couche2 = new JPanel(); + private final JPanel couche3 = new JPanel(); - public EditorFrame(Editor editor) + public EditorFrame(Map map) { - this.editor = editor; + this.map = map; + this.setSize(600, 600); + this.setPreferredSize(getSize()); + this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + this.setExtendedState(JFrame.MAXIMIZED_BOTH); + this.setLocationRelativeTo(null); + this.setLayout(new GridBagLayout()); + this.setContentPane(content); + + GridBagConstraints c = new GridBagConstraints(); + + c.gridx = 0; + c.gridy = 0; + c.gridwidth = 3; + c.gridheight = 1; + + tabs.addTab("Carte", new JPanel()); + tabs.addTab("\u00c9vennements", tabEvents); + tabs.addTab("Collisions", tabColl); + + content.add(tabs, c); + + c.gridx = 0; + c.gridy = 1; + c.gridwidth = 2; + c.gridheight = 3; + + content.add(mapPanel); + + resources.addTab("1", couche1); + resources.addTab("2", couche2); + resources.addTab("3", couche3); + + c.gridx = 2; + c.gridy = 1; + c.gridwidth = 1; + c.gridheight = 3; + + content.add(resources); } } diff --git a/src/main/java/galaxyoyo/unknown/editor/Map.java b/src/main/java/galaxyoyo/unknown/editor/Map.java new file mode 100644 index 0000000..9af4503 --- /dev/null +++ b/src/main/java/galaxyoyo/unknown/editor/Map.java @@ -0,0 +1,53 @@ +package galaxyoyo.unknown.editor; + +import galaxyoyo.unknown.api.editor.Case; +import galaxyoyo.unknown.api.editor.RawCase; +import galaxyoyo.unknown.api.editor.RawMap; +import galaxyoyo.unknown.api.editor.sprites.SpriteRegister; + +import java.awt.Point; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class Map +{ + private final EditorFrame frame; + private List cases = new ArrayList(); + private java.util.Map casesMap = new HashMap(); + + public Map(RawMap raw) + { + for (RawCase rc : raw.getCases()) + { + cases.add(Case.create(rc.getPosX(), rc.getPosY(), SpriteRegister.getCategory(rc.getCoucheOne().getPrimaryIndex()).get(rc.getCoucheOne().getSecondaryIndex()), SpriteRegister.getCategory(rc.getCoucheTwo().getPrimaryIndex()).get(rc.getCoucheTwo().getSecondaryIndex()), SpriteRegister.getCategory(rc.getCoucheThree().getPrimaryIndex()).get(rc.getCoucheThree().getSecondaryIndex()))); + } + + frame = new EditorFrame(this); + + getFrame().setVisible(true); + } + + public EditorFrame getFrame() + { + return frame; + } + + public Case getCase(int x, int y) + { + if (casesMap.isEmpty()) + { + reorganizeMap(); + } + + return casesMap.get(new Point(x, y)); + } + + private void reorganizeMap() + { + for (Case c : cases) + { + casesMap.put(new Point(c.getPosX() - c.getPosX() % 16, c.getPosY() - c.getPosY() % 16), c); + } + } +}