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.mobile; |
|
21 |
|
22 import java.io.File; |
|
23 import java.io.FileNotFoundException; |
|
24 import java.io.FileOutputStream; |
|
25 import java.io.IOException; |
|
26 import java.util.ArrayList; |
|
27 import java.util.HashMap; |
|
28 |
|
29 import org.hedgewars.mobile.EngineProtocol.FrontendDataUtils; |
|
30 import org.hedgewars.mobile.EngineProtocol.Team; |
|
31 |
|
32 import android.app.Activity; |
|
33 import android.graphics.Bitmap; |
|
34 import android.graphics.drawable.Drawable; |
|
35 import android.media.MediaPlayer; |
|
36 import android.os.Bundle; |
|
37 import android.view.View; |
|
38 import android.view.View.OnClickListener; |
|
39 import android.view.View.OnFocusChangeListener; |
|
40 import android.widget.AdapterView; |
|
41 import android.widget.AdapterView.OnItemSelectedListener; |
|
42 import android.widget.ArrayAdapter; |
|
43 import android.widget.EditText; |
|
44 import android.widget.ImageButton; |
|
45 import android.widget.ImageView; |
|
46 import android.widget.LinearLayout; |
|
47 import android.widget.RelativeLayout; |
|
48 import android.widget.ScrollView; |
|
49 import android.widget.SimpleAdapter; |
|
50 import android.widget.Spinner; |
|
51 import android.widget.TextView; |
|
52 import android.widget.Toast; |
|
53 |
|
54 public class TeamCreatorActivity extends Activity { |
|
55 |
|
56 private TextView name; |
|
57 private Spinner difficulty, grave, flag, voice, fort; |
|
58 private ImageView imgFort; |
|
59 private ArrayList<ImageButton> hogDice = new ArrayList<ImageButton>(); |
|
60 private ArrayList<Spinner> hogHat = new ArrayList<Spinner>(); |
|
61 private ArrayList<EditText> hogName = new ArrayList<EditText>(); |
|
62 private ImageButton back, save, voiceButton; |
|
63 private ScrollView scroller; |
|
64 private MediaPlayer mp = null; |
|
65 private boolean settingsChanged = false; |
|
66 private boolean saved = false; |
|
67 |
|
68 public void onCreate(Bundle savedInstanceState) { |
|
69 super.onCreate(savedInstanceState); |
|
70 setContentView(R.layout.team_creation); |
|
71 |
|
72 name = (TextView) findViewById(R.id.txtName); |
|
73 difficulty = (Spinner) findViewById(R.id.spinType); |
|
74 grave = (Spinner) findViewById(R.id.spinGrave); |
|
75 flag = (Spinner) findViewById(R.id.spinFlag); |
|
76 voice = (Spinner) findViewById(R.id.spinVoice); |
|
77 fort = (Spinner) findViewById(R.id.spinFort); |
|
78 |
|
79 imgFort = (ImageView) findViewById(R.id.imgFort); |
|
80 |
|
81 back = (ImageButton) findViewById(R.id.btnBack); |
|
82 save = (ImageButton) findViewById(R.id.btnSave); |
|
83 voiceButton = (ImageButton) findViewById(R.id.btnPlay); |
|
84 |
|
85 scroller = (ScrollView) findViewById(R.id.scroller); |
|
86 |
|
87 save.setOnClickListener(saveClicker); |
|
88 back.setOnClickListener(backClicker); |
|
89 |
|
90 LinearLayout ll = (LinearLayout) findViewById(R.id.HogsContainer); |
|
91 for (int i = 0; i < ll.getChildCount(); i++) { |
|
92 RelativeLayout team_creation_entry = (RelativeLayout) ll |
|
93 .getChildAt(i); |
|
94 |
|
95 hogHat.add((Spinner) team_creation_entry |
|
96 .findViewById(R.id.spinTeam1)); |
|
97 hogDice.add((ImageButton) team_creation_entry |
|
98 .findViewById(R.id.btnTeam1)); |
|
99 hogName.add((EditText) team_creation_entry |
|
100 .findViewById(R.id.txtTeam1)); |
|
101 } |
|
102 ArrayList<HashMap<String, ?>> gravesData = FrontendDataUtils |
|
103 .getGraves(this); |
|
104 SimpleAdapter sa = new SimpleAdapter(this, gravesData, |
|
105 R.layout.spinner_textimg_entry, new String[] { "txt", "img" }, |
|
106 new int[] { R.id.spinner_txt, R.id.spinner_img }); |
|
107 |
|
108 sa.setViewBinder(viewBinder); |
|
109 grave.setAdapter(sa); |
|
110 grave.setOnFocusChangeListener(focusser); |
|
111 |
|
112 ArrayList<HashMap<String, ?>> flagsData = FrontendDataUtils |
|
113 .getFlags(this); |
|
114 sa = new SimpleAdapter(this, flagsData, R.layout.spinner_textimg_entry, |
|
115 new String[] { "txt", "img" }, new int[] { R.id.spinner_txt, |
|
116 R.id.spinner_img }); |
|
117 sa.setViewBinder(viewBinder); |
|
118 flag.setAdapter(sa); |
|
119 flag.setOnFocusChangeListener(focusser); |
|
120 |
|
121 ArrayList<HashMap<String, ?>> typesData = FrontendDataUtils |
|
122 .getTypes(this); |
|
123 sa = new SimpleAdapter(this, typesData, R.layout.spinner_textimg_entry, |
|
124 new String[] { "txt", "img" }, new int[] { R.id.spinner_txt, |
|
125 R.id.spinner_img }); |
|
126 difficulty.setAdapter(sa); |
|
127 difficulty.setOnFocusChangeListener(focusser); |
|
128 |
|
129 ArrayList<HashMap<String, ?>> hatsData = FrontendDataUtils |
|
130 .getHats(this); |
|
131 sa = new SimpleAdapter(this, hatsData, R.layout.spinner_textimg_entry, |
|
132 new String[] { "txt", "img" }, new int[] { R.id.spinner_txt, |
|
133 R.id.spinner_img }); |
|
134 sa.setViewBinder(viewBinder); |
|
135 for (Spinner spin : hogHat) { |
|
136 spin.setAdapter(sa); |
|
137 } |
|
138 |
|
139 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, |
|
140 R.layout.listview_item, FrontendDataUtils.getVoices(this)); |
|
141 voice.setAdapter(adapter); |
|
142 voice.setOnFocusChangeListener(focusser); |
|
143 voiceButton.setOnClickListener(voiceClicker); |
|
144 |
|
145 adapter = new ArrayAdapter<String>(this, R.layout.listview_item, |
|
146 FrontendDataUtils.getForts(this)); |
|
147 fort.setAdapter(adapter); |
|
148 fort.setOnItemSelectedListener(fortSelector); |
|
149 fort.setOnFocusChangeListener(focusser); |
|
150 |
|
151 Team t = this.getIntent().getParcelableExtra("team"); |
|
152 if (t != null) { |
|
153 name.setText(t.name); |
|
154 int position = ((ArrayAdapter<String>) voice.getAdapter()) |
|
155 .getPosition(t.voice); |
|
156 voice.setSelection(position); |
|
157 |
|
158 position = ((ArrayAdapter<String>) fort.getAdapter()) |
|
159 .getPosition(t.fort); |
|
160 fort.setSelection(position); |
|
161 |
|
162 position = 0; |
|
163 for (HashMap<String, ?> hashmap : typesData) { |
|
164 if (hashmap.get("txt").equals(t.levels[0])) { |
|
165 difficulty.setSelection(position); |
|
166 break; |
|
167 } |
|
168 } |
|
169 |
|
170 position = 0; |
|
171 for (HashMap<String, ?> hashmap : gravesData) { |
|
172 if (hashmap.get("txt").equals(t.grave)) { |
|
173 grave.setSelection(position); |
|
174 break; |
|
175 } |
|
176 } |
|
177 |
|
178 position = 0; |
|
179 for (HashMap<String, ?> hashmap : typesData) { |
|
180 if (hashmap.get("txt").equals(t.flag)) { |
|
181 flag.setSelection(position); |
|
182 break; |
|
183 } |
|
184 } |
|
185 |
|
186 for (int i = 0; i < Team.maxNumberOfHogs; i++) { |
|
187 position = 0; |
|
188 for (HashMap<String, ?> hashmap : hatsData) { |
|
189 if (hashmap.get("txt").equals(t.hats[i])) { |
|
190 hogHat.get(i).setSelection(position); |
|
191 } |
|
192 } |
|
193 |
|
194 hogName.get(i).setText(t.hogNames[i]); |
|
195 } |
|
196 } |
|
197 } |
|
198 |
|
199 public void onDestroy() { |
|
200 super.onDestroy(); |
|
201 if (mp != null) { |
|
202 mp.release(); |
|
203 mp = null; |
|
204 } |
|
205 } |
|
206 |
|
207 private OnFocusChangeListener focusser = new OnFocusChangeListener() { |
|
208 public void onFocusChange(View v, boolean hasFocus) { |
|
209 settingsChanged = true; |
|
210 } |
|
211 |
|
212 }; |
|
213 |
|
214 public void onBackPressed() { |
|
215 onFinishing(); |
|
216 super.onBackPressed(); |
|
217 |
|
218 } |
|
219 |
|
220 private OnClickListener backClicker = new OnClickListener() { |
|
221 public void onClick(View v) { |
|
222 onFinishing(); |
|
223 finish(); |
|
224 } |
|
225 }; |
|
226 |
|
227 private void onFinishing() { |
|
228 if (settingsChanged) { |
|
229 setResult(RESULT_OK); |
|
230 } else { |
|
231 setResult(RESULT_CANCELED); |
|
232 } |
|
233 } |
|
234 |
|
235 private OnClickListener saveClicker = new OnClickListener() { |
|
236 public void onClick(View v) { |
|
237 Toast.makeText(TeamCreatorActivity.this, R.string.saved, Toast.LENGTH_SHORT).show(); |
|
238 saved = true; |
|
239 Team team = new Team(); |
|
240 team.name = name.getText().toString(); |
|
241 HashMap<String, Object> hashmap = (HashMap<String, Object>) flag |
|
242 .getSelectedItem(); |
|
243 |
|
244 team.flag = (String) hashmap.get("txt"); |
|
245 team.fort = fort.getSelectedItem().toString(); |
|
246 hashmap = (HashMap<String, Object>) grave.getSelectedItem(); |
|
247 team.grave = hashmap.get("txt").toString(); |
|
248 team.hash = "0"; |
|
249 team.voice = voice.getSelectedItem().toString(); |
|
250 |
|
251 hashmap = ((HashMap<String, Object>) difficulty.getSelectedItem()); |
|
252 String levelString = hashmap.get("txt").toString(); |
|
253 int levelInt; |
|
254 if (levelString.equals(getString(R.string.human))) { |
|
255 levelInt = 0; |
|
256 } else if (levelString.equals(getString(R.string.bot5))) { |
|
257 levelInt = 1; |
|
258 } else if (levelString.equals(getString(R.string.bot4))) { |
|
259 levelInt = 2; |
|
260 } else if (levelString.equals(getString(R.string.bot3))) { |
|
261 levelInt = 3; |
|
262 } else if (levelString.equals(getString(R.string.bot2))) { |
|
263 levelInt = 4; |
|
264 } else { |
|
265 levelInt = 5; |
|
266 } |
|
267 |
|
268 for (int i = 0; i < hogName.size(); i++) { |
|
269 team.hogNames[i] = hogName.get(i).getText().toString(); |
|
270 hashmap = (HashMap<String, Object>) hogHat.get(i) |
|
271 .getSelectedItem(); |
|
272 team.hats[i] = hashmap.get("txt").toString(); |
|
273 team.levels[i] = levelInt; |
|
274 } |
|
275 try { |
|
276 File teamsDir = new File(getFilesDir().getAbsolutePath() + '/' |
|
277 + Team.DIRECTORY_TEAMS); |
|
278 if (!teamsDir.exists()) |
|
279 teamsDir.mkdir(); |
|
280 FileOutputStream fos = new FileOutputStream(String.format( |
|
281 "%s/%s.xml", teamsDir.getAbsolutePath(), team.name)); |
|
282 team.writeToXml(fos); |
|
283 } catch (FileNotFoundException e) { |
|
284 e.printStackTrace(); |
|
285 } |
|
286 } |
|
287 |
|
288 }; |
|
289 |
|
290 private OnItemSelectedListener fortSelector = new OnItemSelectedListener() { |
|
291 @SuppressWarnings("unchecked") |
|
292 public void onItemSelected(AdapterView<?> arg0, View arg1, |
|
293 int position, long arg3) { |
|
294 settingsChanged = true; |
|
295 String fortName = (String) arg0.getAdapter().getItem(position); |
|
296 Drawable fortIconDrawable = Drawable.createFromPath(Utils |
|
297 .getDownloadPath(TeamCreatorActivity.this) |
|
298 + "Forts/" |
|
299 + fortName + "L.png"); |
|
300 imgFort.setImageDrawable(fortIconDrawable); |
|
301 scroller.fullScroll(ScrollView.FOCUS_DOWN);// Scroll the scrollview |
|
302 // to the bottom, work |
|
303 // around for scollview |
|
304 // invalidation (scrolls |
|
305 // back to top) |
|
306 } |
|
307 |
|
308 public void onNothingSelected(AdapterView<?> arg0) { |
|
309 } |
|
310 |
|
311 }; |
|
312 |
|
313 private OnClickListener voiceClicker = new OnClickListener() { |
|
314 public void onClick(View v) { |
|
315 try { |
|
316 File dir = new File(String.format("%sSounds/voices/%s", |
|
317 Utils.getDownloadPath(TeamCreatorActivity.this), |
|
318 voice.getSelectedItem())); |
|
319 String file = ""; |
|
320 File[] dirs = dir.listFiles(); |
|
321 File f = dirs[(int) Math.round(Math.random() * dirs.length)]; |
|
322 if (f.getName().endsWith(".ogg")) |
|
323 file = f.getAbsolutePath(); |
|
324 |
|
325 if (mp == null) |
|
326 mp = new MediaPlayer(); |
|
327 else |
|
328 mp.reset(); |
|
329 mp.setDataSource(file); |
|
330 mp.prepare(); |
|
331 mp.start(); |
|
332 } catch (IllegalArgumentException e) { |
|
333 e.printStackTrace(); |
|
334 } catch (IllegalStateException e) { |
|
335 e.printStackTrace(); |
|
336 } catch (IOException e) { |
|
337 e.printStackTrace(); |
|
338 } |
|
339 } |
|
340 }; |
|
341 |
|
342 private SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() { |
|
343 |
|
344 public boolean setViewValue(View view, Object data, |
|
345 String textRepresentation) { |
|
346 if (view instanceof ImageView && data instanceof Bitmap) { |
|
347 ImageView v = (ImageView) view; |
|
348 v.setImageBitmap((Bitmap) data); |
|
349 return true; |
|
350 } else { |
|
351 return false; |
|
352 } |
|
353 } |
|
354 }; |
|
355 |
|
356 } |
|