blob: 8209c3b76489b2352726fe86381c15300e85eca6 [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;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000028 r.set(0, 0, 10, 10.5f);
reed@android.combbff1d52009-06-05 16:21:03 +000029 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:
rmistry@google.comae933ce2012-08-23 18:19:56 +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 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000055
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 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000063
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 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;
rmistry@google.comae933ce2012-08-23 18:19:56 +000067
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 paint.setAntiAlias(true);
69 paint.setTextAlign(SkPaint::kCenter_Align);
rmistry@google.comae933ce2012-08-23 18:19:56 +000070
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 SkString str;
rmistry@google.comae933ce2012-08-23 18:19:56 +000072
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 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 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000079
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);
rmistry@google.comae933ce2012-08-23 18:19:56 +000089
reed@android.com8a1c16f2008-12-17 15:59:43 +000090 canvas->save();
91 canvas->translate(SkIntToScalar(10), SkIntToScalar(300));
rmistry@google.comae933ce2012-08-23 18:19:56 +000092
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 paint.setStrokeWidth(SkIntToScalar(1));
rmistry@google.comae933ce2012-08-23 18:19:56 +000094
reed@android.com8a1c16f2008-12-17 15:59:43 +000095 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 };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000106
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);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000114
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115 draw_label(canvas, r, gAngles[i], gAngles[i+1]);
116
117 canvas->translate(w * 8 / 7, 0);
118 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000119
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 canvas->restore();
121 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000122
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));
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000126// fSweep = 359.99f;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127
128 SkRect r;
129 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000130
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131 paint.setAntiAlias(true);
132 paint.setStrokeWidth(SkIntToScalar(2));
133 paint.setStyle(SkPaint::kStroke_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000134
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 r.set(0, 0, SkIntToScalar(200), SkIntToScalar(200));
136 r.offset(SkIntToScalar(20), SkIntToScalar(20));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000137
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 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);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000148
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149 // printf("----- sweep %g %X\n", SkScalarToFloat(fSweep), SkDegreesToRadians(fSweep));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000150
151
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 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);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000162
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 paint.setStrokeWidth(0);
164 paint.setColor(SK_ColorBLUE);
165 canvas->drawArc(r, 0, fSweep, false, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000166
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167 drawArcs(canvas);
168 this->inval(NULL);
169 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000170
reed@google.com4d5c26d2013-01-08 16:17:50 +0000171 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
172 unsigned modi) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 // fSweep += SK_Scalar1;
174 this->inval(NULL);
reed@google.com4d5c26d2013-01-08 16:17:50 +0000175 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000177
reed@google.com961ddb02011-05-05 14:03:48 +0000178 virtual bool onClick(Click* click) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179 return this->INHERITED::onClick(click);
180 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000181
reed@android.com8a1c16f2008-12-17 15:59:43 +0000182private:
183 SkScalar fSweep;
184
reed@google.com961ddb02011-05-05 14:03:48 +0000185 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000186};
187
188//////////////////////////////////////////////////////////////////////////////
189
190static SkView* MyFactory() { return new ArcsView; }
191static SkViewRegister reg(MyFactory);