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

Side by Side Diff: samplecode/SampleSVGPong.cpp

Issue 2243853003: [SVGDom] SVGPong sample app (Closed) Base URL: https://chromium.googlesource.com/skia.git@master
Patch Set: review Created 4 years, 4 months 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 | « gyp/samples.gypi ('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 * Copyright 2016 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 "SampleCode.h"
9 #include "SkAnimTimer.h"
10 #include "SkColor.h"
11 #include "SkRandom.h"
12 #include "SkRRect.h"
13 #include "SkSVGDOM.h"
14 #include "SkSVGG.h"
15 #include "SkSVGPath.h"
16 #include "SkSVGRect.h"
17 #include "SkSVGSVG.h"
18
19 namespace {
20
21 static const SkRect kBounds = SkRect::MakeLTRB(0.1f, 0.1f, 0.9f, 0.9f);
22 static const SkSize kPaddleSize = SkSize::Make(0.03f, 0.1f);
23 static const SkScalar kBallSize = 0.04f;
24 static const SkScalar kShadowOpacity = 0.40f;
25 static const SkScalar kShadowParallax = 0.04f;
26 static const SkScalar kBackgroundStroke = 0.01f;
27 static const uint32_t kBackgroundDashCount = 20;
28
29 static const SkScalar kBallSpeedMax = 0.0020f;
30 static const SkScalar kBallSpeedMin = 0.0005f;
31 static const SkScalar kBallSpeedFuzz = 0.0002f;
32
33 static const SkScalar kTimeScaleMin = 0.0f;
34 static const SkScalar kTimeScaleMax = 5.0f;
35
36 // Box the value within [min, max), by applying infinite reflection on the inter val endpoints.
37 SkScalar box_reflect(SkScalar v, SkScalar min, SkScalar max) {
38 const SkScalar intervalLen = max - min;
39 SkASSERT(intervalLen > 0);
40
41 // f(v) is periodic in 2 * intervalLen: one normal progression + one reflect ion
42 const SkScalar P = intervalLen * 2;
43 // relative to P origin
44 const SkScalar vP = v - min;
45 // map to [0, P)
46 const SkScalar vMod = (vP < 0) ? P - SkScalarMod(-vP, P) : SkScalarMod(vP, P );
47 // reflect if needed, to map to [0, intervalLen)
48 const SkScalar vInterval = vMod < intervalLen ? vMod : P - vMod;
49 // finally, reposition relative to min
50 return vInterval + min;
51 }
52
53 // Compute <t, y> for the trajectory intersection with the next vertical edge.
54 std::tuple<SkScalar, SkScalar> find_yintercept(const SkPoint& pos, const SkVecto r& spd,
55 const SkRect& box) {
56 const SkScalar edge = spd.fX > 0 ? box.fRight : box.fLeft;
57 const SkScalar t = (edge - pos.fX) / spd.fX;
58 SkASSERT(t >= 0);
59 const SkScalar dY = t * spd.fY;
60
61 return std::make_tuple(t, box_reflect(pos.fY + dY, box.fTop, box.fBottom));
62 }
63
64 sk_sp<SkSVGRect> make_svg_rrect(const SkRRect& rrect) {
65 sk_sp<SkSVGRect> node = SkSVGRect::Make();
66 node->setX(SkSVGLength(rrect.rect().x()));
67 node->setY(SkSVGLength(rrect.rect().y()));
68 node->setWidth(SkSVGLength(rrect.width()));
69 node->setHeight(SkSVGLength(rrect.height()));
70 node->setRx(SkSVGLength(rrect.getSimpleRadii().x()));
71 node->setRy(SkSVGLength(rrect.getSimpleRadii().y()));
72
73 return node;
74 }
75
76 } // anonymous ns
77
78 class SVGPongView final : public SampleView {
79 public:
80 SVGPongView() {}
81
82 protected:
83 void onOnceBeforeDraw() override {
84 const SkRect fieldBounds = kBounds.makeOutset(kBallSize / 2, kBallSize / 2);
85 const SkRRect ball = SkRRect::MakeOval(SkRect::MakeWH(kBallSize, kBallSi ze));
86 const SkRRect paddle = SkRRect::MakeRectXY(SkRect::MakeWH(kPaddleSize.wi dth(),
87 kPaddleSize.he ight()),
88 kPaddleSize.width() / 2,
89 kPaddleSize.width() / 2);
90 fBall.initialize(ball,
91 SK_ColorGREEN,
92 SkPoint::Make(kBounds.centerX(), kBounds.centerY()),
93 SkVector::Make(fRand.nextRangeScalar(kBallSpeedMin, kBa llSpeedMax),
94 fRand.nextRangeScalar(kBallSpeedMin, kBa llSpeedMax)));
95 fPaddle0.initialize(paddle,
96 SK_ColorBLUE,
97 SkPoint::Make(fieldBounds.left() - kPaddleSize.width () / 2,
98 fieldBounds.centerY()),
99 SkVector::Make(0, 0));
100 fPaddle1.initialize(paddle,
101 SK_ColorRED,
102 SkPoint::Make(fieldBounds.right() + kPaddleSize.widt h() / 2,
103 fieldBounds.centerY()),
104 SkVector::Make(0, 0));
105
106 // Background decoration.
107 SkPath bgPath;
108 bgPath.moveTo(kBounds.left() , fieldBounds.top());
109 bgPath.lineTo(kBounds.right(), fieldBounds.top());
110 bgPath.moveTo(kBounds.left() , fieldBounds.bottom());
111 bgPath.lineTo(kBounds.right(), fieldBounds.bottom());
112 // TODO: stroke-dash support would come in handy right about now.
113 for (uint32_t i = 0; i < kBackgroundDashCount; ++i) {
114 bgPath.moveTo(kBounds.centerX(),
115 kBounds.top() + (i + 0.25f) * kBounds.height() / kBack groundDashCount);
116 bgPath.lineTo(kBounds.centerX(),
117 kBounds.top() + (i + 0.75f) * kBounds.height() / kBack groundDashCount);
118 }
119
120 sk_sp<SkSVGPath> bg = SkSVGPath::Make();
121 bg->setPath(bgPath);
122 bg->setFill(SkSVGPaint(SkSVGPaint::Type::kNone));
123 bg->setStroke(SkSVGPaint(SkSVGColorType(SK_ColorBLACK)));
124 bg->setStrokeWidth(SkSVGLength(kBackgroundStroke));
125
126 // Build the SVG DOM tree.
127 sk_sp<SkSVGSVG> root = SkSVGSVG::Make();
128 root->appendChild(std::move(bg));
129 root->appendChild(fPaddle0.shadowNode);
130 root->appendChild(fPaddle1.shadowNode);
131 root->appendChild(fBall.shadowNode);
132 root->appendChild(fPaddle0.objectNode);
133 root->appendChild(fPaddle1.objectNode);
134 root->appendChild(fBall.objectNode);
135
136 // Handle everything in a normalized 1x1 space.
137 root->setViewBox(SkSVGViewBoxType(SkRect::MakeWH(1, 1)));
138
139 fDom = sk_sp<SkSVGDOM>(new SkSVGDOM(SkSize::Make(this->width(), this->he ight())));
140 fDom->setRoot(std::move(root));
141
142 // Off we go.
143 this->updatePaddleStrategy();
144 }
145
146 bool onQuery(SkEvent* evt) override {
147 if (SampleCode::TitleQ(*evt)) {
148 SampleCode::TitleR(evt, "SVGPong");
149 return true;
150 }
151
152 SkUnichar uni;
153 if (SampleCode::CharQ(*evt, &uni)) {
154 switch (uni) {
155 case '[':
156 fTimeScale = SkTPin(fTimeScale - 0.1f, kTimeScaleMin, kTimeS caleMax);
157 return true;
158 case ']':
159 fTimeScale = SkTPin(fTimeScale + 0.1f, kTimeScaleMin, kTimeS caleMax);
160 return true;
161 default:
162 break;
163 }
164 }
165 return this->INHERITED::onQuery(evt);
166 }
167
168 void onSizeChange() override {
169 if (fDom) {
170 fDom->setContainerSize(SkSize::Make(this->width(), this->height()));
171 }
172
173 this->INHERITED::onSizeChange();
174 }
175
176 void onDrawContent(SkCanvas* canvas) override {
177 fDom->render(canvas);
178 }
179
180 bool onAnimate(const SkAnimTimer& timer) override {
181 SkScalar dt = (timer.msec() - fLastTick) * fTimeScale;
182 fLastTick = timer.msec();
183
184 fPaddle0.posTick(dt);
185 fPaddle1.posTick(dt);
186 fBall.posTick(dt);
187
188 this->enforceConstraints();
189
190 fPaddle0.updateDom();
191 fPaddle1.updateDom();
192 fBall.updateDom();
193
194 return true;
195 }
196
197 private:
198 struct Object {
199 void initialize(const SkRRect& rrect, SkColor color,
200 const SkPoint& p, const SkVector& s) {
201 objectNode = make_svg_rrect(rrect);
202 objectNode->setFill(SkSVGPaint(SkSVGColorType(color)));
203
204 shadowNode = make_svg_rrect(rrect);
205 shadowNode->setFillOpacity(SkSVGNumberType(kShadowOpacity));
206
207 pos = p;
208 spd = s;
209 size = SkSize::Make(rrect.width(), rrect.height());
210 }
211
212 void posTick(SkScalar dt) {
213 pos += spd * dt;
214 }
215
216 void updateDom() {
217 const SkPoint corner = pos - SkPoint::Make(size.width() / 2, size.he ight() / 2);
218 objectNode->setX(SkSVGLength(corner.x()));
219 objectNode->setY(SkSVGLength(corner.y()));
220
221 // Simulate parallax shadow for a centered light source.
222 SkPoint shadowOffset = pos - SkPoint::Make(kBounds.centerX(), kBound s.centerY());
223 shadowOffset.scale(kShadowParallax);
224 const SkPoint shadowCorner = corner + shadowOffset;
225
226 shadowNode->setX(SkSVGLength(shadowCorner.x()));
227 shadowNode->setY(SkSVGLength(shadowCorner.y()));
228 }
229
230 sk_sp<SkSVGRect> objectNode;
231 sk_sp<SkSVGRect> shadowNode;
232 SkPoint pos;
233 SkVector spd;
234 SkSize size;
235 };
236
237 void enforceConstraints() {
238 // Perfect vertical reflection.
239 if (fBall.pos.fY < kBounds.fTop || fBall.pos.fY >= kBounds.fBottom) {
240 fBall.spd.fY = -fBall.spd.fY;
241 fBall.pos.fY = box_reflect(fBall.pos.fY, kBounds.fTop, kBounds.fBott om);
242 }
243
244 // Horizontal bounce - introduces a speed fuzz.
245 if (fBall.pos.fX < kBounds.fLeft || fBall.pos.fX >= kBounds.fRight) {
246 fBall.spd.fX = this->fuzzBallSpeed(-fBall.spd.fX);
247 fBall.spd.fY = this->fuzzBallSpeed(fBall.spd.fY);
248 fBall.pos.fX = box_reflect(fBall.pos.fX, kBounds.fLeft, kBounds.fRig ht);
249 this->updatePaddleStrategy();
250 }
251 }
252
253 SkScalar fuzzBallSpeed(SkScalar spd) {
254 // The speed limits are absolute values.
255 const SkScalar sign = spd >= 0 ? 1.0f : -1.0f;
256 const SkScalar fuzzed = fabs(spd) + fRand.nextRangeScalar(-kBallSpeedFuz z, kBallSpeedFuzz);
257
258 return sign * SkTPin(fuzzed, kBallSpeedMin, kBallSpeedMax);
259 }
260
261 void updatePaddleStrategy() {
262 Object* pitcher = fBall.spd.fX > 0 ? &fPaddle0 : &fPaddle1;
263 Object* catcher = fBall.spd.fX > 0 ? &fPaddle1 : &fPaddle0;
264
265 SkScalar t, yIntercept;
266 std::tie(t, yIntercept) = find_yintercept(fBall.pos, fBall.spd, kBounds) ;
267
268 // The pitcher aims for a neutral/centered position.
269 pitcher->spd.fY = (kBounds.centerY() - pitcher->pos.fY) / t;
270
271 // The catcher goes for the ball. Duh.
272 catcher->spd.fY = (yIntercept - catcher->pos.fY) / t;
273 }
274
275 sk_sp<SkSVGDOM> fDom;
276 Object fPaddle0, fPaddle1, fBall;
277 SkRandom fRand;
278
279 SkMSec fLastTick = 0;
280 SkScalar fTimeScale = 1.0f;
281
282 typedef SampleView INHERITED;
283 };
284
285 static SkView* SVGPongFactory() { return new SVGPongView; }
286 static SkViewRegister reg(SVGPongFactory);
OLDNEW
« no previous file with comments | « gyp/samples.gypi ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698