Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google LLC |
| 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 "SkParticleAffector.h" |
| 9 | |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 10 | #include "SkContourMeasure.h" |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 11 | #include "SkCurve.h" |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 12 | #include "SkParsePath.h" |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 13 | #include "SkParticleData.h" |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 14 | #include "SkPath.h" |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 15 | #include "SkRandom.h" |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 16 | #include "SkTextUtils.h" |
| 17 | |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 18 | |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 19 | void SkParticleAffector::apply(const SkParticleUpdateParams& params, |
| 20 | SkParticleState ps[], int count) { |
Brian Osman | 1b20cd8 | 2019-02-25 14:15:02 -0500 | [diff] [blame] | 21 | if (fEnabled) { |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame] | 22 | this->onApply(params, ps, count); |
Brian Osman | 1b20cd8 | 2019-02-25 14:15:02 -0500 | [diff] [blame] | 23 | } |
| 24 | } |
| 25 | |
| 26 | void SkParticleAffector::visitFields(SkFieldVisitor* v) { |
| 27 | v->visit("Enabled", fEnabled); |
| 28 | } |
| 29 | |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 30 | class SkLinearVelocityAffector : public SkParticleAffector { |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 31 | public: |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 32 | SkLinearVelocityAffector(const SkCurve& angle = 0.0f, |
| 33 | const SkCurve& strength = 0.0f, |
Brian Osman | d5c57fe | 2019-02-22 11:48:18 -0500 | [diff] [blame] | 34 | bool force = true, |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 35 | SkParticleFrame frame = kWorld_ParticleFrame) |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 36 | : fAngle(angle) |
| 37 | , fStrength(strength) |
Brian Osman | d5c57fe | 2019-02-22 11:48:18 -0500 | [diff] [blame] | 38 | , fForce(force) |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 39 | , fFrame(frame) {} |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 40 | |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 41 | REFLECTED(SkLinearVelocityAffector, SkParticleAffector) |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 42 | |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 43 | void onApply(const SkParticleUpdateParams& params, SkParticleState ps[], int count) override { |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame] | 44 | for (int i = 0; i < count; ++i) { |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 45 | float angle = fAngle.eval(params, ps[i]); |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame] | 46 | SkScalar c_local, s_local = SkScalarSinCos(SkDegreesToRadians(angle), &c_local); |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 47 | SkVector heading = ps[i].getFrameHeading(static_cast<SkParticleFrame>(fFrame)); |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame] | 48 | SkScalar c = heading.fX * c_local - heading.fY * s_local; |
| 49 | SkScalar s = heading.fX * s_local + heading.fY * c_local; |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 50 | float strength = fStrength.eval(params, ps[i]); |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame] | 51 | SkVector force = { c * strength, s * strength }; |
| 52 | if (fForce) { |
| 53 | ps[i].fVelocity.fLinear += force * params.fDeltaTime; |
| 54 | } else { |
| 55 | ps[i].fVelocity.fLinear = force; |
| 56 | } |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 57 | } |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void visitFields(SkFieldVisitor* v) override { |
Brian Osman | 1b20cd8 | 2019-02-25 14:15:02 -0500 | [diff] [blame] | 61 | SkParticleAffector::visitFields(v); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 62 | v->visit("Force", fForce); |
Brian Osman | e5d532e | 2019-02-26 14:58:40 -0500 | [diff] [blame] | 63 | v->visit("Frame", fFrame, gParticleFrameMapping, SK_ARRAY_COUNT(gParticleFrameMapping)); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 64 | v->visit("Angle", fAngle); |
| 65 | v->visit("Strength", fStrength); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | private: |
| 69 | SkCurve fAngle; |
| 70 | SkCurve fStrength; |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 71 | bool fForce; |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 72 | int fFrame; |
| 73 | }; |
| 74 | |
| 75 | class SkAngularVelocityAffector : public SkParticleAffector { |
| 76 | public: |
| 77 | SkAngularVelocityAffector(const SkCurve& strength = 0.0f, bool force = true) |
| 78 | : fStrength(strength) |
| 79 | , fForce(force) {} |
| 80 | |
| 81 | REFLECTED(SkAngularVelocityAffector, SkParticleAffector) |
| 82 | |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 83 | void onApply(const SkParticleUpdateParams& params, SkParticleState ps[], int count) override { |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 84 | for (int i = 0; i < count; ++i) { |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 85 | float strength = fStrength.eval(params, ps[i]); |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 86 | if (fForce) { |
| 87 | ps[i].fVelocity.fAngular += strength * params.fDeltaTime; |
| 88 | } else { |
| 89 | ps[i].fVelocity.fAngular = strength; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void visitFields(SkFieldVisitor* v) override { |
| 95 | SkParticleAffector::visitFields(v); |
| 96 | v->visit("Force", fForce); |
| 97 | v->visit("Strength", fStrength); |
| 98 | } |
| 99 | |
| 100 | private: |
| 101 | SkCurve fStrength; |
| 102 | bool fForce; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | class SkPointForceAffector : public SkParticleAffector { |
| 106 | public: |
| 107 | SkPointForceAffector(SkPoint point = { 0.0f, 0.0f }, SkScalar constant = 0.0f, |
| 108 | SkScalar invSquare = 0.0f) |
| 109 | : fPoint(point), fConstant(constant), fInvSquare(invSquare) {} |
| 110 | |
| 111 | REFLECTED(SkPointForceAffector, SkParticleAffector) |
| 112 | |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 113 | void onApply(const SkParticleUpdateParams& params, SkParticleState ps[], int count) override { |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame] | 114 | for (int i = 0; i < count; ++i) { |
| 115 | SkVector toPoint = fPoint - ps[i].fPose.fPosition; |
| 116 | SkScalar lenSquare = toPoint.dot(toPoint); |
| 117 | toPoint.normalize(); |
| 118 | ps[i].fVelocity.fLinear += |
| 119 | toPoint * (fConstant + (fInvSquare / lenSquare)) * params.fDeltaTime; |
| 120 | } |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void visitFields(SkFieldVisitor* v) override { |
Brian Osman | 1b20cd8 | 2019-02-25 14:15:02 -0500 | [diff] [blame] | 124 | SkParticleAffector::visitFields(v); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 125 | v->visit("Point", fPoint); |
| 126 | v->visit("Constant", fConstant); |
| 127 | v->visit("InvSquare", fInvSquare); |
| 128 | } |
| 129 | |
| 130 | private: |
| 131 | SkPoint fPoint; |
| 132 | SkScalar fConstant; |
| 133 | SkScalar fInvSquare; |
| 134 | }; |
| 135 | |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 136 | class SkOrientationAffector : public SkParticleAffector { |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 137 | public: |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 138 | SkOrientationAffector(const SkCurve& angle = 0.0f, |
| 139 | SkParticleFrame frame = kLocal_ParticleFrame) |
| 140 | : fAngle(angle) |
| 141 | , fFrame(frame) {} |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 142 | |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 143 | REFLECTED(SkOrientationAffector, SkParticleAffector) |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 144 | |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 145 | void onApply(const SkParticleUpdateParams& params, SkParticleState ps[], int count) override { |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame] | 146 | for (int i = 0; i < count; ++i) { |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 147 | float angle = fAngle.eval(params, ps[i]); |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 148 | SkScalar c_local, s_local = SkScalarSinCos(SkDegreesToRadians(angle), &c_local); |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 149 | SkVector heading = ps[i].getFrameHeading(static_cast<SkParticleFrame>(fFrame)); |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 150 | ps[i].fPose.fHeading.set(heading.fX * c_local - heading.fY * s_local, |
| 151 | heading.fX * s_local + heading.fY * c_local); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 152 | } |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 153 | } |
| 154 | |
Brian Osman | 1b20cd8 | 2019-02-25 14:15:02 -0500 | [diff] [blame] | 155 | void visitFields(SkFieldVisitor *v) override { |
| 156 | SkParticleAffector::visitFields(v); |
Brian Osman | e5d532e | 2019-02-26 14:58:40 -0500 | [diff] [blame] | 157 | v->visit("Frame", fFrame, gParticleFrameMapping, SK_ARRAY_COUNT(gParticleFrameMapping)); |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 158 | v->visit("Angle", fAngle); |
Brian Osman | 1b20cd8 | 2019-02-25 14:15:02 -0500 | [diff] [blame] | 159 | } |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 160 | |
| 161 | private: |
| 162 | SkCurve fAngle; |
| 163 | int fFrame; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 164 | }; |
| 165 | |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 166 | class SkPositionInCircleAffector : public SkParticleAffector { |
| 167 | public: |
| 168 | SkPositionInCircleAffector(const SkCurve& x = 0.0f, const SkCurve& y = 0.0f, |
| 169 | const SkCurve& radius = 0.0f, bool setHeading = true) |
| 170 | : fX(x) |
| 171 | , fY(y) |
| 172 | , fRadius(radius) |
| 173 | , fSetHeading(setHeading) {} |
| 174 | |
| 175 | REFLECTED(SkPositionInCircleAffector, SkParticleAffector) |
| 176 | |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 177 | void onApply(const SkParticleUpdateParams& params, SkParticleState ps[], int count) override { |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 178 | for (int i = 0; i < count; ++i) { |
| 179 | SkVector v; |
| 180 | do { |
| 181 | v.fX = ps[i].fRandom.nextSScalar1(); |
| 182 | v.fY = ps[i].fRandom.nextSScalar1(); |
| 183 | } while (v.dot(v) > 1); |
| 184 | |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 185 | SkPoint center = { fX.eval(params, ps[i]), fY.eval(params, ps[i]) }; |
| 186 | SkScalar radius = fRadius.eval(params, ps[i]); |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 187 | ps[i].fPose.fPosition = center + (v * radius); |
| 188 | if (fSetHeading) { |
| 189 | if (!v.normalize()) { |
| 190 | v.set(0, -1); |
| 191 | } |
| 192 | ps[i].fPose.fHeading = v; |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | void visitFields(SkFieldVisitor* v) override { |
| 198 | SkParticleAffector::visitFields(v); |
| 199 | v->visit("SetHeading", fSetHeading); |
| 200 | v->visit("X", fX); |
| 201 | v->visit("Y", fY); |
| 202 | v->visit("Radius", fRadius); |
| 203 | } |
| 204 | |
| 205 | private: |
| 206 | SkCurve fX; |
| 207 | SkCurve fY; |
| 208 | SkCurve fRadius; |
| 209 | bool fSetHeading; |
| 210 | }; |
| 211 | |
| 212 | class SkPositionOnPathAffector : public SkParticleAffector { |
| 213 | public: |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 214 | SkPositionOnPathAffector(const char* path = "", bool setHeading = true, |
| 215 | SkParticleValue input = SkParticleValue()) |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 216 | : fPath(path) |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 217 | , fInput(input) |
| 218 | , fSetHeading(setHeading) { |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 219 | this->rebuild(); |
| 220 | } |
| 221 | |
| 222 | REFLECTED(SkPositionOnPathAffector, SkParticleAffector) |
| 223 | |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 224 | void onApply(const SkParticleUpdateParams& params, SkParticleState ps[], int count) override { |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 225 | if (fContours.empty()) { |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | for (int i = 0; i < count; ++i) { |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 230 | float t = fInput.eval(params, ps[i]); |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 231 | SkScalar len = fTotalLength * t; |
| 232 | int idx = 0; |
| 233 | while (idx < fContours.count() && len > fContours[idx]->length()) { |
| 234 | len -= fContours[idx++]->length(); |
| 235 | } |
| 236 | SkVector localXAxis; |
| 237 | if (!fContours[idx]->getPosTan(len, &ps[i].fPose.fPosition, &localXAxis)) { |
| 238 | ps[i].fPose.fPosition = { 0, 0 }; |
| 239 | localXAxis = { 1, 0 }; |
| 240 | } |
| 241 | if (fSetHeading) { |
| 242 | ps[i].fPose.fHeading.set(localXAxis.fY, -localXAxis.fX); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | void visitFields(SkFieldVisitor* v) override { |
| 248 | SkString oldPath = fPath; |
| 249 | |
| 250 | SkParticleAffector::visitFields(v); |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 251 | v->visit("Input", fInput); |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 252 | v->visit("SetHeading", fSetHeading); |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 253 | v->visit("Path", fPath); |
| 254 | |
| 255 | if (fPath != oldPath) { |
| 256 | this->rebuild(); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | private: |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 261 | SkString fPath; |
| 262 | SkParticleValue fInput; |
| 263 | bool fSetHeading; |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 264 | |
| 265 | void rebuild() { |
| 266 | SkPath path; |
| 267 | if (!SkParsePath::FromSVGString(fPath.c_str(), &path)) { |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | fTotalLength = 0; |
| 272 | fContours.reset(); |
| 273 | |
| 274 | SkContourMeasureIter iter(path, false); |
| 275 | while (auto contour = iter.next()) { |
| 276 | fContours.push_back(contour); |
| 277 | fTotalLength += contour->length(); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | // Cached |
| 282 | SkScalar fTotalLength; |
| 283 | SkTArray<sk_sp<SkContourMeasure>> fContours; |
| 284 | }; |
| 285 | |
| 286 | class SkPositionOnTextAffector : public SkParticleAffector { |
| 287 | public: |
| 288 | SkPositionOnTextAffector(const char* text = "", SkScalar fontSize = 96, bool setHeading = true, |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 289 | SkParticleValue input = SkParticleValue()) |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 290 | : fText(text) |
| 291 | , fFontSize(fontSize) |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 292 | , fInput(input) |
| 293 | , fSetHeading(setHeading) { |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 294 | this->rebuild(); |
| 295 | } |
| 296 | |
| 297 | REFLECTED(SkPositionOnTextAffector, SkParticleAffector) |
| 298 | |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 299 | void onApply(const SkParticleUpdateParams& params, SkParticleState ps[], int count) override { |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 300 | if (fContours.empty()) { |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | // TODO: Refactor to share code with PositionOnPathAffector |
| 305 | for (int i = 0; i < count; ++i) { |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 306 | float t = fInput.eval(params, ps[i]); |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 307 | SkScalar len = fTotalLength * t; |
| 308 | int idx = 0; |
| 309 | while (idx < fContours.count() && len > fContours[idx]->length()) { |
| 310 | len -= fContours[idx++]->length(); |
| 311 | } |
| 312 | SkVector localXAxis; |
| 313 | if (!fContours[idx]->getPosTan(len, &ps[i].fPose.fPosition, &localXAxis)) { |
| 314 | ps[i].fPose.fPosition = { 0, 0 }; |
| 315 | localXAxis = { 1, 0 }; |
| 316 | } |
| 317 | if (fSetHeading) { |
| 318 | ps[i].fPose.fHeading.set(localXAxis.fY, -localXAxis.fX); |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | void visitFields(SkFieldVisitor* v) override { |
| 324 | SkString oldText = fText; |
| 325 | SkScalar oldSize = fFontSize; |
| 326 | |
| 327 | SkParticleAffector::visitFields(v); |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 328 | v->visit("Input", fInput); |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 329 | v->visit("SetHeading", fSetHeading); |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 330 | v->visit("Text", fText); |
| 331 | v->visit("FontSize", fFontSize); |
| 332 | |
| 333 | if (fText != oldText || fFontSize != oldSize) { |
| 334 | this->rebuild(); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | private: |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 339 | SkString fText; |
| 340 | SkScalar fFontSize; |
| 341 | SkParticleValue fInput; |
| 342 | bool fSetHeading; |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 343 | |
| 344 | void rebuild() { |
| 345 | fTotalLength = 0; |
| 346 | fContours.reset(); |
| 347 | |
| 348 | if (fText.isEmpty()) { |
| 349 | return; |
| 350 | } |
| 351 | |
Kevin Lubick | 9663484 | 2019-03-05 14:09:24 -0500 | [diff] [blame^] | 352 | // Use the font manager's default font |
| 353 | SkFont font(nullptr, fFontSize); |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 354 | SkPath path; |
| 355 | SkTextUtils::GetPath(fText.c_str(), fText.size(), kUTF8_SkTextEncoding, 0, 0, font, &path); |
| 356 | SkContourMeasureIter iter(path, false); |
| 357 | while (auto contour = iter.next()) { |
| 358 | fContours.push_back(contour); |
| 359 | fTotalLength += contour->length(); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | // Cached |
| 364 | SkScalar fTotalLength; |
| 365 | SkTArray<sk_sp<SkContourMeasure>> fContours; |
| 366 | }; |
| 367 | |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 368 | class SkSizeAffector : public SkParticleAffector { |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 369 | public: |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 370 | SkSizeAffector(const SkCurve& curve = 1.0f) : fCurve(curve) {} |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 371 | |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 372 | REFLECTED(SkSizeAffector, SkParticleAffector) |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 373 | |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 374 | void onApply(const SkParticleUpdateParams& params, SkParticleState ps[], int count) override { |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame] | 375 | for (int i = 0; i < count; ++i) { |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 376 | ps[i].fPose.fScale = fCurve.eval(params, ps[i]); |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame] | 377 | } |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | void visitFields(SkFieldVisitor* v) override { |
Brian Osman | 1b20cd8 | 2019-02-25 14:15:02 -0500 | [diff] [blame] | 381 | SkParticleAffector::visitFields(v); |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 382 | v->visit("Curve", fCurve); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | private: |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 386 | SkCurve fCurve; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 387 | }; |
| 388 | |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 389 | class SkFrameAffector : public SkParticleAffector { |
| 390 | public: |
| 391 | SkFrameAffector(const SkCurve& curve = 1.0f) : fCurve(curve) {} |
| 392 | |
| 393 | REFLECTED(SkFrameAffector, SkParticleAffector) |
| 394 | |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 395 | void onApply(const SkParticleUpdateParams& params, SkParticleState ps[], int count) override { |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame] | 396 | for (int i = 0; i < count; ++i) { |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 397 | ps[i].fFrame = fCurve.eval(params, ps[i]); |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame] | 398 | } |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | void visitFields(SkFieldVisitor* v) override { |
Brian Osman | 1b20cd8 | 2019-02-25 14:15:02 -0500 | [diff] [blame] | 402 | SkParticleAffector::visitFields(v); |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 403 | v->visit("Curve", fCurve); |
| 404 | } |
| 405 | |
| 406 | private: |
| 407 | SkCurve fCurve; |
| 408 | }; |
| 409 | |
| 410 | class SkColorAffector : public SkParticleAffector { |
| 411 | public: |
| 412 | SkColorAffector(const SkColorCurve& curve = SkColor4f{ 1.0f, 1.0f, 1.0f, 1.0f }) |
| 413 | : fCurve(curve) {} |
| 414 | |
| 415 | REFLECTED(SkColorAffector, SkParticleAffector) |
| 416 | |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 417 | void onApply(const SkParticleUpdateParams& params, SkParticleState ps[], int count) override { |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame] | 418 | for (int i = 0; i < count; ++i) { |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 419 | ps[i].fColor = fCurve.eval(params, ps[i]); |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame] | 420 | } |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | void visitFields(SkFieldVisitor* v) override { |
Brian Osman | 1b20cd8 | 2019-02-25 14:15:02 -0500 | [diff] [blame] | 424 | SkParticleAffector::visitFields(v); |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 425 | v->visit("Curve", fCurve); |
| 426 | } |
| 427 | |
| 428 | private: |
| 429 | SkColorCurve fCurve; |
| 430 | }; |
| 431 | |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 432 | void SkParticleAffector::RegisterAffectorTypes() { |
| 433 | REGISTER_REFLECTED(SkParticleAffector); |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 434 | REGISTER_REFLECTED(SkLinearVelocityAffector); |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 435 | REGISTER_REFLECTED(SkAngularVelocityAffector); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 436 | REGISTER_REFLECTED(SkPointForceAffector); |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 437 | REGISTER_REFLECTED(SkOrientationAffector); |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 438 | REGISTER_REFLECTED(SkPositionInCircleAffector); |
| 439 | REGISTER_REFLECTED(SkPositionOnPathAffector); |
| 440 | REGISTER_REFLECTED(SkPositionOnTextAffector); |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 441 | REGISTER_REFLECTED(SkSizeAffector); |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 442 | REGISTER_REFLECTED(SkFrameAffector); |
| 443 | REGISTER_REFLECTED(SkColorAffector); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 444 | } |
| 445 | |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 446 | sk_sp<SkParticleAffector> SkParticleAffector::MakeLinearVelocity(const SkCurve& angle, |
| 447 | const SkCurve& strength, |
Brian Osman | d5c57fe | 2019-02-22 11:48:18 -0500 | [diff] [blame] | 448 | bool force, |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 449 | SkParticleFrame frame) { |
| 450 | return sk_sp<SkParticleAffector>(new SkLinearVelocityAffector(angle, strength, force, frame)); |
| 451 | } |
| 452 | |
| 453 | sk_sp<SkParticleAffector> SkParticleAffector::MakeAngularVelocity(const SkCurve& strength, |
| 454 | bool force) { |
| 455 | return sk_sp<SkParticleAffector>(new SkAngularVelocityAffector(strength, force)); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 456 | } |
| 457 | |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 458 | sk_sp<SkParticleAffector> SkParticleAffector::MakePointForce(SkPoint point, SkScalar constant, |
| 459 | SkScalar invSquare) { |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 460 | return sk_sp<SkParticleAffector>(new SkPointForceAffector(point, constant, invSquare)); |
| 461 | } |
| 462 | |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 463 | sk_sp<SkParticleAffector> SkParticleAffector::MakeOrientation(const SkCurve& angle, |
| 464 | SkParticleFrame frame) { |
| 465 | return sk_sp<SkParticleAffector>(new SkOrientationAffector(angle, frame)); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 466 | } |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 467 | |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 468 | sk_sp<SkParticleAffector> SkParticleAffector::MakeSize(const SkCurve& curve) { |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 469 | return sk_sp<SkParticleAffector>(new SkSizeAffector(curve)); |
| 470 | } |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 471 | |
| 472 | sk_sp<SkParticleAffector> SkParticleAffector::MakeFrame(const SkCurve& curve) { |
| 473 | return sk_sp<SkParticleAffector>(new SkFrameAffector(curve)); |
| 474 | } |
| 475 | |
| 476 | sk_sp<SkParticleAffector> SkParticleAffector::MakeColor(const SkColorCurve& curve) { |
| 477 | return sk_sp<SkParticleAffector>(new SkColorAffector(curve)); |
| 478 | } |