I was at the excellent iOS Dev UK conference last week and a few people talked about combining the power of cocos2d with the features of UIKit. This made a lot of sense to me. Cocos2d is great at graphics and such, but it's a real pig for certain basic behaviours. For example, I've been putting off adding a feature to my latest game that allow you to edit the name of the players. The thought of writing a text field control from scratch was a bit daunting. I now know I can just use a UIKit edit box and avoid all the heavy lifting!
Adding UIKit controls is surprisingly easy. The cocos2d app Director sets up the screen a regular UIView to display the Open GL stuff. All you need to do is add a child to this view, then all the standard UIKit stuff can be used. So easy!
I'm yet to see if there's any performance hits in using this approach, but since it's only going to be used in the menu part of the game, this won't be a problem.
After getting the UIKit working, I next turned to using the equivalent Cocoa controls for the Mac. This turned out to be a little trickier. Unlike the iOS Open GL view, the Mac version does NOT allow any child views to be added to it. D'Oh!
After hunting around for ages, I eventually scraped together some code to do it. Basically, it involves creating a new NSWindow with a NSView inside. You then add this window as a child of the root window. I place it at the top of the view stack and make it transparent, so the user doesn't realise what's going on. All your Cocoa controls can then be added to the NSView.
There were a few gotchas with this. First of all, if you add a Window without any chrome (window title, open/close etc), OSX handily disables all edit functions! That took me AGES to work out what was going on. To get around this, all you need to do is sub-class the Window and add a single function that tells OSX to allow editing. So bizarre!
I'm still working out a few kinks with this approach, but I'm now able to use standard OS controls for editing text, which is amazing. I'll post a complete project with examples on github once I get a nice clean project running.
Adding UIKit controls is surprisingly easy. The cocos2d app Director sets up the screen a regular UIView to display the Open GL stuff. All you need to do is add a child to this view, then all the standard UIKit stuff can be used. So easy!
I'm yet to see if there's any performance hits in using this approach, but since it's only going to be used in the menu part of the game, this won't be a problem.
After getting the UIKit working, I next turned to using the equivalent Cocoa controls for the Mac. This turned out to be a little trickier. Unlike the iOS Open GL view, the Mac version does NOT allow any child views to be added to it. D'Oh!
After hunting around for ages, I eventually scraped together some code to do it. Basically, it involves creating a new NSWindow with a NSView inside. You then add this window as a child of the root window. I place it at the top of the view stack and make it transparent, so the user doesn't realise what's going on. All your Cocoa controls can then be added to the NSView.
There were a few gotchas with this. First of all, if you add a Window without any chrome (window title, open/close etc), OSX handily disables all edit functions! That took me AGES to work out what was going on. To get around this, all you need to do is sub-class the Window and add a single function that tells OSX to allow editing. So bizarre!
I'm still working out a few kinks with this approach, but I'm now able to use standard OS controls for editing text, which is amazing. I'll post a complete project with examples on github once I get a nice clean project running.
No comments:
Post a Comment