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