2015-09-03 02:05:10 +00:00
|
|
|
/*
|
|
|
|
* 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"
|
2017-12-01 15:52:28 +00:00
|
|
|
#include "SkString.h"
|
2015-09-03 02:05:10 +00:00
|
|
|
#include "SkSurface.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
|
2017-03-15 16:19:37 +00:00
|
|
|
const SkBlendMode gModes[] = {
|
|
|
|
SkBlendMode::kSrcOver,
|
|
|
|
SkBlendMode::kSrc,
|
|
|
|
SkBlendMode::kSrcIn,
|
|
|
|
SkBlendMode::kSrcOut,
|
|
|
|
SkBlendMode::kSrcATop,
|
|
|
|
SkBlendMode::kDstOver,
|
|
|
|
SkBlendMode::kDstIn,
|
|
|
|
SkBlendMode::kDstOut,
|
|
|
|
SkBlendMode::kDstATop,
|
2015-09-03 02:05:10 +00:00
|
|
|
};
|
|
|
|
const int N_Modes = SK_ARRAY_COUNT(gModes);
|
|
|
|
|
|
|
|
static SkRandom gRand;
|
|
|
|
|
2017-11-22 21:36:07 +00:00
|
|
|
struct ModeButton {
|
2015-09-03 02:05:10 +00:00
|
|
|
SkString fLabel;
|
|
|
|
SkColor fColor;
|
2017-11-22 21:36:07 +00:00
|
|
|
SkRect fRect;
|
2015-09-03 02:05:10 +00:00
|
|
|
|
|
|
|
public:
|
2017-11-22 21:36:07 +00:00
|
|
|
void init(const char label[], const SkRect& rect) {
|
|
|
|
fLabel = label;
|
|
|
|
fRect = rect;
|
2015-09-03 02:05:10 +00:00
|
|
|
fColor = (gRand.nextU() & 0x7F7F7F7F) | SkColorSetARGB(0xFF, 0, 0, 0x80);
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2017-11-22 21:36:07 +00:00
|
|
|
void draw(SkCanvas* canvas) {
|
2015-09-03 02:05:10 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setColor(fColor);
|
2017-11-22 21:36:07 +00:00
|
|
|
canvas->drawRoundRect(fRect, 8, 8, paint);
|
2015-09-03 02:05:10 +00:00
|
|
|
|
|
|
|
paint.setColor(0xFFFFFFFF);
|
|
|
|
paint.setTextSize(16);
|
|
|
|
paint.setTextAlign(SkPaint::kCenter_Align);
|
|
|
|
paint.setLCDRenderText(true);
|
2017-11-22 21:36:07 +00:00
|
|
|
canvas->drawString(fLabel, fRect.centerX(), fRect.fTop + 0.68f * fRect.height(), paint);
|
2015-09-03 02:05:10 +00:00
|
|
|
}
|
|
|
|
|
2017-11-22 21:36:07 +00:00
|
|
|
bool hitTest(SkScalar x, SkScalar y) {
|
|
|
|
return fRect.intersects(x - 1, y - 1, x + 1, y + 1);
|
2015-09-03 02:05:10 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ModeDrawable : public SkDrawable {
|
|
|
|
public:
|
2016-10-06 00:33:02 +00:00
|
|
|
ModeDrawable() : fMode(SkBlendMode::kSrcOver), fLoc(SkPoint::Make(0, 0)) {}
|
2015-09-03 02:05:10 +00:00
|
|
|
|
2016-10-06 00:33:02 +00:00
|
|
|
SkBlendMode fMode;
|
|
|
|
SkPoint fLoc;
|
2015-09-03 02:05:10 +00:00
|
|
|
|
|
|
|
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 };
|
2016-03-09 02:50:00 +00:00
|
|
|
fPaint.setShader(SkGradientShader::MakeRadial(SkPoint::Make(size/2, size/2), size/2,
|
2015-09-03 02:05:10 +00:00
|
|
|
colors, nullptr, 2,
|
|
|
|
SkShader::kClamp_TileMode));
|
|
|
|
fBounds = SkRect::MakeWH(size, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkRect onGetBounds() override {
|
|
|
|
return fBounds;
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2016-10-06 00:33:02 +00:00
|
|
|
fPaint.setBlendMode(fMode);
|
2015-09-03 02:05:10 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(fLoc.x(), fLoc.y());
|
|
|
|
canvas->drawOval(fBounds, fPaint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class XferDemo : public SampleView {
|
|
|
|
enum {
|
|
|
|
N = 4
|
|
|
|
};
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-09-03 02:05:10 +00:00
|
|
|
SkRect fModeRect[N_Modes];
|
2017-11-22 21:36:07 +00:00
|
|
|
ModeButton fModeButtons[N_Modes];
|
2016-11-04 17:02:54 +00:00
|
|
|
sk_sp<CircDrawable> fDrs[N];
|
2015-09-03 02:05:10 +00:00
|
|
|
CircDrawable* fSelected;
|
|
|
|
|
|
|
|
void addButtons() {
|
|
|
|
SkScalar x = 10;
|
|
|
|
SkScalar y = 10;
|
|
|
|
for (int i = 0; i < N_Modes; ++i) {
|
2017-11-22 21:36:07 +00:00
|
|
|
fModeButtons[i].init(SkBlendMode_Name(gModes[i]), SkRect::MakeXYWH(x, y, 70, 25));
|
2015-09-03 02:05:10 +00:00
|
|
|
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);
|
2016-10-06 00:33:02 +00:00
|
|
|
fDrs[i]->fMode = SkBlendMode::kSrcOver;
|
2015-09-03 02:05:10 +00:00
|
|
|
}
|
|
|
|
fSelected = nullptr;
|
|
|
|
|
|
|
|
this->addButtons();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
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 {
|
2017-11-22 21:36:07 +00:00
|
|
|
for (int i = 0; i < N_Modes; ++i) {
|
|
|
|
fModeButtons[i].draw(canvas);
|
|
|
|
}
|
|
|
|
|
2015-09-03 02:05:10 +00:00
|
|
|
SkPaint paint;
|
|
|
|
if (fSelected) {
|
|
|
|
for (int i = 0; i < N_Modes; ++i) {
|
2017-03-15 16:19:37 +00:00
|
|
|
if (fSelected->fMode == gModes[i]) {
|
2015-09-03 02:05:10 +00:00
|
|
|
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 {
|
2017-11-22 21:36:07 +00:00
|
|
|
// Check mode buttons first
|
|
|
|
for (int i = 0; i < N_Modes; ++i) {
|
|
|
|
if (fModeButtons[i].hitTest(x, y)) {
|
|
|
|
Click* click = new Click(this);
|
|
|
|
click->fMeta.setS32("mode", i);
|
|
|
|
return click;
|
|
|
|
}
|
|
|
|
}
|
2015-09-03 02:05:10 +00:00
|
|
|
fSelected = nullptr;
|
|
|
|
for (int i = N - 1; i >= 0; --i) {
|
|
|
|
if (fDrs[i]->hitTest(x, y)) {
|
2016-11-04 17:02:54 +00:00
|
|
|
fSelected = fDrs[i].get();
|
2015-09-03 02:05:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fSelected ? new Click(this) : nullptr;
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-09-03 02:05:10 +00:00
|
|
|
bool onClick(Click* click) override {
|
2017-11-22 21:36:07 +00:00
|
|
|
int32_t mode;
|
|
|
|
if (click->fMeta.findS32("mode", &mode)) {
|
|
|
|
if (fSelected && Click::kUp_State == click->fState) {
|
|
|
|
fSelected->fMode = gModes[mode];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fSelected->fLoc.fX += click->fCurr.fX - click->fPrev.fX;
|
|
|
|
fSelected->fLoc.fY += click->fCurr.fY - click->fPrev.fY;
|
|
|
|
}
|
2015-09-03 02:05:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef SampleView INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
DEF_SAMPLE( return new XferDemo; )
|