author | Wuzzy <Wuzzy2@mail.ru> |
Sun, 07 Apr 2019 19:26:16 +0200 | |
changeset 14755 | ab7bf5036314 |
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:
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 |
|
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
22 |
import java.io.File; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
23 |
import java.io.FileNotFoundException; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
24 |
import java.io.IOException; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
25 |
import java.util.Collections; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
26 |
import java.util.List; |
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7568
diff
changeset
|
27 |
import java.util.Map; |
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
28 |
|
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
29 |
import org.hedgewars.hedgeroid.R; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
30 |
import org.hedgewars.hedgeroid.Datastructures.FrontendDataUtils; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
31 |
import org.hedgewars.hedgeroid.Datastructures.MapRecipe; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
32 |
import org.hedgewars.hedgeroid.Datastructures.Scheme; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
33 |
import org.hedgewars.hedgeroid.Datastructures.Schemes; |
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7568
diff
changeset
|
34 |
import org.hedgewars.hedgeroid.Datastructures.TeamInGame; |
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
35 |
import org.hedgewars.hedgeroid.Datastructures.Weaponset; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
36 |
import org.hedgewars.hedgeroid.Datastructures.Weaponsets; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
37 |
import org.hedgewars.hedgeroid.util.FileUtils; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
38 |
|
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
39 |
import android.graphics.drawable.Drawable; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
40 |
import android.os.Bundle; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
41 |
import android.support.v4.app.Fragment; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
42 |
import android.util.Log; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
43 |
import android.view.LayoutInflater; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
44 |
import android.view.View; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
45 |
import android.view.ViewGroup; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
46 |
import android.widget.AdapterView; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
47 |
import android.widget.AdapterView.OnItemSelectedListener; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
48 |
import android.widget.ArrayAdapter; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
49 |
import android.widget.ImageView; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
50 |
import android.widget.Spinner; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
51 |
import android.widget.Toast; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
52 |
|
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7568
diff
changeset
|
53 |
public class SettingsFragment extends Fragment { |
10017 | 54 |
private Spinner styleSpinner, schemeSpinner, weaponsetSpinner, themeSpinner; |
55 |
private ImageView themeIcon; |
|
56 |
||
57 |
private List<String> styles; |
|
58 |
private List<Scheme> schemes; |
|
59 |
private List<Weaponset> weaponsets; |
|
60 |
private List<String> themes; |
|
61 |
||
62 |
private RoomStateManager stateManager; |
|
63 |
||
64 |
@Override |
|
65 |
public View onCreateView(LayoutInflater inflater, ViewGroup container, |
|
66 |
Bundle savedInstanceState) { |
|
67 |
View v = inflater.inflate(R.layout.fragment_settings, container, false); |
|
68 |
themeIcon = (ImageView)v.findViewById(R.id.imgTheme); |
|
69 |
||
70 |
try { |
|
71 |
styles = FrontendDataUtils.getGameStyles(getActivity()); |
|
72 |
schemes = Schemes.loadAllSchemes(getActivity()); |
|
73 |
weaponsets = Weaponsets.loadAllWeaponsets(getActivity()); |
|
74 |
themes = FrontendDataUtils.getThemes(getActivity()); |
|
75 |
} catch (IOException e) { |
|
76 |
Toast.makeText(getActivity().getApplicationContext(), R.string.error_missing_sdcard_or_files, Toast.LENGTH_LONG).show(); |
|
77 |
getActivity().finish(); |
|
78 |
return null; |
|
79 |
} |
|
80 |
||
81 |
Collections.sort(styles, String.CASE_INSENSITIVE_ORDER); |
|
82 |
Collections.sort(schemes, Scheme.NAME_ORDER); |
|
83 |
Collections.sort(weaponsets, Weaponset.NAME_ORDER); |
|
84 |
Collections.sort(themes, String.CASE_INSENSITIVE_ORDER); |
|
85 |
||
86 |
styleSpinner = prepareSpinner(v, R.id.spinGameplay, styles, styleSelectedListener); |
|
87 |
schemeSpinner = prepareSpinner(v, R.id.spinGamescheme, Schemes.toNameList(schemes), schemeSelectedListener); |
|
88 |
weaponsetSpinner = prepareSpinner(v, R.id.spinweapons, Weaponsets.toNameList(weaponsets), weaponsetSelectedListener); |
|
89 |
themeSpinner = prepareSpinner(v, R.id.spinTheme, themes, themeSelectedListener); |
|
90 |
||
91 |
stateManager.addListener(roomStateChangeListener); |
|
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
92 |
|
10017 | 93 |
if(stateManager.getGameStyle() != null) { |
94 |
styleSpinner.setSelection(styles.indexOf(stateManager.getGameStyle()), false); |
|
95 |
} |
|
96 |
if(stateManager.getScheme() != null) { |
|
97 |
schemeSpinner.setSelection(getSchemePosition(schemes, stateManager.getScheme().name), false); |
|
98 |
} |
|
99 |
if(stateManager.getWeaponset() != null) { |
|
100 |
weaponsetSpinner.setSelection(getWeaponsetPosition(weaponsets, stateManager.getWeaponset().name), false); |
|
101 |
} |
|
102 |
if(stateManager.getMapRecipe() != null) { |
|
103 |
themeSpinner.setSelection(themes.indexOf(stateManager.getMapRecipe().theme), false); |
|
104 |
} |
|
105 |
||
106 |
setChiefState(stateManager.getChiefStatus()); |
|
107 |
||
108 |
return v; |
|
109 |
} |
|
110 |
||
111 |
private static Spinner prepareSpinner(View v, int id, List<String> items, OnItemSelectedListener itemSelectedListener) { |
|
112 |
Spinner spinner = (Spinner)v.findViewById(id); |
|
113 |
ArrayAdapter<String> adapter = new ArrayAdapter<String>(v.getContext(), R.layout.listview_item, items); |
|
114 |
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
|
115 |
spinner.setAdapter(adapter); |
|
116 |
spinner.setOnItemSelectedListener(itemSelectedListener); |
|
117 |
return spinner; |
|
118 |
} |
|
119 |
||
120 |
@Override |
|
121 |
public void onCreate(Bundle savedInstanceState) { |
|
122 |
super.onCreate(savedInstanceState); |
|
123 |
try { |
|
124 |
stateManager = ((RoomStateManager.Provider)getActivity()).getRoomStateManager(); |
|
125 |
} catch(ClassCastException e) { |
|
126 |
throw new RuntimeException("Hosting activity must implement RoomStateManager.Provider.", e); |
|
127 |
} |
|
128 |
} |
|
129 |
||
130 |
@Override |
|
131 |
public void onDestroy() { |
|
132 |
super.onDestroy(); |
|
133 |
stateManager.removeListener(roomStateChangeListener); |
|
134 |
} |
|
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
135 |
|
10017 | 136 |
private static int getSchemePosition(List<Scheme> schemes, String scheme) { |
137 |
for(int i=0; i<schemes.size(); i++) { |
|
138 |
if(schemes.get(i).name.equals(scheme)) { |
|
139 |
return i; |
|
140 |
} |
|
141 |
} |
|
142 |
return -1; |
|
143 |
} |
|
144 |
||
145 |
private static int getWeaponsetPosition(List<Weaponset> weaponsets, String weaponset) { |
|
146 |
for(int i=0; i<weaponsets.size(); i++) { |
|
147 |
if(weaponsets.get(i).name.equals(weaponset)) { |
|
148 |
return i; |
|
149 |
} |
|
150 |
} |
|
151 |
return -1; |
|
152 |
} |
|
153 |
||
154 |
private void setChiefState(boolean chiefState) { |
|
155 |
styleSpinner.setEnabled(chiefState); |
|
156 |
schemeSpinner.setEnabled(chiefState); |
|
157 |
weaponsetSpinner.setEnabled(chiefState); |
|
158 |
themeSpinner.setEnabled(chiefState); |
|
159 |
||
160 |
if(chiefState) { |
|
161 |
stateManager.changeGameStyle(styles.get(styleSpinner.getSelectedItemPosition())); |
|
162 |
stateManager.changeScheme(schemes.get(schemeSpinner.getSelectedItemPosition())); |
|
163 |
stateManager.changeWeaponset(weaponsets.get(weaponsetSpinner.getSelectedItemPosition())); |
|
164 |
stateManager.changeMapTheme(themes.get(themeSpinner.getSelectedItemPosition())); |
|
165 |
} |
|
166 |
} |
|
167 |
||
168 |
private final OnItemSelectedListener styleSelectedListener = new OnItemSelectedListener() { |
|
169 |
public void onItemSelected(AdapterView<?> adapter, View v, int position, long arg3) { |
|
170 |
stateManager.changeGameStyle(styles.get(position)); |
|
171 |
} |
|
172 |
public void onNothingSelected(AdapterView<?> arg0) {} |
|
173 |
}; |
|
174 |
||
175 |
private final OnItemSelectedListener schemeSelectedListener = new OnItemSelectedListener() { |
|
176 |
public void onItemSelected(AdapterView<?> adapter, View v, int position, long arg3) { |
|
177 |
stateManager.changeScheme(schemes.get(position)); |
|
178 |
} |
|
179 |
public void onNothingSelected(AdapterView<?> arg0) {} |
|
180 |
}; |
|
181 |
||
182 |
private final OnItemSelectedListener weaponsetSelectedListener = new OnItemSelectedListener() { |
|
183 |
public void onItemSelected(AdapterView<?> adapter, View v, int position, long arg3) { |
|
184 |
stateManager.changeWeaponset(weaponsets.get(position)); |
|
185 |
} |
|
186 |
public void onNothingSelected(AdapterView<?> arg0) {} |
|
187 |
}; |
|
188 |
||
189 |
private final OnItemSelectedListener themeSelectedListener = new OnItemSelectedListener() { |
|
190 |
public void onItemSelected(AdapterView<?> adapter, View v, int position, long arg3) { |
|
191 |
stateManager.changeMapTheme(themes.get(position)); |
|
192 |
String theme = themes.get(position); |
|
193 |
try { |
|
194 |
File iconFile = FileUtils.getDataPathFile(getActivity(), "Themes", theme, "icon@2X.png"); |
|
195 |
Drawable themeIconDrawable = Drawable.createFromPath(iconFile.getAbsolutePath()); |
|
196 |
themeIcon.setImageDrawable(themeIconDrawable); |
|
197 |
} catch (FileNotFoundException e) { |
|
198 |
Log.e("SettingsFragment", "Unable to find preview for theme "+theme, e); |
|
199 |
} |
|
200 |
}; |
|
201 |
public void onNothingSelected(AdapterView<?> arg0) {}; |
|
202 |
}; |
|
203 |
||
204 |
private final RoomStateManager.Listener roomStateChangeListener = new RoomStateManager.Listener() { |
|
205 |
public void onWeaponsetChanged(Weaponset weaponset) { |
|
206 |
weaponsetSpinner.setSelection(getWeaponsetPosition(weaponsets, weaponset.name)); |
|
207 |
} |
|
208 |
||
209 |
public void onTeamsChanged(Map<String, TeamInGame> teams) {} |
|
210 |
||
211 |
public void onSchemeChanged(Scheme scheme) { |
|
212 |
schemeSpinner.setSelection(getSchemePosition(schemes, scheme.name)); |
|
213 |
} |
|
214 |
||
215 |
public void onMapChanged(MapRecipe recipe) { |
|
216 |
themeSpinner.setSelection(themes.indexOf(recipe.theme)); |
|
217 |
} |
|
218 |
||
219 |
public void onGameStyleChanged(String gameStyle) { |
|
220 |
styleSpinner.setSelection(styles.indexOf(gameStyle)); |
|
221 |
} |
|
222 |
||
223 |
public void onChiefStatusChanged(boolean isChief) { |
|
224 |
setChiefState(isChief); |
|
225 |
} |
|
226 |
}; |
|
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
227 |
} |