source: advance-wars/src/com/medievaltech/advancewars/Game.java

Last change on this file was bb2fa26, checked in by dportnoy <devnull@…>, 10 years ago

Add a menu for in-game actions.

  • Property mode set to 100644
File size: 3.9 KB
Line 
1package com.medievaltech.advancewars;
2
3import java.io.*;
4
5import android.app.Activity;
6import android.os.Bundle;
7import android.util.Log;
8import android.view.ContextMenu;
9import android.view.Menu;
10import android.view.MenuInflater;
11import android.view.MenuItem;
12import android.view.View;
13import android.view.ContextMenu.ContextMenuInfo;
14import android.view.Window;
15
16import com.medievaltech.advancewars.Enum.*;
17
18public class Game extends Activity {
19
20 /**
21 * Invoked during init to give the Activity a chance to set up its Menu.
22 *
23 * @param menu the Menu to which entries may be added
24 * @return true
25 */
26 @Override
27 public boolean onCreateOptionsMenu(Menu menu) {
28 super.onCreateOptionsMenu(menu);
29
30 menu.add(0, MenuOption.END_TURN.ordinal(), 0, R.string.menu_end_turn);
31 menu.add(0, MenuOption.SAVE.ordinal(), 0, R.string.menu_save);
32 menu.add(0, MenuOption.MAIN.ordinal(), 0, R.string.menu_main);
33 menu.add(0, MenuOption.EXIT.ordinal(), 0, R.string.menu_exit);
34 return true;
35 }
36
37 /**
38 * Invoked when the user selects an item from the Menu.
39 *
40 * @param item the Menu entry which was selected
41 * @return true if the Menu item was legit (and we consumed it), false
42 * otherwise
43 */
44 @Override
45 public boolean onOptionsItemSelected(MenuItem item) {
46 int i = item.getItemId();
47
48 if(i == MenuOption.END_TURN.ordinal()) {
49 Static.turn = Turn.ENEMY_TURN;
50 }else if(i == MenuOption.SAVE.ordinal()) {
51 try {
52 PrintWriter p = new PrintWriter(new FileWriter(android.os.Environment.getExternalStorageDirectory()+"/save.txt"));
53 Static.map.save(p);
54 p.close();
55 }catch(IOException ioe) {
56 ioe.printStackTrace();
57 }
58 }else if(i == MenuOption.MAIN.ordinal()) {
59 Static.gameState = GameState.MAIN_MENU;
60 }else if(i == MenuOption.EXIT.ordinal()) {
61 finish();
62 }
63
64 return true;
65 }
66
67 /**
68 * Invoked when the Activity is created.
69 *
70 * @param savedInstanceState a Bundle containing state saved from a previous
71 * execution, or null if this is a new execution
72 */
73 @Override
74 protected void onCreate(Bundle savedInstanceState) {
75 Log.w("AdvanceWars", "We're inside onCreate");
76
77 super.onCreate(savedInstanceState);
78
79 Log.w("AdvanceWars", "the super constructor was called successfully");
80
81 // turn off the window's title bar
82 requestWindowFeature(Window.FEATURE_NO_TITLE);
83
84 // tell system to use the layout defined in our XML file
85 setContentView(R.layout.main);
86
87 /*View v = findViewById(R.id.main_view);
88 Log.i("AdvanceWars", "view is null?: " + (v==null));*/
89
90 Static.game = this;
91
92 if (savedInstanceState == null) { // we were just launched: set up a new game
93 Log.w("AdvanceWars", "SIS is null");
94 } else {
95 Log.w("AdvanceWars", "SIS is nonnull");
96
97 Static.gameState = (GameState)savedInstanceState.getSerializable("gameState");
98 }
99 }
100
101 @Override
102 public void onCreateContextMenu(ContextMenu menu, View v,
103 ContextMenuInfo menuInfo) {
104 super.onCreateContextMenu(menu, v, menuInfo);
105 MenuInflater inflater = getMenuInflater();
106 inflater.inflate(R.menu.map_menu, menu);
107 }
108
109 /**
110 * Notification that something is about to happen, to give the Activity a
111 * chance to save state.
112 *
113 * @param outState a Bundle into which this Activity should save its state
114 */
115 @Override
116 protected void onSaveInstanceState(Bundle outState) {
117 outState.putSerializable("gameState", Static.gameState);
118
119 super.onSaveInstanceState(outState);
120 Log.w("AdvanceWars", "onSaveInstanceState called");
121 }
122
123 @Override
124 protected void onStop() {
125 System.exit(1);
126 super.onStop();
127 }
128}
Note: See TracBrowser for help on using the repository browser.