Changeset 30 in tmcsimulator-scriptbuilder
- Timestamp:
- 08/02/2017 12:48:06 PM (9 years ago)
- Location:
- trunk/src/scriptbuilder/structures
- Files:
-
- 3 edited
-
ScriptIncident.java (modified) (2 diffs)
-
SimulationScript.java (modified) (7 diffs)
-
TimeSlice.java (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/scriptbuilder/structures/ScriptIncident.java
r24 r30 19 19 * @version 2017/06/29 20 20 */ 21 public class ScriptIncident 21 public class ScriptIncident implements I_XML_Writable 22 22 { 23 23 … … 212 212 } 213 213 return arr; 214 } 215 216 @Override 217 public String toXML() 218 { 219 String output = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"; 220 output += "<!DOCTYPE TMC_SCRIPT SYSTEM \"script.dtd\">"; 221 output += openTag(ELEMENT.TMC_SCRIPT.tag + " title=\"" + this.script.title + "\""); 222 223 ArrayList<TimeSlice> s = (ArrayList) slices.values(); 224 225 for (TimeSlice slice : s) 226 { 227 output += slice.toXML(); 228 } 229 output += closeTag(ELEMENT.TMC_SCRIPT.tag); 230 return output; 231 } 232 233 234 @Override 235 public String openTag(String s) 236 { 237 return "<" + s + ">\n"; 238 } 239 240 @Override 241 public String closeTag(String s) 242 { 243 return "</" + s + ">\n"; 244 } 245 246 @Override 247 public String emptyTag(String s) 248 { 249 return "<" + s + "/>\n"; 214 250 } 215 251 -
trunk/src/scriptbuilder/structures/SimulationScript.java
r24 r30 2 2 3 3 import java.awt.Color; 4 import java.io.BufferedWriter; 4 5 import java.io.File; 6 import java.io.FileWriter; 7 import java.io.OutputStream; 8 import java.io.OutputStreamWriter; 5 9 import java.util.ArrayList; 6 10 import java.util.List; … … 25 29 * @version 2017/06/22 26 30 */ 27 public class SimulationScript extends Observable 31 public class SimulationScript extends Observable implements I_XML_Writable 28 32 { 29 33 … … 45 49 }; 46 50 51 public String title = ""; 52 47 53 /** 48 54 * The incidents displayed by the GUI. … … 93 99 } 94 100 /* 95 //Add some demo events101 Add some demo events 96 102 incidents.add(new ScriptIncident(incidentColors[1], 174, "Blueberry Truck", 97 103 "Blueberry truck crashed on the freeway.", 8800, this)); … … 109 115 "Truck stalled on the freeway.", 2800, this)); 110 116 numberOfIncidents++; 111 //incidents.add(new ScriptIncident(incidentColors[6], 179, "Tomato Truck Spill",112 //"Tomato trucked spilt tomatos all over the freeway.", 3200, this));113 //incidents.add(new ScriptIncident(incidentColors[7], 180, "Crash at Intersection",114 //"Crash at the intersection of Tree Road and Red Road.", 4300, this));115 //incidents.add(new ScriptIncident(incidentColors[8], 181, "Bomb Threat",116 //"Bomb threat and Road X.", 6000, this));117 incidents.add(new ScriptIncident(incidentColors[6], 179, "Tomato Truck Spill", 118 "Tomato trucked spilt tomatos all over the freeway.", 3200, this)); 119 incidents.add(new ScriptIncident(incidentColors[7], 180, "Crash at Intersection", 120 "Crash at the intersection of Tree Road and Red Road.", 4300, this)); 121 incidents.add(new ScriptIncident(incidentColors[8], 181, "Bomb Threat", 122 "Bomb threat and Road X.", 6000, this)); 117 123 incidents.add(null); 118 124 incidents.add(null); … … 146 152 incidents.get(4).setOffset(4400); 147 153 incidents.get(5).setOffset(1400); 148 //incidents.get(6).setOffset(7600);149 //incidents.get(7).setOffset(2400);150 //incidents.get(8).setOffset(4800);154 incidents.get(6).setOffset(7600); 155 incidents.get(7).setOffset(2400); 156 incidents.get(8).setOffset(4800); 151 157 } 152 158 */ … … 217 223 this.update(); 218 224 } 225 226 public void saveScriptToFile(File f) 227 { 228 try 229 { 230 f.createNewFile(); 231 232 BufferedWriter bw = new BufferedWriter(new FileWriter(f)); 233 bw.write(this.toXML()); 234 bw.flush(); 235 bw.close(); 236 237 } 238 catch (Exception ex) 239 { 240 System.out.println("ERROR SAVING SCRIPT"); 241 ex.printStackTrace(); 242 } 243 } 244 245 @Override 246 public String toXML() 247 { 248 String output = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"; 249 output += "<!DOCTYPE TMC_SCRIPT SYSTEM \"script.dtd\">"; 250 ArrayList<TimeSlice> slices = allSlices(); 251 output += openTag(ELEMENT.TMC_SCRIPT.tag + " title=\"" + this.title + "\""); 252 for (TimeSlice slice : slices) 253 { 254 output += slice.toXML(); 255 } 256 output += closeTag(ELEMENT.TMC_SCRIPT.tag); 257 return output; 258 } 259 260 @Override 261 public String openTag(String s) 262 { 263 return "<" + s + ">\n"; 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 /** 279 * Arranges all timeslices in this script in chronological order, then by 280 * incident number. 281 * 282 * @return a list of all timeslices in the simulation script 283 */ 284 public ArrayList<TimeSlice> allSlices() 285 { 286 ArrayList<TimeSlice> list = new ArrayList<TimeSlice>(); 287 int length = absoluteLength(); 288 289 for (int i = 0; i < length; i++) 290 { 291 for (ScriptIncident inc : incidents) 292 { 293 294 if (inc != null && inc.slices.get(i) != null) 295 { 296 list.add(inc.slices.get(i)); 297 } 298 } 299 } 300 return list; 301 } 302 303 /** 304 * Gets the total length of the simulation in seconds. 305 * 306 * @return 307 */ 308 public int absoluteLength() 309 { 310 int length = 0; 311 for (ScriptIncident inc : incidents) 312 { 313 if (inc != null) 314 { 315 int currentLength = inc.length + inc.offset; 316 if (currentLength > length) 317 { 318 length = currentLength; 319 } 320 } 321 } 322 return length; 323 } 324 219 325 } -
trunk/src/scriptbuilder/structures/TimeSlice.java
r21 r30 8 8 import java.util.TimeZone; 9 9 import scriptbuilder.gui.ScriptBuilderGuiConstants; 10 import scriptbuilder.structures.events.*; 10 11 import scriptbuilder.structures.events.I_ScriptEvent; 11 12 … … 194 195 195 196 /** 196 * Converts the contents of this timeslice to a correctly 197 * formatted<ScriptEvent> XML element.197 * Converts the contents of this timeslice to a correctly formatted 198 * <ScriptEvent> XML element. 198 199 * 199 200 * @return XML conversion of this timeslice. … … 202 203 public String toXML() 203 204 { 205 ArrayList<I_ScriptEvent> eventsCopy = new ArrayList<I_ScriptEvent>(); 206 207 for (I_ScriptEvent e : events) 208 { 209 eventsCopy.add(e); 210 } 211 204 212 System.out.println("Seconds:: " + seconds); 205 213 String output = openTag(ELEMENT.SCRIPT_EVENT.tag); … … 212 220 output += thisIncident.name + closeTag(ELEMENT.INCIDENT.tag); 213 221 214 for (I_ScriptEvent ev : events) 222 output += openTag(ELEMENT.CAD_DATA.tag); 223 if (containsCADIncidentEvent()) 224 { 225 output += openTag(ELEMENT.CAD_INCIDENT_EVENT.tag); 226 for (I_ScriptEvent ev : eventsCopy) 227 { 228 if (ev instanceof I_XML_Writable && isCADIncidentEvent(ev)) 229 { 230 I_XML_Writable ex = (I_XML_Writable) ev; 231 output += ex.toXML(); 232 eventsCopy.remove(ev); 233 } 234 } 235 output += closeTag(ELEMENT.CAD_INCIDENT_EVENT.tag); 236 } 237 output += closeTag(ELEMENT.CAD_DATA.tag); 238 239 for (I_ScriptEvent ev : eventsCopy) 215 240 { 216 241 if (ev instanceof I_XML_Writable) … … 242 267 return "<" + s + "/>\n"; 243 268 } 269 270 private boolean containsCADIncidentEvent() 271 { 272 for (I_ScriptEvent ev : events) 273 { 274 if (isCADIncidentEvent(ev)) 275 { 276 return true; 277 } 278 } 279 return false; 280 } 281 282 private boolean isCADIncidentEvent(I_ScriptEvent ev) 283 { 284 return ev instanceof AudioEvent || ev instanceof UnitEvent 285 || ev instanceof ParamicsEvent || ev instanceof TowEvent 286 || ev instanceof WitnessEvent; 287 } 244 288 }
Note: See TracChangeset
for help on using the changeset viewer.
