project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/EngineProtocol/FrontendDataUtils.java
branchhedgeroid
changeset 6047 10011f051f9c
child 6049 7bc38086d771
equal deleted inserted replaced
6045:9a7cc0f29430 6047:10011f051f9c
       
     1 /*
       
     2  * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2011 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.EngineProtocol;
       
    21 
       
    22 import java.io.File;
       
    23 import java.util.ArrayList;
       
    24 import java.util.Arrays;
       
    25 import java.util.Collections;
       
    26 import java.util.HashMap;
       
    27 
       
    28 import org.hedgewars.hedgeroid.Utils;
       
    29 import org.hedgewars.hedgeroid.EngineProtocol.Map.MapType;
       
    30 import org.hedgewars.mobile.R;
       
    31 
       
    32 import android.content.Context;
       
    33 import android.graphics.Bitmap;
       
    34 import android.graphics.BitmapFactory;
       
    35 
       
    36 public class FrontendDataUtils {
       
    37 
       
    38 
       
    39 	public static ArrayList<Map> getMaps(Context c){
       
    40 		File[] files = Utils.getFilesFromRelativeDir(c,"Maps");
       
    41 		ArrayList<Map> ret = new ArrayList<Map>();
       
    42 
       
    43 		for(File f : files){
       
    44 			if(Utils.hasFileWithSuffix(f, ".lua")){
       
    45 				ret.add(new Map(f,MapType.TYPE_MISSION, c));
       
    46 			}else{
       
    47 				ret.add(new Map(f, MapType.TYPE_DEFAULT,c));
       
    48 			}
       
    49 		}
       
    50 		Collections.sort(ret);
       
    51 
       
    52 		return ret;
       
    53 	}
       
    54 
       
    55 	public static String[] getGameplay(Context c){
       
    56 		String[] files = Utils.getFileNamesFromRelativeDir(c, "Scripts/Multiplayer");
       
    57 		int retCounter = 0;
       
    58 
       
    59 		for(int i = 0; i < files.length; i++){
       
    60 			if(files[i].endsWith(".lua")){
       
    61 				files[i] = files[i].replace('_', ' ').substring(0, files[i].length()-4); //replace _ by a space and removed the last four characters (.lua)
       
    62 				retCounter++;
       
    63 			}else files[i] = null;
       
    64 		}
       
    65 		String[] ret = new String[retCounter];
       
    66 		retCounter = 0;
       
    67 		for(String s : files){
       
    68 			if(s != null) ret[retCounter++] = s;
       
    69 		}
       
    70 		Arrays.sort(ret);
       
    71 
       
    72 		return ret;	
       
    73 	}
       
    74 
       
    75 	public static String[] getThemes(Context c){
       
    76 		return Utils.getDirsWithFileSuffix(c, "Themes", "icon.png");
       
    77 	}
       
    78 
       
    79 	public static ArrayList<Scheme> getSchemes(Context c){
       
    80 		return Scheme.getSchemes(c);
       
    81 	}
       
    82 
       
    83 	public static ArrayList<Weapon> getWeapons(Context c){
       
    84 		return Weapon.getWeapons(c);
       
    85 	}
       
    86 
       
    87 	public static ArrayList<HashMap<String, ?>> getGraves(Context c){
       
    88 		String pathPrefix = Utils.getDownloadPath(c) + "Graphics/Graves/";
       
    89 		ArrayList<String> names = Utils.getFilesFromDirWithSuffix(c, "Graphics/Graves", ".png", true);
       
    90 		ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(names.size());
       
    91 
       
    92 		for(String s : names){
       
    93 			HashMap<String, Object> map = new HashMap<String, Object>();
       
    94 			map.put("txt", s);
       
    95 			Bitmap b = BitmapFactory.decodeFile(pathPrefix + s + ".png");//create a full path - decode to to a bitmap
       
    96 			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 				Bitmap tmp = Bitmap.createBitmap(b, 0, 0, width, width);
       
    99 				b.recycle();
       
   100 				b = tmp;
       
   101 			}
       
   102 			map.put("img", b);
       
   103 			data.add(map);
       
   104 		}
       
   105 		return data;
       
   106 	}
       
   107 
       
   108 	public static ArrayList<HashMap<String, ?>> getFlags(Context c){
       
   109 		String pathPrefix = Utils.getDownloadPath(c) + "Graphics/Flags/";
       
   110 		ArrayList<String> names = Utils.getFilesFromDirWithSuffix(c, "Graphics/Flags", ".png", true);
       
   111 		ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(names.size());
       
   112 
       
   113 		for(String s : names){
       
   114 			HashMap<String, Object> map = new HashMap<String, Object>();
       
   115 			map.put("txt", s);
       
   116 			Bitmap b = BitmapFactory.decodeFile(pathPrefix + s + ".png");//create a full path - decode to to a bitmap
       
   117 			map.put("img", b);
       
   118 			data.add(map);
       
   119 		}
       
   120 		return data;
       
   121 	}
       
   122 
       
   123 	public static ArrayList<String> getVoices(Context c){
       
   124 		File[] files = Utils.getFilesFromRelativeDir(c, "Sounds/voices");
       
   125 		ArrayList<String> ret = new ArrayList<String>();
       
   126 
       
   127 		for(File f : files){
       
   128 			if(f.isDirectory()) ret.add(f.getName());
       
   129 		}
       
   130 		return ret;
       
   131 	}
       
   132 
       
   133 	public static ArrayList<String> getForts(Context c){
       
   134 		return Utils.getFilesFromDirWithSuffix(c, "Forts", "L.png", true);
       
   135 	}
       
   136 	public static ArrayList<HashMap<String, ?>> getTypes(Context c){
       
   137 		ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(6);
       
   138 		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)};
       
   139 		int[] images = {R.drawable.human, R.drawable.bot5, R.drawable.bot4, R.drawable.bot3, R.drawable.bot2, R.drawable.bot1};
       
   140 
       
   141 		for(int i = 0; i < levels.length; i++){
       
   142 			HashMap<String, Object> map = new HashMap<String, Object>();
       
   143 			map.put("txt", levels[i]);
       
   144 			map.put("img", images[i]);
       
   145 			data.add(map);
       
   146 		}
       
   147 
       
   148 		return data;
       
   149 	}
       
   150 
       
   151 	public static ArrayList<HashMap<String, ?>> getHats(Context c){
       
   152 		ArrayList<String> files = Utils.getFilesFromDirWithSuffix(c, "Graphics/Hats", ".png", true);
       
   153 		String pathPrefix = Utils.getDownloadPath(c) + "Graphics/Hats/";
       
   154 		int size = files.size();
       
   155 		ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(size);
       
   156 
       
   157 		HashMap<String, Object> hashmap; 
       
   158 		for(String s : files){
       
   159 			hashmap = new HashMap<String, Object>();
       
   160 			hashmap.put("txt", s);
       
   161 			Bitmap b = BitmapFactory.decodeFile(pathPrefix + s + ".png");//create a full path - decode to to a bitmap
       
   162 			b = Bitmap.createBitmap(b, 0,0,b.getWidth()/2, b.getWidth()/2);
       
   163 			hashmap.put("img", b);
       
   164 			data.add(hashmap);
       
   165 		}
       
   166 
       
   167 		return data;
       
   168 	}
       
   169 
       
   170 	public static ArrayList<HashMap<String, Object>> getTeams(Context c){
       
   171 		ArrayList<HashMap<String, Object>> ret = new ArrayList<HashMap<String, Object>>();
       
   172 
       
   173 		File teamsDir = new File(c.getFilesDir().getAbsolutePath() + '/' + Team.DIRECTORY_TEAMS);
       
   174 		File[] teamFileNames = teamsDir.listFiles();
       
   175 		if(teamFileNames != null){
       
   176 			for(File s : teamFileNames){
       
   177 				Team t = Team.getTeamFromXml(s.getAbsolutePath());
       
   178 				if(t != null){
       
   179 					ret.add(teamToHashMap(t));
       
   180 				}
       
   181 			}
       
   182 		}
       
   183 		return ret;
       
   184 	}
       
   185 
       
   186 	public static HashMap<String, Object> teamToHashMap(Team t){
       
   187 		HashMap<String, Object> hashmap = new HashMap<String, Object>();
       
   188 		hashmap.put("team", t);
       
   189 		hashmap.put("txt", t.name);
       
   190 		hashmap.put("color", t.color);
       
   191 		hashmap.put("count", t.hogCount);
       
   192 		switch(t.levels[0]){
       
   193 		case 0:
       
   194 			hashmap.put("img", R.drawable.human);
       
   195 			break;
       
   196 		case 1:
       
   197 			hashmap.put("img", R.drawable.bot5);
       
   198 			break;
       
   199 		case 2:
       
   200 			hashmap.put("img", R.drawable.bot4);
       
   201 			break;
       
   202 		case 3:
       
   203 			hashmap.put("img", R.drawable.bot3);
       
   204 			break;
       
   205 		case 4:
       
   206 			hashmap.put("img", R.drawable.bot2);
       
   207 			break;
       
   208 		default:
       
   209 		case 5:
       
   210 			hashmap.put("img", R.drawable.bot1);
       
   211 			break;
       
   212 		}
       
   213 		return hashmap;
       
   214 	}
       
   215 }