We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › MouseDrag objects
Page Index Toggle Pages: 1
MouseDrag objects (Read 1247 times)
MouseDrag objects
Jan 29th, 2010, 4:06am
 
'mournin everybody
I'm trying to program a small "game", in which the user will have to drag'n drop objects to create relations between them.
I've seen how to make classes, and in another hand a code for dragndrop an object, but i can't make the objects move on their own.…

this is my code for the moment, one ellipse is mooving only.
I feel like I have to use a public class with extends, am i wrong?
Is it the good code for dragndropping in my case?

Many thanks, sorry for my french english

Code:
float bx;
float by;
int bs = 55;
boolean bover = false;
boolean locked = false;
float bdifx = 0.0;
float bdify = 0.0;

Sphere1 hlm1;
Sphere2 hlm2;



void setup() {

 size(700,700);
 bx = 100;
 by = 100;
 
 hlm1 = new Sphere1(color(0,255,0,200));
 hlm2 = new Sphere2(color(0,0,255,200));
 
}


void draw() {
 background(0);
 smooth();
 cadre();
 
 
   // Test if the cursor is over the box
 if (mouseX > bx-bs && mouseX < bx+bs &&
     mouseY > by-bs && mouseY < by+bs) {
   bover = true;  
   if(!locked) { stroke(255); fill(153); } }
   else { stroke(153);
   fill(153);
   bover = false;  
 }
 
         
 hlm1.display();    // Draw the sphere
 hlm2.display();
}



void cadre() {                                  //CADRE
stroke(255);
line(20,20,20,680);
line(20,20,680,20);
line(680,20,680,680);
line(20,680,680,680);
}


 void mousePressed() {
 if(bover) {
   locked = true; fill(255, 255, 255); }
   else { locked = false; }
 bdifx = mouseX-bx;
 bdify = mouseY-by;
}

void mouseDragged() {
 if(locked) {
   bx = mouseX-bdifx;
   by = mouseY-bdify;
 }
}

void mouseReleased() {
 locked = false;
}


class Sphere1 {
 color c;                        // PROPRIETES

 Sphere1(color tempC) {          // CONSTRUCTEUR
   c = tempC;
 }
 
 void display() {                // METHODES
   noStroke();
   fill(c);
   ellipse(bx,by,bs,bs);
 }
 

 
}


class Sphere2 {
 color c;                        // PROPRIETES


 Sphere2(color tempC) {          // CONSTRUCTEUR
   c = tempC;
 }
 
 void display() {                // METHODES
   noStroke();
   fill(c);
   ellipse(300,300,bs,bs);
 }
 
}


Re: MouseDrag objects
Reply #1 - Jan 29th, 2010, 4:29am
 
I shown in the past how to do drag'n'drop and I believe there is a sample showing that as well, and probably a number of forum threads about that.
Basically, you have to handle the "dragged" state in the dragged object class itself, plus you have to manage these states at the global level to drag only one object at the time.

PS.: mourning = pleurer !  Cry
Grin (and it is already PM in France, night elsewhere, etc. Wink)
Re: MouseDrag objects
Reply #2 - Jan 29th, 2010, 4:38am
 
Yes I've actually found some materials, but it's in the combination with the class concept i'm not used to, I'm in troubles Smiley
thk you for the answer, I seems like I need to go back to work!
ps. ahahah sorry, I didn't wanna make my post look so sad Smiley
1pm is the mourning for me when I work 'till 5am  Embarrassed
Re: MouseDrag objects
Reply #3 - Jan 29th, 2010, 6:17am
 
If it's any help this demo includes mouse drag behaviour on objects that move when not dragged; though I now favour the approach used in my later sketches based on some code posted by Quark.

I'd also make a couple of comments about your classes: Sphere1 and Sphere2 are more or less identical.  Rather than have two classes it would make more sense to add some properties to a single class and create multiple objects of that type.  If you don't want a particular 'sphere' to be selectable you can add a boolean property 'selectable' and check against this when a select action is made.
Re: MouseDrag objects
Reply #4 - Jan 29th, 2010, 6:21am
 
Access to my Launchpad code is currently broken [EDIT: Fixed, that was quick! See Handles to get latest files], so I will just paste here the code I just updated: I was horrified to see the awful code I wrote when I started to code in Processing, two years ago! Smiley

Handles.pde Code:
final int HANDLE_NB = 10;
HandleList handles = new HandleList(false);

void setup()
{
 smooth();
 size(400, 400);

 // Create random handles
 for (int i = 0; i < HANDLE_NB; i++)
 {
   int size = int(random(15, 30));
   handles.Add(new Handle(size + random(width - 2 * size), size + random(height - 2 * size),
 size, size / 5,
 color(random(0, 128), 0, 0), color(0, random(200, 255), 0),
 color(0, 0, random(200, 255)), color(random(200, 255), random(90, 140), 0)
   ));
 }
}

