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 "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
26 #include "include/utils/SkParsePath.h"
testparse()27 static void testparse() {
28 SkRect r;
29 r.setLTRB(0, 0, 10, 10.5f);
30 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
39 class ArcsView : public Sample {
40 class MyDrawable : public SkDrawable {
41 SkRect fR;
42 SkScalar fSweep;
43 public:
MyDrawable(const SkRect & r)44 MyDrawable(const SkRect& r) : fR(r), fSweep(0) {}
45
setSweep(SkScalar sweep)46 void setSweep(SkScalar sweep) {
47 if (fSweep != sweep) {
48 fSweep = sweep;
49 this->notifyDrawingChanged();
50 }
51 }
52
onDraw(SkCanvas * canvas)53 void onDraw(SkCanvas* canvas) override {
54 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 }
73
onGetBounds()74 SkRect onGetBounds() override {
75 SkRect r(fR);
76 r.outset(2, 2);
77 return r;
78 }
79 };
80
81 SkRect fRect = {20, 20, 220, 220};
82 sk_sp<MyDrawable> fAnimatingDrawable;
83 sk_sp<SkDrawable> fRootDrawable;
84
name()85 SkString name() override { return SkString("Arcs"); }
86
DrawRectWithLines(SkCanvas * canvas,const SkRect & r,const SkPaint & p)87 static void DrawRectWithLines(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
88 canvas->drawRect(r, p);
89 canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, p);
90 canvas->drawLine(r.fLeft, r.fBottom, r.fRight, r.fTop, p);
91 canvas->drawLine(r.fLeft, r.centerY(), r.fRight, r.centerY(), p);
92 canvas->drawLine(r.centerX(), r.fTop, r.centerX(), r.fBottom, p);
93 }
94
DrawLabel(SkCanvas * canvas,const SkRect & rect,SkScalar start,SkScalar sweep)95 static void DrawLabel(SkCanvas* canvas, const SkRect& rect, SkScalar start, SkScalar sweep) {
96 SkFont font;
97 SkString str;
98 str.appendScalar(start);
99 str.append(", ");
100 str.appendScalar(sweep);
101 SkTextUtils::DrawString(canvas, str.c_str(), rect.centerX(),
102 rect.fBottom + font.getSize() * 5/4, font, SkPaint(),
103 SkTextUtils::kCenter_Align);
104 }
105
DrawArcs(SkCanvas * canvas)106 static void DrawArcs(SkCanvas* canvas) {
107 SkPaint paint;
108 SkRect r;
109 SkScalar w = 75;
110 SkScalar h = 50;
111
112 r.setWH(w, h);
113 paint.setAntiAlias(true);
114 paint.setStyle(SkPaint::kStroke_Style);
115
116 canvas->save();
117 canvas->translate(SkIntToScalar(10), SkIntToScalar(300));
118
119 paint.setStrokeWidth(SkIntToScalar(1));
120
121 static const SkScalar gAngles[] = {
122 0, 360,
123 0, 45,
124 0, -45,
125 720, 135,
126 -90, 269,
127 -90, 270,
128 -90, 271,
129 -180, -270,
130 225, 90
131 };
132
133 for (size_t i = 0; i < SK_ARRAY_COUNT(gAngles); i += 2) {
134 paint.setColor(SK_ColorBLACK);
135 DrawRectWithLines(canvas, r, paint);
136
137 paint.setColor(SK_ColorRED);
138 canvas->drawArc(r, gAngles[i], gAngles[i+1], false, paint);
139
140 DrawLabel(canvas, r, gAngles[i], gAngles[i+1]);
141
142 canvas->translate(w * 8 / 7, 0);
143 }
144
145 canvas->restore();
146 }
147
drawRoot(SkCanvas * canvas)148 void drawRoot(SkCanvas* canvas) {
149 SkPaint paint;
150 paint.setAntiAlias(true);
151 paint.setStrokeWidth(SkIntToScalar(2));
152 paint.setStyle(SkPaint::kStroke_Style);
153
154 DrawRectWithLines(canvas, fRect, paint);
155
156 canvas->drawDrawable(fAnimatingDrawable.get());
157
158 DrawArcs(canvas);
159 }
160
onOnceBeforeDraw()161 void onOnceBeforeDraw() override {
162 testparse();
163 this->setBGColor(0xFFDDDDDD);
164
165 fAnimatingDrawable = sk_make_sp<MyDrawable>(fRect);
166
167 SkPictureRecorder recorder;
168 this->drawRoot(recorder.beginRecording(SkRect::MakeWH(800, 500)));
169 fRootDrawable = recorder.finishRecordingAsDrawable();
170 }
171
onDrawContent(SkCanvas * canvas)172 void onDrawContent(SkCanvas* canvas) override {
173 canvas->drawDrawable(fRootDrawable.get());
174 }
175
onAnimate(double nanos)176 bool onAnimate(double nanos) override {
177 SkScalar angle = SkDoubleToScalar(fmod(1e-9 * nanos * 360 / 24, 360));
178 if (fAnimatingDrawable) {
179 fAnimatingDrawable->setSweep(angle);
180 }
181 return true;
182 }
183 };
184
185 DEF_SAMPLE( return new ArcsView(); )
186