111 lines
2.9 KiB
Java
111 lines
2.9 KiB
Java
package com.rubenvandeven.emotionhero;
|
|
|
|
import android.content.Context;
|
|
import android.graphics.Canvas;
|
|
import android.graphics.Paint;
|
|
import android.graphics.PixelFormat;
|
|
import android.util.AttributeSet;
|
|
import android.util.Log;
|
|
import android.view.SurfaceHolder;
|
|
import android.view.SurfaceView;
|
|
|
|
/**
|
|
* Created by ruben on 16/08/16.
|
|
*/
|
|
|
|
public class ScenarioView extends SurfaceView implements SurfaceHolder.Callback {
|
|
|
|
private PanelThread _thread;
|
|
|
|
private Scenario _scenario;
|
|
|
|
|
|
// see: http://blog.danielnadeau.io/2012/01/android-canvas-beginners-tutorial.html
|
|
class PanelThread extends Thread {
|
|
private SurfaceHolder _surfaceHolder;
|
|
private ScenarioView _panel;
|
|
private boolean _run = false;
|
|
|
|
|
|
public PanelThread(SurfaceHolder surfaceHolder, ScenarioView panel) {
|
|
_surfaceHolder = surfaceHolder;
|
|
_panel = panel;
|
|
}
|
|
|
|
|
|
public void setRunning(boolean run) { //Allow us to stop the thread
|
|
_run = run;
|
|
}
|
|
|
|
|
|
@Override
|
|
public void run() {
|
|
Canvas c;
|
|
while (_run) { //When setRunning(false) occurs, _run is
|
|
c = null; //set to false and loop ends, stopping thread
|
|
|
|
try {
|
|
c = _surfaceHolder.lockCanvas(null);
|
|
synchronized (_surfaceHolder) {
|
|
//Insert methods to modify positions of items in onDraw()
|
|
postInvalidate();
|
|
}
|
|
} finally {
|
|
if (c != null) {
|
|
_surfaceHolder.unlockCanvasAndPost(c);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public ScenarioView(Context context, Scenario s) {
|
|
super(context);
|
|
getHolder().addCallback(this);
|
|
_scenario = s;
|
|
}
|
|
|
|
|
|
@Override
|
|
public void onDraw(Canvas canvas) {
|
|
//do drawing stuff here.
|
|
// Log.e("TEST2", "Jaa!");
|
|
_scenario.drawOnCanvas(canvas);
|
|
}
|
|
|
|
|
|
@Override
|
|
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
|
|
|
|
|
|
}
|
|
|
|
@Override
|
|
public void surfaceCreated(SurfaceHolder holder) {
|
|
setWillNotDraw(false); //Allows us to use invalidate() to call onDraw()
|
|
|
|
this.setZOrderOnTop(true);
|
|
holder.setFormat(PixelFormat.TRANSPARENT);
|
|
|
|
// Log.e("TEST2", "Jaa2!");
|
|
_thread = new PanelThread(getHolder(), this); //Start the thread that
|
|
_thread.setRunning(true); //will make calls to
|
|
_thread.start(); //onDraw()
|
|
}
|
|
|
|
|
|
@Override
|
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
|
try {
|
|
if(_thread != null) {
|
|
_thread.setRunning(false); //Tells thread to stop
|
|
_thread.join(); //Removes thread from mem.
|
|
}
|
|
} catch (InterruptedException e) {}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|