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