// Native per-tool mask editor, shared by original and repeated tool cards. @interface DYToolMaskPanel : DYFlippedView @property(nonatomic,readonly) CGFloat contentHeight; @property(nonatomic,readonly) CGFloat contentWidth; @property(nonatomic) BOOL workspaceMode; @property(nonatomic,readonly) NSString *selectedMaskID; - (instancetype)initWithManager:(id)manager nodeID:(NSString*)nodeID; - (void)refreshWithDocument:(NSDictionary*)document time:(CMTime)time; - (void)addMenu:(NSView*)anchor; - (void)selectMaskID:(NSString*)maskID; @end @implementation DYToolMaskPanel { id _host;NSString *_nodeID,*_maskID;NSDictionary *_document;CMTime _time; NSPopUpButton *_stampMode;NSButton *_reference,*_referenceReset,*_manualKey,*_lastTrusted;NSTextField *_stampGuide;BOOL _trackingBusy; NSPopUpButton *_list,*_combine;NSButton *_eye,*_add,*_remove,*_invert,*_canvas,*_clear,*_penRedraw,*_penFinish,*_penCancel;NSMutableArray *_rows,*_tracking;NSTextField *_trackingTitle,*_status;BOOL _refreshing,_drawing,_canvasActive,_workspaceMode;CGFloat _contentHeight; } @synthesize workspaceMode=_workspaceMode; - (NSString*)selectedMaskID{return _maskID;} - (void)setWorkspaceMode:(BOOL)workspaceMode{if(_workspaceMode==workspaceMode)return;_workspaceMode=workspaceMode;[self setNeedsLayout:YES];} - (CGFloat)contentHeight{return _workspaceMode?self.bounds.size.height:_contentHeight;} - (CGFloat)contentWidth{ if(!_workspaceMode)return self.bounds.size.width;NSUInteger count=0;for(NSDictionary *row in _rows)if(![row[@"label"]isHidden])count++;NSInteger perColumn=MAX(1,NSInteger((self.bounds.size.height-48)/44));BOOL pen=!_penRedraw.hidden,shape=_tracking.count&&![_tracking[0]isHidden];CGFloat actions=shape&&self.bounds.size.height<240?432:216;if(_stampMode&&!_stampMode.hidden)actions+=240;if(pen&&shape&&self.bounds.size.height<242)actions+=216;return MAX(536,16+ceil(double(count)/perColumn)*184+actions); } - (void)selectMaskID:(NSString*)maskID{ if([_maskID isEqual:maskID]){if(!_canvasActive)[self canvas:nil];return;}BOOL found=NO;for(NSDictionary *mask in DYMasksForTool(_document,_nodeID))if([mask[@"id"]isEqual:maskID]){found=YES;break;}if(!found)return; [_maskID release];_maskID=[maskID copy];_drawing=_canvasActive=_trackingBusy=NO;[self refreshWithDocument:_document time:_time];[self canvas:nil]; } - (instancetype)initWithManager:(id)manager nodeID:(NSString*)nodeID{ if((self=[super initWithFrame:NSZeroRect])){_host=manager;_nodeID=[nodeID copy];_rows=[NSMutableArray new];_tracking=[NSMutableArray new];self.identifier=@"toolMasks"; [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(trackingStatus:) name:@"DYToolTrackingStatus" object:_host]; [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(canvasSelected:) name:@"DYToolCanvasSelection" object:_host]; [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(canvasState:) name:@"DYToolCanvasState" object:_host]; }return self; } - (void)buildControls{if(_list)return; _list=[[NSPopUpButton alloc]initWithFrame:NSZeroRect pullsDown:NO];_list.target=self;_list.action=@selector(selection:);_list.font=[NSFont systemFontOfSize:13 weight:NSFontWeightMedium];_list.controlSize=NSControlSizeRegular;_list.accessibilityLabel=@"本层蒙版";[self addSubview:_list]; _eye=[DYButton(@"",self,@selector(enabled:))retain];_eye.image=[NSImage imageWithSystemSymbolName:@"eye" accessibilityDescription:@"蒙版启用"];_eye.imageScaling=NSImageScaleProportionallyUpOrDown;_eye.bordered=NO;[_eye setButtonType:NSButtonTypePushOnPushOff];[self addSubview:_eye]; _add=[DYButton(@"+",self,@selector(add:))retain];_add.toolTip=@"再添加一个蒙版";_add.accessibilityLabel=_add.toolTip;[self addSubview:_add]; _remove=[DYButton(@"−",self,@selector(remove:))retain];_remove.toolTip=@"删除选中的蒙版";_remove.accessibilityLabel=_remove.toolTip;[self addSubview:_remove]; _combine=[[NSPopUpButton alloc]initWithFrame:NSZeroRect pullsDown:NO];[_combine addItemsWithTitles:@[@"相加",@"减去",@"相交"]];_combine.font=[NSFont systemFontOfSize:13];_combine.controlSize=NSControlSizeRegular;_combine.target=self;_combine.action=@selector(combine:);_combine.accessibilityLabel=@"蒙版组合方式";[self addSubview:_combine]; _invert=[[NSButton checkboxWithTitle:@"反转" target:self action:@selector(inverted:)]retain];_invert.font=[NSFont systemFontOfSize:13];_invert.controlSize=NSControlSizeRegular;[self addSubview:_invert]; _canvas=[DYButton(@"画面编辑",self,@selector(canvas:))retain];_canvas.toolTip=@"在宿主画面拖动中心、大小、旋转与钢笔节点;回车闭合钢笔";_canvas.accessibilityLabel=@"编辑本层蒙版画面控件";[self addSubview:_canvas]; _penRedraw=[DYButton(@"重画钢笔",self,@selector(redrawPen:))retain];_penRedraw.toolTip=@"从空白重画钢笔;完成前保留原路径,可随时取消";_penRedraw.accessibilityLabel=@"重画钢笔蒙版";[self addSubview:_penRedraw]; _penFinish=[DYButton(@"完成绘制",self,@selector(penCommand:))retain];_penFinish.toolTip=@"闭合当前钢笔路径并保存";_penFinish.accessibilityLabel=@"完成钢笔绘制";[self addSubview:_penFinish]; _penCancel=[DYButton(@"取消绘制",self,@selector(penCommand:))retain];_penCancel.toolTip=@"取消当前绘制,保留原蒙版路径";_penCancel.accessibilityLabel=@"取消钢笔绘制";[self addSubview:_penCancel]; _clear=[DYButton(@"清除轨迹",self,@selector(clear:))retain];[self addSubview:_clear]; NSArray *names=@[@"强度",@"羽化",@"中心 X",@"中心 Y",@"宽度",@"高度",@"旋转",@"渐变宽度",@"色相中心",@"色相范围",@"色相柔边",@"饱和度下限",@"饱和度上限",@"亮度下限",@"亮度上限",@"输入黑",@"输入白",@"输入伽马"]; for(NSInteger i=0;i=2&&i<=6&&p.kind<4&&(!stamp||i!=6))||(i==7&&p.kind==3)||(i>=8&&i<=14&&p.kind==4)||(i>=15&&p.kind>=4);NSDictionary *row=_rows[i];for(NSView *v in row.allValues)v.hidden=!show;if(!show)continue;visible++;double lo=0,hi=1;if(i==2||i==3)lo=-1,hi=2;if(i==4||i==5||i==7)lo=.001,hi=2;if(i==6)lo=-180,hi=180;if(i==8)hi=360;if(i==9||i==10)hi=180;if(i==15)hi=.99;if(i==16)lo=.01,hi=2;if(i==17)lo=.01,hi=4;NSSlider *slider=row[@"slider"];slider.minValue=lo;slider.maxValue=hi;slider.doubleValue=values[i];[row[@"field"]setStringValue:[NSString stringWithFormat:@"%.3f",values[i]]];} BOOL shape=p.kind<3;for(NSButton *b in _tracking)b.hidden=!shape;_trackingTitle.hidden=!shape;_clear.hidden=!shape;_clear.enabled=[m[@"keys"]count]>0; BOOL pen=p.kind==DYMaskBezier;_penRedraw.hidden=!pen;_penFinish.hidden=!pen;_penCancel.hidden=!pen; [self updateDrawingControls]; _status.stringValue=m[@"status"]?:([m[@"keys"]count]?[NSString stringWithFormat:@"%lu 个轨迹键 · 修改几何会在当前帧建立手工键",(unsigned long)[m[@"keys"]count]]:pen?@"重画钢笔后,点击起点或“完成绘制”闭合;取消或 Esc 保留原路径。":@"各蒙版只影响当前工具层"); if([_nodeID isEqual:@"shared"]){_list.accessibilityLabel=@"共享蒙版";_add.toolTip=@"添加共享蒙版";_remove.toolTip=@"删除此共享蒙版及其模块关联,可撤销";if(!m[@"status"]&&!pen&&![m[@"keys"]count])_status.stringValue=[NSString stringWithFormat:@"共享给 %lu 个模块 · 编辑与跟踪同步更新所有关联模块",(unsigned long)DYToolIDsForSharedMask(doc,_maskID).count];} if(stamp){_remove.toolTip=@"删除仿制图章,可撤销";if(!m[@"status"]&&![m[@"keys"]count])_status.stringValue=DYStampUsesLinkedTracking(m)?@"取样随修复区移动,取样区无需纹理;黄色椭圆仅覆盖瑕疵。":@"独立跟踪要求取样区和覆盖区都有纹理;纯色取样请切换跟随模式。";} _status.toolTip=_status.stringValue; // Keep every scalar readable at the narrowest native inspector width. A // label/value line and a separate full-width slider avoid compressed controls. _contentHeight=136+(stamp?168:0)+(pen?84:0)+visible*58+(shape?184:0)+58;[self setNeedsLayout:YES];_refreshing=NO; } - (void)layout{ [super layout];CGFloat w=self.bounds.size.width; if(_workspaceMode){ CGFloat h=self.bounds.size.height;_eye.frame=NSMakeRect(8,4,26,28);_list.frame=NSMakeRect(38,4,132,28);_add.frame=NSMakeRect(174,4,26,28);_remove.frame=NSMakeRect(204,4,26,28);_combine.frame=NSMakeRect(242,4,92,28);_invert.frame=NSMakeRect(344,6,68,24);_canvas.frame=NSMakeRect(422,4,100,28); NSInteger perColumn=MAX(1,NSInteger((h-48)/44));NSUInteger index=0; for(NSDictionary *row in _rows){NSTextField *label=row[@"label"];if(label.hidden)continue;CGFloat x=8+(index/perColumn)*184,y=42+(index%perColumn)*44;label.font=[NSFont systemFontOfSize:11];label.frame=NSMakeRect(x,y+3,100,18);[row[@"field"]setFont:[NSFont monospacedDigitSystemFontOfSize:11 weight:NSFontWeightRegular]];[row[@"field"]setFrame:NSMakeRect(x+108,y,66,22)];[row[@"slider"]setFrame:NSMakeRect(x,y+23,174,18)];index++;} CGFloat x=16+ceil(double(index)/perColumn)*184,y=42,aw=200;BOOL shape=_tracking.count&&![_tracking[0]isHidden],pen=_penRedraw&&!_penRedraw.hidden; if(_stampMode&&!_stampMode.hidden){ _stampMode.frame=NSMakeRect(x,y,224,26);_reference.frame=NSMakeRect(x,y+32,114,25);_referenceReset.frame=NSMakeRect(x+120,y+32,104,25);_manualKey.frame=NSMakeRect(x,y+64,114,25);_lastTrusted.frame=NSMakeRect(x+120,y+64,104,25);_stampGuide.frame=NSMakeRect(x,y+96,224,MAX(18,MIN(76,h-y-100)));x+=240; } if(pen){_penRedraw.frame=NSMakeRect(x,y,aw,25);y+=29;_penFinish.frame=NSMakeRect(x,y,97,25);_penCancel.frame=NSMakeRect(x+103,y,97,25);y+=33;} if(shape){if(y>42&&y+126>h-4){x+=216;y=42;}_trackingTitle.frame=NSMakeRect(x,y,aw,18);y+=20;const int order[]={0,4,1,3,2,5};for(int i=0;i<6;i++)[_tracking[order[i]]setFrame:NSMakeRect(x+(i%2)*103,y+(i/2)*26,97,23)];y+=78;_clear.frame=NSMakeRect(x,y,aw,22);y+=26;} // Pen and tracking actions get adjacent columns when the short strip cannot // hold both; no vertical clipping and no hidden controls below the viewport. if(y+32>h){x+=216;y=42;}_status.font=[NSFont systemFontOfSize:10];_status.frame=NSMakeRect(x,y,aw,MIN(54,MAX(26,h-y-4)));return; } _eye.frame=NSMakeRect(0,4,32,34);_list.frame=NSMakeRect(38,4,MAX(60,w-114),34);_add.frame=NSMakeRect(w-70,4,32,34);_remove.frame=NSMakeRect(w-32,4,32,34); CGFloat combineWidth=MIN(112,MAX(90,w*.45));_combine.frame=NSMakeRect(0,46,combineWidth,34);_invert.frame=NSMakeRect(combineWidth+12,49,MAX(70,w-combineWidth-12),28);_canvas.frame=NSMakeRect(0,88,w,36); CGFloat y=136; if(_stampMode&&!_stampMode.hidden){CGFloat half=(w-8)/2;_stampMode.frame=NSMakeRect(0,y,w,28);_reference.frame=NSMakeRect(0,y+34,half,28);_referenceReset.frame=NSMakeRect(half+8,y+34,half,28);_manualKey.frame=NSMakeRect(0,y+68,half,28);_lastTrusted.frame=NSMakeRect(half+8,y+68,half,28);_stampGuide.frame=NSMakeRect(0,y+102,w,58);y+=168;} if(_penRedraw&&!_penRedraw.hidden){CGFloat bw=(w-8)/2;_penRedraw.frame=NSMakeRect(0,y,bw,34);_penFinish.frame=NSMakeRect(bw+8,y,bw,34);_penCancel.frame=NSMakeRect(0,y+40,w,34);y+=84;}for(NSDictionary *r in _rows){NSTextField *label=r[@"label"];if(label.hidden)continue;label.frame=NSMakeRect(2,y+6,MAX(60,w-100),22);[r[@"field"]setFrame:NSMakeRect(w-90,y,88,32)];[r[@"slider"]setFrame:NSMakeRect(0,y+32,w,24)];y+=58;} if(_tracking.count&&![_tracking[0]isHidden]){_trackingTitle.frame=NSMakeRect(2,y+2,w-4,20);y+=28;CGFloat bw=(w-8)/2;const int order[]={0,4,1,3,2,5};for(int i=0;i<6;i++)[_tracking[order[i]]setFrame:NSMakeRect((i%2)*(bw+8),y+(i/2)*38,bw,34)];y+=114;_clear.frame=NSMakeRect(0,y,w,34);y+=42;} _status.frame=NSMakeRect(2,y+2,w-4,52); } - (void)edit:(NSString*)title block:(void (^)(NSMutableDictionary*,NSMutableDictionary*))block{ ida=[_host apiForProtocol:@protocol(FxCustomParameterActionAPI_v4)];if(!a)return;[a startAction:self];@try{NSMutableDictionary *doc=DYMutablePlist(DYReadTools(_host,a.currentTime).document);NSMutableArray *masks=(id)DYMasksForTool(doc,_nodeID);NSMutableDictionary *mask=nil;for(NSMutableDictionary *m in masks)if([m[@"id"]isEqual:_maskID])mask=m;if(!mask)return;block(doc,mask);if(!DYValidToolDocument(doc)){NSBeep();return;}idundo=[_host apiForProtocol:@protocol(FxUndoAPI)];[undo startUndoGroup:title];DYWriteTools(_host,doc,a.currentTime);[undo endUndoGroup];}@finally{[a endAction:self];}[NSNotificationCenter.defaultCenter postNotificationName:DYModelChanged object:_host];} - (void)add:(id)sender{[self addMenu:_add];} - (void)addMenu:(NSView*)anchor{NSMenu *menu=[[[NSMenu alloc]initWithTitle:@"添加蒙版"]autorelease];for(int i=0;i<6;i++){NSMenuItem *item=[menu addItemWithTitle:@[@"椭圆",@"矩形",@"钢笔",@"线性渐变",@"HSL 选区",@"亮度选区"][i] action:@selector(addKind:) keyEquivalent:@""];item.target=self;item.tag=i;}[menu popUpMenuPositioningItem:nil atLocation:NSMakePoint(0,anchor.bounds.size.height) inView:anchor];} - (void)addKind:(NSMenuItem*)sender{ ida=[_host apiForProtocol:@protocol(FxCustomParameterActionAPI_v4)];if(!a)return;[a startAction:self];@try{NSMutableDictionary *doc=DYMutablePlist(DYReadTools(_host,a.currentTime).document);BOOL shared=[_nodeID isEqual:@"shared"];NSMutableArray *masks=[[DYMasksForTool(doc,_nodeID)mutableCopy]autorelease];if(masks.count>=(shared?DYSharedMaskLimit:DYToolMaskLimit)){NSBeep();return;}NSDictionary *mask=shared?DYAddSharedMask(doc,sender.tag):DYNewMask(sender.tag);if(!mask)return;if(!shared){[masks addObject:mask];if(!DYSetToolMasks(doc,_nodeID,masks))return;}[_maskID release];_maskID=[mask[@"id"]copy];NSDictionary *staged=[doc copy];[_document release];_document=staged;[self sendCanvasDrawing:sender.tag==DYMaskBezier];idundo=[_host apiForProtocol:@protocol(FxUndoAPI)];[undo startUndoGroup:shared?@"添加共享蒙版":@"添加工具蒙版"];DYWriteTools(_host,doc,a.currentTime);[undo endUndoGroup];[_maskID release];_maskID=[mask[@"id"]copy];}@finally{[a endAction:self];}[NSNotificationCenter.defaultCenter postNotificationName:DYModelChanged object:_host];[self sendCanvasDrawing:sender.tag==DYMaskBezier];} - (void)selection:(id)sender{NSArray *m=DYMasksForTool(_document,_nodeID);if(_list.indexOfSelectedItem<0||_list.indexOfSelectedItem>=m.count)return;[_maskID release];_maskID=[m[_list.indexOfSelectedItem][@"id"]copy];[self refreshWithDocument:_document time:_time];[self canvas:nil];[NSNotificationCenter.defaultCenter postNotificationName:DYModelChanged object:_host];} - (void)remove:(id)sender{[self edit:@"删除工具蒙版" block:^(NSMutableDictionary *doc,NSMutableDictionary *mask){NSMutableArray *masks=[[DYMasksForTool(doc,self->_nodeID)mutableCopy]autorelease];[masks removeObject:mask];DYSetToolMasks(doc,self->_nodeID,masks);}];[self canvas:nil];} - (void)enabled:(NSButton*)sender{[self edit:@"启停蒙版" block:^(NSMutableDictionary *d,NSMutableDictionary *m){m[@"enabled"]=@(sender.state==NSControlStateValueOn);}];} - (void)combine:(id)sender{[self edit:@"蒙版组合" block:^(NSMutableDictionary *d,NSMutableDictionary *m){m[@"combine"]=@(self->_combine.indexOfSelectedItem);}];} - (void)inverted:(NSButton*)sender{[self edit:@"反转蒙版" block:^(NSMutableDictionary *d,NSMutableDictionary *m){DYMaskParameters p;[m[@"parameters"]getBytes:&p length:sizeof(p)];if(sender.state)p.flags|=DYMaskInvert;else p.flags&=~DYMaskInvert;m[@"parameters"]=[NSData dataWithBytes:&p length:sizeof(p)];}];} - (void)valueChanged:(NSControl*)sender{if(_refreshing)return;double value=sender.doubleValue;if([sender isKindOfClass:NSTextField.class]){NSString *text=[sender.stringValue stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];NSScanner *scanner=[NSScanner scannerWithString:text];if(![scanner scanDouble:&value]||!scanner.isAtEnd){NSBeep();[self refreshWithDocument:_document time:_time];return;}}if(!std::isfinite(value)){NSBeep();return;}NSInteger i=sender.tag;[self edit:@"调整工具蒙版" block:^(NSMutableDictionary *d,NSMutableDictionary *m){BOOL stamp=m[@"stampSource"]!=nil,position=i==2||i==3;DYMaskParameters p=DYMaskAtTime(m,stamp&&!position?kCMTimeInvalid:self->_time);float *fields[]={&p.strength,&p.feather,&p.centerX,&p.centerY,&p.width,&p.height,&p.angle,&p.gradientWidth,&p.hueCenterTurns,&p.hueCoreHalfWidth,&p.hueSoftWidth,&p.saturationRange.low,&p.saturationRange.high,&p.lightnessRange.low,&p.lightnessRange.high,&p.inputBlack,&p.inputWhite,&p.inputGamma};*fields[i]=(i==6?value*M_PI/180:i>=8&&i<=10?value/360:value);if(stamp&&(i==4||i==5)){DYStampMotion motion=DYStampMotionAtTime(m,self->_time);*fields[i]/=i==4?motion.targetScaleX:motion.targetScaleY;}if(p.kind==DYMaskHSLKey)p.flags|=DYMaskHueEnabled|DYMaskSaturationEnabled|DYMaskLightnessEnabled;if(!dyValidMaskParameters(p))return;if([m[@"keys"]count]&&(stamp?position:i<8)){NSMutableArray *keys=[[m[@"keys"]mutableCopy]autorelease];[keys filterUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSDictionary *key,NSDictionary *bindings){CMTime t=CMTimeMakeWithEpoch([key[@"value"]longLongValue],[key[@"scale"]intValue],[key[@"epoch"]longLongValue]);return CMTimeCompare(t,self->_time)!=0;}]];[keys addObject:DYStampKey(self->_time,p,YES,1,DYStampMotionAtTime(m,self->_time))];[keys sortUsingComparator:^NSComparisonResult(NSDictionary *a,NSDictionary *b){CMTime x=CMTimeMakeWithEpoch([a[@"value"]longLongValue],[a[@"scale"]intValue],[a[@"epoch"]longLongValue]),y=CMTimeMakeWithEpoch([b[@"value"]longLongValue],[b[@"scale"]intValue],[b[@"epoch"]longLongValue]);return (NSComparisonResult)CMTimeCompare(x,y);}];m[@"keys"]=keys;}else m[@"parameters"]=[NSData dataWithBytes:&p length:sizeof(p)];}];} - (void)updateDrawingControls{ if(!_list)return; for(NSButton *button in _tracking)button.enabled=!_drawing&&(!_trackingBusy||button.tag==9); _clear.enabled=!_drawing&&!_trackingBusy&&[[self mask][@"keys"]count]>0; for(NSControl *control in @[_stampMode,_reference,_referenceReset,_manualKey])control.enabled=!_drawing&&!_trackingBusy; _lastTrusted.enabled=!_drawing&&!_trackingBusy&&CMTIME_IS_NUMERIC(DYTrackingRecordedTime([self mask],@"trackingLastTime")); _penFinish.enabled=_drawing;_penCancel.enabled=_drawing; } - (void)stampModeChanged:(id)sender{[self edit:@"切换图章跟踪方式" block:^(NSMutableDictionary *d,NSMutableDictionary *m){m[@"stampTrackMode"]=self->_stampMode.indexOfSelectedItem==0?@"linked":@"independent";[m removeObjectsForKeys:@[@"status",@"trackingStopped"]];}];} - (void)stampCommand:(NSButton*)sender{[self canvas:nil];[NSNotificationCenter.defaultCenter postNotificationName:@"DYToolCanvasCommand" object:_host userInfo:@{@"node":[self canvasNodeID],@"mask":_maskID?:@"",@"command":sender.identifier,@"timeValue":@(_time.value),@"timeScale":@(_time.timescale),@"timeEpoch":@(_time.epoch)}];} - (NSString*)canvasNodeID{return DYSharedMaskWithID(_document,_maskID)?@"shared":_nodeID;} - (BOOL)representsCanvasMask:(NSDictionary*)info{NSString *maskID=info[@"mask"];if(!maskID.length)return NO;if([info[@"node"]isEqual:_nodeID])return YES;if(!DYSharedMaskWithID(_document,maskID))return NO;for(NSDictionary *mask in DYMasksForTool(_document,_nodeID))if([mask[@"id"]isEqual:maskID])return YES;return NO;} - (void)canvasSelected:(NSNotification*)n{_canvasActive=[self representsCanvasMask:n.userInfo];if(_canvasActive){NSString *ID=[n.userInfo[@"mask"]copy];[_maskID release];_maskID=ID;_drawing=[n.userInfo[@"draw"]boolValue];}else _drawing=NO;[self updateDrawingControls];} - (void)canvasState:(NSNotification*)n{if(!_canvasActive||![self representsCanvasMask:n.userInfo]||![n.userInfo[@"mask"]isEqual:_maskID])return;_drawing=[n.userInfo[@"drawing"]boolValue];[self updateDrawingControls];} - (void)sendCanvasDrawing:(BOOL)drawing{[NSNotificationCenter.defaultCenter postNotificationName:@"DYToolCanvasSelection" object:_host userInfo:@{@"node":[self canvasNodeID],@"mask":_maskID?:@"",@"document":_document?:@{},@"draw":@(drawing)}];} - (void)canvas:(id)sender{[self sendCanvasDrawing:NO];} - (void)redrawPen:(id)sender{[self sendCanvasDrawing:YES];} - (void)penCommand:(NSButton*)sender{[NSNotificationCenter.defaultCenter postNotificationName:@"DYToolCanvasCommand" object:_host userInfo:@{@"node":[self canvasNodeID],@"mask":_maskID?:@"",@"command":sender==_penFinish?@"finish":@"cancel",@"timeValue":@(_time.value),@"timeScale":@(_time.timescale),@"timeEpoch":@(_time.epoch)}];} - (void)clear:(id)sender{ if(_drawing)return; [self edit:@"清除蒙版轨迹" block:^(NSMutableDictionary *d,NSMutableDictionary *m){ DYMaskParameters p=DYMaskAtTime(m,self->_time); if(m[@"stampSource"]){ NSArray *source=DYStampSourceAtTime(m,self->_time); DYStampMotion motion=DYStampMotionAtTime(m,self->_time); m[@"stampSource"]=@[source[0],source[1], @(std::clamp([source[2] doubleValue]*motion.sourceScaleX,.001,1.)), @(std::clamp([source[3] doubleValue]*motion.sourceScaleY,.001,1.))]; m[@"stampSamplingScale"]=DYStampSamplingScaleAtTime(m,self->_time); } m[@"parameters"]=[NSData dataWithBytes:&p length:sizeof(p)]; m[@"keys"]=@[]; [m removeObjectsForKeys:@[@"status",@"trackingStopped",@"trackingLastTime",@"trackingStopTime"]]; }]; } - (void)track:(NSButton*)sender{if(_drawing)return;[self canvas:nil];[NSNotificationCenter.defaultCenter postNotificationName:@"DYToolTrack" object:_host userInfo:@{@"node":[self canvasNodeID],@"mask":_maskID?:@"",@"direction":@(sender.tag)}];} - (void)trackingStatus:(NSNotification*)n{BOOL target=n.userInfo[@"mask"]?[self representsCanvasMask:n.userInfo]:[n.userInfo[@"node"]isEqual:_nodeID];if(target&&(!n.userInfo[@"mask"]||[n.userInfo[@"mask"]isEqual:_maskID])){_status.stringValue=n.userInfo[@"message"]?:@"";_status.toolTip=_status.stringValue;if(n.userInfo[@"tracking"]){_trackingBusy=[n.userInfo[@"tracking"]boolValue];[self updateDrawingControls];}}} @end