blob: ec730e3b8ae8324418c69b89a94e24705c3219d3 [file] [log] [blame]
Mike Reedd5a80a52009-11-12 12:49:34 -05001
2
3package com.android.browser;
4
5import android.graphics.Bitmap;
6import android.graphics.utils.BoundaryPatch;
7import android.graphics.Canvas;
8import android.graphics.Paint;
9import android.webkit.WebView;
10
11/*package*/ class MeshTracker extends WebView.DragTracker {
12
13 private static class Mesh {
Mike Reed2290d212010-01-08 10:31:53 -050014 private int mWhich;
Mike Reedd5a80a52009-11-12 12:49:34 -050015 private int mRows;
16 private int mCols;
17 private BoundaryPatch mPatch = new BoundaryPatch();
18 private float[] mCubics = new float[24];
19 private float[] mOrig = new float[24];
20 private float mStretchX, mStretchY;
21
Mike Reed2290d212010-01-08 10:31:53 -050022 Mesh(int which, int rows, int cols) {
23 mWhich = which;
Mike Reedd5a80a52009-11-12 12:49:34 -050024 mRows = rows;
25 mCols = cols;
26 }
27
28 private void rebuildPatch() {
29 mPatch.setCubicBoundary(mCubics, 0, mRows, mCols);
30 }
31
32 private void setSize(float w, float h) {
33 float[] pts = mCubics;
34 float x1 = w*0.3333f;
35 float y1 = h*0.3333f;
36 float x2 = w*0.6667f;
37 float y2 = h*0.6667f;
38 pts[0*2+0] = 0; pts[0*2+1] = 0;
39 pts[1*2+0] = x1; pts[1*2+1] = 0;
40 pts[2*2+0] = x2; pts[2*2+1] = 0;
41
42 pts[3*2+0] = w; pts[3*2+1] = 0;
43 pts[4*2+0] = w; pts[4*2+1] = y1;
44 pts[5*2+0] = w; pts[5*2+1] = y2;
45
46 pts[6*2+0] = w; pts[6*2+1] = h;
47 pts[7*2+0] = x2; pts[7*2+1] = h;
48 pts[8*2+0] = x1; pts[8*2+1] = h;
49
50 pts[9*2+0] = 0; pts[9*2+1] = h;
51 pts[10*2+0] = 0; pts[10*2+1] = y2;
52 pts[11*2+0] = 0; pts[11*2+1] = y1;
53
54 System.arraycopy(pts, 0, mOrig, 0, 24);
55
56 // recall our stretcher
57 setStretch(mStretchX, mStretchY);
58 }
59
60 public void setBitmap(Bitmap bm) {
61 mPatch.setTexture(bm);
62 setSize(bm.getWidth(), bm.getHeight());
63 }
64
65 // first experimental behavior
Mike Reed2290d212010-01-08 10:31:53 -050066 private void doit1(float dx, float dy) {
Mike Reedd5a80a52009-11-12 12:49:34 -050067 int index;
68
69 if (dx < 0) {
70 index = 10;
71 } else {
72 index = 4;
73 }
74 mCubics[index*2 + 0] = mOrig[index*2 + 0] + dx;
75 mCubics[index*2 + 2] = mOrig[index*2 + 2] + dx;
76
77 if (dy < 0) {
78 index = 1;
79 } else {
80 index = 7;
81 }
82 mCubics[index*2 + 1] = mOrig[index*2 + 1] + dy;
83 mCubics[index*2 + 3] = mOrig[index*2 + 3] + dy;
84 }
85
Mike Reed2290d212010-01-08 10:31:53 -050086 private void doit2(float dx, float dy) {
Mike Reedd5a80a52009-11-12 12:49:34 -050087 int index;
88
89 if (dx < 0) {
90 index = 4;
91 } else {
92 index = 10;
93 }
94 mCubics[index*2 + 0] = mOrig[index*2 + 0] + dx;
95 mCubics[index*2 + 2] = mOrig[index*2 + 2] + dx;
96
97 if (dy < 0) {
98 index = 7;
99 } else {
100 index = 1;
101 }
102 mCubics[index*2 + 1] = mOrig[index*2 + 1] + dy;
103 mCubics[index*2 + 3] = mOrig[index*2 + 3] + dy;
104 }
105
106 public void setStretch(float dx, float dy) {
107 mStretchX = dx;
108 mStretchY = dy;
Mike Reed2290d212010-01-08 10:31:53 -0500109 switch (mWhich) {
110 case 1:
111 doit1(dx, dy);
112 break;
113 case 2:
114 doit2(dx, dy);
115 break;
116 }
Mike Reedd5a80a52009-11-12 12:49:34 -0500117 rebuildPatch();
118 }
119
120 public void draw(Canvas canvas) {
121 mPatch.draw(canvas);
122 }
123 }
124
125 private Mesh mMesh;
126 private Bitmap mBitmap;
Mike Reed2290d212010-01-08 10:31:53 -0500127 private int mWhich;
Mike Reedd5a80a52009-11-12 12:49:34 -0500128
Mike Reed2290d212010-01-08 10:31:53 -0500129 public MeshTracker(int which) {
130 mWhich = which;
131 }
Mike Reedd5a80a52009-11-12 12:49:34 -0500132
133 @Override public void onStartDrag(float x, float y) {
Mike Reed2290d212010-01-08 10:31:53 -0500134 mMesh = new Mesh(mWhich, 16, 16);
Mike Reedd5a80a52009-11-12 12:49:34 -0500135 }
136
137 @Override public void onBitmapChange(Bitmap bm) {
138 mBitmap = bm;
139 mMesh.setBitmap(bm);
140 }
141
142 @Override public boolean onStretchChange(float sx, float sy) {
143 mMesh.setStretch(-sx, -sy);
144 return true;
145 }
146
147 @Override public void onStopDrag() {
148 mMesh = null;
149 }
150
151 @Override public void onDraw(Canvas canvas) {
152 canvas.drawColor(0xFF000000);
153 Paint paint = new Paint();
154 paint.setAlpha(0x80);
155 canvas.drawBitmap(mBitmap, 0, 0, paint);
156 mMesh.draw(canvas);
157 }
158}
159