scoop:scoop.ui.component:0.1.0
src/carousel/geometry.scoop
Selected production source · src/carousel/geometry.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.math.*5
import scoop.ui.*6
import scoop.ui.style.*8
internal class CarouselTrack(9
val extents: Array<Double>,10
val starts: Array<Double>,11
val snaps: Array<Double>,12
val viewport: Double,13
val maximum: Double,14
val cycle: Double,15
val looping: Boolean16
)18
internal fun carouselTrack(19
extents: Array<Double>,20
viewport: Double,21
spacing: Double,22
loop: Boolean23
): CarouselTrack {24
nonnegative(viewport, "invalid carousel viewport extent")25
nonnegative(spacing, "invalid carousel spacing")26
var total = 0.027
val positions = ArrayList<Double>()28
for (extent in extents) {29
nonnegative(extent, "invalid measured carousel item")30
positions.add(total)31
total += extent + spacing32
nonnegative(total, "carousel track extent overflow")33
}34
val starts = positions.toArray()35
val content =36
max(37
0.0,38
total -39
if (extents.size > 0L)40
spacing41
else42
0.043
)44
var looping = loop && extents.size > 1L && viewport > 0.0 && content > viewport45
for (extent in extents)46
if (total - extent < viewport)47
looping = false48
val maximum = max(0.0, content - viewport)49
val circular = looping50
val snaps =51
Array<Double>(starts.size) {52
index -> if (circular)53
starts[index]54
else55
min(starts[index], maximum)56
}57
return CarouselTrack(extents, starts, snaps, viewport, maximum, total, looping)58
}60
internal fun carouselModulo(value: Double, cycle: Double): Double {61
val result = value % cycle62
return if (result < 0.0)63
result + cycle64
else65
result66
}68
internal fun carouselItemPosition(track: CarouselTrack, index: Long, offset: Double): Double {69
val position = track.starts[index] - offset70
if (!track.looping)71
return position72
val wrapped = carouselModulo(position, track.cycle)73
return if (wrapped + track.extents[index] > track.cycle)74
wrapped - track.cycle75
else76
wrapped77
}79
internal fun carouselAdjacent(track: CarouselTrack, index: Long, next: Boolean): Long? {80
if (index < 0L || index >= track.snaps.size || track.maximum <= 0.0)81
return None82
val current = track.snaps[index]83
var candidate = index84
for (_ in 0L ..< track.snaps.size - 1L) {85
candidate += if (next)86
1L87
else88
-1L89
if (candidate < 0L || candidate >= track.snaps.size) {90
if (!track.looping)91
return None92
candidate =93
if (next)94
0L95
else96
track.snaps.size - 1L97
}98
if (track.snaps[candidate] != current)99
return Some(candidate)100
}101
return None102
}104
internal fun carouselNearest(105
track: CarouselTrack,106
position: Double,107
velocity: Double,108
projection: Double109
): SnapTarget? {110
if (!track.looping)111
return snapTarget(position, velocity, track.snaps, projection)112
val normalized = carouselModulo(position, track.cycle)113
val count = track.snaps.size114
val anchors =115
Array<Double>(count * 3L) {116
index -> track.snaps[index % count] + (index / count - 1L).toDouble() * track.cycle117
}118
val target = snapTarget(normalized, velocity, anchors, projection) ?: return None119
return Some(SnapTarget(target.index % count, target.position))120
}122
internal fun carouselSelected(123
keys: Array<ViewKey>,124
controlled: Long?,125
retained: ViewKey?,126
fallback: Long127
): Long {128
if (keys.size == 0L)129
return -1L130
when (controlled) {131
case Some(val index) -> return max(0L, min(index, keys.size - 1L))132
case None -> for (index in 0L ..< keys.size)133
if (Some(keys[index]) == retained)134
return index135
}136
return max(0L, min(fallback, keys.size - 1L))137
}139
internal class CarouselGeometry(140
val size: Size,141
val inner: Rect,142
val track: CarouselTrack,143
val keys: Array<ViewKey>,144
val orientation: Orientation,145
val direction: LayoutDirection146
)148
internal fun sameCarouselGeometry(a: CarouselGeometry, b: CarouselGeometry): Boolean {149
if (150
a.size !=151
b.size ||152
a.inner !=153
b.inner ||154
a.orientation !=155
b.orientation ||156
a.direction !=157
b.direction ||158
a.track.looping !=159
b.track.looping ||160
a.track.cycle !=161
b.track.cycle ||162
a.keys.size !=163
b.keys.size164
)165
return false166
for (index in 0L ..< a.keys.size)167
if (a.keys[index] != b.keys[index] || a.track.extents[index] != b.track.extents[index])168
return false169
return true170
}