reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 8 | #include "Sample.h" |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 9 | #include "SkAnimTimer.h" |
| 10 | #include "SkDrawable.h" |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 11 | #include "SkCanvas.h" |
| 12 | #include "SkDrawable.h" |
| 13 | #include "SkPath.h" |
| 14 | #include "SkRandom.h" |
| 15 | #include "SkRSXform.h" |
Brian Osman | 8ceee43 | 2017-12-01 10:52:28 -0500 | [diff] [blame] | 16 | #include "SkString.h" |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 17 | #include "SkSurface.h" |
Mike Reed | a697df9 | 2018-10-26 07:28:30 -0400 | [diff] [blame] | 18 | #include "SkTextUtils.h" |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 19 | #include "SkGradientShader.h" |
| 20 | |
Brian Osman | d1e67e7 | 2017-03-15 12:19:37 -0400 | [diff] [blame] | 21 | const SkBlendMode gModes[] = { |
| 22 | SkBlendMode::kSrcOver, |
| 23 | SkBlendMode::kSrc, |
| 24 | SkBlendMode::kSrcIn, |
| 25 | SkBlendMode::kSrcOut, |
| 26 | SkBlendMode::kSrcATop, |
| 27 | SkBlendMode::kDstOver, |
| 28 | SkBlendMode::kDstIn, |
| 29 | SkBlendMode::kDstOut, |
| 30 | SkBlendMode::kDstATop, |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 31 | }; |
| 32 | const int N_Modes = SK_ARRAY_COUNT(gModes); |
| 33 | |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 34 | static SkRandom gRand; |
| 35 | |
Brian Osman | ede860e | 2017-11-22 16:36:07 -0500 | [diff] [blame] | 36 | struct ModeButton { |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 37 | SkString fLabel; |
| 38 | SkColor fColor; |
Brian Osman | ede860e | 2017-11-22 16:36:07 -0500 | [diff] [blame] | 39 | SkRect fRect; |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 40 | |
| 41 | public: |
Brian Osman | ede860e | 2017-11-22 16:36:07 -0500 | [diff] [blame] | 42 | void init(const char label[], const SkRect& rect) { |
| 43 | fLabel = label; |
| 44 | fRect = rect; |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 45 | fColor = (gRand.nextU() & 0x7F7F7F7F) | SkColorSetARGB(0xFF, 0, 0, 0x80); |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 46 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 47 | |
Brian Osman | ede860e | 2017-11-22 16:36:07 -0500 | [diff] [blame] | 48 | void draw(SkCanvas* canvas) { |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 49 | SkPaint paint; |
| 50 | paint.setAntiAlias(true); |
| 51 | paint.setColor(fColor); |
Brian Osman | ede860e | 2017-11-22 16:36:07 -0500 | [diff] [blame] | 52 | canvas->drawRoundRect(fRect, 8, 8, paint); |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 53 | |
| 54 | paint.setColor(0xFFFFFFFF); |
Mike Reed | b579f07 | 2019-01-03 15:45:53 -0500 | [diff] [blame^] | 55 | SkFont font; |
| 56 | font.setSize(16); |
| 57 | font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
| 58 | SkTextUtils::DrawString(canvas, fLabel.c_str(), fRect.centerX(), fRect.fTop + 0.68f * fRect.height(), |
| 59 | font, paint, SkTextUtils::kCenter_Align); |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Brian Osman | ede860e | 2017-11-22 16:36:07 -0500 | [diff] [blame] | 62 | bool hitTest(SkScalar x, SkScalar y) { |
| 63 | return fRect.intersects(x - 1, y - 1, x + 1, y + 1); |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 64 | } |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 67 | class ModeDrawable : public SkDrawable { |
| 68 | public: |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 69 | ModeDrawable() : fMode(SkBlendMode::kSrcOver), fLoc(SkPoint::Make(0, 0)) {} |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 70 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 71 | SkBlendMode fMode; |
| 72 | SkPoint fLoc; |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 73 | |
| 74 | bool hitTest(SkScalar x, SkScalar y) { |
| 75 | SkRect target = SkRect::MakeXYWH(x - fLoc.x() - 1, y - fLoc.y() - 1, 3, 3); |
| 76 | return this->getBounds().intersects(target); |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | class CircDrawable : public ModeDrawable { |
| 81 | SkPaint fPaint; |
| 82 | SkRect fBounds; |
| 83 | |
| 84 | public: |
| 85 | CircDrawable(SkScalar size, SkColor c) { |
| 86 | const SkColor colors[] = { 0, c }; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 87 | fPaint.setShader(SkGradientShader::MakeRadial(SkPoint::Make(size/2, size/2), size/2, |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 88 | colors, nullptr, 2, |
| 89 | SkShader::kClamp_TileMode)); |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 90 | fBounds = SkRect::MakeWH(size, size); |
| 91 | } |
| 92 | |
| 93 | protected: |
| 94 | SkRect onGetBounds() override { |
| 95 | return fBounds; |
| 96 | } |
| 97 | |
| 98 | void onDraw(SkCanvas* canvas) override { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 99 | fPaint.setBlendMode(fMode); |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 100 | canvas->save(); |
| 101 | canvas->translate(fLoc.x(), fLoc.y()); |
| 102 | canvas->drawOval(fBounds, fPaint); |
| 103 | canvas->restore(); |
| 104 | } |
| 105 | }; |
| 106 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 107 | class XferDemo : public Sample { |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 108 | enum { |
| 109 | N = 4 |
| 110 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 111 | |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 112 | SkRect fModeRect[N_Modes]; |
Brian Osman | ede860e | 2017-11-22 16:36:07 -0500 | [diff] [blame] | 113 | ModeButton fModeButtons[N_Modes]; |
Hal Canary | 2db8361 | 2016-11-04 13:02:54 -0400 | [diff] [blame] | 114 | sk_sp<CircDrawable> fDrs[N]; |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 115 | CircDrawable* fSelected; |
| 116 | |
| 117 | void addButtons() { |
| 118 | SkScalar x = 10; |
| 119 | SkScalar y = 10; |
| 120 | for (int i = 0; i < N_Modes; ++i) { |
Brian Osman | ede860e | 2017-11-22 16:36:07 -0500 | [diff] [blame] | 121 | fModeButtons[i].init(SkBlendMode_Name(gModes[i]), SkRect::MakeXYWH(x, y, 70, 25)); |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 122 | fModeRect[i] = SkRect::MakeXYWH(x, y + 28, 70, 2); |
| 123 | x += 80; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | public: |
| 128 | XferDemo() { |
| 129 | const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorBLACK }; |
| 130 | for (int i = 0; i < N; ++i) { |
| 131 | fDrs[i].reset(new CircDrawable(200, colors[i])); |
| 132 | fDrs[i]->fLoc.set(100.f + i * 100, 100.f + i * 100); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 133 | fDrs[i]->fMode = SkBlendMode::kSrcOver; |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 134 | } |
| 135 | fSelected = nullptr; |
| 136 | |
| 137 | this->addButtons(); |
| 138 | } |
| 139 | |
| 140 | protected: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 141 | bool onQuery(Sample::Event* evt) override { |
| 142 | if (Sample::TitleQ(*evt)) { |
| 143 | Sample::TitleR(evt, "XferDemo"); |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 144 | return true; |
| 145 | } |
| 146 | return this->INHERITED::onQuery(evt); |
| 147 | } |
| 148 | |
| 149 | void onDrawContent(SkCanvas* canvas) override { |
Brian Osman | ede860e | 2017-11-22 16:36:07 -0500 | [diff] [blame] | 150 | for (int i = 0; i < N_Modes; ++i) { |
| 151 | fModeButtons[i].draw(canvas); |
| 152 | } |
| 153 | |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 154 | SkPaint paint; |
| 155 | if (fSelected) { |
| 156 | for (int i = 0; i < N_Modes; ++i) { |
Brian Osman | d1e67e7 | 2017-03-15 12:19:37 -0400 | [diff] [blame] | 157 | if (fSelected->fMode == gModes[i]) { |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 158 | canvas->drawRect(fModeRect[i], paint); |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | canvas->saveLayer(nullptr, nullptr); |
| 165 | for (int i = 0; i < N; ++i) { |
| 166 | fDrs[i]->draw(canvas); |
| 167 | } |
| 168 | canvas->restore(); |
| 169 | } |
| 170 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 171 | Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override { |
Brian Osman | ede860e | 2017-11-22 16:36:07 -0500 | [diff] [blame] | 172 | // Check mode buttons first |
| 173 | for (int i = 0; i < N_Modes; ++i) { |
| 174 | if (fModeButtons[i].hitTest(x, y)) { |
| 175 | Click* click = new Click(this); |
| 176 | click->fMeta.setS32("mode", i); |
| 177 | return click; |
| 178 | } |
| 179 | } |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 180 | fSelected = nullptr; |
| 181 | for (int i = N - 1; i >= 0; --i) { |
| 182 | if (fDrs[i]->hitTest(x, y)) { |
Hal Canary | 2db8361 | 2016-11-04 13:02:54 -0400 | [diff] [blame] | 183 | fSelected = fDrs[i].get(); |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 184 | break; |
| 185 | } |
| 186 | } |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 187 | return fSelected ? new Click(this) : nullptr; |
| 188 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 189 | |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 190 | bool onClick(Click* click) override { |
Brian Osman | ede860e | 2017-11-22 16:36:07 -0500 | [diff] [blame] | 191 | int32_t mode; |
| 192 | if (click->fMeta.findS32("mode", &mode)) { |
| 193 | if (fSelected && Click::kUp_State == click->fState) { |
| 194 | fSelected->fMode = gModes[mode]; |
| 195 | } |
| 196 | } else { |
| 197 | fSelected->fLoc.fX += click->fCurr.fX - click->fPrev.fX; |
| 198 | fSelected->fLoc.fY += click->fCurr.fY - click->fPrev.fY; |
| 199 | } |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 200 | return true; |
| 201 | } |
| 202 | |
| 203 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 204 | typedef Sample INHERITED; |
reed | 1a9caff | 2015-09-02 19:05:10 -0700 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | ////////////////////////////////////////////////////////////////////////////// |
| 208 | |
| 209 | DEF_SAMPLE( return new XferDemo; ) |