1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
/*
* Copyright 2015 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SampleCode.h"
#include "SkAnimTimer.h"
#include "SkDrawable.h"
#include "SkView.h"
#include "SkCanvas.h"
#include "SkDrawable.h"
#include "SkPath.h"
#include "SkRandom.h"
#include "SkRSXform.h"
#include "SkSurface.h"
#include "SkGradientShader.h"
const SkBlendMode gModes[] = {
SkBlendMode::kSrcOver,
SkBlendMode::kSrc,
SkBlendMode::kSrcIn,
SkBlendMode::kSrcOut,
SkBlendMode::kSrcATop,
SkBlendMode::kDstOver,
SkBlendMode::kDstIn,
SkBlendMode::kDstOut,
SkBlendMode::kDstATop,
};
const int N_Modes = SK_ARRAY_COUNT(gModes);
static SkRandom gRand;
class PushButtonWig : public SkView {
SkString fLabel;
SkColor fColor;
uint32_t fFast32;
public:
PushButtonWig(const char label[], uint32_t fast) : fLabel(label) {
fColor = (gRand.nextU() & 0x7F7F7F7F) | SkColorSetARGB(0xFF, 0, 0, 0x80);
fFast32 = fast;
}
void postWidgetEvent() {
SkEvent evt;
evt.setType("push-button");
evt.setFast32(fFast32);
evt.setString("label", fLabel.c_str());
this->sendEventToParents(evt);
}
protected:
void onDraw(SkCanvas* canvas) override {
SkRect r;
this->getLocalBounds(&r);
SkPaint paint;
paint.setAntiAlias(true);
paint.setColor(fColor);
canvas->drawRoundRect(r, 8, 8, paint);
paint.setColor(0xFFFFFFFF);
paint.setTextSize(16);
paint.setTextAlign(SkPaint::kCenter_Align);
paint.setLCDRenderText(true);
canvas->drawString(fLabel, r.centerX(), r.fTop + 0.68f * r.height(), paint);
}
Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
return new Click(this);
}
bool onClick(Click* click) override {
SkRect target = SkRect::MakeXYWH(click->fCurr.x() - 1, click->fCurr.y() - 1, 3, 3);
SkRect r;
this->getLocalBounds(&r);
if (r.intersects(target)) {
fColor = SkColorSetA(fColor, 0x99);
} else {
fColor = SkColorSetA(fColor, 0xFF);
}
this->inval(nullptr);
if (click->fState == SkView::Click::kUp_State) {
this->postWidgetEvent();
}
return true;
}
};
class ModeDrawable : public SkDrawable {
public:
ModeDrawable() : fMode(SkBlendMode::kSrcOver), fLoc(SkPoint::Make(0, 0)) {}
SkBlendMode fMode;
SkPoint fLoc;
bool hitTest(SkScalar x, SkScalar y) {
SkRect target = SkRect::MakeXYWH(x - fLoc.x() - 1, y - fLoc.y() - 1, 3, 3);
return this->getBounds().intersects(target);
}
};
class CircDrawable : public ModeDrawable {
SkPaint fPaint;
SkRect fBounds;
public:
CircDrawable(SkScalar size, SkColor c) {
const SkColor colors[] = { 0, c };
fPaint.setShader(SkGradientShader::MakeRadial(SkPoint::Make(size/2, size/2), size/2,
colors, nullptr, 2,
SkShader::kClamp_TileMode));
fBounds = SkRect::MakeWH(size, size);
}
protected:
SkRect onGetBounds() override {
return fBounds;
}
void onDraw(SkCanvas* canvas) override {
fPaint.setBlendMode(fMode);
canvas->save();
canvas->translate(fLoc.x(), fLoc.y());
canvas->drawOval(fBounds, fPaint);
canvas->restore();
}
};
class XferDemo : public SampleView {
enum {
N = 4
};
SkRect fModeRect[N_Modes];
sk_sp<CircDrawable> fDrs[N];
CircDrawable* fSelected;
void addButtons() {
SkScalar x = 10;
SkScalar y = 10;
for (int i = 0; i < N_Modes; ++i) {
sk_sp<SkView> v(new PushButtonWig(SkBlendMode_Name(gModes[i]), (int)gModes[i]));
v->setSize(70, 25);
v->setLoc(x, y);
v->setVisibleP(true);
v->setEnabledP(true);
this->attachChildToFront(v.get());
fModeRect[i] = SkRect::MakeXYWH(x, y + 28, 70, 2);
x += 80;
}
}
public:
XferDemo() {
const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorBLACK };
for (int i = 0; i < N; ++i) {
fDrs[i].reset(new CircDrawable(200, colors[i]));
fDrs[i]->fLoc.set(100.f + i * 100, 100.f + i * 100);
fDrs[i]->fMode = SkBlendMode::kSrcOver;
}
fSelected = nullptr;
this->addButtons();
}
protected:
bool onEvent(const SkEvent& evt) override {
if (evt.isType("push-button")) {
if (fSelected) {
fSelected->fMode = (SkBlendMode)evt.getFast32();
this->inval(nullptr);
}
return true;
}
return this->INHERITED::onEvent(evt);
}
bool onQuery(SkEvent* evt) override {
if (SampleCode::TitleQ(*evt)) {
SampleCode::TitleR(evt, "XferDemo");
return true;
}
return this->INHERITED::onQuery(evt);
}
void onDrawContent(SkCanvas* canvas) override {
SkPaint paint;
if (fSelected) {
for (int i = 0; i < N_Modes; ++i) {
if (fSelected->fMode == gModes[i]) {
canvas->drawRect(fModeRect[i], paint);
break;
}
}
}
canvas->saveLayer(nullptr, nullptr);
for (int i = 0; i < N; ++i) {
fDrs[i]->draw(canvas);
}
canvas->restore();
}
SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override {
fSelected = nullptr;
for (int i = N - 1; i >= 0; --i) {
if (fDrs[i]->hitTest(x, y)) {
fSelected = fDrs[i].get();
break;
}
}
this->inval(nullptr);
return fSelected ? new Click(this) : nullptr;
}
bool onClick(Click* click) override {
fSelected->fLoc.fX += click->fCurr.fX - click->fPrev.fX;
fSelected->fLoc.fY += click->fCurr.fY - click->fPrev.fY;
this->inval(nullptr);
return true;
}
private:
typedef SampleView INHERITED;
};
//////////////////////////////////////////////////////////////////////////////
DEF_SAMPLE( return new XferDemo; )
|