project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/LobbyActivity.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.NetplayStateFragment.NetplayStateListener;
       
    23 import org.hedgewars.hedgeroid.netplay.Netplay;
       
    24 import org.hedgewars.hedgeroid.netplay.Netplay.State;
       
    25 import org.hedgewars.hedgeroid.util.TextInputDialog;
       
    26 import org.hedgewars.hedgeroid.util.TextInputDialog.TextInputDialogListener;
       
    27 import org.hedgewars.hedgeroid.util.UiUtils;
       
    28 
       
    29 import android.content.Intent;
       
    30 import android.os.Bundle;
       
    31 import android.support.v4.app.FragmentActivity;
       
    32 import android.support.v4.app.FragmentTransaction;
       
    33 import android.view.Menu;
       
    34 import android.view.MenuItem;
       
    35 import android.widget.LinearLayout;
       
    36 import android.widget.TabHost;
       
    37 
       
    38 /**
       
    39  * Activity for the server lobby of a hedgewars server. Allows you to chat, join
       
    40  * and create rooms and interact with a list of players.
       
    41  * 
       
    42  * Most of the functionality is handled by various fragments.
       
    43  */
       
    44 public class LobbyActivity extends FragmentActivity implements TextInputDialogListener, NetplayStateListener {
       
    45 	private static final int DIALOG_CREATE_ROOM = 0;
       
    46 	
       
    47     private TabHost tabHost;
       
    48     private Netplay netplay;
       
    49     
       
    50     @Override
       
    51     protected void onCreate(Bundle icicle) {
       
    52         super.onCreate(icicle);
       
    53         
       
    54         setContentView(R.layout.activity_lobby);
       
    55         ChatFragment chatFragment = (ChatFragment)getSupportFragmentManager().findFragmentById(R.id.chatFragment);
       
    56         chatFragment.setInRoom(false);
       
    57         
       
    58         FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
       
    59         trans.add(new NetplayStateFragment(), "netplayFragment");
       
    60         trans.commit();
       
    61         
       
    62         netplay = Netplay.getAppInstance(getApplicationContext());
       
    63         
       
    64         // Set up a tabbed UI for medium and small screens
       
    65         tabHost = (TabHost)findViewById(android.R.id.tabhost);
       
    66         if(tabHost != null) {
       
    67 	        tabHost.setup();
       
    68 	        tabHost.getTabWidget().setOrientation(LinearLayout.VERTICAL);
       
    69 
       
    70 	        tabHost.addTab(tabHost.newTabSpec("rooms").setIndicator(UiUtils.createVerticalTabIndicator(tabHost, R.string.lobby_tab_rooms, R.drawable.roomlist_ingame)).setContent(R.id.roomListFragment));
       
    71 	        tabHost.addTab(tabHost.newTabSpec("chat").setIndicator(UiUtils.createVerticalTabIndicator(tabHost, R.string.lobby_tab_chat, R.drawable.edit)).setContent(R.id.chatFragment));
       
    72 	        tabHost.addTab(tabHost.newTabSpec("players").setIndicator(UiUtils.createVerticalTabIndicator(tabHost, R.string.lobby_tab_players, R.drawable.human)).setContent(R.id.playerListFragment));
       
    73 	
       
    74 	        if (icicle != null) {
       
    75 	            tabHost.setCurrentTabByTag(icicle.getString("currentTab"));
       
    76 	        }
       
    77         }
       
    78     }
       
    79     
       
    80 	@Override
       
    81 	public boolean onCreateOptionsMenu(Menu menu) {
       
    82 		super.onCreateOptionsMenu(menu);
       
    83 		getMenuInflater().inflate(R.menu.lobby_options, menu);
       
    84 		return true;
       
    85 	}
       
    86 	
       
    87 	@Override
       
    88 	public boolean onOptionsItemSelected(MenuItem item) {
       
    89 		switch(item.getItemId()) {
       
    90 		case R.id.room_create:
       
    91 	        TextInputDialog dialog = new TextInputDialog(DIALOG_CREATE_ROOM, R.string.dialog_create_room_title, 0, R.string.dialog_create_room_hint);
       
    92 	        dialog.show(getSupportFragmentManager(), "create_room_dialog");
       
    93 			return true;
       
    94 		case R.id.disconnect:
       
    95 			netplay.disconnect();
       
    96 			return true;
       
    97 		default:
       
    98 			return super.onOptionsItemSelected(item);
       
    99 		}
       
   100 	}
       
   101 	
       
   102 	@Override
       
   103 	public void onBackPressed() {
       
   104 		netplay.disconnect();
       
   105 	}
       
   106 	
       
   107     @Override
       
   108     protected void onSaveInstanceState(Bundle icicle) {
       
   109         super.onSaveInstanceState(icicle);
       
   110         if(tabHost != null) {
       
   111         	icicle.putString("currentTab", tabHost.getCurrentTabTag());
       
   112         }
       
   113     }
       
   114     
       
   115     public void onTextInputDialogSubmitted(int dialogId, String text) {
       
   116     	if(text != null && text.length()>0) {
       
   117     		netplay.sendCreateRoom(text);
       
   118     	}
       
   119     }
       
   120     
       
   121     public void onTextInputDialogCancelled(int dialogId) {
       
   122     }
       
   123     
       
   124     public void onNetplayStateChanged(State newState) {
       
   125     	switch(newState) {
       
   126     	case CONNECTING:
       
   127     	case NOT_CONNECTED:
       
   128     		finish();
       
   129     		break;
       
   130     	case ROOM:
       
   131     		startActivity(new Intent(getApplicationContext(), NetRoomActivity.class));
       
   132     		break;
       
   133     	case LOBBY:
       
   134     		// Do nothing
       
   135     		break;
       
   136 		default:
       
   137 			throw new IllegalStateException("Unknown connection state: "+newState);
       
   138     	}
       
   139     }
       
   140 }