From b36032057062456ca70eca1ffe8975ce480ad1af Mon Sep 17 00:00:00 2001 From: philippe lhardy Date: Tue, 5 Dec 2017 21:52:12 +0100 Subject: [PATCH] QnD fix for drawing ( seen when joining cells touching only by one point ) --- .../games/DirectionPosition.java | 34 +++++++++++- java/org/artisanlogiciel/games/Display.java | 55 ++++++++++++------- java/org/artisanlogiciel/games/LabyModel.java | 10 ++-- 3 files changed, 72 insertions(+), 27 deletions(-) diff --git a/java/org/artisanlogiciel/games/DirectionPosition.java b/java/org/artisanlogiciel/games/DirectionPosition.java index 47351e1..96567ac 100644 --- a/java/org/artisanlogiciel/games/DirectionPosition.java +++ b/java/org/artisanlogiciel/games/DirectionPosition.java @@ -40,6 +40,38 @@ public class DirectionPosition } return "?"; } - + + // create a new DirectionPosition from this using one possible direction. + DirectionPosition moveToAdjacentDirection() + { + short pointingdirection = 0; + Position p = null; + if (LabyModel.isFlagSet(direction,LabyModel.RIGHT)) + { + p = new Position(position.getX() + 1, position.getY()); + pointingdirection |= LabyModel.LEFT; + } + else if (LabyModel.isFlagSet(direction,LabyModel.LEFT)) + { + p = new Position(position.getX() -1 ,position.getY()); + pointingdirection |= LabyModel.RIGHT; + } + else if (LabyModel.isFlagSet(direction,LabyModel.UP)) + { + p = new Position(position.getX(),position.getY() -1); + pointingdirection |= LabyModel.DOWN; + } + else if (LabyModel.isFlagSet(direction,LabyModel.DOWN)) + { + p = new Position(position.getX(),position.getY() + 1); + pointingdirection |= LabyModel.UP; + } + else + { + p = position; + } + + return new DirectionPosition((short) pointingdirection, p); + } } diff --git a/java/org/artisanlogiciel/games/Display.java b/java/org/artisanlogiciel/games/Display.java index 2f652fd..1beac4c 100644 --- a/java/org/artisanlogiciel/games/Display.java +++ b/java/org/artisanlogiciel/games/Display.java @@ -319,7 +319,12 @@ public class Display extends JFrame { return maxdepth; } - + + public void setName(String n) + { + name = n; + } + public String getName() { if (name == null) @@ -447,6 +452,7 @@ public class Display extends JFrame setAutoSize( autoSlide.isSelected()); } }); + final JTextField saveName = new JTextField("newlaby "); final JButton savePngButton = new JButton(labels.getString("save") +" png"); Action savePngAction = new AbstractAction() { public void actionPerformed(ActionEvent evt) @@ -463,7 +469,9 @@ public class Display extends JFrame { // System.out.println("save"); - save((MazeParamsFixed) params,model); + MazeParamsFixed p = (MazeParamsFixed) params; + p.setName(saveName.getText()); + save(p,model); } }; saveButton.addActionListener(saveAction); @@ -483,6 +491,7 @@ public class Display extends JFrame resizecontrol.add(showAll); resizecontrol.add(slider); resizecontrol.add(autoSlide); + resizecontrol.add(saveName); resizecontrol.add(savePngButton); resizecontrol.add(saveButton); resizecontrol.add(quitButton); @@ -709,6 +718,7 @@ public class Display extends JFrame // not set by default for debug, will show any path boolean showAll = false; + // for a given (x,y) pixel return cell position. Position getPosition(int x, int y) { int pX = (int) ((double) (x - cp.getOffsetX()) / cp.getWidth()); @@ -725,32 +735,34 @@ public class Display extends JFrame if (drawingPath == null ) { drawingPath = new LinkedList<>(); + last = new DirectionPosition((short) 0,newPosition); + System.out.println("Mouse dragged Cell " + newPosition + " Button " + e.getModifiersEx() + " " + InputEvent.BUTTON1_DOWN_MASK); + drawingPath.addLast(last); } else { // setShowAll(true); - last = drawingPath.getLast(); - if ( last.getPosition().equals(newPosition)) + DirectionPosition first = drawingPath.getLast(); + last = first; + // construct as many move form last to new position as needed. + while ( ! last.getPosition().equals(newPosition)) { - // no move. - return; + path=LabyModel.getDirection(last.getPosition(),newPosition); + last.setDirection(path); + // button 1 : add direction; button 2 : replace with direction. + if ( ( e.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK ) != 0 ) + { + map.addDirection(last.getPosition().getX(),last.getPosition().getY(),path); + } + else + { + map.setDirection(last.getPosition().getX(),last.getPosition().getY(),path); + } + last = last.moveToAdjacentDirection(); + drawingPath.addLast(last); } - path=LabyModel.getDirection(last.getPosition(),newPosition); - last.setDirection(path); - // button 1 : add direction; button 2 : replace with direction. - if ( ( e.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK ) != 0 ) - { - map.addDirection(last.getPosition().getX(),last.getPosition().getY(),path); - } - else - { - map.setDirection(last.getPosition().getX(),last.getPosition().getY(),path); - } + System.out.println("Mouse dragged from Cell " + first.getPosition() + "To" + newPosition + " Button " + e.getModifiersEx() + " " + InputEvent.BUTTON1_DOWN_MASK); } - last = new DirectionPosition((short) 0,newPosition); - System.out.println("Mouse dragged Cell " + newPosition + " Button " + e.getModifiersEx() + " " + InputEvent.BUTTON1_DOWN_MASK); - drawingPath.addLast(last); - // map.noWalls(pX,pY); changed(null,null,map); } @@ -862,6 +874,7 @@ public class Display extends JFrame sX=0; sY=0; cp.resetMazeWidthHeight(map.getWidth(),map.getHeight()); + setPreferredSize(cp.getDimension()); } public void setWallSize(int size) diff --git a/java/org/artisanlogiciel/games/LabyModel.java b/java/org/artisanlogiciel/games/LabyModel.java index 3bd6e36..9e2da37 100644 --- a/java/org/artisanlogiciel/games/LabyModel.java +++ b/java/org/artisanlogiciel/games/LabyModel.java @@ -40,11 +40,11 @@ public class LabyModel implements WallsProvider // completed private final static short CLOSED = 64; // can be reused once generation is // completed - private final static short LEFT = Brick.LEFT << FLAGLENGTH | DIRECTION | HORIZONTAL | NEGATIVE; - private final static short DOWN = Brick.DOWN << FLAGLENGTH | DIRECTION | VERTICAL | POSITIVE; - private final static short RIGHT = Brick.RIGHT << FLAGLENGTH | DIRECTION | HORIZONTAL | POSITIVE; - private final static short UP = Brick.UP << FLAGLENGTH | DIRECTION | VERTICAL | NEGATIVE; - private final static short ENTRY = Brick.ENTRY << FLAGLENGTH; // flag when a + public final static short LEFT = Brick.LEFT << FLAGLENGTH | DIRECTION | HORIZONTAL | NEGATIVE; + public final static short DOWN = Brick.DOWN << FLAGLENGTH | DIRECTION | VERTICAL | POSITIVE; + public final static short RIGHT = Brick.RIGHT << FLAGLENGTH | DIRECTION | HORIZONTAL | POSITIVE; + public final static short UP = Brick.UP << FLAGLENGTH | DIRECTION | VERTICAL | NEGATIVE; + public final static short ENTRY = Brick.ENTRY << FLAGLENGTH; // flag when a // wall should // be open to // access