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