Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Infographic
- // Author: Sukrit Tanticharoenkiat
- //=======================================================================================================================
- // Global Variables
- //=======================================================================================================================
- XML xXML; //Global XML database
- XML[] tCurrentSequence, tActions;
- PImage pBackground;
- boolean bIsThreadActive = false;
- boolean bIsDraw = false;
- String sImageName = "Default.png";
- import processing.net.*;
- Server s;
- Client c;
- XML xAction;
- Minim pMinim;
- String sCurrentAction = "";
- XML[] tAccessibleActions;
- //--------------------------------------------------------
- // Sound Libraries: Minim
- //--------------------------------------------------------
- import ddf.minim.spi.*;
- import ddf.minim.signals.*;
- import ddf.minim.*;
- import ddf.minim.analysis.*;
- import ddf.minim.ugens.*;
- import ddf.minim.effects.*;
- //--------------------------------------------------------
- // Image Class
- //--------------------------------------------------------
- class Image{
- PImage Image;
- int X = 0;
- int Y = 0;
- int Time = -999;
- Image(PImage pImage, Integer iX, Integer iY, Integer iTime){
- Image = pImage;
- X = iX;
- Y = iY;
- Time = iTime;
- }
- void Draw() {
- pushStyle();
- imageMode(CENTER);
- image(Image, X, Y, Image.width, Image.height);
- popStyle();
- if (frameCount % 60 == 0 && Time != -999){
- Time--;
- }
- }
- }
- ArrayList<Image> tImages = new ArrayList<Image>(0);
- //--------------------------------------------------------
- // Dot Class
- //--------------------------------------------------------
- class Dot{
- int X, Y, Opacity;
- Dot (int iX, int iY) {
- this.X = iX;
- this.Y = iY;
- this.Opacity = 255;
- }
- void Draw(){
- //println("X: "+X);
- //Opacity -= 5;
- fill(255, 255, 255, Opacity);
- ellipse(X, Y, 20, 20);
- }
- void DrawPG(PGraphics pg){
- //println("X: "+X);
- //Opacity -= 5;
- pg.noStroke();
- pg.fill(255, 255, 255, Opacity);
- pg.ellipse(X, Y, 20, 20);
- }
- }
- Dot[] tToDraw = new Dot[0];
- //--------------------------------------------------------
- // Colors
- //--------------------------------------------------------
- color cBackground = 25;
- //=======================================================================================================================
- // Core Functions
- //=======================================================================================================================
- // Setup
- //--------------------------------------------------------
- void setup() {
- size(800, 600);
- s = new Server(this, 12345); // Start a simple server on a port
- println(s.ip());
- pMinim = new Minim(this);
- xXML = loadXML("Actions.XML");
- // Fill Applicable Tables
- tActions = xXML.getChildren("Action");
- }
- //--------------------------------------------------------
- // Draw
- //--------------------------------------------------------
- void draw() {
- c = s.available();
- if (c != null){
- byte[] input = c.readBytes();
- String sLocalString = new String(input, java.nio.charset.Charset.forName("UTF-8"));
- while (sLocalString.indexOf("\n") > -1) {
- String sMessage = sLocalString.substring(0, sLocalString.indexOf("\n"));
- sLocalString = sLocalString.substring(sLocalString.indexOf("\n") + 1);
- if (bIsDraw && (sMessage.indexOf("X_") > -1) && (sMessage.indexOf("Y_") > -1)){
- println("sMessage: "+sMessage);
- String sX = sMessage.substring(sMessage.indexOf("X_") + 2, sMessage.indexOf("Y_"));
- String sY = sMessage.substring(sMessage.indexOf("Y_") + 2);
- //println(sX, sY);
- if (sY.indexOf("X_") < 0){
- int iX = round(Float.parseFloat(sX) * width / 320);
- int iY = round(Float.parseFloat(sY) * height / 240);
- tToDraw = (Dot[]) append(tToDraw, new Dot(iX, iY));
- }
- } else if (!bIsThreadActive && GetAction(sMessage) != null){
- bIsThreadActive = true;
- xAction = GetAction(sMessage);
- println("Start Thread");
- thread("DoSequence");
- }
- }
- }
- background(cBackground);
- if (pBackground != null){
- image(pBackground, 0, 0);
- }
- for (int i = tImages.size() - 1; i >= 0; i--) {
- Image pImage = tImages.get(i);
- pImage.Draw();
- if (pImage.Time < 0 && pImage.Time != -999){
- tImages.remove(i);
- }
- }
- pushStyle();
- stroke(255);
- noFill();
- beginShape();
- for (int i = tToDraw.length -1; i > -1; i--){
- // if (i == 0){
- // if (iAvgNum > 0){
- // Dot pDot = new Dot(iAverageX, iAverageY);
- // tToDraw[i] = pDot;
- // }
- // } else {
- // tToDraw[i] = tToDraw [i-1];
- if (tToDraw[i] == null){continue;}
- vertex(tToDraw[i].X, tToDraw[i].Y);
- // tToDraw[i].Draw();
- // }
- }
- endShape();
- popStyle();
- //text(sString, width/2, height/2);
- }
- //--------------------------------------------------------
- // DoSequence
- //--------------------------------------------------------
- void DoSequence(){
- tImages = new ArrayList<Image>(0);
- XML xSequence = xAction.getChild("Sequence");
- tCurrentSequence = xSequence.getChildren();
- for (int iNum = 0; iNum < tCurrentSequence.length; iNum++) {
- XML xChild = tCurrentSequence[iNum];
- String sName = xChild.getName();
- if (sName == "#text"){
- //Do Nothing
- } else if (sName == "Delay") {
- delay(xChild.getIntContent());
- } else if (sName == "Background") {
- pBackground = loadImage(xChild.getContent());
- } else if (sName == "Sound") {
- AudioSample pSample;
- pSample = pMinim.loadSample(xChild.getContent(), 512);
- pSample.trigger();
- } else if (sName == "Image") {
- PImage pImage = loadImage(GetElement_S(xChild, "Name"));
- Integer iX = GetElement_I(xChild, "X");
- Integer iY = GetElement_I(xChild, "Y");
- Integer iTime = GetElement_I(xChild, "Time");
- if(iX == null){iX = width/2;}
- if(iY == null){iY = height/2;}
- //if(iTime == null){iTime = -999;}
- Image pNewImage = new Image(pImage, iX, iY, -999);
- tImages.add(pNewImage);
- } else if (sName == "BeginDraw") {
- sImageName = xChild.getContent();
- bIsDraw = true;
- } else if (sName == "EndDraw") {
- SaveImage();
- }
- }
- xAction = null;
- bIsThreadActive = false;
- println("Thread End");
- }
- //=======================================================================================================================
- // Utility Functions
- //=======================================================================================================================
- // Additional XML functions
- // Allows easy retrieval and adjustment of XML Elements
- //--------------------------------------------------------
- String GetElement_S(XML xXML, String sID) {
- if (xXML.getChild(sID) != null) {
- return xXML.getChild(sID).getContent();
- }
- return "";
- }
- Integer GetElement_I(XML xXML, String sID) {
- if (xXML.getChild(sID) != null) {
- return xXML.getChild(sID).getIntContent();
- }
- return null;
- }
- void SetElement(XML xXML, String sID, String sVal) {
- if (xXML.getChild(sID) == null){
- xXML.addChild(sID);
- }
- xXML.getChild(sID).setContent(sVal);
- }
- //--------------------------------------------------------
- // GetAction
- // Retrieves an XML language based off a string
- //--------------------------------------------------------
- XML GetAction(String sName) {
- //println("sName: "+sName);\
- for (int iNum = 0; iNum < tActions.length; iNum++) {
- XML xAction = tActions[iNum];
- String sName2 = GetElement_S(xAction, "ID");
- if (sName.toUpperCase().equals(sName2.toUpperCase())) {
- return xAction;
- }
- }
- return null;
- }
- //--------------------------------------------------------
- // SaveImage
- // Saves Drawn Item As Image
- //--------------------------------------------------------
- void SaveImage(){
- bIsDraw = false;
- PGraphics pImageToSave = createGraphics(width, height);
- pImageToSave.beginDraw();
- pImageToSave.pushStyle();
- pImageToSave.stroke(255);
- pImageToSave.noFill();
- pImageToSave.beginShape();
- for (int i = tToDraw.length -1; i > -1; i--){
- if (tToDraw[i] == null){continue;}
- pImageToSave.vertex(tToDraw[i].X, tToDraw[i].Y);
- }
- pImageToSave.endShape();
- pImageToSave.popStyle();
- pImageToSave.endDraw();
- pImageToSave.save(sImageName);
- tToDraw = new Dot[0];
- sImageName = "Default.png";
- }
- //=======================================================================================================================
- // Utility Functions
- //=======================================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement