source: galcon-client/src/com/example/helloandroid/Game.java@ b87af8a

Last change on this file since b87af8a was 3a0d468, checked in by dportnoy <devnull@…>, 14 years ago

Added code to connect to the game server.

  • Property mode set to 100644
File size: 4.0 KB
Line 
1package com.example.helloandroid;
2
3import android.app.Activity;
4import android.os.Bundle;
5import android.util.Log;
6import android.view.Menu;
7import android.view.MenuItem;
8import android.view.Window;
9import android.widget.TextView;
10import com.example.helloandroid.GameView.DrawingThread;
11
12public class Game extends Activity {
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;
18
19 /** A handle to the thread that's actually running the animation. */
20 public DrawingThread mThread;
21
22 /** A handle to the View in which the game is running. */
23 private GameView mGameView;
24
25 private ClientThread client;
26
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);
41 menu.add(0, MENU_CONNECT, 0, R.string.menu_connect);
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:
60 mThread.setState(DrawingThread.STATE_LOSE, getText(R.string.message_stopped));
61 return true;
62 case MENU_PAUSE:
63 mThread.pause();
64 return true;
65 case MENU_RESUME:
66 mThread.unpause();
67 return true;
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;
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
102 mThread.setState(DrawingThread.STATE_RUNNING);
103 Log.w("Galcon", "SIS is null");
104 } else {
105 Log.w("Galcon", "SIS is nonnull");
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);
128 Log.w("Galcon", "SIS called");
129 }
130}
Note: See TracBrowser for help on using the repository browser.