project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/TeamSelectionActivity.java
branchhedgeroid
changeset 6047 10011f051f9c
parent 6045 9a7cc0f29430
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.mobile;
       
    21 
       
    22 import java.io.File;
       
    23 import java.util.ArrayList;
       
    24 import java.util.HashMap;
       
    25 
       
    26 import org.hedgewars.mobile.EngineProtocol.FrontendDataUtils;
       
    27 import org.hedgewars.mobile.EngineProtocol.Team;
       
    28 
       
    29 import android.app.Activity;
       
    30 import android.content.Intent;
       
    31 import android.os.Bundle;
       
    32 import android.os.Parcelable;
       
    33 import android.view.ContextMenu;
       
    34 import android.view.MenuItem;
       
    35 import android.view.View;
       
    36 import android.view.View.OnClickListener;
       
    37 import android.widget.AdapterView;
       
    38 import android.widget.AdapterView.AdapterContextMenuInfo;
       
    39 import android.widget.AdapterView.OnItemClickListener;
       
    40 import android.widget.ImageButton;
       
    41 import android.widget.ImageView;
       
    42 import android.widget.ListView;
       
    43 import android.widget.RelativeLayout;
       
    44 import android.widget.SimpleAdapter;
       
    45 import android.widget.SimpleAdapter.ViewBinder;
       
    46 import android.widget.TextView;
       
    47 
       
    48 public class TeamSelectionActivity extends Activity{
       
    49 
       
    50 	private static final int ACTIVITY_TEAMCREATION = 0;
       
    51 
       
    52 	private ImageButton addTeam, back;
       
    53 	private ListView availableTeams, selectedTeams;
       
    54 	private ArrayList<HashMap<String, Object>> availableTeamsList, selectedTeamsList;
       
    55 	private TextView txtInfo;
       
    56 
       
    57 	public void onCreate(Bundle savedInstanceState){
       
    58 		super.onCreate(savedInstanceState);
       
    59 
       
    60 		setContentView(R.layout.team_selector);
       
    61 
       
    62 		addTeam = (ImageButton) findViewById(R.id.btnAdd);
       
    63 		back = (ImageButton) findViewById(R.id.btnBack);
       
    64 		txtInfo = (TextView) findViewById(R.id.txtInfo);
       
    65 
       
    66 		addTeam.setOnClickListener(addTeamClicker);
       
    67 		back.setOnClickListener(backClicker);
       
    68 
       
    69 		availableTeams = (ListView) findViewById(R.id.availableTeams);
       
    70 		availableTeamsList = FrontendDataUtils.getTeams(this);
       
    71 		SimpleAdapter adapter = new SimpleAdapter(this, availableTeamsList, R.layout.team_selection_entry_simple, new String[]{"txt", "img"}, new int[]{R.id.txtName, R.id.imgDifficulty});
       
    72 		availableTeams.setAdapter(adapter);
       
    73 		registerForContextMenu(availableTeams);
       
    74 		availableTeams.setOnItemClickListener(availableClicker);
       
    75 
       
    76 		selectedTeams = (ListView) findViewById(R.id.selectedTeams);
       
    77 		selectedTeamsList = new ArrayList<HashMap<String, Object>>();
       
    78 		ArrayList<HashMap<String, ?>> toBeRemoved = new ArrayList<HashMap<String, ?>>();
       
    79 		ArrayList<Team> teamsStartGame = getIntent().getParcelableArrayListExtra("teams");
       
    80 		for(HashMap<String, Object> hashmap : availableTeamsList){
       
    81 			for(Team t : teamsStartGame){
       
    82 				if(((Team)hashmap.get("team")).equals(t)){
       
    83 					toBeRemoved.add(hashmap);
       
    84 					selectedTeamsList.add(FrontendDataUtils.teamToHashMap(t));//create a new hashmap to ensure all variables are entered into the map
       
    85 				}
       
    86 			}
       
    87 		}
       
    88 		for(HashMap<String, ?> hashmap : toBeRemoved) availableTeamsList.remove(hashmap);
       
    89 
       
    90 		adapter = new SimpleAdapter(this, selectedTeamsList, R.layout.team_selection_entry, new String[]{"txt", "img", "color", "count"}, new int[]{R.id.txtName, R.id.imgDifficulty, R.id.teamColor, R.id.teamCount});
       
    91 		adapter.setViewBinder(viewBinder);
       
    92 		selectedTeams.setAdapter(adapter);
       
    93 		selectedTeams.setOnItemClickListener(selectedClicker);
       
    94 
       
    95 		txtInfo.setText(String.format(getResources().getString(R.string.teams_info_template), selectedTeams.getChildCount()));
       
    96 	}
       
    97 
       
    98 	private ViewBinder viewBinder = new ViewBinder(){
       
    99 		public boolean setViewValue(View view, Object data,	String textRepresentation) {
       
   100 			switch(view.getId()){
       
   101 			case R.id.teamColor:
       
   102 				setTeamColor(view, (Integer)data);
       
   103 				return true;
       
   104 			case R.id.teamCount:
       
   105 				setTeamHogCount((ImageView)view, (Integer)data);
       
   106 				return true;
       
   107 			default:
       
   108 				return false;
       
   109 			}
       
   110 		}
       
   111 	};
       
   112 
       
   113 	public void onActivityResult(int requestCode, int resultCode, Intent data){
       
   114 		if(requestCode == ACTIVITY_TEAMCREATION){
       
   115 			if(resultCode == Activity.RESULT_OK){
       
   116 				updateListViews();
       
   117 			}
       
   118 		}else{
       
   119 			super.onActivityResult(requestCode, resultCode, data);
       
   120 		}
       
   121 	}
       
   122 
       
   123 	private void updateListViews(){
       
   124 		unregisterForContextMenu(availableTeams);
       
   125 		availableTeamsList = FrontendDataUtils.getTeams(this);
       
   126 		ArrayList<HashMap<String, Object>> toBeRemoved = new ArrayList<HashMap<String, Object>>();
       
   127 		for(HashMap<String, Object> hashmap : selectedTeamsList){
       
   128 			String name = (String)hashmap.get("txt");
       
   129 
       
   130 			for(HashMap<String, Object> hash : availableTeamsList){
       
   131 				if(name.equals((String)hash.get("txt"))){
       
   132 					toBeRemoved.add(hash);
       
   133 				}
       
   134 			}
       
   135 		}
       
   136 		for(HashMap<String, Object> hash: toBeRemoved) availableTeamsList.remove(hash);
       
   137 
       
   138 		SimpleAdapter adapter = new SimpleAdapter(this, availableTeamsList, R.layout.team_selection_entry, new String[]{"txt", "img"}, new int[]{R.id.txtName, R.id.imgDifficulty});
       
   139 		availableTeams.setAdapter(adapter);
       
   140 		registerForContextMenu(availableTeams);
       
   141 		availableTeams.setOnItemClickListener(availableClicker);
       
   142 
       
   143 
       
   144 	}
       
   145 
       
   146 	private void setTeamColor(int position, int color){
       
   147 		View iv = ((RelativeLayout)selectedTeams.getChildAt(position)).findViewById(R.id.teamCount);
       
   148 		setTeamColor(iv, color);
       
   149 	}
       
   150 	private void setTeamColor(View iv, int color){
       
   151 		iv.setBackgroundColor(0xFF000000 + color);
       
   152 	}
       
   153 
       
   154 	private void setTeamHogCount(int position, int count){
       
   155 		ImageView iv = (ImageView)((RelativeLayout)selectedTeams.getChildAt(position)).findViewById(R.id.teamCount);
       
   156 		setTeamHogCount(iv, count);
       
   157 	}
       
   158 
       
   159 	private void setTeamHogCount(ImageView iv, int count){
       
   160 
       
   161 		switch(count){
       
   162 		case 0:
       
   163 			iv.setImageResource(R.drawable.teamcount0);
       
   164 			break;
       
   165 		case 1:
       
   166 			iv.setImageResource(R.drawable.teamcount1);
       
   167 			break;
       
   168 		case 2:
       
   169 			iv.setImageResource(R.drawable.teamcount2);
       
   170 			break;
       
   171 		case 3:
       
   172 			iv.setImageResource(R.drawable.teamcount3);
       
   173 			break;
       
   174 		case 4:
       
   175 			iv.setImageResource(R.drawable.teamcount4);
       
   176 			break;
       
   177 		case 5:
       
   178 			iv.setImageResource(R.drawable.teamcount5);
       
   179 			break;
       
   180 		case 6:
       
   181 			iv.setImageResource(R.drawable.teamcount6);
       
   182 			break;
       
   183 		case 7:
       
   184 			iv.setImageResource(R.drawable.teamcount7);
       
   185 			break;
       
   186 		case 8:
       
   187 			iv.setImageResource(R.drawable.teamcount8);
       
   188 			break;
       
   189 		case 9:
       
   190 			iv.setImageResource(R.drawable.teamcount9);
       
   191 			break;
       
   192 		}
       
   193 	}
       
   194 
       
   195 	public void onBackPressed(){
       
   196 		returnTeams();
       
   197 		super.onBackPressed();
       
   198 	}
       
   199 
       
   200 	private OnClickListener addTeamClicker = new OnClickListener(){
       
   201 		public void onClick(View v) {
       
   202 			startActivityForResult(new Intent(TeamSelectionActivity.this, TeamCreatorActivity.class), ACTIVITY_TEAMCREATION);
       
   203 		}
       
   204 	};
       
   205 
       
   206 	private OnClickListener backClicker = new OnClickListener(){
       
   207 		public void onClick(View v){
       
   208 			returnTeams();
       
   209 			finish();
       
   210 		}
       
   211 	};
       
   212 
       
   213 	private OnItemClickListener availableClicker = new OnItemClickListener(){
       
   214 		public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
       
   215 			selectAvailableTeamsItem(position);
       
   216 		}
       
   217 	};
       
   218 	private OnItemClickListener selectedClicker = new OnItemClickListener(){
       
   219 		public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
       
   220 			availableTeamsList.add((HashMap<String, Object>) selectedTeamsList.get(position));
       
   221 			selectedTeamsList.remove(position);
       
   222 			((SimpleAdapter)availableTeams.getAdapter()).notifyDataSetChanged();
       
   223 			((SimpleAdapter)selectedTeams.getAdapter()).notifyDataSetChanged();
       
   224 
       
   225 			txtInfo.setText(String.format(getResources().getString(R.string.teams_info_template), selectedTeamsList.size()));
       
   226 		}
       
   227 
       
   228 	};
       
   229 
       
   230 	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuinfo){
       
   231 		menu.add(ContextMenu.NONE, 0, ContextMenu.NONE, R.string.select);
       
   232 		menu.add(ContextMenu.NONE, 2, ContextMenu.NONE, R.string.edit);
       
   233 		menu.add(ContextMenu.NONE, 1, ContextMenu.NONE, R.string.delete);
       
   234 
       
   235 	}
       
   236 	public boolean onContextItemSelected(MenuItem item){
       
   237 		AdapterView.AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
       
   238 		int position = menuInfo.position;
       
   239 		switch(item.getItemId()){
       
   240 		case 0://select
       
   241 			selectAvailableTeamsItem(position);
       
   242 			return true;
       
   243 		case 1://delete
       
   244 			File f = new File(String.format("%s/%s/%s.xml", TeamSelectionActivity.this.getFilesDir(), Team.DIRECTORY_TEAMS, availableTeamsList.get(position).get("txt")));
       
   245 			f.delete();
       
   246 			availableTeamsList.remove(position);
       
   247 			((SimpleAdapter)availableTeams.getAdapter()).notifyDataSetChanged();
       
   248 			return true;
       
   249 		case 2://edit
       
   250 			Intent i = new Intent(TeamSelectionActivity.this, TeamCreatorActivity.class);
       
   251 			Team t = (Team)availableTeamsList.get(position).get("team");
       
   252 			i.putExtra("team", t);
       
   253 			startActivityForResult(i, ACTIVITY_TEAMCREATION);
       
   254 			return true;
       
   255 		}
       
   256 		return false;
       
   257 	}
       
   258 
       
   259 	private void selectAvailableTeamsItem(int position){
       
   260 		HashMap<String, Object> hash = (HashMap<String, Object>) availableTeamsList.get(position);
       
   261 		Team t = (Team)hash.get("team");
       
   262 		int[] illegalcolors = new int[selectedTeamsList.size()];
       
   263 		for(int i = 0; i < selectedTeamsList.size(); i++){
       
   264 			illegalcolors[i] = ((Team)selectedTeamsList.get(i).get("team")).color;
       
   265 		}
       
   266 		t.setRandomColor(illegalcolors);
       
   267 		hash.put("color", t.color);
       
   268 		hash.put("count", t.hogCount);
       
   269 
       
   270 		selectedTeamsList.add(hash);
       
   271 		availableTeamsList.remove(position);
       
   272 		((SimpleAdapter)availableTeams.getAdapter()).notifyDataSetChanged();
       
   273 		((SimpleAdapter)selectedTeams.getAdapter()).notifyDataSetChanged();
       
   274 
       
   275 		txtInfo.setText(String.format(getResources().getString(R.string.teams_info_template), selectedTeamsList.size()));
       
   276 	}
       
   277 
       
   278 	private void returnTeams(){
       
   279 		int teamsCount = selectedTeamsList.size();
       
   280 		Intent i = new Intent();
       
   281 		Parcelable[] teams = new Parcelable[teamsCount];
       
   282 		for(int x = 0 ; x < teamsCount; x++){
       
   283 			teams[x] = (Team)selectedTeamsList.get(x).get("team");
       
   284 		}
       
   285 		i.putExtra("teams", teams);
       
   286 		setResult(Activity.RESULT_OK, i);
       
   287 
       
   288 	}
       
   289 }