epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 |
sugoi@google.com | 93c7ee3 | 2013-03-12 14:36:57 +0000 | [diff] [blame] | 18 | static void test_circlebounds(SkCanvas*) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | #ifdef SK_SCALAR_IS_FLOAT |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 20 | SkRect r = { 1.39999998f, 1, 21.3999996f, 21 }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 21 | SkPath p; |
| 22 | p.addOval(r); |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 23 | SkASSERT(r == p.getBounds()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | #endif |
| 25 | } |
| 26 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 27 | class CircleView : public SampleView { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | public: |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 29 | static const SkScalar ANIM_DX; |
| 30 | static const SkScalar ANIM_DY; |
| 31 | static const SkScalar ANIM_RAD; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | SkScalar fDX, fDY, fRAD; |
| 33 | |
| 34 | CircleView() { |
| 35 | fDX = fDY = fRAD = 0; |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 36 | fN = 3; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 37 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 38 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | protected: |
| 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 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 48 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | void circle(SkCanvas* canvas, int width, bool aa) { |
| 50 | SkPaint paint; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 51 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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); |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 60 | if (false) { // avoid bit rot, suppress warning |
| 61 | test_circlebounds(canvas); |
| 62 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 63 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 64 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 65 | void drawSix(SkCanvas* canvas, SkScalar dx, SkScalar dy) { |
| 66 | for (int width = -1; width <= 1; width++) { |
| 67 | canvas->save(); |
| 68 | circle(canvas, width, false); |
| 69 | canvas->translate(0, dy); |
| 70 | circle(canvas, width, true); |
| 71 | canvas->restore(); |
| 72 | canvas->translate(dx, 0); |
| 73 | } |
| 74 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 75 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | static void blowup(SkCanvas* canvas, const SkIRect& src, const SkRect& dst) { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 77 | SkBaseDevice* device = canvas->getDevice(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | const SkBitmap& bm = device->accessBitmap(false); |
| 79 | canvas->drawBitmapRect(bm, &src, dst, NULL); |
| 80 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 81 | |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 82 | static void make_poly(SkPath* path, int n) { |
| 83 | if (n <= 0) { |
| 84 | return; |
| 85 | } |
| 86 | path->incReserve(n + 1); |
| 87 | path->moveTo(SK_Scalar1, 0); |
| 88 | SkScalar step = SK_ScalarPI * 2 / n; |
| 89 | SkScalar angle = 0; |
| 90 | for (int i = 1; i < n; i++) { |
| 91 | angle += step; |
| 92 | SkScalar c, s = SkScalarSinCos(angle, &c); |
| 93 | path->lineTo(c, s); |
| 94 | } |
| 95 | path->close(); |
| 96 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 97 | |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 98 | static void rotate(SkCanvas* canvas, SkScalar angle, SkScalar px, SkScalar py) { |
| 99 | canvas->translate(-px, -py); |
| 100 | canvas->rotate(angle); |
| 101 | canvas->translate(px, py); |
| 102 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 103 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 104 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 105 | SkPaint paint; |
| 106 | paint.setAntiAlias(true); |
| 107 | paint.setStyle(SkPaint::kStroke_Style); |
| 108 | // canvas->drawCircle(250, 250, 220, paint); |
| 109 | SkMatrix matrix; |
| 110 | matrix.setScale(SkIntToScalar(100), SkIntToScalar(100)); |
| 111 | matrix.postTranslate(SkIntToScalar(200), SkIntToScalar(200)); |
| 112 | canvas->concat(matrix); |
| 113 | for (int n = 3; n < 20; n++) { |
| 114 | SkPath path; |
| 115 | make_poly(&path, n); |
| 116 | SkAutoCanvasRestore acr(canvas, true); |
| 117 | canvas->rotate(SkIntToScalar(10) * (n - 3)); |
| 118 | canvas->translate(-SK_Scalar1, 0); |
| 119 | canvas->drawPath(path, paint); |
| 120 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 122 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | private: |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 124 | int fN; |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 125 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 128 | const SkScalar CircleView::ANIM_DX(SK_Scalar1 / 67); |
| 129 | const SkScalar CircleView::ANIM_DY(SK_Scalar1 / 29); |
| 130 | const SkScalar CircleView::ANIM_RAD(SK_Scalar1 / 19); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 131 | |
| 132 | ////////////////////////////////////////////////////////////////////////////// |
| 133 | |
| 134 | static SkView* MyFactory() { return new CircleView; } |
| 135 | static SkViewRegister reg(MyFactory); |