forked from genail/pulpcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFixed.java
More file actions
430 lines (371 loc) · 14.1 KB
/
Fixed.java
File metadata and controls
430 lines (371 loc) · 14.1 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
Copyright (c) 2009, Interactive Pulp, LLC
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Interactive Pulp, LLC nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
package pulpcore.animation;
import pulpcore.math.CoreMath;
/**
A Fixed is an fixed-point value (16 bits integer, 16 bits fraction)
that can be animated over time. See {@link pulpcore.math.CoreMath} for
methods to convert between integers and fixed-point numbers.
*/
public final class Fixed extends Property {
public Fixed() {
this(null, 0);
}
public Fixed(PropertyListener listener) {
this(listener, 0);
}
//
// Constructors with setters - 2 methods
//
public Fixed(int value) {
this(null, value);
}
public Fixed(double value) {
this(null, value);
}
//
// Constructors with setters and listeners - 2 methods
//
public Fixed(PropertyListener listener, int value) {
super(listener, CoreMath.toFixed(value));
}
public Fixed(PropertyListener listener, double value) {
super(listener, CoreMath.toFixed(value));
}
//
// Getters
//
public int getAsFixed() {
return super.getValue();
}
public int getAsInt() {
return CoreMath.toInt(super.getValue());
}
public int getAsIntFloor() {
return CoreMath.toIntFloor(super.getValue());
}
public int getAsIntCeil() {
return CoreMath.toIntCeil(super.getValue());
}
public int getAsIntRound() {
return CoreMath.toIntRound(super.getValue());
}
public double get() {
return CoreMath.toDouble(super.getValue());
}
public String toString() {
return CoreMath.toString(super.getValue(), 7);
}
/**
Returns true if the specified object is an
{@code Int},
{@link Fixed},
{@link java.lang.Byte},
{@link java.lang.Short},
{@link java.lang.Integer},
{@link java.lang.Long},
{@link java.lang.Float}, or
{@link java.lang.Double}, and
its value is equal to this value.
*/
public boolean equals(Object obj) {
if (obj instanceof Fixed) {
return getAsFixed() == ((Fixed)obj).getAsFixed();
}
else if (obj instanceof Int) {
long objValue = ((Int)obj).get();
return getAsFixed() == (objValue << CoreMath.FRACTION_BITS);
}
else if (obj instanceof Double) {
return get() == ((Double)obj).doubleValue();
}
else if (obj instanceof Float) {
return get() == ((Float)obj).floatValue();
}
else if (
obj instanceof Byte ||
obj instanceof Short ||
obj instanceof Integer ||
obj instanceof Long)
{
long objValue = ((Number)obj).longValue();
return getAsFixed() == (objValue << CoreMath.FRACTION_BITS);
}
else {
return false;
}
}
public int hashCode() {
// Same as java.lang.Float
return Float.floatToIntBits(CoreMath.toFloat(super.getValue()));
}
//
// Setters - 3 methods
//
protected void setValue(Number value) {
setValue(CoreMath.toFixed(value.doubleValue()));
}
/**
Sets the value of this property.
Any previous animations are stopped.
*/
public void setAsFixed(int fValue) {
setValue(fValue);
setBehavior(null);
}
/**
Sets the value of this property.
Any previous animations are stopped.
*/
public void set(int value) {
setAsFixed(CoreMath.toFixed(value));
}
/**
Sets the value of this property.
Any previous animations are stopped.
*/
public void set(double value) {
setAsFixed(CoreMath.toFixed(value));
}
//
// Setters (with a delay) - 3 methods
//
/**
Sets the value of this property after a specific delay.
Any previous animations are stopped.
*/
public void setAsFixed(int fValue, int delay) {
animateToFixed(fValue, 0, null, delay);
}
/**
Sets the value of this property after a specific delay.
Any previous animations are stopped.
*/
public void set(int value, int delay) {
animateTo(value, 0, null, delay);
}
/**
Sets the value of this property after a specific delay.
Any previous animations are stopped.
*/
public void set(double value, int delay) {
animateTo(value, 0, null, delay);
}
/**
Binds this property to the specified property. If this property is given a new behavior,
the binding is broken.
*/
public void bindTo(Int property) {
setBehavior(new Binding(this, property, false));
}
/**
Bi-directionally binds this property to the specified property.
If this property is given a new behavior, the specified property is then bi-directionally
bound to this property. The binding is permanent, until a new bi-directional binding
is specified.
*/
public void bindWithInverse(Int property) {
setBehavior(new Binding(this, property, true));
}
/**
Binds this property to the specified property. If this property is given a new behavior,
the binding is broken.
*/
public void bindTo(Fixed property) {
setBehavior(new Binding(this, property, false));
}
/**
Bi-directionally binds this property to the specified property.
If this property is given a new behavior, the specified property is then bi-directionally
bound to this property.
*/
public void bindWithInverse(Fixed property) {
setBehavior(new Binding(this, property, true));
}
/**
Binds this property to the specified function.
*/
public void bindTo(BindFunction function) {
setBehavior(new Binding(this, function));
}
//
// Animation convenience methods - fixed-point
//
/**
Animates this property from the one fixed-point value (fFromValue) to another (fToValue).
Any previous animations are stopped.
*/
public void animateAsFixed(int fFromValue, int fToValue, int duration) {
setBehavior(new Tween(fFromValue, fToValue, duration));
}
/**
Animates this property from the one fixed-point value (fFromValue) to another (fToValue).
Any previous animations are stopped.
*/
public void animateAsFixed(int fFromValue, int fToValue, int duration, Easing easing) {
setBehavior(new Tween(fFromValue, fToValue, duration, easing));
}
/**
Animates this property from the one fixed-point value (fFromValue) to another (fToValue).
Any previous animations are stopped.
*/
public void animateAsFixed(int fFromValue, int fToValue, int duration, Easing easing,
int startDelay)
{
setBehavior(new Tween(fFromValue, fToValue, duration, easing, startDelay));
}
/**
Animates this property from the current value to the specified fixed-point value.
Any previous animations are stopped.
*/
public void animateToFixed(int fToValue, int duration) {
setBehavior(new Tween(getAsFixed(), fToValue, duration));
}
/**
Animates this property from the current value to the specified fixed-point value.
Any previous animations are stopped.
*/
public void animateToFixed(int fToValue, int duration, Easing easing) {
setBehavior(new Tween(getAsFixed(), fToValue, duration, easing));
}
/**
Animates this property from the current value to the specified fixed-point value.
Any previous animations are stopped.
*/
public void animateToFixed(int fToValue, int duration, Easing easing, int startDelay) {
setBehavior(new Tween(getAsFixed(), fToValue, duration, easing, startDelay));
}
//
// Animation convenience methods - integer
//
/**
Animates this property from the one integer (fromValue) to another (toValue).
Any previous animations are stopped.
*/
public void animate(int fromValue, int toValue, int duration) {
int fFromValue = CoreMath.toFixed(fromValue);
int fToValue = CoreMath.toFixed(toValue);
setBehavior(new Tween(fFromValue, fToValue, duration));
}
/**
Animates this property from the one integer (fromValue) to another (toValue).
Any previous animations are stopped.
*/
public void animate(int fromValue, int toValue, int duration, Easing easing) {
int fFromValue = CoreMath.toFixed(fromValue);
int fToValue = CoreMath.toFixed(toValue);
setBehavior(new Tween(fFromValue, fToValue, duration, easing));
}
/**
Animates this property from the one integer (fromValue) to another (toValue).
Any previous animations are stopped.
*/
public void animate(int fromValue, int toValue, int duration, Easing easing, int startDelay) {
int fFromValue = CoreMath.toFixed(fromValue);
int fToValue = CoreMath.toFixed(toValue);
setBehavior(new Tween(fFromValue, fToValue, duration, easing, startDelay));
}
/**
Animates this property from the current value to the specified integer.
Any previous animations are stopped.
*/
public void animateTo(int toValue, int duration) {
int fToValue = CoreMath.toFixed(toValue);
setBehavior(new Tween(getAsFixed(), fToValue, duration));
}
/**
Animates this property from the current value to the specified integer.
Any previous animations are stopped.
*/
public void animateTo(int toValue, int duration, Easing easing) {
int fToValue = CoreMath.toFixed(toValue);
setBehavior(new Tween(getAsFixed(), fToValue, duration, easing));
}
/**
Animates this property from the current value to the specified integer.
Any previous animations are stopped.
*/
public void animateTo(int toValue, int duration, Easing easing, int startDelay) {
int fToValue = CoreMath.toFixed(toValue);
setBehavior(new Tween(getAsFixed(), fToValue, duration, easing, startDelay));
}
//
// Animation convenience methods - double
//
/**
Animates this property from the one double (fromValue) to another (toValue).
Any previous animations are stopped.
*/
public void animate(double fromValue, double toValue, int duration) {
int fFromValue = CoreMath.toFixed(fromValue);
int fToValue = CoreMath.toFixed(toValue);
setBehavior(new Tween(fFromValue, fToValue, duration));
}
/**
Animates this property from the one double (fromValue) to another (toValue).
Any previous animations are stopped.
*/
public void animate(double fromValue, double toValue, int duration, Easing easing) {
int fFromValue = CoreMath.toFixed(fromValue);
int fToValue = CoreMath.toFixed(toValue);
setBehavior(new Tween(fFromValue, fToValue, duration, easing));
}
/**
Animates this property from the one double (fromValue) to another (toValue).
Any previous animations are stopped.
*/
public void animate(double fromValue, double toValue, int duration, Easing easing,
int startDelay)
{
int fFromValue = CoreMath.toFixed(fromValue);
int fToValue = CoreMath.toFixed(toValue);
setBehavior(new Tween(fFromValue, fToValue, duration, easing, startDelay));
}
/**
Animates this property from the current value to the specified double.
Any previous animations are stopped.
*/
public void animateTo(double toValue, int duration) {
int fToValue = CoreMath.toFixed(toValue);
setBehavior(new Tween(getAsFixed(), fToValue, duration));
}
/**
Animates this property from the current value to the specified double.
Any previous animations are stopped.
*/
public void animateTo(double toValue, int duration, Easing easing) {
int fToValue = CoreMath.toFixed(toValue);
setBehavior(new Tween(getAsFixed(), fToValue, duration, easing));
}
/**
Animates this property from the current value to the specified double.
Any previous animations are stopped.
*/
public void animateTo(double toValue, int duration, Easing easing, int startDelay) {
int fToValue = CoreMath.toFixed(toValue);
setBehavior(new Tween(getAsFixed(), fToValue, duration, easing, startDelay));
}
}