-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDraggableObject.java
88 lines (74 loc) · 2.15 KB
/
DraggableObject.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import processing.core.*;
/*
Draggable Object Class
_________________________________
Creates a draggable button for a
sound. When clicked, the button plays
the sound and when dragged and dropped
on the timeline, it adds the sound
to the work canvas.
*/
public class DraggableObject {
int x,y, initx, inity, width;
String name;
PApplet p;
WSound sound;
String file;
boolean dragging=false;
boolean pMousePressed=false;
DraggableObject(int x, int y, String file, PApplet p) {
this.x=x;
this.y=y;
this.initx=x;
this.inity=y;
this.file=file;
this.sound=new WSound(file,p);
this.p=p;
// The draggable object's label is just the files name capitalized and cleaned
this.name=file.replaceAll("_"," ").replaceAll(".wav","").trim();
this.name=this.name.substring(0, 1).toUpperCase() + this.name.substring(1);
this.width=(int)p.textWidth(this.name)+50;
}
public void draw(WorkCanvas mainWindow) {
p.noStroke();
if(mouseOver()||dragging) {
p.fill(100,100,100,170);
} else {
p.fill(46);
}
p.rect(x, y, width, 30);
p.fill(255);
p.textAlign(p.LEFT);
p.triangle(x+10, y+10, x+10+10, y+15, x+10, y-10+30);
p.text(name, x+35, y+20);
if((!pMousePressed && p.mousePressed && mouseOver())||dragging) {
sound.play();
x-=p.pmouseX-p.mouseX;
y-=p.pmouseY-p.mouseY;
dragging=true;
}
if(!p.mousePressed && dragging) {
if(mainWindow.addindexAtXY(p.pmouseX,p.mouseY) != -1) {
mainWindow.addSoundAt(sound, mainWindow.addindexAtXY(p.pmouseX,p.mouseY));
}
dragging=false;
pMousePressed=false;
x=initx;
y=inity;
}
if(!p.mousePressed) {
dragging=false;
pMousePressed=false;
}
if(p.mousePressed && !pMousePressed) {
pMousePressed=true;
}
}
public boolean mouseOver() {
return p.mouseX > x && p.mouseX < x + width && p.mouseY>y && p.mouseY<y+30;
}
// Returns the width of the button
public int getWidth() {
return this.width;
}
}