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 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 8 | #include "Sample.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" |
Brian Osman | 8ceee43 | 2017-12-01 10:52:28 -0500 | [diff] [blame] | 24 | #include "Sk1DPathEffect.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | |
reed@android.com | bbff1d5 | 2009-06-05 16:21:03 +0000 | [diff] [blame] | 26 | #include "SkParsePath.h" |
| 27 | static void testparse() { |
| 28 | SkRect r; |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 29 | r.set(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 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 81 | public: |
reed | 6a070dc | 2014-11-11 19:36:09 -0800 | [diff] [blame] | 82 | SkRect fRect; |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 83 | sk_sp<MyDrawable> fAnimatingDrawable; |
| 84 | sk_sp<SkDrawable> fRootDrawable; |
reed | 6a070dc | 2014-11-11 19:36:09 -0800 | [diff] [blame] | 85 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 86 | ArcsView() { |
reed@android.com | bbff1d5 | 2009-06-05 16:21:03 +0000 | [diff] [blame] | 87 | testparse(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | fSweep = SkIntToScalar(100); |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 89 | this->setBGColor(0xFFDDDDDD); |
reed | 6a070dc | 2014-11-11 19:36:09 -0800 | [diff] [blame] | 90 | |
| 91 | fRect.set(0, 0, SkIntToScalar(200), SkIntToScalar(200)); |
| 92 | fRect.offset(SkIntToScalar(20), SkIntToScalar(20)); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 93 | fAnimatingDrawable = sk_make_sp<MyDrawable>(fRect); |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 94 | |
| 95 | SkPictureRecorder recorder; |
| 96 | this->drawRoot(recorder.beginRecording(SkRect::MakeWH(800, 500))); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 97 | fRootDrawable = recorder.finishRecordingAsDrawable(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | protected: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 101 | bool onQuery(Sample::Event* evt) override { |
| 102 | if (Sample::TitleQ(*evt)) { |
| 103 | Sample::TitleR(evt, "Arcs"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | return true; |
| 105 | } |
| 106 | return this->INHERITED::onQuery(evt); |
| 107 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 108 | |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 109 | static void DrawRectWithLines(SkCanvas* canvas, const SkRect& r, const SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 110 | canvas->drawRect(r, p); |
| 111 | canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, p); |
| 112 | canvas->drawLine(r.fLeft, r.fBottom, r.fRight, r.fTop, p); |
| 113 | canvas->drawLine(r.fLeft, r.centerY(), r.fRight, r.centerY(), p); |
| 114 | canvas->drawLine(r.centerX(), r.fTop, r.centerX(), r.fBottom, p); |
| 115 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 116 | |
reed | 61adb1b | 2015-02-09 13:01:05 -0800 | [diff] [blame] | 117 | 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] | 118 | SkPaint paint; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 119 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 120 | paint.setAntiAlias(true); |
| 121 | paint.setTextAlign(SkPaint::kCenter_Align); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 122 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | SkString str; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 124 | |
reed | 61adb1b | 2015-02-09 13:01:05 -0800 | [diff] [blame] | 125 | str.appendScalar(start); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 126 | str.append(", "); |
reed | 61adb1b | 2015-02-09 13:01:05 -0800 | [diff] [blame] | 127 | str.appendScalar(sweep); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 128 | canvas->drawString(str, rect.centerX(), |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 129 | rect.fBottom + paint.getTextSize() * 5/4, paint); |
| 130 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 131 | |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 132 | static void DrawArcs(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 133 | SkPaint paint; |
| 134 | SkRect r; |
reed | 61adb1b | 2015-02-09 13:01:05 -0800 | [diff] [blame] | 135 | SkScalar w = 75; |
| 136 | SkScalar h = 50; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 137 | |
| 138 | r.set(0, 0, w, h); |
| 139 | paint.setAntiAlias(true); |
| 140 | paint.setStyle(SkPaint::kStroke_Style); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 141 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 142 | canvas->save(); |
| 143 | canvas->translate(SkIntToScalar(10), SkIntToScalar(300)); |
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 | paint.setStrokeWidth(SkIntToScalar(1)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 146 | |
reed | 61adb1b | 2015-02-09 13:01:05 -0800 | [diff] [blame] | 147 | static const SkScalar gAngles[] = { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 148 | 0, 360, |
| 149 | 0, 45, |
| 150 | 0, -45, |
| 151 | 720, 135, |
| 152 | -90, 269, |
| 153 | -90, 270, |
| 154 | -90, 271, |
| 155 | -180, -270, |
| 156 | 225, 90 |
| 157 | }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 158 | |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 159 | 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] | 160 | paint.setColor(SK_ColorBLACK); |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 161 | DrawRectWithLines(canvas, r, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 162 | |
| 163 | paint.setColor(SK_ColorRED); |
reed | 61adb1b | 2015-02-09 13:01:05 -0800 | [diff] [blame] | 164 | canvas->drawArc(r, gAngles[i], gAngles[i+1], false, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 165 | |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 166 | DrawLabel(canvas, r, gAngles[i], gAngles[i+1]); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 167 | |
| 168 | canvas->translate(w * 8 / 7, 0); |
| 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 | canvas->restore(); |
| 172 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 173 | |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 174 | void drawRoot(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 175 | SkPaint paint; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 176 | paint.setAntiAlias(true); |
| 177 | paint.setStrokeWidth(SkIntToScalar(2)); |
| 178 | paint.setStyle(SkPaint::kStroke_Style); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 179 | |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 180 | DrawRectWithLines(canvas, fRect, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 181 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 182 | canvas->drawDrawable(fAnimatingDrawable.get()); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 183 | |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 184 | DrawArcs(canvas); |
| 185 | } |
| 186 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 187 | void onDrawContent(SkCanvas* canvas) override { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 188 | canvas->drawDrawable(fRootDrawable.get()); |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 189 | } |
| 190 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 191 | bool onAnimate(const SkAnimTimer& timer) override { |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 192 | SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360)); |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 193 | fAnimatingDrawable->setSweep(angle); |
| 194 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 195 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 196 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 197 | private: |
| 198 | SkScalar fSweep; |
| 199 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 200 | typedef Sample INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | ////////////////////////////////////////////////////////////////////////////// |
| 204 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 205 | DEF_SAMPLE( return new ArcsView(); ) |