project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/EngineProtocol/Map.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 package org.hedgewars.hedgeroid.EngineProtocol;
       
    20 
       
    21 import java.io.File;
       
    22 import java.io.IOException;
       
    23 
       
    24 import android.content.Context;
       
    25 import android.graphics.drawable.Drawable;
       
    26 import android.os.Parcel;
       
    27 import android.os.Parcelable;
       
    28 
       
    29 public class Map implements Comparable<Map>, Parcelable{
       
    30 
       
    31 	private static final String MISSION_PREFIX = "Mission: ";
       
    32 
       
    33 	private String name;
       
    34 	private String path;
       
    35 	private String previewPath;
       
    36 	private MapType type;
       
    37 
       
    38 	public Map(File mapDir, MapType _type, Context c){
       
    39 		type = _type;
       
    40 
       
    41 		name = mapDir.getName();
       
    42 		path = mapDir.getAbsolutePath();
       
    43 		previewPath = path + "/preview.png";
       
    44 		
       
    45 		/*switch(type){
       
    46 		case TYPE_DEFAULT:
       
    47 			
       
    48 			break;
       
    49 		case TYPE_GENERATED:
       
    50 			//TODO
       
    51 			break;
       
    52 		case TYPE_MISSION:
       
    53 			name = MISSION_PREFIX + mapDir.getName();
       
    54 			path = mapDir.getAbsolutePath();
       
    55 			break;
       
    56 		}*/
       
    57 
       
    58 		
       
    59 	}
       
    60 	
       
    61 	public Map(Parcel in){
       
    62 		readFromParcel(in);
       
    63 	}
       
    64 
       
    65 	public String toString(){
       
    66 		switch(type){
       
    67 		default:
       
    68 		case TYPE_DEFAULT:
       
    69 			return name;
       
    70 		case TYPE_GENERATED:
       
    71 			return "bla";
       
    72 		case TYPE_MISSION:
       
    73 			return MISSION_PREFIX + name;
       
    74 		}
       
    75 	}
       
    76 	
       
    77 	public void sendToEngine(EngineProtocolNetwork epn) throws IOException{
       
    78 		epn.sendToEngine(String.format("emap %s",name));
       
    79 	}
       
    80 	
       
    81 	public MapType getType(){
       
    82 		return type;
       
    83 	}
       
    84 
       
    85 	public Drawable getDrawable(){
       
    86 		switch(type){
       
    87 		case TYPE_MISSION:
       
    88 		case TYPE_DEFAULT:
       
    89 			return Drawable.createFromPath(previewPath);
       
    90 		case TYPE_GENERATED:
       
    91 
       
    92 		default:
       
    93 			return null;
       
    94 		}
       
    95 	}
       
    96 
       
    97 	public int compareTo(Map another) {
       
    98 		switch(type){
       
    99 		case TYPE_GENERATED:
       
   100 			switch(another.getType()){
       
   101 			case TYPE_GENERATED:
       
   102 				return name.compareTo(another.name);
       
   103 			case TYPE_MISSION:
       
   104 				return -1;
       
   105 			case TYPE_DEFAULT:
       
   106 				return -1;
       
   107 			}
       
   108 		case TYPE_MISSION:
       
   109 			switch(another.getType()){
       
   110 			case TYPE_GENERATED:
       
   111 				return 1;
       
   112 			case TYPE_MISSION:
       
   113 				return name.compareTo(another.name);
       
   114 			case TYPE_DEFAULT:
       
   115 				return -1;
       
   116 			}
       
   117 		case TYPE_DEFAULT:
       
   118 			switch(another.getType()){
       
   119 			case TYPE_GENERATED:
       
   120 				return 1;
       
   121 			case TYPE_MISSION:
       
   122 				return 1;
       
   123 			case TYPE_DEFAULT:
       
   124 				return name.compareTo(another.name);
       
   125 			}
       
   126 		}
       
   127 		return 0;//default case this should never happen
       
   128 	}
       
   129 
       
   130 	public enum MapType{
       
   131 		TYPE_DEFAULT, TYPE_MISSION, TYPE_GENERATED
       
   132 	}
       
   133 
       
   134 	public int describeContents() {
       
   135 		return 0;
       
   136 	}
       
   137 	
       
   138 	public void writeToParcel(Parcel dest, int flags) {
       
   139 		dest.writeString(name);
       
   140 		dest.writeString(path);
       
   141 		dest.writeString(previewPath);
       
   142 		dest.writeString(type.name());
       
   143 	}
       
   144 	
       
   145 	private void readFromParcel(Parcel src){
       
   146 		name = src.readString();
       
   147 		path = src.readString();
       
   148 		previewPath = src.readString();
       
   149 		type = MapType.valueOf(src.readString());
       
   150 	}
       
   151 	public static final Parcelable.Creator<Map> CREATOR = new Parcelable.Creator<Map>() {
       
   152 		public Map createFromParcel(Parcel source) {
       
   153 			return new Map(source);
       
   154 		}
       
   155 		public Map[] newArray(int size) {
       
   156 			return new Map[size];
       
   157 		}
       
   158 		
       
   159 	};
       
   160 }