void draw()
{
 background(#AADDFF);

 handles.Update();
}

Handle.pde Code:
class Handle
{
 // Lazy (Processing) class: leave direct access to parameters... Avoids having lot of setters.
 float m_x, m_y; // Position of handle
 int m_size; // Diameter of handle
 int m_lineWidth;
 color m_colorLine;
 color m_colorFill;
 color m_colorHover;
 color m_colorDrag;

 private boolean m_bIsHovered, m_bDragged;
 private float m_clickDX, m_clickDY;

 /**
  * Simple constructor with hopefully sensible defaults.
  */
 Handle(float x, float y)
 {
   this(x, y, 5, 1, #000000, #FFFFFF, #FFFF00, #FF8800);
 }

 /**
  * Full constructor.
  */
 Handle(float x, float y, int size, int lineWidth,
color colorLine, color colorFill, color colorHover, color colorDrag
 )
 {
   m_x = x; m_y = y;
   m_size = size;
   m_lineWidth = lineWidth;
   m_colorLine = colorLine;
   m_colorFill = colorFill;
   m_colorHover = colorHover;
   m_colorDrag = colorDrag;
 }

 /**
  * Updates the state of the handle.
  * @param bAlreadyDragging   if true, a dragging is already in effect
  */
 void Update(boolean bAlreadyDragging)
 {
   // Check if mouse is over the handle
   m_bIsHovered = dist(mouseX, mouseY, m_x, m_y) <= m_size / 2;
   // If we are not already dragging and left mouse is pressed over the handle
   if (!bAlreadyDragging && mousePressed && mouseButton == LEFT && m_bIsHovered)
   {
// We record the state
m_bDragged = true;
// And memorize the offset of the mouse position from the center of the handle
m_clickDX = mouseX - m_x;
m_clickDY = mouseY - m_y;
   }
   // If mouse isn't pressed
   if (!mousePressed)
   {
// Any possible dragging is stopped
m_bDragged = false;
   }
 }

 boolean IsDragged()
 {
   return m_bDragged;
 }

  /**
   * If the handle is dragged, the new position is computed with mouse position,
   * taking in account the offset of mouse with center of handle.
   */
 void Move()
 {
   if (m_bDragged)
   {
m_x = mouseX - m_clickDX;
m_y = mouseY - m_clickDY;
   }
 }

  /**
   * Just draw the handle at current posiiton, with color depending if it is dragged or not.
   */
 void Draw()
 {
   strokeWeight(m_lineWidth);
   stroke(m_colorLine);
   if (m_bDragged)
   {
fill(m_colorDrag);
   }
   else if (m_bIsHovered)
   {
fill(m_colorHover);
   }
   else
   {
fill(m_colorFill);
   }

   ellipse(m_x, m_y, m_size, m_size);
 }
}

class HandleList
{
 private ArrayList m_handles = new ArrayList();
 private boolean m_bDragging;
 private boolean m_bGroupDragging; // True if you want to be able to drag several objects at once

 HandleList()
 {
 }

 HandleList(boolean bGroupDragging)
 {
   m_bGroupDragging = bGroupDragging;
 }

 void Add(Handle h)
 {
   m_handles.add(h);
 }

 void Update()
 {
   // We suppose we are not dragging by default
   boolean bDragging = false;
   // Check each handle
   for (int i = 0; i < m_handles.size(); i++)
   {
Handle h = (Handle) m_handles.get(i);
// Check if the user tries to drag it
h.Update(m_bDragging);
// Ah, this one is indeed dragged!
if (h.IsDragged())
{
 // We will remember a dragging is being done
 bDragging = true;
 if (!m_bGroupDragging)
 {
   m_bDragging = true; // Notify immediately we are dragging something
 }
 // And move it to mouse position
 h.Move();
}
// In all case, we redraw the handle
h.Draw();
   }
   // If no dragging is found, we reset the state
   m_bDragging = bDragging;
 }
}
Re: MouseDrag objects
Reply #5 - Jan 29th, 2010, 6:40am
 
This is awesome!
Thank you for this last 2 posts
There are some parts of the code I don't understand for the moment but I'll take the time it needs to Wink
Thanks for your time!  Roll Eyes
Re: MouseDrag objects
Reply #6 - Jan 29th, 2010, 11:46am
 
Hemmmm…

given code was very helpfull, I have now different coloured draggable objects in one class  Cool

Now I'd need to connect them (or not) with a simple line when a circle is close to another (something like an area of 200pts around)…

I've made another class called Connect, but I'm afraid I'm a bit lost in the code of the first class… How can I get back the position values of each object? Do I have to create this connecting method in a new class or in the same objects class? Undecided

Thanks for your help
Re: MouseDrag objects
Reply #7 - Jan 29th, 2010, 11:55am
 
Author of this thread seems to have similar needs: Calculating distance between two objects.
Re: MouseDrag objects
Reply #8 - Feb 9th, 2010, 6:41am
 
Hello again

Still have troubles with my code,
Only one thing is missing now : lines between two objects when they're closed enough…
I'm back in 3D now, and have two classes for two types of objects, that are supposed to link together.

I guess I need a line(m_x,m_y,p_x,p_y)
m for the first class and p for the second
this line would be drawn once processing have checked if objects are closed…

I can't handle the function nearAgent posted beside, don't know where to put it in my code…


void highlight() {
  if (nearAgent != null) {
    stroke(#FF0000);
    line(x, y, nearAgent.x, nearAgent.y);
  }
}

boolean nearAnotherAgent(Agent b) {
   boolean near = dist(x,y,b.x,b.y) <= diameter;
   if (near) {
   nearAgent = b;
   } else {
   nearAgent = null;
   }
   return near;
}

????

I'had a look to the function EmbeddedIteration but I can't get no more result…

Can someone please help me, 'have to show this program in two days!
Thnxxxxx
Re: MouseDrag objects
Reply #9 - Feb 9th, 2010, 9:34am
 
Well, can you give a breakdown of the classes you use, how you manage lists of objects, etc. No need to give full code, I think, just the global picture.
Page Index Toggle Pages: 1