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