182 行
9.0 KiB
Objective-C
182 行
9.0 KiB
Objective-C
#import <CoreVideo/CoreVideo.h>
|
|
|
|
@interface DYTrackingPreviewImage : NSView
|
|
- (void)showFrame:(NSDictionary *)frame;
|
|
@end
|
|
|
|
@implementation DYTrackingPreviewImage {
|
|
NSDictionary *_frame;
|
|
NSImage *_image;
|
|
}
|
|
- (instancetype)initWithFrame:(NSRect)frame {
|
|
if((self=[super initWithFrame:frame]))self.clipsToBounds=YES;
|
|
return self;
|
|
}
|
|
- (BOOL)isFlipped {return YES;}
|
|
- (BOOL)isAccessibilityElement {return YES;}
|
|
- (NSString *)accessibilityRole {return NSAccessibilityImageRole;}
|
|
- (void)dealloc {[_frame release];[_image release];[super dealloc];}
|
|
- (void)showFrame:(NSDictionary *)frame {
|
|
[_frame release];_frame=[frame copy];[_image release];_image=nil;
|
|
NSData *pixels=frame[@"rgba"];
|
|
NSUInteger w=[frame[@"width"] unsignedIntegerValue],h=[frame[@"height"] unsignedIntegerValue];
|
|
CVPixelBufferRef visionImage=(CVPixelBufferRef)frame[@"visionImage"];
|
|
size_t stride=w*4;
|
|
CGBitmapInfo bitmapInfo=kCGImageAlphaNoneSkipLast;
|
|
// Only frames actually presented in the coalesced UI are copied to a
|
|
// display bitmap. The analysis worker passes the shared buffer directly.
|
|
if(visionImage&&CVPixelBufferLockBaseAddress(visionImage,kCVPixelBufferLock_ReadOnly)==kCVReturnSuccess){
|
|
stride=CVPixelBufferGetBytesPerRow(visionImage);
|
|
pixels=[NSData dataWithBytes:CVPixelBufferGetBaseAddress(visionImage) length:stride*h];
|
|
CVPixelBufferUnlockBaseAddress(visionImage,kCVPixelBufferLock_ReadOnly);
|
|
bitmapInfo=kCGBitmapByteOrder32Little|kCGImageAlphaNoneSkipFirst;
|
|
}
|
|
if(w&&h&&pixels.length==stride*h){
|
|
CGDataProviderRef provider=CGDataProviderCreateWithCFData((CFDataRef)pixels);
|
|
CGColorSpaceRef space=CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
|
|
CGImageRef cg=CGImageCreate(w,h,8,32,stride,space,bitmapInfo,provider,nil,true,kCGRenderingIntentDefault);
|
|
if(cg){_image=[[NSImage alloc]initWithCGImage:cg size:NSMakeSize(w,h)];CGImageRelease(cg);}
|
|
CGColorSpaceRelease(space);CGDataProviderRelease(provider);
|
|
}
|
|
self.accessibilityLabel=_image?[NSString stringWithFormat:@"同步预览 · 源帧 %.3f 秒 · %@",[frame[@"time"] doubleValue],
|
|
frame[@"sourceShape"]?@"取样与覆盖区域":@"蒙版轮廓"]:@"等待源帧";
|
|
[self setNeedsDisplay:YES];
|
|
}
|
|
- (void)drawShape:(NSData *)shape nodes:(NSData *)nodes inRect:(NSRect)rect source:(BOOL)source reference:(BOOL)reference {
|
|
if(shape.length!=sizeof(DYMaskParameters))return;
|
|
DYMaskParameters p;[shape getBytes:&p length:sizeof(p)];
|
|
double aspect=rect.size.width/rect.size.height,c=cos(p.angle),s=sin(p.angle);
|
|
auto point=[&](double x,double y){
|
|
double px=x*p.width*aspect,py=y*p.height;
|
|
return NSMakePoint(rect.origin.x+(p.centerX+(px*c-py*s)/aspect)*rect.size.width,
|
|
rect.origin.y+(p.centerY+px*s+py*c)*rect.size.height);
|
|
};
|
|
NSBezierPath *path=[NSBezierPath bezierPath];
|
|
auto add=[&](NSPoint position,int i){if(i)[path lineToPoint:position];else [path moveToPoint:position];};
|
|
if(p.kind==DYMaskBezier){
|
|
auto contour=dyFlattenMask((const DYBezierNode *)nodes.bytes,nodes.length/sizeof(DYBezierNode));
|
|
for(int i=0;i<contour.count;i++)add(point(contour.x[i],contour.y[i]),i);
|
|
}else if(p.kind==DYMaskRectangle){
|
|
const double corners[][2]={{-.5,-.5},{.5,-.5},{.5,.5},{-.5,.5}};
|
|
for(int i=0;i<4;i++)add(point(corners[i][0],corners[i][1]),i);
|
|
}else{
|
|
for(int i=0;i<80;i++){double a=i*2*M_PI/80;add(point(.5*cos(a),.5*sin(a)),i);}
|
|
}
|
|
[path closePath];path.lineWidth=3.5;[NSColor.blackColor setStroke];[path stroke];
|
|
if(source){CGFloat dashes[]={4,3};[path setLineDash:dashes count:2 phase:0];}
|
|
[(reference?NSColor.systemGreenColor:source?NSColor.systemCyanColor:NSColor.systemYellowColor) setStroke];path.lineWidth=1.6;[path stroke];
|
|
}
|
|
- (void)drawRect:(NSRect)dirty {
|
|
[NSColor.blackColor setFill];NSRectFill(NSIntersectionRect(dirty,self.bounds));
|
|
if(!_image||NSIsEmptyRect(self.bounds))return;
|
|
double aspect=[_frame[@"aspect"] doubleValue];
|
|
if(!std::isfinite(aspect)||aspect<=0)aspect=_image.size.width/_image.size.height;
|
|
CGFloat w=MIN(NSWidth(self.bounds),NSHeight(self.bounds)*aspect),h=w/aspect;
|
|
NSRect picture=NSMakeRect(NSMidX(self.bounds)-w/2,NSMidY(self.bounds)-h/2,w,h);
|
|
[NSGraphicsContext saveGraphicsState];
|
|
NSRectClip(picture);
|
|
if([_frame[@"flip"] boolValue]){
|
|
[NSGraphicsContext saveGraphicsState];
|
|
NSAffineTransform *flip=[NSAffineTransform transform];[flip translateXBy:0 yBy:2*NSMidY(picture)];[flip scaleXBy:1 yBy:-1];[flip concat];
|
|
}
|
|
[_image drawInRect:picture fromRect:NSZeroRect operation:NSCompositingOperationSourceOver fraction:1 respectFlipped:YES hints:nil];
|
|
if([_frame[@"flip"] boolValue])[NSGraphicsContext restoreGraphicsState];
|
|
[self drawShape:_frame[@"shape"] nodes:_frame[@"nodes"] inRect:picture source:NO reference:NO];
|
|
[self drawShape:_frame[@"sourceShape"] nodes:nil inRect:picture source:YES reference:NO];
|
|
if(_frame[@"referenceShape"]){[self drawShape:_frame[@"referenceShape"] nodes:nil inRect:picture source:YES reference:YES];}
|
|
[NSGraphicsContext restoreGraphicsState];
|
|
}
|
|
@end
|
|
|
|
@interface DYTrackingProgressBar : NSView
|
|
@property(nonatomic) double doubleValue;
|
|
@end
|
|
|
|
@implementation DYTrackingProgressBar
|
|
- (BOOL)isAccessibilityElement {return YES;}
|
|
- (NSString *)accessibilityRole {return NSAccessibilityProgressIndicatorRole;}
|
|
- (id)accessibilityValue {return @(_doubleValue);}
|
|
- (id)accessibilityMinValue {return @0;}
|
|
- (id)accessibilityMaxValue {return @1;}
|
|
- (void)setDoubleValue:(double)value {
|
|
_doubleValue=std::clamp(value,0.,1.);
|
|
[self setNeedsDisplay:YES];
|
|
}
|
|
- (void)drawRect:(NSRect)dirty {
|
|
NSRect rail=NSInsetRect(self.bounds,0,2);
|
|
CGFloat radius=NSHeight(rail)/2;
|
|
NSBezierPath *track=[NSBezierPath bezierPathWithRoundedRect:rail xRadius:radius yRadius:radius];
|
|
[[[NSColor labelColor] colorWithAlphaComponent:.18] setFill];
|
|
[track fill];
|
|
if(_doubleValue<=0)return;
|
|
[NSGraphicsContext saveGraphicsState];[track addClip];
|
|
NSRect fill=rail;fill.size.width*=_doubleValue;
|
|
[NSColor.controlAccentColor setFill];NSRectFill(fill);
|
|
[NSGraphicsContext restoreGraphicsState];
|
|
}
|
|
@end
|
|
|
|
@interface DYTrackingPreview : NSView
|
|
- (instancetype)initWithTarget:(id)target action:(SEL)action;
|
|
- (void)applyUpdate:(NSDictionary *)info;
|
|
- (void)reset;
|
|
@property(nonatomic,readonly) BOOL running;
|
|
@property(nonatomic,readonly) NSString *maskID;
|
|
@end
|
|
|
|
@implementation DYTrackingPreview {
|
|
DYTrackingPreviewImage *_preview;
|
|
NSTextField *_title,*_detail,*_status;
|
|
NSButton *_action;
|
|
DYTrackingProgressBar *_progress;
|
|
NSString *_maskID;
|
|
BOOL _running;
|
|
}
|
|
- (BOOL)isFlipped {return YES;}
|
|
- (BOOL)running {return _running;}
|
|
- (NSString *)maskID {return _maskID;}
|
|
- (void)dealloc {[_maskID release];[super dealloc];}
|
|
- (instancetype)initWithTarget:(id)target action:(SEL)action {
|
|
if((self=[super initWithFrame:NSZeroRect])){
|
|
self.identifier=@"DYTrackingPreview";
|
|
_title=[DYThemeLabel labelWithString:@"跟踪预览"];_title.font=[NSFont systemFontOfSize:12 weight:NSFontWeightSemibold];
|
|
_detail=[DYThemeLabel labelWithString:@""];_detail.font=[NSFont monospacedDigitSystemFontOfSize:10 weight:NSFontWeightRegular];
|
|
_status=[DYThemeLabel labelWithString:@""];_status.font=[NSFont systemFontOfSize:11];_status.maximumNumberOfLines=4;_status.lineBreakMode=NSLineBreakByWordWrapping;
|
|
_action=DYWorkspaceButton(@"停止跟踪",target,action);
|
|
_progress=[[[DYTrackingProgressBar alloc]init]autorelease];_progress.accessibilityLabel=@"跟踪进度";
|
|
_preview=[[[DYTrackingPreviewImage alloc]init]autorelease];[_preview showFrame:nil];
|
|
for(NSView *view in @[_title,_detail,_status,_action,_progress,_preview])[self addSubview:view];
|
|
self.hidden=YES;
|
|
}
|
|
return self;
|
|
}
|
|
- (void)layout {
|
|
[super layout];
|
|
CGFloat w=NSWidth(self.bounds),h=NSHeight(self.bounds),body=MAX(1,w-16);
|
|
_title.frame=NSMakeRect(8,6,MAX(1,w-108),18);
|
|
_action.frame=NSMakeRect(MAX(0,w-98),2,92,24);
|
|
_progress.frame=NSMakeRect(8,28,body,10);
|
|
BOOL compact=h<280;_status.maximumNumberOfLines=compact?1:3;_status.frame=NSMakeRect(8,compact?38:43,body,compact?18:52);
|
|
CGFloat previewTop=compact?60:102;_preview.frame=NSMakeRect(8,previewTop,body,MAX(1,h-previewTop-24));
|
|
_detail.frame=NSMakeRect(8,MAX(previewTop,h-20),body,18);
|
|
}
|
|
- (void)applyUpdate:(NSDictionary *)info {
|
|
if(![_maskID isEqual:info[@"mask"]]){[_maskID release];_maskID=[info[@"mask"]copy];}
|
|
BOOL active=[info[@"tracking"] boolValue];
|
|
if(active&&!info[@"shape"]){[_preview showFrame:nil];_detail.stringValue=@"";_title.stringValue=@"跟踪预览";}
|
|
if(info[@"shape"]){
|
|
BOOL stamp=info[@"sourceShape"]!=nil;
|
|
_title.stringValue=stamp?@"图章跟踪预览":@"蒙版跟踪预览";
|
|
[_preview showFrame:info];
|
|
_detail.stringValue=[NSString stringWithFormat:@"%.3fs %@",[info[@"time"] doubleValue],stamp?(info[@"referenceShape"]?@"青色取样 · 黄色覆盖 · 绿色参考":@"青色取样 · 黄色覆盖"):@"黄色轮廓"];
|
|
}
|
|
NSUInteger total=[info[@"total"] unsignedIntegerValue];
|
|
if(active)_progress.doubleValue=total?[info[@"completed"] doubleValue]/total:0;
|
|
_running=active;_action.title=active?@"停止跟踪":@"返回示波器";_action.accessibilityLabel=_action.title;
|
|
_status.stringValue=info[@"message"]?:@"";_status.toolTip=_status.stringValue;
|
|
}
|
|
- (void)reset {
|
|
_progress.doubleValue=0;[_preview showFrame:nil];[_maskID release];_maskID=nil;_running=NO;self.hidden=YES;
|
|
}
|
|
@end
|