blob: 3b5288dd08622ba72643a152a258c7c37db4468e [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reed76113a92015-02-02 12:55:02 -08007
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
reed76113a92015-02-02 12:55:02 -08009#include "SkAnimTimer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkView.h"
11#include "SkCanvas.h"
reed3cb38402015-02-06 08:36:15 -080012#include "SkDrawable.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkGradientShader.h"
14#include "SkPath.h"
15#include "SkRegion.h"
16#include "SkShader.h"
17#include "SkUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000018#include "Sk1DPathEffect.h"
19#include "SkCornerPathEffect.h"
20#include "SkPathMeasure.h"
reed1bdfd3f2014-11-24 14:41:51 -080021#include "SkPictureRecorder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022#include "SkRandom.h"
23#include "SkColorPriv.h"
24#include "SkColorFilter.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000025#include "SkLayerRasterizer.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 {
reed3cb38402015-02-06 08:36:15 -080041 class MyDrawable : public SkDrawable {
reed6a070dc2014-11-11 19:36:09 -080042 SkRect fR;
43 SkScalar fSweep;
44 public:
reed1bdfd3f2014-11-24 14:41:51 -080045 MyDrawable(const SkRect& r) : fR(r), fSweep(0) {}
reed6a070dc2014-11-11 19:36:09 -080046
47 void setSweep(SkScalar sweep) {
48 if (fSweep != sweep) {
49 fSweep = sweep;
50 this->notifyDrawingChanged();
51 }
52 }
53
mtklein36352bf2015-03-25 18:17:31 -070054 void onDraw(SkCanvas* canvas) override {
reed6a070dc2014-11-11 19:36:09 -080055 SkPaint paint;
56 paint.setAntiAlias(true);
57 paint.setStrokeWidth(SkIntToScalar(2));
58
59 paint.setStyle(SkPaint::kFill_Style);
60 paint.setColor(0x800000FF);
61 canvas->drawArc(fR, 0, fSweep, true, paint);
62
63 paint.setColor(0x800FF000);
64 canvas->drawArc(fR, 0, fSweep, false, paint);
65
66 paint.setStyle(SkPaint::kStroke_Style);
67 paint.setColor(SK_ColorRED);
68 canvas->drawArc(fR, 0, fSweep, true, paint);
69
70 paint.setStrokeWidth(0);
71 paint.setColor(SK_ColorBLUE);
72 canvas->drawArc(fR, 0, fSweep, false, paint);
73 }
reed6be2aa92014-11-18 11:08:05 -080074
mtklein36352bf2015-03-25 18:17:31 -070075 SkRect onGetBounds() override {
reed6be2aa92014-11-18 11:08:05 -080076 SkRect r(fR);
77 r.outset(2, 2);
78 return r;
79 }
reed6a070dc2014-11-11 19:36:09 -080080 };
81
reed@android.com8a1c16f2008-12-17 15:59:43 +000082public:
reed6a070dc2014-11-11 19:36:09 -080083 SkRect fRect;
reed1bdfd3f2014-11-24 14:41:51 -080084 MyDrawable* fAnimatingDrawable;
reed3cb38402015-02-06 08:36:15 -080085 SkDrawable* fRootDrawable;
reed6a070dc2014-11-11 19:36:09 -080086
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));
halcanary385fe4d2015-08-26 13:07:48 -070094 fAnimatingDrawable = new MyDrawable(fRect);
reed1bdfd3f2014-11-24 14:41:51 -080095
96 SkPictureRecorder recorder;
97 this->drawRoot(recorder.beginRecording(SkRect::MakeWH(800, 500)));
reed3cb38402015-02-06 08:36:15 -080098 fRootDrawable = recorder.endRecordingAsDrawable();
reed6a070dc2014-11-11 19:36:09 -080099 }
100
mtklein36352bf2015-03-25 18:17:31 -0700101 ~ArcsView() override {
reed1bdfd3f2014-11-24 14:41:51 -0800102 fAnimatingDrawable->unref();
103 fRootDrawable->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 }
105
106protected:
107 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -0700108 bool onQuery(SkEvent* evt) override {
reed@google.com961ddb02011-05-05 14:03:48 +0000109 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110 SampleCode::TitleR(evt, "Arcs");
111 return true;
112 }
113 return this->INHERITED::onQuery(evt);
114 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000115
reed1bdfd3f2014-11-24 14:41:51 -0800116 static void DrawRectWithLines(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 canvas->drawRect(r, p);
118 canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, p);
119 canvas->drawLine(r.fLeft, r.fBottom, r.fRight, r.fTop, p);
120 canvas->drawLine(r.fLeft, r.centerY(), r.fRight, r.centerY(), p);
121 canvas->drawLine(r.centerX(), r.fTop, r.centerX(), r.fBottom, p);
122 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000123
reed61adb1b2015-02-09 13:01:05 -0800124 static void DrawLabel(SkCanvas* canvas, const SkRect& rect, SkScalar start, SkScalar sweep) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000126
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127 paint.setAntiAlias(true);
128 paint.setTextAlign(SkPaint::kCenter_Align);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000129
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 SkString str;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000131
reed61adb1b2015-02-09 13:01:05 -0800132 str.appendScalar(start);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133 str.append(", ");
reed61adb1b2015-02-09 13:01:05 -0800134 str.appendScalar(sweep);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 canvas->drawText(str.c_str(), str.size(), rect.centerX(),
136 rect.fBottom + paint.getTextSize() * 5/4, paint);
137 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000138
reed1bdfd3f2014-11-24 14:41:51 -0800139 static void DrawArcs(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140 SkPaint paint;
141 SkRect r;
reed61adb1b2015-02-09 13:01:05 -0800142 SkScalar w = 75;
143 SkScalar h = 50;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144
145 r.set(0, 0, w, h);
146 paint.setAntiAlias(true);
147 paint.setStyle(SkPaint::kStroke_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000148
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149 canvas->save();
150 canvas->translate(SkIntToScalar(10), SkIntToScalar(300));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000151
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 paint.setStrokeWidth(SkIntToScalar(1));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000153
reed61adb1b2015-02-09 13:01:05 -0800154 static const SkScalar gAngles[] = {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 0, 360,
156 0, 45,
157 0, -45,
158 720, 135,
159 -90, 269,
160 -90, 270,
161 -90, 271,
162 -180, -270,
163 225, 90
164 };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000165
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000166 for (size_t i = 0; i < SK_ARRAY_COUNT(gAngles); i += 2) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167 paint.setColor(SK_ColorBLACK);
reed1bdfd3f2014-11-24 14:41:51 -0800168 DrawRectWithLines(canvas, r, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169
170 paint.setColor(SK_ColorRED);
reed61adb1b2015-02-09 13:01:05 -0800171 canvas->drawArc(r, gAngles[i], gAngles[i+1], false, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000172
reed1bdfd3f2014-11-24 14:41:51 -0800173 DrawLabel(canvas, r, gAngles[i], gAngles[i+1]);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174
175 canvas->translate(w * 8 / 7, 0);
176 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000177
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178 canvas->restore();
179 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000180
reed1bdfd3f2014-11-24 14:41:51 -0800181 void drawRoot(SkCanvas* canvas) {
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
reed1bdfd3f2014-11-24 14:41:51 -0800187 DrawRectWithLines(canvas, fRect, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000188
reed3cb38402015-02-06 08:36:15 -0800189 canvas->drawDrawable(fAnimatingDrawable);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000190
reed1bdfd3f2014-11-24 14:41:51 -0800191 DrawArcs(canvas);
192 }
193
mtklein36352bf2015-03-25 18:17:31 -0700194 void onDrawContent(SkCanvas* canvas) override {
reed3cb38402015-02-06 08:36:15 -0800195 canvas->drawDrawable(fRootDrawable);
reedd9adfe62015-02-01 19:01:04 -0800196 }
197
mtklein36352bf2015-03-25 18:17:31 -0700198 bool onAnimate(const SkAnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -0800199 SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360));
reedd9adfe62015-02-01 19:01:04 -0800200 fAnimatingDrawable->setSweep(angle);
201 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000202 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000203
mtklein36352bf2015-03-25 18:17:31 -0700204 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205 // fSweep += SK_Scalar1;
halcanary96fcdcc2015-08-27 07:41:13 -0700206 this->inval(nullptr);
reed@google.com4d5c26d2013-01-08 16:17:50 +0000207 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000208 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000209
reed@android.com8a1c16f2008-12-17 15:59:43 +0000210private:
211 SkScalar fSweep;
212
reed@google.com961ddb02011-05-05 14:03:48 +0000213 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000214};
215
216//////////////////////////////////////////////////////////////////////////////
217
218static SkView* MyFactory() { return new ArcsView; }
219static SkViewRegister reg(MyFactory);