]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/autoplay/PointList.java
13ccf61749f708d918a9237c10e64961d8aee3ea
[GpsPrune.git] / tim / prune / function / autoplay / PointList.java
1 package tim.prune.function.autoplay;
2
3 /**
4  * Class to hold a list of points and hold a running position
5  */
6 public class PointList
7 {
8         /** Array of milliseconds for each point */
9         private long[] _millis = null;
10         /** Array of indexes of corresponding points */
11         private int[]  _indexes = null;
12         /** Array index of current position */
13         private int    _currentItem = 0;
14         /** Max array index */
15         private int    _maxItem = 0;
16
17         /**
18          * Constructor
19          * @param inNumPoints number of points
20          */
21         public PointList(int inNumPoints)
22         {
23                 _millis = new long[inNumPoints];
24                 _indexes = new int[inNumPoints];
25                 _currentItem = 0;
26                 _maxItem = inNumPoints - 1;
27         }
28
29         /**
30          * Add a point to the array
31          * @param inMillis milliseconds since start
32          * @param inIndex point index
33          */
34         public void setPoint(long inMillis, int inIndex)
35         {
36                 _millis[_currentItem] = inMillis;
37                 _indexes[_currentItem] = inIndex;
38                 _currentItem++;
39         }
40
41         /**
42          * Set the position using the current milliseconds
43          * @param inMillis milliseconds since start
44          */
45         public void set(long inMillis)
46         {
47                 if (isFinished() || inMillis < _millis[_currentItem])
48                 {
49                         // must be reset
50                         _currentItem = 0;
51                 }
52                 while (_currentItem < _maxItem && _millis[_currentItem + 1] < inMillis)
53                 {
54                         _currentItem++;
55                 }
56         }
57
58         /**
59          * Normalize the list to cover the requested number of seconds duration
60          * @param inSeconds length of autoplay sequence in seconds
61          */
62         public void normalize(int inSeconds)
63         {
64                 if (_maxItem <= 0)
65                 {
66                         return; // nothing to normalize
67                 }
68                 long currentDuration = _millis[_maxItem] - _millis[0];
69                 if (currentDuration > 0L)
70                 {
71                         double multFactor = inSeconds * 1000.0 / currentDuration;
72                         for (int i=0; i<=_maxItem; i++)
73                         {
74                                 _millis[i] = (long) (_millis[i] * multFactor);
75                         }
76                 }
77         }
78
79         /** @return the milliseconds of the current point */
80         public long getCurrentMilliseconds()
81         {
82                 if (isAtStart() || isFinished()) {
83                         return 0L;
84                 }
85                 return _millis[_currentItem];
86         }
87
88         /** @return the index of the current point */
89         public int getCurrentPointIndex()
90         {
91                 return _indexes[_currentItem];
92         }
93
94         /** @return true if we're on the first point */
95         public boolean isAtStart() {
96                 return _currentItem == 0;
97         }
98
99         /** @return true if we're on the last point */
100         public boolean isFinished() {
101                 return _currentItem >= _maxItem;
102         }
103
104         /**
105          * @param inCurrentMillis current time in milliseconds since start
106          * @return number of milliseconds to wait until next point is due
107          */
108         public long getMillisUntilNextPoint(long inCurrentMillis)
109         {
110                 if (isFinished() || _millis[_currentItem+1] < _millis[_currentItem]) {
111                         return 0; // no next point
112                 }
113                 return _millis[_currentItem+1] - inCurrentMillis;
114         }
115 }