project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Datastructures/FrontendDataUtils.java
changeset 7586 33924ff4af50
parent 7584 7831c84cc644
equal deleted inserted replaced
7584:7831c84cc644 7586:33924ff4af50
    23 import java.io.File;
    23 import java.io.File;
    24 import java.io.FileNotFoundException;
    24 import java.io.FileNotFoundException;
    25 import java.util.ArrayList;
    25 import java.util.ArrayList;
    26 import java.util.HashMap;
    26 import java.util.HashMap;
    27 import java.util.List;
    27 import java.util.List;
       
    28 import java.util.Map;
    28 
    29 
    29 import org.hedgewars.hedgeroid.R;
    30 import org.hedgewars.hedgeroid.R;
    30 import org.hedgewars.hedgeroid.util.FileUtils;
    31 import org.hedgewars.hedgeroid.util.FileUtils;
    31 
    32 
    32 import android.content.Context;
    33 import android.content.Context;
    36 public class FrontendDataUtils {
    37 public class FrontendDataUtils {
    37 
    38 
    38 	/**
    39 	/**
    39 	 * @throws FileNotFoundException if the sdcard isn't available or the Maps directory doesn't exist
    40 	 * @throws FileNotFoundException if the sdcard isn't available or the Maps directory doesn't exist
    40 	 */
    41 	 */
    41 	public static ArrayList<MapFile> getMaps(Context c) throws FileNotFoundException {
    42 	public static List<MapFile> getMaps(Context c) throws FileNotFoundException {
    42 		File[] files = FileUtils.getFilesFromRelativeDir(c,"Maps");
    43 		File[] files = FileUtils.getFilesFromRelativeDir(c,"Maps");
    43 		ArrayList<MapFile> ret = new ArrayList<MapFile>();
    44 		List<MapFile> ret = new ArrayList<MapFile>();
    44 
    45 
    45 		for(File f : files) {
    46 		for(File f : files) {
    46 			boolean isMission = FileUtils.hasFileWithSuffix(f, ".lua");
    47 			boolean isMission = FileUtils.hasFileWithSuffix(f, ".lua");
    47 			ret.add(new MapFile(f.getName(), isMission));
    48 			ret.add(new MapFile(f.getName(), isMission));
    48 		}
    49 		}
    54 	 * Returns a list of all multiplayer scripts (game styles)
    55 	 * Returns a list of all multiplayer scripts (game styles)
    55 	 * @throws FileNotFoundException if the sdcard isn't available or the Scripts/Multiplayer directory doesn't exist
    56 	 * @throws FileNotFoundException if the sdcard isn't available or the Scripts/Multiplayer directory doesn't exist
    56 	 */
    57 	 */
    57 	public static List<String> getGameStyles(Context c) throws FileNotFoundException {
    58 	public static List<String> getGameStyles(Context c) throws FileNotFoundException {
    58 		File[] files = FileUtils.getFilesFromRelativeDir(c, "Scripts/Multiplayer");
    59 		File[] files = FileUtils.getFilesFromRelativeDir(c, "Scripts/Multiplayer");
    59 		ArrayList<String> ret = new ArrayList<String>();
    60 		List<String> ret = new ArrayList<String>();
    60 		/*
    61 		/*
    61 		 * Caution: It is important that the "empty" style has this exact name, because
    62 		 * Caution: It is important that the "empty" style has this exact name, because
    62 		 * it will be interpreted as "don't load a script" by the frontlib, and also by
    63 		 * it will be interpreted as "don't load a script" by the frontlib, and also by
    63 		 * the QtFrontend in a netgame. This should probably be improved some time
    64 		 * the QtFrontend in a netgame. This should probably be improved some time
    64 		 * (maybe TODO add a dummy script called "Normal" to the MP scripts?) 
    65 		 * (maybe TODO add a dummy script called "Normal" to the MP scripts?) 
    82 	}
    83 	}
    83 
    84 
    84 	/**
    85 	/**
    85 	 * @throws FileNotFoundException if the sdcard isn't available or the Graphics/Graves directory doesn't exist
    86 	 * @throws FileNotFoundException if the sdcard isn't available or the Graphics/Graves directory doesn't exist
    86 	 */
    87 	 */
    87 	public static ArrayList<HashMap<String, ?>> getGraves(Context c) throws FileNotFoundException {
    88 	public static List<Map<String, ?>> getGraves(Context c) throws FileNotFoundException {
    88 		File gravePath = new File(new File(FileUtils.getDataPathFile(c), "Graphics"), "Graves");
    89 		File gravePath = FileUtils.getDataPathFile(c, "Graphics", "Graves");
    89 		ArrayList<String> names = FileUtils.getFileNamesFromDirWithSuffix(c,"Graphics/Graves", ".png", true);
    90 		List<String> names = FileUtils.getFileNamesFromDirWithSuffix(c,"Graphics/Graves", ".png", true);
    90 		ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(names.size());
    91 		List<Map<String, ?>> data = new ArrayList<Map<String, ?>>(names.size());
    91 
    92 
    92 		for(String s : names){
    93 		for(String s : names){
    93 			HashMap<String, Object> map = new HashMap<String, Object>();
    94 			HashMap<String, Object> map = new HashMap<String, Object>();
    94 			map.put("txt", s);
    95 			map.put("txt", s);
    95 			Bitmap b = BitmapFactory.decodeFile(new File(gravePath, s + ".png").getAbsolutePath());
    96 			Bitmap b = BitmapFactory.decodeFile(new File(gravePath, s + ".png").getAbsolutePath());
    96 			int width = b.getWidth();
    97 			int width = b.getWidth();
    97 			if(b.getHeight() > width){//some pictures contain more 'frames' underneath each other, if so we only use the first frame
    98 			if(b.getHeight() > width){
    98                                 Bitmap tmp = Bitmap.createBitmap(width, width, b.getConfig());
    99 				// some pictures contain more 'frames' underneath each other, if so we only use the first frame
    99                                 int[] pixels = new int[width * width];
   100 				b = Bitmap.createBitmap(b, 0, 0, width, width);
   100                                 b.getPixels(pixels, 0,width,0,0,width,width);
       
   101 				tmp.setPixels(pixels,0,width,0,0,width,width);
       
   102                                 b.recycle();
       
   103 				b = tmp;
       
   104 			}
   101 			}
   105 			map.put("img", b);
   102 			map.put("img", b);
   106 			data.add(map);
   103 			data.add(map);
   107 		}
   104 		}
   108 		return data;
   105 		return data;
   109 	}
   106 	}
   110 
   107 
   111 	/**
   108 	/**
   112 	 * @throws FileNotFoundException if the sdcard isn't available or the Graphics/Graves directory doesn't exist
   109 	 * @throws FileNotFoundException if the sdcard isn't available or the Graphics/Graves directory doesn't exist
   113 	 */
   110 	 */
   114 	public static ArrayList<HashMap<String, ?>> getFlags(Context c) throws FileNotFoundException {
   111 	public static List<Map<String, ?>> getFlags(Context c) throws FileNotFoundException {
   115 		File flagsPath = new File(new File(FileUtils.getDataPathFile(c), "Graphics"), "Flags");
   112 		File flagsPath = FileUtils.getDataPathFile(c, "Graphics", "Flags");
   116 		ArrayList<String> names = FileUtils.getFileNamesFromDirWithSuffix(c, "Graphics/Flags", ".png", true);
   113 		List<String> names = FileUtils.getFileNamesFromDirWithSuffix(c, "Graphics/Flags", ".png", true);
   117 		ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(names.size());
   114 		List<Map<String, ?>> data = new ArrayList<Map<String, ?>>(names.size());
   118 
   115 
   119 		for(String s : names){
   116 		for(String s : names){
   120 			HashMap<String, Object> map = new HashMap<String, Object>();
   117 			Map<String, Object> map = new HashMap<String, Object>();
   121 			map.put("txt", s);
   118 			map.put("txt", s);
   122 			Bitmap b = BitmapFactory.decodeFile(new File(flagsPath, s + ".png").getAbsolutePath());
   119 			Bitmap b = BitmapFactory.decodeFile(new File(flagsPath, s + ".png").getAbsolutePath());
   123 			map.put("img", b);
   120 			map.put("img", b);
   124 			data.add(map);
   121 			data.add(map);
   125 		}
   122 		}
   127 	}
   124 	}
   128 
   125 
   129 	/**
   126 	/**
   130 	 * @throws FileNotFoundException if the sdcard isn't available or the Sounds/voices directory doesn't exist
   127 	 * @throws FileNotFoundException if the sdcard isn't available or the Sounds/voices directory doesn't exist
   131 	 */
   128 	 */
   132 	public static ArrayList<String> getVoices(Context c) throws FileNotFoundException {
   129 	public static List<String> getVoices(Context c) throws FileNotFoundException {
   133 		File[] files = FileUtils.getFilesFromRelativeDir(c, "Sounds/voices");
   130 		File[] files = FileUtils.getFilesFromRelativeDir(c, "Sounds/voices");
   134 		ArrayList<String> ret = new ArrayList<String>();
   131 		List<String> ret = new ArrayList<String>();
   135 
   132 
   136 		for(File f : files){
   133 		for(File f : files){
   137 			if(f.isDirectory()) ret.add(f.getName());
   134 			if(f.isDirectory()) ret.add(f.getName());
   138 		}
   135 		}
   139 		return ret;
   136 		return ret;
   140 	}
   137 	}
   141 
   138 
   142 	/**
   139 	/**
   143 	 * @throws FileNotFoundException if the sdcard isn't available or the Forts directory doesn't exist
   140 	 * @throws FileNotFoundException if the sdcard isn't available or the Forts directory doesn't exist
   144 	 */
   141 	 */
   145 	public static ArrayList<String> getForts(Context c) throws FileNotFoundException {
   142 	public static List<String> getForts(Context c) throws FileNotFoundException {
   146 		return FileUtils.getFileNamesFromDirWithSuffix(c,"Forts", "L.png", true);
   143 		return FileUtils.getFileNamesFromDirWithSuffix(c,"Forts", "L.png", true);
   147 	}
   144 	}
   148 	
   145 	
   149 	public static ArrayList<HashMap<String, ?>> getTypes(Context c){
   146 	public static List<Map<String, ?>> getTypes(Context c){
   150 		ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(6);
   147 		List<Map<String, ?>> data = new ArrayList<Map<String, ?>>(6);
   151 		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)};
   148 		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)};
   152 		int[] images = {R.drawable.human, R.drawable.bot5, R.drawable.bot4, R.drawable.bot3, R.drawable.bot2, R.drawable.bot1};
   149 		int[] images = {R.drawable.human, R.drawable.bot5, R.drawable.bot4, R.drawable.bot3, R.drawable.bot2, R.drawable.bot1};
   153 
   150 
   154 		for(int i = 0; i < levels.length; i++){
   151 		for(int i = 0; i < levels.length; i++){
   155 			HashMap<String, Object> map = new HashMap<String, Object>();
   152 			Map<String, Object> map = new HashMap<String, Object>();
   156 			map.put("txt", levels[i]);
   153 			map.put("txt", levels[i]);
   157 			map.put("img", images[i]);
   154 			map.put("img", images[i]);
   158 			map.put("level", i);
   155 			map.put("level", i);
   159 			
   156 			
   160 			data.add(map);
   157 			data.add(map);
   164 	}
   161 	}
   165 
   162 
   166 	/**
   163 	/**
   167 	 * @throws FileNotFoundException if the sdcard isn't available or the Graphics/Hats directory doesn't exist
   164 	 * @throws FileNotFoundException if the sdcard isn't available or the Graphics/Hats directory doesn't exist
   168 	 */
   165 	 */
   169 	public static ArrayList<HashMap<String, ?>> getHats(Context c) throws FileNotFoundException {
   166 	public static List<Map<String, ?>> getHats(Context c) throws FileNotFoundException {
   170 		ArrayList<String> files = FileUtils.getFileNamesFromDirWithSuffix(c,"Graphics/Hats", ".png", true);
   167 		List<String> files = FileUtils.getFileNamesFromDirWithSuffix(c,"Graphics/Hats", ".png", true);
   171 		File hatsPath = new File(new File(FileUtils.getDataPathFile(c), "Graphics"), "Hats");
   168 		File hatsPath = FileUtils.getDataPathFile(c, "Graphics", "Hats");
   172 		int size = files.size();
   169 		int size = files.size();
   173 		ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(size);
   170 		List<Map<String, ?>> data = new ArrayList<Map<String, ?>>(size);
   174 
   171 
   175 		HashMap<String, Object> hashmap; 
       
   176 		for(String s : files){
   172 		for(String s : files){
   177 			hashmap = new HashMap<String, Object>();
   173 			Map<String, Object> hashmap = new HashMap<String, Object>();
   178 			hashmap.put("txt", s);
   174 			hashmap.put("txt", s);
   179 			Bitmap b = BitmapFactory.decodeFile(new File(hatsPath, s + ".png").getAbsolutePath());
   175 			Bitmap b = BitmapFactory.decodeFile(new File(hatsPath, s + ".png").getAbsolutePath());
   180 			b = Bitmap.createBitmap(b, 0,0,b.getWidth()/2, b.getWidth()/2);
   176 			b = Bitmap.createBitmap(b, 0,0,b.getWidth()/2, b.getWidth()/2);
   181 			hashmap.put("img", b);
   177 			hashmap.put("img", b);
   182 			data.add(hashmap);
   178 			data.add(hashmap);