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