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