blob: 32b45374e6c7642abffe6676ec94f21a1a4ef73f [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
reed@google.combf0001d2014-01-13 14:53:55 +00007
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00008#include "SampleCode.h"
reed76113a92015-02-02 12:55:02 -08009#include "SkAnimTimer.h"
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000010#include "SkView.h"
11#include "SkCanvas.h"
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000012#include "SkGradientShader.h"
bungeman@google.comfab44db2013-10-11 18:50:45 +000013#include "SkString.h"
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000014
15static void draw_gradient2(SkCanvas* canvas, const SkRect& rect, SkScalar delta) {
16 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorMAGENTA };
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000017 SkScalar pos[] = { 0, 0.25f, 0.75f, SK_Scalar1 };
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000018
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;
rileya@google.com3e332582012-07-03 13:43:35 +000030 SkShader* s = SkGradientShader::CreateTwoPointConical(c0, r0, c1, r1, colors,
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000031 pos, SK_ARRAY_COUNT(pos),
32 SkShader::kClamp_TileMode);
33 SkPaint paint;
34 paint.setShader(s)->unref();
35
36 canvas->drawRect(rect, paint);
37}
38
39
40class DegenerateTwoPtRadialsView : public SampleView {
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000041public:
42 DegenerateTwoPtRadialsView() {
43 fTime = 0;
44 this->setBGColor(0xFFDDDDDD);
45 }
46
47protected:
mtklein36352bf2015-03-25 18:17:31 -070048 bool onQuery(SkEvent* evt) override {
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000049 if (SampleCode::TitleQ(*evt)) {
50 SampleCode::TitleR(evt, "DegenerateTwoPtRadials");
51 return true;
52 }
53 return this->INHERITED::onQuery(evt);
54 }
55
mtklein36352bf2015-03-25 18:17:31 -070056 void onDrawContent(SkCanvas* canvas) override {
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000057 SkScalar delta = fTime / 15.f;
reed@google.come1ca7052013-12-17 19:22:07 +000058 int intPart = SkScalarFloorToInt(delta);
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000059 delta = delta - SK_Scalar1 * intPart;
60 if (intPart % 2) {
61 delta = SK_Scalar1 - delta;
62 }
63 delta -= SK_ScalarHalf;
64 static const int DELTA_SCALE = 500;
65 delta /= DELTA_SCALE;
66
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000067 SkScalar w = SK_Scalar1 * 500;
68 SkScalar h = SK_Scalar1 * 500;
69 SkScalar l = SK_Scalar1 * 100;
70 SkScalar t = SK_Scalar1 * 100;
71 draw_gradient2(canvas, SkRect::MakeXYWH(l, t, w, h), delta);
bungeman@google.comfab44db2013-10-11 18:50:45 +000072 SkString txt;
73 txt.appendf("gap at \"tangent\" pt = %f", SkScalarToFloat(delta));
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000074 SkPaint paint;
75 paint.setAntiAlias(true);
76 paint.setColor(SK_ColorBLACK);
bungeman@google.comfab44db2013-10-11 18:50:45 +000077 canvas->drawText(txt.c_str(), txt.size(), l + w/2 + w*DELTA_SCALE*delta, t + h + SK_Scalar1 * 10, paint);
reedd9adfe62015-02-01 19:01:04 -080078 }
79
mtklein36352bf2015-03-25 18:17:31 -070080 bool onAnimate(const SkAnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -080081 fTime = SkDoubleToScalar(timer.secs() / 15);
reedd9adfe62015-02-01 19:01:04 -080082 return true;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000083 }
84
85private:
86 SkScalar fTime;
87 typedef SampleView INHERITED;
88};
89
90//////////////////////////////////////////////////////////////////////////////
91
92static SkView* MyFactory() { return new DegenerateTwoPtRadialsView; }
93static SkViewRegister reg(MyFactory);