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

382 lines
15 KiB
Java

package com.rubenvandeven.emotionhero;
import android.app.ProgressDialog;
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 org.ocpsoft.prettytime.PrettyTime;
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";
public final static String INTENT_EXTRA_LVL_ID = "com.rubenvandeven.emotionhero.LVL_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;
Game game;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_highscore);
player = Player.getInstance(getApplicationContext());
// // get score from Intent
// String scoreIdString = getIntent().getStringExtra(INTENT_EXTRA_SCORE_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);
// }
// }
// get game from Intent
String gameIdString = getIntent().getStringExtra(INTENT_EXTRA_GAME_ID);
int providedLvlId = getIntent().getIntExtra(INTENT_EXTRA_LVL_ID, 0);
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 game is set, set current page.
if(game != null) {
int levelpage = Scenario.SCENARIOS.indexOf(game.scenario.id);
mViewPager.setCurrentItem(levelpage);
} else if(providedLvlId != 0) {
int levelpage = Scenario.SCENARIOS.indexOf(providedLvlId);
Log.e("HIGHSCORE", "page: "+levelpage);
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_GAME_ID = "GAME_ID";
public HighscoreFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static HighscoreFragment newInstance(int lvl_id, Long gameId) {
HighscoreFragment fragment = new HighscoreFragment();
Bundle args = new Bundle();
args.putInt(ARG_LVL_ID, lvl_id);
if(gameId != null) {
args.putLong(ARG_GAME_ID, gameId);
}
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);
Game currentGame = null;
long gameId= getArguments().getLong(ARG_GAME_ID);
final Scenario scenario = new Scenario(lvl_id, null);
Player player = Player.getInstance(getContext());
GameOpenHelper gameOpenHelper = player.getGameOpenHelper();
Game[] games = gameOpenHelper.getGamesForScenario(scenario, null);
if(gameId != 0) {
Log.d("Highscore", "Request game: " + gameId);
currentGame = gameOpenHelper.getGameByid(gameId);
}
TextView levelName = (TextView) rootView.findViewById(R.id.levelName);
levelName.setText("\""+scenario.toString()+"\"");
LinearLayout topscoreList = (LinearLayout) rootView.findViewById(R.id.topscoreList);
if(games.length == 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);
}
PrettyTime prettyTime = new PrettyTime();
boolean foundScore = false;
int i = 0;
for(Game game: games) {
final long itemGameId = game.id;
i++;
String highscoreText = String.format("%1$d. %2$.4f", i, game.score+game.bonus); //make line by line elements
TextView scoreItem = new TextView(getContext());
scoreItem.setText(highscoreText);
scoreItem.setTextColor(ContextCompat.getColor(getContext(), R.color.highscore));
scoreItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), ReviewActivity.class);
intent.putExtra(ReviewActivity.INTENT_EXTRA_GAME_ID, itemGameId);
startActivity(intent);
// don't finish.. keep previous button :-)
}
});
float multiplier = i <= 5 ? 1 + (5-i)/5f : 1f;
scoreItem.setTextSize(getResources().getDimension(R.dimen.highscore_textsize) * multiplier);
scoreItem.setTypeface(Typeface.DEFAULT_BOLD);
topscoreList.addView(scoreItem);
TextView dateItem = new TextView(getContext());
dateItem.setText(prettyTime.format(game.time));
dateItem.setTextColor(ContextCompat.getColor(getContext(), R.color.textSecondary));
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llp.setMargins(0, -20, 0, 0); // llp.setMargins(left, top, right, bottom);
dateItem.setLayoutParams(llp);
dateItem.setTypeface(Typeface.DEFAULT);
topscoreList.addView(dateItem);
if(currentGame != null && currentGame.id == game.id) {
foundScore = true;
setBlinking(scoreItem);
topscoreList.requestChildFocus(scoreItem, scoreItem); // scroll to item??
}
}
// we now display all scores :-)
// // 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(currentGame != null) { // if we come from the level itself.. show retry!
startLvlButton.setText("Retry");
}
startLvlButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ProgressDialog dialog = ProgressDialog.show(getContext(), "",
getContext().getResources().getString(R.string.load_game_activity), true);
Intent intent = new Intent(getContext(), GamingActivity.class);
intent.putExtra(GamingActivity.INTENT_EXTRA_SCENARIO, scenario.id);
getActivity().finish();
startActivity(intent);
}
});
} else {
startLvlButton.setVisibility(View.INVISIBLE);
}
nextLvlButton.setVisibility(View.GONE);
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);
Long gameId = null;
if(game != null && lvl_id == game.scenario.id) {
gameId = game.id;
}
return HighscoreFragment.newInstance(lvl_id, gameId);
}
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;
}
}