PageRenderTime 53ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/src_android/com/gregmcnew/android/pax/IntroActivity.java

https://github.com/gmcnew/pax
Java | 380 lines | 293 code | 68 blank | 19 comment | 33 complexity | a6ddc32cd58cc144ea63878be5bbeab6 MD5 | raw file
  1. package com.gregmcnew.android.pax;
  2. import java.util.Timer;
  3. import java.util.TimerTask;
  4. import javax.microedition.khronos.egl.EGLConfig;
  5. import javax.microedition.khronos.opengles.GL10;
  6. import android.app.AlertDialog;
  7. import android.content.DialogInterface;
  8. import android.content.Intent;
  9. import android.content.SharedPreferences;
  10. import android.content.SharedPreferences.Editor;
  11. import android.content.res.Configuration;
  12. import android.opengl.GLSurfaceView;
  13. import android.opengl.GLU;
  14. import android.os.Bundle;
  15. import android.os.SystemClock;
  16. import android.preference.PreferenceManager;
  17. import android.util.Log;
  18. import android.view.Display;
  19. import android.view.Menu;
  20. import android.view.MotionEvent;
  21. import android.widget.Toast;
  22. public class IntroActivity extends ActivityWithMenu {
  23. @Override
  24. public void onCreate(Bundle savedInstanceState)
  25. {
  26. super.onCreate(savedInstanceState);
  27. mView = new IntroView(this);
  28. mTimer = new Timer();
  29. setContentView(mView);
  30. mView.requestFocus();
  31. mView.setFocusableInTouchMode(true);
  32. reset();
  33. }
  34. private void reset() {
  35. mPlayerOneAI = true;
  36. mPlayerTwoAI = true;
  37. mTimerIsRunning = false;
  38. mMenuOpen = false;
  39. }
  40. @Override
  41. public void onResume() {
  42. super.onResume();
  43. mView.onResume();
  44. }
  45. @Override
  46. public void onPause() {
  47. super.onPause();
  48. mView.onPause();
  49. stopTimer();
  50. reset();
  51. }
  52. @Override
  53. public boolean onPrepareOptionsMenu(Menu menu) {
  54. super.onPrepareOptionsMenu(menu);
  55. menu.findItem(R.id.restart_game).setVisible(false);
  56. stopTimer();
  57. reset();
  58. mMenuOpen = true;
  59. return true;
  60. }
  61. @Override
  62. public void onOptionsMenuClosed(Menu menu) {
  63. super.onOptionsMenuClosed(menu);
  64. mMenuOpen = false;
  65. }
  66. private void startGame()
  67. {
  68. Intent intent = new Intent(this, Pax.class);
  69. intent.putExtra(Pax.PLAYER_ONE_AI, mPlayerOneAI);
  70. intent.putExtra(Pax.PLAYER_TWO_AI, mPlayerTwoAI);
  71. startActivity(intent);
  72. }
  73. private void startTimer() {
  74. if (!mTimerIsRunning) {
  75. int countdownMs = (Constants.SELF_BENCHMARK ? 0 : (Constants.sDebugMode ? 1 : 3)) * 1000;
  76. mTimerIsRunning = true;
  77. mGameStartTime = SystemClock.uptimeMillis() + countdownMs;
  78. mTimer.schedule(new StartGameTask(), countdownMs);
  79. }
  80. }
  81. private void stopTimer() {
  82. // Cancel the countdown.
  83. if (mTimerIsRunning) {
  84. mTimerIsRunning = false;
  85. mTimer.cancel();
  86. mTimer = new Timer();
  87. }
  88. }
  89. @Override
  90. public void onConfigurationChanged(Configuration newConfig){
  91. super.onConfigurationChanged(newConfig);
  92. mView.updateRotation();
  93. }
  94. public void resetPreferences() {
  95. SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
  96. Editor editor = settings.edit();
  97. editor.clear();
  98. editor.commit();
  99. }
  100. private class StartGameTask extends TimerTask {
  101. @Override
  102. public void run() {
  103. startGame();
  104. }
  105. }
  106. private boolean mMenuOpen;
  107. private IntroView mView;
  108. private Timer mTimer;
  109. private long mGameStartTime;
  110. private boolean mTimerIsRunning;
  111. private boolean mPlayerOneAI;
  112. private boolean mPlayerTwoAI;
  113. // Private classes
  114. private class IntroRenderer extends Renderer {
  115. public IntroRenderer(IntroActivity activity) {
  116. super(activity);
  117. mActivity = activity;
  118. mStarField = new StarField();
  119. }
  120. @Override
  121. public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  122. super.onSurfaceCreated(gl, config);
  123. mStarPainter = getPainter(gl, R.drawable.star);
  124. mTitlePainter = getPainter(gl, R.drawable.title);
  125. mCirclePainter = getPainter(gl, R.drawable.circle);
  126. mNumberPainters = new Painter[10];
  127. mNumberPainters[0] = getPainter(gl, R.drawable.char_gold_0);
  128. mNumberPainters[1] = getPainter(gl, R.drawable.char_gold_1);
  129. mNumberPainters[2] = getPainter(gl, R.drawable.char_gold_2);
  130. mNumberPainters[3] = getPainter(gl, R.drawable.char_gold_3);
  131. mNumberPainters[4] = getPainter(gl, R.drawable.char_gold_4);
  132. mNumberPainters[5] = getPainter(gl, R.drawable.char_gold_5);
  133. mNumberPainters[6] = getPainter(gl, R.drawable.char_gold_6);
  134. mNumberPainters[7] = getPainter(gl, R.drawable.char_gold_7);
  135. mNumberPainters[8] = getPainter(gl, R.drawable.char_gold_8);
  136. mNumberPainters[9] = getPainter(gl, R.drawable.char_gold_9);
  137. }
  138. @Override
  139. public void onSurfaceChanged(GL10 gl, int width, int height) {
  140. super.onSurfaceChanged(gl, width, height);
  141. gl.glMatrixMode(GL10.GL_PROJECTION);
  142. gl.glLoadIdentity();
  143. float halfX = width / 2;
  144. float halfY = height / 2;
  145. GLU.gluOrtho2D(gl, -halfX, halfX, -halfY, halfY);
  146. gl.glMatrixMode(GL10.GL_MODELVIEW);
  147. gl.glLoadIdentity();
  148. }
  149. @Override
  150. public void onDrawFrame(GL10 gl) {
  151. long dt = FramerateCounter.tick();
  152. if (!mActivity.mMenuOpen) {
  153. mStarField.update(dt);
  154. }
  155. gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
  156. // This seems to be necessary to avoid camera offset problems when the
  157. // screen is rotated while the game is paused (and rendering is thus
  158. // paused as well). TODO: Figure this out.
  159. gl.glViewport(0, 0, (int) mScreenWidth, (int) mScreenHeight);
  160. float minDimension = mScreenWidth < mScreenHeight ? mScreenWidth : mScreenHeight;
  161. float maxDimension = mScreenWidth > mScreenHeight ? mScreenWidth : mScreenHeight;
  162. float rotationDegrees = -mRotation * 90;
  163. float buttonSize = maxDimension / 8;
  164. float fadeAlpha = 1f;
  165. float countdownAlpha = 0f;
  166. int secondsLeft = 0;
  167. if (mActivity.mTimerIsRunning) {
  168. long msLeft = mActivity.mGameStartTime - SystemClock.uptimeMillis();
  169. if (msLeft > 0) {
  170. countdownAlpha = ((float) (msLeft % 1000)) / 1000;
  171. secondsLeft = (int) Math.ceil(((float) msLeft) / 1000);
  172. if (msLeft < 1000) {
  173. fadeAlpha = countdownAlpha;
  174. }
  175. }
  176. else {
  177. fadeAlpha = 0f;
  178. }
  179. }
  180. drawStars(gl, mStarField, mStarPainter, mScreenWidth, mScreenHeight, fadeAlpha);
  181. float buttonOffset = maxDimension / 3;
  182. float buttonXPos = mActivity.mLandscapeDevice ? buttonOffset : 0;
  183. float buttonYPos = mActivity.mLandscapeDevice ? 0 : buttonOffset;
  184. // Draw the title text
  185. mTitlePainter.draw(gl, 0, 0, minDimension / 2, minDimension / 2, rotationDegrees, fadeAlpha);
  186. float numberSize = maxDimension / 20;
  187. float numberOffset = ((mRotation < 2) ? -1 : 1) * maxDimension / 6;
  188. float numberXPos = (mRotation % 2 == 0) ? 0 : numberOffset;
  189. float numberYPos = (mRotation % 2 != 0) ? 0 : numberOffset;
  190. // Draw the countdown.
  191. mNumberPainters[secondsLeft].draw(gl, numberXPos, numberYPos, numberSize, numberSize, rotationDegrees, countdownAlpha);
  192. // Draw buttons
  193. float[][] c = Painter.TEAM_COLORS;
  194. for (int i = 0; i < 2; i++) {
  195. int rot = (i == 1 ? 1 : -1);
  196. float bs = ((i == 0 && !mPlayerOneAI) || (i == 1 && !mPlayerTwoAI)) ? buttonSize * 1.25f : buttonSize;
  197. mCirclePainter.draw(gl, buttonXPos * rot, buttonYPos * rot, bs, bs, 180 * i, 1f);
  198. mCirclePainter.draw(gl, buttonXPos * rot, buttonYPos * rot, bs * .85f, bs * .85f, 180 * i, 1f, c[i][0], c[i][1], c[i][2]);
  199. // Draw a black circle on top to fade the button out.
  200. mCirclePainter.draw(gl, buttonXPos * rot, buttonYPos * rot, bs, bs, 180 * i, 1 - fadeAlpha, 0, 0, 0);
  201. }
  202. }
  203. private StarField mStarField;
  204. private IntroActivity mActivity;
  205. private Painter mStarPainter;
  206. private Painter mNumberPainters[];
  207. private Painter mTitlePainter;
  208. private Painter mCirclePainter;
  209. }
  210. private class IntroView extends GLSurfaceView {
  211. private IntroView(IntroActivity activity) {
  212. super(activity);
  213. mActivity = activity;
  214. mRenderer = new IntroRenderer(activity);
  215. setEGLConfigChooser(false);
  216. setRenderer(mRenderer);
  217. }
  218. private void updateRotation() {
  219. Display display = mActivity.getWindowManager().getDefaultDisplay();
  220. mRotation = display.getOrientation();
  221. Log.v(Pax.TAG, String.format("display width %d, height %d, orientation %d", display.getWidth(), display.getHeight(), mRotation));
  222. mRenderer.updateRotation(mRotation);
  223. requestRender();
  224. }
  225. @Override
  226. public void onResume() {
  227. super.onResume();
  228. updateRotation();
  229. }
  230. private void clearTutorialSetting(SharedPreferences settings) {
  231. Editor editor = settings.edit();
  232. editor.putBoolean(getString(R.string.tutorial_prompt_setting), false);
  233. editor.commit();
  234. }
  235. private boolean showTutorial() {
  236. final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
  237. boolean tutorialPrompt = settings.getBoolean(getString(R.string.tutorial_prompt_setting), true);
  238. if (!tutorialPrompt) {
  239. return false;
  240. }
  241. AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
  242. builder.setMessage(getString(R.string.tutorial_prompt));
  243. builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
  244. @Override
  245. public void onClick(DialogInterface dialog, int id) {
  246. // TODO: Launch a tutorial activity.
  247. Toast.makeText(mActivity, "Sorry, buddy, you're on your own!", Toast.LENGTH_LONG).show();
  248. dialog.cancel();
  249. clearTutorialSetting(settings);
  250. }
  251. });
  252. builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
  253. @Override
  254. public void onClick(DialogInterface dialog, int id) {
  255. dialog.cancel();
  256. clearTutorialSetting(settings);
  257. }
  258. });
  259. AlertDialog alert = builder.create();
  260. alert.show();
  261. return true;
  262. }
  263. @Override
  264. public boolean onTouchEvent(MotionEvent event) {
  265. // We don't care which pointer was pressed.
  266. int action = event.getAction() & MotionEvent.ACTION_MASK;
  267. if ((action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_POINTER_DOWN)
  268. && !mActivity.mMenuOpen
  269. && !showTutorial()) {
  270. for (int i = 0; i < event.getPointerCount(); i++) {
  271. // Divide the screen into three sections.
  272. // A touch in section 0 means player 2 should be human.
  273. // A touch in section 2 means player 1 should be human.
  274. int xSection = (int) (event.getX(i) * 3 / getWidth());
  275. int ySection = (int) (event.getY(i) * 3 / getHeight());
  276. int section = mActivity.mLandscapeDevice
  277. ? ((mRotation % 2 != 0) ? ySection : 2 - xSection)
  278. : ((mRotation % 2 == 0) ? ySection : xSection);
  279. if (mRotation >= 2) {
  280. section = 2 - section;
  281. }
  282. if (section == 1) {
  283. // The user clicked in the middle of the screen, even
  284. // though that doesn't do anything. They must not know
  285. // what they're doing -- maybe the options menu will
  286. // help.
  287. openOptionsMenu();
  288. }
  289. else {
  290. if (section == 0) {
  291. mActivity.mPlayerTwoAI = Constants.sDebugMode
  292. ? !mActivity.mPlayerTwoAI
  293. : false;
  294. }
  295. else {
  296. mActivity.mPlayerOneAI = Constants.sDebugMode
  297. ? !mActivity.mPlayerOneAI
  298. : false;
  299. }
  300. mActivity.startTimer();
  301. setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
  302. }
  303. }
  304. }
  305. // We consumed the event.
  306. return true;
  307. }
  308. private int mRotation;
  309. private final IntroRenderer mRenderer;
  310. private final IntroActivity mActivity;
  311. }
  312. }