Thursday, December 16, 2010

More PyObjC


If you're interested in PyObjC, I have lots of good examples up on my iDisk Public folder (see the page here). I think most of that material is still relevant on Snow Leopard. Just for fun, I grabbed the animation code and reassembled it into a new XCode project template. A quick outline:

Do the files first (then IB can figure things out for you):
File > New File > User Templates: Python NSView subclass: MyView
File > New File > User Templates: Python NSObject subclass: MyAnimation

Paste in the code except use the new versions for inits and add @IBAction for button methods and do objc.IBOutlet() for outlets.

Put the following in the nib:

2 buttons (Start and Stop) hooked up to the App Delegate methods
a Custom View with its class set to MyView
this view hooked up as an outlet of the App Delegate
a Progress Indicator is also an outlet of the App Delegate

The class MyAnimation inherits from NSAnimation
It's instantiated from the AppDelegate (no nib representation)

In MyAnimation.init() we specify the
duration = 5 sec
FrameRate = 6 Hz
ProgressMarks = 300 (so 300/5 = 60 Hz)

in MyAnimation I added print calls to show that:
setCurrentProgress_ gets called every Frame (30 times total)
each time we tell the view to save an NSBezierPath
using the current position of the object
we also tell the view to update currentProgress

The AppDelegate is the delegate of the animation
animation_didReachProgressMark_
gets called at 60 Hz (300 calls in 5 sec)
we call setNeedsDisplay_(True) on the view each time

It's simple and beautiful. I put the zipped project files (and a built app) on Dropbox (here).