Advertisement
Guest User

Untitled

a guest
May 26th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.44 KB | None | 0 0
  1. // Infographic
  2. // Author: Sukrit Tanticharoenkiat
  3. //=======================================================================================================================
  4. // Global Variables
  5. //=======================================================================================================================
  6. XML xXML; //Global XML database
  7. XML[] tCurrentSequence, tActions;
  8.  
  9. PImage pBackground;
  10.  
  11. boolean bIsThreadActive = false;
  12. boolean bIsDraw = false;
  13. String sImageName = "Default.png";
  14.  
  15. import processing.net.*;
  16.  
  17. Server s;
  18. Client c;
  19.  
  20. XML xAction;
  21.  
  22. Minim pMinim;
  23.  
  24. String sCurrentAction = "";
  25. XML[] tAccessibleActions;
  26. //--------------------------------------------------------
  27. // Sound Libraries: Minim
  28. //--------------------------------------------------------
  29. import ddf.minim.spi.*;
  30. import ddf.minim.signals.*;
  31. import ddf.minim.*;
  32. import ddf.minim.analysis.*;
  33. import ddf.minim.ugens.*;
  34. import ddf.minim.effects.*;
  35. //--------------------------------------------------------
  36. // Image Class
  37. //--------------------------------------------------------
  38. class Image{
  39.  
  40. PImage Image;
  41. int X = 0;
  42. int Y = 0;
  43. int Time = -999;
  44.  
  45. Image(PImage pImage, Integer iX, Integer iY, Integer iTime){
  46. Image = pImage;
  47. X = iX;
  48. Y = iY;
  49. Time = iTime;
  50. }
  51.  
  52. void Draw() {
  53. pushStyle();
  54. imageMode(CENTER);
  55. image(Image, X, Y, Image.width, Image.height);
  56. popStyle();
  57. if (frameCount % 60 == 0 && Time != -999){
  58. Time--;
  59. }
  60. }
  61.  
  62. }
  63.  
  64. ArrayList<Image> tImages = new ArrayList<Image>(0);
  65. //--------------------------------------------------------
  66. // Dot Class
  67. //--------------------------------------------------------
  68. class Dot{
  69.  
  70. int X, Y, Opacity;
  71.  
  72. Dot (int iX, int iY) {
  73. this.X = iX;
  74. this.Y = iY;
  75. this.Opacity = 255;
  76. }
  77.  
  78. void Draw(){
  79. //println("X: "+X);
  80. //Opacity -= 5;
  81. fill(255, 255, 255, Opacity);
  82. ellipse(X, Y, 20, 20);
  83. }
  84.  
  85. void DrawPG(PGraphics pg){
  86. //println("X: "+X);
  87. //Opacity -= 5;
  88. pg.noStroke();
  89. pg.fill(255, 255, 255, Opacity);
  90. pg.ellipse(X, Y, 20, 20);
  91. }
  92. }
  93.  
  94. Dot[] tToDraw = new Dot[0];
  95. //--------------------------------------------------------
  96. // Colors
  97. //--------------------------------------------------------
  98. color cBackground = 25;
  99. //=======================================================================================================================
  100. // Core Functions
  101. //=======================================================================================================================
  102. // Setup
  103. //--------------------------------------------------------
  104. void setup() {
  105. size(800, 600);
  106.  
  107. s = new Server(this, 12345); // Start a simple server on a port
  108. println(s.ip());
  109.  
  110. pMinim = new Minim(this);
  111.  
  112. xXML = loadXML("Actions.XML");
  113. // Fill Applicable Tables
  114. tActions = xXML.getChildren("Action");
  115. }
  116. //--------------------------------------------------------
  117. // Draw
  118. //--------------------------------------------------------
  119. void draw() {
  120.  
  121. c = s.available();
  122. if (c != null){
  123. byte[] input = c.readBytes();
  124. String sLocalString = new String(input, java.nio.charset.Charset.forName("UTF-8"));
  125.  
  126. while (sLocalString.indexOf("\n") > -1) {
  127.  
  128. String sMessage = sLocalString.substring(0, sLocalString.indexOf("\n"));
  129. sLocalString = sLocalString.substring(sLocalString.indexOf("\n") + 1);
  130.  
  131. if (bIsDraw && (sMessage.indexOf("X_") > -1) && (sMessage.indexOf("Y_") > -1)){
  132. println("sMessage: "+sMessage);
  133. String sX = sMessage.substring(sMessage.indexOf("X_") + 2, sMessage.indexOf("Y_"));
  134. String sY = sMessage.substring(sMessage.indexOf("Y_") + 2);
  135. //println(sX, sY);
  136.  
  137. if (sY.indexOf("X_") < 0){
  138.  
  139. int iX = round(Float.parseFloat(sX) * width / 320);
  140. int iY = round(Float.parseFloat(sY) * height / 240);
  141.  
  142. tToDraw = (Dot[]) append(tToDraw, new Dot(iX, iY));
  143. }
  144. } else if (!bIsThreadActive && GetAction(sMessage) != null){
  145. bIsThreadActive = true;
  146. xAction = GetAction(sMessage);
  147. println("Start Thread");
  148. thread("DoSequence");
  149. }
  150. }
  151. }
  152.  
  153. background(cBackground);
  154. if (pBackground != null){
  155. image(pBackground, 0, 0);
  156. }
  157.  
  158. for (int i = tImages.size() - 1; i >= 0; i--) {
  159. Image pImage = tImages.get(i);
  160. pImage.Draw();
  161. if (pImage.Time < 0 && pImage.Time != -999){
  162. tImages.remove(i);
  163. }
  164. }
  165.  
  166. pushStyle();
  167. stroke(255);
  168. noFill();
  169. beginShape();
  170. for (int i = tToDraw.length -1; i > -1; i--){
  171. // if (i == 0){
  172. // if (iAvgNum > 0){
  173. // Dot pDot = new Dot(iAverageX, iAverageY);
  174. // tToDraw[i] = pDot;
  175. // }
  176. // } else {
  177. // tToDraw[i] = tToDraw [i-1];
  178. if (tToDraw[i] == null){continue;}
  179. vertex(tToDraw[i].X, tToDraw[i].Y);
  180. // tToDraw[i].Draw();
  181. // }
  182. }
  183. endShape();
  184. popStyle();
  185.  
  186. //text(sString, width/2, height/2);
  187. }
  188. //--------------------------------------------------------
  189. // DoSequence
  190. //--------------------------------------------------------
  191. void DoSequence(){
  192. tImages = new ArrayList<Image>(0);
  193.  
  194. XML xSequence = xAction.getChild("Sequence");
  195. tCurrentSequence = xSequence.getChildren();
  196.  
  197. for (int iNum = 0; iNum < tCurrentSequence.length; iNum++) {
  198. XML xChild = tCurrentSequence[iNum];
  199. String sName = xChild.getName();
  200. if (sName == "#text"){
  201. //Do Nothing
  202. } else if (sName == "Delay") {
  203. delay(xChild.getIntContent());
  204. } else if (sName == "Background") {
  205. pBackground = loadImage(xChild.getContent());
  206. } else if (sName == "Sound") {
  207. AudioSample pSample;
  208. pSample = pMinim.loadSample(xChild.getContent(), 512);
  209. pSample.trigger();
  210. } else if (sName == "Image") {
  211. PImage pImage = loadImage(GetElement_S(xChild, "Name"));
  212. Integer iX = GetElement_I(xChild, "X");
  213. Integer iY = GetElement_I(xChild, "Y");
  214. Integer iTime = GetElement_I(xChild, "Time");
  215.  
  216. if(iX == null){iX = width/2;}
  217. if(iY == null){iY = height/2;}
  218. //if(iTime == null){iTime = -999;}
  219.  
  220. Image pNewImage = new Image(pImage, iX, iY, -999);
  221. tImages.add(pNewImage);
  222. } else if (sName == "BeginDraw") {
  223. sImageName = xChild.getContent();
  224. bIsDraw = true;
  225. } else if (sName == "EndDraw") {
  226. SaveImage();
  227. }
  228. }
  229.  
  230. xAction = null;
  231. bIsThreadActive = false;
  232. println("Thread End");
  233. }
  234. //=======================================================================================================================
  235. // Utility Functions
  236. //=======================================================================================================================
  237. // Additional XML functions
  238. // Allows easy retrieval and adjustment of XML Elements
  239. //--------------------------------------------------------
  240. String GetElement_S(XML xXML, String sID) {
  241. if (xXML.getChild(sID) != null) {
  242. return xXML.getChild(sID).getContent();
  243. }
  244. return "";
  245. }
  246.  
  247. Integer GetElement_I(XML xXML, String sID) {
  248. if (xXML.getChild(sID) != null) {
  249. return xXML.getChild(sID).getIntContent();
  250. }
  251. return null;
  252. }
  253.  
  254. void SetElement(XML xXML, String sID, String sVal) {
  255. if (xXML.getChild(sID) == null){
  256. xXML.addChild(sID);
  257. }
  258. xXML.getChild(sID).setContent(sVal);
  259. }
  260. //--------------------------------------------------------
  261. // GetAction
  262. // Retrieves an XML language based off a string
  263. //--------------------------------------------------------
  264. XML GetAction(String sName) {
  265. //println("sName: "+sName);\
  266. for (int iNum = 0; iNum < tActions.length; iNum++) {
  267. XML xAction = tActions[iNum];
  268. String sName2 = GetElement_S(xAction, "ID");
  269. if (sName.toUpperCase().equals(sName2.toUpperCase())) {
  270. return xAction;
  271. }
  272. }
  273.  
  274. return null;
  275. }
  276. //--------------------------------------------------------
  277. // SaveImage
  278. // Saves Drawn Item As Image
  279. //--------------------------------------------------------
  280. void SaveImage(){
  281. bIsDraw = false;
  282. PGraphics pImageToSave = createGraphics(width, height);
  283.  
  284. pImageToSave.beginDraw();
  285. pImageToSave.pushStyle();
  286. pImageToSave.stroke(255);
  287. pImageToSave.noFill();
  288. pImageToSave.beginShape();
  289. for (int i = tToDraw.length -1; i > -1; i--){
  290. if (tToDraw[i] == null){continue;}
  291. pImageToSave.vertex(tToDraw[i].X, tToDraw[i].Y);
  292. }
  293. pImageToSave.endShape();
  294. pImageToSave.popStyle();
  295. pImageToSave.endDraw();
  296. pImageToSave.save(sImageName);
  297.  
  298. tToDraw = new Dot[0];
  299. sImageName = "Default.png";
  300. }
  301. //=======================================================================================================================
  302. // Utility Functions
  303. //=======================================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement