Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2011
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.02 KB | None | 0 0
  1. This is the working code for a container supporting event separation
  2.  
  3. public class Container extends LinearLayout
  4. {  
  5.     LinkedHashMap<Integer,View> pointers = new LinkedHashMap<Integer,View>();
  6.     ArrayList<View> views  = new ArrayList<View>();
  7.    
  8.     public Container(Context context) {
  9.         super(context);
  10.         initialize(context);
  11.      
  12.     }
  13.  
  14.     public Container(Context context, AttributeSet attrs) {
  15.         super(context, attrs);
  16.         initialize(context);
  17.     }
  18.    
  19.     private void initialize(Context context)
  20.     {
  21.  
  22.     }
  23.     @Override
  24.     public void onLayout(boolean changed, int l, int t, int r, int b)
  25.     {
  26.         super.onLayout(changed, l, t, r, b);
  27.         views = LayoutUtil.flattenLayout(this,false);
  28.         Log.v("Container","Child Count " + views.size());
  29.         for(View foo : views)
  30.         {
  31.             Rect rect = new Rect();
  32.             foo.getGlobalVisibleRect(rect);
  33.             Log.i("Container",foo + "- Top: " + rect.top + " Bottom: " + rect.bottom + " Left: " + rect.left+ " Right: " + rect.right);
  34.             Log.v("Container",foo + "- Top: " + foo.getTop() + " Bottom: " + foo.getBottom() + " Left: " + foo.getLeft() + " Right: " + foo.getRight());
  35.         }
  36.     }
  37.     ArrayList<MotionEvent> flattenEvent(MotionEvent event)
  38.     {
  39.         ArrayList<MotionEvent> l= new ArrayList<MotionEvent>();
  40.         if(event.getPointerCount()==1)
  41.         {
  42.             l.add(event);
  43.             return l;
  44.         }
  45.         else
  46.         for(int i = 0; i < event.getPointerCount();i++)
  47.         {
  48.             MotionEvent e = MotionEvent.obtain(event);
  49.             e.setLocation(event.getX(i), event.getY(i));
  50.             l.add(e);
  51.         }
  52.         return l;
  53.     }
  54.    
  55.     @Override
  56.     public boolean onInterceptTouchEvent(MotionEvent event)
  57.     {
  58.         return true;
  59.     }
  60.     @Override
  61.     public boolean onTouchEvent(MotionEvent event)
  62.     {
  63. //      dumpEvent(event);
  64.         int action = event.getAction() & MotionEvent.ACTION_MASK;
  65.         if(action==MotionEvent.ACTION_DOWN)
  66.         {
  67.             for(View v: views)
  68.             {
  69.                 Rect r = new Rect();
  70.                 v.getGlobalVisibleRect(r);
  71.                 if (event.getX() > r.left && event.getX() < r.right
  72.                         && event.getY() > r.top
  73.                         && event.getY() < r.bottom) {
  74.                     pointers.put(event.getPointerId(0),v);
  75.                     pointers.get(event.getPointerId(0)).onTouchEvent(event);
  76.                     break;
  77.                 }
  78.             }
  79.         }
  80.         if(action==MotionEvent.ACTION_POINTER_DOWN)
  81.         {
  82.             int pid = event.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT;
  83.             int index = event.findPointerIndex(pid);
  84.                    
  85.             for(View v: views)
  86.             {
  87.                
  88.                 Rect r = new Rect();
  89.                 v.getGlobalVisibleRect(r);
  90.                 if (event.getX(index) > r.left
  91.                         && event.getX(index) < r.right
  92.                         && event.getY(index) > r.top
  93.                         && event.getY(index) < r.bottom) {
  94.                    
  95.                    
  96.                     pointers.put(pid,v);
  97.                     MotionEvent copy = MotionEvent.obtain(event);
  98.                     copy.setAction(MotionEvent.ACTION_DOWN);
  99.                     copy.setLocation(event.getX(index), event.getY(index));
  100.                     pointers.get(pid).onTouchEvent(copy);
  101.                 }
  102.             }
  103.         }
  104.         if(action==MotionEvent.ACTION_POINTER_UP)
  105.         {
  106.             int pid = event.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT;
  107.             int index = event.findPointerIndex(pid);
  108.        
  109.             if(pointers.get(pid)!=null) // If the touch was outside any view
  110.             {
  111.                 MotionEvent copy = MotionEvent.obtain(event);
  112.                 copy.setAction(MotionEvent.ACTION_UP);
  113.                 pointers.get(pid).onTouchEvent(copy);
  114.                 pointers.remove(pid);
  115.             }
  116.         }
  117.        
  118.         if(action==MotionEvent.ACTION_MOVE)
  119.         {
  120.             for(int i = 0; i<event.getPointerCount();i++)
  121.             {
  122.                 int pid = event.getPointerId(i);
  123.                 MotionEvent copy = MotionEvent.obtain(event);
  124.                 copy.setLocation(event.getX(i), event.getY(i));
  125.                
  126.                 if(pointers.get(pid)==null) continue; // If the touch was outside any view
  127.                 pointers.get(pid).onTouchEvent(copy);
  128.             }
  129.         }
  130.        
  131.         if(action==MotionEvent.ACTION_UP)
  132.         {
  133.             if(pointers.get(event.getPointerId(0))!=null)
  134.             {
  135.                 pointers.get(event.getPointerId(0)).onTouchEvent(event);
  136.                 pointers.remove(event.getPointerId(0));
  137.             }
  138.         }
  139.         return true;
  140.     }
  141.     private void dumpEvent(MotionEvent event) {
  142.            String names[] = { "DOWN" , "UP" , "MOVE" , "CANCEL" , "OUTSIDE" ,
  143.               "POINTER_DOWN" , "POINTER_UP" , "7?" , "8?" , "9?" };
  144.            StringBuilder sb = new StringBuilder();
  145.            int action = event.getAction();
  146.            int actionCode = action & MotionEvent.ACTION_MASK;
  147.            sb.append("event ACTION_" ).append(names[actionCode]);
  148.            if (actionCode == MotionEvent.ACTION_POINTER_DOWN
  149.                  || actionCode == MotionEvent.ACTION_POINTER_UP) {
  150.               sb.append("(pid " ).append(
  151.               action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
  152.               sb.append(")" );
  153.            }
  154.            sb.append("[" );
  155.            for (int i = 0; i < event.getPointerCount(); i++) {
  156.               sb.append("#" ).append(i);
  157.               sb.append("(pid " ).append(event.getPointerId(i));
  158.               sb.append(")=" ).append((int) event.getX(i));
  159.               sb.append("," ).append((int) event.getY(i));
  160.               if (i + 1 < event.getPointerCount())
  161.                  sb.append(";" );
  162.            }
  163.            sb.append("]" );
  164.            Log.d("touchy", sb.toString());
  165.         }
  166. }
  167.  
  168. Code for the extension of the Container generic class
  169.  
  170. package pt.up.fe.dceg.accu.components.controlpad;
  171.  
  172. import java.util.ArrayList;
  173. import java.util.LinkedHashMap;
  174.  
  175. import pt.up.fe.dceg.accu.components.interfaces.JoystickPadChangeListener;
  176. import pt.up.fe.dceg.accu.components.interfaces.PadEventListener;
  177. import pt.up.fe.dceg.accu.components.interfaces.PadStateListener;
  178. import pt.up.fe.dceg.accu.util.Container;
  179. import android.content.Context;
  180. import android.util.AttributeSet;
  181. import android.util.Log;
  182. import android.view.LayoutInflater;
  183. import android.view.MotionEvent;
  184. import android.view.View;
  185. import android.view.View.OnTouchListener;
  186. import android.view.ViewGroup;
  187.  
  188. public class ControlPad2 extends Container
  189. implements JoystickPadChangeListener, OnTouchListener
  190. {
  191.     public static final String TAG = "ControlPad2";
  192.     Context context;
  193.     View view;
  194.     ArrayList<View> viewList = new ArrayList<View>(); // List of pad components
  195.     LinkedHashMap<View,String> actions = new LinkedHashMap<View,String>();
  196.     PadStateListener listener;
  197.     PadEventListener listener2;
  198.    
  199.     int layoutID;
  200.    
  201.     public ControlPad2(Context context) {
  202.         super(context);
  203.         initialize(context);
  204.     }
  205.     public ControlPad2(Context context, AttributeSet attrs) {
  206.         super(context, attrs);
  207.         layoutID = attrs.getAttributeResourceValue(null, "pad_layout", 0);
  208.         initialize(context);
  209.     }
  210.  
  211.     void initialize(Context context)
  212.     {
  213.         this.context = context;
  214.         LayoutInflater.from(context).inflate(layoutID,this,true);
  215.  
  216.         viewList = flattenLayout(this);
  217.         Log.i(TAG,""+viewList.size());
  218.         Log.v("ControlPad",viewList.size()+"");
  219.         setupPad();
  220.     }
  221.    
  222.     /**
  223.      * Registers Layout components as pad components in the padState and the listeners too
  224.      */
  225.     private void setupPad()
  226.     {
  227.         for(View v : viewList)
  228.         {
  229.             if(v instanceof JoystickPad)
  230.             {
  231.                 ((JoystickPad)v).setOnPadChangeListener(this);
  232.             }
  233.             else if(v instanceof PadButton)
  234.             {
  235.                 ((PadButton)v).setOnTouchListener(this);
  236.             }
  237.         }
  238.     }
  239.     public void setPadLayout(int layoutId)
  240.     {
  241.         this.layoutID = layoutId;
  242.         initialize(context); // Re-run initialize for the new pad layout
  243.     }
  244.    
  245.     /**
  246.      * Recursive function that flattens a root layout and returns all the child Views independent of
  247.      * layout level.
  248.      * @param viewgroup The root viewroup to be analized
  249.      * @return an ArrayList containing all the views below the original ViewGroup
  250.      */
  251.     ArrayList<View> flattenLayout(ViewGroup viewgroup)
  252.     {
  253.         ArrayList<View> viewList = new ArrayList<View>();
  254.        
  255.         for(int i = 0; i < viewgroup.getChildCount();i++)
  256.         {
  257.             View v = viewgroup.getChildAt(i);
  258.             if(v instanceof ViewGroup)
  259.             {
  260.                 viewList.addAll(flattenLayout((ViewGroup)v));
  261.             }
  262.             else if(v instanceof View)
  263.             {
  264.                 viewList.add((View)v);
  265.             }
  266.         }      
  267.         return viewList;
  268.     }
  269.    
  270.     public void setPadEventListener(PadEventListener l)
  271.     {
  272.         listener2 = l;
  273.     }
  274.     void notifyEventListener(PadEvent event)
  275.     {
  276.         listener2.onPadEvent(event);
  277.     }
  278.     @Override
  279.     public void onJoystickPadChange(View v, int axisX, int axisY)
  280.     {
  281.         PadEvent padEvent = new PadEvent(v,(float)axisX,(float)axisY,((JoystickPad)v).getAxisXAction(),((JoystickPad)v).getAxisYAction(),0);
  282.         notifyEventListener(padEvent);
  283.     }
  284.    
  285.     @Override
  286.     public boolean onTouch(View v, MotionEvent event)
  287.     {
  288.         PadEvent padEvent=null;
  289.        
  290.         if(event.getAction()==MotionEvent.ACTION_DOWN)
  291.         {
  292.             padEvent = new PadEvent(v,1f,-1f,((PadButton)v).getAction(),"",-1);
  293.         }
  294.         if(event.getAction()==MotionEvent.ACTION_UP)
  295.         {
  296.             padEvent = new PadEvent(v,0f,-1f,((PadButton)v).getAction(),"",-1);
  297.         }
  298.         notifyEventListener(padEvent);
  299.         return true;
  300.     }
  301. }
  302.  
  303.  
  304. This is the code for the Button extension that goes inside the ControlPad class
  305.  
  306. public class PadButton extends Button
  307. {
  308.     boolean toggle;
  309.     String action="";
  310.    
  311.     public PadButton(Context context, AttributeSet attrs) {
  312.         super(context, attrs);
  313.         action = attrs.getAttributeValue(null, "action");
  314.         toggle = attrs.getAttributeBooleanValue("", "toggle", false);
  315.     }
  316.     public boolean isToggle() {
  317.         return toggle;
  318.     }
  319.     public String getAction() {
  320.         return action;
  321.     }
  322.    
  323.     @Override
  324.     public boolean onTouchEvent(MotionEvent event) {
  325.         Log.i("PadButton","touch down!");
  326.         return false;
  327.     }  
  328. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement