// One OSC root owns a lower-right tool rail and independent movable editors. // Positions are view state; opening, moving and channel selection never edit a grade. #import "DYTheme.h" #include "DYViewerParameterPanel.inc.h" static const NSInteger DYViewerPartBase=20000; static void DYDrawDistortionGrid(NSRect rect,NSColor *color){ CGFloat side=MIN(rect.size.width,rect.size.height); auto point=[&](CGFloat x,CGFloat y){ CGFloat scale=side*(.34+.075*(x*x+y*y)); return NSMakePoint(NSMidX(rect)+scale*x,NSMidY(rect)+scale*y); }; NSBezierPath *grid=[NSBezierPath bezierPath]; grid.lineWidth=MAX(.75,side*.067); for(CGFloat edge:{-1.,0.,1.})for(BOOL vertical:{NO,YES}){ for(int i=0;i<=12;i++){ CGFloat t=(i-6)/6.; NSPoint p=vertical?point(edge,t):point(t,edge); if(i==0)[grid moveToPoint:p];else [grid lineToPoint:p]; } } [color setStroke];[grid stroke]; } static void DYDrawLevel(NSRect rect,NSColor *ink){ CGFloat size=NSWidth(rect),x=NSMinX(rect),y=NSMinY(rect); NSBezierPath *guide=[NSBezierPath bezierPath]; [guide moveToPoint:NSMakePoint(x+size*.12,y+size*.75)];[guide lineToPoint:NSMakePoint(x+size*.88,y+size*.75)]; [guide moveToPoint:NSMakePoint(x+size*.19,y+size*.56)];[guide lineToPoint:NSMakePoint(x+size*.81,y+size*.22)]; guide.lineWidth=MAX(1.,size*.085);guide.lineCapStyle=NSLineCapStyleRound; [ink setStroke];[guide stroke]; } static void DYDrawCloneStamp(NSRect rect,NSColor *color){ CGFloat side=NSWidth(rect),x=NSMinX(rect),y=NSMinY(rect); NSBezierPath *handle=[NSBezierPath bezierPathWithRoundedRect:NSMakeRect(x+side*.31,y+side*.05,side*.38,side*.43) xRadius:side*.16 yRadius:side*.16]; NSBezierPath *body=[NSBezierPath bezierPath]; [body moveToPoint:NSMakePoint(x+side*.39,y+side*.31)]; [body lineToPoint:NSMakePoint(x+side*.61,y+side*.31)]; [body lineToPoint:NSMakePoint(x+side*.72,y+side*.55)]; [body lineToPoint:NSMakePoint(x+side*.88,y+side*.55)]; [body lineToPoint:NSMakePoint(x+side*.91,y+side*.78)]; [body lineToPoint:NSMakePoint(x+side*.09,y+side*.78)]; [body lineToPoint:NSMakePoint(x+side*.12,y+side*.55)]; [body lineToPoint:NSMakePoint(x+side*.28,y+side*.55)]; [body closePath]; [color setFill];[handle fill];[body fill]; [[NSBezierPath bezierPathWithRoundedRect:NSMakeRect(x+side*.10,y+side*.86,side*.80,side*.09) xRadius:side*.045 yRadius:side*.045]fill]; } @interface DYViewerControls : NSObject @property(nonatomic,retain) DYViewerParameterBridge *bridge; - (void)setViewport:(NSRect)viewport; - (void)prepareWithSize:(NSSize)size scale:(CGFloat)scale atTime:(CMTime)time; - (void)draw; - (NSRect)drawingBounds; @property(nonatomic) BOOL stampArmed; @property(nonatomic) NSInteger levelMode; // 0 closed, 1 horizontal, 2 vertical @property(nonatomic) BOOL levelResetRequested,levelMeasureOnly,levelMeasureRequested,levelDirectionRequested; - (NSInteger)partAtPoint:(NSPoint)point; - (BOOL)event:(NSEventType)type point:(NSPoint)point atTime:(CMTime)time; - (BOOL)mouseMoved:(NSPoint)point; - (BOOL)mouseExited; - (BOOL)capturing; - (BOOL)finishCapture; - (BOOL)headerAtPoint:(NSPoint)point; @end @implementation DYViewerControls { DYViewerParameterBridge *_bridge;NSMutableArray *_panels,*_areas;DYViewerParameterPanel *_capture; NSRect _viewport,_rail;NSSize _size;CGFloat _scale,_backingScale;CMTime _time;NSInteger _pressed,_hover,_railPage;BOOL _hasViewport,_pendingSkinPreview; } @synthesize bridge=_bridge; - (instancetype)init{if((self=[super init])){_panels=[NSMutableArray new];_areas=[NSMutableArray new];_scale=1;_time=kCMTimeInvalid;}return self;} - (void)dealloc{[self finishCapture];[_bridge release];[_panels release];[_areas release];[super dealloc];} - (void)setBridge:(DYViewerParameterBridge*)bridge{if(bridge==_bridge)return;[self finishCapture];for(DYViewerParameterPanel*p in _panels)p.bridge=nil;[_panels removeAllObjects];[_bridge release];_bridge=[bridge retain];_pendingSkinPreview=NO;_stampArmed=NO;_levelMode=0;_levelResetRequested=_levelMeasureRequested=_levelDirectionRequested=NO;} - (void)setViewport:(NSRect)viewport{_viewport=viewport;_hasViewport=YES;} - (void)prepareWithSize:(NSSize)size scale:(CGFloat)scale atTime:(CMTime)time{ _size=size;_time=time;_backingScale=scale;NSDictionary *command=[_bridge takeScreenCommand];if([command[@"type"]isEqual:@"level"]){_levelMode=[command[@"mode"]integerValue];if(_levelMode<0||_levelMode>2)_levelMode=0;_stampArmed=NO;} if(_pendingSkinPreview){NSDictionary *snapshot=[_bridge snapshotAtTime:time];if(snapshot&&[self skinNodeInSnapshot:snapshot]){_pendingSkinPreview=NO;[self toggleSkinPreview];}} NSRect bounds=NSMakeRect(0,0,size.width,size.height),v=_hasViewport?NSIntersectionRect(_viewport,bounds):bounds;_viewport=v; _scale=MAX(.6,MIN(MAX(1.,scale),MIN(v.size.width/420.,v.size.height/350.)));CGFloat s=_scale; for(DYViewerParameterPanel*p in [[_panels copy]autorelease]){if(!p.visible){if(p==_capture){[p finishCapture];_capture=nil;}p.bridge=nil;[_panels removeObjectIdenticalTo:p];continue;}[p setViewport:v];[p prepareWithSize:size scale:s atTime:time];} if(_capture&&![_capture capturing])_capture=nil; [_areas removeAllObjects];if(!_bridge||v.size.width<100||v.size.height<80)return; // Only tools that benefit from direct image interaction belong on the canvas. NSArray *types=@[@"curve",@"lens",@"skin",@"look",@"whiteBalance",@"cloneStamp",@"level",@"mosaic"],*titles=@[@"曲线",@"畸变修复",@"肤色",@"LUT",@"白平衡吸管",@"仿制图章",@"水平校正",@"局部马赛克"],*symbols=@[@"point.topleft.down.to.point.bottomright.curvepath",@"pincushion-grid",@"person.crop.circle",@"square.stack.3d.up",@"eyedropper",@"clone-stamp",@"level",@"square.grid.3x3.fill"]; CGFloat railScale=MIN(s,v.size.width/560.),y=NSMaxY(v)-33*railScale,w=36*railScale; CGFloat x=NSMaxX(v)-556*railScale; _rail=NSMakeRect(x,y,436*railScale,25*railScale); if(_levelMode){ NSArray *labels=@[@"水平",@"垂直",@"重置",@"仅测量"]; for(NSInteger i=0;i<4;i++){ NSRect r=NSMakeRect(NSMaxX(v)-(354-66*i)*railScale,y-34*railScale,62*railScale,26*railScale); [_areas addObject:@{@"part":@(20031+i),@"kind":@"levelOption",@"index":@(i+1),@"title":labels[i],@"rect":[NSValue valueWithRect:r]}]; } } for(NSInteger i=0;i<3;i++)[_areas addObject:@{@"part":@(20010+i),@"kind":@"mask",@"index":@([@[@1,@0,@2][i]integerValue]),@"title":@[@"矩形",@"椭圆",@"钢笔"][i],@"symbol":@[@"rectangle",@"circle",@"pencil.tip"][i],@"rect":[NSValue valueWithRect:NSMakeRect(x+i*40*railScale,y,w,25*railScale)]}]; for(NSInteger i=0;i=0;i--){NSInteger part=[_panels[i]partAtPoint:p];if(part)return 100000+(i+1)*10000+(part-20000);} return 0; } - (BOOL)headerAtPoint:(NSPoint)p{if(_levelMode||NSPointInRect(p,_rail))return NO;for(DYViewerParameterPanel*panel in [_panels reverseObjectEnumerator]){NSInteger part=[panel partAtPoint:p];if(part)return part==20000;}return NO;} - (NSString*)skinNodeInSnapshot:(NSDictionary*)snapshot{ NSDictionary *doc=snapshot[@"document"];if(!doc)return nil;NSString *node=snapshot[@"selectedNode"]; if([node isEqual:@"base:skin"]&&!DYBaseToolRemoved(doc,@"skin"))return node; if([DYToolWithID(doc,node)[@"type"]isEqual:@"skin"])return node; if(!DYBaseToolRemoved(doc,@"skin"))return @"base:skin"; for(NSString *ID in DYToolDisplayOrder(doc))if([DYToolWithID(doc,ID)[@"type"]isEqual:@"skin"])return ID;return nil; } - (BOOL)skinPreviewEnabled{ NSDictionary *snapshot=[_bridge snapshotAtTime:_time];NSString *node=[self skinNodeInSnapshot:snapshot];if(!node)return NO;DYUniforms u; if([node hasPrefix:@"base:"]){NSData *raw=snapshot[@"uniforms"];if(raw.length!=sizeof(u))return NO;[raw getBytes:&u length:sizeof(u)];}else u=DYToolUniforms(DYToolWithID(snapshot[@"document"],node));return u.skinVisualize>.5; } - (void)toggleSkinPreview{ NSDictionary *snapshot=[_bridge snapshotAtTime:_time];if(!snapshot)return;NSString *node=[self skinNodeInSnapshot:snapshot]; if(!node){_pendingSkinPreview=YES;[_bridge requestTool:@"skin"];return;} DYUniforms u;if([node hasPrefix:@"base:"]){NSData *raw=snapshot[@"uniforms"];if(raw.length!=sizeof(u))return;[raw getBytes:&u length:sizeof(u)];}else u=DYToolUniforms(DYToolWithID(snapshot[@"document"],node)); for(NSDictionary *d in DYDescriptors())if([d[@"key"]isEqual:@"skinVisualize"]){[_bridge beginParameter:d node:node value:u.skinVisualize atTime:_time];[_bridge changeValue:u.skinVisualize>.5?0:1];[_bridge endGesture];break;} } - (NSRect)drawingBounds{ if(!_areas.count)return NSZeroRect; NSRect rect=_rail; for(NSDictionary *area in _areas)rect=NSUnionRect(rect,[area[@"rect"] rectValue]); if(!_levelMode)for(DYViewerParameterPanel *panel in _panels){NSRect bounds=[panel drawingBounds];if(!NSIsEmptyRect(bounds))rect=NSUnionRect(rect,bounds);} // Tooltips and control shadows extend beyond the buttons. return NSIntersectionRect(_viewport,NSInsetRect(rect,-24*_scale,-32*_scale)); } - (void)draw{ if(!_areas.count)return;[NSGraphicsContext saveGraphicsState];NSRectClip(_viewport);CGFloat s=_scale;if(!_levelMode)for(DYViewerParameterPanel*p in _panels)[p draw]; for(NSDictionary*a in _areas){NSRect r=[a[@"rect"]rectValue];BOOL selected=([a[@"toolType"]isEqual:@"skin"]&&[self skinPreviewEnabled])||([a[@"toolType"]isEqual:@"cloneStamp"]&&_stampArmed)||([a[@"toolType"]isEqual:@"level"]&&_levelMode)||([a[@"kind"]isEqual:@"levelOption"]&&([a[@"index"]integerValue]==_levelMode||([a[@"index"]integerValue]==4&&_levelMeasureOnly)));if(!_levelMode)for(DYViewerParameterPanel*p in _panels)if([p.toolType isEqual:a[@"toolType"]])selected=YES; BOOL hover=_hover==[a[@"part"]integerValue]; BOOL pressed=_pressed==[a[@"part"]integerValue];DYDrawSoftControl(r,4*s,NO,selected||hover,pressed); if(a[@"symbol"]){ NSColor *ink=selected?NSColor.systemCyanColor:NSColor.whiteColor; CGFloat side=MIN(17*s,r.size.height*.7); NSRect glyph=NSMakeRect(NSMidX(r)-side/2,NSMidY(r)-side/2,side,side); if([a[@"symbol"]isEqual:@"pincushion-grid"])DYDrawDistortionGrid(NSInsetRect(glyph,1,1),ink); else if([a[@"symbol"]isEqual:@"clone-stamp"])DYDrawCloneStamp(glyph,ink); else if([a[@"symbol"]isEqual:@"level"])DYDrawLevel(glyph,ink); else{ NSImage *icon=[NSImage imageWithSystemSymbolName:a[@"symbol"] accessibilityDescription:a[@"title"]]; NSImageSymbolConfiguration *configuration=[[NSImageSymbolConfiguration configurationWithPointSize:14*s weight:NSFontWeightRegular]configurationByApplyingConfiguration:[NSImageSymbolConfiguration configurationWithPaletteColors:@[ink]]]; [[icon imageWithSymbolConfiguration:configuration]drawInRect:glyph fromRect:NSZeroRect operation:NSCompositingOperationSourceOver fraction:1 respectFlipped:YES hints:nil]; } continue; } // The bottom workspace entry keeps its explicit text label. NSMutableParagraphStyle*style=[[[NSMutableParagraphStyle alloc]init]autorelease];style.alignment=NSTextAlignmentCenter; NSShadow *shadow=[[[NSShadow alloc]init]autorelease];shadow.shadowColor=[NSColor colorWithWhite:0 alpha:.95];shadow.shadowBlurRadius=2;shadow.shadowOffset=NSMakeSize(0,-1); CGFloat fontSize=MIN(11*s,r.size.height*.44);NSDictionary *attributes=@{NSFontAttributeName:[NSFont systemFontOfSize:fontSize weight:selected||hover?NSFontWeightBold:NSFontWeightMedium],NSForegroundColorAttributeName:selected||hover?NSColor.systemCyanColor:NSColor.whiteColor,NSParagraphStyleAttributeName:style,NSShadowAttributeName:shadow}; [a[@"title"]drawInRect:NSInsetRect(r,1,MAX(1.,(r.size.height-fontSize-3)/2)) withAttributes:attributes]; if(selected){NSBezierPath *mark=[NSBezierPath bezierPath];[mark moveToPoint:NSMakePoint(NSMinX(r),NSMidY(r)-3*s)];[mark lineToPoint:NSMakePoint(NSMinX(r),NSMidY(r)+3*s)];[NSColor.systemCyanColor setStroke];mark.lineWidth=2*s;[mark stroke];} } [NSGraphicsContext restoreGraphicsState]; } - (void)activateTool:(NSString*)type{ [_bridge publishScreenTool:type message:nil]; if([type isEqual:@"mosaic"]){[self finishCapture];_stampArmed=NO;_levelMode=0;[_bridge requestMosaicMenu];return;} if([type isEqual:@"level"]){[self finishCapture];_stampArmed=NO;_levelMode=_levelMode?0:1;return;} BOOL returningFromLevel=_levelMode!=0;_levelMode=0; if([type isEqual:@"cloneStamp"]){_stampArmed=!_stampArmed;return;} _stampArmed=NO; if([type isEqual:@"look"]){[_bridge requestWorkspacePage:@"lut"];return;} if([type isEqual:@"skin"]){[self toggleSkinPreview];return;} if([type isEqual:@"whiteBalance"]){[_bridge requestWhiteBalanceAtTime:_time];return;} for(DYViewerParameterPanel*p in [[_panels copy]autorelease])if([p.toolType isEqual:type]){if(returningFromLevel){[_bridge requestTool:type];return;}[p finishCapture];p.bridge=nil;[_panels removeObjectIdenticalTo:p];return;} [_bridge requestTool:type];DYViewerParameterPanel*p=[[[DYViewerParameterPanel alloc]init]autorelease];p.toolType=type;p.bridge=_bridge; [_panels addObject:p]; } - (BOOL)event:(NSEventType)type point:(NSPoint)p atTime:(CMTime)time{ if(!std::isfinite(p.x)||!std::isfinite(p.y))return NO; if(_capture){BOOL used=[_capture event:type point:p atTime:time];if(![_capture capturing]){_capture=nil;[self prepareWithSize:_size scale:_backingScale atTime:time];}return used;} NSInteger hit=[self partAtPoint:p]; if(type==NSEventTypeLeftMouseDown){ if(hit>=100000){NSInteger index=(hit-100000)/10000-1;if(index<0||index>=(NSInteger)_panels.count)return NO;DYViewerParameterPanel*panel=[[_panels[index]retain]autorelease];[_panels removeObjectIdenticalTo:panel];[_panels addObject:panel];_capture=panel;return [panel event:type point:p atTime:time];} if(!hit)return NO;_pressed=hit;return YES; } if(!_pressed)return NO; if(type==NSEventTypeLeftMouseUp){NSInteger pressed=_pressed;_pressed=0;if(hit==pressed){for(NSDictionary*a in _areas)if([a[@"part"]integerValue]==hit){NSString*kind=a[@"kind"];if([kind isEqual:@"tool"])[self activateTool:a[@"toolType"]];else if([kind isEqual:@"levelOption"]){NSInteger choice=[a[@"index"]integerValue];if(choice==3)_levelResetRequested=YES;else if(choice==4)_levelMeasureRequested=YES;else{_levelMode=choice;_levelDirectionRequested=YES;}}else if([kind isEqual:@"mask"]){_stampArmed=NO;_levelMode=0;[_bridge requestMask:[a[@"index"]integerValue]];}else if([kind isEqual:@"workspace"]){_levelMode=0;[_bridge requestWorkspace];}else if([kind isEqual:@"hidePanels"]){for(DYViewerParameterPanel*panel in _panels){[panel finishCapture];panel.bridge=nil;}[_panels removeAllObjects];}else if([kind isEqual:@"railPage"])_railPage++;break;}}[self prepareWithSize:_size scale:_backingScale atTime:time];} return YES; } - (BOOL)capturing{return _pressed||[_capture capturing];} - (BOOL)finishCapture{BOOL had=[self capturing];for(DYViewerParameterPanel*p in _panels)[p finishCapture];_capture=nil;_pressed=0;return had;} - (BOOL)mouseMoved:(NSPoint)p{NSInteger h=[self partAtPoint:p];BOOL changed=h!=_hover;_hover=h;for(DYViewerParameterPanel*panel in _panels)changed=[panel mouseMoved:p]||changed;return changed;} - (BOOL)mouseExited{BOOL changed=_hover!=0;_hover=0;for(DYViewerParameterPanel*p in _panels)changed=[p mouseExited]||changed;return changed;} @end