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