project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/TeamlistAdapter.java
author koda
Tue, 21 Jan 2014 22:43:06 +0100
changeset 10017 de822cd3df3a
parent 7584 7831c84cc644
permissions -rw-r--r--
fixwhitespace and dos2unix
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     1
/*
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     2
 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     4
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     5
 * 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
     6
 * 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
     7
 * 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
     8
 * of the License, or (at your option) any later version.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     9
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    13
 * GNU General Public License for more details.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    14
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    15
 * You should have received a copy 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
    16
 * along with this program; if not, write to the Free Software
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    18
 */
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    19
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
package org.hedgewars.hedgeroid;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    22
import java.util.ArrayList;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    23
import java.util.Collection;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    24
import java.util.Collections;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    25
import java.util.List;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
import org.hedgewars.hedgeroid.R;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
import org.hedgewars.hedgeroid.Datastructures.TeamInGame;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
import org.hedgewars.hedgeroid.Datastructures.TeamIngameAttributes;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
import android.graphics.drawable.ColorDrawable;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
import android.graphics.drawable.Drawable;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
import android.view.LayoutInflater;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
import android.view.View;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
import android.view.View.OnClickListener;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
import android.view.ViewGroup;
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    37
import android.widget.BaseAdapter;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
import android.widget.ImageButton;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
import android.widget.TextView;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    41
public class TeamlistAdapter extends BaseAdapter {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    42
    private boolean colorHogcountEnabled = false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    43
    private Listener listener;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    44
    private List<TeamInGame> teams = new ArrayList<TeamInGame>();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    45
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    46
    public void setColorHogcountEnabled(boolean colorHogcountEnabled) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    47
        this.colorHogcountEnabled = colorHogcountEnabled;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    48
        notifyDataSetChanged();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    49
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    50
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    51
    public void setListener(Listener listener) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    52
        this.listener = listener;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    53
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    54
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    55
    public int getCount() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    56
        return teams.size();
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
    public TeamInGame getItem(int position) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    60
        return teams.get(position);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    61
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    62
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    63
    public long getItemId(int position) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    64
        return position;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    65
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    66
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    67
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    68
    public boolean hasStableIds() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    69
        return false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    70
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    71
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    72
    public void updateTeamlist(Collection<TeamInGame> newTeams) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    73
        teams.clear();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    74
        teams.addAll(newTeams);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    75
        Collections.sort(teams, TeamInGame.NAME_ORDER);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    76
        notifyDataSetChanged();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    77
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    78
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    79
    public View getView(int position, View convertView, ViewGroup parent) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    80
        View v = convertView;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    81
        if (v == null) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    82
            LayoutInflater vi = LayoutInflater.from(parent.getContext());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    83
            v = vi.inflate(R.layout.listview_team, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    84
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    85
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    86
        TeamInGame team = getItem(position);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    87
        TextView teamNameView = (TextView) v.findViewById(android.R.id.text1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    88
        ImageButton colorButton = (ImageButton) v.findViewById(R.id.colorButton);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    89
        ImageButton hogCountButton = (ImageButton) v.findViewById(R.id.hogCountButton);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    90
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    91
        teamNameView.setText(team.team.name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    92
        int teamImage;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    93
        if(team.ingameAttribs.remoteDriven) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    94
            teamImage = R.drawable.team_net_by_level;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    95
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    96
            teamImage = R.drawable.team_local_by_level;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    97
        }
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    98
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    99
        Drawable d = parent.getContext().getResources().getDrawable(teamImage).mutate();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   100
        d.setLevel(team.team.hogs.get(0).level);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   101
        teamNameView.setCompoundDrawablesWithIntrinsicBounds(d, null, null, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   102
        hogCountButton.getDrawable().setLevel(team.ingameAttribs.hogCount);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   103
        colorButton.setImageDrawable(new ColorDrawable(TeamIngameAttributes.TEAM_COLORS[team.ingameAttribs.colorIndex]));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   104
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   105
        colorButton.setEnabled(colorHogcountEnabled);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   106
        hogCountButton.setEnabled(colorHogcountEnabled);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   107
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   108
        colorButton.setOnClickListener(new ButtonClickListener(team, Type.COLOR_BUTTON));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   109
        hogCountButton.setOnClickListener(new ButtonClickListener(team, Type.HOGCOUNT_BUTTON));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   110
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   111
        if(team.ingameAttribs.remoteDriven) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   112
            teamNameView.setClickable(false);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   113
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   114
            teamNameView.setOnClickListener(new ButtonClickListener(team, Type.TEAM_VIEW));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   115
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   116
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   117
        return v;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   118
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   119
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   120
    private static enum Type {COLOR_BUTTON, HOGCOUNT_BUTTON, TEAM_VIEW}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   121
    private final class ButtonClickListener implements OnClickListener {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   122
        private final TeamInGame team;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   123
        private final Type type;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   124
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   125
        public ButtonClickListener(TeamInGame team, Type type) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   126
            this.team = team;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   127
            this.type = type;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   128
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   129
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   130
        public void onClick(View v) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   131
            if(listener != null) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   132
                switch(type) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   133
                case COLOR_BUTTON:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   134
                    listener.onColorClicked(team);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   135
                    break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   136
                case HOGCOUNT_BUTTON:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   137
                    listener.onHogcountClicked(team);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   138
                    break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   139
                case TEAM_VIEW:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   140
                    listener.onTeamClicked(team);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   141
                    break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   142
                default:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   143
                    throw new IllegalStateException();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   144
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   145
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   146
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   147
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   148
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   149
    public interface Listener {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   150
        void onTeamClicked(TeamInGame team);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   151
        void onColorClicked(TeamInGame team);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   152
        void onHogcountClicked(TeamInGame team);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   153
    }
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   154
}