SampleEffects.cpp revision e012e0a3414e8ab93dc90fd139256f2a71fc6f9f
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 */
8#include "SampleCode.h"
9#include "SkCanvas.h"
10#include "SkPaint.h"
11#include "SkView.h"
12
13#include "SkBlurMaskFilter.h"
14#include "SkColorMatrixFilter.h"
15#include "SkDiscretePathEffect.h"
16#include "SkGradientShader.h"
17
18//#define COLOR 0xFFFF8844
19#define COLOR 0xFF888888
20
21static void paint_proc0(SkPaint* paint) {
22}
23
24static void paint_proc1(SkPaint* paint) {
25    paint->setMaskFilter(SkBlurMaskFilter::Create(2,
26                                SkBlurMaskFilter::kNormal_BlurStyle))->unref();
27}
28
29static void paint_proc2(SkPaint* paint) {
30    SkScalar dir[3] = { 1, 1, 1};
31    paint->setMaskFilter(
32                     SkBlurMaskFilter::CreateEmboss(dir, 0.1f, 0.05f, 1))->unref();
33}
34
35static void paint_proc3(SkPaint* paint) {
36    SkColor colors[] = { SK_ColorRED, COLOR, SK_ColorBLUE };
37    SkPoint pts[] = { { 3, 0 }, { 7, 5 } };
38    paint->setShader(SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors),
39                                        SkShader::kMirror_TileMode))->unref();
40}
41
42static void paint_proc5(SkPaint* paint) {
43    paint_proc3(paint);
44    paint_proc2(paint);
45}
46
47typedef void (*PaintProc)(SkPaint*);
48const PaintProc gPaintProcs[] = {
49    paint_proc0,
50    paint_proc1,
51    paint_proc2,
52    paint_proc3,
53    paint_proc5,
54};
55
56///////////////////////////////////////////////////////////////////////////////
57
58class EffectsView : public SampleView {
59public:
60    SkPath fPath;
61    SkPaint fPaint[SK_ARRAY_COUNT(gPaintProcs)];
62
63	EffectsView() {
64        size_t i;
65        const float pts[] = {
66            0, 0,
67            10, 0,
68            10, 5,
69            20, -5,
70            10, -15,
71            10, -10,
72            0, -10
73        };
74        fPath.moveTo(pts[0], pts[1]);
75        for (i = 2; i < SK_ARRAY_COUNT(pts); i += 2) {
76            fPath.lineTo(pts[i], pts[i+1]);
77        }
78
79        for (i = 0; i < SK_ARRAY_COUNT(gPaintProcs); i++) {
80            fPaint[i].setAntiAlias(true);
81            fPaint[i].setColor(COLOR);
82            gPaintProcs[i](&fPaint[i]);
83        }
84
85        SkColorMatrix cm;
86        cm.setRotate(SkColorMatrix::kG_Axis, 180);
87        cm.setIdentity();
88
89        this->setBGColor(0xFFDDDDDD);
90    }
91
92protected:
93    // overrides from SkEventSink
94    virtual bool onQuery(SkEvent* evt) {
95        if (SampleCode::TitleQ(*evt)) {
96            SampleCode::TitleR(evt, "Effects");
97            return true;
98        }
99        return this->INHERITED::onQuery(evt);
100    }
101
102    virtual void onDrawContent(SkCanvas* canvas) {
103        canvas->scale(3, 3);
104        canvas->translate(10, 30);
105        for (size_t i = 0; i < SK_ARRAY_COUNT(fPaint); i++) {
106            canvas->drawPath(fPath, fPaint[i]);
107            canvas->translate(32, 0);
108        }
109    }
110
111private:
112    typedef SampleView INHERITED;
113};
114
115///////////////////////////////////////////////////////////////////////////////
116
117static SkView* MyFactory() { return new EffectsView; }
118static SkViewRegister reg(MyFactory);
119