reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 1 | #include "SampleCode.h" |
| 2 | #include "SkCanvas.h" |
| 3 | #include "SkPaint.h" |
reed@android.com | 09b84a0 | 2009-06-26 20:22:26 +0000 | [diff] [blame] | 4 | #include "SkPicture.h" |
reed@android.com | 0ad336f | 2009-06-29 16:02:20 +0000 | [diff] [blame] | 5 | #include "SkStream.h" |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 6 | #include "SkView.h" |
| 7 | |
| 8 | #include "SkRectShape.h" |
| 9 | #include "SkGroupShape.h" |
| 10 | |
| 11 | static SkRect make_rect(int l, int t, int r, int b) { |
| 12 | SkRect rect; |
| 13 | rect.set(SkIntToScalar(l), SkIntToScalar(t), |
| 14 | SkIntToScalar(r), SkIntToScalar(b)); |
| 15 | return rect; |
| 16 | } |
| 17 | |
reed@android.com | f48f281 | 2009-05-18 04:20:55 +0000 | [diff] [blame] | 18 | static SkShape* make_shape0(bool red) { |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 19 | SkRectShape* s = new SkRectShape; |
| 20 | s->setRect(make_rect(10, 10, 90, 90)); |
reed@android.com | f48f281 | 2009-05-18 04:20:55 +0000 | [diff] [blame] | 21 | if (red) { |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 22 | s->paint().setColor(SK_ColorRED); |
| 23 | } |
| 24 | return s; |
| 25 | } |
| 26 | |
reed@android.com | f48f281 | 2009-05-18 04:20:55 +0000 | [diff] [blame] | 27 | static SkShape* make_shape1() { |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 28 | SkRectShape* s = new SkRectShape; |
| 29 | s->setOval(make_rect(10, 10, 90, 90)); |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 30 | s->paint().setColor(SK_ColorBLUE); |
| 31 | return s; |
| 32 | } |
| 33 | |
reed@android.com | f48f281 | 2009-05-18 04:20:55 +0000 | [diff] [blame] | 34 | static SkShape* make_shape2() { |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 35 | SkRectShape* s = new SkRectShape; |
| 36 | s->setRRect(make_rect(10, 10, 90, 90), |
| 37 | SkIntToScalar(20), SkIntToScalar(20)); |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 38 | s->paint().setColor(SK_ColorGREEN); |
| 39 | return s; |
| 40 | } |
| 41 | |
| 42 | /////////////////////////////////////////////////////////////////////////////// |
| 43 | |
| 44 | class ShapesView : public SkView { |
| 45 | SkGroupShape fGroup; |
reed@android.com | 88c2d8e | 2009-05-27 01:44:24 +0000 | [diff] [blame] | 46 | SkMatrixRef* fMatrixRefs[4]; |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 47 | public: |
| 48 | ShapesView() { |
| 49 | SkMatrix m; |
reed@android.com | f48f281 | 2009-05-18 04:20:55 +0000 | [diff] [blame] | 50 | fGroup.appendShape(make_shape0(false))->unref(); |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 51 | m.setRotate(SkIntToScalar(30), SkIntToScalar(50), SkIntToScalar(50)); |
| 52 | m.postTranslate(0, SkIntToScalar(120)); |
reed@android.com | f48f281 | 2009-05-18 04:20:55 +0000 | [diff] [blame] | 53 | fGroup.appendShape(make_shape0(true), m)->unref(); |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 54 | |
| 55 | m.setTranslate(SkIntToScalar(120), 0); |
reed@android.com | f48f281 | 2009-05-18 04:20:55 +0000 | [diff] [blame] | 56 | fGroup.appendShape(make_shape1(), m)->unref(); |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 57 | m.postTranslate(0, SkIntToScalar(120)); |
reed@android.com | f48f281 | 2009-05-18 04:20:55 +0000 | [diff] [blame] | 58 | fGroup.appendShape(make_shape2(), m)->unref(); |
reed@android.com | 88c2d8e | 2009-05-27 01:44:24 +0000 | [diff] [blame] | 59 | |
| 60 | for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrixRefs); i++) { |
| 61 | SkSafeRef(fMatrixRefs[i] = fGroup.getShapeMatrixRef(i)); |
| 62 | } |
| 63 | |
| 64 | fAngle = 0; |
| 65 | } |
| 66 | |
| 67 | virtual ~ShapesView() { |
| 68 | for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrixRefs); i++) { |
| 69 | SkSafeUnref(fMatrixRefs[i]); |
| 70 | } |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | protected: |
| 74 | // overrides from SkEventSink |
| 75 | virtual bool onQuery(SkEvent* evt) { |
| 76 | if (SampleCode::TitleQ(*evt)) { |
| 77 | SampleCode::TitleR(evt, "Shapes"); |
| 78 | return true; |
| 79 | } |
| 80 | return this->INHERITED::onQuery(evt); |
| 81 | } |
| 82 | |
| 83 | void drawBG(SkCanvas* canvas) { |
| 84 | canvas->drawColor(0xFFDDDDDD); |
| 85 | } |
| 86 | |
reed@android.com | 0ad336f | 2009-06-29 16:02:20 +0000 | [diff] [blame] | 87 | void drawpicture(SkCanvas* canvas, SkPicture& pict) { |
| 88 | #if 1 |
| 89 | SkDynamicMemoryWStream ostream; |
| 90 | pict.serialize(&ostream); |
| 91 | |
| 92 | SkMemoryStream istream(ostream.getStream(), ostream.getOffset()); |
| 93 | SkPicture newPict(&istream); |
| 94 | |
| 95 | canvas->drawPicture(newPict); |
| 96 | #else |
| 97 | canvas->drawPicture(pict); |
| 98 | #endif |
| 99 | } |
| 100 | |
reed@android.com | 88c2d8e | 2009-05-27 01:44:24 +0000 | [diff] [blame] | 101 | int fAngle; |
| 102 | |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 103 | virtual void onDraw(SkCanvas* canvas) { |
| 104 | this->drawBG(canvas); |
| 105 | |
reed@android.com | 88c2d8e | 2009-05-27 01:44:24 +0000 | [diff] [blame] | 106 | SkMatrix saveM = *fMatrixRefs[3]; |
| 107 | fAngle = (fAngle + 5) % 360; |
| 108 | SkScalar c = SkIntToScalar(50); |
| 109 | fMatrixRefs[3]->preRotate(SkIntToScalar(fAngle), c, c); |
| 110 | |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 111 | SkMatrix matrix; |
reed@android.com | 09b84a0 | 2009-06-26 20:22:26 +0000 | [diff] [blame] | 112 | |
| 113 | SkGroupShape* gs = new SkGroupShape; |
| 114 | SkAutoUnref aur(gs); |
| 115 | gs->appendShape(&fGroup); |
reed@android.com | 88c2d8e | 2009-05-27 01:44:24 +0000 | [diff] [blame] | 116 | matrix.setScale(-SK_Scalar1, SK_Scalar1); |
| 117 | matrix.postTranslate(SkIntToScalar(220), SkIntToScalar(240)); |
reed@android.com | 09b84a0 | 2009-06-26 20:22:26 +0000 | [diff] [blame] | 118 | gs->appendShape(&fGroup, matrix); |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 119 | matrix.setTranslate(SkIntToScalar(240), 0); |
| 120 | matrix.preScale(SK_Scalar1*2, SK_Scalar1*2); |
reed@android.com | 09b84a0 | 2009-06-26 20:22:26 +0000 | [diff] [blame] | 121 | gs->appendShape(&fGroup, matrix); |
| 122 | |
reed@android.com | 0ad336f | 2009-06-29 16:02:20 +0000 | [diff] [blame] | 123 | #if 0 |
reed@android.com | 09b84a0 | 2009-06-26 20:22:26 +0000 | [diff] [blame] | 124 | canvas->drawShape(gs); |
reed@android.com | f48f281 | 2009-05-18 04:20:55 +0000 | [diff] [blame] | 125 | #else |
reed@android.com | 09b84a0 | 2009-06-26 20:22:26 +0000 | [diff] [blame] | 126 | SkPicture pict; |
| 127 | SkCanvas* cv = pict.beginRecording(1000, 1000); |
| 128 | cv->scale(SK_ScalarHalf, SK_ScalarHalf); |
| 129 | cv->drawShape(gs); |
| 130 | cv->translate(SkIntToScalar(680), SkIntToScalar(480)); |
| 131 | cv->scale(-SK_Scalar1, SK_Scalar1); |
| 132 | cv->drawShape(gs); |
| 133 | pict.endRecording(); |
reed@android.com | 0ad336f | 2009-06-29 16:02:20 +0000 | [diff] [blame] | 134 | |
| 135 | drawpicture(canvas, pict); |
reed@android.com | f48f281 | 2009-05-18 04:20:55 +0000 | [diff] [blame] | 136 | #endif |
reed@android.com | 88c2d8e | 2009-05-27 01:44:24 +0000 | [diff] [blame] | 137 | |
| 138 | *fMatrixRefs[3] = saveM; |
| 139 | this->inval(NULL); |
| 140 | } |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 141 | |
| 142 | private: |
| 143 | typedef SkView INHERITED; |
| 144 | }; |
| 145 | |
| 146 | /////////////////////////////////////////////////////////////////////////////// |
| 147 | |
| 148 | static SkView* MyFactory() { return new ShapesView; } |
| 149 | static SkViewRegister reg(MyFactory); |
| 150 | |