compositor for 2d glitch effects
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

secondapplet.pde 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. //contains the GUI (ie CP5) in the secondapplet. also contains 3 classes: shaderManager, Brick and Slider
  2. PImage bin;
  3. public class SecondApplet extends PApplet {
  4. ControlP5 cp5;
  5. shaderManager shaderManager = new shaderManager();
  6. ArrayList<Shader> shaderList = new ArrayList<Shader>();
  7. ArrayList<Brick> bricks = new ArrayList<Brick>();
  8. ArrayList<Slider> slider = new ArrayList<Slider>(); //brick
  9. int binX, binY, binS;
  10. public void settings() {
  11. size(275, 120);
  12. }
  13. public void setup() {
  14. surface.setLocation(285, 240);
  15. List l = Arrays.asList(availableFx);
  16. cp5 = new ControlP5(this);
  17. cp5.addScrollableList("")
  18. .setPosition(10, height-110)
  19. .setSize(80, 100)
  20. .setBarHeight(0)
  21. .setItemHeight(20)
  22. .plugTo(this, "CP5_listActions")
  23. .addItems(l)
  24. .setType(ControlP5.LIST);
  25. Toggle rec = cp5.addToggle("record")
  26. .setPosition(100, height-105)
  27. .setSize(15, 15)
  28. .plugTo(this, "CP5_recordAction");
  29. rec.getCaptionLabel().getStyle().setMargin(-17, 0, 0, 20);
  30. Toggle auto = cp5.addToggle("spawn playing")
  31. .setPosition(100, height-85)
  32. .setSize(15, 15)
  33. .setValue(true)
  34. .plugTo(this, "CP5_spawnPlayingAction");
  35. auto.getCaptionLabel().getStyle().setMargin(-17, 0, 0, 20);
  36. Bang pauseAll = cp5.addBang("pause all")
  37. .setPosition(100, height-65)
  38. .setSize(15, 15)
  39. .plugTo(this, "CP5_pauseAllAction");
  40. pauseAll.getCaptionLabel().getStyle().setMargin(-17, 0, 0, 20);
  41. /*
  42. Toggle saveParams = cp5.addToggle("save parameters")
  43. .setPosition(100, height-45)
  44. .setSize(15, 15)
  45. .plugTo(this, "CP5_saveParamsAction");
  46. saveParams.getCaptionLabel().getStyle().setMargin(-17,0,0,20);
  47. */
  48. binX = width-55;
  49. binY = height-85;
  50. binS = 40;
  51. }
  52. void CP5_recordAction() {
  53. recording = !recording;
  54. }
  55. void CP5_spawnPlayingAction() {
  56. spawnPlaying = !spawnPlaying;
  57. }
  58. void CP5_pauseAllAction() {
  59. for (int i = 0; i < bricks.size(); i++) {
  60. if (!bricks.get(i).paused) bricks.get(i).playPause();
  61. }
  62. }
  63. int totalBrickHeight;
  64. public void draw() {
  65. background(68, 68, 68);
  66. image(bin, binX, binY, binS, binS);
  67. stroke(255);
  68. strokeWeight(1);
  69. textSize(25);
  70. fill(255);
  71. text(renderer.width + "x" + renderer.height, 100, height-18);
  72. textSize(10);
  73. line(0, height-120, width, height-120);
  74. totalBrickHeight = 0;
  75. for (int i = 0; i < shaderList.size(); i++) {
  76. bricks.get(i).update();
  77. bricks.get(i).display();
  78. bricks.get(i).y = totalBrickHeight;
  79. totalBrickHeight+= bricks.get(i).h;
  80. }
  81. if (pressedBrick != -1) {
  82. fill(255);
  83. noStroke();
  84. rect(mouseX, mouseY, 35, 15);
  85. }
  86. }
  87. void refreshSurface(int mode) {
  88. if (shaderList.size() > 0) {
  89. if (mode == 0) surface.setSize(width, height+bricks.get(shaderList.size()-1).h);
  90. else if (mode == 1) surface.setSize(width, height-bricks.get(pressedBrick).h);
  91. } else {
  92. surface.setSize(width, 120);
  93. }
  94. cp5.setPosition(0, height-120);
  95. binY = height-87;
  96. }
  97. class shaderManager {
  98. void addShader(Shader shader) {
  99. shaderList.add(shader);
  100. shader.pos = shaderList.size()-1;
  101. bricks.add(new Brick(shaderList.size()-1, shaderList.get(shaderList.size()-1).params, shader.name));
  102. refreshSurface(0);
  103. //if (!spawnPlaying) bricks.get(shaderList.size()-1).pauseAllSliders();
  104. println("Added " + shader + " to fxList at spot. List length: " + shaderList.size());
  105. }
  106. void swapShader(int pos1, int pos2) {
  107. Collections.swap(shaderList, pos1, pos2);
  108. Collections.swap(bricks, pos1, pos2);
  109. shaderList.get(pos1).pos = pos1;
  110. shaderList.get(pos2).pos = pos2;
  111. bricks.get(pos1).setPos(pos1);
  112. bricks.get(pos2).setPos(pos2);
  113. for (int i = 0; i < bricks.size(); i++) {
  114. bricks.get(i).updateSliderPos();
  115. }
  116. }
  117. void moveShader(int pos1, int pos2) {
  118. if (pos2 > pos1) {
  119. pos2 -=1;
  120. for (int i = pos1; i < pos2; i++) {
  121. swapShader(i, i+1);
  122. }
  123. } else {
  124. for (int i = pos1; i > pos2; i--) {
  125. swapShader(i, i-1);
  126. }
  127. }
  128. }
  129. void removeShader(int pos) {
  130. if (pos >= 0 && shaderList.size() > pos) {
  131. refreshSurface(1);
  132. shaderList.remove(pos);
  133. bricks.remove(pos);
  134. for (int i = shaderList.size()-1; i > -1; i--) {
  135. bricks.get(i).setPos(i);
  136. bricks.get(i).updateSliderPos();
  137. shaderList.get(i).pos = i;
  138. }
  139. }
  140. }
  141. void applyShader() {
  142. for (int i = 0; i < shaderList.size(); i++) {
  143. try {
  144. if (bricks.get(i).active) {
  145. shaderList.get(i).getValuesFromGUI();
  146. shaderList.get(i).apply();
  147. }
  148. }
  149. catch(Exception e) {
  150. println("Warning: Brick not yet loaded");
  151. }
  152. if (i > maxFx) break;
  153. }
  154. }
  155. }
  156. int pressedBrick = -1;
  157. class Brick {
  158. ArrayList<Slider> slider = new ArrayList<Slider>();
  159. ArrayList<Param> params = new ArrayList<Param>();
  160. boolean active, paused, dragging, dragHovering, hovering, hoveringLineAbove, hoveringLineBelow, hoverActiveButton, hoverPauseButton;
  161. boolean[] pauseStates;
  162. color brickBGcol;
  163. int pos;
  164. int h, y;
  165. String name;
  166. Brick(int pos_, ArrayList params_, String name_) {
  167. name = name_;
  168. pos = pos_;
  169. params = params_;
  170. active = true;
  171. y = totalBrickHeight;
  172. h = 20 + params.size() * 20;
  173. for (int i = 0; i < params.size(); i++) {
  174. slider.add(new Slider(10, 20+(20*i)+y, 230, 12, params.get(i).minValue, params.get(i).maxValue, params.get(i).name, params.get(i).type, params.get(i).osciModes)); //add possible waves to slider
  175. }
  176. pauseStates = new boolean[slider.size()];
  177. for (int i = 0; i < pauseStates.length; i++) {
  178. pauseStates[i] = false;
  179. }
  180. if (!spawnPlaying) {
  181. playPause();
  182. }
  183. }
  184. void setPos(int pos_) {
  185. pos = pos_;
  186. }
  187. void updateSliderPos() {
  188. y = 0;
  189. for (int i = 0; i < pos; i++) {
  190. y += bricks.get(i).h;
  191. }
  192. for (int i = 0; i < slider.size(); i++) {
  193. slider.get(i).y = 20+(20*i)+y;
  194. }
  195. }
  196. void exchangeSlider(int sliderNo, Param param) {
  197. slider.get(sliderNo).setMinMax(param.minValue, param.maxValue);
  198. slider.get(sliderNo).setMode(param.type);
  199. slider.get(sliderNo).setLabel(param.name);
  200. }
  201. void playPause() {
  202. paused = !paused;
  203. if (paused) {
  204. pauseAllSliders();
  205. } else {
  206. resumeAllSliders();
  207. }
  208. }
  209. void pauseAllSliders() {
  210. for (int i = 0; i < slider.size(); i++) {
  211. pauseStates[i] = slider.get(i).animated;
  212. slider.get(i).animated = false;
  213. }
  214. }
  215. void resumeAllSliders() {
  216. for (int i = 0; i < slider.size(); i++) {
  217. slider.get(i).animated = pauseStates[i];
  218. }
  219. }
  220. void update() {
  221. boolean sliderHover = false;
  222. for (int i = 0; i < slider.size(); i++) {
  223. if (slider.get(i).hovering || slider.get(i).hovering_anim_btn) sliderHover = true;
  224. if (paused && slider.get(i).animated) paused = false;
  225. }
  226. if (mouseX > 10 && mouseX < 22 && mouseY > 4+totalBrickHeight && mouseY < 4+totalBrickHeight+13) {
  227. hoverActiveButton = true;
  228. } else {
  229. hoverActiveButton = false;
  230. }
  231. if (mouseX > 25 && mouseX < 37 && mouseY > 4+totalBrickHeight && mouseY < 4+totalBrickHeight+13) {
  232. hoverPauseButton = true;
  233. } else {
  234. hoverPauseButton = false;
  235. }
  236. if (mouseY > y-5 && mouseY < y+5 && pressedBrick != -1) {
  237. hoveringLineAbove = true;
  238. if (pos > 0) {
  239. bricks.get(pos-1).hovering = false;
  240. }
  241. } else if (pos == bricks.size()-1 && mouseY > y+h-5 && mouseY < y+h+5 && pressedBrick != -1) {
  242. hoveringLineBelow = true;
  243. } else {
  244. hoveringLineBelow = false;
  245. hoveringLineAbove = false;
  246. if (mouseY > totalBrickHeight && mouseY < totalBrickHeight+h && !sliderHover && !hoverActiveButton && !hoverPauseButton) {
  247. hovering = true;
  248. } else {
  249. hovering = false;
  250. }
  251. }
  252. for (int i = 0; i < slider.size(); i++) {
  253. slider.get(i).update();
  254. params.get(i).value = slider.get(i).value;
  255. }
  256. brickBGcol = color(85, 85, 85);
  257. }
  258. void press() {
  259. pressedBrick = pos;
  260. }
  261. void display() {
  262. noStroke();
  263. if (active) {
  264. if (dragging) brickBGcol = color(51, 51, 51);
  265. else if (dragHovering && !hoveringLineBelow && !hoveringLineAbove) brickBGcol= color (105, 105, 105);
  266. else brickBGcol = color(85, 85, 85);
  267. } else {
  268. brickBGcol = color(0);
  269. }
  270. fill(brickBGcol);
  271. rect(0, totalBrickHeight, width, h);
  272. fill(255);
  273. text(name, 40, 13+totalBrickHeight, 10);
  274. stroke(255);
  275. if (active) fill(185);
  276. else noFill();
  277. rect(10, 4+totalBrickHeight, 10, 10);
  278. if (paused) fill(185);
  279. else noFill();
  280. rect(25, 4+totalBrickHeight, 10, 10);
  281. for (int i = 0; i < slider.size(); i++) {
  282. slider.get(i).display();
  283. }
  284. stroke(150);
  285. strokeWeight(1);
  286. line(0, totalBrickHeight+h-1, width, totalBrickHeight+h-1);
  287. if (hoveringLineAbove) {
  288. stroke(200);
  289. strokeWeight(5);
  290. line(0, y, width, y);
  291. }
  292. if (hoveringLineBelow) {
  293. stroke(200);
  294. strokeWeight(5);
  295. line(0, y+h, width, y+h);
  296. }
  297. }
  298. }
  299. int pressedSlider = -1;
  300. //class Slider extends Brick{
  301. class Slider {
  302. int x, y, w, h, mode;
  303. int[] osciModes;
  304. float minVal, maxVal, value; //should be private but doesnt work lol
  305. boolean hovering, hovering_anim_btn, animated;
  306. String label;
  307. Oscillator osci;
  308. Slider(int x_, int y_, int w_, int h_, float minVal_, float maxVal_, String label_, int mode_, int[] osciModes_) {
  309. x = x_;
  310. y = y_;
  311. w = w_;
  312. h = h_;
  313. minVal = minVal_;
  314. maxVal = maxVal_;
  315. mode = mode_;
  316. label = label_ + (mode == INTVAL ? " (INT)" : " (FLOAT)");
  317. osciModes = osciModes_;
  318. osci = new Oscillator(int(random(2, 5)), osciModes);
  319. osci.amplitude(minVal, maxVal);
  320. osci.update();
  321. setValue(osci.value);
  322. animated = true;
  323. slider.add(this);
  324. }
  325. void setPosX(int x_) {
  326. x = x_;
  327. }
  328. void setMinMax(float min, float max) {
  329. float cval = map(value, minVal, maxVal, 0, 10000);
  330. minVal = min;
  331. maxVal = max;
  332. osci.amplitude(minVal, maxVal);
  333. setValue(map(cval, 0, 10000, minVal, maxVal));
  334. }
  335. void setLabel(String lab) {
  336. label = lab + (mode == INTVAL ? " (INT)" : " (FLOAT)");
  337. }
  338. void setMode(int mode_) {
  339. mode = mode_;
  340. }
  341. void press(int myId_) {
  342. pressedSlider = myId_;
  343. animated = false;
  344. setValue(map(mouseX, x, x+w, minVal, maxVal));
  345. }
  346. void setValue(float value_) {
  347. value = value_;
  348. value = constrain(value, minVal, maxVal);
  349. if (mode == INTVAL) value = int(round(value));
  350. }
  351. float getValue() {
  352. return(value);
  353. }
  354. void update() {
  355. osci.update();
  356. if (animated) {
  357. setValue(osci.value);
  358. }
  359. if (mouseX >= x && mouseX <= x+w && mouseY >= y && mouseY <= y+h) {
  360. hovering = true;
  361. } else {
  362. hovering = false;
  363. }
  364. if (mouseX >= x+w+5 && mouseX <= x+w+5+15 && mouseY >= y && mouseY <= y+h) {
  365. hovering_anim_btn = true;
  366. } else {
  367. hovering_anim_btn = false;
  368. }
  369. }
  370. void display() {
  371. stroke(255);
  372. strokeWeight(1);
  373. fill(100+(40*int(hovering)));
  374. rect(x, y, w, h);
  375. fill(185+(40*int(hovering)));
  376. rect(x, y, map(value, minVal, maxVal, 0, w), h);
  377. fill(255);
  378. text(label, x+5, y+10);
  379. if (animated) {
  380. fill(185+(40*int(hovering_anim_btn)));
  381. } else {
  382. fill(100+(40*int(hovering_anim_btn)));
  383. }
  384. rect(x+w+5, y, h, h);
  385. }
  386. }
  387. //CP5
  388. void CP5_listActions(int n) {
  389. if (shaderList.size() < maxFx) {
  390. switch(n) {
  391. case(0):
  392. shaderManager.addShader(new ASDFPIXELSORT());
  393. break;
  394. case(1):
  395. shaderManager.addShader(new DISTORTER());
  396. break;
  397. case(2):
  398. shaderManager.addShader(new FM());
  399. break;
  400. /*
  401. case(3):
  402. shaderManager.addShader(new AUECHO());
  403. break;
  404. */
  405. default:
  406. break;
  407. }
  408. }
  409. }
  410. void mousePressed() {
  411. for (int i = slider.size()-1; i >= 0; i--) {
  412. Slider s = slider.get(i);
  413. if (s.hovering) s.press(i);
  414. if (s.hovering_anim_btn) s.animated = !s.animated;
  415. }
  416. for (int i = bricks.size()-1; i >= 0; i--) {
  417. if (bricks.get(i).hovering) {
  418. bricks.get(i).press();
  419. }
  420. if (bricks.get(i).hoverActiveButton) bricks.get(i).active = !bricks.get(i).active;
  421. if (bricks.get(i).hoverPauseButton) bricks.get(i).playPause();
  422. }
  423. }
  424. void mouseDragged() {
  425. if (pressedSlider != -1) {
  426. Slider s = slider.get(pressedSlider);
  427. s.setValue(map(mouseX, s.x, s.x+s.w, s.minVal, s.maxVal));
  428. }
  429. if (pressedBrick != -1) {
  430. bricks.get(pressedBrick).dragging = true;
  431. for (int i = 0; i < bricks.size(); i++) {
  432. if (pressedBrick != i && bricks.get(i).hovering) {
  433. bricks.get(i).dragHovering = true;
  434. } else {
  435. bricks.get(i).dragHovering = false;
  436. }
  437. }
  438. }
  439. }
  440. void mouseReleased() {
  441. if (pressedBrick != -1) {
  442. bricks.get(pressedBrick).dragging = false;
  443. for (int i = 0; i < bricks.size(); i++) {
  444. bricks.get(i).dragHovering = false;
  445. if (pressedBrick != i) {
  446. if (bricks.get(i).hovering && !bricks.get(i).hoveringLineAbove && !bricks.get(i).hoveringLineBelow) {
  447. shaderManager.swapShader(pressedBrick, i);
  448. }
  449. if (pressedBrick != i-1 && bricks.get(i).hoveringLineAbove) {
  450. shaderManager.moveShader(pressedBrick, i);
  451. break;
  452. }
  453. if (bricks.get(i).hoveringLineBelow) {
  454. shaderManager.moveShader(pressedBrick, bricks.size()); //move to last
  455. }
  456. }
  457. }
  458. if (mouseX > binX && mouseX < binX+binS && mouseY > binY && mouseY < binY+binS) {
  459. println("removing");
  460. shaderManager.removeShader(pressedBrick);
  461. }
  462. }
  463. pressedSlider = -1;
  464. pressedBrick = -1;
  465. }
  466. void mouseWheel(MouseEvent event) {
  467. if (event.getCount() < 0) renderSize += 8;
  468. else renderSize -= 8;
  469. renderSize = constrain(renderSize, 8, 15000);
  470. }
  471. }