PageRenderTime 83ms CodeModel.GetById 41ms app.highlight 13ms RepoModel.GetById 27ms app.codeStats 0ms

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