project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/UserInput/TouchInterface.java
author koda
Tue, 21 Jan 2014 22:43:06 +0100
changeset 10017 de822cd3df3a
parent 7584 7831c84cc644
permissions -rw-r--r--
fixwhitespace and dos2unix
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     1
/*
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     2
 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
6700
e04da46ee43c the most important commit of the year
koda
parents: 6487
diff changeset
     3
 * Copyright (c) 2011-2012 Richard Deurwaarder <xeli@xelification.com>
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     4
 *
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 6700
diff changeset
     5
 * This program is free software; you can redistribute it and/or
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 6700
diff changeset
     6
 * modify it under the terms of the GNU General Public License
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 6700
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 6700
diff changeset
     8
 * of the License, or (at your option) any later version.
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     9
 *
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    13
 * GNU General Public License for more details.
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    14
 *
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    16
 * along with this program; if not, write to the Free Software
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 6700
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    18
 */
6487
bd3c736c1eac android: rename TouchInterface package to UserInput
Xeli
parents: 6332
diff changeset
    19
package org.hedgewars.hedgeroid.UserInput;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    20
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    21
import org.hedgewars.hedgeroid.SDLActivity;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    22
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    23
import android.os.Build;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    24
import android.util.Log;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    25
import android.view.MotionEvent;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    26
import android.view.View;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    27
import android.view.View.OnTouchListener;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    28
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    29
public class TouchInterface{
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    30
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    31
    public static OnTouchListener getTouchInterface(){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    32
        OnTouchListener toucher;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    33
        if(Build.VERSION.SDK_INT < 5){//8 == Build.VERSION_CODES.FROYO
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    34
            toucher = new TouchInterfaceST();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    35
        }else{
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    36
            toucher = new TouchInterfaceMT();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    37
        }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    38
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    39
        return toucher;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    40
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    41
}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    42
/**
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    43
 * Touch interface with multitouch
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    44
 */
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    45
class TouchInterfaceMT implements OnTouchListener {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    46
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    47
    private boolean firstEvent = true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    48
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    49
    public boolean onTouch(View v, MotionEvent event) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    50
        //dumpEvent(event);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    51
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    52
        int action = event.getAction();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    53
        int actionCode = action & MotionEvent.ACTION_MASK;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    54
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    55
        for (int i = 0; i < event.getPointerCount(); i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    56
            SDLActivity.onNativeTouch(event.getDeviceId(),  event.getPointerId(i), actionCode, (int)event.getX(i), (int)event.getY(i), event.getPressure(i));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    57
//          Log.d("Android", String.format("x=%f, y=%f, pntr=%d", event.getX(i), event.getY(i), event.getPointerId(i)));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    58
           }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    59
        return true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    60
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    61
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    62
    /** Show an event in the LogCat view, for debugging */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    63
    private void dumpEvent(MotionEvent event) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    64
       String names[] = { "DOWN" , "UP" , "MOVE" , "CANCEL" , "OUTSIDE" ,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    65
          "POINTER_DOWN" , "POINTER_UP" , "7?" , "8?" , "9?" };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    66
       StringBuilder sb = new StringBuilder();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    67
       int action = event.getAction();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    68
       int actionCode = action & MotionEvent.ACTION_MASK;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    69
       sb.append("event ACTION_" ).append(names[actionCode]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    70
       if (actionCode == MotionEvent.ACTION_POINTER_DOWN
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    71
             || actionCode == MotionEvent.ACTION_POINTER_UP) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    72
          sb.append("(pid " ).append(
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    73
          action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    74
          sb.append(")" );
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    75
       }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    76
       sb.append("[" );
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    77
       for (int i = 0; i < event.getPointerCount(); i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    78
          sb.append("#" ).append(i);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    79
          sb.append("(pid " ).append(event.getPointerId(i));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    80
          sb.append(")=" ).append((int) event.getX(i));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    81
          sb.append("," ).append((int) event.getY(i));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    82
          if (i + 1 < event.getPointerCount())
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    83
             sb.append(";" );
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    84
       }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    85
       sb.append("]" );
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    86
       Log.d("HW_APP_TOUCH", sb.toString());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    87
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    88
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    89
}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    90
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    91
/**
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    92
 * Touch interface without multitouch
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    93
 */
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    94
class TouchInterfaceST implements OnTouchListener {
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    95
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    96
    public boolean onTouch(View v, MotionEvent event) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    97
        return false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    98
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    99
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   100
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   101
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   102
}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   103