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