react-native-nitro-input 0.2.0 → 0.3.1
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 +87 -0
- package/README.md +132 -2
- package/android/CMakeLists.txt +4 -0
- package/android/gradle.properties +1 -1
- package/android/src/main/cpp/JReflowEngine.cpp +32 -0
- package/android/src/main/cpp/JReflowEngine.hpp +13 -0
- package/android/src/main/cpp/JRollingEngine.cpp +43 -2
- package/android/src/main/cpp/JRollingEngine.hpp +9 -1
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputView.kt +20 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberFormatPlatform.kt +284 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberView.kt +113 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroTextMeasure.kt +25 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroTextView.kt +140 -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 +69 -10
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroNumberView.kt +522 -130
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroTextView.kt +868 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/ReflowEngine.kt +21 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/RollingEngine.kt +14 -2
- package/cpp/RollingEngine.cpp +102 -8
- package/cpp/RollingEngine.hpp +44 -3
- 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/ios/HybridNitroInputView.swift +15 -0
- package/ios/HybridNitroNumberFormatPlatform.swift +426 -0
- package/ios/HybridNitroNumberView.swift +101 -1
- package/ios/HybridNitroTextView.swift +134 -0
- package/ios/NitroInputView.swift +79 -19
- package/ios/NitroNumberView.swift +520 -147
- package/ios/NitroTextView.swift +583 -0
- package/lib/commonjs/nitrogen/generated/shared/json/NitroInputViewConfig.json +5 -0
- package/lib/commonjs/nitrogen/generated/shared/json/NitroNumberViewConfig.json +26 -0
- package/lib/commonjs/nitrogen/generated/shared/json/NitroTextViewConfig.json +30 -0
- package/lib/commonjs/src/NitroInput.js +8 -6
- package/lib/commonjs/src/NitroNumber.js +54 -3
- package/lib/commonjs/src/NitroText.js +138 -0
- package/lib/commonjs/src/NitroTime.js +65 -0
- package/lib/commonjs/src/NumberFormat.js +525 -0
- package/lib/commonjs/src/formatProps.js +108 -0
- package/lib/commonjs/src/index.js +8 -1
- package/lib/commonjs/src/specs/NitroText.nitro.js +2 -0
- package/lib/commonjs/src/specs/NumberFormat.nitro.js +2 -0
- package/lib/module/nitrogen/generated/shared/json/NitroInputViewConfig.json +5 -0
- package/lib/module/nitrogen/generated/shared/json/NitroNumberViewConfig.json +26 -0
- package/lib/module/nitrogen/generated/shared/json/NitroTextViewConfig.json +30 -0
- package/lib/module/src/NitroInput.js +8 -6
- package/lib/module/src/NitroNumber.js +54 -3
- package/lib/module/src/NitroText.js +98 -0
- package/lib/module/src/NitroTime.js +29 -0
- package/lib/module/src/NumberFormat.js +521 -0
- package/lib/module/src/formatProps.js +104 -0
- package/lib/module/src/index.js +3 -0
- package/lib/module/src/specs/NitroText.nitro.js +1 -0
- package/lib/module/src/specs/NumberFormat.nitro.js +1 -0
- package/lib/typescript/src/NitroInput.d.ts +22 -0
- package/lib/typescript/src/NitroNumber.d.ts +115 -2
- package/lib/typescript/src/NitroText.d.ts +67 -0
- package/lib/typescript/src/NitroTime.d.ts +19 -0
- package/lib/typescript/src/NumberFormat.d.ts +47 -0
- package/lib/typescript/src/formatProps.d.ts +47 -0
- package/lib/typescript/src/index.d.ts +6 -1
- package/lib/typescript/src/specs/NitroInput.nitro.d.ts +10 -0
- package/lib/typescript/src/specs/NitroNumber.nitro.d.ts +80 -1
- package/lib/typescript/src/specs/NitroText.nitro.d.ts +60 -0
- package/lib/typescript/src/specs/NumberFormat.nitro.d.ts +139 -0
- package/nitro.json +36 -0
- package/nitrogen/generated/android/NitroInput+autolinking.cmake +12 -0
- package/nitrogen/generated/android/NitroInputOnLoad.cpp +62 -0
- 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 +297 -0
- package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.hpp +52 -0
- package/nitrogen/generated/android/c++/JHybridNitroPlatformNumberFormatterSpec.cpp +65 -0
- package/nitrogen/generated/android/c++/JHybridNitroPlatformNumberFormatterSpec.hpp +64 -0
- package/nitrogen/generated/android/c++/JHybridNitroTextMeasureSpec.cpp +58 -0
- package/nitrogen/generated/android/c++/JHybridNitroTextMeasureSpec.hpp +64 -0
- package/nitrogen/generated/android/c++/JHybridNitroTextViewSpec.cpp +253 -0
- package/nitrogen/generated/android/c++/JHybridNitroTextViewSpec.hpp +104 -0
- package/nitrogen/generated/android/c++/JNitroNumberDirection.hpp +3 -0
- package/nitrogen/generated/android/c++/JNitroNumberShimmerDirection.hpp +61 -0
- package/nitrogen/generated/android/c++/JNitroNumberSignDisplay.hpp +67 -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 +130 -0
- package/nitrogen/generated/android/c++/views/JHybridNitroTextViewStateUpdater.cpp +174 -0
- package/nitrogen/generated/android/c++/views/JHybridNitroTextViewStateUpdater.hpp +54 -0
- 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 +172 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroPlatformNumberFormatterSpec.kt +62 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroTextMeasureSpec.kt +60 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroTextViewSpec.kt +179 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberDirection.kt +2 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberShimmerDirection.kt +24 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberSignDisplay.kt +26 -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/HybridNitroTextViewManager.kt +96 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/views/HybridNitroTextViewStateUpdater.kt +23 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.cpp +68 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.hpp +184 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Umbrella.hpp +56 -0
- package/nitrogen/generated/ios/NitroInputAutolinking.mm +34 -0
- package/nitrogen/generated/ios/NitroInputAutolinking.swift +36 -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.hpp +188 -0
- package/nitrogen/generated/ios/c++/HybridNitroPlatformNumberFormatterSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridNitroPlatformNumberFormatterSpecSwift.hpp +94 -0
- package/nitrogen/generated/ios/c++/HybridNitroTextMeasureSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridNitroTextMeasureSpecSwift.hpp +90 -0
- package/nitrogen/generated/ios/c++/HybridNitroTextViewSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridNitroTextViewSpecSwift.hpp +233 -0
- package/nitrogen/generated/ios/c++/views/HybridNitroInputViewComponent.mm +30 -0
- package/nitrogen/generated/ios/c++/views/HybridNitroNumberViewComponent.mm +156 -0
- package/nitrogen/generated/ios/c++/views/HybridNitroTextViewComponent.mm +264 -0
- 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 +26 -0
- package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec_cxx.swift +658 -21
- package/nitrogen/generated/ios/swift/HybridNitroPlatformNumberFormatterSpec.swift +56 -0
- package/nitrogen/generated/ios/swift/HybridNitroPlatformNumberFormatterSpec_cxx.swift +155 -0
- package/nitrogen/generated/ios/swift/HybridNitroTextMeasureSpec.swift +56 -0
- package/nitrogen/generated/ios/swift/HybridNitroTextMeasureSpec_cxx.swift +150 -0
- package/nitrogen/generated/ios/swift/HybridNitroTextViewSpec.swift +75 -0
- package/nitrogen/generated/ios/swift/HybridNitroTextViewSpec_cxx.swift +608 -0
- package/nitrogen/generated/ios/swift/NitroNumberDirection.swift +4 -0
- package/nitrogen/generated/ios/swift/NitroNumberShimmerDirection.swift +44 -0
- package/nitrogen/generated/ios/swift/NitroNumberSignDisplay.swift +52 -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 +52 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.hpp +58 -0
- package/nitrogen/generated/shared/c++/HybridNitroPlatformNumberFormatterSpec.cpp +23 -0
- package/nitrogen/generated/shared/c++/HybridNitroPlatformNumberFormatterSpec.hpp +65 -0
- package/nitrogen/generated/shared/c++/HybridNitroTextMeasureSpec.cpp +22 -0
- package/nitrogen/generated/shared/c++/HybridNitroTextMeasureSpec.hpp +63 -0
- package/nitrogen/generated/shared/c++/HybridNitroTextViewSpec.cpp +62 -0
- package/nitrogen/generated/shared/c++/HybridNitroTextViewSpec.hpp +115 -0
- package/nitrogen/generated/shared/c++/NitroNumberDirection.hpp +4 -0
- package/nitrogen/generated/shared/c++/NitroNumberShimmerDirection.hpp +80 -0
- package/nitrogen/generated/shared/c++/NitroNumberSignDisplay.hpp +88 -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 +52 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.hpp +80 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroTextViewComponent.cpp +74 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroTextViewComponent.hpp +149 -0
- package/nitrogen/generated/shared/json/NitroInputViewConfig.json +5 -0
- package/nitrogen/generated/shared/json/NitroNumberViewConfig.json +26 -0
- package/nitrogen/generated/shared/json/NitroTextViewConfig.json +30 -0
- package/package.json +2 -2
- package/src/NitroInput.tsx +42 -7
- package/src/NitroNumber.tsx +236 -12
- package/src/NitroText.tsx +166 -0
- package/src/NitroTime.tsx +68 -0
- package/src/NumberFormat.ts +563 -0
- package/src/formatProps.ts +149 -0
- package/src/index.ts +23 -0
- package/src/specs/NitroInput.nitro.ts +10 -0
- package/src/specs/NitroNumber.nitro.ts +82 -1
- package/src/specs/NitroText.nitro.ts +65 -0
- package/src/specs/NumberFormat.nitro.ts +145 -0
|
@@ -59,7 +59,10 @@ private const val TEXT_PREFIX = 0
|
|
|
59
59
|
private const val TEXT_SUFFIX = 1
|
|
60
60
|
private const val TEXT_GROUPING = 2
|
|
61
61
|
private const val TEXT_DECIMAL = 3
|
|
62
|
-
private const val
|
|
62
|
+
private const val TEXT_SIGN = 4
|
|
63
|
+
private const val TEXT_SLOTS = 5
|
|
64
|
+
/** Doubles per wheel in the engine's frame buffer (`RollingEngine.frameInto`): the engine's wheel, then its modulus. */
|
|
65
|
+
private const val WHEEL_FIELDS = 15
|
|
63
66
|
|
|
64
67
|
class NitroNumberView(context: Context) : View(context) {
|
|
65
68
|
|
|
@@ -72,6 +75,17 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
72
75
|
val decimalSeparator: String = ".",
|
|
73
76
|
val prefix: String = "",
|
|
74
77
|
val suffix: String = "",
|
|
78
|
+
/** ECMA-402's signDisplay (`RollingEngine.setSignDisplay`), and the glyphs drawn for the two signs. */
|
|
79
|
+
val signDisplay: Int = 0,
|
|
80
|
+
val plusSign: String = "+",
|
|
81
|
+
val minusSign: String = "-",
|
|
82
|
+
/**
|
|
83
|
+
* Digit group sizes counted from the decimal point, the first then every
|
|
84
|
+
* later one (empty: threes). [3, 2] is Indian grouping, [2] a clock's.
|
|
85
|
+
*/
|
|
86
|
+
val groupingSizes: List<Int> = emptyList(),
|
|
87
|
+
/** Per integer position, the highest digit its wheel shows before it wraps (a clock's 5). */
|
|
88
|
+
val digitMax: List<Int> = emptyList(),
|
|
75
89
|
)
|
|
76
90
|
|
|
77
91
|
enum class AffixAlign { BASELINE, CENTER, TOP, BOTTOM }
|
|
@@ -85,6 +99,25 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
85
99
|
val color: Int? = null,
|
|
86
100
|
val prefixAlign: AffixAlign = AffixAlign.BASELINE,
|
|
87
101
|
val suffixAlign: AffixAlign = AffixAlign.BASELINE,
|
|
102
|
+
/** Dp added after every glyph, as `Text`'s letterSpacing; an affix gets it in proportion to its size. */
|
|
103
|
+
val letterSpacing: Float = 0f,
|
|
104
|
+
/** Dp between the prefix and the digits, and between the digits and the suffix, in place of the letter spacing there. */
|
|
105
|
+
val prefixSpacing: Float? = null,
|
|
106
|
+
val suffixSpacing: Float? = null,
|
|
107
|
+
/** Dp an affix is moved down (negative: up) after its alignment. */
|
|
108
|
+
val prefixOffset: Float = 0f,
|
|
109
|
+
val suffixOffset: Float = 0f,
|
|
110
|
+
/** Every digit as wide as the widest (tabular figures, the default), or each at its own width. */
|
|
111
|
+
val tabularNums: Boolean = true,
|
|
112
|
+
/** The prefix's, the suffix's and the fraction's colours (null: [color]). */
|
|
113
|
+
val prefixColor: Int? = null,
|
|
114
|
+
val suffixColor: Int? = null,
|
|
115
|
+
val fractionColor: Int? = null,
|
|
116
|
+
/** The fraction digits' and decimal separator's size (null: [fontSize]) and how they line up. */
|
|
117
|
+
val fractionFontSize: Float? = null,
|
|
118
|
+
val fractionAlign: AffixAlign = AffixAlign.BASELINE,
|
|
119
|
+
/** The glyphs drawn for 0…9 (anything but ten non-empty strings: "0"…"9"). */
|
|
120
|
+
val digitGlyphs: List<String> = emptyList(),
|
|
88
121
|
val adjustsFontSizeToFit: Boolean = false,
|
|
89
122
|
val minimumFontScale: Float = 0.5f,
|
|
90
123
|
val allowFontScaling: Boolean = false,
|
|
@@ -93,7 +126,7 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
93
126
|
|
|
94
127
|
enum class Easing(val raw: Int) { LINEAR(0), EASE_IN(1), EASE_OUT(2), EASE_IN_OUT(3), SPRING(4) }
|
|
95
128
|
|
|
96
|
-
enum class Direction(val raw: Int) { AUTO(0), UP(1), DOWN(2) }
|
|
129
|
+
enum class Direction(val raw: Int) { AUTO(0), UP(1), DOWN(2), SHORTEST(3) }
|
|
97
130
|
|
|
98
131
|
data class Timing(
|
|
99
132
|
/** The odometer roll, or one of the glyph-swap transitions. */
|
|
@@ -118,6 +151,10 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
118
151
|
val revealStaggerMs: Long = 200,
|
|
119
152
|
/** Count style: how long the count pauses on each milestone. */
|
|
120
153
|
val revealMilestoneHoldMs: Long = 0,
|
|
154
|
+
/** Rolls turn the lower wheels a full turn too (`RollingEngine.setContinuous`). */
|
|
155
|
+
val continuous: Boolean = false,
|
|
156
|
+
/** Snap while the user has removed animations. */
|
|
157
|
+
val respectReduceMotion: Boolean = true,
|
|
121
158
|
)
|
|
122
159
|
|
|
123
160
|
enum class RevealStyle(val raw: Int) { COUNT(0), SPIN(1) }
|
|
@@ -136,6 +173,16 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
136
173
|
val color: Int? = null,
|
|
137
174
|
/** Duration of one sweep across the number. */
|
|
138
175
|
val durationMs: Long = 950,
|
|
176
|
+
/** The band's slant in degrees (0 upright, positive leans like "/"). */
|
|
177
|
+
val angle: Float = 31f,
|
|
178
|
+
/** The band's width, a fraction of the number's. */
|
|
179
|
+
val width: Float = 1f,
|
|
180
|
+
/** The glyphs' colour outside the band while loading (null: the text colour). */
|
|
181
|
+
val baseColor: Int? = null,
|
|
182
|
+
/** Sweep direction: null follows the layout direction, true left to right. */
|
|
183
|
+
val leftToRight: Boolean? = null,
|
|
184
|
+
/** A pause after each sweep. */
|
|
185
|
+
val delayMs: Long = 0,
|
|
139
186
|
)
|
|
140
187
|
|
|
141
188
|
/** `AUTO` is the start edge of the layout direction, as `Text` with no `textAlign`; the rest are absolute. */
|
|
@@ -162,6 +209,13 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
162
209
|
change(TEXT_SUFFIX, suffixInk(old.suffix), suffixInk(value.suffix))
|
|
163
210
|
change(TEXT_GROUPING, old.groupingSeparator, value.groupingSeparator)
|
|
164
211
|
change(TEXT_DECIMAL, old.decimalSeparator, value.decimalSeparator)
|
|
212
|
+
val positive = engine.signPositive()
|
|
213
|
+
change(TEXT_SIGN, if (positive) old.plusSign else old.minusSign, if (positive) value.plusSign else value.minusSign)
|
|
214
|
+
}
|
|
215
|
+
engine.setSignDisplay(value.signDisplay)
|
|
216
|
+
if (value.digitMax != old.digitMax) {
|
|
217
|
+
engine.clearDigitMax()
|
|
218
|
+
for ((power, max) in value.digitMax.withIndex()) if (max in 0..8) engine.setDigitMax(power, max)
|
|
165
219
|
}
|
|
166
220
|
affixBlocksDirty = true
|
|
167
221
|
// Played in a glyph-swap transition; snaps otherwise (see the engine).
|
|
@@ -188,6 +242,7 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
188
242
|
engine.setRevealTiming(value.revealDurationMs / 1000.0, value.revealBounce, value.revealStyle.raw, value.revealStaggerMs / 1000.0)
|
|
189
243
|
engine.setRevealGrow(value.revealGrow)
|
|
190
244
|
engine.setRevealMilestoneHold(value.revealMilestoneHoldMs / 1000.0)
|
|
245
|
+
engine.setContinuous(value.continuous)
|
|
191
246
|
}
|
|
192
247
|
|
|
193
248
|
/** Win tiers of a count-style reveal, in the figure's units. */
|
|
@@ -219,7 +274,7 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
219
274
|
set(value) {
|
|
220
275
|
if (field == value) return
|
|
221
276
|
field = value
|
|
222
|
-
|
|
277
|
+
applyReduceMotion()
|
|
223
278
|
engine.setLoading(value, now())
|
|
224
279
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) stateDescription = if (value) "Loading" else null
|
|
225
280
|
scheduleFrameIfNeeded()
|
|
@@ -238,6 +293,11 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
238
293
|
var onRevealEnd: (() -> Unit)? = null
|
|
239
294
|
/** Called when a count-style reveal reaches a milestone (its index and value). */
|
|
240
295
|
var onRevealMilestone: ((index: Int, value: Double) -> Unit)? = null
|
|
296
|
+
/** Called when the figure starts moving from rest, and when it comes to rest (with its value). */
|
|
297
|
+
var onAnimationStart: (() -> Unit)? = null
|
|
298
|
+
var onAnimationEnd: ((value: Double) -> Unit)? = null
|
|
299
|
+
/** A roll or a reveal is under way ([onAnimationStart] has fired, [onAnimationEnd] not yet). */
|
|
300
|
+
private var moving = false
|
|
241
301
|
|
|
242
302
|
/** The value currently shown or being rolled towards. */
|
|
243
303
|
val targetValue: Double
|
|
@@ -251,28 +311,65 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
251
311
|
|
|
252
312
|
// region Fonts
|
|
253
313
|
|
|
254
|
-
private enum class GlyphRole {
|
|
314
|
+
private enum class GlyphRole {
|
|
315
|
+
/** The integer digits, the sign and the grouping separators. */
|
|
316
|
+
DIGIT,
|
|
317
|
+
PREFIX,
|
|
318
|
+
SUFFIX,
|
|
319
|
+
/** The fraction digits and the decimal separator (`fractionFontSize`, `fractionColor`). */
|
|
320
|
+
FRACTION,
|
|
321
|
+
}
|
|
255
322
|
|
|
256
|
-
/** Digit / prefix / suffix paints with per-glyph width caches. */
|
|
323
|
+
/** Digit / prefix / suffix / fraction paints with per-glyph width caches. */
|
|
257
324
|
private inner class FontSet(t: Typography) {
|
|
258
325
|
private val scale = systemFontMultiplier(t)
|
|
259
|
-
val
|
|
260
|
-
val
|
|
261
|
-
val
|
|
326
|
+
private val ink = t.color ?: defaultTextColor()
|
|
327
|
+
val digit = makePaint(t, t.fontSize * scale, ink)
|
|
328
|
+
val prefix = makePaint(t, (t.prefixFontSize ?: t.fontSize) * scale, t.prefixColor ?: ink)
|
|
329
|
+
val suffix = makePaint(t, (t.suffixFontSize ?: t.fontSize) * scale, t.suffixColor ?: ink)
|
|
330
|
+
val fraction = makePaint(t, (t.fractionFontSize ?: t.fontSize) * scale, t.fractionColor ?: ink)
|
|
262
331
|
private val prefixAlign = t.prefixAlign
|
|
263
332
|
private val suffixAlign = t.suffixAlign
|
|
333
|
+
private val fractionAlign = t.fractionAlign
|
|
334
|
+
/** The glyphs drawn for 0…9. */
|
|
335
|
+
val glyphs: Array<String> =
|
|
336
|
+
if (t.digitGlyphs.size == 10 && t.digitGlyphs.none { it.isEmpty() }) t.digitGlyphs.toTypedArray() else DIGITS
|
|
337
|
+
private val glyphsKey = glyphs.joinToString("|")
|
|
338
|
+
/** Every glyph in a row, whose ink the bottom alignment pins to. */
|
|
339
|
+
private val allGlyphs = glyphs.joinToString("")
|
|
340
|
+
/** Letter spacing after a digit / prefix / suffix / fraction glyph, the seams (null: the letter spacing) and the affix offsets, in px. */
|
|
341
|
+
val digitSpacing = t.letterSpacing * scale * density
|
|
342
|
+
val prefixLetterSpacing = digitSpacing * ((t.prefixFontSize ?: t.fontSize) / t.fontSize)
|
|
343
|
+
val suffixLetterSpacing = digitSpacing * ((t.suffixFontSize ?: t.fontSize) / t.fontSize)
|
|
344
|
+
val fractionLetterSpacing = digitSpacing * ((t.fractionFontSize ?: t.fontSize) / t.fontSize)
|
|
345
|
+
val prefixSeam = t.prefixSpacing?.let { it * scale * density }
|
|
346
|
+
val suffixSeam = t.suffixSpacing?.let { it * scale * density }
|
|
347
|
+
private val prefixOffset = t.prefixOffset * scale * density
|
|
348
|
+
private val suffixOffset = t.suffixOffset * scale * density
|
|
264
349
|
private val digitMetrics: Paint.FontMetrics = digit.fontMetrics
|
|
350
|
+
/** Each role's metrics, read once: `Paint.getFontMetrics()` allocates, and the layout asks per glyph per frame. */
|
|
351
|
+
private val roleMetrics = Array(GlyphRole.entries.size) { paint(GlyphRole.entries[it]).fontMetrics }
|
|
265
352
|
/** Height of the line box (the digit paint's line height), in px. */
|
|
266
353
|
val lineHeight: Float = ceil(digitMetrics.descent - digitMetrics.ascent)
|
|
267
354
|
/** The numeric transition's blur radius at full blur, in line heights. */
|
|
268
355
|
val numericBlur: Float = NUMERIC_BLUR
|
|
269
356
|
/** Width of the widest digit glyph, in px. */
|
|
270
357
|
val digitWidth: Float
|
|
358
|
+
/** The same for the fraction digits, in their own font. */
|
|
359
|
+
val fractionDigitWidth: Float
|
|
360
|
+
/** Each digit at its own advance instead of the widest's (`tabularNums={false}`). */
|
|
361
|
+
val proportional = !t.tabularNums
|
|
362
|
+
/** The advance of each digit 0…9, in px. */
|
|
363
|
+
val digitWidths = FloatArray(10)
|
|
364
|
+
val fractionDigitWidths = FloatArray(10)
|
|
271
365
|
private val widthCache = Array(GlyphRole.entries.size) { java.util.concurrent.ConcurrentHashMap<String, Float>() }
|
|
272
366
|
/**
|
|
273
|
-
* A wheel's whole digit strip, per blank-zero variant
|
|
274
|
-
* -1 (blank) to
|
|
275
|
-
*
|
|
367
|
+
* A wheel's whole digit strip, per role, blank-zero variant and modulus:
|
|
368
|
+
* `modulus + 2` slots for index -1 (blank) to `modulus` (the 0 that
|
|
369
|
+
* follows the last digit on a wrap: 10 on a plain wheel, 6 on a clock's
|
|
370
|
+
* tens), each `lineHeight` tall with the digit centred in the role's
|
|
371
|
+
* widest digit; a fraction digit set smaller sits at its aligned height in
|
|
372
|
+
* the slot. The digits are rasterized
|
|
276
373
|
* once by the software text renderer into a bitmap, and the bitmap is the
|
|
277
374
|
* whole content of a `RenderNode` layer; a wheel then draws one slot-high
|
|
278
375
|
* window of that layer, one quad, and a frame costs no glyph work at all:
|
|
@@ -287,22 +384,29 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
287
384
|
* RenderNode and the bitmap is drawn directly.
|
|
288
385
|
*
|
|
289
386
|
* The strips are shared by every rolling number drawn with the same
|
|
290
|
-
* typography (see [StripCache])
|
|
387
|
+
* typography (see [StripCache]); this font set keeps the ones it has used
|
|
388
|
+
* by [stripVariant], so a frame looks them up without building a key.
|
|
291
389
|
*/
|
|
292
|
-
private val
|
|
293
|
-
"${t.fontFamily}|${t.fontWeight}|${digit.textSize}|${digit.color}|${digit.typeface?.hashCode()}"
|
|
390
|
+
private val strips = android.util.SparseArray<Strip>()
|
|
294
391
|
|
|
295
392
|
/**
|
|
296
|
-
* This font set's digit masks by digit and level,
|
|
297
|
-
* cache: a swapping glyph looks two up every frame,
|
|
298
|
-
* built a key string each time. Written by the warm-up
|
|
393
|
+
* This font set's digit masks by role (digit, fraction), digit and level,
|
|
394
|
+
* filled from the shared cache: a swapping glyph looks two up every frame,
|
|
395
|
+
* and a lookup there built a key string each time. Written by the warm-up
|
|
396
|
+
* thread too.
|
|
299
397
|
*/
|
|
300
|
-
private val digitMasks = AtomicReferenceArray<Bitmap>(10 * (NUMERIC_BLUR_LEVELS + 1))
|
|
301
|
-
|
|
302
|
-
fun strip(blankZero: Boolean): Strip? {
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
398
|
+
private val digitMasks = AtomicReferenceArray<Bitmap>(2 * 10 * (NUMERIC_BLUR_LEVELS + 1))
|
|
399
|
+
|
|
400
|
+
fun strip(role: GlyphRole, blankZero: Boolean, modulus: Int): Strip? {
|
|
401
|
+
val cell = digitWidth(role)
|
|
402
|
+
if (cell <= 0f || lineHeight <= 0f) return null
|
|
403
|
+
val variant = stripVariant(role, blankZero, modulus)
|
|
404
|
+
var strip = strips.get(variant)
|
|
405
|
+
if (strip == null) {
|
|
406
|
+
strip = StripCache.get("${key(role)}|$blankZero|$modulus") {
|
|
407
|
+
Strip(renderStrip(role, blankZero, modulus), if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) RenderNode("rolling-number-strip") else null)
|
|
408
|
+
}
|
|
409
|
+
strips.put(variant, strip)
|
|
306
410
|
}
|
|
307
411
|
val node = strip.node
|
|
308
412
|
// HWUI deletes a node's display list once nothing in the view tree draws
|
|
@@ -329,23 +433,26 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
329
433
|
* is a Gaussian to the eye; HWUI has no hardware mask filter), once per
|
|
330
434
|
* font, colour and density and shared like the strips. A transitioning
|
|
331
435
|
* glyph is its sharp text and this bitmap cross-faded, a fixed cost per
|
|
332
|
-
* frame instead of a blur pass. The bitmap is `digitWidth + 2 pad`
|
|
333
|
-
* and `lineHeight + 2 pad` tall with the glyph centred in the column
|
|
436
|
+
* frame instead of a blur pass. The bitmap is `digitWidth(role) + 2 pad`
|
|
437
|
+
* wide and `lineHeight + 2 pad` tall with the glyph centred in the column
|
|
438
|
+
* at its role's height in the line box.
|
|
334
439
|
*/
|
|
335
|
-
fun blurred(digitIndex: Int, level: Int): Bitmap? {
|
|
336
|
-
|
|
440
|
+
fun blurred(role: GlyphRole, digitIndex: Int, level: Int): Bitmap? {
|
|
441
|
+
val cell = digitWidth(role)
|
|
442
|
+
if (cell <= 0f || lineHeight <= 0f) return null
|
|
337
443
|
// Level 0 (or no blur at all) is the glyph unblurred, still a mask: a
|
|
338
444
|
// swapping glyph's two copies must be the same kind of thing, drawn the
|
|
339
445
|
// same way at the same sub-pixel position. Hardware text snaps to whole
|
|
340
446
|
// pixels as it moves and a bitmap does not, and cross-faded they slid in
|
|
341
447
|
// and out of register as the spring settled, a bold/pale flicker.
|
|
342
|
-
val slot =
|
|
448
|
+
val slot = (if (role == GlyphRole.FRACTION) 10 * (NUMERIC_BLUR_LEVELS + 1) else 0) +
|
|
449
|
+
digitIndex * (NUMERIC_BLUR_LEVELS + 1) + level.coerceIn(0, NUMERIC_BLUR_LEVELS)
|
|
343
450
|
digitMasks.get(slot)?.let { return it }
|
|
344
451
|
val radius = if (level <= 0 || numericBlur <= 0f) 0 else Math.round(lineHeight * numericBlur * level / NUMERIC_BLUR_LEVELS).coerceAtLeast(1)
|
|
345
|
-
val mask = BlurredGlyphCache.get("$
|
|
346
|
-
val text =
|
|
452
|
+
val mask = BlurredGlyphCache.get("${key(role)}|$digitIndex|$radius|$blurPad") {
|
|
453
|
+
val text = glyphs[digitIndex]
|
|
347
454
|
val pad = blurPad
|
|
348
|
-
val w = ceil(
|
|
455
|
+
val w = ceil(cell).toInt() + 2 * pad
|
|
349
456
|
val h = ceil(lineHeight).toInt() + 2 * pad
|
|
350
457
|
// An alpha mask, not a coloured bitmap: the glyph's coverage, blurred,
|
|
351
458
|
// that a draw fills with the digit paint's colour. A coloured
|
|
@@ -356,8 +463,8 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
356
463
|
val canvas = Canvas(mask)
|
|
357
464
|
// A paint of its own: the masks are warmed off the main thread, where
|
|
358
465
|
// the digit paint's alpha is being changed by the draws.
|
|
359
|
-
val paint = TextPaint(
|
|
360
|
-
canvas.drawText(text, pad + (
|
|
466
|
+
val paint = TextPaint(paint(role)).apply { alpha = 255 }
|
|
467
|
+
canvas.drawText(text, pad + (cell - width(text, role)) / 2f, pad + digitBaseline(role), paint)
|
|
361
468
|
if (radius > 0) boxBlurMask(mask, radius)
|
|
362
469
|
// Immutable and uploaded ahead of its first draw: HWUI re-pins a mutable
|
|
363
470
|
// bitmap at every sync, and the first frame that drew a fresh mask paid
|
|
@@ -380,7 +487,7 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
380
487
|
fun blurredText(text: String, role: GlyphRole, level: Int): Bitmap? {
|
|
381
488
|
if (text.isEmpty() || lineHeight <= 0f) return null
|
|
382
489
|
val radius = if (level <= 0 || numericBlur <= 0f) 0 else Math.round(lineHeight * numericBlur * level / NUMERIC_BLUR_LEVELS).coerceAtLeast(1)
|
|
383
|
-
return BlurredGlyphCache.get("$
|
|
490
|
+
return BlurredGlyphCache.get("${key(role)}|t|$text|$radius|$blurPad") {
|
|
384
491
|
val pad = blurPad
|
|
385
492
|
val w = ceil(width(text, role)).toInt() + 2 * pad
|
|
386
493
|
val h = ceil(lineHeight).toInt() + 2 * pad
|
|
@@ -399,20 +506,41 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
399
506
|
/** Padding around a blurred glyph's bitmap, enough for the blur's tail. */
|
|
400
507
|
val blurPad: Int get() = ceil(lineHeight * numericBlur * 3f).toInt().coerceAtLeast(2)
|
|
401
508
|
|
|
402
|
-
/**
|
|
403
|
-
|
|
404
|
-
|
|
509
|
+
/**
|
|
510
|
+
* A digit (or fraction) paint in another colour, for the change flash;
|
|
511
|
+
* one per colour, a few per role looked up without boxing the colour.
|
|
512
|
+
*/
|
|
513
|
+
private val tintedColors = IntArray(2 * TINT_SLOTS)
|
|
514
|
+
private val tintedPaints = arrayOfNulls<TextPaint>(2 * TINT_SLOTS)
|
|
515
|
+
private val tintedNext = IntArray(2)
|
|
516
|
+
fun tinted(color: Int, role: GlyphRole): TextPaint {
|
|
517
|
+
val r = if (role == GlyphRole.FRACTION) 1 else 0
|
|
518
|
+
val base = r * TINT_SLOTS
|
|
519
|
+
for (i in base until base + TINT_SLOTS) {
|
|
520
|
+
val paint = tintedPaints[i] ?: break
|
|
521
|
+
if (tintedColors[i] == color) return paint
|
|
522
|
+
}
|
|
523
|
+
val i = base + tintedNext[r]
|
|
524
|
+
tintedNext[r] = (tintedNext[r] + 1) % TINT_SLOTS
|
|
525
|
+
val paint = TextPaint(paint(role)).apply { this.color = color }
|
|
526
|
+
tintedColors[i] = color
|
|
527
|
+
tintedPaints[i] = paint
|
|
528
|
+
return paint
|
|
529
|
+
}
|
|
405
530
|
|
|
406
|
-
private fun renderStrip(blankZero: Boolean): Bitmap {
|
|
407
|
-
val
|
|
408
|
-
val
|
|
531
|
+
private fun renderStrip(role: GlyphRole, blankZero: Boolean, modulus: Int): Bitmap {
|
|
532
|
+
val cell = digitWidth(role)
|
|
533
|
+
val slots = modulus + 2
|
|
534
|
+
val w = ceil(cell).toInt()
|
|
535
|
+
val h = ceil(lineHeight * slots).toInt()
|
|
409
536
|
val drawn = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
|
|
410
537
|
val canvas = Canvas(drawn)
|
|
411
|
-
val baseline =
|
|
412
|
-
|
|
538
|
+
val baseline = digitBaseline(role)
|
|
539
|
+
val paint = paint(role)
|
|
540
|
+
for (index in -1 until slots - 1) {
|
|
413
541
|
if (index < 0 || (blankZero && index == 0)) continue
|
|
414
|
-
val text =
|
|
415
|
-
canvas.drawText(text, (
|
|
542
|
+
val text = glyphs[index % modulus]
|
|
543
|
+
canvas.drawText(text, (cell - width(text, role)) / 2f, (index + 1) * lineHeight + baseline, paint)
|
|
416
544
|
}
|
|
417
545
|
// Immutable: the renderer uploads it once and keeps the texture.
|
|
418
546
|
val strip = drawn.copy(Bitmap.Config.ARGB_8888, false)
|
|
@@ -422,16 +550,53 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
422
550
|
private val capHeightCache = HashMap<GlyphRole, Float>()
|
|
423
551
|
private val inkDescentCache = HashMap<String, Float>()
|
|
424
552
|
|
|
553
|
+
/** A fraction digit's baseline in the line box, at its alignment. */
|
|
554
|
+
private val fractionBaseline: Float
|
|
555
|
+
/** The vertical centre of a fraction digit in the line box (a swapping glyph's). */
|
|
556
|
+
private val fractionCenterY: Float
|
|
557
|
+
/**
|
|
558
|
+
* What a cached bitmap drawn in each role depends on: the font, its
|
|
559
|
+
* colour, where it sits in the line box and the digit glyphs. Shared
|
|
560
|
+
* strips and masks are keyed by it.
|
|
561
|
+
*/
|
|
562
|
+
private val roleKeys: Array<String>
|
|
563
|
+
|
|
425
564
|
init {
|
|
426
|
-
|
|
565
|
+
for (d in 0..9) digitWidths[d] = width(glyphs[d], GlyphRole.DIGIT)
|
|
566
|
+
digitWidth = digitWidths.max()
|
|
567
|
+
for (d in 0..9) fractionDigitWidths[d] = width(glyphs[d], GlyphRole.FRACTION)
|
|
568
|
+
fractionDigitWidth = fractionDigitWidths.max()
|
|
569
|
+
fractionBaseline = baseline(GlyphRole.FRACTION, glyphs[0], 0f)
|
|
570
|
+
val m = roleMetrics[GlyphRole.FRACTION.ordinal]
|
|
571
|
+
fractionCenterY = fractionBaseline + (m.ascent + m.descent) / 2f
|
|
572
|
+
roleKeys = Array(GlyphRole.entries.size) {
|
|
573
|
+
val role = GlyphRole.entries[it]
|
|
574
|
+
val p = paint(role)
|
|
575
|
+
"${t.fontFamily}|${t.fontWeight}|${digit.textSize}|${p.textSize}|${p.color}|${p.typeface?.hashCode()}|${p.fontFeatureSettings}|" +
|
|
576
|
+
"${role.name}|${alignment(role)}|${if (role == GlyphRole.PREFIX) prefixOffset else if (role == GlyphRole.SUFFIX) suffixOffset else 0f}|$glyphsKey"
|
|
577
|
+
}
|
|
427
578
|
}
|
|
428
579
|
|
|
580
|
+
private fun key(role: GlyphRole): String = roleKeys[role.ordinal]
|
|
581
|
+
|
|
429
582
|
fun paint(role: GlyphRole): TextPaint = when (role) {
|
|
430
583
|
GlyphRole.DIGIT -> digit
|
|
431
584
|
GlyphRole.PREFIX -> prefix
|
|
432
585
|
GlyphRole.SUFFIX -> suffix
|
|
586
|
+
GlyphRole.FRACTION -> fraction
|
|
433
587
|
}
|
|
434
588
|
|
|
589
|
+
/** The widest digit of [role]'s font (the digits' or the fraction's). */
|
|
590
|
+
fun digitWidth(role: GlyphRole): Float = if (role == GlyphRole.FRACTION) fractionDigitWidth else digitWidth
|
|
591
|
+
|
|
592
|
+
fun digitWidths(role: GlyphRole): FloatArray = if (role == GlyphRole.FRACTION) fractionDigitWidths else digitWidths
|
|
593
|
+
|
|
594
|
+
/** A [role] digit's baseline, given the top of the digit line box at 0. */
|
|
595
|
+
fun digitBaseline(role: GlyphRole): Float = if (role == GlyphRole.FRACTION) fractionBaseline else -digitMetrics.ascent
|
|
596
|
+
|
|
597
|
+
/** The vertical centre of a [role] digit in the line box (a swapping glyph's). */
|
|
598
|
+
fun digitCenterY(role: GlyphRole): Float = if (role == GlyphRole.FRACTION) fractionCenterY else lineHeight / 2f
|
|
599
|
+
|
|
435
600
|
// One map per role: a joined "role|text" key allocated a string on every
|
|
436
601
|
// lookup, and the layout looks widths up for every glyph on every frame.
|
|
437
602
|
fun width(text: String, role: GlyphRole): Float =
|
|
@@ -452,19 +617,39 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
452
617
|
shapedCache[role.ordinal].getOrPut(text) { ShapedText.of(text, paint(role)) }
|
|
453
618
|
|
|
454
619
|
/** Baseline y for `role` drawing `text`, given the top of the digit line box. */
|
|
455
|
-
fun baseline(role: GlyphRole, text: String, lineTop: Float): Float {
|
|
620
|
+
fun baseline(role: GlyphRole, text: String, lineTop: Float): Float = when (role) {
|
|
621
|
+
GlyphRole.DIGIT -> lineTop - digitMetrics.ascent
|
|
622
|
+
GlyphRole.FRACTION -> alignedBaseline(role, text, lineTop)
|
|
623
|
+
GlyphRole.PREFIX -> alignedBaseline(role, text, lineTop) + prefixOffset
|
|
624
|
+
GlyphRole.SUFFIX -> alignedBaseline(role, text, lineTop) + suffixOffset
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
/** Letter spacing after a glyph of [role], in px. */
|
|
628
|
+
fun spacing(role: GlyphRole): Float = when (role) {
|
|
629
|
+
GlyphRole.DIGIT -> digitSpacing
|
|
630
|
+
GlyphRole.PREFIX -> prefixLetterSpacing
|
|
631
|
+
GlyphRole.SUFFIX -> suffixLetterSpacing
|
|
632
|
+
GlyphRole.FRACTION -> fractionLetterSpacing
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
private fun alignment(role: GlyphRole): AffixAlign = when (role) {
|
|
636
|
+
GlyphRole.PREFIX -> prefixAlign
|
|
637
|
+
GlyphRole.SUFFIX -> suffixAlign
|
|
638
|
+
GlyphRole.FRACTION -> fractionAlign
|
|
639
|
+
GlyphRole.DIGIT -> AffixAlign.BASELINE
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
private fun alignedBaseline(role: GlyphRole, text: String, lineTop: Float): Float {
|
|
456
643
|
val digitBaseline = lineTop - digitMetrics.ascent
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
val m = p.fontMetrics
|
|
460
|
-
return when (if (role == GlyphRole.PREFIX) prefixAlign else suffixAlign) {
|
|
644
|
+
val m = roleMetrics[role.ordinal]
|
|
645
|
+
return when (alignment(role)) {
|
|
461
646
|
AffixAlign.BASELINE -> digitBaseline
|
|
462
647
|
AffixAlign.CENTER -> lineTop + (lineHeight - (m.descent - m.ascent)) / 2f - m.ascent
|
|
463
648
|
AffixAlign.TOP -> lineTop + (-digitMetrics.ascent - capHeight(GlyphRole.DIGIT)) + capHeight(role)
|
|
464
649
|
// Pin the bottom of the ink, not of the line boxes: the digits' ink ends
|
|
465
650
|
// on the baseline, so "USD" sits on it too instead of hanging down to
|
|
466
651
|
// where a comma's tail reaches.
|
|
467
|
-
AffixAlign.BOTTOM -> digitBaseline + inkDescent(
|
|
652
|
+
AffixAlign.BOTTOM -> digitBaseline + inkDescent(allGlyphs, GlyphRole.DIGIT) - inkDescent(text, role)
|
|
468
653
|
}
|
|
469
654
|
}
|
|
470
655
|
|
|
@@ -533,12 +718,13 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
533
718
|
return if (t.maxFontSizeMultiplier > 0f) min(multiplier, t.maxFontSizeMultiplier) else multiplier
|
|
534
719
|
}
|
|
535
720
|
|
|
536
|
-
private fun makePaint(t: Typography, sizeDp: Float): TextPaint {
|
|
721
|
+
private fun makePaint(t: Typography, sizeDp: Float, color: Int): TextPaint {
|
|
537
722
|
val paint = TextPaint(Paint.ANTI_ALIAS_FLAG or Paint.SUBPIXEL_TEXT_FLAG)
|
|
538
723
|
paint.typeface = makeTypeface(t)
|
|
539
724
|
paint.textSize = sizeDp * density
|
|
540
|
-
paint.color =
|
|
541
|
-
|
|
725
|
+
paint.color = color
|
|
726
|
+
// Tabular figures unless each digit is laid out at its own width.
|
|
727
|
+
paint.fontFeatureSettings = if (t.tabularNums) "tnum" else "pnum"
|
|
542
728
|
return paint
|
|
543
729
|
}
|
|
544
730
|
|
|
@@ -591,6 +777,9 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
591
777
|
var focus: Double = 1.0,
|
|
592
778
|
var grow: Double = 1.0,
|
|
593
779
|
var blurOut: Double = 1.0,
|
|
780
|
+
var progress: Double = 1.0,
|
|
781
|
+
/** Places the wheel wraps after: 10, or fewer on a clock's wheel (`RollingEngine.wheelModulus`). */
|
|
782
|
+
var modulus: Int = 10,
|
|
594
783
|
)
|
|
595
784
|
|
|
596
785
|
private val engine = RollingEngine()
|
|
@@ -627,6 +816,7 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
627
816
|
reportMilestones(reachedBefore)
|
|
628
817
|
if (!engine.isRevealing()) onRevealEnd?.invoke()
|
|
629
818
|
}
|
|
819
|
+
updateMoving()
|
|
630
820
|
}
|
|
631
821
|
|
|
632
822
|
/** Fires [onRevealMilestone] for every milestone reached since [reachedBefore]. */
|
|
@@ -656,6 +846,8 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
656
846
|
private var shimmerGradientWidth = -1f
|
|
657
847
|
private var shimmerGradientBase = 0
|
|
658
848
|
private var shimmerGradientHighlight = 0
|
|
849
|
+
private var shimmerGradientLength = -1f
|
|
850
|
+
private var shimmerGradientSlant = Float.NaN
|
|
659
851
|
private val shimmerMatrix = Matrix()
|
|
660
852
|
|
|
661
853
|
init {
|
|
@@ -697,7 +889,10 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
697
889
|
shimmer = Shimmer()
|
|
698
890
|
alignment = Alignment.AUTO
|
|
699
891
|
revealMilestones = DoubleArray(0)
|
|
700
|
-
|
|
892
|
+
cellWidths.clear()
|
|
893
|
+
leavingText.fill(null)
|
|
894
|
+
moving = false
|
|
895
|
+
// onIntrinsicSizeChange, onRevealEnd, onRevealMilestone and the animation events are the hybrid's
|
|
701
896
|
// wiring, not the element's props: they stay across recycling (the hybrid
|
|
702
897
|
// clears its own callback props).
|
|
703
898
|
invalidate()
|
|
@@ -708,19 +903,25 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
708
903
|
* style). Cancels any running roll. Intended to be called every frame.
|
|
709
904
|
*/
|
|
710
905
|
fun setValue(value: Double) {
|
|
906
|
+
val sign = signBefore()
|
|
711
907
|
engine.setValue(value)
|
|
908
|
+
swapSignIfChanged(sign)
|
|
712
909
|
reportIntrinsicSize()
|
|
713
910
|
scheduleFrameIfNeeded()
|
|
714
911
|
invalidate()
|
|
912
|
+
updateMoving()
|
|
715
913
|
}
|
|
716
914
|
|
|
717
915
|
/** Rolls every wheel to [value] (snaps on first show, duration 0 or "remove animations"). */
|
|
718
916
|
fun animateTo(value: Double) {
|
|
719
|
-
|
|
917
|
+
applyReduceMotion()
|
|
918
|
+
val sign = signBefore()
|
|
720
919
|
engine.animateTo(value, now())
|
|
920
|
+
swapSignIfChanged(sign)
|
|
721
921
|
reportIntrinsicSize()
|
|
722
922
|
scheduleFrameIfNeeded()
|
|
723
923
|
invalidate()
|
|
924
|
+
updateMoving()
|
|
724
925
|
}
|
|
725
926
|
|
|
726
927
|
/**
|
|
@@ -728,7 +929,9 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
728
929
|
* target's layout), waiting for [reveal].
|
|
729
930
|
*/
|
|
730
931
|
fun holdReveal(value: Double) {
|
|
932
|
+
val sign = signBefore()
|
|
731
933
|
engine.holdReveal(value)
|
|
934
|
+
swapSignIfChanged(sign)
|
|
732
935
|
reportIntrinsicSize()
|
|
733
936
|
scheduleFrameIfNeeded()
|
|
734
937
|
invalidate()
|
|
@@ -736,16 +939,53 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
736
939
|
|
|
737
940
|
/** Counts up from 0 to [value] and lands with a pop (snaps when animations are off). */
|
|
738
941
|
fun reveal(value: Double) {
|
|
739
|
-
|
|
942
|
+
applyReduceMotion()
|
|
943
|
+
val sign = signBefore()
|
|
740
944
|
engine.reveal(value, now())
|
|
945
|
+
swapSignIfChanged(sign)
|
|
741
946
|
reportIntrinsicSize()
|
|
742
947
|
scheduleFrameIfNeeded()
|
|
743
948
|
invalidate()
|
|
949
|
+
updateMoving()
|
|
744
950
|
reportMilestones(0)
|
|
745
951
|
// Snapped (animations off / duration 0): the reveal is over before it began.
|
|
746
952
|
if (!engine.isRevealing()) onRevealEnd?.invoke()
|
|
747
953
|
}
|
|
748
954
|
|
|
955
|
+
/** Snaps while animations are removed, unless told not to (`respectReduceMotion`). */
|
|
956
|
+
private fun applyReduceMotion() {
|
|
957
|
+
engine.setReduceMotion(timing.respectReduceMotion && animationsDisabled())
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
/** The sign glyph on screen before a change, if one is. */
|
|
961
|
+
private fun signBefore(): String? {
|
|
962
|
+
if (!engine.hasShownValue() || engine.signFactor() <= 0.0) return null
|
|
963
|
+
return if (engine.signPositive()) format.plusSign else format.minusSign
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
/**
|
|
967
|
+
* A plus that turns into a minus (or back) swaps like any text: the old
|
|
968
|
+
* one softens away as the new one comes into focus.
|
|
969
|
+
*/
|
|
970
|
+
private fun swapSignIfChanged(before: String?) {
|
|
971
|
+
if (before == null) return
|
|
972
|
+
val after = if (engine.signPositive()) format.plusSign else format.minusSign
|
|
973
|
+
if (after == before) return
|
|
974
|
+
leavingText[TEXT_SIGN] = before
|
|
975
|
+
engine.changeText(TEXT_SIGN, now())
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
/**
|
|
979
|
+
* Fires [onAnimationStart] when a roll or a reveal sets off from rest and
|
|
980
|
+
* [onAnimationEnd] when the figure is still again, once for a run of changes.
|
|
981
|
+
*/
|
|
982
|
+
private fun updateMoving() {
|
|
983
|
+
val now = engine.isRolling() || engine.isRevealing()
|
|
984
|
+
if (now == moving) return
|
|
985
|
+
moving = now
|
|
986
|
+
if (now) onAnimationStart?.invoke() else onAnimationEnd?.invoke(engine.targetValue())
|
|
987
|
+
}
|
|
988
|
+
|
|
749
989
|
/** Re-sends the last reported intrinsic size (e.g. after a listener was attached). */
|
|
750
990
|
fun resendIntrinsicSize() {
|
|
751
991
|
if (lastReportedWidth < 0f) return
|
|
@@ -823,14 +1063,14 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
823
1063
|
}
|
|
824
1064
|
|
|
825
1065
|
/** Reused per frame so the JNI hop never allocates (room for far more wheels than the engine's 18), then the text slots. */
|
|
826
|
-
private val frameBuffer = DoubleArray(4 +
|
|
1066
|
+
private val frameBuffer = DoubleArray(4 + WHEEL_FIELDS * 32 + 4 * TEXT_SLOTS + 2)
|
|
827
1067
|
|
|
828
1068
|
// The decimal columns laid out now (more than the format's while dropped
|
|
829
1069
|
// ones close) and the decimal separator's factor.
|
|
830
1070
|
private var displayFractionDigits = 0
|
|
831
1071
|
private var decimalFactor = 1.0
|
|
832
1072
|
|
|
833
|
-
// Each text slot's swap (prefix, suffix, grouping, decimal): the text
|
|
1073
|
+
// Each text slot's swap (prefix, suffix, grouping, decimal, sign): the text
|
|
834
1074
|
// leaving, and the engine's clocks for it.
|
|
835
1075
|
private val leavingText = arrayOfNulls<String>(TEXT_SLOTS)
|
|
836
1076
|
private val textGrow = DoubleArray(TEXT_SLOTS) { 1.0 }
|
|
@@ -849,7 +1089,7 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
849
1089
|
while (wheels.size < count) wheels.add(Wheel())
|
|
850
1090
|
while (wheels.size > count) wheels.removeAt(wheels.size - 1)
|
|
851
1091
|
for (i in 0 until count) {
|
|
852
|
-
val base = 4 + i *
|
|
1092
|
+
val base = 4 + i * WHEEL_FIELDS
|
|
853
1093
|
val w = wheels[i]
|
|
854
1094
|
w.position = f[base]
|
|
855
1095
|
w.width = f[base + 1]
|
|
@@ -864,8 +1104,10 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
864
1104
|
w.focus = f[base + 10]
|
|
865
1105
|
w.grow = f[base + 11]
|
|
866
1106
|
w.blurOut = f[base + 12]
|
|
1107
|
+
w.progress = f[base + 13]
|
|
1108
|
+
w.modulus = f[base + 14].toInt()
|
|
867
1109
|
}
|
|
868
|
-
val text = 4 + count *
|
|
1110
|
+
val text = 4 + count * WHEEL_FIELDS
|
|
869
1111
|
for (slot in 0 until TEXT_SLOTS) {
|
|
870
1112
|
textGrow[slot] = f[text + slot * 4]
|
|
871
1113
|
textFocus[slot] = f[text + slot * 4 + 1]
|
|
@@ -883,6 +1125,7 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
883
1125
|
|
|
884
1126
|
private fun rebuildFonts() {
|
|
885
1127
|
fonts = FontSet(typography)
|
|
1128
|
+
cellWidths.clear()
|
|
886
1129
|
warmSwapMasks()
|
|
887
1130
|
fontScale = 1f
|
|
888
1131
|
if (engine.hasShownValue()) reportIntrinsicSize()
|
|
@@ -898,8 +1141,10 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
898
1141
|
private fun warmSwapMasks() {
|
|
899
1142
|
if (timing.transition != Transition.NUMERIC) return
|
|
900
1143
|
val set = fonts
|
|
1144
|
+
val fraction = format.fractionDigits > 0
|
|
901
1145
|
WarmExecutor.execute {
|
|
902
|
-
for (digit in 0..9) for (level in 0..NUMERIC_BLUR_LEVELS) set.blurred(digit, level)
|
|
1146
|
+
for (digit in 0..9) for (level in 0..NUMERIC_BLUR_LEVELS) set.blurred(GlyphRole.DIGIT, digit, level)
|
|
1147
|
+
if (fraction) for (digit in 0..9) for (level in 0..NUMERIC_BLUR_LEVELS) set.blurred(GlyphRole.FRACTION, digit, level)
|
|
903
1148
|
}
|
|
904
1149
|
}
|
|
905
1150
|
|
|
@@ -937,6 +1182,8 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
937
1182
|
var slot = -1
|
|
938
1183
|
var fromText: String? = null
|
|
939
1184
|
var anchor = 1
|
|
1185
|
+
/** Space after the cell (letter spacing, or an affix seam), scaled with it; not part of the glyph's box. */
|
|
1186
|
+
var gap = 0f
|
|
940
1187
|
}
|
|
941
1188
|
|
|
942
1189
|
/** A list of pooled [Element]s: filling it allocates nothing once it has grown to the run's length. */
|
|
@@ -958,7 +1205,7 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
958
1205
|
|
|
959
1206
|
fun totalWidth(): Float {
|
|
960
1207
|
var total = 0f
|
|
961
|
-
for (i in 0 until size) total += items[i].width
|
|
1208
|
+
for (i in 0 until size) total += items[i].width + items[i].gap
|
|
962
1209
|
return total
|
|
963
1210
|
}
|
|
964
1211
|
}
|
|
@@ -1011,38 +1258,149 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1011
1258
|
// the number is "USD " before it.
|
|
1012
1259
|
val rtl = isRtl
|
|
1013
1260
|
if (affixBlocksDirty || affixRtl != rtl) updateAffixBlocks(rtl)
|
|
1261
|
+
val signGlyph = if (engine.signPositive()) format.plusSign else format.minusSign
|
|
1014
1262
|
// A swapping affix keeps to the digits' side; a separator is centred.
|
|
1015
1263
|
if (rtl) {
|
|
1016
1264
|
addGlyph(suffixInk, GlyphRole.SUFFIX, 1.0, TEXT_SUFFIX, anchor = 1)
|
|
1017
1265
|
addGlyph(suffixGap, GlyphRole.SUFFIX, 1.0)
|
|
1018
1266
|
} else {
|
|
1019
1267
|
// Sign first, then the currency prefix: "-$1,234.50".
|
|
1020
|
-
addGlyph(
|
|
1268
|
+
addGlyph(signGlyph, GlyphRole.DIGIT, signFactor, TEXT_SIGN, anchor = 1)
|
|
1021
1269
|
addGlyph(prefixInk, GlyphRole.PREFIX, 1.0, TEXT_PREFIX, anchor = 1)
|
|
1022
1270
|
}
|
|
1271
|
+
// Settled, and throughout a reveal (whose layout is the target's from the
|
|
1272
|
+
// first frame), a proportional digit takes its target digit's width.
|
|
1273
|
+
val targetWidths = fonts.proportional && (!animated || engine.isRevealing())
|
|
1023
1274
|
for (power in wheels.indices.reversed()) {
|
|
1024
1275
|
val wheel = wheels[power]
|
|
1025
1276
|
if (wheel.width > 0.0) {
|
|
1026
1277
|
val e = out.next()
|
|
1278
|
+
// The fraction digits draw in their own font and colour.
|
|
1279
|
+
val role = if (power < fd) GlyphRole.FRACTION else GlyphRole.DIGIT
|
|
1280
|
+
val advance = digitAdvance(wheel, power, role, fonts, targetWidths)
|
|
1027
1281
|
e.wheelIndex = power
|
|
1028
1282
|
e.text = null
|
|
1029
|
-
e.role =
|
|
1030
|
-
e.width = (
|
|
1031
|
-
e.fullWidth =
|
|
1283
|
+
e.role = role
|
|
1284
|
+
e.width = (advance * wheel.width).toFloat()
|
|
1285
|
+
e.fullWidth = advance
|
|
1032
1286
|
e.factor = wheel.width
|
|
1033
1287
|
e.slot = -1
|
|
1034
1288
|
e.fromText = null
|
|
1035
1289
|
}
|
|
1036
|
-
if (power > fd && (power - fd
|
|
1037
|
-
if (fd > 0 && power == fd) addGlyph(format.decimalSeparator, GlyphRole.
|
|
1290
|
+
if (power > fd && isGroupBoundary(power - fd, format.groupingSizes)) addGlyph(format.groupingSeparator, GlyphRole.DIGIT, wheel.width, TEXT_GROUPING, anchor = 0)
|
|
1291
|
+
if (fd > 0 && power == fd) addGlyph(format.decimalSeparator, GlyphRole.FRACTION, decimal, TEXT_DECIMAL, anchor = 0)
|
|
1038
1292
|
}
|
|
1039
1293
|
if (rtl) {
|
|
1040
1294
|
addGlyph(prefixGap, GlyphRole.PREFIX, 1.0)
|
|
1041
1295
|
addGlyph(prefixInk, GlyphRole.PREFIX, 1.0, TEXT_PREFIX, anchor = -1)
|
|
1042
|
-
addGlyph(
|
|
1296
|
+
addGlyph(signGlyph, GlyphRole.DIGIT, signFactor, TEXT_SIGN, anchor = -1)
|
|
1043
1297
|
} else {
|
|
1044
1298
|
addGlyph(suffixInk, GlyphRole.SUFFIX, 1.0, TEXT_SUFFIX, anchor = -1)
|
|
1045
1299
|
}
|
|
1300
|
+
applySpacing(fonts, out)
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
/**
|
|
1304
|
+
* Whether a grouping separator follows the [k]th integer digit counted from
|
|
1305
|
+
* the decimal point: every three by default, or [sizes] (the first group,
|
|
1306
|
+
* then each later one: [3, 2] is 12,34,567). A size of 0 is none.
|
|
1307
|
+
*/
|
|
1308
|
+
private fun isGroupBoundary(k: Int, sizes: List<Int>): Boolean {
|
|
1309
|
+
val primary = if (sizes.isEmpty()) 3 else sizes[0]
|
|
1310
|
+
if (primary <= 0 || k < primary) return false
|
|
1311
|
+
val secondary = if (sizes.size > 1) sizes[1] else primary
|
|
1312
|
+
if (secondary <= 0) return k == primary
|
|
1313
|
+
return (k - primary) % secondary == 0
|
|
1314
|
+
}
|
|
1315
|
+
|
|
1316
|
+
/**
|
|
1317
|
+
* A wheel's cell width at full size: the widest digit's with tabular
|
|
1318
|
+
* figures; otherwise its digit's own advance, blended between the two digits
|
|
1319
|
+
* it is passing through (a roll between them, or a swap), or the target
|
|
1320
|
+
* digit's when [target] is set.
|
|
1321
|
+
*/
|
|
1322
|
+
private fun digitAdvance(wheel: Wheel, power: Int, role: GlyphRole, fonts: FontSet, target: Boolean): Float {
|
|
1323
|
+
if (!fonts.proportional) return fonts.digitWidth(role)
|
|
1324
|
+
val widths = fonts.digitWidths(role)
|
|
1325
|
+
val digit = engine.targetDigit(power).coerceIn(0, 9)
|
|
1326
|
+
val goal = widths[digit]
|
|
1327
|
+
if (target) return goal
|
|
1328
|
+
// The width eases once, from where the column was to its target digit's,
|
|
1329
|
+
// on the wheel's own progress: following the digits it rolled past made
|
|
1330
|
+
// every column pulse as a narrow 1 went by.
|
|
1331
|
+
var cell = cellWidths[power]
|
|
1332
|
+
if (cell == null) {
|
|
1333
|
+
cell = CellWidth()
|
|
1334
|
+
cellWidths[power] = cell
|
|
1335
|
+
cell.progress = 1.0
|
|
1336
|
+
cell.digit = -1
|
|
1337
|
+
cell.role = role
|
|
1338
|
+
}
|
|
1339
|
+
if (cell.role != role) {
|
|
1340
|
+
// A column that became a fraction digit (or stopped being one) starts over in its new font.
|
|
1341
|
+
cell.from = 0f
|
|
1342
|
+
cell.shown = 0f
|
|
1343
|
+
cell.digit = -1
|
|
1344
|
+
cell.progress = 1.0
|
|
1345
|
+
cell.role = role
|
|
1346
|
+
}
|
|
1347
|
+
if (cell.digit != digit || wheel.progress < cell.progress) {
|
|
1348
|
+
// Interrupted mid-change, it carries on from what it showed; from rest,
|
|
1349
|
+
// from the digit the wheel is leaving.
|
|
1350
|
+
cell.from = if (cell.digit >= 0 && cell.progress < 1.0) cell.shown else restingWidth(wheel, widths, goal)
|
|
1351
|
+
cell.digit = digit
|
|
1352
|
+
}
|
|
1353
|
+
cell.progress = wheel.progress
|
|
1354
|
+
cell.shown = cell.from + (goal - cell.from) * wheel.progress.toFloat().coerceIn(0f, 1f)
|
|
1355
|
+
return cell.shown
|
|
1356
|
+
}
|
|
1357
|
+
|
|
1358
|
+
/** The width of the digit a wheel shows at the start of a change (blank takes the target's). */
|
|
1359
|
+
private fun restingWidth(wheel: Wheel, widths: FloatArray, goal: Float): Float {
|
|
1360
|
+
val glyph = if (wheel.blend < 1.0) {
|
|
1361
|
+
wheel.fromGlyph.toInt()
|
|
1362
|
+
} else {
|
|
1363
|
+
val position = if (wheel.linear) wheel.position else wrap(wheel.position, wheel.modulus)
|
|
1364
|
+
Math.round(position).toInt() % wheel.modulus
|
|
1365
|
+
}
|
|
1366
|
+
return if (glyph >= 0) widths[glyph % 10] else goal
|
|
1367
|
+
}
|
|
1368
|
+
|
|
1369
|
+
/** A proportional column's width through a change, by place value. */
|
|
1370
|
+
private class CellWidth(var from: Float = 0f, var shown: Float = 0f, var digit: Int = -1, var progress: Double = 1.0, var role: GlyphRole = GlyphRole.DIGIT)
|
|
1371
|
+
|
|
1372
|
+
private val cellWidths = HashMap<Int, CellWidth>()
|
|
1373
|
+
|
|
1374
|
+
/**
|
|
1375
|
+
* Letter spacing after every cell, and the affix seams: the space between
|
|
1376
|
+
* the prefix and the digits and between the digits and the suffix.
|
|
1377
|
+
*/
|
|
1378
|
+
private fun applySpacing(fonts: FontSet, out: ElementList) {
|
|
1379
|
+
for (i in 0 until out.size) out[i].gap = 0f
|
|
1380
|
+
if (fonts.digitSpacing == 0f && fonts.prefixSeam == null && fonts.suffixSeam == null) return
|
|
1381
|
+
// A wheel's role is its font's: a fraction digit is spaced like the fraction.
|
|
1382
|
+
for (i in 0 until out.size) out[i].gap = fonts.spacing(out[i].role) * out[i].factor.toFloat()
|
|
1383
|
+
// The seam is the gap at the affix's edge that faces the digits (the far side in RTL).
|
|
1384
|
+
var firstPrefix = -1
|
|
1385
|
+
var lastPrefix = -1
|
|
1386
|
+
var firstSuffix = -1
|
|
1387
|
+
var lastSuffix = -1
|
|
1388
|
+
for (i in 0 until out.size) {
|
|
1389
|
+
when (out[i].role) {
|
|
1390
|
+
GlyphRole.PREFIX -> { if (firstPrefix < 0) firstPrefix = i; lastPrefix = i }
|
|
1391
|
+
GlyphRole.SUFFIX -> { if (firstSuffix < 0) firstSuffix = i; lastSuffix = i }
|
|
1392
|
+
GlyphRole.DIGIT, GlyphRole.FRACTION -> Unit
|
|
1393
|
+
}
|
|
1394
|
+
}
|
|
1395
|
+
val rtl = isRtl
|
|
1396
|
+
fonts.prefixSeam?.let { seam ->
|
|
1397
|
+
if (!rtl && lastPrefix >= 0) out[lastPrefix].gap = seam
|
|
1398
|
+
else if (rtl && firstPrefix > 0) out[firstPrefix - 1].gap = seam
|
|
1399
|
+
}
|
|
1400
|
+
fonts.suffixSeam?.let { seam ->
|
|
1401
|
+
if (!rtl && firstSuffix > 0) out[firstSuffix - 1].gap = seam
|
|
1402
|
+
else if (rtl && lastSuffix >= 0) out[lastSuffix].gap = seam
|
|
1403
|
+
}
|
|
1046
1404
|
}
|
|
1047
1405
|
|
|
1048
1406
|
private fun updateAffixBlocks(rtl: Boolean) {
|
|
@@ -1150,20 +1508,30 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1150
1508
|
private fun accessibleText(): String {
|
|
1151
1509
|
val fd = format.fractionDigits
|
|
1152
1510
|
val sb = StringBuilder()
|
|
1153
|
-
|
|
1511
|
+
// Any sign the settled figure carries, a plus included; Latin digits, whatever the glyphs.
|
|
1512
|
+
if (engine.settledNegative()) sb.append(if (engine.signPositive()) format.plusSign else format.minusSign)
|
|
1154
1513
|
sb.append(format.prefix)
|
|
1155
1514
|
for (power in engine.settledPowerCount() - 1 downTo 0) {
|
|
1156
1515
|
sb.append(engine.targetDigit(power))
|
|
1157
|
-
if (power > fd && (power - fd
|
|
1516
|
+
if (power > fd && isGroupBoundary(power - fd, format.groupingSizes)) sb.append(format.groupingSeparator)
|
|
1158
1517
|
if (fd > 0 && power == fd) sb.append(format.decimalSeparator)
|
|
1159
1518
|
}
|
|
1160
1519
|
sb.append(format.suffix)
|
|
1161
1520
|
return sb.toString()
|
|
1162
1521
|
}
|
|
1163
1522
|
|
|
1523
|
+
/** An `accessibilityLabel` the app set, which TalkBack reads instead of the figure. */
|
|
1524
|
+
private var explicitDescription: CharSequence? = null
|
|
1525
|
+
|
|
1526
|
+
override fun setContentDescription(contentDescription: CharSequence?) {
|
|
1527
|
+
explicitDescription = contentDescription
|
|
1528
|
+
super.setContentDescription(contentDescription)
|
|
1529
|
+
}
|
|
1530
|
+
|
|
1164
1531
|
// TalkBack reads the settled figure when it asks for it, so a value update
|
|
1165
1532
|
// (which can come every frame) formats nothing.
|
|
1166
1533
|
override fun getContentDescription(): CharSequence? {
|
|
1534
|
+
explicitDescription?.let { if (it.isNotEmpty()) return it }
|
|
1167
1535
|
if (!engine.hasShownValue()) return null
|
|
1168
1536
|
return if (loading) accessibleText() + ", loading" else accessibleText()
|
|
1169
1537
|
}
|
|
@@ -1240,13 +1608,13 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1240
1608
|
val element = elements[i]
|
|
1241
1609
|
val text = element.text
|
|
1242
1610
|
if (element.wheelIndex >= 0) {
|
|
1243
|
-
drawWheel(canvas, fonts, wheels[element.wheelIndex], x, element.width, originX, originY, scale)
|
|
1611
|
+
drawWheel(canvas, fonts, wheels[element.wheelIndex], element.role, x, element.width, element.fullWidth, originX, originY, scale)
|
|
1244
1612
|
} else if (element.fromText != null) {
|
|
1245
1613
|
drawTextSwap(canvas, fonts, element, x)
|
|
1246
1614
|
} else if (text != null) {
|
|
1247
1615
|
drawGlyph(canvas, fonts, text, element.role, x, element.width, element.fullWidth, element.factor)
|
|
1248
1616
|
}
|
|
1249
|
-
x += element.width
|
|
1617
|
+
x += element.width + element.gap
|
|
1250
1618
|
}
|
|
1251
1619
|
if (dim > 0f) {
|
|
1252
1620
|
drawShimmer(canvas, fonts, total, dim)
|
|
@@ -1262,22 +1630,33 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1262
1630
|
*/
|
|
1263
1631
|
private fun drawShimmer(canvas: Canvas, fonts: FontSet, contentWidth: Float, dim: Float) {
|
|
1264
1632
|
if (contentWidth <= 0f) return
|
|
1265
|
-
val base = fonts.digit.color
|
|
1633
|
+
val base = shimmer.baseColor ?: fonts.digit.color
|
|
1266
1634
|
val highlight = shimmer.color ?: defaultShimmerColor()
|
|
1267
|
-
|
|
1268
|
-
val
|
|
1269
|
-
|
|
1270
|
-
val
|
|
1635
|
+
// One sweep takes `durationMs`; the band then waits off the far edge for `delayMs`.
|
|
1636
|
+
val cycleMs = shimmer.durationMs + shimmer.delayMs
|
|
1637
|
+
val phase = (engine.shimmerPhase(now(), cycleMs / 1000.0).toFloat() * cycleMs / shimmer.durationMs).coerceAtMost(1f)
|
|
1638
|
+
val seeded = SHIMMER_SEED + (1f - SHIMMER_SEED) * phase
|
|
1639
|
+
val ltr = shimmer.leftToRight ?: !isRtl
|
|
1640
|
+
val progress = if (ltr) seeded else 1f - seeded
|
|
1641
|
+
// The gradient runs `length` along x (its slant on top): it enters at the
|
|
1642
|
+
// left edge and leaves past the right, or the mirror image.
|
|
1643
|
+
val length = contentWidth * shimmer.width.coerceAtLeast(0.05f)
|
|
1644
|
+
val startX = (contentWidth + length) * progress - length
|
|
1645
|
+
val slant = Math.tan(Math.toRadians(shimmer.angle.coerceIn(-75f, 75f).toDouble())).toFloat() * (if (ltr) 1f else -1f)
|
|
1271
1646
|
var gradient = shimmerGradient
|
|
1272
|
-
if (gradient == null || shimmerGradientWidth != contentWidth || shimmerGradientBase != base || shimmerGradientHighlight != highlight
|
|
1647
|
+
if (gradient == null || shimmerGradientWidth != contentWidth || shimmerGradientBase != base || shimmerGradientHighlight != highlight ||
|
|
1648
|
+
shimmerGradientLength != length || shimmerGradientSlant != slant
|
|
1649
|
+
) {
|
|
1273
1650
|
gradient = LinearGradient(
|
|
1274
|
-
0f, 0f,
|
|
1651
|
+
0f, 0f, length, slant * length,
|
|
1275
1652
|
intArrayOf(base, highlight, base), floatArrayOf(0.1f, 0.5f, 0.9f), Shader.TileMode.CLAMP,
|
|
1276
1653
|
)
|
|
1277
1654
|
shimmerGradient = gradient
|
|
1278
1655
|
shimmerGradientWidth = contentWidth
|
|
1279
1656
|
shimmerGradientBase = base
|
|
1280
1657
|
shimmerGradientHighlight = highlight
|
|
1658
|
+
shimmerGradientLength = length
|
|
1659
|
+
shimmerGradientSlant = slant
|
|
1281
1660
|
shimmerPaint.shader = gradient
|
|
1282
1661
|
}
|
|
1283
1662
|
shimmerMatrix.setTranslate(startX, 0f)
|
|
@@ -1328,11 +1707,16 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1328
1707
|
* [originX], [originY] and [scale] are the content transform already on the
|
|
1329
1708
|
* canvas, so the strip window can be landed on whole device pixels.
|
|
1330
1709
|
*/
|
|
1331
|
-
private fun drawWheel(canvas: Canvas, fonts: FontSet, wheel: Wheel, x: Float, width: Float, originX: Float, originY: Float, scale: Float) {
|
|
1710
|
+
private fun drawWheel(canvas: Canvas, fonts: FontSet, wheel: Wheel, role: GlyphRole, x: Float, width: Float, advance: Float, originX: Float, originY: Float, scale: Float) {
|
|
1332
1711
|
if (width <= 0f) return
|
|
1333
1712
|
val lineHeight = fonts.lineHeight
|
|
1713
|
+
val column = fonts.digitWidth(role)
|
|
1714
|
+
val modulus = wheel.modulus
|
|
1715
|
+
// The digit column, the widest digit wide, centred on the digit's own cell
|
|
1716
|
+
// (with tabular figures the two are the same, right-aligned in the cell).
|
|
1717
|
+
val columnLeft = x + width - (advance + column) / 2f
|
|
1334
1718
|
if (wheel.blend < 1.0 || wheel.focus < 1.0 || wheel.grow < 1.0) {
|
|
1335
|
-
drawSwap(canvas, fonts, wheel,
|
|
1719
|
+
drawSwap(canvas, fonts, wheel, role, columnLeft, originX, originY, scale)
|
|
1336
1720
|
return
|
|
1337
1721
|
}
|
|
1338
1722
|
// A settled-width wheel is a window onto the shared strip (the strip has
|
|
@@ -1340,13 +1724,14 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1340
1724
|
// canvas without a GPU, which cannot draw a hardware bitmap, draws its
|
|
1341
1725
|
// two glyphs the old way).
|
|
1342
1726
|
if (wheel.width >= 1.0 && canvas.isHardwareAccelerated) {
|
|
1343
|
-
val strip = fonts.strip(wheel.blankZero)
|
|
1727
|
+
val strip = fonts.strip(role, wheel.blankZero, modulus)
|
|
1344
1728
|
if (strip != null) {
|
|
1345
|
-
// An interior wheel wraps modulo 10,
|
|
1346
|
-
// through 0 shows 9); a linear wheel
|
|
1347
|
-
// blank slot. Either way the window
|
|
1348
|
-
|
|
1349
|
-
|
|
1729
|
+
// An interior wheel wraps modulo its places (10, a clock's 6),
|
|
1730
|
+
// negatives included (rolling down through 0 shows 9); a linear wheel
|
|
1731
|
+
// never wraps and uses -1 for the blank slot. Either way the window
|
|
1732
|
+
// lands inside the strip.
|
|
1733
|
+
val position = if (wheel.linear) wheel.position.coerceIn(-1.0, modulus.toDouble()) else wrap(wheel.position, modulus)
|
|
1734
|
+
var tx = columnLeft
|
|
1350
1735
|
var ty = (-(position + 1) * lineHeight).toFloat()
|
|
1351
1736
|
if (scale == 1f && position == floor(position)) {
|
|
1352
1737
|
// At rest, land the window on whole device pixels: composited at a
|
|
@@ -1380,7 +1765,7 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1380
1765
|
return
|
|
1381
1766
|
}
|
|
1382
1767
|
canvas.save()
|
|
1383
|
-
canvas.clipRect(
|
|
1768
|
+
canvas.clipRect(columnLeft, 0f, columnLeft + column, lineHeight)
|
|
1384
1769
|
canvas.translate(tx, ty)
|
|
1385
1770
|
if (strip.node != null) canvas.drawRenderNode(strip.node) else canvas.drawBitmap(strip.bitmap, 0f, 0f, stripPaint)
|
|
1386
1771
|
// The change flash rides the roll: the strip once more, in the tint, at the same offset.
|
|
@@ -1396,34 +1781,34 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1396
1781
|
return
|
|
1397
1782
|
}
|
|
1398
1783
|
}
|
|
1399
|
-
val baseline = fonts.
|
|
1784
|
+
val baseline = fonts.digitBaseline(role)
|
|
1400
1785
|
canvas.save()
|
|
1401
1786
|
// The roll's window, the whole digit wide: a column still opening or
|
|
1402
1787
|
// closing is not cut to its width (the glyph read as a sliver of its right
|
|
1403
1788
|
// edge, a ")" of a 0 rolling past), it overhangs the cell's far side, faded.
|
|
1404
|
-
canvas.clipRect(
|
|
1405
|
-
val
|
|
1406
|
-
val
|
|
1789
|
+
canvas.clipRect(columnLeft, 0f, columnLeft + column, lineHeight)
|
|
1790
|
+
val position = if (wheel.linear) wheel.position else wrap(wheel.position, modulus)
|
|
1791
|
+
val base = floor(position)
|
|
1792
|
+
val fraction = (position - base).toFloat()
|
|
1407
1793
|
val index = base.toInt()
|
|
1408
|
-
val columnLeft = x + width - fonts.digitWidth
|
|
1409
1794
|
fun glyphs(paint: TextPaint, alpha: Float) {
|
|
1410
1795
|
// Scaled from the ink colour's alpha and put back (see [drawGlyph]).
|
|
1411
1796
|
val ink = paint.alpha
|
|
1412
1797
|
paint.alpha = (alpha * ink).toInt().coerceIn(0, 255)
|
|
1413
|
-
glyphAt(index, wheel)?.let { glyph ->
|
|
1414
|
-
canvas.drawText(glyph, columnLeft + (
|
|
1798
|
+
glyphAt(index, wheel, fonts)?.let { glyph ->
|
|
1799
|
+
canvas.drawText(glyph, columnLeft + (column - fonts.width(glyph, role)) / 2f, baseline - fraction * lineHeight, paint)
|
|
1415
1800
|
}
|
|
1416
1801
|
if (fraction > 0.0001f) {
|
|
1417
|
-
glyphAt(index + 1, wheel)?.let { glyph ->
|
|
1418
|
-
canvas.drawText(glyph, columnLeft + (
|
|
1802
|
+
glyphAt(index + 1, wheel, fonts)?.let { glyph ->
|
|
1803
|
+
canvas.drawText(glyph, columnLeft + (column - fonts.width(glyph, role)) / 2f, baseline + (1f - fraction) * lineHeight, paint)
|
|
1419
1804
|
}
|
|
1420
1805
|
}
|
|
1421
1806
|
paint.alpha = ink
|
|
1422
1807
|
}
|
|
1423
1808
|
val opening = openingAlpha(wheel.width).toFloat()
|
|
1424
|
-
glyphs(fonts.
|
|
1809
|
+
glyphs(fonts.paint(role), opening)
|
|
1425
1810
|
// The change flash rides the roll: the same glyphs again, in the tint.
|
|
1426
|
-
flashColor(wheel)?.let { color -> glyphs(fonts.tinted(color), wheel.flash.toFloat() * opening) }
|
|
1811
|
+
flashColor(wheel)?.let { color -> glyphs(fonts.tinted(color, role), wheel.flash.toFloat() * opening) }
|
|
1427
1812
|
canvas.restore()
|
|
1428
1813
|
}
|
|
1429
1814
|
|
|
@@ -1436,9 +1821,9 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1436
1821
|
* around the digit. With a change flash on, the pair is drawn again in
|
|
1437
1822
|
* the tint at the flash's opacity: the ink mixed towards the tint.
|
|
1438
1823
|
*/
|
|
1439
|
-
private fun drawSwap(canvas: Canvas, fonts: FontSet, wheel: Wheel,
|
|
1440
|
-
drawSwapPair(canvas, fonts, wheel,
|
|
1441
|
-
flashColor(wheel)?.let { color -> drawSwapPair(canvas, fonts, wheel,
|
|
1824
|
+
private fun drawSwap(canvas: Canvas, fonts: FontSet, wheel: Wheel, role: GlyphRole, columnLeft: Float, originX: Float, originY: Float, scale: Float) {
|
|
1825
|
+
drawSwapPair(canvas, fonts, wheel, role, columnLeft, fonts.paint(role), 1f, originX, originY, scale)
|
|
1826
|
+
flashColor(wheel)?.let { color -> drawSwapPair(canvas, fonts, wheel, role, columnLeft, fonts.tinted(color, role), wheel.flash.toFloat(), originX, originY, scale) }
|
|
1442
1827
|
}
|
|
1443
1828
|
|
|
1444
1829
|
/**
|
|
@@ -1449,7 +1834,7 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1449
1834
|
* blurred copy hides and a lightly blurred one does not. Landed, the last
|
|
1450
1835
|
* swap frame is pixel for pixel the strip that takes over from it.
|
|
1451
1836
|
*/
|
|
1452
|
-
private fun drawSwapPair(canvas: Canvas, fonts: FontSet, wheel: Wheel,
|
|
1837
|
+
private fun drawSwapPair(canvas: Canvas, fonts: FontSet, wheel: Wheel, role: GlyphRole, columnLeft: Float, paint: TextPaint, opacity: Float, originX: Float, originY: Float, scale: Float) {
|
|
1453
1838
|
val lineHeight = fonts.lineHeight
|
|
1454
1839
|
// The position clock overshoots 1 (a spring); only the offsets follow it there.
|
|
1455
1840
|
val b = wheel.blend.toFloat()
|
|
@@ -1460,21 +1845,23 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1460
1845
|
val f = wheel.focus.toFloat().coerceIn(0f, 1f)
|
|
1461
1846
|
val d = if (wheel.fromAbove) 1f else -1f
|
|
1462
1847
|
val offset = lineHeight * NUMERIC_OFFSET
|
|
1463
|
-
val
|
|
1464
|
-
val
|
|
1848
|
+
val cell = fonts.digitWidth(role)
|
|
1849
|
+
val cx = columnLeft + cell / 2f
|
|
1850
|
+
// A fraction digit scales about its own centre, not the line box's.
|
|
1851
|
+
val cy = fonts.digitCenterY(role)
|
|
1465
1852
|
val column = wheel.width.toFloat().coerceIn(0f, 1f) * opacity
|
|
1466
1853
|
val from = wheel.fromGlyph.toInt()
|
|
1467
1854
|
val to = wheel.toGlyph.toInt()
|
|
1468
|
-
// The bitmap's top-left sits at (centre -
|
|
1469
|
-
val cornerX =
|
|
1470
|
-
val cornerY =
|
|
1855
|
+
// The bitmap's top-left sits at (centre - cell / 2 - pad, centre - centreY - pad); land that on a pixel.
|
|
1856
|
+
val cornerX = cell / 2f + fonts.blurPad
|
|
1857
|
+
val cornerY = cy + fonts.blurPad
|
|
1471
1858
|
fun snapX(c: Float): Float = (Math.round(originX + (c - cornerX) * scale) - originX) / scale + cornerX
|
|
1472
1859
|
fun snapY(c: Float): Float = (Math.round(originY + (c - cornerY) * scale) - originY) / scale + cornerY
|
|
1473
1860
|
if (from >= 0) {
|
|
1474
|
-
drawSwapGlyph(canvas, fonts, paint, from % 10, snapX(cx), snapY(cy + d * offset * b), 1f - (1f - NUMERIC_SCALE) * g, (1f - g) * column, wheel.blurOut.toFloat().coerceIn(0f, 1f))
|
|
1861
|
+
drawSwapGlyph(canvas, fonts, paint, role, from % 10, snapX(cx), snapY(cy + d * offset * b), 1f - (1f - NUMERIC_SCALE) * g, (1f - g) * column, wheel.blurOut.toFloat().coerceIn(0f, 1f))
|
|
1475
1862
|
}
|
|
1476
1863
|
if (to >= 0) {
|
|
1477
|
-
drawSwapGlyph(canvas, fonts, paint, to % 10, snapX(cx), snapY(cy - d * offset * (1f - b)), NUMERIC_SCALE + (1f - NUMERIC_SCALE) * g, g * column, 1f - f)
|
|
1864
|
+
drawSwapGlyph(canvas, fonts, paint, role, to % 10, snapX(cx), snapY(cy - d * offset * (1f - b)), NUMERIC_SCALE + (1f - NUMERIC_SCALE) * g, g * column, 1f - f)
|
|
1478
1865
|
}
|
|
1479
1866
|
}
|
|
1480
1867
|
|
|
@@ -1485,10 +1872,11 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1485
1872
|
* rather than a sharp glyph half-hidden in a fully blurred one, which read
|
|
1486
1873
|
* as a digit inside a glow.
|
|
1487
1874
|
*/
|
|
1488
|
-
private fun drawSwapGlyph(canvas: Canvas, fonts: FontSet, paint: TextPaint, digitIndex: Int, cx: Float, cy: Float, scale: Float, alpha: Float, blur: Float) {
|
|
1875
|
+
private fun drawSwapGlyph(canvas: Canvas, fonts: FontSet, paint: TextPaint, role: GlyphRole, digitIndex: Int, cx: Float, cy: Float, scale: Float, alpha: Float, blur: Float) {
|
|
1489
1876
|
if (alpha <= 0.002f) return
|
|
1490
|
-
val text =
|
|
1491
|
-
|
|
1877
|
+
val text = fonts.glyphs[digitIndex]
|
|
1878
|
+
// The mask is the whole line box; its origin is this far above the glyph's centre.
|
|
1879
|
+
val centerY = fonts.digitCenterY(role)
|
|
1492
1880
|
val x = blur.coerceIn(0f, 1f) * NUMERIC_BLUR_LEVELS
|
|
1493
1881
|
val lo = min(NUMERIC_BLUR_LEVELS, floor(x).toInt())
|
|
1494
1882
|
val hi = min(NUMERIC_BLUR_LEVELS, lo + 1)
|
|
@@ -1505,18 +1893,18 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1505
1893
|
canvas.scale(scale, scale)
|
|
1506
1894
|
for ((level, levelAlpha) in arrayOf(lo to loAlpha, hi to hiAlpha)) {
|
|
1507
1895
|
if (levelAlpha <= 0.002f) continue
|
|
1508
|
-
val mask = fonts.blurred(digitIndex, level)
|
|
1896
|
+
val mask = fonts.blurred(role, digitIndex, level)
|
|
1509
1897
|
if (mask == null) {
|
|
1510
1898
|
// No font metrics yet: the text itself.
|
|
1511
1899
|
val ink = paint.alpha
|
|
1512
1900
|
paint.alpha = (levelAlpha * ink).toInt().coerceIn(0, 255)
|
|
1513
|
-
canvas.drawText(text, -fonts.width(text,
|
|
1901
|
+
canvas.drawText(text, -fonts.width(text, role) / 2f, fonts.digitBaseline(role) - centerY, paint)
|
|
1514
1902
|
paint.alpha = ink
|
|
1515
1903
|
} else {
|
|
1516
1904
|
// An alpha mask draws in the paint's colour; the ink's own alpha scales with the level's.
|
|
1517
1905
|
blurPaint.color = paint.color
|
|
1518
1906
|
blurPaint.alpha = (levelAlpha * (paint.color ushr 24)).toInt().coerceIn(0, 255)
|
|
1519
|
-
canvas.drawBitmap(mask, -fonts.digitWidth / 2f - fonts.blurPad, -
|
|
1907
|
+
canvas.drawBitmap(mask, -fonts.digitWidth(role) / 2f - fonts.blurPad, -centerY - fonts.blurPad, blurPaint)
|
|
1520
1908
|
}
|
|
1521
1909
|
}
|
|
1522
1910
|
canvas.restore()
|
|
@@ -1577,11 +1965,11 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1577
1965
|
return w * w
|
|
1578
1966
|
}
|
|
1579
1967
|
|
|
1580
|
-
private fun glyphAt(index: Int, wheel: Wheel): String? {
|
|
1968
|
+
private fun glyphAt(index: Int, wheel: Wheel, fonts: FontSet): String? {
|
|
1581
1969
|
if (wheel.linear && index < 0) return null
|
|
1582
1970
|
if (wheel.blankZero && index == 0) return null
|
|
1583
|
-
val
|
|
1584
|
-
return
|
|
1971
|
+
val m = wheel.modulus
|
|
1972
|
+
return fonts.glyphs[((index % m) + m) % m]
|
|
1585
1973
|
}
|
|
1586
1974
|
|
|
1587
1975
|
// endregion
|
|
@@ -1597,6 +1985,7 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1597
1985
|
fun release() {
|
|
1598
1986
|
stopAnimation()
|
|
1599
1987
|
engine.reset()
|
|
1988
|
+
moving = false
|
|
1600
1989
|
wheels.clear()
|
|
1601
1990
|
frameElements.clear()
|
|
1602
1991
|
settledElements.clear()
|
|
@@ -1614,10 +2003,11 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1614
2003
|
*/
|
|
1615
2004
|
fun memoryEstimateBytes(): Long = 64L * 1024
|
|
1616
2005
|
|
|
1617
|
-
/** Interior wheels wrap modulo 10; a roll can be any real, so fold it onto 0 ≤ p <
|
|
1618
|
-
private fun
|
|
1619
|
-
val
|
|
1620
|
-
|
|
2006
|
+
/** Interior wheels wrap modulo their places (10, a clock's 6); a roll can be any real, so fold it onto 0 ≤ p < [modulus]. */
|
|
2007
|
+
private fun wrap(position: Double, modulus: Int): Double {
|
|
2008
|
+
val m = modulus.toDouble()
|
|
2009
|
+
val r = position % m
|
|
2010
|
+
return if (r < 0) r + m else r
|
|
1621
2011
|
}
|
|
1622
2012
|
|
|
1623
2013
|
/** A digit strip: the software-rendered bitmap and, from Android 10, the layer that holds it. */
|
|
@@ -1673,14 +2063,16 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1673
2063
|
}
|
|
1674
2064
|
|
|
1675
2065
|
companion object {
|
|
1676
|
-
/**
|
|
1677
|
-
private const val STRIP_SLOTS = 12
|
|
2066
|
+
/** The glyphs drawn for 0…9 unless `digitGlyphs` gives ten others. */
|
|
1678
2067
|
private val DIGITS = Array(10) { it.toString() }
|
|
1679
|
-
|
|
2068
|
+
/** Flash paints a font set keeps per role (a change flash has an up and a down colour). */
|
|
2069
|
+
private const val TINT_SLOTS = 4
|
|
2070
|
+
|
|
2071
|
+
/** Which strip a wheel draws from, as one comparable number. */
|
|
2072
|
+
private fun stripVariant(role: GlyphRole, blankZero: Boolean, modulus: Int): Int =
|
|
2073
|
+
(if (blankZero) 1 else 0) or (modulus shl 1) or (if (role == GlyphRole.FRACTION) 1 shl 8 else 0)
|
|
1680
2074
|
/** The core starts at the glyphs' left edge instead of parked off-screen. */
|
|
1681
2075
|
private const val SHIMMER_SEED = 0.25f
|
|
1682
|
-
/** How far the top of the band leads the bottom, as a fraction of the height ("/" slant). */
|
|
1683
|
-
private const val SHIMMER_SLANT = 0.6f
|
|
1684
2076
|
}
|
|
1685
2077
|
}
|
|
1686
2078
|
|