blob: 2bb2fa23fd92589da748c55b53e0d0da3b1f66bb [file] [log] [blame]
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkPath.h"
12#include "SkRandom.h"
13
14class HairCurvesView : public SampleView {
15public:
16 HairCurvesView() {
17 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000018
bsalomon@google.comaeb21602011-08-30 18:13:44 +000019protected:
20 // overrides from SkEventSink
21 virtual bool onQuery(SkEvent* evt) {
22 if (SampleCode::TitleQ(*evt)) {
23 SampleCode::TitleR(evt, "HairCurves");
24 return true;
25 }
26 return this->INHERITED::onQuery(evt);
27 }
28
29
30 virtual void onDrawContent(SkCanvas* canvas) {
31 SkPaint paint;
32 paint.setAntiAlias(true);
33 paint.setStyle(SkPaint::kStroke_Style);
34 paint.setStrokeWidth(-1);
35 canvas->save();
36 canvas->scale(1000 * SK_Scalar1, 1000 * SK_Scalar1);
37 SkRandom rand;
38 SkPath curves;
39 SkPath hulls;
40 SkPath ctrlPts;
bsalomon@google.comaeb21602011-08-30 18:13:44 +000041 for (int i = 0; i < 100; ++i) {
42 SkScalar pts[] = {
43 rand.nextUScalar1(), rand.nextUScalar1(),
44 rand.nextUScalar1(), rand.nextUScalar1(),
45 rand.nextUScalar1(), rand.nextUScalar1(),
46 rand.nextUScalar1(), rand.nextUScalar1()
47 };
48 curves.moveTo(pts[0], pts[1]);
49 curves.cubicTo(pts[2], pts[3],
50 pts[4], pts[5],
51 pts[6], pts[7]);
52
53 hulls.moveTo(pts[0], pts[1]);
54 hulls.lineTo(pts[2], pts[3]);
55 hulls.lineTo(pts[4], pts[5]);
56 hulls.lineTo(pts[6], pts[7]);
57
58 ctrlPts.addCircle(pts[0], pts[1], SK_Scalar1 / 200);
59 ctrlPts.addCircle(pts[2], pts[3], SK_Scalar1 / 200);
60 ctrlPts.addCircle(pts[4], pts[5], SK_Scalar1 / 200);
61 ctrlPts.addCircle(pts[6], pts[7], SK_Scalar1 / 200);
62 }
63 for (int i = 0; i < 100; ++i) {
64 SkScalar pts[] = {
65 rand.nextUScalar1(), rand.nextUScalar1(),
66 rand.nextUScalar1(), rand.nextUScalar1(),
67 rand.nextUScalar1(), rand.nextUScalar1(),
68 };
69 curves.moveTo(pts[0], pts[1]);
70 curves.quadTo(pts[2], pts[3],
71 pts[4], pts[5]);
72
73 hulls.moveTo(pts[0], pts[1]);
74 hulls.lineTo(pts[2], pts[3]);
75 hulls.lineTo(pts[4], pts[5]);
76
77 ctrlPts.addCircle(pts[0], pts[1], SK_Scalar1 / 200);
78 ctrlPts.addCircle(pts[2], pts[3], SK_Scalar1 / 200);
79 ctrlPts.addCircle(pts[4], pts[5], SK_Scalar1 / 200);
80 }
81 for (int i = 0; i < 100; ++i) {
82 SkScalar pts[] = {
83 rand.nextUScalar1(), rand.nextUScalar1(),
84 rand.nextUScalar1(), rand.nextUScalar1(),
85 };
86 curves.moveTo(pts[0], pts[1]);
87 curves.lineTo(pts[2], pts[3]);
88
89 ctrlPts.addCircle(pts[0], pts[1], SK_Scalar1 / 200);
90 ctrlPts.addCircle(pts[2], pts[3], SK_Scalar1 / 200);
91 }
92
93 paint.setColor(SK_ColorBLACK);
94 canvas->drawPath(curves, paint);
95 paint.setColor(SK_ColorRED);
96 //canvas->drawPath(hulls, paint);
97 paint.setStyle(SkPaint::kFill_Style);
98 paint.setColor(SK_ColorBLUE);
99 //canvas->drawPath(ctrlPts, paint);
100
101 canvas->restore();
102 }
103
104private:
105 typedef SampleView INHERITED;
106};
107
108//////////////////////////////////////////////////////////////////////////////
109
110static SkView* MyFactory() { return new HairCurvesView; }
111static SkViewRegister reg(MyFactory);
112