58e1db2686d21d34b711cdeeb31f231279a63ca1
[platform/upstream/libSkiaSharp.git] / samplecode / SampleTextAlpha.cpp
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 "SkBlurMask.h"
10 #include "SkBlurMaskFilter.h"
11 #include "SkCanvas.h"
12 #include "SkDevice.h"
13 #include "SkGradientShader.h"
14 #include "SkGraphics.h"
15 #include "SkImageDecoder.h"
16 #include "SkPath.h"
17 #include "SkRandom.h"
18 #include "SkRegion.h"
19 #include "SkShader.h"
20 #include "SkUtils.h"
21 #include "SkXfermode.h"
22 #include "SkColorPriv.h"
23 #include "SkColorFilter.h"
24 #include "SkTime.h"
25 #include "SkTypeface.h"
26 #include "SkView.h"
27
28 #include "SkOSFile.h"
29 #include "SkStream.h"
30
31 class TextAlphaView : public SampleView {
32 public:
33     TextAlphaView() {
34         fByte = 0xFF;
35     }
36
37 protected:
38     // overrides from SkEventSink
39     bool onQuery(SkEvent* evt) SK_OVERRIDE {
40         if (SampleCode::TitleQ(*evt)) {
41             SampleCode::TitleR(evt, "TextAlpha");
42             return true;
43         }
44         return this->INHERITED::onQuery(evt);
45     }
46
47     void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
48         const char* str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
49         SkPaint paint;
50         SkScalar    x = SkIntToScalar(10);
51         SkScalar    y = SkIntToScalar(20);
52
53         paint.setFlags(0x105);
54
55         paint.setARGB(fByte, 0xFF, 0xFF, 0xFF);
56
57         paint.setMaskFilter(SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
58                                     SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(3))));
59         paint.getMaskFilter()->unref();
60
61         SkRandom rand;
62
63         for (int ps = 6; ps <= 35; ps++) {
64             paint.setColor(rand.nextU() | (0xFF << 24));
65             paint.setTextSize(SkIntToScalar(ps));
66             paint.setTextSize(SkIntToScalar(24));
67             canvas->drawText(str, strlen(str), x, y, paint);
68             y += paint.getFontMetrics(NULL);
69         }
70     }
71
72     SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) SK_OVERRIDE {
73         return new Click(this);
74     }
75
76     bool onClick(Click* click) SK_OVERRIDE {
77         int y = click->fICurr.fY;
78         if (y < 0) {
79             y = 0;
80         } else if (y > 255) {
81             y = 255;
82         }
83         fByte = y;
84         this->inval(NULL);
85         return true;
86     }
87
88 private:
89     int fByte;
90
91     typedef SampleView INHERITED;
92 };
93
94 //////////////////////////////////////////////////////////////////////////////
95
96 static SkView* MyFactory() { return new TextAlphaView; }
97 static SkViewRegister reg(MyFactory);