Added color and team hog count hedgeroid
authorXeli
Tue, 09 Aug 2011 20:53:37 +0200
branchhedgeroid
changeset 5506 2b0c4fcde4c6
parent 5504 96d735b83d43
child 5508 dcf1b3645af6
Added color and team hog count
project_files/Android-build/SDL-android-project/res/layout/team_selection_entry.xml
project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/EngineProtocol/Team.java
project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/TeamSelectionActivity.java
--- a/project_files/Android-build/SDL-android-project/res/layout/team_selection_entry.xml	Tue Aug 09 20:52:26 2011 +0200
+++ b/project_files/Android-build/SDL-android-project/res/layout/team_selection_entry.xml	Tue Aug 09 20:53:37 2011 +0200
@@ -1,10 +1,10 @@
 <?xml version="1.0" encoding="utf-8"?>
-<LinearLayout
+<RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
-  android:orientation="horizontal"
-  android:layout_width="match_parent"
-  android:layout_height="match_parent"
-  android:background="#8FFF">
+  android:layout_width="fill_parent"
+  android:layout_height="fill_parent"
+  android:background="#8FFF"
+  android:padding="3dip">
   
   <ImageView
   	android:id="@+id/imgDifficulty"
@@ -12,9 +12,34 @@
   	android:layout_width="wrap_content"
   	android:adjustViewBounds="true"
   	android:scaleType="centerInside"/>
+  <ImageView
+  	android:id="@+id/teamColor"
+  	android:layout_height="wrap_content"
+  	android:layout_width="wrap_content"
+  	android:layout_alignParentRight="true"
+  	android:layout_centerVertical="true"
+  	android:adjustViewBounds="true"
+  	android:scaleType="centerInside"
+  	android:src="@drawable/box"
+  	android:background="#FDA"/>
+  <ImageView
+  	android:id="@+id/teamCount"
+  	android:layout_height="fill_parent"
+  	android:layout_width="wrap_content"
+  	android:layout_toLeftOf="@id/teamColor"
+  	android:layout_alignTop="@id/imgDifficulty"
+  	android:layout_alignBottom="@id/imgDifficulty"
+  	android:adjustViewBounds="true"
+  	android:scaleType="centerInside"
+  	android:src="@drawable/teamcount7"/>
   <TextView
   	android:id="@+id/txtName"
   	android:layout_height="fill_parent"
-  	android:layout_width="fill_parent"
+  	android:layout_width="wrap_content"
+  	android:layout_toRightOf="@id/imgDifficulty"
+  	android:layout_toLeftOf="@id/teamCount"
+  	android:layout_alignTop="@id/imgDifficulty"
+  	android:layout_alignBottom="@id/imgDifficulty"
+  	android:gravity="center"
   	android:textColor="#FFF"/>
-</LinearLayout>
+</RelativeLayout>
--- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/EngineProtocol/Team.java	Tue Aug 09 20:52:26 2011 +0200
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/EngineProtocol/Team.java	Tue Aug 09 20:53:37 2011 +0200
@@ -5,6 +5,7 @@
 import java.io.FileReader;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.util.ArrayList;
 
 import org.xmlpull.v1.XmlPullParser;
 import org.xmlpull.v1.XmlPullParserException;
@@ -18,6 +19,28 @@
 public class Team implements Parcelable{
 
 	public static final String DIRECTORY_TEAMS = "teams";
+//	private static final Integer[] TEAM_COLORS = {
+//		0xffd12b42, /* red    */ 
+//		0xff4980c1, /* blue   */ 
+//		0xff6ab530, /* green  */ 
+//		0xffbc64c4, /* purple */ 
+//		0xffe76d14, /* orange */ 
+//		0xff3fb6e6, /* cyan   */ 
+//		0xffe3e90c, /* yellow */ 
+//		0xff61d4ac, /* mint   */ 
+//		0xfff1c3e1, /* pink   */ 
+//		/* add new colors here */
+//	};
+
+	private static final Integer[] TEAM_COLORS = {
+		0xff0000, /* red    */ 
+		0x00ff00, /* blue   */ 
+		0x0000ff, /* green  */ 
+	};
+
+	private static final int STATE_START = 0;
+	private static final int STATE_ROOT = 1;
+	private static final int STATE_HOG_ROOT = 2;
 
 	public String name, grave, flag, voice, fort, hash;
 
@@ -31,10 +54,13 @@
 	public String[] hats = new String[maxNumberOfHogs];
 	public String[] hogNames = new String[maxNumberOfHogs];
 	public int[] levels = new int[maxNumberOfHogs];
-	
+
+	public int hogCount = 4;
+	public int color = TEAM_COLORS[0];
+
 	public Team(){
 	}
-	
+
 	public Team(Parcel in){
 		readFromParcel(in);
 	}
@@ -54,28 +80,41 @@
 			return false;
 		}
 	}
