source: tmcsimulator-scriptbuilder/trunk/src/scriptbuilder/structures/ScriptIncident.java @ 168

Revision 168, 16.6 KB checked in by sdanthin, 6 years ago (diff)

ScriptIncident?.java added addNewEventFromXML function, modified addNewEvent function to place correct file names.

Line 
1package scriptbuilder.structures;
2
3import java.awt.Color;
4import java.io.BufferedWriter;
5import java.io.File;
6import java.io.FileWriter;
7import java.util.ArrayList;
8import java.util.TreeMap;
9import scriptbuilder.structures.events.I_ScriptEvent;
10import scriptbuilder.structures.ScriptEvent.ScriptEventType;
11import scriptbuilder.structures.events.I_AudioEvent;
12import scriptbuilder.structures.events.AudioEvent;
13
14/**
15 * A script incident. It has an ID number, a name, and a description. It may
16 * contain several script events. It also has a color in the GUI window, and is
17 * collapsible. Incidents may start as soon as the script begins to run, or they
18 * can be offset from the start of the script.
19 *
20 * @author Greg Eddington <geddingt@calpoly.edu>
21 * @author Bryan McGuffin
22 * @version 2017/06/29
23 */
24public class ScriptIncident implements I_XML_Writable
25{
26
27    /**
28     * The moments in time which have associated events.
29     */
30    public TreeMap<Integer, TimeSlice> slices;
31
32    /**
33     * GUI display color of this slice.
34     */
35    public Color color;
36
37    /**
38     * ID number for this incident.
39     */
40    public int number;
41
42    /**
43     * Name of the incident.
44     */
45    public String name;
46
47    /**
48     * Description of the incident.
49     */
50    public String description;
51   
52    /**
53     * Length, in seconds, of the incident.
54     */
55    public int length = 0;
56
57    /**
58     * If true, incident appears minimized.
59     */
60    public boolean collapsed = false;
61
62    /**
63     * Number of seconds between start of simulation and start of this incident.
64     */
65    public int offset = 0;
66
67    /**
68     * Start position of the latest timeslice.
69     */
70    private int latestStart = 0;
71
72    /**
73     * Number of events in this incident.
74     */
75    public int eventCount = 0;
76   
77    public int audioEventCount = 0;
78
79    public SimulationScript script;
80
81    /**
82     * Basic constructor.
83     *
84     * @param number The incident ID number
85     * @param name The name of the incident
86     * @param description The description of the incident
87     * @param script The script object holding this incident
88     */
89    public ScriptIncident(int number, String name, String description,
90            SimulationScript script)
91    {
92        color = Color.BLACK;
93        this.number = number;
94        this.name = name;
95        this.description = description;
96        this.script = script;
97        slices = new TreeMap<Integer, TimeSlice>();
98    }
99
100    /**
101     * Constructor with color parameter.
102     *
103     * @param color The color to use in the GUI for this event
104     * @param number The incident ID number
105     * @param name The name of the incident
106     * @param description The description of the incident
107     * @param script The script object holding this incident
108     */
109    public ScriptIncident(Color color, int number, String name,
110            String description, SimulationScript script)
111    {
112        this.color = color;
113        this.number = number;
114        this.name = name;
115        this.description = description;
116        this.script = script;
117        slices = new TreeMap<Integer, TimeSlice>();
118    }
119
120    /**
121     * Constructor with color and offset parameters.
122     *
123     * @param color The color to use in the GUI for this event
124     * @param number The incident ID number
125     * @param name The name of the incident
126     * @param description The description of the incident
127     * @param script The script object holding this incident
128     * @param offset Number of seconds after 00:00:00 that this incident begins
129     */
130    public ScriptIncident(Color color, int number, String name,
131            String description, SimulationScript script,
132            int offset)
133    {
134        this.color = color;
135        this.number = number;
136        this.name = name;
137        this.description = description;
138        this.script = script;
139        slices = new TreeMap<Integer, TimeSlice>();
140        this.setOffset(offset);
141    }
142   
143//    /**
144//     * Constructor with type and location parameters.
145//     * @param color
146//     * @param number
147//     * @param name
148//     * @param description
149//     * @param script
150//     * @param offset
151//     * @param type
152//     * @param location
153//     */
154//    public ScriptIncident(Color color, int number, String name,
155//            String description, SimulationScript script,
156//            int offset, String type, String location)
157//    {
158//        this.color = color;
159//        this.number = number;
160//        this.name = name;
161//        this.description = description;
162//        this.script = script;
163//        slices = new TreeMap<Integer, TimeSlice>();
164//        this.setOffset(offset);
165//        this.location = location;
166//        this.type = type;
167//        insertCadData(offset, new CadData(type,location));
168//    }
169
170    /**
171     * Set whether or not the incident is fully visible or in a compacted state.
172     *
173     * @param collapsed True if the event is compacted
174     */
175    public void setCollapsed(boolean collapsed)
176    {
177        this.collapsed = collapsed;
178        script.update();
179    }
180
181    /**
182     * Set the delay time between the start of the script and the start of this
183     * incident.
184     *
185     * @param offset Number of seconds after 00:00:00 that this incident begins
186     */
187    public void setOffset(int offset)
188    {
189        int old = this.offset;
190        this.offset = offset;
191        TreeMap<Integer, TimeSlice> newSlices = new TreeMap<Integer, TimeSlice>();
192
193        int latest = 0;
194
195        for (Integer k : slices.keySet())
196        {
197            newSlices.put(k + (offset - old), slices.get(k));
198            latest = k + (offset - old);
199        }
200
201        latestStart = latest;
202
203        for (TimeSlice ts : newSlices.values())
204        {
205            ts.shift(offset - old);
206        }
207
208        slices = newSlices;
209        updateLength();
210        script.update();
211    }
212    /**
213     * shifts all timeslices over by timeAdd amount starting at offset
214     * @param offset amount of time preceding where we want to move all of the following events.
215     * @param timeAdd amount of time added after offset
216     */
217    public void moveAllFollowingEvents(int offset, int timeAdd)
218    {
219        //int old = this.offset;
220        TreeMap<Integer, TimeSlice> newSlices = new TreeMap<Integer, TimeSlice>();
221
222       
223
224        for (Integer k : slices.keySet())
225        {
226            if(k<offset)
227            {
228                newSlices.put(k, slices.get(k));
229            }else
230            {
231                if(k+timeAdd >0)
232                {
233                    newSlices.put(k+timeAdd, slices.get(k));
234                }else
235                {
236                    newSlices.put(0, slices.get(k));
237                }
238               
239            }
240           
241           
242        }
243        for (TimeSlice ts : newSlices.values())
244        {
245            if(ts.getTime()>offset )
246            {
247                if( ts.getTime()+timeAdd>=0)
248                {
249                    ts.shift(timeAdd);
250                }
251                else
252                {
253                    ts.shift(-ts.getTime());
254                }
255               
256            }
257           
258        }
259        latestStart = (latestStart + timeAdd>0) ? latestStart + timeAdd: 0;
260        slices = newSlices;
261        updateLength();
262        script.update();
263    }
264    /**
265     * Adds an event to the correct timeslice when imported from an XML file.
266     * @param ev Event to be added to the timeslice.
267     * @param start Time when event needs to be added.
268     */
269    public void addNewEventFromXML(I_ScriptEvent ev, int start)
270    {
271        TimeSlice t = slices.get(start);
272        if(t==null)
273        {
274            t = new TimeSlice(start,this);
275            slices.put(start,t); 
276        }
277        t.addEvent(ev);
278        eventCount++;
279
280        //If this is the latest start time in the incident, update that number
281        if (start > latestStart)
282        {
283            latestStart = start;
284        }
285        //If this event is earlier than all previous events, or if there are
286        //no other events, the offset is equal to this event's start time
287        if (start < offset || eventCount == 1)
288        {
289            offset = start;
290            //System.out.println("Offset: " + offset);
291        }
292        updateLength();
293       
294    }
295    /**
296     * Add a new script event to this incident.
297     *
298     * @param ev The new event
299     * @param start Start time of this event, in seconds, from the beginning of
300     * the simulation
301     */
302    public void addNewEvent(I_ScriptEvent ev, int start)
303    {
304       
305       
306        //Check to see if there's already a timeslice here
307        TimeSlice t = slices.get(start);
308        //logic to check if the added event needs to also have an associated audio event.
309       
310        //If not, make one; then, add the event to it
311        if (t == null)
312        {
313            t = new TimeSlice(start, this);
314            slices.put(start, t);
315        }
316       
317        //checks if event to add is an I_AudioEvent and if the I_AudioEvent already has an AudioEvent to connect with it
318        if(ev instanceof I_AudioEvent){
319           
320           
321            //checks to see if there is already a filename in the I_AudioEvent, if so, set the AudioEvent ID. This is because the ID is not stored within the Script itself.
322            if(!((I_AudioEvent) ev).getFileName().equals(""))
323            {
324                ((I_AudioEvent) ev).setID(((I_AudioEvent) ev).getFileName().replaceAll(".mp3",""));
325            }
326            // checks to see if the I_AudioEvent already has an ID or not, if not, set one based on the audioEventCount.
327            if(((I_AudioEvent) ev).getID().equals(""))
328            {
329                ((I_AudioEvent) ev).setID(number+Integer.toString(audioEventCount));
330                audioEventCount++;
331            }
332            if(t.getCorrespondingAudioEvent((I_AudioEvent)ev)==null)
333            {
334                AudioEvent audio = (AudioEvent) ScriptEvent.factoryByType(ScriptEventType.AUDIO_EVENT);
335
336                //sets AudioEvent id to id of I_AudioEvent
337                audio.id = ((I_AudioEvent) ev).getID();
338                //sets I_AudioEvent filename to the AudioEvent id
339                ((I_AudioEvent) ev).setFileName(audio.id+ ".mp3");
340                //sets the file path of the AudioEvent based on its internal id
341                audio.setAudioFilePathRelative();
342                //adds AudioEvent to the timeslice
343                t.addEvent(audio);
344            }
345           
346           
347        }
348        t.addEvent(ev);
349        eventCount++;
350
351        //If this is the latest start time in the incident, update that number
352        if (start > latestStart)
353        {
354            latestStart = start;
355        }
356        //If this event is earlier than all previous events, or if there are
357        //no other events, the offset is equal to this event's start time
358        if (start < offset || eventCount == 1)
359        {
360            offset = start;
361            //System.out.println("Offset: " + offset);
362        }
363        updateLength();
364    }
365
366    /**
367     * Get an array of all valid timeSlices.
368     *
369     * @return List of timeSlices which are not null
370     */
371    public ArrayList<TimeSlice> getSlices()
372    {
373        ArrayList<TimeSlice> arr = new ArrayList<TimeSlice>();
374        for (int i = 0; i <= latestStart; i++)
375        {
376            TimeSlice ts = slices.get(i);
377            if (ts != null)
378            {
379                arr.add(ts);
380            }
381        }
382        return arr;
383    }
384
385    /**
386     * Write this incident, in proper XML form, to the file in question.
387     *
388     * @param f the destination savefile
389     */
390    public void saveIncidentToFile(File f)
391    {
392        try
393        {
394            f.createNewFile();
395
396            BufferedWriter bw = new BufferedWriter(new FileWriter(f));
397            bw.write(this.toXML());
398            bw.flush();
399            bw.close();
400
401        }
402        catch (Exception ex)
403        {
404            System.out.println("ERROR SAVING SCRIPT");
405            ex.printStackTrace();
406        }
407    }
408
409    @Override
410    public String toXML()
411    {
412        String output = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
413        output += XMLWriter.internalDTD();
414        output += XMLWriter.openTag(ELEMENT.TMC_SCRIPT.tag + " title=\"" + this.script.title + "\"");
415
416        for (TimeSlice slice : slices.values())
417        {
418            output += slice.toXML();
419        }
420        output += XMLWriter.closeTag(ELEMENT.TMC_SCRIPT.tag);
421        return output;
422    }
423
424    public void insertCadData(long currentEventTime, CadData cad)
425    {
426        int time = (int) currentEventTime;
427
428        TimeSlice slice;
429
430        if (slices.get(time) == null)
431        {
432            slices.put(time, new TimeSlice(time, this));
433        }
434        slice = slices.get(time);
435        slice.cadData = cad;
436    }
437   
438    public CadData getCadData(long currentEventTime)
439    {
440        int time = (int) currentEventTime;
441        TimeSlice slice;
442       
443        slice = slices.get(time);
444        return slice.cadData;
445    }
446
447    /**
448     * Update the offset and apparent length of this incident. The offset is the
449     * start time of the earliest event in the incident. The length is the time
450     * that the latest, longest-lasting event ends, minus the offset.
451     */
452    public void updateLength()
453    {
454        int lengthSoFar = 0;
455        for (int i = 0; i <= latestStart; i++)
456        {
457            TimeSlice ts = slices.get(i);
458            if (ts != null)
459            {
460                int reach = ts.getTime() + ts.getEffectiveDuration() - offset;
461                if (reach > lengthSoFar)
462                {
463                    lengthSoFar = reach;
464                }
465            }
466        }
467        length = lengthSoFar;
468    }
469
470    /**
471     * An event which is fired if the focused slice changes.
472     */
473    public static class SliceChangedEvent
474    {
475
476        /**
477         * The slice which has received focus in this event.
478         */
479        public TimeSlice slice;
480
481        SliceChangedEvent(TimeSlice slice)
482        {
483            this.slice = slice;
484        }
485    }
486
487    /**
488     * Update and cause the system to focus on the given timeslice.
489     *
490     * @param i Index of the slice to focus on
491     */
492    public void setSliceActive(int i)
493    {
494        if (this.slices.get(i) != null)
495        {
496            script.broadcastEvent(new SliceChangedEvent(this.slices.get(i)));
497        }
498    }
499
500    /**
501     * An event which is fired if the focused incident changes.
502     */
503    public static class IncidentFocusedEvent
504    {
505
506        /**
507         * The incident which has received focus in this event.
508         */
509        public ScriptIncident incident;
510
511        IncidentFocusedEvent(ScriptIncident i)
512        {
513            incident = i;
514        }
515    }
516
517    /**
518     * Update and cause the system to focus on this incident.
519     */
520    public void setIncidentActive()
521    {
522        script.broadcastEvent(new IncidentFocusedEvent(this));
523    }
524
525    /**
526     * String representation of this incident.
527     *
528     * @return String of the form "[Incident number] - [Incident name]"
529     */
530    @Override
531    public String toString()
532    {
533        return this.number + " - " + this.name;
534    }
535
536    /**
537     * Remove the timeslice at the given position.
538     *
539     * @param seconds absolute start time of the timeslice to remove.
540     */
541    public void removeTimeSlice(int seconds)
542    {
543        //remove the timeslice
544        this.slices.remove(seconds);
545
546        //offset is equivalent to start time of earliest slice
547        if (seconds == offset)
548        {
549            if (slices.size() > 0)
550            {
551                offset = slices.firstKey();
552            }
553            else
554            {
555                offset = 0;
556            }
557        }
558        this.updateLength();
559        script.update();
560    }
561
562    /**
563     * Shifts the start time of a given event in this incident. The first event
564     * in the incident cannot be shifted.
565     *
566     * @param evt the event to be relocated
567     * @param oldTime the current start time of the event
568     * @param newTime the destination start time of the event
569     * @return true if the event is shifted over properly
570     * @pre the event in question exists
571     * @pre oldTime != newTime
572     */
573    public boolean changeEventStart(I_ScriptEvent evt, int oldTime, int newTime)
574    {
575        if (!(oldTime == 0 && newTime > oldTime))
576        {
577            if(evt instanceof I_AudioEvent)
578            {
579                //todo: need to figure out how to remove the audioEvent tha corresponds with the
580                this.slices.get(oldTime).removeCorrespondingAudioEvent((I_AudioEvent)evt);
581                //this.addNewEvent(evt,newTime)
582            }
583            this.slices.get(oldTime).events.remove(evt);
584            this.addNewEvent(evt, newTime);
585            return true;
586        }
587        return false;
588    }
589}
Note: See TracBrowser for help on using the repository browser.