Two new fragments added to the main activity. One of which is the puzzle list on view grid.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package org.dyndns.vahagn.sokoban;
|
||||
package org.dyndns.vahagn.sokoban.menu;
|
||||
|
||||
import org.dyndns.vahagn.sokoban.play.PlayActivity;
|
||||
import android.app.Activity;
|
||||
@@ -12,6 +12,10 @@ import android.graphics.Paint;
|
||||
import android.graphics.Rect;
|
||||
import android.view.View;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v4.app.FragmentTransaction;
|
||||
import android.util.Log;
|
||||
import android.util.TypedValue;
|
||||
import android.view.Display;
|
||||
@@ -22,13 +26,17 @@ import android.widget.AdapterView.OnItemClickListener;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.widget.GridView;
|
||||
import android.widget.ImageView;
|
||||
import org.dyndns.vahagn.sokoban.R;
|
||||
import static org.dyndns.vahagn.sokoban.App.theApp;
|
||||
|
||||
public class MainMenu extends Activity
|
||||
public class MainActivity extends FragmentActivity
|
||||
{
|
||||
protected final String TAG = "SokobanMenu";
|
||||
protected final String LEVEL= "org.dyndns.vahagn.sokoban.LEVEL";
|
||||
protected GridView puzzle_grid;
|
||||
protected MainFragment mainFragment;
|
||||
protected PuzzleListFragment puzzleListFragment;
|
||||
protected float puzzleListXScroll;
|
||||
protected float puzzleListXScrollMax;
|
||||
|
||||
/** Called when the activity is first created. */
|
||||
@Override
|
||||
@@ -37,147 +45,33 @@ public class MainMenu extends Activity
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.menu);
|
||||
|
||||
puzzle_grid = (GridView)findViewById(R.id.puzzle_grid);
|
||||
puzzle_grid.setAdapter( new PuzzlesAdapter(this) );
|
||||
//puzzle_grid.addView(puzzle_grid);
|
||||
puzzle_grid.setOnItemClickListener(new OnItemClickListener() {
|
||||
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
|
||||
{
|
||||
onPuzzleClicked(v,(int)id);
|
||||
setContentView(R.layout.main_activity);
|
||||
|
||||
mainFragment = new MainFragment();
|
||||
puzzleListFragment = new PuzzleListFragment();
|
||||
puzzleListFragment.setOnPuzzleListAction(new PuzzleListFragment.OnPuzzleListAction() {
|
||||
public boolean onPuzzleGridDown() {
|
||||
return onPuzzleGridDown1();
|
||||
}
|
||||
public boolean onPuzzleGridXScroll(float x) {
|
||||
return onPuzzleGridXScroll1(x);
|
||||
}
|
||||
public void onPuzzleLevelClicked(int level) {
|
||||
onPuzzleClicked1(level);
|
||||
}
|
||||
});
|
||||
puzzleListXScrollMax = getWindowManager().getDefaultDisplay().getWidth();
|
||||
|
||||
//
|
||||
// Set initila fragment.
|
||||
//
|
||||
FragmentManager fragmentManager = getSupportFragmentManager();
|
||||
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
|
||||
fragmentTransaction.add(R.id.fragment_container, mainFragment);
|
||||
// fragmentTransaction.add(R.id.fragment_container, puzzleListFragment);
|
||||
fragmentTransaction.commit();
|
||||
}
|
||||
|
||||
public class PuzzlesAdapter extends BaseAdapter
|
||||
{
|
||||
private Context mContext;
|
||||
private int icon_size;
|
||||
private int text_x;
|
||||
private int text_y;
|
||||
private Bitmap lock_icon;
|
||||
private Bitmap unlock_icon;
|
||||
private Paint paint;
|
||||
|
||||
public PuzzlesAdapter(Context c)
|
||||
{
|
||||
mContext = c;
|
||||
icon_size = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 10, getResources().getDisplayMetrics() );
|
||||
int text_size = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 4, getResources().getDisplayMetrics() );
|
||||
text_x = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 9, getResources().getDisplayMetrics() );
|
||||
text_y = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 9, getResources().getDisplayMetrics() );
|
||||
|
||||
Bitmap lock_icon_tmp = BitmapFactory.decodeResource( getResources(), R.drawable.lock );
|
||||
lock_icon = Bitmap.createBitmap(icon_size,icon_size,Bitmap.Config.ARGB_8888);
|
||||
Canvas lock_canvas = new Canvas(lock_icon);
|
||||
lock_canvas.drawBitmap(lock_icon_tmp,
|
||||
new Rect(0,0,lock_icon_tmp.getWidth()-1,lock_icon_tmp.getHeight()-1),
|
||||
new Rect(0,0,icon_size, icon_size),
|
||||
null);
|
||||
|
||||
Bitmap unlock_icon_tmp = BitmapFactory.decodeResource( getResources(), R.drawable.unlock );
|
||||
unlock_icon = Bitmap.createBitmap(icon_size,icon_size,Bitmap.Config.ARGB_8888);
|
||||
Canvas unlock_canvas = new Canvas(unlock_icon);
|
||||
unlock_canvas.drawBitmap(unlock_icon_tmp,
|
||||
new Rect(0,0,unlock_icon_tmp.getWidth()-1,unlock_icon_tmp.getHeight()-1),
|
||||
new Rect(0,0,icon_size, icon_size),
|
||||
null);
|
||||
|
||||
paint = new Paint();
|
||||
paint.setColor( Color.WHITE );
|
||||
paint.setTextSize( text_size );
|
||||
paint.setTextAlign(Paint.Align.RIGHT);
|
||||
}
|
||||
|
||||
public int getCount()
|
||||
{
|
||||
return theApp().getPuzzleCount();
|
||||
}
|
||||
|
||||
public Object getItem(int position)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public long getItemId(int position)
|
||||
{
|
||||
return position+1;
|
||||
}
|
||||
|
||||
// create a new ImageView for each item referenced by the Adapter
|
||||
public View getView(int position, View convertView, ViewGroup parent)
|
||||
{
|
||||
ImageView imageView;
|
||||
if (convertView == null)
|
||||
{ // if it's not recycled, initialize some attributes
|
||||
imageView = new ImageView(mContext);
|
||||
imageView.setLayoutParams(new GridView.LayoutParams(icon_size, icon_size));
|
||||
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
|
||||
// imageView.setPadding(8, 8, 8, 8);
|
||||
}
|
||||
else
|
||||
imageView = (ImageView) convertView;
|
||||
|
||||
int id = (int)getItemId(position);
|
||||
Bitmap icon = ( id <= theApp().getAchivedLevel() )
|
||||
? unlock_icon.copy(Bitmap.Config.ARGB_8888,true)
|
||||
: lock_icon.copy(Bitmap.Config.ARGB_8888,true);
|
||||
Canvas canvas = new Canvas(icon);
|
||||
canvas.drawText( Integer.toString(id), text_x, text_y, paint);
|
||||
imageView.setImageBitmap(icon);
|
||||
return imageView;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart()
|
||||
{
|
||||
Log.d(TAG, "onStart: " );
|
||||
|
||||
super.onStart();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRestart()
|
||||
{
|
||||
Log.d(TAG, "onRestart: " );
|
||||
|
||||
super.onRestart();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume()
|
||||
{
|
||||
Log.d(TAG, "onResume: " );
|
||||
|
||||
super.onResume();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause()
|
||||
{
|
||||
Log.d(TAG, "onPause: " );
|
||||
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop()
|
||||
{
|
||||
Log.d(TAG, "onStop: " );
|
||||
|
||||
super.onStop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy()
|
||||
{
|
||||
Log.d(TAG, "onDestroy: " );
|
||||
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
|
||||
//@Override
|
||||
public void onConfigurationChanged()
|
||||
{
|
||||
@@ -190,13 +84,52 @@ public class MainMenu extends Activity
|
||||
Intent intent = new Intent(this, PlayActivity.class);
|
||||
startActivity( intent );
|
||||
}
|
||||
|
||||
public void onPuzzleClicked(View v, int level )
|
||||
|
||||
public void onPuzzleListShow(View v)
|
||||
{
|
||||
getSupportFragmentManager()
|
||||
.beginTransaction()
|
||||
.setCustomAnimations(R.animator.fragment_slide_left_enter,
|
||||
R.animator.fragment_slide_left_exit)
|
||||
.replace(R.id.fragment_container, puzzleListFragment)
|
||||
.commit();
|
||||
}
|
||||
|
||||
public boolean onPuzzleGridDown1()
|
||||
{
|
||||
puzzleListXScroll = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean onPuzzleGridXScroll1( float x )
|
||||
{
|
||||
puzzleListXScroll += x;
|
||||
Log.d(TAG, Float.toString(puzzleListXScroll));
|
||||
/*
|
||||
getSupportFragmentManager()
|
||||
.beginTransaction()
|
||||
.setCustomAnimations(R.animator.fragment_slide_right_enter,
|
||||
R.animator.fragment_slide_right_exit)
|
||||
.commit();
|
||||
*/
|
||||
if ( -puzzleListXScroll/puzzleListXScrollMax > 0.5 )
|
||||
{
|
||||
Log.d(TAG, "annim");
|
||||
getSupportFragmentManager()
|
||||
.beginTransaction()
|
||||
.setCustomAnimations(R.animator.fragment_slide_right_enter,
|
||||
R.animator.fragment_slide_right_exit)
|
||||
.replace(R.id.fragment_container, mainFragment)
|
||||
.commit();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void onPuzzleClicked1( int level )
|
||||
{
|
||||
if ( level <= theApp().getAchivedLevel() )
|
||||
{
|
||||
theApp().setCurrentLevel(level);
|
||||
|
||||
Intent intent = new Intent(this, PlayActivity.class);
|
||||
startActivity( intent );
|
||||
}
|
||||
|
||||
39
src/org/dyndns/vahagn/sokoban/menu/MainFragment.java
Normal file
39
src/org/dyndns/vahagn/sokoban/menu/MainFragment.java
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.dyndns.vahagn.sokoban.menu;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
import static org.dyndns.vahagn.sokoban.App.theApp;
|
||||
import org.dyndns.vahagn.sokoban.R;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author vahagnk
|
||||
*/
|
||||
public class MainFragment extends Fragment
|
||||
{
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle icicle)
|
||||
{
|
||||
super.onCreate(icicle);
|
||||
View v = inflater.inflate(R.layout.main_fragment, container, false);
|
||||
if ( theApp().getAchivedLevel() == 1 )
|
||||
((Button)v.findViewById(R.id.btn_start)).setText(R.string.btn_start_begin);
|
||||
//
|
||||
// Set Footer.
|
||||
//
|
||||
String str = getString(R.string.copyright) + "\n"
|
||||
+getString(R.string.version_tag) + " " + getString(R.string.git_version);
|
||||
((TextView)v.findViewById(R.id.footer_txt)).setText(str);
|
||||
|
||||
return v;
|
||||
}
|
||||
}
|
||||
187
src/org/dyndns/vahagn/sokoban/menu/PuzzleListFragment.java
Normal file
187
src/org/dyndns/vahagn/sokoban/menu/PuzzleListFragment.java
Normal file
@@ -0,0 +1,187 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.dyndns.vahagn.sokoban.menu;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Rect;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.util.TypedValue;
|
||||
import android.view.GestureDetector;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.widget.Button;
|
||||
import android.widget.GridView;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
import org.dyndns.vahagn.sokoban.R;
|
||||
import org.dyndns.vahagn.sokoban.R;
|
||||
import static org.dyndns.vahagn.sokoban.App.theApp;
|
||||
import org.dyndns.vahagn.sokoban.play.PlayActivity;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author vahagnk
|
||||
*/
|
||||
public class PuzzleListFragment extends Fragment
|
||||
{
|
||||
protected GridView puzzle_grid;
|
||||
GestureDetector gridViewXScrollDetector;
|
||||
|
||||
OnPuzzleListAction lister;
|
||||
|
||||
public interface OnPuzzleListAction
|
||||
{
|
||||
public boolean onPuzzleGridDown();
|
||||
public boolean onPuzzleGridXScroll( float x );
|
||||
public void onPuzzleLevelClicked( int level );
|
||||
}
|
||||
|
||||
public void setOnPuzzleListAction( OnPuzzleListAction l )
|
||||
{
|
||||
lister = l;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle icicle)
|
||||
{
|
||||
super.onCreate(icicle);
|
||||
View v = inflater.inflate(R.layout.puzzle_list_fragment, container, false);
|
||||
//
|
||||
// Configure GridView.
|
||||
//
|
||||
puzzle_grid = (GridView)v.findViewById(R.id.puzzle_grid);
|
||||
puzzle_grid.setAdapter( new PuzzlesAdapter(getActivity()) );
|
||||
//puzzle_grid.addView(puzzle_grid);
|
||||
puzzle_grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
|
||||
{
|
||||
onPuzzleClicked(v,(int)id);
|
||||
}
|
||||
});
|
||||
puzzle_grid.setOnTouchListener( new View.OnTouchListener() {
|
||||
public boolean onTouch(View view, MotionEvent me) {
|
||||
gridViewXScrollDetector.onTouchEvent( me );
|
||||
return false;
|
||||
}
|
||||
});
|
||||
gridViewXScrollDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
|
||||
public boolean onDown(MotionEvent e) {
|
||||
return (lister!=null) ? lister.onPuzzleGridDown() : false;
|
||||
}
|
||||
public boolean onScroll(MotionEvent e1, MotionEvent e2, float dx, float dy) {
|
||||
return (lister!=null) ? lister.onPuzzleGridXScroll(dx) : false;
|
||||
}
|
||||
});
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
public void onPuzzleClicked(View v, int level )
|
||||
{
|
||||
if (lister!=null)
|
||||
lister.onPuzzleLevelClicked(level);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume()
|
||||
{
|
||||
super.onResume();
|
||||
if ( puzzle_grid != null )
|
||||
puzzle_grid.invalidate();
|
||||
}
|
||||
|
||||
public class PuzzlesAdapter extends BaseAdapter
|
||||
{
|
||||
private Context context;
|
||||
private int icon_size;
|
||||
private int text_x;
|
||||
private int text_y;
|
||||
private Bitmap lock_icon;
|
||||
private Bitmap unlock_icon;
|
||||
private Paint paint;
|
||||
|
||||
public PuzzlesAdapter(Context c)
|
||||
{
|
||||
context = c;
|
||||
icon_size = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 10, getResources().getDisplayMetrics() );
|
||||
int text_size = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 4, getResources().getDisplayMetrics() );
|
||||
text_x = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 9, getResources().getDisplayMetrics() );
|
||||
text_y = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 9, getResources().getDisplayMetrics() );
|
||||
|
||||
Bitmap lock_icon_tmp = BitmapFactory.decodeResource( getResources(), R.drawable.lock );
|
||||
lock_icon = Bitmap.createBitmap(icon_size,icon_size,Bitmap.Config.ARGB_8888);
|
||||
Canvas lock_canvas = new Canvas(lock_icon);
|
||||
lock_canvas.drawBitmap(lock_icon_tmp,
|
||||
new Rect(0,0,lock_icon_tmp.getWidth()-1,lock_icon_tmp.getHeight()-1),
|
||||
new Rect(0,0,icon_size, icon_size),
|
||||
null);
|
||||
|
||||
Bitmap unlock_icon_tmp = BitmapFactory.decodeResource( getResources(), R.drawable.unlock );
|
||||
unlock_icon = Bitmap.createBitmap(icon_size,icon_size,Bitmap.Config.ARGB_8888);
|
||||
Canvas unlock_canvas = new Canvas(unlock_icon);
|
||||
unlock_canvas.drawBitmap(unlock_icon_tmp,
|
||||
new Rect(0,0,unlock_icon_tmp.getWidth()-1,unlock_icon_tmp.getHeight()-1),
|
||||
new Rect(0,0,icon_size, icon_size),
|
||||
null);
|
||||
|
||||
paint = new Paint();
|
||||
paint.setColor( Color.WHITE );
|
||||
paint.setTextSize( text_size );
|
||||
paint.setTextAlign(Paint.Align.RIGHT);
|
||||
}
|
||||
|
||||
public int getCount()
|
||||
{
|
||||
return theApp().getPuzzleCount();
|
||||
}
|
||||
|
||||
public Object getItem(int position)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public long getItemId(int position)
|
||||
{
|
||||
return position+1;
|
||||
}
|
||||
|
||||
// create a new ImageView for each item referenced by the Adapter
|
||||
public View getView(int position, View convertView, ViewGroup parent)
|
||||
{
|
||||
ImageView imageView;
|
||||
if (convertView == null)
|
||||
{ // if it's not recycled, initialize some attributes
|
||||
imageView = new ImageView(context);
|
||||
imageView.setLayoutParams(new GridView.LayoutParams(icon_size, icon_size));
|
||||
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
|
||||
// imageView.setPadding(8, 8, 8, 8);
|
||||
}
|
||||
else
|
||||
imageView = (ImageView) convertView;
|
||||
|
||||
int id = (int)getItemId(position);
|
||||
Bitmap icon = ( id <= theApp().getAchivedLevel() )
|
||||
? unlock_icon.copy(Bitmap.Config.ARGB_8888,true)
|
||||
: lock_icon.copy(Bitmap.Config.ARGB_8888,true);
|
||||
Canvas canvas = new Canvas(icon);
|
||||
canvas.drawText( Integer.toString(id), text_x, text_y, paint);
|
||||
imageView.setImageBitmap(icon);
|
||||
return imageView;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user