reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | #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 |
| 11 | static void test_circlebounds(SkCanvas* canvas) { |
| 12 | #ifdef SK_SCALAR_IS_FLOAT |
| 13 | SkRect r = { 1.39999998, 1, 21.3999996, 21 }; |
| 14 | SkPath p; |
| 15 | p.addOval(r); |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 16 | SkASSERT(r == p.getBounds()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | #endif |
| 18 | } |
| 19 | |
| 20 | class CircleView : public SkView { |
| 21 | public: |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 22 | static const SkScalar ANIM_DX; |
| 23 | static const SkScalar ANIM_DY; |
| 24 | static const SkScalar ANIM_RAD; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | SkScalar fDX, fDY, fRAD; |
| 26 | |
| 27 | CircleView() { |
| 28 | fDX = fDY = fRAD = 0; |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 29 | fN = 3; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | protected: |
| 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 | |
| 42 | void drawBG(SkCanvas* canvas) { |
| 43 | canvas->drawColor(SK_ColorWHITE); |
| 44 | } |
| 45 | |
| 46 | void circle(SkCanvas* canvas, int width, bool aa) { |
| 47 | SkPaint paint; |
| 48 | |
| 49 | paint.setAntiAlias(aa); |
| 50 | if (width < 0) { |
| 51 | paint.setStyle(SkPaint::kFill_Style); |
| 52 | } else { |
| 53 | paint.setStyle(SkPaint::kStroke_Style); |
| 54 | paint.setStrokeWidth(SkIntToScalar(width)); |
| 55 | } |
| 56 | canvas->drawCircle(0, 0, SkIntToScalar(9) + fRAD, paint); |
| 57 | } |
| 58 | |
| 59 | void drawSix(SkCanvas* canvas, SkScalar dx, SkScalar dy) { |
| 60 | for (int width = -1; width <= 1; width++) { |
| 61 | canvas->save(); |
| 62 | circle(canvas, width, false); |
| 63 | canvas->translate(0, dy); |
| 64 | circle(canvas, width, true); |
| 65 | canvas->restore(); |
| 66 | canvas->translate(dx, 0); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | static void blowup(SkCanvas* canvas, const SkIRect& src, const SkRect& dst) { |
| 71 | SkDevice* device = canvas->getDevice(); |
| 72 | const SkBitmap& bm = device->accessBitmap(false); |
| 73 | canvas->drawBitmapRect(bm, &src, dst, NULL); |
| 74 | } |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 75 | |
| 76 | static void make_poly(SkPath* path, int n) { |
| 77 | if (n <= 0) { |
| 78 | return; |
| 79 | } |
| 80 | path->incReserve(n + 1); |
| 81 | path->moveTo(SK_Scalar1, 0); |
| 82 | SkScalar step = SK_ScalarPI * 2 / n; |
| 83 | SkScalar angle = 0; |
| 84 | for (int i = 1; i < n; i++) { |
| 85 | angle += step; |
| 86 | SkScalar c, s = SkScalarSinCos(angle, &c); |
| 87 | path->lineTo(c, s); |
| 88 | } |
| 89 | path->close(); |
| 90 | } |
| 91 | |
| 92 | static void rotate(SkCanvas* canvas, SkScalar angle, SkScalar px, SkScalar py) { |
| 93 | canvas->translate(-px, -py); |
| 94 | canvas->rotate(angle); |
| 95 | canvas->translate(px, py); |
| 96 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 97 | |
| 98 | virtual void onDraw(SkCanvas* canvas) { |
| 99 | this->drawBG(canvas); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 100 | |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 101 | SkPaint paint; |
| 102 | paint.setAntiAlias(true); |
| 103 | paint.setStyle(SkPaint::kStroke_Style); |
| 104 | // canvas->drawCircle(250, 250, 220, paint); |
| 105 | SkMatrix matrix; |
| 106 | matrix.setScale(SkIntToScalar(100), SkIntToScalar(100)); |
| 107 | matrix.postTranslate(SkIntToScalar(200), SkIntToScalar(200)); |
| 108 | canvas->concat(matrix); |
| 109 | for (int n = 3; n < 20; n++) { |
| 110 | SkPath path; |
| 111 | make_poly(&path, n); |
| 112 | SkAutoCanvasRestore acr(canvas, true); |
| 113 | canvas->rotate(SkIntToScalar(10) * (n - 3)); |
| 114 | canvas->translate(-SK_Scalar1, 0); |
| 115 | canvas->drawPath(path, paint); |
| 116 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 117 | |
| 118 | if (false) { |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 119 | test_circlebounds(canvas); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 120 | |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 121 | SkScalar dx = SkIntToScalar(32); |
| 122 | SkScalar dy = SkIntToScalar(32); |
| 123 | |
| 124 | canvas->translate(dx + fDX, dy + fDY); |
| 125 | drawSix(canvas, dx, dy); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 126 | |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 127 | canvas->translate(dx, 0); |
| 128 | canvas->translate(SK_ScalarHalf, SK_ScalarHalf); |
| 129 | drawSix(canvas, dx, dy); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 130 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 131 | fDX += ANIM_DX; |
| 132 | fDY += ANIM_DY; |
| 133 | fRAD += ANIM_RAD; |
| 134 | fN += 1; |
| 135 | if (fN > 40) { |
| 136 | fN = 3; |
| 137 | } |
| 138 | this->inval(NULL); |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 139 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | private: |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 143 | int fN; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 144 | typedef SkView INHERITED; |
| 145 | }; |
| 146 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 147 | const SkScalar CircleView::ANIM_DX(SK_Scalar1 / 67); |
| 148 | const SkScalar CircleView::ANIM_DY(SK_Scalar1 / 29); |
| 149 | const SkScalar CircleView::ANIM_RAD(SK_Scalar1 / 19); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 150 | |
| 151 | ////////////////////////////////////////////////////////////////////////////// |
| 152 | |
| 153 | static SkView* MyFactory() { return new CircleView; } |
| 154 | static SkViewRegister reg(MyFactory); |
| 155 | |