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 & HelpProcessing Implementations › Adding predefined variables and event handlers
Page Index Toggle Pages: 1
Adding predefined variables and event handlers? (Read 2267 times)
Adding predefined variables and event handlers?
Jun 2nd, 2009, 11:29am
 
I'm embedding Processing in another environment and want seamless communication with that environment. To achieve this, I want to provide a new predefined variable along the lines of mouseX and mouseY. Let's call this variable 'context', which will provide info about the containing environment. And I'd also like to add more event handlers along the lines of mousePressed() to trigger operations in the sketch when events happen in the containing environment.

Ok, hopefully my goals are clear. I created a subclass of PApplet, let's call it ContextAwarePApplet that provides the featueres I want. I can then export a Processing sketch, and modify the resulting java code to extend from ContextAwarePApplet and then add any logic to use my context features.

This all seems fairly straightforward. Now the real problem is I would like to use my context variable and event handlers in the original Processing sketch code, convert it to Java, automatically modify it to extend from ContextAwarePApplet and any other needed changes, then compile it.

In the processing IDE, I can't export my sketch if I reference the context variable because it doesn't exist in PApplet. It seems like the conversion to Java might work because the error I get is:
processing.app.debug.RunnerException: unexpected token: null
which sounds like it's happening during Runtime of the sketch.

I'm going to start digging into Processing internals to figure out what's going on during export and see if I can hook in somewhere to make this all work end-to-end.

But I was wondering if someone can point me in the right direction to jump-start this project. Maybe point me to another Processing project that does something similar? Or just tell me what classes in the Processing source I will need to work with?

Thanks for any help,
Adam
Re: Adding predefined variables and event handlers?
Reply #1 - Jun 3rd, 2009, 5:13pm
 
My previous post was confusing. Let me back up and make this more concrete:

Say I'm making a standalone Java application in AWT that has two frames. One frame has some of the usual GUI widgets, let's say there's a button. The other frame has an embedded processing sketch.

How do I get those two frames to talk to each other?

(1) I want to press the button and have the processing sketch react to that.

(2) I want the sketch to be able to change the label on the button.

For (1) I'm picturing something similar to mouseX/mouseY/mousePressed(). Like a buttonState variable and/or a buttonPressed() method. I guess this involves modifying PApplet?

For (2) I can provide a setButtonLabel() method in the Java application, but the sketch won't be aware of it. Again I think I need to modify PApplet to let me set an appContext variable, and the sketch code could then call appContext.setButtonLabel(...)

Someone had suggested I look into making a library, but I don't think it helps in this kind of embedding scenario. Am I missing some extension hooks that would make this trivial or do I have to mess with PApplet source or otherwise extend the Processing classes?

Thoughts?

PS - sorry if this should have been posted in the integration forum.
Re: Adding predefined variables and event handlers?
Reply #2 - Jun 4th, 2009, 1:21am
 
Indeed, the question belongs more to Integration section, but a moderator will eventually move it, if needed.

I too was thinking about making a library, it is much easier and reliable than altering Processing's source code: see how some libraries, like Video, implement callbacks like movieEvent(). Although no need to make a full fledged library in a separate jar, it can be just some code in your main application.

For example, you import one of your main application classes in your sketch, say ProcessingHandler. You create a method called buttonPressed() in your sketch, and pass this to the ProcessingHandler class which will be able to call your buttonPressed routine each time it is needed.
Re: Adding predefined variables and event handlers?
Reply #3 - Jun 4th, 2009, 9:25am
 
PhiLho,
Thanks for the response.

Yeah, I was coming to the same conclusion that passing a reference to the sketch's 'this' into my code is a good way to hook everything up. Will try these ideas out soon.

Cheers,
Adam
Page Index Toggle Pages: 1