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