project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/TeamCreatorActivity.java
branchhedgeroid
changeset 5467 88e25840f532
child 5542 141c12a23787
equal deleted inserted replaced
5465:b7e009722465 5467:88e25840f532
       
     1 package org.hedgewars.mobile;
       
     2 
       
     3 import java.io.File;
       
     4 import java.io.FileNotFoundException;
       
     5 import java.io.FileOutputStream;
       
     6 import java.io.IOException;
       
     7 import java.util.ArrayList;
       
     8 import java.util.HashMap;
       
     9 
       
    10 import org.hedgewars.mobile.EngineProtocol.FrontendDataUtils;
       
    11 import org.hedgewars.mobile.EngineProtocol.Team;
       
    12 
       
    13 import android.app.Activity;
       
    14 import android.graphics.Bitmap;
       
    15 import android.graphics.drawable.Drawable;
       
    16 import android.media.MediaPlayer;
       
    17 import android.os.Bundle;
       
    18 import android.view.View;
       
    19 import android.view.View.OnClickListener;
       
    20 import android.widget.AdapterView;
       
    21 import android.widget.AdapterView.OnItemSelectedListener;
       
    22 import android.widget.ArrayAdapter;
       
    23 import android.widget.EditText;
       
    24 import android.widget.ImageButton;
       
    25 import android.widget.ImageView;
       
    26 import android.widget.LinearLayout;
       
    27 import android.widget.RelativeLayout;
       
    28 import android.widget.ScrollView;
       
    29 import android.widget.SimpleAdapter;
       
    30 import android.widget.Spinner;
       
    31 import android.widget.TextView;
       
    32 
       
    33 public class TeamCreatorActivity extends Activity {
       
    34 
       
    35 	private TextView name;
       
    36 	private Spinner difficulty, grave, flag, voice, fort;
       
    37 	private ImageView imgFort;
       
    38 	private ArrayList<ImageButton> hogDice = new ArrayList<ImageButton>();
       
    39 	private ArrayList<Spinner> hogHat = new ArrayList<Spinner>();
       
    40 	private ArrayList<EditText> hogName = new ArrayList<EditText>();
       
    41 	private ImageButton back, save, voiceButton;
       
    42 	private ScrollView scroller;
       
    43 	private MediaPlayer mp = null;
       
    44 	private ArrayList<RelativeLayout> hogs;
       
    45 
       
    46 	public void onCreate(Bundle savedInstanceState){
       
    47 		super.onCreate(savedInstanceState);
       
    48 		setContentView(R.layout.team_creation);
       
    49 
       
    50 		name = (TextView) findViewById(R.id.txtName);
       
    51 		difficulty = (Spinner) findViewById(R.id.spinType);
       
    52 		grave = (Spinner) findViewById(R.id.spinGrave);
       
    53 		flag = (Spinner) findViewById(R.id.spinFlag);
       
    54 		voice = (Spinner) findViewById(R.id.spinVoice);
       
    55 		fort = (Spinner) findViewById(R.id.spinFort);
       
    56 
       
    57 		imgFort = (ImageView) findViewById(R.id.imgFort);
       
    58 
       
    59 		back = (ImageButton) findViewById(R.id.btnBack);
       
    60 		save = (ImageButton) findViewById(R.id.btnSave);
       
    61 		voiceButton = (ImageButton) findViewById(R.id.btnPlay);
       
    62 		
       
    63 		scroller = (ScrollView) findViewById(R.id.scroller);
       
    64 
       
    65 		save.setOnClickListener(saveClicker);
       
    66 		
       
    67 		LinearLayout ll = (LinearLayout) findViewById(R.id.HogsContainer);
       
    68 		hogs = new ArrayList<RelativeLayout>(ll.getChildCount());
       
    69 		for(int i = 0; i < ll.getChildCount(); i++){
       
    70 			RelativeLayout team_creation_entry = (RelativeLayout) ll.getChildAt(i);
       
    71 			
       
    72 			hogHat.add((Spinner)team_creation_entry.findViewById(R.id.spinTeam1));
       
    73 			hogDice.add((ImageButton)team_creation_entry.findViewById(R.id.btnTeam1));
       
    74 			hogName.add((EditText)team_creation_entry.findViewById(R.id.txtTeam1));
       
    75 		}
       
    76 		ArrayList<HashMap<String, ?>> gravesData = FrontendDataUtils.getGraves(this);
       
    77 		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});
       
    78 		sa.setViewBinder(viewBinder);
       
    79 		grave.setAdapter(sa);
       
    80 
       
    81 		ArrayList<HashMap<String, ?>> flagsData = FrontendDataUtils.getFlags(this);
       
    82 		sa = new SimpleAdapter(this, flagsData, R.layout.spinner_textimg_entry, new String[]{"txt", "img"}, new int[]{R.id.spinner_txt, R.id.spinner_img});
       
    83 		sa.setViewBinder(viewBinder);
       
    84 		flag.setAdapter(sa);
       
    85 		
       
    86 		ArrayList<HashMap<String, ?>> typesData = FrontendDataUtils.getTypes(this);
       
    87 		sa = new SimpleAdapter(this, typesData, R.layout.spinner_textimg_entry, new String[]{"txt", "img"}, new int[]{R.id.spinner_txt, R.id.spinner_img});
       
    88 		difficulty.setAdapter(sa);
       
    89 		
       
    90 		ArrayList<HashMap<String, ?>> hatsData = FrontendDataUtils.getHats(this);
       
    91 		sa = new SimpleAdapter(this, hatsData, R.layout.spinner_textimg_entry, new String[]{"txt", "img"}, new int[]{R.id.spinner_txt, R.id.spinner_img});
       
    92 		sa.setViewBinder(viewBinder);
       
    93 		for(Spinner spin : hogHat){
       
    94 			spin.setAdapter(sa);
       
    95 		}
       
    96 
       
    97 		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, FrontendDataUtils.getVoices(this));
       
    98 		voice.setAdapter(adapter);
       
    99 		voiceButton.setOnClickListener(voiceClicker);
       
   100 		
       
   101 		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, FrontendDataUtils.getForts(this));
       
   102 		fort.setAdapter(adapter);
       
   103 		fort.setOnItemSelectedListener(fortSelector);
       
   104 		
       
   105 		
       
   106 		Team t = this.getIntent().getParcelableExtra("team");
       
   107 		if(t != null){
       
   108 			name.setText(t.name);
       
   109 			int position = ((ArrayAdapter<String>)voice.getAdapter()).getPosition(t.voice);
       
   110 			voice.setSelection(position);
       
   111 			position = ((ArrayAdapter<String>)fort.getAdapter()).getPosition(t.fort);
       
   112 			fort.setSelection(position);
       
   113 			
       
   114 			position = 0;
       
   115 			for(HashMap<String, ?> hashmap : typesData){
       
   116 				if(hashmap.get("txt").equals(t.levels[0])){
       
   117 					difficulty.setSelection(position);
       
   118 					break;
       
   119 				}
       
   120 			}
       
   121 			
       
   122 			position = 0;
       
   123 			for(HashMap<String, ?> hashmap : gravesData){
       
   124 				if(hashmap.get("txt").equals(t.grave)){
       
   125 					grave.setSelection(position);
       
   126 					break;
       
   127 				}
       
   128 			}
       
   129 			
       
   130 			position = 0;
       
   131 			for(HashMap<String, ?> hashmap : typesData){
       
   132 				if(hashmap.get("txt").equals(t.flag)){
       
   133 					flag.setSelection(position);
       
   134 					break;
       
   135 				}
       
   136 			}
       
   137 			
       
   138 			for(int i = 0; i < Team.maxNumberOfHogs; i++){
       
   139 				position = 0;
       
   140 				for(HashMap<String, ?> hashmap : hatsData){
       
   141 					if(hashmap.get("txt").equals(t.hats[i])){
       
   142 						hogHat.get(i).setSelection(position);
       
   143 					}
       
   144 				}
       
   145 				
       
   146 				hogName.get(i).setText(t.hogNames[i]);
       
   147 			}
       
   148 		}
       
   149 	}
       
   150 
       
   151 	public void onDestroy(){
       
   152 		super.onDestroy();
       
   153 		if(mp != null){
       
   154 			mp.release();
       
   155 			mp = null;
       
   156 		}
       
   157 	}
       
   158 	
       
   159 	private OnClickListener saveClicker = new OnClickListener(){
       
   160 		public void onClick(View v) {
       
   161 			Team team = new Team();
       
   162 			team.name = name.getText().toString();
       
   163 			HashMap<String, Object> hashmap = (HashMap<String, Object>) flag.getSelectedItem();
       
   164 			
       
   165 			team.flag = (String)hashmap.get("txt");
       
   166 			team.fort = fort.getSelectedItem().toString();
       
   167 			hashmap = (HashMap<String, Object>)grave.getSelectedItem();
       
   168 			team.grave = hashmap.get("txt").toString();
       
   169 			team.hash = "0";
       
   170 			team.voice = voice.getSelectedItem().toString();
       
   171 			
       
   172 			hashmap =  ((HashMap<String, Object>)difficulty.getSelectedItem());
       
   173 			String levelString = hashmap.get("txt").toString();
       
   174 			int levelInt;
       
   175 			if(levelString.equals(getString(R.string.human))){
       
   176 				levelInt = 0;
       
   177 			}else if(levelString.equals(getString(R.string.bot5))){
       
   178 				levelInt = 1;
       
   179 			}else if(levelString.equals(getString(R.string.bot4))){
       
   180 				levelInt = 2;
       
   181 			}else if(levelString.equals(getString(R.string.bot3))){
       
   182 				levelInt = 3;
       
   183 			}else if(levelString.equals(getString(R.string.bot2))){
       
   184 				levelInt = 4;
       
   185 			}else {
       
   186 				levelInt = 5;
       
   187 			}
       
   188 			
       
   189 			for(int i = 0; i < hogName.size(); i++){
       
   190 				team.hogNames[i] = hogName.get(i).getText().toString();
       
   191 				hashmap = (HashMap<String, Object>)hogHat.get(i).getSelectedItem();
       
   192 				team.hats[i] = hashmap.get("txt").toString();
       
   193 				team.levels[i] = levelInt;
       
   194 			}
       
   195 			try {
       
   196 				File teamsDir = new File(getFilesDir().getAbsolutePath() + '/' + Team.DIRECTORY_TEAMS);
       
   197 				if(!teamsDir.exists()) teamsDir.mkdir();
       
   198 				FileOutputStream fos = new FileOutputStream(String.format("%s/%s.xml", teamsDir.getAbsolutePath(), team.name));
       
   199 				team.writeToXml(fos);
       
   200 			} catch (FileNotFoundException e) {
       
   201 				e.printStackTrace();
       
   202 			}
       
   203 		}
       
   204 		
       
   205 	};
       
   206 	
       
   207 	private OnItemSelectedListener fortSelector = new OnItemSelectedListener(){
       
   208 		@SuppressWarnings("unchecked")
       
   209 		public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
       
   210 			String fortName = (String) arg0.getAdapter().getItem(position);
       
   211 			Drawable fortIconDrawable = Drawable.createFromPath(Utils.getDownloadPath(TeamCreatorActivity.this) + "Forts/" + fortName + "L.png");
       
   212 			imgFort.setImageDrawable(fortIconDrawable);
       
   213 			scroller.fullScroll(ScrollView.FOCUS_DOWN);//Scroll the scrollview to the bottom, work around for scollview invalidation (scrolls back to top)
       
   214 		}
       
   215 
       
   216 		public void onNothingSelected(AdapterView<?> arg0) {
       
   217 		}
       
   218 
       
   219 	};
       
   220 
       
   221 	private OnClickListener voiceClicker = new OnClickListener(){
       
   222 		public void onClick(View v) {
       
   223 			try {
       
   224 				File dir = new File(String.format("%sSounds/voices/%s", Utils.getDownloadPath(TeamCreatorActivity.this), voice.getSelectedItem()));
       
   225 				String file = "";
       
   226 				File[] dirs = dir.listFiles();
       
   227 				File f = dirs[(int)Math.round(Math.random()*dirs.length)];
       
   228 				if(f.getName().endsWith(".ogg"))file = f.getAbsolutePath();
       
   229 				
       
   230 				if(mp == null) mp = new MediaPlayer();
       
   231 				else mp.reset();
       
   232 				mp.setDataSource(file);
       
   233 				mp.prepare();
       
   234 				mp.start();
       
   235 			} catch (IllegalArgumentException e) {
       
   236 				e.printStackTrace();
       
   237 			} catch (IllegalStateException e) {
       
   238 				e.printStackTrace();
       
   239 			} catch (IOException e) {
       
   240 				e.printStackTrace();
       
   241 			}
       
   242 		}
       
   243 	};
       
   244 
       
   245 	private SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() {
       
   246 
       
   247 		public boolean setViewValue(View view, Object data,	String textRepresentation) {
       
   248 			if(view instanceof ImageView && data instanceof Bitmap){
       
   249 				ImageView v = (ImageView)view;
       
   250 				v.setImageBitmap((Bitmap)data);
       
   251 				return true;
       
   252 			}else{
       
   253 				return false;
       
   254 			}
       
   255 		}
       
   256 	};
       
   257 
       
   258 }