Make startup and mainmenu

This commit is contained in:
Ruben 2016-08-22 15:47:35 +01:00
parent 3d45a25895
commit c8085df15d
17 changed files with 469 additions and 50 deletions

View File

@ -3,30 +3,42 @@
xmlns:tools="http://schemas.android.com/tools"
package="com.rubenvandeven.emotionhero">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.CAMERA" />
<application
tools:replace="android:allowBackup,android:label"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:hardwareAccelerated="true">
tools:replace="android:allowBackup,android:label">
<activity
android:name=".GamingActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:theme="@style/FullscreenTheme">
<!-- <intent-filter> -->
<!-- <action android:name="android.intent.action.MAIN" /> -->
<!-- <category android:name="android.intent.category.LAUNCHER" /> -->
<!-- </intent-filter> -->
</activity>
<activity
android:name=".IntroActivity"
android:theme="@style/FullscreenTheme"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MenuActivity"
android:theme="@style/FullscreenTheme"
android:label="@string/app_name"></activity>
</application>
</manifest>

View File

@ -42,7 +42,6 @@ public class GamingActivity extends AppCompatActivity implements Detector.ImageL
public final static String INTENT_EXTRA_SCENARIO = "com.rubenvandeven.emotionhero.LEVEL";
final static String LOG_TAG = "EmotionHero";
final static String PLAYERINFO_FILENAME = "playerinfo.json";
final int PERMISSIONS_REQUEST_CAMERA = 1;
@ -68,6 +67,7 @@ public class GamingActivity extends AppCompatActivity implements Detector.ImageL
final static int SOUND_SCORE = 1;
protected Player player;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -77,6 +77,8 @@ public class GamingActivity extends AppCompatActivity implements Detector.ImageL
setContentView(R.layout.activity_gaming);
player = Player.getInstance(getApplicationContext());
Intent intent = getIntent();
// first level is the default to load, if none is given to intent
int scenarioLvlId = intent.getIntExtra(INTENT_EXTRA_SCENARIO, Scenario.SCENARIOS.get(0));
@ -148,6 +150,7 @@ public class GamingActivity extends AppCompatActivity implements Detector.ImageL
// currentScenario = new ScenarioAnger(this);
currentScenario = new Scenario(scenarioLvlId, this);
currentScenario.init(); // "start the clock"...
scenarioView = new ScenarioView(this, currentScenario);
@ -178,10 +181,10 @@ public class GamingActivity extends AppCompatActivity implements Detector.ImageL
String permission = "android.permission.CAMERA";
int res = getApplicationContext().checkCallingOrSelfPermission(permission);
if (res == PackageManager.PERMISSION_GRANTED) {
Log.e(LOG_TAG, "HAS PERM");
Log.i(LOG_TAG, "Has camera permission");
has_camera_permission = true;
} else {
Log.e(LOG_TAG, "NO PERM");
Log.i(LOG_TAG, "No camera permission");
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
PERMISSIONS_REQUEST_CAMERA);
@ -242,11 +245,6 @@ public class GamingActivity extends AppCompatActivity implements Detector.ImageL
Face face = list.get(0);
currentScenario.setCurrentFace(face);
scenarioView.setCurrentAttributeScoresForFace(face);
// if(frame instanceof Frame.ByteArrayFrame)
// {
// Frame.ByteArrayFrame byteArrayFrame = (Frame.ByteArrayFrame) frame;
// }
}
}
@ -342,37 +340,6 @@ public class GamingActivity extends AppCompatActivity implements Detector.ImageL
sound = new SoundPool(5, AudioManager.STREAM_MUSIC,0);
}
public PlayerInfo getPlayerInfo() {
try{
FileInputStream fis = openFileInput(PLAYERINFO_FILENAME );
StringBuilder builder = new StringBuilder();
int ch;
while((ch = fis.read()) != -1){
builder.append((char)ch);
}
return PlayerInfo.fromJson(builder.toString());
} catch (IOException e) {
return new PlayerInfo();
}
}
public void savePlayerScore(Score score) {
PlayerInfo playerInfo = getPlayerInfo();
playerInfo.addScore(score);
savePlayerInfo(playerInfo);
}
public void savePlayerInfo(PlayerInfo playerInfo) {
try {
FileOutputStream fos = openFileOutput(PLAYERINFO_FILENAME, Context.MODE_PRIVATE);
fos.write(playerInfo.toJson().getBytes());
fos.close();
} catch(IOException e) {
// for now skip error
Log.e(LOG_TAG, "Could not save player information!");
}
}
public void finishLevel() {
setText("LEVEL ENDED");
stopDetector();
@ -381,7 +348,7 @@ public class GamingActivity extends AppCompatActivity implements Detector.ImageL
nextLvlButton.setVisibility(View.VISIBLE);
}
PlayerInfo playerInfo = getPlayerInfo();
PlayerInfo playerInfo = player.getPlayerInfo();
ScoreList scores = playerInfo.getScoresForLevel(currentScenario.id);
Score score = currentScenario.getScore();
@ -399,8 +366,15 @@ public class GamingActivity extends AppCompatActivity implements Detector.ImageL
// show the highscores (top 3?)...
// check whether this is the highest reached level by the player
// ... used by 'continue' button in main menu
Scenario nextScenario = new Scenario(currentScenario.getNextLevelId(), this);
if(nextScenario.isHigherThen(playerInfo.reachedLevelId)) {
playerInfo.reachedLevelId = currentScenario.id;
}
scores.add(score);
savePlayerInfo(playerInfo);
player.savePlayerInfo(playerInfo);
String highscoreText = "TOPSCORES\n";

