Updating the bounds of a graphic wrapped in an NSManagedObject during a dragging goperation
Updating the bounds of a graphic wrapped in an NSManagedObject during a dragging goperation
- Subject: Updating the bounds of a graphic wrapped in an NSManagedObject during a dragging goperation
- From: Remco Poelstra <email@hidden>
- Date: Thu, 11 Jan 2007 23:39:04 +0100
Hi,
I'm writing a small application which does some drawing. The user
should be able to move the graphics around with the mouse. That's
actually all working very well, but I'm totally not happy with the
implementation.
The graphic is an NSBezierPath which is wrapped in an
NSManagedObject. The NSManagedObject has an attribute "bounds" in
order to track changes to the NSBezierPath bounds to update the
display. The NSManagedObject also has a method setOffsetPosition
which will call transformUsingAffineTransform on it's graphic and
then update it's bounds attribute using the new graphic bounds. The
setOffsetPosition is called from the NSView's method mouseDragged,
where it is supplied the deltaX and deltaY information from the event.
I'm not very happy with the setOffsetPosition method, since it just
doesn't seem to fit there given the design-pattern of bindings and
KVC/O. So I was wondering how do other people update their model to
reflect some dragging operation and how do they communicate it back
to the view?
I included some code below for clarity.
Thanks in advance,
Remco Poelstra
The view:
-(void)mouseDragged:(NSEvent *)event {
NSPoint delta=NSMakePoint([event deltaX],-1*[event deltaY]);
[chartElementsController setValue:[NSValue valueWithPoint:delta]
forKeyPath:@"selection.offsetPosition"];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if ([keyPath isEqualToString:@"bounds"]) {
[self setNeedsDisplay:YES];
return;
}
}
The NSManagedObject:
- (void) setOffsetPosition:(NSPoint) delta {
NSAffineTransform *transform=[NSAffineTransform transform];
[transform translateXBy:delta.x yBy:delta.y];
[[self graphic] transformUsingAffineTransform:transform];
[self setBounds:[[self graphic] bounds]];
}
- (NSRect)bounds {
NSRect tempBounds;
[self willAccessValueForKey: @"bounds"];
tempBounds=bounds;
[self didAccessValueForKey: @"bounds"];
return tempBounds;
}
- (void) setBounds:(NSRect)value {
[self willChangeValueForKey: @"bounds"];
bounds=value;
[self didChangeValueForKey: @"bounds"];
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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