react-native-nitro-input 0.1.0 → 0.3.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.
- package/CHANGELOG.md +343 -3
- package/NitroInput.podspec +4 -4
- package/README.md +420 -53
- package/android/CMakeLists.txt +9 -3
- package/android/gradle.properties +1 -1
- package/android/src/main/cpp/JOutlineGeometry.hpp +1 -1
- package/android/src/main/cpp/JReflowEngine.cpp +132 -0
- package/android/src/main/cpp/{JMorphEngine.hpp → JReflowEngine.hpp} +7 -7
- package/android/src/main/cpp/JRollingEngine.cpp +223 -0
- package/android/src/main/cpp/JRollingEngine.hpp +68 -0
- package/android/src/main/cpp/cpp-adapter.cpp +4 -2
- package/android/src/main/java/com/margelo/nitro/nitroinput/AmountFormatter.kt +1 -1
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputView.kt +21 -1
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberFormatPlatform.kt +209 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberView.kt +496 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputPackage.kt +2 -1
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputView.kt +107 -26
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroNumberView.kt +1881 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/{MorphEngine.kt → ReflowEngine.kt} +2 -2
- package/android/src/main/java/com/margelo/nitro/nitroinput/RollingEngine.kt +60 -0
- package/cpp/AmountFormatter.cpp +5 -0
- package/cpp/AmountFormatter.hpp +10 -2
- package/cpp/{MorphEngine.cpp → ReflowEngine.cpp} +52 -47
- package/cpp/{MorphEngine.hpp → ReflowEngine.hpp} +12 -4
- package/cpp/RollingEngine.cpp +1234 -0
- package/cpp/RollingEngine.hpp +475 -0
- package/cpp/SwiftNonCopyable.hpp +22 -0
- package/cpp/numberformat/HybridNitroNumberFormat.cpp +498 -0
- package/cpp/numberformat/HybridNitroNumberFormat.hpp +74 -0
- package/cpp/numberformat/HybridNitroNumberFormatFactory.hpp +11 -0
- package/cpp/numberformat/NumberFormatCore.cpp +629 -0
- package/cpp/numberformat/NumberFormatCore.hpp +154 -0
- package/cpp/numberformat/NumberFormatProbe.cpp +354 -0
- package/cpp/numberformat/NumberFormatProbe.hpp +63 -0
- package/cpp/worklets/NitroInputWorkletsBridge.cpp +3 -3
- package/cpp/worklets/NitroInputWorkletsBridge.hpp +1 -1
- package/ios/{HybridMorphInputView.swift → HybridNitroInputView.swift} +16 -1
- package/ios/HybridNitroNumberFormatPlatform.swift +348 -0
- package/ios/HybridNitroNumberView.swift +458 -0
- package/ios/NitroInputView.swift +148 -64
- package/ios/NitroNumberView.swift +2074 -0
- package/lib/commonjs/nitrogen/generated/shared/json/NitroInputViewConfig.json +5 -0
- package/lib/commonjs/nitrogen/generated/shared/json/NitroNumberViewConfig.json +61 -0
- package/lib/commonjs/src/NitroInput.js +27 -56
- package/lib/commonjs/src/NitroNumber.js +141 -0
- package/lib/commonjs/src/NumberFormat.js +525 -0
- package/lib/commonjs/src/formatProps.js +48 -0
- package/lib/commonjs/src/index.js +6 -3
- package/lib/commonjs/src/specs/NitroNumber.nitro.js +2 -0
- package/lib/commonjs/src/specs/NumberFormat.nitro.js +2 -0
- package/lib/commonjs/src/styleHelpers.js +36 -0
- package/lib/module/nitrogen/generated/shared/json/NitroInputViewConfig.json +5 -0
- package/lib/module/nitrogen/generated/shared/json/NitroNumberViewConfig.json +61 -0
- package/lib/module/src/NitroInput.js +18 -47
- package/lib/module/src/NitroNumber.js +102 -0
- package/lib/module/src/NumberFormat.js +521 -0
- package/lib/module/src/formatProps.js +45 -0
- package/lib/module/src/index.js +2 -1
- package/lib/module/src/specs/NitroNumber.nitro.js +1 -0
- package/lib/module/src/specs/NumberFormat.nitro.js +1 -0
- package/lib/module/src/styleHelpers.js +32 -0
- package/lib/typescript/src/NitroInput.d.ts +50 -21
- package/lib/typescript/src/NitroNumber.d.ts +246 -0
- package/lib/typescript/src/NumberFormat.d.ts +47 -0
- package/lib/typescript/src/formatProps.d.ts +21 -0
- package/lib/typescript/src/index.d.ts +5 -2
- package/lib/typescript/src/specs/NitroInput.nitro.d.ts +19 -9
- package/lib/typescript/src/specs/NitroNumber.nitro.d.ts +250 -0
- package/lib/typescript/src/specs/NumberFormat.nitro.d.ts +139 -0
- package/lib/typescript/src/styleHelpers.d.ts +3 -0
- package/nitro.json +26 -0
- package/nitrogen/generated/android/NitroInput+autolinking.cmake +10 -0
- package/nitrogen/generated/android/NitroInputOnLoad.cpp +48 -0
- package/nitrogen/generated/android/c++/JFunc_void.hpp +75 -0
- package/nitrogen/generated/android/c++/JFunc_void_double_double.hpp +10 -10
- package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.cpp +45 -0
- package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.hpp +10 -0
- package/nitrogen/generated/android/c++/JHybridNitroNumberFormatPlatformSpec.cpp +118 -0
- package/nitrogen/generated/android/c++/JHybridNitroNumberFormatPlatformSpec.hpp +64 -0
- package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.cpp +590 -0
- package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.hpp +168 -0
- package/nitrogen/generated/android/c++/JHybridNitroPlatformNumberFormatterSpec.cpp +65 -0
- package/nitrogen/generated/android/c++/JHybridNitroPlatformNumberFormatterSpec.hpp +64 -0
- package/nitrogen/generated/android/c++/JNitroNumberAffixAlign.hpp +64 -0
- package/nitrogen/generated/android/c++/JNitroNumberDirection.hpp +64 -0
- package/nitrogen/generated/android/c++/JNitroNumberEasing.hpp +67 -0
- package/nitrogen/generated/android/c++/JNitroNumberRevealStyle.hpp +58 -0
- package/nitrogen/generated/android/c++/JNitroNumberTextAlign.hpp +64 -0
- package/nitrogen/generated/android/c++/JNitroNumberTransition.hpp +61 -0
- package/nitrogen/generated/android/c++/JNumberFormatCompactDisplay.hpp +58 -0
- package/nitrogen/generated/android/c++/JNumberFormatCurrencyDisplay.hpp +64 -0
- package/nitrogen/generated/android/c++/JNumberFormatCurrencySign.hpp +58 -0
- package/nitrogen/generated/android/c++/JNumberFormatNotation.hpp +64 -0
- package/nitrogen/generated/android/c++/JNumberFormatPlatformOptions.hpp +161 -0
- package/nitrogen/generated/android/c++/JNumberFormatPlatformSymbols.hpp +89 -0
- package/nitrogen/generated/android/c++/JNumberFormatRoundingMode.hpp +79 -0
- package/nitrogen/generated/android/c++/JNumberFormatSignDisplay.hpp +67 -0
- package/nitrogen/generated/android/c++/JNumberFormatStyle.hpp +64 -0
- package/nitrogen/generated/android/c++/JNumberFormatUnitDisplay.hpp +61 -0
- package/nitrogen/generated/android/c++/views/JHybridNitroInputViewStateUpdater.cpp +25 -0
- package/nitrogen/generated/android/c++/views/JHybridNitroNumberViewStateUpdater.cpp +329 -0
- package/nitrogen/generated/android/c++/views/JHybridNitroNumberViewStateUpdater.hpp +54 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/Func_void.kt +78 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/Func_void_double_double.kt +9 -9
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputViewSpec.kt +30 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroNumberFormatPlatformSpec.kt +60 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroNumberViewSpec.kt +399 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroPlatformNumberFormatterSpec.kt +62 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberAffixAlign.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberDirection.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberEasing.kt +26 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberRevealStyle.kt +23 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberTextAlign.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberTransition.kt +24 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatCompactDisplay.kt +23 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatCurrencyDisplay.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatCurrencySign.kt +23 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatNotation.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatPlatformOptions.kt +136 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatPlatformSymbols.kt +91 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatRoundingMode.kt +30 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatSignDisplay.kt +26 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatStyle.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatUnitDisplay.kt +24 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/views/HybridNitroNumberViewManager.kt +96 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/views/HybridNitroNumberViewStateUpdater.kt +23 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.cpp +59 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.hpp +311 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Umbrella.hpp +63 -0
- package/nitrogen/generated/ios/NitroInputAutolinking.mm +26 -0
- package/nitrogen/generated/ios/NitroInputAutolinking.swift +24 -0
- package/nitrogen/generated/ios/c++/HybridNitroInputViewSpecSwift.hpp +30 -0
- package/nitrogen/generated/ios/c++/HybridNitroNumberFormatPlatformSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridNitroNumberFormatPlatformSpecSwift.hpp +122 -0
- package/nitrogen/generated/ios/c++/HybridNitroNumberViewSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridNitroNumberViewSpecSwift.hpp +474 -0
- package/nitrogen/generated/ios/c++/HybridNitroPlatformNumberFormatterSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridNitroPlatformNumberFormatterSpecSwift.hpp +94 -0
- package/nitrogen/generated/ios/c++/views/HybridNitroInputViewComponent.mm +30 -0
- package/nitrogen/generated/ios/c++/views/HybridNitroNumberViewComponent.mm +450 -0
- package/nitrogen/generated/ios/swift/Func_void.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_double_double.swift +5 -5
- package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec.swift +5 -0
- package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec_cxx.swift +55 -0
- package/nitrogen/generated/ios/swift/HybridNitroNumberFormatPlatformSpec.swift +56 -0
- package/nitrogen/generated/ios/swift/HybridNitroNumberFormatPlatformSpec_cxx.swift +159 -0
- package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec.swift +108 -0
- package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec_cxx.swift +1387 -0
- package/nitrogen/generated/ios/swift/HybridNitroPlatformNumberFormatterSpec.swift +56 -0
- package/nitrogen/generated/ios/swift/HybridNitroPlatformNumberFormatterSpec_cxx.swift +155 -0
- package/nitrogen/generated/ios/swift/NitroNumberAffixAlign.swift +48 -0
- package/nitrogen/generated/ios/swift/NitroNumberDirection.swift +48 -0
- package/nitrogen/generated/ios/swift/NitroNumberEasing.swift +52 -0
- package/nitrogen/generated/ios/swift/NitroNumberRevealStyle.swift +40 -0
- package/nitrogen/generated/ios/swift/NitroNumberTextAlign.swift +48 -0
- package/nitrogen/generated/ios/swift/NitroNumberTransition.swift +44 -0
- package/nitrogen/generated/ios/swift/NumberFormatCompactDisplay.swift +40 -0
- package/nitrogen/generated/ios/swift/NumberFormatCurrencyDisplay.swift +48 -0
- package/nitrogen/generated/ios/swift/NumberFormatCurrencySign.swift +40 -0
- package/nitrogen/generated/ios/swift/NumberFormatNotation.swift +48 -0
- package/nitrogen/generated/ios/swift/NumberFormatPlatformOptions.swift +159 -0
- package/nitrogen/generated/ios/swift/NumberFormatPlatformSymbols.swift +69 -0
- package/nitrogen/generated/ios/swift/NumberFormatRoundingMode.swift +68 -0
- package/nitrogen/generated/ios/swift/NumberFormatSignDisplay.swift +52 -0
- package/nitrogen/generated/ios/swift/NumberFormatStyle.swift +48 -0
- package/nitrogen/generated/ios/swift/NumberFormatUnitDisplay.swift +44 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.cpp +10 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.hpp +10 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatFactorySpec.cpp +22 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatFactorySpec.hpp +70 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatPlatformSpec.cpp +22 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatPlatformSpec.hpp +70 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatSpec.cpp +23 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatSpec.hpp +71 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.cpp +127 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.hpp +187 -0
- package/nitrogen/generated/shared/c++/HybridNitroPlatformNumberFormatterSpec.cpp +23 -0
- package/nitrogen/generated/shared/c++/HybridNitroPlatformNumberFormatterSpec.hpp +65 -0
- package/nitrogen/generated/shared/c++/NitroNumberAffixAlign.hpp +84 -0
- package/nitrogen/generated/shared/c++/NitroNumberDirection.hpp +84 -0
- package/nitrogen/generated/shared/c++/NitroNumberEasing.hpp +88 -0
- package/nitrogen/generated/shared/c++/NitroNumberRevealStyle.hpp +76 -0
- package/nitrogen/generated/shared/c++/NitroNumberTextAlign.hpp +84 -0
- package/nitrogen/generated/shared/c++/NitroNumberTransition.hpp +80 -0
- package/nitrogen/generated/shared/c++/NumberFormatCompactDisplay.hpp +76 -0
- package/nitrogen/generated/shared/c++/NumberFormatCurrencyDisplay.hpp +84 -0
- package/nitrogen/generated/shared/c++/NumberFormatCurrencySign.hpp +76 -0
- package/nitrogen/generated/shared/c++/NumberFormatGrouping.hpp +80 -0
- package/nitrogen/generated/shared/c++/NumberFormatNotation.hpp +84 -0
- package/nitrogen/generated/shared/c++/NumberFormatOptions.hpp +193 -0
- package/nitrogen/generated/shared/c++/NumberFormatPart.hpp +87 -0
- package/nitrogen/generated/shared/c++/NumberFormatPlatformOptions.hpp +176 -0
- package/nitrogen/generated/shared/c++/NumberFormatPlatformSymbols.hpp +115 -0
- package/nitrogen/generated/shared/c++/NumberFormatRoundingMode.hpp +104 -0
- package/nitrogen/generated/shared/c++/NumberFormatRoundingPriority.hpp +80 -0
- package/nitrogen/generated/shared/c++/NumberFormatSignDisplay.hpp +88 -0
- package/nitrogen/generated/shared/c++/NumberFormatStyle.hpp +84 -0
- package/nitrogen/generated/shared/c++/NumberFormatTrailingZeroDisplay.hpp +76 -0
- package/nitrogen/generated/shared/c++/NumberFormatUnitDisplay.hpp +80 -0
- package/nitrogen/generated/shared/c++/ResolvedNumberFormatOptions.hpp +197 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.cpp +10 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.hpp +15 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.cpp +136 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.hpp +245 -0
- package/nitrogen/generated/shared/json/NitroInputViewConfig.json +5 -0
- package/nitrogen/generated/shared/json/NitroNumberViewConfig.json +61 -0
- package/package.json +15 -7
- package/src/NitroInput.tsx +79 -67
- package/src/NitroNumber.tsx +531 -0
- package/src/NumberFormat.ts +563 -0
- package/src/formatProps.ts +59 -0
- package/src/index.ts +38 -6
- package/src/specs/NitroInput.nitro.ts +19 -9
- package/src/specs/NitroNumber.nitro.ts +271 -0
- package/src/specs/NumberFormat.nitro.ts +145 -0
- package/src/styleHelpers.ts +32 -0
- package/android/src/main/cpp/JMorphEngine.cpp +0 -132
- package/lib/commonjs/src/MorphInput.js +0 -42
- package/lib/module/src/MorphInput.js +0 -6
- package/lib/typescript/src/MorphInput.d.ts +0 -18
- package/src/MorphInput.tsx +0 -30
|
@@ -0,0 +1,1234 @@
|
|
|
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, or
|
|
434
|
+
// with `shortest` the shorter way round for this wheel alone (8 → 2
|
|
435
|
+
// rolls back through 5), a tie (five apart) going with the change.
|
|
436
|
+
const double base = wrap(from.position);
|
|
437
|
+
from.position = base;
|
|
438
|
+
from.linear = false;
|
|
439
|
+
const double up = wrap(digit - base);
|
|
440
|
+
const double down = wrap(base - digit);
|
|
441
|
+
double delta = increasing ? up : -down;
|
|
442
|
+
if (direction_ == 3 && up != down) delta = up < down ? up : -down;
|
|
443
|
+
to = Wheel{base + delta, 1.0, false, false};
|
|
444
|
+
}
|
|
445
|
+
next.finals.push_back(Wheel{digit, 1.0, false, false});
|
|
446
|
+
} else {
|
|
447
|
+
// Disappearing wheel: roll down to blank while shrinking.
|
|
448
|
+
if (!from.linear) {
|
|
449
|
+
from.position = wrap(from.position);
|
|
450
|
+
}
|
|
451
|
+
from.linear = true;
|
|
452
|
+
to = Wheel{-1.0, 0.0, true, current.blankZero};
|
|
453
|
+
}
|
|
454
|
+
next.wheels.push_back(WheelTransition{from, to});
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
int RollingEngine::shownGlyph(const Wheel& wheel) {
|
|
459
|
+
if (wheel.blend < 1) {
|
|
460
|
+
return static_cast<int>(wheel.toGlyph);
|
|
461
|
+
}
|
|
462
|
+
if (wheel.linear && wheel.position < -0.5) {
|
|
463
|
+
return -1;
|
|
464
|
+
}
|
|
465
|
+
const int digit = static_cast<int>(std::llround(wrap(wheel.position))) % 10;
|
|
466
|
+
if (wheel.blankZero && digit == 0) {
|
|
467
|
+
return -1;
|
|
468
|
+
}
|
|
469
|
+
return digit;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
void RollingEngine::planNumeric(Transition& next, const Target& target, bool increasing, int count, int mandatory, double now) const {
|
|
473
|
+
(void)mandatory;
|
|
474
|
+
const int currentCount = static_cast<int>(wheels_.size());
|
|
475
|
+
const int pad = trailingPad_;
|
|
476
|
+
for (int power = 0; power < count; power++) {
|
|
477
|
+
const Wheel current = power < currentCount ? wheels_[static_cast<size_t>(power)] : Wheel{-1.0, 0.0, true, false};
|
|
478
|
+
// The glyph the wheel shows, or was arriving at when re-targeted: that is
|
|
479
|
+
// the one that leaves now.
|
|
480
|
+
const int showing = power < currentCount ? shownGlyph(current) : -1;
|
|
481
|
+
Wheel from;
|
|
482
|
+
Wheel to;
|
|
483
|
+
from.width = current.width;
|
|
484
|
+
from.fromGlyph = static_cast<double>(showing);
|
|
485
|
+
// A value that grew moves the glyphs up (the new one arrives from below).
|
|
486
|
+
from.fromAbove = !increasing;
|
|
487
|
+
from.linear = false;
|
|
488
|
+
from.blankZero = false;
|
|
489
|
+
// Below `pad`: decimal columns the format dropped (`changeFormat`).
|
|
490
|
+
if (power >= pad && power - pad < target.powerCount) {
|
|
491
|
+
const int digit = target.digit(power - pad);
|
|
492
|
+
from.toGlyph = static_cast<double>(digit);
|
|
493
|
+
from.position = static_cast<double>(digit);
|
|
494
|
+
to = Wheel{static_cast<double>(digit), 1.0, false, false};
|
|
495
|
+
next.finals.push_back(Wheel{static_cast<double>(digit), 1.0, false, false});
|
|
496
|
+
} else {
|
|
497
|
+
// Disappearing wheel: its glyph leaves and the column closes.
|
|
498
|
+
from.toGlyph = -1;
|
|
499
|
+
from.position = static_cast<double>(std::max(0, showing));
|
|
500
|
+
to = Wheel{from.position, 0.0, false, false};
|
|
501
|
+
}
|
|
502
|
+
// A wheel whose glyph does not change is not part of the transition (it
|
|
503
|
+
// may still be growing to full width).
|
|
504
|
+
from.blend = showing == static_cast<int>(from.toGlyph) ? 1.0 : 0.0;
|
|
505
|
+
WheelTransition wt{from, to};
|
|
506
|
+
// Unless it is still swapping to that glyph: typing re-targets every few
|
|
507
|
+
// frames, and a wheel cut out of its swap there jumped to the settled
|
|
508
|
+
// glyph, or, a new leading digit whose column was still opening, was
|
|
509
|
+
// drawn the odometer's way, a sliver clipped to the half-open column. It
|
|
510
|
+
// carries on from where it was, leaving the glyph it was leaving.
|
|
511
|
+
if (from.blend >= 1 && transition_.active && transition_.numeric && power < currentCount &&
|
|
512
|
+
power < static_cast<int>(transition_.wheels.size()) && power < static_cast<int>(transition_.delays.size())) {
|
|
513
|
+
const WheelTransition& previous = transition_.wheels[static_cast<size_t>(power)];
|
|
514
|
+
const double ran = now - (transition_.start + transition_.delays[static_cast<size_t>(power)]);
|
|
515
|
+
const bool swapping = previous.from.blend < 1 || previous.continues;
|
|
516
|
+
if (swapping && ran + previous.clock < transition_.duration) {
|
|
517
|
+
wt.continues = true;
|
|
518
|
+
wt.clock = std::max(0.0, ran) + previous.clock;
|
|
519
|
+
wt.from.fromGlyph = previous.from.fromGlyph;
|
|
520
|
+
wt.from.fromAbove = previous.from.fromAbove;
|
|
521
|
+
wt.from.blend = 0;
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
next.wheels.push_back(wt);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
void RollingEngine::snap(const Target& target) {
|
|
529
|
+
endTransition();
|
|
530
|
+
cancelReveal();
|
|
531
|
+
wheels_.clear();
|
|
532
|
+
wheels_.reserve(static_cast<size_t>(target.powerCount));
|
|
533
|
+
for (int power = 0; power < target.powerCount; power++) {
|
|
534
|
+
wheels_.push_back(Wheel{static_cast<double>(target.digit(power)), 1.0, false, false});
|
|
535
|
+
}
|
|
536
|
+
signFactor_ = target.negative ? 1.0 : 0.0;
|
|
537
|
+
hasShownValue_ = true;
|
|
538
|
+
settle(target);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
void RollingEngine::setLoading(bool loading, double now) {
|
|
542
|
+
if (loading == loading_) {
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
loading_ = loading;
|
|
546
|
+
loadingFadeFrom_ = loadingProgress_;
|
|
547
|
+
loadingFadeStart_ = now;
|
|
548
|
+
loadingFadeActive_ = true;
|
|
549
|
+
if (loading) {
|
|
550
|
+
shimmerStart_ = now;
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
// MARK: - Jackpot reveal
|
|
555
|
+
|
|
556
|
+
void RollingEngine::setRevealTiming(double durationSeconds, double bounce, int style, double staggerSeconds) {
|
|
557
|
+
revealDuration_ = std::max(0.0, durationSeconds);
|
|
558
|
+
revealBounce_ = clamp01(bounce);
|
|
559
|
+
revealStyle_ = style == 1 ? 1 : 0;
|
|
560
|
+
revealStagger_ = std::max(0.0, staggerSeconds);
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
double RollingEngine::revealTotalSeconds() const {
|
|
564
|
+
const double popTail = revealBounce_ > 0 ? kRevealTailSeconds : 0;
|
|
565
|
+
const double holds = revealStyle_ == 1 ? 0 : revealMilestoneHold_ * static_cast<double>(reveal_.milestoneTimes.size());
|
|
566
|
+
// The reels need no tail of their own: the last clunk ends on the duration.
|
|
567
|
+
return revealDuration_ + holds + popTail;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
void RollingEngine::clearRevealMilestones() {
|
|
571
|
+
revealMilestones_.clear();
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
void RollingEngine::addRevealMilestone(double value) {
|
|
575
|
+
if (std::isfinite(value)) {
|
|
576
|
+
revealMilestones_.push_back(value);
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
void RollingEngine::setRevealMilestoneHold(double holdSeconds) {
|
|
581
|
+
revealMilestoneHold_ = std::max(0.0, holdSeconds);
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
double RollingEngine::revealMilestoneValue(int index) const {
|
|
585
|
+
if (index < 0 || index >= static_cast<int>(reveal_.milestoneMagnitudes.size())) {
|
|
586
|
+
return 0;
|
|
587
|
+
}
|
|
588
|
+
return reveal_.milestoneMagnitudes[static_cast<size_t>(index)] / static_cast<double>(kPow10[fractionDigits_]);
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
/// Stops a reveal. `milestonesReached` survives so the view can still see a
|
|
592
|
+
/// milestone that was reached on the reveal's very last tick.
|
|
593
|
+
void RollingEngine::cancelReveal() {
|
|
594
|
+
reveal_.active = false;
|
|
595
|
+
reveal_.counting = false;
|
|
596
|
+
reveal_.holding = false;
|
|
597
|
+
revealScale_ = 1;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
/// One punch: rises to `overshoot` at kPunchPeakSeconds and eases back to
|
|
601
|
+
/// zero (x·e^(1−x)), never negative, ~2 % of the peak left at the tail.
|
|
602
|
+
double RollingEngine::punch(double tau, double overshoot) {
|
|
603
|
+
if (tau <= 0 || overshoot <= 0) {
|
|
604
|
+
return 0;
|
|
605
|
+
}
|
|
606
|
+
const double x = tau / kPunchPeakSeconds;
|
|
607
|
+
return overshoot * x * std::exp(1 - x);
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
void RollingEngine::setRevealGrow(double grow) {
|
|
611
|
+
revealGrow_ = clamp01(grow);
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
/// Resolves the milestones for the target. Like a slot's tiered rollup, the
|
|
615
|
+
/// count then runs tier by tier: every tier gets an equal share of the
|
|
616
|
+
/// duration whatever its size (the counter simply runs faster in a bigger
|
|
617
|
+
/// tier), decelerates into its milestone, punches, pauses for the hold and
|
|
618
|
+
/// accelerates again into the next one.
|
|
619
|
+
void RollingEngine::planMilestones() {
|
|
620
|
+
reveal_.milestoneMagnitudes.clear();
|
|
621
|
+
reveal_.milestoneTimes.clear();
|
|
622
|
+
reveal_.milestonesReached = 0;
|
|
623
|
+
const double magnitude = static_cast<double>(reveal_.target.magnitude);
|
|
624
|
+
if (magnitude <= 0 || revealDuration_ <= 0 || reduceMotion_) {
|
|
625
|
+
return;
|
|
626
|
+
}
|
|
627
|
+
std::vector<double> sorted = revealMilestones_;
|
|
628
|
+
std::sort(sorted.begin(), sorted.end());
|
|
629
|
+
for (double value : sorted) {
|
|
630
|
+
const double m = std::round(std::fabs(value) * static_cast<double>(kPow10[fractionDigits_]));
|
|
631
|
+
if (m <= 0 || m >= magnitude) {
|
|
632
|
+
continue;
|
|
633
|
+
}
|
|
634
|
+
if (!reveal_.milestoneMagnitudes.empty() && m <= reveal_.milestoneMagnitudes.back()) {
|
|
635
|
+
continue;
|
|
636
|
+
}
|
|
637
|
+
reveal_.milestoneMagnitudes.push_back(m);
|
|
638
|
+
}
|
|
639
|
+
const double segment = revealDuration_ / static_cast<double>(reveal_.milestoneMagnitudes.size() + 1);
|
|
640
|
+
for (size_t i = 0; i < reveal_.milestoneMagnitudes.size(); i++) {
|
|
641
|
+
reveal_.milestoneTimes.push_back(segment * static_cast<double>(i + 1));
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
void RollingEngine::holdReveal(double value) {
|
|
646
|
+
endTransition();
|
|
647
|
+
cancelReveal();
|
|
648
|
+
targetValue_ = value;
|
|
649
|
+
hasShownValue_ = true;
|
|
650
|
+
reveal_.holding = true;
|
|
651
|
+
reveal_.milestonesReached = 0;
|
|
652
|
+
reveal_.milestoneTimes.clear();
|
|
653
|
+
reveal_.milestoneMagnitudes.clear();
|
|
654
|
+
reveal_.target = makeTarget(value);
|
|
655
|
+
reveal_.start = 0;
|
|
656
|
+
applyReveal(0);
|
|
657
|
+
settle(reveal_.target);
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
void RollingEngine::reveal(double value, double now) {
|
|
661
|
+
endTransition();
|
|
662
|
+
targetValue_ = value;
|
|
663
|
+
const Target target = makeTarget(value);
|
|
664
|
+
if (revealDuration_ <= 0 || reduceMotion_) {
|
|
665
|
+
snap(target);
|
|
666
|
+
return;
|
|
667
|
+
}
|
|
668
|
+
if (!reveal_.active) {
|
|
669
|
+
reveal_.start = now;
|
|
670
|
+
}
|
|
671
|
+
reveal_.active = true;
|
|
672
|
+
reveal_.holding = false;
|
|
673
|
+
reveal_.target = target;
|
|
674
|
+
reveal_.milestonesReached = 0;
|
|
675
|
+
reveal_.milestoneTimes.clear();
|
|
676
|
+
reveal_.milestoneMagnitudes.clear();
|
|
677
|
+
hasShownValue_ = true;
|
|
678
|
+
if (revealStyle_ == 1) {
|
|
679
|
+
planReels();
|
|
680
|
+
} else {
|
|
681
|
+
planMilestones();
|
|
682
|
+
}
|
|
683
|
+
applyReveal(now - reveal_.start);
|
|
684
|
+
settle(target);
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
/// Lays out the spin-style reels: stop times from the left, then for each reel
|
|
688
|
+
/// the brake window and the braking distance that ends on its digit.
|
|
689
|
+
void RollingEngine::planReels() {
|
|
690
|
+
const Target& target = reveal_.target;
|
|
691
|
+
const int count = target.powerCount;
|
|
692
|
+
// Reel k (from the left) locks at lead + k * stagger. The last one locks a
|
|
693
|
+
// clunk short of the duration, so its bounce is spent by the time the figure
|
|
694
|
+
// pops. The stagger shrinks when the reels wouldn't fit between the earliest
|
|
695
|
+
// lead and that last lock; the lead is still at least kReelMinLead.
|
|
696
|
+
const double last = std::max(0.0, revealDuration_ - kReelBounceSeconds);
|
|
697
|
+
double stagger = revealStagger_;
|
|
698
|
+
const double room = std::max(0.0, last - revealDuration_ * kReelMinLead);
|
|
699
|
+
if (count > 1 && stagger * (count - 1) > room) {
|
|
700
|
+
stagger = room / (count - 1);
|
|
701
|
+
}
|
|
702
|
+
const double lead = last - stagger * (count - 1);
|
|
703
|
+
reveal_.reels.assign(static_cast<size_t>(count), Reel{});
|
|
704
|
+
for (int k = 0; k < count; k++) {
|
|
705
|
+
Reel& reel = reveal_.reels[static_cast<size_t>(k)];
|
|
706
|
+
const int power = count - 1 - k;
|
|
707
|
+
// Different starting digits per reel, so the spinning columns don't move in unison.
|
|
708
|
+
reel.phase = std::fmod(static_cast<double>(k) * 3.7 + 0.5, 10.0);
|
|
709
|
+
reel.stop = lead + stagger * k;
|
|
710
|
+
reel.brakeStart = std::max(0.0, reel.stop - kReelBrakeSeconds);
|
|
711
|
+
reel.from = reel.phase + kReelSpeed * reel.brakeStart;
|
|
712
|
+
// Brake over the occurrence of the digit 4–14 strip positions ahead: with
|
|
713
|
+
// 12 digits of free travel per brake window the curve never reverses.
|
|
714
|
+
const double digit = static_cast<double>(target.digit(power));
|
|
715
|
+
double travel = wrap(digit - reel.from);
|
|
716
|
+
if (travel < 4) {
|
|
717
|
+
travel += 10;
|
|
718
|
+
}
|
|
719
|
+
reel.travel = travel;
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
/// Clock fraction → value fraction of one tally run. The counter's rate winds
|
|
724
|
+
/// up linearly over `rampIn`, holds steady, then falls off quadratically over
|
|
725
|
+
/// `rampOut`, so the count is linear in the middle like a slot's tally and
|
|
726
|
+
/// crawls the last stretch into its target instead of slamming into it.
|
|
727
|
+
double RollingEngine::tally(double t, double rampIn, double rampOut) {
|
|
728
|
+
if (t <= 0) {
|
|
729
|
+
return 0;
|
|
730
|
+
}
|
|
731
|
+
if (t >= 1) {
|
|
732
|
+
return 1;
|
|
733
|
+
}
|
|
734
|
+
const double area = rampIn / 2 + (1 - rampIn - rampOut) + rampOut / 3;
|
|
735
|
+
double distance;
|
|
736
|
+
if (t < rampIn) {
|
|
737
|
+
distance = t * t / (2 * rampIn);
|
|
738
|
+
} else if (t < 1 - rampOut) {
|
|
739
|
+
distance = rampIn / 2 + (t - rampIn);
|
|
740
|
+
} else {
|
|
741
|
+
const double rest = 1 - t;
|
|
742
|
+
distance = area - rest * rest * rest / (3 * rampOut * rampOut);
|
|
743
|
+
}
|
|
744
|
+
return distance / area;
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
void RollingEngine::applyReveal(double elapsed) {
|
|
748
|
+
const bool spin = revealStyle_ == 1 && !reveal_.holding;
|
|
749
|
+
// The scale: the figure's size as the count grows, times the punches. A
|
|
750
|
+
// closed form of the clock, so the whole timeline is deterministic.
|
|
751
|
+
double base = 1;
|
|
752
|
+
double punches = 0;
|
|
753
|
+
if (spin) {
|
|
754
|
+
applyRevealSpin(elapsed);
|
|
755
|
+
reveal_.counting = elapsed < revealDuration_;
|
|
756
|
+
punches += punch(elapsed - revealDuration_, revealBounce_);
|
|
757
|
+
} else {
|
|
758
|
+
applyRevealCount(elapsed);
|
|
759
|
+
// The figure opens smaller and grows with the tally, the way a big win's
|
|
760
|
+
// meter is enlarged as it climbs; each milestone punches when reached and
|
|
761
|
+
// the landing punches hardest. The count has finished once the curve and
|
|
762
|
+
// every hold are behind us.
|
|
763
|
+
const double hold = revealMilestoneHold_;
|
|
764
|
+
const size_t reached = static_cast<size_t>(reveal_.milestonesReached);
|
|
765
|
+
for (size_t i = 0; i < reached; i++) {
|
|
766
|
+
const double wall = reveal_.milestoneTimes[i] + hold * static_cast<double>(i);
|
|
767
|
+
punches += punch(elapsed - wall, revealBounce_ * kMilestonePunch);
|
|
768
|
+
}
|
|
769
|
+
const double end = revealDuration_ + hold * static_cast<double>(reveal_.milestoneTimes.size());
|
|
770
|
+
reveal_.counting = elapsed < end;
|
|
771
|
+
if (!reveal_.holding) {
|
|
772
|
+
base = 1 - revealGrow_ * (1 - clamp01(end > 0 ? elapsed / end : 1.0));
|
|
773
|
+
}
|
|
774
|
+
punches += punch(elapsed - end, revealBounce_);
|
|
775
|
+
}
|
|
776
|
+
signFactor_ = reveal_.target.negative ? 1.0 : 0.0;
|
|
777
|
+
revealScale_ = base * (1 + punches);
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
void RollingEngine::applyRevealCount(double elapsed) {
|
|
781
|
+
const Target& target = reveal_.target;
|
|
782
|
+
const double magnitude = static_cast<double>(target.magnitude);
|
|
783
|
+
|
|
784
|
+
// Wall time → curve time: the clock stands still on each milestone for the
|
|
785
|
+
// hold, and the count sits exactly on the milestone meanwhile.
|
|
786
|
+
double curve = elapsed;
|
|
787
|
+
int reached = 0;
|
|
788
|
+
double heldAt = -1;
|
|
789
|
+
const double hold = revealMilestoneHold_;
|
|
790
|
+
for (size_t i = 0; i < reveal_.milestoneTimes.size(); i++) {
|
|
791
|
+
const double wall = reveal_.milestoneTimes[i] + hold * static_cast<double>(i);
|
|
792
|
+
if (elapsed < wall) {
|
|
793
|
+
break;
|
|
794
|
+
}
|
|
795
|
+
reached = static_cast<int>(i) + 1;
|
|
796
|
+
const double consumed = std::min(hold, elapsed - wall);
|
|
797
|
+
curve -= consumed;
|
|
798
|
+
if (consumed < hold) {
|
|
799
|
+
heldAt = reveal_.milestoneMagnitudes[i];
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
reveal_.milestonesReached = std::max(reveal_.milestonesReached, reached);
|
|
803
|
+
|
|
804
|
+
// The count: a slot's tally. One tier at a time (the whole figure is one
|
|
805
|
+
// tier without milestones), each running linearly in value at a constant
|
|
806
|
+
// rate, winding up out of the tier's start and crawling into its end, so it
|
|
807
|
+
// reads slow → fast → slow and lands squarely on every milestone. The
|
|
808
|
+
// final tier crawls longer so the total is readable as it arrives.
|
|
809
|
+
double count;
|
|
810
|
+
if (heldAt >= 0) {
|
|
811
|
+
count = heldAt;
|
|
812
|
+
} else {
|
|
813
|
+
const size_t tiers = reveal_.milestoneMagnitudes.size() + 1;
|
|
814
|
+
const double segment = revealDuration_ > 0 ? revealDuration_ / static_cast<double>(tiers) : 0;
|
|
815
|
+
size_t tier = segment > 0 ? static_cast<size_t>(std::floor(curve / segment)) : tiers - 1;
|
|
816
|
+
tier = std::min(tier, tiers - 1);
|
|
817
|
+
const double u = segment > 0 ? clamp01((curve - segment * static_cast<double>(tier)) / segment) : 1.0;
|
|
818
|
+
const double lo = tier == 0 ? 0 : reveal_.milestoneMagnitudes[tier - 1];
|
|
819
|
+
const double hi = tier == tiers - 1 ? magnitude : reveal_.milestoneMagnitudes[tier];
|
|
820
|
+
const double rampOut = tier == tiers - 1 ? kTallyFinalRampOut : kTallyRampOut;
|
|
821
|
+
// ceil, not floor: the count leaves its start on the first moving frame.
|
|
822
|
+
count = std::min(hi, std::ceil(lo + (hi - lo) * tally(u, kTallyRampIn, rampOut)));
|
|
823
|
+
}
|
|
824
|
+
const int mandatory = std::min(kMaxPowerCount, minimumIntegerDigits_ + fractionDigits_);
|
|
825
|
+
const size_t powerCount = static_cast<size_t>(target.powerCount);
|
|
826
|
+
|
|
827
|
+
wheels_.resize(powerCount);
|
|
828
|
+
for (size_t power = 0; power < powerCount; power++) {
|
|
829
|
+
const double p10 = static_cast<double>(kPow10[power]);
|
|
830
|
+
// A digit left of the count's leading digit stays blank (no leading zeros),
|
|
831
|
+
// except the mandatory fraction / zero-padded digits.
|
|
832
|
+
const bool hidden = static_cast<int>(power) >= mandatory && count < p10;
|
|
833
|
+
if (hidden) {
|
|
834
|
+
wheels_[power] = Wheel{-1.0, 0.0, true, false};
|
|
835
|
+
} else {
|
|
836
|
+
wheels_[power] = Wheel{std::fmod(std::floor(count / p10), 10.0), 1.0, false, false};
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
void RollingEngine::applyRevealSpin(double elapsed) {
|
|
842
|
+
const size_t powerCount = static_cast<size_t>(reveal_.target.powerCount);
|
|
843
|
+
if (reveal_.reels.size() != powerCount) {
|
|
844
|
+
planReels();
|
|
845
|
+
}
|
|
846
|
+
wheels_.resize(powerCount);
|
|
847
|
+
for (size_t power = 0; power < powerCount; power++) {
|
|
848
|
+
const Reel& reel = reveal_.reels[powerCount - 1 - power];
|
|
849
|
+
double position;
|
|
850
|
+
if (elapsed < reel.brakeStart) {
|
|
851
|
+
position = reel.phase + kReelSpeed * elapsed;
|
|
852
|
+
} else if (elapsed < reel.stop) {
|
|
853
|
+
// Cubic Hermite from full speed to a standstill on the digit.
|
|
854
|
+
const double window = reel.stop - reel.brakeStart;
|
|
855
|
+
const double s = window > 0 ? clamp01((elapsed - reel.brakeStart) / window) : 1.0;
|
|
856
|
+
const double v = kReelSpeed * window;
|
|
857
|
+
const double d = reel.travel;
|
|
858
|
+
position = reel.from + v * s + (3 * d - 2 * v) * s * s + (v - 2 * d) * s * s * s;
|
|
859
|
+
} else {
|
|
860
|
+
// Locked, with the catch's bounce: past the digit and back.
|
|
861
|
+
const double tau = elapsed - reel.stop;
|
|
862
|
+
position = reel.from + reel.travel;
|
|
863
|
+
if (tau < kReelBounceSeconds) {
|
|
864
|
+
position += kReelBounceDigits * std::sin(kPi * tau / kReelBounceSeconds);
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
wheels_[power] = Wheel{position, 1.0, false, false};
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
// MARK: - Animation
|
|
872
|
+
|
|
873
|
+
void RollingEngine::apply(double elapsed) {
|
|
874
|
+
const Transition& tr = transition_;
|
|
875
|
+
wheels_.resize(tr.wheels.size());
|
|
876
|
+
for (size_t i = 0; i < tr.wheels.size(); i++) {
|
|
877
|
+
const WheelTransition& wt = tr.wheels[i];
|
|
878
|
+
const double delay = i < tr.delays.size() ? tr.delays[i] : 0;
|
|
879
|
+
const double raw = tr.duration > 0 ? clamp01((elapsed - delay) / tr.duration) : 1.0;
|
|
880
|
+
// The numeric transition's tail is only its position spring ringing out:
|
|
881
|
+
// a column opens and closes over the duration itself.
|
|
882
|
+
const double span = tr.style == 1 ? tr.duration / kNumericTail : tr.duration;
|
|
883
|
+
const double rawSpan = span > 0 ? clamp01((elapsed - delay) / span) : 1.0;
|
|
884
|
+
double t = tr.fromMotion ? easeFromMotion(rawSpan) : ease(rawSpan);
|
|
885
|
+
if (tr.numeric) {
|
|
886
|
+
// A column opens on the arriving glyph's size-and-opacity spring and
|
|
887
|
+
// closes on the leaving glyph's blur, the faster one: on the
|
|
888
|
+
// transition's easing it lagged the glyph, and a digit that had already
|
|
889
|
+
// left stood as a gap in the number until its column caught up.
|
|
890
|
+
const double settle = wt.to.width < wt.from.width ? kNumericBlurOutSettle : kNumericGrowSettle;
|
|
891
|
+
t = rawSpan >= 1 ? 1.0 : damped(std::max(0.0, elapsed - delay), 1.0, settle * span);
|
|
892
|
+
}
|
|
893
|
+
Wheel& w = wheels_[i];
|
|
894
|
+
w.width = clamp01(wt.from.width + (wt.to.width - wt.from.width) * t);
|
|
895
|
+
w.linear = wt.from.linear;
|
|
896
|
+
w.blankZero = wt.from.blankZero;
|
|
897
|
+
if (tr.numeric && wt.from.blend >= 1) {
|
|
898
|
+
// A wheel whose glyph does not change (it may still be growing to full width).
|
|
899
|
+
w.position = wt.from.position;
|
|
900
|
+
w.fromGlyph = -1;
|
|
901
|
+
w.toGlyph = -1;
|
|
902
|
+
w.blend = 1;
|
|
903
|
+
w.focus = 1;
|
|
904
|
+
w.grow = 1;
|
|
905
|
+
w.progress = 1;
|
|
906
|
+
} else if (tr.style == 2) {
|
|
907
|
+
// Scramble: a different digit every step until the wheel locks.
|
|
908
|
+
const int from = static_cast<int>(wt.from.fromGlyph);
|
|
909
|
+
const int to = static_cast<int>(wt.from.toGlyph);
|
|
910
|
+
if (raw >= 1) {
|
|
911
|
+
w.position = static_cast<double>(std::max(0, to));
|
|
912
|
+
} else {
|
|
913
|
+
const uint64_t step = static_cast<uint64_t>(std::max(0.0, elapsed - delay) / kScrambleStepSeconds);
|
|
914
|
+
// A mixed hash of the step and the wheel (a plain multiply mod 10 is a counter).
|
|
915
|
+
uint64_t h = step * 2654435761ULL + static_cast<uint64_t>(i) * 40503ULL + 7ULL;
|
|
916
|
+
h ^= h >> 13;
|
|
917
|
+
h *= 0x5bd1e995ULL;
|
|
918
|
+
h ^= h >> 15;
|
|
919
|
+
int glyph = static_cast<int>(h % 10);
|
|
920
|
+
// Never sit on the digit the wheel is leaving or arriving at: it would read as a stall.
|
|
921
|
+
while (glyph == from || glyph == to) {
|
|
922
|
+
glyph = (glyph + 1) % 10;
|
|
923
|
+
}
|
|
924
|
+
w.position = static_cast<double>(glyph);
|
|
925
|
+
}
|
|
926
|
+
w.fromGlyph = -1;
|
|
927
|
+
w.toGlyph = -1;
|
|
928
|
+
w.blend = 1;
|
|
929
|
+
w.focus = 1;
|
|
930
|
+
w.grow = 1;
|
|
931
|
+
w.progress = clamp01(t);
|
|
932
|
+
} else if (tr.numeric) {
|
|
933
|
+
// Numeric: the glyphs swap in place; the position is the digit
|
|
934
|
+
// arriving and the three clocks say how far the swap is (see the header).
|
|
935
|
+
w.position = wt.from.position;
|
|
936
|
+
w.fromGlyph = wt.from.fromGlyph;
|
|
937
|
+
w.toGlyph = wt.from.toGlyph;
|
|
938
|
+
w.fromAbove = wt.from.fromAbove;
|
|
939
|
+
// A swap carried over from the previous target is `clock` further in.
|
|
940
|
+
const double local = std::max(0.0, elapsed - delay) + wt.clock;
|
|
941
|
+
if (local >= tr.duration || tr.duration <= 0) {
|
|
942
|
+
w.blend = 1;
|
|
943
|
+
w.grow = 1;
|
|
944
|
+
w.focus = 1;
|
|
945
|
+
w.blurOut = 1;
|
|
946
|
+
} else {
|
|
947
|
+
const double d = tr.duration / kNumericTail;
|
|
948
|
+
w.blend = damped(local, kNumericPositionZeta, d);
|
|
949
|
+
w.grow = damped(local, 1.0, kNumericGrowSettle * d);
|
|
950
|
+
w.focus = damped(local, kNumericFocusZeta, kNumericFocusSettle * d);
|
|
951
|
+
w.blurOut = damped(local, 1.0, kNumericBlurOutSettle * d);
|
|
952
|
+
}
|
|
953
|
+
// The grow clock is critically damped, so a column never rings.
|
|
954
|
+
w.progress = clamp01(w.grow);
|
|
955
|
+
} else {
|
|
956
|
+
w.position = wt.from.position + (wt.to.position - wt.from.position) * t;
|
|
957
|
+
w.fromGlyph = -1;
|
|
958
|
+
w.toGlyph = -1;
|
|
959
|
+
w.blend = 1;
|
|
960
|
+
w.focus = 1;
|
|
961
|
+
w.grow = 1;
|
|
962
|
+
w.progress = clamp01(t);
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
const double signRaw = tr.duration > 0 ? clamp01(elapsed / tr.duration) : 1.0;
|
|
966
|
+
const double signT = tr.fromMotion ? easeFromMotion(signRaw) : ease(signRaw);
|
|
967
|
+
signFactor_ = clamp01(tr.signFrom + (tr.signTo - tr.signFrom) * signT);
|
|
968
|
+
if (tr.decimalFrom != tr.decimalTo) {
|
|
969
|
+
// With the columns: on the glyphs' spring in a swap, the roll's easing in a roll.
|
|
970
|
+
const double span = tr.style == 1 ? tr.duration / kNumericTail : tr.duration;
|
|
971
|
+
const double raw = span > 0 ? clamp01(elapsed / span) : 1.0;
|
|
972
|
+
const double t = raw >= 1 ? 1.0
|
|
973
|
+
: tr.numeric ? damped(std::max(0.0, elapsed), 1.0, kNumericGrowSettle * span)
|
|
974
|
+
: (tr.fromMotion ? easeFromMotion(raw) : ease(raw));
|
|
975
|
+
decimalFactor_ = clamp01(tr.decimalFrom + (tr.decimalTo - tr.decimalFrom) * t);
|
|
976
|
+
} else {
|
|
977
|
+
decimalFactor_ = tr.decimalTo;
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
void RollingEngine::finish() {
|
|
982
|
+
wheels_ = transition_.finals;
|
|
983
|
+
signFactor_ = transition_.signTo;
|
|
984
|
+
decimalFactor_ = transition_.decimalTo;
|
|
985
|
+
if (trailingPad_ > 0) {
|
|
986
|
+
// The closed decimal columns go; the flashes keep their place values.
|
|
987
|
+
const size_t pad = std::min(flashes_.size(), static_cast<size_t>(trailingPad_));
|
|
988
|
+
flashes_.erase(flashes_.begin(), flashes_.begin() + static_cast<std::ptrdiff_t>(pad));
|
|
989
|
+
trailingPad_ = 0;
|
|
990
|
+
}
|
|
991
|
+
transition_.active = false;
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
void RollingEngine::applyEffects(double now) {
|
|
995
|
+
lastNow_ = now;
|
|
996
|
+
bool active = false;
|
|
997
|
+
if (flashes_.size() < wheels_.size()) {
|
|
998
|
+
flashes_.resize(wheels_.size());
|
|
999
|
+
}
|
|
1000
|
+
for (size_t i = 0; i < wheels_.size(); i++) {
|
|
1001
|
+
Wheel& w = wheels_[i];
|
|
1002
|
+
const Flash& f = flashes_[i];
|
|
1003
|
+
if (flashSeconds_ <= 0 || f.start < 0) {
|
|
1004
|
+
w.flash = 0;
|
|
1005
|
+
continue;
|
|
1006
|
+
}
|
|
1007
|
+
// Lights at once, holds while the wheel moves, fades with a tail.
|
|
1008
|
+
const double lit = now - f.start;
|
|
1009
|
+
const double u = lit < f.hold ? 0 : clamp01((lit - f.hold) / flashSeconds_);
|
|
1010
|
+
w.flash = std::pow(1 - u, 1.5);
|
|
1011
|
+
w.flashUp = f.up;
|
|
1012
|
+
if (u < 1) {
|
|
1013
|
+
active = true;
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
if (popStart_ >= 0 && popOnChange_ > 0) {
|
|
1017
|
+
const double tau = now - popStart_;
|
|
1018
|
+
const double p = punch(tau, popOnChange_);
|
|
1019
|
+
popScale_ = 1 + p;
|
|
1020
|
+
if (tau < kPunchPeakSeconds * 8) {
|
|
1021
|
+
active = true;
|
|
1022
|
+
} else {
|
|
1023
|
+
popScale_ = 1;
|
|
1024
|
+
popStart_ = -1;
|
|
1025
|
+
}
|
|
1026
|
+
} else {
|
|
1027
|
+
popScale_ = 1;
|
|
1028
|
+
}
|
|
1029
|
+
effectsActive_ = active;
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
bool RollingEngine::tick(double now) {
|
|
1033
|
+
if (transition_.active) {
|
|
1034
|
+
const double elapsed = now - transition_.start;
|
|
1035
|
+
if (elapsed >= transition_.duration + transition_.maxDelay) {
|
|
1036
|
+
finish();
|
|
1037
|
+
} else {
|
|
1038
|
+
apply(elapsed);
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
if (reveal_.active) {
|
|
1042
|
+
const double elapsed = now - reveal_.start;
|
|
1043
|
+
if (elapsed >= revealTotalSeconds()) {
|
|
1044
|
+
const Target target = reveal_.target;
|
|
1045
|
+
snap(target);
|
|
1046
|
+
} else {
|
|
1047
|
+
applyReveal(elapsed);
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
if (loadingFadeActive_) {
|
|
1051
|
+
const double target = loading_ ? 1.0 : 0.0;
|
|
1052
|
+
const double t = clamp01((now - loadingFadeStart_) / kLoadingFadeSeconds);
|
|
1053
|
+
loadingProgress_ = loadingFadeFrom_ + (target - loadingFadeFrom_) * t;
|
|
1054
|
+
if (t >= 1) {
|
|
1055
|
+
loadingProgress_ = target;
|
|
1056
|
+
loadingFadeActive_ = false;
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
applyEffects(now);
|
|
1060
|
+
applyText(now);
|
|
1061
|
+
return needsFrames();
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
bool RollingEngine::needsFrames() const {
|
|
1065
|
+
const bool sweeping = loadingProgress_ > 0 && !reduceMotion_;
|
|
1066
|
+
return transition_.active || reveal_.active || loadingFadeActive_ || sweeping || effectsActive_ || textActive_;
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
void RollingEngine::changeText(int slot, double now) {
|
|
1070
|
+
if (slot < 0 || slot >= kTextSlots) {
|
|
1071
|
+
return;
|
|
1072
|
+
}
|
|
1073
|
+
if (!hasShownValue_ || duration_ <= 0 || reduceMotion_) {
|
|
1074
|
+
textClocks_[slot].start = -1;
|
|
1075
|
+
textChanges_[slot] = TextChange{};
|
|
1076
|
+
textActive_ = false;
|
|
1077
|
+
for (const TextChange& t : textChanges_) {
|
|
1078
|
+
textActive_ = textActive_ || t.active;
|
|
1079
|
+
}
|
|
1080
|
+
return;
|
|
1081
|
+
}
|
|
1082
|
+
textClocks_[slot].start = now;
|
|
1083
|
+
applyText(now);
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
RollingEngine::TextChange RollingEngine::textChange(int slot) const {
|
|
1087
|
+
return slot >= 0 && slot < kTextSlots ? textChanges_[slot] : TextChange{};
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
void RollingEngine::applyText(double now) {
|
|
1091
|
+
// The numeric transition's clocks for one glyph, over the transition's
|
|
1092
|
+
// duration (not its tail: there is no position spring to ring out).
|
|
1093
|
+
const double d = duration_;
|
|
1094
|
+
bool any = false;
|
|
1095
|
+
for (int i = 0; i < kTextSlots; i++) {
|
|
1096
|
+
TextChange& t = textChanges_[i];
|
|
1097
|
+
const double start = textClocks_[i].start;
|
|
1098
|
+
const double local = now - start;
|
|
1099
|
+
if (start < 0 || d <= 0 || local >= d * kNumericTail) {
|
|
1100
|
+
textClocks_[i].start = -1;
|
|
1101
|
+
t = TextChange{};
|
|
1102
|
+
continue;
|
|
1103
|
+
}
|
|
1104
|
+
t.active = true;
|
|
1105
|
+
t.grow = damped(local, 1.0, kNumericGrowSettle * d);
|
|
1106
|
+
t.focus = std::min(1.0, damped(local, kNumericFocusZeta, kNumericFocusSettle * d));
|
|
1107
|
+
t.blurOut = damped(local, 1.0, kNumericBlurOutSettle * d);
|
|
1108
|
+
any = true;
|
|
1109
|
+
}
|
|
1110
|
+
textActive_ = any;
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
bool RollingEngine::isRolling() const {
|
|
1114
|
+
return transition_.active || reveal_.counting;
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1117
|
+
double RollingEngine::shimmerPhase(double now, double periodSeconds) const {
|
|
1118
|
+
if (reduceMotion_) {
|
|
1119
|
+
return 0;
|
|
1120
|
+
}
|
|
1121
|
+
const double period = std::max(0.2, periodSeconds);
|
|
1122
|
+
return std::fmod(std::max(0.0, now - shimmerStart_) / period, 1.0);
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
double RollingEngine::ease(double t) const {
|
|
1126
|
+
switch (easing_) {
|
|
1127
|
+
case 0:
|
|
1128
|
+
return t;
|
|
1129
|
+
case 1:
|
|
1130
|
+
return t * t * t;
|
|
1131
|
+
case 2:
|
|
1132
|
+
return 1 - std::pow(1 - t, 3);
|
|
1133
|
+
case 4:
|
|
1134
|
+
return spring(t, bounce_);
|
|
1135
|
+
default:
|
|
1136
|
+
return t < 0.5 ? 4 * t * t * t : 1 - std::pow(-2 * t + 2, 3) / 2;
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
double RollingEngine::easeFromMotion(double t) const {
|
|
1141
|
+
switch (easing_) {
|
|
1142
|
+
case 0:
|
|
1143
|
+
case 1:
|
|
1144
|
+
return t; // linear / easeIn: keep the wheel moving at a steady pace
|
|
1145
|
+
case 4:
|
|
1146
|
+
return spring(t, bounce_); // the spring already starts with velocity
|
|
1147
|
+
default:
|
|
1148
|
+
return 1 - std::pow(1 - t, 3); // easeOut / easeInOut: decelerate into the new target
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
/// Step response of a damped spring, normalised so it has settled at t == 1.
|
|
1153
|
+
/// `bounce` maps to the damping ratio like SwiftUI's `.spring(duration:bounce:)`.
|
|
1154
|
+
double RollingEngine::spring(double t, double bounce) {
|
|
1155
|
+
const double zeta = std::min(1.0, std::max(0.05, 1.0 - clamp01(bounce)));
|
|
1156
|
+
const double omega = 3.0 * kPi;
|
|
1157
|
+
const double k = zeta * omega;
|
|
1158
|
+
if (zeta >= 0.999) {
|
|
1159
|
+
return 1 - (1 + k * t) * std::exp(-k * t);
|
|
1160
|
+
}
|
|
1161
|
+
const double wd = omega * std::sqrt(1 - zeta * zeta);
|
|
1162
|
+
return 1 - std::exp(-k * t) * (std::cos(wd * t) + (k / wd) * std::sin(wd * t));
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
double RollingEngine::damped(double t, double zeta, double settle) {
|
|
1166
|
+
if (t <= 0 || settle <= 0) {
|
|
1167
|
+
return 0;
|
|
1168
|
+
}
|
|
1169
|
+
const double z = std::min(1.0, std::max(0.05, zeta));
|
|
1170
|
+
if (z >= 0.999) {
|
|
1171
|
+
// Critically damped: 1 - (1 + kt) e^{-kt} is within 2 % of 1 at kt ≈ 5.83.
|
|
1172
|
+
const double k = 5.83 / settle;
|
|
1173
|
+
return 1 - (1 + k * t) * std::exp(-k * t);
|
|
1174
|
+
}
|
|
1175
|
+
// Underdamped: the envelope e^{-ζωt} is within 2 % at ζωt = 4.
|
|
1176
|
+
const double omega = 4.0 / (z * settle);
|
|
1177
|
+
const double k = z * omega;
|
|
1178
|
+
const double wd = omega * std::sqrt(1 - z * z);
|
|
1179
|
+
return 1 - std::exp(-k * t) * (std::cos(wd * t) + (k / wd) * std::sin(wd * t));
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
int RollingEngine::targetDigit(int power) const {
|
|
1183
|
+
return target_.digit(power);
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
void RollingEngine::reset() {
|
|
1187
|
+
fractionDigits_ = 0;
|
|
1188
|
+
minimumIntegerDigits_ = 1;
|
|
1189
|
+
duration_ = 0.5;
|
|
1190
|
+
easing_ = 3;
|
|
1191
|
+
bounce_ = 0.15;
|
|
1192
|
+
stagger_ = 0;
|
|
1193
|
+
direction_ = 0;
|
|
1194
|
+
transitionStyle_ = 0;
|
|
1195
|
+
flashSeconds_ = 0;
|
|
1196
|
+
popOnChange_ = 0;
|
|
1197
|
+
flashes_.clear();
|
|
1198
|
+
popStart_ = -1;
|
|
1199
|
+
popScale_ = 1;
|
|
1200
|
+
lastNow_ = 0;
|
|
1201
|
+
effectsActive_ = false;
|
|
1202
|
+
trailingPad_ = 0;
|
|
1203
|
+
decimalFactor_ = 1;
|
|
1204
|
+
for (int i = 0; i < kTextSlots; i++) {
|
|
1205
|
+
textClocks_[i] = TextClock{};
|
|
1206
|
+
textChanges_[i] = TextChange{};
|
|
1207
|
+
}
|
|
1208
|
+
textActive_ = false;
|
|
1209
|
+
revealDuration_ = 2.2;
|
|
1210
|
+
revealBounce_ = 0.12;
|
|
1211
|
+
revealStyle_ = 0;
|
|
1212
|
+
revealStagger_ = 0.2;
|
|
1213
|
+
revealGrow_ = 0.2;
|
|
1214
|
+
revealMilestones_.clear();
|
|
1215
|
+
revealMilestoneHold_ = 0;
|
|
1216
|
+
reveal_ = Reveal{};
|
|
1217
|
+
revealScale_ = 1;
|
|
1218
|
+
wheels_.clear();
|
|
1219
|
+
signFactor_ = 0;
|
|
1220
|
+
hasShownValue_ = false;
|
|
1221
|
+
targetValue_ = 0;
|
|
1222
|
+
target_ = Target{0, false, 1};
|
|
1223
|
+
settledPowerCount_ = 1;
|
|
1224
|
+
settledNegative_ = false;
|
|
1225
|
+
transition_ = Transition{};
|
|
1226
|
+
loading_ = false;
|
|
1227
|
+
loadingProgress_ = 0;
|
|
1228
|
+
loadingFadeActive_ = false;
|
|
1229
|
+
loadingFadeFrom_ = 0;
|
|
1230
|
+
loadingFadeStart_ = 0;
|
|
1231
|
+
shimmerStart_ = 0;
|
|
1232
|
+
}
|
|
1233
|
+
|
|
1234
|
+
} // namespace margelo::nitro::nitroinput
|