QnD fix for drawing

( seen when joining cells touching only by one point )
This commit is contained in:
philippe lhardy
2017-12-05 21:52:12 +01:00
parent c5d8503dbb
commit b360320570
3 changed files with 72 additions and 27 deletions

View File

@@ -40,6 +40,38 @@ public class DirectionPosition
} }
return "?"; 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);
}
} }

View File

@@ -319,7 +319,12 @@ public class Display extends JFrame
{ {
return maxdepth; return maxdepth;
} }
public void setName(String n)
{
name = n;
}
public String getName() public String getName()
{ {
if (name == null) if (name == null)
@@ -447,6 +452,7 @@ public class Display extends JFrame
setAutoSize( autoSlide.isSelected()); setAutoSize( autoSlide.isSelected());
} }
}); });
final JTextField saveName = new JTextField("newlaby ");
final JButton savePngButton = new JButton(labels.getString("save") +" png"); final JButton savePngButton = new JButton(labels.getString("save") +" png");
Action savePngAction = new AbstractAction() { Action savePngAction = new AbstractAction() {
public void actionPerformed(ActionEvent evt) public void actionPerformed(ActionEvent evt)
@@ -463,7 +469,9 @@ public class Display extends JFrame
{ {
// //
System.out.println("save"); System.out.println("save");
save((MazeParamsFixed) params,model); MazeParamsFixed p = (MazeParamsFixed) params;
p.setName(saveName.getText());
save(p,model);
} }
}; };
saveButton.addActionListener(saveAction); saveButton.addActionListener(saveAction);
@@ -483,6 +491,7 @@ public class Display extends JFrame
resizecontrol.add(showAll); resizecontrol.add(showAll);
resizecontrol.add(slider); resizecontrol.add(slider);
resizecontrol.add(autoSlide); resizecontrol.add(autoSlide);
resizecontrol.add(saveName);
resizecontrol.add(savePngButton); resizecontrol.add(savePngButton);
resizecontrol.add(saveButton); resizecontrol.add(saveButton);
resizecontrol.add(quitButton); resizecontrol.add(quitButton);
@@ -709,6 +718,7 @@ public class Display extends JFrame
// not set by default for debug, will show any path // not set by default for debug, will show any path
boolean showAll = false; boolean showAll = false;
// for a given (x,y) pixel return cell position.
Position getPosition(int x, int y) Position getPosition(int x, int y)
{ {
int pX = (int) ((double) (x - cp.getOffsetX()) / cp.getWidth()); int pX = (int) ((double) (x - cp.getOffsetX()) / cp.getWidth());
@@ -725,32 +735,34 @@ public class Display extends JFrame
if (drawingPath == null ) if (drawingPath == null )
{ {
drawingPath = new LinkedList<>(); 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 else
{ {
// setShowAll(true); // setShowAll(true);
last = drawingPath.getLast(); DirectionPosition first = drawingPath.getLast();
if ( last.getPosition().equals(newPosition)) last = first;
// construct as many move form last to new position as needed.
while ( ! last.getPosition().equals(newPosition))
{ {
// no move. path=LabyModel.getDirection(last.getPosition(),newPosition);
return; 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); System.out.println("Mouse dragged from Cell " + first.getPosition() + "To" + newPosition + " Button " + e.getModifiersEx() + " " + InputEvent.BUTTON1_DOWN_MASK);
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 = 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); changed(null,null,map);
} }
@@ -862,6 +874,7 @@ public class Display extends JFrame
sX=0; sX=0;
sY=0; sY=0;
cp.resetMazeWidthHeight(map.getWidth(),map.getHeight()); cp.resetMazeWidthHeight(map.getWidth(),map.getHeight());
setPreferredSize(cp.getDimension());
} }
public void setWallSize(int size) public void setWallSize(int size)

View File

@@ -40,11 +40,11 @@ public class LabyModel implements WallsProvider
// completed // completed
private final static short CLOSED = 64; // can be reused once generation is private final static short CLOSED = 64; // can be reused once generation is
// completed // completed
private final static short LEFT = Brick.LEFT << FLAGLENGTH | DIRECTION | HORIZONTAL | NEGATIVE; public final static short LEFT = Brick.LEFT << FLAGLENGTH | DIRECTION | HORIZONTAL | NEGATIVE;
private final static short DOWN = Brick.DOWN << FLAGLENGTH | DIRECTION | VERTICAL | POSITIVE; public final static short DOWN = Brick.DOWN << FLAGLENGTH | DIRECTION | VERTICAL | POSITIVE;
private final static short RIGHT = Brick.RIGHT << FLAGLENGTH | DIRECTION | HORIZONTAL | POSITIVE; public final static short RIGHT = Brick.RIGHT << FLAGLENGTH | DIRECTION | HORIZONTAL | POSITIVE;
private final static short UP = Brick.UP << FLAGLENGTH | DIRECTION | VERTICAL | NEGATIVE; public 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 ENTRY = Brick.ENTRY << FLAGLENGTH; // flag when a
// wall should // wall should
// be open to // be open to
// access // access