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 */
7
8 #include "SampleCode.h"
9 #include "SkAnimTimer.h"
10 #include "SkCanvas.h"
11 #include "SkColorFilter.h"
12 #include "SkColorPriv.h"
13 #include "SkCornerPathEffect.h"
14 #include "SkDrawable.h"
15 #include "SkGradientShader.h"
16 #include "SkPath.h"
17 #include "SkPathMeasure.h"
18 #include "SkPictureRecorder.h"
19 #include "SkRandom.h"
20 #include "SkRegion.h"
21 #include "SkShader.h"
22 #include "SkString.h"
23 #include "SkUtils.h"
24 #include "SkView.h"
25 #include "Sk1DPathEffect.h"
26
27 #include "SkParsePath.h"
testparse()28 static void testparse() {
29 SkRect r;
30 r.set(0, 0, 10, 10.5f);
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
40 class ArcsView : public SampleView {
41 class MyDrawable : public SkDrawable {
42 SkRect fR;
43 SkScalar fSweep;
44 public:
MyDrawable(const SkRect & r)45 MyDrawable(const SkRect& r) : fR(r), fSweep(0) {}
46
setSweep(SkScalar sweep)47 void setSweep(SkScalar sweep) {
48 if (fSweep != sweep) {
49 fSweep = sweep;
50 this->notifyDrawingChanged();
51 }
52 }
53
onDraw(SkCanvas * canvas)54 void onDraw(SkCanvas* canvas) override {
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 }
74
onGetBounds()75 SkRect onGetBounds() override {
76 SkRect r(fR);
77 r.outset(2, 2);
78 return r;
79 }
80 };
81
82 public:
83 SkRect fRect;
84 sk_sp<MyDrawable> fAnimatingDrawable;
85 sk_sp<SkDrawable> fRootDrawable;
86
ArcsView()87 ArcsView() {
88 testparse();
89 fSweep = SkIntToScalar(100);
90 this->setBGColor(0xFFDDDDDD);
91
92 fRect.set(0, 0, SkIntToScalar(200), SkIntToScalar(200));
93 fRect.offset(SkIntToScalar(20), SkIntToScalar(20));
94 fAnimatingDrawable = sk_make_sp<MyDrawable>(fRect);
95
96 SkPictureRecorder recorder;
97 this->drawRoot(recorder.beginRecording(SkRect::MakeWH(800, 500)));
98 fRootDrawable = recorder.finishRecordingAsDrawable();
99 }
100
101 protected:
102 // overrides from SkEventSink
onQuery(SkEvent * evt)103 bool onQuery(SkEvent* evt) override {
104 if (SampleCode::TitleQ(*evt)) {
105 SampleCode::TitleR(evt, "Arcs");
106 return true;
107 }
108 return this->INHERITED::onQuery(evt);
109 }
110
DrawRectWithLines(SkCanvas * canvas,const SkRect & r,const SkPaint & p)111 static void DrawRectWithLines(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
112 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 }
118
DrawLabel(SkCanvas * canvas,const SkRect & rect,SkScalar start,SkScalar sweep)119 static void DrawLabel(SkCanvas* canvas, const SkRect& rect, SkScalar start, SkScalar sweep) {
120 SkPaint paint;
121
122 paint.setAntiAlias(true);
123 paint.setTextAlign(SkPaint::kCenter_Align);
124
125 SkString str;
126
127 str.appendScalar(start);
128 str.append(", ");
129 str.appendScalar(sweep);
130 canvas->drawString(str, rect.centerX(),
131 rect.fBottom + paint.getTextSize() * 5/4, paint);
132 }
133
DrawArcs(SkCanvas * canvas)134 static void DrawArcs(SkCanvas* canvas) {
135 SkPaint paint;
136 SkRect r;
137 SkScalar w = 75;
138 SkScalar h = 50;
139
140 r.set(0, 0, w, h);
141 paint.setAntiAlias(true);
142 paint.setStyle(SkPaint::kStroke_Style);
143
144 canvas->save();
145 canvas->translate(SkIntToScalar(10), SkIntToScalar(300));
146
147 paint.setStrokeWidth(SkIntToScalar(1));
148
149 static const SkScalar gAngles[] = {
150 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 };
160
161 for (size_t i = 0; i < SK_ARRAY_COUNT(gAngles); i += 2) {
162 paint.setColor(SK_ColorBLACK);
163 DrawRectWithLines(canvas, r, paint);
164
165 paint.setColor(SK_ColorRED);
166 canvas->drawArc(r, gAngles[i], gAngles[i+1], false, paint);
167
168 DrawLabel(canvas, r, gAngles[i], gAngles[i+1]);
169
170 canvas->translate(w * 8 / 7, 0);
171 }
172
173 canvas->restore();
174 }
175
drawRoot(SkCanvas * canvas)176 void drawRoot(SkCanvas* canvas) {
177 SkPaint paint;
178 paint.setAntiAlias(true);
179 paint.setStrokeWidth(SkIntToScalar(2));
180 paint.setStyle(SkPaint::kStroke_Style);
181
182 DrawRectWithLines(canvas, fRect, paint);
183
184 canvas->drawDrawable(fAnimatingDrawable.get());
185
186 DrawArcs(canvas);
187 }
188
onDrawContent(SkCanvas * canvas)189 void onDrawContent(SkCanvas* canvas) override {
190 canvas->drawDrawable(fRootDrawable.get());
191 }
192
onAnimate(const SkAnimTimer & timer)193 bool onAnimate(const SkAnimTimer& timer) override {
194 SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360));
195 fAnimatingDrawable->setSweep(angle);
196 return true;
197 }
198
199 private:
200 SkScalar fSweep;
201
202 typedef SampleView INHERITED;
203 };
204
205 //////////////////////////////////////////////////////////////////////////////
206
MyFactory()207 static SkView* MyFactory() { return new ArcsView; }
208 static SkViewRegister reg(MyFactory);
209