[7f84c20] | 1 | package com.example.helloandroid;
|
---|
| 2 |
|
---|
| 3 | import android.app.Activity;
|
---|
| 4 | import android.os.Bundle;
|
---|
| 5 | import android.util.Log;
|
---|
| 6 | import android.view.Menu;
|
---|
| 7 | import android.view.MenuItem;
|
---|
[5053d90] | 8 | import android.view.MotionEvent;
|
---|
[7f84c20] | 9 | import android.view.Window;
|
---|
| 10 | import android.widget.TextView;
|
---|
| 11 | import com.example.helloandroid.GameView.DrawingThread;
|
---|
| 12 |
|
---|
| 13 | public class Game extends Activity {
|
---|
| 14 | private static final int MENU_EASY = 1;
|
---|
| 15 |
|
---|
| 16 | private static final int MENU_HARD = 2;
|
---|
| 17 |
|
---|
| 18 | private static final int MENU_MEDIUM = 3;
|
---|
| 19 |
|
---|
| 20 | private static final int MENU_PAUSE = 4;
|
---|
| 21 |
|
---|
| 22 | private static final int MENU_RESUME = 5;
|
---|
| 23 |
|
---|
| 24 | private static final int MENU_START = 6;
|
---|
| 25 |
|
---|
| 26 | private static final int MENU_STOP = 7;
|
---|
| 27 |
|
---|
| 28 | /** A handle to the thread that's actually running the animation. */
|
---|
| 29 | private DrawingThread mThread;
|
---|
| 30 |
|
---|
| 31 | /** A handle to the View in which the game is running. */
|
---|
| 32 | private GameView mGameView;
|
---|
| 33 |
|
---|
| 34 | /**
|
---|
| 35 | * Invoked during init to give the Activity a chance to set up its Menu.
|
---|
| 36 | *
|
---|
| 37 | * @param menu the Menu to which entries may be added
|
---|
| 38 | * @return true
|
---|
| 39 | */
|
---|
| 40 | @Override
|
---|
| 41 | public boolean onCreateOptionsMenu(Menu menu) {
|
---|
| 42 | super.onCreateOptionsMenu(menu);
|
---|
| 43 |
|
---|
| 44 | menu.add(0, MENU_START, 0, R.string.menu_start);
|
---|
| 45 | menu.add(0, MENU_STOP, 0, R.string.menu_stop);
|
---|
| 46 | menu.add(0, MENU_PAUSE, 0, R.string.menu_pause);
|
---|
| 47 | menu.add(0, MENU_RESUME, 0, R.string.menu_resume);
|
---|
| 48 | menu.add(0, MENU_EASY, 0, R.string.menu_easy);
|
---|
| 49 | menu.add(0, MENU_MEDIUM, 0, R.string.menu_medium);
|
---|
| 50 | menu.add(0, MENU_HARD, 0, R.string.menu_hard);
|
---|
| 51 |
|
---|
| 52 | return true;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | /**
|
---|
| 56 | * Invoked when the user selects an item from the Menu.
|
---|
| 57 | *
|
---|
| 58 | * @param item the Menu entry which was selected
|
---|
| 59 | * @return true if the Menu item was legit (and we consumed it), false
|
---|
| 60 | * otherwise
|
---|
| 61 | */
|
---|
| 62 | @Override
|
---|
| 63 | public boolean onOptionsItemSelected(MenuItem item) {
|
---|
| 64 | switch (item.getItemId()) {
|
---|
| 65 | case MENU_START:
|
---|
| 66 | mThread.doStart();
|
---|
| 67 | return true;
|
---|
| 68 | case MENU_STOP:
|
---|
| 69 | mThread.setState(DrawingThread.STATE_LOSE,
|
---|
| 70 | getText(R.string.message_stopped));
|
---|
| 71 | return true;
|
---|
| 72 | case MENU_PAUSE:
|
---|
| 73 | mThread.pause();
|
---|
| 74 | return true;
|
---|
| 75 | case MENU_RESUME:
|
---|
| 76 | mThread.unpause();
|
---|
| 77 | return true;
|
---|
| 78 | case MENU_EASY:
|
---|
| 79 | mThread.setDifficulty(DrawingThread.DIFFICULTY_EASY);
|
---|
| 80 | return true;
|
---|
| 81 | case MENU_MEDIUM:
|
---|
| 82 | mThread.setDifficulty(DrawingThread.DIFFICULTY_MEDIUM);
|
---|
| 83 | return true;
|
---|
| 84 | case MENU_HARD:
|
---|
| 85 | mThread.setDifficulty(DrawingThread.DIFFICULTY_HARD);
|
---|
| 86 | return true;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | return false;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | /**
|
---|
| 93 | * Invoked when the Activity is created.
|
---|
| 94 | *
|
---|
| 95 | * @param savedInstanceState a Bundle containing state saved from a previous
|
---|
| 96 | * execution, or null if this is a new execution
|
---|
| 97 | */
|
---|
| 98 | @Override
|
---|
| 99 | protected void onCreate(Bundle savedInstanceState) {
|
---|
| 100 | super.onCreate(savedInstanceState);
|
---|
| 101 |
|
---|
| 102 | // turn off the window's title bar
|
---|
| 103 | requestWindowFeature(Window.FEATURE_NO_TITLE);
|
---|
| 104 |
|
---|
| 105 | // tell system to use the layout defined in our XML file
|
---|
| 106 | setContentView(R.layout.main);
|
---|
| 107 |
|
---|
| 108 | mGameView = (GameView) findViewById(R.id.lunar);
|
---|
| 109 | mThread = mGameView.getThread();
|
---|
| 110 |
|
---|
| 111 | mGameView.setTextView((TextView) findViewById(R.id.text));
|
---|
| 112 |
|
---|
| 113 | if (savedInstanceState == null) {
|
---|
| 114 | // we were just launched: set up a new game
|
---|
[b6a9b5f] | 115 | mThread.setState(DrawingThread.STATE_RUNNING);
|
---|
[7f84c20] | 116 | Log.w(this.getClass().getName(), "SIS is null");
|
---|
| 117 | } else {
|
---|
| 118 | // we are being restored: resume a previous game
|
---|
| 119 | mThread.restoreState(savedInstanceState);
|
---|
| 120 | Log.w(this.getClass().getName(), "SIS is nonnull");
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | /**
|
---|
| 125 | * Invoked when the Activity loses user focus.
|
---|
| 126 | */
|
---|
| 127 | @Override
|
---|
| 128 | protected void onPause() {
|
---|
| 129 | super.onPause();
|
---|
| 130 | mGameView.getThread().pause(); // pause game when Activity pauses
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | /**
|
---|
| 134 | * Notification that something is about to happen, to give the Activity a
|
---|
| 135 | * chance to save state.
|
---|
| 136 | *
|
---|
| 137 | * @param outState a Bundle into which this Activity should save its state
|
---|
| 138 | */
|
---|
| 139 | @Override
|
---|
| 140 | protected void onSaveInstanceState(Bundle outState) {
|
---|
| 141 | // just have the View's thread save its state into our Bundle
|
---|
| 142 | super.onSaveInstanceState(outState);
|
---|
| 143 | mThread.saveState(outState);
|
---|
| 144 | Log.w(this.getClass().getName(), "SIS called");
|
---|
| 145 | }
|
---|
| 146 | }
|
---|