blob: a44eeb59ba1545cd30d5a2183355c48e71166e98 [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
reed6a070dc2014-11-11 19:36:09 -080025#include "SkCanvasDrawable.h"
26
reed@android.combbff1d52009-06-05 16:21:03 +000027#include "SkParsePath.h"
28static void testparse() {
29 SkRect r;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000030 r.set(0, 0, 10, 10.5f);
reed@android.combbff1d52009-06-05 16:21:03 +000031 SkPath p, p2;
32 SkString str, str2;
33
34 p.addRect(r);
35 SkParsePath::ToSVGString(p, &str);
36 SkParsePath::FromSVGString(str.c_str(), &p2);
37 SkParsePath::ToSVGString(p2, &str2);
38}
39
reed@google.com961ddb02011-05-05 14:03:48 +000040class ArcsView : public SampleView {
reed6a070dc2014-11-11 19:36:09 -080041 class MyDrawable : public SkCanvasDrawable {
42 SkRect fR;
43 SkScalar fSweep;
44 public:
45 MyDrawable(const SkRect& r) : fR(r), fSweep(0) {
46 }
47
48 void setSweep(SkScalar sweep) {
49 if (fSweep != sweep) {
50 fSweep = sweep;
51 this->notifyDrawingChanged();
52 }
53 }
54
55 void onDraw(SkCanvas* canvas) SK_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 }
reed6be2aa92014-11-18 11:08:05 -080075
76 SkRect onGetBounds() SK_OVERRIDE {
77 SkRect r(fR);
78 r.outset(2, 2);
79 return r;
80 }
reed6a070dc2014-11-11 19:36:09 -080081 };
82
reed@android.com8a1c16f2008-12-17 15:59:43 +000083public:
reed6a070dc2014-11-11 19:36:09 -080084 SkRect fRect;
85 MyDrawable* fDrawable;
86
rmistry@google.comae933ce2012-08-23 18:19:56 +000087 ArcsView() {
reed@android.combbff1d52009-06-05 16:21:03 +000088 testparse();
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 fSweep = SkIntToScalar(100);
reed@google.com961ddb02011-05-05 14:03:48 +000090 this->setBGColor(0xFFDDDDDD);
reed6a070dc2014-11-11 19:36:09 -080091
92 fRect.set(0, 0, SkIntToScalar(200), SkIntToScalar(200));
93 fRect.offset(SkIntToScalar(20), SkIntToScalar(20));
94 fDrawable = SkNEW_ARGS(MyDrawable, (fRect));
95 }
96
97 virtual ~ArcsView() SK_OVERRIDE {
98 fDrawable->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 }
100
101protected:
102 // overrides from SkEventSink
reed@google.com961ddb02011-05-05 14:03:48 +0000103 virtual bool onQuery(SkEvent* evt) {
104 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 SampleCode::TitleR(evt, "Arcs");
106 return true;
107 }
108 return this->INHERITED::onQuery(evt);
109 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000110
reed@google.com961ddb02011-05-05 14:03:48 +0000111 static void drawRectWithLines(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112 canvas->drawRect(r, p);
113 canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, p);
114 canvas->drawLine(r.fLeft, r.fBottom, r.fRight, r.fTop, p);
115 canvas->drawLine(r.fLeft, r.centerY(), r.fRight, r.centerY(), p);
116 canvas->drawLine(r.centerX(), r.fTop, r.centerX(), r.fBottom, p);
117 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000118
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 static void draw_label(SkCanvas* canvas, const SkRect& rect,
reed@google.com961ddb02011-05-05 14:03:48 +0000120 int start, int sweep) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000122
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 paint.setAntiAlias(true);
124 paint.setTextAlign(SkPaint::kCenter_Align);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000125
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 SkString str;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000127
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 str.appendS32(start);
129 str.append(", ");
130 str.appendS32(sweep);
131 canvas->drawText(str.c_str(), str.size(), rect.centerX(),
132 rect.fBottom + paint.getTextSize() * 5/4, paint);
133 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000134
reed@google.com961ddb02011-05-05 14:03:48 +0000135 static void drawArcs(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 SkPaint paint;
137 SkRect r;
138 SkScalar w = SkIntToScalar(75);
139 SkScalar h = SkIntToScalar(50);
140
141 r.set(0, 0, w, h);
142 paint.setAntiAlias(true);
143 paint.setStyle(SkPaint::kStroke_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000144
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 canvas->save();
146 canvas->translate(SkIntToScalar(10), SkIntToScalar(300));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000147
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148 paint.setStrokeWidth(SkIntToScalar(1));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000149
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 static const int gAngles[] = {
151 0, 360,
152 0, 45,
153 0, -45,
154 720, 135,
155 -90, 269,
156 -90, 270,
157 -90, 271,
158 -180, -270,
159 225, 90
160 };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000161
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000162 for (size_t i = 0; i < SK_ARRAY_COUNT(gAngles); i += 2) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 paint.setColor(SK_ColorBLACK);
164 drawRectWithLines(canvas, r, paint);
165
166 paint.setColor(SK_ColorRED);
167 canvas->drawArc(r, SkIntToScalar(gAngles[i]),
168 SkIntToScalar(gAngles[i+1]), false, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000169
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170 draw_label(canvas, r, gAngles[i], gAngles[i+1]);
171
172 canvas->translate(w * 8 / 7, 0);
173 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000174
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175 canvas->restore();
176 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000177
reed@google.com961ddb02011-05-05 14:03:48 +0000178 virtual void onDrawContent(SkCanvas* canvas) {
reed6a070dc2014-11-11 19:36:09 -0800179 fDrawable->setSweep(SampleCode::GetAnimScalar(SkIntToScalar(360)/24,
180 SkIntToScalar(360)));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000181
reed@android.com8a1c16f2008-12-17 15:59:43 +0000182 SkPaint paint;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183 paint.setAntiAlias(true);
184 paint.setStrokeWidth(SkIntToScalar(2));
185 paint.setStyle(SkPaint::kStroke_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000186
reed6a070dc2014-11-11 19:36:09 -0800187 drawRectWithLines(canvas, fRect, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000188
reed6a070dc2014-11-11 19:36:09 -0800189 canvas->EXPERIMENTAL_drawDrawable(fDrawable);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000190
reed@android.com8a1c16f2008-12-17 15:59:43 +0000191 drawArcs(canvas);
192 this->inval(NULL);
193 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000194
reed@google.com4d5c26d2013-01-08 16:17:50 +0000195 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
196 unsigned modi) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197 // fSweep += SK_Scalar1;
198 this->inval(NULL);
reed@google.com4d5c26d2013-01-08 16:17:50 +0000199 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000200 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000201
reed@google.com961ddb02011-05-05 14:03:48 +0000202 virtual bool onClick(Click* click) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000203 return this->INHERITED::onClick(click);
204 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000205
reed@android.com8a1c16f2008-12-17 15:59:43 +0000206private:
207 SkScalar fSweep;
208
reed@google.com961ddb02011-05-05 14:03:48 +0000209 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000210};
211
212//////////////////////////////////////////////////////////////////////////////
213
214static SkView* MyFactory() { return new ArcsView; }
215static SkViewRegister reg(MyFactory);