project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/TeamCreatorActivity.java
changeset 7476 2fb781bbdd51
parent 6842 2e6391f33204
child 7485 0481bd74267c
equal deleted inserted replaced
7473:45b9f25ff611 7476:2fb781bbdd51
    18 
    18 
    19 
    19 
    20 package org.hedgewars.hedgeroid;
    20 package org.hedgewars.hedgeroid;
    21 
    21 
    22 import java.io.File;
    22 import java.io.File;
    23 import java.io.FileNotFoundException;
       
    24 import java.io.FileOutputStream;
       
    25 import java.io.IOException;
    23 import java.io.IOException;
    26 import java.util.ArrayList;
    24 import java.util.ArrayList;
    27 import java.util.HashMap;
    25 import java.util.HashMap;
    28 import java.util.List;
    26 import java.util.List;
       
    27 import java.util.Map;
    29 
    28 
    30 import org.hedgewars.hedgeroid.Datastructures.FrontendDataUtils;
    29 import org.hedgewars.hedgeroid.Datastructures.FrontendDataUtils;
       
    30 import org.hedgewars.hedgeroid.Datastructures.Hog;
    31 import org.hedgewars.hedgeroid.Datastructures.Team;
    31 import org.hedgewars.hedgeroid.Datastructures.Team;
    32 
    32 
    33 import android.app.Activity;
    33 import android.app.Activity;
    34 import android.graphics.Bitmap;
    34 import android.graphics.Bitmap;
    35 import android.graphics.drawable.Drawable;
    35 import android.graphics.drawable.Drawable;
   245 		}
   245 		}
   246 	}
   246 	}
   247 
   247 
   248 	private OnClickListener saveClicker = new OnClickListener() {
   248 	private OnClickListener saveClicker = new OnClickListener() {
   249 		public void onClick(View v) {
   249 		public void onClick(View v) {
   250 			Toast.makeText(TeamCreatorActivity.this, R.string.saved, Toast.LENGTH_SHORT).show();
   250 			String teamName = name.getText().toString();
   251 			saved = true;
   251 			String teamFlag = (String)((Map<String, Object>) flag.getSelectedItem()).get("txt");
   252 			Team team = new Team();
   252 			String teamFort = fort.getSelectedItem().toString();
   253 			team.name = name.getText().toString();
   253 			String teamGrave = (String)((Map<String, Object>) grave.getSelectedItem()).get("txt");
   254 			HashMap<String, Object> hashmap = (HashMap<String, Object>) flag.getSelectedItem();
   254 			String teamVoice = voice.getSelectedItem().toString();
   255 
   255 			String levelString = (String)((Map<String, Object>) difficulty.getSelectedItem()).get("txt");
   256 			team.flag = (String) hashmap.get("txt");
       
   257 			team.fort = fort.getSelectedItem().toString();
       
   258 			hashmap = (HashMap<String, Object>) grave.getSelectedItem();
       
   259 			team.grave = hashmap.get("txt").toString();
       
   260 			team.hash = "0";
       
   261 			team.voice = voice.getSelectedItem().toString();
       
   262 			team.file = fileName;
       
   263 
       
   264 			hashmap = ((HashMap<String, Object>) difficulty.getSelectedItem());
       
   265 			String levelString = hashmap.get("txt").toString();
       
   266 			int levelInt;
   256 			int levelInt;
   267 			if (levelString.equals(getString(R.string.human))) {
   257 			if (levelString.equals(getString(R.string.human))) {
   268 				levelInt = 0;
   258 				levelInt = 0;
   269 			} else if (levelString.equals(getString(R.string.bot5))) {
   259 			} else if (levelString.equals(getString(R.string.bot5))) {
   270 				levelInt = 1;
   260 				levelInt = 1;
   275 			} else if (levelString.equals(getString(R.string.bot2))) {
   265 			} else if (levelString.equals(getString(R.string.bot2))) {
   276 				levelInt = 4;
   266 				levelInt = 4;
   277 			} else {
   267 			} else {
   278 				levelInt = 5;
   268 				levelInt = 5;
   279 			}
   269 			}
   280 
   270 			
       
   271 			List<Hog> hogs = new ArrayList<Hog>();
   281 			for (int i = 0; i < hogName.size(); i++) {
   272 			for (int i = 0; i < hogName.size(); i++) {
   282 				team.hogNames[i] = hogName.get(i).getText().toString();
   273 				String name = hogName.get(i).getText().toString();
   283 				hashmap = (HashMap<String, Object>) hogHat.get(i).getSelectedItem();
   274 				String hat = ((Map<String, Object>) hogHat.get(i).getSelectedItem()).get("txt").toString();
   284 				team.hats[i] = hashmap.get("txt").toString();
   275 				hogs.add(new Hog(name, hat, levelInt));
   285 				team.levels[i] = levelInt;
   276 			}
       
   277 			
       
   278 			Team team = new Team(teamName, teamGrave, teamFlag, teamVoice, teamFort, hogs);
       
   279 			File teamsDir = new File(getFilesDir(), Team.DIRECTORY_TEAMS);
       
   280 			if (!teamsDir.exists()) teamsDir.mkdir();
       
   281 			if(fileName == null){
       
   282 				fileName = createNewFilename(Utils.replaceBadChars(team.name));
   286 			}
   283 			}
   287 			try {
   284 			try {
   288 				File teamsDir = new File(getFilesDir().getAbsolutePath() + '/' + Team.DIRECTORY_TEAMS);
   285 				team.save(new File(teamsDir, fileName));
   289 				if (!teamsDir.exists()) teamsDir.mkdir();
   286 				Toast.makeText(TeamCreatorActivity.this, R.string.saved, Toast.LENGTH_SHORT).show();
   290 				if(team.file == null){
   287 				saved = true;
   291 					team.setFileName(TeamCreatorActivity.this);
   288 			} catch(IOException e) {
   292 				}
   289 				Toast.makeText(getApplicationContext(), R.string.error_save_failed, Toast.LENGTH_SHORT).show();
   293 				FileOutputStream fos = new FileOutputStream(String.format("%s/%s", teamsDir.getAbsolutePath(), team.file));
   290 			}
   294 				team.writeToXml(fos);
   291 		}
   295 			} catch (FileNotFoundException e) {
   292 	};
   296 				e.printStackTrace();
   293 
   297 			}
   294 	private String createNewFilename(String suggestedName){
   298 		}
   295 		File f = new File(getFilesDir(), suggestedName);
   299 
   296 		if(f.exists()){
   300 	};
   297 			return createNewFilename(suggestedName + (int)(Math.random()*10));
   301 
   298 		} else {
       
   299 			return suggestedName + (int)(Math.random()*10);
       
   300 		}
       
   301 	}
       
   302 	
   302 	private OnItemSelectedListener fortSelector = new OnItemSelectedListener() {
   303 	private OnItemSelectedListener fortSelector = new OnItemSelectedListener() {
   303 		public void onItemSelected(AdapterView<?> arg0, View arg1,
   304 		public void onItemSelected(AdapterView<?> arg0, View arg1,
   304 				int position, long arg3) {
   305 				int position, long arg3) {
   305 			settingsChanged = true;
   306 			settingsChanged = true;
   306 			String fortName = (String) arg0.getAdapter().getItem(position);
   307 			String fortName = (String) arg0.getAdapter().getItem(position);
   360 			position = ((ArrayAdapter<String>) fort.getAdapter()).getPosition(t.fort);
   361 			position = ((ArrayAdapter<String>) fort.getAdapter()).getPosition(t.fort);
   361 			fort.setSelection(position);
   362 			fort.setSelection(position);
   362 
   363 
   363 			position = 0;
   364 			position = 0;
   364 			for (HashMap<String, ?> hashmap : typesData) {
   365 			for (HashMap<String, ?> hashmap : typesData) {
   365 				if (hashmap.get("txt").equals(t.levels[0])) {
   366 				if (t.hogs.get(0) != null && hashmap.get("txt").equals(t.hogs.get(0).level)) {
   366 					difficulty.setSelection(position);
   367 					difficulty.setSelection(position);
   367 					break;
   368 					break;
   368 				}
   369 				}
   369 			}
   370 			}
   370 
   371 
   385 			}
   386 			}
   386 
   387 
   387 			for (int i = 0; i < Team.maxNumberOfHogs; i++) {
   388 			for (int i = 0; i < Team.maxNumberOfHogs; i++) {
   388 				position = 0;
   389 				position = 0;
   389 				for (HashMap<String, ?> hashmap : hatsData) {
   390 				for (HashMap<String, ?> hashmap : hatsData) {
   390 					if (hashmap.get("txt").equals(t.hats[i])) {
   391 					if (hashmap.get("txt").equals(t.hogs.get(i).hat)) {
   391 						hogHat.get(i).setSelection(position);
   392 						hogHat.get(i).setSelection(position);
   392 					}
   393 					}
   393 				}
   394 				}
   394 
   395 
   395 				hogName.get(i).setText(t.hogNames[i]);
   396 				hogName.get(i).setText(t.hogs.get(i).name);
   396 			}
   397 			}
   397 			this.fileName = t.file;
   398 			//this.fileName = t.file;
   398 		}
   399 		}
   399 	}
   400 	}
   400 
   401 
   401 
   402 
   402 	private SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() {
   403 	private SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() {