author | Medo <smaxein@googlemail.com> |
Mon, 20 Aug 2012 20:19:35 +0200 | |
changeset 7582 | 714310efad8f |
parent 7568 | 75ba91f14ed5 |
child 7584 | 7831c84cc644 |
permissions | -rw-r--r-- |
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
7485
diff
changeset
|
1 |
package org.hedgewars.hedgeroid; |
7476
2fb781bbdd51
Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
2 |
|
7485 | 3 |
import java.util.ArrayList; |
4 |
import java.util.Collection; |
|
5 |
import java.util.List; |
|
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7568
diff
changeset
|
6 |
import java.util.Map; |
7485 | 7 |
|
8 |
import org.hedgewars.hedgeroid.Datastructures.Team; |
|
9 |
import org.hedgewars.hedgeroid.Datastructures.TeamInGame; |
|
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
7485
diff
changeset
|
10 |
import org.hedgewars.hedgeroid.Datastructures.TeamIngameAttributes; |
7476
2fb781bbdd51
Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
11 |
|
2fb781bbdd51
Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
12 |
import android.os.Bundle; |
2fb781bbdd51
Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
13 |
import android.support.v4.app.ListFragment; |
2fb781bbdd51
Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
14 |
import android.view.LayoutInflater; |
2fb781bbdd51
Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
15 |
import android.view.View; |
7485 | 16 |
import android.view.View.OnClickListener; |
7476
2fb781bbdd51
Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
17 |
import android.view.ViewGroup; |
7485 | 18 |
import android.widget.Button; |
7476
2fb781bbdd51
Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
19 |
|
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7568
diff
changeset
|
20 |
public class TeamlistFragment extends ListFragment implements TeamlistAdapter.Listener { |
7476
2fb781bbdd51
Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
21 |
private TeamlistAdapter adapter; |
7485 | 22 |
private Button addTeamButton; |
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
7485
diff
changeset
|
23 |
private RoomStateManager stateManager; |
7485 | 24 |
|
7476
2fb781bbdd51
Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
25 |
@Override |
2fb781bbdd51
Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
26 |
public void onCreate(Bundle savedInstanceState) { |
2fb781bbdd51
Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
27 |
super.onCreate(savedInstanceState); |
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
7485
diff
changeset
|
28 |
try { |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
7485
diff
changeset
|
29 |
stateManager = ((RoomStateManager.Provider)getActivity()).getRoomStateManager(); |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
7485
diff
changeset
|
30 |
} catch(ClassCastException e) { |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
7485
diff
changeset
|
31 |
throw new RuntimeException("Hosting activity must implement RoomStateManager.Provider.", e); |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
7485
diff
changeset
|
32 |
} |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
7485
diff
changeset
|
33 |
adapter = new TeamlistAdapter(); |
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7568
diff
changeset
|
34 |
adapter.updateTeamlist(stateManager.getTeams().values()); |
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
7485
diff
changeset
|
35 |
adapter.setColorHogcountEnabled(stateManager.getChiefStatus()); |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
7485
diff
changeset
|
36 |
adapter.setListener(this); |
7476
2fb781bbdd51
Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
37 |
setListAdapter(adapter); |
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7568
diff
changeset
|
38 |
stateManager.addListener(roomStateChangeListener); |
7476
2fb781bbdd51
Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
39 |
} |
2fb781bbdd51
Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
40 |
|
2fb781bbdd51
Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
41 |
@Override |
2fb781bbdd51
Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
42 |
public View onCreateView(LayoutInflater inflater, ViewGroup container, |
2fb781bbdd51
Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
43 |
Bundle savedInstanceState) { |
7485 | 44 |
View v = inflater.inflate(R.layout.fragment_teamlist, container, false); |
45 |
addTeamButton = (Button)v.findViewById(R.id.addTeamButton); |
|
46 |
addTeamButton.setOnClickListener(new OnClickListener() { |
|
47 |
public void onClick(View v) { |
|
48 |
new TeamAddDialog(getCurrentTeamNames()).show(getFragmentManager(), "team_add_dialog"); |
|
49 |
} |
|
50 |
}); |
|
51 |
||
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7568
diff
changeset
|
52 |
addTeamButton.setEnabled(stateManager.getTeams().size() < Team.maxNumberOfTeams); |
7485 | 53 |
|
54 |
return v; |
|
7476
2fb781bbdd51
Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
55 |
} |
2fb781bbdd51
Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
56 |
|
2fb781bbdd51
Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
57 |
@Override |
2fb781bbdd51
Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
58 |
public void onDestroy() { |
2fb781bbdd51
Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
59 |
super.onDestroy(); |
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
7485
diff
changeset
|
60 |
adapter.setListener(null); |
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7568
diff
changeset
|
61 |
stateManager.removeListener(roomStateChangeListener); |
7485 | 62 |
} |
63 |
||
64 |
private Collection<String> getCurrentTeamNames() { |
|
65 |
List<String> names = new ArrayList<String>(); |
|
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7568
diff
changeset
|
66 |
for(TeamInGame team : stateManager.getTeams().values()) { |
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
7485
diff
changeset
|
67 |
names.add(team.team.name); |
7485 | 68 |
} |
69 |
return names; |
|
70 |
} |
|
71 |
||
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
7485
diff
changeset
|
72 |
public void onColorClicked(TeamInGame team) { |
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7568
diff
changeset
|
73 |
stateManager.changeTeamColorIndex(team.team.name, (team.ingameAttribs.colorIndex+1)%TeamIngameAttributes.TEAM_COLORS.length); |
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
7485
diff
changeset
|
74 |
} |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
7485
diff
changeset
|
75 |
|
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
7485
diff
changeset
|
76 |
public void onHogcountClicked(TeamInGame team) { |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
7485
diff
changeset
|
77 |
int newHogCount = team.ingameAttribs.hogCount+1; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
7485
diff
changeset
|
78 |
if(newHogCount>Team.HEDGEHOGS_PER_TEAM) { |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
7485
diff
changeset
|
79 |
newHogCount = 1; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
7485
diff
changeset
|
80 |
} |
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7568
diff
changeset
|
81 |
stateManager.changeTeamHogCount(team.team.name, newHogCount); |
7476
2fb781bbdd51
Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
82 |
} |
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
7485
diff
changeset
|
83 |
|
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
7485
diff
changeset
|
84 |
public void onTeamClicked(TeamInGame team) { |
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7568
diff
changeset
|
85 |
stateManager.requestRemoveTeam(team.team.name); |
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
7485
diff
changeset
|
86 |
} |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
7485
diff
changeset
|
87 |
|
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7568
diff
changeset
|
88 |
private final RoomStateManager.Listener roomStateChangeListener = new RoomStateManager.ListenerAdapter() { |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7568
diff
changeset
|
89 |
@Override |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7568
diff
changeset
|
90 |
public void onChiefStatusChanged(boolean isChief) { |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7568
diff
changeset
|
91 |
adapter.setColorHogcountEnabled(isChief); |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7568
diff
changeset
|
92 |
}; |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7568
diff
changeset
|
93 |
|
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7568
diff
changeset
|
94 |
@Override |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7568
diff
changeset
|
95 |
public void onTeamsChanged(Map<String, TeamInGame> teams) { |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7568
diff
changeset
|
96 |
adapter.updateTeamlist(teams.values()); |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7568
diff
changeset
|
97 |
addTeamButton.setEnabled(teams.size() < Team.maxNumberOfTeams); |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7568
diff
changeset
|
98 |
}; |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7568
diff
changeset
|
99 |
}; |
7476
2fb781bbdd51
Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
100 |
} |