blob: a461a22aa986b1424eadbdc5990c63e10871d65c [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"
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"
23#include "SkUtils.h"
24#include "SkView.h"
25#include "Sk1DPathEffect.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000026
reed@android.combbff1d52009-06-05 16:21:03 +000027#include "SkParsePath.h"
28static 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
reed@google.com961ddb02011-05-05 14:03:48 +000040class ArcsView : public SampleView {
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
rmistry@google.comae933ce2012-08-23 18:19:56 +000087 ArcsView() {
reed@android.combbff1d52009-06-05 16:21:03 +000088 testparse();
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 fSweep = SkIntToScalar(100);
reed@google.com961ddb02011-05-05 14:03:48 +000090 this->setBGColor(0xFFDDDDDD);
reed6a070dc2014-11-11 19:36:09 -080091
92 fRect.set(0, 0, SkIntToScalar(200), SkIntToScalar(200));
93 fRect.offset(SkIntToScalar(20), SkIntToScalar(20));
reedca2622b2016-03-18 07:25:55 -070094 fAnimatingDrawable = sk_make_sp<MyDrawable>(fRect);
reed1bdfd3f2014-11-24 14:41:51 -080095
96 SkPictureRecorder recorder;
97 this->drawRoot(recorder.beginRecording(SkRect::MakeWH(800, 500)));
reedca2622b2016-03-18 07:25:55 -070098 fRootDrawable = recorder.finishRecordingAsDrawable();
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 }
100
101protected:
102 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -0700103 bool onQuery(SkEvent* evt) override {
reed@google.com961ddb02011-05-05 14:03:48 +0000104 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 SampleCode::TitleR(evt, "Arcs");
106 return true;
107 }
108 return this->INHERITED::onQuery(evt);
109 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000110
reed1bdfd3f2014-11-24 14:41:51 -0800111 static void DrawRectWithLines(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112 canvas->drawRect(r, p);
113 canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, p);
114 canvas->drawLine(r.fLeft, r.fBottom, r.fRight, r.fTop, p);
115 canvas->drawLine(r.fLeft, r.centerY(), r.fRight, r.centerY(), p);
116 canvas->drawLine(r.centerX(), r.fTop, r.centerX(), r.fBottom, p);
117 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000118
reed61adb1b2015-02-09 13:01:05 -0800119 static void DrawLabel(SkCanvas* canvas, const SkRect& rect, SkScalar start, SkScalar sweep) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000121
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122 paint.setAntiAlias(true);
123 paint.setTextAlign(SkPaint::kCenter_Align);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000124
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 SkString str;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000126
reed61adb1b2015-02-09 13:01:05 -0800127 str.appendScalar(start);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 str.append(", ");
reed61adb1b2015-02-09 13:01:05 -0800129 str.appendScalar(sweep);
Cary Clark2a475ea2017-04-28 15:35:12 -0400130 canvas->drawString(str, rect.centerX(),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131 rect.fBottom + paint.getTextSize() * 5/4, paint);
132 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000133
reed1bdfd3f2014-11-24 14:41:51 -0800134 static void DrawArcs(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 SkPaint paint;
136 SkRect r;
reed61adb1b2015-02-09 13:01:05 -0800137 SkScalar w = 75;
138 SkScalar h = 50;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139
140 r.set(0, 0, w, h);
141 paint.setAntiAlias(true);
142 paint.setStyle(SkPaint::kStroke_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000143
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144 canvas->save();
145 canvas->translate(SkIntToScalar(10), SkIntToScalar(300));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000146
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 paint.setStrokeWidth(SkIntToScalar(1));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000148
reed61adb1b2015-02-09 13:01:05 -0800149 static const SkScalar gAngles[] = {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 0, 360,
151 0, 45,
152 0, -45,
153 720, 135,
154 -90, 269,
155 -90, 270,
156 -90, 271,
157 -180, -270,
158 225, 90
159 };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000160
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000161 for (size_t i = 0; i < SK_ARRAY_COUNT(gAngles); i += 2) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162 paint.setColor(SK_ColorBLACK);
reed1bdfd3f2014-11-24 14:41:51 -0800163 DrawRectWithLines(canvas, r, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164
165 paint.setColor(SK_ColorRED);
reed61adb1b2015-02-09 13:01:05 -0800166 canvas->drawArc(r, gAngles[i], gAngles[i+1], false, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000167
reed1bdfd3f2014-11-24 14:41:51 -0800168 DrawLabel(canvas, r, gAngles[i], gAngles[i+1]);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169
170 canvas->translate(w * 8 / 7, 0);
171 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000172
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 canvas->restore();
174 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000175
reed1bdfd3f2014-11-24 14:41:51 -0800176 void drawRoot(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177 SkPaint paint;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178 paint.setAntiAlias(true);
179 paint.setStrokeWidth(SkIntToScalar(2));
180 paint.setStyle(SkPaint::kStroke_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000181
reed1bdfd3f2014-11-24 14:41:51 -0800182 DrawRectWithLines(canvas, fRect, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000183
reedca2622b2016-03-18 07:25:55 -0700184 canvas->drawDrawable(fAnimatingDrawable.get());
rmistry@google.comae933ce2012-08-23 18:19:56 +0000185
reed1bdfd3f2014-11-24 14:41:51 -0800186 DrawArcs(canvas);
187 }
188
mtklein36352bf2015-03-25 18:17:31 -0700189 void onDrawContent(SkCanvas* canvas) override {
reedca2622b2016-03-18 07:25:55 -0700190 canvas->drawDrawable(fRootDrawable.get());
reedd9adfe62015-02-01 19:01:04 -0800191 }
192
mtklein36352bf2015-03-25 18:17:31 -0700193 bool onAnimate(const SkAnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -0800194 SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360));
reedd9adfe62015-02-01 19:01:04 -0800195 fAnimatingDrawable->setSweep(angle);
196 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000198
reed@android.com8a1c16f2008-12-17 15:59:43 +0000199private:
200 SkScalar fSweep;
201
reed@google.com961ddb02011-05-05 14:03:48 +0000202 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000203};
204
205//////////////////////////////////////////////////////////////////////////////
206
207static SkView* MyFactory() { return new ArcsView; }
208static SkViewRegister reg(MyFactory);