| OLD | NEW | 
|---|
| 1 |  | 
| 2 /* | 1 /* | 
| 3  * Copyright 2011 Google Inc. | 2  * Copyright 2011 Google Inc. | 
| 4  * | 3  * | 
| 5  * Use of this source code is governed by a BSD-style license that can be | 4  * Use of this source code is governed by a BSD-style license that can be | 
| 6  * found in the LICENSE file. | 5  * found in the LICENSE file. | 
| 7  */ | 6  */ | 
| 8 #include "SampleCode.h" | 7 #include "SampleCode.h" | 
| 9 #include "SkView.h" | 8 #include "SkView.h" | 
| 10 #include "SkCanvas.h" | 9 #include "SkCanvas.h" | 
| 11 #include "SkGradientShader.h" | 10 #include "SkGradientShader.h" | 
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 73 DEF_SAMPLE( return new PathClipView; ) | 72 DEF_SAMPLE( return new PathClipView; ) | 
| 74 | 73 | 
| 75 ////////////////////////////////////////////////////////////////////////////// | 74 ////////////////////////////////////////////////////////////////////////////// | 
| 76 | 75 | 
| 77 static int clip_line(const SkRect& bounds, SkPoint p0, SkPoint p1, SkPoint edges
     []) { | 76 static int clip_line(const SkRect& bounds, SkPoint p0, SkPoint p1, SkPoint edges
     []) { | 
| 78     SkPoint* edgesStart = edges; | 77     SkPoint* edgesStart = edges; | 
| 79 | 78 | 
| 80     if (p0.fY == p1.fY) { | 79     if (p0.fY == p1.fY) { | 
| 81         return 0; | 80         return 0; | 
| 82     } | 81     } | 
| 83 | 82 | 
| 84     if (p0.fY > p1.fY) { | 83     if (p0.fY > p1.fY) { | 
| 85         SkTSwap(p0, p1); | 84         SkTSwap(p0, p1); | 
| 86     } | 85     } | 
| 87     // now we're monotonic in Y: p0 <= p1 | 86     // now we're monotonic in Y: p0 <= p1 | 
| 88     if (p1.fY <= bounds.top() || p0.fY >= bounds.bottom()) { | 87     if (p1.fY <= bounds.top() || p0.fY >= bounds.bottom()) { | 
| 89         return 0; | 88         return 0; | 
| 90     } | 89     } | 
| 91 | 90 | 
| 92     double dxdy = (double)(p1.fX - p0.fX) / (p1.fY - p0.fY); | 91     double dxdy = (double)(p1.fX - p0.fX) / (p1.fY - p0.fY); | 
| 93     if (p0.fY < bounds.top()) { | 92     if (p0.fY < bounds.top()) { | 
| 94         p0.fX = SkDoubleToScalar(p0.fX + dxdy * (bounds.top() - p0.fY)); | 93         p0.fX = SkDoubleToScalar(p0.fX + dxdy * (bounds.top() - p0.fY)); | 
| 95         p0.fY = bounds.top(); | 94         p0.fY = bounds.top(); | 
| 96     } | 95     } | 
| 97     if (p1.fY > bounds.bottom()) { | 96     if (p1.fY > bounds.bottom()) { | 
| 98         p1.fX = SkDoubleToScalar(p1.fX + dxdy * (bounds.bottom() - p1.fY)); | 97         p1.fX = SkDoubleToScalar(p1.fX + dxdy * (bounds.bottom() - p1.fY)); | 
| 99         p1.fY = bounds.bottom(); | 98         p1.fY = bounds.bottom(); | 
| 100     } | 99     } | 
| 101 | 100 | 
| 102     // Now p0...p1 is strictly inside bounds vertically, so we just need to clip
      horizontally | 101     // Now p0...p1 is strictly inside bounds vertically, so we just need to clip
      horizontally | 
| 103 | 102 | 
| 104     if (p0.fX > p1.fX) { | 103     if (p0.fX > p1.fX) { | 
| 105         SkTSwap(p0, p1); | 104         SkTSwap(p0, p1); | 
| 106     } | 105     } | 
| 107     // now we're left-to-right: p0 .. p1 | 106     // now we're left-to-right: p0 .. p1 | 
| 108 | 107 | 
| 109     if (p1.fX <= bounds.left()) {   // entirely to the left | 108     if (p1.fX <= bounds.left()) {   // entirely to the left | 
| 110         p0.fX = p1.fX = bounds.left(); | 109         p0.fX = p1.fX = bounds.left(); | 
| 111         *edges++ = p0; | 110         *edges++ = p0; | 
| 112         *edges++ = p1; | 111         *edges++ = p1; | 
| 113         return 2; | 112         return 2; | 
| 114     } | 113     } | 
| 115     if (p0.fX >= bounds.right()) {  // entirely to the right | 114     if (p0.fX >= bounds.right()) {  // entirely to the right | 
| 116         p0.fX = p1.fX = bounds.right(); | 115         p0.fX = p1.fX = bounds.right(); | 
| 117         *edges++ = p0; | 116         *edges++ = p0; | 
| 118         *edges++ = p1; | 117         *edges++ = p1; | 
| 119         return 2; | 118         return 2; | 
| 120     } | 119     } | 
| 121 | 120 | 
| 122     if (p0.fX < bounds.left()) { | 121     if (p0.fX < bounds.left()) { | 
| 123         float y = SkDoubleToScalar(p0.fY + (bounds.left() - p0.fX) / dxdy); | 122         float y = SkDoubleToScalar(p0.fY + (bounds.left() - p0.fX) / dxdy); | 
| 124         *edges++ = SkPoint::Make(bounds.left(), p0.fY); | 123         *edges++ = SkPoint::Make(bounds.left(), p0.fY); | 
| 125         *edges++ = SkPoint::Make(bounds.left(), y); | 124         *edges++ = SkPoint::Make(bounds.left(), y); | 
| 126         p0.set(bounds.left(), y); | 125         p0.set(bounds.left(), y); | 
| 127     } | 126     } | 
| 128     if (p1.fX > bounds.right()) { | 127     if (p1.fX > bounds.right()) { | 
| 129         float y = SkDoubleToScalar(p0.fY + (bounds.right() - p0.fX) / dxdy); | 128         float y = SkDoubleToScalar(p0.fY + (bounds.right() - p0.fX) / dxdy); | 
| 130         *edges++ = p0; | 129         *edges++ = p0; | 
| 131         *edges++ = SkPoint::Make(bounds.right(), y); | 130         *edges++ = SkPoint::Make(bounds.right(), y); | 
| (...skipping 18 matching lines...) Expand all  Loading... | 
| 150 // Demonstrate edge-clipping that is used in the scan converter | 149 // Demonstrate edge-clipping that is used in the scan converter | 
| 151 // | 150 // | 
| 152 class EdgeClipView : public SampleView { | 151 class EdgeClipView : public SampleView { | 
| 153     enum { | 152     enum { | 
| 154         N = 3 | 153         N = 3 | 
| 155     }; | 154     }; | 
| 156 public: | 155 public: | 
| 157     SkPoint fPoly[N]; | 156     SkPoint fPoly[N]; | 
| 158     SkRect  fClip; | 157     SkRect  fClip; | 
| 159     SkColor fEdgeColor[N]; | 158     SkColor fEdgeColor[N]; | 
| 160 | 159 | 
| 161     EdgeClipView() : fClip(SkRect::MakeLTRB(150, 150, 550, 450)) { | 160     EdgeClipView() : fClip(SkRect::MakeLTRB(150, 150, 550, 450)) { | 
| 162         fPoly[0].set(300, 40); | 161         fPoly[0].set(300, 40); | 
| 163         fPoly[1].set(550, 250); | 162         fPoly[1].set(550, 250); | 
| 164         fPoly[2].set(40, 450); | 163         fPoly[2].set(40, 450); | 
| 165 | 164 | 
| 166         fEdgeColor[0] = 0xFFFF0000; | 165         fEdgeColor[0] = 0xFFFF0000; | 
| 167         fEdgeColor[1] = 0xFF00FF00; | 166         fEdgeColor[1] = 0xFF00FF00; | 
| 168         fEdgeColor[2] = 0xFF0000FF; | 167         fEdgeColor[2] = 0xFF0000FF; | 
| 169     } | 168     } | 
| 170 | 169 | 
| 171 protected: | 170 protected: | 
| 172     bool onQuery(SkEvent* evt) override { | 171     bool onQuery(SkEvent* evt) override { | 
| 173         if (SampleCode::TitleQ(*evt)) { | 172         if (SampleCode::TitleQ(*evt)) { | 
| 174             SampleCode::TitleR(evt, "EdgeClip"); | 173             SampleCode::TitleR(evt, "EdgeClip"); | 
| 175             return true; | 174             return true; | 
| 176         } | 175         } | 
| 177         return this->INHERITED::onQuery(evt); | 176         return this->INHERITED::onQuery(evt); | 
| 178     } | 177     } | 
| 179 | 178 | 
| 180     static SkScalar snap(SkScalar x) { | 179     static SkScalar snap(SkScalar x) { | 
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 233             draw_clipped_line(canvas, fClip, fPoly[i], fPoly[j], p); | 232             draw_clipped_line(canvas, fClip, fPoly[i], fPoly[j], p); | 
| 234         } | 233         } | 
| 235         canvas->restore(); | 234         canvas->restore(); | 
| 236     } | 235     } | 
| 237 | 236 | 
| 238     class MyClick : public Click { | 237     class MyClick : public Click { | 
| 239     public: | 238     public: | 
| 240         MyClick(SkView* view) : Click(view) {} | 239         MyClick(SkView* view) : Click(view) {} | 
| 241         virtual void handleMove() = 0; | 240         virtual void handleMove() = 0; | 
| 242     }; | 241     }; | 
| 243 | 242 | 
| 244     class VertClick : public MyClick { | 243     class VertClick : public MyClick { | 
| 245         SkPoint* fPt; | 244         SkPoint* fPt; | 
| 246     public: | 245     public: | 
| 247         VertClick(SkView* view, SkPoint* pt) : MyClick(view), fPt(pt) {} | 246         VertClick(SkView* view, SkPoint* pt) : MyClick(view), fPt(pt) {} | 
| 248         void handleMove() override { *fPt = snap(fCurr); } | 247         void handleMove() override { *fPt = snap(fCurr); } | 
| 249     }; | 248     }; | 
| 250 | 249 | 
| 251     class DragRectClick : public MyClick { | 250     class DragRectClick : public MyClick { | 
| 252         SkRect* fRect; | 251         SkRect* fRect; | 
| 253     public: | 252     public: | 
| 254         DragRectClick(SkView* view, SkRect* rect) : MyClick(view), fRect(rect) {
     } | 253         DragRectClick(SkView* view, SkRect* rect) : MyClick(view), fRect(rect) {
     } | 
| 255         void handleMove() override { fRect->offset(fCurr.x() - fPrev.x(), fCurr.
     y() - fPrev.y()); } | 254         void handleMove() override { fRect->offset(fCurr.x() - fPrev.x(), fCurr.
     y() - fPrev.y()); } | 
| 256     }; | 255     }; | 
| 257 | 256 | 
| 258     class DragPolyClick : public MyClick { | 257     class DragPolyClick : public MyClick { | 
| 259         SkPoint fSrc[100]; | 258         SkPoint fSrc[100]; | 
| 260         SkPoint* fPoly; | 259         SkPoint* fPoly; | 
| 261         int fCount; | 260         int fCount; | 
| 262     public: | 261     public: | 
| 263         DragPolyClick(SkView* view, SkPoint poly[], int count) | 262         DragPolyClick(SkView* view, SkPoint poly[], int count) | 
| 264             : MyClick(view), fPoly(poly), fCount(count) | 263             : MyClick(view), fPoly(poly), fCount(count) | 
| 265         { | 264         { | 
| 266             SkASSERT((size_t)count <= SK_ARRAY_COUNT(fSrc)); | 265             SkASSERT((size_t)count <= SK_ARRAY_COUNT(fSrc)); | 
| 267             memcpy(fSrc, poly, count * sizeof(SkPoint)); | 266             memcpy(fSrc, poly, count * sizeof(SkPoint)); | 
| (...skipping 19 matching lines...) Expand all  Loading... | 
| 287         const SkScalar dy = pt.y() - y; | 286         const SkScalar dy = pt.y() - y; | 
| 288         return dx*dx + dy*dy <= rad*rad; | 287         return dx*dx + dy*dy <= rad*rad; | 
| 289     } | 288     } | 
| 290 | 289 | 
| 291     SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override
      { | 290     SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override
      { | 
| 292         for (int i = 0; i < N; ++i) { | 291         for (int i = 0; i < N; ++i) { | 
| 293             if (hit_test(fPoly[i], x, y)) { | 292             if (hit_test(fPoly[i], x, y)) { | 
| 294                 return new VertClick(this, &fPoly[i]); | 293                 return new VertClick(this, &fPoly[i]); | 
| 295             } | 294             } | 
| 296         } | 295         } | 
| 297 | 296 | 
| 298         SkPath path; | 297         SkPath path; | 
| 299         path.addPoly(fPoly, N, true); | 298         path.addPoly(fPoly, N, true); | 
| 300         if (path.contains(x, y)) { | 299         if (path.contains(x, y)) { | 
| 301             return new DragPolyClick(this, fPoly, N); | 300             return new DragPolyClick(this, fPoly, N); | 
| 302         } | 301         } | 
| 303 | 302 | 
| 304         if (fClip.intersects(SkRect::MakeLTRB(x - 1, y - 1, x + 1, y + 1))) { | 303         if (fClip.intersects(SkRect::MakeLTRB(x - 1, y - 1, x + 1, y + 1))) { | 
| 305             return new DragRectClick(this, &fClip); | 304             return new DragRectClick(this, &fClip); | 
| 306         } | 305         } | 
| 307         return new DoNothingClick(this); | 306         return new DoNothingClick(this); | 
| 308     } | 307     } | 
| 309 | 308 | 
| 310     bool onClick(Click* click) override { | 309     bool onClick(Click* click) override { | 
| 311         ((MyClick*)click)->handleMove(); | 310         ((MyClick*)click)->handleMove(); | 
| 312         this->inval(nullptr); | 311         this->inval(nullptr); | 
| 313         return false; | 312         return false; | 
| 314     } | 313     } | 
| 315 | 314 | 
| 316 private: | 315 private: | 
| 317     typedef SampleView INHERITED; | 316     typedef SampleView INHERITED; | 
| 318 }; | 317 }; | 
| 319 DEF_SAMPLE( return new EdgeClipView; ) | 318 DEF_SAMPLE( return new EdgeClipView; ) | 
| 320 |  | 
| OLD | NEW | 
|---|