【发布时间】:2012-01-03 14:26:55
【问题描述】:
我创建了一个基于黑白位图将鼠标限制在某个区域的程序。该程序按原样 100% 运行,但使用了一种不准确但速度很快的算法来在鼠标偏离区域时重新定位鼠标。
目前,当鼠标移出该区域时,基本上是这样的:
- 在区域内预定义的静态点和鼠标的新位置之间绘制一条线。
- 找到该线与允许区域边缘的交点。
- 鼠标移动到该点。
这有效,但仅适用于 完美 用于将预定义点设置在精确中心的完美圆。不幸的是,这将永远不会发生。该应用程序将用于各种矩形和不规则、无定形的形状。在这样的形状上,画线与边缘相交的点通常不是形状上离鼠标最近的点。
我需要创建一个新的算法来找到最近点到鼠标在允许区域边缘的新位置。我怎样才能做到这一点?优选地,该方法应该能够足够快地执行,以便在将鼠标拖动到区域边缘时提供平滑的鼠标移动。
(我在 OS X 10.7 上的 Objective C / Cocoa 中执行此操作,但是,如果您不想输入代码或不了解 Objective C / C,则可以使用伪代码)
谢谢!
这是我目前的算法:
#import <Cocoa/Cocoa.h>
#import "stuff.h"
#import <CoreMedia/CoreMedia.h>
bool
is_in_area(NSInteger x, NSInteger y, NSBitmapImageRep *mouse_mask){
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSUInteger pixel[4];
[mouse_mask getPixel:pixel atX:x y:y];
if(pixel[0]!= 0){
[pool release];
return false;
}
[pool release];
return true;
}
CGEventRef
mouse_filter(CGEventTapProxy proxy, CGEventType type, CGEventRef event, NSBitmapImageRep *mouse_mask) {
CGPoint point = CGEventGetLocation(event);
float tX = point.x;
float tY = point.y;
if( is_in_area(tX,tY, mouse_mask)){
// target is inside O.K. area, do nothing
}else{
CGPoint target;
//point inside restricted region:
float iX = 600; // inside x
float iY = 500; // inside y
// delta to midpoint between iX,iY and tX,tY
float dX;
float dY;
float accuracy = .5; //accuracy to loop until reached
do {
dX = (tX-iX)/2;
dY = (tY-iY)/2;
if(is_in_area((tX-dX),(tY-dY),mouse_mask)){
iX += dX;
iY += dY;
} else {
tX -= dX;
tY -= dY;
}
} while (abs(dX)>accuracy || abs(dY)>accuracy);
target = CGPointMake(roundf(tX), roundf(tY));
CGDisplayMoveCursorToPoint(CGMainDisplayID(),target);
}
return event;
}
int
main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
stuff *stuff_doer = [[stuff alloc] init];
NSBitmapImageRep *mouse_mask= [stuff_doer get_mouse_mask];
CFRunLoopSourceRef runLoopSource;
CGEventMask event_mask;
event_mask = CGEventMaskBit(kCGEventMouseMoved) | CGEventMaskBit(kCGEventLeftMouseDragged) | CGEventMaskBit(kCGEventRightMouseDragged) | CGEventMaskBit(kCGEventOtherMouseDragged);
CGSetLocalEventsSuppressionInterval(0);
CFMachPortRef eventTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, 0, event_mask, mouse_filter, mouse_mask);
if (!eventTap) {
NSLog(@"Couldn't create event tap!");
exit(1);
}
runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
CGEventTapEnable(eventTap, true);
CFRunLoopRun();
CFRelease(eventTap);
CFRelease(runLoopSource);
[pool release];
exit(0);
}
这是可能使用的区域位图示例,黑色是允许的区域。 这说明了为什么转换为多边形并不方便,甚至不合情理。
【问题讨论】:
标签: objective-c c xcode macos cocoa