29 import java.util.NoSuchElementException; |
29 import java.util.NoSuchElementException; |
30 |
30 |
31 import org.hedgewars.hedgeroid.Datastructures.FrontendDataUtils; |
31 import org.hedgewars.hedgeroid.Datastructures.FrontendDataUtils; |
32 import org.hedgewars.hedgeroid.Datastructures.Hog; |
32 import org.hedgewars.hedgeroid.Datastructures.Hog; |
33 import org.hedgewars.hedgeroid.Datastructures.Team; |
33 import org.hedgewars.hedgeroid.Datastructures.Team; |
|
34 import org.hedgewars.hedgeroid.util.FileUtils; |
34 |
35 |
35 import android.app.Activity; |
36 import android.app.Activity; |
36 import android.graphics.Bitmap; |
37 import android.graphics.Bitmap; |
37 import android.graphics.drawable.Drawable; |
38 import android.graphics.drawable.Drawable; |
38 import android.media.MediaPlayer; |
39 import android.media.MediaPlayer; |
251 } |
252 } |
252 setResult(RESULT_OK); |
253 setResult(RESULT_OK); |
253 super.onBackPressed(); |
254 super.onBackPressed(); |
254 } |
255 } |
255 |
256 |
256 private int getDifficultyLevelFromText(String text) { |
|
257 if (text.equals(getString(R.string.human))) { |
|
258 return 0; |
|
259 } else if (text.equals(getString(R.string.bot5))) { |
|
260 return 1; |
|
261 } else if (text.equals(getString(R.string.bot4))) { |
|
262 return 2; |
|
263 } else if (text.equals(getString(R.string.bot3))) { |
|
264 return 3; |
|
265 } else if (text.equals(getString(R.string.bot2))) { |
|
266 return 4; |
|
267 } else { |
|
268 return 5; |
|
269 } |
|
270 } |
|
271 |
|
272 private String getTxtFromDifficulty(int level) { |
|
273 switch(level) { |
|
274 case 0: return getString(R.string.human); |
|
275 case 1: return getString(R.string.bot5); |
|
276 case 2: return getString(R.string.bot4); |
|
277 case 3: return getString(R.string.bot3); |
|
278 case 4: return getString(R.string.bot2); |
|
279 default: return getString(R.string.bot1); |
|
280 } |
|
281 } |
|
282 |
|
283 private void saveTeam() { |
257 private void saveTeam() { |
284 String teamName = name.getText().toString(); |
258 String teamName = name.getText().toString(); |
285 String teamFlag = (String)((Map<String, Object>) flag.getSelectedItem()).get("txt"); |
259 String teamFlag = (String)((Map<String, Object>) flag.getSelectedItem()).get("txt"); |
286 String teamFort = fort.getSelectedItem().toString(); |
260 String teamFort = fort.getSelectedItem().toString(); |
287 String teamGrave = (String)((Map<String, Object>) grave.getSelectedItem()).get("txt"); |
261 String teamGrave = (String)((Map<String, Object>) grave.getSelectedItem()).get("txt"); |
288 String teamVoice = voice.getSelectedItem().toString(); |
262 String teamVoice = voice.getSelectedItem().toString(); |
289 String levelString = (String)((Map<String, Object>) difficulty.getSelectedItem()).get("txt"); |
263 int levelInt = (Integer)((Map<String, Object>) difficulty.getSelectedItem()).get("level"); |
290 int levelInt = getDifficultyLevelFromText(levelString); |
|
291 |
264 |
292 List<Hog> hogs = new ArrayList<Hog>(); |
265 List<Hog> hogs = new ArrayList<Hog>(); |
293 for (int i = 0; i < hogName.size(); i++) { |
266 for (int i = 0; i < hogName.size(); i++) { |
294 String name = hogName.get(i).getText().toString(); |
267 String name = hogName.get(i).getText().toString(); |
295 String hat = ((Map<String, Object>) hogHat.get(i).getSelectedItem()).get("txt").toString(); |
268 String hat = ((Map<String, Object>) hogHat.get(i).getSelectedItem()).get("txt").toString(); |
305 if(existingTeamName != null) { |
278 if(existingTeamName != null) { |
306 oldFile = Team.getTeamfileByName(this, existingTeamName); |
279 oldFile = Team.getTeamfileByName(this, existingTeamName); |
307 } |
280 } |
308 try { |
281 try { |
309 team.save(newFile); |
282 team.save(newFile); |
310 Toast.makeText(TeamCreatorActivity.this, R.string.saved, Toast.LENGTH_SHORT).show(); |
|
311 // If the team was renamed, delete the old file. |
283 // If the team was renamed, delete the old file. |
312 if(oldFile != null && oldFile.isFile() && !oldFile.equals(newFile)) { |
284 if(oldFile != null && oldFile.isFile() && !oldFile.equals(newFile)) { |
313 oldFile.delete(); |
285 oldFile.delete(); |
314 } |
286 } |
315 existingTeamName = teamName; |
287 existingTeamName = teamName; |
320 |
292 |
321 private OnItemSelectedListener fortSelector = new OnItemSelectedListener() { |
293 private OnItemSelectedListener fortSelector = new OnItemSelectedListener() { |
322 public void onItemSelected(AdapterView<?> arg0, View arg1, |
294 public void onItemSelected(AdapterView<?> arg0, View arg1, |
323 int position, long arg3) { |
295 int position, long arg3) { |
324 String fortName = (String) arg0.getAdapter().getItem(position); |
296 String fortName = (String) arg0.getAdapter().getItem(position); |
325 Drawable fortIconDrawable = Drawable.createFromPath(Utils |
297 Drawable fortIconDrawable = Drawable.createFromPath(FileUtils |
326 .getDataPath(TeamCreatorActivity.this) |
298 .getDataPath(TeamCreatorActivity.this) |
327 + "Forts/" |
299 + "Forts/" |
328 + fortName + "L.png"); |
300 + fortName + "L.png"); |
329 imgFort.setImageDrawable(fortIconDrawable); |
301 imgFort.setImageDrawable(fortIconDrawable); |
330 scroller.fullScroll(ScrollView.FOCUS_DOWN);// Scroll the scrollview |
302 scroller.fullScroll(ScrollView.FOCUS_DOWN);// Scroll the scrollview |
341 |
313 |
342 private OnClickListener voiceClicker = new OnClickListener() { |
314 private OnClickListener voiceClicker = new OnClickListener() { |
343 public void onClick(View v) { |
315 public void onClick(View v) { |
344 try { |
316 try { |
345 File dir = new File(String.format("%sSounds/voices/%s", |
317 File dir = new File(String.format("%sSounds/voices/%s", |
346 Utils.getDataPath(TeamCreatorActivity.this), |
318 FileUtils.getDataPath(TeamCreatorActivity.this), |
347 voice.getSelectedItem())); |
319 voice.getSelectedItem())); |
348 String file = ""; |
320 String file = ""; |
349 File[] dirs = dir.listFiles(); |
321 File[] dirs = dir.listFiles(); |
350 File f = dirs[(int) Math.round(Math.random() * dirs.length)]; |
322 File f = dirs[(int) Math.round(Math.random() * dirs.length)]; |
351 if (f.getName().endsWith(".ogg")) |
323 if (f.getName().endsWith(".ogg")) |
376 |
348 |
377 try { |
349 try { |
378 name.setText(t.name); |
350 name.setText(t.name); |
379 voice.setSelection(findPosition((ArrayAdapter<String>) voice.getAdapter(), t.voice)); |
351 voice.setSelection(findPosition((ArrayAdapter<String>) voice.getAdapter(), t.voice)); |
380 fort.setSelection(findPosition((ArrayAdapter<String>) fort.getAdapter(), t.fort)); |
352 fort.setSelection(findPosition((ArrayAdapter<String>) fort.getAdapter(), t.fort)); |
381 difficulty.setSelection(findPosition(typesData, getTxtFromDifficulty(t.hogs.get(0).level))); // TODO store actual difficulty int in typesData |
353 difficulty.setSelection(findPosition(typesData, "level", Integer.valueOf(t.hogs.get(0).level))); |
382 grave.setSelection(findPosition(gravesData, t.grave)); |
354 grave.setSelection(findPosition(gravesData, "txt", t.grave)); |
383 flag.setSelection(findPosition(flagsData, t.flag)); |
355 flag.setSelection(findPosition(flagsData, "txt", t.flag)); |
384 |
356 |
385 for (int i = 0; i < Team.HEDGEHOGS_PER_TEAM; i++) { |
357 for (int i = 0; i < Team.HEDGEHOGS_PER_TEAM; i++) { |
386 hogHat.get(i).setSelection(findPosition(hatsData, t.hogs.get(i).hat)); |
358 hogHat.get(i).setSelection(findPosition(hatsData, "txt", t.hogs.get(i).hat)); |
387 hogName.get(i).setText(t.hogs.get(i).name); |
359 hogName.get(i).setText(t.hogs.get(i).name); |
388 } |
360 } |
389 } catch(NoSuchElementException e) { |
361 } catch(NoSuchElementException e) { |
390 Toast.makeText(getApplicationContext(), R.string.error_team_attribute_not_found, Toast.LENGTH_LONG).show(); |
362 Toast.makeText(getApplicationContext(), R.string.error_team_attribute_not_found, Toast.LENGTH_LONG).show(); |
391 finish(); |
363 finish(); |
392 } |
364 } |
393 } |
365 } |
394 |
366 |
395 int findPosition(ArrayAdapter<String> adapter, String key) throws NoSuchElementException { |
367 int findPosition(ArrayAdapter<String> adapter, String value) throws NoSuchElementException { |
396 int position = adapter.getPosition(key); |
368 int position = adapter.getPosition(value); |
397 if(position<0) { |
369 if(position<0) { |
398 throw new NoSuchElementException(); |
370 throw new NoSuchElementException(); |
399 } |
371 } |
400 return position; |
372 return position; |
401 } |
373 } |
402 |
374 |
403 int findPosition(List<? extends Map<String, ?>> data, String txtValue) throws NoSuchElementException { |
375 int findPosition(List<? extends Map<String, ?>> data, String key, Object value) throws NoSuchElementException { |
404 int position = 0; |
376 int position = 0; |
405 for (Map<String, ?> map : data) { |
377 for (Map<String, ?> map : data) { |
406 if (map.get("txt").equals(txtValue)) { |
378 if (map.get(key).equals(value)) { |
407 return position; |
379 return position; |
408 } |
380 } |
409 position++; |
381 position++; |
410 } |
382 } |
411 throw new NoSuchElementException(); |
383 throw new NoSuchElementException(); |