Re: Drawing: The Right place to keep bounds
Re: Drawing: The Right place to keep bounds
- Subject: Re: Drawing: The Right place to keep bounds
- From: Graham Cox <email@hidden>
- Date: Thu, 26 Nov 2009 11:25:15 +1100
On 26/11/2009, at 3:40 AM, Paul Bruneau wrote:
> Thank you, I was thinking of the bounding box as dependent on the view. For example, I think of a "zoom" value for a view--that affects the bounding box, doesn't it? Or do I use a sort of "natural bounding box" for my objects that is then modified by the view's zoom?
>
> I guess this is what you mean by "a bounding box transformed into view coordinates". Yes, that is the way I am thinking of it, so that I can do things like hit-testing in however many views my user might have open. So OK I will start thinking about a parallel per-view model.
I can't comment too much on Sketch, but I can on DrawKit, which uses a similar architecture, but also supports multiple (parallel) views of the same model (drawing).
Things like zoom and scroll position are properties of the view, not the model. Each graphic object has a bounding rectangle which occupies some particular part of the drawing's coordinate space. That bounding rectangle never changes as long as the size and position (and angle, in my case) of the object remains the same - zooming and scrolling do not affect this. When a view wants to show some part of the model, it asks each object to draw itself, but does so having established the graphics transform for the view (which is affected by zoom and scroll, but is an intrinsic property of NSView, so there isn't much work to do). This transform maps the view's dirty rectangles to the coordinate system of the underlying drawing model, so that any graphic object can directly compare its own bounds against the view's dirty rects with -needsToDrawRect: and so go ahead or not.
If there are different views of the same drawing, the drawing itself is unaware of it - it just gets called as many times as necessary to draw as many views as there are, but a different view transform will be set for each one.
When an object in the model is moved so that its bounding rect changes, the relevant parts of all views must be marked dirty to ensure that the views redraw the object to show the change. Again, this is done in a way that keeps the model unaware of the view(s). The bounds change is signalled to the view controllers which in turn mark the bounds dirty using -setNeedsDisplayInRect: The appropriate view transform for the view ensures that the right part of the view is so marked, which again is automatically handled by NSView.
The scroll position of the view transform is set automatically when using NScrollView, and the zoom aspect of it is set by using -setUnitSquareToSize:
Since the code for making the view zoomable has nothing to do with what is actually drawn, it's entirely reusable: http://apptree.net/gczoomview.htm which just gives you some simple high-level zooming actions built on -scaleUnitSquareToSize:
--Graham
_______________________________________________
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