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;
}
} |
Partager