View File

@ -0,0 +1,75 @@
package com.rubenvandeven.emotionhero;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;
public class IntroActivity extends AppCompatActivity {
protected Player player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_intro);
player = Player.getInstance(getApplicationContext());
int extraLogoDelay;
if(player.isNew()) {
extraLogoDelay = 1500;
} else {
extraLogoDelay = 0;
}
final ImageView logoV2 = (ImageView) findViewById(R.id.logoV2);
final ImageView logoArquivo = (ImageView) findViewById(R.id.logoArquivo);
final ImageView logoAffectiva = (ImageView) findViewById(R.id.logoAffectiva);
ObjectAnimator fadeOutV2 = ObjectAnimator.ofFloat(logoV2, "alpha", 0f);
fadeOutV2.setDuration(1000);
fadeOutV2.setStartDelay(0 + extraLogoDelay);
ObjectAnimator fadeInV2 = ObjectAnimator.ofFloat(logoV2, "alpha", 0f, 1f);
fadeInV2.setDuration(1000);
ObjectAnimator fadeOutArquivo = ObjectAnimator.ofFloat(logoArquivo, "alpha", 0f);
fadeOutArquivo.setDuration(1000);
fadeOutArquivo.setStartDelay(0 + extraLogoDelay);
ObjectAnimator fadeInArquivo = ObjectAnimator.ofFloat(logoArquivo, "alpha", 0f, 1f);
fadeInArquivo.setDuration(1000);
ObjectAnimator fadeOutAffectiva = ObjectAnimator.ofFloat(logoAffectiva, "alpha", 0f);
fadeOutAffectiva.setDuration(1000);
fadeOutAffectiva.setStartDelay(1000 + extraLogoDelay);
ObjectAnimator fadeInAffectiva = ObjectAnimator.ofFloat(logoAffectiva, "alpha", 0f, 1f);
fadeInAffectiva.setDuration(1000);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playSequentially(fadeInV2, fadeOutV2,
fadeInArquivo, fadeOutArquivo,
fadeInAffectiva, fadeOutAffectiva);
animatorSet.start();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(IntroActivity.this, MenuActivity.class);
finish();
startActivity(intent);
}
}, 7000 + (3*extraLogoDelay));
}
}

View File

