project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/NetRoomActivity.java
branchhedgeroid
changeset 7857 2bc61f8841a1
parent 7586 33924ff4af50
child 10017 de822cd3df3a
equal deleted inserted replaced
7855:ddcdedd3330b 7857:2bc61f8841a1
       
     1 /*
       
     2  * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
       
     3  * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or
       
     6  * modify it under the terms of the GNU General Public License
       
     7  * as published by the Free Software Foundation; either version 2
       
     8  * of the License, or (at your option) any later version.
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program; if not, write to the Free Software
       
    17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
       
    18  */
       
    19 
       
    20 package org.hedgewars.hedgeroid;
       
    21 
       
    22 import org.hedgewars.hedgeroid.R;
       
    23 import org.hedgewars.hedgeroid.Datastructures.GameConfig;
       
    24 import org.hedgewars.hedgeroid.Datastructures.Team;
       
    25 import org.hedgewars.hedgeroid.Datastructures.TeamInGame;
       
    26 import org.hedgewars.hedgeroid.NetplayStateFragment.NetplayStateListener;
       
    27 import org.hedgewars.hedgeroid.netplay.Netplay;
       
    28 import org.hedgewars.hedgeroid.netplay.RunGameListener;
       
    29 import org.hedgewars.hedgeroid.netplay.Netplay.State;
       
    30 import org.hedgewars.hedgeroid.util.UiUtils;
       
    31 
       
    32 import android.content.Intent;
       
    33 import android.os.Bundle;
       
    34 import android.support.v4.app.FragmentActivity;
       
    35 import android.support.v4.app.FragmentTransaction;
       
    36 import android.view.View;
       
    37 import android.view.View.OnClickListener;
       
    38 import android.widget.Button;
       
    39 import android.widget.LinearLayout;
       
    40 import android.widget.TabHost;
       
    41 
       
    42 /**
       
    43  * This activity is used to set up and start a game on the server.
       
    44  */
       
    45 public class NetRoomActivity extends FragmentActivity implements NetplayStateListener, TeamAddDialog.Listener, RoomStateManager.Provider, RunGameListener {
       
    46 	private TabHost tabHost;
       
    47 	private Netplay netplay;
       
    48 	private RoomStateManager stateManager;
       
    49 	private Button startButton;
       
    50 	
       
    51     @Override
       
    52     protected void onCreate(Bundle icicle) {
       
    53         super.onCreate(icicle);
       
    54         netplay = Netplay.getAppInstance(getApplicationContext());
       
    55         netplay.registerRunGameListener(this);
       
    56         stateManager = netplay.getRoomStateManager();
       
    57         stateManager.addListener(roomStateChangeListener);
       
    58         
       
    59         setContentView(R.layout.activity_netroom);
       
    60         startButton = (Button)findViewById(R.id.startGame);
       
    61         
       
    62         ChatFragment chatFragment = (ChatFragment)getSupportFragmentManager().findFragmentById(R.id.chatFragment);
       
    63         chatFragment.setInRoom(true);
       
    64         
       
    65         FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
       
    66         trans.add(new NetplayStateFragment(), "netplayFragment");
       
    67         trans.commit();
       
    68         
       
    69         startButton.setVisibility(netplay.isChief() ? View.VISIBLE : View.GONE);
       
    70         startButton.setOnClickListener(startButtonClickListener);
       
    71         
       
    72         // Set up a tabbed UI for medium and small screens
       
    73         tabHost = (TabHost)findViewById(android.R.id.tabhost);
       
    74         if(tabHost != null) {
       
    75 	        tabHost.setup();
       
    76 	        tabHost.getTabWidget().setOrientation(LinearLayout.VERTICAL);
       
    77 
       
    78 	        tabHost.addTab(tabHost.newTabSpec("map").setIndicator(UiUtils.createVerticalTabIndicator(tabHost, R.string.room_tab_map, 0)).setContent(R.id.mapFragment));
       
    79 	        tabHost.addTab(tabHost.newTabSpec("settings").setIndicator(UiUtils.createVerticalTabIndicator(tabHost, R.string.room_tab_settings, 0)).setContent(R.id.settingsFragment));
       
    80 	        tabHost.addTab(tabHost.newTabSpec("teams").setIndicator(UiUtils.createVerticalTabIndicator(tabHost, R.string.room_tab_teams, 0)).setContent(R.id.teamlistFragment));
       
    81 	        tabHost.addTab(tabHost.newTabSpec("chat").setIndicator(UiUtils.createVerticalTabIndicator(tabHost, R.string.room_tab_chat, 0)).setContent(R.id.chatFragment));
       
    82 	        tabHost.addTab(tabHost.newTabSpec("players").setIndicator(UiUtils.createVerticalTabIndicator(tabHost, R.string.room_tab_players, 0)).setContent(R.id.playerListContainer));
       
    83 	        
       
    84 	        if (icicle != null) {
       
    85 	            tabHost.setCurrentTabByTag(icicle.getString("currentTab"));
       
    86 	        }
       
    87         }
       
    88     }
       
    89 
       
    90     @Override
       
    91     protected void onDestroy() {
       
    92     	super.onDestroy();
       
    93     	stateManager.removeListener(roomStateChangeListener);
       
    94     	netplay.unregisterRunGameListener(this);
       
    95     }
       
    96     
       
    97 	@Override
       
    98 	public void onBackPressed() {
       
    99 		netplay.sendLeaveRoom(null);
       
   100 	}
       
   101     
       
   102     @Override
       
   103     protected void onSaveInstanceState(Bundle icicle) {
       
   104         super.onSaveInstanceState(icicle);
       
   105         if(tabHost != null) {
       
   106         	icicle.putString("currentTab", tabHost.getCurrentTabTag());
       
   107         }
       
   108     }
       
   109     
       
   110     public void onNetplayStateChanged(State newState) {
       
   111     	switch(newState) {
       
   112     	case NOT_CONNECTED:
       
   113     	case CONNECTING:
       
   114     	case LOBBY:
       
   115     		finish();
       
   116     		break;
       
   117     	case ROOM:
       
   118     		// Do nothing
       
   119     		break;
       
   120 		default:
       
   121 			throw new IllegalStateException("Unknown connection state: "+newState);
       
   122     	}
       
   123     }
       
   124     
       
   125 	public void onTeamAddDialogSubmitted(Team newTeam) {
       
   126 		stateManager.requestAddTeam(newTeam, TeamInGame.getUnusedOrRandomColorIndex(stateManager.getTeams().values()));
       
   127 	}
       
   128 	
       
   129 	public RoomStateManager getRoomStateManager() {
       
   130 		return stateManager;
       
   131 	}
       
   132 
       
   133 	private final OnClickListener startButtonClickListener = new OnClickListener() {
       
   134 		public void onClick(View v) {
       
   135 			netplay.sendStartGame();
       
   136 		}
       
   137 	};
       
   138 	
       
   139 	private final RoomStateManager.Listener roomStateChangeListener = new RoomStateManager.ListenerAdapter() {
       
   140 		@Override
       
   141 		public void onChiefStatusChanged(boolean isChief) {
       
   142 			startButton.setVisibility(isChief ? View.VISIBLE : View.GONE);
       
   143 		}
       
   144 	};
       
   145 	
       
   146 	public void runGame(GameConfig config) {
       
   147 		SDLActivity.startConfig = config;
       
   148 		SDLActivity.startNetgame = true;
       
   149 		startActivity(new Intent(this, SDLActivity.class));
       
   150 	}
       
   151 }