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 "SkGradientShader.h"
13 #include "SkString.h"
14
draw_gradient2(SkCanvas * canvas,const SkRect & rect,SkScalar delta)15 static void draw_gradient2(SkCanvas* canvas, const SkRect& rect, SkScalar delta) {
16 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorMAGENTA };
17 SkScalar pos[] = { 0, 0.25f, 0.75f, SK_Scalar1 };
18
19 SkScalar l = rect.fLeft;
20 SkScalar t = rect.fTop;
21 SkScalar w = rect.width();
22 SkScalar h = rect.height();
23
24 SkASSERT(0 == SkScalarMod(w, SK_Scalar1 * 5));
25
26 SkPoint c0 = { l + 2 * w / 5 + delta, t + h / 2 };
27 SkPoint c1 = { l + 3 * w / 5, t + h / 2 };
28 SkScalar r0 = w / 5;
29 SkScalar r1 = 2 * w / 5;
30 SkPaint paint;
31 paint.setShader(SkGradientShader::MakeTwoPointConical(c0, r0, c1, r1, colors,
32 pos, SK_ARRAY_COUNT(pos),
33 SkShader::kClamp_TileMode));
34 canvas->drawRect(rect, paint);
35 }
36
37
38 class DegenerateTwoPtRadialsView : public SampleView {
39 public:
DegenerateTwoPtRadialsView()40 DegenerateTwoPtRadialsView() {
41 fTime = 0;
42 this->setBGColor(0xFFDDDDDD);
43 }
44
45 protected:
onQuery(SkEvent * evt)46 bool onQuery(SkEvent* evt) override {
47 if (SampleCode::TitleQ(*evt)) {
48 SampleCode::TitleR(evt, "DegenerateTwoPtRadials");
49 return true;
50 }
51 return this->INHERITED::onQuery(evt);
52 }
53
onDrawContent(SkCanvas * canvas)54 void onDrawContent(SkCanvas* canvas) override {
55 SkScalar delta = fTime / 15.f;
56 int intPart = SkScalarFloorToInt(delta);
57 delta = delta - SK_Scalar1 * intPart;
58 if (intPart % 2) {
59 delta = SK_Scalar1 - delta;
60 }
61 delta -= SK_ScalarHalf;
62 static const int DELTA_SCALE = 500;
63 delta /= DELTA_SCALE;
64
65 SkScalar w = SK_Scalar1 * 500;
66 SkScalar h = SK_Scalar1 * 500;
67 SkScalar l = SK_Scalar1 * 100;
68 SkScalar t = SK_Scalar1 * 100;
69 draw_gradient2(canvas, SkRect::MakeXYWH(l, t, w, h), delta);
70 SkString txt;
71 txt.appendf("gap at \"tangent\" pt = %f", SkScalarToFloat(delta));
72 SkPaint paint;
73 paint.setAntiAlias(true);
74 paint.setColor(SK_ColorBLACK);
75 canvas->drawString(txt, l + w/2 + w*DELTA_SCALE*delta, t + h + SK_Scalar1 * 10, paint);
76 }
77
onAnimate(const SkAnimTimer & timer)78 bool onAnimate(const SkAnimTimer& timer) override {
79 fTime = SkDoubleToScalar(timer.secs() / 15);
80 return true;
81 }
82
83 private:
84 SkScalar fTime;
85 typedef SampleView INHERITED;
86 };
87
88 //////////////////////////////////////////////////////////////////////////////
89
MyFactory()90 static SkView* MyFactory() { return new DegenerateTwoPtRadialsView; }
91 static SkViewRegister reg(MyFactory);
92