Some questions/problems regarding CALayers
Some questions/problems regarding CALayers
- Subject: Some questions/problems regarding CALayers
- From: Henri Häkkinen <email@hidden>
- Date: Tue, 1 Dec 2009 03:35:57 +0200
Hello.
I'm trying to implement my custom NSView derived class to draw a chessboard with chess pieces on it. I'm using Core Animation's CALayer classes since I want to use animation with other effects later on.
Currently I have have Board and Piece classes, which both derive from CALayer. Board overrides resizeWithSuperlayerSize which resizes bounds to always keep the layer's aspect ratio fixed (since chessboards are always square) and it's position centered relative to the superlayer's frame, and in drawInContext I draw the chess squares with two alternating colors. Piece class likewise overrides drawInContext to draw NSImage on it, which represents piece's vector image loaded from a PDF file. Board layer is added as a sublayer of the NSView's root layer and Piece layers are added as a sublayer of the Board layer and so forth.
My problem now is, however, that I am not sure how do I keep the position and size of a Piece layer fixed to a specific square in the Board layer. Board's bounding rectangle can be any size and it changes dynamically as the view gets resized. Piece layer does not know it's square coordinates on the chessboard, except by the bounds rectangle.
I tried using two methods:
a) using autoresizeMask on a Piece layer with kCALayerWidthSizable | kCALayerHeightSizable
b) using sublayerTransform on the Board layer and then using a 2D scaling matrix to define a virtual coordinate space of (0, 0, 8, 8) where (0, 0) corresponds to the lower left and (8, 8) corresponds the top right corner of the board (this would be ideal since placing pieces on squares would be easy)
Neither of these didn't work very well. By using kCALayerWidthSizable | kCALayerHeightSizable doesn't keep the size of a Piece in synch with the Board somehow, and by using sublayerTransform makes the piece images aliased since they are rasterized in very low resolution and then scaled up using pixel transformations (rather than by transforming the coordinate space of the vector image).
Can anyone recommend me any better way? And by the way, I am using NSImage rather than CGImageRef because it seems that Core Graphics images only supports raster images. Perhaps a better way would be using OpenGL to draw the chessboard altogether instead of using Cocoa/Core Graphics. But then again, I wouldn't be able to use Core Animation effects ...
Henri H.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden