PageRenderTime 126ms CodeModel.GetById 40ms app.highlight 46ms RepoModel.GetById 34ms app.codeStats 0ms

/TheElements/src/sand/falling/opengl/MainActivity.java

http://thelements.googlecode.com/
Java | 505 lines | 412 code | 58 blank | 35 comment | 93 complexity | 63aaab1b36de469ac0dece99845910dd MD5 | raw file
  1/*
  2 * Element Works: Copyright (C) 2010 IDKJava
  3 * ----------------------------------------------------------
  4 * A sandbox type game in which you can play with different elements
  5 * which interact with each other in unique ways.
  6 */
  7
  8package sand.falling.opengl;
  9
 10import java.io.File;
 11import java.io.FileNotFoundException;
 12import java.io.FileOutputStream;
 13import java.io.IOException;
 14import java.io.InputStream;
 15import java.io.OutputStream;
 16import java.util.List;
 17
 18import sand.falling.opengl.preferences.PreferencesFromCode;
 19import sand.falling.opengl.custom.CustomMaker;
 20
 21import android.app.Activity;
 22import android.app.AlertDialog;
 23import android.app.Dialog;
 24import android.content.Context;
 25import android.content.DialogInterface;
 26import android.content.Intent;
 27import android.content.SharedPreferences;
 28import android.hardware.Sensor;
 29import android.hardware.SensorEvent;
 30import android.hardware.SensorEventListener;
 31import android.hardware.SensorManager;
 32import android.os.Bundle; //import android.util.Log;
 33import android.view.Menu;
 34import android.view.MenuInflater;
 35import android.view.MenuItem;
 36import android.view.Window;
 37
 38public class MainActivity extends Activity
 39{
 40	public static final CharSequence[] elementslist = {"Sand", "Water", "Plant", "Wall", "Fire", "Ice", "Generator", "Oil", "Magma", "Stone", "C4", "C4++", "Fuse", "Destructible Wall", "Drag", "Acid", "Steam", "Salt", "Salt Water", "Glass", "Custom Element", "Mud"};
 41	static final CharSequence[] brushlist = {"1", "2", "4", "8", "16", "32"};
 42
 43	static final int maxy = 414; // 454 for g1, 815 for droid
 44	static final int maxx = 319; // 319 for g1, 479 for droid
 45	static public boolean play = true;
 46	static public int speed = 1;
 47	static public int skip = 1;
 48	static public int size = 0;
 49
 50	private SensorManager mSensorManager;
 51
 52	public static final String PREFS_NAME = "MyPrefsfile";
 53	public static boolean loaddemov = false;
 54
 55	public static boolean ui;
 56	public static boolean layout_ui;
 57
 58	public static MenuBar menu_bar;
 59	public static Control control;
 60	public static SandView sand_view;
 61
 62	private SensorManager myManager;
 63	private List<Sensor> sensors;
 64	private Sensor accSensor;
 65
 66	@Override
 67	protected void onCreate(Bundle savedInstanceState)
 68	{
 69		super.onCreate(savedInstanceState); //Uses onCreate from the general Activity
 70
 71		requestWindowFeature(Window.FEATURE_NO_TITLE); //Get rid of title bar
 72
 73		PreferencesFromCode.setpreferences(this);
 74
 75		//Set Sensor + Manager
 76		myManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
 77		accSensor = myManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
 78
 79		//Set the layout based on the settings
 80		if (ui)
 81		{
 82			setContentView(R.layout.ui);
 83		}
 84		else
 85		{
 86			setContentView(R.layout.non_ui);
 87		}
 88
 89		//Sync on startup (layout_ui is not changed on preference changed for smoothness)
 90		layout_ui = ui;
 91
 92		//Need to do this otherwise it gives a nullpointerexception
 93		menu_bar = new MenuBar(this, null);
 94		sand_view = new SandView(this, null);
 95		control = new Control(this, null);
 96
 97		//Set the new view and control box and menu bar to the stuff defined in layout
 98		menu_bar = (MenuBar) findViewById(R.id.menu_bar);
 99		sand_view = (SandView) findViewById(R.id.sand_view);
100		control = (Control) findViewById(R.id.control);
101
102		PreferencesFromCode.setScreenOnOff(this); //Finds out to keep screen on or off
103
104		CustomMaker.loadCustom(); //Load the custom elements
105	}
106
107	private final SensorEventListener mySensorListener = new SensorEventListener()
108	{
109		public void onSensorChanged(SensorEvent event)
110		{
111			sendXGrav(event.values[0]);
112			sendYGrav(event.values[1]);
113		}
114
115		public void onAccuracyChanged(Sensor sensor, int accuracy)
116		{
117		}
118	};
119
120	@Override
121	protected void onPause()
122	{
123		//Quicksave
124		quickSave();
125		//Set the preferences to indicate paused
126		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
127		SharedPreferences.Editor editor = settings.edit();
128		editor.putBoolean("paused", true);
129		editor.commit();
130		//Use the normal onPause
131		super.onPause();
132		//Call onPause for the view
133		sand_view.onPause();
134	}
135
136	@Override
137	protected void onResume()
138	{
139		if (layout_ui != ui)
140		{
141			System.exit(0);
142		}
143		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
144		myManager.registerListener(mySensorListener, accSensor, SensorManager.SENSOR_DELAY_GAME);
145
146		//If we're resuming from a pause (not when it starts)
147		if (settings.getBoolean("paused", true))
148		{
149			//Load the save
150			quickLoad();
151			//Set the preferences to indicate unpaused
152			SharedPreferences.Editor editor = settings.edit();
153			editor.putBoolean("paused", false);
154			editor.commit();
155		}
156
157		//Set firstrun
158		boolean firstrun = settings.getBoolean("firstrun", true);
159		
160		//Input the correct save file (based on screen width)
161		InputStream in;
162		if (sand_view.getWidth() < 300)
163		{
164			in = getResources().openRawResource(R.raw.save);
165		}
166		else
167		{
168			in = getResources().openRawResource(R.raw.droiddemo);
169		}
170
171		try
172		{
173			//Try to create the folder
174			(new File("/sdcard/elementworks/")).mkdir();
175
176			//Try to copy the demo file to the save file
177			OutputStream out = new FileOutputStream("/sdcard/elementworks/save2.txt");
178			byte[] buf = new byte[100024];
179			int len;
180			while ((len = in.read(buf)) != -1)
181			{
182				out.write(buf, 0, len);
183			}
184			in.close();
185			out.close();
186		}
187		catch (FileNotFoundException e)
188		{
189			//FileNotFoundException is normal, ignore it
190			e.printStackTrace();
191		}
192		catch (IOException e)
193		{
194			//IOException is also fine, ignore
195			e.printStackTrace();
196		}
197		
198		//If it's the first run, tell it to load the demo and unset firstrun
199		if (firstrun == true)
200		{
201			loaddemov = true;
202			SharedPreferences.Editor editor = settings.edit();
203			editor.putBoolean("firstrun", false);
204			editor.commit();
205
206			showDialog(1); //Pop up intro message
207		}
208
209		if (layout_ui)
210		{
211			// This is where I set the activity for Control so that I can call showDialog() from it
212			control.setActivity(this);
213			//Set instance of activity for MenuBar also
214			menu_bar.setActivity(this);
215		}
216		
217		//Use the super onResume as well
218		super.onResume();
219		//Call onResume() for view too
220		sand_view.onResume();
221	}
222
223	protected Dialog onCreateDialog(int id) //This is called when showDialog is called
224	{
225		if (id == 1) // The first dialog - the intro message
226		{
227			AlertDialog.Builder builder = new AlertDialog.Builder(this);
228			builder.setMessage(R.string.App_Intro).setCancelable(false).setPositiveButton("Exit", new DialogInterface.OnClickListener()
229			{
230				public void onClick(DialogInterface dialog, int id)
231				{
232					MainActivity.this.finish(); // Exit button
233					// closes
234					// program
235				}
236			}).setNegativeButton("Proceed", new DialogInterface.OnClickListener()
237			{
238				public void onClick(DialogInterface dialog, int id)
239				{
240					dialog.cancel(); // Proceed closes the
241					// message
242				}
243			});
244			AlertDialog alert = builder.create(); // Actually create the message
245			return alert; // Return the object created
246		}
247		else if (id == 2) // Element picker
248		{
249 			
250			AlertDialog.Builder builder = new AlertDialog.Builder(this); // Create a new one
251			builder.setTitle("Pick an element"); // Set the title
252			builder.setItems(elementslist, new DialogInterface.OnClickListener() //Create the list
253			{
254				public void onClick(DialogInterface dialog, int item)
255				{
256					if (MenuBar.eraser_on == true)
257					{
258							MenuBar.seteraseroff();
259					}
260
261					if (item == 0) // Sand
262					{
263						setElement(0);
264					}
265					else if (item == 1) // Water
266					{
267						setElement(1);
268					}
269					else if (item == 2) // Plant
270					{
271						setElement(4);
272					}
273					else if (item == 3) // Wall
274					{
275						setElement(2);
276					}
277					else if (item == 4) // Fire
278					{
279						setElement(5);
280					}
281					else if (item == 5) // Ice
282					{
283						setElement(6);
284					}
285					else if (item == 6)// Generator
286					{
287						setElement(7);
288					}
289					else if (item == 7)// Oil
290					{
291						setElement(9);
292					}
293					else if (item == 8)// Magma
294					{
295						setElement(10);
296					}
297					else if (item == 9)// Stone
298					{
299						setElement(11);
300					}
301					else if (item == 10)// C4
302					{
303						setElement(12);
304					}
305					else if (item == 11)// C4++
306					{
307						setElement(13);
308					}
309					else if (item == 12)// Fuse
310					{
311						setElement(14);
312					}
313					else if (item == 13)// Destructible wall
314					{
315						setElement(15);
316					}
317					else if (item == 14)// Drag
318					{
319						setElement(16);
320					}
321					else if (item == 15)// Acid
322					{
323						setElement(17);
324					}
325					else if (item == 16)// Steam
326					{
327						setElement(18);
328					}
329					else if (item == 17)// Salt
330					{
331						setElement(19);
332					}
333					else if (item == 18)// Salt-water
334					{
335						setElement(20);
336					}
337					else if (item == 19)// Glass
338					{
339						setElement(21);
340					}
341					else if (item == 20)// Custom 1
342					{
343						setElement(22);
344					}
345					else if (item == 21)// Mud
346					{
347						setElement(23);
348					}
349					else if (item == 22)// Custom 3
350					{
351						setElement(24);
352					}
353				}
354			});
355			AlertDialog alert = builder.create(); // Create the dialog
356
357			return alert; // Return handle
358		}
359		else if (id == 3)
360		{
361			AlertDialog.Builder builder = new AlertDialog.Builder(this); // Declare the object
362			builder.setTitle("Brush Size Picker");
363			builder.setItems(brushlist, new DialogInterface.OnClickListener()
364			{
365				public void onClick(DialogInterface dialog, int item)
366				{
367					setBrushSize((int) java.lang.Math.pow(2, item));
368				}
369			});
370			AlertDialog alert = builder.create(); // Create object
371			return alert; // Return handle
372		}
373
374		return null; // No need to return anything, just formality
375	}
376
377	public boolean onPrepareOptionsMenu(Menu menu) // Pops up when you press Menu
378	{
379		// Create an inflater to "inflate" the menu already defined in res/menu/options_menu.xml
380		// This seems to be a bit faster at loading the menu, and easier to modify
381		MenuInflater inflater = getMenuInflater();
382		if (layout_ui)
383		{
384			menu.clear();
385			inflater.inflate(R.menu.options_menu_small, menu);
386		}
387		else
388		{
389			menu.clear();
390			inflater.inflate(R.menu.options_menu_large, menu);
391		}
392
393		return true;
394	}
395
396	public boolean onOptionsItemSelected(MenuItem item)
397	{
398		switch (item.getItemId())
399		{
400			case R.id.element_picker:
401	
402				showDialog(2);
403	
404				return true;
405			case R.id.brush_size_picker:
406	
407				showDialog(3);
408	
409				return true;
410			case R.id.clear_screen:
411	
412				setup();
413	
414				return true;
415			case R.id.play_pause:
416	
417				if (play)
418				{
419					pause();
420				}
421				else
422				{
423					play();
424				}
425				play = !play;
426				return true;
427			case R.id.eraser:
428	
429				setElement(3);
430				return true;
431			case R.id.toggle_size:
432	
433				if (size == 1)
434				{
435					size = 0;
436				}
437				else
438				{
439					size = 1;
440				}
441				toggleSize();
442				return true;
443			case R.id.save:
444	
445				save();
446				return true;
447			case R.id.load:
448	
449				load();
450				return true;
451			case R.id.load_demo:
452	
453				loadDemo();
454				return true;
455			case R.id.preferences:
456	
457				startActivity(new Intent(MainActivity.this, PreferencesFromCode.class));
458				return true;
459			case R.id.exit:
460	
461				System.exit(0);
462				return true;
463		}
464		return false;
465	}
466
467	// JNI functions
468	public native static int save();
469	public native static int loadDemo();
470	public native static int load();
471	public native static void setup(); //Set up arrays and such
472	public native static void setFingerState(int fingerstate); //Sets finger up or down, 1 is down
473	public native static void setMouseLocation(int xpos, int ypos); //Sets x mouse and y mouse
474	public native static void tester();
475	public native static void play(); // Jni play
476	public native static void pause(); // Jni pause
477	public native static int  getPlayState(); //Get the play state
478	public native static void toggleSize(); // Jni toggle size
479	public native static void quickSave();
480	public native static void quickLoad();
481	public native static void setBackgroundColor(int colorcode);
482	public native static void setFlip(int flipped);
483	public native static void setElement(int element);
484	public native static void setBrushSize(int jsize);
485	public native static int  getElement();
486	public native static void clearQuickSave();
487	public native static void sendYGrav(float ygrav);
488	public native static void sendXGrav(float xgrav);
489	public native static void setAccelOnOff(int state);
490	public native static void setCollision(int custnumber, int elementnumb, int collisionspot, int collisionnumber);
491	public native static void setExplosiveness(int explosiveness);
492	public native static void setRed(int redness);
493	public native static void setBlue(int blueness);
494	public native static void setGreen(int greenness);
495	public native static void setDensity( int jdensity );
496	public native static void setUsername( char[] username);
497	public native static void setPassword ( char[] password);
498	public native static boolean login();
499	public native static boolean register();
500
501	static
502	{
503		System.loadLibrary("thelements"); // Load the JNI library (libthelements.so)
504	}
505}