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