project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/StartGameActivity.java
changeset 7582 714310efad8f
parent 7580 c92596feac0d
child 7584 7831c84cc644
equal deleted inserted replaced
7580:c92596feac0d 7582:714310efad8f
     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 java.io.File;
       
    23 import java.io.FileNotFoundException;
       
    24 import java.io.IOException;
       
    25 import java.util.ArrayList;
       
    26 import java.util.Collections;
       
    27 import java.util.List;
       
    28 import java.util.UUID;
       
    29 
       
    30 import org.hedgewars.hedgeroid.Datastructures.FrontendDataUtils;
       
    31 import org.hedgewars.hedgeroid.Datastructures.GameConfig;
       
    32 import org.hedgewars.hedgeroid.Datastructures.MapFile;
       
    33 import org.hedgewars.hedgeroid.Datastructures.MapRecipe;
       
    34 import org.hedgewars.hedgeroid.Datastructures.Scheme;
       
    35 import org.hedgewars.hedgeroid.Datastructures.Schemes;
       
    36 import org.hedgewars.hedgeroid.Datastructures.TeamInGame;
       
    37 import org.hedgewars.hedgeroid.Datastructures.Weaponset;
       
    38 import org.hedgewars.hedgeroid.Datastructures.Weaponsets;
       
    39 import org.hedgewars.hedgeroid.util.FileUtils;
       
    40 
       
    41 import android.app.Activity;
       
    42 import android.content.Intent;
       
    43 import android.graphics.drawable.Drawable;
       
    44 import android.os.Bundle;
       
    45 import android.view.View;
       
    46 import android.view.View.OnClickListener;
       
    47 import android.widget.AdapterView;
       
    48 import android.widget.AdapterView.OnItemSelectedListener;
       
    49 import android.widget.ArrayAdapter;
       
    50 import android.widget.ImageButton;
       
    51 import android.widget.ImageView;
       
    52 import android.widget.Spinner;
       
    53 import android.widget.Toast;
       
    54 
       
    55 public class StartGameActivity extends Activity {
       
    56 	public static final int ACTIVITY_TEAM_SELECTOR = 0;
       
    57 	
       
    58 	private ImageButton start, back, team;
       
    59 	private Spinner mapSpinner, styleSpinner, schemeSpinner, weaponsetSpinner, themeSpinner;
       
    60 	private ImageView themeIcon, mapPreview, teamCount;
       
    61 
       
    62 	private List<MapFile> mapFiles;
       
    63 	private List<String> styles;
       
    64 	private List<Scheme> schemes;
       
    65 	private List<Weaponset> weaponsets;
       
    66 	private List<String> themes;
       
    67 	
       
    68 	private List<TeamInGame> teams = new ArrayList<TeamInGame>();
       
    69 
       
    70 	public void onCreate(Bundle savedInstanceState){
       
    71 		super.onCreate(savedInstanceState);
       
    72 
       
    73 		setContentView(R.layout.starting_game);
       
    74 
       
    75 		back = (ImageButton) findViewById(R.id.btnBack);
       
    76 		team = (ImageButton) findViewById(R.id.btnTeams);
       
    77 		start = (ImageButton) findViewById(R.id.btnStart);
       
    78 
       
    79 		themeIcon = (ImageView) findViewById(R.id.imgTheme);
       
    80 		mapPreview = (ImageView) findViewById(R.id.mapPreview);
       
    81 		teamCount = (ImageView) findViewById(R.id.imgTeamsCount);
       
    82 
       
    83 		start.setOnClickListener(startClicker);
       
    84 		back.setOnClickListener(backClicker);
       
    85 		team.setOnClickListener(teamClicker);
       
    86 
       
    87 		try {
       
    88 			mapFiles = FrontendDataUtils.getMaps(this);
       
    89 			styles = FrontendDataUtils.getGameStyles(this);
       
    90 			schemes = Schemes.loadAllSchemes(this);
       
    91 			weaponsets = Weaponsets.loadAllWeaponsets(this);
       
    92 			themes = FrontendDataUtils.getThemes(this);
       
    93 		} catch (IOException e) {
       
    94 			Toast.makeText(getApplicationContext(), R.string.error_missing_sdcard_or_files, Toast.LENGTH_LONG).show();
       
    95 			finish();
       
    96 		}
       
    97 		
       
    98 		Collections.sort(mapFiles, MapFile.MISSIONS_FIRST_NAME_ORDER);
       
    99 		Collections.sort(styles, String.CASE_INSENSITIVE_ORDER);
       
   100 		Collections.sort(schemes, Scheme.NAME_ORDER);
       
   101 		Collections.sort(weaponsets, Weaponset.NAME_ORDER);
       
   102 		Collections.sort(themes, String.CASE_INSENSITIVE_ORDER);
       
   103 		
       
   104 		List<String> mapNames = MapFile.toDisplayNameList(mapFiles, getResources());
       
   105 		List<String> schemeNames = Schemes.toNameList(schemes);
       
   106 		List<String> weaponsetNames = Weaponsets.toNameList(weaponsets);
       
   107 		View rootView = findViewById(android.R.id.content);
       
   108 		mapSpinner = prepareSpinner(rootView, R.id.spinMaps, mapNames, mapsClicker);
       
   109 		styleSpinner = prepareSpinner(rootView, R.id.spinGameplay, styles, null);
       
   110 		schemeSpinner = prepareSpinner(rootView, R.id.spinGamescheme, schemeNames, null);
       
   111 		weaponsetSpinner = prepareSpinner(rootView, R.id.spinweapons, weaponsetNames, null);
       
   112 		themeSpinner = prepareSpinner(rootView, R.id.spinTheme, themes, themesClicker);
       
   113 		
       
   114 		// set map to first nonmission
       
   115 		for(int i = 0; i < mapFiles.size(); i++){
       
   116 			if(!mapFiles.get(i).isMission){
       
   117 				mapSpinner.setSelection(i, false);
       
   118 				break;
       
   119 			}
       
   120 		}
       
   121 		styleSpinner.setSelection(styles.indexOf(GameConfig.DEFAULT_STYLE), false);
       
   122 		schemeSpinner.setSelection(schemeNames.indexOf(GameConfig.DEFAULT_SCHEME), false);
       
   123 		weaponsetSpinner.setSelection(weaponsetNames.indexOf(GameConfig.DEFAULT_WEAPONSET), false);
       
   124 		themeSpinner.setSelection(themes.indexOf(GameConfig.DEFAULT_THEME), false);
       
   125 	}
       
   126 
       
   127 	private static Spinner prepareSpinner(View v, int id, List<String> items, OnItemSelectedListener itemSelectedListener) {
       
   128 		Spinner spinner = (Spinner)v.findViewById(id);
       
   129 		ArrayAdapter<String> adapter = new ArrayAdapter<String>(v.getContext(), R.layout.listview_item, items);
       
   130 		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       
   131 		spinner.setAdapter(adapter);
       
   132 		spinner.setOnItemSelectedListener(itemSelectedListener);
       
   133 		return spinner;
       
   134 	}
       
   135 	
       
   136 	private void startTeamsActivity(){
       
   137 		Intent i = new Intent(StartGameActivity.this, TeamSelectionActivity.class);
       
   138 		TeamSelectionActivity.activityParams = new ArrayList<TeamInGame>(teams);
       
   139 		startActivityForResult(i, ACTIVITY_TEAM_SELECTOR);
       
   140 	}
       
   141 
       
   142 	public void onActivityResult(int requestCode, int resultCode, Intent data){
       
   143 		switch(requestCode){
       
   144 		case ACTIVITY_TEAM_SELECTOR:
       
   145 			if(resultCode == Activity.RESULT_OK){
       
   146 				teams = new ArrayList<TeamInGame>(TeamSelectionActivity.activityReturn);
       
   147 				TeamSelectionActivity.activityReturn = Collections.emptyList();
       
   148 				teamCount.getDrawable().setLevel(teams.size());
       
   149 			}
       
   150 			break;
       
   151 		}
       
   152 	}
       
   153 
       
   154 
       
   155 	private OnItemSelectedListener themesClicker = new OnItemSelectedListener(){
       
   156 
       
   157 		public void onItemSelected(AdapterView<?> arg0, View view, int position, long rowId) {
       
   158 			String themeName = themes.get(position);
       
   159 			Drawable themeIconDrawable = Drawable.createFromPath(FileUtils.getDataPath(StartGameActivity.this) + "Themes/" + themeName + "/icon@2X.png");
       
   160 			themeIcon.setImageDrawable(themeIconDrawable);
       
   161 		}
       
   162 
       
   163 		public void onNothingSelected(AdapterView<?> arg0) {
       
   164 		}
       
   165 
       
   166 	};
       
   167 
       
   168 	private OnItemSelectedListener mapsClicker = new OnItemSelectedListener(){
       
   169 
       
   170 		public void onItemSelected(AdapterView<?> arg0, View view, int position,long rowId) {
       
   171 			MapFile map = mapFiles.get(position);
       
   172 			try {
       
   173 				File previewFile = map.getPreviewFile(getApplicationContext());
       
   174 				mapPreview.setImageDrawable(Drawable.createFromPath(previewFile.getAbsolutePath()));
       
   175 			} catch (FileNotFoundException e) {
       
   176 				mapPreview.setImageDrawable(null);
       
   177 			}
       
   178 		}
       
   179 
       
   180 		public void onNothingSelected(AdapterView<?> arg0) {
       
   181 		}
       
   182 
       
   183 	};
       
   184 
       
   185 	private OnClickListener startClicker = new OnClickListener(){
       
   186 		public void onClick(View v) {
       
   187 			if(teams.size() < 2) {
       
   188 				Toast.makeText(getApplicationContext(), R.string.not_enough_teams, Toast.LENGTH_LONG).show();
       
   189 				startTeamsActivity();
       
   190 			} else {
       
   191 				String style = styles.get(styleSpinner.getSelectedItemPosition());
       
   192 				Scheme scheme = schemes.get(schemeSpinner.getSelectedItemPosition());
       
   193 				String mapName = mapFiles.get(mapSpinner.getSelectedItemPosition()).name;
       
   194 				String theme = themes.get(themeSpinner.getSelectedItemPosition());
       
   195 				MapRecipe map = MapRecipe.makeMap(mapName, UUID.randomUUID().toString(), theme);
       
   196 				Weaponset weaponset = weaponsets.get(weaponsetSpinner.getSelectedItemPosition());
       
   197 				SDLActivity.startConfig = new GameConfig(style, scheme, map, teams, weaponset);
       
   198 				SDLActivity.startNetgame = false;
       
   199 				Intent i = new Intent(StartGameActivity.this, SDLActivity.class);
       
   200 				startActivity(i);
       
   201 			}
       
   202 		}
       
   203 	};
       
   204 
       
   205 	private OnClickListener backClicker = new OnClickListener(){
       
   206 		public void onClick(View v) {
       
   207 			finish();
       
   208 		}
       
   209 	};
       
   210 
       
   211 	private OnClickListener teamClicker = new OnClickListener(){
       
   212 		public void onClick(View v) {
       
   213 			startTeamsActivity();
       
   214 		}
       
   215 	};
       
   216 
       
   217 }