|
1 /* |
|
2 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game |
|
3 * Copyright (c) 2011 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.io.File; |
|
23 import java.util.ArrayList; |
|
24 import java.util.HashMap; |
|
25 |
|
26 import org.hedgewars.hedgeroid.EngineProtocol.FrontendDataUtils; |
|
27 import org.hedgewars.hedgeroid.EngineProtocol.Team; |
|
28 import org.hedgewars.mobile.R; |
|
29 |
|
30 import android.app.Activity; |
|
31 import android.content.Intent; |
|
32 import android.os.Bundle; |
|
33 import android.os.Parcelable; |
|
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.RelativeLayout; |
|
45 import android.widget.SimpleAdapter; |
|
46 import android.widget.SimpleAdapter.ViewBinder; |
|
47 import android.widget.TextView; |
|
48 |
|
49 public class TeamSelectionActivity extends Activity{ |
|
50 |
|
51 private static final int ACTIVITY_TEAMCREATION = 0; |
|
52 |
|
53 private ImageButton addTeam, back; |
|
54 private ListView availableTeams, selectedTeams; |
|
55 private ArrayList<HashMap<String, Object>> availableTeamsList, selectedTeamsList; |
|
56 private TextView txtInfo; |
|
57 |
|
58 public void onCreate(Bundle savedInstanceState){ |
|
59 super.onCreate(savedInstanceState); |
|
60 |
|
61 setContentView(R.layout.team_selector); |
|
62 |
|
63 addTeam = (ImageButton) findViewById(R.id.btnAdd); |
|
64 back = (ImageButton) findViewById(R.id.btnBack); |
|
65 txtInfo = (TextView) findViewById(R.id.txtInfo); |
|
66 |
|
67 addTeam.setOnClickListener(addTeamClicker); |
|
68 back.setOnClickListener(backClicker); |
|
69 |
|
70 availableTeams = (ListView) findViewById(R.id.availableTeams); |
|
71 availableTeamsList = FrontendDataUtils.getTeams(this); |
|
72 SimpleAdapter adapter = new SimpleAdapter(this, availableTeamsList, R.layout.team_selection_entry_simple, new String[]{"txt", "img"}, new int[]{R.id.txtName, R.id.imgDifficulty}); |
|
73 availableTeams.setAdapter(adapter); |
|
74 registerForContextMenu(availableTeams); |
|
75 availableTeams.setOnItemClickListener(availableClicker); |
|
76 |
|
77 selectedTeams = (ListView) findViewById(R.id.selectedTeams); |
|
78 selectedTeamsList = new ArrayList<HashMap<String, Object>>(); |
|
79 ArrayList<HashMap<String, ?>> toBeRemoved = new ArrayList<HashMap<String, ?>>(); |
|
80 ArrayList<Team> teamsStartGame = getIntent().getParcelableArrayListExtra("teams"); |
|
81 for(HashMap<String, Object> hashmap : availableTeamsList){ |
|
82 for(Team t : teamsStartGame){ |
|
83 if(((Team)hashmap.get("team")).equals(t)){ |
|
84 toBeRemoved.add(hashmap); |
|
85 selectedTeamsList.add(FrontendDataUtils.teamToHashMap(t));//create a new hashmap to ensure all variables are entered into the map |
|
86 } |
|
87 } |
|
88 } |
|
89 for(HashMap<String, ?> hashmap : toBeRemoved) availableTeamsList.remove(hashmap); |
|
90 |
|
91 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}); |
|
92 adapter.setViewBinder(viewBinder); |
|
93 selectedTeams.setAdapter(adapter); |
|
94 selectedTeams.setOnItemClickListener(selectedClicker); |
|
95 |
|
96 txtInfo.setText(String.format(getResources().getString(R.string.teams_info_template), selectedTeams.getChildCount())); |
|
97 } |
|
98 |
|
99 private ViewBinder viewBinder = new ViewBinder(){ |
|
100 public boolean setViewValue(View view, Object data, String textRepresentation) { |
|
101 switch(view.getId()){ |
|
102 case R.id.teamColor: |
|
103 setTeamColor(view, (Integer)data); |
|
104 return true; |
|
105 case R.id.teamCount: |
|
106 setTeamHogCount((ImageView)view, (Integer)data); |
|
107 return true; |
|
108 default: |
|
109 return false; |
|
110 } |
|
111 } |
|
112 }; |
|
113 |
|
114 public void onActivityResult(int requestCode, int resultCode, Intent data){ |
|
115 if(requestCode == ACTIVITY_TEAMCREATION){ |
|
116 if(resultCode == Activity.RESULT_OK){ |
|
117 updateListViews(); |
|
118 } |
|
119 }else{ |
|
120 super.onActivityResult(requestCode, resultCode, data); |
|
121 } |
|
122 } |
|
123 |
|
124 private void updateListViews(){ |
|
125 unregisterForContextMenu(availableTeams); |
|
126 availableTeamsList = FrontendDataUtils.getTeams(this); |
|
127 ArrayList<HashMap<String, Object>> toBeRemoved = new ArrayList<HashMap<String, Object>>(); |
|
128 for(HashMap<String, Object> hashmap : selectedTeamsList){ |
|
129 String name = (String)hashmap.get("txt"); |
|
130 |
|
131 for(HashMap<String, Object> hash : availableTeamsList){ |
|
132 if(name.equals((String)hash.get("txt"))){ |
|
133 toBeRemoved.add(hash); |
|
134 } |
|
135 } |
|
136 } |
|
137 for(HashMap<String, Object> hash: toBeRemoved) availableTeamsList.remove(hash); |
|
138 |
|
139 SimpleAdapter adapter = new SimpleAdapter(this, availableTeamsList, R.layout.team_selection_entry, new String[]{"txt", "img"}, new int[]{R.id.txtName, R.id.imgDifficulty}); |
|
140 availableTeams.setAdapter(adapter); |
|
141 registerForContextMenu(availableTeams); |
|
142 availableTeams.setOnItemClickListener(availableClicker); |
|
143 |
|
144 |
|
145 } |
|
146 |
|
147 private void setTeamColor(int position, int color){ |
|
148 View iv = ((RelativeLayout)selectedTeams.getChildAt(position)).findViewById(R.id.teamCount); |
|
149 setTeamColor(iv, color); |
|
150 } |
|
151 private void setTeamColor(View iv, int color){ |
|
152 iv.setBackgroundColor(0xFF000000 + color); |
|
153 } |
|
154 |
|
155 private void setTeamHogCount(int position, int count){ |
|
156 ImageView iv = (ImageView)((RelativeLayout)selectedTeams.getChildAt(position)).findViewById(R.id.teamCount); |
|
157 setTeamHogCount(iv, count); |
|
158 } |
|
159 |
|
160 private void setTeamHogCount(ImageView iv, int count){ |
|
161 |
|
162 switch(count){ |
|
163 case 0: |
|
164 iv.setImageResource(R.drawable.teamcount0); |
|
165 break; |
|
166 case 1: |
|
167 iv.setImageResource(R.drawable.teamcount1); |
|
168 break; |
|
169 case 2: |
|
170 iv.setImageResource(R.drawable.teamcount2); |
|
171 break; |
|
172 case 3: |
|
173 iv.setImageResource(R.drawable.teamcount3); |
|
174 break; |
|
175 case 4: |
|
176 iv.setImageResource(R.drawable.teamcount4); |
|
177 break; |
|
178 case 5: |
|
179 iv.setImageResource(R.drawable.teamcount5); |
|
180 break; |
|
181 case 6: |
|
182 iv.setImageResource(R.drawable.teamcount6); |
|
183 break; |
|
184 case 7: |
|
185 iv.setImageResource(R.drawable.teamcount7); |
|
186 break; |
|
187 case 8: |
|
188 iv.setImageResource(R.drawable.teamcount8); |
|
189 break; |
|
190 case 9: |
|
191 iv.setImageResource(R.drawable.teamcount9); |
|
192 break; |
|
193 } |
|
194 } |
|
195 |
|
196 public void onBackPressed(){ |
|
197 returnTeams(); |
|
198 super.onBackPressed(); |
|
199 } |
|
200 |
|
201 private OnClickListener addTeamClicker = new OnClickListener(){ |
|
202 public void onClick(View v) { |
|
203 startActivityForResult(new Intent(TeamSelectionActivity.this, TeamCreatorActivity.class), ACTIVITY_TEAMCREATION); |
|
204 } |
|
205 }; |
|
206 |
|
207 private OnClickListener backClicker = new OnClickListener(){ |
|
208 public void onClick(View v){ |
|
209 returnTeams(); |
|
210 finish(); |
|
211 } |
|
212 }; |
|
213 |
|
214 private OnItemClickListener availableClicker = new OnItemClickListener(){ |
|
215 public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) { |
|
216 selectAvailableTeamsItem(position); |
|
217 } |
|
218 }; |
|
219 private OnItemClickListener selectedClicker = new OnItemClickListener(){ |
|
220 public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) { |
|
221 availableTeamsList.add((HashMap<String, Object>) selectedTeamsList.get(position)); |
|
222 selectedTeamsList.remove(position); |
|
223 ((SimpleAdapter)availableTeams.getAdapter()).notifyDataSetChanged(); |
|
224 ((SimpleAdapter)selectedTeams.getAdapter()).notifyDataSetChanged(); |
|
225 |
|
226 txtInfo.setText(String.format(getResources().getString(R.string.teams_info_template), selectedTeamsList.size())); |
|
227 } |
|
228 |
|
229 }; |
|
230 |
|
231 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuinfo){ |
|
232 menu.add(ContextMenu.NONE, 0, ContextMenu.NONE, R.string.select); |
|
233 menu.add(ContextMenu.NONE, 2, ContextMenu.NONE, R.string.edit); |
|
234 menu.add(ContextMenu.NONE, 1, ContextMenu.NONE, R.string.delete); |
|
235 |
|
236 } |
|
237 public boolean onContextItemSelected(MenuItem item){ |
|
238 AdapterView.AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo(); |
|
239 int position = menuInfo.position; |
|
240 switch(item.getItemId()){ |
|
241 case 0://select |
|
242 selectAvailableTeamsItem(position); |
|
243 return true; |
|
244 case 1://delete |
|
245 File f = new File(String.format("%s/%s/%s.xml", TeamSelectionActivity.this.getFilesDir(), Team.DIRECTORY_TEAMS, availableTeamsList.get(position).get("txt"))); |
|
246 f.delete(); |
|
247 availableTeamsList.remove(position); |
|
248 ((SimpleAdapter)availableTeams.getAdapter()).notifyDataSetChanged(); |
|
249 return true; |
|
250 case 2://edit |
|
251 Intent i = new Intent(TeamSelectionActivity.this, TeamCreatorActivity.class); |
|
252 Team t = (Team)availableTeamsList.get(position).get("team"); |
|
253 i.putExtra("team", t); |
|
254 startActivityForResult(i, ACTIVITY_TEAMCREATION); |
|
255 return true; |
|
256 } |
|
257 return false; |
|
258 } |
|
259 |
|
260 private void selectAvailableTeamsItem(int position){ |
|
261 HashMap<String, Object> hash = (HashMap<String, Object>) availableTeamsList.get(position); |
|
262 Team t = (Team)hash.get("team"); |
|
263 int[] illegalcolors = new int[selectedTeamsList.size()]; |
|
264 for(int i = 0; i < selectedTeamsList.size(); i++){ |
|
265 illegalcolors[i] = ((Team)selectedTeamsList.get(i).get("team")).color; |
|
266 } |
|
267 t.setRandomColor(illegalcolors); |
|
268 hash.put("color", t.color); |
|
269 hash.put("count", t.hogCount); |
|
270 |
|
271 selectedTeamsList.add(hash); |
|
272 availableTeamsList.remove(position); |
|
273 ((SimpleAdapter)availableTeams.getAdapter()).notifyDataSetChanged(); |
|
274 ((SimpleAdapter)selectedTeams.getAdapter()).notifyDataSetChanged(); |
|
275 |
|
276 txtInfo.setText(String.format(getResources().getString(R.string.teams_info_template), selectedTeamsList.size())); |
|
277 } |
|
278 |
|
279 private void returnTeams(){ |
|
280 int teamsCount = selectedTeamsList.size(); |
|
281 Intent i = new Intent(); |
|
282 Parcelable[] teams = new Parcelable[teamsCount]; |
|
283 for(int x = 0 ; x < teamsCount; x++){ |
|
284 teams[x] = (Team)selectedTeamsList.get(x).get("team"); |
|
285 } |
|
286 i.putExtra("teams", teams); |
|
287 setResult(Activity.RESULT_OK, i); |
|
288 |
|
289 } |
|
290 } |