Refactor input, fix color crash bugs, prototype multi-block placement
This commit is contained in:
@@ -4,7 +4,7 @@ import static io.anuke.mindustry.Vars.*;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Input.Keys;
|
||||
import com.badlogic.gdx.InputAdapter;
|
||||
import com.badlogic.gdx.input.GestureDetector;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
|
||||
import io.anuke.mindustry.Vars;
|
||||
@@ -14,17 +14,26 @@ import io.anuke.mindustry.resource.ItemStack;
|
||||
import io.anuke.mindustry.world.Tile;
|
||||
import io.anuke.mindustry.world.blocks.types.Configurable;
|
||||
import io.anuke.ucore.core.Graphics;
|
||||
import io.anuke.ucore.core.Inputs;
|
||||
import io.anuke.ucore.core.Timers;
|
||||
import io.anuke.ucore.scene.ui.layout.Unit;
|
||||
import io.anuke.ucore.util.Mathf;
|
||||
|
||||
public class AndroidInput extends InputAdapter{
|
||||
public static float mousex, mousey;
|
||||
public static PlaceMode mode = PlaceMode.cursor;
|
||||
public static boolean brokeBlock = false;
|
||||
private static float lmousex, lmousey;
|
||||
private static float warmup;
|
||||
private static float warmupDelay = 20;
|
||||
public class AndroidInput extends InputHandler{
|
||||
public float mousex, mousey;
|
||||
public boolean brokeBlock = false;
|
||||
private float lmousex, lmousey;
|
||||
private float warmup;
|
||||
private float warmupDelay = 20;
|
||||
|
||||
public AndroidInput(){
|
||||
Inputs.addProcessor(new GestureDetector(20, 0.5f, 2, 0.15f, new GestureHandler(this)));
|
||||
}
|
||||
|
||||
@Override public float getCursorEndX(){ return Gdx.input.getX(0); }
|
||||
@Override public float getCursorEndY(){ return Gdx.input.getY(0); }
|
||||
@Override public float getCursorX(){ return mousex; }
|
||||
@Override public float getCursorY(){ return mousey; }
|
||||
|
||||
@Override
|
||||
public boolean keyDown(int keycode){
|
||||
@@ -62,13 +71,24 @@ public class AndroidInput extends InputAdapter{
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetCursor(){
|
||||
mousex = Gdx.graphics.getWidth()/2;
|
||||
mousey = Gdx.graphics.getHeight()/2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean cursorNear(){
|
||||
return true;
|
||||
}
|
||||
|
||||
public static Tile selected(){
|
||||
public Tile selected(){
|
||||
Vector2 vec = Graphics.world(mousex, mousey);
|
||||
return Vars.world.tile(Mathf.scl2(vec.x, tilesize), Mathf.scl2(vec.y, tilesize));
|
||||
}
|
||||
|
||||
public static void breakBlock(){
|
||||
public void breakBlock(){
|
||||
Tile tile = selected();
|
||||
player.breaktime += Timers.delta();
|
||||
|
||||
@@ -79,7 +99,7 @@ public class AndroidInput extends InputAdapter{
|
||||
}
|
||||
}
|
||||
|
||||
public static void place(){
|
||||
public void place(){
|
||||
Vector2 vec = Graphics.world(mousex, mousey);
|
||||
|
||||
int tilex = Mathf.scl2(vec.x, tilesize);
|
||||
@@ -95,7 +115,8 @@ public class AndroidInput extends InputAdapter{
|
||||
}
|
||||
}
|
||||
|
||||
public static void doInput(){
|
||||
@Override
|
||||
public void update(){
|
||||
|
||||
if(Gdx.input.isTouched(0) && Mathf.near2d(lmousex, lmousey, Gdx.input.getX(0), Gdx.input.getY(0), Unit.dp.inPixels(50))
|
||||
&& !ui.hasMouse()){
|
||||
@@ -133,7 +154,7 @@ public class AndroidInput extends InputAdapter{
|
||||
}
|
||||
}
|
||||
|
||||
public static int touches(){
|
||||
public int touches(){
|
||||
int sum = 0;
|
||||
for(int i = 0; i < 10; i++){
|
||||
if(Gdx.input.isTouched(i))
|
||||
|
||||
@@ -9,35 +9,33 @@ import com.badlogic.gdx.math.Vector2;
|
||||
import io.anuke.mindustry.Vars;
|
||||
import io.anuke.ucore.core.Core;
|
||||
import io.anuke.ucore.scene.ui.layout.Unit;
|
||||
import io.anuke.ucore.util.Mathf;
|
||||
|
||||
public class GestureHandler extends GestureAdapter{
|
||||
AndroidInput input;
|
||||
Vector2 pinch1 = new Vector2(-1, -1), pinch2 = pinch1.cpy();
|
||||
Vector2 vector = new Vector2();
|
||||
float initzoom = -1;
|
||||
boolean zoomed = false;
|
||||
|
||||
GestureHandler(AndroidInput input){
|
||||
this.input = input;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean longPress(float x, float y){
|
||||
/*
|
||||
Tile tile = World.cursorTile();
|
||||
player.breaktime += Timers.delta();
|
||||
if(!GameState.is(State.menu) && player.breaktime >= tile.block().breaktime){
|
||||
Effects.effect("break", tile.worldx(), tile.worldy());
|
||||
Effects.shake(3f, 1f);
|
||||
tile.setBlock(Blocks.air);
|
||||
player.breaktime = 0f;
|
||||
Sounds.play("break");
|
||||
}*/
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tap (float x, float y, int count, int button) {
|
||||
if(AndroidInput.mode == PlaceMode.touch && !ui.hasMouse() && player.recipe != null &&
|
||||
Vars.control.hasItems(player.recipe.requirements) && !Vars.ui.hasMouse() && !AndroidInput.brokeBlock){
|
||||
AndroidInput.mousex = x;
|
||||
AndroidInput.mousey = y;
|
||||
AndroidInput.place();
|
||||
if(!ui.hasMouse() && player.recipe != null &&
|
||||
Vars.control.hasItems(player.recipe.requirements) && !Vars.ui.hasMouse() && !input.brokeBlock){
|
||||
|
||||
player.placeMode.tapped(Mathf.scl2(x, Vars.tilesize), Mathf.scl2(y, Vars.tilesize), Mathf.scl2(x, Vars.tilesize), Mathf.scl2(y, Vars.tilesize));
|
||||
|
||||
input.mousex = x;
|
||||
input.mousey = y;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -45,12 +43,12 @@ public class GestureHandler extends GestureAdapter{
|
||||
|
||||
@Override
|
||||
public boolean pan(float x, float y, float deltaX, float deltaY){
|
||||
if(player.recipe == null || !Vars.control.hasItems(player.recipe.requirements) || AndroidInput.mode == PlaceMode.touch){
|
||||
if(player.recipe == null || !Vars.control.hasItems(player.recipe.requirements) || !player.placeMode.lockCamera){
|
||||
player.x -= deltaX*Core.camera.zoom/Core.cameraScale;
|
||||
player.y += deltaY*Core.camera.zoom/Core.cameraScale;
|
||||
}else if(AndroidInput.mode == PlaceMode.cursor){
|
||||
AndroidInput.mousex += deltaX;
|
||||
AndroidInput.mousey += deltaY;
|
||||
}else if(player.placeMode.lockCamera){
|
||||
input.mousex += deltaX;
|
||||
input.mousey += deltaY;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
@@ -2,6 +2,7 @@ package io.anuke.mindustry.input;
|
||||
|
||||
import static io.anuke.mindustry.Vars.*;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Input.Buttons;
|
||||
import com.badlogic.gdx.Input.Keys;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
@@ -19,11 +20,36 @@ import io.anuke.ucore.core.Timers;
|
||||
import io.anuke.ucore.scene.utils.Cursors;
|
||||
import io.anuke.ucore.util.Mathf;
|
||||
|
||||
public class Input{
|
||||
public class Input extends InputHandler{
|
||||
float mousex, mousey;
|
||||
|
||||
public static void doInput(){
|
||||
//player is dead
|
||||
if(player.health <= 0) return;
|
||||
@Override public float getCursorEndX(){ return Gdx.input.getX(); }
|
||||
@Override public float getCursorEndY(){ return Gdx.input.getY(); }
|
||||
@Override public float getCursorX(){ return mousex; }
|
||||
@Override public float getCursorY(){ return mousey; }
|
||||
|
||||
@Override
|
||||
public boolean touchDown (int screenX, int screenY, int pointer, int button) {
|
||||
if(button == Buttons.LEFT){
|
||||
mousex = screenX;
|
||||
mousey = screenY;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean touchUp(int screenX, int screenY, int pointer, int button){
|
||||
player.placeMode.tapped(getBlockX(), getBlockY(), getBlockEndX(), getBlockEndY());
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(){
|
||||
if(player.isDead()) return;
|
||||
|
||||
if(!Inputs.buttonDown(Buttons.LEFT)){
|
||||
mousex = Gdx.input.getX();
|
||||
mousey = Gdx.input.getY();
|
||||
}
|
||||
|
||||
if(Inputs.scrolled() && Inputs.keyDown("zoom_hold") && !GameState.is(State.menu) && !Vars.ui.onDialog()){
|
||||
Vars.renderer.scaleCamera(Inputs.scroll());
|
||||
@@ -52,21 +78,7 @@ public class Input{
|
||||
|
||||
Tile cursor = Vars.world.tile(tilex(), tiley());
|
||||
|
||||
if(Inputs.buttonUp(Buttons.LEFT) && player.recipe != null &&
|
||||
Vars.world.validPlace(tilex(), tiley(), player.recipe.result) && !ui.hasMouse() && cursorNear() &&
|
||||
Vars.control.hasItems(player.recipe.requirements)){
|
||||
|
||||
Vars.world.placeBlock(tilex(), tiley(), player.recipe.result, player.rotation, true);
|
||||
|
||||
for(ItemStack stack : player.recipe.requirements){
|
||||
Vars.control.removeItem(stack);
|
||||
}
|
||||
|
||||
if(!Vars.control.hasItems(player.recipe.requirements)){
|
||||
Cursors.restoreCursor();
|
||||
}
|
||||
|
||||
}else if(Inputs.buttonUp(Buttons.LEFT)){
|
||||
if(Inputs.buttonUp(Buttons.LEFT) && cursor != null){
|
||||
Tile linked = cursor.isLinked() ? cursor.getLinked() : cursor;
|
||||
if(linked != null && linked.block() instanceof Configurable){
|
||||
Vars.ui.showConfig(linked);
|
||||
@@ -98,28 +110,40 @@ public class Input{
|
||||
|
||||
}
|
||||
|
||||
public static boolean cursorNear(){
|
||||
return Vector2.dst(player.x, player.y, tilex() * tilesize, tiley() * tilesize) <= placerange;
|
||||
public void tryPlaceBlock(int x, int y){
|
||||
if(player.recipe != null &&
|
||||
Vars.world.validPlace(x, y, player.recipe.result) && !ui.hasMouse() && cursorNear() &&
|
||||
Vars.control.hasItems(player.recipe.requirements)){
|
||||
|
||||
Vars.world.placeBlock(x, y, player.recipe.result, player.rotation, true);
|
||||
|
||||
for(ItemStack stack : player.recipe.requirements){
|
||||
Vars.control.removeItem(stack);
|
||||
}
|
||||
|
||||
if(!Vars.control.hasItems(player.recipe.requirements)){
|
||||
Cursors.restoreCursor();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean onConfigurable(){
|
||||
Tile tile = Vars.world.tile(tilex(), tiley());
|
||||
return tile != null && (tile.block() instanceof Configurable || (tile.isLinked() && tile.getLinked().block() instanceof Configurable));
|
||||
public boolean cursorNear(){
|
||||
return Vector2.dst(player.x, player.y, tilex() * tilesize, tiley() * tilesize) <= placerange;
|
||||
}
|
||||
|
||||
public static int tilex(){
|
||||
public int tilex(){
|
||||
return (player.recipe != null && player.recipe.result.isMultiblock() &&
|
||||
player.recipe.result.width % 2 == 0) ?
|
||||
Mathf.scl(Graphics.mouseWorld().x, tilesize) : Mathf.scl2(Graphics.mouseWorld().x, tilesize);
|
||||
}
|
||||
|
||||
public static int tiley(){
|
||||
public int tiley(){
|
||||
return (player.recipe != null && player.recipe.result.isMultiblock() &&
|
||||
player.recipe.result.height % 2 == 0) ?
|
||||
Mathf.scl(Graphics.mouseWorld().y, tilesize) : Mathf.scl2(Graphics.mouseWorld().y, tilesize);
|
||||
}
|
||||
|
||||
public static int currentWeapon(){
|
||||
public int currentWeapon(){
|
||||
int i = 0;
|
||||
for(Weapon weapon : control.getWeapons()){
|
||||
if(player.weapon == weapon)
|
||||
|
||||
53
core/src/io/anuke/mindustry/input/InputHandler.java
Normal file
53
core/src/io/anuke/mindustry/input/InputHandler.java
Normal file
@@ -0,0 +1,53 @@
|
||||
package io.anuke.mindustry.input;
|
||||
|
||||
import static io.anuke.mindustry.Vars.*;
|
||||
|
||||
import com.badlogic.gdx.InputAdapter;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
|
||||
import io.anuke.mindustry.Vars;
|
||||
import io.anuke.mindustry.resource.ItemStack;
|
||||
import io.anuke.mindustry.world.Tile;
|
||||
import io.anuke.mindustry.world.blocks.types.Configurable;
|
||||
import io.anuke.ucore.core.Graphics;
|
||||
import io.anuke.ucore.scene.utils.Cursors;
|
||||
import io.anuke.ucore.util.Mathf;
|
||||
|
||||
public abstract class InputHandler extends InputAdapter{
|
||||
public abstract void update();
|
||||
public abstract float getCursorX();
|
||||
public abstract float getCursorY();
|
||||
public abstract float getCursorEndX();
|
||||
public abstract float getCursorEndY();
|
||||
public int getBlockX(){ return Mathf.scl2(Graphics.world(getCursorX(), getCursorY()).x, Vars.tilesize); }
|
||||
public int getBlockY(){ return Mathf.scl2(Graphics.world(getCursorX(), getCursorY()).y, Vars.tilesize); }
|
||||
public int getBlockEndX(){ return Mathf.scl2(Graphics.world(getCursorEndX(), getCursorEndY()).x, Vars.tilesize); }
|
||||
public int getBlockEndY(){ return Mathf.scl2(Graphics.world(getCursorEndX(), getCursorEndY()).y, Vars.tilesize); }
|
||||
public void resetCursor(){}
|
||||
|
||||
public boolean onConfigurable(){
|
||||
Tile tile = Vars.world.tile(getBlockX(), getBlockY());
|
||||
return tile != null && (tile.block() instanceof Configurable || (tile.isLinked() && tile.getLinked().block() instanceof Configurable));
|
||||
}
|
||||
|
||||
public boolean cursorNear(){
|
||||
return Vector2.dst(player.x, player.y, getBlockX() * tilesize, getBlockY() * tilesize) <= placerange;
|
||||
}
|
||||
|
||||
public void tryPlaceBlock(int x, int y){
|
||||
if(player.recipe != null &&
|
||||
Vars.world.validPlace(x, y, player.recipe.result) && !ui.hasMouse() && cursorNear() &&
|
||||
Vars.control.hasItems(player.recipe.requirements)){
|
||||
|
||||
Vars.world.placeBlock(x, y, player.recipe.result, player.rotation, true);
|
||||
|
||||
for(ItemStack stack : player.recipe.requirements){
|
||||
Vars.control.removeItem(stack);
|
||||
}
|
||||
|
||||
if(!Vars.control.hasItems(player.recipe.requirements)){
|
||||
Cursors.restoreCursor();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,144 @@
|
||||
package io.anuke.mindustry.input;
|
||||
|
||||
import static io.anuke.mindustry.Vars.*;
|
||||
|
||||
import com.badlogic.gdx.Input.Buttons;
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
import com.badlogic.gdx.math.MathUtils;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
|
||||
import io.anuke.mindustry.Vars;
|
||||
import io.anuke.mindustry.world.Tile;
|
||||
import io.anuke.ucore.core.Draw;
|
||||
import io.anuke.ucore.core.Inputs;
|
||||
import io.anuke.ucore.core.Timers;
|
||||
import io.anuke.ucore.scene.utils.Cursors;
|
||||
import io.anuke.ucore.util.Tmp;
|
||||
|
||||
public enum PlaceMode{
|
||||
cursor, touch;
|
||||
cursor{
|
||||
public void draw(int tilex, int tiley, int endx, int endy){
|
||||
float x = tilex * Vars.tilesize;
|
||||
float y = tiley * Vars.tilesize;
|
||||
|
||||
boolean valid = world.validPlace(tilex, tiley, player.recipe.result) && (android || control.getInput().cursorNear());
|
||||
|
||||
Vector2 offset = player.recipe.result.getPlaceOffset();
|
||||
|
||||
float si = MathUtils.sin(Timers.time() / 6f) + 1;
|
||||
|
||||
Draw.color(valid ? Color.PURPLE : Color.SCARLET);
|
||||
Draw.thickness(2f);
|
||||
Draw.linecrect(x + offset.x, y + offset.y, tilesize * player.recipe.result.width + si, tilesize * player.recipe.result.height + si);
|
||||
|
||||
player.recipe.result.drawPlace(tilex, tiley, player.rotation, valid);
|
||||
Draw.thickness(2f);
|
||||
|
||||
if(player.recipe.result.rotate){
|
||||
Draw.color(Color.ORANGE);
|
||||
Tmp.v1.set(7, 0).rotate(player.rotation * 90);
|
||||
Draw.line(x, y, x + Tmp.v1.x, y + Tmp.v1.y);
|
||||
}
|
||||
|
||||
if(valid)
|
||||
Cursors.setHand();
|
||||
else
|
||||
Cursors.restoreCursor();
|
||||
}
|
||||
},
|
||||
touch{
|
||||
{
|
||||
lockCamera = true;
|
||||
}
|
||||
|
||||
public void draw(int tilex, int tiley, int endx, int endy){
|
||||
|
||||
float t = Vars.tilesize;
|
||||
|
||||
float x = tilex * t, y = tiley * t,
|
||||
x2 = endx * t, y2 = endy * t;
|
||||
|
||||
if(x2 < x){
|
||||
float d = x;
|
||||
x = x2;
|
||||
x2 = d;
|
||||
}
|
||||
|
||||
if(y2 < y){
|
||||
float d = y;
|
||||
y = y2;
|
||||
y2 = d;
|
||||
}
|
||||
|
||||
if(x2 >= x){
|
||||
x -= t/2;
|
||||
x2 += t/2;
|
||||
}
|
||||
|
||||
if(y2 >= y){
|
||||
y -= t/2;
|
||||
y2 += t/2;
|
||||
}
|
||||
|
||||
if(tilex == endx && tiley == endy){
|
||||
cursor.draw(tilex, tiley, endx, endy);
|
||||
}else{
|
||||
Draw.color(Color.GREEN);
|
||||
Draw.linerect(x, y, x2 - x, y2 - y);
|
||||
Draw.alpha(0.3f);
|
||||
Draw.crect("blank", x, y, x2 - x, y2 - y);
|
||||
Draw.reset();
|
||||
}
|
||||
}
|
||||
|
||||
public void tapped(int tilex, int tiley, int endx, int endy){
|
||||
if(endx < tilex){
|
||||
int t = endx;
|
||||
endx = tilex;
|
||||
tilex = t;
|
||||
}
|
||||
if(endy < tiley){
|
||||
int t = endy;
|
||||
endy = tiley;
|
||||
tiley = t;
|
||||
}
|
||||
|
||||
for(int x = tilex; x <= endx; x ++){
|
||||
for(int y = tiley; y <= endy; y ++){
|
||||
control.getInput().tryPlaceBlock(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
},
|
||||
breaker{
|
||||
public void draw(int tilex, int tiley, int endx, int endy){
|
||||
Tile tile = world.tile(tilex, tiley);
|
||||
|
||||
if(tile != null && world.validBreak(tilex, tiley)){
|
||||
if(tile.isLinked())
|
||||
tile = tile.getLinked();
|
||||
Vector2 offset = tile.block().getPlaceOffset();
|
||||
float fract = player.breaktime / tile.getBreakTime();
|
||||
|
||||
if(Inputs.buttonDown(Buttons.RIGHT)){
|
||||
Draw.color(Color.YELLOW, Color.SCARLET, fract);
|
||||
Draw.linecrect(tile.worldx() + offset.x, tile.worldy() + offset.y, tile.block().width * Vars.tilesize, tile.block().height * Vars.tilesize);
|
||||
}else if(android && player.breaktime > 0){
|
||||
Draw.color(Color.YELLOW, Color.SCARLET, fract);
|
||||
Draw.circle(tile.worldx(), tile.worldy(), 4 + (1f - fract) * 26);
|
||||
}
|
||||
Draw.reset();
|
||||
}
|
||||
}
|
||||
};
|
||||
public boolean lockCamera;
|
||||
|
||||
public void draw(int tilex, int tiley, int endx, int endy){
|
||||
|
||||
}
|
||||
|
||||
public void tapped(int tilex, int tiley, int endx, int endy){
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user