PageRenderTime 24ms CodeModel.GetById 11ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/Code/src/com/game/ViewData/OptionsViewData.java

https://bitbucket.org/DeveloperUX/behaviortree
Java | 154 lines | 117 code | 23 blank | 14 comment | 2 complexity | c15b3be908d0c1d580dec3e2177a5f09 MD5 | raw file
  1package com.game.ViewData;
  2
  3
  4import com.game.MessageHandler;
  5import com.game.MsgType;
  6import com.game.Preferences;
  7import com.game.R;
  8import com.game.MessageHandler.MsgReceiver;
  9
 10import android.app.Activity;
 11import android.content.Context;
 12import android.util.Log;
 13import android.view.Display;
 14import android.view.Gravity;
 15import android.view.LayoutInflater;
 16import android.view.View;
 17import android.view.WindowManager;
 18import android.view.View.OnClickListener;
 19import android.widget.AdapterView;
 20import android.widget.ArrayAdapter;
 21import android.widget.Button;
 22import android.widget.CheckBox;
 23import android.widget.FrameLayout;
 24import android.widget.LinearLayout;
 25import android.widget.Spinner;
 26import android.widget.AdapterView.OnItemSelectedListener;
 27
 28/**
 29 * Specific view data class for the "Options" screen.
 30 * @author Ying
 31 *
 32 */
 33public class OptionsViewData extends ViewData 
 34{
 35	// Used to access data inside the OnClickListener classes
 36	private View auxView = null;
 37
 38	/**
 39	 * @see ViewData createXMLView(Activity activity) 
 40	 */
 41	@Override
 42	public View createXMLView(Activity activity) 
 43	{
 44		Log.i("OptionsViewData", "createXMLView");
 45		
 46		// Access xml layout
 47		LayoutInflater li = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 48        View xmlLayout = (View) li.inflate(R.layout.options, null);
 49
 50        // Center in screen at 80% width
 51        LinearLayout centerLinerarLayout = (LinearLayout)xmlLayout.findViewById(R.id.options_layout_inner_scroll);
 52        this.Set80PercentWidth(activity, centerLinerarLayout);
 53
 54        // Callback for the button      
 55        Button okButton = (Button) xmlLayout.findViewById(R.id.ok_options_but);
 56        okButton.setOnClickListener(new OnClickListener() 
 57        {
 58          @Override
 59          public void onClick(View v) {
 60            Log.i("ViewData", " Clicked OK button");
 61            MessageHandler.Get().Send(MsgReceiver.LOGIC, MsgType.BUTTON_CLICK, R.id.ok_options_but);
 62          }
 63        });
 64        
 65        // Callback for the spinners
 66        Spinner numberUnitsSpin = (Spinner) xmlLayout.findViewById(R.id.num_units_spin);
 67        ArrayAdapter numberUnitsAdapter = ArrayAdapter.createFromResource(	activity, R.array.count_names, android.R.layout.simple_spinner_item);
 68        numberUnitsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 69        numberUnitsSpin.setAdapter(numberUnitsAdapter);
 70        numberUnitsSpin.setOnItemSelectedListener(new OnItemSelectedListener()
 71        {
 72        	public void onItemSelected(AdapterView<?> parent,
 73        			View view, int position, long id) 
 74        	{
 75        		Log.i("OptionsViewData", "Selected number units spinner item");
 76        		MessageHandler.Get().Send(MsgReceiver.LOGIC, MsgType.SPINNER_ITEM_CLICK, R.id.num_units_spin, position);
 77        	}
 78
 79        	public void onNothingSelected(AdapterView<?> parent) {
 80        		Log.i("OptionsViewData", "No number units spinner item has been selected");
 81        	}
 82
 83        });
 84        numberUnitsSpin.setSelection(Preferences.Get().optionsUnitCuantity);
 85        
 86        Spinner moveSpeedSpin = (Spinner) xmlLayout.findViewById(R.id.move_speed_spin);
 87        ArrayAdapter moveSpeedAdapter = ArrayAdapter.createFromResource(	activity, R.array.speed_names, android.R.layout.simple_spinner_item);
 88        moveSpeedAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 89        moveSpeedSpin.setAdapter(moveSpeedAdapter);
 90        moveSpeedSpin.setOnItemSelectedListener(new OnItemSelectedListener()
 91        {
 92        	public void onItemSelected(AdapterView<?> parent,
 93        			View view, int position, long id) 
 94        	{
 95        		Log.i("OptionsViewData", "Selected move speed spinner item");
 96        		MessageHandler.Get().Send(MsgReceiver.LOGIC, MsgType.SPINNER_ITEM_CLICK, R.id.move_speed_spin, position);
 97        	}
 98
 99        	public void onNothingSelected(AdapterView<?> parent) {
100        		Log.i("OptionsViewData", "No eat speed spinner item has been selected");
101        	}
102
103        });
104        moveSpeedSpin.setSelection(Preferences.Get().optionsUnitMoveSpeed);
105        
106        Spinner eatSpeedSpin = (Spinner) xmlLayout.findViewById(R.id.eat_speed_spin);
107        ArrayAdapter eatSpeedAdapter = ArrayAdapter.createFromResource(	activity, R.array.speed_names, android.R.layout.simple_spinner_item);
108        eatSpeedAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
109        eatSpeedSpin.setAdapter(eatSpeedAdapter);
110        eatSpeedSpin.setOnItemSelectedListener(new OnItemSelectedListener()
111        {
112        	public void onItemSelected(AdapterView<?> parent,
113        			View view, int position, long id) 
114        	{
115        		Log.i("OptionsViewData", "Selected eat speed spinner item");
116        		MessageHandler.Get().Send(MsgReceiver.LOGIC, MsgType.SPINNER_ITEM_CLICK, R.id.eat_speed_spin, position);
117        	}
118
119        	public void onNothingSelected(AdapterView<?> parent) {
120        		Log.i("OptionsViewData", "No eat speed spinner item has been selected");
121        	}
122
123        });
124        eatSpeedSpin.setSelection(Preferences.Get().optionsUnitEatSpeed);
125        
126        // Callback for the checkboxes
127        CheckBox soundCheckBox = (CheckBox) xmlLayout.findViewById(R.id.sound_mute);
128        soundCheckBox.setChecked(Preferences.Get().optionsSoundMute);
129        
130        this.auxView = soundCheckBox;
131        soundCheckBox.setOnClickListener(new OnClickListener() 
132        {			
133			@Override
134			public void onClick(View v) {
135				Log.i("OptionsViewData", "Clicked mute");				
136				
137				int checked = 0;
138				if(((CheckBox) OptionsViewData.this.auxView).isChecked() == true)
139				{
140					checked = 1;
141				}
142				else
143				{
144					checked = 0;
145				}
146				
147				MessageHandler.Get().Send(MsgReceiver.LOGIC, MsgType.CHECKBOX_CLICK, R.id.sound_mute, checked);	
148			}
149		});
150        
151		return xmlLayout;
152	}
153
154}