summaryrefslogtreecommitdiffstats
path: root/src/com/android/browser/MeshTracker.java
blob: a02f451bc12f5bf6c803f30f288792d830f08db8 (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


package com.android.browser;

import android.graphics.Bitmap;
import android.graphics.utils.BoundaryPatch;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.webkit.WebView;

/*package*/ class MeshTracker extends WebView.DragTracker {

    private static class Mesh {
        private int mRows;
        private int mCols;
        private BoundaryPatch mPatch = new BoundaryPatch();
        private float[] mCubics = new float[24];
        private float[] mOrig = new float[24];
        private float mStretchX, mStretchY;

        Mesh(int rows, int cols) {
            mRows = rows;
            mCols = cols;
        }

        private void rebuildPatch() {
            mPatch.setCubicBoundary(mCubics, 0, mRows, mCols);
        }

        private void setSize(float w, float h) {
            float[] pts = mCubics;
            float x1 = w*0.3333f;
            float y1 = h*0.3333f;
            float x2 = w*0.6667f;
            float y2 = h*0.6667f;
            pts[0*2+0] = 0;  pts[0*2+1] = 0;
            pts[1*2+0] = x1; pts[1*2+1] = 0;
            pts[2*2+0] = x2; pts[2*2+1] = 0;

            pts[3*2+0] = w; pts[3*2+1] = 0;
            pts[4*2+0] = w; pts[4*2+1] = y1;
            pts[5*2+0] = w; pts[5*2+1] = y2;

            pts[6*2+0] = w; pts[6*2+1] = h;
            pts[7*2+0] = x2; pts[7*2+1] = h;
            pts[8*2+0] = x1; pts[8*2+1] = h;

            pts[9*2+0] = 0;  pts[9*2+1] = h;
            pts[10*2+0] = 0; pts[10*2+1] = y2;
            pts[11*2+0] = 0; pts[11*2+1] = y1;

            System.arraycopy(pts, 0, mOrig, 0, 24);

            // recall our stretcher
            setStretch(mStretchX, mStretchY);
        }

        public void setBitmap(Bitmap bm) {
            mPatch.setTexture(bm);
            setSize(bm.getWidth(), bm.getHeight());
        }

        // first experimental behavior
        private void doit0(float dx, float dy) {
            int index;

            if (dx < 0) {
                index = 10;
            } else {
                index = 4;
            }
            mCubics[index*2 + 0] = mOrig[index*2 + 0] + dx;
            mCubics[index*2 + 2] = mOrig[index*2 + 2] + dx;

            if (dy < 0) {
                index = 1;
            } else {
                index = 7;
            }
            mCubics[index*2 + 1] = mOrig[index*2 + 1] + dy;
            mCubics[index*2 + 3] = mOrig[index*2 + 3] + dy;
        }

        private void doit1(float dx, float dy) {
            int index;

            if (dx < 0) {
                index = 4;
            } else {
                index = 10;
            }
            mCubics[index*2 + 0] = mOrig[index*2 + 0] + dx;
            mCubics[index*2 + 2] = mOrig[index*2 + 2] + dx;

            if (dy < 0) {
                index = 7;
            } else {
                index = 1;
            }
            mCubics[index*2 + 1] = mOrig[index*2 + 1] + dy;
            mCubics[index*2 + 3] = mOrig[index*2 + 3] + dy;
        }

        public void setStretch(float dx, float dy) {
            mStretchX = dx;
            mStretchY = dy;
            doit1(dx, dy);
            rebuildPatch();
        }

        public void draw(Canvas canvas) {
            mPatch.draw(canvas);
        }
    }

    private Mesh mMesh;
    private Bitmap mBitmap;

    public MeshTracker() {}

    @Override public void onStartDrag(float x, float y) {
        mMesh = new Mesh(16, 16);
    }

    @Override public void onBitmapChange(Bitmap bm) {
        mBitmap = bm;
        mMesh.setBitmap(bm);
    }

    @Override public boolean onStretchChange(float sx, float sy) {
        mMesh.setStretch(-sx, -sy);
        return true;
    }

    @Override public void onStopDrag() {
        mMesh = null;
    }

    @Override public void onDraw(Canvas canvas) {
        canvas.drawColor(0xFF000000);
        Paint paint = new Paint();
        paint.setAlpha(0x80);
        canvas.drawBitmap(mBitmap, 0, 0, paint);
        mMesh.draw(canvas);
    }
}