blob: c4a4ec5478b0ef75b38d36841226e2782acff18d [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
yangsu@google.com4c295a32011-06-01 16:11:58 +00008#include "SampleCode.h"
9#include "SkCanvas.h"
10#include "SkGradientShader.h"
11
12class GradientView : public SampleView {
13public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000014 GradientView() {
yangsu@google.com4c295a32011-06-01 16:11:58 +000015 this->setBGColor(0xFFDDDDDD);
rmistry@google.comae933ce2012-08-23 18:19:56 +000016 }
17
yangsu@google.com4c295a32011-06-01 16:11:58 +000018protected:
rmistry@google.comae933ce2012-08-23 18:19:56 +000019 struct GradData {
yangsu@google.com4c295a32011-06-01 16:11:58 +000020 int fCount;
21 const SkColor* fColors;
22 const SkScalar* fPos;
23 };
24 // overrides from SkEventSink
25 virtual bool onQuery(SkEvent* evt) {
26 if (SampleCode::TitleQ(*evt)) {
27 SampleCode::TitleR(evt, "Gradients");
28 return true;
29 }
30 return this->INHERITED::onQuery(evt);
31 }
32 virtual void onDrawContent(SkCanvas* canvas) {
33 SkPaint paint;
34 paint.setAntiAlias(true);
35 paint.setStyle(SkPaint::kStroke_Style);
36 paint.setStrokeWidth(SkScalarHalf(SkIntToScalar(3)));
37 paint.setStyle(SkPaint::kFill_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +000038
yangsu@google.com4c295a32011-06-01 16:11:58 +000039 SkPoint p = SkPoint::Make(0,0);
40 SkPoint q = SkPoint::Make(100,100);
41 SkPoint pts[] = {p, q};
rmistry@google.comae933ce2012-08-23 18:19:56 +000042
yangsu@google.com4c295a32011-06-01 16:11:58 +000043 SkScalar t, temp, x, y;
44 SkColor gColors[] = {
45 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK
46 };
47 t = SampleCode::GetAnimScalar(SkIntToScalar(2), SkIntToScalar(20));
48 temp = SampleCode::GetAnimScalar(SkIntToScalar(1), SkIntToScalar(8));
49 SkScalar step = SK_ScalarPI / (10);
50 SkScalar angle = t * step;
51 x = SkScalarSinCos(angle, &y);
52 SkScalar colorPositions[] = { 0, 0.1 + x, 0.4 + y, 0.9 - x + y, 1.0};
53 GradData data = { 5, gColors, colorPositions };
rmistry@google.comae933ce2012-08-23 18:19:56 +000054
55
yangsu@google.com4c295a32011-06-01 16:11:58 +000056 SkRect r = { 0, 0, SkIntToScalar(200), SkIntToScalar(200) };
57 SkShader* shader1 = SkGradientShader::CreateLinear(
rmistry@google.comae933ce2012-08-23 18:19:56 +000058 pts, data.fColors, data.fPos,data.fCount,
yangsu@google.com4c295a32011-06-01 16:11:58 +000059 SkShader::kMirror_TileMode);
60 paint.setShader(shader1)->unref();
rmistry@google.comae933ce2012-08-23 18:19:56 +000061
yangsu@google.com4c295a32011-06-01 16:11:58 +000062 canvas->drawRect(r, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000063
64
yangsu@google.com4c295a32011-06-01 16:11:58 +000065 SkPoint s = SkPoint::Make(100,100);
66 SkShader* shader2 = SkGradientShader::CreateRadial(
rmistry@google.comae933ce2012-08-23 18:19:56 +000067 s, 100, data.fColors, data.fPos, data.fCount,
yangsu@google.com4c295a32011-06-01 16:11:58 +000068 SkShader::kMirror_TileMode);
69 paint.setShader(shader2)->unref();
70 canvas->translate(250, 0);
71 canvas->drawRect(r, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000072
yangsu@google.com4c295a32011-06-01 16:11:58 +000073 SkShader* shader3 = SkGradientShader::CreateTwoPointRadial(
74 p, 0, q, 100, data.fColors, data.fPos, data.fCount,
75 SkShader::kMirror_TileMode);
76 paint.setShader(shader3)->unref();
77 canvas->translate(0, 250);
78 canvas->drawRect(r, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000079
yangsu@google.com4c295a32011-06-01 16:11:58 +000080 SkShader* shader4 = SkGradientShader::CreateSweep(
81 100, 100, data.fColors, data.fPos, data.fCount);
rmistry@google.comae933ce2012-08-23 18:19:56 +000082
yangsu@google.com4c295a32011-06-01 16:11:58 +000083 paint.setShader(shader4)->unref();
84 canvas->translate(-250, 0);
85 canvas->drawRect(r, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000086
87 this->inval(NULL);
yangsu@google.com4c295a32011-06-01 16:11:58 +000088 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000089
yangsu@google.com4c295a32011-06-01 16:11:58 +000090private:
91 typedef SampleView INHERITED;
92};
93
94//////////////////////////////////////////////////////////////////////////////
95
96static SkView* MyFactory() { return new GradientView; }
rmistry@google.combda03db2012-08-14 20:27:54 +000097static SkViewRegister reg(MyFactory);
98