epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 7 | #include "SampleCode.h" |
| 8 | #include "SkView.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkGradientShader.h" |
| 11 | #include "SkGraphics.h" |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 12 | #include "SkPath.h" |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 13 | #include "SkRegion.h" |
| 14 | #include "SkShader.h" |
| 15 | #include "SkUtils.h" |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 16 | #include "SkColorPriv.h" |
| 17 | #include "SkColorFilter.h" |
| 18 | #include "SkTime.h" |
| 19 | #include "SkTypeface.h" |
| 20 | |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 21 | class PathClipView : public SampleView { |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 22 | public: |
| 23 | SkRect fOval; |
| 24 | SkPoint fCenter; |
| 25 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 26 | PathClipView() : fOval(SkRect::MakeWH(200, 50)), fCenter(SkPoint::Make(250, 250)) {} |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 27 | |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 28 | protected: |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 29 | bool onQuery(SkEvent* evt) override { |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 30 | if (SampleCode::TitleQ(*evt)) { |
| 31 | SampleCode::TitleR(evt, "PathClip"); |
| 32 | return true; |
| 33 | } |
| 34 | return this->INHERITED::onQuery(evt); |
| 35 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 36 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 37 | void onDrawContent(SkCanvas* canvas) override { |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 38 | const SkRect oval = fOval.makeOffset(fCenter.fX - fOval.centerX(), |
| 39 | fCenter.fY - fOval.centerY()); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 40 | |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 41 | SkPaint p; |
| 42 | p.setAntiAlias(true); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 43 | |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 44 | p.setStyle(SkPaint::kStroke_Style); |
| 45 | canvas->drawOval(oval, p); |
| 46 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 47 | const SkRect r = SkRect::MakeLTRB(200, 200, 300, 300); |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 48 | canvas->clipRect(r); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 49 | |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 50 | p.setStyle(SkPaint::kFill_Style); |
| 51 | p.setColor(SK_ColorRED); |
| 52 | canvas->drawRect(r, p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 53 | |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 54 | p.setColor(0x800000FF); |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 55 | canvas->drawOval(oval, p); |
| 56 | } |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 57 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 58 | SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override { |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 59 | return new Click(this); |
| 60 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 61 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 62 | bool onClick(Click* click) override { |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 63 | fCenter.set(click->fCurr.fX, click->fCurr.fY); |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 64 | return false; |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 65 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 66 | |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 67 | private: |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 68 | typedef SampleView INHERITED; |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 69 | }; |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 70 | DEF_SAMPLE( return new PathClipView; ) |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 71 | |
| 72 | ////////////////////////////////////////////////////////////////////////////// |
| 73 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 74 | static int clip_line(const SkRect& bounds, SkPoint p0, SkPoint p1, SkPoint edges[]) { |
| 75 | SkPoint* edgesStart = edges; |
| 76 | |
| 77 | if (p0.fY == p1.fY) { |
| 78 | return 0; |
| 79 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 80 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 81 | if (p0.fY > p1.fY) { |
| 82 | SkTSwap(p0, p1); |
| 83 | } |
| 84 | // now we're monotonic in Y: p0 <= p1 |
| 85 | if (p1.fY <= bounds.top() || p0.fY >= bounds.bottom()) { |
| 86 | return 0; |
| 87 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 88 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 89 | double dxdy = (double)(p1.fX - p0.fX) / (p1.fY - p0.fY); |
| 90 | if (p0.fY < bounds.top()) { |
| 91 | p0.fX = SkDoubleToScalar(p0.fX + dxdy * (bounds.top() - p0.fY)); |
| 92 | p0.fY = bounds.top(); |
| 93 | } |
| 94 | if (p1.fY > bounds.bottom()) { |
| 95 | p1.fX = SkDoubleToScalar(p1.fX + dxdy * (bounds.bottom() - p1.fY)); |
| 96 | p1.fY = bounds.bottom(); |
| 97 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 98 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 99 | // Now p0...p1 is strictly inside bounds vertically, so we just need to clip horizontally |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 100 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 101 | if (p0.fX > p1.fX) { |
| 102 | SkTSwap(p0, p1); |
| 103 | } |
| 104 | // now we're left-to-right: p0 .. p1 |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 105 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 106 | if (p1.fX <= bounds.left()) { // entirely to the left |
| 107 | p0.fX = p1.fX = bounds.left(); |
| 108 | *edges++ = p0; |
| 109 | *edges++ = p1; |
| 110 | return 2; |
| 111 | } |
| 112 | if (p0.fX >= bounds.right()) { // entirely to the right |
| 113 | p0.fX = p1.fX = bounds.right(); |
| 114 | *edges++ = p0; |
| 115 | *edges++ = p1; |
| 116 | return 2; |
| 117 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 118 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 119 | if (p0.fX < bounds.left()) { |
| 120 | float y = SkDoubleToScalar(p0.fY + (bounds.left() - p0.fX) / dxdy); |
| 121 | *edges++ = SkPoint::Make(bounds.left(), p0.fY); |
| 122 | *edges++ = SkPoint::Make(bounds.left(), y); |
| 123 | p0.set(bounds.left(), y); |
| 124 | } |
| 125 | if (p1.fX > bounds.right()) { |
| 126 | float y = SkDoubleToScalar(p0.fY + (bounds.right() - p0.fX) / dxdy); |
| 127 | *edges++ = p0; |
| 128 | *edges++ = SkPoint::Make(bounds.right(), y); |
| 129 | *edges++ = SkPoint::Make(bounds.right(), p1.fY); |
| 130 | } else { |
| 131 | *edges++ = p0; |
| 132 | *edges++ = p1; |
| 133 | } |
| 134 | return SkToInt(edges - edgesStart); |
| 135 | } |
| 136 | |
| 137 | static void draw_clipped_line(SkCanvas* canvas, const SkRect& bounds, |
| 138 | SkPoint p0, SkPoint p1, const SkPaint& paint) { |
| 139 | SkPoint verts[6]; |
| 140 | int count = clip_line(bounds, p0, p1, verts); |
| 141 | |
| 142 | SkPath path; |
| 143 | path.addPoly(verts, count, false); |
| 144 | canvas->drawPath(path, paint); |
| 145 | } |
| 146 | |
| 147 | // Demonstrate edge-clipping that is used in the scan converter |
| 148 | // |
| 149 | class EdgeClipView : public SampleView { |
| 150 | enum { |
| 151 | N = 3 |
| 152 | }; |
| 153 | public: |
| 154 | SkPoint fPoly[N]; |
| 155 | SkRect fClip; |
| 156 | SkColor fEdgeColor[N]; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 157 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 158 | EdgeClipView() : fClip(SkRect::MakeLTRB(150, 150, 550, 450)) { |
| 159 | fPoly[0].set(300, 40); |
| 160 | fPoly[1].set(550, 250); |
| 161 | fPoly[2].set(40, 450); |
| 162 | |
| 163 | fEdgeColor[0] = 0xFFFF0000; |
| 164 | fEdgeColor[1] = 0xFF00FF00; |
| 165 | fEdgeColor[2] = 0xFF0000FF; |
| 166 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 167 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 168 | protected: |
| 169 | bool onQuery(SkEvent* evt) override { |
| 170 | if (SampleCode::TitleQ(*evt)) { |
| 171 | SampleCode::TitleR(evt, "EdgeClip"); |
| 172 | return true; |
| 173 | } |
| 174 | return this->INHERITED::onQuery(evt); |
| 175 | } |
| 176 | |
| 177 | static SkScalar snap(SkScalar x) { |
| 178 | return SkScalarRoundToScalar(x * 0.5f) * 2; |
| 179 | } |
| 180 | static SkPoint snap(const SkPoint& pt) { |
| 181 | return SkPoint::Make(snap(pt.x()), snap(pt.y())); |
| 182 | } |
| 183 | static void snap(SkPoint dst[], const SkPoint src[], int count) { |
| 184 | for (int i = 0; i < count; ++i) { |
| 185 | dst[i] = snap(src[i]); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | void onDrawContent(SkCanvas* canvas) override { |
| 190 | SkPath path; |
| 191 | path.addPoly(fPoly, N, true); |
| 192 | |
| 193 | // Draw the full triangle, stroked and filled |
| 194 | SkPaint p; |
| 195 | p.setAntiAlias(true); |
| 196 | p.setColor(0xFFE0E0E0); |
| 197 | canvas->drawPath(path, p); |
| 198 | p.setStyle(SkPaint::kStroke_Style); |
| 199 | p.setStrokeWidth(2); |
| 200 | for (int i = 0; i < N; ++i) { |
| 201 | const int j = (i + 1) % N; |
| 202 | p.setColor(fEdgeColor[i]); |
| 203 | p.setAlpha(0x88); |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 204 | canvas->drawLine(fPoly[i], fPoly[j], p); |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 205 | } |
| 206 | p.setStyle(SkPaint::kFill_Style); |
| 207 | |
| 208 | // Draw the clip itself |
| 209 | p.setColor(0xFF8888CC); |
| 210 | canvas->drawRect(fClip, p); |
| 211 | |
| 212 | // Draw the filled triangle through the clip |
| 213 | p.setColor(0xFF88CC88); |
| 214 | canvas->save(); |
| 215 | canvas->clipRect(fClip); |
| 216 | canvas->drawPath(path, p); |
| 217 | canvas->restore(); |
| 218 | |
| 219 | p.setStyle(SkPaint::kStroke_Style); |
| 220 | p.setStrokeWidth(6); |
| 221 | |
| 222 | // Draw each of the "Edges" that survived the clipping |
| 223 | // We use a layer, so we can PLUS the different edge-colors, showing where two edges |
| 224 | // canceled each other out. |
| 225 | canvas->saveLayer(nullptr, nullptr); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 226 | p.setBlendMode(SkBlendMode::kPlus); |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 227 | for (int i = 0; i < N; ++i) { |
| 228 | const int j = (i + 1) % N; |
| 229 | p.setColor(fEdgeColor[i]); |
| 230 | draw_clipped_line(canvas, fClip, fPoly[i], fPoly[j], p); |
| 231 | } |
| 232 | canvas->restore(); |
| 233 | } |
| 234 | |
| 235 | class MyClick : public Click { |
| 236 | public: |
| 237 | MyClick(SkView* view) : Click(view) {} |
| 238 | virtual void handleMove() = 0; |
| 239 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 240 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 241 | class VertClick : public MyClick { |
| 242 | SkPoint* fPt; |
| 243 | public: |
| 244 | VertClick(SkView* view, SkPoint* pt) : MyClick(view), fPt(pt) {} |
| 245 | void handleMove() override { *fPt = snap(fCurr); } |
| 246 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 247 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 248 | class DragRectClick : public MyClick { |
| 249 | SkRect* fRect; |
| 250 | public: |
| 251 | DragRectClick(SkView* view, SkRect* rect) : MyClick(view), fRect(rect) {} |
| 252 | void handleMove() override { fRect->offset(fCurr.x() - fPrev.x(), fCurr.y() - fPrev.y()); } |
| 253 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 254 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 255 | class DragPolyClick : public MyClick { |
| 256 | SkPoint fSrc[100]; |
| 257 | SkPoint* fPoly; |
| 258 | int fCount; |
| 259 | public: |
| 260 | DragPolyClick(SkView* view, SkPoint poly[], int count) |
| 261 | : MyClick(view), fPoly(poly), fCount(count) |
| 262 | { |
| 263 | SkASSERT((size_t)count <= SK_ARRAY_COUNT(fSrc)); |
| 264 | memcpy(fSrc, poly, count * sizeof(SkPoint)); |
| 265 | } |
| 266 | void handleMove() override { |
| 267 | const SkScalar dx = fCurr.x() - fOrig.x(); |
| 268 | const SkScalar dy = fCurr.y() - fOrig.y(); |
| 269 | for (int i = 0; i < fCount; ++i) { |
| 270 | fPoly[i].set(snap(fSrc[i].x() + dx), snap(fSrc[i].y() + dy)); |
| 271 | } |
| 272 | } |
| 273 | }; |
| 274 | |
| 275 | class DoNothingClick : public MyClick { |
| 276 | public: |
| 277 | DoNothingClick(SkView* view) : MyClick(view) {} |
| 278 | void handleMove() override {} |
| 279 | }; |
| 280 | |
| 281 | static bool hit_test(const SkPoint& pt, SkScalar x, SkScalar y) { |
| 282 | const SkScalar rad = 8; |
| 283 | const SkScalar dx = pt.x() - x; |
| 284 | const SkScalar dy = pt.y() - y; |
| 285 | return dx*dx + dy*dy <= rad*rad; |
| 286 | } |
| 287 | |
| 288 | SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override { |
| 289 | for (int i = 0; i < N; ++i) { |
| 290 | if (hit_test(fPoly[i], x, y)) { |
| 291 | return new VertClick(this, &fPoly[i]); |
| 292 | } |
| 293 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 294 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 295 | SkPath path; |
| 296 | path.addPoly(fPoly, N, true); |
| 297 | if (path.contains(x, y)) { |
| 298 | return new DragPolyClick(this, fPoly, N); |
| 299 | } |
| 300 | |
| 301 | if (fClip.intersects(SkRect::MakeLTRB(x - 1, y - 1, x + 1, y + 1))) { |
| 302 | return new DragRectClick(this, &fClip); |
| 303 | } |
| 304 | return new DoNothingClick(this); |
| 305 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 306 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 307 | bool onClick(Click* click) override { |
| 308 | ((MyClick*)click)->handleMove(); |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 309 | return false; |
| 310 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 311 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 312 | private: |
| 313 | typedef SampleView INHERITED; |
| 314 | }; |
| 315 | DEF_SAMPLE( return new EdgeClipView; ) |