Ajouté fonctions pour ouverture/sauvegarde d'une carte

This commit is contained in:
galaxyoyo 2015-01-07 21:17:17 +01:00
parent 67e2860057
commit b960938805
9 changed files with 190 additions and 32 deletions

View File

@ -13,6 +13,7 @@
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="src" path="src/main/resources"/> <classpathentry kind="src" path="src/main/resources"/>
<classpathentry kind="src" path="src/api/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes> <attributes>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>

View File

@ -0,0 +1,120 @@
package galaxyoyo.unknown.api.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 void open()
{
JFileChooser jfc = createJFC();
File file = null;
while (file == null)
{
jfc.showOpenDialog(null);
file = jfc.getSelectedFile();
System.out.println(file);
}
open(file);
}
public static void open(File f)
{
System.out.println(f);
try
{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
byte[] bytes = new byte[(int) f.length()];
System.out.println(bis);
while (bis.read(bytes) != -1);
for (byte b : bytes)
{
System.err.println(b);
}
bis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void open(byte[] bytes)
{
}
}

View File

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

View File

@ -3,7 +3,7 @@
*/ */
package galaxyoyo.unknown.client.main; package galaxyoyo.unknown.client.main;
import galaxyoyo.unknown.editor.Editor; import galaxyoyo.unknown.api.editor.EditorAPI;
import galaxyoyo.unknown.frame.MainFrame; import galaxyoyo.unknown.frame.MainFrame;
import java.awt.Color; import java.awt.Color;
@ -185,13 +185,19 @@ public class Main
g.drawLine(0, y, width, y); g.drawLine(0, y, width, y);
} }
Byte[] bytes = Editor.toBytes(baseWidth, baseHeight); Byte[] Bytes = EditorAPI.toBytes(baseWidth, baseHeight);
for (byte b : bytes) byte[] bytes = new byte[Bytes.length];
for (int i = 0; i < Bytes.length; ++i)
{ {
System.err.print(b); bytes[i] = Bytes[i];
} }
EditorAPI.saveAs(bytes);
EditorAPI.open(bytes);
try try
{ {
ImageIO.write(image, "png", new File("img.png")); ImageIO.write(image, "png", new File("img.png"));

View File

@ -1,25 +1,28 @@
package galaxyoyo.unknown.editor; package galaxyoyo.unknown.editor;
import java.util.ArrayList;
import java.util.List;
public class Editor public class Editor
{ {
public static Byte[] toBytes(int width, int height) private final EditorFrame frame;
private byte[] bytes;
public Editor(byte[] bytes)
{ {
List<Byte> bytes = new ArrayList<Byte>(); frame = new EditorFrame();
this.bytes = bytes;
}
for (int x = 1; x < width; x += 16) public EditorFrame getFrame()
{ {
for (int y = 1; y < height; y += 16) return frame;
{ }
bytes.add((byte) 0);
}
bytes.add(Byte.MIN_VALUE);
}
bytes.remove(bytes.lastIndexOf(Byte.MIN_VALUE)); public byte[] getBytes()
{
return bytes;
}
return bytes.toArray(new Byte[0]); public void setBytes(byte[] bytes)
{
this.bytes = bytes;
} }
} }

View File

@ -0,0 +1,12 @@
package galaxyoyo.unknown.editor;
import javax.swing.JFrame;
public class EditorFrame extends JFrame
{
private static final long serialVersionUID = -2705122356101556462L;
public EditorFrame()
{
}
}

View File

@ -4,6 +4,7 @@
package galaxyoyo.unknown.frame; package galaxyoyo.unknown.frame;
import galaxyoyo.unknown.frame.listeners.CreateMapListener; import galaxyoyo.unknown.frame.listeners.CreateMapListener;
import galaxyoyo.unknown.frame.listeners.OpenMapListener;
import java.awt.Dimension; import java.awt.Dimension;
@ -45,7 +46,7 @@ public class MainFrame extends JFrame
private JMenu editer = new JMenu("\u00c9diter"); private JMenu editer = new JMenu("\u00c9diter");
private JMenu editMaps = new JMenu("Cartes"); private JMenu editMaps = new JMenu("Cartes");
private JMenuItem createMap = new JMenuItem("Cr\u00e9er"); private JMenuItem createMap = new JMenuItem("Cr\u00e9er");
private JMenuItem editMap = new JMenuItem("\u00c9diter"); private JMenuItem openMap = new JMenuItem("Ouvrir");
/** /**
* Constructeur * Constructeur
@ -66,7 +67,8 @@ public class MainFrame extends JFrame
createMap.addActionListener(new CreateMapListener()); createMap.addActionListener(new CreateMapListener());
editMaps.add(createMap); editMaps.add(createMap);
editMaps.add(editMap); openMap.addActionListener(new OpenMapListener());
editMaps.add(openMap);
editer.add(editMaps); editer.add(editMaps);
menuBar.add(editer); menuBar.add(editer);

View File

@ -13,13 +13,6 @@ import java.awt.event.ActionListener;
*/ */
public class CreateMapListener implements ActionListener public class CreateMapListener implements ActionListener
{ {
/**
*
*/
public CreateMapListener()
{
}
/* !CodeTemplates.overridecomment.nonjd! /* !CodeTemplates.overridecomment.nonjd!
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/ */
@ -28,5 +21,4 @@ public class CreateMapListener implements ActionListener
{ {
Main.main("--edit", Main.isInDebugMode() ? " --debug true" : ""); Main.main("--edit", Main.isInDebugMode() ? " --debug true" : "");
} }
} }

View File

@ -0,0 +1,18 @@
package galaxyoyo.unknown.frame.listeners;
import galaxyoyo.unknown.api.editor.EditorAPI;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class OpenMapListener implements ActionListener
{
/* !CodeTemplates.overridecomment.nonjd!
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
@Override
public void actionPerformed(ActionEvent event)
{
EditorAPI.open();
}
}