1
0
mirror of https://github.com/ynerant/Level-Editor.git synced 2025-07-03 22:32:51 +02:00

Bougé API

This commit is contained in:
galaxyoyo
2015-01-07 23:05:23 +01:00
parent 968536742b
commit 0eff34c418
6 changed files with 1 additions and 1 deletions

View File

@ -0,0 +1,124 @@
package galaxyoyo.unknown.api.editor;
import galaxyoyo.unknown.editor.Editor;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
public class EditorAPI
{
public static Byte[] toBytes(int width, int height)
{
List<Byte> bytes = new ArrayList<Byte>();
for (int y = 1; y < height; y += 16)
{
for (int x = 1; x < width; x += 16)
{
bytes.add((byte) 0);
}
bytes.add(Byte.MIN_VALUE);
}
bytes.remove(bytes.lastIndexOf(Byte.MIN_VALUE));
return bytes.toArray(new Byte[0]);
}
public static JFileChooser createJFC()
{
JFileChooser jfc = new JFileChooser();
jfc.setFileFilter(new FileNameExtensionFilter("Fichiers monde (*.gworld, *.dat)", "gworld", "dat"));
jfc.setFileHidingEnabled(true);
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
File dir = new File("maps");
dir.mkdirs();
jfc.setCurrentDirectory(dir);
return jfc;
}
public static void saveAs(byte ... bytes)
{
JFileChooser jfc = createJFC();
File file = null;
while (file == null)
{
jfc.showSaveDialog(null);
file = jfc.getSelectedFile();
}
if (!file.getName().toLowerCase().endsWith(".gworld") && !file.getName().toLowerCase().endsWith(".dat"))
{
file = new File(file.getParentFile(), file.getName() + ".gworld");
}
save(file, bytes);
}
public static void save(File file, byte ... bytes)
{
try
{
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
bos.write(bytes);
bos.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
public static Editor open()
{
JFileChooser jfc = createJFC();
File file = null;
while (file == null)
{
jfc.showOpenDialog(null);
file = jfc.getSelectedFile();
System.out.println(file);
}
return open(file);
}
public static Editor open(File f)
{
byte[] bytes = null;
try
{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
bytes = new byte[(int) f.length()];
while (bis.read(bytes) != -1);
for (byte b : bytes)
{
System.err.println(b);
}
bis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
return open(bytes);
}
public static Editor open(byte[] bytes)
{
return new Editor(bytes);
}
}

View File

@ -0,0 +1,4 @@
/**
* @author galaxyoyo
*/
package galaxyoyo.unknown.api.editor;

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