import proxml.*; /** * XML Pulses * *

Elie Zananiri
* CART 253, Winter 2008

*/ // ---------------------------------------------------------------------- // GLOBAL CONSTANTS // ---------------------------------------------------------------------- int MAX_PULSES = 500; // ---------------------------------------------------------------------- // GLOBAL VARIABLES // ---------------------------------------------------------------------- int numPulses = 0; Pulse pulses[] = new Pulse[MAX_PULSES]; XMLInOut xmlIO; XMLElement xmlPulses; // ---------------------------------------------------------------------- // BUILT-IN FUNCTIONS // ---------------------------------------------------------------------- void setup() { size(400, 400); smooth(); noStroke(); // load pulses from XML file, if it exists xmlIO = new XMLInOut(this); try { xmlIO.loadElement("pulses.xml"); } catch (Exception e) { // the XML file could not be found, create a new XML root xmlEvent(new XMLElement("pulses")); } } /* called automatically whenever an XML file is loaded */ void xmlEvent(XMLElement element) { xmlPulses = element; initPulses(); } void draw() { background(0); // draw all the pulses for (int i=0; i < numPulses; i++) { pulses[i].draw(); } } void mousePressed() { addNewPulse(mouseX, mouseY); } void mouseDragged() { addNewPulse(mouseX, mouseY); } void mouseReleased() { savePulsesToDisk(); } void keyPressed() { if (key == ' ') { // clear all clearPulses(); } } // ---------------------------------------------------------------------- // USER FUNCTIONS // ---------------------------------------------------------------------- /* creates all pulses saved in the XML file */ void initPulses() { // create temporary XML nodes XMLElement pulse; XMLElement pos; XMLElement col; // parse through all pulse nodes to create Pulse objects for (int i=0; i < xmlPulses.countChildren(); i++) { pulse = xmlPulses.getChild(i); pos = pulse.getChild(0); col = pulse.getChild(1); addSavedPulse( pos.getIntAttribute("x"), pos.getIntAttribute("y"), col.getIntAttribute("rgb"), i ); } } /* adds a new pulse to the display and XML lists */ void addNewPulse(int newX, int newY) { if (numPulses < MAX_PULSES) { pulses[numPulses] = new Pulse(newX, newY, color(random(255), random(255), random(255))); // create a new pulse XML node XMLElement newPulse = new XMLElement("pulse"); // add it to the XML root xmlPulses.addChild(newPulse); // add a position XML node to the new pulse XMLElement pos = new XMLElement("position"); pos.addAttribute("x", newX); pos.addAttribute("y", newY); newPulse.addChild(pos); // add a color XML node to the new pulse XMLElement col = new XMLElement("color"); col.addAttribute("rgb", pulses[numPulses].c); newPulse.addChild(col); numPulses++; } } /* adds a saved pulse to the display list */ void addSavedPulse(int newX, int newY, color newCol, int index) { if (numPulses < MAX_PULSES) { pulses[numPulses] = new Pulse(newX, newY, newCol); pulses[numPulses].s += 0.1*index; numPulses++; } } /* clears all pulses from the display and XML lists */ void clearPulses() { numPulses = 0; // create a new empty pulses XML list to overwrite the previous one xmlPulses = new XMLElement("pulses"); // save the new empty list to disk savePulsesToDisk(); } /* saves the pulses XML list to disk */ void savePulsesToDisk() { xmlIO.saveElement(xmlPulses, "pulses.xml"); }