blob: ccf4281c1b349ebcc8db85670f4553e880e6d83a [file] [log] [blame]
reed@android.com70149062009-11-16 20:39:43 +00001#include "SampleCode.h"
2#include "SkView.h"
3#include "SkCanvas.h"
4#include "SkGradientShader.h"
5#include "SkGraphics.h"
6#include "SkImageDecoder.h"
7#include "SkPath.h"
8#include "SkRegion.h"
9#include "SkShader.h"
10#include "SkUtils.h"
11#include "SkXfermode.h"
12#include "SkColorPriv.h"
13#include "SkColorFilter.h"
14#include "SkTime.h"
15#include "SkRandom.h"
reed@android.com77f0ef72009-11-17 18:47:52 +000016
reed@android.com70149062009-11-16 20:39:43 +000017#include "SkLineClipper.h"
reed@android.com77f0ef72009-11-17 18:47:52 +000018#include "SkQuadClipper.h"
19
20static void drawQuad(SkCanvas* canvas, const SkPoint pts[3], const SkPaint& p) {
21 SkPath path;
22 path.moveTo(pts[0]);
23 path.quadTo(pts[1], pts[2]);
24 canvas->drawPath(path, p);
25}
26
27typedef void (*clipper_proc)(const SkPoint src[], const SkRect& clip,
28 SkCanvas*, const SkPaint&, const SkPaint&);
29
30static void check_clipper(int count, const SkPoint pts[], const SkRect& clip) {
31 for (int i = 0; i < count; i++) {
32 SkASSERT(pts[i].fX >= clip.fLeft);
33 SkASSERT(pts[i].fX <= clip.fRight);
34 SkASSERT(pts[i].fY >= clip.fTop);
35 SkASSERT(pts[i].fY <= clip.fBottom);
36 }
37}
38
39static void line_clipper(const SkPoint src[], const SkRect& clip,
40 SkCanvas* canvas, const SkPaint& p0, const SkPaint& p1) {
41 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, src, p1);
42
43 SkPoint dst[SkLineClipper::kMaxPoints];
44 int count = SkLineClipper::ClipLine(src, clip, dst);
45 for (int i = 0; i < count; i++) {
46 check_clipper(2, &dst[i], clip);
47 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, &dst[i], p0);
48 }
49}
50
51static void quad_clipper(const SkPoint src[], const SkRect& clip,
52 SkCanvas* canvas, const SkPaint& p0, const SkPaint& p1) {
53 drawQuad(canvas, src, p1);
54
55 SkQuadClipper2 clipper;
56 if (clipper.clipQuad(src, clip)) {
57 SkPoint pts[3];
58 SkPath::Verb verb;
59 while ((verb = clipper.next(pts)) != SkPath::kDone_Verb) {
60 switch (verb) {
61 case SkPath::kLine_Verb:
62 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p0);
63 break;
64 case SkPath::kQuad_Verb:
65 drawQuad(canvas, pts, p0);
66 break;
67 default:
68 SkASSERT(!"unexpected verb");
69 }
70 }
71 }
72}
73
74static const clipper_proc gProcs[] = {
75 line_clipper,
76 quad_clipper
77};
78
79///////////////////////////////////////////////////////////////////////////////
reed@android.com70149062009-11-16 20:39:43 +000080
81enum {
reed@android.com77f0ef72009-11-17 18:47:52 +000082 W = 640/3,
83 H = 480/3
reed@android.com70149062009-11-16 20:39:43 +000084};
85
86class LineClipperView : public SkView {
reed@android.com77f0ef72009-11-17 18:47:52 +000087 int fProcIndex;
reed@android.com70149062009-11-16 20:39:43 +000088 SkRect fClip;
89 SkRandom fRand;
reed@android.com77f0ef72009-11-17 18:47:52 +000090 SkPoint fPts[4];
reed@android.com70149062009-11-16 20:39:43 +000091
92 void randPts() {
reed@android.com77f0ef72009-11-17 18:47:52 +000093 for (int i = 0; i < SK_ARRAY_COUNT(fPts); i++) {
94 fPts[i].set(fRand.nextUScalar1() * 640,
95 fRand.nextUScalar1() * 480);
96 }
reed@android.com70149062009-11-16 20:39:43 +000097 }
98
99public:
100 LineClipperView() {
101 int x = (640 - W)/2;
102 int y = (480 - H)/2;
103 fClip.set(x, y, x + W, y + H);
104 this->randPts();
reed@android.com77f0ef72009-11-17 18:47:52 +0000105
106 fProcIndex = 1;
reed@android.com70149062009-11-16 20:39:43 +0000107 }
108
109protected:
110 // overrides from SkEventSink
111 virtual bool onQuery(SkEvent* evt) {
112 if (SampleCode::TitleQ(*evt)) {
113 SampleCode::TitleR(evt, "LineClipper");
114 return true;
115 }
116 return this->INHERITED::onQuery(evt);
117 }
118
119 void drawBG(SkCanvas* canvas) {
120 canvas->drawColor(SK_ColorWHITE);
121 }
122
123 static void drawVLine(SkCanvas* canvas, SkScalar x, const SkPaint& paint) {
124 canvas->drawLine(x, -999, x, 999, paint);
125 }
126
127 static void drawHLine(SkCanvas* canvas, SkScalar y, const SkPaint& paint) {
128 canvas->drawLine(-999, y, 999, y, paint);
129 }
130
reed@android.com70149062009-11-16 20:39:43 +0000131 virtual void onDraw(SkCanvas* canvas) {
132 this->drawBG(canvas);
133
reed@android.com77f0ef72009-11-17 18:47:52 +0000134 SkPaint paint, paint1;
reed@android.com70149062009-11-16 20:39:43 +0000135
136 drawVLine(canvas, fClip.fLeft + SK_ScalarHalf, paint);
137 drawVLine(canvas, fClip.fRight - SK_ScalarHalf, paint);
138 drawHLine(canvas, fClip.fTop + SK_ScalarHalf, paint);
139 drawHLine(canvas, fClip.fBottom - SK_ScalarHalf, paint);
140
141 paint.setColor(SK_ColorLTGRAY);
142 canvas->drawRect(fClip, paint);
143
144 paint.setAntiAlias(true);
145 paint.setColor(SK_ColorBLUE);
reed@android.com77f0ef72009-11-17 18:47:52 +0000146 paint.setStyle(SkPaint::kStroke_Style);
147 // paint.setStrokeWidth(SkIntToScalar(3));
reed@android.com70149062009-11-16 20:39:43 +0000148 paint.setStrokeCap(SkPaint::kRound_Cap);
reed@android.com77f0ef72009-11-17 18:47:52 +0000149
150 paint1.setAntiAlias(true);
151 paint1.setColor(SK_ColorRED);
152 paint1.setStyle(SkPaint::kStroke_Style);
153 gProcs[fProcIndex](fPts, fClip, canvas, paint, paint1);
reed@android.com70149062009-11-16 20:39:43 +0000154
155 if (true) {
156 this->randPts();
157 this->inval(NULL);
158 }
159 }
160
161 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
reed@android.com77f0ef72009-11-17 18:47:52 +0000162 // fProcIndex = (fProcIndex + 1) % SK_ARRAY_COUNT(gProcs);
163 if (x < 50 && y < 50) {
164 this->randPts();
165 }
reed@android.com70149062009-11-16 20:39:43 +0000166 this->inval(NULL);
167 return NULL;
168 }
169
170 virtual bool onClick(Click* click) {
171 return false;
172 }
173
174private:
175 typedef SkView INHERITED;
176};
177
178//////////////////////////////////////////////////////////////////////////////
179
180static SkView* MyFactory() { return new LineClipperView; }
181static SkViewRegister reg(MyFactory);
182