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