Home | History | Annotate | Download | only in samplecode
      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 
     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     SkShader* s = SkGradientShader::CreateTwoPointConical(c0, r0, c1, r1, colors,
     31                                                          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 
     40 class DegenerateTwoPtRadialsView : public SampleView {
     41 public:
     42     DegenerateTwoPtRadialsView() {
     43         fTime = 0;
     44         this->setBGColor(0xFFDDDDDD);
     45     }
     46 
     47 protected:
     48     bool onQuery(SkEvent* evt) override {
     49         if (SampleCode::TitleQ(*evt)) {
     50             SampleCode::TitleR(evt, "DegenerateTwoPtRadials");
     51             return true;
     52         }
     53         return this->INHERITED::onQuery(evt);
     54     }
     55 
     56     void onDrawContent(SkCanvas* canvas) override {
     57         SkScalar delta = fTime / 15.f;
     58         int intPart = SkScalarFloorToInt(delta);
     59         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 
     67         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);
     72         SkString txt;
     73         txt.appendf("gap at \"tangent\" pt = %f", SkScalarToFloat(delta));
     74         SkPaint paint;
     75         paint.setAntiAlias(true);
     76         paint.setColor(SK_ColorBLACK);
     77         canvas->drawText(txt.c_str(), txt.size(), l + w/2 + w*DELTA_SCALE*delta, t + h + SK_Scalar1 * 10, paint);
     78     }
     79 
     80     bool onAnimate(const SkAnimTimer& timer) override {
     81         fTime = SkDoubleToScalar(timer.secs() / 15);
     82         return true;
     83     }
     84 
     85 private:
     86     SkScalar           fTime;
     87     typedef SampleView INHERITED;
     88 };
     89 
     90 //////////////////////////////////////////////////////////////////////////////
     91 
     92 static SkView* MyFactory() { return new DegenerateTwoPtRadialsView; }
     93 static SkViewRegister reg(MyFactory);
     94