blob: ed1f8dfb76b5fb1691a0556a0cc1d0c65d059776 [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
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04008#include "Sample.h"
reed76113a92015-02-02 12:55:02 -08009#include "SkAnimTimer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkCanvas.h"
Brian Osman8ceee432017-12-01 10:52:28 -050011#include "SkColorFilter.h"
12#include "SkColorPriv.h"
13#include "SkCornerPathEffect.h"
reed3cb38402015-02-06 08:36:15 -080014#include "SkDrawable.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkGradientShader.h"
16#include "SkPath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017#include "SkPathMeasure.h"
reed1bdfd3f2014-11-24 14:41:51 -080018#include "SkPictureRecorder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019#include "SkRandom.h"
Brian Osman8ceee432017-12-01 10:52:28 -050020#include "SkRegion.h"
21#include "SkShader.h"
22#include "SkString.h"
Hal Canaryea60b952018-08-21 11:45:46 -040023#include "SkUTF.h"
Brian Osman8ceee432017-12-01 10:52:28 -050024#include "Sk1DPathEffect.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000025
reed@android.combbff1d52009-06-05 16:21:03 +000026#include "SkParsePath.h"
27static void testparse() {
28 SkRect r;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000029 r.set(0, 0, 10, 10.5f);
reed@android.combbff1d52009-06-05 16:21:03 +000030 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 Wagnerb2c4ea62018-08-08 11:36:17 -040039class ArcsView : public Sample {
reed3cb38402015-02-06 08:36:15 -080040 class MyDrawable : public SkDrawable {
reed6a070dc2014-11-11 19:36:09 -080041 SkRect fR;
42 SkScalar fSweep;
43 public:
reed1bdfd3f2014-11-24 14:41:51 -080044 MyDrawable(const SkRect& r) : fR(r), fSweep(0) {}
reed6a070dc2014-11-11 19:36:09 -080045
46 void setSweep(SkScalar sweep) {
47 if (fSweep != sweep) {
48 fSweep = sweep;
49 this->notifyDrawingChanged();
50 }
51 }
52
mtklein36352bf2015-03-25 18:17:31 -070053 void onDraw(SkCanvas* canvas) override {
reed6a070dc2014-11-11 19:36:09 -080054 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 }
reed6be2aa92014-11-18 11:08:05 -080073
mtklein36352bf2015-03-25 18:17:31 -070074 SkRect onGetBounds() override {
reed6be2aa92014-11-18 11:08:05 -080075 SkRect r(fR);
76 r.outset(2, 2);
77 return r;
78 }
reed6a070dc2014-11-11 19:36:09 -080079 };
80
reed@android.com8a1c16f2008-12-17 15:59:43 +000081public:
reed6a070dc2014-11-11 19:36:09 -080082 SkRect fRect;
reedca2622b2016-03-18 07:25:55 -070083 sk_sp<MyDrawable> fAnimatingDrawable;
84 sk_sp<SkDrawable> fRootDrawable;
reed6a070dc2014-11-11 19:36:09 -080085
rmistry@google.comae933ce2012-08-23 18:19:56 +000086 ArcsView() {
reed@android.combbff1d52009-06-05 16:21:03 +000087 testparse();
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 fSweep = SkIntToScalar(100);
reed@google.com961ddb02011-05-05 14:03:48 +000089 this->setBGColor(0xFFDDDDDD);
reed6a070dc2014-11-11 19:36:09 -080090
91 fRect.set(0, 0, SkIntToScalar(200), SkIntToScalar(200));
92 fRect.offset(SkIntToScalar(20), SkIntToScalar(20));
reedca2622b2016-03-18 07:25:55 -070093 fAnimatingDrawable = sk_make_sp<MyDrawable>(fRect);
reed1bdfd3f2014-11-24 14:41:51 -080094
95 SkPictureRecorder recorder;
96 this->drawRoot(recorder.beginRecording(SkRect::MakeWH(800, 500)));
reedca2622b2016-03-18 07:25:55 -070097 fRootDrawable = recorder.finishRecordingAsDrawable();
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 }
99
100protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400101 bool onQuery(Sample::Event* evt) override {
102 if (Sample::TitleQ(*evt)) {
103 Sample::TitleR(evt, "Arcs");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 return true;
105 }
106 return this->INHERITED::onQuery(evt);
107 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000108
reed1bdfd3f2014-11-24 14:41:51 -0800109 static void DrawRectWithLines(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110 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.comae933ce2012-08-23 18:19:56 +0000116
reed61adb1b2015-02-09 13:01:05 -0800117 static void DrawLabel(SkCanvas* canvas, const SkRect& rect, SkScalar start, SkScalar sweep) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000119
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 paint.setAntiAlias(true);
121 paint.setTextAlign(SkPaint::kCenter_Align);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000122
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 SkString str;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000124
reed61adb1b2015-02-09 13:01:05 -0800125 str.appendScalar(start);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 str.append(", ");
reed61adb1b2015-02-09 13:01:05 -0800127 str.appendScalar(sweep);
Cary Clark2a475ea2017-04-28 15:35:12 -0400128 canvas->drawString(str, rect.centerX(),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 rect.fBottom + paint.getTextSize() * 5/4, paint);
130 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000131
reed1bdfd3f2014-11-24 14:41:51 -0800132 static void DrawArcs(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133 SkPaint paint;
134 SkRect r;
reed61adb1b2015-02-09 13:01:05 -0800135 SkScalar w = 75;
136 SkScalar h = 50;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137
138 r.set(0, 0, w, h);
139 paint.setAntiAlias(true);
140 paint.setStyle(SkPaint::kStroke_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000141
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 canvas->save();
143 canvas->translate(SkIntToScalar(10), SkIntToScalar(300));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000144
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 paint.setStrokeWidth(SkIntToScalar(1));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000146
reed61adb1b2015-02-09 13:01:05 -0800147 static const SkScalar gAngles[] = {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148 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.comae933ce2012-08-23 18:19:56 +0000158
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000159 for (size_t i = 0; i < SK_ARRAY_COUNT(gAngles); i += 2) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 paint.setColor(SK_ColorBLACK);
reed1bdfd3f2014-11-24 14:41:51 -0800161 DrawRectWithLines(canvas, r, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162
163 paint.setColor(SK_ColorRED);
reed61adb1b2015-02-09 13:01:05 -0800164 canvas->drawArc(r, gAngles[i], gAngles[i+1], false, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000165
reed1bdfd3f2014-11-24 14:41:51 -0800166 DrawLabel(canvas, r, gAngles[i], gAngles[i+1]);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167
168 canvas->translate(w * 8 / 7, 0);
169 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000170
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171 canvas->restore();
172 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000173
reed1bdfd3f2014-11-24 14:41:51 -0800174 void drawRoot(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175 SkPaint paint;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 paint.setAntiAlias(true);
177 paint.setStrokeWidth(SkIntToScalar(2));
178 paint.setStyle(SkPaint::kStroke_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000179
reed1bdfd3f2014-11-24 14:41:51 -0800180 DrawRectWithLines(canvas, fRect, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000181
reedca2622b2016-03-18 07:25:55 -0700182 canvas->drawDrawable(fAnimatingDrawable.get());
rmistry@google.comae933ce2012-08-23 18:19:56 +0000183
reed1bdfd3f2014-11-24 14:41:51 -0800184 DrawArcs(canvas);
185 }
186
mtklein36352bf2015-03-25 18:17:31 -0700187 void onDrawContent(SkCanvas* canvas) override {
reedca2622b2016-03-18 07:25:55 -0700188 canvas->drawDrawable(fRootDrawable.get());
reedd9adfe62015-02-01 19:01:04 -0800189 }
190
mtklein36352bf2015-03-25 18:17:31 -0700191 bool onAnimate(const SkAnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -0800192 SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360));
reedd9adfe62015-02-01 19:01:04 -0800193 fAnimatingDrawable->setSweep(angle);
194 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000195 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000196
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197private:
198 SkScalar fSweep;
199
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400200 typedef Sample INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000201};
202
203//////////////////////////////////////////////////////////////////////////////
204
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400205DEF_SAMPLE( return new ArcsView(); )