-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathTimeline.hx
93 lines (79 loc) · 2.15 KB
/
Timeline.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package tweenxcore.structure;
using tweenxcore.Tools;
class Timeline<T>
{
public var totalWeight(default, null):Float;
var dataArray:Array<T>;
var weightArray:Array<Float>;
public var length(get, null):Int;
function get_length():Int
{
return dataArray.length;
}
public inline function new()
{
this.dataArray = [];
this.weightArray = [];
totalWeight = 0;
}
public inline function add(data:T, weight:Float = 1.0):Timeline<T>
{
if (weight <= 0) {
throw "weight must be positive number";
}
if (dataArray.length == 0) {
totalWeight = weight;
} else {
weightArray.push(totalWeight);
totalWeight += weight;
}
dataArray.push(data);
return this;
}
public inline function search(rate:Float, boundaryMode:BoundaryMode = BoundaryMode.High):TimelineSearchResult<T>
{
if (dataArray.length == 0) {
throw "timeline is not initialized";
}
var searchResult = weightArray.binarySearch(rate * totalWeight, boundaryMode);
var baseWeight = if (searchResult == 0) {
0;
} else {
weightArray[searchResult - 1] / totalWeight;
}
var nextWeight = if (searchResult == dataArray.length - 1) {
1;
} else {
weightArray[searchResult] / totalWeight;
}
return new TimelineSearchResult(
dataArray[searchResult],
searchResult,
baseWeight,
nextWeight
);
}
public inline function dataAt(index:Int):T
{
if (dataArray.length == 0) {
throw "timeline is not initialized";
}
return dataArray[index];
}
public inline function rangeLeft(index:Int):Float
{
if (index == 0)
{
return 0.0;
}
return weightArray[index - 1] / totalWeight;
}
public inline function rangeRight(index:Int):Float
{
if (index == dataArray.length)
{
return 1.0;
}
return weightArray[index] / totalWeight;
}
}