62 行
14 KiB
Objective-C
62 行
14 KiB
Objective-C
// Native per-tool mask editor, shared by original and repeated tool cards.
|
||
@interface DYToolMaskPanel : DYFlippedView
|
||
@property(nonatomic,readonly) CGFloat contentHeight;
|
||
- (instancetype)initWithManager:(id<PROAPIAccessing>)manager nodeID:(NSString*)nodeID;
|
||
- (void)refreshWithDocument:(NSDictionary*)document time:(CMTime)time;
|
||
- (void)addMenu:(NSView*)anchor;
|
||
@end
|
||
@implementation DYToolMaskPanel {
|
||
id<PROAPIAccessing> _host;NSString *_nodeID,*_maskID;NSDictionary *_document;CMTime _time;
|
||
NSPopUpButton *_list,*_combine;NSButton *_eye,*_add,*_remove,*_invert,*_canvas,*_clear;NSMutableArray *_rows,*_tracking;NSTextField *_status;BOOL _refreshing;CGFloat _contentHeight;
|
||
}
|
||
@synthesize contentHeight=_contentHeight;
|
||
- (instancetype)initWithManager:(id<PROAPIAccessing>)manager nodeID:(NSString*)nodeID{
|
||
if((self=[super initWithFrame:NSZeroRect])){_host=manager;_nodeID=[nodeID copy];_rows=[NSMutableArray new];_tracking=[NSMutableArray new];self.identifier=@"toolMasks";
|
||
_list=[[NSPopUpButton alloc]initWithFrame:NSZeroRect pullsDown:NO];_list.target=self;_list.action=@selector(selection:);_list.font=[NSFont systemFontOfSize:10];_list.accessibilityLabel=@"本层蒙版";[self addSubview:_list];
|
||
_eye=[DYButton(@"",self,@selector(enabled:))retain];_eye.image=[NSImage imageWithSystemSymbolName:@"eye" accessibilityDescription:@"蒙版启用"];_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.target=self;_combine.action=@selector(combine:);_combine.accessibilityLabel=@"蒙版组合方式";[self addSubview:_combine];
|
||
_invert=[[NSButton checkboxWithTitle:@"反转" target:self action:@selector(inverted:)]retain];[self addSubview:_invert];
|
||
_canvas=[DYButton(@"画面编辑",self,@selector(canvas:))retain];_canvas.toolTip=@"在宿主画面拖动中心、大小、旋转与钢笔节点;回车闭合钢笔";_canvas.accessibilityLabel=@"编辑本层蒙版画面控件";[self addSubview:_canvas];
|
||
_clear=[DYButton(@"清除轨迹",self,@selector(clear:))retain];[self addSubview:_clear];
|
||
NSArray *names=@[@"强度",@"羽化",@"中心 X",@"中心 Y",@"宽度",@"高度",@"旋转",@"渐变宽度",@"色相中心",@"色相范围",@"色相柔边",@"饱和度下限",@"饱和度上限",@"亮度下限",@"亮度上限",@"输入黑",@"输入白",@"输入伽马"];
|
||
for(NSInteger i=0;i<names.count;i++){NSTextField *label=[DYLabel(names[i],10)retain];NSSlider *slider=[[NSSlider alloc]initWithFrame:NSZeroRect];slider.tag=i;slider.target=self;slider.action=@selector(valueChanged:);slider.continuous=NO;slider.controlSize=NSControlSizeSmall;slider.accessibilityLabel=[names[i]stringByAppendingString:@"蒙版"];NSTextField *field=[[NSTextField alloc]initWithFrame:NSZeroRect];field.font=[NSFont monospacedDigitSystemFontOfSize:10 weight:NSFontWeightRegular];field.tag=i;field.target=self;field.action=@selector(valueChanged:);field.accessibilityLabel=[names[i]stringByAppendingString:@"蒙版数值"];field.alignment=NSTextAlignmentRight;[self addSubview:label];[self addSubview:slider];[self addSubview:field];[_rows addObject:@{@"label":label,@"slider":slider,@"field":field}];[label release];[slider release];[field release];}
|
||
NSArray *titles=@[@"◀ 单帧",@"◀ 跟踪",@"双向",@"跟踪 ▶",@"单帧 ▶",@"取消"];
|
||
for(int i=0;i<6;i++){NSButton *b=DYButton(titles[i],self,@selector(track:));b.tag=((int[]){-2,-1,0,1,2,9})[i];b.font=[NSFont systemFontOfSize:9];b.accessibilityLabel=@[@"反向跟踪一帧",@"反向蒙版跟踪",@"双向蒙版跟踪",@"正向蒙版跟踪",@"正向跟踪一帧",@"取消蒙版跟踪"][i];[self addSubview:b];[_tracking addObject:b];}
|
||
_status=[DYLabel(@"",10)retain];_status.maximumNumberOfLines=2;[self addSubview:_status];_contentHeight=0;
|
||
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(trackingStatus:) name:@"DYToolTrackingStatus" object:_host];
|
||
}return self;
|
||
}
|
||
- (void)dealloc{[NSNotificationCenter.defaultCenter removeObserver:self];[_nodeID release];[_maskID release];[_document release];[_list release];[_combine release];[_eye release];[_add release];[_remove release];[_invert release];[_canvas release];[_clear release];[_rows release];[_tracking release];[_status release];[super dealloc];}
|
||
- (NSDictionary*)mask{for(NSDictionary *m in DYMasksForTool(_document,_nodeID))if([m[@"id"]isEqual:_maskID])return m;return nil;}
|
||
- (void)refreshWithDocument:(NSDictionary*)doc time:(CMTime)time{
|
||
_refreshing=YES;NSDictionary *saved=[doc copy];[_document release];_document=saved;_time=time;NSArray *masks=DYMasksForTool(doc,_nodeID);[_list removeAllItems];NSInteger selected=0,index=0;
|
||
for(NSDictionary *m in masks){DYMaskParameters p=DYMaskAtTime(m,time);[_list addItemWithTitle:[NSString stringWithFormat:@"%@ %ld",@[@"椭圆",@"矩形",@"钢笔",@"线性渐变",@"HSL 选区",@"亮度选区"][p.kind],(long)index+1]];if([m[@"id"]isEqual:_maskID])selected=index;index++;}
|
||
if(!masks.count){_contentHeight=0;_refreshing=NO;return;}[_list selectItemAtIndex:selected];[_maskID release];_maskID=[masks[selected][@"id"]copy];NSDictionary *m=[self mask];DYMaskParameters p=DYMaskAtTime(m,time);
|
||
_eye.state=[m[@"enabled"]boolValue]?NSControlStateValueOn:NSControlStateValueOff;_eye.image=[NSImage imageWithSystemSymbolName:[m[@"enabled"]boolValue]?@"eye":@"eye.slash" accessibilityDescription:@"启用蒙版"];[_combine selectItemAtIndex:[m[@"combine"]integerValue]];_invert.state=(p.flags&DYMaskInvert)?NSControlStateValueOn:NSControlStateValueOff;
|
||
float values[]={p.strength,p.feather,p.centerX,p.centerY,p.width,p.height,float(p.angle*180.f/M_PI),p.gradientWidth,p.hueCenterTurns*360.f,p.hueCoreHalfWidth*360.f,p.hueSoftWidth*360.f,p.saturationRange.low,p.saturationRange.high,p.lightnessRange.low,p.lightnessRange.high,p.inputBlack,p.inputWhite,p.inputGamma};
|
||
NSInteger visible=0;for(NSInteger i=0;i<_rows.count;i++){BOOL show=i==0||(i==1&&p.kind<3)||(i>=2&&i<=6&&p.kind<4)||(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;_clear.hidden=!shape;_clear.enabled=[m[@"keys"]count]>0;
|
||
_status.stringValue=m[@"status"]?:([m[@"keys"]count]?[NSString stringWithFormat:@"%lu 个轨迹键 · 修改几何会在当前帧建立手工键",(unsigned long)[m[@"keys"]count]]:p.kind==2?@"画面编辑:拖动节点;⌘ 点击加点,回车闭合。":@"各蒙版只影响当前工具层");
|
||
_contentHeight=108+visible*27+(shape?63:0)+36;[self setNeedsLayout:YES];_refreshing=NO;
|
||
}
|
||
- (void)layout{[super layout];CGFloat w=self.bounds.size.width;_eye.frame=NSMakeRect(0,2,20,24);_list.frame=NSMakeRect(22,1,MAX(60,w-82),26);_add.frame=NSMakeRect(w-57,1,26,25);_remove.frame=NSMakeRect(w-29,1,26,25);_combine.frame=NSMakeRect(0,33,83,25);_invert.frame=NSMakeRect(88,34,58,23);_canvas.frame=NSMakeRect(w-94,33,94,25);CGFloat y=67;for(NSDictionary *r in _rows){NSTextField *label=r[@"label"];if(label.hidden)continue;label.frame=NSMakeRect(2,y+4,68,18);[r[@"slider"]setFrame:NSMakeRect(72,y+4,MAX(20,w-133),18)];[r[@"field"]setFrame:NSMakeRect(w-57,y+1,55,23)];y+=27;}if(![_tracking[0]isHidden]){CGFloat bw=(w-8)/3;for(int i=0;i<6;i++)[_tracking[i]setFrame:NSMakeRect((i%3)*(bw+4),y+(i/3)*28,bw,25)];y+=58;_clear.frame=NSMakeRect(0,y,w,25);y+=29;}_status.frame=NSMakeRect(2,y+2,w-4,33);}
|
||
- (void)edit:(NSString*)title block:(void (^)(NSMutableDictionary*,NSMutableDictionary*))block{
|
||
id<FxCustomParameterActionAPI_v4>a=[_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;}id<FxUndoAPI>undo=[_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{
|
||
id<FxCustomParameterActionAPI_v4>a=[_host apiForProtocol:@protocol(FxCustomParameterActionAPI_v4)];if(!a)return;[a startAction:self];@try{NSMutableDictionary *doc=DYMutablePlist(DYReadTools(_host,a.currentTime).document);NSMutableArray *masks=[[DYMasksForTool(doc,_nodeID)mutableCopy]autorelease];if(masks.count>=DYToolMaskLimit){NSBeep();return;}NSDictionary *mask=DYNewMask(sender.tag);[masks addObject:mask];if(!DYSetToolMasks(doc,_nodeID,masks))return;id<FxUndoAPI>undo=[_host apiForProtocol:@protocol(FxUndoAPI)];[undo startUndoGroup:@"添加工具蒙版"];DYWriteTools(_host,doc,a.currentTime);[undo endUndoGroup];[_maskID release];_maskID=[mask[@"id"]copy];}@finally{[a endAction:self];}[NSNotificationCenter.defaultCenter postNotificationName:DYModelChanged object:_host];[self canvas:nil];}
|
||
- (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);}];}
|
||
- (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(!std::isfinite(value)){NSBeep();return;}NSInteger i=sender.tag;[self edit:@"调整工具蒙版" block:^(NSMutableDictionary *d,NSMutableDictionary *m){DYMaskParameters p=DYMaskAtTime(m,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(p.kind==DYMaskHSLKey)p.flags|=DYMaskHueEnabled|DYMaskSaturationEnabled|DYMaskLightnessEnabled;if(!dyValidMaskParameters(p))return;if([m[@"keys"]count]&&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:DYMaskKey(self->_time,p,YES,1)];[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)canvas:(id)sender{[NSNotificationCenter.defaultCenter postNotificationName:@"DYToolCanvasSelection" object:_host userInfo:@{@"node":_nodeID,@"mask":_maskID?:@""}];}
|
||
- (void)clear:(id)sender{[self edit:@"清除蒙版轨迹" block:^(NSMutableDictionary *d,NSMutableDictionary *m){DYMaskParameters p=DYMaskAtTime(m,self->_time);m[@"parameters"]=[NSData dataWithBytes:&p length:sizeof(p)];m[@"keys"]=@[];[m removeObjectForKey:@"status"];}];}
|
||
- (void)track:(NSButton*)sender{[self canvas:nil];[NSNotificationCenter.defaultCenter postNotificationName:@"DYToolTrack" object:_host userInfo:@{@"node":_nodeID,@"mask":_maskID?:@"",@"direction":@(sender.tag)}];}
|
||
- (void)trackingStatus:(NSNotification*)n{if([n.userInfo[@"node"]isEqual:_nodeID])_status.stringValue=n.userInfo[@"message"]?:@"";}
|
||
@end
|