blob: f95833e83035f5afed4060230460cbbb36ff3a15 [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;
reedca2622b2016-03-18 07:25:55 -070084 sk_sp<MyDrawable> fAnimatingDrawable;
85 sk_sp<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));
reedca2622b2016-03-18 07:25:55 -070094 fAnimatingDrawable = sk_make_sp<MyDrawable>(fRect);
reed1bdfd3f2014-11-24 14:41:51 -080095
96 SkPictureRecorder recorder;
97 this->drawRoot(recorder.beginRecording(SkRect::MakeWH(800, 500)));
reedca2622b2016-03-18 07:25:55 -070098 fRootDrawable = recorder.finishRecordingAsDrawable();
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 }
100
101protected:
102 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -0700103 bool onQuery(SkEvent* evt) override {
reed@google.com961ddb02011-05-05 14:03:48 +0000104 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
reed1bdfd3f2014-11-24 14:41:51 -0800111 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
reed61adb1b2015-02-09 13:01:05 -0800119 static void DrawLabel(SkCanvas* canvas, const SkRect& rect, SkScalar start, SkScalar sweep) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000121
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122 paint.setAntiAlias(true);
123 paint.setTextAlign(SkPaint::kCenter_Align);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000124
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 SkString str;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000126
reed61adb1b2015-02-09 13:01:05 -0800127 str.appendScalar(start);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 str.append(", ");
reed61adb1b2015-02-09 13:01:05 -0800129 str.appendScalar(sweep);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 canvas->drawText(str.c_str(), str.size(), rect.centerX(),
131 rect.fBottom + paint.getTextSize() * 5/4, paint);
132 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000133
reed1bdfd3f2014-11-24 14:41:51 -0800134 static void DrawArcs(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 SkPaint paint;
136 SkRect r;
reed61adb1b2015-02-09 13:01:05 -0800137 SkScalar w = 75;
138 SkScalar h = 50;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139
140 r.set(0, 0, w, h);
141 paint.setAntiAlias(true);
142 paint.setStyle(SkPaint::kStroke_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000143
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144 canvas->save();
145 canvas->translate(SkIntToScalar(10), SkIntToScalar(300));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000146
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 paint.setStrokeWidth(SkIntToScalar(1));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000148
reed61adb1b2015-02-09 13:01:05 -0800149 static const SkScalar gAngles[] = {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 0, 360,
151 0, 45,
152 0, -45,
153 720, 135,
154 -90, 269,
155 -90, 270,
156 -90, 271,
157 -180, -270,
158 225, 90
159 };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000160
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000161 for (size_t i = 0; i < SK_ARRAY_COUNT(gAngles); i += 2) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162 paint.setColor(SK_ColorBLACK);
reed1bdfd3f2014-11-24 14:41:51 -0800163 DrawRectWithLines(canvas, r, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164
165 paint.setColor(SK_ColorRED);
reed61adb1b2015-02-09 13:01:05 -0800166 canvas->drawArc(r, gAngles[i], gAngles[i+1], false, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000167
reed1bdfd3f2014-11-24 14:41:51 -0800168 DrawLabel(canvas, r, gAngles[i], gAngles[i+1]);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169
170 canvas->translate(w * 8 / 7, 0);
171 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000172
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 canvas->restore();
174 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000175
reed1bdfd3f2014-11-24 14:41:51 -0800176 void drawRoot(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177 SkPaint paint;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178 paint.setAntiAlias(true);
179 paint.setStrokeWidth(SkIntToScalar(2));
180 paint.setStyle(SkPaint::kStroke_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000181
reed1bdfd3f2014-11-24 14:41:51 -0800182 DrawRectWithLines(canvas, fRect, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000183
reedca2622b2016-03-18 07:25:55 -0700184 canvas->drawDrawable(fAnimatingDrawable.get());
rmistry@google.comae933ce2012-08-23 18:19:56 +0000185
reed1bdfd3f2014-11-24 14:41:51 -0800186 DrawArcs(canvas);
187 }
188
mtklein36352bf2015-03-25 18:17:31 -0700189 void onDrawContent(SkCanvas* canvas) override {
reedca2622b2016-03-18 07:25:55 -0700190 canvas->drawDrawable(fRootDrawable.get());
reedd9adfe62015-02-01 19:01:04 -0800191 }
192
mtklein36352bf2015-03-25 18:17:31 -0700193 bool onAnimate(const SkAnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -0800194 SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360));
reedd9adfe62015-02-01 19:01:04 -0800195 fAnimatingDrawable->setSweep(angle);
196 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000198
mtklein36352bf2015-03-25 18:17:31 -0700199 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000200 // fSweep += SK_Scalar1;
halcanary96fcdcc2015-08-27 07:41:13 -0700201 this->inval(nullptr);
reed@google.com4d5c26d2013-01-08 16:17:50 +0000202 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000203 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000204
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205private:
206 SkScalar fSweep;
207
reed@google.com961ddb02011-05-05 14:03:48 +0000208 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000209};
210
211//////////////////////////////////////////////////////////////////////////////
212
213static SkView* MyFactory() { return new ArcsView; }
214static SkViewRegister reg(MyFactory);