import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;
import android.content.res.Resources;
import space.kozachok.a2048.MainView.*;
class InputListener implements View.OnTouchListener {
//MainActivityGame exmp = new MainActivityGame();
private static final int SWIPE_MIN_DISTANCE = 0;
private static final int SWIPE_THRESHOLD_VELOCITY = 25;
private static final int MOVE_THRESHOLD = 250;
private static final int RESET_STARTING = 10;
private final MainView mView;
private float x;
private float y;
private float lastDx;
private float lastDy;
private float previousX;
private float previousY;
private float startingX;
private float startingY;
private int previousDirection = 1;
private int veryLastDirection = 1;
// Whether or not we have made a move, i.e. the blocks shifted or tried to shift.
private boolean hasMoved = false;
// Whether or not we began the press on an icon. This is to disable swipes if the user began
// the press on an icon.
public static boolean home = false;
public static boolean beganOnIcon = false;
public Drawable backgroundRectangle = MainView.backgroundRectangle;
public InputListener(MainView view) {
super();
this.mView = view;
}
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = event.getX();
y = event.getY();
startingX = x;
startingY = y;
previousX = x;
previousY = y;
lastDx = 0;
lastDy = 0;
hasMoved = false;
beganOnIcon = iconPressed(mView.sXNewGame, mView.sYIcons)
|| iconPressed(mView.sXUndo, mView.sYIcons)
|| iconPressed(mView.sXHome, mView.sYIcons);
return true;
case MotionEvent.ACTION_MOVE:
x = event.getX();
y = event.getY();
if (mView.game.isActive() && !beganOnIcon) {
float dx = x - previousX;
if (Math.abs(lastDx + dx) < Math.abs(lastDx) + Math.abs(dx) && Math.abs(dx) > RESET_STARTING
&& Math.abs(x - startingX) > SWIPE_MIN_DISTANCE) {
startingX = x;
startingY = y;
lastDx = dx;
previousDirection = veryLastDirection;
}
if (lastDx == 0) {
lastDx = dx;
}
float dy = y - previousY;
if (Math.abs(lastDy + dy) < Math.abs(lastDy) + Math.abs(dy) && Math.abs(dy) > RESET_STARTING
&& Math.abs(y - startingY) > SWIPE_MIN_DISTANCE) {
startingX = x;
startingY = y;
lastDy = dy;
previousDirection = veryLastDirection;
}
if (lastDy == 0) {
lastDy = dy;
}
if (pathMoved() > SWIPE_MIN_DISTANCE * SWIPE_MIN_DISTANCE && !hasMoved) {
boolean moved = false;
//Vertical
if (((dy >= SWIPE_THRESHOLD_VELOCITY && Math.abs(dy) >= Math.abs(dx)) || y - startingY >= MOVE_THRESHOLD) && previousDirection % 2 != 0) {
moved = true;
previousDirection = previousDirection * 2;
veryLastDirection = 2;
mView.game.move(2);
} else if (((dy <= -SWIPE_THRESHOLD_VELOCITY && Math.abs(dy) >= Math.abs(dx)) || y - startingY <= -MOVE_THRESHOLD) && previousDirection % 3 != 0) {
moved = true;
previousDirection = previousDirection * 3;
veryLastDirection = 3;
mView.game.move(0);
}
//Horizontal
if (((dx >= SWIPE_THRESHOLD_VELOCITY && Math.abs(dx) >= Math.abs(dy)) || x - startingX >= MOVE_THRESHOLD) && previousDirection % 5 != 0) {
moved = true;
previousDirection = previousDirection * 5;
veryLastDirection = 5;
mView.game.move(1);
} else if (((dx <= -SWIPE_THRESHOLD_VELOCITY && Math.abs(dx) >= Math.abs(dy)) || x - startingX <= -MOVE_THRESHOLD) && previousDirection % 7 != 0) {
moved = true;
previousDirection = previousDirection * 7;
veryLastDirection = 7;
mView.game.move(3);
}
if (moved) {
hasMoved = true;
startingX = x;
startingY = y;
}
}
}
previousX = x;
previousY = y;
return true;
case MotionEvent.ACTION_UP:
x = event.getX();
y = event.getY();
previousDirection = 1;
veryLastDirection = 1;
//"Menu" inputs
if (!hasMoved) {
if (iconPressed(mView.sXNewGame, mView.sYIcons)) {
//alpha
if (!mView.game.gameLost()) {
new AlertDialog.Builder(mView.getContext())
.setPositiveButton(R.string.reset, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mView.game.newGame();
}
})
.setNegativeButton(R.string.continue_game, null)
.setTitle(R.string.reset_dialog_title)
.setMessage(R.string.reset_dialog_message)
.show();
} else {
mView.game.newGame();
}
} else if (iconPressed(mView.sXUndo, mView.sYIcons)) {
//alpha
mView.game.revertUndoState();
} else if (iconPressed(mView.sXHome, mView.sYIcons)) {
//alpha
beganOnIcon = true;
//MainActivityGame.this.onClick();
//exmp.onClick();
} else if (isTap(2) && inRange(mView.startingX, x, mView.endingX)
&& inRange(mView.startingY, x, mView.endingY) && mView.continueButtonEnabled) {
mView.game.setEndlessMode();
}
}
}
return true;
}
private float pathMoved() {
return (x - startingX) * (x - startingX) + (y - startingY) * (y - startingY);
}
private boolean iconPressed(int sx, int sy) {
return isTap(1) && inRange(sx, x, sx + mView.iconSize)
&& inRange(sy, y, sy + mView.iconSize);
}
private boolean inRange(float starting, float check, float ending) {
return (starting <= check && check <= ending);
}
private boolean isTap(int factor) {
return pathMoved() <= mView.iconSize * mView.iconSize * factor;
}
}
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivityGame extends AppCompatActivity {
private long backPressedTime;
private static final String WIDTH = "width";
private static final String HEIGHT = "height";
private static final String SCORE = "score";
private static final String HIGH_SCORE = "high score temp";
private static final String UNDO_SCORE = "undo score";
private static final String CAN_UNDO = "can undo";
private static final String UNDO_GRID = "undo";
private static final String GAME_STATE = "game state";
private static final String UNDO_GAME_STATE = "undo game state";
private MainView view;
/*public static void onClick(View v) {
try {
Intent intent = new Intent(MainActivityGame.this, MainActivity.class);
startActivity(intent);
finish();
} catch (Exception e) {
}
}*/
@Override
public void onBackPressed() {
onClick();
backPressedTime = System.currentTimeMillis();
}
public void onClick (){
Intent intent = new Intent(MainActivityGame.this, MainActivity.class);
startActivity(intent);
finish();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = new MainView(this);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
view.hasSaveState = settings.getBoolean("save_state", false);
if (savedInstanceState != null) {
if (savedInstanceState.getBoolean("hasState")) {
load();
}
}
setContentView(view);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
//Do nothing
return true;
} else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
view.game.move(2);
return true;
} else if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
view.game.move(0);
return true;
} else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
view.game.move(3);
return true;
} else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
view.game.move(1);
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putBoolean("hasState", true);
save();
}
protected void onPause() {
super.onPause();
save();
}
private void save() {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = settings.edit();
Tile[][] field = view.game.grid.field;
Tile[][] undoField = view.game.grid.undoField;
editor.putInt(WIDTH, field.length);
editor.putInt(HEIGHT, field.length);
for (int xx = 0; xx < field.length; xx++) {
for (int yy = 0; yy < field[0].length; yy++) {
if (field[xx][yy] != null) {
editor.putInt(xx + " " + yy, field[xx][yy].getValue());
} else {
editor.putInt(xx + " " + yy, 0);
}
if (undoField[xx][yy] != null) {
editor.putInt(UNDO_GRID + xx + " " + yy, undoField[xx][yy].getValue());
} else {
editor.putInt(UNDO_GRID + xx + " " + yy, 0);
}
}
}
editor.putLong(SCORE, view.game.score);
editor.putLong(HIGH_SCORE, view.game.highScore);
editor.putLong(UNDO_SCORE, view.game.lastScore);
editor.putBoolean(CAN_UNDO, view.game.canUndo);
editor.putInt(GAME_STATE, view.game.gameState);
editor.putInt(UNDO_GAME_STATE, view.game.lastGameState);
editor.commit();
}
protected void onResume() {
super.onResume();
load();
}
private void load() {
//Stopping all animations
view.game.aGrid.cancelAnimations();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
for (int xx = 0; xx < view.game.grid.field.length; xx++) {
for (int yy = 0; yy < view.game.grid.field[0].length; yy++) {
int value = settings.getInt(xx + " " + yy, -1);
if (value > 0) {
view.game.grid.field[xx][yy] = new Tile(xx, yy, value);
} else if (value == 0) {
view.game.grid.field[xx][yy] = null;
}
int undoValue = settings.getInt(UNDO_GRID + xx + " " + yy, -1);
if (undoValue > 0) {
view.game.grid.undoField[xx][yy] = new Tile(xx, yy, undoValue);
} else if (value == 0) {
view.game.grid.undoField[xx][yy] = null;
}
}
}
view.game.score = settings.getLong(SCORE, view.game.score);
view.game.highScore = settings.getLong(HIGH_SCORE, view.game.highScore);
view.game.lastScore = settings.getLong(UNDO_SCORE, view.game.lastScore);
view.game.canUndo = settings.getBoolean(CAN_UNDO, view.game.canUndo);
view.game.gameState = settings.getInt(GAME_STATE, view.game.gameState);
view.game.lastGameState = settings.getInt(UNDO_GAME_STATE, view.game.lastGameState);
}
}