project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/TeamSelectionActivity.java
author Xeli
Thu, 11 Aug 2011 14:59:14 +0200
branchhedgeroid
changeset 5542 141c12a23787
parent 5532 3d7ac2b3b703
child 5603 4e4a579a60af
permissions -rw-r--r--
start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5467
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
     1
package org.hedgewars.mobile;
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
     2
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
     3
import java.io.File;
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
     4
import java.util.ArrayList;
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
     5
import java.util.HashMap;
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
     6
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
     7
import org.hedgewars.mobile.EngineProtocol.FrontendDataUtils;
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
     8
import org.hedgewars.mobile.EngineProtocol.Team;
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
     9
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    10
import android.app.Activity;
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    11
import android.content.Intent;
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    12
import android.os.Bundle;
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    13
import android.os.Parcelable;
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    14
import android.view.ContextMenu;
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    15
import android.view.MenuItem;
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    16
import android.view.View;
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    17
import android.view.View.OnClickListener;
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    18
import android.widget.AdapterView;
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    19
import android.widget.AdapterView.AdapterContextMenuInfo;
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    20
import android.widget.AdapterView.OnItemClickListener;
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    21
import android.widget.ImageButton;
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
    22
import android.widget.ImageView;
5467
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    23
import android.widget.ListView;
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
    24
import android.widget.RelativeLayout;
5467
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    25
import android.widget.SimpleAdapter;
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
    26
import android.widget.SimpleAdapter.ViewBinder;
5467
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    27
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    28
public class TeamSelectionActivity extends Activity{
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    29
5542
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
    30
	private static final int ACTIVITY_TEAMCREATION = 0;
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
    31
	
5467
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    32
	private ImageButton addTeam, back;
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    33
	private ListView availableTeams, selectedTeams;
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    34
	private ArrayList<HashMap<String, ?>> availableTeamsList, selectedTeamsList;
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    35
	private int minTeams = 2;
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    36
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    37
	public void onCreate(Bundle savedInstanceState){
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    38
		super.onCreate(savedInstanceState);
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    39
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    40
		setContentView(R.layout.team_selector);
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    41
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    42
		addTeam = (ImageButton) findViewById(R.id.btnAdd);
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    43
		back = (ImageButton) findViewById(R.id.btnBack);
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
    44
5467
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    45
		addTeam.setOnClickListener(addTeamClicker);
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    46
		back.setOnClickListener(backClicker);
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    47
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    48
		availableTeams = (ListView) findViewById(R.id.availableTeams);
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    49
		availableTeamsList = FrontendDataUtils.getTeams(this);
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    50
		SimpleAdapter adapter = new SimpleAdapter(this, availableTeamsList, R.layout.team_selection_entry, new String[]{"txt", "img"}, new int[]{R.id.txtName, R.id.imgDifficulty});
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    51
		availableTeams.setAdapter(adapter);
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    52
		registerForContextMenu(availableTeams);
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    53
		availableTeams.setOnItemClickListener(availableClicker);
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
    54
5467
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    55
		selectedTeams = (ListView) findViewById(R.id.selectedTeams);
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    56
		selectedTeamsList = new ArrayList<HashMap<String, ?>>();
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    57
		ArrayList<HashMap<String, ?>> toBeRemoved = new ArrayList<HashMap<String, ?>>();
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    58
		ArrayList<Team> teamsStartGame = getIntent().getParcelableArrayListExtra("teams");
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    59
		for(HashMap<String, ?> hashmap : availableTeamsList){
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    60
			for(Team t : teamsStartGame){
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    61
				if(((Team)hashmap.get("team")).equals(t)){
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    62
					toBeRemoved.add(hashmap);
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    63
					selectedTeamsList.add(hashmap);
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    64
				}
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    65
			}
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    66
		}
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    67
		for(HashMap<String, ?> hashmap : toBeRemoved) availableTeamsList.remove(hashmap);
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
    68
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
    69
		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});
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
    70
		adapter.setViewBinder(viewBinder);
5467
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    71
		selectedTeams.setAdapter(adapter);
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
    72
		selectedTeams.setOnItemClickListener(selectedClicker);
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
    73
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
    74
	}
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
    75
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
    76
	private ViewBinder viewBinder = new ViewBinder(){
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
    77
		public boolean setViewValue(View view, Object data,	String textRepresentation) {
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
    78
			switch(view.getId()){
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
    79
			case R.id.teamColor:
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
    80
				setTeamColor(view, (Integer)data);
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
    81
				return true;
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
    82
			case R.id.teamCount:
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
    83
				setTeamHogCount((ImageView)view, (Integer)data);
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
    84
				return true;
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
    85
			default:
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
    86
				return false;
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
    87
			}
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
    88
		}
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
    89
	};
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
    90
