project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/EngineProtocol/FrontendDataUtils.java
changeset 6488 c356ddebab84
parent 6487 bd3c736c1eac
child 6489 e1f0058cfedd
equal deleted inserted replaced
6487:bd3c736c1eac 6488:c356ddebab84
     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.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.EngineProtocol.Map.MapType;
       
    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 List<String> getGameplay(Context c){
       
    56 		String[] files = Utils.getFileNamesFromRelativeDir(c, "Scripts/Multiplayer");
       
    57 		ArrayList<String> ret = new ArrayList<String>();
       
    58 		
       
    59 		for(int i = 0; i < files.length; i++){
       
    60 			if(files[i].endsWith(".lua")){
       
    61 				ret.add(files[i].replace('_', ' ').substring(0, files[i].length()-4)); //replace _ by a space and removed the last four characters (.lua)
       
    62 			}
       
    63 		}
       
    64 		ret.add(0,"None");
       
    65 		Collections.sort(ret);
       
    66 		return ret;	
       
    67 	}
       
    68 
       
    69 	public static List<String> getThemes(Context c){
       
    70 		List<String> list = Utils.getDirsWithFileSuffix(c, "Themes", "icon.png");
       
    71 		Collections.sort(list);
       
    72 		return list;
       
    73 	}
       
    74 
       
    75 	public static List<Scheme> getSchemes(Context c){
       
    76 		List<Scheme> list = Scheme.getSchemes(c);
       
    77 		Collections.sort(list);
       
    78 		return list;
       
    79 	}
       
    80 
       
    81 	public static List<Weapon> getWeapons(Context c){
       
    82 		List<Weapon> list = Weapon.getWeapons(c);
       
    83 		Collections.sort(list);
       
    84 		return list;
       
    85 	}
       
    86 
       
    87 	public static ArrayList<HashMap<String, ?>> getGraves(Context c){
       
    88 		String pathPrefix = Utils.getDataPath(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.getDataPath(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.getDataPath(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 List<HashMap<String, Object>> getTeams(Context c){
       
   171 		List<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 					t.file = s.getName();
       
   180 					ret.add(teamToMap(t));
       
   181 				}
       
   182 			}
       
   183 		}
       
   184 		return ret;
       
   185 	}
       
   186 
       
   187 	public static HashMap<String, Object> teamToMap(Team t){
       
   188 		HashMap<String, Object> hashmap = new HashMap<String, Object>();
       
   189 		hashmap.put("team", t);
       
   190 		hashmap.put("txt", t.name);
       
   191 		hashmap.put("color", t.color);
       
   192 		hashmap.put("count", t.hogCount);
       
   193 		switch(t.levels[0]){
       
   194 		case 0:
       
   195 			hashmap.put("img", R.drawable.human);
       
   196 			break;
       
   197 		case 1:
       
   198 			hashmap.put("img", R.drawable.bot5);
       
   199 			break;
       
   200 		case 2:
       
   201 			hashmap.put("img", R.drawable.bot4);
       
   202 			break;
       
   203 		case 3:
       
   204 			hashmap.put("img", R.drawable.bot3);
       
   205 			break;
       
   206 		case 4:
       
   207 			hashmap.put("img", R.drawable.bot2);
       
   208 			break;
       
   209 		default:
       
   210 		case 5:
       
   211 			hashmap.put("img", R.drawable.bot1);
       
   212 			break;
       
   213 		}
       
   214 		return hashmap;
       
   215 	}
       
   216 }