Undo functionality restored.

This commit is contained in:
2021-10-17 21:59:01 +01:00
parent 0a4f131e2b
commit faa6ccc296
8 changed files with 21 additions and 243 deletions

View File

@@ -78,10 +78,7 @@ public class PuzzleBoardFragment extends Fragment
PuzzleBoardFragment.this.initAndShowCurrentPuzzle(); PuzzleBoardFragment.this.initAndShowCurrentPuzzle();
}); });
binding.btnUndo.setOnClickListener((View.OnClickListener) v -> { binding.btnUndo.setOnClickListener((View.OnClickListener) v -> {
createNextLevelDialog((d, w) -> { binding.gameBoard.undoLastMove();
gameState.advanceCurrentLevel();
initAndShowCurrentPuzzle();
}).show();
}); });
} }
@@ -99,17 +96,6 @@ public class PuzzleBoardFragment extends Fragment
// //
// Bring a dialog for user to choose to continue or stop. // Bring a dialog for user to choose to continue or stop.
// //
// DialogFragment df = new DialogFragment() {
// public Dialog onCreateDialog(Bundle savedInstanceState) {
// return createNextLevelDialog(new DialogInterface.OnClickListener(){
// public void onClick(DialogInterface d, int w){
// gameState.advanceCurrentLevel();
// initAndShowCurrentPuzzle();
// }
// });
// }
// };
// df.show(getChildFragmentManager(), null);
createNextLevelDialog((d, w) -> { createNextLevelDialog((d, w) -> {
gameState.advanceCurrentLevel(); gameState.advanceCurrentLevel();
initAndShowCurrentPuzzle(); initAndShowCurrentPuzzle();

View File

@@ -1,211 +0,0 @@
package org.vostan.banvor.board;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentActivity;
import android.util.AttributeSet;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.TextView;
import org.vostan.banvor.App;
import static org.vostan.banvor.App.theApp;
import org.vostan.banvor.R;
import org.vostan.banvor.model.Puzzle;
public class PlayActivity extends FragmentActivity
implements PuzzleControl.PuzzleControlLister
{
protected final static int RESET_ITEM = 1;
protected final static int UNDO_ITEM = 2;
public Puzzle puzzle = null;
public PuzzleControl puzzle_view;
View title_view;
public TextView title_text;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//
// No title.
//
requestWindowFeature(Window.FEATURE_NO_TITLE);
//
// Create and set up the puzzle_view.
//
// puzzle_view = new PuzzleControl(this, new AttributeSet() {
// });
setContentView( puzzle_view );
puzzle_view.setPuzzleControlLister( this );
//
// Create action bar.
//
FrameLayout rootLayout = (FrameLayout)findViewById(android.R.id.content);
View.inflate(this, R.layout.fragment_puzzle_board, rootLayout);
title_text = (TextView)findViewById(R.id.level_text);
// title_view = findViewById(R.id.puzzle_view_title_layout);
//
// Load the puzzle.
//
loadCurrentPuzzle();
}
public void onSolved()
{
//
// Advance current level and achieved level.
//
final int nextl = theApp().state().getCurrentLevel()+1;
// theApp().state().(nextl);
theApp().state().setCurrentLevel(nextl);
//
// Bring a dialog for user to choose to continue or stop.
//
DialogFragment df = new DialogFragment()
{
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Congratulations! You Won!");
builder.setMessage( "Would you like to try the next puzzle?" );
builder.setNegativeButton("Enough",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface d, int w)
{
finish();
}
});
builder.setPositiveButton("Please",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface d, int w)
{
loadCurrentPuzzle();
puzzle_view.invalidate();
}
});
// Create the AlertDialog object and return it
Dialog dlg = builder.create();
dlg.setCancelable(false);
dlg.setCanceledOnTouchOutside(false);
return dlg;
}
};
df.setCancelable(false);
df.show(getSupportFragmentManager(), null);
//dlg.show();
}
public void onLongPress()
{
if ( title_view.getVisibility() != View.VISIBLE )
showTitle();
}
public void onTouch()
{
if ( title_view.getVisibility() == View.VISIBLE )
hideTitle();
}
public void showTitle()
{
Animation anim = AnimationUtils.loadAnimation(this, R.animator.puzzle_action_bar_enter);
title_view.startAnimation(anim);
title_view.setVisibility(View.VISIBLE);
}
public void hideTitle()
{
Animation anim = AnimationUtils.loadAnimation(this, R.animator.puzzle_action_bar_exit);
title_view.startAnimation(anim);
title_view.setVisibility(View.INVISIBLE);
}
public boolean onReset( View v )
{
while ( puzzle.isUndoable() )
puzzle.restore();
puzzle_view.invalidate();
return true;
}
//
// Go to previouse puzzle.
//
public boolean onPrev( View v )
{
//
// If current level is less than achived level the move to next
// puzzle.
//
if ( theApp().state().getCurrentLevel() > App.MIN_LEVEL )
{
theApp().state().setCurrentLevel( theApp().state().getCurrentLevel()-1 );
loadCurrentPuzzle();
puzzle_view.invalidate();
}
return true;
}
//
// Go to next puzzle.
//
public boolean onNext( View v )
{
//
// If current level is less than achived level the move to next
// puzzle.
//
if ( theApp().state().getCurrentLevel() < theApp().state().getHighestSolvedLevel() )
{
theApp().state().setCurrentLevel( theApp().state().getCurrentLevel()+1 );
loadCurrentPuzzle();
puzzle_view.invalidate();
}
return true;
}
//
// Undo last action.
//
public boolean onUndo( View v )
{
if ( puzzle.isUndoable() )
{
puzzle.restore();
puzzle_view.invalidate();
return true;
}
else
return false;
}
private void loadCurrentPuzzle()
{
puzzle = theApp().state().getCurrentPuzzle();
puzzle_view.setPuzzle(puzzle);
updateTitle();
}
public void updateTitle()
{
String title = "Level " + new Integer(theApp().state().getCurrentLevel()).toString();
setTitle(title);
}
public void setTitle(CharSequence title)
{
super.setTitle(title);
if (title_text != null) {
title_text.setText(title);
}
}
}

