1 /* |
|
2 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game |
|
3 * Copyright (c) 2011-2012 Richard Deurwaarder <xeli@xelification.com> |
|
4 * |
|
5 * This program is free software; you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation; version 2 of the License |
|
8 * |
|
9 * This program is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 * GNU General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU General Public License |
|
15 * along with this program; if not, write to the Free Software |
|
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 */ |
|
18 |
|
19 |
|
20 package org.hedgewars.hedgeroid; |
|
21 |
|
22 import java.util.ArrayList; |
|
23 import java.util.HashMap; |
|
24 import java.util.List; |
|
25 |
|
26 import org.hedgewars.hedgeroid.Datastructures.FrontendDataUtils; |
|
27 import org.hedgewars.hedgeroid.Datastructures.Team; |
|
28 import org.hedgewars.hedgeroid.Datastructures.TeamInGame; |
|
29 import org.hedgewars.hedgeroid.Datastructures.TeamIngameAttributes; |
|
30 |
|
31 import android.app.Activity; |
|
32 import android.content.Intent; |
|
33 import android.os.Bundle; |
|
34 import android.view.ContextMenu; |
|
35 import android.view.MenuItem; |
|
36 import android.view.View; |
|
37 import android.view.View.OnClickListener; |
|
38 import android.widget.AdapterView; |
|
39 import android.widget.AdapterView.AdapterContextMenuInfo; |
|
40 import android.widget.AdapterView.OnItemClickListener; |
|
41 import android.widget.ImageButton; |
|
42 import android.widget.ImageView; |
|
43 import android.widget.ListView; |
|
44 import android.widget.SimpleAdapter; |
|
45 import android.widget.SimpleAdapter.ViewBinder; |
|
46 import android.widget.TextView; |
|
47 |
|
48 public class TeamSelectionActivity extends Activity implements Runnable{ |
|
49 private static final int ACTIVITY_TEAMCREATION = 0; |
|
50 |
|
51 public static volatile List<TeamInGame> activityParams; |
|
52 public static volatile List<TeamInGame> activityReturn; |
|
53 |
|
54 private ImageButton addTeam; |
|
55 private ListView availableTeams, selectedTeams; |
|
56 private List<HashMap<String, Object>> availableTeamsList, selectedTeamsList; |
|
57 private TextView txtInfo; |
|
58 |
|
59 public void onCreate(Bundle savedInstanceState){ |
|
60 super.onCreate(savedInstanceState); |
|
61 |
|
62 setContentView(R.layout.team_selector); |
|
63 |
|
64 addTeam = (ImageButton) findViewById(R.id.btnAdd); |
|
65 txtInfo = (TextView) findViewById(R.id.txtInfo); |
|
66 selectedTeams = (ListView) findViewById(R.id.selectedTeams); |
|
67 availableTeams = (ListView) findViewById(R.id.availableTeams); |
|
68 addTeam.setOnClickListener(addTeamClicker); |
|
69 |
|
70 availableTeamsList = new ArrayList<HashMap<String, Object>>(); |
|
71 SimpleAdapter adapter = new SimpleAdapter(this, availableTeamsList, R.layout.team_selection_entry_simple, new String[]{"txt", "img"}, new int[]{R.id.txtName, R.id.imgDifficulty}); |
|
72 availableTeams.setAdapter(adapter); |
|
73 availableTeams.setOnItemClickListener(availableClicker); |
|
74 registerForContextMenu(availableTeams); |
|
75 |
|
76 selectedTeamsList = new ArrayList<HashMap<String, Object>>(); |
|
77 adapter = new SimpleAdapter(this, selectedTeamsList, R.layout.team_selection_entry, new String[]{"txt", "img", "color", "count"}, new int[]{R.id.txtName, R.id.imgDifficulty, R.id.teamColor, R.id.teamCount}); |
|
78 adapter.setViewBinder(viewBinder); |
|
79 selectedTeams.setAdapter(adapter); |
|
80 selectedTeams.setOnItemClickListener(selectedClicker); |
|
81 |
|
82 txtInfo.setText(String.format(getResources().getString(R.string.teams_info_template), selectedTeams.getChildCount())); |
|
83 |
|
84 new Thread(this).start();//load the teams from xml async |
|
85 } |
|
86 |
|
87 public void run(){ |
|
88 List<Team> teams = FrontendDataUtils.getTeams(this); |
|
89 List<TeamInGame> existingTeams = activityParams; |
|
90 final List<TeamInGame> newSelectedList = new ArrayList<TeamInGame>(); |
|
91 final List<Team> newAvailableList = new ArrayList<Team>(); |
|
92 |
|
93 for(Team team : teams){ |
|
94 boolean added = false; |
|
95 for(TeamInGame existingTeam : existingTeams){ |
|
96 if(team.name.equals(existingTeam.team.name)){ // add to available or add to selected |
|
97 newSelectedList.add(new TeamInGame(team, existingTeam.ingameAttribs)); |
|
98 added = true; |
|
99 break; |
|
100 } |
|
101 } |
|
102 if(!added) newAvailableList.add(team); |
|
103 } |
|
104 |
|
105 this.runOnUiThread(new Runnable(){ |
|
106 public void run() { |
|
107 availableTeamsList.clear(); |
|
108 selectedTeamsList.clear(); |
|
109 for(TeamInGame t : newSelectedList) { |
|
110 selectedTeamsList.add(toMap(t)); |
|
111 } |
|
112 for(Team t : newAvailableList) { |
|
113 availableTeamsList.add(toMap(t)); |
|
114 } |
|
115 ((SimpleAdapter)selectedTeams.getAdapter()).notifyDataSetChanged(); |
|
116 ((SimpleAdapter)availableTeams.getAdapter()).notifyDataSetChanged(); |
|
117 } |
|
118 }); |
|
119 } |
|
120 |
|
121 private ViewBinder viewBinder = new ViewBinder(){ |
|
122 public boolean setViewValue(View view, Object data, String textRepresentation) { |
|
123 switch(view.getId()){ |
|
124 case R.id.teamColor: |
|
125 setTeamColor(view, (Integer)data); |
|
126 return true; |
|
127 case R.id.teamCount: |
|
128 ((ImageView)view).getDrawable().setLevel((Integer)data); |
|
129 return true; |
|
130 default: |
|
131 return false; |
|
132 } |
|
133 } |
|
134 }; |
|
135 |
|
136 public void onActivityResult(int requestCode, int resultCode, Intent data){ |
|
137 if(requestCode == ACTIVITY_TEAMCREATION){ |
|
138 if(resultCode == Activity.RESULT_OK){ |
|
139 updateListViews(); |
|
140 } |
|
141 }else{ |
|
142 super.onActivityResult(requestCode, resultCode, data); |
|
143 } |
|
144 } |
|
145 |
|
146 /* |
|
147 * Updates the list view when TeamCreationActivity is shutdown and the user returns to this point |
|
148 */ |
|
149 private void updateListViews(){ |
|
150 List<Team> teams = FrontendDataUtils.getTeams(this); |
|
151 availableTeamsList.clear(); |
|
152 for(Team team : teams) { |
|
153 availableTeamsList.add(toMap(team)); |
|
154 } |
|
155 |
|
156 ArrayList<HashMap<String, Object>> toBeRemoved = new ArrayList<HashMap<String, Object>>(); |
|
157 ArrayList<HashMap<String, Object>> toBeRemovedFromSelected = new ArrayList<HashMap<String, Object>>(); |
|
158 for(HashMap<String, Object> hashmap : selectedTeamsList){ |
|
159 String name = (String)hashmap.get("txt"); |
|
160 boolean exists = false; |
|
161 for(HashMap<String, Object> hash : availableTeamsList){ |
|
162 if(name.equals((String)hash.get("txt"))){ |
|
163 toBeRemoved.add(hash); |
|
164 exists = true; |
|
165 break; |
|
166 } |
|
167 } |
|
168 if(!exists) { |
|
169 toBeRemovedFromSelected.add(hashmap); |
|
170 } |
|
171 } |
|
172 for(HashMap<String, Object> hash: toBeRemoved) availableTeamsList.remove(hash); |
|
173 for(HashMap<String, Object> hash: toBeRemovedFromSelected) selectedTeamsList.remove(hash); |
|
174 ((SimpleAdapter)selectedTeams.getAdapter()).notifyDataSetChanged(); |
|
175 ((SimpleAdapter)availableTeams.getAdapter()).notifyDataSetChanged(); |
|
176 } |
|
177 |
|
178 private void setTeamColor(View iv, int colorIndex){ |
|
179 iv.setBackgroundColor(0xFF000000 + TeamIngameAttributes.TEAM_COLORS[colorIndex]); |
|
180 } |
|
181 |
|
182 public void onBackPressed(){ |
|
183 returnTeams(); |
|
184 super.onBackPressed(); |
|
185 } |
|
186 |
|
187 private OnClickListener addTeamClicker = new OnClickListener(){ |
|
188 public void onClick(View v) { |
|
189 startActivityForResult(new Intent(TeamSelectionActivity.this, TeamCreatorActivity.class), ACTIVITY_TEAMCREATION); |
|
190 } |
|
191 }; |
|
192 |
|
193 private OnItemClickListener availableClicker = new OnItemClickListener(){ |
|
194 public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) { |
|
195 selectAvailableTeamsItem(position); |
|
196 } |
|
197 }; |
|
198 private OnItemClickListener selectedClicker = new OnItemClickListener(){ |
|
199 public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) { |
|
200 availableTeamsList.add((HashMap<String, Object>) selectedTeamsList.get(position)); |
|
201 selectedTeamsList.remove(position); |
|
202 ((SimpleAdapter)availableTeams.getAdapter()).notifyDataSetChanged(); |
|
203 ((SimpleAdapter)selectedTeams.getAdapter()).notifyDataSetChanged(); |
|
204 |
|
205 txtInfo.setText(String.format(getResources().getString(R.string.teams_info_template), selectedTeamsList.size())); |
|
206 } |
|
207 |
|
208 }; |
|
209 |
|
210 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuinfo){ |
|
211 menu.add(ContextMenu.NONE, 0, ContextMenu.NONE, R.string.select); |
|
212 menu.add(ContextMenu.NONE, 2, ContextMenu.NONE, R.string.edit); |
|
213 menu.add(ContextMenu.NONE, 1, ContextMenu.NONE, R.string.delete); |
|
214 |
|
215 } |
|
216 public boolean onContextItemSelected(MenuItem item){ |
|
217 AdapterView.AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo(); |
|
218 int position = menuInfo.position; |
|
219 Team team = (Team)availableTeamsList.get(position).get("team"); |
|
220 switch(item.getItemId()){ |
|
221 case 0://select |
|
222 selectAvailableTeamsItem(position); |
|
223 return true; |
|
224 case 1://delete |
|
225 Team.getTeamfileByName(getApplicationContext(), team.name).delete(); |
|
226 availableTeamsList.remove(position); |
|
227 ((SimpleAdapter)availableTeams.getAdapter()).notifyDataSetChanged(); |
|
228 return true; |
|
229 case 2://edit |
|
230 Intent i = new Intent(TeamSelectionActivity.this, TeamCreatorActivity.class); |
|
231 i.putExtra(TeamCreatorActivity.PARAMETER_EXISTING_TEAMNAME, team.name); |
|
232 startActivityForResult(i, ACTIVITY_TEAMCREATION); |
|
233 return true; |
|
234 } |
|
235 return false; |
|
236 } |
|
237 |
|
238 private void selectAvailableTeamsItem(int position){ |
|
239 HashMap<String, Object> hash = (HashMap<String, Object>) availableTeamsList.get(position); |
|
240 int[] illegalcolors = new int[selectedTeamsList.size()]; |
|
241 for(int i = 0; i < selectedTeamsList.size(); i++){ |
|
242 illegalcolors[i] = (Integer)selectedTeamsList.get(i).get("color"); |
|
243 } |
|
244 hash.put("color", TeamIngameAttributes.randomColorIndex(illegalcolors)); |
|
245 hash.put("count", TeamIngameAttributes.DEFAULT_HOG_COUNT); |
|
246 |
|
247 selectedTeamsList.add(hash); |
|
248 availableTeamsList.remove(position); |
|
249 ((SimpleAdapter)availableTeams.getAdapter()).notifyDataSetChanged(); |
|
250 ((SimpleAdapter)selectedTeams.getAdapter()).notifyDataSetChanged(); |
|
251 |
|
252 txtInfo.setText(String.format(getResources().getString(R.string.teams_info_template), selectedTeamsList.size())); |
|
253 } |
|
254 |
|
255 private void returnTeams() { |
|
256 List<TeamInGame> result = new ArrayList<TeamInGame>(); |
|
257 for(HashMap<String, Object> item : selectedTeamsList) { |
|
258 result.add(new TeamInGame((Team)item.get("team"), new TeamIngameAttributes("Player", (Integer)item.get("color"), (Integer)item.get("count"), false))); |
|
259 } |
|
260 activityReturn = result; |
|
261 setResult(Activity.RESULT_OK); |
|
262 } |
|
263 |
|
264 private static final int[] botlevelDrawables = new int[] { |
|
265 R.drawable.human, R.drawable.bot5, R.drawable.bot4, R.drawable.bot3, R.drawable.bot2, R.drawable.bot1 |
|
266 }; |
|
267 |
|
268 private static HashMap<String, Object> toMap(Team t) { |
|
269 HashMap<String, Object> map = new HashMap<String, Object>(); |
|
270 map.put("team", t); |
|
271 map.put("txt", t.name); |
|
272 int botlevel = t.hogs.get(0).level; |
|
273 if(botlevel<0 || botlevel>=botlevelDrawables.length) { |
|
274 map.put("img", R.drawable.bot1); |
|
275 } else { |
|
276 map.put("img", botlevelDrawables[botlevel]); |
|
277 } |
|
278 return map; |
|
279 } |
|
280 |
|
281 private static HashMap<String, Object> toMap(TeamInGame t) { |
|
282 HashMap<String, Object> map = toMap(t.team); |
|
283 map.put("color", t.ingameAttribs.colorIndex); |
|
284 map.put("count", t.ingameAttribs.hogCount); |
|
285 return map; |
|
286 } |
|
287 } |
|