blob: 2abc28de3e34b6dce8c816ba6e9d227f5ae73d1d [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001#include "SampleCode.h"
2#include "SkView.h"
3#include "SkCanvas.h"
4#include "SkDevice.h"
5#include "SkPaint.h"
6
7// ensure that we don't accidentally screw up the bounds when the oval is
8// fractional, and the impl computes the center and radii, and uses them to
9// reconstruct the edges of the circle.
10// see bug# 1504910
11static void test_circlebounds(SkCanvas* canvas) {
12#ifdef SK_SCALAR_IS_FLOAT
reed@google.com261b8e22011-04-14 17:53:24 +000013 SkRect r = { 1.39999998f, 1, 21.3999996f, 21 };
reed@android.com8a1c16f2008-12-17 15:59:43 +000014 SkPath p;
15 p.addOval(r);
reed@android.comd252db02009-04-01 18:31:44 +000016 SkASSERT(r == p.getBounds());
reed@android.com8a1c16f2008-12-17 15:59:43 +000017#endif
18}
19
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000020class CircleView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000021public:
reed@android.comf2b98d62010-12-20 18:26:13 +000022 static const SkScalar ANIM_DX;
23 static const SkScalar ANIM_DY;
24 static const SkScalar ANIM_RAD;
reed@android.com8a1c16f2008-12-17 15:59:43 +000025 SkScalar fDX, fDY, fRAD;
26
27 CircleView() {
28 fDX = fDY = fRAD = 0;
reed@android.com7a99eb12009-07-16 01:13:14 +000029 fN = 3;
reed@android.com8a1c16f2008-12-17 15:59:43 +000030 }
31
32protected:
33 // overrides from SkEventSink
34 virtual bool onQuery(SkEvent* evt) {
35 if (SampleCode::TitleQ(*evt)) {
36 SampleCode::TitleR(evt, "Circles");
37 return true;
38 }
39 return this->INHERITED::onQuery(evt);
40 }
41
reed@android.com8a1c16f2008-12-17 15:59:43 +000042 void circle(SkCanvas* canvas, int width, bool aa) {
43 SkPaint paint;
44
45 paint.setAntiAlias(aa);
46 if (width < 0) {
47 paint.setStyle(SkPaint::kFill_Style);
48 } else {
49 paint.setStyle(SkPaint::kStroke_Style);
50 paint.setStrokeWidth(SkIntToScalar(width));
51 }
52 canvas->drawCircle(0, 0, SkIntToScalar(9) + fRAD, paint);
53 }
54
55 void drawSix(SkCanvas* canvas, SkScalar dx, SkScalar dy) {
56 for (int width = -1; width <= 1; width++) {
57 canvas->save();
58 circle(canvas, width, false);
59 canvas->translate(0, dy);
60 circle(canvas, width, true);
61 canvas->restore();
62 canvas->translate(dx, 0);
63 }
64 }
65
66 static void blowup(SkCanvas* canvas, const SkIRect& src, const SkRect& dst) {
67 SkDevice* device = canvas->getDevice();
68 const SkBitmap& bm = device->accessBitmap(false);
69 canvas->drawBitmapRect(bm, &src, dst, NULL);
70 }
reed@android.com7a99eb12009-07-16 01:13:14 +000071
72 static void make_poly(SkPath* path, int n) {
73 if (n <= 0) {
74 return;
75 }
76 path->incReserve(n + 1);
77 path->moveTo(SK_Scalar1, 0);
78 SkScalar step = SK_ScalarPI * 2 / n;
79 SkScalar angle = 0;
80 for (int i = 1; i < n; i++) {
81 angle += step;
82 SkScalar c, s = SkScalarSinCos(angle, &c);
83 path->lineTo(c, s);
84 }
85 path->close();
86 }
87
88 static void rotate(SkCanvas* canvas, SkScalar angle, SkScalar px, SkScalar py) {
89 canvas->translate(-px, -py);
90 canvas->rotate(angle);
91 canvas->translate(px, py);
92 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000093
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000094 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com7a99eb12009-07-16 01:13:14 +000095 SkPaint paint;
96 paint.setAntiAlias(true);
97 paint.setStyle(SkPaint::kStroke_Style);
98// canvas->drawCircle(250, 250, 220, paint);
99 SkMatrix matrix;
100 matrix.setScale(SkIntToScalar(100), SkIntToScalar(100));
101 matrix.postTranslate(SkIntToScalar(200), SkIntToScalar(200));
102 canvas->concat(matrix);
103 for (int n = 3; n < 20; n++) {
104 SkPath path;
105 make_poly(&path, n);
106 SkAutoCanvasRestore acr(canvas, true);
107 canvas->rotate(SkIntToScalar(10) * (n - 3));
108 canvas->translate(-SK_Scalar1, 0);
109 canvas->drawPath(path, paint);
110 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 }
112
113private:
reed@android.com7a99eb12009-07-16 01:13:14 +0000114 int fN;
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000115 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116};
117
reed@android.comf2b98d62010-12-20 18:26:13 +0000118const SkScalar CircleView::ANIM_DX(SK_Scalar1 / 67);
119const SkScalar CircleView::ANIM_DY(SK_Scalar1 / 29);
120const SkScalar CircleView::ANIM_RAD(SK_Scalar1 / 19);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121
122//////////////////////////////////////////////////////////////////////////////
123
124static SkView* MyFactory() { return new CircleView; }
125static SkViewRegister reg(MyFactory);
126