blob: b3b1cfefafd1f364d0aeaa076bb742e606327a9c [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
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.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"
Brian Osman8ceee432017-12-01 10:52:28 -050016#include "SkLayerRasterizer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017#include "SkPath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000018#include "SkPathMeasure.h"
reed1bdfd3f2014-11-24 14:41:51 -080019#include "SkPictureRecorder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020#include "SkRandom.h"
Brian Osman8ceee432017-12-01 10:52:28 -050021#include "SkRegion.h"
22#include "SkShader.h"
23#include "SkString.h"
24#include "SkUtils.h"
25#include "SkView.h"
26#include "Sk1DPathEffect.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000027
reed@android.combbff1d52009-06-05 16:21:03 +000028#include "SkParsePath.h"
29static void testparse() {
30 SkRect r;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000031 r.set(0, 0, 10, 10.5f);
reed@android.combbff1d52009-06-05 16:21:03 +000032 SkPath p, p2;
33 SkString str, str2;
34
35 p.addRect(r);
36 SkParsePath::ToSVGString(p, &str);
37 SkParsePath::FromSVGString(str.c_str(), &p2);
38 SkParsePath::ToSVGString(p2, &str2);
39}
40
reed@google.com961ddb02011-05-05 14:03:48 +000041class ArcsView : public SampleView {
reed3cb38402015-02-06 08:36:15 -080042 class MyDrawable : public SkDrawable {
reed6a070dc2014-11-11 19:36:09 -080043 SkRect fR;
44 SkScalar fSweep;
45 public:
reed1bdfd3f2014-11-24 14:41:51 -080046 MyDrawable(const SkRect& r) : fR(r), fSweep(0) {}
reed6a070dc2014-11-11 19:36:09 -080047
48 void setSweep(SkScalar sweep) {
49 if (fSweep != sweep) {
50 fSweep = sweep;
51 this->notifyDrawingChanged();
52 }
53 }
54
mtklein36352bf2015-03-25 18:17:31 -070055 void onDraw(SkCanvas* canvas) override {
reed6a070dc2014-11-11 19:36:09 -080056 SkPaint paint;
57 paint.setAntiAlias(true);
58 paint.setStrokeWidth(SkIntToScalar(2));
59
60 paint.setStyle(SkPaint::kFill_Style);
61 paint.setColor(0x800000FF);
62 canvas->drawArc(fR, 0, fSweep, true, paint);
63
64 paint.setColor(0x800FF000);
65 canvas->drawArc(fR, 0, fSweep, false, paint);
66
67 paint.setStyle(SkPaint::kStroke_Style);
68 paint.setColor(SK_ColorRED);
69 canvas->drawArc(fR, 0, fSweep, true, paint);
70
71 paint.setStrokeWidth(0);
72 paint.setColor(SK_ColorBLUE);
73 canvas->drawArc(fR, 0, fSweep, false, paint);
74 }
reed6be2aa92014-11-18 11:08:05 -080075
mtklein36352bf2015-03-25 18:17:31 -070076 SkRect onGetBounds() override {
reed6be2aa92014-11-18 11:08:05 -080077 SkRect r(fR);
78 r.outset(2, 2);
79 return r;
80 }
reed6a070dc2014-11-11 19:36:09 -080081 };
82
reed@android.com8a1c16f2008-12-17 15:59:43 +000083public:
reed6a070dc2014-11-11 19:36:09 -080084 SkRect fRect;
reedca2622b2016-03-18 07:25:55 -070085 sk_sp<MyDrawable> fAnimatingDrawable;
86 sk_sp<SkDrawable> fRootDrawable;
reed6a070dc2014-11-11 19:36:09 -080087
rmistry@google.comae933ce2012-08-23 18:19:56 +000088 ArcsView() {
reed@android.combbff1d52009-06-05 16:21:03 +000089 testparse();
reed@android.com8a1c16f2008-12-17 15:59:43 +000090 fSweep = SkIntToScalar(100);
reed@google.com961ddb02011-05-05 14:03:48 +000091 this->setBGColor(0xFFDDDDDD);
reed6a070dc2014-11-11 19:36:09 -080092
93 fRect.set(0, 0, SkIntToScalar(200), SkIntToScalar(200));
94 fRect.offset(SkIntToScalar(20), SkIntToScalar(20));
reedca2622b2016-03-18 07:25:55 -070095 fAnimatingDrawable = sk_make_sp<MyDrawable>(fRect);
reed1bdfd3f2014-11-24 14:41:51 -080096
97 SkPictureRecorder recorder;
98 this->drawRoot(recorder.beginRecording(SkRect::MakeWH(800, 500)));
reedca2622b2016-03-18 07:25:55 -070099 fRootDrawable = recorder.finishRecordingAsDrawable();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100 }
101
102protected:
103 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -0700104 bool onQuery(SkEvent* evt) override {
reed@google.com961ddb02011-05-05 14:03:48 +0000105 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 SampleCode::TitleR(evt, "Arcs");
107 return true;
108 }
109 return this->INHERITED::onQuery(evt);
110 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000111
reed1bdfd3f2014-11-24 14:41:51 -0800112 static void DrawRectWithLines(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113 canvas->drawRect(r, p);
114 canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, p);
115 canvas->drawLine(r.fLeft, r.fBottom, r.fRight, r.fTop, p);
116 canvas->drawLine(r.fLeft, r.centerY(), r.fRight, r.centerY(), p);
117 canvas->drawLine(r.centerX(), r.fTop, r.centerX(), r.fBottom, p);
118 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000119
reed61adb1b2015-02-09 13:01:05 -0800120 static void DrawLabel(SkCanvas* canvas, const SkRect& rect, SkScalar start, SkScalar sweep) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000122
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 paint.setAntiAlias(true);
124 paint.setTextAlign(SkPaint::kCenter_Align);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000125
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 SkString str;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000127
reed61adb1b2015-02-09 13:01:05 -0800128 str.appendScalar(start);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 str.append(", ");
reed61adb1b2015-02-09 13:01:05 -0800130 str.appendScalar(sweep);
Cary Clark2a475ea2017-04-28 15:35:12 -0400131 canvas->drawString(str, rect.centerX(),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132 rect.fBottom + paint.getTextSize() * 5/4, paint);
133 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000134
reed1bdfd3f2014-11-24 14:41:51 -0800135 static void DrawArcs(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 SkPaint paint;
137 SkRect r;
reed61adb1b2015-02-09 13:01:05 -0800138 SkScalar w = 75;
139 SkScalar h = 50;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140
141 r.set(0, 0, w, h);
142 paint.setAntiAlias(true);
143 paint.setStyle(SkPaint::kStroke_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000144
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 canvas->save();
146 canvas->translate(SkIntToScalar(10), SkIntToScalar(300));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000147
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148 paint.setStrokeWidth(SkIntToScalar(1));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000149
reed61adb1b2015-02-09 13:01:05 -0800150 static const SkScalar gAngles[] = {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 0, 360,
152 0, 45,
153 0, -45,
154 720, 135,
155 -90, 269,
156 -90, 270,
157 -90, 271,
158 -180, -270,
159 225, 90
160 };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000161
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000162 for (size_t i = 0; i < SK_ARRAY_COUNT(gAngles); i += 2) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 paint.setColor(SK_ColorBLACK);
reed1bdfd3f2014-11-24 14:41:51 -0800164 DrawRectWithLines(canvas, r, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165
166 paint.setColor(SK_ColorRED);
reed61adb1b2015-02-09 13:01:05 -0800167 canvas->drawArc(r, gAngles[i], gAngles[i+1], false, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000168
reed1bdfd3f2014-11-24 14:41:51 -0800169 DrawLabel(canvas, r, gAngles[i], gAngles[i+1]);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170
171 canvas->translate(w * 8 / 7, 0);
172 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000173
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174 canvas->restore();
175 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000176
reed1bdfd3f2014-11-24 14:41:51 -0800177 void drawRoot(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178 SkPaint paint;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179 paint.setAntiAlias(true);
180 paint.setStrokeWidth(SkIntToScalar(2));
181 paint.setStyle(SkPaint::kStroke_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000182
reed1bdfd3f2014-11-24 14:41:51 -0800183 DrawRectWithLines(canvas, fRect, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000184
reedca2622b2016-03-18 07:25:55 -0700185 canvas->drawDrawable(fAnimatingDrawable.get());
rmistry@google.comae933ce2012-08-23 18:19:56 +0000186
reed1bdfd3f2014-11-24 14:41:51 -0800187 DrawArcs(canvas);
188 }
189
mtklein36352bf2015-03-25 18:17:31 -0700190 void onDrawContent(SkCanvas* canvas) override {
reedca2622b2016-03-18 07:25:55 -0700191 canvas->drawDrawable(fRootDrawable.get());
reedd9adfe62015-02-01 19:01:04 -0800192 }
193
mtklein36352bf2015-03-25 18:17:31 -0700194 bool onAnimate(const SkAnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -0800195 SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360));
reedd9adfe62015-02-01 19:01:04 -0800196 fAnimatingDrawable->setSweep(angle);
197 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000199
reed@android.com8a1c16f2008-12-17 15:59:43 +0000200private:
201 SkScalar fSweep;
202
reed@google.com961ddb02011-05-05 14:03:48 +0000203 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000204};
205
206//////////////////////////////////////////////////////////////////////////////
207
208static SkView* MyFactory() { return new ArcsView; }
209static SkViewRegister reg(MyFactory);