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 | */ |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 7 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 9 | #include "SkAnimTimer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
Brian Osman | 8ceee43 | 2017-12-01 10:52:28 -0500 | [diff] [blame] | 11 | #include "SkColorFilter.h" |
| 12 | #include "SkColorPriv.h" |
| 13 | #include "SkCornerPathEffect.h" |
reed | 3cb3840 | 2015-02-06 08:36:15 -0800 | [diff] [blame] | 14 | #include "SkDrawable.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | #include "SkGradientShader.h" |
| 16 | #include "SkPath.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | #include "SkPathMeasure.h" |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 18 | #include "SkPictureRecorder.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | #include "SkRandom.h" |
Brian Osman | 8ceee43 | 2017-12-01 10:52:28 -0500 | [diff] [blame] | 20 | #include "SkRegion.h" |
| 21 | #include "SkShader.h" |
| 22 | #include "SkString.h" |
| 23 | #include "SkUtils.h" |
| 24 | #include "SkView.h" |
| 25 | #include "Sk1DPathEffect.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | |
reed@android.com | bbff1d5 | 2009-06-05 16:21:03 +0000 | [diff] [blame] | 27 | #include "SkParsePath.h" |
| 28 | static void testparse() { |
| 29 | SkRect r; |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 30 | r.set(0, 0, 10, 10.5f); |
reed@android.com | bbff1d5 | 2009-06-05 16:21:03 +0000 | [diff] [blame] | 31 | SkPath p, p2; |
| 32 | SkString str, str2; |
| 33 | |
| 34 | p.addRect(r); |
| 35 | SkParsePath::ToSVGString(p, &str); |
| 36 | SkParsePath::FromSVGString(str.c_str(), &p2); |
| 37 | SkParsePath::ToSVGString(p2, &str2); |
| 38 | } |
| 39 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 40 | class ArcsView : public SampleView { |
reed | 3cb3840 | 2015-02-06 08:36:15 -0800 | [diff] [blame] | 41 | class MyDrawable : public SkDrawable { |
reed | 6a070dc | 2014-11-11 19:36:09 -0800 | [diff] [blame] | 42 | SkRect fR; |
| 43 | SkScalar fSweep; |
| 44 | public: |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 45 | MyDrawable(const SkRect& r) : fR(r), fSweep(0) {} |
reed | 6a070dc | 2014-11-11 19:36:09 -0800 | [diff] [blame] | 46 | |
| 47 | void setSweep(SkScalar sweep) { |
| 48 | if (fSweep != sweep) { |
| 49 | fSweep = sweep; |
| 50 | this->notifyDrawingChanged(); |
| 51 | } |
| 52 | } |
| 53 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 54 | void onDraw(SkCanvas* canvas) override { |
reed | 6a070dc | 2014-11-11 19:36:09 -0800 | [diff] [blame] | 55 | SkPaint paint; |
| 56 | paint.setAntiAlias(true); |
| 57 | paint.setStrokeWidth(SkIntToScalar(2)); |
| 58 | |
| 59 | paint.setStyle(SkPaint::kFill_Style); |
| 60 | paint.setColor(0x800000FF); |
| 61 | canvas->drawArc(fR, 0, fSweep, true, paint); |
| 62 | |
| 63 | paint.setColor(0x800FF000); |
| 64 | canvas->drawArc(fR, 0, fSweep, false, paint); |
| 65 | |
| 66 | paint.setStyle(SkPaint::kStroke_Style); |
| 67 | paint.setColor(SK_ColorRED); |
| 68 | canvas->drawArc(fR, 0, fSweep, true, paint); |
| 69 | |
| 70 | paint.setStrokeWidth(0); |
| 71 | paint.setColor(SK_ColorBLUE); |
| 72 | canvas->drawArc(fR, 0, fSweep, false, paint); |
| 73 | } |
reed | 6be2aa9 | 2014-11-18 11:08:05 -0800 | [diff] [blame] | 74 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 75 | SkRect onGetBounds() override { |
reed | 6be2aa9 | 2014-11-18 11:08:05 -0800 | [diff] [blame] | 76 | SkRect r(fR); |
| 77 | r.outset(2, 2); |
| 78 | return r; |
| 79 | } |
reed | 6a070dc | 2014-11-11 19:36:09 -0800 | [diff] [blame] | 80 | }; |
| 81 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 82 | public: |
reed | 6a070dc | 2014-11-11 19:36:09 -0800 | [diff] [blame] | 83 | SkRect fRect; |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 84 | sk_sp<MyDrawable> fAnimatingDrawable; |
| 85 | sk_sp<SkDrawable> fRootDrawable; |
reed | 6a070dc | 2014-11-11 19:36:09 -0800 | [diff] [blame] | 86 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 87 | ArcsView() { |
reed@android.com | bbff1d5 | 2009-06-05 16:21:03 +0000 | [diff] [blame] | 88 | testparse(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 | fSweep = SkIntToScalar(100); |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 90 | this->setBGColor(0xFFDDDDDD); |
reed | 6a070dc | 2014-11-11 19:36:09 -0800 | [diff] [blame] | 91 | |
| 92 | fRect.set(0, 0, SkIntToScalar(200), SkIntToScalar(200)); |
| 93 | fRect.offset(SkIntToScalar(20), SkIntToScalar(20)); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 94 | fAnimatingDrawable = sk_make_sp<MyDrawable>(fRect); |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 95 | |
| 96 | SkPictureRecorder recorder; |
| 97 | this->drawRoot(recorder.beginRecording(SkRect::MakeWH(800, 500))); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 98 | fRootDrawable = recorder.finishRecordingAsDrawable(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | protected: |
| 102 | // overrides from SkEventSink |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 103 | bool onQuery(SkEvent* evt) override { |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 104 | if (SampleCode::TitleQ(*evt)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | SampleCode::TitleR(evt, "Arcs"); |
| 106 | return true; |
| 107 | } |
| 108 | return this->INHERITED::onQuery(evt); |
| 109 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 110 | |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 111 | static void DrawRectWithLines(SkCanvas* canvas, const SkRect& r, const SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 112 | canvas->drawRect(r, p); |
| 113 | canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, p); |
| 114 | canvas->drawLine(r.fLeft, r.fBottom, r.fRight, r.fTop, p); |
| 115 | canvas->drawLine(r.fLeft, r.centerY(), r.fRight, r.centerY(), p); |
| 116 | canvas->drawLine(r.centerX(), r.fTop, r.centerX(), r.fBottom, p); |
| 117 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 118 | |
reed | 61adb1b | 2015-02-09 13:01:05 -0800 | [diff] [blame] | 119 | static void DrawLabel(SkCanvas* canvas, const SkRect& rect, SkScalar start, SkScalar sweep) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 120 | SkPaint paint; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 121 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 122 | paint.setAntiAlias(true); |
| 123 | paint.setTextAlign(SkPaint::kCenter_Align); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 124 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 125 | SkString str; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 126 | |
reed | 61adb1b | 2015-02-09 13:01:05 -0800 | [diff] [blame] | 127 | str.appendScalar(start); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | str.append(", "); |
reed | 61adb1b | 2015-02-09 13:01:05 -0800 | [diff] [blame] | 129 | str.appendScalar(sweep); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 130 | canvas->drawString(str, rect.centerX(), |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 131 | rect.fBottom + paint.getTextSize() * 5/4, paint); |
| 132 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 133 | |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 134 | static void DrawArcs(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 135 | SkPaint paint; |
| 136 | SkRect r; |
reed | 61adb1b | 2015-02-09 13:01:05 -0800 | [diff] [blame] | 137 | SkScalar w = 75; |
| 138 | SkScalar h = 50; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 139 | |
| 140 | r.set(0, 0, w, h); |
| 141 | paint.setAntiAlias(true); |
| 142 | paint.setStyle(SkPaint::kStroke_Style); |
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 | canvas->save(); |
| 145 | canvas->translate(SkIntToScalar(10), SkIntToScalar(300)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 146 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 147 | paint.setStrokeWidth(SkIntToScalar(1)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 148 | |
reed | 61adb1b | 2015-02-09 13:01:05 -0800 | [diff] [blame] | 149 | static const SkScalar gAngles[] = { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 150 | 0, 360, |
| 151 | 0, 45, |
| 152 | 0, -45, |
| 153 | 720, 135, |
| 154 | -90, 269, |
| 155 | -90, 270, |
| 156 | -90, 271, |
| 157 | -180, -270, |
| 158 | 225, 90 |
| 159 | }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 160 | |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 161 | for (size_t i = 0; i < SK_ARRAY_COUNT(gAngles); i += 2) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 162 | paint.setColor(SK_ColorBLACK); |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 163 | DrawRectWithLines(canvas, r, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 164 | |
| 165 | paint.setColor(SK_ColorRED); |
reed | 61adb1b | 2015-02-09 13:01:05 -0800 | [diff] [blame] | 166 | canvas->drawArc(r, gAngles[i], gAngles[i+1], false, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 167 | |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 168 | DrawLabel(canvas, r, gAngles[i], gAngles[i+1]); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 169 | |
| 170 | canvas->translate(w * 8 / 7, 0); |
| 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 | canvas->restore(); |
| 174 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 175 | |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 176 | void drawRoot(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 177 | SkPaint paint; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 178 | paint.setAntiAlias(true); |
| 179 | paint.setStrokeWidth(SkIntToScalar(2)); |
| 180 | paint.setStyle(SkPaint::kStroke_Style); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 181 | |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 182 | DrawRectWithLines(canvas, fRect, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 183 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 184 | canvas->drawDrawable(fAnimatingDrawable.get()); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 185 | |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 186 | DrawArcs(canvas); |
| 187 | } |
| 188 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 189 | void onDrawContent(SkCanvas* canvas) override { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 190 | canvas->drawDrawable(fRootDrawable.get()); |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 191 | } |
| 192 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 193 | bool onAnimate(const SkAnimTimer& timer) override { |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 194 | SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360)); |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 195 | fAnimatingDrawable->setSweep(angle); |
| 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 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 199 | private: |
| 200 | SkScalar fSweep; |
| 201 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 202 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | ////////////////////////////////////////////////////////////////////////////// |
| 206 | |
| 207 | static SkView* MyFactory() { return new ArcsView; } |
| 208 | static SkViewRegister reg(MyFactory); |