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