2016-08-16 19:25:28 +02:00
|
|
|
package com.rubenvandeven.emotionhero;
|
|
|
|
|
2016-08-17 13:17:04 +02:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.Canvas;
|
|
|
|
import android.graphics.Color;
|
|
|
|
import android.graphics.Paint;
|
2016-08-16 19:25:28 +02:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import com.affectiva.android.affdex.sdk.detector.Face;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by ruben on 16/08/16.
|
|
|
|
*/
|
|
|
|
|
|
|
|
abstract public class Scenario {
|
|
|
|
|
|
|
|
float duration = 0;
|
|
|
|
|
2016-08-17 13:17:04 +02:00
|
|
|
long startTime = 0;
|
|
|
|
|
2016-08-16 19:25:28 +02:00
|
|
|
ArrayList<Target> targets = new ArrayList<>();
|
|
|
|
|
|
|
|
abstract void createScenario();
|
|
|
|
|
|
|
|
class Target {
|
|
|
|
public Emotion emotion;
|
|
|
|
public float value;
|
|
|
|
public float timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayList<Score> scores = new ArrayList<>();
|
|
|
|
|
|
|
|
class Score {
|
|
|
|
public float value;
|
|
|
|
/**
|
|
|
|
* Extra bonus given
|
|
|
|
*/
|
|
|
|
public boolean isSpotOn = false;
|
|
|
|
/**
|
|
|
|
* The target the score is awarded for
|
|
|
|
*/
|
|
|
|
public Target target;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
|
|
|
public Scenario()
|
|
|
|
{
|
|
|
|
createScenario();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a target on given timestamp
|
|
|
|
* @param emotion
|
|
|
|
* @param value
|
|
|
|
* @param timestamp
|
|
|
|
*/
|
|
|
|
public void setTarget(Emotion emotion, float value, float timestamp)
|
|
|
|
{
|
2016-08-17 13:17:04 +02:00
|
|
|
// Log.e(GamingActivity.LOG_TAG, "Set target:" + Float.toString(timestamp) + " " + Float.toString(duration));
|
2016-08-16 19:25:28 +02:00
|
|
|
if(timestamp > duration)
|
|
|
|
{
|
|
|
|
duration = timestamp;
|
|
|
|
}
|
|
|
|
Target target = new Target();
|
|
|
|
target.timestamp = timestamp;
|
|
|
|
target.value = value;
|
|
|
|
target.emotion = emotion;
|
|
|
|
targets.add(target);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add target after existing targets, give delta with last item instead of absolute time.
|
|
|
|
* @param emotion
|
|
|
|
* @param value
|
|
|
|
* @param interval
|
|
|
|
*/
|
|
|
|
public void addTarget(Emotion emotion, float value, float interval)
|
|
|
|
{
|
|
|
|
float timestamp;
|
|
|
|
if(targets.isEmpty()) {
|
|
|
|
timestamp = interval;
|
|
|
|
} else {
|
|
|
|
timestamp = targets.get(targets.size() - 1).timestamp + interval;
|
|
|
|
}
|
|
|
|
setTarget(emotion, value, timestamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void validateFaceOnTime(Face face, float timestamp)
|
|
|
|
{
|
|
|
|
// TODO: interpolation of time
|
|
|
|
for (int i = targets.size() - 1; i >= 0; i--) {
|
|
|
|
Target target = targets.get(i);
|
|
|
|
if(target.timestamp > timestamp - 0.2 && target.timestamp < timestamp + 0.2) {
|
|
|
|
float scored_value = target.emotion.getValueFromFace(face);
|
|
|
|
float required_value = target.value;
|
|
|
|
Score score = new Score();
|
|
|
|
score.value = 100 - Math.abs(scored_value-required_value);
|
|
|
|
score.target = target;
|
|
|
|
scores.add(score);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getTotalScore()
|
|
|
|
{
|
|
|
|
float value = 0;
|
|
|
|
for (Score score : scores) {
|
|
|
|
value += score.value;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether given timestamp is within duration of the scenario
|
|
|
|
* @param timestamp
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public boolean isWithinTime(float timestamp)
|
|
|
|
{
|
|
|
|
return timestamp <= duration;
|
|
|
|
}
|
|
|
|
|
2016-08-17 13:17:04 +02:00
|
|
|
public void drawOnCanvas(Canvas canvas) {
|
|
|
|
// canvas;
|
|
|
|
Paint paint = new Paint();
|
|
|
|
paint.setColor(Color.RED);
|
|
|
|
float height = (float) canvas.getHeight();
|
|
|
|
float pix_per_sec = height / duration;
|
|
|
|
|
|
|
|
float sec_height = canvas.getHeight() * (float) 0.2; // each second is 20% of canvas height
|
|
|
|
|
|
|
|
float diff_y;
|
|
|
|
// if not started, don't move the labels, if started, move them by diff_y
|
|
|
|
if(startTime == 0) {
|
|
|
|
diff_y = 0;
|
|
|
|
start();
|
|
|
|
} else {
|
|
|
|
float diff_t = ((System.currentTimeMillis() - startTime)) / (float) 1000;
|
|
|
|
Log.d("TIME", Float.toString(diff_t));
|
|
|
|
if(diff_t > duration) { // never larger than scenario duration
|
|
|
|
diff_t = duration;
|
|
|
|
}
|
|
|
|
diff_y = sec_height * diff_t;
|
|
|
|
}
|
|
|
|
|
|
|
|
// bottom at 80%;
|
|
|
|
float bottomline_height = height * (float) 0.8;
|
|
|
|
|
|
|
|
canvas.drawLine(0, bottomline_height, canvas.getWidth(), bottomline_height, paint);
|
|
|
|
canvas.drawLine(0, bottomline_height+1, canvas.getWidth(), bottomline_height+1, paint);
|
|
|
|
|
|
|
|
for(Target target: targets) {
|
|
|
|
float y_pos = bottomline_height - (target.timestamp * sec_height);
|
|
|
|
String target_text = target.emotion.toString() + " " + target.value + "%";
|
|
|
|
canvas.drawText(target_text, canvas.getWidth()/2, y_pos + diff_y , paint);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void start()
|
|
|
|
{
|
|
|
|
startTime = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
|
2016-08-16 19:25:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class ScenarioAnger extends Scenario{
|
|
|
|
void createScenario()
|
|
|
|
{
|
2016-08-17 13:17:04 +02:00
|
|
|
Log.d(GamingActivity.LOG_TAG, "CREATE SCENARIO: anger");
|
2016-08-16 19:25:28 +02:00
|
|
|
setTarget(Emotion.ANGER, 10, 1);
|
|
|
|
setTarget(Emotion.ANGER, 20, 2);
|
|
|
|
setTarget(Emotion.ANGER, 40, 3);
|
|
|
|
setTarget(Emotion.ANGER, 70, 4);
|
|
|
|
setTarget(Emotion.ANGER, 100, 5);
|
|
|
|
}
|
|
|
|
}
|