project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/TeamlistAdapter.java
changeset 10017 de822cd3df3a
parent 7584 7831c84cc644
equal deleted inserted replaced
10015:4feced261c68 10017:de822cd3df3a
    37 import android.widget.BaseAdapter;
    37 import android.widget.BaseAdapter;
    38 import android.widget.ImageButton;
    38 import android.widget.ImageButton;
    39 import android.widget.TextView;
    39 import android.widget.TextView;
    40 
    40 
    41 public class TeamlistAdapter extends BaseAdapter {
    41 public class TeamlistAdapter extends BaseAdapter {
    42 	private boolean colorHogcountEnabled = false;
    42     private boolean colorHogcountEnabled = false;
    43 	private Listener listener;
    43     private Listener listener;
    44 	private List<TeamInGame> teams = new ArrayList<TeamInGame>();
    44     private List<TeamInGame> teams = new ArrayList<TeamInGame>();
    45 	
       
    46 	public void setColorHogcountEnabled(boolean colorHogcountEnabled) {
       
    47 		this.colorHogcountEnabled = colorHogcountEnabled;
       
    48 		notifyDataSetChanged();
       
    49 	}
       
    50 	
       
    51 	public void setListener(Listener listener) {
       
    52 		this.listener = listener;
       
    53 	}
       
    54 	
       
    55 	public int getCount() {
       
    56 		return teams.size();
       
    57 	}
       
    58 	
       
    59 	public TeamInGame getItem(int position) {
       
    60 		return teams.get(position);
       
    61 	}
       
    62 	
       
    63 	public long getItemId(int position) {
       
    64 		return position;
       
    65 	}
       
    66 	
       
    67 	@Override
       
    68 	public boolean hasStableIds() {
       
    69 		return false;
       
    70 	}
       
    71 	
       
    72 	public void updateTeamlist(Collection<TeamInGame> newTeams) {
       
    73 		teams.clear();
       
    74 		teams.addAll(newTeams);
       
    75 		Collections.sort(teams, TeamInGame.NAME_ORDER);
       
    76 		notifyDataSetChanged();
       
    77 	}
       
    78 	
       
    79 	public View getView(int position, View convertView, ViewGroup parent) {
       
    80 		View v = convertView;
       
    81 		if (v == null) {
       
    82 			LayoutInflater vi = LayoutInflater.from(parent.getContext());
       
    83 			v = vi.inflate(R.layout.listview_team, null);
       
    84 		}
       
    85 
    45 
    86 		TeamInGame team = getItem(position);
    46     public void setColorHogcountEnabled(boolean colorHogcountEnabled) {
    87 		TextView teamNameView = (TextView) v.findViewById(android.R.id.text1);
    47         this.colorHogcountEnabled = colorHogcountEnabled;
    88 		ImageButton colorButton = (ImageButton) v.findViewById(R.id.colorButton);
    48         notifyDataSetChanged();
    89 		ImageButton hogCountButton = (ImageButton) v.findViewById(R.id.hogCountButton);
    49     }
    90 		
    50 
    91 		teamNameView.setText(team.team.name);
    51     public void setListener(Listener listener) {
    92 		int teamImage;
    52         this.listener = listener;
    93 		if(team.ingameAttribs.remoteDriven) {
    53     }
    94 			teamImage = R.drawable.team_net_by_level;
    54 
    95 		} else {
    55     public int getCount() {
    96 			teamImage = R.drawable.team_local_by_level;
    56         return teams.size();
    97 		}
    57     }
    98 		
    58 
    99 		Drawable d = parent.getContext().getResources().getDrawable(teamImage).mutate();
    59     public TeamInGame getItem(int position) {
   100 		d.setLevel(team.team.hogs.get(0).level);
    60         return teams.get(position);
   101 		teamNameView.setCompoundDrawablesWithIntrinsicBounds(d, null, null, null);
    61     }
   102 		hogCountButton.getDrawable().setLevel(team.ingameAttribs.hogCount);
    62 
   103 		colorButton.setImageDrawable(new ColorDrawable(TeamIngameAttributes.TEAM_COLORS[team.ingameAttribs.colorIndex]));
    63     public long getItemId(int position) {
   104 		
    64         return position;
   105 		colorButton.setEnabled(colorHogcountEnabled);
    65     }
   106 		hogCountButton.setEnabled(colorHogcountEnabled);
    66 
   107 		
    67     @Override
   108 		colorButton.setOnClickListener(new ButtonClickListener(team, Type.COLOR_BUTTON));
    68     public boolean hasStableIds() {
   109 		hogCountButton.setOnClickListener(new ButtonClickListener(team, Type.HOGCOUNT_BUTTON));
    69         return false;
   110 		
    70     }
   111 		if(team.ingameAttribs.remoteDriven) {
    71 
   112 			teamNameView.setClickable(false);
    72     public void updateTeamlist(Collection<TeamInGame> newTeams) {
   113 		} else {
    73         teams.clear();
   114 			teamNameView.setOnClickListener(new ButtonClickListener(team, Type.TEAM_VIEW));
    74         teams.addAll(newTeams);
   115 		}
    75         Collections.sort(teams, TeamInGame.NAME_ORDER);
   116 		
    76         notifyDataSetChanged();
   117 		return v;
    77     }
   118 	}
    78 
   119 	
    79     public View getView(int position, View convertView, ViewGroup parent) {
   120 	private static enum Type {COLOR_BUTTON, HOGCOUNT_BUTTON, TEAM_VIEW}
    80         View v = convertView;
   121 	private final class ButtonClickListener implements OnClickListener {
    81         if (v == null) {
   122 		private final TeamInGame team;
    82             LayoutInflater vi = LayoutInflater.from(parent.getContext());
   123 		private final Type type;
    83             v = vi.inflate(R.layout.listview_team, null);
   124 		
    84         }
   125 		public ButtonClickListener(TeamInGame team, Type type) {
    85 
   126 			this.team = team;
    86         TeamInGame team = getItem(position);
   127 			this.type = type;
    87         TextView teamNameView = (TextView) v.findViewById(android.R.id.text1);
   128 		}
    88         ImageButton colorButton = (ImageButton) v.findViewById(R.id.colorButton);
   129 		
    89         ImageButton hogCountButton = (ImageButton) v.findViewById(R.id.hogCountButton);
   130 		public void onClick(View v) {
    90 
   131 			if(listener != null) {
    91         teamNameView.setText(team.team.name);
   132 				switch(type) {
    92         int teamImage;
   133 				case COLOR_BUTTON:
    93         if(team.ingameAttribs.remoteDriven) {
   134 					listener.onColorClicked(team);
    94             teamImage = R.drawable.team_net_by_level;
   135 					break;
    95         } else {
   136 				case HOGCOUNT_BUTTON:
    96             teamImage = R.drawable.team_local_by_level;
   137 					listener.onHogcountClicked(team);
    97         }
   138 					break;
    98 
   139 				case TEAM_VIEW:
    99         Drawable d = parent.getContext().getResources().getDrawable(teamImage).mutate();
   140 					listener.onTeamClicked(team);
   100         d.setLevel(team.team.hogs.get(0).level);
   141 					break;
   101         teamNameView.setCompoundDrawablesWithIntrinsicBounds(d, null, null, null);
   142 				default:
   102         hogCountButton.getDrawable().setLevel(team.ingameAttribs.hogCount);
   143 					throw new IllegalStateException();	
   103         colorButton.setImageDrawable(new ColorDrawable(TeamIngameAttributes.TEAM_COLORS[team.ingameAttribs.colorIndex]));
   144 				}
   104 
   145 			}
   105         colorButton.setEnabled(colorHogcountEnabled);
   146 		}
   106         hogCountButton.setEnabled(colorHogcountEnabled);
   147 	}
   107 
   148 	
   108         colorButton.setOnClickListener(new ButtonClickListener(team, Type.COLOR_BUTTON));
   149 	public interface Listener {
   109         hogCountButton.setOnClickListener(new ButtonClickListener(team, Type.HOGCOUNT_BUTTON));
   150 		void onTeamClicked(TeamInGame team);
   110 
   151 		void onColorClicked(TeamInGame team);
   111         if(team.ingameAttribs.remoteDriven) {
   152 		void onHogcountClicked(TeamInGame team);
   112             teamNameView.setClickable(false);
   153 	}
   113         } else {
       
   114             teamNameView.setOnClickListener(new ButtonClickListener(team, Type.TEAM_VIEW));
       
   115         }
       
   116 
       
   117         return v;
       
   118     }
       
   119 
       
   120     private static enum Type {COLOR_BUTTON, HOGCOUNT_BUTTON, TEAM_VIEW}
       
   121     private final class ButtonClickListener implements OnClickListener {
       
   122         private final TeamInGame team;
       
   123         private final Type type;
       
   124 
       
   125         public ButtonClickListener(TeamInGame team, Type type) {
       
   126             this.team = team;
       
   127             this.type = type;
       
   128         }
       
   129 
       
   130         public void onClick(View v) {
       
   131             if(listener != null) {
       
   132                 switch(type) {
       
   133                 case COLOR_BUTTON:
       
   134                     listener.onColorClicked(team);
       
   135                     break;
       
   136                 case HOGCOUNT_BUTTON:
       
   137                     listener.onHogcountClicked(team);
       
   138                     break;
       
   139                 case TEAM_VIEW:
       
   140                     listener.onTeamClicked(team);
       
   141                     break;
       
   142                 default:
       
   143                     throw new IllegalStateException();
       
   144                 }
       
   145             }
       
   146         }
       
   147     }
       
   148 
       
   149     public interface Listener {
       
   150         void onTeamClicked(TeamInGame team);
       
   151         void onColorClicked(TeamInGame team);
       
   152         void onHogcountClicked(TeamInGame team);
       
   153     }
   154 }
   154 }