emotionhero/app/src/main/java/com/rubenvandeven/emotionhero/ScenarioView.java

199 lines
6.0 KiB
Java

package com.rubenvandeven.emotionhero;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import com.affectiva.android.affdex.sdk.detector.Face;
import java.util.HashMap;
import java.util.Map;
/**
* Created by ruben on 16/08/16.
*/
public class ScenarioView extends SurfaceView implements SurfaceHolder.Callback {
private PanelThread _thread;
private Scenario _scenario;
/**
* The scorres in this moment, as to draw them on the screen.
* Indexes are Emotion ordinals
*/
private Map<Emotion, Float> currentAttributeScores = new HashMap<>();
// 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.
float height = canvas.getHeight();
float width = canvas.getWidth();
// bottom at 80%;
float bottomline_height = height * (float) 0.8;
// draw current scores:
float used_width = (float) 0.8; // 0.7: only use center
float padding_left = canvas.getWidth() * (1-used_width) / 2;
float step_y = (canvas.getWidth() * used_width) / Emotion.values().length;
float max_ball_radius = step_y * (float) 0.8 / 2;
// bottom at 80%;
Paint mainPaint = new Paint();
mainPaint.setColor(Color.GRAY);
Paint linePaint = new Paint();
linePaint.setColor(Color.GRAY);
linePaint.setStrokeWidth(5);
// canvas.drawLine(0, bottomline_height, width, bottomline_height, linePaint);
for(Emotion emotion: Emotion.values())
{
float value = 0;
if(currentAttributeScores.containsKey(emotion)) {
value = currentAttributeScores.get(emotion);
}
if(value < 5) {
value = 5;
}
Paint emoPaint = new Paint();
Paint emoPaintOutline = new Paint();
emoPaint.setColor(emotion.getColor());
emoPaintOutline.setColor(emotion.getColor());
emoPaintOutline.setStyle(Paint.Style.STROKE);
emoPaintOutline.setStrokeWidth(2);
float cx = padding_left + (step_y * emotion.ordinal() + step_y / 2);
float cy = bottomline_height;
canvas.drawCircle(cx, cy, max_ball_radius, mainPaint);
canvas.drawCircle(cx, cy, max_ball_radius, emoPaintOutline);
canvas.drawCircle(cx, cy, max_ball_radius * value/100, emoPaint);
canvas.drawText(emotion.toString(), cx, cy + max_ball_radius * (float) 1.3, emoPaint);
}
// Draw targets:
float sec_height = height * (float) 0.2; // each second is 20% of canvas height
// current moved position
float diff_y = sec_height * _scenario.getTime();
for(Scenario.Target target: _scenario.getTargets()) {
Paint emoPaint = new Paint();
emoPaint.setColor(target.emotion.getColor());
float cy = bottomline_height - (target.timestamp * sec_height) + diff_y;
float cx = padding_left + (step_y * target.emotion.ordinal() + step_y / 2);
canvas.drawCircle(cx, cy, max_ball_radius * target.value/100, emoPaint);
// String target_text = target.emotion.toString() + " " + target.value + "%";
// canvas.drawText(target_text, cx, y_pos + diff_y , emoPaint);
}
}
public void setCurrentAttributeScoresForFace(Face face)
{
for(Emotion emotion: Emotion.values()) {
currentAttributeScores.put(emotion, emotion.getValueFromFace(face));
}
}
@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) {}
}
}