OSDN Git Service

Added panStop to GestureListener.
authorNathanSweet <nathan.sweet@gmail.com>
Thu, 19 Sep 2013 14:58:30 +0000 (16:58 +0200)
committerNathanSweet <nathan.sweet@gmail.com>
Thu, 19 Sep 2013 14:58:30 +0000 (16:58 +0200)
Otherwise trying to determine when a pan stopped was tricky.

CHANGES
gdx/src/com/badlogic/gdx/input/GestureDetector.java
tests/gdx-tests/src/com/badlogic/gdx/tests/Box2DTestCollection.java
tests/gdx-tests/src/com/badlogic/gdx/tests/BulletTestCollection.java
tests/gdx-tests/src/com/badlogic/gdx/tests/GestureDetectorTest.java
tests/gdx-tests/src/com/badlogic/gdx/tests/bullet/BulletTest.java

diff --git a/CHANGES b/CHANGES
index cfd9aa6..0a2ead3 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -80,6 +80,7 @@
        - Added getCache() to BitmapFont, for expert users who wish to use the BitmapFontCache (see BitmapFontTest)
        - FreeTypeFontGenerator now includes setMaxTextureSize and getMaxTextureSize to cap the generated glyph atlas size (default 1024)
 - added render-hooks beginRender() and endRender() to BatchTiledMapRenderer
+- Added panStop to GestureListener interface.
 
 [0.9.8]
 - see http://www.badlogicgames.com/wordpress/?p=2791
index 4e49597..e854883 100644 (file)
 package com.badlogic.gdx.input;\r
 \r
 import com.badlogic.gdx.Gdx;\r
+import com.badlogic.gdx.Input;\r
 import com.badlogic.gdx.InputAdapter;\r
 import com.badlogic.gdx.InputProcessor;\r
 import com.badlogic.gdx.math.Vector2;\r
+import com.badlogic.gdx.scenes.scene2d.InputListener;\r
 import com.badlogic.gdx.utils.TimeUtils;\r
 import com.badlogic.gdx.utils.Timer;\r
 import com.badlogic.gdx.utils.Timer.Task;\r
