react-native-nitro-input 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/CHANGELOG.md +305 -3
  2. package/NitroInput.podspec +4 -4
  3. package/README.md +378 -53
  4. package/android/CMakeLists.txt +5 -3
  5. package/android/src/main/cpp/JOutlineGeometry.hpp +1 -1
  6. package/android/src/main/cpp/JReflowEngine.cpp +132 -0
  7. package/android/src/main/cpp/{JMorphEngine.hpp → JReflowEngine.hpp} +7 -7
  8. package/android/src/main/cpp/JRollingEngine.cpp +222 -0
  9. package/android/src/main/cpp/JRollingEngine.hpp +68 -0
  10. package/android/src/main/cpp/cpp-adapter.cpp +4 -2
  11. package/android/src/main/java/com/margelo/nitro/nitroinput/AmountFormatter.kt +1 -1
  12. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputView.kt +1 -1
  13. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberView.kt +471 -0
  14. package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputPackage.kt +2 -1
  15. package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputView.kt +38 -16
  16. package/android/src/main/java/com/margelo/nitro/nitroinput/NitroNumberView.kt +1741 -0
  17. package/android/src/main/java/com/margelo/nitro/nitroinput/{MorphEngine.kt → ReflowEngine.kt} +2 -2
  18. package/android/src/main/java/com/margelo/nitro/nitroinput/RollingEngine.kt +59 -0
  19. package/cpp/AmountFormatter.cpp +5 -0
  20. package/cpp/AmountFormatter.hpp +10 -2
  21. package/cpp/{MorphEngine.cpp → ReflowEngine.cpp} +52 -47
  22. package/cpp/{MorphEngine.hpp → ReflowEngine.hpp} +12 -4
  23. package/cpp/RollingEngine.cpp +1224 -0
  24. package/cpp/RollingEngine.hpp +468 -0
  25. package/cpp/SwiftNonCopyable.hpp +22 -0
  26. package/cpp/worklets/NitroInputWorkletsBridge.cpp +3 -3
  27. package/cpp/worklets/NitroInputWorkletsBridge.hpp +1 -1
  28. package/ios/{HybridMorphInputView.swift → HybridNitroInputView.swift} +1 -1
  29. package/ios/HybridNitroNumberView.swift +439 -0
  30. package/ios/NitroInputView.swift +69 -45
  31. package/ios/NitroNumberView.swift +1934 -0
  32. package/lib/commonjs/nitrogen/generated/shared/json/NitroNumberViewConfig.json +55 -0
  33. package/lib/commonjs/src/NitroInput.js +21 -52
  34. package/lib/commonjs/src/NitroNumber.js +139 -0
  35. package/lib/commonjs/src/index.js +4 -3
  36. package/lib/commonjs/src/specs/NitroNumber.nitro.js +2 -0
  37. package/lib/commonjs/src/styleHelpers.js +36 -0
  38. package/lib/module/nitrogen/generated/shared/json/NitroNumberViewConfig.json +55 -0
  39. package/lib/module/src/NitroInput.js +12 -43
  40. package/lib/module/src/NitroNumber.js +100 -0
  41. package/lib/module/src/index.js +1 -1
  42. package/lib/module/src/specs/NitroNumber.nitro.js +1 -0
  43. package/lib/module/src/styleHelpers.js +32 -0
  44. package/lib/typescript/src/NitroInput.d.ts +28 -21
  45. package/lib/typescript/src/NitroNumber.d.ts +211 -0
  46. package/lib/typescript/src/index.d.ts +3 -2
  47. package/lib/typescript/src/specs/NitroInput.nitro.d.ts +9 -9
  48. package/lib/typescript/src/specs/NitroNumber.nitro.d.ts +238 -0
  49. package/lib/typescript/src/styleHelpers.d.ts +3 -0
  50. package/nitro.json +10 -0
  51. package/nitrogen/generated/android/NitroInput+autolinking.cmake +4 -0
  52. package/nitrogen/generated/android/NitroInputOnLoad.cpp +20 -0
  53. package/nitrogen/generated/android/c++/JFunc_void.hpp +75 -0
  54. package/nitrogen/generated/android/c++/JFunc_void_double_double.hpp +10 -10
  55. package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.cpp +536 -0
  56. package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.hpp +156 -0
  57. package/nitrogen/generated/android/c++/JNitroNumberAffixAlign.hpp +64 -0
  58. package/nitrogen/generated/android/c++/JNitroNumberDirection.hpp +61 -0
  59. package/nitrogen/generated/android/c++/JNitroNumberEasing.hpp +67 -0
  60. package/nitrogen/generated/android/c++/JNitroNumberRevealStyle.hpp +58 -0
  61. package/nitrogen/generated/android/c++/JNitroNumberTextAlign.hpp +64 -0
  62. package/nitrogen/generated/android/c++/JNitroNumberTransition.hpp +61 -0
  63. package/nitrogen/generated/android/c++/views/JHybridNitroNumberViewStateUpdater.cpp +299 -0
  64. package/nitrogen/generated/android/c++/views/JHybridNitroNumberViewStateUpdater.hpp +54 -0
  65. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/Func_void.kt +78 -0
  66. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/Func_void_double_double.kt +9 -9
  67. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroNumberViewSpec.kt +363 -0
  68. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberAffixAlign.kt +25 -0
  69. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberDirection.kt +24 -0
  70. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberEasing.kt +26 -0
  71. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberRevealStyle.kt +23 -0
  72. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberTextAlign.kt +25 -0
  73. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberTransition.kt +24 -0
  74. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/views/HybridNitroNumberViewManager.kt +96 -0
  75. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/views/HybridNitroNumberViewStateUpdater.kt +23 -0
  76. package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.cpp +25 -0
  77. package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.hpp +248 -0
  78. package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Umbrella.hpp +23 -0
  79. package/nitrogen/generated/ios/NitroInputAutolinking.mm +8 -0
  80. package/nitrogen/generated/ios/NitroInputAutolinking.swift +12 -0
  81. package/nitrogen/generated/ios/c++/HybridNitroNumberViewSpecSwift.cpp +11 -0
  82. package/nitrogen/generated/ios/c++/HybridNitroNumberViewSpecSwift.hpp +432 -0
  83. package/nitrogen/generated/ios/c++/views/HybridNitroNumberViewComponent.mm +414 -0
  84. package/nitrogen/generated/ios/swift/Func_void.swift +46 -0
  85. package/nitrogen/generated/ios/swift/Func_void_double_double.swift +5 -5
  86. package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec.swift +102 -0
  87. package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec_cxx.swift +1243 -0
  88. package/nitrogen/generated/ios/swift/NitroNumberAffixAlign.swift +48 -0
  89. package/nitrogen/generated/ios/swift/NitroNumberDirection.swift +44 -0
  90. package/nitrogen/generated/ios/swift/NitroNumberEasing.swift +52 -0
  91. package/nitrogen/generated/ios/swift/NitroNumberRevealStyle.swift +40 -0
  92. package/nitrogen/generated/ios/swift/NitroNumberTextAlign.swift +48 -0
  93. package/nitrogen/generated/ios/swift/NitroNumberTransition.swift +44 -0
  94. package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.cpp +115 -0
  95. package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.hpp +175 -0
  96. package/nitrogen/generated/shared/c++/NitroNumberAffixAlign.hpp +84 -0
  97. package/nitrogen/generated/shared/c++/NitroNumberDirection.hpp +80 -0
  98. package/nitrogen/generated/shared/c++/NitroNumberEasing.hpp +88 -0
  99. package/nitrogen/generated/shared/c++/NitroNumberRevealStyle.hpp +76 -0
  100. package/nitrogen/generated/shared/c++/NitroNumberTextAlign.hpp +84 -0
  101. package/nitrogen/generated/shared/c++/NitroNumberTransition.hpp +80 -0
  102. package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.cpp +124 -0
  103. package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.hpp +227 -0
  104. package/nitrogen/generated/shared/json/NitroNumberViewConfig.json +55 -0
  105. package/package.json +15 -7
  106. package/src/NitroInput.tsx +37 -60
  107. package/src/NitroNumber.tsx +481 -0
  108. package/src/index.ts +20 -6
  109. package/src/specs/NitroInput.nitro.ts +9 -9
  110. package/src/specs/NitroNumber.nitro.ts +259 -0
  111. package/src/styleHelpers.ts +32 -0
  112. package/android/src/main/cpp/JMorphEngine.cpp +0 -132
  113. package/lib/commonjs/src/MorphInput.js +0 -42
  114. package/lib/module/src/MorphInput.js +0 -6
  115. package/lib/typescript/src/MorphInput.d.ts +0 -18
  116. package/src/MorphInput.tsx +0 -30
