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