blob: 19bf5f8533c9a40891e53b055cc81b157750cdaf [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001#include "SampleCode.h"
2#include "SkView.h"
3#include "SkCanvas.h"
4#include "SkGradientShader.h"
5#include "SkPath.h"
6#include "SkRegion.h"
7#include "SkShader.h"
8#include "SkUtils.h"
reed@android.com64ad9662008-12-19 19:15:15 +00009#include "SkComposeShader.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "Sk1DPathEffect.h"
11#include "SkCornerPathEffect.h"
12#include "SkPathMeasure.h"
13#include "SkRandom.h"
14#include "SkColorPriv.h"
15#include "SkColorFilter.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016#include "SkLayerRasterizer.h"
17
reed@android.combbff1d52009-06-05 16:21:03 +000018#include "SkParsePath.h"
19static void testparse() {
20 SkRect r;
21 r.set(0, 0, SkFloatToScalar(10), SkFloatToScalar(10.5));
22 SkPath p, p2;
23 SkString str, str2;
24
25 p.addRect(r);
26 SkParsePath::ToSVGString(p, &str);
27 SkParsePath::FromSVGString(str.c_str(), &p2);
28 SkParsePath::ToSVGString(p2, &str2);
29}
30
reed@android.com8a1c16f2008-12-17 15:59:43 +000031class ArcsView : public SkView {
32public:
33 ArcsView()
34 {
reed@android.combbff1d52009-06-05 16:21:03 +000035 testparse();
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 fSweep = SkIntToScalar(100);
37 }
38
39protected:
40 // overrides from SkEventSink
41 virtual bool onQuery(SkEvent* evt)
42 {
43 if (SampleCode::TitleQ(*evt))
44 {
45 SampleCode::TitleR(evt, "Arcs");
46 return true;
47 }
48 return this->INHERITED::onQuery(evt);
49 }
50
51 void drawBG(SkCanvas* canvas)
52 {
53 canvas->drawColor(0xFFDDDDDD);
54 }
55
56 static void drawRectWithLines(SkCanvas* canvas, const SkRect& r, const SkPaint& p)
57 {
58 canvas->drawRect(r, p);
59 canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, p);
60 canvas->drawLine(r.fLeft, r.fBottom, r.fRight, r.fTop, p);
61 canvas->drawLine(r.fLeft, r.centerY(), r.fRight, r.centerY(), p);
62 canvas->drawLine(r.centerX(), r.fTop, r.centerX(), r.fBottom, p);
63 }
64
65 static void draw_label(SkCanvas* canvas, const SkRect& rect,
66 int start, int sweep)
67 {
68 SkPaint paint;
69
70 paint.setAntiAlias(true);
71 paint.setTextAlign(SkPaint::kCenter_Align);
72
73 SkString str;
74
75 str.appendS32(start);
76 str.append(", ");
77 str.appendS32(sweep);
78 canvas->drawText(str.c_str(), str.size(), rect.centerX(),
79 rect.fBottom + paint.getTextSize() * 5/4, paint);
80 }
81
82 static void drawArcs(SkCanvas* canvas)
83 {
84 SkPaint paint;
85 SkRect r;
86 SkScalar w = SkIntToScalar(75);
87 SkScalar h = SkIntToScalar(50);
88
89 r.set(0, 0, w, h);
90 paint.setAntiAlias(true);
91 paint.setStyle(SkPaint::kStroke_Style);
92
93 canvas->save();
94 canvas->translate(SkIntToScalar(10), SkIntToScalar(300));
95
96 paint.setStrokeWidth(SkIntToScalar(1));
97
98 static const int gAngles[] = {
99 0, 360,
100 0, 45,
101 0, -45,
102 720, 135,
103 -90, 269,
104 -90, 270,
105 -90, 271,
106 -180, -270,
107 225, 90
108 };
109
110 for (int i = 0; i < SK_ARRAY_COUNT(gAngles); i += 2)
111 {
112 paint.setColor(SK_ColorBLACK);
113 drawRectWithLines(canvas, r, paint);
114
115 paint.setColor(SK_ColorRED);
116 canvas->drawArc(r, SkIntToScalar(gAngles[i]),
117 SkIntToScalar(gAngles[i+1]), false, paint);
118
119 draw_label(canvas, r, gAngles[i], gAngles[i+1]);
120
121 canvas->translate(w * 8 / 7, 0);
122 }
123
124 canvas->restore();
125 }
126
127 virtual void onDraw(SkCanvas* canvas)
128 {
129 this->drawBG(canvas);
130
131 SkRect r;
132 SkPaint paint;
133
134 paint.setAntiAlias(true);
135 paint.setStrokeWidth(SkIntToScalar(2));
136 paint.setStyle(SkPaint::kStroke_Style);
137
138 r.set(0, 0, SkIntToScalar(200), SkIntToScalar(200));
139 r.offset(SkIntToScalar(20), SkIntToScalar(20));
140
141 if (false) {
142 const SkScalar d = SkIntToScalar(3);
143 const SkScalar rad[] = { d, d, d, d, d, d, d, d };
144 SkPath path;
145 path.addRoundRect(r, rad);
146 canvas->drawPath(path, paint);
147 return;
148 }
149
150 drawRectWithLines(canvas, r, paint);
151
152 // printf("----- sweep %g %X\n", SkScalarToFloat(fSweep), SkDegreesToRadians(fSweep));
153
154
155 paint.setStyle(SkPaint::kFill_Style);
156 paint.setColor(0x800000FF);
157 canvas->drawArc(r, 0, fSweep, true, paint);
158
159 paint.setColor(0x800FF000);
160 canvas->drawArc(r, 0, fSweep, false, paint);
161
162 paint.setStyle(SkPaint::kStroke_Style);
163 paint.setColor(SK_ColorRED);
164 canvas->drawArc(r, 0, fSweep, true, paint);
165
166 paint.setStrokeWidth(0);
167 paint.setColor(SK_ColorBLUE);
168 canvas->drawArc(r, 0, fSweep, false, paint);
169
170 fSweep += SK_Scalar1/4;
171 if (fSweep > SkIntToScalar(360))
172 fSweep = 0;
173
174 drawArcs(canvas);
175 this->inval(NULL);
176 }
177
178 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y)
179 {
180 // fSweep += SK_Scalar1;
181 this->inval(NULL);
182 return this->INHERITED::onFindClickHandler(x, y);
183 }
184
185 virtual bool onClick(Click* click)
186 {
187 return this->INHERITED::onClick(click);
188 }
189
190private:
191 SkScalar fSweep;
192
193 typedef SkView INHERITED;
194};
195
196//////////////////////////////////////////////////////////////////////////////
197
198static SkView* MyFactory() { return new ArcsView; }
199static SkViewRegister reg(MyFactory);
200