-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathFloatChange.hx
More file actions
181 lines (161 loc) · 6.32 KB
/
FloatChange.hx
File metadata and controls
181 lines (161 loc) · 6.32 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package tweenxcore.structure;
import tweenxcore.structure.FloatChangeRepeatPart;
import tweenxcore.structure.FloatChangeRepeatPart;
using tweenxcore.Tools;
class FloatChange {
// ===============
// Properties
// ===============
public var previous(default, null):Float;
public var current(default, null):Float;
public inline function new(previousValue:Float, currentValue:Float) {
this.previous = previousValue;
this.current = currentValue;
}
// ===============
// Methods
// ===============
public inline function direction():Direction
{
return if (previous < current) {
Direction.Forward;
} else if (current < previous) {
Direction.Backward;
} else {
Direction.Stopped;
}
}
public function mapFloatChange(func:Float->Float):FloatChange
{
return new FloatChange(func(previous), func(current));
}
public function isCrossOver(threshold:Float, boundaryMode:BoundaryMode = BoundaryMode.High):Bool
{
return switch (boundaryMode) {
case BoundaryMode.Low:
(previous < threshold && threshold <= current) || (current < threshold && threshold <= previous);
case BoundaryMode.High:
(previous <= threshold && threshold < current) || (current <= threshold && threshold < previous);
}
}
public inline function handlePart(from:Float, to:Float, updatePart:FloatChangePart->Void):Void
{
if (
(
(from < previous && current < to) ||
(from < current && previous < to) ||
(to < previous && current < from) ||
(to < current && previous < from)
) &&
(previous != current)
) {
updatePart(
new FloatChangePart(
previous.inverseLerp(from, to).clamp(),
current.inverseLerp(from, to).clamp()
)
);
}
}
public function handleRepeatPart(
firstPartFrom:Float,
firstPartTo:Float,
repeatLimit:Int,
updateRepeatPart:FloatChangeRepeatPart->Void):Void
{
if (firstPartFrom != firstPartTo) {
var p = previous.inverseLerp(firstPartFrom, firstPartTo);
var c = current.inverseLerp(firstPartFrom, firstPartTo);
if ((0 < c && p < repeatLimit) || (0 < p && c < repeatLimit)) {
inline function update(previousValue:Float, currentValue:Float, index:Int, miner:Bool) {
if (previousValue != currentValue) {
updateRepeatPart(new FloatChangeRepeatPart(previousValue, currentValue, index, repeatLimit, miner));
}
}
p = p.clamp(0, repeatLimit);
c = c.clamp(0, repeatLimit);
var pCount = Std.int(p);
var cCount = Std.int(c);
var hasNext = true;
if (p < c) {
do {
if (cCount == pCount) {
update(p - pCount, c - pCount, pCount, hasNext = false);
} else {
hasNext = (pCount + 1 != c);
update(p - pCount, 1, pCount, hasNext);
}
p = (pCount += 1);
} while (hasNext);
} else {
do {
if (pCount == cCount) {
update(p - pCount, c - pCount, pCount, hasNext = false);
} else {
hasNext = (pCount - 1 != c);
update(p - pCount, 0, pCount, hasNext);
}
p = pCount;
pCount -= 1;
} while (hasNext);
}
}
}
}
public inline function handleTimelinePart(
timelineFrom:Float,
timelineTo:Float,
updatePartTimeline:Timeline<FloatChangeTimelinePart->Void>):Void
{
if (timelineFrom != timelineTo) {
var p = previous.inverseLerp(timelineFrom, timelineTo);
var c = current.inverseLerp(timelineFrom, timelineTo);
if ((0 < p && c < 1) || (0 < c && p < 1)) {
var length = updatePartTimeline.length;
inline function update(previousValue:Float, currentValue:Float, index:Int, isMiner:Bool) {
var part = new FloatChangeTimelinePart(
previousValue,
currentValue,
index,
updatePartTimeline.rangeLeft(index),
updatePartTimeline.rangeRight(index),
isMiner
);
updatePartTimeline.dataAt(index)(part);
}
p = p.clamp(0, 1);
c = c.clamp(0, 1);
var pResult = updatePartTimeline.search(p);
var cResult = updatePartTimeline.search(c);
var pCount = pResult.index;
var cCount = cResult.index;
var pRate = pResult.innerRate(p);
var cRate = cResult.innerRate(c);
var hasNext = false;
if (p < c) {
do {
if (pCount == cCount) {
update(pRate, cRate, pCount, hasNext = false);
} else {
hasNext = (pCount + 1 != cCount) || (cRate != 0);
update(pRate, 1, pCount, hasNext);
}
pRate = 0;
pCount += 1;
} while (hasNext);
} else {
do {
if (pCount == cCount) {
update(pRate, cRate, pCount, hasNext = false);
} else {
hasNext = (pCount - 1 != cCount) || (cRate != 1);
update(pRate, 0, pCount, hasNext);
}
pRate = 1;
pCount -= 1;
} while (hasNext);
}
}
}
}
}