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