Ajouté quelques choses pour l'éditeur

This commit is contained in:
galaxyoyo 2015-01-08 18:20:59 +01:00
parent 5360aaefe9
commit d1905acb8e
10 changed files with 326 additions and 89 deletions

View File

@ -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;
}
}

View File

@ -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,27 +14,36 @@ 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<Byte> bytes = new ArrayList<Byte>();
List<RawCase> cases = new ArrayList<RawCase>();
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 RawMap.create(cases);
}
return bytes.toArray(new Byte[0]);
private static Gson createGson()
{
GsonBuilder builder = new GsonBuilder();
builder.enableComplexMapKeySerialization();
builder.serializeNulls();
builder.setPrettyPrinting();
return builder.create();
}
public static JFileChooser createJFC()
@ -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);
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,20 @@
package galaxyoyo.unknown.api.editor;
import java.util.List;
public class RawMap
{
private List<RawCase> cases;
public List<RawCase> getCases()
{
return cases;
}
public static RawMap create(List<RawCase> cases)
{
RawMap rm = new RawMap();
rm.cases = cases;
return rm;
}
}

View File

@ -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;
}
}

View File

@ -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)

View File

@ -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<String> 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
{

View File

@ -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<List<Sprite>> sprites = new ArrayList<List<Sprite>>();
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);
}
}

View File

@ -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;
public EditorFrame(Editor editor)
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(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);
}
}

View File

@ -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<Case> cases = new ArrayList<Case>();
private java.util.Map<Point, Case> casesMap = new HashMap<Point, Case>();
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);
}
}
}