react-native-nitro-input 0.3.0 → 0.3.2
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 +80 -0
- package/README.md +120 -5
- package/android/src/main/cpp/JMaskEngine.cpp +45 -0
- package/android/src/main/cpp/JMaskEngine.hpp +9 -0
- 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 +42 -2
- package/android/src/main/cpp/JRollingEngine.hpp +9 -1
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputView.kt +24 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberFormatPlatform.kt +76 -1
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberView.kt +88 -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/MaskEngine.kt +16 -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 +96 -6
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroNumberView.kt +388 -136
- 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 +13 -2
- package/cpp/MaskEngine.cpp +294 -33
- package/cpp/MaskEngine.hpp +68 -0
- package/cpp/RollingEngine.cpp +92 -8
- package/cpp/RollingEngine.hpp +36 -2
- package/ios/HybridNitroInputView.swift +18 -0
- package/ios/HybridNitroNumberFormatPlatform.swift +87 -9
- package/ios/HybridNitroNumberView.swift +82 -1
- package/ios/HybridNitroTextView.swift +134 -0
- package/ios/NitroInputView.swift +125 -7
- package/ios/NitroNumberView.swift +368 -135
- package/ios/NitroTextView.swift +583 -0
- package/lib/commonjs/nitrogen/generated/shared/json/NitroInputViewConfig.json +6 -0
- package/lib/commonjs/nitrogen/generated/shared/json/NitroNumberViewConfig.json +20 -0
- package/lib/commonjs/nitrogen/generated/shared/json/NitroTextViewConfig.json +30 -0
- package/lib/commonjs/src/NitroInput.js +15 -2
- package/lib/commonjs/src/NitroNumber.js +52 -3
- package/lib/commonjs/src/NitroText.js +138 -0
- package/lib/commonjs/src/NitroTime.js +65 -0
- package/lib/commonjs/src/formatProps.js +76 -16
- package/lib/commonjs/src/index.js +6 -1
- package/lib/commonjs/src/specs/NitroText.nitro.js +2 -0
- package/lib/module/nitrogen/generated/shared/json/NitroInputViewConfig.json +6 -0
- package/lib/module/nitrogen/generated/shared/json/NitroNumberViewConfig.json +20 -0
- package/lib/module/nitrogen/generated/shared/json/NitroTextViewConfig.json +30 -0
- package/lib/module/src/NitroInput.js +15 -2
- package/lib/module/src/NitroNumber.js +53 -4
- package/lib/module/src/NitroText.js +98 -0
- package/lib/module/src/NitroTime.js +29 -0
- package/lib/module/src/formatProps.js +76 -17
- package/lib/module/src/index.js +2 -0
- package/lib/module/src/specs/NitroText.nitro.js +1 -0
- package/lib/typescript/src/NitroInput.d.ts +39 -1
- package/lib/typescript/src/NitroNumber.d.ts +79 -1
- package/lib/typescript/src/NitroText.d.ts +67 -0
- package/lib/typescript/src/NitroTime.d.ts +19 -0
- package/lib/typescript/src/formatProps.d.ts +30 -4
- package/lib/typescript/src/index.d.ts +5 -2
- package/lib/typescript/src/specs/NitroInput.nitro.d.ts +43 -0
- package/lib/typescript/src/specs/NitroNumber.nitro.d.ts +67 -0
- package/lib/typescript/src/specs/NitroText.nitro.d.ts +60 -0
- package/nitro.json +20 -0
- package/nitrogen/generated/android/NitroInput+autolinking.cmake +6 -0
- package/nitrogen/generated/android/NitroInputOnLoad.cpp +34 -0
- package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.cpp +102 -0
- package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.hpp +12 -0
- package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.cpp +243 -0
- package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.hpp +40 -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++/JNitroInputCharacterMapping.hpp +61 -0
- package/nitrogen/generated/android/c++/JNitroInputMaskAffinity.hpp +64 -0
- package/nitrogen/generated/android/c++/JNitroInputTextCase.hpp +61 -0
- package/nitrogen/generated/android/c++/JNitroNumberShimmerDirection.hpp +61 -0
- package/nitrogen/generated/android/c++/JNitroNumberSignDisplay.hpp +67 -0
- package/nitrogen/generated/android/c++/views/JHybridNitroInputViewStateUpdater.cpp +30 -0
- package/nitrogen/generated/android/c++/views/JHybridNitroNumberViewStateUpdater.cpp +100 -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 +36 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroNumberViewSpec.kt +136 -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/NitroInputCharacterMapping.kt +56 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputMaskAffinity.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputTextCase.kt +24 -0
- 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/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 +34 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.hpp +146 -11
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Umbrella.hpp +25 -0
- package/nitrogen/generated/ios/NitroInputAutolinking.mm +16 -0
- package/nitrogen/generated/ios/NitroInputAutolinking.swift +24 -0
- package/nitrogen/generated/ios/c++/HybridNitroInputViewSpecSwift.hpp +50 -0
- package/nitrogen/generated/ios/c++/HybridNitroNumberViewSpecSwift.hpp +146 -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 +36 -0
- package/nitrogen/generated/ios/c++/views/HybridNitroNumberViewComponent.mm +120 -0
- package/nitrogen/generated/ios/c++/views/HybridNitroTextViewComponent.mm +264 -0
- package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec.swift +6 -0
- package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec_cxx.swift +78 -0
- package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec.swift +20 -0
- package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec_cxx.swift +493 -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/NitroInputCharacterMapping.swift +34 -0
- package/nitrogen/generated/ios/swift/NitroInputMaskAffinity.swift +48 -0
- package/nitrogen/generated/ios/swift/NitroInputTextCase.swift +44 -0
- package/nitrogen/generated/ios/swift/NitroNumberShimmerDirection.swift +44 -0
- package/nitrogen/generated/ios/swift/NitroNumberSignDisplay.swift +52 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.cpp +12 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.hpp +21 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.cpp +40 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.hpp +46 -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++/NitroInputCharacterMapping.hpp +87 -0
- package/nitrogen/generated/shared/c++/NitroInputMaskAffinity.hpp +84 -0
- package/nitrogen/generated/shared/c++/NitroInputTextCase.hpp +80 -0
- package/nitrogen/generated/shared/c++/NitroNumberShimmerDirection.hpp +80 -0
- package/nitrogen/generated/shared/c++/NitroNumberSignDisplay.hpp +88 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.cpp +12 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.hpp +21 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.cpp +40 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.hpp +62 -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 +6 -0
- package/nitrogen/generated/shared/json/NitroNumberViewConfig.json +20 -0
- package/nitrogen/generated/shared/json/NitroTextViewConfig.json +30 -0
- package/package.json +2 -2
- package/src/NitroInput.tsx +73 -0
- package/src/NitroNumber.tsx +183 -9
- package/src/NitroText.tsx +166 -0
- package/src/NitroTime.tsx +68 -0
- package/src/formatProps.ts +108 -18
- package/src/index.ts +8 -0
- package/src/specs/NitroInput.nitro.ts +46 -0
- package/src/specs/NitroNumber.nitro.ts +69 -0
- package/src/specs/NitroText.nitro.ts +65 -0
|
@@ -59,9 +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
|
|
63
|
-
|
|
64
|
-
|
|
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
|
|
65
66
|
|
|
66
67
|
class NitroNumberView(context: Context) : View(context) {
|
|
67
68
|
|
|
@@ -74,6 +75,17 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
74
75
|
val decimalSeparator: String = ".",
|
|
75
76
|
val prefix: String = "",
|
|
76
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(),
|
|
77
89
|
)
|
|
78
90
|
|
|
79
91
|
enum class AffixAlign { BASELINE, CENTER, TOP, BOTTOM }
|
|
@@ -97,6 +109,15 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
97
109
|
val suffixOffset: Float = 0f,
|
|
98
110
|
/** Every digit as wide as the widest (tabular figures, the default), or each at its own width. */
|
|
99
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(),
|
|
100
121
|
val adjustsFontSizeToFit: Boolean = false,
|
|
101
122
|
val minimumFontScale: Float = 0.5f,
|
|
102
123
|
val allowFontScaling: Boolean = false,
|
|
@@ -130,6 +151,10 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
130
151
|
val revealStaggerMs: Long = 200,
|
|
131
152
|
/** Count style: how long the count pauses on each milestone. */
|
|
132
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,
|
|
133
158
|
)
|
|
134
159
|
|
|
135
160
|
enum class RevealStyle(val raw: Int) { COUNT(0), SPIN(1) }
|
|
@@ -148,6 +173,16 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
148
173
|
val color: Int? = null,
|
|
149
174
|
/** Duration of one sweep across the number. */
|
|
150
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,
|
|
151
186
|
)
|
|
152
187
|
|
|
153
188
|
/** `AUTO` is the start edge of the layout direction, as `Text` with no `textAlign`; the rest are absolute. */
|
|
@@ -174,6 +209,13 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
174
209
|
change(TEXT_SUFFIX, suffixInk(old.suffix), suffixInk(value.suffix))
|
|
175
210
|
change(TEXT_GROUPING, old.groupingSeparator, value.groupingSeparator)
|
|
176
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)
|
|
177
219
|
}
|
|
178
220
|
affixBlocksDirty = true
|
|
179
221
|
// Played in a glyph-swap transition; snaps otherwise (see the engine).
|
|
@@ -200,6 +242,7 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
200
242
|
engine.setRevealTiming(value.revealDurationMs / 1000.0, value.revealBounce, value.revealStyle.raw, value.revealStaggerMs / 1000.0)
|
|
201
243
|
engine.setRevealGrow(value.revealGrow)
|
|
202
244
|
engine.setRevealMilestoneHold(value.revealMilestoneHoldMs / 1000.0)
|
|
245
|
+
engine.setContinuous(value.continuous)
|
|
203
246
|
}
|
|
204
247
|
|
|
205
248
|
/** Win tiers of a count-style reveal, in the figure's units. */
|
|
@@ -231,7 +274,7 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
231
274
|
set(value) {
|
|
232
275
|
if (field == value) return
|
|
233
276
|
field = value
|
|
234
|
-
|
|
277
|
+
applyReduceMotion()
|
|
235
278
|
engine.setLoading(value, now())
|
|
236
279
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) stateDescription = if (value) "Loading" else null
|
|
237
280
|
scheduleFrameIfNeeded()
|
|
@@ -250,6 +293,11 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
250
293
|
var onRevealEnd: (() -> Unit)? = null
|
|
251
294
|
/** Called when a count-style reveal reaches a milestone (its index and value). */
|
|
252
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
|
|
253
301
|
|
|
254
302
|
/** The value currently shown or being rolled towards. */
|
|
255
303
|
val targetValue: Double
|
|
@@ -263,40 +311,65 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
263
311
|
|
|
264
312
|
// region Fonts
|
|
265
313
|
|
|
266
|
-
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
|
+
}
|
|
267
322
|
|
|
268
|
-
/** Digit / prefix / suffix paints with per-glyph width caches. */
|
|
323
|
+
/** Digit / prefix / suffix / fraction paints with per-glyph width caches. */
|
|
269
324
|
private inner class FontSet(t: Typography) {
|
|
270
325
|
private val scale = systemFontMultiplier(t)
|
|
271
|
-
val
|
|
272
|
-
val
|
|
273
|
-
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)
|
|
274
331
|
private val prefixAlign = t.prefixAlign
|
|
275
332
|
private val suffixAlign = t.suffixAlign
|
|
276
|
-
|
|
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. */
|
|
277
341
|
val digitSpacing = t.letterSpacing * scale * density
|
|
278
342
|
val prefixLetterSpacing = digitSpacing * ((t.prefixFontSize ?: t.fontSize) / t.fontSize)
|
|
279
343
|
val suffixLetterSpacing = digitSpacing * ((t.suffixFontSize ?: t.fontSize) / t.fontSize)
|
|
344
|
+
val fractionLetterSpacing = digitSpacing * ((t.fractionFontSize ?: t.fontSize) / t.fontSize)
|
|
280
345
|
val prefixSeam = t.prefixSpacing?.let { it * scale * density }
|
|
281
346
|
val suffixSeam = t.suffixSpacing?.let { it * scale * density }
|
|
282
347
|
private val prefixOffset = t.prefixOffset * scale * density
|
|
283
348
|
private val suffixOffset = t.suffixOffset * scale * density
|
|
284
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 }
|
|
285
352
|
/** Height of the line box (the digit paint's line height), in px. */
|
|
286
353
|
val lineHeight: Float = ceil(digitMetrics.descent - digitMetrics.ascent)
|
|
287
354
|
/** The numeric transition's blur radius at full blur, in line heights. */
|
|
288
355
|
val numericBlur: Float = NUMERIC_BLUR
|
|
289
356
|
/** Width of the widest digit glyph, in px. */
|
|
290
357
|
val digitWidth: Float
|
|
358
|
+
/** The same for the fraction digits, in their own font. */
|
|
359
|
+
val fractionDigitWidth: Float
|
|
291
360
|
/** Each digit at its own advance instead of the widest's (`tabularNums={false}`). */
|
|
292
361
|
val proportional = !t.tabularNums
|
|
293
362
|
/** The advance of each digit 0…9, in px. */
|
|
294
363
|
val digitWidths = FloatArray(10)
|
|
364
|
+
val fractionDigitWidths = FloatArray(10)
|
|
295
365
|
private val widthCache = Array(GlyphRole.entries.size) { java.util.concurrent.ConcurrentHashMap<String, Float>() }
|
|
296
366
|
/**
|
|
297
|
-
* A wheel's whole digit strip, per blank-zero variant
|
|
298
|
-
* -1 (blank) to
|
|
299
|
-
*
|
|
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
|
|
300
373
|
* once by the software text renderer into a bitmap, and the bitmap is the
|
|
301
374
|
* whole content of a `RenderNode` layer; a wheel then draws one slot-high
|
|
302
375
|
* window of that layer, one quad, and a frame costs no glyph work at all:
|
|
@@ -311,22 +384,29 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
311
384
|
* RenderNode and the bitmap is drawn directly.
|
|
312
385
|
*
|
|
313
386
|
* The strips are shared by every rolling number drawn with the same
|
|
314
|
-
* 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.
|
|
315
389
|
*/
|
|
316
|
-
private val
|
|
317
|
-
"${t.fontFamily}|${t.fontWeight}|${digit.textSize}|${digit.color}|${digit.typeface?.hashCode()}|${digit.fontFeatureSettings}"
|
|
390
|
+
private val strips = android.util.SparseArray<Strip>()
|
|
318
391
|
|
|
319
392
|
/**
|
|
320
|
-
* This font set's digit masks by digit and level,
|
|
321
|
-
* cache: a swapping glyph looks two up every frame,
|
|
322
|
-
* 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.
|
|
323
397
|
*/
|
|
324
|
-
private val digitMasks = AtomicReferenceArray<Bitmap>(10 * (NUMERIC_BLUR_LEVELS + 1))
|
|
325
|
-
|
|
326
|
-
fun strip(blankZero: Boolean): Strip? {
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
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)
|
|
330
410
|
}
|
|
331
411
|
val node = strip.node
|
|
332
412
|
// HWUI deletes a node's display list once nothing in the view tree draws
|
|
@@ -353,23 +433,26 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
353
433
|
* is a Gaussian to the eye; HWUI has no hardware mask filter), once per
|
|
354
434
|
* font, colour and density and shared like the strips. A transitioning
|
|
355
435
|
* glyph is its sharp text and this bitmap cross-faded, a fixed cost per
|
|
356
|
-
* frame instead of a blur pass. The bitmap is `digitWidth + 2 pad`
|
|
357
|
-
* 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.
|
|
358
439
|
*/
|
|
359
|
-
fun blurred(digitIndex: Int, level: Int): Bitmap? {
|
|
360
|
-
|
|
440
|
+
fun blurred(role: GlyphRole, digitIndex: Int, level: Int): Bitmap? {
|
|
441
|
+
val cell = digitWidth(role)
|
|
442
|
+
if (cell <= 0f || lineHeight <= 0f) return null
|
|
361
443
|
// Level 0 (or no blur at all) is the glyph unblurred, still a mask: a
|
|
362
444
|
// swapping glyph's two copies must be the same kind of thing, drawn the
|
|
363
445
|
// same way at the same sub-pixel position. Hardware text snaps to whole
|
|
364
446
|
// pixels as it moves and a bitmap does not, and cross-faded they slid in
|
|
365
447
|
// and out of register as the spring settled, a bold/pale flicker.
|
|
366
|
-
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)
|
|
367
450
|
digitMasks.get(slot)?.let { return it }
|
|
368
451
|
val radius = if (level <= 0 || numericBlur <= 0f) 0 else Math.round(lineHeight * numericBlur * level / NUMERIC_BLUR_LEVELS).coerceAtLeast(1)
|
|
369
|
-
val mask = BlurredGlyphCache.get("$
|
|
370
|
-
val text =
|
|
452
|
+
val mask = BlurredGlyphCache.get("${key(role)}|$digitIndex|$radius|$blurPad") {
|
|
453
|
+
val text = glyphs[digitIndex]
|
|
371
454
|
val pad = blurPad
|
|
372
|
-
val w = ceil(
|
|
455
|
+
val w = ceil(cell).toInt() + 2 * pad
|
|
373
456
|
val h = ceil(lineHeight).toInt() + 2 * pad
|
|
374
457
|
// An alpha mask, not a coloured bitmap: the glyph's coverage, blurred,
|
|
375
458
|
// that a draw fills with the digit paint's colour. A coloured
|
|
@@ -380,8 +463,8 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
380
463
|
val canvas = Canvas(mask)
|
|
381
464
|
// A paint of its own: the masks are warmed off the main thread, where
|
|
382
465
|
// the digit paint's alpha is being changed by the draws.
|
|
383
|
-
val paint = TextPaint(
|
|
384
|
-
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)
|
|
385
468
|
if (radius > 0) boxBlurMask(mask, radius)
|
|
386
469
|
// Immutable and uploaded ahead of its first draw: HWUI re-pins a mutable
|
|
387
470
|
// bitmap at every sync, and the first frame that drew a fresh mask paid
|
|
@@ -404,7 +487,7 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
404
487
|
fun blurredText(text: String, role: GlyphRole, level: Int): Bitmap? {
|
|
405
488
|
if (text.isEmpty() || lineHeight <= 0f) return null
|
|
406
489
|
val radius = if (level <= 0 || numericBlur <= 0f) 0 else Math.round(lineHeight * numericBlur * level / NUMERIC_BLUR_LEVELS).coerceAtLeast(1)
|
|
407
|
-
return BlurredGlyphCache.get("$
|
|
490
|
+
return BlurredGlyphCache.get("${key(role)}|t|$text|$radius|$blurPad") {
|
|
408
491
|
val pad = blurPad
|
|
409
492
|
val w = ceil(width(text, role)).toInt() + 2 * pad
|
|
410
493
|
val h = ceil(lineHeight).toInt() + 2 * pad
|
|
@@ -423,20 +506,41 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
423
506
|
/** Padding around a blurred glyph's bitmap, enough for the blur's tail. */
|
|
424
507
|
val blurPad: Int get() = ceil(lineHeight * numericBlur * 3f).toInt().coerceAtLeast(2)
|
|
425
508
|
|
|
426
|
-
/**
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
private
|
|
431
|
-
|
|
432
|
-
|
|
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
|
+
}
|
|
530
|
+
|
|
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()
|
|
433
536
|
val drawn = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
|
|
434
537
|
val canvas = Canvas(drawn)
|
|
435
|
-
val baseline =
|
|
436
|
-
|
|
538
|
+
val baseline = digitBaseline(role)
|
|
539
|
+
val paint = paint(role)
|
|
540
|
+
for (index in -1 until slots - 1) {
|
|
437
541
|
if (index < 0 || (blankZero && index == 0)) continue
|
|
438
|
-
val text =
|
|
439
|
-
canvas.drawText(text, (
|
|
542
|
+
val text = glyphs[index % modulus]
|
|
543
|
+
canvas.drawText(text, (cell - width(text, role)) / 2f, (index + 1) * lineHeight + baseline, paint)
|
|
440
544
|
}
|
|
441
545
|
// Immutable: the renderer uploads it once and keeps the texture.
|
|
442
546
|
val strip = drawn.copy(Bitmap.Config.ARGB_8888, false)
|
|
@@ -446,17 +550,53 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
446
550
|
private val capHeightCache = HashMap<GlyphRole, Float>()
|
|
447
551
|
private val inkDescentCache = HashMap<String, Float>()
|
|
448
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
|
+
|
|
449
564
|
init {
|
|
450
|
-
for (d in 0..9) digitWidths[d] = width(d
|
|
565
|
+
for (d in 0..9) digitWidths[d] = width(glyphs[d], GlyphRole.DIGIT)
|
|
451
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
|
+
}
|
|
452
578
|
}
|
|
453
579
|
|
|
580
|
+
private fun key(role: GlyphRole): String = roleKeys[role.ordinal]
|
|
581
|
+
|
|
454
582
|
fun paint(role: GlyphRole): TextPaint = when (role) {
|
|
455
583
|
GlyphRole.DIGIT -> digit
|
|
456
584
|
GlyphRole.PREFIX -> prefix
|
|
457
585
|
GlyphRole.SUFFIX -> suffix
|
|
586
|
+
GlyphRole.FRACTION -> fraction
|
|
458
587
|
}
|
|
459
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
|
+
|
|
460
600
|
// One map per role: a joined "role|text" key allocated a string on every
|
|
461
601
|
// lookup, and the layout looks widths up for every glyph on every frame.
|
|
462
602
|
fun width(text: String, role: GlyphRole): Float =
|
|
@@ -477,9 +617,11 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
477
617
|
shapedCache[role.ordinal].getOrPut(text) { ShapedText.of(text, paint(role)) }
|
|
478
618
|
|
|
479
619
|
/** Baseline y for `role` drawing `text`, given the top of the digit line box. */
|
|
480
|
-
fun baseline(role: GlyphRole, text: String, lineTop: Float): Float {
|
|
481
|
-
|
|
482
|
-
|
|
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
|
|
483
625
|
}
|
|
484
626
|
|
|
485
627
|
/** Letter spacing after a glyph of [role], in px. */
|
|
@@ -487,20 +629,27 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
487
629
|
GlyphRole.DIGIT -> digitSpacing
|
|
488
630
|
GlyphRole.PREFIX -> prefixLetterSpacing
|
|
489
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
|
|
490
640
|
}
|
|
491
641
|
|
|
492
642
|
private fun alignedBaseline(role: GlyphRole, text: String, lineTop: Float): Float {
|
|
493
643
|
val digitBaseline = lineTop - digitMetrics.ascent
|
|
494
|
-
val
|
|
495
|
-
|
|
496
|
-
return when (if (role == GlyphRole.PREFIX) prefixAlign else suffixAlign) {
|
|
644
|
+
val m = roleMetrics[role.ordinal]
|
|
645
|
+
return when (alignment(role)) {
|
|
497
646
|
AffixAlign.BASELINE -> digitBaseline
|
|
498
647
|
AffixAlign.CENTER -> lineTop + (lineHeight - (m.descent - m.ascent)) / 2f - m.ascent
|
|
499
648
|
AffixAlign.TOP -> lineTop + (-digitMetrics.ascent - capHeight(GlyphRole.DIGIT)) + capHeight(role)
|
|
500
649
|
// Pin the bottom of the ink, not of the line boxes: the digits' ink ends
|
|
501
650
|
// on the baseline, so "USD" sits on it too instead of hanging down to
|
|
502
651
|
// where a comma's tail reaches.
|
|
503
|
-
AffixAlign.BOTTOM -> digitBaseline + inkDescent(
|
|
652
|
+
AffixAlign.BOTTOM -> digitBaseline + inkDescent(allGlyphs, GlyphRole.DIGIT) - inkDescent(text, role)
|
|
504
653
|
}
|
|
505
654
|
}
|
|
506
655
|
|
|
@@ -569,11 +718,11 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
569
718
|
return if (t.maxFontSizeMultiplier > 0f) min(multiplier, t.maxFontSizeMultiplier) else multiplier
|
|
570
719
|
}
|
|
571
720
|
|
|
572
|
-
private fun makePaint(t: Typography, sizeDp: Float): TextPaint {
|
|
721
|
+
private fun makePaint(t: Typography, sizeDp: Float, color: Int): TextPaint {
|
|
573
722
|
val paint = TextPaint(Paint.ANTI_ALIAS_FLAG or Paint.SUBPIXEL_TEXT_FLAG)
|
|
574
723
|
paint.typeface = makeTypeface(t)
|
|
575
724
|
paint.textSize = sizeDp * density
|
|
576
|
-
paint.color =
|
|
725
|
+
paint.color = color
|
|
577
726
|
// Tabular figures unless each digit is laid out at its own width.
|
|
578
727
|
paint.fontFeatureSettings = if (t.tabularNums) "tnum" else "pnum"
|
|
579
728
|
return paint
|
|
@@ -629,6 +778,8 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
629
778
|
var grow: Double = 1.0,
|
|
630
779
|
var blurOut: Double = 1.0,
|
|
631
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,
|
|
632
783
|
)
|
|
633
784
|
|
|
634
785
|
private val engine = RollingEngine()
|
|
@@ -665,6 +816,7 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
665
816
|
reportMilestones(reachedBefore)
|
|
666
817
|
if (!engine.isRevealing()) onRevealEnd?.invoke()
|
|
667
818
|
}
|
|
819
|
+
updateMoving()
|
|
668
820
|
}
|
|
669
821
|
|
|
670
822
|
/** Fires [onRevealMilestone] for every milestone reached since [reachedBefore]. */
|
|
@@ -694,6 +846,8 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
694
846
|
private var shimmerGradientWidth = -1f
|
|
695
847
|
private var shimmerGradientBase = 0
|
|
696
848
|
private var shimmerGradientHighlight = 0
|
|
849
|
+
private var shimmerGradientLength = -1f
|
|
850
|
+
private var shimmerGradientSlant = Float.NaN
|
|
697
851
|
private val shimmerMatrix = Matrix()
|
|
698
852
|
|
|
699
853
|
init {
|
|
@@ -736,7 +890,9 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
736
890
|
alignment = Alignment.AUTO
|
|
737
891
|
revealMilestones = DoubleArray(0)
|
|
738
892
|
cellWidths.clear()
|
|
739
|
-
|
|
893
|
+
leavingText.fill(null)
|
|
894
|
+
moving = false
|
|
895
|
+
// onIntrinsicSizeChange, onRevealEnd, onRevealMilestone and the animation events are the hybrid's
|
|
740
896
|
// wiring, not the element's props: they stay across recycling (the hybrid
|
|
741
897
|
// clears its own callback props).
|
|
742
898
|
invalidate()
|
|
@@ -747,19 +903,25 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
747
903
|
* style). Cancels any running roll. Intended to be called every frame.
|
|
748
904
|
*/
|
|
749
905
|
fun setValue(value: Double) {
|
|
906
|
+
val sign = signBefore()
|
|
750
907
|
engine.setValue(value)
|
|
908
|
+
swapSignIfChanged(sign)
|
|
751
909
|
reportIntrinsicSize()
|
|
752
910
|
scheduleFrameIfNeeded()
|
|
753
911
|
invalidate()
|
|
912
|
+
updateMoving()
|
|
754
913
|
}
|
|
755
914
|
|
|
756
915
|
/** Rolls every wheel to [value] (snaps on first show, duration 0 or "remove animations"). */
|
|
757
916
|
fun animateTo(value: Double) {
|
|
758
|
-
|
|
917
|
+
applyReduceMotion()
|
|
918
|
+
val sign = signBefore()
|
|
759
919
|
engine.animateTo(value, now())
|
|
920
|
+
swapSignIfChanged(sign)
|
|
760
921
|
reportIntrinsicSize()
|
|
761
922
|
scheduleFrameIfNeeded()
|
|
762
923
|
invalidate()
|
|
924
|
+
updateMoving()
|
|
763
925
|
}
|
|
764
926
|
|
|
765
927
|
/**
|
|
@@ -767,7 +929,9 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
767
929
|
* target's layout), waiting for [reveal].
|
|
768
930
|
*/
|
|
769
931
|
fun holdReveal(value: Double) {
|
|
932
|
+
val sign = signBefore()
|
|
770
933
|
engine.holdReveal(value)
|
|
934
|
+
swapSignIfChanged(sign)
|
|
771
935
|
reportIntrinsicSize()
|
|
772
936
|
scheduleFrameIfNeeded()
|
|
773
937
|
invalidate()
|
|
@@ -775,16 +939,53 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
775
939
|
|
|
776
940
|
/** Counts up from 0 to [value] and lands with a pop (snaps when animations are off). */
|
|
777
941
|
fun reveal(value: Double) {
|
|
778
|
-
|
|
942
|
+
applyReduceMotion()
|
|
943
|
+
val sign = signBefore()
|
|
779
944
|
engine.reveal(value, now())
|
|
945
|
+
swapSignIfChanged(sign)
|
|
780
946
|
reportIntrinsicSize()
|
|
781
947
|
scheduleFrameIfNeeded()
|
|
782
948
|
invalidate()
|
|
949
|
+
updateMoving()
|
|
783
950
|
reportMilestones(0)
|
|
784
951
|
// Snapped (animations off / duration 0): the reveal is over before it began.
|
|
785
952
|
if (!engine.isRevealing()) onRevealEnd?.invoke()
|
|
786
953
|
}
|
|
787
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
|
+
|
|
788
989
|
/** Re-sends the last reported intrinsic size (e.g. after a listener was attached). */
|
|
789
990
|
fun resendIntrinsicSize() {
|
|
790
991
|
if (lastReportedWidth < 0f) return
|
|
@@ -869,7 +1070,7 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
869
1070
|
private var displayFractionDigits = 0
|
|
870
1071
|
private var decimalFactor = 1.0
|
|
871
1072
|
|
|
872
|
-
// Each text slot's swap (prefix, suffix, grouping, decimal): the text
|
|
1073
|
+
// Each text slot's swap (prefix, suffix, grouping, decimal, sign): the text
|
|
873
1074
|
// leaving, and the engine's clocks for it.
|
|
874
1075
|
private val leavingText = arrayOfNulls<String>(TEXT_SLOTS)
|
|
875
1076
|
private val textGrow = DoubleArray(TEXT_SLOTS) { 1.0 }
|
|
@@ -904,6 +1105,7 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
904
1105
|
w.grow = f[base + 11]
|
|
905
1106
|
w.blurOut = f[base + 12]
|
|
906
1107
|
w.progress = f[base + 13]
|
|
1108
|
+
w.modulus = f[base + 14].toInt()
|
|
907
1109
|
}
|
|
908
1110
|
val text = 4 + count * WHEEL_FIELDS
|
|
909
1111
|
for (slot in 0 until TEXT_SLOTS) {
|
|
@@ -939,8 +1141,10 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
939
1141
|
private fun warmSwapMasks() {
|
|
940
1142
|
if (timing.transition != Transition.NUMERIC) return
|
|
941
1143
|
val set = fonts
|
|
1144
|
+
val fraction = format.fractionDigits > 0
|
|
942
1145
|
WarmExecutor.execute {
|
|
943
|
-
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)
|
|
944
1148
|
}
|
|
945
1149
|
}
|
|
946
1150
|
|
|
@@ -1054,13 +1258,14 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1054
1258
|
// the number is "USD " before it.
|
|
1055
1259
|
val rtl = isRtl
|
|
1056
1260
|
if (affixBlocksDirty || affixRtl != rtl) updateAffixBlocks(rtl)
|
|
1261
|
+
val signGlyph = if (engine.signPositive()) format.plusSign else format.minusSign
|
|
1057
1262
|
// A swapping affix keeps to the digits' side; a separator is centred.
|
|
1058
1263
|
if (rtl) {
|
|
1059
1264
|
addGlyph(suffixInk, GlyphRole.SUFFIX, 1.0, TEXT_SUFFIX, anchor = 1)
|
|
1060
1265
|
addGlyph(suffixGap, GlyphRole.SUFFIX, 1.0)
|
|
1061
1266
|
} else {
|
|
1062
1267
|
// Sign first, then the currency prefix: "-$1,234.50".
|
|
1063
|
-
addGlyph(
|
|
1268
|
+
addGlyph(signGlyph, GlyphRole.DIGIT, signFactor, TEXT_SIGN, anchor = 1)
|
|
1064
1269
|
addGlyph(prefixInk, GlyphRole.PREFIX, 1.0, TEXT_PREFIX, anchor = 1)
|
|
1065
1270
|
}
|
|
1066
1271
|
// Settled, and throughout a reveal (whose layout is the target's from the
|
|
@@ -1070,38 +1275,53 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1070
1275
|
val wheel = wheels[power]
|
|
1071
1276
|
if (wheel.width > 0.0) {
|
|
1072
1277
|
val e = out.next()
|
|
1073
|
-
|
|
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)
|
|
1074
1281
|
e.wheelIndex = power
|
|
1075
1282
|
e.text = null
|
|
1076
|
-
e.role =
|
|
1283
|
+
e.role = role
|
|
1077
1284
|
e.width = (advance * wheel.width).toFloat()
|
|
1078
1285
|
e.fullWidth = advance
|
|
1079
1286
|
e.factor = wheel.width
|
|
1080
1287
|
e.slot = -1
|
|
1081
1288
|
e.fromText = null
|
|
1082
1289
|
}
|
|
1083
|
-
if (power > fd && (power - fd
|
|
1084
|
-
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)
|
|
1085
1292
|
}
|
|
1086
1293
|
if (rtl) {
|
|
1087
1294
|
addGlyph(prefixGap, GlyphRole.PREFIX, 1.0)
|
|
1088
1295
|
addGlyph(prefixInk, GlyphRole.PREFIX, 1.0, TEXT_PREFIX, anchor = -1)
|
|
1089
|
-
addGlyph(
|
|
1296
|
+
addGlyph(signGlyph, GlyphRole.DIGIT, signFactor, TEXT_SIGN, anchor = -1)
|
|
1090
1297
|
} else {
|
|
1091
1298
|
addGlyph(suffixInk, GlyphRole.SUFFIX, 1.0, TEXT_SUFFIX, anchor = -1)
|
|
1092
1299
|
}
|
|
1093
1300
|
applySpacing(fonts, out)
|
|
1094
1301
|
}
|
|
1095
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
|
+
|
|
1096
1316
|
/**
|
|
1097
1317
|
* A wheel's cell width at full size: the widest digit's with tabular
|
|
1098
1318
|
* figures; otherwise its digit's own advance, blended between the two digits
|
|
1099
1319
|
* it is passing through (a roll between them, or a swap), or the target
|
|
1100
1320
|
* digit's when [target] is set.
|
|
1101
1321
|
*/
|
|
1102
|
-
private fun digitAdvance(wheel: Wheel, power: Int, fonts: FontSet, target: Boolean): Float {
|
|
1103
|
-
if (!fonts.proportional) return fonts.digitWidth
|
|
1104
|
-
val widths = fonts.digitWidths
|
|
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)
|
|
1105
1325
|
val digit = engine.targetDigit(power).coerceIn(0, 9)
|
|
1106
1326
|
val goal = widths[digit]
|
|
1107
1327
|
if (target) return goal
|
|
@@ -1114,6 +1334,15 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1114
1334
|
cellWidths[power] = cell
|
|
1115
1335
|
cell.progress = 1.0
|
|
1116
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
|
|
1117
1346
|
}
|
|
1118
1347
|
if (cell.digit != digit || wheel.progress < cell.progress) {
|
|
1119
1348
|
// Interrupted mid-change, it carries on from what it showed; from rest,
|
|
@@ -1131,14 +1360,14 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1131
1360
|
val glyph = if (wheel.blend < 1.0) {
|
|
1132
1361
|
wheel.fromGlyph.toInt()
|
|
1133
1362
|
} else {
|
|
1134
|
-
val position = if (wheel.linear) wheel.position else
|
|
1135
|
-
Math.round(position).toInt()
|
|
1363
|
+
val position = if (wheel.linear) wheel.position else wrap(wheel.position, wheel.modulus)
|
|
1364
|
+
Math.round(position).toInt() % wheel.modulus
|
|
1136
1365
|
}
|
|
1137
1366
|
return if (glyph >= 0) widths[glyph % 10] else goal
|
|
1138
1367
|
}
|
|
1139
1368
|
|
|
1140
1369
|
/** A proportional column's width through a change, by place value. */
|
|
1141
|
-
private class CellWidth(var from: Float = 0f, var shown: Float = 0f, var digit: Int = -1, var progress: Double = 1.0)
|
|
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)
|
|
1142
1371
|
|
|
1143
1372
|
private val cellWidths = HashMap<Int, CellWidth>()
|
|
1144
1373
|
|
|
@@ -1149,18 +1378,18 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1149
1378
|
private fun applySpacing(fonts: FontSet, out: ElementList) {
|
|
1150
1379
|
for (i in 0 until out.size) out[i].gap = 0f
|
|
1151
1380
|
if (fonts.digitSpacing == 0f && fonts.prefixSeam == null && fonts.suffixSeam == null) return
|
|
1152
|
-
|
|
1153
|
-
for (i in 0 until out.size) out[i].gap = fonts.spacing(
|
|
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()
|
|
1154
1383
|
// The seam is the gap at the affix's edge that faces the digits (the far side in RTL).
|
|
1155
1384
|
var firstPrefix = -1
|
|
1156
1385
|
var lastPrefix = -1
|
|
1157
1386
|
var firstSuffix = -1
|
|
1158
1387
|
var lastSuffix = -1
|
|
1159
1388
|
for (i in 0 until out.size) {
|
|
1160
|
-
when (
|
|
1389
|
+
when (out[i].role) {
|
|
1161
1390
|
GlyphRole.PREFIX -> { if (firstPrefix < 0) firstPrefix = i; lastPrefix = i }
|
|
1162
1391
|
GlyphRole.SUFFIX -> { if (firstSuffix < 0) firstSuffix = i; lastSuffix = i }
|
|
1163
|
-
GlyphRole.DIGIT -> Unit
|
|
1392
|
+
GlyphRole.DIGIT, GlyphRole.FRACTION -> Unit
|
|
1164
1393
|
}
|
|
1165
1394
|
}
|
|
1166
1395
|
val rtl = isRtl
|
|
@@ -1279,11 +1508,12 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1279
1508
|
private fun accessibleText(): String {
|
|
1280
1509
|
val fd = format.fractionDigits
|
|
1281
1510
|
val sb = StringBuilder()
|
|
1282
|
-
|
|
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)
|
|
1283
1513
|
sb.append(format.prefix)
|
|
1284
1514
|
for (power in engine.settledPowerCount() - 1 downTo 0) {
|
|
1285
1515
|
sb.append(engine.targetDigit(power))
|
|
1286
|
-
if (power > fd && (power - fd
|
|
1516
|
+
if (power > fd && isGroupBoundary(power - fd, format.groupingSizes)) sb.append(format.groupingSeparator)
|
|
1287
1517
|
if (fd > 0 && power == fd) sb.append(format.decimalSeparator)
|
|
1288
1518
|
}
|
|
1289
1519
|
sb.append(format.suffix)
|
|
@@ -1378,7 +1608,7 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1378
1608
|
val element = elements[i]
|
|
1379
1609
|
val text = element.text
|
|
1380
1610
|
if (element.wheelIndex >= 0) {
|
|
1381
|
-
drawWheel(canvas, fonts, wheels[element.wheelIndex], x, element.width, element.fullWidth, originX, originY, scale)
|
|
1611
|
+
drawWheel(canvas, fonts, wheels[element.wheelIndex], element.role, x, element.width, element.fullWidth, originX, originY, scale)
|
|
1382
1612
|
} else if (element.fromText != null) {
|
|
1383
1613
|
drawTextSwap(canvas, fonts, element, x)
|
|
1384
1614
|
} else if (text != null) {
|
|
@@ -1400,22 +1630,33 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1400
1630
|
*/
|
|
1401
1631
|
private fun drawShimmer(canvas: Canvas, fonts: FontSet, contentWidth: Float, dim: Float) {
|
|
1402
1632
|
if (contentWidth <= 0f) return
|
|
1403
|
-
val base = fonts.digit.color
|
|
1633
|
+
val base = shimmer.baseColor ?: fonts.digit.color
|
|
1404
1634
|
val highlight = shimmer.color ?: defaultShimmerColor()
|
|
1405
|
-
|
|
1406
|
-
val
|
|
1407
|
-
|
|
1408
|
-
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)
|
|
1409
1646
|
var gradient = shimmerGradient
|
|
1410
|
-
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
|
+
) {
|
|
1411
1650
|
gradient = LinearGradient(
|
|
1412
|
-
0f, 0f,
|
|
1651
|
+
0f, 0f, length, slant * length,
|
|
1413
1652
|
intArrayOf(base, highlight, base), floatArrayOf(0.1f, 0.5f, 0.9f), Shader.TileMode.CLAMP,
|
|
1414
1653
|
)
|
|
1415
1654
|
shimmerGradient = gradient
|
|
1416
1655
|
shimmerGradientWidth = contentWidth
|
|
1417
1656
|
shimmerGradientBase = base
|
|
1418
1657
|
shimmerGradientHighlight = highlight
|
|
1658
|
+
shimmerGradientLength = length
|
|
1659
|
+
shimmerGradientSlant = slant
|
|
1419
1660
|
shimmerPaint.shader = gradient
|
|
1420
1661
|
}
|
|
1421
1662
|
shimmerMatrix.setTranslate(startX, 0f)
|
|
@@ -1466,14 +1707,16 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1466
1707
|
* [originX], [originY] and [scale] are the content transform already on the
|
|
1467
1708
|
* canvas, so the strip window can be landed on whole device pixels.
|
|
1468
1709
|
*/
|
|
1469
|
-
private fun drawWheel(canvas: Canvas, fonts: FontSet, wheel: Wheel, x: Float, width: Float, advance: 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) {
|
|
1470
1711
|
if (width <= 0f) return
|
|
1471
1712
|
val lineHeight = fonts.lineHeight
|
|
1713
|
+
val column = fonts.digitWidth(role)
|
|
1714
|
+
val modulus = wheel.modulus
|
|
1472
1715
|
// The digit column, the widest digit wide, centred on the digit's own cell
|
|
1473
1716
|
// (with tabular figures the two are the same, right-aligned in the cell).
|
|
1474
|
-
val columnLeft = x + width - (advance +
|
|
1717
|
+
val columnLeft = x + width - (advance + column) / 2f
|
|
1475
1718
|
if (wheel.blend < 1.0 || wheel.focus < 1.0 || wheel.grow < 1.0) {
|
|
1476
|
-
drawSwap(canvas, fonts, wheel, columnLeft, originX, originY, scale)
|
|
1719
|
+
drawSwap(canvas, fonts, wheel, role, columnLeft, originX, originY, scale)
|
|
1477
1720
|
return
|
|
1478
1721
|
}
|
|
1479
1722
|
// A settled-width wheel is a window onto the shared strip (the strip has
|
|
@@ -1481,12 +1724,13 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1481
1724
|
// canvas without a GPU, which cannot draw a hardware bitmap, draws its
|
|
1482
1725
|
// two glyphs the old way).
|
|
1483
1726
|
if (wheel.width >= 1.0 && canvas.isHardwareAccelerated) {
|
|
1484
|
-
val strip = fonts.strip(wheel.blankZero)
|
|
1727
|
+
val strip = fonts.strip(role, wheel.blankZero, modulus)
|
|
1485
1728
|
if (strip != null) {
|
|
1486
|
-
// An interior wheel wraps modulo 10,
|
|
1487
|
-
// through 0 shows 9); a linear wheel
|
|
1488
|
-
// blank slot. Either way the window
|
|
1489
|
-
|
|
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)
|
|
1490
1734
|
var tx = columnLeft
|
|
1491
1735
|
var ty = (-(position + 1) * lineHeight).toFloat()
|
|
1492
1736
|
if (scale == 1f && position == floor(position)) {
|
|
@@ -1521,7 +1765,7 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1521
1765
|
return
|
|
1522
1766
|
}
|
|
1523
1767
|
canvas.save()
|
|
1524
|
-
canvas.clipRect(columnLeft, 0f, columnLeft +
|
|
1768
|
+
canvas.clipRect(columnLeft, 0f, columnLeft + column, lineHeight)
|
|
1525
1769
|
canvas.translate(tx, ty)
|
|
1526
1770
|
if (strip.node != null) canvas.drawRenderNode(strip.node) else canvas.drawBitmap(strip.bitmap, 0f, 0f, stripPaint)
|
|
1527
1771
|
// The change flash rides the roll: the strip once more, in the tint, at the same offset.
|
|
@@ -1537,33 +1781,34 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1537
1781
|
return
|
|
1538
1782
|
}
|
|
1539
1783
|
}
|
|
1540
|
-
val baseline = fonts.
|
|
1784
|
+
val baseline = fonts.digitBaseline(role)
|
|
1541
1785
|
canvas.save()
|
|
1542
1786
|
// The roll's window, the whole digit wide: a column still opening or
|
|
1543
1787
|
// closing is not cut to its width (the glyph read as a sliver of its right
|
|
1544
1788
|
// edge, a ")" of a 0 rolling past), it overhangs the cell's far side, faded.
|
|
1545
|
-
canvas.clipRect(columnLeft, 0f, columnLeft +
|
|
1546
|
-
val
|
|
1547
|
-
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()
|
|
1548
1793
|
val index = base.toInt()
|
|
1549
1794
|
fun glyphs(paint: TextPaint, alpha: Float) {
|
|
1550
1795
|
// Scaled from the ink colour's alpha and put back (see [drawGlyph]).
|
|
1551
1796
|
val ink = paint.alpha
|
|
1552
1797
|
paint.alpha = (alpha * ink).toInt().coerceIn(0, 255)
|
|
1553
|
-
glyphAt(index, wheel)?.let { glyph ->
|
|
1554
|
-
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)
|
|
1555
1800
|
}
|
|
1556
1801
|
if (fraction > 0.0001f) {
|
|
1557
|
-
glyphAt(index + 1, wheel)?.let { glyph ->
|
|
1558
|
-
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)
|
|
1559
1804
|
}
|
|
1560
1805
|
}
|
|
1561
1806
|
paint.alpha = ink
|
|
1562
1807
|
}
|
|
1563
1808
|
val opening = openingAlpha(wheel.width).toFloat()
|
|
1564
|
-
glyphs(fonts.
|
|
1809
|
+
glyphs(fonts.paint(role), opening)
|
|
1565
1810
|
// The change flash rides the roll: the same glyphs again, in the tint.
|
|
1566
|
-
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) }
|
|
1567
1812
|
canvas.restore()
|
|
1568
1813
|
}
|
|
1569
1814
|
|
|
@@ -1576,9 +1821,9 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1576
1821
|
* around the digit. With a change flash on, the pair is drawn again in
|
|
1577
1822
|
* the tint at the flash's opacity: the ink mixed towards the tint.
|
|
1578
1823
|
*/
|
|
1579
|
-
private fun drawSwap(canvas: Canvas, fonts: FontSet, wheel: Wheel, columnLeft: Float, originX: Float, originY: Float, scale: Float) {
|
|
1580
|
-
drawSwapPair(canvas, fonts, wheel, columnLeft, fonts.
|
|
1581
|
-
flashColor(wheel)?.let { color -> drawSwapPair(canvas, fonts, wheel, columnLeft, fonts.tinted(color), wheel.flash.toFloat(), originX, originY, scale) }
|
|
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) }
|
|
1582
1827
|
}
|
|
1583
1828
|
|
|
1584
1829
|
/**
|
|
@@ -1589,7 +1834,7 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1589
1834
|
* blurred copy hides and a lightly blurred one does not. Landed, the last
|
|
1590
1835
|
* swap frame is pixel for pixel the strip that takes over from it.
|
|
1591
1836
|
*/
|
|
1592
|
-
private fun drawSwapPair(canvas: Canvas, fonts: FontSet, wheel: Wheel, columnLeft: Float, paint: TextPaint, opacity: Float, originX: Float, originY: Float, scale: Float) {
|
|
1837
|
+
private fun drawSwapPair(canvas: Canvas, fonts: FontSet, wheel: Wheel, role: GlyphRole, columnLeft: Float, paint: TextPaint, opacity: Float, originX: Float, originY: Float, scale: Float) {
|
|
1593
1838
|
val lineHeight = fonts.lineHeight
|
|
1594
1839
|
// The position clock overshoots 1 (a spring); only the offsets follow it there.
|
|
1595
1840
|
val b = wheel.blend.toFloat()
|
|
@@ -1600,21 +1845,23 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1600
1845
|
val f = wheel.focus.toFloat().coerceIn(0f, 1f)
|
|
1601
1846
|
val d = if (wheel.fromAbove) 1f else -1f
|
|
1602
1847
|
val offset = lineHeight * NUMERIC_OFFSET
|
|
1603
|
-
val
|
|
1604
|
-
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)
|
|
1605
1852
|
val column = wheel.width.toFloat().coerceIn(0f, 1f) * opacity
|
|
1606
1853
|
val from = wheel.fromGlyph.toInt()
|
|
1607
1854
|
val to = wheel.toGlyph.toInt()
|
|
1608
|
-
// The bitmap's top-left sits at (centre -
|
|
1609
|
-
val cornerX =
|
|
1610
|
-
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
|
|
1611
1858
|
fun snapX(c: Float): Float = (Math.round(originX + (c - cornerX) * scale) - originX) / scale + cornerX
|
|
1612
1859
|
fun snapY(c: Float): Float = (Math.round(originY + (c - cornerY) * scale) - originY) / scale + cornerY
|
|
1613
1860
|
if (from >= 0) {
|
|
1614
|
-
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))
|
|
1615
1862
|
}
|
|
1616
1863
|
if (to >= 0) {
|
|
1617
|
-
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)
|
|
1618
1865
|
}
|
|
1619
1866
|
}
|
|
1620
1867
|
|
|
@@ -1625,10 +1872,11 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1625
1872
|
* rather than a sharp glyph half-hidden in a fully blurred one, which read
|
|
1626
1873
|
* as a digit inside a glow.
|
|
1627
1874
|
*/
|
|
1628
|
-
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) {
|
|
1629
1876
|
if (alpha <= 0.002f) return
|
|
1630
|
-
val text =
|
|
1631
|
-
|
|
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)
|
|
1632
1880
|
val x = blur.coerceIn(0f, 1f) * NUMERIC_BLUR_LEVELS
|
|
1633
1881
|
val lo = min(NUMERIC_BLUR_LEVELS, floor(x).toInt())
|
|
1634
1882
|
val hi = min(NUMERIC_BLUR_LEVELS, lo + 1)
|
|
@@ -1645,18 +1893,18 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1645
1893
|
canvas.scale(scale, scale)
|
|
1646
1894
|
for ((level, levelAlpha) in arrayOf(lo to loAlpha, hi to hiAlpha)) {
|
|
1647
1895
|
if (levelAlpha <= 0.002f) continue
|
|
1648
|
-
val mask = fonts.blurred(digitIndex, level)
|
|
1896
|
+
val mask = fonts.blurred(role, digitIndex, level)
|
|
1649
1897
|
if (mask == null) {
|
|
1650
1898
|
// No font metrics yet: the text itself.
|
|
1651
1899
|
val ink = paint.alpha
|
|
1652
1900
|
paint.alpha = (levelAlpha * ink).toInt().coerceIn(0, 255)
|
|
1653
|
-
canvas.drawText(text, -fonts.width(text,
|
|
1901
|
+
canvas.drawText(text, -fonts.width(text, role) / 2f, fonts.digitBaseline(role) - centerY, paint)
|
|
1654
1902
|
paint.alpha = ink
|
|
1655
1903
|
} else {
|
|
1656
1904
|
// An alpha mask draws in the paint's colour; the ink's own alpha scales with the level's.
|
|
1657
1905
|
blurPaint.color = paint.color
|
|
1658
1906
|
blurPaint.alpha = (levelAlpha * (paint.color ushr 24)).toInt().coerceIn(0, 255)
|
|
1659
|
-
canvas.drawBitmap(mask, -fonts.digitWidth / 2f - fonts.blurPad, -
|
|
1907
|
+
canvas.drawBitmap(mask, -fonts.digitWidth(role) / 2f - fonts.blurPad, -centerY - fonts.blurPad, blurPaint)
|
|
1660
1908
|
}
|
|
1661
1909
|
}
|
|
1662
1910
|
canvas.restore()
|
|
@@ -1717,11 +1965,11 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1717
1965
|
return w * w
|
|
1718
1966
|
}
|
|
1719
1967
|
|
|
1720
|
-
private fun glyphAt(index: Int, wheel: Wheel): String? {
|
|
1968
|
+
private fun glyphAt(index: Int, wheel: Wheel, fonts: FontSet): String? {
|
|
1721
1969
|
if (wheel.linear && index < 0) return null
|
|
1722
1970
|
if (wheel.blankZero && index == 0) return null
|
|
1723
|
-
val
|
|
1724
|
-
return
|
|
1971
|
+
val m = wheel.modulus
|
|
1972
|
+
return fonts.glyphs[((index % m) + m) % m]
|
|
1725
1973
|
}
|
|
1726
1974
|
|
|
1727
1975
|
// endregion
|
|
@@ -1737,6 +1985,7 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1737
1985
|
fun release() {
|
|
1738
1986
|
stopAnimation()
|
|
1739
1987
|
engine.reset()
|
|
1988
|
+
moving = false
|
|
1740
1989
|
wheels.clear()
|
|
1741
1990
|
frameElements.clear()
|
|
1742
1991
|
settledElements.clear()
|
|
@@ -1754,10 +2003,11 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1754
2003
|
*/
|
|
1755
2004
|
fun memoryEstimateBytes(): Long = 64L * 1024
|
|
1756
2005
|
|
|
1757
|
-
/** Interior wheels wrap modulo 10; a roll can be any real, so fold it onto 0 ≤ p <
|
|
1758
|
-
private fun
|
|
1759
|
-
val
|
|
1760
|
-
|
|
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
|
|
1761
2011
|
}
|
|
1762
2012
|
|
|
1763
2013
|
/** A digit strip: the software-rendered bitmap and, from Android 10, the layer that holds it. */
|
|
@@ -1813,14 +2063,16 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1813
2063
|
}
|
|
1814
2064
|
|
|
1815
2065
|
companion object {
|
|
1816
|
-
/**
|
|
1817
|
-
private const val STRIP_SLOTS = 12
|
|
2066
|
+
/** The glyphs drawn for 0…9 unless `digitGlyphs` gives ten others. */
|
|
1818
2067
|
private val DIGITS = Array(10) { it.toString() }
|
|
1819
|
-
|
|
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)
|
|
1820
2074
|
/** The core starts at the glyphs' left edge instead of parked off-screen. */
|
|
1821
2075
|
private const val SHIMMER_SEED = 0.25f
|
|
1822
|
-
/** How far the top of the band leads the bottom, as a fraction of the height ("/" slant). */
|
|
1823
|
-
private const val SHIMMER_SLANT = 0.6f
|
|
1824
2076
|
}
|
|
1825
2077
|
}
|
|
1826
2078
|
|