| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ | 
| bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" | 
|  | 9 | #include "SkView.h" | 
|  | 10 | #include "SkCanvas.h" | 
|  | 11 | #include "Sk64.h" | 
|  | 12 | #include "SkGradientShader.h" | 
|  | 13 |  | 
|  | 14 | static void draw_gradient2(SkCanvas* canvas, const SkRect& rect, SkScalar delta) { | 
|  | 15 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorMAGENTA }; | 
|  | 16 | SkScalar pos[] = { 0, SkFloatToScalar(0.25f), SkFloatToScalar(0.75f), SK_Scalar1 }; | 
|  | 17 |  | 
|  | 18 | SkScalar l = rect.fLeft; | 
|  | 19 | SkScalar t = rect.fTop; | 
|  | 20 | SkScalar w = rect.width(); | 
|  | 21 | SkScalar h = rect.height(); | 
|  | 22 |  | 
|  | 23 | SkASSERT(0 == SkScalarMod(w, SK_Scalar1 * 5)); | 
|  | 24 |  | 
|  | 25 | SkPoint c0 = { l + 2 * w / 5 + delta, t + h / 2 }; | 
|  | 26 | SkPoint c1 = { l + 3 * w / 5, t + h / 2 }; | 
|  | 27 | SkScalar r0 = w / 5; | 
|  | 28 | SkScalar r1 = 2 * w / 5; | 
| rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame^] | 29 | SkShader* s = SkGradientShader::CreateTwoPointConical(c0, r0, c1, r1, colors, | 
| bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 30 | pos, SK_ARRAY_COUNT(pos), | 
|  | 31 | SkShader::kClamp_TileMode); | 
|  | 32 | SkPaint paint; | 
|  | 33 | paint.setShader(s)->unref(); | 
|  | 34 |  | 
|  | 35 | canvas->drawRect(rect, paint); | 
|  | 36 | } | 
|  | 37 |  | 
|  | 38 |  | 
|  | 39 | class DegenerateTwoPtRadialsView : public SampleView { | 
|  | 40 |  | 
|  | 41 | public: | 
|  | 42 | DegenerateTwoPtRadialsView() { | 
|  | 43 | fTime = 0; | 
|  | 44 | this->setBGColor(0xFFDDDDDD); | 
|  | 45 | } | 
|  | 46 |  | 
|  | 47 | protected: | 
|  | 48 | // overrides from SkEventSink | 
|  | 49 | virtual bool onQuery(SkEvent* evt) { | 
|  | 50 | if (SampleCode::TitleQ(*evt)) { | 
|  | 51 | SampleCode::TitleR(evt, "DegenerateTwoPtRadials"); | 
|  | 52 | return true; | 
|  | 53 | } | 
|  | 54 | return this->INHERITED::onQuery(evt); | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | virtual void onDrawContent(SkCanvas* canvas) { | 
|  | 58 | fTime += SampleCode::GetAnimSecondsDelta(); | 
|  | 59 | SkScalar delta = fTime / 15.f; | 
|  | 60 | int intPart = SkScalarFloor(delta); | 
|  | 61 | delta = delta - SK_Scalar1 * intPart; | 
|  | 62 | if (intPart % 2) { | 
|  | 63 | delta = SK_Scalar1 - delta; | 
|  | 64 | } | 
|  | 65 | delta -= SK_ScalarHalf; | 
|  | 66 | static const int DELTA_SCALE = 500; | 
|  | 67 | delta /= DELTA_SCALE; | 
|  | 68 |  | 
| bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 69 | SkScalar w = SK_Scalar1 * 500; | 
|  | 70 | SkScalar h = SK_Scalar1 * 500; | 
|  | 71 | SkScalar l = SK_Scalar1 * 100; | 
|  | 72 | SkScalar t = SK_Scalar1 * 100; | 
|  | 73 | draw_gradient2(canvas, SkRect::MakeXYWH(l, t, w, h), delta); | 
|  | 74 | char txt[512]; | 
|  | 75 | sprintf(txt, "gap at \"tangent\" pt = %f", SkScalarToFloat(delta)); | 
|  | 76 | SkPaint paint; | 
|  | 77 | paint.setAntiAlias(true); | 
|  | 78 | paint.setColor(SK_ColorBLACK); | 
|  | 79 | canvas->drawText(txt, strlen(txt), l + w/2 + w*DELTA_SCALE*delta, t + h + SK_Scalar1 * 10, paint); | 
|  | 80 | this->inval(NULL); | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | private: | 
|  | 84 | SkScalar           fTime; | 
|  | 85 | typedef SampleView INHERITED; | 
|  | 86 | }; | 
|  | 87 |  | 
|  | 88 | ////////////////////////////////////////////////////////////////////////////// | 
|  | 89 |  | 
|  | 90 | static SkView* MyFactory() { return new DegenerateTwoPtRadialsView; } | 
|  | 91 | static SkViewRegister reg(MyFactory); |