Commit 9957ed54 authored by jcqln's avatar jcqln
Browse files

Client: map (unconnected and so on)

parent 928435c6
package com.sist.j.grandquest;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link MapFragment.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link MapFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class MapFragment extends Fragment implements View.OnTouchListener {
private MapGridView sqrTable;
private int currentSqrDraw = R.drawable.blue_sqr40; //like put the chara on that square
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.table_fragment, container, true);
sqrTable = (MapGridView) v.findViewById(R.id.sqrTable);
sqrTable.setOnTouchListener(this);
//create grid
sqrTable.setCurrentColor(currentSqrDraw);
this.setRetainInstance(true);
return v;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX() ;
int y = (int) event.getY() ;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
sqrTable.changeSqrColor(x, y); //like changePlace
return true;
}
else if (event.getAction() == MotionEvent.ACTION_MOVE) {
sqrTable.changeSqrColor(x, y); //like changePlace
return true;
}
return false;
}
public void setCurrentSqr(int id) {
this.currentSqrDraw = id;
sqrTable.setCurrentColor(currentSqrDraw); //like setCurrentPlace
}
/* public void clearBoard() {
sqrTable.removeAllViews();
sqrTable.invalidate();
createBoard();
}
public Bitmap createBitmap() {
return sqrTable.createBitmap();
}
public void setBackground(int resourceId) {
sqrTable.setBackgroundResource(resourceId);
}*/
/*
@SuppressWarnings("deprecation")
public void setBackground(Bitmap b) {
sqrTable.setBackgroundDrawable(new BitmapDrawable(this.getResources(), b ) );
}
*/
/* private void createBoard() {
//define table grid
Display d = getActivity().getWindowManager().getDefaultDisplay();
//draw square, not really quite like this
int sqrSize = (int) (TableConfig.convertDpToPixel(TableConfig.DEFAULT_SQR_SIZE, getActivity()));
int[] vals = sqrTable.disposeSqr(d.getWidth(), d.getHeight(), sqrSize); //later, getWidth & getHeight from server
}*/
}
\ No newline at end of file
package com.sist.j.grandquest;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.widget.SlidingPaneLayout;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.GridView;
import com.sist.j.grandquest.MapFragment;
import com.sist.j.grandquest.MenuFragment;
public class MapGridActivity extends Activity {
//plus slidingpanelayout
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_grid);
GridView gridview = (GridView) findViewById(R.id.gridview);
final MapGridView.GridAdapter gridadapter = new MapGridView.GridAdapter(this);
gridview.setAdapter(gridadapter));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(gridadapter.getItem(position).getImageUrlString()));
startActivity(i);
}
});
}
}
/*
public class MapGridActivity extends Activity implements
MenuFragment.MenuEventListener {
private SlidingPaneLayout sPaneLy;
private MapFragment sqrTableFrag;
private static final int SELECT_PICTURE = 1410;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
this.sPaneLy = (SlidingPaneLayout) findViewById(R.id.sp);
sqrTableFrag = (MapFragment) getFragmentManager()
.findFragmentById(R.id.sqrTable);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_donate) {
// Show popup
// Handle donate here
}
return false;
}
@Override
public void onSqrSelected(int sqrId) {
sqrTableFrag.setCurrentSqr(sqrId);
closeMenu();
}
@Override
/* public void onClearSelected() {
closeMenu();
LayoutInflater inf = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inf.inflate(R.layout.popclear_layout, null, false);
final PopupWindow pw = new PopupWindow(v);
pw.setWidth(RelativeLayout.LayoutParams.WRAP_CONTENT);
pw.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView yesTxt = (TextView) v.findViewById(R.id.dlgYes);
TextView noTxt = (TextView) v.findViewById(R.id.dlgNo);
noTxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pw.dismiss();
}
});
yesTxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pw.dismiss();
sqrTableFrag.clearBoard();
}
});
pw.showAtLocation(v, Gravity.CENTER, 0, 0);
}*/
/*@Override
public void onSaveSelected() {
closeMenu();
LayoutInflater inf = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inf.inflate(R.layout.popsave_layout, null, false);
final PopupWindow pw = new PopupWindow(v);
pw.setFocusable(true);
pw.setWidth(RelativeLayout.LayoutParams.WRAP_CONTENT);
pw.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
final EditText edt = (EditText) v.findViewById(R.id.edtFileName);
TextView saveTxt = (TextView) v.findViewById(R.id.dlgSave);
TextView cancelTxt = (TextView) v.findViewById(R.id.dlgCancel);
cancelTxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pw.dismiss();
}
});
saveTxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pw.dismiss();
Bitmap b = sqrTableFrag.createBitmap();
try {//to save data
ImageUtility.saveImage(b, edt.getEditableText().toString(),
MapGridActivity.this);
} catch (SaveFileException sfe) {
Toast.makeText(MapGridActivity.this,
getResources().getText(R.string.msgSaveFileError),
Toast.LENGTH_LONG).show();
}
}
});
pw.showAtLocation(v, Gravity.CENTER, 0, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case SELECT_PICTURE:
if (resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap bkgImg = BitmapFactory.decodeFile(filePath);
Display disp = getWindowManager().getDefaultDisplay();
int w = disp.getWidth();
int h = disp.getHeight();
double scaleX = (w * 1.0) / bkgImg.getWidth();
double scaleY = (h * 1.0) / bkgImg.getHeight();
double scale = Math.min(scaleX, scaleY);
double sw = w * scaleX;
double sh = h * scaleY;
bkgImg = Bitmap.createBitmap(bkgImg, 0, 0, w, h);
sqrTableFrag.setBackground(bkgImg);
}
}
}
private void closeMenu() {
if (sPaneLy.isSlideable()) {
//close the left fragment
sPaneLy.closePane();
}
}
}*/
\ No newline at end of file
package com.sist.j.grandquest;
import com.sist.j.grandquest.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.Toast;
public class MapGridView extends GridView {
public class GridAdapter extends BaseAdapter {
private Context context;
//should retrieve from server the "col x row"
private Integer[] imageIds = {
R.drawable.tilebkg
};
public GridAdapter(Context c) {
context = c;
}
public int getCount() {
return imageIds.length;
}
public Object getItem(int position) {
return imageIds[position];
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View view, ViewGroup parent) {
SqrImageView iview;
if (view == null) {
iview = new SqrImageView(context);
iview.setLayoutParams(new GridView.LayoutParams(150, 200));
iview.setScaleType(SqrImageView.ScaleType.CENTER_CROP);
iview.setPadding(5, 5, 5, 5);
} else {
iview = (SqrImageView) view;
}
iview.setImageResource(imageIds[position]);
return iview;
}
//connect to server to get/collect item from that place/field
//should be random
private CharaToCollect[] imageIds = {
new CharaToCollect(R.drawable.tilebkg, "") //"" = to server
};
/*private ImageWithUrl[] imageIds = {
new ImageWithUrl(R.drawable.file1, "http://example.com/link1.html"),
new ImageWithUrl(R.drawable.file2, "http://example.com/link2.html"),
// and all the rest
};*/
public CharaToCollect getItem(int position) {
return imageIds[position];
}
public View getView(int position, View view, ViewGroup parent) {
// most of this is just as before
SqrImageView iview;
iview.setImageResource(imageIds[position].getImageId());
return iview;
}
protected class CharaToCollect extends Object {
private int imageId;
private String imageUrlString;
CharaToCollect(int id, String url) { //url = connect server
imageId = id;
imageUrlString = url;
}
public int getImageId() {
return imageId;
}
public String getImageUrlString() {
return imageUrlString;
}
}
}
}
/*
private int parentWidth;
private int parentHeight;
private int sqrSize;
private int currentColor;
private int numRow;
private int numCol;
private Context context;
public MapGridView(Context context) {
super(context);
this.context = context;
}
public MapGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
}
public MapGridView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public int[] disposeSqr(int width, int height, int sqrSize) {
//draw the table squares?
this.sqrSize = sqrSize;
numRow = height / sqrSize + 1;
numCol = width / sqrSize + 1;
int[] sqrColors = Sqr.createSqrArray(sqrSize, true);
for (int r=0; r < numRow ; r++) {
for (int c=0; c < numCol; c++) {
SqrImageView charaImg = new SqrImageView(getContext(), r, c);
this.addView(charaImg);
}
}
return new int[]{numRow, numCol};
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
parentWidth = MeasureSpec.getSize(widthMeasureSpec) ;
parentHeight = MeasureSpec.getSize(heightMeasureSpec) ;
this.setMeasuredDimension(parentWidth, parentHeight);
}
@Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
int childCount = getChildCount();
for (int i=0; i < childCount; i++) {
SqrImageView charaImg = (SqrImageView) getChildAt(i);
int left = charaImg.getCol() * sqrSize;
int top = charaImg.getRow() * sqrSize;
int right = left + sqrSize ;
int bottom = top + sqrSize ;
charaImg.layout(left, top, right, bottom);
}
}
public int getCurrentColor() {
return currentColor;
} //current place?
public void setCurrentColor(int currentColor) {
this.currentColor = currentColor;
}
public void changeSqrColor(int x, int y) {
//show the chara on that place?
int row = getRow(y);
int col = getColumn(x);
SqrImageView charaImg = (SqrImageView) getChildAt(col + ( row * numCol ));
if (charaImg != null) {
charaImg.setSqrColor(currentColor);
}
}
public int verify(int x, int y) {
int x1 = x ;
int y1 = y;
int col = getColumn(x1);
int row = getRow(y1);
if (x1 < 0 || y1 < 0)
return -1;
// Calc board size
int width = numCol * sqrSize;
int height = numRow * sqrSize;
if (x1 > width)
return -1;
if (y1 > height)
return 0;
return 1;
}
public int getRow(int y) {
return (int) Math.ceil(y / sqrSize);
}
public int getColumn(int x) {
return (int) Math.ceil( x / sqrSize);
}
public Bitmap createBitmap() {
Bitmap b = Bitmap.createBitmap(this.getWidth(), this.getHeight(), Bitmap.Config.RGB_565);
Canvas c = new Canvas(b);
this.draw(c);
return b;
}
*/
package com.sist.j.grandquest;
import com.sist.j.grandquest.R;
import com.sist.j.grandquest.R.drawable;
import com.sist.j.grandquest.R.id;
import com.sist.j.grandquest.R.layout;
import com.sist.j.grandquest.TableConfig;
import android.app.Fragment;
import android.content.Context;
import android.graphics.Shader;
import android.graphics.drawable.ShapeDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
public class MenuFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.menu_fragment, container, true);
ImageAdapter ia = new ImageAdapter(getActivity());
GridView gv = (GridView) v.findViewById(R.id.sqrGrid);
gv.setAdapter(ia);
// Clear btn
final TextView clearTxt = (TextView) v.findViewById(R.id.clearTxt);
final Animation animClear = TableConfig.getRotateAnim(this.getActivity());
animClear.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
( (MenuEventListener) getActivity()).onClearSelected();
}
});