blob: d8fd1044d640c73991197644660f2071d5bafaf2 [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 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +00009#include "SkBlurMask.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkBlurMaskFilter.h"
11#include "SkCanvas.h"
caryclark@google.com02939ce2012-06-06 12:09:51 +000012#include "SkDevice.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkGradientShader.h"
14#include "SkGraphics.h"
15#include "SkImageDecoder.h"
16#include "SkPath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017#include "SkRandom.h"
18#include "SkRegion.h"
19#include "SkShader.h"
20#include "SkUtils.h"
21#include "SkXfermode.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022#include "SkColorPriv.h"
23#include "SkColorFilter.h"
24#include "SkTime.h"
25#include "SkTypeface.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000026#include "SkView.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000027
reed@android.com8a1c16f2008-12-17 15:59:43 +000028#include "SkOSFile.h"
29#include "SkStream.h"
30
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000031class TextAlphaView : public SampleView {
rmistry@google.comae933ce2012-08-23 18:19:56 +000032public:
33 TextAlphaView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000034 fByte = 0xFF;
35 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000036
reed@android.com8a1c16f2008-12-17 15:59:43 +000037protected:
38 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -070039 bool onQuery(SkEvent* evt) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 if (SampleCode::TitleQ(*evt)) {
tfarina@chromium.org45369a32012-09-30 11:30:01 +000041 SampleCode::TitleR(evt, "TextAlpha");
reed@android.com8a1c16f2008-12-17 15:59:43 +000042 return true;
43 }
44 return this->INHERITED::onQuery(evt);
45 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000046
mtklein36352bf2015-03-25 18:17:31 -070047 void onDrawContent(SkCanvas* canvas) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 const char* str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
49 SkPaint paint;
50 SkScalar x = SkIntToScalar(10);
51 SkScalar y = SkIntToScalar(20);
rmistry@google.comae933ce2012-08-23 18:19:56 +000052
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 paint.setFlags(0x105);
rmistry@google.comae933ce2012-08-23 18:19:56 +000054
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 paint.setARGB(fByte, 0xFF, 0xFF, 0xFF);
rmistry@google.comae933ce2012-08-23 18:19:56 +000056
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000057 paint.setMaskFilter(SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +000058 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(3))));
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 paint.getMaskFilter()->unref();
rmistry@google.comae933ce2012-08-23 18:19:56 +000060
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000061 SkRandom rand;
rmistry@google.comae933ce2012-08-23 18:19:56 +000062
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 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);
halcanary96fcdcc2015-08-27 07:41:13 -070068 y += paint.getFontMetrics(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000071
mtklein36352bf2015-03-25 18:17:31 -070072 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 return new Click(this);
74 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000075
mtklein36352bf2015-03-25 18:17:31 -070076 bool onClick(Click* click) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 int y = click->fICurr.fY;
78 if (y < 0) {
79 y = 0;
80 } else if (y > 255) {
81 y = 255;
82 }
83 fByte = y;
halcanary96fcdcc2015-08-27 07:41:13 -070084 this->inval(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 return true;
86 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000087
reed@android.com8a1c16f2008-12-17 15:59:43 +000088private:
89 int fByte;
90
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000091 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000092};
93
94//////////////////////////////////////////////////////////////////////////////
95
96static SkView* MyFactory() { return new TextAlphaView; }
97static SkViewRegister reg(MyFactory);