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

http://thelements.googlecode.com/ · Java · 222 lines · 176 code · 26 blank · 20 comment · 12 complexity · 47a25946d18c0e57595df5e1654d5160 MD5 · raw file

  1. package sand.falling.opengl;
  2. import android.content.Context;
  3. import android.util.AttributeSet;
  4. import android.view.Gravity;
  5. import android.view.View;
  6. import android.widget.ImageButton;
  7. import android.widget.LinearLayout;
  8. import android.widget.Toast;
  9. public class MenuBar extends LinearLayout
  10. {
  11. //Used when exit is called because we need the specific instance of the activity to end
  12. private MainActivity activity;
  13. private Context context;
  14. static ImageButton eraser_button;
  15. static ImageButton play_pause_button;
  16. private ImageButton save_button;
  17. private ImageButton load_button;
  18. private ImageButton load_demo_button;
  19. private ImageButton exit_button;
  20. //Used for eraser
  21. static boolean eraser_on = false;
  22. private static int temp_element = 0;
  23. //Used for play/pause
  24. static boolean play = true;
  25. //Constructor
  26. public MenuBar(Context context, AttributeSet attrs)
  27. {
  28. super(context, attrs);
  29. this.context = context;
  30. this.setGravity(Gravity.CENTER_HORIZONTAL);
  31. }
  32. //Used to get specific instance of activity
  33. public void setActivity(MainActivity act)
  34. {
  35. activity = act;
  36. }
  37. //Testing
  38. public void hi()
  39. {}
  40. public static void seteraseroff()
  41. {
  42. eraser_button.setImageResource(R.drawable.eraser);
  43. eraser_on = false;
  44. MainActivity.setElement(temp_element);
  45. }
  46. //Called when it's finished inflating the XML layout
  47. @Override
  48. protected void onFinishInflate()
  49. {
  50. //Set up all the variables for the objects
  51. eraser_button = (ImageButton) findViewById(R.id.eraser_button);
  52. play_pause_button = (ImageButton) findViewById(R.id.play_pause_button);
  53. save_button = (ImageButton) findViewById(R.id.save_button);
  54. load_button = (ImageButton) findViewById(R.id.load_button);
  55. load_demo_button = (ImageButton) findViewById(R.id.load_demo_button);
  56. exit_button = (ImageButton) findViewById(R.id.exit_button);
  57. //Set up the OnClickListener for the eraser button
  58. eraser_button.setOnClickListener
  59. (
  60. new OnClickListener()
  61. {
  62. @Override
  63. public void onClick(View v)
  64. {
  65. //If it was on eraser, swap back to regular element
  66. if(eraser_on)
  67. {
  68. eraser_on = false;
  69. MainActivity.setElement(temp_element);
  70. //Change the button to look unclicked
  71. eraser_button.setImageResource(R.drawable.eraser);
  72. }
  73. //If it is on a normal element, go to eraser and store that element for later
  74. else
  75. {
  76. eraser_on = true;
  77. temp_element = MainActivity.getElement();
  78. MainActivity.setElement(3);
  79. //Change the button to look clicked
  80. eraser_button.setImageResource(R.drawable.eraser_on);
  81. }
  82. }
  83. }
  84. );
  85. if(MainActivity.getElement() == 3) //If the current element is eraser
  86. {
  87. //Start off the button to being on
  88. eraser_button.setImageResource(R.drawable.eraser_on);
  89. }
  90. else
  91. {
  92. //Start off the eraser in "off" position
  93. eraser_button.setImageResource(R.drawable.eraser);
  94. }
  95. //Set up the OnClickListener for the play/pause button
  96. play_pause_button.setOnClickListener
  97. (
  98. new OnClickListener()
  99. {
  100. @Override
  101. public void onClick(View v)
  102. {
  103. if(play)
  104. {
  105. play = false;
  106. play_pause_button.setImageResource(R.drawable.play);
  107. MainActivity.pause();
  108. }
  109. else
  110. {
  111. play = true;
  112. play_pause_button.setImageResource(R.drawable.pause);
  113. MainActivity.play();
  114. }
  115. }
  116. }
  117. );
  118. if(MainActivity.getPlayState() == 1)
  119. {
  120. play_pause_button.setImageResource(R.drawable.pause);
  121. }
  122. else
  123. {
  124. play_pause_button.setImageResource(R.drawable.play);
  125. }
  126. //Set up the OnClickListener for the save button
  127. save_button.setOnClickListener
  128. (
  129. new OnClickListener()
  130. {
  131. @Override
  132. public void onClick(View v)
  133. {
  134. if (MainActivity.save() == 1)
  135. {
  136. Toast.makeText(context, "File Saved", Toast.LENGTH_SHORT).show();
  137. }
  138. else
  139. {
  140. Toast.makeText(context, "No SDcard", Toast.LENGTH_LONG).show();
  141. }
  142. }
  143. }
  144. );
  145. //Set up the OnClickListener for the load button
  146. load_button.setOnClickListener
  147. (
  148. new OnClickListener()
  149. {
  150. @Override
  151. public void onClick(View v)
  152. {
  153. if (MainActivity.load() == 1)
  154. {
  155. Toast.makeText(context, "File Loaded", Toast.LENGTH_SHORT).show();
  156. }
  157. else
  158. {
  159. Toast.makeText(context, "No Save File or SDcard", Toast.LENGTH_LONG).show();
  160. }
  161. }
  162. }
  163. );
  164. //Set up the OnClickListener for the load demo button
  165. load_demo_button.setOnClickListener
  166. (
  167. new OnClickListener()
  168. {
  169. @Override
  170. public void onClick(View v)
  171. {
  172. if (MainActivity.loadDemo() == 1)
  173. {
  174. Toast.makeText(context, "Demo Loaded", Toast.LENGTH_SHORT).show();
  175. }
  176. else
  177. {
  178. Toast.makeText(context, "No Demo File or SDcard", Toast.LENGTH_LONG).show();
  179. }
  180. }
  181. }
  182. );
  183. //Set up the OnClickListener for the exit button
  184. exit_button.setOnClickListener
  185. (
  186. new OnClickListener()
  187. {
  188. @Override
  189. public void onClick(View v)
  190. {
  191. activity.finish();
  192. }
  193. }
  194. );
  195. }
  196. }