project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/TeamCreatorActivity.java
changeset 6447 0bb16bc5c8b4
parent 6432 ffb18b44415d
child 6448 88e49851d814
equal deleted inserted replaced
6446:a49d01b96185 6447:0bb16bc5c8b4
    22 import java.io.File;
    22 import java.io.File;
    23 import java.io.FileNotFoundException;
    23 import java.io.FileNotFoundException;
    24 import java.io.FileOutputStream;
    24 import java.io.FileOutputStream;
    25 import java.io.IOException;
    25 import java.io.IOException;
    26 import java.util.ArrayList;
    26 import java.util.ArrayList;
       
    27 import java.util.Collections;
    27 import java.util.HashMap;
    28 import java.util.HashMap;
       
    29 import java.util.List;
    28 
    30 
    29 import org.hedgewars.hedgeroid.EngineProtocol.FrontendDataUtils;
    31 import org.hedgewars.hedgeroid.EngineProtocol.FrontendDataUtils;
    30 import org.hedgewars.hedgeroid.EngineProtocol.Team;
    32 import org.hedgewars.hedgeroid.EngineProtocol.Team;
    31 
    33 
    32 import android.app.Activity;
    34 import android.app.Activity;
    49 import android.widget.SimpleAdapter;
    51 import android.widget.SimpleAdapter;
    50 import android.widget.Spinner;
    52 import android.widget.Spinner;
    51 import android.widget.TextView;
    53 import android.widget.TextView;
    52 import android.widget.Toast;
    54 import android.widget.Toast;
    53 
    55 
    54 public class TeamCreatorActivity extends Activity {
    56 public class TeamCreatorActivity extends Activity implements Runnable{
    55 
    57 
    56 	private TextView name;
    58 	private TextView name;
    57 	private Spinner difficulty, grave, flag, voice, fort;
    59 	private Spinner difficulty, grave, flag, voice, fort;
    58 	private ImageView imgFort;
    60 	private ImageView imgFort;
    59 	private ArrayList<ImageButton> hogDice = new ArrayList<ImageButton>();
    61 	private ArrayList<ImageButton> hogDice = new ArrayList<ImageButton>();
    64 	private MediaPlayer mp = null;
    66 	private MediaPlayer mp = null;
    65 	private boolean settingsChanged = false;
    67 	private boolean settingsChanged = false;
    66 	private boolean saved = false;
    68 	private boolean saved = false;
    67 	private String fileName = null;
    69 	private String fileName = null;
    68 
    70 
       
    71 	private List<HashMap<String, ?>> flagsData, typesData, gravesData, hatsData;
       
    72 	private List<String> voicesData, fortsData;
       
    73 
    69 	public void onCreate(Bundle savedInstanceState) {
    74 	public void onCreate(Bundle savedInstanceState) {
    70 		super.onCreate(savedInstanceState);
    75 		super.onCreate(savedInstanceState);
    71 		setContentView(R.layout.team_creation);
    76 		setContentView(R.layout.team_creation);
    72 
    77 
    73 		name = (TextView) findViewById(R.id.txtName);
    78 		name = (TextView) findViewById(R.id.txtName);
    88 		save.setOnClickListener(saveClicker);
    93 		save.setOnClickListener(saveClicker);
    89 		back.setOnClickListener(backClicker);
    94 		back.setOnClickListener(backClicker);
    90 
    95 
    91 		LinearLayout ll = (LinearLayout) findViewById(R.id.HogsContainer);
    96 		LinearLayout ll = (LinearLayout) findViewById(R.id.HogsContainer);
    92 		for (int i = 0; i < ll.getChildCount(); i++) {
    97 		for (int i = 0; i < ll.getChildCount(); i++) {
    93 			RelativeLayout team_creation_entry = (RelativeLayout) ll
    98 			RelativeLayout team_creation_entry = (RelativeLayout) ll.getChildAt(i);
    94 					.getChildAt(i);
       
    95 
    99 
    96 			hogHat.add((Spinner) team_creation_entry
   100 			hogHat.add((Spinner) team_creation_entry
    97 					.findViewById(R.id.spinTeam1));
   101 					.findViewById(R.id.spinTeam1));
    98 			hogDice.add((ImageButton) team_creation_entry
   102 			hogDice.add((ImageButton) team_creation_entry
    99 					.findViewById(R.id.btnTeam1));
   103 					.findViewById(R.id.btnTeam1));
   100 			hogName.add((EditText) team_creation_entry
   104 			hogName.add((EditText) team_creation_entry
   101 					.findViewById(R.id.txtTeam1));
   105 					.findViewById(R.id.txtTeam1));
   102 		}
   106 		}
   103 		ArrayList<HashMap<String, ?>> gravesData = FrontendDataUtils.getGraves(this);
   107 
       
   108 		gravesData = new ArrayList<HashMap<String, ?>>();
   104 		SimpleAdapter sa = new SimpleAdapter(this, gravesData,
   109 		SimpleAdapter sa = new SimpleAdapter(this, gravesData,
   105 				R.layout.spinner_textimg_entry, new String[] { "txt", "img" },
   110 				R.layout.spinner_textimg_entry, new String[] { "txt", "img" },
   106 				new int[] { R.id.spinner_txt, R.id.spinner_img });
   111 				new int[] { R.id.spinner_txt, R.id.spinner_img });
   107 		sa.setDropDownViewResource(R.layout.spinner_textimg_dropdown_entry);
   112 		sa.setDropDownViewResource(R.layout.spinner_textimg_dropdown_entry);
   108 		sa.setViewBinder(viewBinder);
   113 		sa.setViewBinder(viewBinder);
   109 		grave.setAdapter(sa);
   114 		grave.setAdapter(sa);
   110 		grave.setOnFocusChangeListener(focusser);
   115 		grave.setOnFocusChangeListener(focusser);
   111 
   116 
   112 		ArrayList<HashMap<String, ?>> flagsData = FrontendDataUtils
   117 		flagsData = new ArrayList<HashMap<String, ?>>();
   113 				.getFlags(this);
       
   114 		sa = new SimpleAdapter(this, flagsData, R.layout.spinner_textimg_entry,
   118 		sa = new SimpleAdapter(this, flagsData, R.layout.spinner_textimg_entry,
   115 				new String[] { "txt", "img" }, new int[] { R.id.spinner_txt,
   119 				new String[] { "txt", "img" }, new int[] { R.id.spinner_txt,
   116 						R.id.spinner_img });
   120 				R.id.spinner_img });
   117 		sa.setDropDownViewResource(R.layout.spinner_textimg_dropdown_entry);
   121 		sa.setDropDownViewResource(R.layout.spinner_textimg_dropdown_entry);
   118 		sa.setViewBinder(viewBinder);
   122 		sa.setViewBinder(viewBinder);
   119 		flag.setAdapter(sa);
   123 		flag.setAdapter(sa);
   120 		flag.setOnFocusChangeListener(focusser);
   124 		flag.setOnFocusChangeListener(focusser);
   121 
   125 
   122 		ArrayList<HashMap<String, ?>> typesData = FrontendDataUtils
   126 		typesData = new ArrayList<HashMap<String, ?>>();
   123 				.getTypes(this);
       
   124 		sa = new SimpleAdapter(this, typesData, R.layout.spinner_textimg_entry,
   127 		sa = new SimpleAdapter(this, typesData, R.layout.spinner_textimg_entry,
   125 				new String[] { "txt", "img" }, new int[] { R.id.spinner_txt,
   128 				new String[] { "txt", "img" }, new int[] { R.id.spinner_txt,
   126 						R.id.spinner_img });
   129 				R.id.spinner_img });
   127 		sa.setDropDownViewResource(R.layout.spinner_textimg_dropdown_entry);
   130 		sa.setDropDownViewResource(R.layout.spinner_textimg_dropdown_entry);
   128 		difficulty.setAdapter(sa);
   131 		difficulty.setAdapter(sa);
   129 		difficulty.setOnFocusChangeListener(focusser);
   132 		difficulty.setOnFocusChangeListener(focusser);
   130 
   133 
   131 		ArrayList<HashMap<String, ?>> hatsData = FrontendDataUtils
   134 		hatsData = new ArrayList<HashMap<String, ?>>();
   132 				.getHats(this);
       
   133 		sa = new SimpleAdapter(this, hatsData, R.layout.spinner_textimg_entry,
   135 		sa = new SimpleAdapter(this, hatsData, R.layout.spinner_textimg_entry,
   134 				new String[] { "txt", "img" }, new int[] { R.id.spinner_txt,
   136 				new String[] { "txt", "img" }, new int[] { R.id.spinner_txt,
   135 						R.id.spinner_img });
   137 				R.id.spinner_img });
   136 		sa.setDropDownViewResource(R.layout.spinner_textimg_dropdown_entry);
   138 		sa.setDropDownViewResource(R.layout.spinner_textimg_dropdown_entry);
   137 		sa.setViewBinder(viewBinder);
   139 		sa.setViewBinder(viewBinder);
   138 		for (Spinner spin : hogHat) {
   140 		for (Spinner spin : hogHat) {
   139 			spin.setAdapter(sa);
   141 			spin.setAdapter(sa);
   140 		}
   142 		}
   141 
   143 
   142 		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
   144 		voicesData = new ArrayList<String>();
   143 				R.layout.listview_item, FrontendDataUtils.getVoices(this));
   145 		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listview_item, voicesData);
   144 		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
   146 		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
   145 		voice.setAdapter(adapter);
   147 		voice.setAdapter(adapter);
   146 		voice.setOnFocusChangeListener(focusser);
   148 		voice.setOnFocusChangeListener(focusser);
   147 		voiceButton.setOnClickListener(voiceClicker);
   149 		voiceButton.setOnClickListener(voiceClicker);
   148 
   150 
   149 		adapter = new ArrayAdapter<String>(this, R.layout.listview_item,
   151 		fortsData = new ArrayList<String>();
   150 				FrontendDataUtils.getForts(this));
   152 		adapter = new ArrayAdapter<String>(this, R.layout.listview_item, fortsData);
   151 		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
   153 		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
   152 		fort.setAdapter(adapter);
   154 		fort.setAdapter(adapter);
   153 		fort.setOnItemSelectedListener(fortSelector);
   155 		fort.setOnItemSelectedListener(fortSelector);
   154 		fort.setOnFocusChangeListener(focusser);
   156 		fort.setOnFocusChangeListener(focusser);
   155 
   157 
   156 		Team t = this.getIntent().getParcelableExtra("team");
   158 		new Thread(this).start();
   157 		if (t != null) {
   159 	}
   158 			name.setText(t.name);
   160 
   159 			int position = ((ArrayAdapter<String>) voice.getAdapter())
   161 	public void run(){
   160 					.getPosition(t.voice);
   162 		try {
   161 			voice.setSelection(position);
   163 			Thread.sleep(2000);
   162 
   164 		} catch (InterruptedException e) {
   163 			position = ((ArrayAdapter<String>) fort.getAdapter())
   165 			// TODO Auto-generated catch block
   164 					.getPosition(t.fort);
   166 			e.printStackTrace();
   165 			fort.setSelection(position);
   167 		}
   166 
   168 		ArrayList<HashMap<String, ?>> gravesData = FrontendDataUtils.getGraves(this);
   167 			position = 0;
   169 		ArrayList<HashMap<String, ?>> flagsData = FrontendDataUtils.getFlags(this);
   168 			for (HashMap<String, ?> hashmap : typesData) {
   170 		ArrayList<HashMap<String, ?>> typesData = FrontendDataUtils.getTypes(this);
   169 				if (hashmap.get("txt").equals(t.levels[0])) {
   171 		ArrayList<HashMap<String, ?>> hatsData = FrontendDataUtils.getHats(this);
   170 					difficulty.setSelection(position);
   172 		ArrayList<String> voicesData = FrontendDataUtils.getVoices(this);
   171 					break;
   173 		ArrayList<String> fortsData = FrontendDataUtils.getForts(this);
   172 				}
   174 		
   173 			}
   175 		copy(this.gravesData, gravesData);
   174 
   176 		copy(this.flagsData, flagsData);
   175 			position = 0;
   177 		copy(this.typesData, typesData);
   176 			for (HashMap<String, ?> hashmap : gravesData) {
   178 		copy(this.hatsData, hatsData);
   177 				if (hashmap.get("txt").equals(t.grave)) {
   179 		copy(this.voicesData, voicesData);
   178 					grave.setSelection(position);
   180 		copy(this.fortsData, fortsData);
   179 					break;
   181 		
   180 				}
   182 		this.runOnUiThread(new Runnable(){
   181 			}
   183 			public void run() {
   182 
   184 				((SimpleAdapter)grave.getAdapter()).notifyDataSetChanged();
   183 			position = 0;
   185 				((SimpleAdapter)flag.getAdapter()).notifyDataSetChanged();
   184 			for (HashMap<String, ?> hashmap : typesData) {
   186 				((SimpleAdapter)difficulty.getAdapter()).notifyDataSetChanged();
   185 				if (hashmap.get("txt").equals(t.flag)) {
   187 				((SimpleAdapter)hogHat.get(0).getAdapter()).notifyDataSetChanged();				
   186 					flag.setSelection(position);
   188 				((ArrayAdapter<String>)fort.getAdapter()).notifyDataSetChanged();
   187 					break;
   189 				((ArrayAdapter<String>)voice.getAdapter()).notifyDataSetChanged();
   188 				}
   190 			}
   189 			}
   191 		});		
   190 
   192 
   191 			for (int i = 0; i < Team.maxNumberOfHogs; i++) {
   193 	}
   192 				position = 0;
   194 	
   193 				for (HashMap<String, ?> hashmap : hatsData) {
   195 	private static <T> void copy(List<T> dest, List<T> src){
   194 					if (hashmap.get("txt").equals(t.hats[i])) {
   196 		for(T t: src) dest.add(t);
   195 						hogHat.get(i).setSelection(position);
       
   196 					}
       
   197 				}
       
   198 
       
   199 				hogName.get(i).setText(t.hogNames[i]);
       
   200 			}
       
   201 			this.fileName = t.file;
       
   202 		}
       
   203 	}
   197 	}
   204 
   198 
   205 	public void onDestroy() {
   199 	public void onDestroy() {
   206 		super.onDestroy();
   200 		super.onDestroy();
   207 		if (mp != null) {
   201 		if (mp != null) {
   302 					.getDataPath(TeamCreatorActivity.this)
   296 					.getDataPath(TeamCreatorActivity.this)
   303 					+ "Forts/"
   297 					+ "Forts/"
   304 					+ fortName + "L.png");
   298 					+ fortName + "L.png");
   305 			imgFort.setImageDrawable(fortIconDrawable);
   299 			imgFort.setImageDrawable(fortIconDrawable);
   306 			scroller.fullScroll(ScrollView.FOCUS_DOWN);// Scroll the scrollview
   300 			scroller.fullScroll(ScrollView.FOCUS_DOWN);// Scroll the scrollview
   307 														// to the bottom, work
   301 			// to the bottom, work
   308 														// around for scollview
   302 			// around for scollview
   309 														// invalidation (scrolls
   303 			// invalidation (scrolls
   310 														// back to top)
   304 			// back to top)
   311 		}
   305 		}
   312 
   306 
   313 		public void onNothingSelected(AdapterView<?> arg0) {
   307 		public void onNothingSelected(AdapterView<?> arg0) {
   314 		}
   308 		}
   315 
   309 
   342 				e.printStackTrace();
   336 				e.printStackTrace();
   343 			}
   337 			}
   344 		}
   338 		}
   345 	};
   339 	};
   346 
   340 
       
   341 	private void setTeamValues(Team t){
       
   342 
       
   343 		if (t != null) {
       
   344 			name.setText(t.name);
       
   345 			int position = ((ArrayAdapter<String>) voice.getAdapter()).getPosition(t.voice);
       
   346 			voice.setSelection(position);
       
   347 
       
   348 			position = ((ArrayAdapter<String>) fort.getAdapter()).getPosition(t.fort);
       
   349 			fort.setSelection(position);
       
   350 
       
   351 			position = 0;
       
   352 			for (HashMap<String, ?> hashmap : typesData) {
       
   353 				if (hashmap.get("txt").equals(t.levels[0])) {
       
   354 					difficulty.setSelection(position);
       
   355 					break;
       
   356 				}
       
   357 			}
       
   358 
       
   359 			position = 0;
       
   360 			for (HashMap<String, ?> hashmap : gravesData) {
       
   361 				if (hashmap.get("txt").equals(t.grave)) {
       
   362 					grave.setSelection(position);
       
   363 					break;
       
   364 				}
       
   365 			}
       
   366 
       
   367 			position = 0;
       
   368 			for (HashMap<String, ?> hashmap : typesData) {
       
   369 				if (hashmap.get("txt").equals(t.flag)) {
       
   370 					flag.setSelection(position);
       
   371 					break;
       
   372 				}
       
   373 			}
       
   374 
       
   375 			for (int i = 0; i < Team.maxNumberOfHogs; i++) {
       
   376 				position = 0;
       
   377 				for (HashMap<String, ?> hashmap : hatsData) {
       
   378 					if (hashmap.get("txt").equals(t.hats[i])) {
       
   379 						hogHat.get(i).setSelection(position);
       
   380 					}
       
   381 				}
       
   382 
       
   383 				hogName.get(i).setText(t.hogNames[i]);
       
   384 			}
       
   385 			this.fileName = t.file;
       
   386 		}
       
   387 	}
       
   388 
       
   389 
   347 	private SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() {
   390 	private SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() {
   348 
   391 
   349 		public boolean setViewValue(View view, Object data,
   392 		public boolean setViewValue(View view, Object data,
   350 				String textRepresentation) {
   393 				String textRepresentation) {
   351 			if (view instanceof ImageView && data instanceof Bitmap) {
   394 			if (view instanceof ImageView && data instanceof Bitmap) {