project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/TouchInterface/TouchInterface.java
branchhedgeroid
changeset 6047 10011f051f9c
child 6332 5d9cc2441c48
equal deleted inserted replaced
6045:9a7cc0f29430 6047:10011f051f9c
       
     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.TouchInterface;
       
    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 		if(firstEvent){
       
    53 			firstEvent = false;
       
    54 			SDLActivity.onNativeTouch(-1, -1, v.getWidth(), v.getHeight(), 1);
       
    55 		}
       
    56 		
       
    57 		int action = event.getAction();
       
    58 		int actionCode = action & MotionEvent.ACTION_MASK;
       
    59 		
       
    60 		for (int i = 0; i < event.getPointerCount(); i++) {
       
    61 			SDLActivity.onNativeTouch(actionCode, event.getPointerId(i), (int)event.getX(i), (int)event.getY(i), event.getPressure(i));
       
    62 //			Log.d("Android", String.format("x=%f, y=%f, pntr=%d", event.getX(i), event.getY(i), event.getPointerId(i)));
       
    63 		   }
       
    64 		return true;
       
    65 	}
       
    66 
       
    67 	/** Show an event in the LogCat view, for debugging */
       
    68 	private void dumpEvent(MotionEvent event) {
       
    69 	   String names[] = { "DOWN" , "UP" , "MOVE" , "CANCEL" , "OUTSIDE" ,
       
    70 	      "POINTER_DOWN" , "POINTER_UP" , "7?" , "8?" , "9?" };
       
    71 	   StringBuilder sb = new StringBuilder();
       
    72 	   int action = event.getAction();
       
    73 	   int actionCode = action & MotionEvent.ACTION_MASK;
       
    74 	   sb.append("event ACTION_" ).append(names[actionCode]);
       
    75 	   if (actionCode == MotionEvent.ACTION_POINTER_DOWN
       
    76 	         || actionCode == MotionEvent.ACTION_POINTER_UP) {
       
    77 	      sb.append("(pid " ).append(
       
    78 	      action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
       
    79 	      sb.append(")" );
       
    80 	   }
       
    81 	   sb.append("[" );
       
    82 	   for (int i = 0; i < event.getPointerCount(); i++) {
       
    83 	      sb.append("#" ).append(i);
       
    84 	      sb.append("(pid " ).append(event.getPointerId(i));
       
    85 	      sb.append(")=" ).append((int) event.getX(i));
       
    86 	      sb.append("," ).append((int) event.getY(i));
       
    87 	      if (i + 1 < event.getPointerCount())
       
    88 	         sb.append(";" );
       
    89 	   }
       
    90 	   sb.append("]" );
       
    91 	   Log.d("HW_APP_TOUCH", sb.toString());
       
    92 	}
       
    93 
       
    94 }
       
    95 
       
    96 /**
       
    97  * Touch interface without multitouch
       
    98  */
       
    99 class TouchInterfaceST implements OnTouchListener {
       
   100 
       
   101 	public boolean onTouch(View v, MotionEvent event) {
       
   102 		return false;
       
   103 	}
       
   104 
       
   105 
       
   106 
       
   107 }
       
   108