feat: reinplémentation de touchmaptiles avec des inputprocessors et gesturedetector.

This commit is contained in:
Nicolas Hordé 2016-01-23 18:41:58 +01:00
parent 26698bd7ca
commit ad84ac96ed
3 changed files with 143 additions and 91 deletions

View File

@ -5,8 +5,10 @@ import java.util.Arrays;
import com.badlogic.gdx.Game; import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.input.GestureDetector.GestureListener;
import com.badlogic.gdx.maps.MapLayers; import com.badlogic.gdx.maps.MapLayers;
import com.badlogic.gdx.maps.tiled.TiledMap; import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer; import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
@ -25,7 +27,7 @@ import fr.evolving.assets.AssetLoader;
import fr.evolving.automata.Level; import fr.evolving.automata.Level;
import fr.evolving.screens.GameScreen.calling; import fr.evolving.screens.GameScreen.calling;
public class TouchMaptiles extends Actor { public class TouchMaptiles extends Actor implements GestureListener,InputProcessor {
private TiledMap map; private TiledMap map;
private OrthogonalTiledMapRenderer MapRenderer; private OrthogonalTiledMapRenderer MapRenderer;
@ -35,11 +37,13 @@ public class TouchMaptiles extends Actor {
private int sizey; private int sizey;
private float viewwidth, viewheight, decx, decy; private float viewwidth, viewheight, decx, decy;
private String selected; private String selected;
private boolean mapexit;
public TouchMaptiles(Level level, int sizex, int sizey) { public TouchMaptiles(Level level, int sizex, int sizey) {
this.level = level; this.level = level;
this.sizex = sizex; this.sizex = sizex;
this.sizey = sizey; this.sizey = sizey;
this.mapexit=false;
map = new TiledMap(); map = new TiledMap();
map.getTileSets().addTileSet(AssetLoader.tileSet); map.getTileSets().addTileSet(AssetLoader.tileSet);
MapLayers layers = map.getLayers(); MapLayers layers = map.getLayers();
@ -60,85 +64,17 @@ public class TouchMaptiles extends Actor {
MapRenderer = new OrthogonalTiledMapRenderer(map, 1 / 128.0f); MapRenderer = new OrthogonalTiledMapRenderer(map, 1 / 128.0f);
camera = new OrthographicCamera(); camera = new OrthographicCamera();
initzoom(); initzoom();
this.addListener(new ActorGestureListener() {
@Override
public void zoom(InputEvent event, float initialDistance,
float distance) {
String[] exec = { "zoomp", "zoomm" };
int zooming = (int) (distance / initialDistance * 1000f);
event_coordination(0, 0, zooming, calling.zoom, exec);
}
@Override
public void pinch(InputEvent event, Vector2 initialPointer1,
Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
float deltaX = pointer2.x - pointer1.x;
float deltaY = pointer2.y - pointer1.y;
int angle = (int) ((float) Math.atan2((double) deltaY,
(double) deltaX) * MathUtils.radiansToDegrees);
angle += 90;
if (angle < 0)
angle = 360 - (-angle);
String[] exec = { "transmuter" };
event_coordination(initialPointer1.x, initialPointer1.y, angle,
calling.pinch, exec);
}
@Override
public boolean longPress(Actor actor, float x, float y) {
String[] exec = { "transmuter" };
return event_coordination(x, y, 0, calling.longpress, exec);
}
@Override
public void tap(InputEvent event, float x, float y, int count,
int button) {
String[] exec = { "transmuter" };
if (count == 1)
event_coordination(x, y, button, calling.tap, exec);
else if (count >= 2)
event_coordination(x, y, button, calling.taptap, exec);
}
});
this.addListener(new InputListener() {
@Override
public boolean mouseMoved(InputEvent event, float x, float y) {
String[] exec = { "transmuter" };
return event_coordination(x, y, 0, calling.mouseover, exec);
}
@Override
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
//oldx = 0;
//oldy = 0;
String[] exec = { "cleaner", "infos", "zoomp", "zoomm",
"copper_pen", "fiber_pen", "copper_eraser",
"fiber_eraser", "transmuter_eraser", "all_eraser",
"blank", "transmuter", "copper_brush", "fiber_brush" };
return event_coordination(x, y, button, calling.mouseclick,
exec);
}
@Override
public void touchDragged(InputEvent event, float x, float y,
int pointer) {
String[] exec = { "transmuter", "move", "copper_brush",
"fiber_brush", "copper_eraser", "fiber_eraser",
"transmuter_eraser", "all_eraser", "blank" };
event_coordination(x, y, 0, calling.mousedrag, exec);
}
});
} }
boolean event_coordination(float x, float y, int button, calling call, boolean event_coordination(float x, float y, int button, calling call,
String[] exec) { String[] exec) {
Gdx.app.debug("menu", "xy"+x+","+y);
if (selected != null) { if (selected != null) {
if (Arrays.asList(exec).contains(selected)) { if (Arrays.asList(exec).contains(selected)) {
Vector2 coords = this.screentoworld(x, y); Vector3 coordsscreen = new Vector3();
AssetLoader.Camera.unproject(coordsscreen.set(x, y, 0));
Vector2 coords = this.screentoworld(coordsscreen.x, coordsscreen.y);
if (level.Grid.GetXY(coords.x, coords.y) != null) { if (level.Grid.GetXY(coords.x, coords.y) != null) {
//mapexit = false; mapexit = false;
if (call != calling.mouseover) if (call != calling.mouseover)
Gdx.app.debug("evenement", "mode:" + call + " outil:" Gdx.app.debug("evenement", "mode:" + call + " outil:"
+ selected + " X: " + coords.x + selected + " X: " + coords.x
@ -148,17 +84,17 @@ public class TouchMaptiles extends Actor {
Class<?> base = Class.forName("fr.evolving.screens.GameScreen"); Class<?> base = Class.forName("fr.evolving.screens.GameScreen");
Class<?>[] params = { float.class, float.class, int.class, int.class, boolean.class, int.class, calling.class }; Class<?>[] params = { float.class, float.class, int.class, int.class, boolean.class, int.class, calling.class };
method = base.getDeclaredMethod("map_" + selected, params); method = base.getDeclaredMethod("map_" + selected, params);
method.invoke(((Game) Gdx.app.getApplicationListener()).getScreen(), (float) x, (float) y,(int) coords.x, (int) coords.y, true,(int) button, (calling) call); method.invoke(((Game) Gdx.app.getApplicationListener()).getScreen(), (float) coordsscreen.x, (float) coordsscreen.y,(int) coords.x, (int) coords.y, true,(int) button, (calling) call);
} catch (Exception e) { } catch (Exception e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} /*else { } else {
if (mapexit == false) { if (mapexit == false) {
mapexit = true; mapexit = true;
map.tempclear(); this.tempclear();
} }
}*/ }
} }
} }
@ -272,8 +208,7 @@ public class TouchMaptiles extends Actor {
camera.setToOrtho(false, viewwidth, viewheight); camera.setToOrtho(false, viewwidth, viewheight);
decx = (level.Grid.sizeX - viewwidth) / 2.0f; decx = (level.Grid.sizeX - viewwidth) / 2.0f;
decy = (level.Grid.sizeY - viewheight) / 2.0f; decy = (level.Grid.sizeY - viewheight) / 2.0f;
Gdx.app.debug(getClass().getSimpleName(), "Décalage:" + decx + "x" Gdx.app.debug(getClass().getSimpleName(), "Décalage:" + decx + "x" + decy);
+ decy);
camera.translate(decx, decy); camera.translate(decx, decy);
} }
@ -319,12 +254,125 @@ public class TouchMaptiles extends Actor {
@Override @Override
public void draw(Batch batch, float parentAlpha) { public void draw(Batch batch, float parentAlpha) {
batch.end(); batch.end();
// batch.setProjectionMatrix(camera.combined);
camera.update(); camera.update();
MapRenderer.setView(camera); MapRenderer.setView(camera);
// MapRenderer.setView(camera.combined,0,0,maxx,maxx);
MapRenderer.render(); MapRenderer.render();
batch.begin(); batch.begin();
} }
@Override
public boolean touchDown(float x, float y, int pointer, int button) {
String[] exec = { "cleaner", "infos", "zoomp", "zoomm",
"copper_pen", "fiber_pen", "copper_eraser",
"fiber_eraser", "transmuter_eraser", "all_eraser",
"blank", "transmuter", "copper_brush", "fiber_brush" };
return event_coordination(x, y, button, calling.mouseclick,
exec);
}
@Override
public boolean tap(float x, float y, int count, int button) {
String[] exec = { "transmuter" };
if (count == 1)
event_coordination(x, y, button, calling.tap, exec);
else if (count >= 2)
event_coordination(x, y, button, calling.taptap, exec);
return false;
}
@Override
public boolean longPress(float x, float y) {
String[] exec = { "transmuter" };
return event_coordination(x, y, 0, calling.longpress, exec);
}
@Override
public boolean fling(float velocityX, float velocityY, int button) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean pan(float x, float y, float deltaX, float deltaY) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean panStop(float x, float y, int pointer, int button) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean zoom(float initialDistance, float distance) {
String[] exec = { "zoomp", "zoomm" };
int zooming = (int) (distance / initialDistance * 1000f);
return event_coordination(0, 0, zooming, calling.zoom, exec);
}
@Override
public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2,
Vector2 pointer1, Vector2 pointer2) {
float deltaX = pointer2.x - pointer1.x;
float deltaY = pointer2.y - pointer1.y;
int angle = (int) ((float) Math.atan2((double) deltaY,
(double) deltaX) * MathUtils.radiansToDegrees);
angle += 90;
if (angle < 0)
angle = 360 - (-angle);
String[] exec = { "transmuter" };
return event_coordination(initialPointer1.x, initialPointer1.y, angle, calling.pinch, exec);
}
@Override
public boolean keyDown(int keycode) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean keyUp(int keycode) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean keyTyped(char character) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
String[] exec = { "transmuter", "move", "copper_brush",
"fiber_brush", "copper_eraser", "fiber_eraser",
"transmuter_eraser", "all_eraser", "blank" };
return event_coordination(screenX, screenY, 0, calling.mousedrag, exec);
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
String[] exec = { "transmuter" };
return event_coordination(screenX, screenY, 0, calling.mouseover, exec);
}
@Override
public boolean scrolled(int amount) {
// TODO Auto-generated method stub
return false;
}
} }

View File

@ -27,6 +27,9 @@ public class GameRenderer {
if (layer == 0) { if (layer == 0) {
Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batcher.begin();
GameScreen.map.draw(batcher, 1f);
batcher.end();
} else if (layer == 1) { } else if (layer == 1) {
batcher.begin(); batcher.begin();
batcher.setColor(0.25f, 0.25f, 0.25f, 1f); batcher.setColor(0.25f, 0.25f, 0.25f, 1f);

View File

@ -18,6 +18,7 @@ import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture.TextureFilter; import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.input.GestureDetector; import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.input.GestureDetector.GestureListener;
import com.badlogic.gdx.maps.MapProperties; import com.badlogic.gdx.maps.MapProperties;
import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
@ -71,7 +72,7 @@ public class GameScreen implements Screen {
private InputMultiplexer multiplexer; private InputMultiplexer multiplexer;
private Array<InputProcessor> processors; private Array<InputProcessor> processors;
private WarnDialog dialog; private WarnDialog dialog;
private Stage stage, stage_map, stage_info, stage_tooltip; private Stage stage, stage_info, stage_tooltip;
private VerticalGroup table2; private VerticalGroup table2;
private GameRenderer Renderer; private GameRenderer Renderer;
private float runTime; private float runTime;
@ -97,7 +98,7 @@ public class GameScreen implements Screen {
"separator", "stat" }; "separator", "stat" };
private ButtonLevel buttonlevel; private ButtonLevel buttonlevel;
private Objectives objectives; private Objectives objectives;
private TouchMaptiles map; public TouchMaptiles map;
private Menu menu; private Menu menu;
private HorizBarre horizbar; private HorizBarre horizbar;
private float oldx, oldy; private float oldx, oldy;
@ -220,6 +221,7 @@ public class GameScreen implements Screen {
@Override @Override
public void changed(ChangeEvent event, Actor actor) { public void changed(ChangeEvent event, Actor actor) {
Gdx.app.debug("Barre", "Element changé"); Gdx.app.debug("Barre", "Element changé");
hideInfo();
map.tempclear(); map.tempclear();
map.fillempty(60); map.fillempty(60);
menu.unSelect(); menu.unSelect();
@ -238,9 +240,8 @@ public class GameScreen implements Screen {
multiplexer = new InputMultiplexer(); multiplexer = new InputMultiplexer();
processors = new Array<InputProcessor>(); processors = new Array<InputProcessor>();
stage = new Stage(AssetLoader.viewport); stage = new Stage(AssetLoader.viewport);
stage_map = new Stage(AssetLoader.viewport);
stage_info = new Stage(AssetLoader.viewport); stage_info = new Stage(AssetLoader.viewport);
stage_tooltip = new Stage(AssetLoader.viewport); //stage_tooltip = new Stage(AssetLoader.viewport);
oldx = 0; oldx = 0;
oldy = 0; oldy = 0;
unroll = false; unroll = false;
@ -418,6 +419,7 @@ public class GameScreen implements Screen {
@Override @Override
public void changed(ChangeEvent event, Actor actor) { public void changed(ChangeEvent event, Actor actor) {
Gdx.app.debug("Menu", "Element changé"); Gdx.app.debug("Menu", "Element changé");
hideInfo();
map.tempclear(); map.tempclear();
map.fillempty(60); map.fillempty(60);
if (menu.getTransmuter() != null) if (menu.getTransmuter() != null)
@ -673,13 +675,12 @@ public class GameScreen implements Screen {
if (Preference.prefs.getBoolean("Refresh")) if (Preference.prefs.getBoolean("Refresh"))
fpsLabel.setText(Gdx.graphics.getFramesPerSecond() + "FPS"); fpsLabel.setText(Gdx.graphics.getFramesPerSecond() + "FPS");
Renderer.render(delta, runTime, 0); Renderer.render(delta, runTime, 0);
stage_map.draw();
Renderer.render(delta, runTime, 1); Renderer.render(delta, runTime, 1);
stage.draw(); stage.draw();
if (unroll) if (unroll)
stage_info.draw(); stage_info.draw();
Renderer.render(delta, runTime, 2); Renderer.render(delta, runTime, 2);
stage_tooltip.draw(); //stage_tooltip.draw();
} }
@Override @Override
@ -713,7 +714,6 @@ public class GameScreen implements Screen {
stage_info.addActor(info_cout); stage_info.addActor(info_cout);
stage_info.addActor(info_up); stage_info.addActor(info_up);
stage_info.addActor(info_desc); stage_info.addActor(info_desc);
stage_map.addActor(map);
//stage_tooltip.addActor(tooltip); //stage_tooltip.addActor(tooltip);
stage.addActor(horizbar); stage.addActor(horizbar);
stage.addActor(nextpage); stage.addActor(nextpage);
@ -731,12 +731,13 @@ public class GameScreen implements Screen {
stage.addActor(cout); stage.addActor(cout);
stage.addActor(research); stage.addActor(research);
stage.addActor(menu); stage.addActor(menu);
//gesturedetector=new GestureDetector(null); gesturedetector=new GestureDetector(map);
//processors.add(gesturedetector);
//processors.add(stage_tooltip); //processors.add(stage_tooltip);
processors.add(stage_info); processors.add(stage_info);
processors.add(stage); processors.add(stage);
processors.add(stage_map); processors.add(map);
processors.add(gesturedetector);
multiplexer.setProcessors(processors); multiplexer.setProcessors(processors);
Gdx.input.setInputProcessor(multiplexer); Gdx.input.setInputProcessor(multiplexer);
preparemenu(0); preparemenu(0);