Mike Reed | d5a80a5 | 2009-11-12 12:49:34 -0500 | [diff] [blame] | 1 | |
| 2 | |
| 3 | package com.android.browser; |
| 4 | |
| 5 | import android.graphics.Bitmap; |
| 6 | import android.graphics.utils.BoundaryPatch; |
| 7 | import android.graphics.Canvas; |
| 8 | import android.graphics.Paint; |
| 9 | import android.webkit.WebView; |
| 10 | |
| 11 | /*package*/ class MeshTracker extends WebView.DragTracker { |
| 12 | |
| 13 | private static class Mesh { |
| 14 | private int mRows; |
| 15 | private int mCols; |
| 16 | private BoundaryPatch mPatch = new BoundaryPatch(); |
| 17 | private float[] mCubics = new float[24]; |
| 18 | private float[] mOrig = new float[24]; |
| 19 | private float mStretchX, mStretchY; |
| 20 | |
| 21 | Mesh(int rows, int cols) { |
| 22 | mRows = rows; |
| 23 | mCols = cols; |
| 24 | } |
| 25 | |
| 26 | private void rebuildPatch() { |
| 27 | mPatch.setCubicBoundary(mCubics, 0, mRows, mCols); |
| 28 | } |
| 29 | |
| 30 | private void setSize(float w, float h) { |
| 31 | float[] pts = mCubics; |
| 32 | float x1 = w*0.3333f; |
| 33 | float y1 = h*0.3333f; |
| 34 | float x2 = w*0.6667f; |
| 35 | float y2 = h*0.6667f; |
| 36 | pts[0*2+0] = 0; pts[0*2+1] = 0; |
| 37 | pts[1*2+0] = x1; pts[1*2+1] = 0; |
| 38 | pts[2*2+0] = x2; pts[2*2+1] = 0; |
| 39 | |
| 40 | pts[3*2+0] = w; pts[3*2+1] = 0; |
| 41 | pts[4*2+0] = w; pts[4*2+1] = y1; |
| 42 | pts[5*2+0] = w; pts[5*2+1] = y2; |
| 43 | |
| 44 | pts[6*2+0] = w; pts[6*2+1] = h; |
| 45 | pts[7*2+0] = x2; pts[7*2+1] = h; |
| 46 | pts[8*2+0] = x1; pts[8*2+1] = h; |
| 47 | |
| 48 | pts[9*2+0] = 0; pts[9*2+1] = h; |
| 49 | pts[10*2+0] = 0; pts[10*2+1] = y2; |
| 50 | pts[11*2+0] = 0; pts[11*2+1] = y1; |
| 51 | |
| 52 | System.arraycopy(pts, 0, mOrig, 0, 24); |
| 53 | |
| 54 | // recall our stretcher |
| 55 | setStretch(mStretchX, mStretchY); |
| 56 | } |
| 57 | |
| 58 | public void setBitmap(Bitmap bm) { |
| 59 | mPatch.setTexture(bm); |
| 60 | setSize(bm.getWidth(), bm.getHeight()); |
| 61 | } |
| 62 | |
| 63 | // first experimental behavior |
| 64 | private void doit0(float dx, float dy) { |
| 65 | int index; |
| 66 | |
| 67 | if (dx < 0) { |
| 68 | index = 10; |
| 69 | } else { |
| 70 | index = 4; |
| 71 | } |
| 72 | mCubics[index*2 + 0] = mOrig[index*2 + 0] + dx; |
| 73 | mCubics[index*2 + 2] = mOrig[index*2 + 2] + dx; |
| 74 | |
| 75 | if (dy < 0) { |
| 76 | index = 1; |
| 77 | } else { |
| 78 | index = 7; |
| 79 | } |
| 80 | mCubics[index*2 + 1] = mOrig[index*2 + 1] + dy; |
| 81 | mCubics[index*2 + 3] = mOrig[index*2 + 3] + dy; |
| 82 | } |
| 83 | |
| 84 | private void doit1(float dx, float dy) { |
| 85 | int index; |
| 86 | |
| 87 | if (dx < 0) { |
| 88 | index = 4; |
| 89 | } else { |
| 90 | index = 10; |
| 91 | } |
| 92 | mCubics[index*2 + 0] = mOrig[index*2 + 0] + dx; |
| 93 | mCubics[index*2 + 2] = mOrig[index*2 + 2] + dx; |
| 94 | |
| 95 | if (dy < 0) { |
| 96 | index = 7; |
| 97 | } else { |
| 98 | index = 1; |
| 99 | } |
| 100 | mCubics[index*2 + 1] = mOrig[index*2 + 1] + dy; |
| 101 | mCubics[index*2 + 3] = mOrig[index*2 + 3] + dy; |
| 102 | } |
| 103 | |
| 104 | public void setStretch(float dx, float dy) { |
| 105 | mStretchX = dx; |
| 106 | mStretchY = dy; |
| 107 | doit1(dx, dy); |
| 108 | rebuildPatch(); |
| 109 | } |
| 110 | |
| 111 | public void draw(Canvas canvas) { |
| 112 | mPatch.draw(canvas); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | private Mesh mMesh; |
| 117 | private Bitmap mBitmap; |
| 118 | |
| 119 | public MeshTracker() {} |
| 120 | |
| 121 | @Override public void onStartDrag(float x, float y) { |
| 122 | mMesh = new Mesh(16, 16); |
| 123 | } |
| 124 | |
| 125 | @Override public void onBitmapChange(Bitmap bm) { |
| 126 | mBitmap = bm; |
| 127 | mMesh.setBitmap(bm); |
| 128 | } |
| 129 | |
| 130 | @Override public boolean onStretchChange(float sx, float sy) { |
| 131 | mMesh.setStretch(-sx, -sy); |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | @Override public void onStopDrag() { |
| 136 | mMesh = null; |
| 137 | } |
| 138 | |
| 139 | @Override public void onDraw(Canvas canvas) { |
| 140 | canvas.drawColor(0xFF000000); |
| 141 | Paint paint = new Paint(); |
| 142 | paint.setAlpha(0x80); |
| 143 | canvas.drawBitmap(mBitmap, 0, 0, paint); |
| 144 | mMesh.draw(canvas); |
| 145 | } |
| 146 | } |
| 147 | |