From d180e7d724813402436f0f5e2af4621c8e8a52f8 Mon Sep 17 00:00:00 2001 From: Vahagn Khachatryan Date: Tue, 21 May 2013 22:17:16 +0400 Subject: [PATCH] Action bar added. --- res/animator/puzzle_action_bar_enter.xml | 27 ++++ res/animator/puzzle_action_bar_exit.xml | 27 ++++ res/layout/puzzle_view_title.xml | 49 +++++++ .../vahagn/sokoban/play/PlayActivity.java | 138 +++++++++--------- .../vahagn/sokoban/play/PuzzleControl.java | 6 + .../vahagn/sokoban/play/PuzzleView.java | 2 +- 6 files changed, 181 insertions(+), 68 deletions(-) create mode 100644 res/animator/puzzle_action_bar_enter.xml create mode 100644 res/animator/puzzle_action_bar_exit.xml create mode 100644 res/layout/puzzle_view_title.xml diff --git a/res/animator/puzzle_action_bar_enter.xml b/res/animator/puzzle_action_bar_enter.xml new file mode 100644 index 0000000..7c6e824 --- /dev/null +++ b/res/animator/puzzle_action_bar_enter.xml @@ -0,0 +1,27 @@ + + + + + diff --git a/res/animator/puzzle_action_bar_exit.xml b/res/animator/puzzle_action_bar_exit.xml new file mode 100644 index 0000000..953ad0c --- /dev/null +++ b/res/animator/puzzle_action_bar_exit.xml @@ -0,0 +1,27 @@ + + + + + diff --git a/res/layout/puzzle_view_title.xml b/res/layout/puzzle_view_title.xml new file mode 100644 index 0000000..912f3df --- /dev/null +++ b/res/layout/puzzle_view_title.xml @@ -0,0 +1,49 @@ + + + + + + + + diff --git a/src/org/dyndns/vahagn/sokoban/play/PlayActivity.java b/src/org/dyndns/vahagn/sokoban/play/PlayActivity.java index fb6447c..4987275 100644 --- a/src/org/dyndns/vahagn/sokoban/play/PlayActivity.java +++ b/src/org/dyndns/vahagn/sokoban/play/PlayActivity.java @@ -11,12 +11,22 @@ import android.view.ContextMenu.ContextMenuInfo; import android.view.Menu; import android.view.MenuItem; import android.view.View; +import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; +import android.view.animation.Animation; +import android.view.animation.AnimationUtils; +import android.widget.AbsListView; +import android.widget.FrameLayout; +import android.widget.TextView; +import java.util.Timer; +import java.util.TimerTask; +import java.util.zip.Inflater; import static org.dyndns.vahagn.sokoban.App.TAG; import static org.dyndns.vahagn.sokoban.App.theApp; +import org.dyndns.vahagn.sokoban.R; import org.dyndns.vahagn.sokoban.Puzzle; -import org.dyndns.vahagn.sokoban.MainMenu; +import org.dyndns.vahagn.sokoban.menu.MainActivity; public class PlayActivity extends Activity implements PuzzleControl.PuzzleControlLister @@ -25,38 +35,33 @@ public class PlayActivity extends Activity protected final static int UNDO_ITEM = 2; public Puzzle puzzle = null; - public PuzzleControl view; - public View title; - - @Override + public PuzzleControl puzzle_view; + View title_view; + public TextView title_text; + + @Override public void onCreate(Bundle savedInstanceState) { Log.d(TAG, "onCreate: " + savedInstanceState ); super.onCreate(savedInstanceState); - + // + // No title. + // requestWindowFeature(Window.FEATURE_NO_TITLE); - // getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - // WindowManager.LayoutParams.FLAG_FULLSCREEN); - //requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); - //requestWindowFeature(Window.FEATURE_OPTIONS_PANEL); - //getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, RESULT_OK); - //getWindow().getWindowStyle(). - //getWindow(). - - - - - - - - // - // Create and set up the view. + // Create and set up the puzzle_view. // - view = new PuzzleControl( this ); - setContentView( view ); - view.setPuzzleControlLister( this ); - registerForContextMenu(view); + puzzle_view = new PuzzleControl( this ); + setContentView( puzzle_view ); + puzzle_view.setPuzzleControlLister( this ); + registerForContextMenu(puzzle_view); + // + // Create action bar. + // + FrameLayout rootLayout = (FrameLayout)findViewById(android.R.id.content); + View.inflate(this, R.layout.puzzle_view_title, rootLayout); + title_text = (TextView)findViewById(R.id.title_text); + title_view = findViewById(R.id.puzzle_view_title_layout); // // Load the puzzle. // @@ -64,8 +69,6 @@ public class PlayActivity extends Activity if (savedInstanceState == null) { - - // We were just launched -- set up a new game // mSnakeView.setMode(SnakeView.READY); } @@ -110,8 +113,7 @@ public class PlayActivity extends Activity { public void onClick(DialogInterface d, int w) { - Intent intent = new Intent(PlayActivity.this, MainMenu.class); - startActivity( intent ); + finish(); } }); builder.setPositiveButton("Next", @@ -120,7 +122,7 @@ public class PlayActivity extends Activity public void onClick(DialogInterface d, int w) { loadCurrentPuzzle(); - view.invalidate(); + puzzle_view.invalidate(); } }); AlertDialog dlg = builder.create(); @@ -129,62 +131,64 @@ public class PlayActivity extends Activity public void onLongPress() { - openContextMenu(view); + if ( title_view.getVisibility() != View.VISIBLE ) + showTitle(); } - public boolean onReset() + public void onTouch() { - loadCurrentPuzzle(); - view.invalidate(); + 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; } - public boolean onUndo() + public boolean onUndo( View v ) { if ( puzzle.isUndoable() ) { puzzle.restore(); - view.invalidate(); + puzzle_view.invalidate(); return true; } else return false; } - @Override - public void onCreateContextMenu(ContextMenu menu, View v, - ContextMenuInfo menuInfo) - { - super.onCreateContextMenu(menu, v, menuInfo); - // - // Undo item. - // - if ( puzzle.isUndoable() ) - { - menu.add("Undo").setOnMenuItemClickListener( - new MenuItem.OnMenuItemClickListener(){ - public boolean onMenuItemClick(MenuItem mi){ - return onUndo(); - } - }); - } - // - // Reset item. - // - menu.add("Reset").setOnMenuItemClickListener( - new MenuItem.OnMenuItemClickListener(){ - public boolean onMenuItemClick(MenuItem mi){ - return onReset(); - } - }); - } - private void loadCurrentPuzzle() { puzzle = theApp().getCurrentPuzzle(); - view.setPuzzle(puzzle); + puzzle_view.setPuzzle(puzzle); - String title = "Sokoban: level " + new Integer(theApp().getCurrentLevel()).toString(); + String title = "Level " + new Integer(theApp().getCurrentLevel()).toString(); setTitle(title); } + + public void setTitle(CharSequence title) + { + super.setTitle(title); + if (title_text != null) { + title_text.setText(title); + } + } } diff --git a/src/org/dyndns/vahagn/sokoban/play/PuzzleControl.java b/src/org/dyndns/vahagn/sokoban/play/PuzzleControl.java index 0fa042f..d2e9601 100644 --- a/src/org/dyndns/vahagn/sokoban/play/PuzzleControl.java +++ b/src/org/dyndns/vahagn/sokoban/play/PuzzleControl.java @@ -41,6 +41,7 @@ public class PuzzleControl extends PuzzleView { public void onSolved(); public void onLongPress(); + public void onTouch(); } public PuzzleControl(Context c) @@ -135,6 +136,11 @@ public class PuzzleControl extends PuzzleView animator.play(); } // + // Notify that we were touched. + // + if ( lister != null ) + lister.onTouch(); + // // This event is processed. // return true; diff --git a/src/org/dyndns/vahagn/sokoban/play/PuzzleView.java b/src/org/dyndns/vahagn/sokoban/play/PuzzleView.java index baf3d50..7dbffce 100644 --- a/src/org/dyndns/vahagn/sokoban/play/PuzzleView.java +++ b/src/org/dyndns/vahagn/sokoban/play/PuzzleView.java @@ -20,7 +20,7 @@ import org.dyndns.vahagn.sokoban.R; /** */ -public class PuzzleView extends View +public class PuzzleView extends View { protected Puzzle puzzle;