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