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