@@ -173,9 +175,12 @@ public class GestureDetector extends InputAdapter {
                // check if we are still tapping.\r
                if (inTapSquare && !isWithinTapSquare(x, y, tapSquareCenterX, tapSquareCenterY)) inTapSquare = false;\r
 \r
-               longPressTask.cancel();\r
+               boolean wasPanning = panning;\r
                panning = false;\r
+\r
+               longPressTask.cancel();\r
                if (longPressFired) return false;\r
+\r
                if (inTapSquare) {\r
                        // handle taps\r
                        if (lastTapButton != button || lastTapPointer != pointer || TimeUtils.nanoTime() - lastTapTime > tapCountInterval\r
@@ -188,7 +193,9 @@ public class GestureDetector extends InputAdapter {
                        lastTapPointer = pointer;\r
                        gestureStartTime = 0;\r
                        return listener.tap(x, y, tapCount, button);\r
-               } else if (pinching) {\r
+               }\r
+\r
+               if (pinching) {\r
                        // handle pinch end\r
                        pinching = false;\r
                        panning = true;\r
@@ -200,16 +207,21 @@ public class GestureDetector extends InputAdapter {
                                // second pointer has lifted off, set up panning to use the first pointer...\r
                                tracker.start(pointer1.x, pointer1.y, Gdx.input.getCurrentEventTime());\r
                        }\r
-               } else {\r
-                       gestureStartTime = 0;\r
-                       // handle fling\r
-                       long time = Gdx.input.getCurrentEventTime();\r
-                       if (time - tracker.lastTime < maxFlingDelay) {\r
-                               tracker.update(x, y, time);\r
-                               return listener.fling(tracker.getVelocityX(), tracker.getVelocityY(), button);\r
-                       }\r
+                       return false;\r
                }\r
-               return false;\r
+\r
+               // handle no longer panning\r
+               boolean handled = false;\r
+               if (wasPanning && !panning) handled = listener.panStop(x, y, pointer, button);\r
+\r
+               // handle fling\r
+               gestureStartTime = 0;\r
+               long time = Gdx.input.getCurrentEventTime();\r
+               if (time - tracker.lastTime < maxFlingDelay) {\r
+                       tracker.update(x, y, time);\r
+                       handled = listener.fling(tracker.getVelocityX(), tracker.getVelocityY(), button) || handled;\r
+               }\r
+               return handled;\r
        }\r
 \r
        /** @return whether the user touched the screen long enough to trigger a long press event. */\r
@@ -265,7 +277,7 @@ public class GestureDetector extends InputAdapter {
         * to hand it to the next listener, true otherwise).\r
         * @author mzechner */\r
        public static interface GestureListener {\r
-               /** Called when a finger went down on the screen or a mouse button was pressed. */\r
+               /** @see InputProcessor#touchDown(int, int, int, int) */\r
                public boolean touchDown (float x, float y, int pointer, int button);\r
 \r
                /** Called when a tap occured. A tap happens if a touch went down on the screen and was lifted again without moving outside\r
@@ -283,12 +295,13 @@ public class GestureDetector extends InputAdapter {
                public boolean fling (float velocityX, float velocityY, int button);\r
 \r
                /** Called when the user drags a finger over the screen.\r
-                * @param x\r
-                * @param y\r
                 * @param deltaX the difference in pixels to the last drag event on x.\r
                 * @param deltaY the difference in pixels to the last drag event on y. */\r
                public boolean pan (float x, float y, float deltaX, float deltaY);\r
 \r
+               /** Called when no longer panning. */\r
+               public boolean panStop (float x, float y, int pointer, int button);\r
+\r
                /** Called when the user performs a pinch zoom gesture. The original distance is the distance in pixels when the gesture\r
                 * started.\r
                 * @param initialDistance distance between fingers when the gesture started.\r
@@ -333,6 +346,11 @@ public class GestureDetector extends InputAdapter {
                }\r
 \r
                @Override\r
+               public boolean panStop (float x, float y, int pointer, int button) {\r
+                       return false;\r
+               }\r
+\r
+               @Override\r
                public boolean zoom (float initialDistance, float distance) {\r
                        return false;\r
                }\r
index 52cebd1..06ee1f3 100644 (file)
@@ -46,7 +46,8 @@ import com.badlogic.gdx.tests.utils.GdxTest;
 public class Box2DTestCollection extends GdxTest implements InputProcessor, GestureListener {\r
        private final Box2DTest[] tests = {new DebugRendererTest(), new CollisionFiltering(), new Chain(), new Bridge(),\r
                new SphereStack(), new Cantilever(), new ApplyForce(), new ContinuousTest(), new Prismatic(), new CharacterCollision(),\r
-               new BodyTypes(), new SimpleTest(), new Pyramid(), new OneSidedPlatform(), new VerticalStack(), new VaryingRestitution(), new ConveyorBelt()};\r
+               new BodyTypes(), new SimpleTest(), new Pyramid(), new OneSidedPlatform(), new VerticalStack(), new VaryingRestitution(),\r
+               new ConveyorBelt()};\r
 \r
        private int testIndex = 0;\r
 \r
@@ -147,25 +148,26 @@ public class Box2DTestCollection extends GdxTest implements InputProcessor, Gest
 \r
        @Override\r
        public boolean longPress (float x, float y) {\r
-               // TODO Auto-generated method stub\r
                return false;\r
        }\r
 \r
        @Override\r
        public boolean fling (float velocityX, float velocityY, int button) {\r
-               // TODO Auto-generated method stub\r
                return false;\r
        }\r
 \r
        @Override\r
        public boolean pan (float x, float y, float deltaX, float deltaY) {\r
-               // TODO Auto-generated method stub\r
+               return false;\r
+       }\r
+\r
+       @Override\r
+       public boolean panStop (float x, float y, int pointer, int button) {\r
                return false;\r
        }\r
 \r
        @Override\r
        public boolean zoom (float originalDistance, float currentDistance) {\r
-               // TODO Auto-generated method stub\r
                return false;\r
        }\r
 \r
index b65f937..f1098fc 100644 (file)
@@ -57,15 +57,16 @@ import com.badlogic.gdx.tests.utils.GdxTest;
 
 /** @author xoppa */
 public class BulletTestCollection extends GdxTest implements InputProcessor, GestureListener {
-       protected final BulletTest[] tests = {new BasicBulletTest(),  new ShootTest(), new BasicShapesTest(), new KinematicTest(), 
-               new ConstraintsTest(), new MeshShapeTest(), new ConvexHullTest(), new RayCastTest(), new RayPickRagdollTest(), 
-               new InternalTickTest(), new CollisionWorldTest(), new CollisionTest(), new FrustumCullingTest(), new ContactCallbackTest(), 
-               new ContactCallbackTest2(), new ContactCacheTest(), new SoftBodyTest(), new SoftMeshTest(), new VehicleTest(), new ImportTest()};
-       
+       protected final BulletTest[] tests = {new BasicBulletTest(), new ShootTest(), new BasicShapesTest(), new KinematicTest(),
+               new ConstraintsTest(), new MeshShapeTest(), new ConvexHullTest(), new RayCastTest(), new RayPickRagdollTest(),
+               new InternalTickTest(), new CollisionWorldTest(), new CollisionTest(), new FrustumCullingTest(), new ContactCallbackTest(),
+               new ContactCallbackTest2(), new ContactCacheTest(), new SoftBodyTest(), new SoftMeshTest(), new VehicleTest(),
+               new ImportTest()};
+
        protected int testIndex = 0;
-       
+
        private Application app = null;
-       
+
        private BitmapFont font;
        private Stage hud;
        private Label fpsLabel;
@@ -73,17 +74,16 @@ public class BulletTestCollection extends GdxTest implements InputProcessor, Ges
        private Label instructLabel;
        private int loading = 0;
        private CameraInputController cameraController;
-       
+
        @Override
        public void render () {
-               if ((loading > 0) && (++loading > 2))
-                       loadnext();
-                       
+               if ((loading > 0) && (++loading > 2)) loadnext();
+
                tests[testIndex].render();
                fpsLabel.setText(tests[testIndex].performance);
                hud.draw();
        }
-       
+
        @Override
        public void create () {
                if (app == null) {
@@ -97,31 +97,31 @@ public class BulletTestCollection extends GdxTest implements InputProcessor, Ges
                cameraController.forwardTarget = false;
                cameraController.translateTarget = false;
                Gdx.input.setInputProcessor(new InputMultiplexer(cameraController, this, new GestureDetector(this)));
-               
+
                font = new BitmapFont(Gdx.files.internal("data/arial-15.fnt"), false);
                hud = new Stage(480, 320, true);
                hud.addActor(fpsLabel = new Label(" ", new Label.LabelStyle(font, Color.WHITE)));
                fpsLabel.setPosition(0, 0);
                hud.addActor(titleLabel = new Label(tests[testIndex].getClass().getSimpleName(), new Label.LabelStyle(font, Color.WHITE)));
-               titleLabel.setY(hud.getHeight()-titleLabel.getHeight());
+               titleLabel.setY(hud.getHeight() - titleLabel.getHeight());
                hud.addActor(instructLabel = new Label("A\nB\nC\nD\nE\nF", new Label.LabelStyle(font, Color.WHITE)));
                instructLabel.setY(titleLabel.getY() - instructLabel.getHeight());
                instructLabel.setAlignment(Align.top | Align.left);
                instructLabel.setText(tests[testIndex].instructions);
        }
-       
+
        @Override
        public void dispose () {
                tests[testIndex].dispose();
                app = null;
        }
-       
-       public void next() {
+
+       public void next () {
                titleLabel.setText("Loading...");
                loading = 1;
        }
-       
-       public void loadnext() {
+
+       public void loadnext () {
                app.log("TestCollection", "disposing test '" + tests[testIndex].getClass().getName() + "'");
                tests[testIndex].dispose();
                // This would be a good time for GC to kick in.
@@ -131,12 +131,12 @@ public class BulletTestCollection extends GdxTest implements InputProcessor, Ges
                tests[testIndex].create();
                cameraController.camera = tests[testIndex].camera;
                app.log("TestCollection", "created test '" + tests[testIndex].getClass().getName() + "'");
-               
+
                titleLabel.setText(tests[testIndex].getClass().getSimpleName());
                instructLabel.setText(tests[testIndex].instructions);
                loading = 0;
        }
-       
+
        @Override
        public boolean keyDown (int keycode) {
                return tests[testIndex].keyDown(keycode);
@@ -149,7 +149,7 @@ public class BulletTestCollection extends GdxTest implements InputProcessor, Ges
 
        @Override
        public boolean keyUp (int keycode) {
-               boolean result = tests[testIndex].keyUp(keycode); 
+               boolean result = tests[testIndex].keyUp(keycode);
                if ((result == false) && (keycode == Keys.SPACE || keycode == Keys.MENU)) {
                        next();
                        result = true;
@@ -179,48 +179,52 @@ public class BulletTestCollection extends GdxTest implements InputProcessor, Ges
 
        @Override
        public boolean mouseMoved (int x, int y) {
-               return tests[testIndex].mouseMoved (x, y);
+               return tests[testIndex].mouseMoved(x, y);
        }
 
        @Override
        public boolean scrolled (int amount) {
-               return tests[testIndex].scrolled (amount);
+               return tests[testIndex].scrolled(amount);
        }
 
        @Override
        public boolean touchDown (float x, float y, int pointer, int button) {
-               return tests[testIndex].touchDown (x, y, pointer, button);
+               return tests[testIndex].touchDown(x, y, pointer, button);
        }
 
        @Override
        public boolean tap (float x, float y, int count, int button) {
-               return tests[testIndex].tap (x, y, count, button);
+               return tests[testIndex].tap(x, y, count, button);
        }
 
        @Override
        public boolean longPress (float x, float y) {
-               return tests[testIndex].longPress (x, y);
+               return tests[testIndex].longPress(x, y);
        }
 
        @Override
        public boolean fling (float velocityX, float velocityY, int button) {
-               if (tests[testIndex].fling (velocityX, velocityY, button) == false)
-                       next();
+               if (tests[testIndex].fling(velocityX, velocityY, button) == false) next();
                return true;
        }
 
        @Override
        public boolean pan (float x, float y, float deltaX, float deltaY) {
-               return tests[testIndex].pan (x, y, deltaX, deltaY);
+               return tests[testIndex].pan(x, y, deltaX, deltaY);
+       }
+
+       @Override
+       public boolean panStop (float x, float y, int pointer, int button) {
+               return tests[testIndex].panStop(x, y, pointer, button);
        }
 
        @Override
        public boolean zoom (float originalDistance, float currentDistance) {
-               return tests[testIndex].zoom (originalDistance, currentDistance);
+               return tests[testIndex].zoom(originalDistance, currentDistance);
        }
 
        @Override
        public boolean pinch (Vector2 initialFirstPointer, Vector2 initialSecondPointer, Vector2 firstPointer, Vector2 secondPointer) {
-               return tests[testIndex].pinch (initialFirstPointer, initialSecondPointer, firstPointer, secondPointer);
+               return tests[testIndex].pinch(initialFirstPointer, initialSecondPointer, firstPointer, secondPointer);
        }
 }
index 9a64426..3a75ef8 100644 (file)
@@ -59,6 +59,7 @@ public class GestureDetectorTest extends GdxTest implements ApplicationListener
 \r
                @Override\r
                public boolean fling (float velocityX, float velocityY, int button) {\r
+                       Gdx.app.log("GestureDetectorTest", "fling " + velocityX + ", " + velocityY);\r
                        flinging = true;\r
                        velX = camera.zoom * velocityX * 0.5f;\r
                        velY = camera.zoom * velocityY * 0.5f;\r
@@ -67,9 +68,16 @@ public class GestureDetectorTest extends GdxTest implements ApplicationListener
 \r
                @Override\r
                public boolean pan (float x, float y, float deltaX, float deltaY) {\r
+                       // Gdx.app.log("GestureDetectorTest", "pan at " + x + ", " + y);\r
                        camera.position.add(-deltaX * camera.zoom, deltaY * camera.zoom, 0);\r
                        return false;\r
                }\r
+               \r
+               @Override\r
+               public boolean panStop (float x, float y, int pointer, int button) {\r
+                       Gdx.app.log("GestureDetectorTest", "pan stop at " + x + ", " + y);\r
+                       return false;\r
+               }\r
 \r
                @Override\r
                public boolean zoom (float originalDistance, float currentDistance) {\r
index e1a559c..e8e4798 100644 (file)
@@ -33,7 +33,7 @@ public class BulletTest implements ApplicationListener, InputProcessor, GestureL
        public PerformanceCounter performanceCounter = new PerformanceCounter(this.getClass().getSimpleName());
        public FloatCounter fpsCounter = new FloatCounter(5);
        public PerspectiveCamera camera;
-       
+
        @Override
        public boolean keyDown (int keycode) {
                return false;
@@ -124,6 +124,11 @@ public class BulletTest implements ApplicationListener, InputProcessor, GestureL
        }
 
        @Override
+       public boolean panStop (float x, float y, int pointer, int button) {
+               return false;
+       }
+
+       @Override
        public boolean zoom (float initialDistance, float distance) {
                return false;
        }