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