blob: 842e7a0ef96053350493077209b1f5b395da9398 [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.com64ad9662008-12-19 19:15:15 +000018#include "SkComposeShader.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019#include "Sk1DPathEffect.h"
20#include "SkCornerPathEffect.h"
21#include "SkPathMeasure.h"
reed1bdfd3f2014-11-24 14:41:51 -080022#include "SkPictureRecorder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000023#include "SkRandom.h"
24#include "SkColorPriv.h"
25#include "SkColorFilter.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000026#include "SkLayerRasterizer.h"
27
reed@android.combbff1d52009-06-05 16:21:03 +000028#include "SkParsePath.h"
29static void testparse() {
30 SkRect r;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000031 r.set(0, 0, 10, 10.5f);
reed@android.combbff1d52009-06-05 16:21:03 +000032 SkPath p, p2;
33 SkString str, str2;
34
35 p.addRect(r);
36 SkParsePath::ToSVGString(p, &str);
37 SkParsePath::FromSVGString(str.c_str(), &p2);
38 SkParsePath::ToSVGString(p2, &str2);
39}
40
reed@google.com961ddb02011-05-05 14:03:48 +000041class ArcsView : public SampleView {
reed3cb38402015-02-06 08:36:15 -080042 class MyDrawable : public SkDrawable {
reed6a070dc2014-11-11 19:36:09 -080043 SkRect fR;
44 SkScalar fSweep;
45 public:
reed1bdfd3f2014-11-24 14:41:51 -080046 MyDrawable(const SkRect& r) : fR(r), fSweep(0) {}
reed6a070dc2014-11-11 19:36:09 -080047
48 void setSweep(SkScalar sweep) {
49 if (fSweep != sweep) {
50 fSweep = sweep;
51 this->notifyDrawingChanged();
52 }
53 }
54
mtklein36352bf2015-03-25 18:17:31 -070055 void onDraw(SkCanvas* canvas) override {
reed6a070dc2014-11-11 19:36:09 -080056 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
mtklein36352bf2015-03-25 18:17:31 -070076 SkRect onGetBounds() override {
reed6be2aa92014-11-18 11:08:05 -080077 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;
reed1bdfd3f2014-11-24 14:41:51 -080085 MyDrawable* fAnimatingDrawable;
reed3cb38402015-02-06 08:36:15 -080086 SkDrawable* fRootDrawable;
reed6a070dc2014-11-11 19:36:09 -080087
rmistry@google.comae933ce2012-08-23 18:19:56 +000088 ArcsView() {
reed@android.combbff1d52009-06-05 16:21:03 +000089 testparse();
reed@android.com8a1c16f2008-12-17 15:59:43 +000090 fSweep = SkIntToScalar(100);
reed@google.com961ddb02011-05-05 14:03:48 +000091 this->setBGColor(0xFFDDDDDD);
reed6a070dc2014-11-11 19:36:09 -080092
93 fRect.set(0, 0, SkIntToScalar(200), SkIntToScalar(200));
94 fRect.offset(SkIntToScalar(20), SkIntToScalar(20));
reed1bdfd3f2014-11-24 14:41:51 -080095 fAnimatingDrawable = SkNEW_ARGS(MyDrawable, (fRect));
96
97 SkPictureRecorder recorder;
98 this->drawRoot(recorder.beginRecording(SkRect::MakeWH(800, 500)));
reed3cb38402015-02-06 08:36:15 -080099 fRootDrawable = recorder.endRecordingAsDrawable();
reed6a070dc2014-11-11 19:36:09 -0800100 }
101
mtklein36352bf2015-03-25 18:17:31 -0700102 ~ArcsView() override {
reed1bdfd3f2014-11-24 14:41:51 -0800103 fAnimatingDrawable->unref();
104 fRootDrawable->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 }
106
107protected:
108 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -0700109 bool onQuery(SkEvent* evt) override {
reed@google.com961ddb02011-05-05 14:03:48 +0000110 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 SampleCode::TitleR(evt, "Arcs");
112 return true;
113 }
114 return this->INHERITED::onQuery(evt);
115 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000116
reed1bdfd3f2014-11-24 14:41:51 -0800117 static void DrawRectWithLines(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 canvas->drawRect(r, p);
119 canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, p);
120 canvas->drawLine(r.fLeft, r.fBottom, r.fRight, r.fTop, p);
121 canvas->drawLine(r.fLeft, r.centerY(), r.fRight, r.centerY(), p);
122 canvas->drawLine(r.centerX(), r.fTop, r.centerX(), r.fBottom, p);
123 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000124
reed61adb1b2015-02-09 13:01:05 -0800125 static void DrawLabel(SkCanvas* canvas, const SkRect& rect, SkScalar start, SkScalar sweep) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000127
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 paint.setAntiAlias(true);
129 paint.setTextAlign(SkPaint::kCenter_Align);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000130
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131 SkString str;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000132
reed61adb1b2015-02-09 13:01:05 -0800133 str.appendScalar(start);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 str.append(", ");
reed61adb1b2015-02-09 13:01:05 -0800135 str.appendScalar(sweep);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 canvas->drawText(str.c_str(), str.size(), rect.centerX(),
137 rect.fBottom + paint.getTextSize() * 5/4, paint);
138 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000139
reed1bdfd3f2014-11-24 14:41:51 -0800140 static void DrawArcs(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141 SkPaint paint;
142 SkRect r;
reed61adb1b2015-02-09 13:01:05 -0800143 SkScalar w = 75;
144 SkScalar h = 50;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145
146 r.set(0, 0, w, h);
147 paint.setAntiAlias(true);
148 paint.setStyle(SkPaint::kStroke_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000149
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 canvas->save();
151 canvas->translate(SkIntToScalar(10), SkIntToScalar(300));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000152
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 paint.setStrokeWidth(SkIntToScalar(1));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000154
reed61adb1b2015-02-09 13:01:05 -0800155 static const SkScalar gAngles[] = {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 0, 360,
157 0, 45,
158 0, -45,
159 720, 135,
160 -90, 269,
161 -90, 270,
162 -90, 271,
163 -180, -270,
164 225, 90
165 };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000166
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000167 for (size_t i = 0; i < SK_ARRAY_COUNT(gAngles); i += 2) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168 paint.setColor(SK_ColorBLACK);
reed1bdfd3f2014-11-24 14:41:51 -0800169 DrawRectWithLines(canvas, r, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170
171 paint.setColor(SK_ColorRED);
reed61adb1b2015-02-09 13:01:05 -0800172 canvas->drawArc(r, gAngles[i], gAngles[i+1], false, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000173
reed1bdfd3f2014-11-24 14:41:51 -0800174 DrawLabel(canvas, r, gAngles[i], gAngles[i+1]);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175
176 canvas->translate(w * 8 / 7, 0);
177 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000178
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179 canvas->restore();
180 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000181
reed1bdfd3f2014-11-24 14:41:51 -0800182 void drawRoot(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183 SkPaint paint;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000184 paint.setAntiAlias(true);
185 paint.setStrokeWidth(SkIntToScalar(2));
186 paint.setStyle(SkPaint::kStroke_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000187
reed1bdfd3f2014-11-24 14:41:51 -0800188 DrawRectWithLines(canvas, fRect, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000189
reed3cb38402015-02-06 08:36:15 -0800190 canvas->drawDrawable(fAnimatingDrawable);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000191
reed1bdfd3f2014-11-24 14:41:51 -0800192 DrawArcs(canvas);
193 }
194
mtklein36352bf2015-03-25 18:17:31 -0700195 void onDrawContent(SkCanvas* canvas) override {
reed3cb38402015-02-06 08:36:15 -0800196 canvas->drawDrawable(fRootDrawable);
reedd9adfe62015-02-01 19:01:04 -0800197 }
198
mtklein36352bf2015-03-25 18:17:31 -0700199 bool onAnimate(const SkAnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -0800200 SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360));
reedd9adfe62015-02-01 19:01:04 -0800201 fAnimatingDrawable->setSweep(angle);
202 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000203 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000204
mtklein36352bf2015-03-25 18:17:31 -0700205 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000206 // fSweep += SK_Scalar1;
207 this->inval(NULL);
reed@google.com4d5c26d2013-01-08 16:17:50 +0000208 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000209 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000210
reed@android.com8a1c16f2008-12-17 15:59:43 +0000211private:
212 SkScalar fSweep;
213
reed@google.com961ddb02011-05-05 14:03:48 +0000214 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000215};
216
217//////////////////////////////////////////////////////////////////////////////
218
219static SkView* MyFactory() { return new ArcsView; }
220static SkViewRegister reg(MyFactory);