Amélioré grandement interface d'édition

This commit is contained in:
galaxyoyo 2015-01-10 11:45:19 +01:00
parent 2813f325dc
commit a5ef697469
7 changed files with 331 additions and 26 deletions

View File

@ -2,6 +2,9 @@ package galaxyoyo.unknown.api.editor;
import galaxyoyo.unknown.editor.Map; import galaxyoyo.unknown.editor.Map;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
@ -49,7 +52,7 @@ public class EditorAPI
{ {
JFileChooser jfc = new JFileChooser(); JFileChooser jfc = new JFileChooser();
jfc.setFileFilter(new FileNameExtensionFilter("Fichiers monde (*.gworld, *.dat)", "gworld", "dat")); jfc.setFileFilter(new FileNameExtensionFilter("Fichiers monde (*.gmap, *.dat)", "gmap", "dat"));
jfc.setFileHidingEnabled(true); jfc.setFileHidingEnabled(true);
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY); jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
File dir = new File("maps"); File dir = new File("maps");
@ -156,6 +159,30 @@ public class EditorAPI
public static Map open(RawMap map) public static Map open(RawMap map)
{ {
if (map.getFont() == null)
{
int baseWidth = map.getWidth();
int baseHeight = map.getHeight();
int width = baseWidth + ((int) baseWidth / 16) + 1;
int height = baseHeight + ((int) baseHeight / 16) + 1;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
g.setColor(Color.black);
g.drawLine(0, 0, width, 0);
g.drawLine(0, 0, 0, height);
for (int x = 17; x <= width; x += 17)
{
g.drawLine(x, 0, x, height);
}
for (int y = 17; y <= height; y += 17)
{
g.drawLine(0, y, width, y);
}
}
return new Map(map); return new Map(map);
} }
} }

View File

@ -1,5 +1,6 @@
package galaxyoyo.unknown.api.editor; package galaxyoyo.unknown.api.editor;
import java.awt.image.BufferedImage;
import java.util.List; import java.util.List;
public class RawMap public class RawMap
@ -7,6 +8,7 @@ public class RawMap
private List<RawCase> cases; private List<RawCase> cases;
private int width; private int width;
private int height; private int height;
private transient BufferedImage font;
public List<RawCase> getCases() public List<RawCase> getCases()
{ {
@ -31,4 +33,14 @@ public class RawMap
rm.height = height; rm.height = height;
return rm; return rm;
} }
public BufferedImage getFont()
{
return font;
}
public void setFont(BufferedImage font)
{
this.font = font;
}
} }

View File

@ -170,7 +170,7 @@ public class Main
width = baseWidth + ((int) baseWidth / 16) + 1; width = baseWidth + ((int) baseWidth / 16) + 1;
height = baseHeight + ((int) baseHeight / 16) + 1; height = baseHeight + ((int) baseHeight / 16) + 1;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics(); Graphics2D g = image.createGraphics();
g.setColor(Color.white); g.setColor(Color.white);
g.fillRect(0, 0, width, height); g.fillRect(0, 0, width, height);
@ -188,6 +188,7 @@ public class Main
} }
RawMap rm = EditorAPI.toRawMap(baseWidth, baseHeight); RawMap rm = EditorAPI.toRawMap(baseWidth, baseHeight);
rm.setFont(image);
EditorAPI.saveAs(rm); EditorAPI.saveAs(rm);

View File

