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