Hello everyone,
I’m a bit stack with a rather simple file trigger, the thing is:
I have some text() that I can drag and drop, and I would like that when one of them pass the width/2 value, it play a specific sound file. When I drag the text() back and xpos < width/2 it should stop only that sound file. I’m currently using sound library, minim seems overcomplicated for what I’m looking for, but I’m open to anything.
I currently have some problem with the trigger, because it does it at frameRate, rather than an impulse. I tried using some boolean for control, but it’s not working… maybe you can give me some hints
Thank you
The drang and drop function is adapted from https://forum.processing.org/two/discussion/19190/click-and-drag-with-arrays-of-objects
import processing.sound.*;
SoundFile file;
Text[] textboxes = new Text[20];
boolean isPlaying = false;
boolean play = false;
void setup() {
size(500, 500);
textSize(32);
for (int i = 0; i < 20; i++) {
textboxes[i] = (new Text("CIAO", 20, 20 + (i*32)));
}
// Load a soundfile from the /data folder of the sketch and play it back
file = new SoundFile(this, "Broken Code - Supreme Instruction - 152bpm - 24 BIT MASTER.wav");
}
void draw() {
background(0, 200, 200);
for (int i = 0; i < textboxes.length; i++) {
textboxes[i].show();
if (textboxes[0].xpos > width/2) {
play = true;
isPlaying = true;
} else {
play = false;
}
}
isItPlaying();
pushMatrix();
stroke(0);
strokeWeight(10);
line(width/2, 0, width/2, height);
popMatrix();
}
void isItPlaying() {
if (isPlaying == true && play == true) {
println("now play");
file.play();
isPlaying = false;
} else if (isPlaying == false && play == false) {
file.stop();
}
}
void mousePressed() {
for (int i = 0; i < textboxes.length; i++) {
//checking to see if the mouse is over the box and turning it white if it is
if (textboxes[i].textover == true) {
textboxes[i].locked = true;
// print("mouse is pressed");
} else {
textboxes[i].locked = false;
// print("mouse isn't pressed");
}
textboxes[i].xoffset = mouseX - textboxes[i].xpos;
textboxes[i].yoffset = mouseY - textboxes[i].ypos;
// print(textboxes[i].locked);
}
}
void mouseDragged() {
for (int i = 0; i < textboxes.length; i++) {
if (textboxes[i].locked) {
textboxes[i].xpos = mouseX - textboxes[i].xoffset;
textboxes[i].ypos = mouseY - textboxes[i].yoffset;
}
}
}
void mouseReleased() {
for (int i = 0; i < textboxes.length; i++) {
textboxes[i].locked = false;
}
}
class Text {
float xpos = random(width);
float ypos = random(height);
float textsize;
float textheight;
boolean textover = false;
boolean locked = false;
float xoffset = 0;
float yoffset = 0;
String text;
Text(String text, float xpos, float ypos) {
this.xpos = xpos;
this.ypos = ypos;
this.text = text;
textsize = textWidth(text);
textheight = textAscent() + textDescent();
}
void show() {
text(text, this.xpos, this.ypos);
if (mouseX > this.xpos && mouseX < this.xpos + this.textsize &&
mouseY > this.ypos - this.textheight/2 && mouseY < this.ypos + this.textheight/2) {
this.textover = true;
fill(255);
if (mousePressed && this.textover) {
stroke(200, 79, 100);
strokeWeight(5);
} else {
noStroke();
}
} else {
this.textover = false;
noStroke();
}
// rect(this.xpos, this.ypos, this.textsize, this.textsize, 7);
};
}