@ -0,0 +1,117 @@
package com.rubenvandeven.emotionhero;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MenuActivity extends AppCompatActivity {
Player player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_menu);
player = Player.getInstance(getApplicationContext());
Button startButton = (Button) findViewById(R.id.startButton);
Button continueButton = (Button) findViewById(R.id.continueButton);
Button highscoreButton = (Button) findViewById(R.id.highscoresButton);
Button quitButton = (Button) findViewById(R.id.quitButton);
final ImageView logoEmotionHero = (ImageView) findViewById(R.id.logoEmotionHero);
if(!player.getPlayerInfo().canContinueLevel()) {
continueButton.setVisibility(View.GONE);
}
// prepare for animation
startButton.setAlpha(0f);
continueButton.setAlpha(0f);
highscoreButton.setAlpha(0f);
quitButton.setAlpha(0f);
ObjectAnimator fadeInEmotionHero = ObjectAnimator.ofFloat(logoEmotionHero, "alpha", 0f, 1f);
fadeInEmotionHero.setDuration(1000);
ObjectAnimator fadeInstartButton = ObjectAnimator.ofFloat(startButton, "alpha", 0f, 1f);
fadeInstartButton.setDuration(1000);
ObjectAnimator fadeInContinueButton = ObjectAnimator.ofFloat(continueButton, "alpha", 0f, 1f);
fadeInContinueButton.setDuration(1000);
fadeInContinueButton.setStartDelay(300);
ObjectAnimator fadeInHighscoreButton = ObjectAnimator.ofFloat(highscoreButton, "alpha", 0f, 1f);
fadeInHighscoreButton.setDuration(1000);
fadeInHighscoreButton.setStartDelay(600);
ObjectAnimator fadeInQuitButton = ObjectAnimator.ofFloat(quitButton, "alpha", 0f, 1f);
fadeInQuitButton.setDuration(1000);
fadeInQuitButton.setStartDelay(900);
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(1000); //You can manage the blinking time with this parameter
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
logoEmotionHero.startAnimation(anim);
AnimatorSet buttonAnimatorSet = new AnimatorSet();
buttonAnimatorSet.playTogether(fadeInstartButton,
fadeInContinueButton,
fadeInHighscoreButton,
fadeInQuitButton);
buttonAnimatorSet.start();
// AnimatorSet animatorSet = new AnimatorSet();
// animatorSet.playSequentially(fadeInEmotionHero, buttonAnimatorSet);
// animatorSet.start();
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MenuActivity.this, GamingActivity.class);
startActivity(intent);
}
});
continueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MenuActivity.this, GamingActivity.class);
intent.putExtra(GamingActivity.INTENT_EXTRA_SCENARIO, player.getPlayerInfo().reachedLevelId);
startActivity(intent);
}
});
highscoreButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast toast = Toast.makeText(getApplicationContext(), "Not implemented yet", Toast.LENGTH_LONG);
toast.show();
}
});
quitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}

View File

@ -0,0 +1,77 @@
package com.rubenvandeven.emotionhero;
import android.content.Context;
import android.util.Log;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* The current player of the game (for this device)
* Created by ruben on 22/08/16.
*/
public class Player {
final static String PLAYERINFO_FILENAME = "playerinfo.json";
private static Player ourInstance;
private Context c;
PlayerInfo info;
public static Player getInstance(Context c) {
if(ourInstance == null) {
ourInstance = new Player(c);
}
return ourInstance;
}
private Player(Context c) {
this.c = c;
this.info = loadPlayerInfo();
}
public PlayerInfo getPlayerInfo() {
return this.info;
}
public PlayerInfo loadPlayerInfo() {
try{
FileInputStream fis = c.openFileInput(PLAYERINFO_FILENAME );
StringBuilder builder = new StringBuilder();
int ch;
while((ch = fis.read()) != -1){
builder.append((char)ch);
}
return PlayerInfo.fromJson(builder.toString());
} catch (IOException e) {
return new PlayerInfo();
}
}
public boolean isNew() {
if(this.info.reachedLevelId < 0) {
return true;
}
return false;
}
public void savePlayerScore(Score score) {
info.addScore(score);
savePlayerInfo(info);
}
public void savePlayerInfo(PlayerInfo playerInfo) {
try {
FileOutputStream fos = c.openFileOutput(PLAYERINFO_FILENAME, Context.MODE_PRIVATE);
fos.write(playerInfo.toJson().getBytes());
fos.close();
} catch(IOException e) {
// for now skip error
Log.e("PlayerInfo", "Could not save player information!");
}
}
}

View File