View File

@@ -5,7 +5,6 @@
package org.vostan.banvor.board; package org.vostan.banvor.board;
import android.content.Context; import android.content.Context;
import android.graphics.Point;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.GestureDetector; import android.view.GestureDetector;
import android.view.MotionEvent; import android.view.MotionEvent;
@@ -29,7 +28,7 @@ public class PuzzleControl extends PuzzleView
{ {
protected GestureDetector simpled; protected GestureDetector simpled;
protected ScaleGestureDetector scaled; protected ScaleGestureDetector scaled;
protected PuzzleChoreographer logic; protected PuzzleChoreographer choregrapher;
protected Animator animator; protected Animator animator;
protected PuzzleAnimator puzzleAnimator; protected PuzzleAnimator puzzleAnimator;
protected PuzzleControlLister lister; protected PuzzleControlLister lister;
@@ -47,7 +46,7 @@ public class PuzzleControl extends PuzzleView
public PuzzleControl(Context c, AttributeSet attributeSet) public PuzzleControl(Context c, AttributeSet attributeSet)
{ {
super(c,attributeSet); super(c,attributeSet);
logic = new PuzzleChoreographer(); choregrapher = new PuzzleChoreographer();
animator = new Animator( this ); animator = new Animator( this );
animator.setAnimationLister(this); animator.setAnimationLister(this);
puzzleAnimator = new PuzzleAnimator(animator); puzzleAnimator = new PuzzleAnimator(animator);
@@ -60,14 +59,21 @@ public class PuzzleControl extends PuzzleView
public void setPuzzle( Puzzle p ) public void setPuzzle( Puzzle p )
{ {
super.setPuzzle(p); super.setPuzzle(p);
logic.setPuzzle(p); choregrapher.setPuzzle(p);
} }
public void setPuzzleControlLister( PuzzleControlLister l ) public void setPuzzleControlLister( PuzzleControlLister l )
{ {
lister = l; lister = l;
} }
public void undoLastMove(){
if ( getPuzzle().isUndoable() ){
getPuzzle().restore();
invalidate();
}
}
@Override @Override
public boolean onTouchEvent(MotionEvent event) public boolean onTouchEvent(MotionEvent event)
{ {
@@ -125,7 +131,7 @@ public class PuzzleControl extends PuzzleView
// //
// Create sequence of steps and then animate it. // Create sequence of steps and then animate it.
// //
if ( logic.createSteps(puzzleAnimator, singleTapTile)) if ( choregrapher.createSteps(puzzleAnimator, singleTapTile))
{ {
puzzle.save(); puzzle.save();
animator.play(); animator.play();

View File

@@ -0,0 +1,8 @@
package org.vostan.banvor.game;
import org.vostan.banvor.model.Puzzle;
public interface IPuzzleSource {
public int getCount();
public Puzzle getPuzzle(int i );
}

View File

@@ -4,10 +4,7 @@ import java.io.InputStream;
import static org.vostan.banvor.App.theApp; import static org.vostan.banvor.App.theApp;
import org.vostan.banvor.game.PuzzleBinLoader;
import org.vostan.banvor.R; import org.vostan.banvor.R;
import org.vostan.banvor.model.IPuzzleSource;
import org.vostan.banvor.model.Puzzle; import org.vostan.banvor.model.Puzzle;
public class PuzzleContainer implements IPuzzleSource public class PuzzleContainer implements IPuzzleSource

View File

@@ -7,7 +7,6 @@ import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import org.vostan.banvor.model.IPuzzleSource;
import org.vostan.banvor.model.Puzzle; import org.vostan.banvor.model.Puzzle;
public class State { public class State {

View File

@@ -1,6 +0,0 @@
package org.vostan.banvor.model;
public interface IPuzzleSource {
public int getCount();
public Puzzle getPuzzle( int i );
}

View File

@@ -109,7 +109,6 @@
android:background="@color/transparent" android:background="@color/transparent"
android:scaleType="fitCenter" android:scaleType="fitCenter"
android:src="@drawable/undo" android:src="@drawable/undo"
android:visibility="gone"
tools:visibility="visible" /> tools:visibility="visible" />
</LinearLayout> </LinearLayout>