We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I searched the intertubes high and low to find an answer, but either the answers where incomplete, obscure or deprecated.. How does the syntax in the class looks like (because this seems to be a bit unusual(?). and how do I get the handler(the state of the mouse) into the class?, it has something to do with register function, but I found no info about this keyword...
thx in advance
I am working the whole day already on this problem and still I cannot get any meaningful results, Its irritating...
Answers
It is usually done by using Booleans. In your main program create
void mouseClicked()
, inside that function add something likeclicked = true
. Now, in your class you can checkif (clicked)
. You need however to set this Boolean to false manually.That's exactly what I am doing now, but I find the system a bit cumbersome...
@Ater: Why are you adding an extra boolean? Why not just use the existing mousePressed variable?
@Magnetic_Garden: Your question is pretty unclear. Can you post an MCVE showing what you're talking about?
@KevinWorkman: It is connected with the fact that mousePressed virable is true all the time mouse button is pressed. And this may last couple of frames. Usually, when you look for a click event, that means that you want the action to occur only once after button is pressed. That's where another boolean came from.
Edit: And yeah, I find this system strange too. But it is only simple method to do it, as there is now mouseClicked virable in Processing. I think the goal may be achieved by creation of own threads, but I don't know yet how it is done. If there is a simplier way, please, advice.
I'm not really sure why you're talking about threads- it sounds like the OP is simply asking how to pass data into another class, but we can't really be sure until he posts an MCVE.
The way I usually do it is to give each class that needs to respond to a mouse click a onClick() function, and then call it from mousePressed():
Sorry, I didn't realized my question was so vague so here is my MCVE (pseudocode)
problem is how to read the mouse from out of this class, since its outsides the scope...
That isn't an MCVE, since it wouldn't compile- is this in its own tab, or is it inside the main sketch tab?
OKay, I updated the pseudo code to something that actualy functions. the problem is that this class is dependent from draw() for its imput... thats the problem.
Uh, don't bother. The variable mousePressed is global, and can be used anywhere, even within your own classes:
Sorry for the late reply, but it does not seem to work this way when you use Intellij, I cannot seem to get it to work in this IDE as it does in Processing itself. Anyway, took Ater's advice and reverted back to the old method by passing a bool.
When you work inside the Processing IDE what TfGuy44 said is correct as your class is Nested into the PApplet, so you have access to every member of the PApplet class.
When you work in Intellij or eclipse for example, if your class is not nested (you still can of course) a nice solution is to pass a reference to the PApplet, the context.
Then you will have access to all the information you might need.
Here is an example:
Thx! this will be very usefull!! Now I can sanitize my code some more ;)