-	
-	
-	public void sendToEngine(OutputStream os, int hogCount, int health, int color) throws IOException{
-		os.write(String.format("eaddteam %s %d %s", hash, color, name).getBytes());
-		os.write(String.format("egrave %s", grave).getBytes());
-		os.write(String.format("efort %s", fort).getBytes());
-		os.write(String.format("evoicepack %s", voice).getBytes());
-		os.write(String.format("eflag %s", flag).getBytes());
-		
+
+	public void setRandomColor(int[] illegalcolors){
+		Integer[] colorsToPickFrom = TEAM_COLORS;
+		if(illegalcolors != null){
+			ArrayList<Integer> colors = new ArrayList<Integer>();
+			for(int color : TEAM_COLORS){
+				boolean validColor = true;
+				for(int illegal : illegalcolors){
+					if(color == illegal) validColor = false;
+				}
+				if(validColor) colors.add(color);
+			}
+			if(colors.size() != 0) colorsToPickFrom = colors.toArray(new Integer[1]);
+		}
+		int index = (int)Math.round(Math.random()*(colorsToPickFrom.length-1));
+		color = colorsToPickFrom[index];
+	}
+
+
+	public void sendToEngine(EngineProtocolNetwork epn, int hogCount, int health) throws IOException{
+		epn.sendToEngine(String.format("eaddteam %s %d %s", hash, color, name));
+		epn.sendToEngine(String.format("egrave %s", grave));
+		epn.sendToEngine(String.format("efort %s", fort));
+		epn.sendToEngine(String.format("evoicepack %s", voice));
+		epn.sendToEngine(String.format("eflag %s", flag));
+
 		for(int i = 0; i < hogCount; i++){
-			os.write(String.format("eaddhh %d %d %s", levels[i], health, hogNames[i]).getBytes());
-			os.write(String.format("ehat %s", hats[i]).getBytes());
+			epn.sendToEngine(String.format("eaddhh %d %d %s", levels[i], health, hogNames[i]));
+			epn.sendToEngine(String.format("ehat %s", hats[i]));
 		}
-		os.flush();
 	}
-	
-	public static final int STATE_START = 0;
-	public static final int STATE_ROOT = 1;
-	public static final int STATE_HOG_ROOT = 2;
-	public static final int STATE_HOG_HAT = 3;
-	public static final int STATE_HOG_NAME = 4;
-	public static final int STATE_HOG_LEVEL = 5;
+
+	/*
+	 * XML METHODS
+	 */
 
 	/**
 	 * Read the xml file path and convert it to a Team object
@@ -181,45 +220,45 @@
 			serializer.setOutput(os, "UTF-8");	
 			serializer.startDocument("UTF-8", true);
 			serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
-			
+
 			serializer.startTag(null, "team");
+			serializer.startTag(null, "name");
+			serializer.text(name);
+			serializer.endTag(null, "name");
+			serializer.startTag(null, "flag");
+			serializer.text(flag);
+			serializer.endTag(null, "flag");
+			serializer.startTag(null, "fort");
+			serializer.text(fort);
+			serializer.endTag(null, "fort");
+			serializer.startTag(null, "grave");
+			serializer.text(grave);
+			serializer.endTag(null, "grave");
+			serializer.startTag(null, "voice");
+			serializer.text(voice);
+			serializer.endTag(null, "voice");
+			serializer.startTag(null, "hash");
+			serializer.text(hash);
+			serializer.endTag(null, "hash");
+
+			for(int i = 0; i < maxNumberOfHogs; i++){
+				serializer.startTag(null, "hog");
 				serializer.startTag(null, "name");
-				serializer.text(name);
+				serializer.text(hogNames[i]);
 				serializer.endTag(null, "name");
-				serializer.startTag(null, "flag");
-				serializer.text(flag);
-				serializer.endTag(null, "flag");
-				serializer.startTag(null, "fort");
-				serializer.text(fort);
-				serializer.endTag(null, "fort");
-				serializer.startTag(null, "grave");
-				serializer.text(grave);
-				serializer.endTag(null, "grave");
-				serializer.startTag(null, "voice");
-				serializer.text(voice);
-				serializer.endTag(null, "voice");
-				serializer.startTag(null, "hash");
-				serializer.text(hash);
-				serializer.endTag(null, "hash");
-				
-				for(int i = 0; i < maxNumberOfHogs; i++){
-					serializer.startTag(null, "hog");
-					serializer.startTag(null, "name");
-					serializer.text(hogNames[i]);
-					serializer.endTag(null, "name");
-					serializer.startTag(null, "hat");
-					serializer.text(hats[i]);
-					serializer.endTag(null, "hat");
-					serializer.startTag(null, "level");
-					serializer.text(String.valueOf(levels[i]));
-					serializer.endTag(null, "level");
-					
-					serializer.endTag(null, "hog");
-				}
+				serializer.startTag(null, "hat");
+				serializer.text(hats[i]);
+				serializer.endTag(null, "hat");
+				serializer.startTag(null, "level");
+				serializer.text(String.valueOf(levels[i]));
+				serializer.endTag(null, "level");
+
+				serializer.endTag(null, "hog");
+			}
 			serializer.endTag(null, "team");
 			serializer.endDocument();
 			serializer.flush();
-			
+
 		} catch (IOException e) {
 			e.printStackTrace();
 		}finally{
@@ -228,6 +267,15 @@
 			} catch (IOException e) {}
 		}
 	}
+	/*
+	 * END XML METHODS
+	 */
+
+
+
+	/*
+	 * PARCABLE METHODS
+	 */
 
 	public int describeContents() {
 		return 0;
@@ -243,9 +291,11 @@
 		dest.writeStringArray(hats);
 		dest.writeStringArray(hogNames);
 		dest.writeIntArray(levels);
+		dest.writeInt(color);
+		dest.writeInt(hogCount);
 	}
-		
-	
+
+
 	public void readFromParcel(Parcel src){
 		name = src.readString();
 		grave = src.readString();
@@ -256,6 +306,8 @@
 		src.readStringArray(hats);
 		src.readStringArray(hogNames);
 		src.readIntArray(levels);
+		color = src.readInt();
+		hogCount = src.readInt();
 	}
 
 	public static final Parcelable.Creator<Team> CREATOR = new Parcelable.Creator<Team>() {
@@ -265,7 +317,11 @@
 		public Team[] newArray(int size) {
 			return new Team[size];
 		}
-		
+
 	};
-	
+
+	/*
+	 * END PARCABLE METHODS
+	 */
+
 }
--- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/TeamSelectionActivity.java	Tue Aug 09 20:52:26 2011 +0200
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/TeamSelectionActivity.java	Tue Aug 09 20:53:37 2011 +0200
@@ -19,8 +19,11 @@
 import android.widget.AdapterView.AdapterContextMenuInfo;
 import android.widget.AdapterView.OnItemClickListener;
 import android.widget.ImageButton;
+import android.widget.ImageView;
 import android.widget.ListView;
+import android.widget.RelativeLayout;
 import android.widget.SimpleAdapter;
+import android.widget.SimpleAdapter.ViewBinder;
 
 public class TeamSelectionActivity extends Activity{
 
@@ -36,7 +39,7 @@
 
 		addTeam = (ImageButton) findViewById(R.id.btnAdd);
 		back = (ImageButton) findViewById(R.id.btnBack);
-		
+
 		addTeam.setOnClickListener(addTeamClicker);
 		back.setOnClickListener(backClicker);
 
@@ -46,7 +49,7 @@
 		availableTeams.setAdapter(adapter);
 		registerForContextMenu(availableTeams);
 		availableTeams.setOnItemClickListener(availableClicker);
-		
+
 		selectedTeams = (ListView) findViewById(R.id.selectedTeams);
 		selectedTeamsList = new ArrayList<HashMap<String, ?>>();
 		ArrayList<HashMap<String, ?>> toBeRemoved = new ArrayList<HashMap<String, ?>>();
@@ -60,28 +63,90 @@
 			}
 		}
 		for(HashMap<String, ?> hashmap : toBeRemoved) availableTeamsList.remove(hashmap);
