diff --git a/src/main/java/galaxyoyo/unknown/api/editor/EditorAPI.java b/src/main/java/galaxyoyo/unknown/api/editor/EditorAPI.java index ae27483..32240e0 100644 --- a/src/main/java/galaxyoyo/unknown/api/editor/EditorAPI.java +++ b/src/main/java/galaxyoyo/unknown/api/editor/EditorAPI.java @@ -2,6 +2,9 @@ package galaxyoyo.unknown.api.editor; 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.BufferedWriter; import java.io.File; @@ -49,7 +52,7 @@ public class EditorAPI { 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.setFileSelectionMode(JFileChooser.FILES_ONLY); File dir = new File("maps"); @@ -156,6 +159,30 @@ public class EditorAPI 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); } } diff --git a/src/main/java/galaxyoyo/unknown/api/editor/RawMap.java b/src/main/java/galaxyoyo/unknown/api/editor/RawMap.java index 7c5fad9..828d801 100644 --- a/src/main/java/galaxyoyo/unknown/api/editor/RawMap.java +++ b/src/main/java/galaxyoyo/unknown/api/editor/RawMap.java @@ -1,5 +1,6 @@ package galaxyoyo.unknown.api.editor; +import java.awt.image.BufferedImage; import java.util.List; public class RawMap @@ -7,6 +8,7 @@ public class RawMap private List cases; private int width; private int height; + private transient BufferedImage font; public List getCases() { @@ -31,4 +33,14 @@ public class RawMap rm.height = height; return rm; } + + public BufferedImage getFont() + { + return font; + } + + public void setFont(BufferedImage font) + { + this.font = font; + } } diff --git a/src/main/java/galaxyoyo/unknown/client/main/Main.java b/src/main/java/galaxyoyo/unknown/client/main/Main.java index 3f9cd4b..c725f7b 100644 --- a/src/main/java/galaxyoyo/unknown/client/main/Main.java +++ b/src/main/java/galaxyoyo/unknown/client/main/Main.java @@ -170,7 +170,7 @@ public class Main width = baseWidth + ((int) baseWidth / 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(); g.setColor(Color.white); g.fillRect(0, 0, width, height); @@ -188,6 +188,7 @@ public class Main } RawMap rm = EditorAPI.toRawMap(baseWidth, baseHeight); + rm.setFont(image); EditorAPI.saveAs(rm); diff --git a/src/main/java/galaxyoyo/unknown/editor/EditorFrame.java b/src/main/java/galaxyoyo/unknown/editor/EditorFrame.java index 74c1119..cc44ddc 100644 --- a/src/main/java/galaxyoyo/unknown/editor/EditorFrame.java +++ b/src/main/java/galaxyoyo/unknown/editor/EditorFrame.java @@ -1,11 +1,17 @@ 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.event.ComponentEvent; import java.awt.event.ComponentListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; +import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; @@ -17,19 +23,18 @@ public class EditorFrame extends JFrame implements ComponentListener, MouseListe { private static final long serialVersionUID = -2705122356101556462L; - @SuppressWarnings("unused") private final Map map; 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 tabColl = new JPanel(); private final JPanel mapPanel = new JPanel(); private final JTabbedPane resources = new JTabbedPane(); - private final ResourcePanel couche1 = new ResourcePanel(); - private final ResourcePanel couche2 = new ResourcePanel(); - private final ResourcePanel couche3 = new ResourcePanel(); + private final JPanel couche1 = new JPanel(); + private final JPanel couche2 = new JPanel(); + private final JPanel couche3 = new JPanel(); public EditorFrame(Map map) { @@ -39,44 +44,111 @@ public class EditorFrame extends JFrame implements ComponentListener, MouseListe this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.setExtendedState(JFrame.MAXIMIZED_BOTH); this.setLocationRelativeTo(null); + content.setLayout(new BorderLayout()); this.setContentPane(content); this.addComponentListener(this); this.setVisible(true); this.setVisible(false); - tabs.addTab("Carte", new JPanel()); + tabs.addTab("Carte", mapPanel); tabs.addTab("\u00c9vennments", tabEvents); tabs.addTab("Collisions", tabColl); tabs.addMouseListener(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 scroll2 = new JScrollPane(couche2, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); - JScrollPane scroll3 = new JScrollPane(couche3, 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_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); + JScrollPane scroll3 = new JScrollPane(couche3, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); scroll1.getHorizontalScrollBar().setMaximum(0); scroll2.getHorizontalScrollBar().setMaximum(0); scroll3.getHorizontalScrollBar().setMaximum(0); - resources.addTab("1", scroll1); - resources.addTab("2", scroll2); - resources.addTab("3", scroll3); + 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.addMouseListener(this); resources.addChangeListener(this); - content.add(resources); + content.add(resources, BorderLayout.EAST); this.componentResized(null); + resize(); - for (int i = 0; i < 3; ++i) + drawResources(); + + drawMap(); + } + + private void drawMap() + { + if (mapPanel.getGraphics() == null) { - ResourcePanel rp = (ResourcePanel) ((JScrollPane) resources.getComponentAt(i)).getViewport().getComponent(0); - rp.repaint(); + 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 @@ -92,12 +164,30 @@ public class EditorFrame extends JFrame implements ComponentListener, MouseListe @Override public void componentResized(ComponentEvent paramComponentEvent) { - tabs.setPreferredSize(new Dimension(getWidth(), getHeight() / 5)); - tabs.setLocation(0, 0); - mapPanel.setPreferredSize(new Dimension(getWidth() / 4 * 3, getHeight() / 5 * 4)); - mapPanel.setLocation(0, getHeight() / 5); - resources.setPreferredSize(new Dimension(getWidth() / 4, getHeight() / 5 * 4)); - resources.setLocation(getWidth() / 4 * 3, getHeight() / 5); + } + + public void resize() + { + + int cursorPos = ((JScrollPane) resources.getSelectedComponent()).getVerticalScrollBar().getValue(); + tabs.setPreferredSize(new Dimension(getWidth(), getHeight() / 5)); + tabs.setLocation(0, 0); + mapPanel.setPreferredSize(new Dimension(getWidth() / 4 * 3, getHeight() / 5 * 4)); + mapPanel.setLocation(0, getHeight() / 5); + resources.setPreferredSize(new Dimension(getWidth() / 4 - 15, getHeight() / 5 * 4 - 40)); + 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 diff --git a/src/main/java/galaxyoyo/unknown/editor/Map.java b/src/main/java/galaxyoyo/unknown/editor/Map.java index 3747f28..4068285 100644 --- a/src/main/java/galaxyoyo/unknown/editor/Map.java +++ b/src/main/java/galaxyoyo/unknown/editor/Map.java @@ -6,6 +6,7 @@ import galaxyoyo.unknown.api.editor.RawMap; import galaxyoyo.unknown.api.editor.sprites.SpriteRegister; import java.awt.Point; +import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -17,11 +18,13 @@ public class Map private int height; private List cases = new ArrayList(); private java.util.Map casesMap = new HashMap(); + private transient BufferedImage font; public Map(RawMap raw) { this.width = raw.getWidth(); this.height = raw.getHeight(); + this.font = raw.getFont(); for (RawCase rc : raw.getCases()) { @@ -58,6 +61,16 @@ public class Map return casesMap.get(new Point(x, y)); } + public BufferedImage getFont() + { + return font; + } + + public void setFont(BufferedImage font) + { + this.font = font; + } + private void reorganizeMap() { for (Case c : cases) diff --git a/src/main/java/galaxyoyo/unknown/editor/SpriteComp.java b/src/main/java/galaxyoyo/unknown/editor/SpriteComp.java new file mode 100644 index 0000000..c534c90 --- /dev/null +++ b/src/main/java/galaxyoyo/unknown/editor/SpriteComp.java @@ -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); + } +} diff --git a/src/main/java/galaxyoyo/unknown/editor/WrapLayout.java b/src/main/java/galaxyoyo/unknown/editor/WrapLayout.java new file mode 100644 index 0000000..86719d7 --- /dev/null +++ b/src/main/java/galaxyoyo/unknown/editor/WrapLayout.java @@ -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; + } +} \ No newline at end of file