From bb1727bed70d3000824c79808220281b5bbdece4 Mon Sep 17 00:00:00 2001 From: galaxyoyo Date: Sun, 11 Jan 2015 13:20:06 +0100 Subject: [PATCH] =?UTF-8?q?=C3=89dition=20de=20collision=20termin=C3=A9e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../unknown/api/editor/Collision.java | 2 +- .../galaxyoyo/unknown/editor/CollidPanel.java | 104 ++++++++++++++++++ .../galaxyoyo/unknown/editor/EditorFrame.java | 17 ++- .../galaxyoyo/unknown/editor/MapPanel.java | 4 +- .../listeners/CollidMapMouseListener.java | 69 ++++++++++++ 5 files changed, 192 insertions(+), 4 deletions(-) create mode 100644 src/main/java/galaxyoyo/unknown/editor/CollidPanel.java create mode 100644 src/main/java/galaxyoyo/unknown/frame/listeners/CollidMapMouseListener.java diff --git a/src/main/java/galaxyoyo/unknown/api/editor/Collision.java b/src/main/java/galaxyoyo/unknown/api/editor/Collision.java index 9a551ce..8cb9f1d 100644 --- a/src/main/java/galaxyoyo/unknown/api/editor/Collision.java +++ b/src/main/java/galaxyoyo/unknown/api/editor/Collision.java @@ -2,5 +2,5 @@ package galaxyoyo.unknown.api.editor; public enum Collision { - FULL, LEFT, RIGHT, TOP, BOTTOM, ANY; + FULL, PARTIAL, ANY; } diff --git a/src/main/java/galaxyoyo/unknown/editor/CollidPanel.java b/src/main/java/galaxyoyo/unknown/editor/CollidPanel.java new file mode 100644 index 0000000..658a8d7 --- /dev/null +++ b/src/main/java/galaxyoyo/unknown/editor/CollidPanel.java @@ -0,0 +1,104 @@ +package galaxyoyo.unknown.editor; + +import galaxyoyo.unknown.api.editor.Case; +import galaxyoyo.unknown.api.editor.Collision; + +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.image.BufferedImage; + +import javax.swing.JPanel; + +public class CollidPanel extends JPanel +{ + private static final long serialVersionUID = -138754019431984881L; + + private final EditorFrame frame; + + public CollidPanel(EditorFrame frame) + { + super (); + this.frame = frame; + } + + public EditorFrame getFrame() + { + return frame; + } + + public Map getMap() + { + return frame.getMap(); + } + + @Override + public void paintComponent(Graphics g) + { + g.fillRect(0, 0, getWidth(), getHeight()); + BufferedImage img = getMap().getFont(); + int x = getWidth() / 2 - img.getWidth(); + int y = getHeight() / 2 - img.getHeight(); + int width = img.getWidth() * 2; + int height = img.getHeight() * 2; + g.drawImage(getMap().getFont(), x, y, width, height, null); + + for (Case c : getMap().getAllCases()) + { + if (isEmpty(c.getCoucheOne().getImage())) + continue; + + g.drawImage(c.getCoucheOne().getImage(), x + c.getPosX() * 34 + 2, y + c.getPosY() * 34 + 2, 32, 32, null); + + if (isEmpty(c.getCoucheTwo().getImage())) + continue; + + g.drawImage(c.getCoucheTwo().getImage(), x + c.getPosX() * 34 + 2, y + c.getPosY() * 34 + 2, 32, 32, null); + + if (isEmpty(c.getCoucheThree().getImage())) + continue; + + g.drawImage(c.getCoucheThree().getImage(), x + c.getPosX() * 34 + 2, y + c.getPosY() * 34 + 2, 32, 32, null); + } + + for (Case c : getMap().getAllCases()) + { + if (c.getCollision() != Collision.ANY) + { + BufferedImage alpha = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB); + + if (c.getCollision() == Collision.FULL) + { + Graphics2D grap = alpha.createGraphics(); + grap.setColor(new Color(0, 0, 0, 100)); + grap.fillRect(0, 0, 16, 16); + grap.dispose(); + } + else if (c.getCollision() == Collision.PARTIAL) + { + Graphics2D grap = alpha.createGraphics(); + grap.setColor(new Color(255, 0, 255, 70)); + grap.fillRect(0, 0, 16, 16); + grap.dispose(); + } + + g.drawImage(alpha, x + c.getPosX() * 34 + 2, y + c.getPosY() * 34 + 2, 32, 32, null); + } + } + } + + private boolean isEmpty(BufferedImage image) + { + int allrgba = 0; + + for (int x = 0; x < image.getWidth(); ++x) + { + for (int y = 0; y < image.getHeight(); ++y) + { + allrgba += image.getRGB(x, y) + 1; + } + } + + return allrgba == 0; + } +} diff --git a/src/main/java/galaxyoyo/unknown/editor/EditorFrame.java b/src/main/java/galaxyoyo/unknown/editor/EditorFrame.java index 6c143ae..6826750 100644 --- a/src/main/java/galaxyoyo/unknown/editor/EditorFrame.java +++ b/src/main/java/galaxyoyo/unknown/editor/EditorFrame.java @@ -5,6 +5,7 @@ import galaxyoyo.unknown.api.editor.RawMap; import galaxyoyo.unknown.api.editor.sprites.Category; import galaxyoyo.unknown.api.editor.sprites.Sprite; import galaxyoyo.unknown.api.editor.sprites.SpriteRegister; +import galaxyoyo.unknown.frame.listeners.CollidMapMouseListener; import galaxyoyo.unknown.frame.listeners.CreateMapListener; import galaxyoyo.unknown.frame.listeners.MapMouseListener; import galaxyoyo.unknown.frame.listeners.OpenMapListener; @@ -48,7 +49,7 @@ public class EditorFrame extends JFrame implements ChangeListener, ActionListene private JMenuItem exit = new JMenuItem("Quitter"); private final JTabbedPane tabs = new JTabbedPane(); private final JPanel tabEvents = new JPanel(); - private final JPanel tabColl = new JPanel(); + private final CollidPanel tabColl = new CollidPanel(this); private final MapPanel mapPanel = new MapPanel(this); private final JTabbedPane resources = new JTabbedPane(); private final JPanel couche1 = new JPanel(); @@ -64,6 +65,7 @@ public class EditorFrame extends JFrame implements ChangeListener, ActionListene this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.setExtendedState(JFrame.MAXIMIZED_BOTH); this.setLocationRelativeTo(null); + this.addWindowListener(this); content.setLayout(new BorderLayout()); this.setContentPane(content); this.setVisible(true); @@ -100,9 +102,13 @@ public class EditorFrame extends JFrame implements ChangeListener, ActionListene mapPanel.addMouseListener(new MapMouseListener(mapPanel, this)); mapPanel.addMouseMotionListener(new MapMouseListener(mapPanel, this)); + tabColl.addMouseListener(new CollidMapMouseListener(tabColl, this)); + tabColl.addMouseMotionListener(new CollidMapMouseListener(tabColl, this)); + tabs.addTab("Carte", new JScrollPane(mapPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED)); tabs.addTab("\u00c9vennments", new JScrollPane(tabEvents)); tabs.addTab("Collisions", new JScrollPane(tabColl)); + tabs.addChangeListener(this); content.add(tabs, BorderLayout.CENTER); @@ -240,6 +246,15 @@ public class EditorFrame extends JFrame implements ChangeListener, ActionListene resources.setBackgroundAt(2, Color.white); } + repaint(); + } + else if (event.getSource() == tabs) + { + resources.setEnabled(tabs.getSelectedIndex() == 0); + couche1.setEnabled(resources.isEnabled()); + couche2.setEnabled(resources.isEnabled()); + couche3.setEnabled(resources.isEnabled()); + repaint(); } } diff --git a/src/main/java/galaxyoyo/unknown/editor/MapPanel.java b/src/main/java/galaxyoyo/unknown/editor/MapPanel.java index 49edce3..8da5ab2 100644 --- a/src/main/java/galaxyoyo/unknown/editor/MapPanel.java +++ b/src/main/java/galaxyoyo/unknown/editor/MapPanel.java @@ -48,8 +48,8 @@ public class MapPanel extends JPanel // BufferedImage image; g.drawImage(c.getCoucheOne().getImage(), x + c.getPosX() * 34 + 2, y + c.getPosY() * 34 + 2, 32, 32, null); - if (frame.getSelectedLayerIndex() != 0) - /* { + /* if (frame.getSelectedLayerIndex() != 0) + { image = recalculateAplha(c.getCoucheOne().getImage(), 0); g.drawImage(image, x + c.getPosX() * 34 + 2, y + c.getPosY() * 34 + 2, 32, 32, null); }*/ diff --git a/src/main/java/galaxyoyo/unknown/frame/listeners/CollidMapMouseListener.java b/src/main/java/galaxyoyo/unknown/frame/listeners/CollidMapMouseListener.java new file mode 100644 index 0000000..3aaaeb9 --- /dev/null +++ b/src/main/java/galaxyoyo/unknown/frame/listeners/CollidMapMouseListener.java @@ -0,0 +1,69 @@ +package galaxyoyo.unknown.frame.listeners; + +import galaxyoyo.unknown.api.editor.Case; +import galaxyoyo.unknown.api.editor.Collision; +import galaxyoyo.unknown.editor.CollidPanel; +import galaxyoyo.unknown.editor.EditorFrame; +import galaxyoyo.unknown.editor.Map; + +import java.awt.Cursor; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; + +public class CollidMapMouseListener extends MouseAdapter +{ + private final EditorFrame frame; + private final CollidPanel panel; + + public CollidMapMouseListener(CollidPanel panel, EditorFrame frame) + { + this.frame = frame; + this.panel = panel; + } + + public EditorFrame getFrame() + { + return frame; + } + + @Override + public void mouseMoved(MouseEvent event) + { + Map map = getFrame().getMap(); + + int x = panel.getWidth() / 2 - map.getFont().getWidth(); + int y = panel.getHeight() / 2 - map.getFont().getHeight(); + + if (map.getCase((event.getX() - x - 2) / 34, (event.getY() - y - 2) / 34) != null && event.getX() - x >= 2 && event.getY() - y >= 2) + { + getFrame().setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); + } + else + { + getFrame().setCursor(Cursor.getDefaultCursor()); + } + } + + @Override + public void mouseReleased(MouseEvent event) + { + Map map = getFrame().getMap(); + + int x = panel.getWidth() / 2 - map.getFont().getWidth(); + int y = panel.getHeight() / 2 - map.getFont().getHeight(); + Case c = null; + + if ((c = map.getCase((event.getX() - x + 2) / 34, (event.getY() - y + 2) / 34)) != null && event.getX() - x >= 2 && event.getY() - y >= 2) + { + int colIndex = c.getCollision().ordinal(); + int newColIndex = colIndex + 1; + if (newColIndex >= Collision.values().length) + newColIndex = 0; + Collision col = Collision.values()[newColIndex]; + Case n = Case.create(c.getPosX(), c.getPosY(), c.getCoucheOne(), c.getCoucheTwo(), c.getCoucheThree(), col); + + map.setCase((event.getX() - x + 2) / 34, (event.getY() - y + 2) / 34, n); + panel.repaint(); + } + } +}