epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkView.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkGradientShader.h" |
| 12 | #include "SkGraphics.h" |
| 13 | #include "SkImageDecoder.h" |
| 14 | #include "SkPath.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | #include "SkRandom.h" |
| 16 | #include "SkRegion.h" |
| 17 | #include "SkShader.h" |
| 18 | #include "SkUtils.h" |
| 19 | #include "SkXfermode.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | #include "SkColorPriv.h" |
| 21 | #include "SkColorFilter.h" |
| 22 | #include "SkTime.h" |
| 23 | #include "SkTypeface.h" |
| 24 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | #include "SkOSFile.h" |
| 26 | #include "SkStream.h" |
| 27 | |
| 28 | #include "SkGeometry.h" // private include :( |
| 29 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 30 | static SkShader* make_shader0(SkIPoint* size) { |
| 31 | SkBitmap bm; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 32 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | // SkImageDecoder::DecodeFile("/skimages/progressivejpg.jpg", &bm); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 34 | SkImageDecoder::DecodeFile("/skimages/logo.png", &bm); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 35 | size->set(bm.width(), bm.height()); |
| 36 | return SkShader::CreateBitmapShader(bm, SkShader::kClamp_TileMode, |
| 37 | SkShader::kClamp_TileMode); |
| 38 | } |
| 39 | |
| 40 | static SkShader* make_shader1(const SkIPoint& size) { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 41 | SkPoint pts[] = { { 0, 0, }, |
| 42 | { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED }; |
| 44 | return SkGradientShader::CreateLinear(pts, colors, NULL, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 45 | SK_ARRAY_COUNT(colors), SkShader::kMirror_TileMode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | /////////////////////////////////////////////////////////////////////////////// |
| 49 | |
| 50 | class Patch { |
| 51 | public: |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 52 | Patch() { sk_bzero(fPts, sizeof(fPts)); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | ~Patch() {} |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 54 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | void setPatch(const SkPoint pts[12]) { |
| 56 | memcpy(fPts, pts, 12 * sizeof(SkPoint)); |
| 57 | fPts[12] = pts[0]; // the last shall be first |
| 58 | } |
| 59 | void setBounds(int w, int h) { fW = w; fH = h; } |
| 60 | |
| 61 | void draw(SkCanvas*, const SkPaint&, int segsU, int segsV, |
| 62 | bool doTextures, bool doColors); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 63 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | private: |
| 65 | SkPoint fPts[13]; |
| 66 | int fW, fH; |
| 67 | }; |
| 68 | |
| 69 | static void eval_patch_edge(const SkPoint cubic[], SkPoint samples[], int segs) { |
| 70 | SkScalar t = 0; |
| 71 | SkScalar dt = SK_Scalar1 / segs; |
| 72 | |
| 73 | samples[0] = cubic[0]; |
| 74 | for (int i = 1; i < segs; i++) { |
| 75 | t += dt; |
| 76 | SkEvalCubicAt(cubic, t, &samples[i], NULL, NULL); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | static void eval_sheet(const SkPoint edge[], int nu, int nv, int iu, int iv, |
| 81 | SkPoint* pt) { |
| 82 | const int TL = 0; |
| 83 | const int TR = nu; |
| 84 | const int BR = TR + nv; |
| 85 | const int BL = BR + nu; |
| 86 | |
| 87 | SkScalar u = SkIntToScalar(iu) / nu; |
| 88 | SkScalar v = SkIntToScalar(iv) / nv; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 89 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | SkScalar uv = SkScalarMul(u, v); |
| 91 | SkScalar Uv = SkScalarMul(SK_Scalar1 - u, v); |
| 92 | SkScalar uV = SkScalarMul(u, SK_Scalar1 - v); |
| 93 | SkScalar UV = SkScalarMul(SK_Scalar1 - u, SK_Scalar1 - v); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 94 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 95 | SkScalar x0 = SkScalarMul(UV, edge[TL].fX) + SkScalarMul(uV, edge[TR].fX) + |
| 96 | SkScalarMul(Uv, edge[BL].fX) + SkScalarMul(uv, edge[BR].fX); |
| 97 | SkScalar y0 = SkScalarMul(UV, edge[TL].fY) + SkScalarMul(uV, edge[TR].fY) + |
| 98 | SkScalarMul(Uv, edge[BL].fY) + SkScalarMul(uv, edge[BR].fY); |
| 99 | |
| 100 | SkScalar x = SkScalarMul(SK_Scalar1 - v, edge[TL+iu].fX) + |
| 101 | SkScalarMul(u, edge[TR+iv].fX) + |
| 102 | SkScalarMul(v, edge[BR+nu-iu].fX) + |
| 103 | SkScalarMul(SK_Scalar1 - u, edge[BL+nv-iv].fX) - x0; |
| 104 | SkScalar y = SkScalarMul(SK_Scalar1 - v, edge[TL+iu].fY) + |
| 105 | SkScalarMul(u, edge[TR+iv].fY) + |
| 106 | SkScalarMul(v, edge[BR+nu-iu].fY) + |
| 107 | SkScalarMul(SK_Scalar1 - u, edge[BL+nv-iv].fY) - y0; |
| 108 | pt->set(x, y); |
| 109 | } |
| 110 | |
| 111 | static int ScalarTo255(SkScalar v) { |
| 112 | int scale = SkScalarToFixed(v) >> 8; |
| 113 | if (scale < 0) { |
| 114 | scale = 0; |
| 115 | } else if (scale > 255) { |
| 116 | scale = 255; |
| 117 | } |
| 118 | return scale; |
| 119 | } |
| 120 | |
| 121 | static SkColor make_color(SkScalar s, SkScalar t) { |
| 122 | int cs = ScalarTo255(s); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 123 | int ct = ScalarTo255(t); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 124 | return SkColorSetARGB(0xFF, cs, 0, 0) + SkColorSetARGB(0, 0, ct, 0); |
| 125 | } |
| 126 | |
| 127 | void Patch::draw(SkCanvas* canvas, const SkPaint& paint, int nu, int nv, |
| 128 | bool doTextures, bool doColors) { |
| 129 | if (nu < 1 || nv < 1) { |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | int i, npts = (nu + nv) * 2; |
| 134 | SkAutoSTMalloc<16, SkPoint> storage(npts + 1); |
| 135 | SkPoint* edge0 = storage.get(); |
| 136 | SkPoint* edge1 = edge0 + nu; |
| 137 | SkPoint* edge2 = edge1 + nv; |
| 138 | SkPoint* edge3 = edge2 + nu; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 139 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 140 | // evaluate the edge points |
| 141 | eval_patch_edge(fPts + 0, edge0, nu); |
| 142 | eval_patch_edge(fPts + 3, edge1, nv); |
| 143 | eval_patch_edge(fPts + 6, edge2, nu); |
| 144 | eval_patch_edge(fPts + 9, edge3, nv); |
| 145 | edge3[nv] = edge0[0]; // the last shall be first |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 146 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 147 | for (i = 0; i < npts; i++) { |
| 148 | // canvas->drawLine(edge0[i].fX, edge0[i].fY, edge0[i+1].fX, edge0[i+1].fY, paint); |
| 149 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 150 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | int row, vertCount = (nu + 1) * (nv + 1); |
| 152 | SkAutoTMalloc<SkPoint> vertStorage(vertCount); |
| 153 | SkPoint* verts = vertStorage.get(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 154 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 155 | // first row |
| 156 | memcpy(verts, edge0, (nu + 1) * sizeof(SkPoint)); |
| 157 | // rows |
| 158 | SkPoint* r = verts; |
| 159 | for (row = 1; row < nv; row++) { |
| 160 | r += nu + 1; |
| 161 | r[0] = edge3[nv - row]; |
| 162 | for (int col = 1; col < nu; col++) { |
| 163 | eval_sheet(edge0, nu, nv, col, row, &r[col]); |
| 164 | } |
| 165 | r[nu] = edge1[row]; |
| 166 | } |
| 167 | // last row |
| 168 | SkPoint* last = verts + nv * (nu + 1); |
| 169 | for (i = 0; i <= nu; i++) { |
| 170 | last[i] = edge2[nu - i]; |
| 171 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 172 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 173 | // canvas->drawPoints(verts, vertCount, paint); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 174 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 175 | int stripCount = (nu + 1) * 2; |
| 176 | SkAutoTMalloc<SkPoint> stripStorage(stripCount * 2); |
| 177 | SkAutoTMalloc<SkColor> colorStorage(stripCount); |
| 178 | SkPoint* strip = stripStorage.get(); |
| 179 | SkPoint* tex = strip + stripCount; |
| 180 | SkColor* colors = colorStorage.get(); |
| 181 | SkScalar t = 0; |
| 182 | const SkScalar ds = SK_Scalar1 * fW / nu; |
| 183 | const SkScalar dt = SK_Scalar1 * fH / nv; |
| 184 | r = verts; |
| 185 | for (row = 0; row < nv; row++) { |
| 186 | SkPoint* upper = r; |
| 187 | SkPoint* lower = r + nu + 1; |
| 188 | r = lower; |
| 189 | SkScalar s = 0; |
| 190 | for (i = 0; i <= nu; i++) { |
| 191 | strip[i*2 + 0] = *upper++; |
| 192 | strip[i*2 + 1] = *lower++; |
| 193 | tex[i*2 + 0].set(s, t); |
| 194 | tex[i*2 + 1].set(s, t + dt); |
| 195 | colors[i*2 + 0] = make_color(s/fW, t/fH); |
| 196 | colors[i*2 + 1] = make_color(s/fW, (t + dt)/fH); |
| 197 | s += ds; |
| 198 | } |
| 199 | t += dt; |
| 200 | canvas->drawVertices(SkCanvas::kTriangleStrip_VertexMode, stripCount, |
| 201 | strip, doTextures ? tex : NULL, |
| 202 | doColors ? colors : NULL, NULL, |
| 203 | NULL, 0, paint); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | static void drawpatches(SkCanvas* canvas, const SkPaint& paint, int nu, int nv, |
| 208 | Patch* patch) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 209 | SkAutoCanvasRestore ar(canvas, true); |
| 210 | |
reed | cdf2db9 | 2014-08-06 09:54:19 -0700 | [diff] [blame] | 211 | patch->draw(canvas, paint, nu, nv, false, false); |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 212 | canvas->translate(SkIntToScalar(180), 0); |
reed | cdf2db9 | 2014-08-06 09:54:19 -0700 | [diff] [blame] | 213 | patch->draw(canvas, paint, nu, nv, true, false); |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 214 | canvas->translate(SkIntToScalar(180), 0); |
reed | cdf2db9 | 2014-08-06 09:54:19 -0700 | [diff] [blame] | 215 | patch->draw(canvas, paint, nu, nv, false, true); |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 216 | canvas->translate(SkIntToScalar(180), 0); |
reed | cdf2db9 | 2014-08-06 09:54:19 -0700 | [diff] [blame] | 217 | patch->draw(canvas, paint, nu, nv, true, true); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 218 | } |
| 219 | |
reed | 147476d | 2014-08-06 07:00:27 -0700 | [diff] [blame] | 220 | const SkScalar DX = 20; |
| 221 | const SkScalar DY = 0; |
| 222 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 223 | class PatchView : public SampleView { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 224 | SkShader* fShader0; |
| 225 | SkShader* fShader1; |
| 226 | SkIPoint fSize0, fSize1; |
| 227 | SkPoint fPts[12]; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 228 | |
| 229 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 230 | PatchView() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 231 | fShader0 = make_shader0(&fSize0); |
| 232 | fSize1 = fSize0; |
| 233 | if (fSize0.fX == 0 || fSize0.fY == 0) { |
| 234 | fSize1.set(2, 2); |
| 235 | } |
| 236 | fShader1 = make_shader1(fSize1); |
| 237 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 238 | const SkScalar S = SkIntToScalar(50); |
| 239 | const SkScalar T = SkIntToScalar(40); |
| 240 | fPts[0].set(S*0, T); |
| 241 | fPts[1].set(S*1, T); |
| 242 | fPts[2].set(S*2, T); |
| 243 | fPts[3].set(S*3, T); |
| 244 | fPts[4].set(S*3, T*2); |
| 245 | fPts[5].set(S*3, T*3); |
| 246 | fPts[6].set(S*3, T*4); |
| 247 | fPts[7].set(S*2, T*4); |
| 248 | fPts[8].set(S*1, T*4); |
| 249 | fPts[9].set(S*0, T*4); |
| 250 | fPts[10].set(S*0, T*3); |
| 251 | fPts[11].set(S*0, T*2); |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 252 | |
| 253 | this->setBGColor(SK_ColorGRAY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 254 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 255 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 256 | virtual ~PatchView() { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 257 | SkSafeUnref(fShader0); |
| 258 | SkSafeUnref(fShader1); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 259 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 260 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 261 | protected: |
| 262 | // overrides from SkEventSink |
| 263 | virtual bool onQuery(SkEvent* evt) { |
tfarina@chromium.org | 45369a3 | 2012-09-30 11:30:01 +0000 | [diff] [blame] | 264 | if (SampleCode::TitleQ(*evt)) { |
| 265 | SampleCode::TitleR(evt, "Patch"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 266 | return true; |
| 267 | } |
| 268 | return this->INHERITED::onQuery(evt); |
| 269 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 270 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 271 | virtual void onDrawContent(SkCanvas* canvas) { |
reed | cdf2db9 | 2014-08-06 09:54:19 -0700 | [diff] [blame] | 272 | const int nu = 10; |
| 273 | const int nv = 10; |
| 274 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 275 | SkPaint paint; |
| 276 | paint.setDither(true); |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 277 | paint.setFilterLevel(SkPaint::kLow_FilterLevel); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 278 | |
reed | 147476d | 2014-08-06 07:00:27 -0700 | [diff] [blame] | 279 | canvas->translate(DX, DY); |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 280 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 281 | Patch patch; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 282 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 283 | paint.setShader(fShader0); |
| 284 | if (fSize0.fX == 0) { |
| 285 | fSize0.fX = 1; |
| 286 | } |
| 287 | if (fSize0.fY == 0) { |
| 288 | fSize0.fY = 1; |
| 289 | } |
| 290 | patch.setBounds(fSize0.fX, fSize0.fY); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 291 | |
| 292 | patch.setPatch(fPts); |
reed | cdf2db9 | 2014-08-06 09:54:19 -0700 | [diff] [blame] | 293 | drawpatches(canvas, paint, nu, nv, &patch); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 294 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 295 | paint.setShader(NULL); |
| 296 | paint.setAntiAlias(true); |
| 297 | paint.setStrokeWidth(SkIntToScalar(5)); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 298 | canvas->drawPoints(SkCanvas::kPoints_PointMode, SK_ARRAY_COUNT(fPts), fPts, paint); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 299 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 300 | canvas->translate(0, SkIntToScalar(300)); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 301 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 302 | paint.setAntiAlias(false); |
| 303 | paint.setShader(fShader1); |
reed | cdf2db9 | 2014-08-06 09:54:19 -0700 | [diff] [blame] | 304 | if (true) { |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 305 | SkMatrix m; |
| 306 | m.setSkew(1, 0); |
| 307 | SkShader* s = SkShader::CreateLocalMatrixShader(paint.getShader(), m); |
| 308 | paint.setShader(s)->unref(); |
| 309 | } |
reed | cdf2db9 | 2014-08-06 09:54:19 -0700 | [diff] [blame] | 310 | if (true) { |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 311 | static int gAngle; |
| 312 | SkMatrix m; |
| 313 | m.setRotate(SkIntToScalar(gAngle++)); |
| 314 | SkShader* s = SkShader::CreateLocalMatrixShader(paint.getShader(), m); |
| 315 | paint.setShader(s)->unref(); |
| 316 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 317 | patch.setBounds(fSize1.fX, fSize1.fY); |
reed | cdf2db9 | 2014-08-06 09:54:19 -0700 | [diff] [blame] | 318 | drawpatches(canvas, paint, nu, nv, &patch); |
skia.committer@gmail.com | b2c82c9 | 2014-05-08 03:05:29 +0000 | [diff] [blame] | 319 | |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 320 | this->inval(NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 321 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 322 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 323 | class PtClick : public Click { |
| 324 | public: |
| 325 | int fIndex; |
| 326 | PtClick(SkView* view, int index) : Click(view), fIndex(index) {} |
| 327 | }; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 328 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 329 | static bool hittest(const SkPoint& pt, SkScalar x, SkScalar y) { |
| 330 | return SkPoint::Length(pt.fX - x, pt.fY - y) < SkIntToScalar(5); |
| 331 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 332 | |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 333 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, |
| 334 | unsigned modi) SK_OVERRIDE { |
reed | 147476d | 2014-08-06 07:00:27 -0700 | [diff] [blame] | 335 | x -= DX; |
| 336 | y -= DY; |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 337 | for (size_t i = 0; i < SK_ARRAY_COUNT(fPts); i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 338 | if (hittest(fPts[i], x, y)) { |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 339 | return new PtClick(this, (int)i); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 340 | } |
| 341 | } |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 342 | return this->INHERITED::onFindClickHandler(x, y, modi); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 343 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 344 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 345 | virtual bool onClick(Click* click) { |
reed | 147476d | 2014-08-06 07:00:27 -0700 | [diff] [blame] | 346 | fPts[((PtClick*)click)->fIndex].set(click->fCurr.fX - DX, click->fCurr.fY - DY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 347 | this->inval(NULL); |
| 348 | return true; |
| 349 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 350 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 351 | private: |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 352 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 353 | }; |
| 354 | |
| 355 | ////////////////////////////////////////////////////////////////////////////// |
| 356 | |
| 357 | static SkView* MyFactory() { return new PatchView; } |
| 358 | static SkViewRegister reg(MyFactory); |