Home | History | Annotate | Download | only in samplecode
      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.39999998f, 1, 21.3999996f, 21 };
     14     SkPath p;
     15     p.addOval(r);
     16     SkASSERT(r == p.getBounds());
     17 #endif
     18 }
     19 
     20 class CircleView : public SampleView {
     21 public:
     22     static const SkScalar ANIM_DX;
     23     static const SkScalar ANIM_DY;
     24     static const SkScalar ANIM_RAD;
     25     SkScalar fDX, fDY, fRAD;
     26 
     27     CircleView() {
     28         fDX = fDY = fRAD = 0;
     29         fN = 3;
     30     }
     31 
     32 protected:
     33     // overrides from SkEventSink
     34     virtual bool onQuery(SkEvent* evt) {
     35         if (SampleCode::TitleQ(*evt)) {
     36             SampleCode::TitleR(evt, "Circles");
     37             return true;
     38         }
     39         return this->INHERITED::onQuery(evt);
     40     }
     41 
     42     void circle(SkCanvas* canvas, int width, bool aa) {
     43         SkPaint paint;
     44 
     45         paint.setAntiAlias(aa);
     46         if (width < 0) {
     47             paint.setStyle(SkPaint::kFill_Style);
     48         } else {
     49             paint.setStyle(SkPaint::kStroke_Style);
     50             paint.setStrokeWidth(SkIntToScalar(width));
     51         }
     52         canvas->drawCircle(0, 0, SkIntToScalar(9) + fRAD, paint);
     53     }
     54 
     55     void drawSix(SkCanvas* canvas, SkScalar dx, SkScalar dy) {
     56         for (int width = -1; width <= 1; width++) {
     57             canvas->save();
     58             circle(canvas, width, false);
     59             canvas->translate(0, dy);
     60             circle(canvas, width, true);
     61             canvas->restore();
     62             canvas->translate(dx, 0);
     63         }
     64     }
     65 
     66     static void blowup(SkCanvas* canvas, const SkIRect& src, const SkRect& dst) {
     67         SkDevice* device = canvas->getDevice();
     68         const SkBitmap& bm = device->accessBitmap(false);
     69         canvas->drawBitmapRect(bm, &src, dst, NULL);
     70     }
     71 
     72     static void make_poly(SkPath* path, int n) {
     73         if (n <= 0) {
     74             return;
     75         }
     76         path->incReserve(n + 1);
     77         path->moveTo(SK_Scalar1, 0);
     78         SkScalar step = SK_ScalarPI * 2 / n;
     79         SkScalar angle = 0;
     80         for (int i = 1; i < n; i++) {
     81             angle += step;
     82             SkScalar c, s = SkScalarSinCos(angle, &c);
     83             path->lineTo(c, s);
     84         }
     85         path->close();
     86     }
     87 
     88     static void rotate(SkCanvas* canvas, SkScalar angle, SkScalar px, SkScalar py) {
     89         canvas->translate(-px, -py);
     90         canvas->rotate(angle);
     91         canvas->translate(px, py);
     92     }
     93 
     94     virtual void onDrawContent(SkCanvas* canvas) {
     95         SkPaint paint;
     96         paint.setAntiAlias(true);
     97         paint.setStyle(SkPaint::kStroke_Style);
     98 //        canvas->drawCircle(250, 250, 220, paint);
     99         SkMatrix matrix;
    100         matrix.setScale(SkIntToScalar(100), SkIntToScalar(100));
    101         matrix.postTranslate(SkIntToScalar(200), SkIntToScalar(200));
    102         canvas->concat(matrix);
    103         for (int n = 3; n < 20; n++) {
    104             SkPath path;
    105             make_poly(&path, n);
    106             SkAutoCanvasRestore acr(canvas, true);
    107             canvas->rotate(SkIntToScalar(10) * (n - 3));
    108             canvas->translate(-SK_Scalar1, 0);
    109             canvas->drawPath(path, paint);
    110         }
    111     }
    112 
    113 private:
    114     int fN;
    115     typedef SampleView INHERITED;
    116 };
    117 
    118 const SkScalar CircleView::ANIM_DX(SK_Scalar1 / 67);
    119 const SkScalar CircleView::ANIM_DY(SK_Scalar1 / 29);
    120 const SkScalar CircleView::ANIM_RAD(SK_Scalar1 / 19);
    121 
    122 //////////////////////////////////////////////////////////////////////////////
    123 
    124 static SkView* MyFactory() { return new CircleView; }
    125 static SkViewRegister reg(MyFactory);
    126 
    127