blob: c9310d6dd64b1fae55b1349bba10f102cd7489d4 [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 }
75 };
76
reed@android.com8a1c16f2008-12-17 15:59:43 +000077public:
reed6a070dc2014-11-11 19:36:09 -080078 SkRect fRect;
79 MyDrawable* fDrawable;
80
rmistry@google.comae933ce2012-08-23 18:19:56 +000081 ArcsView() {
reed@android.combbff1d52009-06-05 16:21:03 +000082 testparse();
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 fSweep = SkIntToScalar(100);
reed@google.com961ddb02011-05-05 14:03:48 +000084 this->setBGColor(0xFFDDDDDD);
reed6a070dc2014-11-11 19:36:09 -080085
86 fRect.set(0, 0, SkIntToScalar(200), SkIntToScalar(200));
87 fRect.offset(SkIntToScalar(20), SkIntToScalar(20));
88 fDrawable = SkNEW_ARGS(MyDrawable, (fRect));
89 }
90
91 virtual ~ArcsView() SK_OVERRIDE {
92 fDrawable->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 }
94
95protected:
96 // overrides from SkEventSink
reed@google.com961ddb02011-05-05 14:03:48 +000097 virtual bool onQuery(SkEvent* evt) {
98 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 SampleCode::TitleR(evt, "Arcs");
100 return true;
101 }
102 return this->INHERITED::onQuery(evt);
103 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000104
reed@google.com961ddb02011-05-05 14:03:48 +0000105 static void drawRectWithLines(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 canvas->drawRect(r, p);
107 canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, p);
108 canvas->drawLine(r.fLeft, r.fBottom, r.fRight, r.fTop, p);
109 canvas->drawLine(r.fLeft, r.centerY(), r.fRight, r.centerY(), p);
110 canvas->drawLine(r.centerX(), r.fTop, r.centerX(), r.fBottom, p);
111 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000112
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113 static void draw_label(SkCanvas* canvas, const SkRect& rect,
reed@google.com961ddb02011-05-05 14:03:48 +0000114 int start, int sweep) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000116
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 paint.setAntiAlias(true);
118 paint.setTextAlign(SkPaint::kCenter_Align);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000119
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 SkString str;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000121
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122 str.appendS32(start);
123 str.append(", ");
124 str.appendS32(sweep);
125 canvas->drawText(str.c_str(), str.size(), rect.centerX(),
126 rect.fBottom + paint.getTextSize() * 5/4, paint);
127 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000128
reed@google.com961ddb02011-05-05 14:03:48 +0000129 static void drawArcs(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 SkPaint paint;
131 SkRect r;
132 SkScalar w = SkIntToScalar(75);
133 SkScalar h = SkIntToScalar(50);
134
135 r.set(0, 0, w, h);
136 paint.setAntiAlias(true);
137 paint.setStyle(SkPaint::kStroke_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000138
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 canvas->save();
140 canvas->translate(SkIntToScalar(10), SkIntToScalar(300));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000141
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 paint.setStrokeWidth(SkIntToScalar(1));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000143
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144 static const int gAngles[] = {
145 0, 360,
146 0, 45,
147 0, -45,
148 720, 135,
149 -90, 269,
150 -90, 270,
151 -90, 271,
152 -180, -270,
153 225, 90
154 };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000155
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000156 for (size_t i = 0; i < SK_ARRAY_COUNT(gAngles); i += 2) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157 paint.setColor(SK_ColorBLACK);
158 drawRectWithLines(canvas, r, paint);
159
160 paint.setColor(SK_ColorRED);
161 canvas->drawArc(r, SkIntToScalar(gAngles[i]),
162 SkIntToScalar(gAngles[i+1]), false, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000163
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164 draw_label(canvas, r, gAngles[i], gAngles[i+1]);
165
166 canvas->translate(w * 8 / 7, 0);
167 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000168
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169 canvas->restore();
170 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000171
reed@google.com961ddb02011-05-05 14:03:48 +0000172 virtual void onDrawContent(SkCanvas* canvas) {
reed6a070dc2014-11-11 19:36:09 -0800173 fDrawable->setSweep(SampleCode::GetAnimScalar(SkIntToScalar(360)/24,
174 SkIntToScalar(360)));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 SkPaint paint;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177 paint.setAntiAlias(true);
178 paint.setStrokeWidth(SkIntToScalar(2));
179 paint.setStyle(SkPaint::kStroke_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000180
reed6a070dc2014-11-11 19:36:09 -0800181 drawRectWithLines(canvas, fRect, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000182
reed6a070dc2014-11-11 19:36:09 -0800183 canvas->EXPERIMENTAL_drawDrawable(fDrawable);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000184
reed@android.com8a1c16f2008-12-17 15:59:43 +0000185 drawArcs(canvas);
186 this->inval(NULL);
187 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000188
reed@google.com4d5c26d2013-01-08 16:17:50 +0000189 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
190 unsigned modi) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000191 // fSweep += SK_Scalar1;
192 this->inval(NULL);
reed@google.com4d5c26d2013-01-08 16:17:50 +0000193 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000194 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000195
reed@google.com961ddb02011-05-05 14:03:48 +0000196 virtual bool onClick(Click* click) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197 return this->INHERITED::onClick(click);
198 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000199
reed@android.com8a1c16f2008-12-17 15:59:43 +0000200private:
201 SkScalar fSweep;
202
reed@google.com961ddb02011-05-05 14:03:48 +0000203 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000204};
205
206//////////////////////////////////////////////////////////////////////////////
207
208static SkView* MyFactory() { return new ArcsView; }
209static SkViewRegister reg(MyFactory);