epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 9 | #include "SkAnimTimer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkView.h" |
| 11 | #include "SkCanvas.h" |
| 12 | #include "SkGradientShader.h" |
| 13 | #include "SkGraphics.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | #include "SkPath.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | #include "SkRegion.h" |
| 16 | #include "SkShader.h" |
| 17 | #include "SkUtils.h" |
| 18 | #include "SkXfermode.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | #include "SkColorPriv.h" |
| 20 | #include "SkColorFilter.h" |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 21 | #include "SkParsePath.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | #include "SkTime.h" |
| 23 | #include "SkTypeface.h" |
| 24 | |
reed@android.com | a964028 | 2009-08-28 20:06:54 +0000 | [diff] [blame] | 25 | #include "SkGeometry.h" |
| 26 | |
bungeman | 60e0fee | 2015-08-26 05:15:46 -0700 | [diff] [blame] | 27 | #include <stdlib.h> |
| 28 | |
reed@android.com | a964028 | 2009-08-28 20:06:54 +0000 | [diff] [blame] | 29 | // http://code.google.com/p/skia/issues/detail?id=32 |
| 30 | static void test_cubic() { |
| 31 | SkPoint src[4] = { |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 32 | { 556.25000f, 523.03003f }, |
| 33 | { 556.23999f, 522.96002f }, |
| 34 | { 556.21997f, 522.89001f }, |
| 35 | { 556.21997f, 522.82001f } |
reed@android.com | a964028 | 2009-08-28 20:06:54 +0000 | [diff] [blame] | 36 | }; |
| 37 | SkPoint dst[11]; |
| 38 | dst[10].set(42, -42); // one past the end, that we don't clobber these |
| 39 | SkScalar tval[] = { 0.33333334f, 0.99999994f }; |
| 40 | |
| 41 | SkChopCubicAt(src, dst, tval, 2); |
| 42 | |
| 43 | #if 0 |
| 44 | for (int i = 0; i < 11; i++) { |
| 45 | SkDebugf("--- %d [%g %g]\n", i, dst[i].fX, dst[i].fY); |
| 46 | } |
| 47 | #endif |
| 48 | } |
| 49 | |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 50 | static void test_cubic2() { |
| 51 | const char* str = "M2242 -590088L-377758 9.94099e+07L-377758 9.94099e+07L2242 -590088Z"; |
| 52 | SkPath path; |
| 53 | SkParsePath::FromSVGString(str, &path); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 54 | |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 55 | { |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 56 | SkRect r = path.getBounds(); |
| 57 | SkIRect ir; |
| 58 | r.round(&ir); |
bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 59 | SkDebugf("[%g %g %g %g] [%x %x %x %x]\n", |
| 60 | SkScalarToDouble(r.fLeft), SkScalarToDouble(r.fTop), |
| 61 | SkScalarToDouble(r.fRight), SkScalarToDouble(r.fBottom), |
| 62 | ir.fLeft, ir.fTop, ir.fRight, ir.fBottom); |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 63 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 64 | |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 65 | SkBitmap bitmap; |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 66 | bitmap.allocN32Pixels(300, 200); |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 67 | |
| 68 | SkCanvas canvas(bitmap); |
| 69 | SkPaint paint; |
| 70 | paint.setAntiAlias(true); |
| 71 | canvas.drawPath(path, paint); |
| 72 | } |
| 73 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 74 | class PathView : public SampleView { |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 75 | SkScalar fPrevSecs; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | public: |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 77 | SkScalar fDStroke, fStroke, fMinStroke, fMaxStroke; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | SkPath fPath[6]; |
| 79 | bool fShowHairline; |
reed@google.com | c9fa63c | 2012-03-12 21:14:09 +0000 | [diff] [blame] | 80 | bool fOnce; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 81 | |
| 82 | PathView() { |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 83 | fPrevSecs = 0; |
reed@google.com | c9fa63c | 2012-03-12 21:14:09 +0000 | [diff] [blame] | 84 | fOnce = false; |
| 85 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 86 | |
reed@google.com | c9fa63c | 2012-03-12 21:14:09 +0000 | [diff] [blame] | 87 | void init() { |
| 88 | if (fOnce) { |
| 89 | return; |
| 90 | } |
| 91 | fOnce = true; |
| 92 | |
reed@android.com | a964028 | 2009-08-28 20:06:54 +0000 | [diff] [blame] | 93 | test_cubic(); |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 94 | test_cubic2(); |
reed@android.com | a964028 | 2009-08-28 20:06:54 +0000 | [diff] [blame] | 95 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 96 | fShowHairline = false; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 97 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | fDStroke = 1; |
| 99 | fStroke = 10; |
| 100 | fMinStroke = 10; |
| 101 | fMaxStroke = 180; |
| 102 | |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 103 | const SkScalar V = 85; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 104 | |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 105 | fPath[0].moveTo(40, 70); |
| 106 | fPath[0].lineTo(70, 70 + SK_ScalarHalf); |
| 107 | fPath[0].lineTo(110, 70); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 108 | |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 109 | fPath[1].moveTo(40, 70); |
| 110 | fPath[1].lineTo(70, 70 - SK_ScalarHalf); |
| 111 | fPath[1].lineTo(110, 70); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 112 | |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 113 | fPath[2].moveTo(V, V); |
| 114 | fPath[2].lineTo(50, V); |
| 115 | fPath[2].lineTo(50, 50); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 116 | |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 117 | fPath[3].moveTo(50, 50); |
| 118 | fPath[3].lineTo(50, V); |
| 119 | fPath[3].lineTo(V, V); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 120 | |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 121 | fPath[4].moveTo(50, 50); |
| 122 | fPath[4].lineTo(50, V); |
| 123 | fPath[4].lineTo(52, 50); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 124 | |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 125 | fPath[5].moveTo(52, 50); |
| 126 | fPath[5].lineTo(50, V); |
| 127 | fPath[5].lineTo(50, 50); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 128 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 129 | this->setBGColor(0xFFDDDDDD); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 130 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 131 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 132 | protected: |
| 133 | // overrides from SkEventSink |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 134 | bool onQuery(SkEvent* evt) override { |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 135 | if (SampleCode::TitleQ(*evt)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 136 | SampleCode::TitleR(evt, "Paths"); |
| 137 | return true; |
| 138 | } |
| 139 | return this->INHERITED::onQuery(evt); |
| 140 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 141 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 142 | void drawPath(SkCanvas* canvas, const SkPath& path, SkPaint::Join j) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 143 | SkPaint paint; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 144 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 145 | paint.setAntiAlias(true); |
| 146 | paint.setStyle(SkPaint::kStroke_Style); |
| 147 | paint.setStrokeJoin(j); |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 148 | paint.setStrokeWidth(fStroke); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 149 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 150 | if (fShowHairline) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | SkPath fill; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 152 | |
| 153 | paint.getFillPath(path, &fill); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 154 | paint.setStrokeWidth(0); |
| 155 | canvas->drawPath(fill, paint); |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 156 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 157 | canvas->drawPath(path, paint); |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 158 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 159 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 160 | paint.setColor(SK_ColorRED); |
| 161 | paint.setStrokeWidth(0); |
| 162 | canvas->drawPath(path, paint); |
| 163 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 164 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 165 | void onDrawContent(SkCanvas* canvas) override { |
reed@google.com | c9fa63c | 2012-03-12 21:14:09 +0000 | [diff] [blame] | 166 | this->init(); |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 167 | canvas->translate(50, 50); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 168 | |
| 169 | static const SkPaint::Join gJoins[] = { |
| 170 | SkPaint::kBevel_Join, |
| 171 | SkPaint::kMiter_Join, |
| 172 | SkPaint::kRound_Join |
| 173 | }; |
| 174 | |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 175 | for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 176 | canvas->save(); |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 177 | for (size_t j = 0; j < SK_ARRAY_COUNT(fPath); j++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 178 | this->drawPath(canvas, fPath[j], gJoins[i]); |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 179 | canvas->translate(200, 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 180 | } |
| 181 | canvas->restore(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 182 | |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 183 | canvas->translate(0, 200); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 184 | } |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 185 | } |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 186 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 187 | bool onAnimate(const SkAnimTimer& timer) override { |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 188 | SkScalar currSecs = timer.scaled(100); |
| 189 | SkScalar delta = currSecs - fPrevSecs; |
| 190 | fPrevSecs = currSecs; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 191 | |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 192 | fStroke += fDStroke * delta; |
| 193 | if (fStroke > fMaxStroke || fStroke < fMinStroke) { |
| 194 | fDStroke = -fDStroke; |
| 195 | } |
| 196 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 197 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 198 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 199 | SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 200 | fShowHairline = !fShowHairline; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 201 | this->inval(nullptr); |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 202 | return this->INHERITED::onFindClickHandler(x, y, modi); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 203 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 204 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 205 | private: |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 206 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 207 | }; |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 208 | DEF_SAMPLE( return new PathView; ) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 209 | |
| 210 | ////////////////////////////////////////////////////////////////////////////// |
| 211 | |
reed | 8b57524 | 2014-12-17 01:47:32 -0800 | [diff] [blame] | 212 | #include "SkArcToPathEffect.h" |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 213 | #include "SkCornerPathEffect.h" |
| 214 | #include "SkRandom.h" |
| 215 | |
| 216 | class ArcToView : public SampleView { |
reed | 8b57524 | 2014-12-17 01:47:32 -0800 | [diff] [blame] | 217 | bool fDoFrame, fDoArcTo, fDoCorner, fDoConic; |
| 218 | SkPaint fPtsPaint, fArcToPaint, fSkeletonPaint, fCornerPaint; |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 219 | public: |
| 220 | enum { |
| 221 | N = 4 |
| 222 | }; |
| 223 | SkPoint fPts[N]; |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 224 | |
reed | 8b57524 | 2014-12-17 01:47:32 -0800 | [diff] [blame] | 225 | ArcToView() |
| 226 | : fDoFrame(false), fDoArcTo(false), fDoCorner(false), fDoConic(false) |
| 227 | { |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 228 | SkRandom rand; |
| 229 | for (int i = 0; i < N; ++i) { |
| 230 | fPts[i].fX = 20 + rand.nextUScalar1() * 640; |
| 231 | fPts[i].fY = 20 + rand.nextUScalar1() * 480; |
| 232 | } |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 233 | |
reed | 8b57524 | 2014-12-17 01:47:32 -0800 | [diff] [blame] | 234 | const SkScalar rad = 50; |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 235 | |
| 236 | fPtsPaint.setAntiAlias(true); |
| 237 | fPtsPaint.setStrokeWidth(15); |
| 238 | fPtsPaint.setStrokeCap(SkPaint::kRound_Cap); |
| 239 | |
reed | 8b57524 | 2014-12-17 01:47:32 -0800 | [diff] [blame] | 240 | fArcToPaint.setAntiAlias(true); |
| 241 | fArcToPaint.setStyle(SkPaint::kStroke_Style); |
| 242 | fArcToPaint.setStrokeWidth(9); |
| 243 | fArcToPaint.setColor(0x800000FF); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 244 | fArcToPaint.setPathEffect(SkArcToPathEffect::Make(rad)); |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 245 | |
| 246 | fCornerPaint.setAntiAlias(true); |
| 247 | fCornerPaint.setStyle(SkPaint::kStroke_Style); |
| 248 | fCornerPaint.setStrokeWidth(13); |
| 249 | fCornerPaint.setColor(SK_ColorGREEN); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 250 | fCornerPaint.setPathEffect(SkCornerPathEffect::Make(rad*2)); |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 251 | |
| 252 | fSkeletonPaint.setAntiAlias(true); |
| 253 | fSkeletonPaint.setStyle(SkPaint::kStroke_Style); |
| 254 | fSkeletonPaint.setColor(SK_ColorRED); |
| 255 | } |
| 256 | |
reed | 8b57524 | 2014-12-17 01:47:32 -0800 | [diff] [blame] | 257 | void toggle(bool& value) { |
| 258 | value = !value; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 259 | this->inval(nullptr); |
reed | 8b57524 | 2014-12-17 01:47:32 -0800 | [diff] [blame] | 260 | } |
| 261 | |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 262 | protected: |
| 263 | // overrides from SkEventSink |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 264 | bool onQuery(SkEvent* evt) override { |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 265 | if (SampleCode::TitleQ(*evt)) { |
| 266 | SampleCode::TitleR(evt, "ArcTo"); |
| 267 | return true; |
| 268 | } |
reed | 8b57524 | 2014-12-17 01:47:32 -0800 | [diff] [blame] | 269 | SkUnichar uni; |
| 270 | if (SampleCode::CharQ(*evt, &uni)) { |
| 271 | switch (uni) { |
| 272 | case '1': this->toggle(fDoFrame); return true; |
| 273 | case '2': this->toggle(fDoArcTo); return true; |
| 274 | case '3': this->toggle(fDoCorner); return true; |
| 275 | case '4': this->toggle(fDoConic); return true; |
| 276 | default: break; |
| 277 | } |
| 278 | } |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 279 | return this->INHERITED::onQuery(evt); |
| 280 | } |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 281 | |
reed | 8b57524 | 2014-12-17 01:47:32 -0800 | [diff] [blame] | 282 | void makePath(SkPath* path) { |
| 283 | path->moveTo(fPts[0]); |
| 284 | for (int i = 1; i < N; ++i) { |
| 285 | path->lineTo(fPts[i]); |
| 286 | } |
| 287 | if (!fDoFrame) { |
| 288 | path->close(); |
| 289 | } |
| 290 | } |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 291 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 292 | void onDrawContent(SkCanvas* canvas) override { |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 293 | canvas->drawPoints(SkCanvas::kPoints_PointMode, N, fPts, fPtsPaint); |
| 294 | |
| 295 | SkPath path; |
reed | 8b57524 | 2014-12-17 01:47:32 -0800 | [diff] [blame] | 296 | this->makePath(&path); |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 297 | |
reed | 8b57524 | 2014-12-17 01:47:32 -0800 | [diff] [blame] | 298 | if (fDoCorner) { |
| 299 | canvas->drawPath(path, fCornerPaint); |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 300 | } |
reed | 8b57524 | 2014-12-17 01:47:32 -0800 | [diff] [blame] | 301 | if (fDoArcTo) { |
| 302 | canvas->drawPath(path, fArcToPaint); |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 303 | } |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 304 | |
reed | 8b57524 | 2014-12-17 01:47:32 -0800 | [diff] [blame] | 305 | canvas->drawPath(path, fSkeletonPaint); |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 306 | } |
| 307 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 308 | bool onClick(Click* click) override { |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 309 | int32_t index; |
| 310 | if (click->fMeta.findS32("index", &index)) { |
| 311 | SkASSERT((unsigned)index < N); |
| 312 | fPts[index] = click->fCurr; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 313 | this->inval(nullptr); |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 314 | return true; |
| 315 | } |
| 316 | return false; |
| 317 | } |
| 318 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 319 | SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override { |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 320 | const SkScalar tol = 4; |
| 321 | const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2); |
| 322 | for (int i = 0; i < N; ++i) { |
| 323 | if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) { |
| 324 | Click* click = new Click(this); |
| 325 | click->fMeta.setS32("index", i); |
| 326 | return click; |
| 327 | } |
| 328 | } |
| 329 | return this->INHERITED::onFindClickHandler(x, y, modi); |
| 330 | } |
| 331 | |
| 332 | private: |
| 333 | typedef SampleView INHERITED; |
| 334 | }; |
| 335 | DEF_SAMPLE( return new ArcToView; ) |