Skip to content

Commit

Permalink
Merge branch 'better-multitouch'
Browse files Browse the repository at this point in the history
  • Loading branch information
italankin committed May 24, 2022
2 parents 4093fe9 + a917d53 commit d42cbd3
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 32 deletions.
41 changes: 28 additions & 13 deletions app/src/main/java/com/italankin/fifteen/GameSurface.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ public class GameSurface extends View implements TopPanelView.Callback {

private final StatisticsManager statisticsManager;

private int mGestureStartX;
private int mGestureStartY;
private float mGestureStartX;
private float mGestureStartY;
private boolean mGestureTrail = false;
private boolean mSecondaryPointer = false;
private long lastSolvedTimestamp = 0;
private long lastOnDrawTimestamp = 0;

Expand Down Expand Up @@ -164,34 +165,46 @@ protected void onDraw(Canvas canvas) {

@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
int x = (int) event.getX();
int y = (int) event.getY();
int action = event.getActionMasked();

GameState state = GameState.get();

switch (action) {
case MotionEvent.ACTION_DOWN: {
mGestureStartX = x;
mGestureStartY = y;
float x = mGestureStartX = event.getX();
float y = mGestureStartY = event.getY();
boolean fieldFullyVisible = isFieldFullyVisible();
mGestureTrail = fieldFullyVisible && mRectField.contains(x, y) && mField.emptySpaceAt(x, y);
mSecondaryPointer = false;
if (fieldFullyVisible && Settings.hardmode && !state.isNotStarted() && !state.paused) {
state.peeking = mHardModeView.isPeekAt(x, y);
}
return true;
}

case MotionEvent.ACTION_POINTER_DOWN: {
if (!mSecondaryPointer) {
mSecondaryPointer = true;
mField.moveTiles(event.getX(), event.getY(), Game.DIRECTION_DEFAULT);
}
mGestureTrail = false;
int index = event.getActionIndex();
mField.moveTiles(event.getX(index), event.getY(index), Game.DIRECTION_DEFAULT);
return true;
}

case MotionEvent.ACTION_MOVE: {
if (state.paused || state.isSolved() || !isFieldFullyVisible()) {
if (state.paused || state.isSolved() || !isFieldFullyVisible() || mSecondaryPointer) {
return true;
}
float x = event.getX();
float y = event.getY();
if (mGestureTrail) {
mField.moveTiles(x, y, Game.DIRECTION_DEFAULT, false);
return true;
}
int dx = x - mGestureStartX;
int dy = y - mGestureStartY;
float dx = x - mGestureStartX;
float dy = y - mGestureStartY;
float minSwipeDistance = Dimensions.tileSize / 6.0f;
if (Math.abs(dx) > minSwipeDistance || Math.abs(dy) > minSwipeDistance) {
mField.moveTiles(mGestureStartX, mGestureStartY, Game.direction(dx, dy));
Expand All @@ -201,10 +214,12 @@ public boolean onTouchEvent(MotionEvent event) {
}

case MotionEvent.ACTION_UP: {
if (mGestureTrail) {
if (mGestureTrail || mSecondaryPointer) {
return true;
}

float x = event.getX();
float y = event.getY();
if (mHelpOverlay != null && mHelpOverlay.onClick(x, y) || hideHelpOverlay()) {
return true;
}
Expand All @@ -213,8 +228,8 @@ public boolean onTouchEvent(MotionEvent event) {
state.peeking = false;
}

int dx = x - mGestureStartX;
int dy = y - mGestureStartY;
float dx = x - mGestureStartX;
float dy = y - mGestureStartY;

if (mLeaderboard.isShown()) {
mLeaderboard.onClick(mGestureStartX, mGestureStartY, dx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ public void setCallbacks(Callbacks callbacks) {
mCallbacks = callbacks;
}

public boolean isPeekAt(int x, int y) {
public boolean isPeekAt(float x, float y) {
Button peek = findButtonById(ID_PEEK);
return peek.contains(x, y);
}

public boolean onClick(int x, int y) {
public boolean onClick(float x, float y) {
GameState state = GameState.get();
if (mCallbacks != null && !state.isNotStarted() && !state.paused) {
Button check = findButtonById(ID_CHECK);
Expand Down
22 changes: 11 additions & 11 deletions app/src/main/java/com/italankin/fifteen/views/LeaderboardView.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public class LeaderboardView extends BaseView {
private final String mTextBack;
private final String mTextNoData;

private final Rect mRectWidth;
private final Rect mRectHeight;
private final Rect mRectType;
private final Rect mRectMode;
private final Rect mRectSort;
private final RectF mRectWidth;
private final RectF mRectHeight;
private final RectF mRectType;
private final RectF mRectMode;
private final RectF mRectSort;
private final RectF mRectExport;
private final RectF mRectImport;
private final RectF mRectBack;
Expand Down Expand Up @@ -124,18 +124,18 @@ public LeaderboardView(DBHelper helper, Resources res) {
int mLineGap = (int) (Dimensions.surfaceHeight * 0.06f);
mTableMarginTop = marginTop + lineHeight + 3.4f * mLineGap;

mRectType = new Rect(0, marginTop, mSettingsGuides[2], marginTop + lineHeight);
mRectType = new RectF(0, marginTop, mSettingsGuides[2], marginTop + lineHeight);
mRectType.inset(0, -lineHeight / 3);
mRectWidth = new Rect(mSettingsGuides[2], marginTop,
mRectWidth = new RectF(mSettingsGuides[2], marginTop,
(int) Dimensions.surfaceWidth, marginTop + lineHeight);
mRectWidth.inset(0, -lineHeight / 3);
mRectMode = new Rect(0, marginTop + mLineGap,
mRectMode = new RectF(0, marginTop + mLineGap,
mSettingsGuides[2], marginTop + mLineGap + lineHeight);
mRectMode.inset(0, -lineHeight / 3);
mRectHeight = new Rect(mSettingsGuides[2], marginTop + mLineGap,
mRectHeight = new RectF(mSettingsGuides[2], marginTop + mLineGap,
(int) Dimensions.surfaceWidth, marginTop + mLineGap + lineHeight);
mRectHeight.inset(0, -lineHeight / 3);
mRectSort = new Rect(0, marginTop + 2 * mLineGap,
mRectSort = new RectF(0, marginTop + 2 * mLineGap,
(int) Dimensions.surfaceWidth, marginTop + 2 * mLineGap + lineHeight);
mRectSort.inset(0, -lineHeight / 3);

Expand All @@ -153,7 +153,7 @@ public LeaderboardView(DBHelper helper, Resources res) {
mRectImport.inset(0, padding);
}

public void onClick(int x, int y, int dx) {
public void onClick(float x, float y, float dx) {
if (Math.abs(dx) < 15) {
dx = 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public boolean show() {
return super.show();
}

public void onClick(int x, int y, int dx) {
public void onClick(float x, float y, float dx) {
if (Math.abs(dx) < 15) {
dx = 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public void addCallbacks(Callbacks callbacks) {
this.callbacks = callbacks;
}

public void onClick(int x, int y) {
public void onClick(float x, float y) {
if (mRectBack.contains(x, y)) {
hide();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void draw(Canvas canvas, float valueRight, float textLeft, float textYOff
}

@Override
public void onClick(int x, int y, int dx) {
public void onClick(float x, float y, float dx) {
if (mRectBf.contains(x, y)) {
Settings.hardmode = !Settings.hardmode;
Settings.save();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void draw(Canvas canvas, float valueRight, float textLeft, float textYOff
}

@Override
public void onClick(int x, int y, int dx) {
public void onClick(float x, float y, float dx) {
if (mRectWidth.contains(x, y)) {
Settings.gameWidth += ((dx == 0) ? 1 : Math.signum(dx));
if (Settings.gameWidth < Constants.MIN_GAME_WIDTH) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void draw(Canvas canvas, float valueRight, float textLeft, float textYOff
}

@Override
public void onClick(int x, int y, int dx) {
public void onClick(float x, float y, float dx) {
if (mRectTimeFormat.contains(x, y)) {
Settings.timeFormat = (++Settings.timeFormat % Constants.TIME_FORMATS);
Settings.save();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface SettingsPage {

void draw(Canvas canvas, float valueRight, float textLeft, float textYOffset);

void onClick(int x, int y, int dx);
void onClick(float x, float y, float dx);

void update();

Expand Down

0 comments on commit d42cbd3

Please sign in to comment.