project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/TouchInterface/TouchInterface.java
branchhedgeroid
changeset 5546 d36dac11a9b2
child 5550 50650032c251
equal deleted inserted replaced
5544:f72a87bfa655 5546:d36dac11a9b2
       
     1 package org.hedgewars.mobile.TouchInterface;
       
     2 
       
     3 import org.hedgewars.mobile.SDLActivity;
       
     4 
       
     5 import android.os.Build;
       
     6 import android.util.Log;
       
     7 import android.view.MotionEvent;
       
     8 import android.view.View;
       
     9 import android.view.View.OnTouchListener;
       
    10 
       
    11 public class TouchInterface{
       
    12 
       
    13 	public static OnTouchListener getTouchInterface(){
       
    14 		OnTouchListener toucher;
       
    15 		if(Build.VERSION.SDK_INT < 5){//8 == Build.VERSION_CODES.FROYO
       
    16 			toucher = new TouchInterfaceST();
       
    17 		}else{
       
    18 			toucher = new TouchInterfaceMT();
       
    19 		}
       
    20 
       
    21 		return toucher;
       
    22 	}
       
    23 }
       
    24 /**
       
    25  * Touch interface with multitouch
       
    26  */
       
    27 class TouchInterfaceMT implements OnTouchListener {
       
    28 	
       
    29 	private boolean firstEvent = true;
       
    30 	
       
    31 	public boolean onTouch(View v, MotionEvent event) {
       
    32 		//dumpEvent(event);
       
    33 		
       
    34 		if(firstEvent){
       
    35 			firstEvent = false;
       
    36 			SDLActivity.onNativeTouch(-1, -1, v.getWidth(), v.getHeight(), 1);
       
    37 		}
       
    38 		
       
    39 		int action = event.getAction();
       
    40 		int actionCode = action & MotionEvent.ACTION_MASK;
       
    41 		
       
    42 		for (int i = 0; i < event.getPointerCount(); i++) {
       
    43 			SDLActivity.onNativeTouch(actionCode, event.getPointerId(i), event.getX(i), event.getY(i), event.getPressure(i));
       
    44 		   }
       
    45 		return true;
       
    46 	}
       
    47 
       
    48 	/** Show an event in the LogCat view, for debugging */
       
    49 	private void dumpEvent(MotionEvent event) {
       
    50 	   String names[] = { "DOWN" , "UP" , "MOVE" , "CANCEL" , "OUTSIDE" ,
       
    51 	      "POINTER_DOWN" , "POINTER_UP" , "7?" , "8?" , "9?" };
       
    52 	   StringBuilder sb = new StringBuilder();
       
    53 	   int action = event.getAction();
       
    54 	   int actionCode = action & MotionEvent.ACTION_MASK;
       
    55 	   sb.append("event ACTION_" ).append(names[actionCode]);
       
    56 	   if (actionCode == MotionEvent.ACTION_POINTER_DOWN
       
    57 	         || actionCode == MotionEvent.ACTION_POINTER_UP) {
       
    58 	      sb.append("(pid " ).append(
       
    59 	      action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
       
    60 	      sb.append(")" );
       
    61 	   }
       
    62 	   sb.append("[" );
       
    63 	   for (int i = 0; i < event.getPointerCount(); i++) {
       
    64 	      sb.append("#" ).append(i);
       
    65 	      sb.append("(pid " ).append(event.getPointerId(i));
       
    66 	      sb.append(")=" ).append((int) event.getX(i));
       
    67 	      sb.append("," ).append((int) event.getY(i));
       
    68 	      if (i + 1 < event.getPointerCount())
       
    69 	         sb.append(";" );
       
    70 	   }
       
    71 	   sb.append("]" );
       
    72 	   Log.d("HW_APP_TOUCH", sb.toString());
       
    73 	}
       
    74 
       
    75 }
       
    76 
       
    77 /**
       
    78  * Touch interface without multitouch
       
    79  */
       
    80 class TouchInterfaceST implements OnTouchListener {
       
    81 
       
    82 	public boolean onTouch(View v, MotionEvent event) {
       
    83 		return false;
       
    84 	}
       
    85 
       
    86 
       
    87 
       
    88 }
       
    89