mouseMoved question

Discussion in 'Mac Programming' started by robvas, Oct 21, 2010.

  1. macrumors 68000

    Joined:
    Mar 29, 2009
    Location:
    USA
    #1
    Pretend you have a chess/checkerboard type setup, and you want to highlight the square on the board that is under the mouse cursor. I have it working except that you have to click the mouse in order for it to work.

    I would like it to automatically highlight once you roll the mouse cursor over it, but it's not working. I have tried to send the setacceptsMouseMoved:YES message, but I guess I'm not doing it wrong.

    Can someone point me in the right direction? Here's the code:

    Code:
    //
    //  StretchView.m
    //
    
    #import "StretchView.h"
    
    @implementation StretchView
    
    /*** this doesn't seem to affect anything 
     
    - (void)awakeFromNib
    {
    	[[self window] setAcceptsMouseMovedEvents:YES];
    }
     
    ***/
     
    /*** this doesn't either
     
    - (id)initWithFrame:(NSRect)frame
    {
    	self = [super initWithFrame:frame];
    	
    	if (self)
    	{
    		if ([self window] != nil)
    			[[self window] setAcceptsMouseMovedEvents:YES];
    	}
    	return self;
    }
    ***/
    
    - (void)drawRect:(NSRect)rect
    {
    	NSBezierPath *highlightRect = [NSBezierPath bezierPath];
    	NSRect bounds = [self bounds];
    	
    	[[NSColor grayColor] set];
    	[NSBezierPath fillRect:bounds];
    	
    	NSPoint highlight[] = { tx*16, ty*16, 
    							tx*16, (ty*16)+16,
    							(tx*16)+16, (ty*16)+16,
    							(tx*16)+16, ty*16 };
    
    	[highlightRect moveToPoint:highlight[0]];
    	[highlightRect lineToPoint:highlight[1]];
    	[highlightRect lineToPoint:highlight[2]];
    	[highlightRect lineToPoint:highlight[3]];
    	[highlightRect closePath];
    	
    	[[NSColor yellowColor] set];
    	[highlightRect stroke];
    }
    
    
    - (void)mouseMoved:(NSEvent *)event
    {
    	NSPoint p = [event locationInWindow];
    	downPoint = [self convertPoint:p fromView:nil];
    	currentPoint = downPoint;
    	
    	[self setNeedsDisplay:YES];
    	
    	hx = downPoint.x;
    	hy = downPoint.y;
    	
    	NSLog(@"Real X: %f, Real Y: %f. Chosen tile = (%f, %f)", 
    		  downPoint.x, downPoint.y, tx, ty);
    }
    
    
    - (void)mouseDown:(NSEvent *)event
    {
    	NSPoint p = [event locationInWindow];
    	downPoint = [self convertPoint:p fromView:nil];
    	currentPoint = downPoint;
    	
    	[self setNeedsDisplay:YES];
    	
    	hx = downPoint.x;
    	hy = downPoint.y;
    	
    	tx = floor(hx / 16);
    	ty = floor(hy / 16);
    	
    	NSLog(@"Real X: %f, Real Y: %f. Chosen tile = (%f, %f)", 
    		  hx, hy, tx, ty);
    }
    
    @end
    
    
    Code:
    //
    //  StretchView.h
    //
    //
    
    #import <Cocoa/Cocoa.h>
    
    @interface StretchView : NSView {
    	
    	NSPoint downPoint, currentPoint;
    	
    	float tx, ty;
    	float hx, hy;
     
    }
    
    @end
    
    
    StretchView is just a subclass of NSView, embedded in an NSScroller
     
  2. macrumors member

    Joined:
    Oct 9, 2008

Share This Page