From ce485f2263b268097b7aa69d858adb7f9ff6f633 Mon Sep 17 00:00:00 2001 From: philippe lhardy Date: Tue, 28 Nov 2017 22:42:17 +0100 Subject: [PATCH] draging mouse with button pushed over maze clear walls warning this break resolving ( endless => hang ) TO FIX. --- java/org/artisanlogiciel/games/Display.java | 39 ++++++++++++++++--- java/org/artisanlogiciel/games/LabyModel.java | 15 ++++++- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/java/org/artisanlogiciel/games/Display.java b/java/org/artisanlogiciel/games/Display.java index 85894c4..26408e0 100644 --- a/java/org/artisanlogiciel/games/Display.java +++ b/java/org/artisanlogiciel/games/Display.java @@ -12,6 +12,8 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; +import java.awt.event.MouseEvent; +import java.awt.event.MouseMotionListener; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -69,8 +71,11 @@ public class Display extends JFrame } this.model = model; maze = createMazeComponent(model,W,H); + Container con = this.getContentPane(); - con.add(new JScrollPane(maze), BorderLayout.CENTER); + JScrollPane scrollableMaze = new JScrollPane(maze); + scrollableMaze.addMouseMotionListener(maze); + con.add(scrollableMaze, BorderLayout.CENTER); controler = new MazeControler(params); con.add(controler.getMoveControl(), BorderLayout.NORTH); con.add(controler.getGenerationControl(), BorderLayout.SOUTH); @@ -572,11 +577,16 @@ public class Display extends JFrame } - private static class MazeComponent extends JComponent implements MazeCreationListener, MazeResolutionListener + private static class MazeComponent + extends JComponent + implements MazeCreationListener, + MazeResolutionListener, + MouseMotionListener { private static final long serialVersionUID = 3163272907991176390L; - WallsProvider map; + // WallsProvider map; + LabyModel map; final MazeCellParameters cp; Position current = null; LinkedList solvedPath = null; @@ -588,6 +598,21 @@ public class Display extends JFrame int gX = -1; int gY = -1; + @Override + public void mouseDragged(MouseEvent e) { + // should find the cell ... + int pX = (int) ((double) (e.getX() - cp.getOffsetX()) / cp.getWidth()); + int pY = (int) ((double) (e.getY() - cp.getOffsetY()) / cp.getHeight()); + System.out.println("Mouse dragged Cell [" + pX + ',' + pY + ']'); + map.noWalls(pX,pY); + changed(null,null,map); + } + + @Override + public void mouseMoved(MouseEvent e) { + System.out.println("Mouse moved (" + e.getX() + ',' + e.getY() + ')'); + } + void checkExit() { if ((sX == gX) && (sY == gY)) @@ -659,7 +684,8 @@ public class Display extends JFrame width, height of one cell, offsetX, offsetY of upper left corner **/ - public MazeComponent(WallsProvider map, MazeCellParameters cp) + // public MazeComponent(WallsProvider map, MazeCellParameters cp) + public MazeComponent(LabyModel map, MazeCellParameters cp) { super(); this.cp = cp; @@ -672,7 +698,8 @@ public class Display extends JFrame /** warning works only if new map has same dimensions than previous */ - public void resetWallsProvider(WallsProvider map) + // public void resetWallsProvider(WallsProvider map) + public void resetWallsProvider(LabyModel map) { this.map = map; } @@ -782,6 +809,8 @@ public class Display extends JFrame } g.setColor(Color.black); + + // draw all walls within clip bounds horiz first then lines for (; pY < mY; pY++) { for (pX = 0; pX < mX; pX++) diff --git a/java/org/artisanlogiciel/games/LabyModel.java b/java/org/artisanlogiciel/games/LabyModel.java index b74afde..fceb05a 100644 --- a/java/org/artisanlogiciel/games/LabyModel.java +++ b/java/org/artisanlogiciel/games/LabyModel.java @@ -61,6 +61,7 @@ public class LabyModel implements WallsProvider private int width; private int height; + // wall flags + status flags for each position (x,y) private short[][] t; private int depth = 0; /** @@ -114,6 +115,17 @@ public class LabyModel implements WallsProvider this.listener = listener; } + // FIXME FULLY BREAK RESOLVING... + public void noWalls(int x, int y) + { + if ((x > 0) && (x < width) && (y > 0) && (y < height)) + { + // t[x][y] |= DIRECTION | VERTICAL | POSITIVE | HORIZONTAL | NEGATIVE | LEFT | RIGHT | UP | DOWN; + t[x][y] |= DIRECTION | VERTICAL | POSITIVE | HORIZONTAL | NEGATIVE; + t[x][y] |= LEFT | RIGHT | UP | DOWN; + } + } + // entry and exit can be outside the model boundaries a one x or one y out. public boolean addEntryOrExit(int x, int y) { @@ -395,7 +407,8 @@ public class LabyModel implements WallsProvider { for (int x = 0; x < width; x++) { - t[x][y] &= ~OPEN; + // resetCell(x,y); + t[x][y] &= ~OPEN; } }