project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/StartGameActivity.java
branchhedgeroid
changeset 7857 2bc61f8841a1
parent 7855 ddcdedd3330b
parent 7695 6237d2f002ba
child 7859 519d5bc91dd3
equal deleted inserted replaced
7855:ddcdedd3330b 7857:2bc61f8841a1
     1 /*
       
     2  * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2011-2012 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;
       
    21 
       
    22 import org.hedgewars.hedgeroid.Datastructures.FrontendDataUtils;
       
    23 import org.hedgewars.hedgeroid.Datastructures.Map;
       
    24 import org.hedgewars.hedgeroid.Datastructures.Map.MapType;
       
    25 import org.hedgewars.hedgeroid.Datastructures.Scheme;
       
    26 import org.hedgewars.hedgeroid.Datastructures.Team;
       
    27 import org.hedgewars.hedgeroid.Datastructures.Weapon;
       
    28 import org.hedgewars.hedgeroid.EngineProtocol.GameConfig;
       
    29 
       
    30 import android.app.Activity;
       
    31 import android.content.Intent;
       
    32 import android.content.SharedPreferences;
       
    33 import android.graphics.drawable.Drawable;
       
    34 import android.os.Bundle;
       
    35 import android.os.Parcelable;
       
    36 import android.preference.PreferenceManager;
       
    37 import android.view.View;
       
    38 import android.view.View.OnClickListener;
       
    39 import android.widget.AdapterView;
       
    40 import android.widget.AdapterView.OnItemSelectedListener;
       
    41 import android.widget.ArrayAdapter;
       
    42 import android.widget.ImageButton;
       
    43 import android.widget.ImageView;
       
    44 import android.widget.Spinner;
       
    45 import android.widget.Toast;
       
    46 
       
    47 public class StartGameActivity extends Activity {
       
    48 
       
    49 	public static final int ACTIVITY_TEAM_SELECTOR = 0;
       
    50 
       
    51 	private GameConfig config = null;
       
    52 	private ImageButton start, back, team;
       
    53 	private Spinner maps, gameplay, gamescheme, weapons, themes;
       
    54 	private ImageView themeIcon, mapPreview, teamCount;
       
    55 
       
    56 	public void onCreate(Bundle savedInstanceState){
       
    57 		super.onCreate(savedInstanceState);
       
    58 
       
    59 		Scheme.parseBasicFlags(this);
       
    60 		config = new GameConfig();
       
    61 
       
    62 		setContentView(R.layout.starting_game);
       
    63 
       
    64 		back = (ImageButton) findViewById(R.id.btnBack);
       
    65 		team = (ImageButton) findViewById(R.id.btnTeams);
       
    66 		start = (ImageButton) findViewById(R.id.btnStart);
       
    67 
       
    68 		maps = (Spinner) findViewById(R.id.spinMaps);
       
    69 		gameplay = (Spinner) findViewById(R.id.spinGameplay);
       
    70 		gamescheme = (Spinner) findViewById(R.id.spinGamescheme);
       
    71 		weapons = (Spinner) findViewById(R.id.spinweapons);
       
    72 		themes = (Spinner) findViewById(R.id.spinTheme);
       
    73 
       
    74 		themeIcon = (ImageView) findViewById(R.id.imgTheme);
       
    75 		mapPreview = (ImageView) findViewById(R.id.mapPreview);
       
    76 		teamCount = (ImageView) findViewById(R.id.imgTeamsCount);
       
    77 
       
    78 		start.setOnClickListener(startClicker);
       
    79 		back.setOnClickListener(backClicker);
       
    80 		team.setOnClickListener(teamClicker);
       
    81 
       
    82 		ArrayAdapter<?> adapter = new ArrayAdapter<Map>(this, R.layout.listview_item, FrontendDataUtils.getMaps(this));
       
    83 		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       
    84 		maps.setAdapter(adapter);
       
    85 		maps.setOnItemSelectedListener(mapsClicker);
       
    86 		//set to first nonmap
       
    87 		for(int i = 0; i < adapter.getCount(); i++){
       
    88 			if(((Map)adapter.getItem(i)).getType() == MapType.TYPE_DEFAULT){
       
    89 				maps.setSelection(i, false);
       
    90 				break;
       
    91 			}
       
    92 		}
       
    93 
       
    94 		adapter = new ArrayAdapter<String>(this, R.layout.listview_item, FrontendDataUtils.getGameplay(this));
       
    95 		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       
    96 		gameplay.setAdapter(adapter);
       
    97 		gameplay.setOnItemSelectedListener(gameplayClicker);
       
    98 		//set to first nonmap
       
    99 		for(int i = 0; i < adapter.getCount(); i++){
       
   100 			if(((String)adapter.getItem(i)).equals("None")){
       
   101 				gameplay.setSelection(i, false);
       
   102 				break;
       
   103 			}
       
   104 		}
       
   105 
       
   106 		adapter = new ArrayAdapter<Scheme>(this, R.layout.listview_item, FrontendDataUtils.getSchemes(this));
       
   107 		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       
   108 		gamescheme.setAdapter(adapter);
       
   109 		gamescheme.setOnItemSelectedListener(schemeClicker);
       
   110 		//set to first nonmap
       
   111 		for(int i = 0; i < adapter.getCount(); i++){
       
   112 			if(((Scheme)adapter.getItem(i)).toString().equals("Default")){
       
   113 				gamescheme.setSelection(i, false);
       
   114 				break;
       
   115 			}
       
   116 		}
       
   117 		
       
   118 		
       
   119 		adapter = new ArrayAdapter<Weapon>(this, R.layout.listview_item, FrontendDataUtils.getWeapons(this));
       
   120 		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       
   121 		weapons.setAdapter(adapter);
       
   122 		weapons.setOnItemSelectedListener(weaponClicker);
       
   123 		for(int i = 0; i < adapter.getCount(); i++){
       
   124 			if(((Weapon)adapter.getItem(i)).toString().equals("Crazy")){
       
   125 				weapons.setSelection(i, false);
       
   126 				break;
       
   127 			}
       
   128 		}
       
   129 		adapter = new ArrayAdapter<String>(this, R.layout.listview_item, FrontendDataUtils.getThemes(this));
       
   130 		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       
   131 		themes.setAdapter(adapter);
       
   132 		themes.setOnItemSelectedListener(themesClicker);
       
   133 	}
       
   134 
       
   135 	private void startTeamsActivity(){
       
   136 		Intent i = new Intent(StartGameActivity.this, TeamSelectionActivity.class);
       
   137 		i.putParcelableArrayListExtra("teams", config.teams);
       
   138 		startActivityForResult(i, ACTIVITY_TEAM_SELECTOR);
       
   139 	}
       
   140 
       
   141 	public void onActivityResult(int requestCode, int resultCode, Intent data){
       
   142 		switch(requestCode){
       
   143 		case ACTIVITY_TEAM_SELECTOR:
       
   144 			if(resultCode == Activity.RESULT_OK){
       
   145 				Parcelable[] parcelables = (Parcelable[])data.getParcelableArrayExtra("teams");
       
   146 				config.teams.clear();
       
   147 				for(Parcelable t : parcelables){
       
   148 					config.teams.add((Team)t);
       
   149 				}
       
   150 				teamCount.getDrawable().setLevel(config.teams.size());
       
   151 			}
       
   152 			break;
       
   153 		}
       
   154 	}
       
   155 
       
   156 
       
   157 	private OnItemSelectedListener themesClicker = new OnItemSelectedListener(){
       
   158 
       
   159 		public void onItemSelected(AdapterView<?> arg0, View view, int position, long rowId) {
       
   160 			String themeName = (String) arg0.getAdapter().getItem(position);
       
   161 			Drawable themeIconDrawable = Drawable.createFromPath(Utils.getDataPath(StartGameActivity.this) + "Themes/" + themeName + "/icon@2X.png");
       
   162 			themeIcon.setImageDrawable(themeIconDrawable);
       
   163 			config.theme = themeName;
       
   164 		}
       
   165 
       
   166 		public void onNothingSelected(AdapterView<?> arg0) {
       
   167 		}
       
   168 
       
   169 	};
       
   170 
       
   171 	private OnItemSelectedListener mapsClicker = new OnItemSelectedListener(){
       
   172 
       
   173 		public void onItemSelected(AdapterView<?> arg0, View view, int position,long rowId) {
       
   174 			Map map = (Map)arg0.getAdapter().getItem(position);
       
   175 			mapPreview.setImageDrawable(map.getDrawable());
       
   176 			config.map = map;
       
   177 		}
       
   178 
       
   179 		public void onNothingSelected(AdapterView<?> arg0) {
       
   180 		}
       
   181 
       
   182 	};
       
   183 
       
   184 	private OnItemSelectedListener weaponClicker = new OnItemSelectedListener(){
       
   185 		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
       
   186 			config.weapon = (Weapon)arg0.getAdapter().getItem(arg2);
       
   187 		}
       
   188 		public void onNothingSelected(AdapterView<?> arg0) {
       
   189 
       
   190 		}
       
   191 	};
       
   192 	private OnItemSelectedListener schemeClicker = new OnItemSelectedListener(){
       
   193 		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
       
   194 			config.scheme = (Scheme)arg0.getAdapter().getItem(arg2);
       
   195 		}
       
   196 		public void onNothingSelected(AdapterView<?> arg0) {
       
   197 
       
   198 		}
       
   199 	};
       
   200 	private OnItemSelectedListener gameplayClicker = new OnItemSelectedListener(){
       
   201 		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
       
   202 			//config = ()arg0.getAdapter().getItem(arg2);
       
   203 		}
       
   204 		public void onNothingSelected(AdapterView<?> arg0) {
       
   205 
       
   206 		}
       
   207 	};
       
   208 
       
   209 	private OnClickListener startClicker = new OnClickListener(){
       
   210 		public void onClick(View v) {
       
   211 			if(config.teams.size() < 2){
       
   212 				Toast.makeText(StartGameActivity.this, R.string.not_enough_teams, Toast.LENGTH_LONG).show();
       
   213 				startTeamsActivity();
       
   214 			}
       
   215 			else{
       
   216 				Intent i = new Intent(StartGameActivity.this, SDLActivity.class);
       
   217 				i.putExtra("config", config);
       
   218 				startActivity(i);}
       
   219 		}
       
   220 	};
       
   221 
       
   222 	private OnClickListener backClicker = new OnClickListener(){
       
   223 		public void onClick(View v) {
       
   224 			finish();
       
   225 		}
       
   226 	};
       
   227 
       
   228 	private OnClickListener teamClicker = new OnClickListener(){
       
   229 		public void onClick(View v) {
       
   230 			startTeamsActivity();
       
   231 		}
       
   232 	};
       
   233 
       
   234 }