Ajouté entièrement l'enregistrement des sprites

This commit is contained in:
galaxyoyo 2015-01-07 22:50:11 +01:00
parent 5847486e2f
commit 968536742b
9 changed files with 139 additions and 9 deletions

View File

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

View File

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

View File

@ -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<String, List<List<Double>>> nameToCoords;
private static Map<String, List<Sprite>> sprites = new HashMap<String, List<Sprite>>();
@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<Sprite> lSprites = new ArrayList<Sprite>();
for (List<Double> 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<Sprite> getCategory(String name)
{
return sprites.get(name);
}
public static List<Sprite> getCategory(int index)
{
return getCategory(new ArrayList<String>(sprites.keySet()).get(index));
}
}

View File

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

View File

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

View File

@ -6,7 +6,11 @@ public class EditorFrame extends JFrame
{
private static final long serialVersionUID = -2705122356101556462L;
public EditorFrame()
@SuppressWarnings("unused")
private final Editor editor;
public EditorFrame(Editor editor)
{
this.editor = editor;
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

View File

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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB