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