Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MrFusion

macrumors 6502a
Original poster
Jun 8, 2005
613
0
West-Europe
I have a subclass of NSView that is able to track the mouse.
In the mouseMoved method I can determine the mouse position from the NSEvent.

Now I want to draw something at that position, however the coordinate system seems to have shifted. In a normal NSView subclass, you can only draw within the view itself and the origin of the coordinate system corresponds to the lower left corner of the NSView. When I do any drawing in the mouseMoved, it's relative to the lower left corner of the window.

Any trick/idea as to how I can find out where my NSView is, or to restrict drawing in the manner accustomed to the NSView subclass?

Thanks.

EDIT:
Another case of formulating the question to find the answer, although it's an inefficient one.

Adding a variable to class that holds a NSPoint currentLocation.
In mouseMoved: set the currentLocation and set needsDisplay: to yes for the view.

But this causes the entire view to redraw, which is unnecessary. Only a small portion needs to redraw itself.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Are you using [self convertPoint:[event locationInWindow] fromView:nil] in your mouseMoved event?

OK about the drawing part... drawRect: gets called when some part of the view needs to be redrawn. Quartz is smart enough to pass you the rect of what part needs to be drawn. However, the majority of the time you will always redraw everything. To avoid this, create some smart code that only redraws certain parts based on the passed in rect, or use subviews (easier).
 

MrFusion

macrumors 6502a
Original poster
Jun 8, 2005
613
0
West-Europe
Are you using [self convertPoint:[event locationInWindow] fromView:nil] in your mouseMoved event?
OK about the drawing part... drawRect: gets called when some part of the view needs to be redrawn. Quartz is smart enough to pass you the rect of what part needs to be drawn. However, the majority of the time you will always redraw everything. To avoid this, create some smart code that only redraws certain parts based on the passed in rect, or use subviews (easier).

I found out yesterday while working on the problem in this thread.
.I now call [frame bounds] at the beginning of my drawRect:
In the next iteration I'll change this so that only the necessary part is redrawn.
The more features I add, the more I learn about drawing. The greater complexity/feature range casuses me to rework the class anyway.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.