@ -1,6 +1,7 @@
package com.rubenvandeven.emotionhero;
import android.support.annotation.NonNull;
import android.util.Log;
import android.util.SparseArray;
import com.google.gson.Gson;
@ -16,6 +17,7 @@ import java.util.Set;
public class PlayerInfo {
public Map<Integer, ScoreList> levelScores = new HashMap<>();
public int reachedLevelId = -1;
public ScoreList getScoresForLevel(int lvl_id) {
ScoreList scoreList;
@ -45,6 +47,24 @@ public class PlayerInfo {
public String toJson() {
Gson gson = new Gson();
return gson.toJson(this);
String json = gson.toJson(this);
Log.i("PlayerInfo", "Generated: "+json);
return json;
}
/**
* Whether the reachedLevelId is higher than the first level
* used to show 'continue' button in menu
* @return
*/
public boolean canContinueLevel() {
if(reachedLevelId < 0)
return false;
if(Scenario.SCENARIOS.indexOf(reachedLevelId) > 0) {
return true;
}
return false;
}
}

View File

@ -6,6 +6,8 @@ import android.util.Log;
import com.affectiva.android.affdex.sdk.detector.Face;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
@ -285,7 +287,9 @@ public class Scenario {
return null;
}
return new Score(id, getHitTotalValue());
Score score = new Score(id, getHitTotalValue());
score.setTargets(targets);
return score;
}
public int getMaxScore() {
@ -332,4 +336,16 @@ public class Scenario {
return false;
}
public boolean isHigherThen(int lvl_id) {
return isHigherLevel(id, lvl_id);
}
public static boolean isHigherLevel(int lvl_id, int then_lvl_id) {
if(lvl_id == then_lvl_id) {
return false;
}
return SCENARIOS.indexOf(lvl_id) > SCENARIOS.indexOf(then_lvl_id);
}
}

View File

@ -1,5 +1,6 @@
package com.rubenvandeven.emotionhero;
import java.util.ArrayList;
import java.util.Date;
/**
@ -11,9 +12,15 @@ public class Score {
float score;
Date time;
ArrayList<Scenario.Target> targets = new ArrayList<>();
public Score(int lvl_id, float score) {
this.lvl_id = lvl_id;
this.score = score;
this.time = new Date();
}
public void setTargets(ArrayList<Scenario.Target> targets) {
this.targets = targets;
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 96 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

View File

@ -0,0 +1,47 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_intro"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.rubenvandeven.emotionhero.IntroActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/v2_logo"
android:id="@+id/logoV2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:alpha="0"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/arquivo237_logo"
android:id="@+id/logoArquivo"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:alpha="0" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/affectiva_logo"
android:id="@+id/logoAffectiva"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:alpha="0" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/emotionhero_logo"
android:id="@+id/logoEmotionHero"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:alpha="0" />
</RelativeLayout>

View File

@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.rubenvandeven.emotionhero.MenuActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/logoEmotionHero"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp">
<Button
android:text="Start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/startButton"
android:fontFamily="sans-serif" />
<Button
android:text="Continue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/continueButton"
android:fontFamily="sans-serif" />
<Button
android:text="Highscores"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/highscoresButton"
android:fontFamily="sans-serif" />
<Button
android:text="Exit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/quitButton"
android:fontFamily="sans-serif"
android:layout_marginTop="20dp" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/emotionhero_logo"
android:id="@+id/logoEmotionHero"
android:cropToPadding="false"
android:adjustViewBounds="true"
android:layout_marginTop="58dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>

View File

@ -0,0 +1,6 @@
<resources>
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
<dimen name="activity_horizontal_margin">64dp</dimen>
</resources>

View File

@ -0,0 +1,5 @@
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>

View File

@ -11,7 +11,7 @@
<style name="FullscreenTheme" parent="AppTheme">
<item name="android:actionBarStyle">@style/FullscreenActionBarStyle</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowBackground">@null</item>
<item name="android:windowBackground">@color/colorPrimary</item>
<item name="metaButtonBarStyle">?android:attr/buttonBarStyle</item>
<item name="metaButtonBarButtonStyle">?android:attr/buttonBarButtonStyle</item>
</style>