project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/TeamListActivity.java
changeset 7582 714310efad8f
child 7584 7831c84cc644
equal deleted inserted replaced
7580:c92596feac0d 7582:714310efad8f
       
     1 /*
       
     2  * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2011-2012 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.hedgeroid;
       
    21 
       
    22 import java.util.ArrayList;
       
    23 import java.util.Collections;
       
    24 import java.util.HashMap;
       
    25 import java.util.List;
       
    26 import java.util.Map;
       
    27 
       
    28 import org.hedgewars.hedgeroid.Datastructures.FrontendDataUtils;
       
    29 import org.hedgewars.hedgeroid.Datastructures.Team;
       
    30 
       
    31 import android.app.ListActivity;
       
    32 import android.content.Intent;
       
    33 import android.os.Bundle;
       
    34 import android.view.ContextMenu;
       
    35 import android.view.MenuItem;
       
    36 import android.view.View;
       
    37 import android.view.View.OnClickListener;
       
    38 import android.widget.AdapterView;
       
    39 import android.widget.AdapterView.AdapterContextMenuInfo;
       
    40 import android.widget.AdapterView.OnItemClickListener;
       
    41 import android.widget.ImageButton;
       
    42 import android.widget.SimpleAdapter;
       
    43 
       
    44 public class TeamListActivity extends ListActivity implements OnItemClickListener {
       
    45 	private List<Team> teams;
       
    46 	private ImageButton addButton;
       
    47 
       
    48 	@Override
       
    49 	protected void onCreate(Bundle savedInstanceState) {
       
    50 		super.onCreate(savedInstanceState);
       
    51 		setContentView(R.layout.activity_teamlist);
       
    52 		addButton = (ImageButton)findViewById(R.id.btnAdd);
       
    53 		addButton.setOnClickListener(new OnClickListener() {
       
    54 			public void onClick(View v) {
       
    55 				editTeam(null);
       
    56 			}
       
    57 		});
       
    58 	}
       
    59 	
       
    60 	@Override
       
    61 	public void onResume() {
       
    62 		super.onResume();
       
    63 		updateList();
       
    64 		getListView().setOnItemClickListener(this);
       
    65 		registerForContextMenu(getListView());
       
    66 	}
       
    67 	
       
    68 	public void onItemClick(AdapterView<?> adapterView, View v, int position, long arg3) {
       
    69 		editTeam(teams.get(position).name);
       
    70 	}
       
    71 	
       
    72 	@Override
       
    73 	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuinfo){
       
    74 		menu.add(ContextMenu.NONE, 0, ContextMenu.NONE, R.string.edit);
       
    75 		menu.add(ContextMenu.NONE, 1, ContextMenu.NONE, R.string.delete);
       
    76 	}
       
    77 	
       
    78 	@Override
       
    79 	public boolean onContextItemSelected(MenuItem item){
       
    80 		AdapterView.AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
       
    81 		int position = menuInfo.position;
       
    82 		Team team = teams.get(position);
       
    83 		switch(item.getItemId()){
       
    84 		case 0:
       
    85 			editTeam(team.name);
       
    86 			return true;
       
    87 		case 1:
       
    88 			Team.getTeamfileByName(getApplicationContext(), team.name).delete();
       
    89 			updateList();
       
    90 			return true;
       
    91 		}
       
    92 		return false;
       
    93 	}
       
    94 	
       
    95 	private void updateList() {
       
    96 		teams = FrontendDataUtils.getTeams(getApplicationContext());
       
    97 		Collections.sort(teams, Team.NAME_ORDER);
       
    98 		SimpleAdapter adapter = new SimpleAdapter(this, teamsToMaps(teams), R.layout.team_selection_entry_simple, new String[]{"txt", "img"}, new int[]{R.id.txtName, R.id.imgDifficulty});
       
    99 		setListAdapter(adapter);
       
   100 	}
       
   101 	
       
   102 	private void editTeam(String teamName) {
       
   103 		Intent i = new Intent(this, TeamCreatorActivity.class);
       
   104 		i.putExtra(TeamCreatorActivity.PARAMETER_EXISTING_TEAMNAME, teamName);
       
   105 		startActivity(i);
       
   106 	}
       
   107 
       
   108 	private static final int[] botlevelDrawables = new int[] {
       
   109 		R.drawable.human, R.drawable.bot5, R.drawable.bot4, R.drawable.bot3, R.drawable.bot2, R.drawable.bot1
       
   110 	};
       
   111 	
       
   112 	private List<Map<String, ?>> teamsToMaps(List<Team> teams) {
       
   113 		List<Map<String, ?>> result = new ArrayList<Map<String,?>>();
       
   114 		for(Team t : teams) {
       
   115 			HashMap<String, Object> map = new HashMap<String, Object>();
       
   116 			map.put("team", t);
       
   117 			map.put("txt", t.name);
       
   118 			int botlevel = t.hogs.get(0).level;
       
   119 			if(botlevel<0 || botlevel>=botlevelDrawables.length) {
       
   120 				map.put("img", R.drawable.bot1);
       
   121 			} else {
       
   122 				map.put("img", botlevelDrawables[botlevel]);
       
   123 			}
       
   124 			result.add(map);
       
   125 		}
       
   126 		return result;
       
   127 	}
       
   128 }