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