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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkColorFilter.h" |
| 10 | #include "include/core/SkColorPriv.h" |
| 11 | #include "include/core/SkDrawable.h" |
| 12 | #include "include/core/SkPath.h" |
| 13 | #include "include/core/SkPathMeasure.h" |
| 14 | #include "include/core/SkPictureRecorder.h" |
| 15 | #include "include/core/SkRegion.h" |
| 16 | #include "include/core/SkShader.h" |
| 17 | #include "include/core/SkString.h" |
| 18 | #include "include/effects/Sk1DPathEffect.h" |
| 19 | #include "include/effects/SkCornerPathEffect.h" |
| 20 | #include "include/effects/SkGradientShader.h" |
| 21 | #include "include/utils/SkRandom.h" |
| 22 | #include "include/utils/SkTextUtils.h" |
| 23 | #include "samplecode/Sample.h" |
| 24 | #include "src/utils/SkUTF.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 26 | #include "include/utils/SkParsePath.h" |
reed@android.com | bbff1d5 | 2009-06-05 16:21:03 +0000 | [diff] [blame] | 27 | static void testparse() { |
| 28 | SkRect r; |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 29 | r.setLTRB(0, 0, 10, 10.5f); |
reed@android.com | bbff1d5 | 2009-06-05 16:21:03 +0000 | [diff] [blame] | 30 | SkPath p, p2; |
| 31 | SkString str, str2; |
| 32 | |
| 33 | p.addRect(r); |
| 34 | SkParsePath::ToSVGString(p, &str); |
| 35 | SkParsePath::FromSVGString(str.c_str(), &p2); |
| 36 | SkParsePath::ToSVGString(p2, &str2); |
| 37 | } |
| 38 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 39 | class ArcsView : public Sample { |
reed | 3cb3840 | 2015-02-06 08:36:15 -0800 | [diff] [blame] | 40 | class MyDrawable : public SkDrawable { |
reed | 6a070dc | 2014-11-11 19:36:09 -0800 | [diff] [blame] | 41 | SkRect fR; |
| 42 | SkScalar fSweep; |
| 43 | public: |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 44 | MyDrawable(const SkRect& r) : fR(r), fSweep(0) {} |
reed | 6a070dc | 2014-11-11 19:36:09 -0800 | [diff] [blame] | 45 | |
| 46 | void setSweep(SkScalar sweep) { |
| 47 | if (fSweep != sweep) { |
| 48 | fSweep = sweep; |
| 49 | this->notifyDrawingChanged(); |
| 50 | } |
| 51 | } |
| 52 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 53 | void onDraw(SkCanvas* canvas) override { |
reed | 6a070dc | 2014-11-11 19:36:09 -0800 | [diff] [blame] | 54 | SkPaint paint; |
| 55 | paint.setAntiAlias(true); |
| 56 | paint.setStrokeWidth(SkIntToScalar(2)); |
| 57 | |
| 58 | paint.setStyle(SkPaint::kFill_Style); |
| 59 | paint.setColor(0x800000FF); |
| 60 | canvas->drawArc(fR, 0, fSweep, true, paint); |
| 61 | |
| 62 | paint.setColor(0x800FF000); |
| 63 | canvas->drawArc(fR, 0, fSweep, false, paint); |
| 64 | |
| 65 | paint.setStyle(SkPaint::kStroke_Style); |
| 66 | paint.setColor(SK_ColorRED); |
| 67 | canvas->drawArc(fR, 0, fSweep, true, paint); |
| 68 | |
| 69 | paint.setStrokeWidth(0); |
| 70 | paint.setColor(SK_ColorBLUE); |
| 71 | canvas->drawArc(fR, 0, fSweep, false, paint); |
| 72 | } |
reed | 6be2aa9 | 2014-11-18 11:08:05 -0800 | [diff] [blame] | 73 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 74 | SkRect onGetBounds() override { |
reed | 6be2aa9 | 2014-11-18 11:08:05 -0800 | [diff] [blame] | 75 | SkRect r(fR); |
| 76 | r.outset(2, 2); |
| 77 | return r; |
| 78 | } |
reed | 6a070dc | 2014-11-11 19:36:09 -0800 | [diff] [blame] | 79 | }; |
| 80 | |
Hal Canary | d7639af | 2019-07-17 09:08:11 -0400 | [diff] [blame] | 81 | SkRect fRect = {20, 20, 220, 220}; |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 82 | sk_sp<MyDrawable> fAnimatingDrawable; |
| 83 | sk_sp<SkDrawable> fRootDrawable; |
reed | 6a070dc | 2014-11-11 19:36:09 -0800 | [diff] [blame] | 84 | |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 85 | SkString name() override { return SkString("Arcs"); } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 86 | |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 87 | static void DrawRectWithLines(SkCanvas* canvas, const SkRect& r, const SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | canvas->drawRect(r, p); |
| 89 | canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, p); |
| 90 | canvas->drawLine(r.fLeft, r.fBottom, r.fRight, r.fTop, p); |
| 91 | canvas->drawLine(r.fLeft, r.centerY(), r.fRight, r.centerY(), p); |
| 92 | canvas->drawLine(r.centerX(), r.fTop, r.centerX(), r.fBottom, p); |
| 93 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 94 | |
reed | 61adb1b | 2015-02-09 13:01:05 -0800 | [diff] [blame] | 95 | static void DrawLabel(SkCanvas* canvas, const SkRect& rect, SkScalar start, SkScalar sweep) { |
Mike Reed | b579f07 | 2019-01-03 15:45:53 -0500 | [diff] [blame] | 96 | SkFont font; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 97 | SkString str; |
reed | 61adb1b | 2015-02-09 13:01:05 -0800 | [diff] [blame] | 98 | str.appendScalar(start); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 99 | str.append(", "); |
reed | 61adb1b | 2015-02-09 13:01:05 -0800 | [diff] [blame] | 100 | str.appendScalar(sweep); |
Mike Reed | b579f07 | 2019-01-03 15:45:53 -0500 | [diff] [blame] | 101 | SkTextUtils::DrawString(canvas, str.c_str(), rect.centerX(), |
| 102 | rect.fBottom + font.getSize() * 5/4, font, SkPaint(), |
Mike Reed | 3a42ec0 | 2018-10-30 12:53:21 -0400 | [diff] [blame] | 103 | SkTextUtils::kCenter_Align); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 105 | |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 106 | static void DrawArcs(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | SkPaint paint; |
| 108 | SkRect r; |
reed | 61adb1b | 2015-02-09 13:01:05 -0800 | [diff] [blame] | 109 | SkScalar w = 75; |
| 110 | SkScalar h = 50; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 111 | |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 112 | r.setWH(w, h); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 113 | paint.setAntiAlias(true); |
| 114 | paint.setStyle(SkPaint::kStroke_Style); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 115 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 116 | canvas->save(); |
| 117 | canvas->translate(SkIntToScalar(10), SkIntToScalar(300)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 118 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 119 | paint.setStrokeWidth(SkIntToScalar(1)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 120 | |
reed | 61adb1b | 2015-02-09 13:01:05 -0800 | [diff] [blame] | 121 | static const SkScalar gAngles[] = { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 122 | 0, 360, |
| 123 | 0, 45, |
| 124 | 0, -45, |
| 125 | 720, 135, |
| 126 | -90, 269, |
| 127 | -90, 270, |
| 128 | -90, 271, |
| 129 | -180, -270, |
| 130 | 225, 90 |
| 131 | }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 132 | |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 133 | 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] | 134 | paint.setColor(SK_ColorBLACK); |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 135 | DrawRectWithLines(canvas, r, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 136 | |
| 137 | paint.setColor(SK_ColorRED); |
reed | 61adb1b | 2015-02-09 13:01:05 -0800 | [diff] [blame] | 138 | canvas->drawArc(r, gAngles[i], gAngles[i+1], false, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 139 | |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 140 | DrawLabel(canvas, r, gAngles[i], gAngles[i+1]); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 141 | |
| 142 | canvas->translate(w * 8 / 7, 0); |
| 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 | canvas->restore(); |
| 146 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 147 | |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 148 | void drawRoot(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 149 | SkPaint paint; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 150 | paint.setAntiAlias(true); |
| 151 | paint.setStrokeWidth(SkIntToScalar(2)); |
| 152 | paint.setStyle(SkPaint::kStroke_Style); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 153 | |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 154 | DrawRectWithLines(canvas, fRect, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 155 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 156 | canvas->drawDrawable(fAnimatingDrawable.get()); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 157 | |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 158 | DrawArcs(canvas); |
| 159 | } |
| 160 | |
Ben Wagner | ee4db8e | 2018-11-30 16:11:24 -0500 | [diff] [blame] | 161 | void onOnceBeforeDraw() override { |
| 162 | testparse(); |
Ben Wagner | ee4db8e | 2018-11-30 16:11:24 -0500 | [diff] [blame] | 163 | this->setBGColor(0xFFDDDDDD); |
| 164 | |
Ben Wagner | ee4db8e | 2018-11-30 16:11:24 -0500 | [diff] [blame] | 165 | fAnimatingDrawable = sk_make_sp<MyDrawable>(fRect); |
| 166 | |
| 167 | SkPictureRecorder recorder; |
| 168 | this->drawRoot(recorder.beginRecording(SkRect::MakeWH(800, 500))); |
| 169 | fRootDrawable = recorder.finishRecordingAsDrawable(); |
| 170 | } |
| 171 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 172 | void onDrawContent(SkCanvas* canvas) override { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 173 | canvas->drawDrawable(fRootDrawable.get()); |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 176 | bool onAnimate(double nanos) override { |
| 177 | SkScalar angle = SkDoubleToScalar(fmod(1e-9 * nanos * 360 / 24, 360)); |
Jim Van Verth | 7a7a97d | 2019-04-17 09:44:32 -0400 | [diff] [blame] | 178 | if (fAnimatingDrawable) { |
| 179 | fAnimatingDrawable->setSweep(angle); |
| 180 | } |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 181 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 182 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 183 | }; |
| 184 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 185 | DEF_SAMPLE( return new ArcsView(); ) |