-		
-		adapter = new SimpleAdapter(this, selectedTeamsList, R.layout.team_selection_entry, new String[]{"txt", "img"}, new int[]{R.id.txtName, R.id.imgDifficulty});
+
+		adapter = new SimpleAdapter(this, selectedTeamsList, R.layout.team_selection_entry, new String[]{"txt", "img", "color", "count"}, new int[]{R.id.txtName, R.id.imgDifficulty, R.id.teamColor, R.id.teamCount});
+		adapter.setViewBinder(viewBinder);
 		selectedTeams.setAdapter(adapter);
 		selectedTeams.setOnItemClickListener(selectedClicker);
-		
-		
-		
-		
+
+	}
+
+	private ViewBinder viewBinder = new ViewBinder(){
+		public boolean setViewValue(View view, Object data,	String textRepresentation) {
+			switch(view.getId()){
+			case R.id.teamColor:
+				setTeamColor(view, (Integer)data);
+				return true;
+			case R.id.teamCount:
+				setTeamHogCount((ImageView)view, (Integer)data);
+				return true;
+			default:
+				return false;
+			}
+		}
+	};
+
+	private void setTeamColor(int position, int color){
+		View iv = ((RelativeLayout)selectedTeams.getChildAt(position)).findViewById(R.id.teamCount);
+		setTeamColor(iv, color);
+	}
+	private void setTeamColor(View iv, int color){
+		iv.setBackgroundColor(color);
+	}
+
+	private void setTeamHogCount(int position, int count){
+		ImageView iv = (ImageView)((RelativeLayout)selectedTeams.getChildAt(position)).findViewById(R.id.teamCount);
+		setTeamHogCount(iv, count);
 	}
-	
+
+	private void setTeamHogCount(ImageView iv, int count){
+
+		switch(count){
+		case 0:
+			iv.setImageResource(R.drawable.teamcount0);
+			break;
+		case 1:
+			iv.setImageResource(R.drawable.teamcount1);
+			break;
+		case 2:
+			iv.setImageResource(R.drawable.teamcount2);
+			break;
+		case 3:
+			iv.setImageResource(R.drawable.teamcount3);
+			break;
+		case 4:
+			iv.setImageResource(R.drawable.teamcount4);
+			break;
+		case 5:
+			iv.setImageResource(R.drawable.teamcount5);
+			break;
+		case 6:
+			iv.setImageResource(R.drawable.teamcount6);
+			break;
+		case 7:
+			iv.setImageResource(R.drawable.teamcount7);
+			break;
+		case 8:
+			iv.setImageResource(R.drawable.teamcount8);
+			break;
+		case 9:
+			iv.setImageResource(R.drawable.teamcount9);
+			break;
+		}
+	}
+
 	public void onBackPressed(){
 		returnTeams();
 		super.onBackPressed();
 	}
-	
+
 	private OnClickListener addTeamClicker = new OnClickListener(){
 		public void onClick(View v) {
 			startActivity(new Intent(TeamSelectionActivity.this, TeamCreatorActivity.class));
-			
+
 		}
 	};
-	
+
 	private OnClickListener backClicker = new OnClickListener(){
 		public void onClick(View v){
 			returnTeams();
@@ -108,7 +173,7 @@
 		menu.add(ContextMenu.NONE, 0, ContextMenu.NONE, R.string.select);
 		menu.add(ContextMenu.NONE, 2, ContextMenu.NONE, R.string.edit);
 		menu.add(ContextMenu.NONE, 1, ContextMenu.NONE, R.string.delete);
-		
+
 	}
 	public boolean onContextItemSelected(MenuItem item){
 		AdapterView.AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
@@ -134,7 +199,17 @@
 	}
 
 	private void selectAvailableTeamsItem(int position){
-		selectedTeamsList.add((HashMap<String, ?>) availableTeamsList.get(position));
+		HashMap<String, Object> hash = (HashMap<String, Object>) availableTeamsList.get(position);
+		Team t = (Team)hash.get("team");
+		int[] illegalcolors = new int[selectedTeamsList.size()];
+		for(int i = 0; i < selectedTeamsList.size(); i++){
+			illegalcolors[i] = ((Team)selectedTeamsList.get(i).get("team")).color;
+		}
+		t.setRandomColor(illegalcolors);
+		hash.put("color", t.color);
+		hash.put("count", t.hogCount);
+		
+		selectedTeamsList.add(hash);
 		availableTeamsList.remove(position);
 		((SimpleAdapter)availableTeams.getAdapter()).notifyDataSetChanged();
 		((SimpleAdapter)selectedTeams.getAdapter()).notifyDataSetChanged();