Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- This is the working code for a container supporting event separation
- public class Container extends LinearLayout
- {
- LinkedHashMap<Integer,View> pointers = new LinkedHashMap<Integer,View>();
- ArrayList<View> views = new ArrayList<View>();
- public Container(Context context) {
- super(context);
- initialize(context);
- }
- public Container(Context context, AttributeSet attrs) {
- super(context, attrs);
- initialize(context);
- }
- private void initialize(Context context)
- {
- }
- @Override
- public void onLayout(boolean changed, int l, int t, int r, int b)
- {
- super.onLayout(changed, l, t, r, b);
- views = LayoutUtil.flattenLayout(this,false);
- Log.v("Container","Child Count " + views.size());
- for(View foo : views)
- {
- Rect rect = new Rect();
- foo.getGlobalVisibleRect(rect);
- Log.i("Container",foo + "- Top: " + rect.top + " Bottom: " + rect.bottom + " Left: " + rect.left+ " Right: " + rect.right);
- Log.v("Container",foo + "- Top: " + foo.getTop() + " Bottom: " + foo.getBottom() + " Left: " + foo.getLeft() + " Right: " + foo.getRight());
- }
- }
- ArrayList<MotionEvent> flattenEvent(MotionEvent event)
- {
- ArrayList<MotionEvent> l= new ArrayList<MotionEvent>();
- if(event.getPointerCount()==1)
- {
- l.add(event);
- return l;
- }
- else
- for(int i = 0; i < event.getPointerCount();i++)
- {
- MotionEvent e = MotionEvent.obtain(event);
- e.setLocation(event.getX(i), event.getY(i));
- l.add(e);
- }
- return l;
- }
- @Override
- public boolean onInterceptTouchEvent(MotionEvent event)
- {
- return true;
- }
- @Override
- public boolean onTouchEvent(MotionEvent event)
- {
- // dumpEvent(event);
- int action = event.getAction() & MotionEvent.ACTION_MASK;
- if(action==MotionEvent.ACTION_DOWN)
- {
- for(View v: views)
- {
- Rect r = new Rect();
- v.getGlobalVisibleRect(r);
- if (event.getX() > r.left && event.getX() < r.right
- && event.getY() > r.top
- && event.getY() < r.bottom) {
- pointers.put(event.getPointerId(0),v);
- pointers.get(event.getPointerId(0)).onTouchEvent(event);
- break;
- }
- }
- }
- if(action==MotionEvent.ACTION_POINTER_DOWN)
- {
- int pid = event.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT;
- int index = event.findPointerIndex(pid);
- for(View v: views)
- {
- Rect r = new Rect();
- v.getGlobalVisibleRect(r);
- if (event.getX(index) > r.left
- && event.getX(index) < r.right
- && event.getY(index) > r.top
- && event.getY(index) < r.bottom) {
- pointers.put(pid,v);
- MotionEvent copy = MotionEvent.obtain(event);
- copy.setAction(MotionEvent.ACTION_DOWN);
- copy.setLocation(event.getX(index), event.getY(index));
- pointers.get(pid).onTouchEvent(copy);
- }
- }
- }
- if(action==MotionEvent.ACTION_POINTER_UP)
- {
- int pid = event.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT;
- int index = event.findPointerIndex(pid);
- if(pointers.get(pid)!=null) // If the touch was outside any view
- {
- MotionEvent copy = MotionEvent.obtain(event);
- copy.setAction(MotionEvent.ACTION_UP);
- pointers.get(pid).onTouchEvent(copy);
- pointers.remove(pid);
- }
- }
- if(action==MotionEvent.ACTION_MOVE)
- {
- for(int i = 0; i<event.getPointerCount();i++)
- {
- int pid = event.getPointerId(i);
- MotionEvent copy = MotionEvent.obtain(event);
- copy.setLocation(event.getX(i), event.getY(i));
- if(pointers.get(pid)==null) continue; // If the touch was outside any view
- pointers.get(pid).onTouchEvent(copy);
- }
- }
- if(action==MotionEvent.ACTION_UP)
- {
- if(pointers.get(event.getPointerId(0))!=null)
- {
- pointers.get(event.getPointerId(0)).onTouchEvent(event);
- pointers.remove(event.getPointerId(0));
- }
- }
- return true;
- }
- private void dumpEvent(MotionEvent event) {
- String names[] = { "DOWN" , "UP" , "MOVE" , "CANCEL" , "OUTSIDE" ,
- "POINTER_DOWN" , "POINTER_UP" , "7?" , "8?" , "9?" };
- StringBuilder sb = new StringBuilder();
- int action = event.getAction();
- int actionCode = action & MotionEvent.ACTION_MASK;
- sb.append("event ACTION_" ).append(names[actionCode]);
- if (actionCode == MotionEvent.ACTION_POINTER_DOWN
- || actionCode == MotionEvent.ACTION_POINTER_UP) {
- sb.append("(pid " ).append(
- action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
- sb.append(")" );
- }
- sb.append("[" );
- for (int i = 0; i < event.getPointerCount(); i++) {
- sb.append("#" ).append(i);
- sb.append("(pid " ).append(event.getPointerId(i));
- sb.append(")=" ).append((int) event.getX(i));
- sb.append("," ).append((int) event.getY(i));
- if (i + 1 < event.getPointerCount())
- sb.append(";" );
- }
- sb.append("]" );
- Log.d("touchy", sb.toString());
- }
- }
- Code for the extension of the Container generic class
- package pt.up.fe.dceg.accu.components.controlpad;
- import java.util.ArrayList;
- import java.util.LinkedHashMap;
- import pt.up.fe.dceg.accu.components.interfaces.JoystickPadChangeListener;
- import pt.up.fe.dceg.accu.components.interfaces.PadEventListener;
- import pt.up.fe.dceg.accu.components.interfaces.PadStateListener;
- import pt.up.fe.dceg.accu.util.Container;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.View.OnTouchListener;
- import android.view.ViewGroup;
- public class ControlPad2 extends Container
- implements JoystickPadChangeListener, OnTouchListener
- {
- public static final String TAG = "ControlPad2";
- Context context;
- View view;
- ArrayList<View> viewList = new ArrayList<View>(); // List of pad components
- LinkedHashMap<View,String> actions = new LinkedHashMap<View,String>();
- PadStateListener listener;
- PadEventListener listener2;
- int layoutID;
- public ControlPad2(Context context) {
- super(context);
- initialize(context);
- }
- public ControlPad2(Context context, AttributeSet attrs) {
- super(context, attrs);
- layoutID = attrs.getAttributeResourceValue(null, "pad_layout", 0);
- initialize(context);
- }
- void initialize(Context context)
- {
- this.context = context;
- LayoutInflater.from(context).inflate(layoutID,this,true);
- viewList = flattenLayout(this);
- Log.i(TAG,""+viewList.size());
- Log.v("ControlPad",viewList.size()+"");
- setupPad();
- }
- /**
- * Registers Layout components as pad components in the padState and the listeners too
- */
- private void setupPad()
- {
- for(View v : viewList)
- {
- if(v instanceof JoystickPad)
- {
- ((JoystickPad)v).setOnPadChangeListener(this);
- }
- else if(v instanceof PadButton)
- {
- ((PadButton)v).setOnTouchListener(this);
- }
- }
- }
- public void setPadLayout(int layoutId)
- {
- this.layoutID = layoutId;
- initialize(context); // Re-run initialize for the new pad layout
- }
- /**
- * Recursive function that flattens a root layout and returns all the child Views independent of
- * layout level.
- * @param viewgroup The root viewroup to be analized
- * @return an ArrayList containing all the views below the original ViewGroup
- */
- ArrayList<View> flattenLayout(ViewGroup viewgroup)
- {
- ArrayList<View> viewList = new ArrayList<View>();
- for(int i = 0; i < viewgroup.getChildCount();i++)
- {
- View v = viewgroup.getChildAt(i);
- if(v instanceof ViewGroup)
- {
- viewList.addAll(flattenLayout((ViewGroup)v));
- }
- else if(v instanceof View)
- {
- viewList.add((View)v);
- }
- }
- return viewList;
- }
- public void setPadEventListener(PadEventListener l)
- {
- listener2 = l;
- }
- void notifyEventListener(PadEvent event)
- {
- listener2.onPadEvent(event);
- }
- @Override
- public void onJoystickPadChange(View v, int axisX, int axisY)
- {
- PadEvent padEvent = new PadEvent(v,(float)axisX,(float)axisY,((JoystickPad)v).getAxisXAction(),((JoystickPad)v).getAxisYAction(),0);
- notifyEventListener(padEvent);
- }
- @Override
- public boolean onTouch(View v, MotionEvent event)
- {
- PadEvent padEvent=null;
- if(event.getAction()==MotionEvent.ACTION_DOWN)
- {
- padEvent = new PadEvent(v,1f,-1f,((PadButton)v).getAction(),"",-1);
- }
- if(event.getAction()==MotionEvent.ACTION_UP)
- {
- padEvent = new PadEvent(v,0f,-1f,((PadButton)v).getAction(),"",-1);
- }
- notifyEventListener(padEvent);
- return true;
- }
- }
- This is the code for the Button extension that goes inside the ControlPad class
- public class PadButton extends Button
- {
- boolean toggle;
- String action="";
- public PadButton(Context context, AttributeSet attrs) {
- super(context, attrs);
- action = attrs.getAttributeValue(null, "action");
- toggle = attrs.getAttributeBooleanValue("", "toggle", false);
- }
- public boolean isToggle() {
- return toggle;
- }
- public String getAction() {
- return action;
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- Log.i("PadButton","touch down!");
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement