Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(182)

Side by Side Diff: samplecode/SampleHT.cpp

Issue 760023002: add sample to exercise hittesting (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « samplecode/SampleApp.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1
2 /*
robertphillips 2014/11/26 14:21:35 2014 ?
reed1 2014/11/26 14:25:53 Done.
3 * Copyright 2011 Google Inc.
tfarina 2014/11/26 14:10:09 s/2011/2014?
reed1 2014/11/26 14:12:58 Done.
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"
robertphillips 2014/11/26 14:21:35 not needed ?
reed1 2014/11/26 14:25:53 Done.
11 #include "SkGradientShader.h"
12 #include "SkPath.h"
robertphillips 2014/11/26 14:21:35 same ?
reed1 2014/11/26 14:25:53 Done.
13 #include "SkRegion.h"
14 #include "SkShader.h"
15 #include "SkUtils.h"
robertphillips 2014/11/26 14:21:35 same ?
reed1 2014/11/26 14:25:53 Done.
16 #include "SkComposeShader.h"
17 #include "Sk1DPathEffect.h"
18 #include "SkCornerPathEffect.h"
19 #include "SkPathMeasure.h"
20 #include "SkPictureRecorder.h"
21 #include "SkRandom.h"
22 #include "SkColorPriv.h"
23 #include "SkColorFilter.h"
24 #include "SkLayerRasterizer.h"
25
26 #include "SkCanvasDrawable.h"
27 #include "SkInterpolator.h"
28
29 const SkRect gUnitSquare = { -1, -1, 1, 1 };
30
31 static void color_to_floats(SkColor c, SkScalar f[4]) {
32 f[0] = SkIntToScalar(SkColorGetA(c));
33 f[1] = SkIntToScalar(SkColorGetR(c));
34 f[2] = SkIntToScalar(SkColorGetG(c));
35 f[3] = SkIntToScalar(SkColorGetB(c));
36 }
37
38 static SkColor floats_to_color(const SkScalar f[4]) {
39 return SkColorSetARGB(SkScalarRoundToInt(f[0]),
40 SkScalarRoundToInt(f[1]),
41 SkScalarRoundToInt(f[2]),
42 SkScalarRoundToInt(f[3]));
43 }
44
45 static bool oval_contains(const SkRect& r, SkScalar x, SkScalar y) {
46 SkMatrix m;
47 m.setRectToRect(r, gUnitSquare, SkMatrix::kFill_ScaleToFit);
48 SkPoint pt;
49 m.mapXY(x, y, &pt);
50 return pt.lengthSqd() <= 1;
51 }
52
53 static SkColor rand_opaque_color(uint32_t seed) {
54 SkRandom rand(seed);
55 return rand.nextU() | (0xFF << 24);
56 }
57
58 class HTDrawable : public SkCanvasDrawable {
59 SkRect fR;
60 SkColor fColor;
61 SkInterpolator* fInterp;
62
63 public:
64 HTDrawable(SkRandom& rand) {
65 fR = SkRect::MakeXYWH(rand.nextRangeF(0, 640), rand.nextRangeF(0, 480),
66 rand.nextRangeF(20, 200), rand.nextRangeF(20, 200) );
67 fColor = rand_opaque_color(rand.nextU());
68 fInterp = NULL;
69 }
70
71 void spawnAnimation() {
72 SkDELETE(fInterp);
73 fInterp = SkNEW_ARGS(SkInterpolator, (5, 3));
74 SkScalar values[5];
75 color_to_floats(fColor, values); values[4] = 0;
76 fInterp->setKeyFrame(0, SampleCode::GetAnimTime(), values);
77 values[0] = 0; values[4] = 180;
78 fInterp->setKeyFrame(1, SampleCode::GetAnimTime() + 1000, values);
79 color_to_floats(rand_opaque_color(fColor), values); values[4] = 360;
80 fInterp->setKeyFrame(2, SampleCode::GetAnimTime() + 2000, values);
81
82 fInterp->setMirror(true);
83 fInterp->setRepeatCount(3);
84
85 this->notifyDrawingChanged();
86 }
87
88 bool hitTest(SkScalar x, SkScalar y) {
89 return oval_contains(fR, x, y);
90 }
91
92 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
93 SkAutoCanvasRestore acr(canvas, false);
94
95 SkPaint paint;
96 paint.setAntiAlias(true);
97
98 if (fInterp) {
99 SkScalar values[5];
100 SkInterpolator::Result res = fInterp->timeToValues(SampleCode::GetAn imTime(), values);
101 fColor = floats_to_color(values);
102
103 canvas->save();
104 canvas->translate(fR.centerX(), fR.centerY());
105 canvas->rotate(values[4]);
106 canvas->translate(-fR.centerX(), -fR.centerY());
107
108 switch (res) {
109 case SkInterpolator::kFreezeEnd_Result:
110 SkDELETE(fInterp);
111 fInterp = NULL;
112 break;
113 default:
114 break;
115 }
116 }
117 paint.setColor(fColor);
118 canvas->drawRect(fR, paint);
119 }
120
121 SkRect onGetBounds() SK_OVERRIDE { return fR; }
122 };
123
124 class HTView : public SampleView {
125 public:
126 enum {
127 N = 50,
128 W = 640,
129 H = 480,
130 };
131
132 struct Rec {
133 HTDrawable* fDrawable;
134 };
135 Rec fArray[N];
136 SkAutoTUnref<SkCanvasDrawable> fRoot;
137
138 HTView() {
139 SkRandom rand;
140
141 SkPictureRecorder recorder;
142 SkCanvas* canvas = recorder.beginRecording(SkRect::MakeWH(W, H));
143 for (int i = 0; i < N; ++i) {
144 fArray[i].fDrawable = new HTDrawable(rand);
145 canvas->EXPERIMENTAL_drawDrawable(fArray[i].fDrawable);
146 fArray[i].fDrawable->unref();
147 }
148 fRoot.reset(recorder.EXPERIMENTAL_endRecordingAsDrawable());
149 }
150
151 protected:
152 // overrides from SkEventSink
153 bool onQuery(SkEvent* evt) SK_OVERRIDE {
154 if (SampleCode::TitleQ(*evt)) {
155 SampleCode::TitleR(evt, "HT");
156 return true;
157 }
158 return this->INHERITED::onQuery(evt);
159 }
160
161 void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
162 canvas->EXPERIMENTAL_drawDrawable(fRoot);
163 this->inval(NULL);
164 }
165
166 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) SK_ OVERRIDE {
167 // search backwards to find the top-most
168 for (int i = N - 1; i >= 0; --i) {
169 if (fArray[i].fDrawable->hitTest(x, y)) {
170 fArray[i].fDrawable->spawnAnimation();
171 break;
172 }
173 }
174 this->inval(NULL);
175 return NULL;
176 }
177
178 private:
179 typedef SampleView INHERITED;
180 };
181
182 //////////////////////////////////////////////////////////////////////////////
183
184 static SkView* MyFactory() { return new HTView; }
185 static SkViewRegister reg(MyFactory);
OLDNEW
« no previous file with comments | « samplecode/SampleApp.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698