Re: Drawing from upper left
Re: Drawing from upper left
- Subject: Re: Drawing from upper left
- From: Ken Tozier <email@hidden>
- Date: Tue, 26 Jun 2007 11:52:30 -0400
On Jun 26, 2007, at 10:10 AM, Wagner Truppel wrote:
Hi Ken,
you could do all your logic using the bottom-left origin, but from
your message it appears that you prefer to use the upper-left origin.
Ultimately, It doesn't matter to me where it's drawn from. The
important thing is that the offset of subviews from the top of the
window remains constant on window resizes. Conceptually, it's much
easier to think about if the top left corner of views and frames were
the origin. With the origin in the lower left, if you want to
preserve the top offset, you have to recalculate the frames for every
subview on every superview resize. But that's just tilting at
windmills. I doubt this lower left convention will ever change...
That's perfectly fine, too, but then it seems to me that the
simplest approach is to have *all* your custom views return YES to -
(BOOL)isFlipped. Then you can think in terms of upper-left origins
and not have to deal with converting anything yourself.
Alternatively, you can work with non-flipped views and *still*
think in terms of upper-left origins if you proceed as follows: do
all your "thinking" and processing using the upper-left paradigm
but when it comes time to actually perform any drawing inside of
any of your custom views, replace every y coordinate by (H - y)
where H is the height of the view in question. The x coordinates
are not, and should never be, affected.
Here's an example to clarify the alternative procedure I just
suggested. Say you have two rectangles, A and B. In the default
coordinate system (bottom-left origin), the bottom edges of these
rectangles have y coordinates yA and yB. In the flipped system
(upper-left origin), the same edges have y coordinates yA' = H - yA
and yB' = H - yB, but the relative position of the two edges
remains the same, so the rectangles are drawn as shown below
regardless of whether the view is flipped or not.
|--------------------------------------|
| | | |
| | | |
| ------------- | H - yA |
| | | | | |
| | A | | | |
| | | | | |
| -------------... | .......... |
| | |
| ------------- H - yB | H
| | | | |
| | B | | | |
| | | | yA |
| -------------....... | |
| | | |
| yB | |
| | | |
|--------------------------------------|
When you return YES to -(BOOL)isFlipped, the view takes care of
this transformation for you, but if you're having trouble using
isFlipped and/or prefer not to use it, then just do all the logic
*as if* you were flipped and then perform the transformation above
at the last moment before drawing. *DON'T* do both, however, that
is, don't return YES to -(BOOL)isFlipped and do the transformation
yourself.
Thanks Wagner. I'll need to digest this for a bit.
Ken
_______________________________________________
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