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

http://thelements.googlecode.com/ · Java · 74 lines · 53 code · 9 blank · 12 comment · 0 complexity · b2814dc89c530ce6d5faa284fa824210 MD5 · raw file

  1. package sand.falling.opengl;
  2. import android.content.Context;
  3. import android.util.AttributeSet;
  4. import android.view.View;
  5. import android.widget.ImageButton;
  6. import android.widget.LinearLayout;
  7. import android.widget.SeekBar;
  8. public class Control extends LinearLayout
  9. {
  10. //The instance of the current activity is stored here and modified through setActvity (call from DemoActivity)
  11. private MainActivity activity;
  12. //Two objects in the control area
  13. private ImageButton control_button;
  14. private SeekBar brush_size_slider;
  15. final CharSequence[] elementslist = {"Sand", "Water", "Plant", "Wall", "Fire", "Ice", "Generator", "Oil", "Magma", "Stone", "C4"};
  16. //Constructor
  17. public Control(Context context, AttributeSet attrs)
  18. {
  19. super(context, attrs);
  20. }
  21. //Sets the current instance of the activity
  22. public void setActivity(MainActivity act)
  23. {
  24. activity = act;
  25. }
  26. //Called once the the xml is finished inflating
  27. @Override
  28. protected void onFinishInflate()
  29. {
  30. //Define the ImageButton and SeekBar set before using the res ids
  31. control_button = (ImageButton) findViewById(R.id.element_picker_button);
  32. brush_size_slider = (SeekBar) findViewById(R.id.brush_size_slider);
  33. //Set a click listener for the button which should pop up element picker dialog when clicked
  34. control_button.setOnClickListener
  35. (
  36. new OnClickListener()
  37. {
  38. public void onClick(View v)
  39. {
  40. activity.showDialog(2); //Run the element picker dialog
  41. }
  42. }
  43. );
  44. //Set a palette image for the button
  45. control_button.setImageResource(R.drawable.palette);
  46. //Set a change listener for the seekbar
  47. brush_size_slider.setOnSeekBarChangeListener
  48. (
  49. new SeekBar.OnSeekBarChangeListener()
  50. {
  51. public void onProgressChanged(SeekBar seekbar, int progress, boolean fromTouch)
  52. {
  53. //When it is dragged, set the brush size to 32 * the fraction of the bar dragged
  54. int p = 32 * progress/seekbar.getMax();
  55. MainActivity.setBrushSize(p);
  56. }
  57. //These aren't needed for now
  58. public void onStartTrackingTouch(SeekBar seekbar) {}
  59. public void onStopTrackingTouch(SeekBar seekbar) {}
  60. }
  61. );
  62. //Start off the progress bar at a brush size of 4
  63. brush_size_slider.setProgress((int)4*brush_size_slider.getMax()/32);
  64. }
  65. }