Pour une meilleure utilisation, les sprites sont désormais exportés pour

permettre au joueur de créer ses propres sprites
This commit is contained in:
galaxyoyo 2015-01-13 18:37:58 +01:00
parent 873609a43b
commit eba4deb674
3 changed files with 85 additions and 31 deletions

View File

@ -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<String, List<List<Double>>> nameToCoords;
private static Map<String, Category> sprites = new HashMap<String, Category>();
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<String> assets = new ArrayList<String>();
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();
assets.add(dir.getName());
}
for (String key : nameToCoords.keySet())
for (String asset : assets)
{
try
{
try
{
InputStream is = SpriteRegister.class.getResourceAsStream("/assets/unknown/textures/sprites/" + key + ".png");
BufferedImage img = ImageIO.read(is);
Category cat = Category.create(key, new ArrayList<String>(nameToCoords.keySet()).indexOf(key), new ArrayList<Sprite>());
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 (List<Double> list : nameToCoords.get(key))
for (String key : nameToCoords.keySet())
{
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));
}
sprites.put(key, cat);
}
catch (Throwable t)
{
LogManager.getLogger("SpriteRegister").fatal("Erreur lors de la lecture du sprite '" + key + "'", t);
continue;
BufferedInputStream is = new BufferedInputStream(new FileInputStream(new File(f, key + ".png")));
BufferedImage img = ImageIO.read(is);
Category cat = Category.create(key, new ArrayList<String>(nameToCoords.keySet()).indexOf(key), new ArrayList<Sprite>());
for (List<Double> 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;
}
}
}
}
catch (IOException e)
{
e.printStackTrace();
catch (IOException e)
{
e.printStackTrace();
}
}
}

View File

@ -110,6 +110,8 @@ public class Main
}
}
SpriteRegister.unpack();
SpriteRegister.refreshAllSprites();
if (set.has(edit))

View File

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