forked from genail/pulpcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPath.java
More file actions
1416 lines (1255 loc) · 52.9 KB
/
Path.java
File metadata and controls
1416 lines (1255 loc) · 52.9 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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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.math;
import java.util.StringTokenizer;
import java.util.ArrayList;
import pulpcore.animation.Tween;
import pulpcore.animation.Easing;
import pulpcore.animation.Timeline;
import pulpcore.image.CoreGraphics;
import pulpcore.sprite.Sprite;
/*
Internal notes:
* Each bezier segment is converted to line segments so that
animation along the path can occur at a constant rate.
* For now, MOVE_TO commands that don't occur at the first draw command
are interpreted at LINE_TO commands.
* Inputs are floats, but the segments are stored as fixed point.
*/
/**
The Path class is a series of straight lines and curves that a
Sprite can animate along.
<p>
Paths points are immutable, but the path can be translated to another location.
<p>
Paths are created from SVG path commands. For example, a triangle path:
<pre>path = new Path("M 100 100 L 300 100 L 200 300 L 100 100");</pre>
A simple curve:
<pre>path = new Path("M100,200 C100,100 400,100 400,200");</pre>
See <a href="http://www.w3.org/TR/SVG/paths.html#PathData">http://www.w3.org/TR/SVG/paths.html#PathData</a>.
All SVG commands are supported, however, move-to commands in the middle of a path are
treated as line-to commands. That is, subpaths are concatenated together to form one path.
<p>
Note, the Path class is not used for rendering paths or shapes.
*/
public class Path {
/**
The move command. The move-to command requires one point: the location
to move to.
*/
private static final int MOVE_TO = 0;
/**
The line segment command. The line-to command requires one point:
the location to draw a line segment to.
*/
private static final int LINE_TO = 1;
/**
The cubic bezier command. The curve-to command requires three points.
The first point is the control point at the beginning of the curve,
the second point is the control point at the end of the curve,
and the third point is the final destination point. All points are
absolute values.
*/
private static final int CURVE_TO = 2;
/** The number of points in the path. */
private final int numPoints;
/** The x-location of each point (fixed). */
private final int[] xPoints;
/** The y-location of each point (fixed). */
private final int[] yPoints;
/** The value, from 0 to 1, representing the portion of the total length at each point. */
private final int[] pPoints;
private int totalLength;
private boolean isClosed;
private transient int lastCalcP = -1;
private transient int[] lastCalcPoint = new int[2];
/**
Parse an SVG path data string.
supported. See http://www.w3.org/TR/SVG/paths.html#PathData
@throws IllegalArgumentException If the path data string could not be parsed.
*/
public Path(String svgPathData) throws IllegalArgumentException {
ArrayList commands = parseSVGPathData(svgPathData);
int[][] points = toLineSegments(commands);
this.xPoints = points[0];
this.yPoints = points[1];
this.numPoints = xPoints.length;
this.pPoints = new int[numPoints];
init();
}
/**
Creates a new Path with the specified points.
*/
public Path(float[] xPoints, float[] yPoints) {
this.xPoints = toFixed(xPoints);
this.yPoints = toFixed(yPoints);
this.numPoints = xPoints.length;
this.pPoints = new int[numPoints];
init();
}
/**
Returns true if the path is closed, that is, the first point and the last point
are identical.
*/
public boolean isClosed() {
return isClosed;
}
public double getLength() {
return CoreMath.toDouble(totalLength);
}
// These two methods are commented out because they are untested
//
///**
// Returns the area of the polygon defined by this closed path.
//*/
//public float getArea() {
// if (area == 0) {
//
// // Based on "Calculating the area and centroid of a polygon" by Paul Bourke
// // http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/
// for (int i = 0; i < numPoints; i++) {
// int j = (i + 1) % numPoints;
// area += CoreMath.toFloat(xPoints[i]) * CoreMath.toFloat(yPoints[j]);
// area -= CoreMath.toFloat(xPoints[j]) * CoreMath.toFloat(yPoints[i]);
// }
// area /= 2;
// }
// return area;
//}
//
//
///**
// Returns true if the specified point is contained inside
// the polygon defined by this closed path.
//*/
//public boolean contains(float x, float y) {
// int fx = CoreMath.toFixed(x);
// int fy = CoreMath.toFixed(y);
//
// // Based on the "Point Inclusion in Polygon Test" article by W. Randolph Franklin
// // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
// boolean isInside = false;
// int xpj = xPoints[numPoints - 1];
// int ypj = yPoints[numPoints - 1];
// for (int i = 0; i < numPoints; i++) {
// int xpi = xPoints[i];
// int ypi = yPoints[i];
//
// if (((ypi <= fy && fy < ypj) || (ypj <= fy && fy < ypi)) &&
// (fx < CoreMath.mulDiv(xpj - xpi, fy - ypi, ypj - ypi) + xpi))
// {
// // The ray crossed an edge
// isInside = !isInside;
// }
// xpj = xpi;
// ypj = ypi;
// }
//
// return isInside;
//}
private int[] toFixed(float[] n) {
int[] f = new int[n.length];
for (int i = 0; i < n.length; i++) {
f[i] = CoreMath.toFixed(n[i]);
}
return f;
}
private void init() {
// Find the lengths of each segment;
totalLength = 0;
int[] segmentLength = new int[numPoints - 1];
for (int i = 0; i < numPoints - 1; i++) {
int dist = (int)CoreMath.dist(xPoints[i], yPoints[i], xPoints[i + 1], yPoints[i + 1]);
segmentLength[i] = dist;
totalLength += dist;
}
pPoints[0] = 0;
pPoints[numPoints - 1] = CoreMath.ONE;
int length = 0;
for (int i = 1; i < numPoints - 1; i++) {
length += segmentLength[i - 1];
pPoints[i] = CoreMath.div(length, totalLength);
}
isClosed = xPoints[0] == xPoints[numPoints - 1] && yPoints[0] == yPoints[numPoints - 1];
}
public void translate(double x, double y) {
if (x == 0 && y == 0) {
return;
}
int fX = CoreMath.toFixed(x);
int fY = CoreMath.toFixed(y);
for (int i = 0; i < numPoints; i++) {
xPoints[i] += fX;
yPoints[i] += fY;
}
}
public int getStartX() {
return CoreMath.toInt(xPoints[0]);
}
public int getStartY() {
return CoreMath.toInt(yPoints[0]);
}
public int getEndX() {
return CoreMath.toInt(xPoints[numPoints - 1]);
}
public int getEndY() {
return CoreMath.toInt(yPoints[numPoints - 1]);
}
//
//
//
/**
Gets the x location of point p on the path, where p is typically from 0 (start of the path)
to 1 (end of the path).
@param p The position along the path to place the sprite, from 0 to 1.
*/
public double getX(double p) {
int px = clampP(p);
return CoreMath.toDouble(get(0, px));
}
/**
Gets the y location of point p on the path, where p is typically from 0 (start of the path)
to 1 (end of the path).
@param p The position along the path to place the sprite, from 0 to 1.
*/
public double getY(double p) {
int px = clampP(p);
return CoreMath.toDouble(get(1, px));
}
/**
Gets the angle of point p on the path, where p is typically from 0 (start of the path)
to 1 (end of the path).
@param p The position along the path to place the sprite, from 0 to 1.
*/
public double getAngle(double p) {
int px = clampP(p);
return CoreMath.toDouble(get(2, px));
//int i = getLowerSegment(px);
//return Math.atan2(yPoints[i + 1] - yPoints[i], xPoints[i + 1] - xPoints[i]);
}
/**
Places a Sprite at a position along the path.
@param sprite The Sprite to place.
@param p The position along the path to place the sprite, from 0 to 1.
*/
public void place(Sprite sprite, double p) {
int px = clampP(p);
sprite.x.setAsFixed(get(0, px));
sprite.y.setAsFixed(get(1, px));
}
/**
Moves a sprite along this path.
<p>If the path is open ({@link #isClosed()} returns false), the position is clamped from
0 to 1. Otherwise, if the path is open, the position wraps. For example, for a closed path,
moving from 0 to 1.5 is the same as moving from 0 to 1 and then from 0 to 0.5.
@param sprite The sprite
@param startP The start position along the path, typically from 0 to 1.
@param endP The start position along the path, typically from 0 to 1.
@param duration The animation duration.
@see Timeline#move(pulpcore.sprite.Sprite, pulpcore.math.Path, double, double, int)
*/
public void move(Sprite sprite, double startP, double endP, int duration) {
moveAsFixed(null, sprite, false, CoreMath.toFixed(startP), CoreMath.toFixed(endP),
duration, null, 0);
}
/**
Moves a sprite along this path, rotating the sprite so that it is tangent to the path
at all times.
<p>If the path is open ({@link #isClosed()} returns false), the position is clamped from
0 to 1. Otherwise, if the path is open, the position wraps. For example, for a closed path,
moving from 0 to 1.5 is the same as moving from 0 to 1 and then from 0 to 0.5.
@param sprite The sprite
@param startP The start position along the path, typically from 0 to 1.
@param endP The start position along the path, typically from 0 to 1.
@param duration The animation duration.
@see Timeline#moveAndRotate(pulpcore.sprite.Sprite, pulpcore.math.Path, double, double, int)
*/
public void moveAndRotate(Sprite sprite, double startP, double endP, int duration) {
moveAsFixed(null, sprite, true, CoreMath.toFixed(startP), CoreMath.toFixed(endP),
duration, null, 0);
}
/**
Moves a sprite along this path.
<p>If the path is open ({@link #isClosed()} returns false), the position is clamped from
0 to 1. Otherwise, if the path is open, the position wraps. For example, for a closed path,
moving from 0 to 1.5 is the same as moving from 0 to 1 and then from 0 to 0.5.
@param sprite The sprite
@param startP The start position along the path, typically from 0 to 1.
@param endP The start position along the path, typically from 0 to 1.
@param duration The animation duration.
@param easing The animation easing.
@see Timeline#move(pulpcore.sprite.Sprite, pulpcore.math.Path, double, double, int, pulpcore.animation.Easing)
*/
public void move(Sprite sprite, double startP, double endP, int duration,
Easing easing)
{
moveAsFixed(null, sprite, false, CoreMath.toFixed(startP), CoreMath.toFixed(endP),
duration, easing, 0);
}
/**
Moves a sprite along this path, rotating the sprite so that it is tangent to the path
at all times.
<p>If the path is open ({@link #isClosed()} returns false), the position is clamped from
0 to 1. Otherwise, if the path is open, the position wraps. For example, for a closed path,
moving from 0 to 1.5 is the same as moving from 0 to 1 and then from 0 to 0.5.
@param sprite The sprite
@param startP The start position along the path, typically from 0 to 1.
@param endP The start position along the path, typically from 0 to 1.
@param duration The animation duration.
@param easing The animation easing.
@see Timeline#moveAndRotate(pulpcore.sprite.Sprite, pulpcore.math.Path, double, double, int, pulpcore.animation.Easing)
*/
public void moveAndRotate(Sprite sprite, double startP, double endP, int duration,
Easing easing)
{
moveAsFixed(null, sprite, true, CoreMath.toFixed(startP), CoreMath.toFixed(endP),
duration, easing, 0);
}
/**
Moves a sprite along this path.
<p>If the path is open ({@link #isClosed()} returns false), the position is clamped from
0 to 1. Otherwise, if the path is open, the position wraps. For example, for a closed path,
moving from 0 to 1.5 is the same as moving from 0 to 1 and then from 0 to 0.5.
@param sprite The sprite
@param startP The start position along the path, typically from 0 to 1.
@param endP The start position along the path, typically from 0 to 1.
@param duration The animation duration.
@param easing The animation easing.
@param startDelay The animation start delay.
@see Timeline#move(pulpcore.sprite.Sprite, pulpcore.math.Path, double, double, int, pulpcore.animation.Easing, int)
*/
public void move(Sprite sprite, double startP, double endP, int duration,
Easing easing, int startDelay)
{
moveAsFixed(null, sprite, false, CoreMath.toFixed(startP), CoreMath.toFixed(endP),
duration, easing, startDelay);
}
/**
Moves a sprite along this path, rotating the sprite so that it is tangent to the path
at all times.
<p>If the path is open ({@link #isClosed()} returns false), the position is clamped from
0 to 1. Otherwise, if the path is open, the position wraps. For example, for a closed path,
moving from 0 to 1.5 is the same as moving from 0 to 1 and then from 0 to 0.5.
@param sprite The sprite
@param startP The start position along the path, typically from 0 to 1.
@param endP The start position along the path, typically from 0 to 1.
@param duration The animation duration.
@param easing The animation easing.
@param startDelay The animation start delay.
@see Timeline#moveAndRotate(pulpcore.sprite.Sprite, pulpcore.math.Path, double, double, int, pulpcore.animation.Easing, int)
*/
public void moveAndRotate(Sprite sprite, double startP, double endP, int duration,
Easing easing, int startDelay)
{
moveAsFixed(null, sprite, true, CoreMath.toFixed(startP), CoreMath.toFixed(endP),
duration, easing, startDelay);
}
/**
Moves a sprite along this path.
<p>If the path is open ({@link #isClosed()} returns false), the position is clamped from
0 to 1. Otherwise, if the path is open, the position wraps. For example, for a closed path,
moving from 0 to 1.5 is the same as moving from 0 to 1 and then from 0 to 0.5.
@param sprite The sprite
@param startP The start position along the path, typically from 0 to 1.
@param endP The start position along the path, typically from 0 to 1.
@param duration The animation duration.
@param easing The animation easing.
@param startDelay The animation start delay.
@see Timeline#move(pulpcore.sprite.Sprite, pulpcore.math.Path, double, double, int, pulpcore.animation.Easing, int)
*/
public void moveOnTimeline(Timeline timeline, Sprite sprite, double startP, double endP,
int duration, Easing easing, int startDelay)
{
moveAsFixed(timeline, sprite, false, CoreMath.toFixed(startP), CoreMath.toFixed(endP),
duration, easing, startDelay);
}
/**
Moves a sprite along this path, rotating the sprite so that it is tangent to the path
at all times.
<p>If the path is open ({@link #isClosed()} returns false), the position is clamped from
0 to 1. Otherwise, if the path is open, the position wraps. For example, for a closed path,
moving from 0 to 1.5 is the same as moving from 0 to 1 and then from 0 to 0.5.
@param sprite The sprite
@param startP The start position along the path, typically from 0 to 1.
@param endP The start position along the path, typically from 0 to 1.
@param duration The animation duration.
@param easing The animation easing.
@param startDelay The animation start delay.
@see Timeline#moveAndRotate(pulpcore.sprite.Sprite, pulpcore.math.Path, double, double, int, pulpcore.animation.Easing, int)
*/
public void moveAndRotateOnTimeline(Timeline timeline, Sprite sprite, double startP, double endP,
int duration, Easing easing, int startDelay)
{
moveAsFixed(timeline, sprite, true, CoreMath.toFixed(startP), CoreMath.toFixed(endP),
duration, easing, startDelay);
}
/**
@deprecated
*/
public void guide(Timeline timeline, Sprite sprite, int duration) {
moveAsFixed(timeline, sprite, false, 0, CoreMath.ONE, duration, null, 0);
}
/**
@deprecated
*/
public void guide(Timeline timeline, Sprite sprite, int duration, Easing easing) {
moveAsFixed(timeline, sprite, false, 0, CoreMath.ONE, duration, easing, 0);
}
/**
@deprecated
*/
public void guide(Timeline timeline, Sprite sprite, int duration, Easing easing,
int startDelay)
{
moveAsFixed(timeline, sprite, false, 0, CoreMath.ONE, duration, easing, startDelay);
}
/**
@deprecated
*/
public void guideBackwards(Timeline timeline, Sprite sprite, int duration) {
moveAsFixed(timeline, sprite, false, CoreMath.ONE, 0, duration, null, 0);
}
/**
@deprecated
*/
public void guideBackwards(Timeline timeline, Sprite sprite, int duration, Easing easing) {
moveAsFixed(timeline, sprite, false, CoreMath.ONE, 0, duration, easing, 0);
}
/**
@deprecated
*/
public void guideBackwards(Timeline timeline, Sprite sprite, int duration, Easing easing,
int startDelay)
{
moveAsFixed(timeline, sprite, false, CoreMath.ONE, 0, duration, easing, startDelay);
}
/**
@deprecated
*/
public void guide(Timeline timeline, Sprite sprite, double startP, double endP, int duration) {
moveAsFixed(timeline, sprite, false, CoreMath.toFixed(startP), CoreMath.toFixed(endP),
duration, null, 0);
}
/**
@deprecated
*/
public void guide(Timeline timeline, Sprite sprite, double startP, double endP, int duration,
Easing easing)
{
moveAsFixed(timeline, sprite, false, CoreMath.toFixed(startP), CoreMath.toFixed(endP),
duration, easing, 0);
}
/**
@deprecated
*/
public void guide(Timeline timeline, Sprite sprite, double startP, double endP, int duration,
Easing easing, int startDelay)
{
moveAsFixed(timeline, sprite, false, CoreMath.toFixed(startP), CoreMath.toFixed(endP),
duration, easing, startDelay);
}
private void moveAsFixed(Timeline timeline, Sprite sprite, boolean includeAngle,
int startP, int endP, int duration, Easing easing, int startDelay)
{
PathAnimation xAnimation =
new PathAnimation(PathAnimation.X_AXIS, startP, endP, duration, easing, startDelay);
PathAnimation yAnimation =
new PathAnimation(PathAnimation.Y_AXIS, startP, endP, duration, easing, startDelay);
PathAnimation angleAnimation =
new PathAnimation(PathAnimation.ANGLE, startP, endP, duration, easing, startDelay);
if (timeline == null) {
sprite.x.setBehavior(xAnimation);
sprite.y.setBehavior(yAnimation);
if (includeAngle) {
sprite.angle.setBehavior(angleAnimation);
}
}
else {
timeline.add(sprite.x, xAnimation);
timeline.add(sprite.y, yAnimation);
if (includeAngle) {
timeline.add(sprite.angle, angleAnimation);
}
}
}
private class PathAnimation extends Tween {
public static final int X_AXIS = 0;
public static final int Y_AXIS = 1;
public static final int ANGLE = 2;
private final int axis;
private final int startP;
private final int endP;
PathAnimation(int axis, int startP, int endP, int duration, Easing easing, int startDelay) {
super(
get(axis, startP),
get(axis, endP),
duration, easing, startDelay);
this.axis = axis;
this.startP = startP;
this.endP = endP;
}
protected void updateState(int animTime) {
if (getDuration() == 0) {
super.updateState(animTime);
}
else {
int p = startP + CoreMath.mulDiv(endP - startP, animTime, getDuration());
super.setValue(get(axis, p));
}
}
}
//
//
//
private int clampP(double p) {
return clampP(CoreMath.toFixed(p));
}
private int clampP(int p) {
if (isClosed()) {
// Only wrap if the path is closed.
// Special case: ONE is considered the end.
if (p != CoreMath.ONE) {
p &= 0xffff;
}
}
else {
p = CoreMath.clamp(p, 0, CoreMath.ONE);
}
return p;
}
private int get(int axis, int p) {
int px = clampP(p);
if (axis == 0 || axis == 1) {
return getLocation(px)[axis];
}
else {
int i = getLowerSegment(px);
int lowerAngle = CoreMath.atan2(yPoints[i + 1] - yPoints[i],
xPoints[i + 1] - xPoints[i]);
if (i == numPoints - 2) {
return lowerAngle;
}
else {
int higherAngle = CoreMath.atan2(yPoints[i + 2] - yPoints[i + 1],
xPoints[i + 2] - xPoints[i + 1]);
int dAngle = higherAngle - lowerAngle;
if (Math.abs(dAngle + CoreMath.TWO_PI) < Math.abs(dAngle)) {
dAngle += CoreMath.TWO_PI;
}
else if (Math.abs(dAngle - CoreMath.TWO_PI) < Math.abs(dAngle)) {
dAngle -= CoreMath.TWO_PI;
}
return lowerAngle + CoreMath.mulDiv(dAngle, px - pPoints[i], pPoints[i + 1] - pPoints[i]);
}
}
}
private int getLowerSegment(int p) {
// Binary search for P
int high = numPoints - 1;
int low = 0;
while (high - low > 1) {
int index = (high + low) >>> 1;
if (pPoints[index] > p) {
high = index;
}
else {
low = index;
}
}
return low;
}
private int[] getLocation(int p) {
if (p == lastCalcP) {
return lastCalcPoint;
}
lastCalcP = p;
if (p == 0) {
lastCalcPoint[0] = xPoints[0];
lastCalcPoint[1] = yPoints[0];
return lastCalcPoint;
}
else if (p == CoreMath.ONE) {
lastCalcPoint[0] = xPoints[numPoints - 1];
lastCalcPoint[1] = yPoints[numPoints - 1];
return lastCalcPoint;
}
// Binary search for P
int high = numPoints - 1;
int low = 0;
while (high - low > 1) {
int index = (high + low) >>> 1;
if (pPoints[index] > p) {
high = index;
}
else {
low = index;
}
}
if (high == low) {
lastCalcPoint[0] = xPoints[low];
lastCalcPoint[1] = yPoints[low];
}
else {
int q = p - pPoints[low];
int r = pPoints[high] - pPoints[low];
lastCalcPoint[0] = xPoints[low] + CoreMath.mulDiv(xPoints[high] - xPoints[low], q, r);
lastCalcPoint[1] = yPoints[low] + CoreMath.mulDiv(yPoints[high] - yPoints[low], q, r);
}
return lastCalcPoint;
}
/**
Draws the segments of this path using the current color.
@param drawJoints if true, draw rectangles at the joints between line segments
*/
public void draw(CoreGraphics g, boolean drawJoints) {
int x1 = xPoints[0];
int y1 = yPoints[0];
for (int i = 1; i < numPoints; i++) {
if (drawJoints) {
g.fillRectFixedPoint(x1-CoreMath.ONE, y1-CoreMath.ONE,
CoreMath.toFixed(3), CoreMath.toFixed(3));
}
int x2 = xPoints[i];
int y2 = yPoints[i];
g.drawLineFixedPoint(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
}
if (drawJoints) {
g.fillRectFixedPoint(x1-CoreMath.ONE, y1-CoreMath.ONE,
CoreMath.toFixed(3), CoreMath.toFixed(3));
}
}
//
// SVG path-data parsing
//
/**
Parses an SVG Path to simple move-to, line-to, curve-to commands.
Returns a List of float[] arrays. The first item in the array is the command,
(MOVE_TO, LINE_TO, or CURVE_TO) and the remaining items in the array are the command
parameters (2, 2, and 6 respectively).
*/
private static ArrayList parseSVGPathData(String svgPathData) throws IllegalArgumentException {
ArrayList commands = new ArrayList();
svgPathData = svgPathData.trim();
StringTokenizer tokenizer = new StringTokenizer(svgPathData, "MmLlHhVvAaQqTtCcSsZz", true);
float currX = 0;
float currY = 0;
float initialX = 0;
float initialY = 0;
float lastControlX = 0;
float lastControlY = 0;
String lastCommand = "";
while (tokenizer.hasMoreTokens()) {
String commandType = tokenizer.nextToken();
if (commandType.trim().length() == 0) {
// This might happen after a Z command
continue;
}
boolean isRelative = false;
if (commandType.length() == 1 && Character.isLowerCase(commandType.charAt(0))) {
commandType = commandType.toUpperCase();
isRelative = true;
}
if ("Z".equals(commandType)) {
// Special case: don't parse numbers
if (currX != initialX || currY != initialY) {
commands.add(new float[] { LINE_TO, initialX, initialY });
currX = initialX;
currY = initialY;
}
}
else {
float[] numbers = parseNumberList(tokenizer.nextToken());
if ("M".equals(commandType)) {
checkIfSizeIsMultipleOf(commandType, numbers, 2);
for (int i = 0; i < numbers.length; i+=2) {
float endX = numbers[i];
float endY = numbers[i + 1];
if (isRelative) {
endX += currX;
endY += currY;
}
if (i == 0) {
commands.add(new float[] { MOVE_TO, endX, endY });
initialX = endX;
initialY = endY;
}
else {
commands.add(new float[] { LINE_TO, endX, endY });
}
currX = endX;
currY = endY;
}
}
else if ("L".equals(commandType)) {
checkIfSizeIsMultipleOf(commandType, numbers, 2);
for (int i = 0; i < numbers.length; i+=2) {
float endX = numbers[i];
float endY = numbers[i + 1];
if (isRelative) {
endX += currX;
endY += currY;
}
commands.add(new float[] { LINE_TO, endX, endY });
currX = endX;
currY = endY;
}
}
else if ("H".equals(commandType)) {
for (int i = 0; i < numbers.length; i++) {
float endX = numbers[i];
if (isRelative) {
endX += currX;
}
commands.add(new float[] { LINE_TO, endX, currY });
currX = endX;
}
}
else if ("V".equals(commandType)) {
for (int i = 0; i < numbers.length; i++) {
float endY = numbers[i];
if (isRelative) {
endY += currY;
}
commands.add(new float[] { LINE_TO, currX, endY });
currY = endY;
}
}
else if ("C".equals(commandType)) {
checkIfSizeIsMultipleOf(commandType, numbers, 6);
for (int i = 0; i < numbers.length; i+=6) {
float x1 = numbers[i + 0];
float y1 = numbers[i + 1];
float x2 = numbers[i + 2];
float y2 = numbers[i + 3];
float endX = numbers[i + 4];
float endY = numbers[i + 5];
if (isRelative) {
x1 += currX;
y1 += currY;
x2 += currX;
y2 += currY;
endX += currX;
endY += currY;
}
commands.add(new float[] { CURVE_TO, x1, y1, x2, y2, endX, endY });
currX = endX;
currY = endY;
lastControlX = x2;
lastControlY = y2;
}
}
else if ("S".equals(commandType)) {
checkIfSizeIsMultipleOf(commandType, numbers, 4);
for (int i = 0; i < numbers.length; i+=4) {
float x1;
float y1;
float x2 = numbers[i + 0];
float y2 = numbers[i + 1];
float endX = numbers[i + 2];
float endY = numbers[i + 3];
if (isRelative) {
x2 += currX;
y2 += currY;
endX += currX;
endY += currY;
}
if (i > 0 || "C".equals(lastCommand) || "S".equals(lastCommand)) {
// "The reflection of the second control point on the previous command
// relative to the current point"
x1 = 2*currX - lastControlX;
y1 = 2*currY - lastControlY;
}
else {
// "Coincident with the current point"
x1 = currX;
y1 = currY;
}
commands.add(new float[] { CURVE_TO, x1, y1, x2, y2, endX, endY });
currX = endX;
currY = endY;
lastControlX = x2;
lastControlY = y2;
}
}
else if ("Q".equals(commandType)) {
checkIfSizeIsMultipleOf(commandType, numbers, 4);
for (int i = 0; i < numbers.length; i+=4) {
float qx = numbers[i + 0];
float qy = numbers[i + 1];
float endX = numbers[i + 2];
float endY = numbers[i + 3];
if (isRelative) {
qx += currX;
qy += currY;
endX += currX;
endY += currY;
}
// Convert quadratic bezier to cubic
float x1 = (currX + 2 * qx) / 3;
float y1 = (currY + 2 * qy) / 3;
float x2 = (endX + 2 * qx) / 3;
float y2 = (endY + 2 * qy) / 3;
commands.add(new float[] { CURVE_TO, x1, y1, x2, y2, endX, endY });
currX = endX;
currY = endY;
lastControlX = qx;
lastControlY = qy;
}
}
else if ("T".equals(commandType)) {
checkIfSizeIsMultipleOf(commandType, numbers, 2);
for (int i = 0; i < numbers.length; i+=2) {
float qx;
float qy;
float endX = numbers[i + 0];
float endY = numbers[i + 1];
if (isRelative) {
endX += currX;
endY += currY;
}
if (i > 0 || "Q".equals(lastCommand) || "T".equals(lastCommand)) {
// "The reflection of the control point on the previous command relative
// to the current point"
qx = 2*currX - lastControlX;
qy = 2*currY - lastControlY;
}
else {
// "Coincident with the current point"
qx = currX;
qy = currY;
}
// Convert quadratic bezier to cubic
float x1 = (currX + 2 * qx) / 3;
float y1 = (currY + 2 * qy) / 3;
float x2 = (endX + 2 * qx) / 3;
float y2 = (endY + 2 * qy) / 3;
commands.add(new float[] { CURVE_TO, x1, y1, x2, y2, endX, endY });
currX = endX;
currY = endY;
lastControlX = qx;
lastControlY = qy;
}
}
else if ("A".equals(commandType)) {
checkIfSizeIsMultipleOf(commandType, numbers, 7);
for (int i = 0; i < numbers.length; i+=7) {
// See http://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
float rx = numbers[i + 0];
float ry = numbers[i + 1];
float angle = (float)Math.toRadians(numbers[i + 2] % 360);
boolean largeArc = numbers[i + 3] != 0;
boolean sweep = numbers[i + 4] != 0;
float endX = numbers[i + 5];
float endY = numbers[i + 6];
if (isRelative) {
endX += currX;
endY += currY;
}
if (currX == endX && currY == endY) {
// Do nothing
}
else if (rx == 0 || ry == 0) {
commands.add(new float[] { LINE_TO, endX, endY });
}
else {
commands.addAll(parseArc(currX, currY, rx, ry, angle,
largeArc, sweep, endX, endY));
}
currX = endX;
currY = endY;
}
}
else {
throw new IllegalArgumentException("Not a supported command: " + commandType);
}
}
lastCommand = commandType;
}
return commands;
}
/**
Convert arc (ellipse) to cubic bezier curve(s).
*/
private static ArrayList parseArc(float currX, float currY, float rx, float ry, float angle,
boolean largeArc, boolean sweep,
float endX, float endY)
{