blob: af7f38ede9fc130947d064251ca13d752fad858e [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 */
Mike Reedebfce6d2016-12-12 10:02:12 -05007
bsalomon@google.com7239aab2011-03-02 15:35:28 +00008#include "SampleCode.h"
9#include "SkCanvas.h"
bsalomon@google.com7239aab2011-03-02 15:35:28 +000010#include "SkPath.h"
bsalomon@google.com7239aab2011-03-02 15:35:28 +000011#include "SkView.h"
Mike Reedebfce6d2016-12-12 10:02:12 -050012#include "SkClipOpPriv.h"
bsalomon@google.com7239aab2011-03-02 15:35:28 +000013
reed@google.com961ddb02011-05-05 14:03:48 +000014class ComplexClipView : public SampleView {
bsalomon@google.com7239aab2011-03-02 15:35:28 +000015public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000016 ComplexClipView() {
reed@google.com961ddb02011-05-05 14:03:48 +000017 this->setBGColor(0xFFA0DDA0);
bsalomon@google.com7239aab2011-03-02 15:35:28 +000018 }
reed@google.com55b6b582011-03-02 15:58:18 +000019
bsalomon@google.com7239aab2011-03-02 15:35:28 +000020protected:
21 // overrides from SkEventSink
22 virtual bool onQuery(SkEvent* evt) {
23 if (SampleCode::TitleQ(*evt)) {
24 SampleCode::TitleR(evt, "ComplexClip");
25 return true;
26 }
27 return this->INHERITED::onQuery(evt);
28 }
reed@google.com55b6b582011-03-02 15:58:18 +000029
reed@google.com961ddb02011-05-05 14:03:48 +000030 virtual void onDrawContent(SkCanvas* canvas) {
bsalomon@google.com7239aab2011-03-02 15:35:28 +000031 SkPath path;
32 path.moveTo(SkIntToScalar(0), SkIntToScalar(50));
33 path.quadTo(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(50), SkIntToScalar(0));
34 path.lineTo(SkIntToScalar(175), SkIntToScalar(0));
35 path.quadTo(SkIntToScalar(200), SkIntToScalar(0), SkIntToScalar(200), SkIntToScalar(25));
36 path.lineTo(SkIntToScalar(200), SkIntToScalar(150));
37 path.quadTo(SkIntToScalar(200), SkIntToScalar(200), SkIntToScalar(150), SkIntToScalar(200));
38 path.lineTo(SkIntToScalar(0), SkIntToScalar(200));
39 path.close();
40 path.moveTo(SkIntToScalar(50), SkIntToScalar(50));
41 path.lineTo(SkIntToScalar(150), SkIntToScalar(50));
42 path.lineTo(SkIntToScalar(150), SkIntToScalar(125));
43 path.quadTo(SkIntToScalar(150), SkIntToScalar(150), SkIntToScalar(125), SkIntToScalar(150));
44 path.lineTo(SkIntToScalar(50), SkIntToScalar(150));
45 path.close();
46 path.setFillType(SkPath::kEvenOdd_FillType);
47 SkColor pathColor = SK_ColorBLACK;
48 SkPaint pathPaint;
49 pathPaint.setAntiAlias(true);
50 pathPaint.setColor(pathColor);
51
52 SkPath clipA;
53 clipA.moveTo(SkIntToScalar(10), SkIntToScalar(20));
54 clipA.lineTo(SkIntToScalar(165), SkIntToScalar(22));
55 clipA.lineTo(SkIntToScalar(70), SkIntToScalar(105));
56 clipA.lineTo(SkIntToScalar(165), SkIntToScalar(177));
57 clipA.lineTo(SkIntToScalar(-5), SkIntToScalar(180));
58 clipA.close();
59 SkColor colorA = SK_ColorCYAN;
60
61 SkPath clipB;
62 clipB.moveTo(SkIntToScalar(40), SkIntToScalar(10));
63 clipB.lineTo(SkIntToScalar(190), SkIntToScalar(15));
64 clipB.lineTo(SkIntToScalar(195), SkIntToScalar(190));
65 clipB.lineTo(SkIntToScalar(40), SkIntToScalar(185));
66 clipB.lineTo(SkIntToScalar(155), SkIntToScalar(100));
67 clipB.close();
68 SkColor colorB = SK_ColorRED;
69
bsalomon@google.com7239aab2011-03-02 15:35:28 +000070 SkPaint paint;
71 paint.setAntiAlias(true);
72
73 paint.setStyle(SkPaint::kStroke_Style);
74 paint.setStrokeWidth(0);
75
76 canvas->translate(SkIntToScalar(10),SkIntToScalar(10));
77 canvas->drawPath(path, pathPaint);
78 paint.setColor(colorA);
79 canvas->drawPath(clipA, paint);
80 paint.setColor(colorB);
81 canvas->drawPath(clipB, paint);
82
83 static const struct {
Mike Reedc1f77742016-12-09 09:00:50 -050084 SkClipOp fOp;
85 const char* fName;
bsalomon@google.com7239aab2011-03-02 15:35:28 +000086 } gOps[] = { //extra spaces in names for measureText
Mike Reedc1f77742016-12-09 09:00:50 -050087 {kIntersect_SkClipOp, "Isect "},
88 {kDifference_SkClipOp, "Diff " },
89 {kUnion_SkClipOp, "Union "},
90 {kXOR_SkClipOp, "Xor " },
91 {kReverseDifference_SkClipOp, "RDiff "}
bsalomon@google.com7239aab2011-03-02 15:35:28 +000092 };
93
94 canvas->translate(0, SkIntToScalar(40));
95 canvas->scale(3 * SK_Scalar1 / 4, 3 * SK_Scalar1 / 4);
96 canvas->save();
97
bsalomon@google.com807cec42011-03-10 19:20:15 +000098 for (int invA = 0; invA < 2; ++invA) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000099 for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); ++op) {
reed@google.com7fa2a652014-01-27 13:42:58 +0000100 size_t idx = invA * SK_ARRAY_COUNT(gOps) + op;
bsalomon@google.com7239aab2011-03-02 15:35:28 +0000101 if (!(idx % 3)) {
102 canvas->restore();
103 canvas->translate(0, SkIntToScalar(250));
104 canvas->save();
105 }
106 canvas->save();
107 // set clip
108 clipA.setFillType(invA ? SkPath::kInverseEvenOdd_FillType :
109 SkPath::kEvenOdd_FillType);
110 canvas->clipPath(clipA);
111 canvas->clipPath(clipB, gOps[op].fOp);
112
113 // draw path clipped
114 canvas->drawPath(path, pathPaint);
115 canvas->restore();
116
117 // draw path in hairline
118 paint.setColor(pathColor);
119 canvas->drawPath(path, paint);
120
121 // draw clips in hair line
122 paint.setColor(colorA);
123 canvas->drawPath(clipA, paint);
124 paint.setColor(colorB);
125 canvas->drawPath(clipB, paint);
126
127 paint.setTextSize(SkIntToScalar(20));
128
129 SkScalar txtX = SkIntToScalar(55);
130 paint.setColor(colorA);
131 const char* aTxt = invA ? "InverseA " : "A ";
Cary Clark2a475ea2017-04-28 15:35:12 -0400132 canvas->drawString(aTxt, txtX, SkIntToScalar(220), paint);
bsalomon@google.com7239aab2011-03-02 15:35:28 +0000133 txtX += paint.measureText(aTxt, strlen(aTxt));
134 paint.setColor(SK_ColorBLACK);
Cary Clark2a475ea2017-04-28 15:35:12 -0400135 canvas->drawString(gOps[op].fName,
bsalomon@google.com7239aab2011-03-02 15:35:28 +0000136 txtX, SkIntToScalar(220), paint);
137 txtX += paint.measureText(gOps[op].fName, strlen(gOps[op].fName));
138 paint.setColor(colorB);
Cary Clark2a475ea2017-04-28 15:35:12 -0400139 canvas->drawString("B", txtX, SkIntToScalar(220), paint);
bsalomon@google.com7239aab2011-03-02 15:35:28 +0000140
141 canvas->translate(SkIntToScalar(250),0);
142 }
143 }
144 canvas->restore();
145 }
146
bsalomon@google.com7239aab2011-03-02 15:35:28 +0000147private:
reed@google.com961ddb02011-05-05 14:03:48 +0000148 typedef SampleView INHERITED;
bsalomon@google.com7239aab2011-03-02 15:35:28 +0000149};
150
151//////////////////////////////////////////////////////////////////////////////
152
153static SkView* MyFactory() { return new ComplexClipView; }
154static SkViewRegister reg(MyFactory);