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