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: |
| 22 | static const SkScalar ANIM_DX = SK_Scalar1 / 67; |
| 23 | static const SkScalar ANIM_DY = SK_Scalar1 / 29; |
| 24 | static const SkScalar ANIM_RAD = SK_Scalar1 / 19; |
| 25 | SkScalar fDX, fDY, fRAD; |
| 26 | |
| 27 | CircleView() { |
| 28 | fDX = fDY = fRAD = 0; |
| 29 | } |
| 30 | |
| 31 | protected: |
| 32 | // overrides from SkEventSink |
| 33 | virtual bool onQuery(SkEvent* evt) { |
| 34 | if (SampleCode::TitleQ(*evt)) { |
| 35 | SampleCode::TitleR(evt, "Circles"); |
| 36 | return true; |
| 37 | } |
| 38 | return this->INHERITED::onQuery(evt); |
| 39 | } |
| 40 | |
| 41 | void drawBG(SkCanvas* canvas) { |
| 42 | canvas->drawColor(SK_ColorWHITE); |
| 43 | } |
| 44 | |
| 45 | void circle(SkCanvas* canvas, int width, bool aa) { |
| 46 | SkPaint paint; |
| 47 | |
| 48 | paint.setAntiAlias(aa); |
| 49 | if (width < 0) { |
| 50 | paint.setStyle(SkPaint::kFill_Style); |
| 51 | } else { |
| 52 | paint.setStyle(SkPaint::kStroke_Style); |
| 53 | paint.setStrokeWidth(SkIntToScalar(width)); |
| 54 | } |
| 55 | canvas->drawCircle(0, 0, SkIntToScalar(9) + fRAD, paint); |
| 56 | } |
| 57 | |
| 58 | void drawSix(SkCanvas* canvas, SkScalar dx, SkScalar dy) { |
| 59 | for (int width = -1; width <= 1; width++) { |
| 60 | canvas->save(); |
| 61 | circle(canvas, width, false); |
| 62 | canvas->translate(0, dy); |
| 63 | circle(canvas, width, true); |
| 64 | canvas->restore(); |
| 65 | canvas->translate(dx, 0); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | static void blowup(SkCanvas* canvas, const SkIRect& src, const SkRect& dst) { |
| 70 | SkDevice* device = canvas->getDevice(); |
| 71 | const SkBitmap& bm = device->accessBitmap(false); |
| 72 | canvas->drawBitmapRect(bm, &src, dst, NULL); |
| 73 | } |
| 74 | |
| 75 | virtual void onDraw(SkCanvas* canvas) { |
| 76 | this->drawBG(canvas); |
| 77 | |
| 78 | test_circlebounds(canvas); |
| 79 | |
| 80 | if (false) { |
| 81 | // canvas->translate(SK_ScalarHalf, SK_ScalarHalf); |
| 82 | |
| 83 | SkPaint paint; |
| 84 | paint.setAntiAlias(true); |
| 85 | paint.setStyle(SkPaint::kStroke_Style); |
| 86 | canvas->drawCircle(SkIntToScalar(10), SkIntToScalar(10), SkIntToScalar(2), paint); |
| 87 | |
| 88 | SkIRect r; |
| 89 | r.set(7, 7, 13, 13); |
| 90 | SkRect dst; |
| 91 | dst.set(SkIntToScalar(100), SkIntToScalar(10), SkIntToScalar(200), SkIntToScalar(110)); |
| 92 | blowup(canvas, r, dst); |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | // test that degenerate rects do nothing |
| 97 | if (true) { |
| 98 | SkScalar x = SkIntToScalar(30); |
| 99 | SkRect r; |
| 100 | r.set(x, x, x, x); |
| 101 | SkPaint p; |
| 102 | canvas->drawRect(r, p); |
| 103 | p.setAntiAlias(true); |
| 104 | canvas->drawRect(r, p); |
| 105 | } |
| 106 | |
| 107 | SkScalar dx = SkIntToScalar(32); |
| 108 | SkScalar dy = SkIntToScalar(32); |
| 109 | |
| 110 | canvas->translate(dx + fDX, dy + fDY); |
| 111 | drawSix(canvas, dx, dy); |
| 112 | |
| 113 | canvas->translate(dx, 0); |
| 114 | canvas->translate(SK_ScalarHalf, SK_ScalarHalf); |
| 115 | drawSix(canvas, dx, dy); |
| 116 | |
| 117 | fDX += ANIM_DX; |
| 118 | fDY += ANIM_DY; |
| 119 | fRAD += ANIM_RAD; |
| 120 | this->inval(NULL); |
| 121 | } |
| 122 | |
| 123 | private: |
| 124 | typedef SkView INHERITED; |
| 125 | }; |
| 126 | |
| 127 | const SkScalar CircleView::ANIM_DX; |
| 128 | const SkScalar CircleView::ANIM_DY; |
| 129 | const SkScalar CircleView::ANIM_RAD; |
| 130 | |
| 131 | ////////////////////////////////////////////////////////////////////////////// |
| 132 | |
| 133 | static SkView* MyFactory() { return new CircleView; } |
| 134 | static SkViewRegister reg(MyFactory); |
| 135 | |