From 1a7e64c139aead09fdfacc05430906eaf37deb0b Mon Sep 17 00:00:00 2001 From: philippe lhardy Date: Mon, 19 Oct 2020 21:58:05 +0200 Subject: [PATCH] When drawing a path within maze, restart a newline after 200ms delay - 200 ms is hardcoded for gragTimeout - when drawing a line and wanting to start a new line, it is enough to wait dragDelay wihout move --- .../games/maze/gui/Display.java | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/java/org/artisanlogiciel/games/maze/gui/Display.java b/java/org/artisanlogiciel/games/maze/gui/Display.java index a214b36..18f5e3a 100644 --- a/java/org/artisanlogiciel/games/maze/gui/Display.java +++ b/java/org/artisanlogiciel/games/maze/gui/Display.java @@ -17,6 +17,7 @@ import java.awt.event.InputEvent; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import java.io.*; +import java.util.Date; import java.util.LinkedList; import java.util.Locale; import java.util.ResourceBundle; @@ -821,6 +822,10 @@ public class Display extends JFrame { int gX = -1; int gY = -1; + Date lastDrag = null; + // FIXME HARCDODED delay after which a draging to draw a continous line is ended, a new line will then start. + long dragTimeout = 200; + // not set by default for debug, will show any path boolean showAll = false; @@ -833,14 +838,33 @@ public class Display extends JFrame { @Override public void mouseDragged(MouseEvent e) { + boolean add = ((e.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK) != 0); + Position newPosition = getPosition(e.getX(), e.getY()); + Date now = new Date(System.currentTimeMillis()); + if (lastDrag == null ) + { + drawingPath = null; + } + else + { + if ( now.getTime() - lastDrag.getTime() > dragTimeout ) + { + drawingPath = null; + addStatus("move timeout"); + } + } + lastDrag = now; + addPosition(newPosition,add); + } + + public void addPosition(Position newPosition, boolean add ) { // should find the cell ... DirectionPosition last = null; short path = 0; - Position newPosition = getPosition(e.getX(), e.getY()); if (drawingPath == null) { drawingPath = new LinkedList<>(); last = new DirectionPosition((short) 0, newPosition); - addStatus("Mouse dragged Cell " + newPosition + " Button " + e.getModifiersEx() + " " + InputEvent.BUTTON1_DOWN_MASK); + addStatus("Mouse dragged Cell " + newPosition + " Button " + add ); drawingPath.addLast(last); } else { // setShowAll(true); @@ -851,7 +875,7 @@ public class Display extends JFrame { 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) { + if (add) { map.addDirection(last.getPosition().getX(), last.getPosition().getY(), path); } else { map.setDirection(last.getPosition().getX(), last.getPosition().getY(), path); @@ -859,7 +883,7 @@ public class Display extends JFrame { last = last.moveToAdjacentDirection(); drawingPath.addLast(last); } - addStatus("Mouse dragged from Cell " + first.getPosition() + "To" + newPosition + " Button " + e.getModifiersEx() + " " + InputEvent.BUTTON1_DOWN_MASK); + addStatus("Mouse dragged from Cell " + first.getPosition() + "To" + newPosition + " Button " + add ); } changed(null, null, map); }