1
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkDevice.h"
12#include "SkPaint.h"
13
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
18static void test_circlebounds(SkCanvas* canvas) {
19#ifdef SK_SCALAR_IS_FLOAT
20    SkRect r = { 1.39999998f, 1, 21.3999996f, 21 };
21    SkPath p;
22    p.addOval(r);
23    SkASSERT(r == p.getBounds());
24#endif
25}
26
27class CircleView : public SampleView {
28public:
29    static const SkScalar ANIM_DX;
30    static const SkScalar ANIM_DY;
31    static const SkScalar ANIM_RAD;
32    SkScalar fDX, fDY, fRAD;
33
34    CircleView() {
35        fDX = fDY = fRAD = 0;
36        fN = 3;
37    }
38
39protected:
40    // overrides from SkEventSink
41    virtual bool onQuery(SkEvent* evt) {
42        if (SampleCode::TitleQ(*evt)) {
43            SampleCode::TitleR(evt, "Circles");
44            return true;
45        }
46        return this->INHERITED::onQuery(evt);
47    }
48
49    void circle(SkCanvas* canvas, int width, bool aa) {
50        SkPaint paint;
51
52        paint.setAntiAlias(aa);
53        if (width < 0) {
54            paint.setStyle(SkPaint::kFill_Style);
55        } else {
56            paint.setStyle(SkPaint::kStroke_Style);
57            paint.setStrokeWidth(SkIntToScalar(width));
58        }
59        canvas->drawCircle(0, 0, SkIntToScalar(9) + fRAD, paint);
60    }
61
62    void drawSix(SkCanvas* canvas, SkScalar dx, SkScalar dy) {
63        for (int width = -1; width <= 1; width++) {
64            canvas->save();
65            circle(canvas, width, false);
66            canvas->translate(0, dy);
67            circle(canvas, width, true);
68            canvas->restore();
69            canvas->translate(dx, 0);
70        }
71    }
72
73    static void blowup(SkCanvas* canvas, const SkIRect& src, const SkRect& dst) {
74        SkDevice* device = canvas->getDevice();
75        const SkBitmap& bm = device->accessBitmap(false);
76        canvas->drawBitmapRect(bm, &src, dst, NULL);
77    }
78
79    static void make_poly(SkPath* path, int n) {
80        if (n <= 0) {
81            return;
82        }
83        path->incReserve(n + 1);
84        path->moveTo(SK_Scalar1, 0);
85        SkScalar step = SK_ScalarPI * 2 / n;
86        SkScalar angle = 0;
87        for (int i = 1; i < n; i++) {
88            angle += step;
89            SkScalar c, s = SkScalarSinCos(angle, &c);
90            path->lineTo(c, s);
91        }
92        path->close();
93    }
94
95    static void rotate(SkCanvas* canvas, SkScalar angle, SkScalar px, SkScalar py) {
96        canvas->translate(-px, -py);
97        canvas->rotate(angle);
98        canvas->translate(px, py);
99    }
100
101    virtual void onDrawContent(SkCanvas* canvas) {
102        SkPaint paint;
103        paint.setAntiAlias(true);
104        paint.setStyle(SkPaint::kStroke_Style);
105//        canvas->drawCircle(250, 250, 220, paint);
106        SkMatrix matrix;
107        matrix.setScale(SkIntToScalar(100), SkIntToScalar(100));
108        matrix.postTranslate(SkIntToScalar(200), SkIntToScalar(200));
109        canvas->concat(matrix);
110        for (int n = 3; n < 20; n++) {
111            SkPath path;
112            make_poly(&path, n);
113            SkAutoCanvasRestore acr(canvas, true);
114            canvas->rotate(SkIntToScalar(10) * (n - 3));
115            canvas->translate(-SK_Scalar1, 0);
116            canvas->drawPath(path, paint);
117        }
118    }
119
120private:
121    int fN;
122    typedef SampleView INHERITED;
123};
124
125const SkScalar CircleView::ANIM_DX(SK_Scalar1 / 67);
126const SkScalar CircleView::ANIM_DY(SK_Scalar1 / 29);
127const SkScalar CircleView::ANIM_RAD(SK_Scalar1 / 19);
128
129//////////////////////////////////////////////////////////////////////////////
130
131static SkView* MyFactory() { return new CircleView; }
132static SkViewRegister reg(MyFactory);
133
134