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 › void mousePressed in classes
Page Index Toggle Pages: 1
void mousePressed in classes (Read 905 times)
void mousePressed in classes
Nov 26th, 2007, 10:17pm
 
hello.
im doing some controls, and i need to calculate the offset of the mouse-position on my slider button. i just started using classes and i cant seem to get the void mousePressed and released to work inside a class. if i use if(mousePressed) this offset will calculate simultaneously. how can i replicate the effect of void mousePressed which is only called once inside a class?

thank you
Re: void mousePressed in classes
Reply #1 - Nov 26th, 2007, 10:24pm
 
I think the only way would be to have the top level mousePressed tell your class that the mouse has been pressed via some method.

e.g.
Code:
class myClass
{
//...
void mousePressed(int x, int y, int button)
{
//do stuff..
}
}

myClas myObject;

void setup()
{
//stuff
myObject=new MyClass(...);
}

void mousePressed()
{
myObject.mousePressed(mouseX,mouseY,mouseButton);
}


Your class will then have to work out if it needs to do anything or store values etc.. you'll probably also have to do similar for mouseDragged etc etc.
Re: void mousePressed in classes
Reply #2 - Nov 26th, 2007, 10:30pm
 
beautiful!
thank you so much for the quick answer!

Slm
Re: void mousePressed in classes
Reply #3 - Nov 27th, 2007, 4:33am
 
hi,

I have a similar question. I have a few objects - created using the same class - that i want to drag around the screen independently.  The method you just provided works great, adapted for the mouseDragged method, but when one object goes near another they inherit each other's position and become entangled/locked together. Can you think of a way to drag objects without this happening?
Re: void mousePressed in classes
Reply #4 - Nov 27th, 2007, 6:32am
 
Hi. For drag & drop purpose, I usually use a global variable which stores a reference to the object dragged. And I add a condition test so when the mouse is dragged, only this object can be moved.

Code:
class myObject {
void mouseDragged() {
if (draggedObj != this) return;
// drag code...
}
void mousePressed() {
if (isMouseOver()) draggedObj = this;
}
}

myObject draggedObj;
myObject myObj1;

void mousePressed() {
myObj1.mousePressed();
}

void mouseDragged() {
myObj1.mouseDragged();
}

void mouseReleased() {
draggedObj = null;
}
Re: void mousePressed in classes
Reply #5 - Nov 27th, 2007, 10:13am
 
nkelly16 wrote on Nov 27th, 2007, 4:33am:
Can you think of a way to drag objects without this happening


You could make the object's mouseDragged method return true if it has reacted to the mouse dragging, and then in the global mouseDragged stop sending mouseDragged info to other objects after one has reacted.

Code:
void mouseDragged()
{
for(int i=0;i<draggableObjects.lenght;i++)
{
if(draggableObjects[i].mouseDragged(mouseX,mouseY,mouseButton))
break; //I think this gets you out of the for loop.
}
}
Re: void mousePressed in classes
Reply #6 - Nov 27th, 2007, 1:08pm
 
Lovely stuff. Thanks for that guys!
Re: void mousePressed in classes
Reply #7 - Nov 30th, 2007, 11:07am
 
hi,
i'm new to work with classes and nearly in the same question.
have not understood how to implement mousefunctions for all bubbles.
i work on the bouncy bubbles example.

http://processing.org/learning/basics/mousefunctions.html
http://processing.org/learning/topics/bouncybubbles.html
please help the beginner
Reply #8 - Nov 30th, 2007, 7:02pm
 
sorry if i'm not understanding everything and asking here.

the overCheck() is working, but i get for every object in my class a "false" for the boolean "over", when i press the mouse. i don't understand?

Code:

// the variables
int numBalls = 10;
Ball[] balls = new Ball[numBalls];
boolean over = false;

// setup is doing things one time in the beginning
void setup() {
size(1024, 768);
noStroke();
smooth();
for (int i = 0; i < numBalls; i++) {
balls[i] = new Ball(random(width), random(height), random(20, 40), i, balls);
}
}

// draw is doing things everytime
void draw() {
background(0);
for (int i = 0; i < numBalls; i++) {
// the class methods for every object in my array
balls[i].display();
balls[i].distCheck();
balls[i].overCheck();
}
}

// mousemethods classextern
// mousePressed
void mousePressed() {
for (int i = 0; i < numBalls; i++) {
balls[i].mousePressed(mouseX, mouseY, mouseButton);
}
}

// mouseReleased
void mouseReleased() {
for (int i = 0; i < numBalls; i++) {
balls[i].mouseReleased();
}
}

// creating the class
class Ball {
float x, y;
float diameter;
int id;
Ball[] others;

Ball(float xin, float yin, float din, int idin, Ball[] oin) {
x = xin;
y = yin;
diameter = din;
id = idin;
others = oin;
}

// show all
void display() {
fill(255,125);
rect(x, y, diameter, diameter);
}

// check distance and draw the line
void distCheck() {
for (int i = id + 1; i < numBalls; i++) {
float dx = others[i].x - x;
float dy = others[i].y - y;
float distance = sqrt(dx*dx + dy*dy);
float minDist = 200;
if (distance < minDist) {
stroke(255);
line(x, y, others[i].x, others[i].y);
}
}
}

// check for every ball if mouse is over
void overCheck(){
if(mouseX > x && mouseX < (x+diameter) && mouseY > y && mouseY < (y+diameter)){
over = true;
println("w");
} else {
over = false;
}
}

// Mousemethods class intern
// mousepressed
void mousePressed(int mx, int my, int mbutton){
println(over);
}

// mousereleased
void mouseReleased(){
}

// the End
}

Page Index Toggle Pages: 1