blob: d6400782654eac9e255c639b8f960d24c41e8c5c [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
reed76113a92015-02-02 12:55:02 -08007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#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.com8a1c16f2008-12-17 15:59:43 +000026
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "include/utils/SkParsePath.h"
reed@android.combbff1d52009-06-05 16:21:03 +000028static void testparse() {
29 SkRect r;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000030 r.set(0, 0, 10, 10.5f);
reed@android.combbff1d52009-06-05 16:21:03 +000031 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 Wagnerb2c4ea62018-08-08 11:36:17 -040040class ArcsView : public Sample {
reed3cb38402015-02-06 08:36:15 -080041 class MyDrawable : public SkDrawable {
reed6a070dc2014-11-11 19:36:09 -080042 SkRect fR;
43 SkScalar fSweep;
44 public:
reed1bdfd3f2014-11-24 14:41:51 -080045 MyDrawable(const SkRect& r) : fR(r), fSweep(0) {}
reed6a070dc2014-11-11 19:36:09 -080046
47 void setSweep(SkScalar sweep) {
48 if (fSweep != sweep) {
49 fSweep = sweep;
50 this->notifyDrawingChanged();
51 }
52 }
53
mtklein36352bf2015-03-25 18:17:31 -070054 void onDraw(SkCanvas* canvas) override {
reed6a070dc2014-11-11 19:36:09 -080055 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 }
reed6be2aa92014-11-18 11:08:05 -080074
mtklein36352bf2015-03-25 18:17:31 -070075 SkRect onGetBounds() override {
reed6be2aa92014-11-18 11:08:05 -080076 SkRect r(fR);
77 r.outset(2, 2);
78 return r;
79 }
reed6a070dc2014-11-11 19:36:09 -080080 };
81
reed@android.com8a1c16f2008-12-17 15:59:43 +000082public:
reed6a070dc2014-11-11 19:36:09 -080083 SkRect fRect;
reedca2622b2016-03-18 07:25:55 -070084 sk_sp<MyDrawable> fAnimatingDrawable;
85 sk_sp<SkDrawable> fRootDrawable;
reed6a070dc2014-11-11 19:36:09 -080086
Ben Wagneree4db8e2018-11-30 16:11:24 -050087 ArcsView() { }
reed@android.com8a1c16f2008-12-17 15:59:43 +000088
89protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040090 bool onQuery(Sample::Event* evt) override {
91 if (Sample::TitleQ(*evt)) {
92 Sample::TitleR(evt, "Arcs");
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 return true;
94 }
95 return this->INHERITED::onQuery(evt);
96 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000097
reed1bdfd3f2014-11-24 14:41:51 -080098 static void DrawRectWithLines(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 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.comae933ce2012-08-23 18:19:56 +0000105
reed61adb1b2015-02-09 13:01:05 -0800106 static void DrawLabel(SkCanvas* canvas, const SkRect& rect, SkScalar start, SkScalar sweep) {
Mike Reedb579f072019-01-03 15:45:53 -0500107 SkFont font;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108 SkString str;
reed61adb1b2015-02-09 13:01:05 -0800109 str.appendScalar(start);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110 str.append(", ");
reed61adb1b2015-02-09 13:01:05 -0800111 str.appendScalar(sweep);
Mike Reedb579f072019-01-03 15:45:53 -0500112 SkTextUtils::DrawString(canvas, str.c_str(), rect.centerX(),
113 rect.fBottom + font.getSize() * 5/4, font, SkPaint(),
Mike Reed3a42ec02018-10-30 12:53:21 -0400114 SkTextUtils::kCenter_Align);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000116
reed1bdfd3f2014-11-24 14:41:51 -0800117 static void DrawArcs(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 SkPaint paint;
119 SkRect r;
reed61adb1b2015-02-09 13:01:05 -0800120 SkScalar w = 75;
121 SkScalar h = 50;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122
123 r.set(0, 0, w, h);
124 paint.setAntiAlias(true);
125 paint.setStyle(SkPaint::kStroke_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000126
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127 canvas->save();
128 canvas->translate(SkIntToScalar(10), SkIntToScalar(300));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000129
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 paint.setStrokeWidth(SkIntToScalar(1));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000131
reed61adb1b2015-02-09 13:01:05 -0800132 static const SkScalar gAngles[] = {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133 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.comae933ce2012-08-23 18:19:56 +0000143
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000144 for (size_t i = 0; i < SK_ARRAY_COUNT(gAngles); i += 2) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 paint.setColor(SK_ColorBLACK);
reed1bdfd3f2014-11-24 14:41:51 -0800146 DrawRectWithLines(canvas, r, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147
148 paint.setColor(SK_ColorRED);
reed61adb1b2015-02-09 13:01:05 -0800149 canvas->drawArc(r, gAngles[i], gAngles[i+1], false, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000150
reed1bdfd3f2014-11-24 14:41:51 -0800151 DrawLabel(canvas, r, gAngles[i], gAngles[i+1]);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152
153 canvas->translate(w * 8 / 7, 0);
154 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000155
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 canvas->restore();
157 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000158
reed1bdfd3f2014-11-24 14:41:51 -0800159 void drawRoot(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 SkPaint paint;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161 paint.setAntiAlias(true);
162 paint.setStrokeWidth(SkIntToScalar(2));
163 paint.setStyle(SkPaint::kStroke_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000164
reed1bdfd3f2014-11-24 14:41:51 -0800165 DrawRectWithLines(canvas, fRect, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000166
reedca2622b2016-03-18 07:25:55 -0700167 canvas->drawDrawable(fAnimatingDrawable.get());
rmistry@google.comae933ce2012-08-23 18:19:56 +0000168
reed1bdfd3f2014-11-24 14:41:51 -0800169 DrawArcs(canvas);
170 }
171
Ben Wagneree4db8e2018-11-30 16:11:24 -0500172 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
mtklein36352bf2015-03-25 18:17:31 -0700186 void onDrawContent(SkCanvas* canvas) override {
reedca2622b2016-03-18 07:25:55 -0700187 canvas->drawDrawable(fRootDrawable.get());
reedd9adfe62015-02-01 19:01:04 -0800188 }
189
Mike Kleincd5104e2019-03-20 11:55:08 -0500190 bool onAnimate(const AnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -0800191 SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360));
Jim Van Verth7a7a97d2019-04-17 09:44:32 -0400192 if (fAnimatingDrawable) {
193 fAnimatingDrawable->setSweep(angle);
194 }
reedd9adfe62015-02-01 19:01:04 -0800195 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000197
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198private:
199 SkScalar fSweep;
200
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400201 typedef Sample INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000202};
203
204//////////////////////////////////////////////////////////////////////////////
205
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400206DEF_SAMPLE( return new ArcsView(); )