1 package org.hedgewars.hedgeroid; |
1 package org.hedgewars.hedgeroid; |
2 |
2 |
3 import java.util.ArrayList; |
3 import java.util.ArrayList; |
4 import java.util.Collection; |
4 import java.util.Collection; |
5 import java.util.List; |
5 import java.util.List; |
|
6 import java.util.Map; |
6 |
7 |
7 import org.hedgewars.hedgeroid.R; |
|
8 import org.hedgewars.hedgeroid.Datastructures.MapRecipe; |
|
9 import org.hedgewars.hedgeroid.Datastructures.Scheme; |
|
10 import org.hedgewars.hedgeroid.Datastructures.Team; |
8 import org.hedgewars.hedgeroid.Datastructures.Team; |
11 import org.hedgewars.hedgeroid.Datastructures.TeamInGame; |
9 import org.hedgewars.hedgeroid.Datastructures.TeamInGame; |
12 import org.hedgewars.hedgeroid.Datastructures.TeamIngameAttributes; |
10 import org.hedgewars.hedgeroid.Datastructures.TeamIngameAttributes; |
13 import org.hedgewars.hedgeroid.Datastructures.Weaponset; |
|
14 import org.hedgewars.hedgeroid.netplay.Netplay; |
|
15 |
11 |
16 import android.database.DataSetObserver; |
|
17 import android.os.Bundle; |
12 import android.os.Bundle; |
18 import android.support.v4.app.ListFragment; |
13 import android.support.v4.app.ListFragment; |
19 import android.view.LayoutInflater; |
14 import android.view.LayoutInflater; |
20 import android.view.View; |
15 import android.view.View; |
21 import android.view.View.OnClickListener; |
16 import android.view.View.OnClickListener; |
22 import android.view.ViewGroup; |
17 import android.view.ViewGroup; |
23 import android.widget.Button; |
18 import android.widget.Button; |
24 |
19 |
25 /** |
20 public class TeamlistFragment extends ListFragment implements TeamlistAdapter.Listener { |
26 * TODO use an interface for querying and manipulating the team list, to allow re-using this fragment |
|
27 * in local play |
|
28 */ |
|
29 public class TeamlistFragment extends ListFragment implements TeamlistAdapter.Listener, RoomStateManager.Observer { |
|
30 private Netplay netplay; |
|
31 private TeamlistAdapter adapter; |
21 private TeamlistAdapter adapter; |
32 private Button addTeamButton; |
22 private Button addTeamButton; |
33 private DataSetObserver teamlistObserver; |
|
34 private RoomStateManager stateManager; |
23 private RoomStateManager stateManager; |
35 |
24 |
36 @Override |
25 @Override |
37 public void onCreate(Bundle savedInstanceState) { |
26 public void onCreate(Bundle savedInstanceState) { |
38 super.onCreate(savedInstanceState); |
27 super.onCreate(savedInstanceState); |
39 try { |
28 try { |
40 stateManager = ((RoomStateManager.Provider)getActivity()).getRoomStateManager(); |
29 stateManager = ((RoomStateManager.Provider)getActivity()).getRoomStateManager(); |
41 } catch(ClassCastException e) { |
30 } catch(ClassCastException e) { |
42 throw new RuntimeException("Hosting activity must implement RoomStateManager.Provider.", e); |
31 throw new RuntimeException("Hosting activity must implement RoomStateManager.Provider.", e); |
43 } |
32 } |
44 netplay = Netplay.getAppInstance(getActivity().getApplicationContext()); |
|
45 adapter = new TeamlistAdapter(); |
33 adapter = new TeamlistAdapter(); |
46 adapter.setSource(netplay.roomTeamlist); |
34 adapter.updateTeamlist(stateManager.getTeams().values()); |
47 adapter.setColorHogcountEnabled(stateManager.getChiefStatus()); |
35 adapter.setColorHogcountEnabled(stateManager.getChiefStatus()); |
48 adapter.setListener(this); |
36 adapter.setListener(this); |
49 setListAdapter(adapter); |
37 setListAdapter(adapter); |
50 stateManager.registerObserver(this); |
38 stateManager.addListener(roomStateChangeListener); |
51 } |
39 } |
52 |
40 |
53 @Override |
41 @Override |
54 public View onCreateView(LayoutInflater inflater, ViewGroup container, |
42 public View onCreateView(LayoutInflater inflater, ViewGroup container, |
55 Bundle savedInstanceState) { |
43 Bundle savedInstanceState) { |
59 public void onClick(View v) { |
47 public void onClick(View v) { |
60 new TeamAddDialog(getCurrentTeamNames()).show(getFragmentManager(), "team_add_dialog"); |
48 new TeamAddDialog(getCurrentTeamNames()).show(getFragmentManager(), "team_add_dialog"); |
61 } |
49 } |
62 }); |
50 }); |
63 |
51 |
64 teamlistObserver = new DataSetObserver() { |
52 addTeamButton.setEnabled(stateManager.getTeams().size() < Team.maxNumberOfTeams); |
65 @Override |
|
66 public void onChanged() { |
|
67 addTeamButton.setEnabled(netplay.roomTeamlist.getMap().size() < Team.maxNumberOfTeams); |
|
68 } |
|
69 }; |
|
70 netplay.roomTeamlist.registerObserver(teamlistObserver); |
|
71 teamlistObserver.onChanged(); |
|
72 |
53 |
73 return v; |
54 return v; |
74 } |
55 } |
75 |
56 |
76 @Override |
57 @Override |
77 public void onDestroy() { |
58 public void onDestroy() { |
78 super.onDestroy(); |
59 super.onDestroy(); |
79 adapter.invalidate(); |
|
80 adapter.setListener(null); |
60 adapter.setListener(null); |
81 netplay.roomTeamlist.unregisterObserver(teamlistObserver); |
61 stateManager.removeListener(roomStateChangeListener); |
82 stateManager.unregisterObserver(this); |
|
83 } |
62 } |
84 |
63 |
85 @Override |
|
86 public void onActivityCreated(Bundle savedInstanceState) { |
|
87 super.onActivityCreated(savedInstanceState); |
|
88 } |
|
89 |
|
90 private Collection<String> getCurrentTeamNames() { |
64 private Collection<String> getCurrentTeamNames() { |
91 List<String> names = new ArrayList<String>(); |
65 List<String> names = new ArrayList<String>(); |
92 for(TeamInGame team : netplay.roomTeamlist.getMap().values()) { |
66 for(TeamInGame team : stateManager.getTeams().values()) { |
93 names.add(team.team.name); |
67 names.add(team.team.name); |
94 } |
68 } |
95 return names; |
69 return names; |
96 } |
70 } |
97 |
71 |
98 public void onColorClicked(TeamInGame team) { |
72 public void onColorClicked(TeamInGame team) { |
99 netplay.changeTeamColorIndex(team.team.name, (team.ingameAttribs.colorIndex+1)%TeamIngameAttributes.TEAM_COLORS.length); |
73 stateManager.changeTeamColorIndex(team.team.name, (team.ingameAttribs.colorIndex+1)%TeamIngameAttributes.TEAM_COLORS.length); |
100 } |
74 } |
101 |
75 |
102 public void onHogcountClicked(TeamInGame team) { |
76 public void onHogcountClicked(TeamInGame team) { |
103 int newHogCount = team.ingameAttribs.hogCount+1; |
77 int newHogCount = team.ingameAttribs.hogCount+1; |
104 if(newHogCount>Team.HEDGEHOGS_PER_TEAM) { |
78 if(newHogCount>Team.HEDGEHOGS_PER_TEAM) { |
105 newHogCount = 1; |
79 newHogCount = 1; |
106 } |
80 } |
107 netplay.changeTeamHogCount(team.team.name, newHogCount); |
81 stateManager.changeTeamHogCount(team.team.name, newHogCount); |
108 } |
82 } |
109 |
83 |
110 public void onTeamClicked(TeamInGame team) { |
84 public void onTeamClicked(TeamInGame team) { |
111 netplay.sendRemoveTeam(team.team.name); |
85 stateManager.requestRemoveTeam(team.team.name); |
112 } |
86 } |
113 |
87 |
114 public void onChiefStatusChanged(boolean isChief) { |
88 private final RoomStateManager.Listener roomStateChangeListener = new RoomStateManager.ListenerAdapter() { |
115 adapter.setColorHogcountEnabled(isChief); |
89 @Override |
116 } |
90 public void onChiefStatusChanged(boolean isChief) { |
117 |
91 adapter.setColorHogcountEnabled(isChief); |
118 public void onGameStyleChanged(String gameStyle) { } |
92 }; |
119 public void onMapChanged(MapRecipe recipe) { } |
93 |
120 public void onSchemeChanged(Scheme scheme) { } |
94 @Override |
121 public void onWeaponsetChanged(Weaponset weaponset) { } |
95 public void onTeamsChanged(Map<String, TeamInGame> teams) { |
|
96 adapter.updateTeamlist(teams.values()); |
|
97 addTeamButton.setEnabled(teams.size() < Team.maxNumberOfTeams); |
|
98 }; |
|
99 }; |
122 } |
100 } |