|
1 /* |
|
2 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game |
|
3 * Copyright (c) 2011-2012 Richard Deurwaarder <xeli@xelification.com> |
|
4 * |
|
5 * This program is free software; you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation; version 2 of the License |
|
8 * |
|
9 * This program is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 * GNU General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU General Public License |
|
15 * along with this program; if not, write to the Free Software |
|
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 */ |
|
18 |
|
19 |
|
20 package org.hedgewars.hedgeroid.Datastructures; |
|
21 |
|
22 import java.io.File; |
|
23 import java.util.ArrayList; |
|
24 import java.util.Collections; |
|
25 import java.util.HashMap; |
|
26 import java.util.List; |
|
27 |
|
28 import org.hedgewars.hedgeroid.R; |
|
29 import org.hedgewars.hedgeroid.Utils; |
|
30 import org.hedgewars.hedgeroid.Datastructures.Map.MapType; |
|
31 |
|
32 import android.content.Context; |
|
33 import android.graphics.Bitmap; |
|
34 import android.graphics.BitmapFactory; |
|
35 import java.nio.ByteBuffer; |
|
36 |
|
37 public class FrontendDataUtils { |
|
38 |
|
39 |
|
40 public static ArrayList<Map> getMaps(Context c){ |
|
41 File[] files = Utils.getFilesFromRelativeDir(c,"Maps"); |
|
42 ArrayList<Map> ret = new ArrayList<Map>(); |
|
43 |
|
44 for(File f : files){ |
|
45 if(Utils.hasFileWithSuffix(f, ".lua")){ |
|
46 ret.add(new Map(f,MapType.TYPE_MISSION, c)); |
|
47 }else{ |
|
48 ret.add(new Map(f, MapType.TYPE_DEFAULT,c)); |
|
49 } |
|
50 } |
|
51 Collections.sort(ret); |
|
52 |
|
53 return ret; |
|
54 } |
|
55 |
|
56 public static List<String> getGameplay(Context c){ |
|
57 String[] files = Utils.getFileNamesFromRelativeDir(c, "Scripts/Multiplayer"); |
|
58 ArrayList<String> ret = new ArrayList<String>(); |
|
59 |
|
60 for(int i = 0; i < files.length; i++){ |
|
61 if(files[i].endsWith(".lua")){ |
|
62 ret.add(files[i].replace('_', ' ').substring(0, files[i].length()-4)); //replace _ by a space and removed the last four characters (.lua) |
|
63 } |
|
64 } |
|
65 ret.add(0,"None"); |
|
66 Collections.sort(ret); |
|
67 return ret; |
|
68 } |
|
69 |
|
70 public static List<String> getThemes(Context c){ |
|
71 List<String> list = Utils.getDirsWithFileSuffix(c, "Themes", "icon.png"); |
|
72 Collections.sort(list); |
|
73 return list; |
|
74 } |
|
75 |
|
76 public static List<Scheme> getSchemes(Context c){ |
|
77 List<Scheme> list = Scheme.getSchemes(c); |
|
78 Collections.sort(list); |
|
79 return list; |
|
80 } |
|
81 |
|
82 public static List<Weapon> getWeapons(Context c){ |
|
83 List<Weapon> list = Weapon.getWeapons(c); |
|
84 Collections.sort(list); |
|
85 return list; |
|
86 } |
|
87 |
|
88 public static ArrayList<HashMap<String, ?>> getGraves(Context c){ |
|
89 String pathPrefix = Utils.getDataPath(c) + "Graphics/Graves/"; |
|
90 ArrayList<String> names = Utils.getFilesFromDirWithSuffix(c,"Graphics/Graves", ".png", true); |
|
91 ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(names.size()); |
|
92 |
|
93 for(String s : names){ |
|
94 HashMap<String, Object> map = new HashMap<String, Object>(); |
|
95 map.put("txt", s); |
|
96 Bitmap b = BitmapFactory.decodeFile(pathPrefix + s + ".png");//create a full path - decode to to a bitmap |
|
97 int width = b.getWidth(); |
|
98 if(b.getHeight() > width){//some pictures contain more 'frames' underneath each other, if so we only use the first frame |
|
99 Bitmap tmp = Bitmap.createBitmap(width, width, b.getConfig()); |
|
100 int[] pixels = new int[width * width]; |
|
101 b.getPixels(pixels, 0,width,0,0,width,width); |
|
102 tmp.setPixels(pixels,0,width,0,0,width,width); |
|
103 b.recycle(); |
|
104 b = tmp; |
|
105 } |
|
106 map.put("img", b); |
|
107 data.add(map); |
|
108 } |
|
109 return data; |
|
110 } |
|
111 |
|
112 public static ArrayList<HashMap<String, ?>> getFlags(Context c){ |
|
113 String pathPrefix = Utils.getDataPath(c) + "Graphics/Flags/"; |
|
114 ArrayList<String> names = Utils.getFilesFromDirWithSuffix(c, "Graphics/Flags", ".png", true); |
|
115 ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(names.size()); |
|
116 |
|
117 for(String s : names){ |
|
118 HashMap<String, Object> map = new HashMap<String, Object>(); |
|
119 map.put("txt", s); |
|
120 Bitmap b = BitmapFactory.decodeFile(pathPrefix + s + ".png");//create a full path - decode to to a bitmap |
|
121 map.put("img", b); |
|
122 data.add(map); |
|
123 } |
|
124 return data; |
|
125 } |
|
126 |
|
127 public static ArrayList<String> getVoices(Context c){ |
|
128 File[] files = Utils.getFilesFromRelativeDir(c, "Sounds/voices"); |
|
129 ArrayList<String> ret = new ArrayList<String>(); |
|
130 |
|
131 for(File f : files){ |
|
132 if(f.isDirectory()) ret.add(f.getName()); |
|
133 } |
|
134 return ret; |
|
135 } |
|
136 |
|
137 public static ArrayList<String> getForts(Context c){ |
|
138 return Utils.getFilesFromDirWithSuffix(c,"Forts", "L.png", true); |
|
139 } |
|
140 public static ArrayList<HashMap<String, ?>> getTypes(Context c){ |
|
141 ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(6); |
|
142 String[] levels = {c.getString(R.string.human), c.getString(R.string.bot5), c.getString(R.string.bot4), c.getString(R.string.bot3), c.getString(R.string.bot2), c.getString(R.string.bot1)}; |
|
143 int[] images = {R.drawable.human, R.drawable.bot5, R.drawable.bot4, R.drawable.bot3, R.drawable.bot2, R.drawable.bot1}; |
|
144 |
|
145 for(int i = 0; i < levels.length; i++){ |
|
146 HashMap<String, Object> map = new HashMap<String, Object>(); |
|
147 map.put("txt", levels[i]); |
|
148 map.put("img", images[i]); |
|
149 data.add(map); |
|
150 } |
|
151 |
|
152 return data; |
|
153 } |
|
154 |
|
155 public static ArrayList<HashMap<String, ?>> getHats(Context c){ |
|
156 ArrayList<String> files = Utils.getFilesFromDirWithSuffix(c,"Graphics/Hats", ".png", true); |
|
157 String pathPrefix = Utils.getDataPath(c) + "Graphics/Hats/"; |
|
158 int size = files.size(); |
|
159 ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(size); |
|
160 |
|
161 HashMap<String, Object> hashmap; |
|
162 for(String s : files){ |
|
163 hashmap = new HashMap<String, Object>(); |
|
164 hashmap.put("txt", s); |
|
165 Bitmap b = BitmapFactory.decodeFile(pathPrefix + s + ".png");//create a full path - decode to to a bitmap |
|
166 b = Bitmap.createBitmap(b, 0,0,b.getWidth()/2, b.getWidth()/2); |
|
167 hashmap.put("img", b); |
|
168 data.add(hashmap); |
|
169 } |
|
170 |
|
171 return data; |
|
172 } |
|
173 |
|
174 public static List<HashMap<String, Object>> getTeams(Context c){ |
|
175 List<HashMap<String, Object>> ret = new ArrayList<HashMap<String, Object>>(); |
|
176 |
|
177 File teamsDir = new File(c.getFilesDir().getAbsolutePath() + '/' + Team.DIRECTORY_TEAMS); |
|
178 File[] teamFileNames = teamsDir.listFiles(); |
|
179 if(teamFileNames != null){ |
|
180 for(File s : teamFileNames){ |
|
181 Team t = Team.getTeamFromXml(s.getAbsolutePath()); |
|
182 if(t != null){ |
|
183 t.file = s.getName(); |
|
184 ret.add(teamToMap(t)); |
|
185 } |
|
186 } |
|
187 } |
|
188 return ret; |
|
189 } |
|
190 |
|
191 public static HashMap<String, Object> teamToMap(Team t){ |
|
192 HashMap<String, Object> hashmap = new HashMap<String, Object>(); |
|
193 hashmap.put("team", t); |
|
194 hashmap.put("txt", t.name); |
|
195 hashmap.put("color", t.color); |
|
196 hashmap.put("count", t.hogCount); |
|
197 switch(t.levels[0]){ |
|
198 case 0: |
|
199 hashmap.put("img", R.drawable.human); |
|
200 break; |
|
201 case 1: |
|
202 hashmap.put("img", R.drawable.bot5); |
|
203 break; |
|
204 case 2: |
|
205 hashmap.put("img", R.drawable.bot4); |
|
206 break; |
|
207 case 3: |
|
208 hashmap.put("img", R.drawable.bot3); |
|
209 break; |
|
210 case 4: |
|
211 hashmap.put("img", R.drawable.bot2); |
|
212 break; |
|
213 default: |
|
214 case 5: |
|
215 hashmap.put("img", R.drawable.bot1); |
|
216 break; |
|
217 } |
|
218 return hashmap; |
|
219 } |
|
220 } |