Advertisement
Guest User

Untitled

a guest
Aug 15th, 2016
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.86 KB | None | 0 0
  1. package com.gavin.framework.implementation;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import android.view.MotionEvent;
  7. import android.view.View;
  8.  
  9. import com.gavin.framework.Pool;
  10. import com.gavin.framework.Input.TouchEvent;
  11. import com.gavin.framework.Pool.PoolObjectFactory;
  12.  
  13. public class MultiTouchHandler implements TouchHandler{
  14.  
  15.     private static final int MAX_TOUCHPOINTS = 10;
  16.  
  17.     boolean[] isTouched = new boolean[MAX_TOUCHPOINTS];
  18.     int[] touchX = new int[MAX_TOUCHPOINTS];
  19.     int[] touchY = new int[MAX_TOUCHPOINTS];
  20.     int[] id = new int[MAX_TOUCHPOINTS];
  21.     Pool<TouchEvent> touchEventPool;
  22.     List<TouchEvent> touchEvents = new ArrayList<TouchEvent>();
  23.     List<TouchEvent> touchEventsBuffer = new ArrayList<TouchEvent>();
  24.     float scaleX;
  25.     float scaleY;
  26.  
  27.     public MultiTouchHandler(View view, float scaleX, float scaleY){
  28.         PoolObjectFactory<TouchEvent> factory = new PoolObjectFactory<TouchEvent>() {
  29.             @Override
  30.             public TouchEvent createObject() {
  31.                 return new TouchEvent();
  32.             }
  33.         };
  34.         touchEventPool = new Pool<TouchEvent>(factory,100);
  35.         view.setOnTouchListener(this);
  36.  
  37.         this.scaleX = scaleX;
  38.         this.scaleY = scaleY;
  39.     }
  40.  
  41.     @Override
  42.     public boolean onTouch(View v, MotionEvent event){
  43.         synchronized (this){
  44.             int action = event.getAction() & MotionEvent.ACTION_MASK;
  45.             int pointerIndex = (event.getActionIndex() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
  46.             int pointerCount = event.getPointerCount();
  47.             TouchEvent touchEvent;
  48.             for(int i = 0; i < MAX_TOUCHPOINTS; i++){
  49.                 if(i >= pointerCount){
  50.                     isTouched[i] = false;
  51.                     id[i] = -1;
  52.                     continue;
  53.                 }
  54.                 int pointerId = event.getPointerId(i);
  55.                 if(event.getAction() != MotionEvent.ACTION_MOVE && i!=pointerIndex){
  56.                     // if it's an up/down/cancel/out event, mask the id to see if we should process it for this touch
  57.                     // point
  58.                     continue;
  59.                 }
  60.                 switch(action){
  61.                     case MotionEvent.ACTION_DOWN:
  62.                         touchEvent = touchEventPool.newObject();
  63.                         touchEvent.type = touchEvent.TOUCH_DOWN;
  64.                         touchEvent.pointer = pointerId;
  65.                         touchEvent.x = touchX[i] = (int) (event.getX(i) * scaleX);
  66.                         touchEvent.y = touchY[i] = (int) (event.getY(i) * scaleY);
  67.                         isTouched[i] = true;
  68.                         id[i] = pointerId;
  69.                         touchEventsBuffer.add(touchEvent);
  70.                         break;
  71.                     case MotionEvent.ACTION_POINTER_DOWN:
  72.                         touchEvent = touchEventPool.newObject();
  73.                         touchEvent.type = touchEvent.TOUCH_DOWN;
  74.                         touchEvent.pointer = pointerId;
  75.                         touchEvent.x = touchX[i] = (int) (event.getX(i) * scaleX);
  76.                         touchEvent.y = touchY[i] = (int) (event.getY(i) * scaleY);
  77.                         isTouched[i] = true;
  78.                         id[i] = pointerId;
  79.                         touchEventsBuffer.add(touchEvent);
  80.                         break;
  81.                     case MotionEvent.ACTION_UP:
  82.                         touchEvent = touchEventPool.newObject();
  83.                         touchEvent.type = TouchEvent.TOUCH_UP;
  84.                         touchEvent.pointer = pointerId;
  85.                         touchEvent.x = touchX[i] = (int) (event.getX(i) * scaleX);
  86.                         touchEvent.y = touchY[i] = (int) (event.getY(i) * scaleY);
  87.                         isTouched[i] = false;
  88.                         id[i] = pointerId;
  89.                         touchEventsBuffer.add(touchEvent);
  90.                         break;
  91.                     case MotionEvent.ACTION_POINTER_UP:
  92.                         touchEvent = touchEventPool.newObject();
  93.                         touchEvent.type = TouchEvent.TOUCH_UP;
  94.                         touchEvent.pointer = pointerId;
  95.                         touchEvent.x = touchX[i] = (int) (event.getX(i) * scaleX);
  96.                         touchEvent.y = touchY[i] = (int) (event.getY(i) * scaleY);
  97.                         isTouched[i] = false;
  98.                         id[i] = pointerId;
  99.                         touchEventsBuffer.add(touchEvent);
  100.                         break;
  101.                     case MotionEvent.ACTION_CANCEL:
  102.                     case MotionEvent.ACTION_MOVE:
  103.                         touchEvent = touchEventPool.newObject();
  104.                         touchEvent.type = TouchEvent.TOUCH_DRAGGED;
  105.                         touchEvent.pointer = pointerId;
  106.                         touchEvent.x = touchX[i] = (int) (event.getX(i) * scaleX);
  107.                         touchEvent.y = touchY[i] = (int) (event.getY(i) * scaleY);
  108.                         isTouched[i] = true;
  109.                         id[i] = pointerId;
  110.                         touchEventsBuffer.add(touchEvent);
  111.                         break;
  112.                 }
  113.             }
  114.             return true;
  115.         }
  116.     }
  117.  
  118.     @Override
  119.     public boolean isTouchDown(int pointer){
  120.         synchronized (this){
  121.             int index = getIndex(pointer);
  122.             if (index < 0 || index >= MAX_TOUCHPOINTS){
  123.                 return false;
  124.             }else{
  125.                 return isTouched[index];
  126.             }
  127.         }
  128.     }
  129.  
  130.     @Override
  131.     public int getTouchX(int pointer){
  132.         synchronized (this){
  133.             int index = getIndex(pointer);
  134.             if(index < 0 || index >= MAX_TOUCHPOINTS){
  135.                 return 0;
  136.             }else{
  137.                 return touchX[index];
  138.             }
  139.         }
  140.     }
  141.  
  142.     @Override
  143.     public int getTouchY(int pointer){
  144.         synchronized (this){
  145.             int index = getIndex(pointer);
  146.             if(index < 0 || index >= MAX_TOUCHPOINTS){
  147.                 return 0;
  148.             }else{
  149.                 return touchY[index];
  150.             }
  151.         }
  152.     }
  153.  
  154.     @Override
  155.     public List<TouchEvent> getTouchEvents(){
  156.         synchronized (this){
  157.             int len = touchEvents.size();
  158.             for(int i = 0; i < len; i++){
  159.                 touchEventPool.free(touchEvents.get(i));
  160.             }
  161.             touchEvents.clear();
  162.             touchEvents.addAll(touchEventsBuffer);
  163.             touchEventsBuffer.clear();
  164.             return touchEvents;
  165.         }
  166.     }
  167.  
  168.     // returns index for a given pointerID or -1 for no index
  169.     private int getIndex(int pointerId){
  170.         for (int i = 0; i < MAX_TOUCHPOINTS; i++){
  171.             if(id[i] == pointerId){
  172.                 return i;
  173.             }
  174.         }
  175.         return -1;
  176.     }
  177.  
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement