IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

Pinch to Zoom avec GestureDetector de LibGdx


Sujet :

Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Futur Membre du Club
    Homme Profil pro
    Développeur Java
    Inscrit en
    Août 2014
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Meuse (Lorraine)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Août 2014
    Messages : 3
    Par défaut Pinch to Zoom avec GestureDetector de LibGdx
    Salut tout le monde, je viens ici pour demander un petit coup de pouce :

    J'ai une classe qui extends GestureAdapter qui est presque terminée mais pour zoomer, au lieu de zoomer/dézoomer simplement au centre de l'écran j'aimerai que l'endroit qui est au milieu des 2 doigts (par une moyenne des 2 pointeurs) reste au milieu des 2 doigts, l'histoire d'avoir un zoom précis et clair, j'ai essayé plusieurs et je commence à désespérer !

    Je vous remerci d'avance pour votre aide

    Voici la classe WeedFarmerGame.java :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    package com.bertho.weedFarmer;
     
    import com.badlogic.gdx.ApplicationAdapter;
    import com.badlogic.gdx.Gdx;
    import com.badlogic.gdx.graphics.GL20;
    import com.badlogic.gdx.graphics.OrthographicCamera;
    import com.badlogic.gdx.graphics.g2d.BitmapFont;
    import com.badlogic.gdx.graphics.g2d.SpriteBatch;
    import com.badlogic.gdx.input.GestureDetector;
    import com.badlogic.gdx.maps.tiled.TiledMap;
    import com.badlogic.gdx.maps.tiled.TmxMapLoader;
    import com.badlogic.gdx.maps.tiled.renderers.IsometricTiledMapRenderer;
    import com.bertho.weedFarmer.utils.CameraGestureController;
     
    public class WeedFarmerGame extends ApplicationAdapter {
        private TiledMap map;
        private IsometricTiledMapRenderer renderer;
        private OrthographicCamera camera;
        private BitmapFont font;
        private SpriteBatch batch;
     
        private CameraGestureController cameraController;
        private GestureDetector gestureDetector;
     
        @Override
        public void create () {
            float w = Gdx.graphics.getWidth();
            float h = Gdx.graphics.getHeight();
     
            map = new TmxMapLoader().load("maps/isometric.tmx");
            renderer = new IsometricTiledMapRenderer(map);
     
            int mapPixelWidth = map.getProperties().get("width", Integer.class) * map.getProperties().get("tilewidth", Integer.class);
            int mapPixelHeight = map.getProperties().get("height", Integer.class) * map.getProperties().get("tileheight", Integer.class);
     
            camera = new OrthographicCamera(w/(h/mapPixelHeight), mapPixelHeight);
            camera.position.set(mapPixelWidth/2, map.getProperties().get("tileheight", Integer.class) / 2, 0);
            camera.update();
     
            cameraController = new CameraGestureController(camera, map);
            gestureDetector = new GestureDetector(cameraController);
            Gdx.input.setInputProcessor(gestureDetector);
     
            batch = new SpriteBatch();
            font = new BitmapFont();
     
            Gdx.app.log("Width & Height", "Gdx.graphics.getWidth() " + Gdx.graphics.getWidth() + ", Gdx.graphics.getHeight() " + Gdx.graphics.getHeight());
            Gdx.app.log("Camera viewport width & height", "camera.viewportWidth " + camera.viewportWidth + ", camera.viewportHeight " + camera.viewportHeight);
            Gdx.app.log("Map", "mapPixelWidth " + mapPixelWidth + "px, mapPixelHeight " + mapPixelHeight + "px");
        }
     
        @Override
        public void render () {
            Gdx.gl.glClearColor(0.55f, 0.55f, 0.55f, 1f);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
     
            cameraController.update();
            camera.update();
     
            renderer.setView(camera);
            renderer.render();
     
            batch.begin();
            font.draw(batch, "FPS: " + Gdx.graphics.getFramesPerSecond() + " | Camera position : X=" + camera.position.x + " Y=" + camera.position.y + " | Zoom : zoom=" + camera.zoom +
                    " | Camera Top=" + (camera.position.y + camera.viewportHeight/2*camera.zoom) + " Bottom=" + (camera.position.y + camera.viewportHeight/2*camera.zoom) + " Left=" + (camera.position.x - camera.viewportWidth/2*camera.zoom) + " Right=" + (camera.position.x + camera.viewportWidth/2*camera.zoom), 10, 20);
            batch.end();
        }
     
        @Override
        public void resize(int width, int height) {
            //TODO resize la camera proportionnellement par rapport à avant
        }
     
        @Override
        public void dispose () {
            map.dispose();
        }
    }
    Et maintenant la classe concernée, CameraGestureController.java :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    package com.bertho.weedFarmer.utils;
     
    import com.badlogic.gdx.Gdx;
    import com.badlogic.gdx.graphics.OrthographicCamera;
    import com.badlogic.gdx.input.GestureDetector.GestureAdapter;
    import com.badlogic.gdx.maps.tiled.TiledMap;
    import com.badlogic.gdx.math.Vector2;
    import com.badlogic.gdx.math.Vector3;
     
    public class CameraGestureController extends GestureAdapter {
        final private float MINIMUM_FLING_VELOCITY = 200;
     
        private OrthographicCamera camera;
        private TiledMap map;
     
        final public float TOP, BOTTOM, LEFT, RIGHT;
        private int mapPixelWidth, mapPixelHeight;
     
        private float origDistance, baseDistance, origZoom;
     
        private Vector2 pinchInitialPointer1 = new Vector2(0,0), pinchInitialPointer2 = new Vector2(0,0);
        private Vector3 pinchPosition = new Vector3(0,0,0), pinchLastPosition = new Vector3(0,0,0);
        private Vector3 pinchLastMapPosition = new Vector3(0,0,0), pinchMapPositionBeforeZoom = new Vector3(0,0,0);
     
        private boolean flingable = true;
        private float velX = 0, velY = 0;
     
     
        public CameraGestureController (OrthographicCamera camera, TiledMap map) {
            this.camera = camera;
            this.map = map;
     
            mapPixelWidth = map.getProperties().get("width", Integer.class) * map.getProperties().get("tilewidth", Integer.class);
            mapPixelHeight = map.getProperties().get("height", Integer.class) * map.getProperties().get("tileheight", Integer.class);
     
            LEFT = 0;
            RIGHT = mapPixelWidth;
            TOP = mapPixelHeight/2 + map.getProperties().get("tileheight", Integer.class)/2;
            BOTTOM = -(mapPixelHeight/2 - map.getProperties().get("tileheight", Integer.class)/2);
        }
     
        @Override
        public boolean longPress (float x, float y) {
            Gdx.app.log("GestureDetectorTest", "long press at " + x + ", " + y);
            return false;
        }
     
        @Override
        public boolean fling (float velocityX, float velocityY, int button) {
            if(Math.abs(velocityX) > MINIMUM_FLING_VELOCITY ||  Math.abs(velocityY) > MINIMUM_FLING_VELOCITY) {
                velX = camera.zoom * velocityX;
                velY = camera.zoom * velocityY;
            }
     
            return false;
        }
     
        @Override
        public boolean pan (float x, float y, float deltaX, float deltaY) {
            moveCamera(true, camera.zoom * -deltaX, camera.zoom * deltaY);
     
            return false;
        }
     
        @Override
        public boolean panStop (float x, float y, int pointer, int button) {
            return false;
        }
     
        @Override
        public boolean zoom (float originalDistance, float currentDistance) {
            flingable = false;
     
            if(origDistance != originalDistance){
                origDistance = originalDistance;
                baseDistance = originalDistance;
                origZoom = camera.zoom;
            }
     
            if (pinchLastPosition.x != 0 && pinchLastPosition.y != 0) {
                //Gdx.app.log("GestureDetectorTest", "Pinch test x=" + (pinchLastPosition.x - pinchPosition.x) + ", y=" + (pinchLastPosition.y - pinchPosition.y));
                moveCamera(true, (pinchLastPosition.x - pinchPosition.x)*camera.zoom, -(pinchLastPosition.y - pinchPosition.y)*camera.zoom);
            }
     
            float ratio = baseDistance/currentDistance;
            float newZoom = origZoom*ratio;
     
            if (newZoom >= 1) {
                camera.zoom = 1;
                origZoom = 1;
                baseDistance = currentDistance;
            } else if (newZoom <= 0.5) {
                camera.zoom = (float) 0.5;
                origZoom = (float) 0.5;
                baseDistance = currentDistance;
            } else {
                camera.zoom = newZoom;
            }
     
            camera.update();
     
            Vector3 pinchMapPosition = camera.unproject(new Vector3(pinchPosition.x, pinchPosition.y, 0));
            Gdx.app.log("GestureDetectorTest", "Actual position x=" + pinchMapPosition.x + ", y=" + pinchMapPosition.y + " to x=" + pinchPosition.x + ", y=" + pinchPosition.y);
            Gdx.app.log("GestureDetectorTest", "-----------------");
     
            return false;
        }
     
        @Override
        public boolean pinch (Vector2 initialFirstPointer, Vector2 initialSecondPointer, Vector2 firstPointer, Vector2 secondPointer) {
            if (pinchPosition.x != 0 && pinchPosition.y != 0) {
                pinchLastPosition = new Vector3(pinchPosition.x, pinchPosition.y, 0);
                pinchLastMapPosition = camera.unproject(new Vector3(pinchPosition.x, pinchPosition.y, 0));
            }
     
            if (!pinchInitialPointer1.equals(initialFirstPointer) || !pinchInitialPointer2.equals(initialSecondPointer)) {
                pinchInitialPointer1 = initialFirstPointer.cpy();
                pinchInitialPointer2 = initialSecondPointer.cpy();
     
                pinchLastPosition.set(0,0,0);
                pinchLastMapPosition.set(0,0,0);
            }
     
            pinchPosition = new Vector3((firstPointer.x+secondPointer.x)/2, (firstPointer.y+secondPointer.y)/2, 0);
     
            pinchMapPositionBeforeZoom = camera.unproject(new Vector3(pinchPosition.x, pinchPosition.y, 0));
            Gdx.app.log("GestureDetectorTest", "BeforeZoom x=" + pinchMapPositionBeforeZoom.x + ", y=" + pinchMapPositionBeforeZoom.y + " to x=" + pinchPosition.x + ", y=" + pinchPosition.y);
     
     
            return false;
        }
     
        @Override
        public boolean touchDown (float x, float y, int pointer, int button) {
            return false;
        }
     
        public void update () {
            if (velX != 0 && velY != 0) {
                if (flingable && !Gdx.input.justTouched()) {
                    velX *= 0.94f;
                    velY *= 0.94f;
     
                    moveCamera(true, -velX * Gdx.graphics.getDeltaTime(), velY * Gdx.graphics.getDeltaTime());
     
                    if (Math.abs(velX) < MINIMUM_FLING_VELOCITY && Math.abs(velY) < MINIMUM_FLING_VELOCITY) {
                        velX = 0;
                        velY = 0;
                    }
                } else {
                    flingable = true;
                    velX = 0;
                    velY = 0;
                }
            }
        }
     
        public void moveCamera (boolean add, float x, float y)
        {
            float newX, newY;
     
            if (add)
            {
                newX = camera.position.x + x;
                newY = camera.position.y + y;
            } else
            {
                newX = x;
                newY = y;
            }
     
            if (newX - camera.viewportWidth/2*camera.zoom < LEFT)
                newX = LEFT + camera.viewportWidth/2*camera.zoom;
            if (newX + camera.viewportWidth/2*camera.zoom > RIGHT)
                newX = RIGHT - camera.viewportWidth/2*camera.zoom;
            if (newY + camera.viewportHeight/2*camera.zoom > TOP)
                newY = TOP - camera.viewportHeight/2*camera.zoom;
            if (newY - camera.viewportHeight/2*camera.zoom < BOTTOM)
                newY = BOTTOM + camera.viewportHeight/2*camera.zoom;
     
            camera.position.x = newX;
            camera.position.y = newY;
        }
    }
    J'espère vraiment que vous pourez m'aider car je reste bloqué à tout essayé depuis plusieurs jours et rien ne va comme je voudrais

    Encore merci et à bientôt

  2. #2
    Futur Membre du Club
    Homme Profil pro
    Développeur Java
    Inscrit en
    Août 2014
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Meuse (Lorraine)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Août 2014
    Messages : 3
    Par défaut
    J'ai finalement trouvé la solution moi m^me, c'est dans Pinch et Zoom que ça bouge :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    package com.bertho.weedFarmer.utils;
     
    import com.badlogic.gdx.Gdx;
    import com.badlogic.gdx.graphics.OrthographicCamera;
    import com.badlogic.gdx.input.GestureDetector.GestureAdapter;
    import com.badlogic.gdx.maps.tiled.TiledMap;
    import com.badlogic.gdx.math.Vector2;
    import com.badlogic.gdx.math.Vector3;
     
    public class CameraGestureController extends GestureAdapter {
    	final private float MINIMUM_FLING_VELOCITY = 200;
     
    	private OrthographicCamera camera;
    	private TiledMap map;
     
    	final public float TOP, BOTTOM, LEFT, RIGHT;
    	private int mapPixelWidth, mapPixelHeight;
     
    	private float origDistance, baseDistance, origZoom;
     
    	private Vector2 pinchInitialPointer1 = new Vector2(0,0), pinchInitialPointer2 = new Vector2(0,0);
    	private Vector3 pinchPosition = new Vector3(0,0,0), pinchLastPosition = new Vector3(0,0,0);
    	private Vector3 pinchLastMapPosition = new Vector3(0,0,0);
     
    	private boolean flingable = true;
    	private float velX = 0, velY = 0;
     
     
    	public CameraGestureController (OrthographicCamera camera, TiledMap map) {
    		this.camera = camera;
    		this.map = map;
     
    		mapPixelWidth = map.getProperties().get("width", Integer.class) * map.getProperties().get("tilewidth", Integer.class);
    		mapPixelHeight = map.getProperties().get("height", Integer.class) * map.getProperties().get("tileheight", Integer.class);
     
    		LEFT = 0;
    		RIGHT = mapPixelWidth;
    		TOP = mapPixelHeight/2 + map.getProperties().get("tileheight", Integer.class)/2;
    		BOTTOM = -(mapPixelHeight/2 - map.getProperties().get("tileheight", Integer.class)/2);
    	}
     
    	@Override
    	public boolean longPress (float x, float y) {
    		Gdx.app.log("GestureDetectorTest", "long press at " + x + ", " + y);
    		return false;
    	}
     
    	@Override
    	public boolean fling (float velocityX, float velocityY, int button) {
    		if(Math.abs(velocityX) > MINIMUM_FLING_VELOCITY ||  Math.abs(velocityY) > MINIMUM_FLING_VELOCITY) {
    			velX = camera.zoom * velocityX;
    			velY = camera.zoom * velocityY;
    		}
     
    		return false;
    	}
     
    	@Override
    	public boolean pan (float x, float y, float deltaX, float deltaY) {
    		moveCamera(true, camera.zoom * -deltaX, camera.zoom * deltaY);
     
    		return false;
    	}
     
    	@Override
    	public boolean panStop (float x, float y, int pointer, int button) {
    		return false;
    	}
     
    	@Override
    	public boolean zoom (float originalDistance, float currentDistance) {
    		flingable = false;
     
    		if(origDistance != originalDistance){
    			origDistance = originalDistance;
    			baseDistance = originalDistance;
    			origZoom = camera.zoom;
    		}
     
    		float ratio = baseDistance/currentDistance;
    		float newZoom = origZoom*ratio;
     
    		if (newZoom >= 1) {
    			camera.zoom = 1;
    			origZoom = 1;
    			baseDistance = currentDistance;
    		} else if (newZoom <= 0.5) {
    			camera.zoom = (float) 0.5;
    			origZoom = (float) 0.5;
    			baseDistance = currentDistance;
    		} else {
    			camera.zoom = newZoom;
    		}
     
    		moveCamera(true, 0, 0);
     
    		camera.update();
     
    		/**************************************************************/
     
    		Vector3 pinchMapPosition = camera.unproject(new Vector3(pinchPosition.x, pinchPosition.y, 0));
    		moveCamera(true, pinchLastMapPosition.x - pinchMapPosition.x, pinchLastMapPosition.y - pinchMapPosition.y);
     
    		camera.update();
     
    		pinchLastPosition.set(pinchPosition.x, pinchPosition.y, 0);
    		pinchLastMapPosition = camera.unproject(new Vector3(pinchLastPosition.x, pinchLastPosition.y, 0));
     
    		/**************************************************************/
     
    		return false;
    	}
     
    	@Override
    	public boolean pinch (Vector2 initialFirstPointer, Vector2 initialSecondPointer, Vector2 firstPointer, Vector2 secondPointer) {
    		if (!pinchInitialPointer1.equals(initialFirstPointer) || !pinchInitialPointer2.equals(initialSecondPointer)) {
    			pinchInitialPointer1 = initialFirstPointer.cpy();
    			pinchInitialPointer2 = initialSecondPointer.cpy();
     
    			pinchLastPosition.set((initialFirstPointer.x+initialSecondPointer.x)/2, (initialFirstPointer.y+initialSecondPointer.y)/2, 0);
    			pinchLastMapPosition = camera.unproject(new Vector3(pinchLastPosition.x, pinchLastPosition.y, 0));
    		}
     
    		pinchPosition.set((firstPointer.x+secondPointer.x)/2, (firstPointer.y+secondPointer.y)/2, 0);
     
    		return false;
    	}
     
    	@Override
    	public boolean touchDown (float x, float y, int pointer, int button) {
    		return false;
    	}
     
    	public void update () {
    		if (velX != 0 && velY != 0) {
    			if (flingable && !Gdx.input.justTouched()) {
    				velX *= 0.94f;
    				velY *= 0.94f;
     
    				moveCamera(true, -velX * Gdx.graphics.getDeltaTime(), velY * Gdx.graphics.getDeltaTime());
     
    				if (Math.abs(velX) < MINIMUM_FLING_VELOCITY && Math.abs(velY) < MINIMUM_FLING_VELOCITY) {
    					velX = 0;
    					velY = 0;
    				}
    			} else {
    				flingable = true;
    				velX = 0;
    				velY = 0;
    			}
    		}
    	}
     
    	public void moveCamera (boolean add, float x, float y)
    	{
    		float newX, newY;
     
    		if (add)
    		{
    			newX = camera.position.x + x;
    			newY = camera.position.y + y;
    		} else
    		{
    			newX = x;
    			newY = y;
    		}
     
    		if (newX - camera.viewportWidth/2*camera.zoom < LEFT)
    			newX = LEFT + camera.viewportWidth/2*camera.zoom;
    		if (newX + camera.viewportWidth/2*camera.zoom > RIGHT)
    			newX = RIGHT - camera.viewportWidth/2*camera.zoom;
    		if (newY + camera.viewportHeight/2*camera.zoom > TOP)
    			newY = TOP - camera.viewportHeight/2*camera.zoom;
    		if (newY - camera.viewportHeight/2*camera.zoom < BOTTOM)
    			newY = BOTTOM + camera.viewportHeight/2*camera.zoom;
     
    		camera.position.x = newX;
    		camera.position.y = newY;
    	}
    }
    A bientôt

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Faire un zoom avec adobe première element.
    Par Torim dans le forum Vidéo
    Réponses: 17
    Dernier message: 25/08/2007, 14h17
  2. Probleme de zoom avec scrollpane
    Par SQUAL dans le forum AWT/Swing
    Réponses: 4
    Dernier message: 19/01/2007, 21h49
  3. probleme avec le zoom avec une modification d'image
    Par yoyo59650 dans le forum VB 6 et antérieur
    Réponses: 1
    Dernier message: 22/02/2006, 09h22
  4. zoom avec un formulaire?
    Par ft035580 dans le forum Access
    Réponses: 9
    Dernier message: 18/02/2006, 01h27
  5. Zoom avec du TJPEG
    Par Thylia dans le forum C++Builder
    Réponses: 3
    Dernier message: 17/06/2002, 16h03

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo