Re: Attempting to draw upon a CGLayer upon a NSView
Re: Attempting to draw upon a CGLayer upon a NSView
- Subject: Re: Attempting to draw upon a CGLayer upon a NSView
- From: "Frederick C. Lee" <email@hidden>
- Date: Fri, 10 Mar 2006 20:25:21 -0800
Thanks for replying.
I'm still having a problem connecting the CGLayer to the GUI for
human interaction.
1) I've created the CGLayer (via context) within the subView's
initWithFrame; and created the Layer's Context:
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
bezierPath = [[NSBezierPath bezierPath] retain];
CGSize layerSize = CGSizeMake(frame.size.width,
frame.size.height);
CGContextRef context = [[NSGraphicsContext currentContext]
graphicsPort];
layer = CGLayerCreateWithContext(context, layerSize, NULL);
if (layer != NULL) {
// Get the context corresonding to the layer.
drawingLayerContext = CGLayerGetContext(layer);
}
}
return self;
}
...
==========
2) Making the Drawing Layer Context the 'current' context.
Note: *** I'm having trouble converting Quartz context -->
Cocoa context: ***
- (void)mouseDown:(NSEvent *)theEvent {
// Need to make the drawing Context current:
[NSGraphicsContext setCurrentContext:(NSGraphicsContext *)
drawingLayerContext]; <-- problem.
...
[bezierPath moveToPoint:loc];
}
The result here:
2006-03-10 ... -[NSCFType isDrawingToScreen]: selector not recognized
[self = 0x3bb110]
2006-03-10 ... No current point for line
============
3) Attempting to draw an overlay (I'm assuming 'drawingLayerContext'
is the current context):
- (void)mouseDragged:(NSEvent *)theEvent {
...
[bezierPath lineToPoint:loc];
[self setNeedsDisplay:YES];
}
============
3a) Thinking of resetting context to the image-layer here:
- (void)mouseUp:(NSEvent *)theEvent {
NSLog(@"{mouseUp}");
}
============
4) Render the Drawing:
- (void)drawRect:(NSRect)rect {
[super drawRect:rect];
// Base Layer here
if (nil != [self image]) {
// Draw the Map image to the screen
[[self image] compositeToPoint:([self bounds].origin)
operation:NSCompositeSourceOver];
}
// Drawing Layer Here.
[[NSColor redColor] set];
[bezierPath stroke];
CGContextDrawLayerAtPoint(drawingLayerContext, CGPointZero, layer);
}
I tried doing the [NSGraphicsContext saveGraphicsState] &
[NSGraphicsContext restoreGraphicsState]
in the mouseDown()/mouseDragged() but they froze the display.
Am I on the right tack?
How do I convert Quartz context to Cocoa Context?
Ric.
On Mar 7, 2006, at 5:38 PM, Scott Thompson wrote:
First of all, you never create a CGLayer. You would have to create
it using CGLayerCreateWithContext. After creating the layer, you
could draw into it in the mouseDown routine if you wanted to by
getting it's CGContext (CGLayerGetContext) and making that the
current context using the methods of NSGraphicsContext.
Then in your drawRect routine you would draw the image and then
draw the layer using CGContextDrawLayerAtPoint.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden