[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[sc-dev] Announcement: Cocoa Bridge

I've been working on a project that I expect many of you will be interested in and recently got the core functionality implemented. The project is a full Objective-C bridge in the style of PyObjC or RubyCocoa. My motivation for writing it is so that users can create GUIs in Interface Builder that work seamlessly with SuperCollider. I'm hoping to release it towards the end of December.

It supports:
	1- Subclassing Objective-C classes inside of SuperCollider.
2- Calling Objective-C objects with the same syntax you use to call Supercollider objects. 3- Connecting actions and outlets in Interface Builder to SuperCollider objects.
	4- Bindings.

As an example, lets say you wanted to build a simple UI that called a method upon pressing a button. First, you would drag a button into a window in Interface Builder. Then, still in Interface Builder, you instantiate an object named TestClass. You create an action in TestClass named test: and connect the button to the action. In SuperCollider you create a new class like so:

TestClass : NSObject {
	test_ { arg sender;
		"You hit the test button".postln;

Note that the colons in Objective-C are replaced with underscores in SuperCollider. You recompile the class library, call a function to load the NIB and your UI pops up. Using things like a table view is similarly easy; you just implement two more methods: tableView_objectValueForTableColumn_row_ and numberOfRowsInTableView_. Doing an NSView subclass that draws a string and random color looks like:

CustomViewTest : NSView {
	initWithFrame_ { arg frameRect;
	drawRect_ { arg rect;
NSColor.colorWithCalibratedRed_green_blue_alpha_(rrand(0, 1.0), rrand(0, 1.0), rrand(0, 1.0), 0.6).set;
		rect = rect.moveBy(1, -2.2);
"I'm an NSView subclass running inside of SuperCollider".asNSString.drawInRect_withAttributes_(rect, nil);

Some things to note: you call Objective-C methods by giving the method/argument names first and then the arguments in that order. NSRect (a C structure) is transparently bridged with the SuperCollider Rect class. As a convenience you can create an NSString by calling .asNSString on a SuperCollider string.

Those of you that are familiar with Jan's bridge might be thinking, "but hasn't this already been done?". Jan's bridge only supports one way communication with Cocoa. You can instantiate Cocoa objects and send them messages, but you can't have Cocoa call SuperCollider (which essential if you want to use NIB files or create delegates). Although I had to rewrite most of his code, my bridge is the next step in what he was doing.