project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/TeamListActivity.java
author Wuzzy <almikes@aol.com>
Wed, 13 Apr 2016 12:17:30 +0200
changeset 11765 10860d4bca22
parent 10017 de822cd3df3a
permissions -rw-r--r--
Add sound effects for: cleaver impact, air mine impact, using extra time
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
     1
/*
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
     2
 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
     3
 * Copyright (c) 2011-2012 Richard Deurwaarder <xeli@xelification.com>
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     4
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
     5
 *
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     6
 * This program is free software; you can redistribute it and/or
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     7
 * modify it under the terms of the GNU General Public License
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     8
 * as published by the Free Software Foundation; either version 2
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     9
 * of the License, or (at your option) any later version.
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    10
 *
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    11
 * This program is distributed in the hope that it will be useful,
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    14
 * GNU General Public License for more details.
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    15
 *
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    17
 * along with this program; if not, write to the Free Software
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    19
 */
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
package org.hedgewars.hedgeroid;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
import java.util.ArrayList;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
import java.util.Collections;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
import java.util.HashMap;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
import java.util.List;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
import java.util.Map;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
import org.hedgewars.hedgeroid.Datastructures.FrontendDataUtils;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
import org.hedgewars.hedgeroid.Datastructures.Team;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
import android.app.ListActivity;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
import android.content.Intent;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
import android.os.Bundle;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
import android.view.ContextMenu;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
import android.view.MenuItem;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
import android.view.View;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
import android.view.View.OnClickListener;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
import android.widget.AdapterView;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
import android.widget.AdapterView.AdapterContextMenuInfo;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
import android.widget.AdapterView.OnItemClickListener;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
import android.widget.ImageButton;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
import android.widget.SimpleAdapter;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
public class TeamListActivity extends ListActivity implements OnItemClickListener {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    46
    private List<Team> teams;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    47
    private ImageButton addButton;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    48
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    49
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    50
    protected void onCreate(Bundle savedInstanceState) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    51
        super.onCreate(savedInstanceState);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    52
        setContentView(R.layout.activity_teamlist);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    53
        addButton = (ImageButton)findViewById(R.id.btnAdd);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    54
        addButton.setOnClickListener(new OnClickListener() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    55
            public void onClick(View v) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    56
                editTeam(null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    57
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    58
        });
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    59
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    60
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    61
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    62
    public void onResume() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    63
        super.onResume();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    64
        updateList();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    65
        getListView().setOnItemClickListener(this);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    66
        registerForContextMenu(getListView());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    67
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    68
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    69
    public void onItemClick(AdapterView<?> adapterView, View v, int position, long arg3) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    70
        editTeam(teams.get(position).name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    71
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    72
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    73
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    74
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuinfo){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    75
        menu.add(ContextMenu.NONE, 0, ContextMenu.NONE, R.string.edit);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    76
        menu.add(ContextMenu.NONE, 1, ContextMenu.NONE, R.string.delete);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    77
    }
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    78
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    79
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    80
    public boolean onContextItemSelected(MenuItem item){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    81
        AdapterView.AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    82
        int position = menuInfo.position;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    83
        Team team = teams.get(position);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    84
        switch(item.getItemId()){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    85
        case 0:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    86
            editTeam(team.name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    87
            return true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    88
        case 1:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    89
            Team.getTeamfileByName(getApplicationContext(), team.name).delete();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    90
            updateList();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    91
            return true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    92
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    93
        return false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    94
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    95
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    96
    private void updateList() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    97
        teams = FrontendDataUtils.getTeams(getApplicationContext());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    98
        Collections.sort(teams, Team.NAME_ORDER);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    99
        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});
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   100
        setListAdapter(adapter);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   101
    }
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   102
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   103
    private void editTeam(String teamName) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   104
        Intent i = new Intent(this, TeamCreatorActivity.class);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   105
        i.putExtra(TeamCreatorActivity.PARAMETER_EXISTING_TEAMNAME, teamName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   106
        startActivity(i);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   107
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   108
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   109
    private static final int[] botlevelDrawables = new int[] {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   110
        R.drawable.human, R.drawable.bot5, R.drawable.bot4, R.drawable.bot3, R.drawable.bot2, R.drawable.bot1
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   111
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   112
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   113
    private List<Map<String, ?>> teamsToMaps(List<Team> teams) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   114
        List<Map<String, ?>> result = new ArrayList<Map<String,?>>();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   115
        for(Team t : teams) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   116
            HashMap<String, Object> map = new HashMap<String, Object>();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   117
            map.put("team", t);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   118
            map.put("txt", t.name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   119
            int botlevel = t.hogs.get(0).level;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   120
            if(botlevel<0 || botlevel>=botlevelDrawables.length) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   121
                map.put("img", R.drawable.bot1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   122
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   123
                map.put("img", botlevelDrawables[botlevel]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   124
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   125
            result.add(map);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   126
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   127
        return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   128
    }
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   129
}