17 */ |
17 */ |
18 |
18 |
19 |
19 |
20 package org.hedgewars.hedgeroid; |
20 package org.hedgewars.hedgeroid; |
21 |
21 |
22 import java.io.File; |
|
23 import java.util.ArrayList; |
22 import java.util.ArrayList; |
24 import java.util.HashMap; |
23 import java.util.HashMap; |
25 import java.util.List; |
24 import java.util.List; |
26 |
25 |
27 import org.hedgewars.hedgeroid.Datastructures.FrontendDataUtils; |
26 import org.hedgewars.hedgeroid.Datastructures.FrontendDataUtils; |
28 import org.hedgewars.hedgeroid.Datastructures.Team; |
27 import org.hedgewars.hedgeroid.Datastructures.Team; |
29 import org.hedgewars.hedgeroid.Datastructures.TeamFile; |
|
30 import org.hedgewars.hedgeroid.Datastructures.TeamInGame; |
28 import org.hedgewars.hedgeroid.Datastructures.TeamInGame; |
31 import org.hedgewars.hedgeroid.Datastructures.TeamIngameAttributes; |
29 import org.hedgewars.hedgeroid.Datastructures.TeamIngameAttributes; |
32 |
30 |
33 import android.app.Activity; |
31 import android.app.Activity; |
34 import android.content.Context; |
|
35 import android.content.Intent; |
32 import android.content.Intent; |
36 import android.os.Bundle; |
33 import android.os.Bundle; |
37 import android.os.Parcelable; |
|
38 import android.util.Pair; |
|
39 import android.view.ContextMenu; |
34 import android.view.ContextMenu; |
40 import android.view.MenuItem; |
35 import android.view.MenuItem; |
41 import android.view.View; |
36 import android.view.View; |
42 import android.view.View.OnClickListener; |
37 import android.view.View.OnClickListener; |
43 import android.widget.AdapterView; |
38 import android.widget.AdapterView; |
44 import android.widget.AdapterView.AdapterContextMenuInfo; |
39 import android.widget.AdapterView.AdapterContextMenuInfo; |
45 import android.widget.AdapterView.OnItemClickListener; |
40 import android.widget.AdapterView.OnItemClickListener; |
46 import android.widget.ImageButton; |
41 import android.widget.ImageButton; |
47 import android.widget.ImageView; |
42 import android.widget.ImageView; |
48 import android.widget.ListView; |
43 import android.widget.ListView; |
49 import android.widget.RelativeLayout; |
|
50 import android.widget.SimpleAdapter; |
44 import android.widget.SimpleAdapter; |
51 import android.widget.SimpleAdapter.ViewBinder; |
45 import android.widget.SimpleAdapter.ViewBinder; |
52 import android.widget.TextView; |
46 import android.widget.TextView; |
53 |
47 |
54 public class TeamSelectionActivity extends Activity implements Runnable{ |
48 public class TeamSelectionActivity extends Activity implements Runnable{ |
55 private static final int ACTIVITY_TEAMCREATION = 0; |
49 private static final int ACTIVITY_TEAMCREATION = 0; |
56 |
50 |
57 private static volatile List<Pair<TeamFile, TeamIngameAttributes>> activityParams; |
51 public static volatile List<TeamInGame> activityParams; |
58 private static volatile List<Pair<TeamFile, TeamIngameAttributes>> activityReturn; |
52 public static volatile List<TeamInGame> activityReturn; |
59 |
53 |
60 private ImageButton addTeam, back; |
54 private ImageButton addTeam; |
61 private ListView availableTeams, selectedTeams; |
55 private ListView availableTeams, selectedTeams; |
62 private List<HashMap<String, Object>> availableTeamsList, selectedTeamsList; |
56 private List<HashMap<String, Object>> availableTeamsList, selectedTeamsList; |
63 private TextView txtInfo; |
57 private TextView txtInfo; |
64 |
58 |
65 public void onCreate(Bundle savedInstanceState){ |
59 public void onCreate(Bundle savedInstanceState){ |
66 super.onCreate(savedInstanceState); |
60 super.onCreate(savedInstanceState); |
67 |
61 |
68 setContentView(R.layout.team_selector); |
62 setContentView(R.layout.team_selector); |
69 |
63 |
70 addTeam = (ImageButton) findViewById(R.id.btnAdd); |
64 addTeam = (ImageButton) findViewById(R.id.btnAdd); |
71 back = (ImageButton) findViewById(R.id.btnBack); |
|
72 txtInfo = (TextView) findViewById(R.id.txtInfo); |
65 txtInfo = (TextView) findViewById(R.id.txtInfo); |
73 selectedTeams = (ListView) findViewById(R.id.selectedTeams); |
66 selectedTeams = (ListView) findViewById(R.id.selectedTeams); |
74 availableTeams = (ListView) findViewById(R.id.availableTeams); |
67 availableTeams = (ListView) findViewById(R.id.availableTeams); |
75 addTeam.setOnClickListener(addTeamClicker); |
68 addTeam.setOnClickListener(addTeamClicker); |
76 back.setOnClickListener(backClicker); |
|
77 |
69 |
78 availableTeamsList = new ArrayList<HashMap<String, Object>>(); |
70 availableTeamsList = new ArrayList<HashMap<String, Object>>(); |
79 SimpleAdapter adapter = new SimpleAdapter(this, availableTeamsList, R.layout.team_selection_entry_simple, new String[]{"txt", "img"}, new int[]{R.id.txtName, R.id.imgDifficulty}); |
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}); |
80 availableTeams.setAdapter(adapter); |
72 availableTeams.setAdapter(adapter); |
81 availableTeams.setOnItemClickListener(availableClicker); |
73 availableTeams.setOnItemClickListener(availableClicker); |
91 |
83 |
92 new Thread(this).start();//load the teams from xml async |
84 new Thread(this).start();//load the teams from xml async |
93 } |
85 } |
94 |
86 |
95 public void run(){ |
87 public void run(){ |
96 List<HashMap<String, Object>> teamsList = getTeams(this);//teams from xml |
88 List<Team> teams = FrontendDataUtils.getTeams(this); |
97 ArrayList<String> teamsStartGame = getIntent().getStringArrayListExtra("selectedTeamNames"); |
89 List<TeamInGame> existingTeams = activityParams; |
98 |
90 final List<TeamInGame> newSelectedList = new ArrayList<TeamInGame>(); |
99 for(HashMap<String, Object> hashmap : teamsList){ |
91 final List<Team> newAvailableList = new ArrayList<Team>(); |
|
92 |
|
93 for(Team team : teams){ |
100 boolean added = false; |
94 boolean added = false; |
101 for(String teamName : teamsStartGame){ |
95 for(TeamInGame existingTeam : existingTeams){ |
102 if(((Team)hashmap.get("team")).name.equals(teamName)){ // add to available or add to selected |
96 if(team.name.equals(existingTeam.team.name)){ // add to available or add to selected |
103 selectedTeamsList.add(hashmap); |
97 newSelectedList.add(new TeamInGame(team, existingTeam.ingameAttribs)); |
104 added = true; |
98 added = true; |
105 break; |
99 break; |
106 } |
100 } |
107 } |
101 } |
108 if(!added) availableTeamsList.add(hashmap); |
102 if(!added) newAvailableList.add(team); |
109 } |
103 } |
110 |
104 |
111 this.runOnUiThread(new Runnable(){ |
105 this.runOnUiThread(new Runnable(){ |
112 public void run() { |
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 } |
113 ((SimpleAdapter)selectedTeams.getAdapter()).notifyDataSetChanged(); |
115 ((SimpleAdapter)selectedTeams.getAdapter()).notifyDataSetChanged(); |
114 ((SimpleAdapter)availableTeams.getAdapter()).notifyDataSetChanged(); |
116 ((SimpleAdapter)availableTeams.getAdapter()).notifyDataSetChanged(); |
115 } |
117 } |
116 }); |
118 }); |
117 } |
119 } |
143 |
145 |
144 /* |
146 /* |
145 * Updates the list view when TeamCreationActivity is shutdown and the user returns to this point |
147 * Updates the list view when TeamCreationActivity is shutdown and the user returns to this point |
146 */ |
148 */ |
147 private void updateListViews(){ |
149 private void updateListViews(){ |
148 unregisterForContextMenu(availableTeams); |
150 List<Team> teams = FrontendDataUtils.getTeams(this); |
149 availableTeamsList = getTeams(this); |
151 availableTeamsList.clear(); |
|
152 for(Team team : teams) { |
|
153 availableTeamsList.add(toMap(team)); |
|
154 } |
|
155 |
150 ArrayList<HashMap<String, Object>> toBeRemoved = new ArrayList<HashMap<String, Object>>(); |
156 ArrayList<HashMap<String, Object>> toBeRemoved = new ArrayList<HashMap<String, Object>>(); |
|
157 ArrayList<HashMap<String, Object>> toBeRemovedFromSelected = new ArrayList<HashMap<String, Object>>(); |
151 for(HashMap<String, Object> hashmap : selectedTeamsList){ |
158 for(HashMap<String, Object> hashmap : selectedTeamsList){ |
152 String name = (String)hashmap.get("txt"); |
159 String name = (String)hashmap.get("txt"); |
153 |
160 boolean exists = false; |
154 for(HashMap<String, Object> hash : availableTeamsList){ |
161 for(HashMap<String, Object> hash : availableTeamsList){ |
155 if(name.equals((String)hash.get("txt"))){ |
162 if(name.equals((String)hash.get("txt"))){ |
156 toBeRemoved.add(hash); |
163 toBeRemoved.add(hash); |
|
164 exists = true; |
|
165 break; |
157 } |
166 } |
158 } |
167 } |
|
168 if(!exists) { |
|
169 toBeRemovedFromSelected.add(hashmap); |
|
170 } |
159 } |
171 } |
160 for(HashMap<String, Object> hash: toBeRemoved) availableTeamsList.remove(hash); |
172 for(HashMap<String, Object> hash: toBeRemoved) availableTeamsList.remove(hash); |
161 |
173 for(HashMap<String, Object> hash: toBeRemovedFromSelected) selectedTeamsList.remove(hash); |
162 SimpleAdapter adapter = new SimpleAdapter(this, availableTeamsList, R.layout.team_selection_entry, new String[]{"txt", "img"}, new int[]{R.id.txtName, R.id.imgDifficulty}); |
174 ((SimpleAdapter)selectedTeams.getAdapter()).notifyDataSetChanged(); |
163 availableTeams.setAdapter(adapter); |
175 ((SimpleAdapter)availableTeams.getAdapter()).notifyDataSetChanged(); |
164 registerForContextMenu(availableTeams); |
176 } |
165 availableTeams.setOnItemClickListener(availableClicker); |
177 |
166 |
178 private void setTeamColor(View iv, int colorIndex){ |
167 |
179 iv.setBackgroundColor(0xFF000000 + TeamIngameAttributes.TEAM_COLORS[colorIndex]); |
168 } |
|
169 |
|
170 private void setTeamColor(int position, int color){ |
|
171 View iv = ((RelativeLayout)selectedTeams.getChildAt(position)).findViewById(R.id.teamCount); |
|
172 setTeamColor(iv, color); |
|
173 } |
|
174 private void setTeamColor(View iv, int color){ |
|
175 iv.setBackgroundColor(0xFF000000 + color); |
|
176 } |
|
177 |
|
178 private void setTeamHogCount(int position, int count){ |
|
179 ImageView iv = (ImageView)((RelativeLayout)selectedTeams.getChildAt(position)).findViewById(R.id.teamCount); |
|
180 setTeamHogCount(iv, count); |
|
181 } |
180 } |
182 |
181 |
183 private void setTeamHogCount(ImageView iv, int count){ |
182 private void setTeamHogCount(ImageView iv, int count){ |
184 |
183 |
185 switch(count){ |
184 switch(count){ |
258 |
250 |
259 } |
251 } |
260 public boolean onContextItemSelected(MenuItem item){ |
252 public boolean onContextItemSelected(MenuItem item){ |
261 AdapterView.AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo(); |
253 AdapterView.AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo(); |
262 int position = menuInfo.position; |
254 int position = menuInfo.position; |
263 TeamFile teamfile = (TeamFile)availableTeamsList.get(position).get("teamfile"); |
255 Team team = (Team)availableTeamsList.get(position).get("team"); |
264 switch(item.getItemId()){ |
256 switch(item.getItemId()){ |
265 case 0://select |
257 case 0://select |
266 selectAvailableTeamsItem(position); |
258 selectAvailableTeamsItem(position); |
267 return true; |
259 return true; |
268 case 1://delete |
260 case 1://delete |
269 teamfile.file.delete(); |
261 Team.getTeamfileByName(getApplicationContext(), team.name).delete(); |
270 availableTeamsList.remove(position); |
262 availableTeamsList.remove(position); |
271 ((SimpleAdapter)availableTeams.getAdapter()).notifyDataSetChanged(); |
263 ((SimpleAdapter)availableTeams.getAdapter()).notifyDataSetChanged(); |
272 return true; |
264 return true; |
273 case 2://edit |
265 case 2://edit |
274 Intent i = new Intent(TeamSelectionActivity.this, TeamCreatorActivity.class); |
266 Intent i = new Intent(TeamSelectionActivity.this, TeamCreatorActivity.class); |
275 i.putExtra("teamfile", teamfile.file); |
267 i.putExtra(TeamCreatorActivity.PARAMETER_EXISTING_TEAMNAME, team.name); |
276 startActivityForResult(i, ACTIVITY_TEAMCREATION); |
268 startActivityForResult(i, ACTIVITY_TEAMCREATION); |
277 return true; |
269 return true; |
278 } |
270 } |
279 return false; |
271 return false; |
280 } |
272 } |
281 |
273 |
282 private void selectAvailableTeamsItem(int position){ |
274 private void selectAvailableTeamsItem(int position){ |
283 HashMap<String, Object> hash = (HashMap<String, Object>) availableTeamsList.get(position); |
275 HashMap<String, Object> hash = (HashMap<String, Object>) availableTeamsList.get(position); |
284 Team t = (Team)hash.get("team"); |
|
285 int[] illegalcolors = new int[selectedTeamsList.size()]; |
276 int[] illegalcolors = new int[selectedTeamsList.size()]; |
286 for(int i = 0; i < selectedTeamsList.size(); i++){ |
277 for(int i = 0; i < selectedTeamsList.size(); i++){ |
287 illegalcolors[i] = ((Team)selectedTeamsList.get(i).get("team")).color; |
278 illegalcolors[i] = (Integer)selectedTeamsList.get(i).get("color"); |
288 } |
279 } |
289 t.setRandomColor(illegalcolors); |
280 hash.put("color", TeamIngameAttributes.randomColorIndex(illegalcolors)); |
290 hash.put("color", t.color); |
281 hash.put("count", TeamIngameAttributes.DEFAULT_HOG_COUNT); |
291 hash.put("count", t.hogCount); |
|
292 |
282 |
293 selectedTeamsList.add(hash); |
283 selectedTeamsList.add(hash); |
294 availableTeamsList.remove(position); |
284 availableTeamsList.remove(position); |
295 ((SimpleAdapter)availableTeams.getAdapter()).notifyDataSetChanged(); |
285 ((SimpleAdapter)availableTeams.getAdapter()).notifyDataSetChanged(); |
296 ((SimpleAdapter)selectedTeams.getAdapter()).notifyDataSetChanged(); |
286 ((SimpleAdapter)selectedTeams.getAdapter()).notifyDataSetChanged(); |
297 |
287 |
298 txtInfo.setText(String.format(getResources().getString(R.string.teams_info_template), selectedTeamsList.size())); |
288 txtInfo.setText(String.format(getResources().getString(R.string.teams_info_template), selectedTeamsList.size())); |
299 } |
289 } |
300 |
290 |
301 private void returnTeams(){ |
291 private void returnTeams() { |
302 int teamsCount = selectedTeamsList.size(); |
292 List<TeamInGame> result = new ArrayList<TeamInGame>(); |
303 Intent i = new Intent(); |
293 for(HashMap<String, Object> item : selectedTeamsList) { |
304 Parcelable[] teams = new Parcelable[teamsCount]; |
294 result.add(new TeamInGame((Team)item.get("team"), new TeamIngameAttributes("Player", (Integer)item.get("color"), (Integer)item.get("count"), false))); |
305 for(int x = 0 ; x < teamsCount; x++){ |
295 } |
306 teams[x] = (Team)selectedTeamsList.get(x).get("team"); |
296 activityReturn = result; |
307 } |
297 setResult(Activity.RESULT_OK); |
308 i.putExtra("teams", teams); |
|
309 setResult(Activity.RESULT_OK, i); |
|
310 |
|
311 } |
|
312 |
|
313 private static List<HashMap<String, Object>> getTeams(Context c){ |
|
314 List<HashMap<String, Object>> ret = new ArrayList<HashMap<String, Object>>(); |
|
315 List<TeamFile> teamfiles = FrontendDataUtils.getTeamFiles(c); |
|
316 for(TeamFile tf : teamfiles) { |
|
317 ret.add(teamfileToMap(tf)); |
|
318 } |
|
319 return ret; |
|
320 } |
298 } |
321 |
299 |
322 private static final int[] botlevelDrawables = new int[] { |
300 private static final int[] botlevelDrawables = new int[] { |
323 R.drawable.human, R.drawable.bot5, R.drawable.bot4, R.drawable.bot3, R.drawable.bot2, R.drawable.bot1 |
301 R.drawable.human, R.drawable.bot5, R.drawable.bot4, R.drawable.bot3, R.drawable.bot2, R.drawable.bot1 |
324 }; |
302 }; |
325 |
|
326 private static HashMap<String, Object> teamfileToMap(TeamFile tf){ |
|
327 HashMap<String, Object> hashmap = new HashMap<String, Object>(); |
|
328 Team t = tf.team; |
|
329 hashmap.put("team", t); |
|
330 hashmap.put("teamfile", tf); |
|
331 hashmap.put("txt", t.name); |
|
332 hashmap.put("color", t.color); |
|
333 hashmap.put("count", t.hogCount); |
|
334 |
303 |
|
304 private static HashMap<String, Object> toMap(Team t) { |
|
305 HashMap<String, Object> map = new HashMap<String, Object>(); |
|
306 map.put("team", t); |
|
307 map.put("txt", t.name); |
335 int botlevel = t.hogs.get(0).level; |
308 int botlevel = t.hogs.get(0).level; |
336 if(botlevel<0 || botlevel>=botlevelDrawables.length) { |
309 if(botlevel<0 || botlevel>=botlevelDrawables.length) { |
337 hashmap.put("img", R.drawable.bot1); |
310 map.put("img", R.drawable.bot1); |
338 } else { |
311 } else { |
339 hashmap.put("img", botlevelDrawables[botlevel]); |
312 map.put("img", botlevelDrawables[botlevel]); |
340 } |
313 } |
341 return hashmap; |
314 return map; |
342 return null; |
315 } |
|
316 |
|
317 private static HashMap<String, Object> toMap(TeamInGame t) { |
|
318 HashMap<String, Object> map = toMap(t.team); |
|
319 map.put("color", t.ingameAttribs.colorIndex); |
|
320 map.put("count", t.ingameAttribs.hogCount); |
|
321 return map; |
343 } |
322 } |
344 } |
323 } |