195 lines
7.5 KiB
Java
195 lines
7.5 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.view.LayoutInflater;
|
|
import android.view.MenuItem;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
|
|
import android.view.WindowManager;
|
|
import android.widget.LinearLayout;
|
|
import android.widget.TextView;
|
|
|
|
public class HighscoreActivity extends AppCompatActivity {
|
|
|
|
/**
|
|
* 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;
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
|
setContentView(R.layout.activity_highscore);
|
|
|
|
player = Player.getInstance(getApplicationContext());
|
|
|
|
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);
|
|
|
|
|
|
// 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";
|
|
|
|
public HighscoreFragment() {
|
|
}
|
|
|
|
/**
|
|
* Returns a new instance of this fragment for the given section
|
|
* number.
|
|
*/
|
|
public static HighscoreFragment newInstance(int lvl_id) {
|
|
HighscoreFragment fragment = new HighscoreFragment();
|
|
Bundle args = new Bundle();
|
|
args.putInt(ARG_LVL_ID, lvl_id);
|
|
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);
|
|
// TextView textView = (TextView) rootView.findViewById(R.id.section_label);
|
|
Player player = Player.getInstance(getContext());
|
|
ScoreList scores = player.getPlayerInfo().getScoresForLevel(getArguments().getInt(ARG_LVL_ID));
|
|
|
|
// String highscoreText = "";
|
|
|
|
ScoreList topscores = scores.getTopN(8); // TODO: only highscores for current lEVEL!!!!!!!
|
|
|
|
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);
|
|
}
|
|
|
|
int i = 0;
|
|
for(Score topscore: topscores) {
|
|
i++;
|
|
String highscoreText = String.format("%1$d. %2$.2f\n", i, topscore.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);
|
|
}
|
|
// textView.setText(highscoreText);
|
|
// textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_LVL_ID)));
|
|
return rootView;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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.
|
|
return HighscoreFragment.newInstance(getLevelIdForPosition(position));
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|