author | Wuzzy <Wuzzy2@mail.ru> |
Thu, 15 Feb 2018 18:32:28 +0100 | |
changeset 13009 | 67cc3cac4c8e |
parent 10017 | de822cd3df3a |
permissions | -rw-r--r-- |
7584
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
1 |
/* |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7508
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:
7508
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:
7508
diff
changeset
|
4 |
* |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7508
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:
7508
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:
7508
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:
7508
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:
7508
diff
changeset
|
9 |
* |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7508
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:
7508
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:
7508
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:
7508
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:
7508
diff
changeset
|
14 |
* |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7508
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:
7508
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:
7508
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:
7508
diff
changeset
|
18 |
*/ |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
19 |
|
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
7485
diff
changeset
|
20 |
package org.hedgewars.hedgeroid; |
7485 | 21 |
|
22 |
import java.util.ArrayList; |
|
23 |
import java.util.Collection; |
|
24 |
import java.util.Collections; |
|
25 |
import java.util.List; |
|
26 |
||
27 |
import org.hedgewars.hedgeroid.R; |
|
28 |
import org.hedgewars.hedgeroid.Datastructures.FrontendDataUtils; |
|
29 |
import org.hedgewars.hedgeroid.Datastructures.Team; |
|
30 |
||
31 |
import android.app.Activity; |
|
32 |
import android.app.AlertDialog; |
|
33 |
import android.app.Dialog; |
|
34 |
import android.content.DialogInterface; |
|
35 |
import android.content.DialogInterface.OnClickListener; |
|
36 |
import android.os.Bundle; |
|
37 |
import android.support.v4.app.DialogFragment; |
|
38 |
||
39 |
public class TeamAddDialog extends DialogFragment { |
|
10017 | 40 |
private static final String STATE_TEAMS_ALREADY_IN_GAME = "teamAlreadyInGame"; |
41 |
private ArrayList<String> teamsAlreadyInGame; |
|
42 |
private List<Team> availableTeams; |
|
43 |
private Listener listener; |
|
44 |
||
45 |
public static interface Listener { |
|
46 |
void onTeamAddDialogSubmitted(Team newTeam); |
|
47 |
} |
|
48 |
||
49 |
public TeamAddDialog() { |
|
50 |
// Only for reflection-based instantiation by the framework |
|
51 |
} |
|
52 |
||
53 |
TeamAddDialog(Collection<String> teamsAlreadyInGame) { |
|
54 |
this.teamsAlreadyInGame = new ArrayList<String>(teamsAlreadyInGame); |
|
55 |
} |
|
56 |
||
57 |
@Override |
|
58 |
public void onAttach(Activity activity) { |
|
59 |
super.onAttach(activity); |
|
60 |
try { |
|
61 |
listener = (Listener) activity; |
|
62 |
} catch(ClassCastException e) { |
|
63 |
throw new ClassCastException("Activity " + activity + " must implement TeamAddDialog.Listener to use TeamAddDialog."); |
|
64 |
} |
|
65 |
} |
|
66 |
||
67 |
@Override |
|
68 |
public void onDetach() { |
|
69 |
super.onDetach(); |
|
70 |
listener = null; |
|
71 |
} |
|
72 |
||
73 |
@Override |
|
74 |
public void onCreate(Bundle savedInstanceState) { |
|
75 |
super.onCreate(savedInstanceState); |
|
76 |
if(savedInstanceState != null) { |
|
77 |
teamsAlreadyInGame = savedInstanceState.getStringArrayList(STATE_TEAMS_ALREADY_IN_GAME); |
|
78 |
} |
|
79 |
availableTeams = new ArrayList<Team>(); |
|
80 |
List<Team> teams = FrontendDataUtils.getTeams(getActivity()); |
|
81 |
for(Team team : teams) { |
|
82 |
if(!teamsAlreadyInGame.contains(team.name)) { |
|
83 |
availableTeams.add(team); |
|
84 |
} |
|
85 |
} |
|
86 |
Collections.sort(availableTeams, Team.NAME_ORDER); |
|
87 |
} |
|
88 |
||
89 |
// TODO use icons for the teams (corresponding to botlevel) |
|
90 |
@Override |
|
91 |
public Dialog onCreateDialog(Bundle savedInstanceState) { |
|
92 |
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); |
|
93 |
builder.setTitle(R.string.dialog_addteam_title); |
|
94 |
builder.setIcon(R.drawable.human); |
|
95 |
String[] teamNames = new String[availableTeams.size()]; |
|
96 |
for(int i=0; i<availableTeams.size(); i++) { |
|
97 |
teamNames[i] = availableTeams.get(i).name; |
|
98 |
} |
|
99 |
builder.setItems(teamNames, new OnClickListener() { |
|
100 |
public void onClick(DialogInterface dialog, int which) { |
|
101 |
listener.onTeamAddDialogSubmitted(availableTeams.get(which)); |
|
102 |
} |
|
103 |
}); |
|
104 |
return builder.create(); |
|
105 |
} |
|
106 |
||
107 |
@Override |
|
108 |
public void onSaveInstanceState(Bundle outState) { |
|
109 |
super.onSaveInstanceState(outState); |
|
110 |
outState.putStringArrayList(STATE_TEAMS_ALREADY_IN_GAME, teamsAlreadyInGame); |
|
111 |
} |
|
7485 | 112 |
} |