|
1 package org.hedgewars.hedgeroid; |
|
2 |
|
3 import org.hedgewars.hedgeroid.R; |
|
4 import org.hedgewars.hedgeroid.Datastructures.GameConfig; |
|
5 import org.hedgewars.hedgeroid.Datastructures.Team; |
|
6 import org.hedgewars.hedgeroid.Datastructures.TeamInGame; |
|
7 import org.hedgewars.hedgeroid.NetplayStateFragment.NetplayStateListener; |
|
8 import org.hedgewars.hedgeroid.netplay.Netplay; |
|
9 import org.hedgewars.hedgeroid.netplay.RunGameListener; |
|
10 import org.hedgewars.hedgeroid.netplay.Netplay.State; |
|
11 import org.hedgewars.hedgeroid.util.UiUtils; |
|
12 |
|
13 import android.content.Intent; |
|
14 import android.os.Bundle; |
|
15 import android.support.v4.app.FragmentActivity; |
|
16 import android.support.v4.app.FragmentTransaction; |
|
17 import android.view.View; |
|
18 import android.view.View.OnClickListener; |
|
19 import android.widget.Button; |
|
20 import android.widget.LinearLayout; |
|
21 import android.widget.TabHost; |
|
22 |
|
23 public class NetRoomActivity extends FragmentActivity implements NetplayStateListener, TeamAddDialog.Listener, RoomStateManager.Provider, RunGameListener { |
|
24 private TabHost tabHost; |
|
25 private Netplay netplay; |
|
26 private RoomStateManager stateManager; |
|
27 private Button startButton; |
|
28 |
|
29 @Override |
|
30 protected void onCreate(Bundle icicle) { |
|
31 super.onCreate(icicle); |
|
32 netplay = Netplay.getAppInstance(getApplicationContext()); |
|
33 netplay.registerRunGameListener(this); |
|
34 stateManager = netplay.getRoomStateManager(); |
|
35 stateManager.addListener(roomStateChangeListener); |
|
36 |
|
37 setContentView(R.layout.activity_netroom); |
|
38 startButton = (Button)findViewById(R.id.startGame); |
|
39 |
|
40 ChatFragment chatFragment = (ChatFragment)getSupportFragmentManager().findFragmentById(R.id.chatFragment); |
|
41 chatFragment.setInRoom(true); |
|
42 |
|
43 FragmentTransaction trans = getSupportFragmentManager().beginTransaction(); |
|
44 trans.add(new NetplayStateFragment(), "netplayFragment"); |
|
45 trans.commit(); |
|
46 |
|
47 startButton.setVisibility(netplay.isChief() ? View.VISIBLE : View.GONE); |
|
48 startButton.setOnClickListener(startButtonClickListener); |
|
49 |
|
50 // Set up a tabbed UI for medium and small screens |
|
51 tabHost = (TabHost)findViewById(android.R.id.tabhost); |
|
52 if(tabHost != null) { |
|
53 tabHost.setup(); |
|
54 tabHost.getTabWidget().setOrientation(LinearLayout.VERTICAL); |
|
55 |
|
56 tabHost.addTab(tabHost.newTabSpec("map").setIndicator(UiUtils.createTabIndicator(tabHost, R.string.room_tab_map, 0)).setContent(R.id.mapFragment)); |
|
57 tabHost.addTab(tabHost.newTabSpec("settings").setIndicator(UiUtils.createTabIndicator(tabHost, R.string.room_tab_settings, 0)).setContent(R.id.settingsFragment)); |
|
58 tabHost.addTab(tabHost.newTabSpec("teams").setIndicator(UiUtils.createTabIndicator(tabHost, R.string.room_tab_teams, 0)).setContent(R.id.teamlistFragment)); |
|
59 tabHost.addTab(tabHost.newTabSpec("chat").setIndicator(UiUtils.createTabIndicator(tabHost, R.string.room_tab_chat, 0)).setContent(R.id.chatFragment)); |
|
60 tabHost.addTab(tabHost.newTabSpec("players").setIndicator(UiUtils.createTabIndicator(tabHost, R.string.room_tab_players, 0)).setContent(R.id.playerListContainer)); |
|
61 |
|
62 if (icicle != null) { |
|
63 tabHost.setCurrentTabByTag(icicle.getString("currentTab")); |
|
64 } |
|
65 } |
|
66 } |
|
67 |
|
68 @Override |
|
69 protected void onDestroy() { |
|
70 super.onDestroy(); |
|
71 stateManager.removeListener(roomStateChangeListener); |
|
72 netplay.unregisterRunGameListener(this); |
|
73 } |
|
74 |
|
75 @Override |
|
76 public void onBackPressed() { |
|
77 netplay.sendLeaveRoom(null); |
|
78 } |
|
79 |
|
80 @Override |
|
81 protected void onSaveInstanceState(Bundle icicle) { |
|
82 super.onSaveInstanceState(icicle); |
|
83 if(tabHost != null) { |
|
84 icicle.putString("currentTab", tabHost.getCurrentTabTag()); |
|
85 } |
|
86 } |
|
87 |
|
88 public void onNetplayStateChanged(State newState) { |
|
89 switch(newState) { |
|
90 case NOT_CONNECTED: |
|
91 case CONNECTING: |
|
92 case LOBBY: |
|
93 finish(); |
|
94 break; |
|
95 case ROOM: |
|
96 // Do nothing |
|
97 break; |
|
98 default: |
|
99 throw new IllegalStateException("Unknown connection state: "+newState); |
|
100 } |
|
101 } |
|
102 |
|
103 public void onTeamAddDialogSubmitted(Team newTeam) { |
|
104 stateManager.requestAddTeam(newTeam, TeamInGame.getUnusedOrRandomColorIndex(stateManager.getTeams().values())); |
|
105 } |
|
106 |
|
107 public RoomStateManager getRoomStateManager() { |
|
108 return stateManager; |
|
109 } |
|
110 |
|
111 private final OnClickListener startButtonClickListener = new OnClickListener() { |
|
112 public void onClick(View v) { |
|
113 netplay.sendStartGame(); |
|
114 } |
|
115 }; |
|
116 |
|
117 private final RoomStateManager.Listener roomStateChangeListener = new RoomStateManager.ListenerAdapter() { |
|
118 @Override |
|
119 public void onChiefStatusChanged(boolean isChief) { |
|
120 startButton.setVisibility(isChief ? View.VISIBLE : View.GONE); |
|
121 } |
|
122 }; |
|
123 |
|
124 public void runGame(GameConfig config) { |
|
125 SDLActivity.startConfig = config; |
|
126 SDLActivity.startNetgame = true; |
|
127 startActivity(new Intent(this, SDLActivity.class)); |
|
128 } |
|
129 } |