blob: 521c5a04e117af4512d5ed0b48d951adb7c74f89 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
Hal Canaryc640d0d2018-06-13 09:59:02 -04007
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04008#include "Sample.h"
reed@android.comc07d23a2009-02-06 13:30:58 +00009#include "SkCanvas.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040010#include "SkColorFilter.h"
11#include "SkColorPriv.h"
reed@android.comc07d23a2009-02-06 13:30:58 +000012#include "SkGradientShader.h"
13#include "SkGraphics.h"
reed@android.comc07d23a2009-02-06 13:30:58 +000014#include "SkPath.h"
reed@android.comc07d23a2009-02-06 13:30:58 +000015#include "SkRegion.h"
16#include "SkShader.h"
Hal Canaryfdcfb8b2018-06-13 09:42:32 -040017#include "SkTime.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040018#include "SkTo.h"
Hal Canaryfdcfb8b2018-06-13 09:42:32 -040019#include "SkTypeface.h"
Hal Canaryea60b952018-08-21 11:45:46 -040020#include "SkUTF.h"
reed@android.comc07d23a2009-02-06 13:30:58 +000021
Ben Wagnerf08d1d02018-06-18 15:11:00 -040022#include <utility>
23
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040024class PathClipView : public Sample {
reed@android.comc07d23a2009-02-06 13:30:58 +000025public:
26 SkRect fOval;
27 SkPoint fCenter;
28
reed67c65132015-09-28 06:16:07 -070029 PathClipView() : fOval(SkRect::MakeWH(200, 50)), fCenter(SkPoint::Make(250, 250)) {}
rmistry@google.comae933ce2012-08-23 18:19:56 +000030
reed@android.comc07d23a2009-02-06 13:30:58 +000031protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040032 bool onQuery(Sample::Event* evt) override {
33 if (Sample::TitleQ(*evt)) {
34 Sample::TitleR(evt, "PathClip");
reed@android.comc07d23a2009-02-06 13:30:58 +000035 return true;
36 }
37 return this->INHERITED::onQuery(evt);
38 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000039
mtkleinf0599002015-07-13 06:18:39 -070040 void onDrawContent(SkCanvas* canvas) override {
reed67c65132015-09-28 06:16:07 -070041 const SkRect oval = fOval.makeOffset(fCenter.fX - fOval.centerX(),
42 fCenter.fY - fOval.centerY());
rmistry@google.comae933ce2012-08-23 18:19:56 +000043
reed@android.comc07d23a2009-02-06 13:30:58 +000044 SkPaint p;
45 p.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +000046
reed@android.comc07d23a2009-02-06 13:30:58 +000047 p.setStyle(SkPaint::kStroke_Style);
48 canvas->drawOval(oval, p);
49
reed67c65132015-09-28 06:16:07 -070050 const SkRect r = SkRect::MakeLTRB(200, 200, 300, 300);
reed@android.comc07d23a2009-02-06 13:30:58 +000051 canvas->clipRect(r);
rmistry@google.comae933ce2012-08-23 18:19:56 +000052
reed@android.comc07d23a2009-02-06 13:30:58 +000053 p.setStyle(SkPaint::kFill_Style);
54 p.setColor(SK_ColorRED);
55 canvas->drawRect(r, p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000056
reed@android.comc07d23a2009-02-06 13:30:58 +000057 p.setColor(0x800000FF);
reed@android.comc07d23a2009-02-06 13:30:58 +000058 canvas->drawOval(oval, p);
59 }
reed@android.come72fee52009-11-16 14:52:01 +000060
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040061 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override {
reed@android.come72fee52009-11-16 14:52:01 +000062 return new Click(this);
63 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000064
mtkleinf0599002015-07-13 06:18:39 -070065 bool onClick(Click* click) override {
reed@android.come72fee52009-11-16 14:52:01 +000066 fCenter.set(click->fCurr.fX, click->fCurr.fY);
caryclark@google.com02939ce2012-06-06 12:09:51 +000067 return false;
reed@android.comc07d23a2009-02-06 13:30:58 +000068 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000069
reed@android.comc07d23a2009-02-06 13:30:58 +000070private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040071 typedef Sample INHERITED;
reed@android.comc07d23a2009-02-06 13:30:58 +000072};
reed67c65132015-09-28 06:16:07 -070073DEF_SAMPLE( return new PathClipView; )
reed@android.comc07d23a2009-02-06 13:30:58 +000074
75//////////////////////////////////////////////////////////////////////////////
76
reed67c65132015-09-28 06:16:07 -070077static int clip_line(const SkRect& bounds, SkPoint p0, SkPoint p1, SkPoint edges[]) {
78 SkPoint* edgesStart = edges;
79
80 if (p0.fY == p1.fY) {
81 return 0;
82 }
halcanary9d524f22016-03-29 09:03:52 -070083
reed67c65132015-09-28 06:16:07 -070084 if (p0.fY > p1.fY) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -040085 using std::swap;
86 swap(p0, p1);
reed67c65132015-09-28 06:16:07 -070087 }
88 // now we're monotonic in Y: p0 <= p1
89 if (p1.fY <= bounds.top() || p0.fY >= bounds.bottom()) {
90 return 0;
91 }
halcanary9d524f22016-03-29 09:03:52 -070092
reed67c65132015-09-28 06:16:07 -070093 double dxdy = (double)(p1.fX - p0.fX) / (p1.fY - p0.fY);
94 if (p0.fY < bounds.top()) {
95 p0.fX = SkDoubleToScalar(p0.fX + dxdy * (bounds.top() - p0.fY));
96 p0.fY = bounds.top();
97 }
98 if (p1.fY > bounds.bottom()) {
99 p1.fX = SkDoubleToScalar(p1.fX + dxdy * (bounds.bottom() - p1.fY));
100 p1.fY = bounds.bottom();
101 }
halcanary9d524f22016-03-29 09:03:52 -0700102
reed67c65132015-09-28 06:16:07 -0700103 // Now p0...p1 is strictly inside bounds vertically, so we just need to clip horizontally
halcanary9d524f22016-03-29 09:03:52 -0700104
reed67c65132015-09-28 06:16:07 -0700105 if (p0.fX > p1.fX) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400106 using std::swap;
107 swap(p0, p1);
reed67c65132015-09-28 06:16:07 -0700108 }
109 // now we're left-to-right: p0 .. p1
halcanary9d524f22016-03-29 09:03:52 -0700110
reed67c65132015-09-28 06:16:07 -0700111 if (p1.fX <= bounds.left()) { // entirely to the left
112 p0.fX = p1.fX = bounds.left();
113 *edges++ = p0;
114 *edges++ = p1;
115 return 2;
116 }
117 if (p0.fX >= bounds.right()) { // entirely to the right
118 p0.fX = p1.fX = bounds.right();
119 *edges++ = p0;
120 *edges++ = p1;
121 return 2;
122 }
halcanary9d524f22016-03-29 09:03:52 -0700123
reed67c65132015-09-28 06:16:07 -0700124 if (p0.fX < bounds.left()) {
125 float y = SkDoubleToScalar(p0.fY + (bounds.left() - p0.fX) / dxdy);
126 *edges++ = SkPoint::Make(bounds.left(), p0.fY);
127 *edges++ = SkPoint::Make(bounds.left(), y);
128 p0.set(bounds.left(), y);
129 }
130 if (p1.fX > bounds.right()) {
131 float y = SkDoubleToScalar(p0.fY + (bounds.right() - p0.fX) / dxdy);
132 *edges++ = p0;
133 *edges++ = SkPoint::Make(bounds.right(), y);
134 *edges++ = SkPoint::Make(bounds.right(), p1.fY);
135 } else {
136 *edges++ = p0;
137 *edges++ = p1;
138 }
139 return SkToInt(edges - edgesStart);
140}
141
142static void draw_clipped_line(SkCanvas* canvas, const SkRect& bounds,
143 SkPoint p0, SkPoint p1, const SkPaint& paint) {
144 SkPoint verts[6];
145 int count = clip_line(bounds, p0, p1, verts);
146
147 SkPath path;
148 path.addPoly(verts, count, false);
149 canvas->drawPath(path, paint);
150}
151
152// Demonstrate edge-clipping that is used in the scan converter
153//
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400154class EdgeClipView : public Sample {
reed67c65132015-09-28 06:16:07 -0700155 enum {
156 N = 3
157 };
158public:
159 SkPoint fPoly[N];
160 SkRect fClip;
161 SkColor fEdgeColor[N];
halcanary9d524f22016-03-29 09:03:52 -0700162
reed67c65132015-09-28 06:16:07 -0700163 EdgeClipView() : fClip(SkRect::MakeLTRB(150, 150, 550, 450)) {
164 fPoly[0].set(300, 40);
165 fPoly[1].set(550, 250);
166 fPoly[2].set(40, 450);
167
168 fEdgeColor[0] = 0xFFFF0000;
169 fEdgeColor[1] = 0xFF00FF00;
170 fEdgeColor[2] = 0xFF0000FF;
171 }
halcanary9d524f22016-03-29 09:03:52 -0700172
reed67c65132015-09-28 06:16:07 -0700173protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400174 bool onQuery(Sample::Event* evt) override {
175 if (Sample::TitleQ(*evt)) {
176 Sample::TitleR(evt, "EdgeClip");
reed67c65132015-09-28 06:16:07 -0700177 return true;
178 }
179 return this->INHERITED::onQuery(evt);
180 }
181
182 static SkScalar snap(SkScalar x) {
183 return SkScalarRoundToScalar(x * 0.5f) * 2;
184 }
185 static SkPoint snap(const SkPoint& pt) {
186 return SkPoint::Make(snap(pt.x()), snap(pt.y()));
187 }
188 static void snap(SkPoint dst[], const SkPoint src[], int count) {
189 for (int i = 0; i < count; ++i) {
190 dst[i] = snap(src[i]);
191 }
192 }
193
194 void onDrawContent(SkCanvas* canvas) override {
195 SkPath path;
196 path.addPoly(fPoly, N, true);
197
198 // Draw the full triangle, stroked and filled
199 SkPaint p;
200 p.setAntiAlias(true);
201 p.setColor(0xFFE0E0E0);
202 canvas->drawPath(path, p);
203 p.setStyle(SkPaint::kStroke_Style);
204 p.setStrokeWidth(2);
205 for (int i = 0; i < N; ++i) {
206 const int j = (i + 1) % N;
207 p.setColor(fEdgeColor[i]);
208 p.setAlpha(0x88);
Hal Canary23e474c2017-05-15 13:35:35 -0400209 canvas->drawLine(fPoly[i], fPoly[j], p);
reed67c65132015-09-28 06:16:07 -0700210 }
211 p.setStyle(SkPaint::kFill_Style);
212
213 // Draw the clip itself
214 p.setColor(0xFF8888CC);
215 canvas->drawRect(fClip, p);
216
217 // Draw the filled triangle through the clip
218 p.setColor(0xFF88CC88);
219 canvas->save();
220 canvas->clipRect(fClip);
221 canvas->drawPath(path, p);
222 canvas->restore();
223
224 p.setStyle(SkPaint::kStroke_Style);
225 p.setStrokeWidth(6);
226
227 // Draw each of the "Edges" that survived the clipping
228 // We use a layer, so we can PLUS the different edge-colors, showing where two edges
229 // canceled each other out.
230 canvas->saveLayer(nullptr, nullptr);
reed374772b2016-10-05 17:33:02 -0700231 p.setBlendMode(SkBlendMode::kPlus);
reed67c65132015-09-28 06:16:07 -0700232 for (int i = 0; i < N; ++i) {
233 const int j = (i + 1) % N;
234 p.setColor(fEdgeColor[i]);
235 draw_clipped_line(canvas, fClip, fPoly[i], fPoly[j], p);
236 }
237 canvas->restore();
238 }
239
240 class MyClick : public Click {
241 public:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400242 MyClick(Sample* view) : Click(view) {}
reed67c65132015-09-28 06:16:07 -0700243 virtual void handleMove() = 0;
244 };
halcanary9d524f22016-03-29 09:03:52 -0700245
reed67c65132015-09-28 06:16:07 -0700246 class VertClick : public MyClick {
247 SkPoint* fPt;
248 public:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400249 VertClick(Sample* view, SkPoint* pt) : MyClick(view), fPt(pt) {}
reed67c65132015-09-28 06:16:07 -0700250 void handleMove() override { *fPt = snap(fCurr); }
251 };
halcanary9d524f22016-03-29 09:03:52 -0700252
reed67c65132015-09-28 06:16:07 -0700253 class DragRectClick : public MyClick {
254 SkRect* fRect;
255 public:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400256 DragRectClick(Sample* view, SkRect* rect) : MyClick(view), fRect(rect) {}
reed67c65132015-09-28 06:16:07 -0700257 void handleMove() override { fRect->offset(fCurr.x() - fPrev.x(), fCurr.y() - fPrev.y()); }
258 };
halcanary9d524f22016-03-29 09:03:52 -0700259
reed67c65132015-09-28 06:16:07 -0700260 class DragPolyClick : public MyClick {
261 SkPoint fSrc[100];
262 SkPoint* fPoly;
263 int fCount;
264 public:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400265 DragPolyClick(Sample* view, SkPoint poly[], int count)
reed67c65132015-09-28 06:16:07 -0700266 : MyClick(view), fPoly(poly), fCount(count)
267 {
268 SkASSERT((size_t)count <= SK_ARRAY_COUNT(fSrc));
269 memcpy(fSrc, poly, count * sizeof(SkPoint));
270 }
271 void handleMove() override {
272 const SkScalar dx = fCurr.x() - fOrig.x();
273 const SkScalar dy = fCurr.y() - fOrig.y();
274 for (int i = 0; i < fCount; ++i) {
275 fPoly[i].set(snap(fSrc[i].x() + dx), snap(fSrc[i].y() + dy));
276 }
277 }
278 };
279
280 class DoNothingClick : public MyClick {
281 public:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400282 DoNothingClick(Sample* view) : MyClick(view) {}
reed67c65132015-09-28 06:16:07 -0700283 void handleMove() override {}
284 };
285
286 static bool hit_test(const SkPoint& pt, SkScalar x, SkScalar y) {
287 const SkScalar rad = 8;
288 const SkScalar dx = pt.x() - x;
289 const SkScalar dy = pt.y() - y;
290 return dx*dx + dy*dy <= rad*rad;
291 }
292
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400293 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override {
reed67c65132015-09-28 06:16:07 -0700294 for (int i = 0; i < N; ++i) {
295 if (hit_test(fPoly[i], x, y)) {
296 return new VertClick(this, &fPoly[i]);
297 }
298 }
halcanary9d524f22016-03-29 09:03:52 -0700299
reed67c65132015-09-28 06:16:07 -0700300 SkPath path;
301 path.addPoly(fPoly, N, true);
302 if (path.contains(x, y)) {
303 return new DragPolyClick(this, fPoly, N);
304 }
305
306 if (fClip.intersects(SkRect::MakeLTRB(x - 1, y - 1, x + 1, y + 1))) {
307 return new DragRectClick(this, &fClip);
308 }
309 return new DoNothingClick(this);
310 }
halcanary9d524f22016-03-29 09:03:52 -0700311
reed67c65132015-09-28 06:16:07 -0700312 bool onClick(Click* click) override {
313 ((MyClick*)click)->handleMove();
reed67c65132015-09-28 06:16:07 -0700314 return false;
315 }
halcanary9d524f22016-03-29 09:03:52 -0700316
reed67c65132015-09-28 06:16:07 -0700317private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400318 typedef Sample INHERITED;
reed67c65132015-09-28 06:16:07 -0700319};
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400320
reed67c65132015-09-28 06:16:07 -0700321DEF_SAMPLE( return new EdgeClipView; )