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

Revision 40, 7.8 KB checked in by bmcguffin, 9 years ago (diff)

Added handling methods for injecting CAD data into timeslices.

Line 
1package scriptbuilder.structures;
2
3import java.text.SimpleDateFormat;
4import java.util.ArrayList;
5import java.util.Collections;
6import java.util.Date;
7import java.util.List;
8import java.util.TimeZone;
9import scriptbuilder.gui.ScriptBuilderGuiConstants;
10import scriptbuilder.structures.events.*;
11import scriptbuilder.structures.events.I_ScriptEvent;
12
13/**
14 * An instance in time in the simulation. Each timeslice has a start time, an X
15 * position on the GUI timeline, and a list of ScriptEvents which occur at that
16 * time.
17 *
18 * @author Greg Eddington <geddingt@calpoly.edu>
19 * @author Bryan McGuffin
20 * @version 2017/06/30
21 */
22public class TimeSlice implements Comparable, I_XML_Writable
23{
24    CadData cadData = null;
25
26    /**
27     * Reference to the incident which contains this timeslice.
28     */
29    private ScriptIncident thisIncident;
30
31    /**
32     * List of Script Events which begin at this instance.
33     */
34    public List<I_ScriptEvent> events;
35    //Absolute start time of this time slice
36    private int seconds;
37
38    /**
39     * Constructor.
40     *
41     * @param seconds Number of seconds from the beginning of the simulation
42     * that this timeslice occurs
43     */
44    public TimeSlice(int seconds, ScriptIncident inc)
45    {
46        this.seconds = seconds;
47        events = new ArrayList<I_ScriptEvent>();
48        thisIncident = inc;
49    }
50
51    /**
52     * Add a new script event to this time slice. Sort events by event type.
53     *
54     * @param event the ScriptEvent to be added
55     */
56    public void addEvent(I_ScriptEvent event)
57    {
58        events.add(event);
59        Collections.sort(events);
60    }
61
62    /**
63     * Get the X position of this timeslice on the GUI timeline. Affected by
64     * zoom level.
65     *
66     * @return Screen distance from the start of the timeline that this
67     * timeslice occurs
68     */
69    public int getX()
70    {
71        return seconds / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION
72                * ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK;
73    }
74
75    /**
76     * Get the start time of this timeSlice.
77     *
78     * @return the start time of the timeslice in seconds, from the beginning of
79     * the simulation
80     */
81    public int getTime()
82    {
83        return seconds;
84    }
85
86    /**
87     * Shift the start position of this timeslice to the right by some amount.
88     *
89     * @param amnt the number of seconds forward in time to push this slice
90     */
91    public void shift(int amnt)
92    {
93        seconds += amnt;
94    }
95
96    /**
97     * Get the number of seconds that events which start in this timeslice are
98     * active.
99     *
100     * @return The duration of the longest-lasting event in this slice.
101     */
102    public int getEffectiveDuration()
103    {
104        int dur = 0;
105        for (I_ScriptEvent e : events)
106        {
107            if (e.getLength() > dur)
108            {
109                dur = e.getLength();
110            }
111        }
112        return dur;
113    }
114
115    /**
116     * Get the text to be displayed when hovering over this timeSlice.
117     *
118     * @param y the y-position of the cursor on the GUI
119     * @return The text to be displayed: all events are listed if we're on the
120     * main incident line, but only one event is listed if we're hovering over
121     * that particular event
122     */
123    public String getToolTipText(int y)
124    {
125        int i = (y - ScriptBuilderGuiConstants.SCRIPT_EVENT_ICON_TOP_MARGIN);
126        if (i < 0)
127        {
128            if (i > (-1 * ScriptBuilderGuiConstants.SCRIPT_EVENT_ICON_STEP))
129            {
130                String s = toString();
131                if (s.equals(""))
132                {
133                    return null;
134                }
135
136                return "<html>" + s.replace("\n", "<br/>") + "</html>";
137            }
138            else
139            {
140                return null;
141            }
142        }
143
144        i /= ScriptBuilderGuiConstants.SCRIPT_EVENT_ICON_STEP;
145        if (i < events.size())
146        {
147            return events.get(i).toString();
148        }
149        return null;
150    }
151
152    /**
153     * List all the events attached to this timeslice, with line breaks between.
154     *
155     * @return A list of the form: event1_type - event1_description event2_type
156     * - event2_description ...
157     */
158    @Override
159    public String toString()
160    {
161        StringBuilder sb = new StringBuilder();
162
163        for (I_ScriptEvent event : events)
164        {
165            sb.append(event.toString());
166            sb.append('\n');
167        }
168
169        return sb.toString();
170    }
171
172    /**
173     * Order the timeslices by start time. Earlier start times come first.
174     *
175     * @param o the other timeSlice to be compared
176     * @return -1, 0 , or 1 depending on if this timeSlice comes before,
177     * simultaneously, or after the other
178     */
179    @Override
180    public int compareTo(Object o)
181    {
182        TimeSlice other = (TimeSlice) o;
183        if (this.getTime() < other.getTime())
184        {
185            return -1;
186        }
187        else if (this.getTime() > other.getTime())
188        {
189            return 1;
190        }
191        else
192        {
193            return 0;
194        }
195    }
196
197    /**
198     * Converts the contents of this timeslice to a correctly formatted
199     * <ScriptEvent> XML element.
200     *
201     * @return XML conversion of this timeslice.
202     */
203    @Override
204    public String toXML()
205    {
206        ArrayList<I_ScriptEvent> eventsCopy = new ArrayList<I_ScriptEvent>();
207        ArrayList<I_ScriptEvent> eventsCopy2 = new ArrayList<I_ScriptEvent>();
208
209        for (I_ScriptEvent e : events)
210        {
211            eventsCopy.add(e);
212        }
213
214        System.out.println("Seconds:: " + seconds);
215        String output = openTag(ELEMENT.SCRIPT_EVENT.tag);
216        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
217        df.setTimeZone(TimeZone.getTimeZone("GMT"));
218        output += openTag(ELEMENT.TIME_INDEX.tag) + df.format(new Date(seconds * 1000))
219                + closeTag(ELEMENT.TIME_INDEX.tag);
220
221        output += openTag(ELEMENT.INCIDENT.tag + " LogNum=\"" + thisIncident.number + "\"");
222        output += thisIncident.name + closeTag(ELEMENT.INCIDENT.tag);
223
224        output += openTag(ELEMENT.CAD_DATA.tag);
225        if (containsCADIncidentEvent())
226        {
227            output += openTag(ELEMENT.CAD_INCIDENT_EVENT.tag);
228            for (I_ScriptEvent ev : eventsCopy)
229            {
230                if (ev instanceof I_XML_Writable && isCADIncidentEvent(ev))
231                {
232                    I_XML_Writable ex = (I_XML_Writable) ev;
233                    output += ex.toXML();
234                }
235                else
236                {
237                    eventsCopy2.add(ev);
238                }
239            }
240
241            eventsCopy = eventsCopy2;
242
243            output += closeTag(ELEMENT.CAD_INCIDENT_EVENT.tag);
244        }
245        output += closeTag(ELEMENT.CAD_DATA.tag);
246
247        for (I_ScriptEvent ev : eventsCopy)
248        {
249            if (ev instanceof I_XML_Writable)
250            {
251                I_XML_Writable ex = (I_XML_Writable) ev;
252                output += ex.toXML();
253            }
254        }
255        output += closeTag(ELEMENT.SCRIPT_EVENT.tag);
256        System.out.println(output);
257        return output;
258    }
259
260    @Override
261    public String openTag(String s)
262    {
263        return "<" + s + ">";
264    }
265
266    @Override
267    public String closeTag(String s)
268    {
269        return "</" + s + ">\n";
270    }
271
272    @Override
273    public String emptyTag(String s)
274    {
275        return "<" + s + "/>\n";
276    }
277
278    private boolean containsCADIncidentEvent()
279    {
280        for (I_ScriptEvent ev : events)
281        {
282            if (isCADIncidentEvent(ev))
283            {
284                return true;
285            }
286        }
287        return false;
288    }
289
290    private boolean isCADIncidentEvent(I_ScriptEvent ev)
291    {
292        return ev instanceof AudioEvent || ev instanceof UnitEvent
293                || ev instanceof ParamicsEvent || ev instanceof TowEvent
294                || ev instanceof WitnessEvent || ev instanceof CADEvent;
295    }
296}
Note: See TracBrowser for help on using the repository browser.