SamplePathClip.cpp 8.6 KB

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