Home | History | Annotate | Download | only in samplecode
      1 #include "SampleCode.h"
      2 #include "SkView.h"
      3 #include "SkCanvas.h"
      4 #include "SkGradientShader.h"
      5 #include "SkPath.h"
      6 #include "SkRegion.h"
      7 #include "SkShader.h"
      8 #include "SkUtils.h"
      9 #include "SkComposeShader.h"
     10 #include "Sk1DPathEffect.h"
     11 #include "SkCornerPathEffect.h"
     12 #include "SkPathMeasure.h"
     13 #include "SkRandom.h"
     14 #include "SkColorPriv.h"
     15 #include "SkColorFilter.h"
     16 #include "SkLayerRasterizer.h"
     17 
     18 #include "SkParsePath.h"
     19 static void testparse() {
     20     SkRect r;
     21     r.set(0, 0, SkFloatToScalar(10), SkFloatToScalar(10.5));
     22     SkPath p, p2;
     23     SkString str, str2;
     24 
     25     p.addRect(r);
     26     SkParsePath::ToSVGString(p, &str);
     27     SkParsePath::FromSVGString(str.c_str(), &p2);
     28     SkParsePath::ToSVGString(p2, &str2);
     29 }
     30 
     31 class ArcsView : public SampleView {
     32 public:
     33 	ArcsView() {
     34         testparse();
     35         fSweep = SkIntToScalar(100);
     36         this->setBGColor(0xFFDDDDDD);
     37     }
     38 
     39 protected:
     40     // overrides from SkEventSink
     41     virtual bool onQuery(SkEvent* evt) {
     42         if (SampleCode::TitleQ(*evt)) {
     43             SampleCode::TitleR(evt, "Arcs");
     44             return true;
     45         }
     46         return this->INHERITED::onQuery(evt);
     47     }
     48 
     49     static void drawRectWithLines(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
     50         canvas->drawRect(r, p);
     51         canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, p);
     52         canvas->drawLine(r.fLeft, r.fBottom, r.fRight, r.fTop, p);
     53         canvas->drawLine(r.fLeft, r.centerY(), r.fRight, r.centerY(), p);
     54         canvas->drawLine(r.centerX(), r.fTop, r.centerX(), r.fBottom, p);
     55     }
     56 
     57     static void draw_label(SkCanvas* canvas, const SkRect& rect,
     58                             int start, int sweep) {
     59         SkPaint paint;
     60 
     61         paint.setAntiAlias(true);
     62         paint.setTextAlign(SkPaint::kCenter_Align);
     63 
     64         SkString    str;
     65 
     66         str.appendS32(start);
     67         str.append(", ");
     68         str.appendS32(sweep);
     69         canvas->drawText(str.c_str(), str.size(), rect.centerX(),
     70                          rect.fBottom + paint.getTextSize() * 5/4, paint);
     71     }
     72 
     73     static void drawArcs(SkCanvas* canvas) {
     74         SkPaint paint;
     75         SkRect  r;
     76         SkScalar w = SkIntToScalar(75);
     77         SkScalar h = SkIntToScalar(50);
     78 
     79         r.set(0, 0, w, h);
     80         paint.setAntiAlias(true);
     81         paint.setStyle(SkPaint::kStroke_Style);
     82 
     83         canvas->save();
     84         canvas->translate(SkIntToScalar(10), SkIntToScalar(300));
     85 
     86         paint.setStrokeWidth(SkIntToScalar(1));
     87 
     88         static const int gAngles[] = {
     89             0, 360,
     90             0, 45,
     91             0, -45,
     92             720, 135,
     93             -90, 269,
     94             -90, 270,
     95             -90, 271,
     96             -180, -270,
     97             225, 90
     98         };
     99 
    100         for (size_t i = 0; i < SK_ARRAY_COUNT(gAngles); i += 2) {
    101             paint.setColor(SK_ColorBLACK);
    102             drawRectWithLines(canvas, r, paint);
    103 
    104             paint.setColor(SK_ColorRED);
    105             canvas->drawArc(r, SkIntToScalar(gAngles[i]),
    106                             SkIntToScalar(gAngles[i+1]), false, paint);
    107 
    108             draw_label(canvas, r, gAngles[i], gAngles[i+1]);
    109 
    110             canvas->translate(w * 8 / 7, 0);
    111         }
    112 
    113         canvas->restore();
    114     }
    115 
    116     virtual void onDrawContent(SkCanvas* canvas) {
    117         fSweep = SampleCode::GetAnimScalar(SkIntToScalar(360)/24,
    118                                            SkIntToScalar(360));
    119 //        fSweep = SkFloatToScalar(359.99f);
    120 
    121         SkRect  r;
    122         SkPaint paint;
    123 
    124         paint.setAntiAlias(true);
    125         paint.setStrokeWidth(SkIntToScalar(2));
    126         paint.setStyle(SkPaint::kStroke_Style);
    127 
    128         r.set(0, 0, SkIntToScalar(200), SkIntToScalar(200));
    129         r.offset(SkIntToScalar(20), SkIntToScalar(20));
    130 
    131         if (false) {
    132             const SkScalar d = SkIntToScalar(3);
    133             const SkScalar rad[] = { d, d, d, d, d, d, d, d };
    134             SkPath path;
    135             path.addRoundRect(r, rad);
    136             canvas->drawPath(path, paint);
    137             return;
    138         }
    139 
    140         drawRectWithLines(canvas, r, paint);
    141 
    142    //     printf("----- sweep %g %X\n", SkScalarToFloat(fSweep), SkDegreesToRadians(fSweep));
    143 
    144 
    145         paint.setStyle(SkPaint::kFill_Style);
    146         paint.setColor(0x800000FF);
    147         canvas->drawArc(r, 0, fSweep, true, paint);
    148 
    149         paint.setColor(0x800FF000);
    150         canvas->drawArc(r, 0, fSweep, false, paint);
    151 
    152         paint.setStyle(SkPaint::kStroke_Style);
    153         paint.setColor(SK_ColorRED);
    154         canvas->drawArc(r, 0, fSweep, true, paint);
    155 
    156         paint.setStrokeWidth(0);
    157         paint.setColor(SK_ColorBLUE);
    158         canvas->drawArc(r, 0, fSweep, false, paint);
    159 
    160         drawArcs(canvas);
    161         this->inval(NULL);
    162     }
    163 
    164     virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
    165      //   fSweep += SK_Scalar1;
    166         this->inval(NULL);
    167         return this->INHERITED::onFindClickHandler(x, y);
    168     }
    169 
    170     virtual bool onClick(Click* click) {
    171         return this->INHERITED::onClick(click);
    172     }
    173 
    174 private:
    175     SkScalar fSweep;
    176 
    177     typedef SampleView INHERITED;
    178 };
    179 
    180 //////////////////////////////////////////////////////////////////////////////
    181 
    182 static SkView* MyFactory() { return new ArcsView; }
    183 static SkViewRegister reg(MyFactory);
    184 
    185