project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/UserInput/TouchInterface.java
changeset 6487 bd3c736c1eac
parent 6332 5d9cc2441c48
child 6700 e04da46ee43c
equal deleted inserted replaced
6486:2a3ee24764bb 6487:bd3c736c1eac
       
     1 /*
       
     2  * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2011 Richard Deurwaarder <xeli@xelification.com>
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation; version 2 of the License
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program; if not, write to the Free Software
       
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
       
    17  */
       
    18 
       
    19 package org.hedgewars.hedgeroid.UserInput;
       
    20 
       
    21 import org.hedgewars.hedgeroid.SDLActivity;
       
    22 
       
    23 import android.os.Build;
       
    24 import android.util.Log;
       
    25 import android.view.MotionEvent;
       
    26 import android.view.View;
       
    27 import android.view.View.OnTouchListener;
       
    28 
       
    29 public class TouchInterface{
       
    30 
       
    31 	public static OnTouchListener getTouchInterface(){
       
    32 		OnTouchListener toucher;
       
    33 		if(Build.VERSION.SDK_INT < 5){//8 == Build.VERSION_CODES.FROYO
       
    34 			toucher = new TouchInterfaceST();
       
    35 		}else{
       
    36 			toucher = new TouchInterfaceMT();
       
    37 		}
       
    38 
       
    39 		return toucher;
       
    40 	}
       
    41 }
       
    42 /**
       
    43  * Touch interface with multitouch
       
    44  */
       
    45 class TouchInterfaceMT implements OnTouchListener {
       
    46 	
       
    47 	private boolean firstEvent = true;
       
    48 	
       
    49 	public boolean onTouch(View v, MotionEvent event) {
       
    50 		//dumpEvent(event);
       
    51 		
       
    52 		int action = event.getAction();
       
    53 		int actionCode = action & MotionEvent.ACTION_MASK;
       
    54 		
       
    55 		for (int i = 0; i < event.getPointerCount(); i++) {
       
    56 			SDLActivity.onNativeTouch(event.getDeviceId(),  event.getPointerId(i), actionCode, (int)event.getX(i), (int)event.getY(i), event.getPressure(i));
       
    57 //			Log.d("Android", String.format("x=%f, y=%f, pntr=%d", event.getX(i), event.getY(i), event.getPointerId(i)));
       
    58 		   }
       
    59 		return true;
       
    60 	}
       
    61 
       
    62 	/** Show an event in the LogCat view, for debugging */
       
    63 	private void dumpEvent(MotionEvent event) {
       
    64 	   String names[] = { "DOWN" , "UP" , "MOVE" , "CANCEL" , "OUTSIDE" ,
       
    65 	      "POINTER_DOWN" , "POINTER_UP" , "7?" , "8?" , "9?" };
       
    66 	   StringBuilder sb = new StringBuilder();
       
    67 	   int action = event.getAction();
       
    68 	   int actionCode = action & MotionEvent.ACTION_MASK;
       
    69 	   sb.append("event ACTION_" ).append(names[actionCode]);
       
    70 	   if (actionCode == MotionEvent.ACTION_POINTER_DOWN
       
    71 	         || actionCode == MotionEvent.ACTION_POINTER_UP) {
       
    72 	      sb.append("(pid " ).append(
       
    73 	      action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
       
    74 	      sb.append(")" );
       
    75 	   }
       
    76 	   sb.append("[" );
       
    77 	   for (int i = 0; i < event.getPointerCount(); i++) {
       
    78 	      sb.append("#" ).append(i);
       
    79 	      sb.append("(pid " ).append(event.getPointerId(i));
       
    80 	      sb.append(")=" ).append((int) event.getX(i));
       
    81 	      sb.append("," ).append((int) event.getY(i));
       
    82 	      if (i + 1 < event.getPointerCount())
       
    83 	         sb.append(";" );
       
    84 	   }
       
    85 	   sb.append("]" );
       
    86 	   Log.d("HW_APP_TOUCH", sb.toString());
       
    87 	}
       
    88 
       
    89 }
       
    90 
       
    91 /**
       
    92  * Touch interface without multitouch
       
    93  */
       
    94 class TouchInterfaceST implements OnTouchListener {
       
    95 
       
    96 	public boolean onTouch(View v, MotionEvent event) {
       
    97 		return false;
       
    98 	}
       
    99 
       
   100 
       
   101 
       
   102 }
       
   103