• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright 2011 See AUTHORS file.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 
17 package com.badlogic.gdx.tests;
18 
19 import com.badlogic.gdx.ApplicationListener;
20 import com.badlogic.gdx.Gdx;
21 import com.badlogic.gdx.graphics.GL20;
22 import com.badlogic.gdx.graphics.OrthographicCamera;
23 import com.badlogic.gdx.graphics.Texture;
24 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
25 import com.badlogic.gdx.input.GestureDetector;
26 import com.badlogic.gdx.input.GestureDetector.GestureListener;
27 import com.badlogic.gdx.math.Vector2;
28 import com.badlogic.gdx.tests.utils.GdxTest;
29 
30 public class GestureDetectorTest extends GdxTest implements ApplicationListener {
31 	Texture texture;
32 	SpriteBatch batch;
33 	OrthographicCamera camera;
34 	CameraController controller;
35 	GestureDetector gestureDetector;
36 
37 	class CameraController implements GestureListener {
38 		float velX, velY;
39 		boolean flinging = false;
40 		float initialScale = 1;
41 
touchDown(float x, float y, int pointer, int button)42 		public boolean touchDown (float x, float y, int pointer, int button) {
43 			flinging = false;
44 			initialScale = camera.zoom;
45 			return false;
46 		}
47 
48 		@Override
tap(float x, float y, int count, int button)49 		public boolean tap (float x, float y, int count, int button) {
50 			Gdx.app.log("GestureDetectorTest", "tap at " + x + ", " + y + ", count: " + count);
51 			return false;
52 		}
53 
54 		@Override
longPress(float x, float y)55 		public boolean longPress (float x, float y) {
56 			Gdx.app.log("GestureDetectorTest", "long press at " + x + ", " + y);
57 			return false;
58 		}
59 
60 		@Override
fling(float velocityX, float velocityY, int button)61 		public boolean fling (float velocityX, float velocityY, int button) {
62 			Gdx.app.log("GestureDetectorTest", "fling " + velocityX + ", " + velocityY);
63 			flinging = true;
64 			velX = camera.zoom * velocityX * 0.5f;
65 			velY = camera.zoom * velocityY * 0.5f;
66 			return false;
67 		}
68 
69 		@Override
pan(float x, float y, float deltaX, float deltaY)70 		public boolean pan (float x, float y, float deltaX, float deltaY) {
71 			// Gdx.app.log("GestureDetectorTest", "pan at " + x + ", " + y);
72 			camera.position.add(-deltaX * camera.zoom, deltaY * camera.zoom, 0);
73 			return false;
74 		}
75 
76 		@Override
panStop(float x, float y, int pointer, int button)77 		public boolean panStop (float x, float y, int pointer, int button) {
78 			Gdx.app.log("GestureDetectorTest", "pan stop at " + x + ", " + y);
79 			return false;
80 		}
81 
82 		@Override
zoom(float originalDistance, float currentDistance)83 		public boolean zoom (float originalDistance, float currentDistance) {
84 			float ratio = originalDistance / currentDistance;
85 			camera.zoom = initialScale * ratio;
86 			System.out.println(camera.zoom);
87 			return false;
88 		}
89 
90 		@Override
pinch(Vector2 initialFirstPointer, Vector2 initialSecondPointer, Vector2 firstPointer, Vector2 secondPointer)91 		public boolean pinch (Vector2 initialFirstPointer, Vector2 initialSecondPointer, Vector2 firstPointer, Vector2 secondPointer) {
92 			return false;
93 		}
94 
update()95 		public void update () {
96 			if (flinging) {
97 				velX *= 0.98f;
98 				velY *= 0.98f;
99 				camera.position.add(-velX * Gdx.graphics.getDeltaTime(), velY * Gdx.graphics.getDeltaTime(), 0);
100 				if (Math.abs(velX) < 0.01f) velX = 0;
101 				if (Math.abs(velY) < 0.01f) velY = 0;
102 			}
103 		}
104 
105 		@Override
pinchStop()106 		public void pinchStop () {
107 		}
108 	}
109 
110 	@Override
create()111 	public void create () {
112 		texture = new Texture("data/stones.jpg");
113 		batch = new SpriteBatch();
114 		camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
115 		controller = new CameraController();
116 		gestureDetector = new GestureDetector(20, 0.5f, 2, 0.15f, controller);
117 		Gdx.input.setInputProcessor(gestureDetector);
118 	}
119 
120 	@Override
render()121 	public void render () {
122 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
123 		controller.update();
124 		camera.update();
125 		batch.setProjectionMatrix(camera.combined);
126 		batch.begin();
127 		batch.draw(texture, 0, 0, texture.getWidth() * 2, texture.getHeight() * 2);
128 		batch.end();
129 	}
130 
131 	@Override
dispose()132 	public void dispose () {
133 		texture.dispose();
134 		batch.dispose();
135 	}
136 }
137