|
|
|
@ -18,39 +18,34 @@ import android.view.View; |
|
|
|
|
|
|
|
|
|
public class Puzzle15Activity extends Activity implements CvCameraViewListener, View.OnTouchListener { |
|
|
|
|
|
|
|
|
|
private static final String TAG = "Sample::Puzzle15::Activity"; |
|
|
|
|
private static final String TAG = "Sample::Puzzle15::Activity"; |
|
|
|
|
|
|
|
|
|
private OpenCvNativeCameraView mOpenCvCameraView; |
|
|
|
|
private Puzzle15Processor mPuzzle15; |
|
|
|
|
|
|
|
|
|
private int mGameWidth; |
|
|
|
|
private int mGameHeight; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) { |
|
|
|
|
@Override |
|
|
|
|
public void onManagerConnected(int status) { |
|
|
|
|
switch (status) { |
|
|
|
|
case LoaderCallbackInterface.SUCCESS: |
|
|
|
|
{ |
|
|
|
|
Log.i(TAG, "OpenCV loaded successfully"); |
|
|
|
|
|
|
|
|
|
mPuzzle15 = new Puzzle15Processor(); |
|
|
|
|
mPuzzle15.prepareNewGame(); |
|
|
|
|
/* Now enable camera view to start receiving frames */ |
|
|
|
|
mOpenCvCameraView.setOnTouchListener(Puzzle15Activity.this); |
|
|
|
|
mOpenCvCameraView.enableView(); |
|
|
|
|
} break; |
|
|
|
|
default: |
|
|
|
|
{ |
|
|
|
|
super.onManagerConnected(status); |
|
|
|
|
} break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
private OpenCvNativeCameraView mOpenCvCameraView; |
|
|
|
|
private Puzzle15Processor mPuzzle15; |
|
|
|
|
|
|
|
|
|
private int mGameWidth; |
|
|
|
|
private int mGameHeight; |
|
|
|
|
|
|
|
|
|
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onManagerConnected(int status) { |
|
|
|
|
switch (status) { |
|
|
|
|
case LoaderCallbackInterface.SUCCESS: |
|
|
|
|
{ |
|
|
|
|
Log.i(TAG, "OpenCV loaded successfully"); |
|
|
|
|
|
|
|
|
|
/* Now enable camera view to start receiving frames */ |
|
|
|
|
mOpenCvCameraView.setOnTouchListener(Puzzle15Activity.this); |
|
|
|
|
mOpenCvCameraView.enableView(); |
|
|
|
|
} break; |
|
|
|
|
default: |
|
|
|
|
{ |
|
|
|
|
super.onManagerConnected(status); |
|
|
|
|
} break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onCreate(Bundle savedInstanceState) { |
|
|
|
@ -61,12 +56,27 @@ public class Puzzle15Activity extends Activity implements CvCameraViewListener, |
|
|
|
|
|
|
|
|
|
mOpenCvCameraView = (OpenCvNativeCameraView) findViewById(R.id.puzzle_activity_surface_view); |
|
|
|
|
mOpenCvCameraView.setCvCameraViewListener(this); |
|
|
|
|
mPuzzle15 = new Puzzle15Processor(); |
|
|
|
|
mPuzzle15.prepareNewGame(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onPause() |
|
|
|
|
{ |
|
|
|
|
mOpenCvCameraView.disableView(); |
|
|
|
|
super.onPause(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onResume() |
|
|
|
|
{ |
|
|
|
|
super.onResume(); |
|
|
|
|
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mLoaderCallback); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void onDestroy() { |
|
|
|
|
super.onDestroy(); |
|
|
|
|
mOpenCvCameraView.disableView(); |
|
|
|
|
super.onDestroy(); |
|
|
|
|
mOpenCvCameraView.disableView(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@ -79,49 +89,46 @@ public class Puzzle15Activity extends Activity implements CvCameraViewListener, |
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) { |
|
|
|
|
Log.i(TAG, "Menu Item selected " + item); |
|
|
|
|
if (item.getItemId() == R.id.menu_start_new_game) { |
|
|
|
|
/* We need to start new game */ |
|
|
|
|
mPuzzle15.prepareNewGame(); |
|
|
|
|
/* We need to start new game */ |
|
|
|
|
mPuzzle15.prepareNewGame(); |
|
|
|
|
} else if (item.getItemId() == R.id.menu_toggle_tile_numbers) { |
|
|
|
|
/* We need to enable or disable drawing of the tile numbers */ |
|
|
|
|
mPuzzle15.toggleTileNumbers(); |
|
|
|
|
/* We need to enable or disable drawing of the tile numbers */ |
|
|
|
|
mPuzzle15.toggleTileNumbers(); |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onCameraViewStarted(int width, int height) { |
|
|
|
|
mGameWidth = width; |
|
|
|
|
mGameHeight = height; |
|
|
|
|
mPuzzle15.prepareGameSize(width, height); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onCameraViewStarted(int width, int height) { |
|
|
|
|
mGameWidth = width; |
|
|
|
|
mGameHeight = height; |
|
|
|
|
mPuzzle15.prepareGameSize(width, height); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onCameraViewStopped() { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Mat onCameraFrame(Mat inputFrame) { |
|
|
|
|
return mPuzzle15.puzzleFrame(inputFrame); |
|
|
|
|
} |
|
|
|
|
@Override |
|
|
|
|
public void onCameraViewStopped() { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public boolean onTouch(View view, MotionEvent event) { |
|
|
|
|
int xpos, ypos; |
|
|
|
|
@Override |
|
|
|
|
public Mat onCameraFrame(Mat inputFrame) { |
|
|
|
|
return mPuzzle15.puzzleFrame(inputFrame); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
xpos = (view.getWidth() - mGameWidth) / 2; |
|
|
|
|
xpos = (int)event.getX() - xpos; |
|
|
|
|
@Override |
|
|
|
|
public boolean onTouch(View view, MotionEvent event) { |
|
|
|
|
int xpos, ypos; |
|
|
|
|
|
|
|
|
|
ypos = (view.getHeight() - mGameHeight) / 2; |
|
|
|
|
ypos = (int)event.getY() - ypos; |
|
|
|
|
xpos = (view.getWidth() - mGameWidth) / 2; |
|
|
|
|
xpos = (int)event.getX() - xpos; |
|
|
|
|
|
|
|
|
|
if (xpos >=0 && xpos <= mGameWidth && ypos >=0 && ypos <= mGameHeight) { |
|
|
|
|
/* click is inside the picture. Deliver this event to processor */ |
|
|
|
|
mPuzzle15.deliverTouchEvent(xpos, ypos); |
|
|
|
|
} |
|
|
|
|
ypos = (view.getHeight() - mGameHeight) / 2; |
|
|
|
|
ypos = (int)event.getY() - ypos; |
|
|
|
|
|
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
return false; |
|
|
|
|
if (xpos >=0 && xpos <= mGameWidth && ypos >=0 && ypos <= mGameHeight) { |
|
|
|
|
/* click is inside the picture. Deliver this event to processor */ |
|
|
|
|
mPuzzle15.deliverTouchEvent(xpos, ypos); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|