Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fed3f728 authored by Alan Viverette's avatar Alan Viverette
Browse files

Generalize mapping between local and global View coordinates

Change-Id: Ib7f5d51debe2e9773d9ef2fa60a33379b229371a
parent e5d2af6b
Loading
Loading
Loading
Loading
+32 −24
Original line number Diff line number Diff line
@@ -15935,8 +15935,10 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
            return false;
        }
        transformMotionEventToGlobal(ev);
        ev.offsetLocation(info.mWindowLeft, info.mWindowTop);
        final Matrix m = info.mTmpMatrix;
        m.set(Matrix.IDENTITY_MATRIX);
        transformMatrixToGlobal(m);
        ev.transform(m);
        return true;
    }
@@ -15954,54 +15956,60 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
            return false;
        }
        ev.offsetLocation(-info.mWindowLeft, -info.mWindowTop);
        transformMotionEventToLocal(ev);
        final Matrix m = info.mTmpMatrix;
        m.set(Matrix.IDENTITY_MATRIX);
        transformMatrixToLocal(m);
        ev.transform(m);
        return true;
    }
    /**
     * Recursive helper method that applies transformations in post-order.
     * Modifies the input matrix such that it maps view-local coordinates to
     * on-screen coordinates.
     *
     * @param ev the on-screen motion event
     * @param m input matrix to modify
     */
    private void transformMotionEventToLocal(MotionEvent ev) {
    void transformMatrixToGlobal(Matrix m) {
        final ViewParent parent = mParent;
        if (parent instanceof View) {
            final View vp = (View) parent;
            vp.transformMotionEventToLocal(ev);
            ev.offsetLocation(vp.mScrollX, vp.mScrollY);
            vp.transformMatrixToGlobal(m);
            m.postTranslate(-vp.mScrollX, -vp.mScrollY);
        } else if (parent instanceof ViewRootImpl) {
            final ViewRootImpl vr = (ViewRootImpl) parent;
            ev.offsetLocation(0, vr.mCurScrollY);
            vr.transformMatrixToGlobal(m);
            m.postTranslate(0, -vr.mCurScrollY);
        }
        ev.offsetLocation(-mLeft, -mTop);
        m.postTranslate(mLeft, mTop);
        if (!hasIdentityMatrix()) {
            ev.transform(getInverseMatrix());
            m.postConcat(getMatrix());
        }
    }
    /**
     * Recursive helper method that applies transformations in pre-order.
     * Modifies the input matrix such that it maps on-screen coordinates to
     * view-local coordinates.
     *
     * @param ev the on-screen motion event
     * @param m input matrix to modify
     */
    private void transformMotionEventToGlobal(MotionEvent ev) {
        if (!hasIdentityMatrix()) {
            ev.transform(getMatrix());
        }
        ev.offsetLocation(mLeft, mTop);
    void transformMatrixToLocal(Matrix m) {
        final ViewParent parent = mParent;
        if (parent instanceof View) {
            final View vp = (View) parent;
            ev.offsetLocation(-vp.mScrollX, -vp.mScrollY);
            vp.transformMotionEventToGlobal(ev);
            vp.transformMatrixToLocal(m);
            m.preTranslate(vp.mScrollX, vp.mScrollY);
        } else if (parent instanceof ViewRootImpl) {
            final ViewRootImpl vr = (ViewRootImpl) parent;
            ev.offsetLocation(0, -vr.mCurScrollY);
            vr.transformMatrixToLocal(m);
            m.preTranslate(0, vr.mCurScrollY);
        }
        m.preTranslate(-mLeft, -mTop);
        if (!hasIdentityMatrix()) {
            m.preConcat(getInverseMatrix());
        }
    }
+23 −0
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import android.content.res.CompatibilityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Point;
@@ -1124,6 +1125,28 @@ public final class ViewRootImpl implements ViewParent,
        return windowSizeMayChange;
    }

    /**
     * Modifies the input matrix such that it maps view-local coordinates to
     * on-screen coordinates.
     *
     * @param m input matrix to modify
     */
    void transformMatrixToGlobal(Matrix m) {
        final View.AttachInfo attachInfo = mAttachInfo;
        m.postTranslate(attachInfo.mWindowLeft, attachInfo.mWindowTop);
    }

    /**
     * Modifies the input matrix such that it maps on-screen coordinates to
     * view-local coordinates.
     *
     * @param m input matrix to modify
     */
    void transformMatrixToLocal(Matrix m) {
        final View.AttachInfo attachInfo = mAttachInfo;
        m.preTranslate(-attachInfo.mWindowLeft, -attachInfo.mWindowTop);
    }

    private void performTraversals() {
        // cache mView since it is used so much below...
        final View host = mView;