mirror of
				https://github.com/ynerant/Level-Editor.git
				synced 2025-11-03 23:52:05 +01:00 
			
		
		
		
	Ajouté fonctions pour ouverture/sauvegarde d'une carte
This commit is contained in:
		@@ -13,6 +13,7 @@
 | 
			
		||||
		</attributes>
 | 
			
		||||
	</classpathentry>
 | 
			
		||||
	<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">
 | 
			
		||||
		<attributes>
 | 
			
		||||
			<attribute name="maven.pomderived" value="true"/>
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										120
									
								
								src/api/java/galaxyoyo/unknown/api/editor/EditorAPI.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										120
									
								
								src/api/java/galaxyoyo/unknown/api/editor/EditorAPI.java
									
									
									
									
									
										Normal 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)
 | 
			
		||||
	{
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,4 @@
 | 
			
		||||
/**
 | 
			
		||||
 * @author galaxyoyo
 | 
			
		||||
 */
 | 
			
		||||
package galaxyoyo.unknown.api.editor;
 | 
			
		||||
@@ -3,7 +3,7 @@
 | 
			
		||||
 */
 | 
			
		||||
package galaxyoyo.unknown.client.main;
 | 
			
		||||
 | 
			
		||||
import galaxyoyo.unknown.editor.Editor;
 | 
			
		||||
import galaxyoyo.unknown.api.editor.EditorAPI;
 | 
			
		||||
import galaxyoyo.unknown.frame.MainFrame;
 | 
			
		||||
 | 
			
		||||
import java.awt.Color;
 | 
			
		||||
@@ -185,13 +185,19 @@ public class Main
 | 
			
		||||
			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
 | 
			
		||||
		{
 | 
			
		||||
			ImageIO.write(image, "png", new File("img.png"));
 | 
			
		||||
 
 | 
			
		||||
@@ -1,25 +1,28 @@
 | 
			
		||||
package galaxyoyo.unknown.editor;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
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>();
 | 
			
		||||
		
 | 
			
		||||
		for (int x = 1; x < width; x += 16)
 | 
			
		||||
		{
 | 
			
		||||
			for (int y = 1; y < height; y += 16)
 | 
			
		||||
			{
 | 
			
		||||
				bytes.add((byte) 0);
 | 
			
		||||
			}
 | 
			
		||||
			bytes.add(Byte.MIN_VALUE);
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		bytes.remove(bytes.lastIndexOf(Byte.MIN_VALUE));
 | 
			
		||||
		
 | 
			
		||||
		return bytes.toArray(new Byte[0]);
 | 
			
		||||
		frame = new EditorFrame();
 | 
			
		||||
		this.bytes = bytes;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public EditorFrame getFrame()
 | 
			
		||||
	{
 | 
			
		||||
		return frame;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public byte[] getBytes()
 | 
			
		||||
	{
 | 
			
		||||
		return bytes;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public void setBytes(byte[] bytes)
 | 
			
		||||
	{
 | 
			
		||||
		this.bytes = bytes;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										12
									
								
								src/main/java/galaxyoyo/unknown/editor/EditorFrame.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								src/main/java/galaxyoyo/unknown/editor/EditorFrame.java
									
									
									
									
									
										Normal 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()
 | 
			
		||||
	{
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -4,6 +4,7 @@
 | 
			
		||||
package galaxyoyo.unknown.frame;
 | 
			
		||||
 | 
			
		||||
import galaxyoyo.unknown.frame.listeners.CreateMapListener;
 | 
			
		||||
import galaxyoyo.unknown.frame.listeners.OpenMapListener;
 | 
			
		||||
 | 
			
		||||
import java.awt.Dimension;
 | 
			
		||||
 | 
			
		||||
@@ -45,7 +46,7 @@ public class MainFrame extends JFrame
 | 
			
		||||
	private JMenu editer = new JMenu("\u00c9diter");
 | 
			
		||||
	private JMenu editMaps = new JMenu("Cartes");
 | 
			
		||||
	private JMenuItem createMap = new JMenuItem("Cr\u00e9er");
 | 
			
		||||
	private JMenuItem editMap = new JMenuItem("\u00c9diter");
 | 
			
		||||
	private JMenuItem openMap = new JMenuItem("Ouvrir");
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Constructeur
 | 
			
		||||
@@ -66,7 +67,8 @@ public class MainFrame extends JFrame
 | 
			
		||||
		
 | 
			
		||||
		createMap.addActionListener(new CreateMapListener());
 | 
			
		||||
		editMaps.add(createMap);
 | 
			
		||||
		editMaps.add(editMap);
 | 
			
		||||
		openMap.addActionListener(new OpenMapListener());
 | 
			
		||||
		editMaps.add(openMap);
 | 
			
		||||
		editer.add(editMaps);
 | 
			
		||||
		
 | 
			
		||||
		menuBar.add(editer);
 | 
			
		||||
 
 | 
			
		||||
@@ -13,13 +13,6 @@ import java.awt.event.ActionListener;
 | 
			
		||||
 */
 | 
			
		||||
public class CreateMapListener implements ActionListener
 | 
			
		||||
{
 | 
			
		||||
	/**
 | 
			
		||||
	 * 
 | 
			
		||||
	 */
 | 
			
		||||
	public CreateMapListener()
 | 
			
		||||
	{
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/* !CodeTemplates.overridecomment.nonjd!
 | 
			
		||||
	 * @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" : "");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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();
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user