summaryrefslogtreecommitdiffstats
path: root/src/com/android/dreams/phototable/PhotoTouchListener.java
blob: 190dc3d19fc41e50e69510cd8cb0695a598a3ba5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.dreams.phototable;

import android.content.Context;
import android.content.res.Resources;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;

/**
 * Touch listener that implements phototable interactions.
 */
public class PhotoTouchListener implements View.OnTouchListener {
    private static final String TAG = "PhotoTouchListener";
    private static final boolean DEBUG = false;
    private static final int INVALID_POINTER = -1;
    private static final int MAX_POINTER_COUNT = 10;
    private final int mTouchSlop;
    private final int mTapTimeout;
    private final PhotoTable mTable;
    private final float mBeta;
    private final float mTableRatio;
    private final boolean mEnableFling;
    private final boolean mManualImageRotation;
    private long mLastEventTime;
    private float mLastTouchX;
    private float mLastTouchY;
    private float mInitialTouchX;
    private float mInitialTouchY;
    private float mInitialTouchA;
    private long mInitialTouchTime;
    private float mInitialTargetX;
    private float mInitialTargetY;
    private float mInitialTargetA;
    private float mDX;
    private float mDY;
    private int mA = INVALID_POINTER;
    private int mB = INVALID_POINTER;
    private float[] pts = new float[MAX_POINTER_COUNT];
    private float[] tmp = new float[MAX_POINTER_COUNT];

    public PhotoTouchListener(Context context, PhotoTable table) {
        mTable = table;
        final ViewConfiguration configuration = ViewConfiguration.get(context);
        mTouchSlop = configuration.getScaledTouchSlop();
        mTapTimeout = configuration.getTapTimeout();
        final Resources resources = context.getResources();
        mBeta = resources.getInteger(R.integer.table_damping) / 1000000f;
        mTableRatio = resources.getInteger(R.integer.table_ratio) / 1000000f;
        mEnableFling = resources.getBoolean(R.bool.enable_fling);
        mManualImageRotation = resources.getBoolean(R.bool.enable_manual_image_rotation);
    }

    /** Get angle defined by first two touches, in degrees */
    private float getAngle(View target, MotionEvent ev) {
        float alpha = 0f;
        int a = ev.findPointerIndex(mA);
        int b = ev.findPointerIndex(mB);
        if (a >=0 && b >=0) {
            alpha = (float) (Math.atan2(pts[2*a + 1] - pts[2*b + 1],
                                        pts[2*a] - pts[2*b]) *
                             180f / Math.PI);
        }
        return alpha;
    }

    private void resetTouch(View target) {
        mInitialTouchX = -1;
        mInitialTouchY = -1;
        mInitialTouchA = 0f;
        mInitialTargetX = (float) target.getX();
        mInitialTargetY = (float) target.getY();
        mInitialTargetA = (float) target.getRotation();
    }

    public void onFling(View target, float dX, float dY) {
        if (!mEnableFling) {
            return;
        }
        log("fling " + dX + ", " + dY);

        // convert to pixel per frame
        dX /= 60f;
        dY /= 60f;

        // starting position compionents in global corrdinate frame
        final float x0 = pts[0];
        final float y0 = pts[1];

        // velocity
        final float v = (float) Math.hypot(dX, dY);

        if (v == 0f) {
            return;
        }

        // number of steps to come to a stop
        final float n = (float) Math.max(1.0, (- Math.log(v) / Math.log(mBeta)));
        // distance travelled before stopping
        final float s = (float) Math.max(0.0, (v * (1f - Math.pow(mBeta, n)) / (1f - mBeta)));

        // ending posiiton after stopping
        final float x1 = x0 + s * dX / v;
        final float y1 = y0 + s * dY / v;

        mTable.fling(target, x1 - x0, y1 - y0, (int) (1000f * n / 60f), false);
    }

