blob: f4e6cc99b480865d69b5436a9e17a3781f8bf452 [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 */
bungemand3ebb482015-08-05 13:57:49 -07007
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00009#include "SkCanvas.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkPaint.h"
bungemand3ebb482015-08-05 13:57:49 -070011#include "SkPath.h"
12#include "SkView.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013
14// ensure that we don't accidentally screw up the bounds when the oval is
15// fractional, and the impl computes the center and radii, and uses them to
16// reconstruct the edges of the circle.
17// see bug# 1504910
sugoi@google.com93c7ee32013-03-12 14:36:57 +000018static void test_circlebounds(SkCanvas*) {
reed@google.com261b8e22011-04-14 17:53:24 +000019 SkRect r = { 1.39999998f, 1, 21.3999996f, 21 };
reed@android.com8a1c16f2008-12-17 15:59:43 +000020 SkPath p;
21 p.addOval(r);
reed@android.comd252db02009-04-01 18:31:44 +000022 SkASSERT(r == p.getBounds());
reed@android.com8a1c16f2008-12-17 15:59:43 +000023}
24
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000025class CircleView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000026public:
reed@android.comf2b98d62010-12-20 18:26:13 +000027 static const SkScalar ANIM_DX;
28 static const SkScalar ANIM_DY;
29 static const SkScalar ANIM_RAD;
reed@android.com8a1c16f2008-12-17 15:59:43 +000030 SkScalar fDX, fDY, fRAD;
31
32 CircleView() {
33 fDX = fDY = fRAD = 0;
reed@android.com7a99eb12009-07-16 01:13:14 +000034 fN = 3;
reed@android.com8a1c16f2008-12-17 15:59:43 +000035 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000036
reed@android.com8a1c16f2008-12-17 15:59:43 +000037protected:
38 // overrides from SkEventSink
39 virtual bool onQuery(SkEvent* evt) {
40 if (SampleCode::TitleQ(*evt)) {
41 SampleCode::TitleR(evt, "Circles");
42 return true;
43 }
44 return this->INHERITED::onQuery(evt);
45 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000046
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 void circle(SkCanvas* canvas, int width, bool aa) {
48 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +000049
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 paint.setAntiAlias(aa);
51 if (width < 0) {
52 paint.setStyle(SkPaint::kFill_Style);
53 } else {
54 paint.setStyle(SkPaint::kStroke_Style);
55 paint.setStrokeWidth(SkIntToScalar(width));
56 }
57 canvas->drawCircle(0, 0, SkIntToScalar(9) + fRAD, paint);
caryclark@google.com02939ce2012-06-06 12:09:51 +000058 if (false) { // avoid bit rot, suppress warning
59 test_circlebounds(canvas);
60 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000062
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 void drawSix(SkCanvas* canvas, SkScalar dx, SkScalar dy) {
64 for (int width = -1; width <= 1; width++) {
65 canvas->save();
66 circle(canvas, width, false);
67 canvas->translate(0, dy);
68 circle(canvas, width, true);
69 canvas->restore();
70 canvas->translate(dx, 0);
71 }
72 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000073
reed@android.com7a99eb12009-07-16 01:13:14 +000074 static void make_poly(SkPath* path, int n) {
75 if (n <= 0) {
76 return;
77 }
78 path->incReserve(n + 1);
79 path->moveTo(SK_Scalar1, 0);
80 SkScalar step = SK_ScalarPI * 2 / n;
81 SkScalar angle = 0;
82 for (int i = 1; i < n; i++) {
83 angle += step;
84 SkScalar c, s = SkScalarSinCos(angle, &c);
85 path->lineTo(c, s);
86 }
87 path->close();
88 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000089
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000090 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com7a99eb12009-07-16 01:13:14 +000091 SkPaint paint;
92 paint.setAntiAlias(true);
93 paint.setStyle(SkPaint::kStroke_Style);
reed@android.com7a99eb12009-07-16 01:13:14 +000094 SkMatrix matrix;
95 matrix.setScale(SkIntToScalar(100), SkIntToScalar(100));
96 matrix.postTranslate(SkIntToScalar(200), SkIntToScalar(200));
97 canvas->concat(matrix);
98 for (int n = 3; n < 20; n++) {
99 SkPath path;
100 make_poly(&path, n);
101 SkAutoCanvasRestore acr(canvas, true);
102 canvas->rotate(SkIntToScalar(10) * (n - 3));
103 canvas->translate(-SK_Scalar1, 0);
104 canvas->drawPath(path, paint);
105 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000107
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108private:
reed@android.com7a99eb12009-07-16 01:13:14 +0000109 int fN;
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000110 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111};
112
reed@android.comf2b98d62010-12-20 18:26:13 +0000113const SkScalar CircleView::ANIM_DX(SK_Scalar1 / 67);
114const SkScalar CircleView::ANIM_DY(SK_Scalar1 / 29);
115const SkScalar CircleView::ANIM_RAD(SK_Scalar1 / 19);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116
117//////////////////////////////////////////////////////////////////////////////
118
119static SkView* MyFactory() { return new CircleView; }
120static SkViewRegister reg(MyFactory);