|
1 /* |
|
2 * 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 java.util.ArrayList; |
|
23 import java.util.Map; |
|
24 import java.util.Set; |
|
25 import java.util.TreeSet; |
|
26 |
|
27 import org.hedgewars.hedgeroid.Datastructures.GameConfig; |
|
28 import org.hedgewars.hedgeroid.Datastructures.Team; |
|
29 import org.hedgewars.hedgeroid.Datastructures.TeamInGame; |
|
30 import org.hedgewars.hedgeroid.netplay.Netplay; |
|
31 import org.hedgewars.hedgeroid.util.UiUtils; |
|
32 |
|
33 import android.content.Intent; |
|
34 import android.os.Bundle; |
|
35 import android.support.v4.app.FragmentActivity; |
|
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 import android.widget.Toast; |
|
42 |
|
43 /** |
|
44 * This activity is used to set up a local game. |
|
45 */ |
|
46 public class LocalRoomActivity extends FragmentActivity implements RoomStateManager.Provider, TeamAddDialog.Listener { |
|
47 private TabHost tabHost; |
|
48 private RoomStateManager stateManager; |
|
49 private Button startButton; |
|
50 |
|
51 @Override |
|
52 protected void onCreate(Bundle icicle) { |
|
53 super.onCreate(icicle); |
|
54 // TODO find a better central location / way to set up the default scheme and weaponset |
|
55 Netplay netplay = Netplay.getAppInstance(getApplicationContext()); |
|
56 stateManager = new LocalRoomStateManager(netplay.defaultScheme, netplay.defaultWeaponset); |
|
57 |
|
58 setContentView(R.layout.activity_localroom); |
|
59 startButton = (Button)findViewById(R.id.startGame); |
|
60 |
|
61 startButton.setOnClickListener(startButtonClickListener); |
|
62 |
|
63 // Set up a tabbed UI for medium and small screens |
|
64 tabHost = (TabHost)findViewById(android.R.id.tabhost); |
|
65 if(tabHost != null) { |
|
66 tabHost.setup(); |
|
67 tabHost.getTabWidget().setOrientation(LinearLayout.VERTICAL); |
|
68 |
|
69 tabHost.addTab(tabHost.newTabSpec("map").setIndicator(UiUtils.createTabIndicator(tabHost, R.string.room_tab_map, 0)).setContent(R.id.mapFragment)); |
|
70 tabHost.addTab(tabHost.newTabSpec("settings").setIndicator(UiUtils.createTabIndicator(tabHost, R.string.room_tab_settings, 0)).setContent(R.id.settingsFragment)); |
|
71 tabHost.addTab(tabHost.newTabSpec("teams").setIndicator(UiUtils.createTabIndicator(tabHost, R.string.room_tab_teams, 0)).setContent(R.id.teamlistContainer)); |
|
72 |
|
73 if (icicle != null) { |
|
74 tabHost.setCurrentTabByTag(icicle.getString("currentTab")); |
|
75 } |
|
76 } |
|
77 } |
|
78 |
|
79 @Override |
|
80 protected void onSaveInstanceState(Bundle icicle) { |
|
81 super.onSaveInstanceState(icicle); |
|
82 if(tabHost != null) { |
|
83 icicle.putString("currentTab", tabHost.getCurrentTabTag()); |
|
84 } |
|
85 } |
|
86 |
|
87 public void onTeamAddDialogSubmitted(Team newTeam) { |
|
88 stateManager.requestAddTeam(newTeam, TeamInGame.getUnusedOrRandomColorIndex(stateManager.getTeams().values())); |
|
89 } |
|
90 |
|
91 public RoomStateManager getRoomStateManager() { |
|
92 return stateManager; |
|
93 } |
|
94 |
|
95 private final OnClickListener startButtonClickListener = new OnClickListener() { |
|
96 public void onClick(View v) { |
|
97 Map<String, TeamInGame> teams = stateManager.getTeams(); |
|
98 Set<Integer> clanColors = new TreeSet<Integer>(); |
|
99 for(TeamInGame t : teams.values()) { |
|
100 clanColors.add(t.ingameAttribs.colorIndex); |
|
101 } |
|
102 if(clanColors.size()<2) { |
|
103 if(tabHost != null) { |
|
104 tabHost.setCurrentTabByTag("teams"); |
|
105 } |
|
106 int errortext = teams.size()<2 ? R.string.not_enough_teams : R.string.not_enough_clans; |
|
107 Toast.makeText(getApplicationContext(), errortext, Toast.LENGTH_SHORT).show(); |
|
108 return; |
|
109 } |
|
110 |
|
111 SDLActivity.startNetgame = false; |
|
112 SDLActivity.startConfig = new GameConfig( |
|
113 stateManager.getGameStyle(), |
|
114 stateManager.getScheme(), |
|
115 stateManager.getMapRecipe(), |
|
116 new ArrayList<TeamInGame>(stateManager.getTeams().values()), |
|
117 stateManager.getWeaponset()); |
|
118 startActivity(new Intent(LocalRoomActivity.this, SDLActivity.class)); |
|
119 } |
|
120 }; |
|
121 } |