340 lines
13 KiB
Java
340 lines
13 KiB
Java
package com.rubenvandeven.emotionhero;
|
|
|
|
import android.content.Intent;
|
|
import android.graphics.Typeface;
|
|
import android.support.design.widget.FloatingActionButton;
|
|
import android.support.design.widget.Snackbar;
|
|
import android.support.v4.content.ContextCompat;
|
|
import android.support.v7.app.AppCompatActivity;
|
|
import android.support.v7.widget.LinearLayoutCompat;
|
|
import android.support.v7.widget.Toolbar;
|
|
|
|
import android.support.v4.app.Fragment;
|
|
import android.support.v4.app.FragmentManager;
|
|
import android.support.v4.app.FragmentPagerAdapter;
|
|
import android.support.v4.view.ViewPager;
|
|
import android.os.Bundle;
|
|
import android.util.Log;
|
|
import android.view.LayoutInflater;
|
|
import android.view.MenuItem;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
|
|
import android.view.WindowManager;
|
|
import android.view.animation.AlphaAnimation;
|
|
import android.view.animation.Animation;
|
|
import android.widget.Button;
|
|
import android.widget.LinearLayout;
|
|
import android.widget.TextView;
|
|
|
|
import java.util.UUID;
|
|
|
|
public class HighscoreActivity extends AppCompatActivity {
|
|
|
|
|
|
public final static String INTENT_EXTRA_SCORE_ID = "com.rubenvandeven.emotionhero.SCORE_ID";
|
|
public final static String INTENT_EXTRA_GAME_ID = "com.rubenvandeven.emotionhero.GAME_ID";
|
|
|
|
/**
|
|
* The {@link android.support.v4.view.PagerAdapter} that will provide
|
|
* fragments for each of the sections. We use a
|
|
* {@link FragmentPagerAdapter} derivative, which will keep every
|
|
* loaded fragment in memory. If this becomes too memory intensive, it
|
|
* may be best to switch to a
|
|
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
|
|
*/
|
|
private SectionsPagerAdapter mSectionsPagerAdapter;
|
|
|
|
/**
|
|
* The {@link ViewPager} that will host the section contents.
|
|
*/
|
|
private ViewPager mViewPager;
|
|
|
|
Player player;
|
|
|
|
|
|
/**
|
|
* If the intent is loaded from the game, it should show a specific highscorelist
|
|
* with the given score highligted
|
|
*/
|
|
Score score;
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
|
setContentView(R.layout.activity_highscore);
|
|
|
|
player = Player.getInstance(getApplicationContext());
|
|
|
|
String scoreIdString = getIntent().getStringExtra(INTENT_EXTRA_SCORE_ID);
|
|
String gameIdString = getIntent().getStringExtra(INTENT_EXTRA_GAME_ID);
|
|
if(scoreIdString != null) {
|
|
UUID score_id = UUID.fromString(scoreIdString);
|
|
score = player.getPlayerInfo().getScore(score_id);
|
|
if(score == null) {
|
|
Log.e("Highscore", "CANNOT FIND SCORE!! " + scoreIdString);
|
|
}
|
|
}
|
|
if(gameIdString != null) {
|
|
long gameId = Long.valueOf(gameIdString);
|
|
Game game = player.getGameOpenHelper().getGameByid(gameId);
|
|
if(game == null) {
|
|
Log.e("Highscore", "CANNOT FIND GAME!! " + gameIdString);
|
|
} else {
|
|
Log.i("Highscore", "FOUND GAME" + game.id + " " + game.score);
|
|
Log.i("Highscore", "RANK " + player.getGameOpenHelper().getLocalRankOfGame(game));
|
|
player.api.syncGame(game);
|
|
}
|
|
}
|
|
|
|
Log.i("Highscore", ""+player.getPlayerInfo().reachedLevelId);
|
|
|
|
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
|
setSupportActionBar(toolbar);
|
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
|
getSupportActionBar().setTitle("Highscores");
|
|
// Create the adapter that will return a fragment for each of the three
|
|
// primary sections of the activity.
|
|
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
|
|
|
|
// Set up the ViewPager with the sections adapter.
|
|
mViewPager = (ViewPager) findViewById(R.id.container);
|
|
mViewPager.setAdapter(mSectionsPagerAdapter);
|
|
|
|
// if score is set, set current page.
|
|
if(score != null) {
|
|
int levelpage = Scenario.SCENARIOS.indexOf(score.lvl_id);
|
|
mViewPager.setCurrentItem(levelpage);
|
|
}
|
|
|
|
|
|
|
|
// start the current level when pressing the big red button
|
|
// ... but only if having access.
|
|
// FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
|
|
// fab.setOnClickListener(new View.OnClickListener() {
|
|
// @Override
|
|
// public void onClick(View view) {
|
|
// int lvl_id = mSectionsPagerAdapter.getLevelIdForPosition(mViewPager.getCurrentItem());
|
|
// if(!player.getPlayerInfo().hasAccessToLevel(lvl_id)) {
|
|
// Snackbar.make(view, "No access to this level yet... complete the other levels first" , Snackbar.LENGTH_LONG)
|
|
// .setAction("Action", null).show();
|
|
// } else {
|
|
// Intent intent = new Intent(HighscoreActivity.this, GamingActivity.class);
|
|
// intent.putExtra(GamingActivity.INTENT_EXTRA_SCENARIO, lvl_id);
|
|
// finish();
|
|
// startActivity(intent);
|
|
// }
|
|
// }
|
|
// });
|
|
|
|
}
|
|
|
|
/**
|
|
* A placeholder fragment containing a simple view.
|
|
*/
|
|
public static class HighscoreFragment extends Fragment {
|
|
/**
|
|
* The fragment argument representing the section number for this
|
|
* fragment.
|
|
*/
|
|
private static final String ARG_LVL_ID = "LVL_ID";
|
|
private static final String ARG_SCORE_ID = "SCORE_ID";
|
|
|
|
public HighscoreFragment() {
|
|
}
|
|
|
|
/**
|
|
* Returns a new instance of this fragment for the given section
|
|
* number.
|
|
*/
|
|
public static HighscoreFragment newInstance(int lvl_id, String scoreIdString) {
|
|
HighscoreFragment fragment = new HighscoreFragment();
|
|
Bundle args = new Bundle();
|
|
args.putInt(ARG_LVL_ID, lvl_id);
|
|
args.putString(ARG_SCORE_ID, scoreIdString);
|
|
fragment.setArguments(args);
|
|
return fragment;
|
|
}
|
|
|
|
@Override
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
Bundle savedInstanceState) {
|
|
|
|
View rootView = inflater.inflate(R.layout.fragment_highscore, container, false);
|
|
|
|
|
|
Button startLvlButton = (Button) rootView.findViewById(R.id.startLvlButton);
|
|
Button nextLvlButton = (Button) rootView.findViewById(R.id.nextLvlButton);
|
|
|
|
int lvl_id = getArguments().getInt(ARG_LVL_ID);
|
|
|
|
Score score = null;
|
|
String scoreIdString = getArguments().getString(ARG_SCORE_ID);
|
|
|
|
final Scenario scenario = new Scenario(lvl_id, null);
|
|
|
|
Player player = Player.getInstance(getContext());
|
|
ScoreList scores = player.getPlayerInfo().getScoresForLevel(lvl_id);
|
|
|
|
if(scoreIdString != null) {
|
|
UUID scoreId = UUID.fromString(scoreIdString);
|
|
score = player.getPlayerInfo().getScore(scoreId);
|
|
}
|
|
|
|
|
|
TextView levelName = (TextView) rootView.findViewById(R.id.levelName);
|
|
levelName.setText(scenario.toString());
|
|
|
|
ScoreList topscores = scores.getTopN(5);
|
|
|
|
LinearLayout topscoreList = (LinearLayout) rootView.findViewById(R.id.topscoreList);
|
|
|
|
if(topscores.size() == 0) {
|
|
// TextView noScoreItemText = new TextView(getContext(), null, R.style.AppTheme);
|
|
TextView noScoreItemText = new TextView(getContext());
|
|
noScoreItemText.setText("No Scores");
|
|
noScoreItemText.setTextColor(ContextCompat.getColor(getContext(), R.color.highscore));
|
|
noScoreItemText.setTextSize(getResources().getDimension(R.dimen.highscore_textsize));
|
|
noScoreItemText.setTypeface(Typeface.DEFAULT_BOLD);
|
|
topscoreList.addView(noScoreItemText);
|
|
}
|
|
|
|
boolean foundScore = false;
|
|
int i = 0;
|
|
for(Score topscore: topscores) {
|
|
i++;
|
|
String highscoreText = String.format("%1$d. %2$.2f", i, topscore.score); //make line by line elements
|
|
TextView scoreItem = new TextView(getContext());
|
|
scoreItem.setText(highscoreText);
|
|
scoreItem.setTextColor(ContextCompat.getColor(getContext(), R.color.highscore));
|
|
float multiplier = 1 + (5-i)/5f;
|
|
scoreItem.setTextSize(getResources().getDimension(R.dimen.highscore_textsize) * multiplier);
|
|
scoreItem.setTypeface(Typeface.DEFAULT_BOLD);
|
|
topscoreList.addView(scoreItem);
|
|
|
|
if(score != null && topscore.id == score.id) {
|
|
foundScore = true;
|
|
setBlinking(scoreItem);
|
|
}
|
|
}
|
|
|
|
// if score is set.. but not a highscore...
|
|
if(score != null && foundScore == false) {
|
|
String highscoreText = String.format("\n... %1$.2f", score.score); //make line by line elements
|
|
TextView scoreItem = new TextView(getContext());
|
|
scoreItem.setText(highscoreText);
|
|
scoreItem.setTextColor(ContextCompat.getColor(getContext(), R.color.highscore));
|
|
scoreItem.setTextSize(getResources().getDimension(R.dimen.highscore_textsize));
|
|
scoreItem.setTypeface(Typeface.DEFAULT_BOLD);
|
|
topscoreList.addView(scoreItem);
|
|
setBlinking(scoreItem);
|
|
}
|
|
|
|
|
|
if(player.getPlayerInfo().hasAccessToLevel(lvl_id)) {
|
|
if(score != null) { // if we come from the level itself.. show retry!
|
|
startLvlButton.setText("Retry");
|
|
}
|
|
startLvlButton.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
Intent intent = new Intent(getContext(), GamingActivity.class);
|
|
intent.putExtra(GamingActivity.INTENT_EXTRA_SCENARIO, scenario.id);
|
|
getActivity().finish();
|
|
startActivity(intent);
|
|
}
|
|
});
|
|
} else {
|
|
startLvlButton.setVisibility(View.INVISIBLE);
|
|
}
|
|
|
|
if(scenario.isFinalLevel()) {
|
|
nextLvlButton.setVisibility(View.INVISIBLE);
|
|
} else {
|
|
nextLvlButton.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
int next_lvl_id = scenario.getNextLevelId();
|
|
int next_page_id = Scenario.SCENARIOS.indexOf(next_lvl_id);
|
|
((HighscoreActivity)getActivity()).getViewPager().setCurrentItem(next_page_id, true);
|
|
}
|
|
});
|
|
}
|
|
|
|
// textView.setText(highscoreText);
|
|
// textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_LVL_ID)));
|
|
return rootView;
|
|
}
|
|
|
|
public static void setBlinking(View viewToBlink) {
|
|
Animation anim = new AlphaAnimation(1.0f, 0.0f);
|
|
anim.setDuration(500); //You can manage the blinking time with this parameter
|
|
// anim.setStartOffset(20);
|
|
anim.setRepeatMode(Animation.REVERSE);
|
|
anim.setRepeatCount(Animation.INFINITE);
|
|
viewToBlink.startAnimation(anim);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
|
|
* one of the sections/tabs/pages.
|
|
*/
|
|
public class SectionsPagerAdapter extends FragmentPagerAdapter {
|
|
|
|
public SectionsPagerAdapter(FragmentManager fm) {
|
|
super(fm);
|
|
}
|
|
|
|
@Override
|
|
public Fragment getItem(int position) {
|
|
// getItem is called to instantiate the fragment for the given page.
|
|
// Return a HighscoreFragment (defined as a static inner class below).
|
|
// position is the used to get lvl_id.
|
|
int lvl_id = getLevelIdForPosition(position);
|
|
String scoreString = null;
|
|
if(score != null && lvl_id == score.lvl_id) {
|
|
scoreString = score.id.toString();
|
|
}
|
|
return HighscoreFragment.newInstance(lvl_id, scoreString);
|
|
}
|
|
|
|
public int getLevelIdForPosition(int position) {
|
|
return Scenario.SCENARIOS.get(position);
|
|
}
|
|
|
|
@Override
|
|
public int getCount() {
|
|
// Show 3 total pages.
|
|
return Scenario.SCENARIOS.size();
|
|
}
|
|
|
|
@Override
|
|
public CharSequence getPageTitle(int position) {
|
|
switch (position) {
|
|
case 0:
|
|
return "SECTION 1";
|
|
case 1:
|
|
return "SECTION 2";
|
|
case 2:
|
|
return "SECTION 3";
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* A little hack: http://stackoverflow.com/a/14777003
|
|
*/
|
|
public ViewPager getViewPager() {
|
|
if (null == mViewPager) {
|
|
mViewPager = (ViewPager) findViewById(R.id.container);
|
|
}
|
|
return mViewPager;
|
|
}
|
|
|
|
}
|