diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2d40793..5fee9ec 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -72,9 +72,16 @@ android:value="org.secuso.privacyfriendly2048.activities.MainActivity" /> + + + + diff --git a/app/src/main/java/org/secuso/privacyfriendly2048/activities/GameActivity.java b/app/src/main/java/org/secuso/privacyfriendly2048/activities/GameActivity.java index 6f665e3..0190ecd 100644 --- a/app/src/main/java/org/secuso/privacyfriendly2048/activities/GameActivity.java +++ b/app/src/main/java/org/secuso/privacyfriendly2048/activities/GameActivity.java @@ -30,6 +30,7 @@ import android.preference.PreferenceManager; import android.util.DisplayMetrics; import android.util.Log; +import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -164,7 +165,36 @@ protected void onCreate(Bundle savedInstanceState) { } - + private void handleUndo() { + undoButton.setVisibility(View.INVISIBLE); + if (undo && last_elements != null) { + gameStatistics.undo(); + elements = last_elements; + points = last_points; + number_field.removeAllViews(); + //number_field_background.removeAllViews(); + points = last_points; + textFieldPoints.setText("" + points); + setDPositions(false); + for (Element[] i : elements) { + for (Element j : i) { + j.setVisibility(View.INVISIBLE); + number_field.addView(j); + j.drawItem(); + } + } + for (int i = 0; i < elements.length; i++) { + for (int j = 0; j < elements[i].length; j++) { + elements[i][j].setOnTouchListener(swipeListener); + backgroundElements[i][j].setOnTouchListener(swipeListener); + } + } + updateGameState(); + drawAllElements(elements); + number_field.refreshDrawableState(); + } + undo = false; + } public void initResources() { number_field = (RelativeLayout) findViewById(R.id.number_field); number_field_background = (RelativeLayout) findViewById(R.id.number_field_background); @@ -180,37 +210,11 @@ public void onClick(View v) { } }); undoButton = (ImageButton) findViewById(R.id.undoButton); + undoButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - undoButton.setVisibility(View.INVISIBLE); - if (undo && last_elements != null) { - gameStatistics.undo(); - elements = last_elements; - points = last_points; - number_field.removeAllViews(); - //number_field_background.removeAllViews(); - points = last_points; - textFieldPoints.setText("" + points); - setDPositions(false); - for (Element[] i : elements) { - for (Element j : i) { - j.setVisibility(View.INVISIBLE); - number_field.addView(j); - j.drawItem(); - } - } - for (int i = 0; i < elements.length; i++) { - for (int j = 0; j < elements[i].length; j++) { - elements[i][j].setOnTouchListener(swipeListener); - backgroundElements[i][j].setOnTouchListener(swipeListener); - } - } - updateGameState(); - drawAllElements(elements); - number_field.refreshDrawableState(); - } - undo = false; + handleUndo(); } }); @@ -408,331 +412,371 @@ public Element[][] deepCopy(Element[][] e) { return r; } - public void setListener() { - swipeListener = new Gestures(this) { - public boolean onSwipeTop() { - Element[][] temp = deepCopy(elements); - int temp_points = points; - moved = false; - Element s = new Element(myActivity); - - for (int i = 0; i < elements.length; i++) { - s.number = elements[0][i].number; - s.posX = 0; - s.posY = i; - - - for (int j = 1; j < elements[i].length; j++) { - if (elements[j][i].number != 0 && (s.number == 0 || s.number == elements[j][i].number)) { - moved = true; - elements[j][i].setNumber(s.number + elements[j][i].number); - elements[s.posX][s.posY].setNumber(0); - switchElementPositions(elements[j][i], elements[s.posX][s.posY]); - Element z = elements[j][i]; - elements[j][i] = elements[s.posX][s.posY]; - elements[s.posX][s.posY] = z; - if (s.number != 0) - points += elements[s.posX][s.posY].number; - if (s.number != 0) - s.posX++; - j = s.posX; - s.number = elements[j][i].number; - - } else if (elements[j][i].number != 0) { - s.number = elements[j][i].number; - s.posX = j; - s.posY = i; - } - } + private boolean handleSwipeTop() { + Element[][] temp = deepCopy(elements); + int temp_points = points; + moved = false; + Element s = new Element(myActivity); - } - for (int i = 0; i < elements.length; i++) { - s.number = elements[0][i].number; - s.posX = 0; + for (int i = 0; i < elements.length; i++) { + s.number = elements[0][i].number; + s.posX = 0; + s.posY = i; + + + for (int j = 1; j < elements[i].length; j++) { + if (elements[j][i].number != 0 && (s.number == 0 || s.number == elements[j][i].number)) { + moved = true; + elements[j][i].setNumber(s.number + elements[j][i].number); + elements[s.posX][s.posY].setNumber(0); + switchElementPositions(elements[j][i], elements[s.posX][s.posY]); + Element z = elements[j][i]; + elements[j][i] = elements[s.posX][s.posY]; + elements[s.posX][s.posY] = z; + if (s.number != 0) + points += elements[s.posX][s.posY].number; + if (s.number != 0) + s.posX++; + j = s.posX; + s.number = elements[j][i].number; + + } else if (elements[j][i].number != 0) { + s.number = elements[j][i].number; + s.posX = j; s.posY = i; - - - for (int j = 1; j < elements[i].length; j++) { - if (elements[j][i].number != 0 && s.number == 0) { - moved = true; - elements[j][i].setNumber(s.number + elements[j][i].number); - elements[s.posX][s.posY].setNumber(0); - switchElementPositions(elements[j][i], elements[s.posX][s.posY]); - Element z = elements[j][i]; - elements[j][i] = elements[s.posX][s.posY]; - elements[s.posX][s.posY] = z; - if (s.number != 0) - s.posX++; - j = s.posX; - s.number = elements[j][i].number; - - } else if (s.number != 0) { - s.number = elements[j][i].number; - s.posX = j; - s.posY = i; - } - } - } - if (moved) { - gameStatistics.addMoves(1); - last_points = temp_points; - last_elements = temp; - undoButton.setVisibility(View.VISIBLE); - undo = true; + } + + } + for (int i = 0; i < elements.length; i++) { + s.number = elements[0][i].number; + s.posX = 0; + s.posY = i; + + + for (int j = 1; j < elements[i].length; j++) { + if (elements[j][i].number != 0 && s.number == 0) { + moved = true; + elements[j][i].setNumber(s.number + elements[j][i].number); + elements[s.posX][s.posY].setNumber(0); + switchElementPositions(elements[j][i], elements[s.posX][s.posY]); + Element z = elements[j][i]; + elements[j][i] = elements[s.posX][s.posY]; + elements[s.posX][s.posY] = z; + if (s.number != 0) + s.posX++; + j = s.posX; + s.number = elements[j][i].number; + + } else if (s.number != 0) { + s.number = elements[j][i].number; + s.posX = j; + s.posY = i; } - if (moved) - gameStatistics.moveT(); - addNumber(); - setDPositions(animationActivated); - updateGameState(); - //es wurde nach oben gewischt, hier den Code einfügen - return false; } - public boolean onSwipeRight() { - Element[][] temp = deepCopy(elements); - int temp_points = points; - moved = false; - Element s = new Element(myActivity); - for (int i = 0; i < elements.length; i++) { - s.number = elements[i][elements[i].length - 1].number; - s.posX = i; - s.posY = elements[i].length - 1; - - - for (int j = elements[i].length - 2; j >= 0; j--) { - if (elements[i][j].number != 0 && (s.number == 0 || s.number == elements[i][j].number)) { - moved = true; - - elements[i][j].setNumber(s.number + elements[i][j].number); - elements[s.posX][s.posY].setNumber(0); - switchElementPositions(elements[i][j], elements[s.posX][s.posY]); - Element z = elements[i][j]; - elements[i][j] = elements[s.posX][s.posY]; - elements[s.posX][s.posY] = z; - - if (s.number != 0) - points += elements[s.posX][s.posY].number; - if (s.number != 0) - s.posY--; - j = s.posY; - s.number = elements[i][j].number; - } else if (elements[i][j].number != 0) { - s.number = elements[i][j].number; - s.posX = i; - s.posY = j; - } - } + } + if (moved) { + gameStatistics.addMoves(1); + last_points = temp_points; + last_elements = temp; + undoButton.setVisibility(View.VISIBLE); + undo = true; + } + if (moved) + gameStatistics.moveT(); + addNumber(); + setDPositions(animationActivated); + updateGameState(); + //es wurde nach oben gewischt, hier den Code einfügen + return false; + } - } - for (int i = 0; i < elements.length; i++) { - s.number = elements[i][elements[i].length - 1].number; + private boolean handleSwipeRight() { + Element[][] temp = deepCopy(elements); + int temp_points = points; + moved = false; + Element s = new Element(myActivity); + for (int i = 0; i < elements.length; i++) { + s.number = elements[i][elements[i].length - 1].number; + s.posX = i; + s.posY = elements[i].length - 1; + + + for (int j = elements[i].length - 2; j >= 0; j--) { + if (elements[i][j].number != 0 && (s.number == 0 || s.number == elements[i][j].number)) { + moved = true; + + elements[i][j].setNumber(s.number + elements[i][j].number); + elements[s.posX][s.posY].setNumber(0); + switchElementPositions(elements[i][j], elements[s.posX][s.posY]); + Element z = elements[i][j]; + elements[i][j] = elements[s.posX][s.posY]; + elements[s.posX][s.posY] = z; + + if (s.number != 0) + points += elements[s.posX][s.posY].number; + if (s.number != 0) + s.posY--; + j = s.posY; + s.number = elements[i][j].number; + } else if (elements[i][j].number != 0) { + s.number = elements[i][j].number; s.posX = i; - s.posY = elements[i].length - 1; + s.posY = j; + } + } + } + for (int i = 0; i < elements.length; i++) { + s.number = elements[i][elements[i].length - 1].number; + s.posX = i; + s.posY = elements[i].length - 1; - for (int j = elements[i].length - 2; j >= 0; j--) { - if (elements[i][j].number != 0 && s.number == 0) { - moved = true; - elements[i][j].setNumber(s.number + elements[i][j].number); - elements[s.posX][s.posY].setNumber(0); - switchElementPositions(elements[i][j], elements[s.posX][s.posY]); - Element z = elements[i][j]; - elements[i][j] = elements[s.posX][s.posY]; - elements[s.posX][s.posY] = z; + for (int j = elements[i].length - 2; j >= 0; j--) { + if (elements[i][j].number != 0 && s.number == 0) { + moved = true; + elements[i][j].setNumber(s.number + elements[i][j].number); + elements[s.posX][s.posY].setNumber(0); + switchElementPositions(elements[i][j], elements[s.posX][s.posY]); + Element z = elements[i][j]; + elements[i][j] = elements[s.posX][s.posY]; + elements[s.posX][s.posY] = z; - if (s.number != 0) - s.posY--; - j = s.posY; - s.number = elements[i][j].number; - } else if (s.number != 0) { - s.number = elements[i][j].number; - s.posX = i; - s.posY = j; - } - } + if (s.number != 0) + s.posY--; + j = s.posY; + s.number = elements[i][j].number; + } else if (s.number != 0) { + s.number = elements[i][j].number; + s.posX = i; + s.posY = j; } - if (moved) { - gameStatistics.addMoves(1); - last_points = temp_points; - last_elements = temp; - undoButton.setVisibility(View.VISIBLE); - undo = true; - } - if (moved) - gameStatistics.moveR(); - addNumber(); - setDPositions(animationActivated); - updateGameState(); - - //es wurde nach rechts gewischt, hier den Code einfügen - return false; } - public boolean onSwipeLeft() { - Element[][] temp = deepCopy(elements); - int temp_points = points; - moved = false; - Element s = new Element(myActivity); - for (int i = 0; i < elements.length; i++) { - s.number = elements[i][0].number; - s.posX = i; - s.posY = 0; - - - for (int j = 1; j < elements[i].length; j++) { - if (elements[i][j].number != 0 && (s.number == 0 || s.number == elements[i][j].number)) { - moved = true; - - - elements[i][j].setNumber(s.number + elements[i][j].number); - elements[s.posX][s.posY].setNumber(0); - switchElementPositions(elements[i][j], elements[s.posX][s.posY]); - Element z = elements[i][j]; - elements[i][j] = elements[s.posX][s.posY]; - elements[s.posX][s.posY] = z; - - if (s.number != 0) - points += elements[s.posX][s.posY].number; - if (s.number != 0) - s.posY++; - j = s.posY; - s.number = elements[i][j].number; - } else if (elements[i][j].number != 0) { - s.number = elements[i][j].number; - s.posX = i; - s.posY = j; - } - } + } + if (moved) { + gameStatistics.addMoves(1); + last_points = temp_points; + last_elements = temp; + undoButton.setVisibility(View.VISIBLE); + undo = true; + } + if (moved) + gameStatistics.moveR(); + addNumber(); + setDPositions(animationActivated); + updateGameState(); - } - for (int i = 0; i < elements.length; i++) { - s.number = elements[i][0].number; - s.posX = i; - s.posY = 0; - - for (int j = 1; j < elements[i].length; j++) { - if (elements[i][j].number != 0 && s.number == 0) { - moved = true; - - elements[i][j].setNumber(s.number + elements[i][j].number); - elements[s.posX][s.posY].setNumber(0); - switchElementPositions(elements[i][j], elements[s.posX][s.posY]); - Element z = elements[i][j]; - elements[i][j] = elements[s.posX][s.posY]; - elements[s.posX][s.posY] = z; - - if (s.number != 0) - s.posY++; - j = s.posY; - s.number = elements[i][j].number; - } else if (s.number != 0) { - s.number = elements[i][j].number; - s.posX = i; - s.posY = j; - } - } + //es wurde nach rechts gewischt, hier den Code einfügen + return false; + } + private boolean handleSwipeLeft() { + Element[][] temp = deepCopy(elements); + int temp_points = points; + moved = false; + Element s = new Element(myActivity); + for (int i = 0; i < elements.length; i++) { + s.number = elements[i][0].number; + s.posX = i; + s.posY = 0; + + + for (int j = 1; j < elements[i].length; j++) { + if (elements[i][j].number != 0 && (s.number == 0 || s.number == elements[i][j].number)) { + moved = true; + + + elements[i][j].setNumber(s.number + elements[i][j].number); + elements[s.posX][s.posY].setNumber(0); + switchElementPositions(elements[i][j], elements[s.posX][s.posY]); + Element z = elements[i][j]; + elements[i][j] = elements[s.posX][s.posY]; + elements[s.posX][s.posY] = z; + + if (s.number != 0) + points += elements[s.posX][s.posY].number; + if (s.number != 0) + s.posY++; + j = s.posY; + s.number = elements[i][j].number; + } else if (elements[i][j].number != 0) { + s.number = elements[i][j].number; + s.posX = i; + s.posY = j; } - if (moved) { - gameStatistics.addMoves(1); - last_points = temp_points; - last_elements = temp; - undoButton.setVisibility(View.VISIBLE); - undo = true; + } + + } + for (int i = 0; i < elements.length; i++) { + s.number = elements[i][0].number; + s.posX = i; + s.posY = 0; + + for (int j = 1; j < elements[i].length; j++) { + if (elements[i][j].number != 0 && s.number == 0) { + moved = true; + + elements[i][j].setNumber(s.number + elements[i][j].number); + elements[s.posX][s.posY].setNumber(0); + switchElementPositions(elements[i][j], elements[s.posX][s.posY]); + Element z = elements[i][j]; + elements[i][j] = elements[s.posX][s.posY]; + elements[s.posX][s.posY] = z; + + if (s.number != 0) + s.posY++; + j = s.posY; + s.number = elements[i][j].number; + } else if (s.number != 0) { + s.number = elements[i][j].number; + s.posX = i; + s.posY = j; } - if (moved) - gameStatistics.moveL(); - addNumber(); - setDPositions(animationActivated); - updateGameState(); - //es wurde nach links gewischt, hier den Code einfügen - return false; } - public boolean onSwipeBottom() { - Element[][] temp = deepCopy(elements); - int temp_points = points; - moved = false; - Element s = new Element(myActivity); - for (int i = 0; i < elements.length; i++) { - s.number = elements[elements[i].length - 1][i].number; - s.posX = elements[i].length - 1; + } + if (moved) { + gameStatistics.addMoves(1); + last_points = temp_points; + last_elements = temp; + undoButton.setVisibility(View.VISIBLE); + undo = true; + } + if (moved) + gameStatistics.moveL(); + addNumber(); + setDPositions(animationActivated); + updateGameState(); + //es wurde nach links gewischt, hier den Code einfügen + return false; + } + + private boolean handleSwipeBottom() { + Element[][] temp = deepCopy(elements); + int temp_points = points; + moved = false; + Element s = new Element(myActivity); + for (int i = 0; i < elements.length; i++) { + s.number = elements[elements[i].length - 1][i].number; + s.posX = elements[i].length - 1; + s.posY = i; + + + for (int j = elements[i].length - 2; j >= 0; j--) { + if (elements[j][i].number != 0 && (s.number == 0 || s.number == elements[j][i].number)) { + moved = true; + + elements[j][i].setNumber(s.number + elements[j][i].number); + elements[s.posX][s.posY].setNumber(0); + switchElementPositions(elements[j][i], elements[s.posX][s.posY]); + Element z = elements[j][i]; + elements[j][i] = elements[s.posX][s.posY]; + elements[s.posX][s.posY] = z; + + if (s.number != 0) + points += elements[s.posX][s.posY].number; + if (s.number != 0) + s.posX--; + j = s.posX; + s.number = elements[j][i].number; + } else if (elements[j][i].number != 0) { + s.number = elements[j][i].number; + s.posX = j; s.posY = i; + } + } + } + for (int i = 0; i < elements.length; i++) { + s.number = elements[elements[i].length - 1][i].number; + s.posX = elements[i].length - 1; + s.posY = i; + + + for (int j = elements[i].length - 2; j >= 0; j--) { + if (elements[j][i].number != 0 && s.number == 0) { + moved = true; + + elements[j][i].setNumber(s.number + elements[j][i].number); + elements[s.posX][s.posY].setNumber(0); + switchElementPositions(elements[j][i], elements[s.posX][s.posY]); + Element z = elements[j][i]; + elements[j][i] = elements[s.posX][s.posY]; + elements[s.posX][s.posY] = z; + + if (s.number != 0) + s.posX--; + j = s.posX; + s.number = elements[j][i].number; + } else if (s.number != 0) { + s.number = elements[j][i].number; + s.posX = j; + s.posY = i; + } + } - for (int j = elements[i].length - 2; j >= 0; j--) { - if (elements[j][i].number != 0 && (s.number == 0 || s.number == elements[j][i].number)) { - moved = true; - - elements[j][i].setNumber(s.number + elements[j][i].number); - elements[s.posX][s.posY].setNumber(0); - switchElementPositions(elements[j][i], elements[s.posX][s.posY]); - Element z = elements[j][i]; - elements[j][i] = elements[s.posX][s.posY]; - elements[s.posX][s.posY] = z; - - if (s.number != 0) - points += elements[s.posX][s.posY].number; - if (s.number != 0) - s.posX--; - j = s.posX; - s.number = elements[j][i].number; - } else if (elements[j][i].number != 0) { - s.number = elements[j][i].number; - s.posX = j; - s.posY = i; - } - } + } + if (moved) { + gameStatistics.addMoves(1); + last_points = temp_points; + last_elements = temp; + undoButton.setVisibility(View.VISIBLE); + undo = true; + } + if (moved) + gameStatistics.moveD(); + addNumber(); + setDPositions(animationActivated); + updateGameState(); + //es wurde nach unten gewischt, hier den Code einfügen + return false; + } + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + switch (keyCode) { + case KeyEvent.KEYCODE_DPAD_UP: + handleSwipeTop(); + return true; + case KeyEvent.KEYCODE_DPAD_RIGHT: + handleSwipeRight(); + return true; + case KeyEvent.KEYCODE_DPAD_LEFT: + handleSwipeLeft(); + return true; + case KeyEvent.KEYCODE_DPAD_DOWN: + handleSwipeBottom(); + return true; + case KeyEvent.KEYCODE_Z: + if (event.isCtrlPressed()) { + handleUndo(); } - for (int i = 0; i < elements.length; i++) { - s.number = elements[elements[i].length - 1][i].number; - s.posX = elements[i].length - 1; - s.posY = i; + return true; + } + return super.onKeyDown(keyCode, event); + } + public void setListener() { + swipeListener = new Gestures(this) { + public boolean onSwipeTop() { + return handleSwipeTop(); + } - for (int j = elements[i].length - 2; j >= 0; j--) { - if (elements[j][i].number != 0 && s.number == 0) { - moved = true; - - elements[j][i].setNumber(s.number + elements[j][i].number); - elements[s.posX][s.posY].setNumber(0); - switchElementPositions(elements[j][i], elements[s.posX][s.posY]); - Element z = elements[j][i]; - elements[j][i] = elements[s.posX][s.posY]; - elements[s.posX][s.posY] = z; - - if (s.number != 0) - s.posX--; - j = s.posX; - s.number = elements[j][i].number; - } else if (s.number != 0) { - s.number = elements[j][i].number; - s.posX = j; - s.posY = i; - } - } + public boolean onSwipeRight() { + return handleSwipeRight(); + } - } - if (moved) { - gameStatistics.addMoves(1); - last_points = temp_points; - last_elements = temp; - undoButton.setVisibility(View.VISIBLE); - undo = true; - } - if (moved) - gameStatistics.moveD(); - addNumber(); - setDPositions(animationActivated); - updateGameState(); - //es wurde nach unten gewischt, hier den Code einfügen - return false; + public boolean onSwipeLeft() { + return handleSwipeLeft(); + } + + public boolean onSwipeBottom() { + return handleSwipeBottom(); } public boolean nichts() { diff --git a/app/src/main/java/org/secuso/privacyfriendly2048/activities/helper/BaseActivityWithoutNavBar.java b/app/src/main/java/org/secuso/privacyfriendly2048/activities/helper/BaseActivityWithoutNavBar.java index dfc144d..db23755 100644 --- a/app/src/main/java/org/secuso/privacyfriendly2048/activities/helper/BaseActivityWithoutNavBar.java +++ b/app/src/main/java/org/secuso/privacyfriendly2048/activities/helper/BaseActivityWithoutNavBar.java @@ -20,6 +20,7 @@ import android.os.Bundle; import android.os.Handler; +import android.view.KeyEvent; import android.view.View; import androidx.appcompat.app.AppCompatActivity; @@ -78,4 +79,9 @@ protected void onPostCreate(Bundle savedInstanceState) { } } + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + return super.onKeyDown(keyCode, event); + } + } diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 5a4a9e6..9c48cf1 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -105,4 +105,11 @@ Zeit pro Wischer Statistik zurücksetzem + + Streiche Oben + Streiche Unten + Streiche Links + Streiche Rechts + Rückgängig + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7fa3230..64f867f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -104,5 +104,11 @@ Time per Swipe Reset Statistics + + Swipe Up + Swipe Down + Swipe Left + Swipe Right + Undo diff --git a/app/src/main/res/xml/air_actions.xml b/app/src/main/res/xml/air_actions.xml new file mode 100644 index 0000000..f68526a --- /dev/null +++ b/app/src/main/res/xml/air_actions.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + +