project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/TeamCreatorActivity.java
branchhedgeroid
changeset 5542 141c12a23787
parent 5467 88e25840f532
child 5607 d3a3e80ad1da
equal deleted inserted replaced
5540:76392a62da2f 5542:141c12a23787
    15 import android.graphics.drawable.Drawable;
    15 import android.graphics.drawable.Drawable;
    16 import android.media.MediaPlayer;
    16 import android.media.MediaPlayer;
    17 import android.os.Bundle;
    17 import android.os.Bundle;
    18 import android.view.View;
    18 import android.view.View;
    19 import android.view.View.OnClickListener;
    19 import android.view.View.OnClickListener;
       
    20 import android.view.View.OnFocusChangeListener;
    20 import android.widget.AdapterView;
    21 import android.widget.AdapterView;
    21 import android.widget.AdapterView.OnItemSelectedListener;
    22 import android.widget.AdapterView.OnItemSelectedListener;
    22 import android.widget.ArrayAdapter;
    23 import android.widget.ArrayAdapter;
    23 import android.widget.EditText;
    24 import android.widget.EditText;
    24 import android.widget.ImageButton;
    25 import android.widget.ImageButton;
    40 	private ArrayList<EditText> hogName = new ArrayList<EditText>();
    41 	private ArrayList<EditText> hogName = new ArrayList<EditText>();
    41 	private ImageButton back, save, voiceButton;
    42 	private ImageButton back, save, voiceButton;
    42 	private ScrollView scroller;
    43 	private ScrollView scroller;
    43 	private MediaPlayer mp = null;
    44 	private MediaPlayer mp = null;
    44 	private ArrayList<RelativeLayout> hogs;
    45 	private ArrayList<RelativeLayout> hogs;
       
    46 	private boolean settingsChanged = false;
       
    47 	private boolean saved = false;
    45 
    48 
    46 	public void onCreate(Bundle savedInstanceState){
    49 	public void onCreate(Bundle savedInstanceState){
    47 		super.onCreate(savedInstanceState);
    50 		super.onCreate(savedInstanceState);
    48 		setContentView(R.layout.team_creation);
    51 		setContentView(R.layout.team_creation);
    49 
    52 
    57 		imgFort = (ImageView) findViewById(R.id.imgFort);
    60 		imgFort = (ImageView) findViewById(R.id.imgFort);
    58 
    61 
    59 		back = (ImageButton) findViewById(R.id.btnBack);
    62 		back = (ImageButton) findViewById(R.id.btnBack);
    60 		save = (ImageButton) findViewById(R.id.btnSave);
    63 		save = (ImageButton) findViewById(R.id.btnSave);
    61 		voiceButton = (ImageButton) findViewById(R.id.btnPlay);
    64 		voiceButton = (ImageButton) findViewById(R.id.btnPlay);
    62 		
    65 
    63 		scroller = (ScrollView) findViewById(R.id.scroller);
    66 		scroller = (ScrollView) findViewById(R.id.scroller);
    64 
    67 
    65 		save.setOnClickListener(saveClicker);
    68 		save.setOnClickListener(saveClicker);
    66 		
    69 		back.setOnClickListener(backClicker);
       
    70 
    67 		LinearLayout ll = (LinearLayout) findViewById(R.id.HogsContainer);
    71 		LinearLayout ll = (LinearLayout) findViewById(R.id.HogsContainer);
    68 		hogs = new ArrayList<RelativeLayout>(ll.getChildCount());
    72 		hogs = new ArrayList<RelativeLayout>(ll.getChildCount());
    69 		for(int i = 0; i < ll.getChildCount(); i++){
    73 		for(int i = 0; i < ll.getChildCount(); i++){
    70 			RelativeLayout team_creation_entry = (RelativeLayout) ll.getChildAt(i);
    74 			RelativeLayout team_creation_entry = (RelativeLayout) ll.getChildAt(i);
    71 			
    75 
    72 			hogHat.add((Spinner)team_creation_entry.findViewById(R.id.spinTeam1));
    76 			hogHat.add((Spinner)team_creation_entry.findViewById(R.id.spinTeam1));
    73 			hogDice.add((ImageButton)team_creation_entry.findViewById(R.id.btnTeam1));
    77 			hogDice.add((ImageButton)team_creation_entry.findViewById(R.id.btnTeam1));
    74 			hogName.add((EditText)team_creation_entry.findViewById(R.id.txtTeam1));
    78 			hogName.add((EditText)team_creation_entry.findViewById(R.id.txtTeam1));
    75 		}
    79 		}
    76 		ArrayList<HashMap<String, ?>> gravesData = FrontendDataUtils.getGraves(this);
    80 		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});
    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});
    78 		sa.setViewBinder(viewBinder);
    82 		sa.setViewBinder(viewBinder);
    79 		grave.setAdapter(sa);
    83 		grave.setAdapter(sa);
       
    84 		grave.setOnFocusChangeListener(focusser);
    80 
    85 
    81 		ArrayList<HashMap<String, ?>> flagsData = FrontendDataUtils.getFlags(this);
    86 		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});
    87 		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);
    88 		sa.setViewBinder(viewBinder);
    84 		flag.setAdapter(sa);
    89 		flag.setAdapter(sa);
    85 		
    90 		flag.setOnFocusChangeListener(focusser);
       
    91 
    86 		ArrayList<HashMap<String, ?>> typesData = FrontendDataUtils.getTypes(this);
    92 		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});
    93 		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);
    94 		difficulty.setAdapter(sa);
    89 		
    95 		difficulty.setOnFocusChangeListener(focusser);
       
    96 
    90 		ArrayList<HashMap<String, ?>> hatsData = FrontendDataUtils.getHats(this);
    97 		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});
    98 		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);
    99 		sa.setViewBinder(viewBinder);
    93 		for(Spinner spin : hogHat){
   100 		for(Spinner spin : hogHat){
    94 			spin.setAdapter(sa);
   101 			spin.setAdapter(sa);
    95 		}
   102 		}
    96 
   103 
    97 		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, FrontendDataUtils.getVoices(this));
   104 		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, FrontendDataUtils.getVoices(this));
    98 		voice.setAdapter(adapter);
   105 		voice.setAdapter(adapter);
       
   106 		voice.setOnFocusChangeListener(focusser);
    99 		voiceButton.setOnClickListener(voiceClicker);
   107 		voiceButton.setOnClickListener(voiceClicker);
   100 		
   108 
   101 		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, FrontendDataUtils.getForts(this));
   109 		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, FrontendDataUtils.getForts(this));
   102 		fort.setAdapter(adapter);
   110 		fort.setAdapter(adapter);
   103 		fort.setOnItemSelectedListener(fortSelector);
   111 		fort.setOnItemSelectedListener(fortSelector);
   104 		
   112 		fort.setOnFocusChangeListener(focusser);
   105 		
   113 
   106 		Team t = this.getIntent().getParcelableExtra("team");
   114 		Team t = this.getIntent().getParcelableExtra("team");
   107 		if(t != null){
   115 		if(t != null){
   108 			name.setText(t.name);
   116 			name.setText(t.name);
   109 			int position = ((ArrayAdapter<String>)voice.getAdapter()).getPosition(t.voice);
   117 			int position = ((ArrayAdapter<String>)voice.getAdapter()).getPosition(t.voice);
   110 			voice.setSelection(position);
   118 			voice.setSelection(position);
       
   119 
   111 			position = ((ArrayAdapter<String>)fort.getAdapter()).getPosition(t.fort);
   120 			position = ((ArrayAdapter<String>)fort.getAdapter()).getPosition(t.fort);
   112 			fort.setSelection(position);
   121 			fort.setSelection(position);
   113 			
   122 
   114 			position = 0;
   123 			position = 0;
   115 			for(HashMap<String, ?> hashmap : typesData){
   124 			for(HashMap<String, ?> hashmap : typesData){
   116 				if(hashmap.get("txt").equals(t.levels[0])){
   125 				if(hashmap.get("txt").equals(t.levels[0])){
   117 					difficulty.setSelection(position);
   126 					difficulty.setSelection(position);
   118 					break;
   127 					break;
   119 				}
   128 				}
   120 			}
   129 			}
   121 			
   130 
   122 			position = 0;
   131 			position = 0;
   123 			for(HashMap<String, ?> hashmap : gravesData){
   132 			for(HashMap<String, ?> hashmap : gravesData){
   124 				if(hashmap.get("txt").equals(t.grave)){
   133 				if(hashmap.get("txt").equals(t.grave)){
   125 					grave.setSelection(position);
   134 					grave.setSelection(position);
   126 					break;
   135 					break;
   127 				}
   136 				}
   128 			}
   137 			}
   129 			
   138 
   130 			position = 0;
   139 			position = 0;
   131 			for(HashMap<String, ?> hashmap : typesData){
   140 			for(HashMap<String, ?> hashmap : typesData){
   132 				if(hashmap.get("txt").equals(t.flag)){
   141 				if(hashmap.get("txt").equals(t.flag)){
   133 					flag.setSelection(position);
   142 					flag.setSelection(position);
   134 					break;
   143 					break;
   135 				}
   144 				}
   136 			}
   145 			}
   137 			
   146 
   138 			for(int i = 0; i < Team.maxNumberOfHogs; i++){
   147 			for(int i = 0; i < Team.maxNumberOfHogs; i++){
   139 				position = 0;
   148 				position = 0;
   140 				for(HashMap<String, ?> hashmap : hatsData){
   149 				for(HashMap<String, ?> hashmap : hatsData){
   141 					if(hashmap.get("txt").equals(t.hats[i])){
   150 					if(hashmap.get("txt").equals(t.hats[i])){
   142 						hogHat.get(i).setSelection(position);
   151 						hogHat.get(i).setSelection(position);
   143 					}
   152 					}
   144 				}
   153 				}
   145 				
   154 
   146 				hogName.get(i).setText(t.hogNames[i]);
   155 				hogName.get(i).setText(t.hogNames[i]);
   147 			}
   156 			}
   148 		}
   157 		}
   149 	}
   158 	}
   150 
   159 
   153 		if(mp != null){
   162 		if(mp != null){
   154 			mp.release();
   163 			mp.release();
   155 			mp = null;
   164 			mp = null;
   156 		}
   165 		}
   157 	}
   166 	}
       
   167 
       
   168 	private OnFocusChangeListener focusser = new OnFocusChangeListener(){
       
   169 		public void onFocusChange(View v, boolean hasFocus) {
       
   170 			settingsChanged = true;
       
   171 		}
       
   172 		
       
   173 	};
   158 	
   174 	
       
   175 	public void onBackPressed(){
       
   176 		onFinishing();
       
   177 		super.onBackPressed();
       
   178 		
       
   179 	}
       
   180 	
       
   181 	private OnClickListener backClicker = new OnClickListener(){
       
   182 		public void onClick(View v){
       
   183 			onFinishing();
       
   184 			finish();
       
   185 		}
       
   186 	};
       
   187 
       
   188 	private void onFinishing(){
       
   189 		if(settingsChanged){
       
   190 			setResult(RESULT_OK);
       
   191 		}else{
       
   192 			setResult(RESULT_CANCELED);
       
   193 		}
       
   194 	}
       
   195 
   159 	private OnClickListener saveClicker = new OnClickListener(){
   196 	private OnClickListener saveClicker = new OnClickListener(){
   160 		public void onClick(View v) {
   197 		public void onClick(View v) {
       
   198 			saved = true;
   161 			Team team = new Team();
   199 			Team team = new Team();
   162 			team.name = name.getText().toString();
   200 			team.name = name.getText().toString();
   163 			HashMap<String, Object> hashmap = (HashMap<String, Object>) flag.getSelectedItem();
   201 			HashMap<String, Object> hashmap = (HashMap<String, Object>) flag.getSelectedItem();
   164 			
   202 
   165 			team.flag = (String)hashmap.get("txt");
   203 			team.flag = (String)hashmap.get("txt");
   166 			team.fort = fort.getSelectedItem().toString();
   204 			team.fort = fort.getSelectedItem().toString();
   167 			hashmap = (HashMap<String, Object>)grave.getSelectedItem();
   205 			hashmap = (HashMap<String, Object>)grave.getSelectedItem();
   168 			team.grave = hashmap.get("txt").toString();
   206 			team.grave = hashmap.get("txt").toString();
   169 			team.hash = "0";
   207 			team.hash = "0";
   170 			team.voice = voice.getSelectedItem().toString();
   208 			team.voice = voice.getSelectedItem().toString();
   171 			
   209 
   172 			hashmap =  ((HashMap<String, Object>)difficulty.getSelectedItem());
   210 			hashmap =  ((HashMap<String, Object>)difficulty.getSelectedItem());
   173 			String levelString = hashmap.get("txt").toString();
   211 			String levelString = hashmap.get("txt").toString();
   174 			int levelInt;
   212 			int levelInt;
   175 			if(levelString.equals(getString(R.string.human))){
   213 			if(levelString.equals(getString(R.string.human))){
   176 				levelInt = 0;
   214 				levelInt = 0;
   183 			}else if(levelString.equals(getString(R.string.bot2))){
   221 			}else if(levelString.equals(getString(R.string.bot2))){
   184 				levelInt = 4;
   222 				levelInt = 4;
   185 			}else {
   223 			}else {
   186 				levelInt = 5;
   224 				levelInt = 5;
   187 			}
   225 			}
   188 			
   226 
   189 			for(int i = 0; i < hogName.size(); i++){
   227 			for(int i = 0; i < hogName.size(); i++){
   190 				team.hogNames[i] = hogName.get(i).getText().toString();
   228 				team.hogNames[i] = hogName.get(i).getText().toString();
   191 				hashmap = (HashMap<String, Object>)hogHat.get(i).getSelectedItem();
   229 				hashmap = (HashMap<String, Object>)hogHat.get(i).getSelectedItem();
   192 				team.hats[i] = hashmap.get("txt").toString();
   230 				team.hats[i] = hashmap.get("txt").toString();
   193 				team.levels[i] = levelInt;
   231 				team.levels[i] = levelInt;
   199 				team.writeToXml(fos);
   237 				team.writeToXml(fos);
   200 			} catch (FileNotFoundException e) {
   238 			} catch (FileNotFoundException e) {
   201 				e.printStackTrace();
   239 				e.printStackTrace();
   202 			}
   240 			}
   203 		}
   241 		}
   204 		
   242 
   205 	};
   243 	};
   206 	
   244 
   207 	private OnItemSelectedListener fortSelector = new OnItemSelectedListener(){
   245 	private OnItemSelectedListener fortSelector = new OnItemSelectedListener(){
   208 		@SuppressWarnings("unchecked")
   246 		@SuppressWarnings("unchecked")
   209 		public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
   247 		public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
       
   248 			settingsChanged = true;
   210 			String fortName = (String) arg0.getAdapter().getItem(position);
   249 			String fortName = (String) arg0.getAdapter().getItem(position);
   211 			Drawable fortIconDrawable = Drawable.createFromPath(Utils.getDownloadPath(TeamCreatorActivity.this) + "Forts/" + fortName + "L.png");
   250 			Drawable fortIconDrawable = Drawable.createFromPath(Utils.getDownloadPath(TeamCreatorActivity.this) + "Forts/" + fortName + "L.png");
   212 			imgFort.setImageDrawable(fortIconDrawable);
   251 			imgFort.setImageDrawable(fortIconDrawable);
   213 			scroller.fullScroll(ScrollView.FOCUS_DOWN);//Scroll the scrollview to the bottom, work around for scollview invalidation (scrolls back to top)
   252 			scroller.fullScroll(ScrollView.FOCUS_DOWN);//Scroll the scrollview to the bottom, work around for scollview invalidation (scrolls back to top)
   214 		}
   253 		}
   224 				File dir = new File(String.format("%sSounds/voices/%s", Utils.getDownloadPath(TeamCreatorActivity.this), voice.getSelectedItem()));
   263 				File dir = new File(String.format("%sSounds/voices/%s", Utils.getDownloadPath(TeamCreatorActivity.this), voice.getSelectedItem()));
   225 				String file = "";
   264 				String file = "";
   226 				File[] dirs = dir.listFiles();
   265 				File[] dirs = dir.listFiles();
   227 				File f = dirs[(int)Math.round(Math.random()*dirs.length)];
   266 				File f = dirs[(int)Math.round(Math.random()*dirs.length)];
   228 				if(f.getName().endsWith(".ogg"))file = f.getAbsolutePath();
   267 				if(f.getName().endsWith(".ogg"))file = f.getAbsolutePath();
   229 				
   268 
   230 				if(mp == null) mp = new MediaPlayer();
   269 				if(mp == null) mp = new MediaPlayer();
   231 				else mp.reset();
   270 				else mp.reset();
   232 				mp.setDataSource(file);
   271 				mp.setDataSource(file);
   233 				mp.prepare();
   272 				mp.prepare();
   234 				mp.start();
   273 				mp.start();