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 "include/core/SkCanvas.h" 9 #include "include/core/SkColorFilter.h" 10 #include "include/core/SkColorPriv.h" 11 #include "include/core/SkDrawable.h" 12 #include "include/core/SkFont.h" 13 #include "include/core/SkPath.h" 14 #include "include/core/SkPathMeasure.h" 15 #include "include/core/SkPictureRecorder.h" 16 #include "include/core/SkRegion.h" 17 #include "include/core/SkShader.h" 18 #include "include/core/SkString.h" 19 #include "include/effects/Sk1DPathEffect.h" 20 #include "include/effects/SkCornerPathEffect.h" 21 #include "include/effects/SkGradientShader.h" 22 #include "include/utils/SkParsePath.h" 23 #include "include/utils/SkTextUtils.h" 24 #include "src/base/SkRandom.h" 25 #include "src/base/SkUTF.h" 26 #include "tools/viewer/Slide.h" 27 28 class ArcsSlide : public Slide { 29 class MyDrawable : public SkDrawable { 30 SkRect fR; 31 SkScalar fSweep; 32 public: MyDrawable(const SkRect & r)33 MyDrawable(const SkRect& r) : fR(r), fSweep(0) {} 34 setSweep(SkScalar sweep)35 void setSweep(SkScalar sweep) { 36 if (fSweep != sweep) { 37 fSweep = sweep; 38 this->notifyDrawingChanged(); 39 } 40 } 41 onDraw(SkCanvas * canvas)42 void onDraw(SkCanvas* canvas) override { 43 SkPaint paint; 44 paint.setAntiAlias(true); 45 paint.setStrokeWidth(SkIntToScalar(2)); 46 47 paint.setStyle(SkPaint::kFill_Style); 48 paint.setColor(0x800000FF); 49 canvas->drawArc(fR, 0, fSweep, true, paint); 50 51 paint.setColor(0x800FF000); 52 canvas->drawArc(fR, 0, fSweep, false, paint); 53 54 paint.setStyle(SkPaint::kStroke_Style); 55 paint.setColor(SK_ColorRED); 56 canvas->drawArc(fR, 0, fSweep, true, paint); 57 58 paint.setStrokeWidth(0); 59 paint.setColor(SK_ColorBLUE); 60 canvas->drawArc(fR, 0, fSweep, false, paint); 61 } 62 onGetBounds()63 SkRect onGetBounds() override { 64 SkRect r(fR); 65 r.outset(2, 2); 66 return r; 67 } 68 }; 69 70 SkRect fRect = {20, 20, 220, 220}; 71 sk_sp<MyDrawable> fAnimatingDrawable; 72 sk_sp<SkDrawable> fRootDrawable; 73 DrawRectWithLines(SkCanvas * canvas,const SkRect & r,const SkPaint & p)74 static void DrawRectWithLines(SkCanvas* canvas, const SkRect& r, const SkPaint& p) { 75 canvas->drawRect(r, p); 76 canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, p); 77 canvas->drawLine(r.fLeft, r.fBottom, r.fRight, r.fTop, p); 78 canvas->drawLine(r.fLeft, r.centerY(), r.fRight, r.centerY(), p); 79 canvas->drawLine(r.centerX(), r.fTop, r.centerX(), r.fBottom, p); 80 } 81 DrawLabel(SkCanvas * canvas,const SkRect & rect,SkScalar start,SkScalar sweep)82 static void DrawLabel(SkCanvas* canvas, const SkRect& rect, SkScalar start, SkScalar sweep) { 83 SkFont font; 84 SkString str; 85 str.appendScalar(start); 86 str.append(", "); 87 str.appendScalar(sweep); 88 SkTextUtils::DrawString(canvas, str.c_str(), rect.centerX(), 89 rect.fBottom + font.getSize() * 5/4, font, SkPaint(), 90 SkTextUtils::kCenter_Align); 91 } 92 DrawArcs(SkCanvas * canvas)93 static void DrawArcs(SkCanvas* canvas) { 94 SkPaint paint; 95 SkRect r; 96 SkScalar w = 75; 97 SkScalar h = 50; 98 99 r.setWH(w, h); 100 paint.setAntiAlias(true); 101 paint.setStyle(SkPaint::kStroke_Style); 102 103 canvas->save(); 104 canvas->translate(SkIntToScalar(10), SkIntToScalar(300)); 105 106 paint.setStrokeWidth(SkIntToScalar(1)); 107 108 static const SkScalar gAngles[] = { 109 0, 360, 110 0, 45, 111 0, -45, 112 720, 135, 113 -90, 269, 114 -90, 270, 115 -90, 271, 116 -180, -270, 117 225, 90 118 }; 119 120 for (size_t i = 0; i < std::size(gAngles); i += 2) { 121 paint.setColor(SK_ColorBLACK); 122 DrawRectWithLines(canvas, r, paint); 123 124 paint.setColor(SK_ColorRED); 125 canvas->drawArc(r, gAngles[i], gAngles[i+1], false, paint); 126 127 DrawLabel(canvas, r, gAngles[i], gAngles[i+1]); 128 129 canvas->translate(w * 8 / 7, 0); 130 } 131 132 canvas->restore(); 133 } 134 drawRoot(SkCanvas * canvas)135 void drawRoot(SkCanvas* canvas) { 136 SkPaint paint; 137 paint.setAntiAlias(true); 138 paint.setStrokeWidth(SkIntToScalar(2)); 139 paint.setStyle(SkPaint::kStroke_Style); 140 141 DrawRectWithLines(canvas, fRect, paint); 142 143 canvas->drawDrawable(fAnimatingDrawable.get()); 144 145 DrawArcs(canvas); 146 } 147 148 public: ArcsSlide()149 ArcsSlide() { fName = "Arcs"; } 150 load(SkScalar w,SkScalar h)151 void load(SkScalar w, SkScalar h) override { 152 fAnimatingDrawable = sk_make_sp<MyDrawable>(fRect); 153 154 SkPictureRecorder recorder; 155 this->drawRoot(recorder.beginRecording(SkRect::MakeWH(800, 500))); 156 fRootDrawable = recorder.finishRecordingAsDrawable(); 157 } 158 draw(SkCanvas * canvas)159 void draw(SkCanvas* canvas) override { 160 canvas->clear(0xFFDDDDDD); 161 canvas->drawDrawable(fRootDrawable.get()); 162 } 163 animate(double nanos)164 bool animate(double nanos) override { 165 SkScalar angle = SkDoubleToScalar(fmod(1e-9 * nanos * 360 / 24, 360)); 166 if (fAnimatingDrawable) { 167 fAnimatingDrawable->setSweep(angle); 168 } 169 return true; 170 } 171 }; 172 173 DEF_SLIDE( return new ArcsSlide(); ) 174