Warning: Can't use blame annotator:
svn blame failed on src/scriptbuilder/structures/TimeSlice.java: ("Can't find a temporary directory: Internal error", 20014)

source: tmcsimulator-scriptbuilder/src/scriptbuilder/structures/TimeSlice.java @ 1

Revision 1, 4.8 KB checked in by bmcguffin, 9 years ago (diff)

2017/07/18: Uploaded entire prototype to SVN repo.

RevLine 
1package scriptbuilder.structures;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.List;
6import scriptbuilder.gui.ScriptBuilderGuiConstants;
7import scriptbuilder.structures.events.ScriptEventInterface;
8
9/**
10 * An instance in time in the simulation. Each timeslice has a start time, an X
11 * position on the GUI timeline, and a list of ScriptEvents which occur at that
12 * time.
13 *
14 * @author Greg Eddington <geddingt@calpoly.edu>
15 * @author Bryan McGuffin
16 * @version 2017/06/30
17 */
18public class TimeSlice implements Comparable
19{
20
21    //Events which occur at this instance
22    /**
23     * List of Script Events which begin at this instance.
24     */
25    public List<ScriptEventInterface> events;
26    //Absolute start time of this time slice
27    private int seconds;
28
29    /**
30     * Constructor.
31     *
32     * @param seconds Number of seconds from the beginning of the simulation
33     * that this timeslice occurs
34     */
35    public TimeSlice(int seconds)
36    {
37        this.seconds = seconds;
38        events = new ArrayList<ScriptEventInterface>();
39    }
40
41    /**
42     * Add a new script event to this time slice. Sort events by event type.
43     *
44     * @param event the ScriptEvent to be added
45     */
46    public void addEvent(ScriptEventInterface event)
47    {
48        events.add(event);
49        Collections.sort(events);
50    }
51
52    /**
53     * Get the X position of this timeslice on the GUI timeline. Affected by
54     * zoom level.
55     *
56     * @return Screen distance from the start of the timeline that this
57     * timeslice occurs
58     */
59    public int getX()
60    {
61        return seconds / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION
62                * ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK;
63    }
64
65    /**
66     * Get the start time of this timeSlice.
67     *
68     * @return the start time of the timeslice in seconds, from the beginning of
69     * the simulation
70     */
71    public int getTime()
72    {
73        return seconds;
74    }
75
76    /**
77     * Shift the start position of this timeslice to the right by some amount.
78     *
79     * @param amnt the number of seconds forward in time to push this slice
80     */
81    public void shift(int amnt)
82    {
83        seconds += amnt;
84    }
85
86    /**
87     * Get the number of seconds that events which start in this timeslice are
88     * active.
89     *
90     * @return The duration of the longest-lasting event in this slice.
91     */
92    public int getEffectiveDuration()
93    {
94        int dur = 0;
95        for (ScriptEventInterface e : events)
96        {
97            if (e.getLength() > dur)
98            {
99                dur = e.getLength();
100            }
101        }
102        return dur;
103    }
104
105    /**
106     * Get the text to be displayed when hovering over this timeSlice.
107     *
108     * @param y the y-position of the cursor on the GUI
109     * @return The text to be displayed: all events are listed if we're on the
110     * main incident line, but only one event is listed if we're hovering over
111     * that particular event
112     */
113    public String getToolTipText(int y)
114    {
115        int i = (y - ScriptBuilderGuiConstants.SCRIPT_EVENT_ICON_TOP_MARGIN);
116        if (i < 0)
117        {
118            if (i > (-1 * ScriptBuilderGuiConstants.SCRIPT_EVENT_ICON_STEP))
119            {
120                String s = toString();
121                if (s.equals(""))
122                {
123                    return null;
124                }
125
126                return "<html>" + s.replace("\n", "<br/>") + "</html>";
127            }
128            else
129            {
130                return null;
131            }
132        }
133
134        i /= ScriptBuilderGuiConstants.SCRIPT_EVENT_ICON_STEP;
135        if (i < events.size())
136        {
137            return events.get(i).toString();
138        }
139        return null;
140    }
141
142    /**
143     * List all the events attached to this timeslice, with line breaks between.
144     *
145     * @return A list of the form: event1_type - event1_description event2_type
146     * - event2_description ...
147     */
148    @Override
149    public String toString()
150    {
151        StringBuilder sb = new StringBuilder();
152
153        for (ScriptEventInterface event : events)
154        {
155            sb.append(event.toString());
156            sb.append('\n');
157        }
158
159        return sb.toString();
160    }
161
162    /**
163     * Order the timeslices by start time. Earlier start times come first.
164     *
165     * @param o the other timeSlice to be compared
166     * @return -1, 0 , or 1 depending on if this timeSlice comes before,
167     * simultaneously, or after the other
168     */
169    @Override
170    public int compareTo(Object o)
171    {
172        TimeSlice other = (TimeSlice) o;
173        if (this.getTime() < other.getTime())
174        {
175            return -1;
176        }
177        else if (this.getTime() > other.getTime())
178        {
179            return 1;
180        }
181        else
182        {
183            return 0;
184        }
185
186    }
187}
Note: See TracBrowser for help on using the repository browser.