We are about to switch to a new forum software. Until then we have removed the registration on this forum.
ArrayList photos = new ArrayList();
PImage[] loadPhotos;
int pos_x, pos_y;
int dragged_item;
void setup() {
size(800, 800);
smooth();
loadPhotos = new PImage[4];
loadPhotos();
imageMode(CENTER);
pos_x = 700;
pos_y = 100;
}
void draw() {
background(51);
//strokeWeight(1);
for (int i = 0; i <= 800; i = i+100) {
stroke(126);
line(0, i, 600, i);
}
for (int j = 0; j <= 600; j = j+100) {
//stroke(126);
line(j, 0, j, 800);
}
int x=pos_x, y=pos_y;
for (int i = 0; i < 4; i++) {
image(loadPhotos[i], x, y);
y = y + 120;
}
for (int i = 0; i < photos.size (); i++) {
Photo s = (Photo) photos.get(i);
s.display();
s.move();
}
}
void loadPhotos() {
for (int i=0; i<4; i++) {
loadPhotos[i] = loadImage("icon" + i + ".png");
}
}
void keyPressed() {
if (key == 'x') {
if (photos.size() > 0) {
photos.remove(photos.size()-1);
}
}
if (key == 'c') {
photos.clear();
}
}
int find_item() {
dragged_item = -1;
for (int i = 0; i < 4; i++) {
if (mouseX>pos_x-50 && mouseX<pos_x+50 && mouseY>pos_y+120*i-50 && mouseY<pos_y+120*i+50) {
dragged_item = i;
}
}
return dragged_item;
}
void mousePressed()
{
if (find_item() == 0) photos.add(new Photo(700, 100, 0, 0));
if (find_item() == 1) photos.add(new Photo(700, 220, 0, 1));
if (find_item() == 2) photos.add(new Photo(700, 340, 0, 2));
if (find_item() == 3) photos.add(new Photo(700, 460, 0, 3));
}
void mouseDragged() {
if (find_item() != -1) {
???
}
}
void mouseReleased() {
dragged_item = -1;
}
class Photo {
PImage photo;
float rotation;
float Xpos;
float Ypos;
int item;
Photo(float XposTemp, float YposTemp, float rotationTemp, int Nitem) {
item = Nitem;
photo = loadPhotos[item];
Xpos = XposTemp;
Ypos = YposTemp;
rotation = rotationTemp;
}
void display() {
rectMode(CENTER);
imageMode(CENTER);
noStroke();
pushMatrix();
translate(width/2, height/2);
rotate(radians(rotation));
translate(-width/2, -height/2);
translate(Xpos, Ypos);
fill(0, 0, 0, 70);
rect(0, 0, 100, 100);
image(photo, 0, 0, 100, 100);
popMatrix();
}
void move() {
???
}
}
https://dl.dropboxusercontent.com/u/70391886/icon0.png https://dl.dropboxusercontent.com/u/70391886/icon1.png https://dl.dropboxusercontent.com/u/70391886/icon2.png https://dl.dropboxusercontent.com/u/70391886/icon3.png https://dl.dropboxusercontent.com/u/70391886/photo.png
Please, help with mouseDragged function...
Solution with only one PImage with rotation (center mouse buttom) and snap an image to the grid possibility..
PImage img1;
boolean overImage, locked, kontrola, sit;
float mX, mY, offX, offY, angle;
int l = 0;
float[] udaljenosti = new float[48];
PVector[] sredista = new PVector[48];
void setup() {
size(800, 800);
mX=700;
mY=height/2;
angle=0;
imageMode(CENTER);
ellipseMode(CENTER);
for (int i1 = 50; i1 < 600; i1=i1+100) {
for (int j1 = 50; j1 < 800; j1=j1+100) {
sredista[l] = new PVector(i1, j1);
udaljenosti[l] = 100.0;
l++;
}
}
img1 = loadImage(url, "png");
}
void draw() {
background(0);
strokeWeight(1);
for (int i = 0; i <= 800; i = i+100) {
stroke(126);
line(0, i, 600, i);
}
for (int j = 0; j <= 600; j = j+100) {
line(j, 0, j, 800);
}
if (mouseX>mX-img1.width/2 && mouseX< mX+img1.width/2 && mouseY>mY-img1.height/2 && mouseY< mY+img1.height/2) {
overImage=true;
} else
overImage=false;
pushMatrix();
translate(mX, mY);
rotate(angle);
image(img1, 0, 0);
popMatrix();
noStroke();
fill(255, 0, 0);
ellipse(mX, mY, 10, 10);
if (kontrola) paint_blue();
if (sit) {
fill(20, 220, 20, 70);
rect(mX-img1.width/2, mY-img1.height/2, img1.width, img1.height);
}
}
void mousePressed()
{
if (overImage) {
locked=true;
if (mouseButton == CENTER) kontrola = true;
if (mouseButton != CENTER) kontrola = false;
} else {
locked=false;
}
offX=mouseX-mX;
offY=mouseY-mY;
}
void mouseDragged() {
if (locked && mouseButton != CENTER) {
mX=mouseX-offX;
mY=mouseY-offY;
for (int k = 0; k < 48; k++) {
float[] f = sredista[k].array();
udaljenosti[k] = dist(mX, mY, f[0], f[1]);
if (udaljenosti[k] > 20) sit = false;
if (udaljenosti[k] <= 20) {
mX = f[0];
mY = f[1];
sit = true;
break;
}
}
noFill();
strokeWeight(4);
stroke(255, 2, 2);
rect(mX-img1.width/2, mY-img1.height/2, img1.width, img1.height);
}
}
void mouseReleased() {
locked=false;
}
void keyPressed() {
if (key == 'l' && kontrola || key == 'L' && kontrola) {
angle = angle + HALF_PI;
}
if (key == 'r' && kontrola || key == 'R' && kontrola) {
angle = angle - HALF_PI;
}
}
void paint_blue() {
noFill();
strokeWeight(4);
stroke(2, 2, 255);
rect(mX-img1.width/2, mY-img1.height/2, img1.width, img1.height);
}
Answers
Not sure what is the question. An old example of drag and drop with rects. no rotation though..
I have four different object (PImage) on the left side (menu) that I have to put into the grid. Some images are repeated and I choose an image from menu with mouse click-drag-drop.. The solution with one PImage above is OK. I have a problem with arraylist..
I see, sorry I haven't run the code.
So the thing is... you gotta go OOP, I mean make a class of your draggable images. Or the code might become a mess..
Each time the user clicks in a predetermined place it creates and add a new instance to the array.
I adapted my code above to exemplify, as tweaking your code is harder and it is not OOP. C if this can help:
Get the idea, and translate your cool working mouse interaction logic (grid constraint and all that) to a class. Test with one instance. Once satisfied implement the ArrayList.
Thanks _vk!
The code will be useful but what I first noticed is when images are located one above the other, the mouse drag them all.. Further more, what I want: When you click on a image it becomes the last in the list and the only one draggable element in the list.
something like this:
How to prevent Dragging multiple selected elements - NOT SOLVED !!!
Solution for pushing current object to the end of the list:
OK...here it is...
Are you all done?
Why do you need them at the end of the list? Actually they are added at the end, aren't they?
If the mouse is hovering over an overlapping part, yes. You would need some more input to tell which one it should drag. But if images are overlapping and there are parts not overlapping, clicking in one not overlapping part will drag only the one below mouse pointer.
There are always a ton of ways to do things.
You could just re work the way
dontMove
is being set to avoid images to continue draggable... Or just set a image in the spot after the user releases the draggable one getting rid os Draggable arrayList... Tons of ways.cheers
SOLVED !!! (Thanks to _vk)
"L" and "R" for tile rotation
"X" - delete tile
Cool!
what about this? :)
You should consider renaming
DragMe
anddrags
to something more meaningful.Also if the image is not 'snapped' and you release them (even if outside the grid) it will stay there. Shouldn't they be 'dropped' only if they are aslo 'snapped' ?
Another thought
this:
When you repeat yourself a lot :) is aways a place to go OOP. A button class could make the code nicer...
What's your favourite way to code a button?
_vk, this is just a trial version, of course, that there will be changes.. DragMe and drags terms are ok :)
Thanks!
I have a problem now with exporting PGraphics with transparency and high-resolution (It's not in the center - scaleFactor1 and does not scale entire PGraphics object - scaleFactor2)
scaleFactor = 1
scaleFactor = 2
Here is the solution: