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 | case MENU_EASY:
|
---|
78 | mThread.setDifficulty(DrawingThread.DIFFICULTY_EASY);
|
---|
79 | return true;
|
---|
80 | case MENU_MEDIUM:
|
---|
81 | mThread.setDifficulty(DrawingThread.DIFFICULTY_MEDIUM);
|
---|
82 | return true;
|
---|
83 | case MENU_HARD:
|
---|
84 | mThread.setDifficulty(DrawingThread.DIFFICULTY_HARD);
|
---|
85 | return true;
|
---|
86 | }
|
---|
87 |
|
---|
88 | return false;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Invoked when the Activity is created.
|
---|
93 | *
|
---|
94 | * @param savedInstanceState a Bundle containing state saved from a previous
|
---|
95 | * execution, or null if this is a new execution
|
---|
96 | */
|
---|
97 | @Override
|
---|
98 | protected void onCreate(Bundle savedInstanceState) {
|
---|
99 | super.onCreate(savedInstanceState);
|
---|
100 |
|
---|
101 | // turn off the window's title bar
|
---|
102 | requestWindowFeature(Window.FEATURE_NO_TITLE);
|
---|
103 |
|
---|
104 | // tell system to use the layout defined in our XML file
|
---|
105 | setContentView(R.layout.main);
|
---|
106 |
|
---|
107 | mGameView = (GameView) findViewById(R.id.lunar);
|
---|
108 | mThread = mGameView.getThread();
|
---|
109 |
|
---|
110 | mGameView.setTextView((TextView) findViewById(R.id.text));
|
---|
111 |
|
---|
112 | if (savedInstanceState == null) {
|
---|
113 | // we were just launched: set up a new game
|
---|
114 | mThread.setState(DrawingThread.STATE_READY);
|
---|
115 | Log.w(this.getClass().getName(), "SIS is null");
|
---|
116 | } else {
|
---|
117 | // we are being restored: resume a previous game
|
---|
118 | mThread.restoreState(savedInstanceState);
|
---|
119 | Log.w(this.getClass().getName(), "SIS is nonnull");
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Invoked when the Activity loses user focus.
|
---|
125 | */
|
---|
126 | @Override
|
---|
127 | protected void onPause() {
|
---|
128 | super.onPause();
|
---|
129 | mGameView.getThread().pause(); // pause game when Activity pauses
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Notification that something is about to happen, to give the Activity a
|
---|
134 | * chance to save state.
|
---|
135 | *
|
---|
136 | * @param outState a Bundle into which this Activity should save its state
|
---|
137 | */
|
---|
138 | @Override
|
---|
139 | protected void onSaveInstanceState(Bundle outState) {
|
---|
140 | // just have the View's thread save its state into our Bundle
|
---|
141 | super.onSaveInstanceState(outState);
|
---|
142 | mThread.saveState(outState);
|
---|
143 | Log.w(this.getClass().getName(), "SIS called");
|
---|
144 | }
|
---|
145 | }
|
---|