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