diff --git a/src/org/dyndns/vahagn/sokoban/App.java b/src/org/dyndns/vahagn/sokoban/App.java index 8963272..48d5d47 100644 --- a/src/org/dyndns/vahagn/sokoban/App.java +++ b/src/org/dyndns/vahagn/sokoban/App.java @@ -10,7 +10,7 @@ public class App extends Application { public static final String TAG = "Sokoban"; public static final String PREFS = "org.dyndns.vahagn.sokoban.prefs"; - static final int MIN_LEVEL = 1; + public static final int MIN_LEVEL = 1; private static App mApp = null; protected PuzzleContainer pc; @@ -20,8 +20,8 @@ public class App extends Application protected SharedPreferences prefs; protected SharedPreferences.Editor prefsEdit; - /* - * + /* + * */ @Override public void onCreate() @@ -33,7 +33,7 @@ public class App extends Application prefsEdit = prefs.edit(); max_level = pc.getCount(); current_level = prefs.getInt("current_level", MIN_LEVEL); - achieved_level = prefs.getInt("achieved_level", MIN_LEVEL); + achieved_level = prefs.getInt("achieved_level", MIN_LEVEL+10); } // // This is a singleton object. @@ -53,6 +53,20 @@ public class App extends Application { return getPuzzle( getCurrentLevel() ); } + public Puzzle getPrevPuzzle() + { + if ( current_level != MIN_LEVEL ) + return getPuzzle( getCurrentLevel()-1 ); + else + return null; + } + public Puzzle getNextPuzzle() + { + if ( current_level != achieved_level ) + return getPuzzle( getCurrentLevel()+1 ); + else + return null; + } // // Provide amount of puzzles. // @@ -75,11 +89,15 @@ public class App extends Application return current_level; } // - // Set the current puzzle level. It deons't alter + // Set the current puzzle level. // public void setCurrentLevel( int l ) { - if ( current_level != l ) + if ( l > achieved_level ) + l = MIN_LEVEL; + if ( l < MIN_LEVEL ) + l = achieved_level; + if ( l != current_level ) { current_level = l; prefsEdit.putInt("current_level", current_level); @@ -89,22 +107,19 @@ public class App extends Application } } // - // Advances current level. It also reviews achived level if the - // new level is bigger than achieved level. + // Set achieved level. // - public void advanceCurrentLevel() + public void advanceAchivedLevel( int l ) { - int new_level = current_level+1; - if ( new_level == max_level ) - new_level = MIN_LEVEL; - if ( new_level > achieved_level ) + if ( l > max_level ) + l = max_level; + if ( l > achieved_level ) { - achieved_level = new_level; + achieved_level = l; prefsEdit.putInt("achieved_level", achieved_level); - // Don't call apply() here since the call below - // to setCurrentLevel() will do that. + prefsEdit.apply(); + if ( !prefs.edit().commit() ) + Log.d(TAG, "prefs.edit().commit() failed."); } - setCurrentLevel(new_level); } - } diff --git a/src/org/dyndns/vahagn/sokoban/play/PlayActivity.java b/src/org/dyndns/vahagn/sokoban/play/PlayActivity.java index fa44901..e4560ed 100644 --- a/src/org/dyndns/vahagn/sokoban/play/PlayActivity.java +++ b/src/org/dyndns/vahagn/sokoban/play/PlayActivity.java @@ -2,9 +2,12 @@ package org.dyndns.vahagn.sokoban.play; import android.app.Activity; import android.app.AlertDialog; +import android.app.Dialog; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; +import android.support.v4.app.DialogFragment; +import android.support.v4.app.FragmentActivity; import android.util.Log; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; @@ -29,7 +32,7 @@ import org.dyndns.vahagn.sokoban.R; import org.dyndns.vahagn.sokoban.Puzzle; import org.dyndns.vahagn.sokoban.menu.MainActivity; -public class PlayActivity extends Activity +public class PlayActivity extends FragmentActivity implements PuzzleControl.PuzzleControlLister { protected final static int RESET_ITEM = 1; @@ -55,7 +58,6 @@ public class PlayActivity extends Activity puzzle_view = new PuzzleControl( this ); setContentView( puzzle_view ); puzzle_view.setPuzzleControlLister( this ); - registerForContextMenu(puzzle_view); // // Create action bar. // @@ -67,34 +69,6 @@ public class PlayActivity extends Activity // Load the puzzle. // loadCurrentPuzzle(); - - if (savedInstanceState == null) - { - // We were just launched -- set up a new game -// mSnakeView.setMode(SnakeView.READY); - } - else - { - // We are being restored -// Bundle map = savedInstanceState.getBundle(ICICLE_KEY); -// if (map != null) { -// mSnakeView.restoreState(map); -// } else { -// mSnakeView.setMode(SnakeView.PAUSE); -// } - } -// -// -// -// super.onCreate(savedInstanceState); -// setContentView(R.layout.menu); -// -// try { -// theApp().getPuzzleContainer().getPuzzle( 0 ); -// } catch (Exception e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } } public void onSolved() @@ -102,32 +76,46 @@ public class PlayActivity extends Activity // // Advance current level and achieved level. // - theApp().advanceCurrentLevel(); + final int nextl = theApp().getCurrentLevel()+1; + theApp().advanceAchivedLevel(nextl); + theApp().setCurrentLevel(nextl); // // Bring a dialog for user to choose to continue or stop. // - AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setTitle(""); - builder.setMessage( "Congratulations! You Won!" ); - builder.setNegativeButton("Menu", - new DialogInterface.OnClickListener() - { - public void onClick(DialogInterface d, int w) - { - finish(); - } - }); - builder.setPositiveButton("Next", - new DialogInterface.OnClickListener() - { - public void onClick(DialogInterface d, int w) - { - loadCurrentPuzzle(); - puzzle_view.invalidate(); - } - }); - AlertDialog dlg = builder.create(); - dlg.show(); + 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() @@ -163,7 +151,9 @@ public class PlayActivity extends Activity puzzle_view.invalidate(); return true; } - + // + // Go to previouse puzzle. + // public boolean onPrev( View v ) { // @@ -178,7 +168,9 @@ public class PlayActivity extends Activity } return true; } - + // + // Go to next puzzle. + // public boolean onNext( View v ) { // @@ -190,10 +182,13 @@ public class PlayActivity extends Activity theApp().setCurrentLevel( theApp().getCurrentLevel()+1 ); loadCurrentPuzzle(); puzzle_view.invalidate(); + } return true; } - + // + // Undo last action. + // public boolean onUndo( View v ) { if ( puzzle.isUndoable() ) @@ -210,11 +205,13 @@ public class PlayActivity extends Activity { puzzle = theApp().getCurrentPuzzle(); puzzle_view.setPuzzle(puzzle); - + updateTitle(); + } + public void updateTitle() + { String title = "Level " + new Integer(theApp().getCurrentLevel()).toString(); setTitle(title); } - public void setTitle(CharSequence title) { super.setTitle(title);