diff --git a/core/src/cz/nic/tablexia/game/games/pursuit/helper/PositionHelper.java b/core/src/cz/nic/tablexia/game/games/pursuit/helper/PositionHelper.java index aca4d61522db6c976adb1a093650c78fe0ad9532..528e15bd7411cf8e7cc4c3b8392d14d37df53dcc 100644 --- a/core/src/cz/nic/tablexia/game/games/pursuit/helper/PositionHelper.java +++ b/core/src/cz/nic/tablexia/game/games/pursuit/helper/PositionHelper.java @@ -23,9 +23,9 @@ public class PositionHelper { } public static RotationDefinition getRotationDefinition(float rotation) { - float newrRtation = Math.abs(rotation)%360; + rotation = Math.abs(rotation)%360; for (RotationDefinition rotationDefinition : RotationDefinition.values()) { - if (rotationDefinition.getRotation() == newrRtation) return rotationDefinition; + if (rotationDefinition.getRotation() == rotation) return rotationDefinition; } return null; } diff --git a/core/src/cz/nic/tablexia/util/entity/Touch.java b/core/src/cz/nic/tablexia/util/entity/Touch.java new file mode 100644 index 0000000000000000000000000000000000000000..e238d66b5d0704f34cedec0381041e472781e534 --- /dev/null +++ b/core/src/cz/nic/tablexia/util/entity/Touch.java @@ -0,0 +1,44 @@ +package cz.nic.tablexia.util.entity; + +import com.badlogic.gdx.scenes.scene2d.Actor; + +/** + * Created by Vitaliy Vashchenko on 22.8.16. + */ +public class Touch { + public static final float TIME_BETWEEN_TOUCHES = 100; + public static final float TIME_BETWEEN_TAPS = 700; + + 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; + } + + private static boolean compareTouches(Touch firstTouch, Touch secondTouch, float time) { + if (firstTouch.getTouchedActor() != null && secondTouch.getTouchedActor() != null) { + return firstTouch.getTouchedActor().equals(secondTouch.getTouchedActor()) && Math.abs(secondTouch.getTime() - firstTouch.getTime()) < time; + } else { + return false; + } + } + + public static boolean isTap(Touch firstTouch, Touch secondTouch) { + return compareTouches(firstTouch, secondTouch, TIME_BETWEEN_TOUCHES); + } + + public static boolean isDoubleTap(Touch firstTouch, Touch secondTouch) { + return compareTouches(firstTouch, secondTouch, TIME_BETWEEN_TAPS); + } + +} \ No newline at end of file diff --git a/core/src/cz/nic/tablexia/util/listener/DragAndRotateActorListener.java b/core/src/cz/nic/tablexia/util/listener/DragAndRotateActorListener.java index dbff95e76d6a358c75d1cb1fbe59fc3765eb86d2..b2151ccbc806445e15910a843c157885b36b3a2e 100644 --- a/core/src/cz/nic/tablexia/util/listener/DragAndRotateActorListener.java +++ b/core/src/cz/nic/tablexia/util/listener/DragAndRotateActorListener.java @@ -17,6 +17,7 @@ import cz.nic.tablexia.game.games.pursuit.model.Grid; import cz.nic.tablexia.game.games.pursuit.model.PuzzlePiece; import cz.nic.tablexia.util.Log; import cz.nic.tablexia.util.Point; +import cz.nic.tablexia.util.entity.Touch; /** * Created by Vaclav Tarantik on 6/18/15. @@ -26,56 +27,18 @@ public class DragAndRotateActorListener extends InputListener { public static final float ROTATION_ANIMATION_DURATION = 0.3f; public static final float MOVETO_ANIMATION_DURATION = 0.2f; private float grabX, grabY; - private float initialFingersAngle; - private Actor draggedActor; - private Actor parentActor; + private float initialFingersAngle; + private Actor draggedActor; + private Actor parentActor; private IOnRotationFinished iOnRotationFinished; - private float actorOriginalRotation; - private boolean performingAction; - private boolean pieceAnimationDone; - private Touch lastTouch, newTouch; + private float actorOriginalRotation; + private boolean performingAction; + private boolean pieceAnimationDone; + private Touch lastTouch, newTouchDown; 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 = 700; - - 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) { - if (getTouchedActor() != null && secondTouch.getTouchedActor() != null) { - return getTouchedActor().equals(secondTouch.getTouchedActor()) && Math.abs(secondTouch.getTime() - getTime()) < time; - } else { - return false; - } - } - - public boolean isDoubleTap(Touch touch) { - return controlIfSame(touch, TIME_BETWEEN_TAPS); - } - - } public DragAndRotateActorListener(IOnRotationFinished iOnRotationFinished) { this.iOnRotationFinished = iOnRotationFinished; @@ -97,8 +60,8 @@ 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 out of the time boundaries + newTouchDown = new Touch(((Grid) parentActor).pieceAtPoint(x, y), TimeUtils.millis()); //save touched piece and time + if (lastTouch != null && newTouchDown.getTime() - lastTouch.getTime() > Touch.TIME_BETWEEN_TAPS) //check if new touch is out of the time boundaries lastTouch = null; } if (button != 1) { @@ -161,22 +124,21 @@ public class DragAndRotateActorListener extends InputListener { @Override public void touchUp(InputEvent event, final float x, final float y, int pointer, int button) { if (pointer==0 && button !=1) { //don't react on right mouse button double tap - Touch tempTouch = new Touch(((Grid) parentActor).pieceAtPoint(x, y), TimeUtils.millis()); - if (newTouch != null) { - boolean tapped = tempTouch.isTap(newTouch); - if (tapped) { + Touch newTouchUp = new Touch(((Grid) parentActor).pieceAtPoint(x, y), TimeUtils.millis()); + if (newTouchDown != null) { + if (Touch.isTap(newTouchUp,newTouchDown)) { if (lastTouch != null) { - if (tempTouch.isDoubleTap(lastTouch) && !lastTouch.getTouchedActor().hasActions()) { + if (Touch.isDoubleTap(newTouchUp,lastTouch) && !lastTouch.getTouchedActor().hasActions()) { Actor actorToRotate = lastTouch.getTouchedActor(); float newRotationAngle = ArithmeticsHelper.getClosestRightAngle(actorToRotate.getRotation()-90); // FIXME: 22.8.16 sometime piece is rotaed back to the original rotation actorToRotate.addAction(Actions.rotateTo(newRotationAngle, ROTATION_ANIMATION_DURATION)); rotated(newRotationAngle, actorToRotate); } else { - lastTouch = new Touch(draggedActor, (tempTouch.getTime() + newTouch.getTime()) / 2); //not double - save least touch + lastTouch = new Touch(draggedActor, (newTouchUp.getTime() + newTouchDown.getTime()) / 2); //not double - save least touch } } else { - lastTouch = new Touch(draggedActor, (tempTouch.getTime() + newTouch.getTime()) / 2); + lastTouch = new Touch(draggedActor, (newTouchUp.getTime() + newTouchDown.getTime()) / 2); } } }