41 |
41 |
42 /** |
42 /** |
43 * This activity is used to set up and start a game on the server. |
43 * This activity is used to set up and start a game on the server. |
44 */ |
44 */ |
45 public class NetRoomActivity extends FragmentActivity implements NetplayStateListener, TeamAddDialog.Listener, RoomStateManager.Provider, RunGameListener { |
45 public class NetRoomActivity extends FragmentActivity implements NetplayStateListener, TeamAddDialog.Listener, RoomStateManager.Provider, RunGameListener { |
46 private TabHost tabHost; |
46 private TabHost tabHost; |
47 private Netplay netplay; |
47 private Netplay netplay; |
48 private RoomStateManager stateManager; |
48 private RoomStateManager stateManager; |
49 private Button startButton; |
49 private Button startButton; |
50 |
50 |
51 @Override |
51 @Override |
52 protected void onCreate(Bundle icicle) { |
52 protected void onCreate(Bundle icicle) { |
53 super.onCreate(icicle); |
53 super.onCreate(icicle); |
54 netplay = Netplay.getAppInstance(getApplicationContext()); |
54 netplay = Netplay.getAppInstance(getApplicationContext()); |
55 netplay.registerRunGameListener(this); |
55 netplay.registerRunGameListener(this); |
56 stateManager = netplay.getRoomStateManager(); |
56 stateManager = netplay.getRoomStateManager(); |
57 stateManager.addListener(roomStateChangeListener); |
57 stateManager.addListener(roomStateChangeListener); |
58 |
58 |
59 setContentView(R.layout.activity_netroom); |
59 setContentView(R.layout.activity_netroom); |
60 startButton = (Button)findViewById(R.id.startGame); |
60 startButton = (Button)findViewById(R.id.startGame); |
61 |
61 |
62 ChatFragment chatFragment = (ChatFragment)getSupportFragmentManager().findFragmentById(R.id.chatFragment); |
62 ChatFragment chatFragment = (ChatFragment)getSupportFragmentManager().findFragmentById(R.id.chatFragment); |
63 chatFragment.setInRoom(true); |
63 chatFragment.setInRoom(true); |
64 |
64 |
65 FragmentTransaction trans = getSupportFragmentManager().beginTransaction(); |
65 FragmentTransaction trans = getSupportFragmentManager().beginTransaction(); |
66 trans.add(new NetplayStateFragment(), "netplayFragment"); |
66 trans.add(new NetplayStateFragment(), "netplayFragment"); |
67 trans.commit(); |
67 trans.commit(); |
68 |
68 |
69 startButton.setVisibility(netplay.isChief() ? View.VISIBLE : View.GONE); |
69 startButton.setVisibility(netplay.isChief() ? View.VISIBLE : View.GONE); |
70 startButton.setOnClickListener(startButtonClickListener); |
70 startButton.setOnClickListener(startButtonClickListener); |
71 |
71 |
72 // Set up a tabbed UI for medium and small screens |
72 // Set up a tabbed UI for medium and small screens |
73 tabHost = (TabHost)findViewById(android.R.id.tabhost); |
73 tabHost = (TabHost)findViewById(android.R.id.tabhost); |
74 if(tabHost != null) { |
74 if(tabHost != null) { |
75 tabHost.setup(); |
75 tabHost.setup(); |
76 tabHost.getTabWidget().setOrientation(LinearLayout.VERTICAL); |
76 tabHost.getTabWidget().setOrientation(LinearLayout.VERTICAL); |
77 |
77 |
78 tabHost.addTab(tabHost.newTabSpec("map").setIndicator(UiUtils.createVerticalTabIndicator(tabHost, R.string.room_tab_map, 0)).setContent(R.id.mapFragment)); |
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)); |
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)); |
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)); |
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)); |
82 tabHost.addTab(tabHost.newTabSpec("players").setIndicator(UiUtils.createVerticalTabIndicator(tabHost, R.string.room_tab_players, 0)).setContent(R.id.playerListContainer)); |
83 |
83 |
84 if (icicle != null) { |
84 if (icicle != null) { |
85 tabHost.setCurrentTabByTag(icicle.getString("currentTab")); |
85 tabHost.setCurrentTabByTag(icicle.getString("currentTab")); |
86 } |
86 } |
87 } |
87 } |
88 } |
88 } |
89 |
89 |
90 @Override |
90 @Override |
91 protected void onDestroy() { |
91 protected void onDestroy() { |
92 super.onDestroy(); |
92 super.onDestroy(); |
93 stateManager.removeListener(roomStateChangeListener); |
93 stateManager.removeListener(roomStateChangeListener); |
94 netplay.unregisterRunGameListener(this); |
94 netplay.unregisterRunGameListener(this); |
95 } |
95 } |
96 |
96 |
97 @Override |
97 @Override |
98 public void onBackPressed() { |
98 public void onBackPressed() { |
99 netplay.sendLeaveRoom(null); |
99 netplay.sendLeaveRoom(null); |
100 } |
100 } |
101 |
101 |
102 @Override |
102 @Override |
103 protected void onSaveInstanceState(Bundle icicle) { |
103 protected void onSaveInstanceState(Bundle icicle) { |
104 super.onSaveInstanceState(icicle); |
104 super.onSaveInstanceState(icicle); |
105 if(tabHost != null) { |
105 if(tabHost != null) { |
106 icicle.putString("currentTab", tabHost.getCurrentTabTag()); |
106 icicle.putString("currentTab", tabHost.getCurrentTabTag()); |
107 } |
107 } |
108 } |
108 } |
109 |
109 |
110 public void onNetplayStateChanged(State newState) { |
110 public void onNetplayStateChanged(State newState) { |
111 switch(newState) { |
111 switch(newState) { |
112 case NOT_CONNECTED: |
112 case NOT_CONNECTED: |
113 case CONNECTING: |
113 case CONNECTING: |
114 case LOBBY: |
114 case LOBBY: |
115 finish(); |
115 finish(); |
116 break; |
116 break; |
117 case ROOM: |
117 case ROOM: |
118 // Do nothing |
118 // Do nothing |
119 break; |
119 break; |
120 default: |
120 default: |
121 throw new IllegalStateException("Unknown connection state: "+newState); |
121 throw new IllegalStateException("Unknown connection state: "+newState); |
122 } |
122 } |
123 } |
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 |
124 |
133 private final OnClickListener startButtonClickListener = new OnClickListener() { |
125 public void onTeamAddDialogSubmitted(Team newTeam) { |
134 public void onClick(View v) { |
126 stateManager.requestAddTeam(newTeam, TeamInGame.getUnusedOrRandomColorIndex(stateManager.getTeams().values())); |
135 netplay.sendStartGame(); |
127 } |
136 } |
128 |
137 }; |
129 public RoomStateManager getRoomStateManager() { |
138 |
130 return stateManager; |
139 private final RoomStateManager.Listener roomStateChangeListener = new RoomStateManager.ListenerAdapter() { |
131 } |
140 @Override |
132 |
141 public void onChiefStatusChanged(boolean isChief) { |
133 private final OnClickListener startButtonClickListener = new OnClickListener() { |
142 startButton.setVisibility(isChief ? View.VISIBLE : View.GONE); |
134 public void onClick(View v) { |
143 } |
135 netplay.sendStartGame(); |
144 }; |
136 } |
145 |
137 }; |
146 public void runGame(GameConfig config) { |
138 |
147 SDLActivity.startConfig = config; |
139 private final RoomStateManager.Listener roomStateChangeListener = new RoomStateManager.ListenerAdapter() { |
148 SDLActivity.startNetgame = true; |
140 @Override |
149 startActivity(new Intent(this, SDLActivity.class)); |
141 public void onChiefStatusChanged(boolean isChief) { |
150 } |
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 } |
151 } |