[abe7b3d] | 1 | package com.medievaltech.advancewars;
|
---|
| 2 |
|
---|
[113d7cf] | 3 | import java.io.*;
|
---|
| 4 |
|
---|
[5d9e7bb] | 5 | import android.app.Activity;
|
---|
| 6 | import android.os.Bundle;
|
---|
| 7 | import android.util.Log;
|
---|
[bb2fa26] | 8 | import android.view.ContextMenu;
|
---|
[5d9e7bb] | 9 | import android.view.Menu;
|
---|
[bb2fa26] | 10 | import android.view.MenuInflater;
|
---|
[5d9e7bb] | 11 | import android.view.MenuItem;
|
---|
[bb2fa26] | 12 | import android.view.View;
|
---|
| 13 | import android.view.ContextMenu.ContextMenuInfo;
|
---|
[5d9e7bb] | 14 | import android.view.Window;
|
---|
| 15 |
|
---|
[379005b] | 16 | import com.medievaltech.advancewars.Enum.*;
|
---|
| 17 |
|
---|
[5d9e7bb] | 18 | public class Game extends Activity {
|
---|
[4666fae] | 19 |
|
---|
[5d9e7bb] | 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 |
|
---|
[fea4b77] | 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);
|
---|
[5d9e7bb] | 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) {
|
---|
[fea4b77] | 46 | int i = item.getItemId();
|
---|
| 47 |
|
---|
| 48 | if(i == MenuOption.END_TURN.ordinal()) {
|
---|
[4666fae] | 49 | Static.turn = Turn.ENEMY_TURN;
|
---|
[fea4b77] | 50 | }else if(i == MenuOption.SAVE.ordinal()) {
|
---|
| 51 | try {
|
---|
| 52 | PrintWriter p = new PrintWriter(new FileWriter(android.os.Environment.getExternalStorageDirectory()+"/save.txt"));
|
---|
[41c11dd] | 53 | Static.map.save(p);
|
---|
[fea4b77] | 54 | p.close();
|
---|
| 55 | }catch(IOException ioe) {
|
---|
| 56 | ioe.printStackTrace();
|
---|
| 57 | }
|
---|
| 58 | }else if(i == MenuOption.MAIN.ordinal()) {
|
---|
[4666fae] | 59 | Static.gameState = GameState.MAIN_MENU;
|
---|
[fea4b77] | 60 | }else if(i == MenuOption.EXIT.ordinal()) {
|
---|
| 61 | finish();
|
---|
[5d9e7bb] | 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) {
|
---|
[205f525] | 75 | Log.w("AdvanceWars", "We're inside onCreate");
|
---|
[5d9e7bb] | 76 |
|
---|
| 77 | super.onCreate(savedInstanceState);
|
---|
| 78 |
|
---|
[205f525] | 79 | Log.w("AdvanceWars", "the super constructor was called successfully");
|
---|
[5d9e7bb] | 80 |
|
---|
| 81 | // turn off the window's title bar
|
---|
| 82 | requestWindowFeature(Window.FEATURE_NO_TITLE);
|
---|
[bb2fa26] | 83 |
|
---|
[5d9e7bb] | 84 | // tell system to use the layout defined in our XML file
|
---|
| 85 | setContentView(R.layout.main);
|
---|
[bb2fa26] | 86 |
|
---|
| 87 | /*View v = findViewById(R.id.main_view);
|
---|
| 88 | Log.i("AdvanceWars", "view is null?: " + (v==null));*/
|
---|
| 89 |
|
---|
[4666fae] | 90 | Static.game = this;
|
---|
[5d9e7bb] | 91 |
|
---|
| 92 | if (savedInstanceState == null) { // we were just launched: set up a new game
|
---|
[205f525] | 93 | Log.w("AdvanceWars", "SIS is null");
|
---|
[5d9e7bb] | 94 | } else {
|
---|
[205f525] | 95 | Log.w("AdvanceWars", "SIS is nonnull");
|
---|
[5d9e7bb] | 96 |
|
---|
[4666fae] | 97 | Static.gameState = (GameState)savedInstanceState.getSerializable("gameState");
|
---|
[5d9e7bb] | 98 | }
|
---|
| 99 | }
|
---|
[bb2fa26] | 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 | }
|
---|
[5d9e7bb] | 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) {
|
---|
[4666fae] | 117 | outState.putSerializable("gameState", Static.gameState);
|
---|
[113d7cf] | 118 |
|
---|
[5d9e7bb] | 119 | super.onSaveInstanceState(outState);
|
---|
[113d7cf] | 120 | Log.w("AdvanceWars", "onSaveInstanceState called");
|
---|
[5d9e7bb] | 121 | }
|
---|
| 122 |
|
---|
| 123 | @Override
|
---|
| 124 | protected void onStop() {
|
---|
| 125 | System.exit(1);
|
---|
| 126 | super.onStop();
|
---|
| 127 | }
|
---|
| 128 | } |
---|