Home | History | Annotate | Download | only in samplecode
      1 /*
      2  * Copyright 2011 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #include "SampleCode.h"
      9 #include "SkAnimTimer.h"
     10 #include "SkView.h"
     11 #include "SkCanvas.h"
     12 #include "SkDrawable.h"
     13 #include "SkGradientShader.h"
     14 #include "SkPath.h"
     15 #include "SkRegion.h"
     16 #include "SkShader.h"
     17 #include "SkUtils.h"
     18 #include "Sk1DPathEffect.h"
     19 #include "SkCornerPathEffect.h"
     20 #include "SkPathMeasure.h"
     21 #include "SkPictureRecorder.h"
     22 #include "SkRandom.h"
     23 #include "SkColorPriv.h"
     24 #include "SkColorFilter.h"
     25 #include "SkLayerRasterizer.h"
     26 
     27 #include "SkParsePath.h"
     28 static void testparse() {
     29     SkRect r;
     30     r.set(0, 0, 10, 10.5f);
     31     SkPath p, p2;
     32     SkString str, str2;
     33 
     34     p.addRect(r);
     35     SkParsePath::ToSVGString(p, &str);
     36     SkParsePath::FromSVGString(str.c_str(), &p2);
     37     SkParsePath::ToSVGString(p2, &str2);
     38 }
     39 
     40 class ArcsView : public SampleView {
     41     class MyDrawable : public SkDrawable {
     42         SkRect   fR;
     43         SkScalar fSweep;
     44     public:
     45         MyDrawable(const SkRect& r) : fR(r), fSweep(0) {}
     46 
     47         void setSweep(SkScalar sweep) {
     48             if (fSweep != sweep) {
     49                 fSweep = sweep;
     50                 this->notifyDrawingChanged();
     51             }
     52         }
     53 
     54         void onDraw(SkCanvas* canvas) override {
     55             SkPaint paint;
     56             paint.setAntiAlias(true);
     57             paint.setStrokeWidth(SkIntToScalar(2));
     58 
     59             paint.setStyle(SkPaint::kFill_Style);
     60             paint.setColor(0x800000FF);
     61             canvas->drawArc(fR, 0, fSweep, true, paint);
     62 
     63             paint.setColor(0x800FF000);
     64             canvas->drawArc(fR, 0, fSweep, false, paint);
     65 
     66             paint.setStyle(SkPaint::kStroke_Style);
     67             paint.setColor(SK_ColorRED);
     68             canvas->drawArc(fR, 0, fSweep, true, paint);
     69 
     70             paint.setStrokeWidth(0);
     71             paint.setColor(SK_ColorBLUE);
     72             canvas->drawArc(fR, 0, fSweep, false, paint);
     73         }
     74 
     75         SkRect onGetBounds() override {
     76             SkRect r(fR);
     77             r.outset(2, 2);
     78             return r;
     79         }
     80     };
     81 
     82 public:
     83     SkRect fRect;
     84     MyDrawable* fAnimatingDrawable;
     85     SkDrawable* fRootDrawable;
     86 
     87     ArcsView() {
     88         testparse();
     89         fSweep = SkIntToScalar(100);
     90         this->setBGColor(0xFFDDDDDD);
     91 
     92         fRect.set(0, 0, SkIntToScalar(200), SkIntToScalar(200));
     93         fRect.offset(SkIntToScalar(20), SkIntToScalar(20));
     94         fAnimatingDrawable = new MyDrawable(fRect);
     95 
     96         SkPictureRecorder recorder;
     97         this->drawRoot(recorder.beginRecording(SkRect::MakeWH(800, 500)));
     98         fRootDrawable = recorder.endRecordingAsDrawable();
     99     }
    100 
    101     ~ArcsView() override {
    102         fAnimatingDrawable->unref();
    103         fRootDrawable->unref();
    104     }
    105 
    106 protected:
    107     // overrides from SkEventSink
    108     bool onQuery(SkEvent* evt) override {
    109         if (SampleCode::TitleQ(*evt)) {
    110             SampleCode::TitleR(evt, "Arcs");
    111             return true;
    112         }
    113         return this->INHERITED::onQuery(evt);
    114     }
    115 
    116     static void DrawRectWithLines(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
    117         canvas->drawRect(r, p);
    118         canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, p);
    119         canvas->drawLine(r.fLeft, r.fBottom, r.fRight, r.fTop, p);
    120         canvas->drawLine(r.fLeft, r.centerY(), r.fRight, r.centerY(), p);
    121         canvas->drawLine(r.centerX(), r.fTop, r.centerX(), r.fBottom, p);
    122     }
    123 
    124     static void DrawLabel(SkCanvas* canvas, const SkRect& rect, SkScalar start, SkScalar sweep) {
    125         SkPaint paint;
    126 
    127         paint.setAntiAlias(true);
    128         paint.setTextAlign(SkPaint::kCenter_Align);
    129 
    130         SkString    str;
    131 
    132         str.appendScalar(start);
    133         str.append(", ");
    134         str.appendScalar(sweep);
    135         canvas->drawText(str.c_str(), str.size(), rect.centerX(),
    136                          rect.fBottom + paint.getTextSize() * 5/4, paint);
    137     }
    138 
    139     static void DrawArcs(SkCanvas* canvas) {
    140         SkPaint paint;
    141         SkRect  r;
    142         SkScalar w = 75;
    143         SkScalar h = 50;
    144 
    145         r.set(0, 0, w, h);
    146         paint.setAntiAlias(true);
    147         paint.setStyle(SkPaint::kStroke_Style);
    148 
    149         canvas->save();
    150         canvas->translate(SkIntToScalar(10), SkIntToScalar(300));
    151 
    152         paint.setStrokeWidth(SkIntToScalar(1));
    153 
    154         static const SkScalar gAngles[] = {
    155             0, 360,
    156             0, 45,
    157             0, -45,
    158             720, 135,
    159             -90, 269,
    160             -90, 270,
    161             -90, 271,
    162             -180, -270,
    163             225, 90
    164         };
    165 
    166         for (size_t i = 0; i < SK_ARRAY_COUNT(gAngles); i += 2) {
    167             paint.setColor(SK_ColorBLACK);
    168             DrawRectWithLines(canvas, r, paint);
    169 
    170             paint.setColor(SK_ColorRED);
    171             canvas->drawArc(r, gAngles[i], gAngles[i+1], false, paint);
    172 
    173             DrawLabel(canvas, r, gAngles[i], gAngles[i+1]);
    174 
    175             canvas->translate(w * 8 / 7, 0);
    176         }
    177 
    178         canvas->restore();
    179     }
    180 
    181     void drawRoot(SkCanvas* canvas) {
    182         SkPaint paint;
    183         paint.setAntiAlias(true);
    184         paint.setStrokeWidth(SkIntToScalar(2));
    185         paint.setStyle(SkPaint::kStroke_Style);
    186 
    187         DrawRectWithLines(canvas, fRect, paint);
    188 
    189         canvas->drawDrawable(fAnimatingDrawable);
    190 
    191         DrawArcs(canvas);
    192     }
    193 
    194     void onDrawContent(SkCanvas* canvas) override {
    195         canvas->drawDrawable(fRootDrawable);
    196     }
    197 
    198     bool onAnimate(const SkAnimTimer& timer) override {
    199         SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360));
    200         fAnimatingDrawable->setSweep(angle);
    201         return true;
    202     }
    203 
    204     SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
    205      //   fSweep += SK_Scalar1;
    206         this->inval(nullptr);
    207         return this->INHERITED::onFindClickHandler(x, y, modi);
    208     }
    209 
    210 private:
    211     SkScalar fSweep;
    212 
    213     typedef SampleView INHERITED;
    214 };
    215 
    216 //////////////////////////////////////////////////////////////////////////////
    217 
    218 static SkView* MyFactory() { return new ArcsView; }
    219 static SkViewRegister reg(MyFactory);
    220