diff --git a/core/src/cz/nic/tablexia/util/listener/DragAndRotateActorListener.java b/core/src/cz/nic/tablexia/util/listener/DragAndRotateActorListener.java index 959156c5bf168695155cd62fecd040da4a815336..124acf3ee95fba36569cfd1a0bb38e5033c27241 100644 --- a/core/src/cz/nic/tablexia/util/listener/DragAndRotateActorListener.java +++ b/core/src/cz/nic/tablexia/util/listener/DragAndRotateActorListener.java @@ -6,6 +6,7 @@ import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.InputListener; import com.badlogic.gdx.scenes.scene2d.actions.Actions; +import com.badlogic.gdx.utils.TimeUtils; import java.util.LinkedHashMap; @@ -32,10 +33,47 @@ public class DragAndRotateActorListener extends InputListener { private float actorOriginalRotation; private boolean performingAction; private boolean pieceAnimationDone; + private Touch lastTouch, newTouch; + private RotateAndMovePieceInPosition pieceTranslationAction; private LinkedHashMap<Integer, Point> activePointers; + class Touch { + public static final float TIME_BETWEEN_TOUCHES = 100; + public static final float TIME_BETWEEN_TAPS = 600; + + private long time; + private Actor touchedActor; + + public Touch(Actor touchedActor, long time) { + this.touchedActor = touchedActor; + this.time = time; + } + + public Actor getTouchedActor() { + return touchedActor; + } + + public long getTime() { + return time; + } + + public boolean isTap(Touch touch) { + return controlIfSame(touch, TIME_BETWEEN_TOUCHES); + } + + private boolean controlIfSame(Touch secondTouch, float time) { + return getTouchedActor().equals(secondTouch.getTouchedActor()) && Math.abs(secondTouch.getTime() - getTime()) < time; + } + + + public boolean isDoubleTap(Touch touch) { + return controlIfSame(touch, TIME_BETWEEN_TAPS); + } + + } + public DragAndRotateActorListener(IOnRotationFinished iOnRotationFinished) { this.iOnRotationFinished = iOnRotationFinished; activePointers = new LinkedHashMap<Integer, Point>(); @@ -55,6 +93,11 @@ public class DragAndRotateActorListener extends InputListener { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { + if (pointer==0) { //one finger + newTouch = new Touch(((Grid) parentActor).pieceAtPoint(x, y), TimeUtils.millis()); //save touched piece and time + if (lastTouch != null && newTouch.getTime() - lastTouch.getTime() > Touch.TIME_BETWEEN_TAPS) //check if new touch is + lastTouch = null; + } if (button != 1) { activePointers.put(pointer, new Point(x, y)); } @@ -114,6 +157,24 @@ public class DragAndRotateActorListener extends InputListener { @Override public void touchUp(InputEvent event, final float x, final float y, int pointer, int button) { + if (pointer==0) { + Touch tempTouch = new Touch(((Grid) parentActor).pieceAtPoint(x, y), TimeUtils.millis()); + if (newTouch != null) { + boolean tapped = tempTouch.isTap(newTouch); + if (tapped) { + if (lastTouch != null) { + if (tempTouch.isDoubleTap(lastTouch)) { + lastTouch.getTouchedActor().rotateBy(90); + rotated((lastTouch.getTouchedActor().getRotation() + 90), lastTouch.getTouchedActor()); + } else { + lastTouch = new Touch(draggedActor, (tempTouch.getTime() + newTouch.getTime()) / 2); //not double - save least touch + } + } else { + lastTouch = new Touch(draggedActor, (tempTouch.getTime() + newTouch.getTime()) / 2); + } + } + } + } if (button != 1) { if (draggedActor != null && !performingAction && activePointers.size()>=1) { //storing original fingers positions and deciding which finger was released