@@ -0,0 +1,1224 @@
1
+ //
2
+ // RollingEngine.cpp
3
+ // NitroInput
4
+ //
5
+
6
+ #include "RollingEngine.hpp"
7
+
8
+ #include <algorithm>
9
+ #include <cmath>
10
+
11
+ namespace margelo::nitro::nitroinput {
12
+
13
+ namespace {
14
+
15
+ constexpr int kMaxPowerCount = 18;
16
+ /// Upper bound of |value| × 10^fractionDigits, shared by `setValue` and
17
+ /// `animateTo` so a jump and a roll to the same figure show the same digits.
18
+ /// 10^17 still fits the 18 wheels; beyond 2^53 a double has no exact integers
19
+ /// anyway.
20
+ constexpr double kMaxMagnitude = 1e17;
21
+ constexpr double kLoadingFadeSeconds = 0.25;
22
+ constexpr double kPi = 3.14159265358979323846;
23
+
24
+ // Jackpot reveal. The count is a slot's win tally, a rank at a time (US
25
+ // 9,495,843 sets seconds per win rank; US 9,111,423 derives the speed from
26
+ // the remaining count, so it slows into the final value, and shows the win
27
+ // meter enlarged during a big win's increment): the counter winds up out of
28
+ // the rank's start, runs at a constant rate (the low digits blur) and crawls
29
+ // into the rank's boundary so the milestone is readable as it lands.
30
+ // `kTallyRampIn` / `kTallyRampOut` are the fractions of a tier spent winding
31
+ // up and crawling; the final tier crawls longer. The figure grows over the
32
+ // count (`revealGrow_`), and every punch is one overshoot that settles back
33
+ // without ever dipping under the resting size (a spring that rang out under
34
+ // the size read as the figure shrinking): a pulse peaking `kPunchPeakSeconds`
35
+ // after the kick and gone by the tail.
36
+ constexpr double kTallyRampIn = 0.25;
37
+ constexpr double kTallyRampOut = 0.35;
38
+ constexpr double kTallyFinalRampOut = 0.45;
39
+ constexpr double kPunchPeakSeconds = 0.1;
40
+ constexpr double kRevealTailSeconds = 0.8;
41
+ /// A milestone punch relative to the landing pop.
42
+ constexpr double kMilestonePunch = 0.75;
43
+
44
+ // Spin-style reels. A reel free-spins at `kReelSpeed` digits per second, the
45
+ // brake takes `kReelBrakeSeconds` to bring it from full speed to a standstill
46
+ // exactly on its digit (a Hermite curve with the spin velocity going in and
47
+ // zero coming out, over 4–14 digits of travel so it only ever decelerates),
48
+ // and the lock overshoots by `kReelBounceDigits` for `kReelBounceSeconds`,
49
+ // the mechanical clunk of a reel catching its stop. The first reel locks no
50
+ // earlier than `kReelMinLead` of the duration so every reel is seen spinning,
51
+ // and the last locks a clunk short of the end so the whole figure is still by
52
+ // the duration — otherwise the rightmost reel hops on alone after every other
53
+ // digit has stopped, which reads as a glitch rather than as machinery.
54
+ constexpr double kReelSpeed = 24;
55
+ constexpr double kReelBrakeSeconds = 0.5;
56
+ constexpr double kReelBounceDigits = 0.15;
57
+ constexpr double kReelBounceSeconds = 0.2;
58
+ constexpr double kReelMinLead = 0.3;
59
+
60
+ const uint64_t kPow10[20] = {
61
+ 1ULL,
62
+ 10ULL,
63
+ 100ULL,
64
+ 1000ULL,
65
+ 10000ULL,
66
+ 100000ULL,
67
+ 1000000ULL,
68
+ 10000000ULL,
69
+ 100000000ULL,
70
+ 1000000000ULL,
71
+ 10000000000ULL,
72
+ 100000000000ULL,
73
+ 1000000000000ULL,
74
+ 10000000000000ULL,
75
+ 100000000000000ULL,
76
+ 1000000000000000ULL,
77
+ 10000000000000000ULL,
78
+ 100000000000000000ULL,
79
+ 1000000000000000000ULL,
80
+ 10000000000000000000ULL,
81
+ };
82
+
83
+ double clamp01(double x) {
84
+ return std::min(1.0, std::max(0.0, x));
85
+ }
86
+
87
+ } // namespace
88
+
89
+ // MARK: - Target
90
+
91
+ int RollingEngine::Target::digit(int power) const {
92
+ if (power < 0 || power >= 20) {
93
+ return 0;
94
+ }
95
+ return static_cast<int>((magnitude / kPow10[power]) % 10);
96
+ }
97
+
98
+ RollingEngine::RollingEngine() = default;
99
+ RollingEngine::~RollingEngine() = default;
100
+ RollingEngine::RollingEngine(const RollingEngine&) = default;
101
+ RollingEngine& RollingEngine::operator=(const RollingEngine&) = default;
102
+ RollingEngine::RollingEngine(RollingEngine&&) noexcept = default;
103
+ RollingEngine& RollingEngine::operator=(RollingEngine&&) noexcept = default;
104
+
105
+ int RollingEngine::digitCount(uint64_t n) {
106
+ int count = 1;
107
+ while (n >= 10) {
108
+ n /= 10;
109
+ count += 1;
110
+ }
111
+ return count;
112
+ }
113
+
114
+ double RollingEngine::wrap(double x) {
115
+ double r = std::fmod(x, 10.0);
116
+ return r < 0 ? r + 10.0 : r;
117
+ }
118
+
119
+ RollingEngine::Target RollingEngine::makeTarget(double value) const {
120
+ double scaled = std::round(std::fabs(value) * static_cast<double>(kPow10[fractionDigits_]));
121
+ if (!std::isfinite(scaled)) {
122
+ scaled = 0;
123
+ }
124
+ uint64_t magnitude = static_cast<uint64_t>(std::min(scaled, kMaxMagnitude));
125
+ uint64_t integerPart = magnitude / kPow10[fractionDigits_];
126
+ int intDigits = std::max(minimumIntegerDigits_, digitCount(integerPart));
127
+ return Target{magnitude, value < 0 && magnitude > 0, std::min(kMaxPowerCount, intDigits + fractionDigits_)};
128
+ }
129
+
130
+ void RollingEngine::settle(const Target& target) {
131
+ target_ = target;
132
+ settledPowerCount_ = target.powerCount;
133
+ settledNegative_ = target.negative;
134
+ }
135
+
136
+ // MARK: - Configuration
137
+
138
+ void RollingEngine::setFormat(int fractionDigits, int minimumIntegerDigits) {
139
+ int fd = std::min(9, std::max(0, fractionDigits));
140
+ int minInt = std::min(15, std::max(1, minimumIntegerDigits));
141
+ if (fd == fractionDigits_ && minInt == minimumIntegerDigits_) {
142
+ return;
143
+ }
144
+ fractionDigits_ = fd;
145
+ minimumIntegerDigits_ = minInt;
146
+ trailingPad_ = 0;
147
+ decimalFactor_ = fd > 0 ? 1.0 : 0.0;
148
+ if (hasShownValue_) {
149
+ if (reveal_.holding) {
150
+ holdReveal(targetValue_);
151
+ } else {
152
+ snap(makeTarget(targetValue_));
153
+ }
154
+ }
155
+ }
156
+
157
+ void RollingEngine::changeFormat(int fractionDigits, int minimumIntegerDigits, double now) {
158
+ const int fd = std::min(9, std::max(0, fractionDigits));
159
+ const int minInt = std::min(15, std::max(1, minimumIntegerDigits));
160
+ if (fd == fractionDigits_ || !hasShownValue_ || reveal_.active || reveal_.holding || duration_ <= 0 || reduceMotion_) {
161
+ setFormat(fractionDigits, minimumIntegerDigits);
162
+ return;
163
+ }
164
+ if (trailingPad_ > 0) {
165
+ // Columns from the last change are still closing (a currency switched
166
+ // again mid-switch): they go now, nearly closed, and this change plays
167
+ // from what is left. Snapped, the figure showed the old amount in the
168
+ // new format ("¥838,712.00") before the new one arrived.
169
+ const size_t pad = static_cast<size_t>(trailingPad_);
170
+ auto drop = [pad](auto& v) { v.erase(v.begin(), v.begin() + static_cast<std::ptrdiff_t>(std::min(pad, v.size()))); };
171
+ drop(wheels_);
172
+ drop(flashes_);
173
+ if (transition_.active) {
174
+ drop(transition_.wheels);
175
+ drop(transition_.delays);
176
+ }
177
+ trailingPad_ = 0;
178
+ }
179
+ const int display = std::max(fractionDigits_, fd);
180
+ const size_t added = static_cast<size_t>(display - fractionDigits_);
181
+ if (added > 0) {
182
+ // New decimal columns open below the ones on screen, blank: the columns
183
+ // keep their place value, so a transition in flight keeps its wheels too.
184
+ wheels_.insert(wheels_.begin(), added, Wheel{-1.0, 0.0, true, false});
185
+ if (flashes_.size() > 0) {
186
+ flashes_.insert(flashes_.begin(), added, Flash{});
187
+ }
188
+ if (transition_.active) {
189
+ transition_.wheels.insert(transition_.wheels.begin(), added, WheelTransition{});
190
+ transition_.delays.insert(transition_.delays.begin(), added, 0.0);
191
+ }
192
+ }
193
+ fractionDigits_ = fd;
194
+ minimumIntegerDigits_ = minInt;
195
+ // Decimal columns the format no longer has: they close where they are.
196
+ trailingPad_ = display - fd;
197
+ // The same value in the new format, as a change.
198
+ animateTo(targetValue_, now);
199
+ }
200
+
201
+ void RollingEngine::setTiming(double durationSeconds, int easing, double bounce, double staggerSeconds, int direction) {
202
+ duration_ = std::max(0.0, durationSeconds);
203
+ easing_ = easing;
204
+ bounce_ = bounce;
205
+ stagger_ = std::max(0.0, staggerSeconds);
206
+ direction_ = direction;
207
+ }
208
+
209
+ void RollingEngine::setTransition(int transition) {
210
+ transitionStyle_ = transition >= 0 && transition <= 2 ? transition : 0;
211
+ }
212
+
213
+ void RollingEngine::setFlash(double seconds) {
214
+ flashSeconds_ = std::max(0.0, seconds);
215
+ }
216
+
217
+ void RollingEngine::setPopOnChange(double overshoot) {
218
+ popOnChange_ = clamp01(overshoot);
219
+ }
220
+
221
+ void RollingEngine::setReduceMotion(bool reduceMotion) {
222
+ reduceMotion_ = reduceMotion;
223
+ }
224
+
225
+ // MARK: - Commands
226
+
227
+ void RollingEngine::endTransition() {
228
+ transition_.active = false;
229
+ trailingPad_ = 0;
230
+ decimalFactor_ = fractionDigits_ > 0 ? 1.0 : 0.0;
231
+ }
232
+
233
+ void RollingEngine::setValue(double value) {
234
+ endTransition();
235
+ cancelReveal();
236
+ targetValue_ = value;
237
+ hasShownValue_ = true;
238
+
239
+ const int fd = fractionDigits_;
240
+ double scaled = std::fabs(value) * static_cast<double>(kPow10[fd]);
241
+ if (!std::isfinite(scaled)) {
242
+ scaled = 0;
243
+ }
244
+ scaled = std::min(scaled, kMaxMagnitude);
245
+ const double whole = std::floor(scaled);
246
+ const uint64_t integerPart = static_cast<uint64_t>(whole) / kPow10[fd];
247
+ const int needed = std::min(kMaxPowerCount, std::max(minimumIntegerDigits_, digitCount(integerPart)) + fd);
248
+
249
+ wheels_.clear();
250
+ wheels_.reserve(static_cast<size_t>(needed) + 1);
251
+ for (int power = 0; power <= needed; power++) {
252
+ const double p10 = static_cast<double>(kPow10[power]);
253
+ const double digit = std::fmod(std::floor(scaled / p10), 10.0);
254
+ double carry;
255
+ if (power == 0) {
256
+ carry = scaled - whole;
257
+ } else {
258
+ // A wheel only turns while every lower wheel is on its way from 9 to 0.
259
+ carry = clamp01(std::fmod(scaled, p10) - (p10 - 1));
260
+ }
261
+ if (power == needed) {
262
+ // The next higher wheel emerges (blank → 1) while the carry is in progress.
263
+ if (carry <= 0) {
264
+ break;
265
+ }
266
+ wheels_.push_back(Wheel{carry, carry, false, true});
267
+ } else {
268
+ wheels_.push_back(Wheel{digit + carry, 1.0, false, false});
269
+ }
270
+ }
271
+ signFactor_ = value < 0 ? std::min(1.0, scaled) : 0.0;
272
+ Target target = makeTarget(value);
273
+ target.powerCount = static_cast<int>(wheels_.size());
274
+ target.negative = value < 0;
275
+ settle(target);
276
+ }
277
+
278
+ void RollingEngine::animateTo(double value, double now) {
279
+ cancelReveal();
280
+ const double previous = targetValue_;
281
+ targetValue_ = value;
282
+ const Target target = makeTarget(value);
283
+ bool increasing;
284
+ switch (direction_) {
285
+ case 1:
286
+ increasing = true;
287
+ break;
288
+ case 2:
289
+ increasing = false;
290
+ break;
291
+ default:
292
+ increasing = value >= previous;
293
+ break;
294
+ }
295
+ if (!hasShownValue_ || duration_ <= 0 || reduceMotion_) {
296
+ // A snap changes the digits at once; the flash and the pop still say so.
297
+ const bool first = !hasShownValue_;
298
+ std::vector<int> before;
299
+ for (const Wheel& w : wheels_) {
300
+ before.push_back(shownGlyph(w));
301
+ }
302
+ snap(target);
303
+ if (!first) {
304
+ flashes_.resize(wheels_.size());
305
+ for (size_t i = 0; i < wheels_.size(); i++) {
306
+ const int was = i < before.size() ? before[i] : -1;
307
+ if (was != shownGlyph(wheels_[i])) {
308
+ flashes_[i] = Flash{now, 0, increasing};
309
+ }
310
+ }
311
+ if (popOnChange_ > 0) {
312
+ popStart_ = now;
313
+ }
314
+ applyEffects(now);
315
+ }
316
+ return;
317
+ }
318
+ const int mandatory = minimumIntegerDigits_ + fractionDigits_;
319
+ const int currentCount = static_cast<int>(wheels_.size());
320
+ const int count = std::max(currentCount, target.powerCount + trailingPad_);
321
+
322
+ Transition next;
323
+ next.active = true;
324
+ next.start = now;
325
+ // The numeric transition rings its position spring out past the duration.
326
+ next.duration = transitionStyle_ == 1 ? duration_ * kNumericTail : duration_;
327
+ next.fromMotion = transition_.active;
328
+ next.style = transitionStyle_;
329
+ next.numeric = transitionStyle_ != 0;
330
+ next.wheels.reserve(static_cast<size_t>(count));
331
+ next.finals.reserve(static_cast<size_t>(target.powerCount));
332
+ next.delays.reserve(static_cast<size_t>(count));
333
+
334
+ if (next.numeric) {
335
+ planNumeric(next, target, increasing, count, mandatory, now);
336
+ } else {
337
+ planRoll(next, target, increasing, count, mandatory);
338
+ }
339
+
340
+ // Stagger: a roll cascades from the least significant wheel up, like a
341
+ // carry. The numeric transition cascades from the leftmost digit to the
342
+ // right, the way the effect moves on iOS, and only across the wheels that
343
+ // change: a units digit ticking over does not wait for three columns that
344
+ // stay put.
345
+ std::vector<int> ranks(static_cast<size_t>(count), 0);
346
+ int changing = 0;
347
+ if (next.numeric) {
348
+ for (int power = count - 1; power >= 0; power--) {
349
+ const WheelTransition& wt = next.wheels[static_cast<size_t>(power)];
350
+ if (wt.from.blend < 1 && !wt.continues) {
351
+ ranks[static_cast<size_t>(power)] = changing++;
352
+ }
353
+ }
354
+ } else {
355
+ for (int power = 0; power < count; power++) {
356
+ ranks[static_cast<size_t>(power)] = power;
357
+ }
358
+ }
359
+ for (int power = 0; power < count; power++) {
360
+ // When re-targeting mid-roll, a wheel that hasn't started yet keeps its
361
+ // original start time instead of being pushed back again, so rapid
362
+ // updates can't starve it.
363
+ // A roll's stagger is per wheel; the numeric cascade's is the whole
364
+ // span, shared out over the columns that change.
365
+ double delay = next.numeric
366
+ ? (changing > 1 ? stagger_ * static_cast<double>(ranks[static_cast<size_t>(power)]) / static_cast<double>(changing - 1) : 0.0)
367
+ : stagger_ * static_cast<double>(ranks[static_cast<size_t>(power)]);
368
+ const bool continues = next.numeric && next.wheels[static_cast<size_t>(power)].continues;
369
+ if (transition_.active && power < static_cast<int>(transition_.delays.size())) {
370
+ const double pending = std::max(0.0, (transition_.start + transition_.delays[static_cast<size_t>(power)]) - now);
371
+ // A continuing swap is under way, or still waits on its old delay.
372
+ delay = continues ? pending : std::min(delay, pending);
373
+ }
374
+ next.delays.push_back(delay);
375
+ next.maxDelay = std::max(next.maxDelay, delay);
376
+ }
377
+ next.signFrom = signFactor_;
378
+ next.signTo = target.negative ? 1.0 : 0.0;
379
+ next.decimalFrom = decimalFactor_;
380
+ next.decimalTo = fractionDigits_ > 0 ? 1.0 : 0.0;
381
+
382
+ // The change flash lights every wheel whose glyph changes; the pop kicks once.
383
+ flashes_.resize(static_cast<size_t>(count));
384
+ bool anyChange = false;
385
+ for (int power = 0; power < count; power++) {
386
+ const WheelTransition& wt = next.wheels[static_cast<size_t>(power)];
387
+ // A continuing swap is lit already.
388
+ const bool changes = next.numeric ? wt.from.blend < 1 && !wt.continues : (wt.from.position != wt.to.position || wt.from.width != wt.to.width);
389
+ if (changes) {
390
+ // Lit until the wheel has landed: its delay, then the transition.
391
+ flashes_[static_cast<size_t>(power)] = Flash{now, next.delays[static_cast<size_t>(power)] + next.duration, increasing};
392
+ anyChange = true;
393
+ }
394
+ }
395
+ if (anyChange && popOnChange_ > 0) {
396
+ popStart_ = now;
397
+ }
398
+
399
+ transition_ = std::move(next);
400
+ apply(0);
401
+ applyEffects(now);
402
+ settle(target);
403
+ }
404
+
405
+ void RollingEngine::planRoll(Transition& next, const Target& target, bool increasing, int count, int mandatory) const {
406
+ const int currentCount = static_cast<int>(wheels_.size());
407
+ // Below `pad`: decimal columns the format dropped (`changeFormat`).
408
+ const int pad = trailingPad_;
409
+ for (int power = 0; power < count; power++) {
410
+ const Wheel current = power < currentCount ? wheels_[static_cast<size_t>(power)] : Wheel{-1.0, 0.0, true, false};
411
+ Wheel from = current;
412
+ // A wheel the numeric transition left mid-swap rolls on from the glyph it was arriving at.
413
+ if (current.blend < 1) {
414
+ from.position = std::max(0.0, current.toGlyph);
415
+ from.linear = current.toGlyph < 0;
416
+ }
417
+ from.blend = 1;
418
+ Wheel to;
419
+ if (power >= pad && power - pad < target.powerCount) {
420
+ const double digit = static_cast<double>(target.digit(power - pad));
421
+ // An edge wheel rolls from blank: a leading one appearing, or a decimal
422
+ // column `changeFormat` opened (blank and closed, below the mandatory ones).
423
+ const bool opening = from.linear && current.width < 1.0;
424
+ const bool isEdge = opening || (power - pad >= mandatory && (power >= currentCount || current.width < 1.0 || from.linear || current.blankZero));
425
+ if (isEdge) {
426
+ // Appearing (or still appearing) wheel: linear strip blank → digit.
427
+ if (!from.linear) {
428
+ from.position = wrap(from.position);
429
+ }
430
+ from.linear = true;
431
+ to = Wheel{digit, 1.0, true, current.blankZero};
432
+ } else {
433
+ // Interior wheel: shortest roll in the direction of the change.
434
+ const double base = wrap(from.position);
435
+ from.position = base;
436
+ from.linear = false;
437
+ const double delta = increasing ? wrap(digit - base) : -wrap(base - digit);
438
+ to = Wheel{base + delta, 1.0, false, false};
439
+ }
440
+ next.finals.push_back(Wheel{digit, 1.0, false, false});
441
+ } else {
442
+ // Disappearing wheel: roll down to blank while shrinking.
443
+ if (!from.linear) {
444
+ from.position = wrap(from.position);
445
+ }
446
+ from.linear = true;
447
+ to = Wheel{-1.0, 0.0, true, current.blankZero};
448
+ }
449
+ next.wheels.push_back(WheelTransition{from, to});
450
+ }
451
+ }
452
+
453
+ int RollingEngine::shownGlyph(const Wheel& wheel) {
454
+ if (wheel.blend < 1) {
455
+ return static_cast<int>(wheel.toGlyph);
456
+ }
457
+ if (wheel.linear && wheel.position < -0.5) {
458
+ return -1;
459
+ }
460
+ const int digit = static_cast<int>(std::llround(wrap(wheel.position))) % 10;
461
+ if (wheel.blankZero && digit == 0) {
462
+ return -1;
463
+ }
464
+ return digit;
465
+ }
466
+
467
+ void RollingEngine::planNumeric(Transition& next, const Target& target, bool increasing, int count, int mandatory, double now) const {
468
+ (void)mandatory;
469
+ const int currentCount = static_cast<int>(wheels_.size());
470
+ const int pad = trailingPad_;
471
+ for (int power = 0; power < count; power++) {
472
+ const Wheel current = power < currentCount ? wheels_[static_cast<size_t>(power)] : Wheel{-1.0, 0.0, true, false};
473
+ // The glyph the wheel shows, or was arriving at when re-targeted: that is
474
+ // the one that leaves now.
475
+ const int showing = power < currentCount ? shownGlyph(current) : -1;
476
+ Wheel from;
477
+ Wheel to;
478
+ from.width = current.width;
479
+ from.fromGlyph = static_cast<double>(showing);
480
+ // A value that grew moves the glyphs up (the new one arrives from below).
481
+ from.fromAbove = !increasing;
482
+ from.linear = false;
483
+ from.blankZero = false;
484
+ // Below `pad`: decimal columns the format dropped (`changeFormat`).
485
+ if (power >= pad && power - pad < target.powerCount) {
486
+ const int digit = target.digit(power - pad);
487
+ from.toGlyph = static_cast<double>(digit);
488
+ from.position = static_cast<double>(digit);
489
+ to = Wheel{static_cast<double>(digit), 1.0, false, false};
490
+ next.finals.push_back(Wheel{static_cast<double>(digit), 1.0, false, false});
491
+ } else {
492
+ // Disappearing wheel: its glyph leaves and the column closes.
493
+ from.toGlyph = -1;
494
+ from.position = static_cast<double>(std::max(0, showing));
495
+ to = Wheel{from.position, 0.0, false, false};
496
+ }
497
+ // A wheel whose glyph does not change is not part of the transition (it
498
+ // may still be growing to full width).
499
+ from.blend = showing == static_cast<int>(from.toGlyph) ? 1.0 : 0.0;
500
+ WheelTransition wt{from, to};
501
+ // Unless it is still swapping to that glyph: typing re-targets every few
502
+ // frames, and a wheel cut out of its swap there jumped to the settled
503
+ // glyph, or, a new leading digit whose column was still opening, was
504
+ // drawn the odometer's way, a sliver clipped to the half-open column. It
505
+ // carries on from where it was, leaving the glyph it was leaving.
506
+ if (from.blend >= 1 && transition_.active && transition_.numeric && power < currentCount &&
507
+ power < static_cast<int>(transition_.wheels.size()) && power < static_cast<int>(transition_.delays.size())) {
508
+ const WheelTransition& previous = transition_.wheels[static_cast<size_t>(power)];
509
+ const double ran = now - (transition_.start + transition_.delays[static_cast<size_t>(power)]);
510
+ const bool swapping = previous.from.blend < 1 || previous.continues;
511
+ if (swapping && ran + previous.clock < transition_.duration) {
512
+ wt.continues = true;
513
+ wt.clock = std::max(0.0, ran) + previous.clock;
514
+ wt.from.fromGlyph = previous.from.fromGlyph;
515
+ wt.from.fromAbove = previous.from.fromAbove;
516
+ wt.from.blend = 0;
517
+ }
518
+ }
519
+ next.wheels.push_back(wt);
520
+ }
521
+ }
522
+
523
+ void RollingEngine::snap(const Target& target) {
524
+ endTransition();
525
+ cancelReveal();
526
+ wheels_.clear();
527
+ wheels_.reserve(static_cast<size_t>(target.powerCount));
528
+ for (int power = 0; power < target.powerCount; power++) {
529
+ wheels_.push_back(Wheel{static_cast<double>(target.digit(power)), 1.0, false, false});
530
+ }
531
+ signFactor_ = target.negative ? 1.0 : 0.0;
532
+ hasShownValue_ = true;
533
+ settle(target);
534
+ }
535
+
536
+ void RollingEngine::setLoading(bool loading, double now) {
537
+ if (loading == loading_) {
538
+ return;
539
+ }
540
+ loading_ = loading;
541
+ loadingFadeFrom_ = loadingProgress_;
542
+ loadingFadeStart_ = now;
543
+ loadingFadeActive_ = true;
544
+ if (loading) {
545
+ shimmerStart_ = now;
546
+ }
547
+ }
548
+
549
+ // MARK: - Jackpot reveal
550
+
551
+ void RollingEngine::setRevealTiming(double durationSeconds, double bounce, int style, double staggerSeconds) {
552
+ revealDuration_ = std::max(0.0, durationSeconds);
553
+ revealBounce_ = clamp01(bounce);
554
+ revealStyle_ = style == 1 ? 1 : 0;
555
+ revealStagger_ = std::max(0.0, staggerSeconds);
556
+ }
557
+
558
+ double RollingEngine::revealTotalSeconds() const {
559
+ const double popTail = revealBounce_ > 0 ? kRevealTailSeconds : 0;
560
+ const double holds = revealStyle_ == 1 ? 0 : revealMilestoneHold_ * static_cast<double>(reveal_.milestoneTimes.size());
561
+ // The reels need no tail of their own: the last clunk ends on the duration.
562
+ return revealDuration_ + holds + popTail;
563
+ }
564
+
565
+ void RollingEngine::clearRevealMilestones() {
566
+ revealMilestones_.clear();
567
+ }
568
+
569
+ void RollingEngine::addRevealMilestone(double value) {
570
+ if (std::isfinite(value)) {
571
+ revealMilestones_.push_back(value);
572
+ }
573
+ }
574
+
575
+ void RollingEngine::setRevealMilestoneHold(double holdSeconds) {
576
+ revealMilestoneHold_ = std::max(0.0, holdSeconds);
577
+ }
578
+
579
+ double RollingEngine::revealMilestoneValue(int index) const {
580
+ if (index < 0 || index >= static_cast<int>(reveal_.milestoneMagnitudes.size())) {
581
+ return 0;
582
+ }
583
+ return reveal_.milestoneMagnitudes[static_cast<size_t>(index)] / static_cast<double>(kPow10[fractionDigits_]);
584
+ }
585
+
586
+ /// Stops a reveal. `milestonesReached` survives so the view can still see a
587
+ /// milestone that was reached on the reveal's very last tick.
588
+ void RollingEngine::cancelReveal() {
589
+ reveal_.active = false;
590
+ reveal_.counting = false;
591
+ reveal_.holding = false;
592
+ revealScale_ = 1;
593
+ }
594
+
595
+ /// One punch: rises to `overshoot` at kPunchPeakSeconds and eases back to
596
+ /// zero (x·e^(1−x)), never negative, ~2 % of the peak left at the tail.
597
+ double RollingEngine::punch(double tau, double overshoot) {
598
+ if (tau <= 0 || overshoot <= 0) {
599
+ return 0;
600
+ }
601
+ const double x = tau / kPunchPeakSeconds;
602
+ return overshoot * x * std::exp(1 - x);
603
+ }
604
+
605
+ void RollingEngine::setRevealGrow(double grow) {
606
+ revealGrow_ = clamp01(grow);
607
+ }
608
+
609
+ /// Resolves the milestones for the target. Like a slot's tiered rollup, the
610
+ /// count then runs tier by tier: every tier gets an equal share of the
611
+ /// duration whatever its size (the counter simply runs faster in a bigger
612
+ /// tier), decelerates into its milestone, punches, pauses for the hold and
613
+ /// accelerates again into the next one.
614
+ void RollingEngine::planMilestones() {
615
+ reveal_.milestoneMagnitudes.clear();
616
+ reveal_.milestoneTimes.clear();
617
+ reveal_.milestonesReached = 0;
618
+ const double magnitude = static_cast<double>(reveal_.target.magnitude);
619
+ if (magnitude <= 0 || revealDuration_ <= 0 || reduceMotion_) {
620
+ return;
621
+ }
622
+ std::vector<double> sorted = revealMilestones_;
623
+ std::sort(sorted.begin(), sorted.end());
624
+ for (double value : sorted) {
625
+ const double m = std::round(std::fabs(value) * static_cast<double>(kPow10[fractionDigits_]));
626
+ if (m <= 0 || m >= magnitude) {
627
+ continue;
628
+ }
629
+ if (!reveal_.milestoneMagnitudes.empty() && m <= reveal_.milestoneMagnitudes.back()) {
630
+ continue;
631
+ }
632
+ reveal_.milestoneMagnitudes.push_back(m);
633
+ }
634
+ const double segment = revealDuration_ / static_cast<double>(reveal_.milestoneMagnitudes.size() + 1);
635
+ for (size_t i = 0; i < reveal_.milestoneMagnitudes.size(); i++) {
636
+ reveal_.milestoneTimes.push_back(segment * static_cast<double>(i + 1));
637
+ }
638
+ }
639
+
640
+ void RollingEngine::holdReveal(double value) {
641
+ endTransition();
642
+ cancelReveal();
643
+ targetValue_ = value;
644
+ hasShownValue_ = true;
645
+ reveal_.holding = true;
646
+ reveal_.milestonesReached = 0;
647
+ reveal_.milestoneTimes.clear();
648
+ reveal_.milestoneMagnitudes.clear();
649
+ reveal_.target = makeTarget(value);
650
+ reveal_.start = 0;
651
+ applyReveal(0);
652
+ settle(reveal_.target);
653
+ }
654
+
655
+ void RollingEngine::reveal(double value, double now) {
656
+ endTransition();
657
+ targetValue_ = value;
658
+ const Target target = makeTarget(value);
659
+ if (revealDuration_ <= 0 || reduceMotion_) {
660
+ snap(target);
661
+ return;
662
+ }
663
+ if (!reveal_.active) {
664
+ reveal_.start = now;
665
+ }
666
+ reveal_.active = true;
667
+ reveal_.holding = false;
668
+ reveal_.target = target;
669
+ reveal_.milestonesReached = 0;
670
+ reveal_.milestoneTimes.clear();
671
+ reveal_.milestoneMagnitudes.clear();
672
+ hasShownValue_ = true;
673
+ if (revealStyle_ == 1) {
674
+ planReels();
675
+ } else {
676
+ planMilestones();
677
+ }
678
+ applyReveal(now - reveal_.start);
679
+ settle(target);
680
+ }
681
+
682
+ /// Lays out the spin-style reels: stop times from the left, then for each reel
683
+ /// the brake window and the braking distance that ends on its digit.
684
+ void RollingEngine::planReels() {
685
+ const Target& target = reveal_.target;
686
+ const int count = target.powerCount;
687
+ // Reel k (from the left) locks at lead + k * stagger. The last one locks a
688
+ // clunk short of the duration, so its bounce is spent by the time the figure
689
+ // pops. The stagger shrinks when the reels wouldn't fit between the earliest
690
+ // lead and that last lock; the lead is still at least kReelMinLead.
691
+ const double last = std::max(0.0, revealDuration_ - kReelBounceSeconds);
692
+ double stagger = revealStagger_;
693
+ const double room = std::max(0.0, last - revealDuration_ * kReelMinLead);
694
+ if (count > 1 && stagger * (count - 1) > room) {
695
+ stagger = room / (count - 1);
696
+ }
697
+ const double lead = last - stagger * (count - 1);
698
+ reveal_.reels.assign(static_cast<size_t>(count), Reel{});
699
+ for (int k = 0; k < count; k++) {
700
+ Reel& reel = reveal_.reels[static_cast<size_t>(k)];
701
+ const int power = count - 1 - k;
702
+ // Different starting digits per reel, so the spinning columns don't move in unison.
703
+ reel.phase = std::fmod(static_cast<double>(k) * 3.7 + 0.5, 10.0);
704
+ reel.stop = lead + stagger * k;
705
+ reel.brakeStart = std::max(0.0, reel.stop - kReelBrakeSeconds);
706
+ reel.from = reel.phase + kReelSpeed * reel.brakeStart;
707
+ // Brake over the occurrence of the digit 4–14 strip positions ahead: with
708
+ // 12 digits of free travel per brake window the curve never reverses.
709
+ const double digit = static_cast<double>(target.digit(power));
710
+ double travel = wrap(digit - reel.from);
711
+ if (travel < 4) {
712
+ travel += 10;
713
+ }
714
+ reel.travel = travel;
715
+ }
716
+ }
717
+
718
+ /// Clock fraction → value fraction of one tally run. The counter's rate winds
719
+ /// up linearly over `rampIn`, holds steady, then falls off quadratically over
720
+ /// `rampOut`, so the count is linear in the middle like a slot's tally and
721
+ /// crawls the last stretch into its target instead of slamming into it.
722
+ double RollingEngine::tally(double t, double rampIn, double rampOut) {
723
+ if (t <= 0) {
724
+ return 0;
725
+ }
726
+ if (t >= 1) {
727
+ return 1;
728
+ }
729
+ const double area = rampIn / 2 + (1 - rampIn - rampOut) + rampOut / 3;
730
+ double distance;
731
+ if (t < rampIn) {
732
+ distance = t * t / (2 * rampIn);
733
+ } else if (t < 1 - rampOut) {
734
+ distance = rampIn / 2 + (t - rampIn);
735
+ } else {
736
+ const double rest = 1 - t;
737
+ distance = area - rest * rest * rest / (3 * rampOut * rampOut);
738
+ }
739
+ return distance / area;
740
+ }
741
+
742
+ void RollingEngine::applyReveal(double elapsed) {
743
+ const bool spin = revealStyle_ == 1 && !reveal_.holding;
744
+ // The scale: the figure's size as the count grows, times the punches. A
745
+ // closed form of the clock, so the whole timeline is deterministic.
746
+ double base = 1;
747
+ double punches = 0;
748
+ if (spin) {
749
+ applyRevealSpin(elapsed);
750
+ reveal_.counting = elapsed < revealDuration_;
751
+ punches += punch(elapsed - revealDuration_, revealBounce_);
752
+ } else {
753
+ applyRevealCount(elapsed);
754
+ // The figure opens smaller and grows with the tally, the way a big win's
755
+ // meter is enlarged as it climbs; each milestone punches when reached and
756
+ // the landing punches hardest. The count has finished once the curve and
757
+ // every hold are behind us.
758
+ const double hold = revealMilestoneHold_;
759
+ const size_t reached = static_cast<size_t>(reveal_.milestonesReached);
760
+ for (size_t i = 0; i < reached; i++) {
761
+ const double wall = reveal_.milestoneTimes[i] + hold * static_cast<double>(i);
762
+ punches += punch(elapsed - wall, revealBounce_ * kMilestonePunch);
763
+ }
764
+ const double end = revealDuration_ + hold * static_cast<double>(reveal_.milestoneTimes.size());
765
+ reveal_.counting = elapsed < end;
766
+ if (!reveal_.holding) {
767
+ base = 1 - revealGrow_ * (1 - clamp01(end > 0 ? elapsed / end : 1.0));
768
+ }
769
+ punches += punch(elapsed - end, revealBounce_);
770
+ }
771
+ signFactor_ = reveal_.target.negative ? 1.0 : 0.0;
772
+ revealScale_ = base * (1 + punches);
773
+ }
774
+
775
+ void RollingEngine::applyRevealCount(double elapsed) {
776
+ const Target& target = reveal_.target;
777
+ const double magnitude = static_cast<double>(target.magnitude);
778
+
779
+ // Wall time → curve time: the clock stands still on each milestone for the
780
+ // hold, and the count sits exactly on the milestone meanwhile.
781
+ double curve = elapsed;
782
+ int reached = 0;
783
+ double heldAt = -1;
784
+ const double hold = revealMilestoneHold_;
785
+ for (size_t i = 0; i < reveal_.milestoneTimes.size(); i++) {
786
+ const double wall = reveal_.milestoneTimes[i] + hold * static_cast<double>(i);
787
+ if (elapsed < wall) {
788
+ break;
789
+ }
790
+ reached = static_cast<int>(i) + 1;
791
+ const double consumed = std::min(hold, elapsed - wall);
792
+ curve -= consumed;
793
+ if (consumed < hold) {
794
+ heldAt = reveal_.milestoneMagnitudes[i];
795
+ }
796
+ }
797
+ reveal_.milestonesReached = std::max(reveal_.milestonesReached, reached);
798
+
799
+ // The count: a slot's tally. One tier at a time (the whole figure is one
800
+ // tier without milestones), each running linearly in value at a constant
801
+ // rate, winding up out of the tier's start and crawling into its end, so it
802
+ // reads slow → fast → slow and lands squarely on every milestone. The
803
+ // final tier crawls longer so the total is readable as it arrives.
804
+ double count;
805
+ if (heldAt >= 0) {
806
+ count = heldAt;
807
+ } else {
808
+ const size_t tiers = reveal_.milestoneMagnitudes.size() + 1;
809
+ const double segment = revealDuration_ > 0 ? revealDuration_ / static_cast<double>(tiers) : 0;
810
+ size_t tier = segment > 0 ? static_cast<size_t>(std::floor(curve / segment)) : tiers - 1;
811
+ tier = std::min(tier, tiers - 1);
812
+ const double u = segment > 0 ? clamp01((curve - segment * static_cast<double>(tier)) / segment) : 1.0;
813
+ const double lo = tier == 0 ? 0 : reveal_.milestoneMagnitudes[tier - 1];
814
+ const double hi = tier == tiers - 1 ? magnitude : reveal_.milestoneMagnitudes[tier];
815
+ const double rampOut = tier == tiers - 1 ? kTallyFinalRampOut : kTallyRampOut;
816
+ // ceil, not floor: the count leaves its start on the first moving frame.
817
+ count = std::min(hi, std::ceil(lo + (hi - lo) * tally(u, kTallyRampIn, rampOut)));
818
+ }
819
+ const int mandatory = std::min(kMaxPowerCount, minimumIntegerDigits_ + fractionDigits_);
820
+ const size_t powerCount = static_cast<size_t>(target.powerCount);
821
+
822
+ wheels_.resize(powerCount);
823
+ for (size_t power = 0; power < powerCount; power++) {
824
+ const double p10 = static_cast<double>(kPow10[power]);
825
+ // A digit left of the count's leading digit stays blank (no leading zeros),
826
+ // except the mandatory fraction / zero-padded digits.
827
+ const bool hidden = static_cast<int>(power) >= mandatory && count < p10;
828
+ if (hidden) {
829
+ wheels_[power] = Wheel{-1.0, 0.0, true, false};
830
+ } else {
831
+ wheels_[power] = Wheel{std::fmod(std::floor(count / p10), 10.0), 1.0, false, false};
832
+ }
833
+ }
834
+ }
835
+
836
+ void RollingEngine::applyRevealSpin(double elapsed) {
837
+ const size_t powerCount = static_cast<size_t>(reveal_.target.powerCount);
838
+ if (reveal_.reels.size() != powerCount) {
839
+ planReels();
840
+ }
841
+ wheels_.resize(powerCount);
842
+ for (size_t power = 0; power < powerCount; power++) {
843
+ const Reel& reel = reveal_.reels[powerCount - 1 - power];
844
+ double position;
845
+ if (elapsed < reel.brakeStart) {
846
+ position = reel.phase + kReelSpeed * elapsed;
847
+ } else if (elapsed < reel.stop) {
848
+ // Cubic Hermite from full speed to a standstill on the digit.
849
+ const double window = reel.stop - reel.brakeStart;
850
+ const double s = window > 0 ? clamp01((elapsed - reel.brakeStart) / window) : 1.0;
851
+ const double v = kReelSpeed * window;
852
+ const double d = reel.travel;
853
+ position = reel.from + v * s + (3 * d - 2 * v) * s * s + (v - 2 * d) * s * s * s;
854
+ } else {
855
+ // Locked, with the catch's bounce: past the digit and back.
856
+ const double tau = elapsed - reel.stop;
857
+ position = reel.from + reel.travel;
858
+ if (tau < kReelBounceSeconds) {
859
+ position += kReelBounceDigits * std::sin(kPi * tau / kReelBounceSeconds);
860
+ }
861
+ }
862
+ wheels_[power] = Wheel{position, 1.0, false, false};
863
+ }
864
+ }
865
+
866
+ // MARK: - Animation
867
+
868
+ void RollingEngine::apply(double elapsed) {
869
+ const Transition& tr = transition_;
870
+ wheels_.resize(tr.wheels.size());
871
+ for (size_t i = 0; i < tr.wheels.size(); i++) {
872
+ const WheelTransition& wt = tr.wheels[i];
873
+ const double delay = i < tr.delays.size() ? tr.delays[i] : 0;
874
+ const double raw = tr.duration > 0 ? clamp01((elapsed - delay) / tr.duration) : 1.0;
875
+ // The numeric transition's tail is only its position spring ringing out:
876
+ // a column opens and closes over the duration itself.
877
+ const double span = tr.style == 1 ? tr.duration / kNumericTail : tr.duration;
878
+ const double rawSpan = span > 0 ? clamp01((elapsed - delay) / span) : 1.0;
879
+ double t = tr.fromMotion ? easeFromMotion(rawSpan) : ease(rawSpan);
880
+ if (tr.numeric) {
881
+ // A column opens on the arriving glyph's size-and-opacity spring and
882
+ // closes on the leaving glyph's blur, the faster one: on the
883
+ // transition's easing it lagged the glyph, and a digit that had already
884
+ // left stood as a gap in the number until its column caught up.
885
+ const double settle = wt.to.width < wt.from.width ? kNumericBlurOutSettle : kNumericGrowSettle;
886
+ t = rawSpan >= 1 ? 1.0 : damped(std::max(0.0, elapsed - delay), 1.0, settle * span);
887
+ }
888
+ Wheel& w = wheels_[i];
889
+ w.width = clamp01(wt.from.width + (wt.to.width - wt.from.width) * t);
890
+ w.linear = wt.from.linear;
891
+ w.blankZero = wt.from.blankZero;
892
+ if (tr.numeric && wt.from.blend >= 1) {
893
+ // A wheel whose glyph does not change (it may still be growing to full width).
894
+ w.position = wt.from.position;
895
+ w.fromGlyph = -1;
896
+ w.toGlyph = -1;
897
+ w.blend = 1;
898
+ w.focus = 1;
899
+ w.grow = 1;
900
+ } else if (tr.style == 2) {
901
+ // Scramble: a different digit every step until the wheel locks.
902
+ const int from = static_cast<int>(wt.from.fromGlyph);
903
+ const int to = static_cast<int>(wt.from.toGlyph);
904
+ if (raw >= 1) {
905
+ w.position = static_cast<double>(std::max(0, to));
906
+ } else {
907
+ const uint64_t step = static_cast<uint64_t>(std::max(0.0, elapsed - delay) / kScrambleStepSeconds);
908
+ // A mixed hash of the step and the wheel (a plain multiply mod 10 is a counter).
909
+ uint64_t h = step * 2654435761ULL + static_cast<uint64_t>(i) * 40503ULL + 7ULL;
910
+ h ^= h >> 13;
911
+ h *= 0x5bd1e995ULL;
912
+ h ^= h >> 15;
913
+ int glyph = static_cast<int>(h % 10);
914
+ // Never sit on the digit the wheel is leaving or arriving at: it would read as a stall.
915
+ while (glyph == from || glyph == to) {
916
+ glyph = (glyph + 1) % 10;
917
+ }
918
+ w.position = static_cast<double>(glyph);
919
+ }
920
+ w.fromGlyph = -1;
921
+ w.toGlyph = -1;
922
+ w.blend = 1;
923
+ w.focus = 1;
924
+ w.grow = 1;
925
+ } else if (tr.numeric) {
926
+ // Numeric: the glyphs swap in place; the position is the digit
927
+ // arriving and the three clocks say how far the swap is (see the header).
928
+ w.position = wt.from.position;
929
+ w.fromGlyph = wt.from.fromGlyph;
930
+ w.toGlyph = wt.from.toGlyph;
931
+ w.fromAbove = wt.from.fromAbove;
932
+ // A swap carried over from the previous target is `clock` further in.
933
+ const double local = std::max(0.0, elapsed - delay) + wt.clock;
934
+ if (local >= tr.duration || tr.duration <= 0) {
935
+ w.blend = 1;
936
+ w.grow = 1;
937
+ w.focus = 1;
938
+ w.blurOut = 1;
939
+ } else {
940
+ const double d = tr.duration / kNumericTail;
941
+ w.blend = damped(local, kNumericPositionZeta, d);
942
+ w.grow = damped(local, 1.0, kNumericGrowSettle * d);
943
+ w.focus = damped(local, kNumericFocusZeta, kNumericFocusSettle * d);
944
+ w.blurOut = damped(local, 1.0, kNumericBlurOutSettle * d);
945
+ }
946
+ } else {
947
+ w.position = wt.from.position + (wt.to.position - wt.from.position) * t;
948
+ w.fromGlyph = -1;
949
+ w.toGlyph = -1;
950
+ w.blend = 1;
951
+ w.focus = 1;
952
+ w.grow = 1;
953
+ }
954
+ }
955
+ const double signRaw = tr.duration > 0 ? clamp01(elapsed / tr.duration) : 1.0;
956
+ const double signT = tr.fromMotion ? easeFromMotion(signRaw) : ease(signRaw);
957
+ signFactor_ = clamp01(tr.signFrom + (tr.signTo - tr.signFrom) * signT);
958
+ if (tr.decimalFrom != tr.decimalTo) {
959
+ // With the columns: on the glyphs' spring in a swap, the roll's easing in a roll.
960
+ const double span = tr.style == 1 ? tr.duration / kNumericTail : tr.duration;
961
+ const double raw = span > 0 ? clamp01(elapsed / span) : 1.0;
962
+ const double t = raw >= 1 ? 1.0
963
+ : tr.numeric ? damped(std::max(0.0, elapsed), 1.0, kNumericGrowSettle * span)
964
+ : (tr.fromMotion ? easeFromMotion(raw) : ease(raw));
965
+ decimalFactor_ = clamp01(tr.decimalFrom + (tr.decimalTo - tr.decimalFrom) * t);
966
+ } else {
967
+ decimalFactor_ = tr.decimalTo;
968
+ }
969
+ }
970
+
971
+ void RollingEngine::finish() {
972
+ wheels_ = transition_.finals;
973
+ signFactor_ = transition_.signTo;
974
+ decimalFactor_ = transition_.decimalTo;
975
+ if (trailingPad_ > 0) {
976
+ // The closed decimal columns go; the flashes keep their place values.
977
+ const size_t pad = std::min(flashes_.size(), static_cast<size_t>(trailingPad_));
978
+ flashes_.erase(flashes_.begin(), flashes_.begin() + static_cast<std::ptrdiff_t>(pad));
979
+ trailingPad_ = 0;
980
+ }
981
+ transition_.active = false;
982
+ }
983
+
984
+ void RollingEngine::applyEffects(double now) {
985
+ lastNow_ = now;
986
+ bool active = false;
987
+ if (flashes_.size() < wheels_.size()) {
988
+ flashes_.resize(wheels_.size());
989
+ }
990
+ for (size_t i = 0; i < wheels_.size(); i++) {
991
+ Wheel& w = wheels_[i];
992
+ const Flash& f = flashes_[i];
993
+ if (flashSeconds_ <= 0 || f.start < 0) {
994
+ w.flash = 0;
995
+ continue;
996
+ }
997
+ // Lights at once, holds while the wheel moves, fades with a tail.
998
+ const double lit = now - f.start;
999
+ const double u = lit < f.hold ? 0 : clamp01((lit - f.hold) / flashSeconds_);
1000
+ w.flash = std::pow(1 - u, 1.5);
1001
+ w.flashUp = f.up;
1002
+ if (u < 1) {
1003
+ active = true;
1004
+ }
1005
+ }
1006
+ if (popStart_ >= 0 && popOnChange_ > 0) {
1007
+ const double tau = now - popStart_;
1008
+ const double p = punch(tau, popOnChange_);
1009
+ popScale_ = 1 + p;
1010
+ if (tau < kPunchPeakSeconds * 8) {
1011
+ active = true;
1012
+ } else {
1013
+ popScale_ = 1;
1014
+ popStart_ = -1;
1015
+ }
1016
+ } else {
1017
+ popScale_ = 1;
1018
+ }
1019
+ effectsActive_ = active;
1020
+ }
1021
+
1022
+ bool RollingEngine::tick(double now) {
1023
+ if (transition_.active) {
1024
+ const double elapsed = now - transition_.start;
1025
+ if (elapsed >= transition_.duration + transition_.maxDelay) {
1026
+ finish();
1027
+ } else {
1028
+ apply(elapsed);
1029
+ }
1030
+ }
1031
+ if (reveal_.active) {
1032
+ const double elapsed = now - reveal_.start;
1033
+ if (elapsed >= revealTotalSeconds()) {
1034
+ const Target target = reveal_.target;
1035
+ snap(target);
1036
+ } else {
1037
+ applyReveal(elapsed);
1038
+ }
1039
+ }
1040
+ if (loadingFadeActive_) {
1041
+ const double target = loading_ ? 1.0 : 0.0;
1042
+ const double t = clamp01((now - loadingFadeStart_) / kLoadingFadeSeconds);
1043
+ loadingProgress_ = loadingFadeFrom_ + (target - loadingFadeFrom_) * t;
1044
+ if (t >= 1) {
1045
+ loadingProgress_ = target;
1046
+ loadingFadeActive_ = false;
1047
+ }
1048
+ }
1049
+ applyEffects(now);
1050
+ applyText(now);
1051
+ return needsFrames();
1052
+ }
1053
+
1054
+ bool RollingEngine::needsFrames() const {
1055
+ const bool sweeping = loadingProgress_ > 0 && !reduceMotion_;
1056
+ return transition_.active || reveal_.active || loadingFadeActive_ || sweeping || effectsActive_ || textActive_;
1057
+ }
1058
+
1059
+ void RollingEngine::changeText(int slot, double now) {
1060
+ if (slot < 0 || slot >= kTextSlots) {
1061
+ return;
1062
+ }
1063
+ if (!hasShownValue_ || duration_ <= 0 || reduceMotion_) {
1064
+ textClocks_[slot].start = -1;
1065
+ textChanges_[slot] = TextChange{};
1066
+ textActive_ = false;
1067
+ for (const TextChange& t : textChanges_) {
1068
+ textActive_ = textActive_ || t.active;
1069
+ }
1070
+ return;
1071
+ }
1072
+ textClocks_[slot].start = now;
1073
+ applyText(now);
1074
+ }
1075
+
1076
+ RollingEngine::TextChange RollingEngine::textChange(int slot) const {
1077
+ return slot >= 0 && slot < kTextSlots ? textChanges_[slot] : TextChange{};
1078
+ }
1079
+
1080
+ void RollingEngine::applyText(double now) {
1081
+ // The numeric transition's clocks for one glyph, over the transition's
1082
+ // duration (not its tail: there is no position spring to ring out).
1083
+ const double d = duration_;
1084
+ bool any = false;
1085
+ for (int i = 0; i < kTextSlots; i++) {
1086
+ TextChange& t = textChanges_[i];
1087
+ const double start = textClocks_[i].start;
1088
+ const double local = now - start;
1089
+ if (start < 0 || d <= 0 || local >= d * kNumericTail) {
1090
+ textClocks_[i].start = -1;
1091
+ t = TextChange{};
1092
+ continue;
1093
+ }
1094
+ t.active = true;
1095
+ t.grow = damped(local, 1.0, kNumericGrowSettle * d);
1096
+ t.focus = std::min(1.0, damped(local, kNumericFocusZeta, kNumericFocusSettle * d));
1097
+ t.blurOut = damped(local, 1.0, kNumericBlurOutSettle * d);
1098
+ any = true;
1099
+ }
1100
+ textActive_ = any;
1101
+ }
1102
+
1103
+ bool RollingEngine::isRolling() const {
1104
+ return transition_.active || reveal_.counting;
1105
+ }
1106
+
1107
+ double RollingEngine::shimmerPhase(double now, double periodSeconds) const {
1108
+ if (reduceMotion_) {
1109
+ return 0;
1110
+ }
1111
+ const double period = std::max(0.2, periodSeconds);
1112
+ return std::fmod(std::max(0.0, now - shimmerStart_) / period, 1.0);
1113
+ }
1114
+
1115
+ double RollingEngine::ease(double t) const {
1116
+ switch (easing_) {
1117
+ case 0:
1118
+ return t;
1119
+ case 1:
1120
+ return t * t * t;
1121
+ case 2:
1122
+ return 1 - std::pow(1 - t, 3);
1123
+ case 4:
1124
+ return spring(t, bounce_);
1125
+ default:
1126
+ return t < 0.5 ? 4 * t * t * t : 1 - std::pow(-2 * t + 2, 3) / 2;
1127
+ }
1128
+ }
1129
+
1130
+ double RollingEngine::easeFromMotion(double t) const {
1131
+ switch (easing_) {
1132
+ case 0:
1133
+ case 1:
1134
+ return t; // linear / easeIn: keep the wheel moving at a steady pace
1135
+ case 4:
1136
+ return spring(t, bounce_); // the spring already starts with velocity
1137
+ default:
1138
+ return 1 - std::pow(1 - t, 3); // easeOut / easeInOut: decelerate into the new target
1139
+ }
1140
+ }
1141
+
1142
+ /// Step response of a damped spring, normalised so it has settled at t == 1.
1143
+ /// `bounce` maps to the damping ratio like SwiftUI's `.spring(duration:bounce:)`.
1144
+ double RollingEngine::spring(double t, double bounce) {
1145
+ const double zeta = std::min(1.0, std::max(0.05, 1.0 - clamp01(bounce)));
1146
+ const double omega = 3.0 * kPi;
1147
+ const double k = zeta * omega;
1148
+ if (zeta >= 0.999) {
1149
+ return 1 - (1 + k * t) * std::exp(-k * t);
1150
+ }
1151
+ const double wd = omega * std::sqrt(1 - zeta * zeta);
1152
+ return 1 - std::exp(-k * t) * (std::cos(wd * t) + (k / wd) * std::sin(wd * t));
1153
+ }
1154
+
1155
+ double RollingEngine::damped(double t, double zeta, double settle) {
1156
+ if (t <= 0 || settle <= 0) {
1157
+ return 0;
1158
+ }
1159
+ const double z = std::min(1.0, std::max(0.05, zeta));
1160
+ if (z >= 0.999) {
1161
+ // Critically damped: 1 - (1 + kt) e^{-kt} is within 2 % of 1 at kt ≈ 5.83.
1162
+ const double k = 5.83 / settle;
1163
+ return 1 - (1 + k * t) * std::exp(-k * t);
1164
+ }
1165
+ // Underdamped: the envelope e^{-ζωt} is within 2 % at ζωt = 4.
1166
+ const double omega = 4.0 / (z * settle);
1167
+ const double k = z * omega;
1168
+ const double wd = omega * std::sqrt(1 - z * z);
1169
+ return 1 - std::exp(-k * t) * (std::cos(wd * t) + (k / wd) * std::sin(wd * t));
1170
+ }
1171
+
1172
+ int RollingEngine::targetDigit(int power) const {
1173
+ return target_.digit(power);
1174
+ }
1175
+
1176
+ void RollingEngine::reset() {
1177
+ fractionDigits_ = 0;
1178
+ minimumIntegerDigits_ = 1;
1179
+ duration_ = 0.5;
1180
+ easing_ = 3;
1181
+ bounce_ = 0.15;
1182
+ stagger_ = 0;
1183
+ direction_ = 0;
1184
+ transitionStyle_ = 0;
1185
+ flashSeconds_ = 0;
1186
+ popOnChange_ = 0;
1187
+ flashes_.clear();
1188
+ popStart_ = -1;
1189
+ popScale_ = 1;
1190
+ lastNow_ = 0;
1191
+ effectsActive_ = false;
1192
+ trailingPad_ = 0;
1193
+ decimalFactor_ = 1;
1194
+ for (int i = 0; i < kTextSlots; i++) {
1195
+ textClocks_[i] = TextClock{};
1196
+ textChanges_[i] = TextChange{};
1197
+ }
1198
+ textActive_ = false;
1199
+ revealDuration_ = 2.2;
1200
+ revealBounce_ = 0.12;
1201
+ revealStyle_ = 0;
1202
+ revealStagger_ = 0.2;
1203
+ revealGrow_ = 0.2;
1204
+ revealMilestones_.clear();
1205
+ revealMilestoneHold_ = 0;
1206
+ reveal_ = Reveal{};
1207
+ revealScale_ = 1;
1208
+ wheels_.clear();
1209
+ signFactor_ = 0;
1210
+ hasShownValue_ = false;
1211
+ targetValue_ = 0;
1212
+ target_ = Target{0, false, 1};
1213
+ settledPowerCount_ = 1;
1214
+ settledNegative_ = false;
1215
+ transition_ = Transition{};
1216
+ loading_ = false;
1217
+ loadingProgress_ = 0;
1218
+ loadingFadeActive_ = false;
1219
+ loadingFadeFrom_ = 0;
1220
+ loadingFadeStart_ = 0;
1221
+ shimmerStart_ = 0;
1222
+ }
1223
+
1224
+ } // namespace margelo::nitro::nitroinput