blob: 993af38be8000c4c303417b553f11cc8d166b3b2 [file] [log] [blame]
fmalitaca39d712016-08-12 13:17:11 -07001/*
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"
Florin Malitaca02c0a2018-01-22 16:48:22 -050013
14#include "SkSGColor.h"
15#include "SkSGDraw.h"
16#include "SkSGGroup.h"
17#include "SkSGPath.h"
18#include "SkSGRect.h"
19#include "SkSGScene.h"
20#include "SkSGTransform.h"
fmalitaca39d712016-08-12 13:17:11 -070021
22namespace {
23
24static const SkRect kBounds = SkRect::MakeLTRB(0.1f, 0.1f, 0.9f, 0.9f);
25static const SkSize kPaddleSize = SkSize::Make(0.03f, 0.1f);
26static const SkScalar kBallSize = 0.04f;
27static const SkScalar kShadowOpacity = 0.40f;
28static const SkScalar kShadowParallax = 0.04f;
29static const SkScalar kBackgroundStroke = 0.01f;
30static const uint32_t kBackgroundDashCount = 20;
31
32static const SkScalar kBallSpeedMax = 0.0020f;
33static const SkScalar kBallSpeedMin = 0.0005f;
34static const SkScalar kBallSpeedFuzz = 0.0002f;
35
36static const SkScalar kTimeScaleMin = 0.0f;
37static const SkScalar kTimeScaleMax = 5.0f;
38
39// Box the value within [min, max), by applying infinite reflection on the interval endpoints.
40SkScalar box_reflect(SkScalar v, SkScalar min, SkScalar max) {
41 const SkScalar intervalLen = max - min;
42 SkASSERT(intervalLen > 0);
43
44 // f(v) is periodic in 2 * intervalLen: one normal progression + one reflection
45 const SkScalar P = intervalLen * 2;
46 // relative to P origin
47 const SkScalar vP = v - min;
48 // map to [0, P)
49 const SkScalar vMod = (vP < 0) ? P - SkScalarMod(-vP, P) : SkScalarMod(vP, P);
50 // reflect if needed, to map to [0, intervalLen)
51 const SkScalar vInterval = vMod < intervalLen ? vMod : P - vMod;
52 // finally, reposition relative to min
53 return vInterval + min;
54}
55
56// Compute <t, y> for the trajectory intersection with the next vertical edge.
57std::tuple<SkScalar, SkScalar> find_yintercept(const SkPoint& pos, const SkVector& spd,
58 const SkRect& box) {
59 const SkScalar edge = spd.fX > 0 ? box.fRight : box.fLeft;
60 const SkScalar t = (edge - pos.fX) / spd.fX;
61 SkASSERT(t >= 0);
62 const SkScalar dY = t * spd.fY;
63
64 return std::make_tuple(t, box_reflect(pos.fY + dY, box.fTop, box.fBottom));
65}
66
Florin Malitaca02c0a2018-01-22 16:48:22 -050067void update_pos(const sk_sp<sksg::RRect>& rr, const SkPoint& pos) {
68 // TODO: position setters on RRect?
fmalitaca39d712016-08-12 13:17:11 -070069
Florin Malitaca02c0a2018-01-22 16:48:22 -050070 const auto r = rr->getRRect().rect();
71 const auto offsetX = pos.x() - r.x(),
72 offsetY = pos.y() - r.y();
73 rr->setRRect(rr->getRRect().makeOffset(offsetX, offsetY));
fmalitaca39d712016-08-12 13:17:11 -070074}
75
76} // anonymous ns
77
Florin Malitaca02c0a2018-01-22 16:48:22 -050078class PongView final : public SampleView {
fmalitaca39d712016-08-12 13:17:11 -070079public:
Florin Malitaca02c0a2018-01-22 16:48:22 -050080 PongView() = default;
fmalitaca39d712016-08-12 13:17:11 -070081
82protected:
83 void onOnceBeforeDraw() override {
84 const SkRect fieldBounds = kBounds.makeOutset(kBallSize / 2, kBallSize / 2);
85 const SkRRect ball = SkRRect::MakeOval(SkRect::MakeWH(kBallSize, kBallSize));
86 const SkRRect paddle = SkRRect::MakeRectXY(SkRect::MakeWH(kPaddleSize.width(),
87 kPaddleSize.height()),
88 kPaddleSize.width() / 2,
89 kPaddleSize.width() / 2);
90 fBall.initialize(ball,
fmalitaca39d712016-08-12 13:17:11 -070091 SkPoint::Make(kBounds.centerX(), kBounds.centerY()),
92 SkVector::Make(fRand.nextRangeScalar(kBallSpeedMin, kBallSpeedMax),
93 fRand.nextRangeScalar(kBallSpeedMin, kBallSpeedMax)));
94 fPaddle0.initialize(paddle,
fmalitaca39d712016-08-12 13:17:11 -070095 SkPoint::Make(fieldBounds.left() - kPaddleSize.width() / 2,
96 fieldBounds.centerY()),
97 SkVector::Make(0, 0));
98 fPaddle1.initialize(paddle,
fmalitaca39d712016-08-12 13:17:11 -070099 SkPoint::Make(fieldBounds.right() + kPaddleSize.width() / 2,
100 fieldBounds.centerY()),
101 SkVector::Make(0, 0));
102
103 // Background decoration.
104 SkPath bgPath;
105 bgPath.moveTo(kBounds.left() , fieldBounds.top());
106 bgPath.lineTo(kBounds.right(), fieldBounds.top());
107 bgPath.moveTo(kBounds.left() , fieldBounds.bottom());
108 bgPath.lineTo(kBounds.right(), fieldBounds.bottom());
109 // TODO: stroke-dash support would come in handy right about now.
110 for (uint32_t i = 0; i < kBackgroundDashCount; ++i) {
111 bgPath.moveTo(kBounds.centerX(),
112 kBounds.top() + (i + 0.25f) * kBounds.height() / kBackgroundDashCount);
113 bgPath.lineTo(kBounds.centerX(),
114 kBounds.top() + (i + 0.75f) * kBounds.height() / kBackgroundDashCount);
115 }
116
Florin Malitaca02c0a2018-01-22 16:48:22 -0500117 auto bg_path = sksg::Path::Make(bgPath);
118 auto bg_paint = sksg::Color::Make(SK_ColorBLACK);
119 bg_paint->setStyle(SkPaint::kStroke_Style);
120 bg_paint->setStrokeWidth(kBackgroundStroke);
fmalitaca39d712016-08-12 13:17:11 -0700121
Florin Malitaca02c0a2018-01-22 16:48:22 -0500122 auto ball_paint = sksg::Color::Make(SK_ColorGREEN),
123 paddle0_paint = sksg::Color::Make(SK_ColorBLUE),
124 paddle1_paint = sksg::Color::Make(SK_ColorRED),
125 shadow_paint = sksg::Color::Make(SK_ColorBLACK);
126 ball_paint->setAntiAlias(true);
127 paddle0_paint->setAntiAlias(true);
128 paddle1_paint->setAntiAlias(true);
129 shadow_paint->setAntiAlias(true);
130 shadow_paint->setOpacity(kShadowOpacity);
131
132 // Build the scene graph.
133 auto group = sksg::Group::Make();
134 group->addChild(sksg::Draw::Make(std::move(bg_path), std::move(bg_paint)));
135 group->addChild(sksg::Draw::Make(fPaddle0.shadowNode, shadow_paint));
136 group->addChild(sksg::Draw::Make(fPaddle1.shadowNode, shadow_paint));
137 group->addChild(sksg::Draw::Make(fBall.shadowNode, shadow_paint));
138 group->addChild(sksg::Draw::Make(fPaddle0.objectNode, paddle0_paint));
139 group->addChild(sksg::Draw::Make(fPaddle1.objectNode, paddle1_paint));
140 group->addChild(sksg::Draw::Make(fBall.objectNode, ball_paint));
fmalitaca39d712016-08-12 13:17:11 -0700141
142 // Handle everything in a normalized 1x1 space.
Florin Malitaca02c0a2018-01-22 16:48:22 -0500143 fContentMatrix = sksg::Matrix::Make(
144 SkMatrix::MakeRectToRect(SkRect::MakeWH(1, 1),
145 SkRect::MakeIWH(this->width(), this->height()),
146 SkMatrix::kFill_ScaleToFit));
147 auto root = sksg::Transform::Make(std::move(group), fContentMatrix);
Florin Malitacca86f32018-01-29 10:49:49 -0500148 fScene = sksg::Scene::Make(std::move(root), sksg::AnimatorList());
fmalitaca39d712016-08-12 13:17:11 -0700149
150 // Off we go.
151 this->updatePaddleStrategy();
152 }
153
154 bool onQuery(SkEvent* evt) override {
155 if (SampleCode::TitleQ(*evt)) {
Florin Malitaca02c0a2018-01-22 16:48:22 -0500156 SampleCode::TitleR(evt, "SGPong");
fmalitaca39d712016-08-12 13:17:11 -0700157 return true;
158 }
159
160 SkUnichar uni;
161 if (SampleCode::CharQ(*evt, &uni)) {
162 switch (uni) {
163 case '[':
164 fTimeScale = SkTPin(fTimeScale - 0.1f, kTimeScaleMin, kTimeScaleMax);
165 return true;
166 case ']':
167 fTimeScale = SkTPin(fTimeScale + 0.1f, kTimeScaleMin, kTimeScaleMax);
168 return true;
Florin Malitaca02c0a2018-01-22 16:48:22 -0500169 case 'I':
170 fShowInval = !fShowInval;
171 fScene->setShowInval(fShowInval);
172 return true;
fmalitaca39d712016-08-12 13:17:11 -0700173 default:
174 break;
175 }
176 }
177 return this->INHERITED::onQuery(evt);
178 }
179
180 void onSizeChange() override {
Florin Malitaca02c0a2018-01-22 16:48:22 -0500181 if (fContentMatrix) {
182 fContentMatrix->setMatrix(SkMatrix::MakeRectToRect(SkRect::MakeWH(1, 1),
183 SkRect::MakeIWH(this->width(),
184 this->height()),
185 SkMatrix::kFill_ScaleToFit));
fmalitaca39d712016-08-12 13:17:11 -0700186 }
187
188 this->INHERITED::onSizeChange();
189 }
190
191 void onDrawContent(SkCanvas* canvas) override {
Florin Malitaca02c0a2018-01-22 16:48:22 -0500192 fScene->render(canvas);
fmalitaca39d712016-08-12 13:17:11 -0700193 }
194
195 bool onAnimate(const SkAnimTimer& timer) override {
Florin Malitad75f3f92017-12-01 17:13:22 -0500196 // onAnimate may fire before the first draw.
Florin Malitaca02c0a2018-01-22 16:48:22 -0500197 if (fScene) {
Florin Malitad75f3f92017-12-01 17:13:22 -0500198 SkScalar dt = (timer.msec() - fLastTick) * fTimeScale;
199 fLastTick = timer.msec();
fmalitaca39d712016-08-12 13:17:11 -0700200
Florin Malitad75f3f92017-12-01 17:13:22 -0500201 fPaddle0.posTick(dt);
202 fPaddle1.posTick(dt);
203 fBall.posTick(dt);
fmalitaca39d712016-08-12 13:17:11 -0700204
Florin Malitad75f3f92017-12-01 17:13:22 -0500205 this->enforceConstraints();
fmalitaca39d712016-08-12 13:17:11 -0700206
Florin Malitad75f3f92017-12-01 17:13:22 -0500207 fPaddle0.updateDom();
208 fPaddle1.updateDom();
209 fBall.updateDom();
210 }
fmalitaca39d712016-08-12 13:17:11 -0700211 return true;
212 }
213
214private:
215 struct Object {
Florin Malitaca02c0a2018-01-22 16:48:22 -0500216 void initialize(const SkRRect& rrect, const SkPoint& p, const SkVector& s) {
217 objectNode = sksg::RRect::Make(rrect);
218 shadowNode = sksg::RRect::Make(rrect);
fmalitaca39d712016-08-12 13:17:11 -0700219
220 pos = p;
221 spd = s;
222 size = SkSize::Make(rrect.width(), rrect.height());
223 }
224
225 void posTick(SkScalar dt) {
226 pos += spd * dt;
227 }
228
229 void updateDom() {
230 const SkPoint corner = pos - SkPoint::Make(size.width() / 2, size.height() / 2);
Florin Malitaca02c0a2018-01-22 16:48:22 -0500231 update_pos(objectNode, corner);
fmalitaca39d712016-08-12 13:17:11 -0700232
233 // Simulate parallax shadow for a centered light source.
234 SkPoint shadowOffset = pos - SkPoint::Make(kBounds.centerX(), kBounds.centerY());
235 shadowOffset.scale(kShadowParallax);
236 const SkPoint shadowCorner = corner + shadowOffset;
237
Florin Malitaca02c0a2018-01-22 16:48:22 -0500238 update_pos(shadowNode, shadowCorner);
fmalitaca39d712016-08-12 13:17:11 -0700239 }
240
Florin Malitaca02c0a2018-01-22 16:48:22 -0500241 sk_sp<sksg::RRect> objectNode,
242 shadowNode;
243 SkPoint pos;
244 SkVector spd;
245 SkSize size;
fmalitaca39d712016-08-12 13:17:11 -0700246 };
247
248 void enforceConstraints() {
249 // Perfect vertical reflection.
250 if (fBall.pos.fY < kBounds.fTop || fBall.pos.fY >= kBounds.fBottom) {
251 fBall.spd.fY = -fBall.spd.fY;
252 fBall.pos.fY = box_reflect(fBall.pos.fY, kBounds.fTop, kBounds.fBottom);
253 }
254
255 // Horizontal bounce - introduces a speed fuzz.
256 if (fBall.pos.fX < kBounds.fLeft || fBall.pos.fX >= kBounds.fRight) {
257 fBall.spd.fX = this->fuzzBallSpeed(-fBall.spd.fX);
258 fBall.spd.fY = this->fuzzBallSpeed(fBall.spd.fY);
259 fBall.pos.fX = box_reflect(fBall.pos.fX, kBounds.fLeft, kBounds.fRight);
260 this->updatePaddleStrategy();
261 }
262 }
263
264 SkScalar fuzzBallSpeed(SkScalar spd) {
265 // The speed limits are absolute values.
266 const SkScalar sign = spd >= 0 ? 1.0f : -1.0f;
267 const SkScalar fuzzed = fabs(spd) + fRand.nextRangeScalar(-kBallSpeedFuzz, kBallSpeedFuzz);
268
269 return sign * SkTPin(fuzzed, kBallSpeedMin, kBallSpeedMax);
270 }
271
272 void updatePaddleStrategy() {
273 Object* pitcher = fBall.spd.fX > 0 ? &fPaddle0 : &fPaddle1;
274 Object* catcher = fBall.spd.fX > 0 ? &fPaddle1 : &fPaddle0;
275
276 SkScalar t, yIntercept;
277 std::tie(t, yIntercept) = find_yintercept(fBall.pos, fBall.spd, kBounds);
278
279 // The pitcher aims for a neutral/centered position.
280 pitcher->spd.fY = (kBounds.centerY() - pitcher->pos.fY) / t;
281
282 // The catcher goes for the ball. Duh.
283 catcher->spd.fY = (yIntercept - catcher->pos.fY) / t;
284 }
285
Florin Malitaca02c0a2018-01-22 16:48:22 -0500286 std::unique_ptr<sksg::Scene> fScene;
287 sk_sp<sksg::Matrix> fContentMatrix;
288 Object fPaddle0, fPaddle1, fBall;
289 SkRandom fRand;
fmalitaca39d712016-08-12 13:17:11 -0700290
Florin Malitaca02c0a2018-01-22 16:48:22 -0500291 SkMSec fLastTick = 0;
292 SkScalar fTimeScale = 1.0f;
293 bool fShowInval = false;
fmalitaca39d712016-08-12 13:17:11 -0700294
295 typedef SampleView INHERITED;
296};
297
Florin Malitaca02c0a2018-01-22 16:48:22 -0500298static SkView* PongFactory() { return new PongView; }
299static SkViewRegister reg(PongFactory);