blob: d418a4b8f422d838f9d8830ff2b585c43c66f0d4 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkGradientShader.h"
12#include "SkPath.h"
13#include "SkRegion.h"
14#include "SkShader.h"
15#include "SkUtils.h"
reed@android.com64ad9662008-12-19 19:15:15 +000016#include "SkComposeShader.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017#include "Sk1DPathEffect.h"
18#include "SkCornerPathEffect.h"
19#include "SkPathMeasure.h"
20#include "SkRandom.h"
21#include "SkColorPriv.h"
22#include "SkColorFilter.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000023#include "SkLayerRasterizer.h"
24
reed@android.combbff1d52009-06-05 16:21:03 +000025#include "SkParsePath.h"
26static void testparse() {
27 SkRect r;
28 r.set(0, 0, SkFloatToScalar(10), SkFloatToScalar(10.5));
29 SkPath p, p2;
30 SkString str, str2;
31
32 p.addRect(r);
33 SkParsePath::ToSVGString(p, &str);
34 SkParsePath::FromSVGString(str.c_str(), &p2);
35 SkParsePath::ToSVGString(p2, &str2);
36}
37
reed@google.com961ddb02011-05-05 14:03:48 +000038class ArcsView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000039public:
reed@google.com961ddb02011-05-05 14:03:48 +000040 ArcsView() {
reed@android.combbff1d52009-06-05 16:21:03 +000041 testparse();
reed@android.com8a1c16f2008-12-17 15:59:43 +000042 fSweep = SkIntToScalar(100);
reed@google.com961ddb02011-05-05 14:03:48 +000043 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 }
45
46protected:
47 // overrides from SkEventSink
reed@google.com961ddb02011-05-05 14:03:48 +000048 virtual bool onQuery(SkEvent* evt) {
49 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 SampleCode::TitleR(evt, "Arcs");
51 return true;
52 }
53 return this->INHERITED::onQuery(evt);
54 }
55
reed@google.com961ddb02011-05-05 14:03:48 +000056 static void drawRectWithLines(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 canvas->drawRect(r, p);
58 canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, p);
59 canvas->drawLine(r.fLeft, r.fBottom, r.fRight, r.fTop, p);
60 canvas->drawLine(r.fLeft, r.centerY(), r.fRight, r.centerY(), p);
61 canvas->drawLine(r.centerX(), r.fTop, r.centerX(), r.fBottom, p);
62 }
63
64 static void draw_label(SkCanvas* canvas, const SkRect& rect,
reed@google.com961ddb02011-05-05 14:03:48 +000065 int start, int sweep) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 SkPaint paint;
67
68 paint.setAntiAlias(true);
69 paint.setTextAlign(SkPaint::kCenter_Align);
70
71 SkString str;
72
73 str.appendS32(start);
74 str.append(", ");
75 str.appendS32(sweep);
76 canvas->drawText(str.c_str(), str.size(), rect.centerX(),
77 rect.fBottom + paint.getTextSize() * 5/4, paint);
78 }
79
reed@google.com961ddb02011-05-05 14:03:48 +000080 static void drawArcs(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 SkPaint paint;
82 SkRect r;
83 SkScalar w = SkIntToScalar(75);
84 SkScalar h = SkIntToScalar(50);
85
86 r.set(0, 0, w, h);
87 paint.setAntiAlias(true);
88 paint.setStyle(SkPaint::kStroke_Style);
89
90 canvas->save();
91 canvas->translate(SkIntToScalar(10), SkIntToScalar(300));
92
93 paint.setStrokeWidth(SkIntToScalar(1));
94
95 static const int gAngles[] = {
96 0, 360,
97 0, 45,
98 0, -45,
99 720, 135,
100 -90, 269,
101 -90, 270,
102 -90, 271,
103 -180, -270,
104 225, 90
105 };
106
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000107 for (size_t i = 0; i < SK_ARRAY_COUNT(gAngles); i += 2) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108 paint.setColor(SK_ColorBLACK);
109 drawRectWithLines(canvas, r, paint);
110
111 paint.setColor(SK_ColorRED);
112 canvas->drawArc(r, SkIntToScalar(gAngles[i]),
113 SkIntToScalar(gAngles[i+1]), false, paint);
114
115 draw_label(canvas, r, gAngles[i], gAngles[i+1]);
116
117 canvas->translate(w * 8 / 7, 0);
118 }
119
120 canvas->restore();
121 }
122
reed@google.com961ddb02011-05-05 14:03:48 +0000123 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com44177402009-11-23 21:07:51 +0000124 fSweep = SampleCode::GetAnimScalar(SkIntToScalar(360)/24,
125 SkIntToScalar(360));
reed@android.com7d970c72010-04-22 16:07:49 +0000126// fSweep = SkFloatToScalar(359.99f);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127
128 SkRect r;
129 SkPaint paint;
130
131 paint.setAntiAlias(true);
132 paint.setStrokeWidth(SkIntToScalar(2));
133 paint.setStyle(SkPaint::kStroke_Style);
134
135 r.set(0, 0, SkIntToScalar(200), SkIntToScalar(200));
136 r.offset(SkIntToScalar(20), SkIntToScalar(20));
137
138 if (false) {
139 const SkScalar d = SkIntToScalar(3);
140 const SkScalar rad[] = { d, d, d, d, d, d, d, d };
141 SkPath path;
142 path.addRoundRect(r, rad);
143 canvas->drawPath(path, paint);
144 return;
145 }
146
147 drawRectWithLines(canvas, r, paint);
148
149 // printf("----- sweep %g %X\n", SkScalarToFloat(fSweep), SkDegreesToRadians(fSweep));
150
151
152 paint.setStyle(SkPaint::kFill_Style);
153 paint.setColor(0x800000FF);
154 canvas->drawArc(r, 0, fSweep, true, paint);
155
156 paint.setColor(0x800FF000);
157 canvas->drawArc(r, 0, fSweep, false, paint);
158
159 paint.setStyle(SkPaint::kStroke_Style);
160 paint.setColor(SK_ColorRED);
161 canvas->drawArc(r, 0, fSweep, true, paint);
162
163 paint.setStrokeWidth(0);
164 paint.setColor(SK_ColorBLUE);
165 canvas->drawArc(r, 0, fSweep, false, paint);
166
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167 drawArcs(canvas);
168 this->inval(NULL);
169 }
170
reed@google.com961ddb02011-05-05 14:03:48 +0000171 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172 // fSweep += SK_Scalar1;
173 this->inval(NULL);
174 return this->INHERITED::onFindClickHandler(x, y);
175 }
176
reed@google.com961ddb02011-05-05 14:03:48 +0000177 virtual bool onClick(Click* click) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178 return this->INHERITED::onClick(click);
179 }
180
181private:
182 SkScalar fSweep;
183
reed@google.com961ddb02011-05-05 14:03:48 +0000184 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000185};
186
187//////////////////////////////////////////////////////////////////////////////
188
189static SkView* MyFactory() { return new ArcsView; }
190static SkViewRegister reg(MyFactory);
191