5542
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
    91
	public void onActivityResult(int requestCode, int resultCode, Intent data){
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
    92
		if(requestCode == ACTIVITY_TEAMCREATION){
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
    93
			if(resultCode == Activity.RESULT_OK){
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
    94
				updateListViews();
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
    95
			}
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
    96
		}else{
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
    97
			super.onActivityResult(requestCode, resultCode, data);
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
    98
		}
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
    99
	}
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
   100
	
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
   101
	private void updateListViews(){
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
   102
		unregisterForContextMenu(availableTeams);
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
   103
		availableTeamsList = FrontendDataUtils.getTeams(this);
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
   104
		for(HashMap<String, ?> hashmap : selectedTeamsList){
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
   105
			String name = (String)hashmap.get("txt");
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
   106
			for(HashMap<String, ?> hash : availableTeamsList){
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
   107
				if(name.equals((String)hash.get("txt"))){
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
   108
					availableTeamsList.remove(hash);
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
   109
				}
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
   110
			}
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
   111
		}
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
   112
		SimpleAdapter adapter = new SimpleAdapter(this, availableTeamsList, R.layout.team_selection_entry, new String[]{"txt", "img"}, new int[]{R.id.txtName, R.id.imgDifficulty});
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
   113
		availableTeams.setAdapter(adapter);
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
   114
		registerForContextMenu(availableTeams);
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
   115
		availableTeams.setOnItemClickListener(availableClicker);
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
   116
		
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
   117
		
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
   118
	}
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
   119
	
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   120
	private void setTeamColor(int position, int color){
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   121
		View iv = ((RelativeLayout)selectedTeams.getChildAt(position)).findViewById(R.id.teamCount);
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   122
		setTeamColor(iv, color);
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   123
	}
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   124
	private void setTeamColor(View iv, int color){
5532
3d7ac2b3b703 Fixed team colors in the UI, delete and a nullpointer bug when no teams are created
Xeli
parents: 5506
diff changeset
   125
		iv.setBackgroundColor(0xFF000000 + color);
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   126
	}
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   127
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   128
	private void setTeamHogCount(int position, int count){
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   129
		ImageView iv = (ImageView)((RelativeLayout)selectedTeams.getChildAt(position)).findViewById(R.id.teamCount);
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   130
		setTeamHogCount(iv, count);
5467
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   131
	}
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   132
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   133
	private void setTeamHogCount(ImageView iv, int count){
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   134
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   135
		switch(count){
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   136
		case 0:
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   137
			iv.setImageResource(R.drawable.teamcount0);
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   138
			break;
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   139
		case 1:
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   140
			iv.setImageResource(R.drawable.teamcount1);
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   141
			break;
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   142
		case 2:
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   143
			iv.setImageResource(R.drawable.teamcount2);
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   144
			break;
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   145
		case 3:
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   146
			iv.setImageResource(R.drawable.teamcount3);
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   147
			break;
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   148
		case 4:
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   149
			iv.setImageResource(R.drawable.teamcount4);
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   150
			break;
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   151
		case 5:
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   152
			iv.setImageResource(R.drawable.teamcount5);
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   153
			break;
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   154
		case 6:
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   155
			iv.setImageResource(R.drawable.teamcount6);
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   156
			break;
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   157
		case 7:
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   158
			iv.setImageResource(R.drawable.teamcount7);
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   159
			break;
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   160
		case 8:
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   161
			iv.setImageResource(R.drawable.teamcount8);
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   162
			break;
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   163
		case 9:
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   164
			iv.setImageResource(R.drawable.teamcount9);
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   165
			break;
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   166
		}
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   167
	}
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   168
5467
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   169
	public void onBackPressed(){
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   170
		returnTeams();
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   171
		super.onBackPressed();
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   172
	}
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   173
5467
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   174
	private OnClickListener addTeamClicker = new OnClickListener(){
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   175
		public void onClick(View v) {
5542
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
   176
			startActivityForResult(new Intent(TeamSelectionActivity.this, TeamCreatorActivity.class), ACTIVITY_TEAMCREATION);
5467
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   177
		}
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   178
	};
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   179
5467
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   180
	private OnClickListener backClicker = new OnClickListener(){
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   181
		public void onClick(View v){
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   182
			returnTeams();
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   183
			finish();
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   184
		}
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   185
	};
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   186
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   187
	private OnItemClickListener availableClicker = new OnItemClickListener(){
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   188
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   189
			selectAvailableTeamsItem(position);
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   190
		}
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   191
	};
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   192
	private OnItemClickListener selectedClicker = new OnItemClickListener(){
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   193
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   194
			availableTeamsList.add((HashMap<String, ?>) selectedTeamsList.get(position));
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   195
			selectedTeamsList.remove(position);
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   196
			((SimpleAdapter)availableTeams.getAdapter()).notifyDataSetChanged();
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   197
			((SimpleAdapter)selectedTeams.getAdapter()).notifyDataSetChanged();
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   198
		}
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   199
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   200
	};
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   201
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   202
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuinfo){
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   203
		menu.add(ContextMenu.NONE, 0, ContextMenu.NONE, R.string.select);
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   204
		menu.add(ContextMenu.NONE, 2, ContextMenu.NONE, R.string.edit);
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   205
		menu.add(ContextMenu.NONE, 1, ContextMenu.NONE, R.string.delete);
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   206
5467
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   207
	}
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   208
	public boolean onContextItemSelected(MenuItem item){
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   209
		AdapterView.AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   210
		int position = menuInfo.position;
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   211
		switch(item.getItemId()){
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   212
		case 0://select
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   213
			selectAvailableTeamsItem(position);
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   214
			return true;
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   215
		case 1://delete
5532
3d7ac2b3b703 Fixed team colors in the UI, delete and a nullpointer bug when no teams are created
Xeli
parents: 5506
diff changeset
   216
			File f = new File(String.format("%s/%s/%s.xml", TeamSelectionActivity.this.getFilesDir(), Team.DIRECTORY_TEAMS, availableTeamsList.get(position).get("txt")));
3d7ac2b3b703 Fixed team colors in the UI, delete and a nullpointer bug when no teams are created
Xeli
parents: 5506
diff changeset
   217
			f.delete();
5467
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   218
			availableTeamsList.remove(position);
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   219
			((SimpleAdapter)availableTeams.getAdapter()).notifyDataSetChanged();
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   220
			return true;
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   221
		case 2://edit
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   222
			Intent i = new Intent(TeamSelectionActivity.this, TeamCreatorActivity.class);
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   223
			Team t = (Team)availableTeamsList.get(position).get("team");
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   224
			i.putExtra("team", t);
5542
141c12a23787 start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
Xeli
parents: 5532
diff changeset
   225
			startActivityForResult(i, ACTIVITY_TEAMCREATION);
5467
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   226
			return true;
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   227
		}
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   228
		return false;
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   229
	}
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   230
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   231
	private void selectAvailableTeamsItem(int position){
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   232
		HashMap<String, Object> hash = (HashMap<String, Object>) availableTeamsList.get(position);
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   233
		Team t = (Team)hash.get("team");
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   234
		int[] illegalcolors = new int[selectedTeamsList.size()];
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   235
		for(int i = 0; i < selectedTeamsList.size(); i++){
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   236
			illegalcolors[i] = ((Team)selectedTeamsList.get(i).get("team")).color;
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   237
		}
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   238
		t.setRandomColor(illegalcolors);
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   239
		hash.put("color", t.color);
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   240
		hash.put("count", t.hogCount);
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   241
		
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5467
diff changeset
   242
		selectedTeamsList.add(hash);
5467
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   243
		availableTeamsList.remove(position);
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   244
		((SimpleAdapter)availableTeams.getAdapter()).notifyDataSetChanged();
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   245
		((SimpleAdapter)selectedTeams.getAdapter()).notifyDataSetChanged();
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   246
	}
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   247
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   248
	private void returnTeams(){
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   249
		int teamsCount = selectedTeamsList.size();
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   250
		if(teamsCount >= minTeams){
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   251
			Intent i = new Intent();
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   252
			Parcelable[] teams = new Parcelable[teamsCount];
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   253
			for(int x = 0 ; x < teamsCount; x++){
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   254
				teams[x] = (Team)selectedTeamsList.get(x).get("team");
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   255
			}
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   256
			i.putExtra("teams", teams);
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   257
			setResult(Activity.RESULT_OK, i);
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   258
		}else{
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   259
			setResult(Activity.RESULT_CANCELED);
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   260
		}
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   261
	}
88e25840f532 Main activities for starting up the game, changing gameconfig, selecting and creating teams with their respective layouts and values
Xeli
parents:
diff changeset
   262
}