@ -1,11 +1,17 @@
package galaxyoyo.unknown.editor; package galaxyoyo.unknown.editor;
import galaxyoyo.unknown.api.editor.sprites.Category;
import galaxyoyo.unknown.api.editor.sprites.Sprite;
import galaxyoyo.unknown.api.editor.sprites.SpriteRegister;
import java.awt.BorderLayout;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.event.ComponentEvent; import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener; import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseListener; import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JScrollPane; import javax.swing.JScrollPane;
@ -17,19 +23,18 @@ public class EditorFrame extends JFrame implements ComponentListener, MouseListe
{ {
private static final long serialVersionUID = -2705122356101556462L; private static final long serialVersionUID = -2705122356101556462L;
@SuppressWarnings("unused")
private final Map map; private final Map map;
private final JPanel content = new JPanel(); private final JPanel content = new JPanel();
private final JTabbedPane tabs = new JTabbedPane(JTabbedPane.BOTTOM); private final JTabbedPane tabs = new JTabbedPane();
private final JPanel tabEvents = new JPanel(); private final JPanel tabEvents = new JPanel();
private final JPanel tabColl = new JPanel(); private final JPanel tabColl = new JPanel();
private final JPanel mapPanel = new JPanel(); private final JPanel mapPanel = new JPanel();
private final JTabbedPane resources = new JTabbedPane(); private final JTabbedPane resources = new JTabbedPane();
private final ResourcePanel couche1 = new ResourcePanel(); private final JPanel couche1 = new JPanel();
private final ResourcePanel couche2 = new ResourcePanel(); private final JPanel couche2 = new JPanel();
private final ResourcePanel couche3 = new ResourcePanel(); private final JPanel couche3 = new JPanel();
public EditorFrame(Map map) public EditorFrame(Map map)
{ {
@ -39,44 +44,111 @@ public class EditorFrame extends JFrame implements ComponentListener, MouseListe
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setExtendedState(JFrame.MAXIMIZED_BOTH); this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setLocationRelativeTo(null); this.setLocationRelativeTo(null);
content.setLayout(new BorderLayout());
this.setContentPane(content); this.setContentPane(content);
this.addComponentListener(this); this.addComponentListener(this);
this.setVisible(true); this.setVisible(true);
this.setVisible(false); this.setVisible(false);
tabs.addTab("Carte", new JPanel()); tabs.addTab("Carte", mapPanel);
tabs.addTab("\u00c9vennments", tabEvents); tabs.addTab("\u00c9vennments", tabEvents);
tabs.addTab("Collisions", tabColl); tabs.addTab("Collisions", tabColl);
tabs.addMouseListener(this); tabs.addMouseListener(this);
tabs.addChangeListener(this); tabs.addChangeListener(this);
content.add(tabs); content.add(tabs, BorderLayout.CENTER);
content.add(mapPanel); couche1.setLayout(new WrapLayout(WrapLayout.LEFT));
couche2.setLayout(new WrapLayout(WrapLayout.LEFT));
couche3.setLayout(new WrapLayout(WrapLayout.LEFT));
JScrollPane scroll1 = new JScrollPane(couche1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); JScrollPane scroll1 = new JScrollPane(couche1, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JScrollPane scroll2 = new JScrollPane(couche2, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); JScrollPane scroll2 = new JScrollPane(couche2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JScrollPane scroll3 = new JScrollPane(couche3, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); JScrollPane scroll3 = new JScrollPane(couche3, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroll1.getHorizontalScrollBar().setMaximum(0); scroll1.getHorizontalScrollBar().setMaximum(0);
scroll2.getHorizontalScrollBar().setMaximum(0); scroll2.getHorizontalScrollBar().setMaximum(0);
scroll3.getHorizontalScrollBar().setMaximum(0); scroll3.getHorizontalScrollBar().setMaximum(0);
resources.addTab("1", scroll1); resources.addTab("", new ImageIcon(getClass().getResource("/assets/unknown/textures/layer 1.png")), scroll1);
resources.addTab("2", scroll2); resources.addTab("", new ImageIcon(getClass().getResource("/assets/unknown/textures/layer 2.png")), scroll2);
resources.addTab("3", scroll3); resources.addTab("", new ImageIcon(getClass().getResource("/assets/unknown/textures/layer 3.png")), scroll3);
resources.addMouseListener(this); resources.addMouseListener(this);
resources.addChangeListener(this); resources.addChangeListener(this);
content.add(resources); content.add(resources, BorderLayout.EAST);
this.componentResized(null); this.componentResized(null);
resize();
for (int i = 0; i < 3; ++i) drawResources();
{
ResourcePanel rp = (ResourcePanel) ((JScrollPane) resources.getComponentAt(i)).getViewport().getComponent(0); drawMap();
rp.repaint();
} }
private void drawMap()
{
if (mapPanel.getGraphics() == null)
{
mapPanel.repaint();
}
mapPanel.getGraphics().drawImage(map.getFont(), 0, 0, null);
mapPanel.revalidate();
mapPanel.repaint();
repaint();
}
private void drawResources()
{
int x = 0;
int y = 0;
couche1.removeAll();
couche2.removeAll();
couche3.removeAll();
if (couche1.getComponents().length > 0)
{
return;
}
if (couche1.getWidth() == 0 || couche2.getWidth() == 0 || couche3.getWidth() == 0)
{
couche1.repaint();
couche2.repaint();
couche3.repaint();
}
for (Category cat : SpriteRegister.getAllCategories())
{
for (Sprite spr : cat.getSprites())
{
SpriteComp sprc1 = new SpriteComp(spr, 1);
SpriteComp sprc2 = new SpriteComp(spr, 2);
SpriteComp sprc3 = new SpriteComp(spr, 3);
sprc1.setLocation(x, y);
sprc2.setLocation(x, y);
sprc3.setLocation(x, y);
couche1.add(sprc1);
couche2.add(sprc2);
couche3.add(sprc3);
x += 48;
if (couche1.getWidth() - x < 48)
{
x = 0;
y += 48;
}
}
}
couche1.revalidate();
couche2.revalidate();
couche3.revalidate();
couche1.repaint();
couche2.repaint();
couche3.repaint();
} }
@Override @Override
@ -92,12 +164,30 @@ public class EditorFrame extends JFrame implements ComponentListener, MouseListe
@Override @Override
public void componentResized(ComponentEvent paramComponentEvent) public void componentResized(ComponentEvent paramComponentEvent)
{ {
}
public void resize()
{
int cursorPos = ((JScrollPane) resources.getSelectedComponent()).getVerticalScrollBar().getValue();
tabs.setPreferredSize(new Dimension(getWidth(), getHeight() / 5)); tabs.setPreferredSize(new Dimension(getWidth(), getHeight() / 5));
tabs.setLocation(0, 0); tabs.setLocation(0, 0);
mapPanel.setPreferredSize(new Dimension(getWidth() / 4 * 3, getHeight() / 5 * 4)); mapPanel.setPreferredSize(new Dimension(getWidth() / 4 * 3, getHeight() / 5 * 4));
mapPanel.setLocation(0, getHeight() / 5); mapPanel.setLocation(0, getHeight() / 5);
resources.setPreferredSize(new Dimension(getWidth() / 4, getHeight() / 5 * 4)); resources.setPreferredSize(new Dimension(getWidth() / 4 - 15, getHeight() / 5 * 4 - 40));
resources.setLocation(getWidth() / 4 * 3, getHeight() / 5); resources.setLocation(getWidth() / 4 * 3, getHeight() / 5);
JScrollPane scroll1 = (JScrollPane) resources.getComponent(0);
JScrollPane scroll2 = (JScrollPane) resources.getComponent(1);
JScrollPane scroll3 = (JScrollPane) resources.getComponent(2);
scroll1.getHorizontalScrollBar().setMaximum(0);
scroll2.getHorizontalScrollBar().setMaximum(0);
scroll3.getHorizontalScrollBar().setMaximum(0);
drawResources();
((JScrollPane) resources.getSelectedComponent()).getVerticalScrollBar().setValue(cursorPos);
} }
@Override @Override

View File

@ -6,6 +6,7 @@ import galaxyoyo.unknown.api.editor.RawMap;
import galaxyoyo.unknown.api.editor.sprites.SpriteRegister; import galaxyoyo.unknown.api.editor.sprites.SpriteRegister;
import java.awt.Point; import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -17,11 +18,13 @@ public class Map
private int height; private int height;
private List<Case> cases = new ArrayList<Case>(); private List<Case> cases = new ArrayList<Case>();
private java.util.Map<Point, Case> casesMap = new HashMap<Point, Case>(); private java.util.Map<Point, Case> casesMap = new HashMap<Point, Case>();
private transient BufferedImage font;
public Map(RawMap raw) public Map(RawMap raw)
{ {
this.width = raw.getWidth(); this.width = raw.getWidth();
this.height = raw.getHeight(); this.height = raw.getHeight();
this.font = raw.getFont();
for (RawCase rc : raw.getCases()) for (RawCase rc : raw.getCases())
{ {
@ -58,6 +61,16 @@ public class Map
return casesMap.get(new Point(x, y)); return casesMap.get(new Point(x, y));
} }
public BufferedImage getFont()
{
return font;
}
public void setFont(BufferedImage font)
{
this.font = font;
}
private void reorganizeMap() private void reorganizeMap()
{ {
for (Case c : cases) for (Case c : cases)

View File

@ -0,0 +1,50 @@
package galaxyoyo.unknown.editor;
import galaxyoyo.unknown.api.editor.sprites.Sprite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JComponent;
public class SpriteComp extends JComponent
{
private static final long serialVersionUID = -6512257366877053285L;
private final Sprite sprite;
private final int couche;
public SpriteComp(Sprite sprite, int couche)
{
super ();
this.sprite = sprite;
this.couche = couche;
this.setMinimumSize(new Dimension(32, 32));
this.setMaximumSize(new Dimension(32, 32));
this.setPreferredSize(new Dimension(32, 32));
this.setSize(new Dimension(32, 32));
repaint();
}
public Sprite getSprite()
{
return sprite;
}
public int getCouche()
{
return couche;
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.white);
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(sprite.getImage(), 0, 0, 32, 32, Color.white, null);
}
}

View File

@ -0,0 +1,112 @@
package galaxyoyo.unknown.editor;
import java.awt.*;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class WrapLayout extends FlowLayout
{
private static final long serialVersionUID = 8777237960365591646L;
public WrapLayout()
{
super();
}
public WrapLayout(int align)
{
super(align);
}
public WrapLayout(int align, int hgap, int vgap)
{
super(align, hgap, vgap);
}
@Override
public Dimension preferredLayoutSize(Container target)
{
return layoutSize(target, true);
}
@Override
public Dimension minimumLayoutSize(Container target)
{
Dimension minimum = layoutSize(target, false);
minimum.width -= (getHgap() + 1);
return minimum;
}
private Dimension layoutSize(Container target, boolean preferred)
{
synchronized (target.getTreeLock())
{
int targetWidth = target.getSize().width;
if (targetWidth == 0)
targetWidth = Integer.MAX_VALUE;
int hgap = getHgap();
int vgap = getVgap();
Insets insets = target.getInsets();
int horizontalInsetsAndGap = insets.left + insets.right + (hgap * 2);
int maxWidth = targetWidth - horizontalInsetsAndGap;
Dimension dim = new Dimension(0, 0);
int rowWidth = 0;
int rowHeight = 0;
int nmembers = target.getComponentCount();
for (int i = 0; i < nmembers; i++)
{
Component m = target.getComponent(i);
if (m.isVisible())
{
Dimension d = preferred ? m.getPreferredSize() : m.getMinimumSize();
if (rowWidth + d.width > maxWidth)
{
addRow(dim, rowWidth, rowHeight);
rowWidth = 0;
rowHeight = 0;
}
if (rowWidth != 0)
{
rowWidth += hgap;
}
rowWidth += d.width;
rowHeight = Math.max(rowHeight, d.height);
}
}
addRow(dim, rowWidth, rowHeight);
dim.width += horizontalInsetsAndGap;
dim.height += insets.top + insets.bottom + vgap * 2;
Container scrollPane = SwingUtilities.getAncestorOfClass(JScrollPane.class, target);
if (scrollPane != null)
{
dim.width -= (hgap + 1);
}
return dim;
}
}
private void addRow(Dimension dim, int rowWidth, int rowHeight)
{
dim.width = Math.max(dim.width, rowWidth);
if (dim.height > 0)
{
dim.height += getVgap();
}
dim.height += rowHeight;
}
}