source: advance-wars/src/com/example/advancewars/Game.java@ 26a9fd6

Last change on this file since 26a9fd6 was 205f525, checked in by dportnoy <devnull@…>, 14 years ago

Added some buttons to the main menu. Added a new package to the project to make gui creation easier.

  • Property mode set to 100644
File size: 4.0 KB
RevLine 
[5d9e7bb]1package com.example.advancewars;
2
3import com.example.advancewars.R;
4import com.example.advancewars.GameView;
5import com.example.advancewars.GameView.DrawingThread;
6import android.app.Activity;
7import android.os.Bundle;
8import android.util.Log;
9import android.view.Menu;
10import android.view.MenuItem;
11import android.view.Window;
12import android.widget.TextView;
13import java.io.*;
14
15public class Game extends Activity {
16 private static final int MENU_EXIT = 1;
17 private static final int MENU_NEW = 2;
18
19 public enum State{BUST, ACTIVE, DOUBLEDOWN};
20
21 /** A handle to the thread that's actually running the animation. */
22 public DrawingThread mThread;
23
24 /** A handle to the View in which the game is running. */
25 private GameView mGameView;
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_EXIT, 0, R.string.menu_exit);
38 return true;
39 }
40
41 /**
42 * Invoked when the user selects an item from the Menu.
43 *
44 * @param item the Menu entry which was selected
45 * @return true if the Menu item was legit (and we consumed it), false
46 * otherwise
47 */
48 @Override
49 public boolean onOptionsItemSelected(MenuItem item) {
50 switch (item.getItemId()) {
51 case MENU_EXIT:
52 this.finish();
53 break;
54 }
55
56 return true;
57 }
58
59 /**
60 * Invoked when the Activity is created.
61 *
62 * @param savedInstanceState a Bundle containing state saved from a previous
63 * execution, or null if this is a new execution
64 */
65 @Override
66 protected void onCreate(Bundle savedInstanceState) {
[205f525]67 Log.w("AdvanceWars", "We're inside onCreate");
[5d9e7bb]68
69 super.onCreate(savedInstanceState);
70
[205f525]71 Log.w("AdvanceWars", "the super constructor was called successfully");
[5d9e7bb]72
73 // turn off the window's title bar
74 requestWindowFeature(Window.FEATURE_NO_TITLE);
75
76 // tell system to use the layout defined in our XML file
77 setContentView(R.layout.main);
78
79 mGameView = (GameView) findViewById(R.id.lunar);
80 mThread = mGameView.getThread();
[205f525]81 mGameView.mGame = this;
[5d9e7bb]82
83 mGameView.setTextView((TextView) findViewById(R.id.text));
84
85 if (savedInstanceState == null) { // we were just launched: set up a new game
[205f525]86 Log.w("AdvanceWars", "SIS is null");
[5d9e7bb]87
88 mThread.setState(AppState.RUNNING);
89 } else {
[205f525]90 Log.w("AdvanceWars", "SIS is nonnull");
[5d9e7bb]91
92 mThread.setState(AppState.RUNNING);
93
94 mGameView.getThread().mGameState = (GameState)savedInstanceState.getSerializable("gameState");
95
96 mGameView.getThread().mPlayerWins = savedInstanceState.getInt("playerWins");
97 mGameView.getThread().mPlayerLosses = savedInstanceState.getInt("playerLosses");
98 }
99 }
100
101 /**
102 * Invoked when the Activity loses user focus.
103 */
104 @Override
105 protected void onPause() {
106 super.onPause();
107 mGameView.getThread().pause(); // pause game when Activity pauses
108 }
109
110 /**
111 * Notification that something is about to happen, to give the Activity a
112 * chance to save state.
113 *
114 * @param outState a Bundle into which this Activity should save its state
115 */
116 @Override
117 protected void onSaveInstanceState(Bundle outState) {
118 outState.putSerializable("gameState", mGameView.getThread().mGameState);
119 outState.putSerializable("playerWins", mGameView.getThread().mPlayerWins);
120 outState.putSerializable("playerLosses", mGameView.getThread().mPlayerLosses);
121
122 super.onSaveInstanceState(outState);
123 Log.w("Blackjack", "onSaveInstanceState called");
124 }
125
126 @Override
127 protected void onStop() {
128 System.exit(1);
129 super.onStop();
130 }
131}
Note: See TracBrowser for help on using the repository browser.