    @Override
    public boolean onTouch(View target, MotionEvent ev) {
        final int action = ev.getActionMasked();

        // compute raw coordinates
        for(int i = 0; i < 10 && i < ev.getPointerCount(); i++) {
            pts[i*2] = ev.getX(i);
            pts[i*2 + 1] = ev.getY(i);
        }
        target.getMatrix().mapPoints(pts);

        switch (action) {
        case MotionEvent.ACTION_DOWN:
            mTable.moveToTopOfPile(target);
            mInitialTouchTime = ev.getEventTime();
            mA = ev.getPointerId(ev.getActionIndex());
            resetTouch(target);
            break;

        case MotionEvent.ACTION_POINTER_DOWN:
            if (mB == INVALID_POINTER) {
                mB = ev.getPointerId(ev.getActionIndex());
                mInitialTouchA = getAngle(target, ev);
            }
            break;

        case MotionEvent.ACTION_POINTER_UP:
            if (mB == ev.getPointerId(ev.getActionIndex())) {
                mB = INVALID_POINTER;
                mInitialTargetA = (float) target.getRotation();
            }
            if (mA == ev.getPointerId(ev.getActionIndex())) {
                log("primary went up!");
                mA = mB;
                resetTouch(target);
                mB = INVALID_POINTER;
            }
            break;

        case MotionEvent.ACTION_MOVE: {
                if (mA != INVALID_POINTER) {
                    int idx = ev.findPointerIndex(mA);
                    float x = pts[2 * idx];
                    float y = pts[2 * idx + 1];
                    if (mInitialTouchX == -1 && mInitialTouchY == -1) {
                        mInitialTouchX = x;
                        mInitialTouchY = y;
                    } else {
                        float dt = (float) (ev.getEventTime() - mLastEventTime) / 1000f;
                        float tmpDX = (x - mLastTouchX) / dt;
                        float tmpDY = (y - mLastTouchY) / dt;
                        if (dt > 0f && (Math.abs(tmpDX) > 5f || Math.abs(tmpDY) > 5f)) {
                            // work around odd bug with multi-finger flings
                            mDX = tmpDX;
                            mDY = tmpDY;
                        }
                        log("move " + mDX + ", " + mDY);

                        mLastEventTime = ev.getEventTime();
                        mLastTouchX = x;
                        mLastTouchY = y;
                    }

                    if (mTable.getSelected() != target) {
                        target.animate().cancel();

                        target.setX((int) (mInitialTargetX + x - mInitialTouchX));
                        target.setY((int) (mInitialTargetY + y - mInitialTouchY));
                        if (mManualImageRotation && mB != INVALID_POINTER) {
                            float a = getAngle(target, ev);
                            target.setRotation(
                                    (int) (mInitialTargetA + a - mInitialTouchA));
                        }
                    }
                }
            }
            break;

        case MotionEvent.ACTION_UP: {
                if (mA != INVALID_POINTER) {
                    int idx = ev.findPointerIndex(mA);
                    float x0 = pts[2 * idx];
                    float y0 = pts[2 * idx + 1];
                    if (mInitialTouchX == -1 && mInitialTouchY == -1) {
                        mInitialTouchX = x0;
                        mInitialTouchY = y0;
                    }
                    double distance = Math.hypot(x0 - mInitialTouchX,
                                                 y0 - mInitialTouchY);
                    if (mTable.getSelected() == target) {
                        mTable.dropOnTable(target);
                        mTable.clearSelection();
                    } else if ((ev.getEventTime() - mInitialTouchTime) < mTapTimeout &&
                               distance < mTouchSlop) {
                        // tap
                        target.animate().cancel();
                        mTable.setSelection(target);
                    } else {
                        onFling(target, mDX, mDY);
                    }
                    mA = INVALID_POINTER;
                    mB = INVALID_POINTER;
                }
            }
            break;

        case MotionEvent.ACTION_CANCEL:
            log("action cancel!");
            break;
        }

        return true;
    }

    private static void log(String message) {
        if (DEBUG) {
            Log.i(TAG, message);
        }
    }
}