2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SampleCode.h"
9 #include "SkAnimTimer.h"
12 #include "SkDrawable.h"
13 #include "SkGradientShader.h"
18 #include "SkComposeShader.h"
19 #include "Sk1DPathEffect.h"
20 #include "SkCornerPathEffect.h"
21 #include "SkPathMeasure.h"
22 #include "SkPictureRecorder.h"
24 #include "SkColorPriv.h"
25 #include "SkColorFilter.h"
26 #include "SkLayerRasterizer.h"
28 #include "SkParsePath.h"
29 static void testparse() {
31 r.set(0, 0, 10, 10.5f);
36 SkParsePath::ToSVGString(p, &str);
37 SkParsePath::FromSVGString(str.c_str(), &p2);
38 SkParsePath::ToSVGString(p2, &str2);
41 class ArcsView : public SampleView {
42 class MyDrawable : public SkDrawable {
46 MyDrawable(const SkRect& r) : fR(r), fSweep(0) {}
48 void setSweep(SkScalar sweep) {
49 if (fSweep != sweep) {
51 this->notifyDrawingChanged();
55 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
57 paint.setAntiAlias(true);
58 paint.setStrokeWidth(SkIntToScalar(2));
60 paint.setStyle(SkPaint::kFill_Style);
61 paint.setColor(0x800000FF);
62 canvas->drawArc(fR, 0, fSweep, true, paint);
64 paint.setColor(0x800FF000);
65 canvas->drawArc(fR, 0, fSweep, false, paint);
67 paint.setStyle(SkPaint::kStroke_Style);
68 paint.setColor(SK_ColorRED);
69 canvas->drawArc(fR, 0, fSweep, true, paint);
71 paint.setStrokeWidth(0);
72 paint.setColor(SK_ColorBLUE);
73 canvas->drawArc(fR, 0, fSweep, false, paint);
76 SkRect onGetBounds() SK_OVERRIDE {
85 MyDrawable* fAnimatingDrawable;
86 SkDrawable* fRootDrawable;
90 fSweep = SkIntToScalar(100);
91 this->setBGColor(0xFFDDDDDD);
93 fRect.set(0, 0, SkIntToScalar(200), SkIntToScalar(200));
94 fRect.offset(SkIntToScalar(20), SkIntToScalar(20));
95 fAnimatingDrawable = SkNEW_ARGS(MyDrawable, (fRect));
97 SkPictureRecorder recorder;
98 this->drawRoot(recorder.beginRecording(SkRect::MakeWH(800, 500)));
99 fRootDrawable = recorder.endRecordingAsDrawable();
102 ~ArcsView() SK_OVERRIDE {
103 fAnimatingDrawable->unref();
104 fRootDrawable->unref();
108 // overrides from SkEventSink
109 bool onQuery(SkEvent* evt) SK_OVERRIDE {
110 if (SampleCode::TitleQ(*evt)) {
111 SampleCode::TitleR(evt, "Arcs");
114 return this->INHERITED::onQuery(evt);
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);
125 static void DrawLabel(SkCanvas* canvas, const SkRect& rect, SkScalar start, SkScalar sweep) {
128 paint.setAntiAlias(true);
129 paint.setTextAlign(SkPaint::kCenter_Align);
133 str.appendScalar(start);
135 str.appendScalar(sweep);
136 canvas->drawText(str.c_str(), str.size(), rect.centerX(),
137 rect.fBottom + paint.getTextSize() * 5/4, paint);
140 static void DrawArcs(SkCanvas* canvas) {
147 paint.setAntiAlias(true);
148 paint.setStyle(SkPaint::kStroke_Style);
151 canvas->translate(SkIntToScalar(10), SkIntToScalar(300));
153 paint.setStrokeWidth(SkIntToScalar(1));
155 static const SkScalar gAngles[] = {
167 for (size_t i = 0; i < SK_ARRAY_COUNT(gAngles); i += 2) {
168 paint.setColor(SK_ColorBLACK);
169 DrawRectWithLines(canvas, r, paint);
171 paint.setColor(SK_ColorRED);
172 canvas->drawArc(r, gAngles[i], gAngles[i+1], false, paint);
174 DrawLabel(canvas, r, gAngles[i], gAngles[i+1]);
176 canvas->translate(w * 8 / 7, 0);
182 void drawRoot(SkCanvas* canvas) {
184 paint.setAntiAlias(true);
185 paint.setStrokeWidth(SkIntToScalar(2));
186 paint.setStyle(SkPaint::kStroke_Style);
188 DrawRectWithLines(canvas, fRect, paint);
190 canvas->drawDrawable(fAnimatingDrawable);
195 void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
196 canvas->drawDrawable(fRootDrawable);
199 bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE {
200 SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360));
201 fAnimatingDrawable->setSweep(angle);
205 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) SK_OVERRIDE {
206 // fSweep += SK_Scalar1;
208 return this->INHERITED::onFindClickHandler(x, y, modi);
214 typedef SampleView INHERITED;
217 //////////////////////////////////////////////////////////////////////////////
219 static SkView* MyFactory() { return new ArcsView; }
220 static SkViewRegister reg(MyFactory);