project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/TeamlistFragment.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: 7485
diff changeset
    20
package org.hedgewars.hedgeroid;
7476
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
    22
import java.util.ArrayList;
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
    23
import java.util.Collection;
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
    24
import java.util.List;
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
    25
import java.util.Map;
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
    26
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
    27
import org.hedgewars.hedgeroid.Datastructures.Team;
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
    28
import org.hedgewars.hedgeroid.Datastructures.TeamInGame;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    29
import org.hedgewars.hedgeroid.Datastructures.TeamIngameAttributes;
7476
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
import android.os.Bundle;
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
import android.support.v4.app.ListFragment;
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
import android.view.LayoutInflater;
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
import android.view.View;
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
    35
import android.view.View.OnClickListener;
7476
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
import android.view.ViewGroup;
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
    37
import android.widget.Button;
7476
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
    39
public class TeamlistFragment extends ListFragment implements TeamlistAdapter.Listener {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    40
    private TeamlistAdapter adapter;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    41
    private Button addTeamButton;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    42
    private RoomStateManager stateManager;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    43
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    44
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    45
    public void onCreate(Bundle savedInstanceState) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    46
        super.onCreate(savedInstanceState);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    47
        try {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    48
            stateManager = ((RoomStateManager.Provider)getActivity()).getRoomStateManager();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    49
        } catch(ClassCastException e) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    50
            throw new RuntimeException("Hosting activity must implement RoomStateManager.Provider.", e);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    51
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    52
        adapter = new TeamlistAdapter();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    53
        adapter.updateTeamlist(stateManager.getTeams().values());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    54
        adapter.setColorHogcountEnabled(stateManager.getChiefStatus());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    55
        adapter.setListener(this);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    56
        setListAdapter(adapter);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    57
        stateManager.addListener(roomStateChangeListener);
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
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    61
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    62
            Bundle savedInstanceState) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    63
        View v = inflater.inflate(R.layout.fragment_teamlist, container, false);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    64
        addTeamButton = (Button)v.findViewById(R.id.addTeamButton);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    65
        addTeamButton.setOnClickListener(new OnClickListener() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    66
            public void onClick(View v) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    67
                new TeamAddDialog(getCurrentTeamNames()).show(getFragmentManager(), "team_add_dialog");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    68
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    69
        });
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    70
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    71
        addTeamButton.setEnabled(stateManager.getTeams().size() < Team.maxNumberOfTeams);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    72
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    73
        return v;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    74
    }
7476
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    75
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    76
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    77
    public void onDestroy() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    78
        super.onDestroy();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    79
        adapter.setListener(null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    80
        stateManager.removeListener(roomStateChangeListener);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    81
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    82
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    83
    private Collection<String> getCurrentTeamNames() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    84
        List<String> names = new ArrayList<String>();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    85
        for(TeamInGame team : stateManager.getTeams().values()) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    86
            names.add(team.team.name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    87
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    88
        return names;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    89
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    90
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    91
    public void onColorClicked(TeamInGame team) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    92
        stateManager.changeTeamColorIndex(team.team.name, (team.ingameAttribs.colorIndex+1)%TeamIngameAttributes.TEAM_COLORS.length);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    93
    }
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
    94
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    95
    public void onHogcountClicked(TeamInGame team) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    96
        int newHogCount = team.ingameAttribs.hogCount+1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    97
        if(newHogCount>Team.HEDGEHOGS_PER_TEAM) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    98
            newHogCount = 1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    99
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   100
        stateManager.changeTeamHogCount(team.team.name, newHogCount);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   101
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   102
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   103
    public void onTeamClicked(TeamInGame team) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   104
        stateManager.requestRemoveTeam(team.team.name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   105
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   106
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   107
    private final RoomStateManager.Listener roomStateChangeListener = new RoomStateManager.ListenerAdapter() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   108
        @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   109
        public void onChiefStatusChanged(boolean isChief) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   110
            adapter.setColorHogcountEnabled(isChief);
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
        @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   114
        public void onTeamsChanged(Map<String, TeamInGame> teams) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   115
            adapter.updateTeamlist(teams.values());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   116
            addTeamButton.setEnabled(teams.size() < Team.maxNumberOfTeams);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   117
        };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   118
    };
7476
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
   119
}