文件
dayu-color/大鱼调色/Plugin/DYTrackingPreview.inc.h
T

168 行
7.9 KiB
Objective-C

@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];
if(w&&h&&pixels.length==w*h*4){
CGDataProviderRef provider=CGDataProviderCreateWithCFData((CFDataRef)pixels);
CGColorSpaceRef space=CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
CGImageRef cg=CGImageCreate(w,h,8,32,w*4,space,kCGImageAlphaNoneSkipLast,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 {
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];}
[(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];
[self drawShape:_frame[@"sourceShape"] nodes:nil inRect:picture source: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=2;_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);
_status.frame=NSMakeRect(8,43,body,32);
_preview.frame=NSMakeRect(8,80,body,MAX(1,h-106));
_detail.frame=NSMakeRect(8,MAX(80,h-22),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?@"青色取样 · 黄色覆盖":@"黄色轮廓"];
}
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