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

137 行
20 KiB
C++

// Methods of DYViewerParameterPanel. Geometry is shared by drawing and input.
- (BOOL)graphicKind:(NSString*)kind{return [@[@"curvePlot",@"wheel",@"hslPlot",@"spiderPlot",@"lensFrame",@"lensCenter",@"lensStrength"]containsObject:kind];}
- (NSData*)spiderData {if(_spiderDraft)return _spiderDraft;return [_nodeID hasPrefix:@"base:"]?_snapshot[@"spider"]:[self node][@"layer"][@"spider"];}
- (void)beginSpiderData {[_bridge beginSpider:[self spiderData] node:_nodeID enabled:[self graphicValue:@"spiderEnabled"] atTime:_time];}
- (void)spiderAction:(NSInteger)action {
auto mesh=DYSpiderMesh([self spiderData]);if(action==0){mesh=dy::neutralMesh(mesh.hues,mesh.rings+1);}
else if(action==3||action==4){int count=action==3?mesh.hues:mesh.rings+1;int next=count<12?12:count<24?24:6;mesh=dy::resampleMesh(mesh,action==3?next:mesh.hues,action==4?next:mesh.rings+1);_selectedSpider=-1;}
else if(_selectedSpider>=0&&_selectedSpider<dy::meshCount(mesh)){if(action==1)mesh.pins[_selectedSpider]=dy::meshBoundary(mesh,(int)_selectedSpider)||!mesh.pins[_selectedSpider];else {auto neutral=dy::neutralMesh(mesh.hues,mesh.rings+1);mesh.x[_selectedSpider]=neutral.x[_selectedSpider];mesh.y[_selectedSpider]=neutral.y[_selectedSpider];}}
else return;
[self beginSpiderData];[_bridge changeSpider:[NSData dataWithBytes:&mesh length:sizeof(mesh)]];[_bridge endGesture];
}
- (double)graphicValue:(NSString*)key{return [self valueForDescriptor:[self descriptorForKey:key]];}
- (NSArray*)curveSlots{
NSMutableArray*slots=[NSMutableArray array];NSInteger n=_channel==0?4:2;
for(NSInteger i=0;i<n;i++)[slots addObject:@{@"xKey":_channel==0?[NSString stringWithFormat:@"masterX%ld",(long)i]:[NSString stringWithFormat:@"channel%ldX%ld",(long)_channel-1,(long)i],@"yKey":[NSString stringWithFormat:@"curveY%ld",(long)(_channel==0?i:4+(_channel-1)*2+i)]}];
for(NSInteger i=0;i<4;i++)[slots addObject:@{@"xKey":[NSString stringWithFormat:@"curve%ldExtraX%ld",(long)_channel,(long)i],@"yKey":[NSString stringWithFormat:@"curve%ldExtra%ld",(long)_channel,(long)i]}];return slots;
}
- (NSArray*)curvePoints{
NSMutableArray*points=[NSMutableArray array];for(NSDictionary*slot in [self curveSlots]){double x=[self graphicValue:slot[@"xKey"]];if(x<0)continue;[points addObject:@{@"xKey":slot[@"xKey"],@"yKey":slot[@"yKey"],@"x":@(x),@"y":@(MAX(0.,MIN(1.,x+[self graphicValue:slot[@"yKey"]]*.01)))}];}
[points sortUsingComparator:^NSComparisonResult(NSDictionary*a,NSDictionary*b){return [a[@"x"]compare:b[@"x"]];}];return points;
}
- (void)layoutGraphicAt:(CGFloat)y{
if(!_snapshot||![self node]||_panel.size.width<180*_scale||_panel.size.height<180*_scale)return;CGFloat s=_scale,x=_panel.origin.x,w=_panel.size.width;
if([_toolType isEqual:@"hsl"]){
for(NSInteger axis=0;axis<3;axis++)[self addArea:NSMakeRect(x+12*s+axis*(w-24*s)/3,y,(w-30*s)/3,23*s)part:22210+axis kind:@"hslAxis" title:@[@"色相",@"饱和度",@"明亮度"][axis]descriptor:nil index:axis];
[self addArea:NSMakeRect(x+18*s,y+34*s,w-36*s,MAX(40*s,NSMaxY(_panel)-y-85*s))part:22200 kind:@"hslPlot" title:@"" descriptor:nil index:0];
}else if([_toolType isEqual:@"spider"]){
CGFloat d=MAX(30*s,MIN(w-36*s,NSMaxY(_panel)-y-67*s));[self addArea:NSMakeRect(x+(w-d)/2,y,d,d)part:22300 kind:@"spiderPlot" title:@"" descriptor:nil index:0];
auto mesh=DYSpiderMesh([self spiderData]);NSArray *titles=@[@"全盘还原",@"固定点",@"还原点",[NSString stringWithFormat:@"色相 %d",mesh.hues],[NSString stringWithFormat:@"环数 %d",mesh.rings+1]];
for(NSInteger i=0;i<5;i++)[self addArea:NSMakeRect(x+8*s+i*(w-16*s)/5,NSMaxY(_panel)-53*s,(w-20*s)/5,23*s)part:22310+i kind:@"spiderAction" title:titles[i] descriptor:nil index:i];
}else if([_toolType isEqual:@"curve"]){
for(NSInteger c=0;c<4;c++)[self addArea:NSMakeRect(x+12*s+c*(w-24*s)/4,y,(w-30*s)/4,23*s)part:22010+c kind:@"channel" title:@[@"RGB",@"红",@"绿",@"蓝"][c] descriptor:nil index:c];
NSRect graph=NSMakeRect(x+18*s,y+31*s,w-36*s,MAX(45*s,NSMaxY(_panel)-y-84*s));[self addArea:graph part:22000 kind:@"curvePlot" title:@"" descriptor:nil index:0];
if(_selectedX)[self addArea:NSMakeRect(x+12*s,NSMaxY(_panel)-47*s,90*s,20*s)part:22020 kind:@"deletePoint" title:@"删除选中点" descriptor:nil index:0];
}else{
CGFloat diameter=MAX(20*s,MIN((w-48*s)/2,(NSMaxY(_panel)-y-52*s)/2-20*s));
for(NSInteger i=0;i<4;i++){CGFloat cx=x+w*(i%2?.75:.25),top=y+(i/2)*(diameter+29*s);[self addArea:NSMakeRect(cx-diameter/2,top+20*s,diameter,diameter)part:22100+i kind:@"wheel" title:@[@"阴影",@"中间调",@"高光",@"全局"][i] descriptor:nil index:i];}
}
}
- (NSPoint)curvePosition:(NSDictionary*)p rect:(NSRect)r{return NSMakePoint(r.origin.x+[p[@"x"]doubleValue]*r.size.width,r.origin.y+(1-[p[@"y"]doubleValue])*r.size.height);}
- (void)drawGraphic:(NSDictionary*)area{
CGFloat s=_scale;NSRect r=[area[@"rect"]rectValue];NSColor*ink=DYThemeColor(@"text"),*accent=DYThemeColor(@"accent");
if([@[@"curve",@"lens"]containsObject:_toolType]){ink=NSColor.whiteColor;accent=NSColor.systemCyanColor;NSShadow *shadow=[[[NSShadow alloc]init]autorelease];shadow.shadowColor=[NSColor colorWithWhite:0 alpha:.9];shadow.shadowBlurRadius=2;shadow.shadowOffset=NSZeroSize;[shadow set];}
if([area[@"kind"]isEqual:@"lensFrame"]){
NSBezierPath *grid=[NSBezierPath bezierPath];grid.lineWidth=.6*s;
for(int i=1;i<8;i++){CGFloat x=NSMinX(r)+r.size.width*i/8.,y=NSMinY(r)+r.size.height*i/8.;[grid moveToPoint:NSMakePoint(x,NSMinY(r))];[grid lineToPoint:NSMakePoint(x,NSMaxY(r))];[grid moveToPoint:NSMakePoint(NSMinX(r),y)];[grid lineToPoint:NSMakePoint(NSMaxX(r),y)];}[[NSColor colorWithWhite:1 alpha:.3]setStroke];[grid stroke];return;
}
if([area[@"kind"]isEqual:@"lensCenter"]||[area[@"kind"]isEqual:@"lensStrength"]){
BOOL center=[area[@"kind"]isEqual:@"lensCenter"];NSPoint point=NSMakePoint(NSMidX(r),NSMidY(r));NSBezierPath *mark=[NSBezierPath bezierPath];mark.lineWidth=1.4*s;
[mark moveToPoint:NSMakePoint(point.x-7*s,point.y)];[mark lineToPoint:NSMakePoint(point.x+7*s,point.y)];
if(center){[mark moveToPoint:NSMakePoint(point.x,point.y-7*s)];[mark lineToPoint:NSMakePoint(point.x,point.y+7*s)];}else{[mark moveToPoint:NSMakePoint(point.x-3*s,point.y-4*s)];[mark lineToPoint:NSMakePoint(point.x-7*s,point.y)];[mark lineToPoint:NSMakePoint(point.x-3*s,point.y+4*s)];[mark moveToPoint:NSMakePoint(point.x+3*s,point.y-4*s)];[mark lineToPoint:NSMakePoint(point.x+7*s,point.y)];[mark lineToPoint:NSMakePoint(point.x+3*s,point.y+4*s)];}
[accent setStroke];[mark stroke];if(_hover==[area[@"part"]integerValue]||_pressed==[area[@"part"]integerValue]){NSString *hint=center?@"光学中心":[NSString stringWithFormat:@"畸变 %.1f",[self graphicValue:@"lensStrength"]];[self text:hint rect:NSMakeRect(point.x-50*s,point.y+12*s,100*s,18*s)size:10 color:ink alignment:NSTextAlignmentCenter];}return;
}
if([area[@"kind"]isEqual:@"hslPlot"]){
[NSGraphicsContext saveGraphicsState];NSRectClip(r);[[ink colorWithAlphaComponent:.18]setStroke];NSBezierPath *mid=[NSBezierPath bezierPath];[mid moveToPoint:NSMakePoint(NSMinX(r),NSMidY(r))];[mid lineToPoint:NSMakePoint(NSMaxX(r),NSMidY(r))];[mid stroke];
NSBezierPath *curve=[NSBezierPath bezierPath];curve.lineWidth=1.5*s;
for(int i=0;i<8;i++){double v=[self graphicValue:[NSString stringWithFormat:@"hsl%ld_%d",(long)_hslAxis,i]];CGFloat xx=NSMinX(r)+(i+.5)*r.size.width/8,yy=NSMidY(r)-v/200*r.size.height;NSPoint point=NSMakePoint(xx,yy);if(i==0)[curve moveToPoint:point];else[curve lineToPoint:point];}
[accent setStroke];[curve stroke];
for(int i=0;i<8;i++){double v=[self graphicValue:[NSString stringWithFormat:@"hsl%ld_%d",(long)_hslAxis,i]];CGFloat xx=NSMinX(r)+(i+.5)*r.size.width/8,yy=NSMidY(r)-v/200*r.size.height;NSColor *c=[NSColor colorWithHue:i/8. saturation:.7 brightness:.95 alpha:.9];[c setFill];[[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(xx-5*s,yy-5*s,10*s,10*s)]fill];[self text:[NSString stringWithFormat:@"%.0f",v]rect:NSMakeRect(xx-15*s,MIN(NSMaxY(r)-15*s,yy+7*s),30*s,15*s)size:8 color:ink alignment:NSTextAlignmentCenter];}
[NSGraphicsContext restoreGraphicsState];[self text:@"红 橙 黄 绿 青 蓝 紫 洋红" rect:NSMakeRect(r.origin.x,NSMaxY(r)+3*s,r.size.width,15*s)size:9 color:ink alignment:NSTextAlignmentCenter];return;
}
if([area[@"kind"]isEqual:@"spiderPlot"]){
auto mesh=DYSpiderMesh([self spiderData]);CGFloat rad=r.size.width/2;NSPoint center=NSMakePoint(NSMidX(r),NSMidY(r));
for(int i=0;i<64;i++){double a=i*2*M_PI/64,b=(i+1)*2*M_PI/64;NSBezierPath *wedge=[NSBezierPath bezierPath];[wedge moveToPoint:center];[wedge lineToPoint:NSMakePoint(center.x+cos(a)*rad,center.y+sin(a)*rad)];[wedge lineToPoint:NSMakePoint(center.x+cos(b)*rad,center.y+sin(b)*rad)];[wedge closePath];[[NSColor colorWithHue:i/64. saturation:.8 brightness:.19 alpha:1]setFill];[wedge fill];}
DYDrawSpiderScope(r,[_bridge canvasSpiderScopeAtTime:_time],s);
ink=[NSColor colorWithWhite:.94 alpha:1];
auto point=[&](int i){return NSMakePoint(r.origin.x+mesh.x[i]*r.size.width,r.origin.y+mesh.y[i]*r.size.height);};NSBezierPath *net=[NSBezierPath bezierPath];net.lineWidth=.6*s;
for(int ring=1;ring<=mesh.rings;ring++)for(int hue=0;hue<=mesh.hues;hue++){NSPoint q=point(dy::meshIndex(mesh,hue%mesh.hues,ring));if(hue==0)[net moveToPoint:q];else[net lineToPoint:q];}
for(int hue=0;hue<mesh.hues;hue++){[net moveToPoint:point(dy::meshCount(mesh)-1)];for(int ring=1;ring<=mesh.rings;ring++)[net lineToPoint:point(dy::meshIndex(mesh,hue,ring))];}
[[ink colorWithAlphaComponent:.26]setStroke];[net stroke];
for(int i=0;i<dy::meshCount(mesh);i++){NSPoint q=point(i);CGFloat rr=(i==_selectedSpider?4:2)*s;[(i==_selectedSpider?accent:ink)setFill];NSBezierPath *dot=[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(q.x-rr,q.y-rr,2*rr,2*rr)];[dot fill];dot.lineWidth=.85*s;[[NSColor colorWithWhite:0 alpha:.8]setStroke];[dot stroke];if(mesh.pins[i]){[NSColor.blackColor setFill];NSRectFill(NSMakeRect(q.x-s,q.y-s,2*s,2*s));}}return;
}
if([area[@"kind"]isEqual:@"wheel"]){
NSInteger index=[area[@"index"]integerValue];[self text:area[@"title"]rect:NSMakeRect(r.origin.x,r.origin.y-19*s,r.size.width,17*s)size:10 color:ink alignment:NSTextAlignmentCenter];
// Cached raster is generated from the same HSV convention as the renderer controls.
static NSImage*wheel=nil;static dispatch_once_t once;dispatch_once(&once,^{NSInteger n=192;NSBitmapImageRep*b=[[[NSBitmapImageRep alloc]initWithBitmapDataPlanes:NULL pixelsWide:n pixelsHigh:n bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO colorSpaceName:NSCalibratedRGBColorSpace bytesPerRow:n*4 bitsPerPixel:32]autorelease];
for(NSInteger yy=0;yy<n;yy++)for(NSInteger xx=0;xx<n;xx++){double dx=(xx+.5-n*.5)/(n*.5-1),dy=(n*.5-yy-.5)/(n*.5-1),sat=hypot(dx,dy),h=atan2(dy,dx)/(2*M_PI);if(h<0)h++;NSColor*c=[NSColor colorWithSRGBRed:1 green:1 blue:1 alpha:1];c=[[NSColor colorWithHue:h saturation:MIN(1.,sat) brightness:1 alpha:1]colorUsingColorSpace:NSColorSpace.sRGBColorSpace];NSUInteger at=(yy*n+xx)*4;b.bitmapData[at]=lrint(c.redComponent*255);b.bitmapData[at+1]=lrint(c.greenComponent*255);b.bitmapData[at+2]=lrint(c.blueComponent*255);b.bitmapData[at+3]=sat<=1?230:0;}
wheel=[[NSImage alloc]initWithSize:NSMakeSize(n,n)];[wheel addRepresentation:b];});
[wheel drawInRect:r fromRect:NSZeroRect operation:NSCompositingOperationSourceOver fraction:.8 respectFlipped:YES hints:nil];
double hue=[self graphicValue:[NSString stringWithFormat:@"grade%ld_0",(long)index]],sat=[self graphicValue:[NSString stringWithFormat:@"grade%ld_1",(long)index]],angle=hue*M_PI/180,rad=r.size.width*.5*sat/100;
NSPoint p=NSMakePoint(NSMidX(r)+cos(angle)*rad,NSMidY(r)-sin(angle)*rad);[NSColor.blackColor setStroke];NSBezierPath*knob=[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(p.x-5*s,p.y-5*s,10*s,10*s)];knob.lineWidth=3*s;[knob stroke];[NSColor.whiteColor setStroke];knob.lineWidth=1.4*s;[knob stroke];
return;
}
[NSGraphicsContext saveGraphicsState];NSRectClip(r);
NSData *rawHistogram=[_bridge canvasHistogramAtTime:_time];const DYHistogram *hist=rawHistogram.length==sizeof(DYHistogram)?(const DYHistogram*)rawHistogram.bytes:nullptr;
if(hist&&hist->samples){uint32_t peak=1;for(int c=0;c<4;c++)if(_channel==0||c==_channel)for(int b=0;b<256;b++)peak=MAX(peak,hist->bins[c][b]);NSArray *colors=@[ink,NSColor.systemRedColor,NSColor.systemGreenColor,NSColor.systemBlueColor];for(int c=3;c>=0;c--){if(_channel&&c!=_channel)continue;NSBezierPath *area=[NSBezierPath bezierPath];[area moveToPoint:NSMakePoint(NSMinX(r),NSMaxY(r))];for(int b=0;b<256;b++)[area lineToPoint:NSMakePoint(NSMinX(r)+b/255.*r.size.width,NSMaxY(r)-sqrt(hist->bins[c][b]/double(peak))*r.size.height*.86)];[area lineToPoint:NSMakePoint(NSMaxX(r),NSMaxY(r))];[area closePath];[[colors[c]colorWithAlphaComponent:c==0?.08:.2]setFill];[area fill];}}
[[ink colorWithAlphaComponent:.16]setStroke];NSBezierPath*grid=[NSBezierPath bezierPath];grid.lineWidth=.5*s;
for(int i=0;i<5;i++){CGFloat x=r.origin.x+r.size.width*i/4,y=r.origin.y+r.size.height*i/4;[grid moveToPoint:NSMakePoint(x,NSMinY(r))];[grid lineToPoint:NSMakePoint(x,NSMaxY(r))];[grid moveToPoint:NSMakePoint(NSMinX(r),y)];[grid lineToPoint:NSMakePoint(NSMaxX(r),y)];}[grid stroke];
[[ink colorWithAlphaComponent:.2]setStroke];NSBezierPath*diagonal=[NSBezierPath bezierPath];[diagonal moveToPoint:NSMakePoint(NSMinX(r),NSMaxY(r))];[diagonal lineToPoint:NSMakePoint(NSMaxX(r),NSMinY(r))];[diagonal stroke];
// Identical monotone Hermite interpolation to the existing curve editor.
NSArray*points=[self curvePoints];std::vector<NSPoint> p;p.push_back(NSMakePoint(0,0));for(NSDictionary*point in points)p.push_back(NSMakePoint([point[@"x"]doubleValue],[point[@"y"]doubleValue]));p.push_back(NSMakePoint(1,1));
for(size_t i=1;i<p.size();i++)p[i].x=MAX(p[i].x,p[i-1].x+.0001);p.back().x=1;
std::vector<double>slopes(p.size()-1),m(p.size());for(size_t i=0;i<slopes.size();i++)slopes[i]=(p[i+1].y-p[i].y)/MAX(.0001,p[i+1].x-p[i].x);m.front()=slopes.front();m.back()=slopes.back();for(size_t i=1;i+1<p.size();i++)m[i]=slopes[i-1]*slopes[i]>0?(slopes[i-1]+slopes[i])*.5:0;
for(size_t i=0;i<slopes.size();i++){if(fabs(slopes[i])<.00001)m[i]=m[i+1]=0;else{double a=m[i]/slopes[i],b=m[i+1]/slopes[i],v=hypot(a,b);if(v>3){m[i]=3*slopes[i]*a/v;m[i+1]=3*slopes[i]*b/v;}}}
NSBezierPath*curve=[NSBezierPath bezierPath];curve.lineWidth=1.8*s;size_t segment=0;
for(int sample=0;sample<=256;sample++){double x=sample/256.;while(segment+2<p.size()&&x>p[segment+1].x)segment++;double width=MAX(.0001,p[segment+1].x-p[segment].x),t=MAX(0.,MIN(1.,(x-p[segment].x)/width)),t2=t*t,t3=t2*t;double y=(2*t3-3*t2+1)*p[segment].y+(t3-2*t2+t)*width*m[segment]+(-2*t3+3*t2)*p[segment+1].y+(t3-t2)*width*m[segment+1];NSPoint q=NSMakePoint(r.origin.x+x*r.size.width,r.origin.y+(1-MAX(0.,MIN(1.,y)))*r.size.height);if(sample==0)[curve moveToPoint:q];else[curve lineToPoint:q];}
NSColor*color=_channel==0?accent:_channel==1?NSColor.systemRedColor:_channel==2?NSColor.systemGreenColor:NSColor.systemBlueColor;[color setStroke];[curve stroke];
for(NSDictionary*point in points){NSPoint q=[self curvePosition:point rect:r];BOOL selected=[point[@"xKey"]isEqual:_selectedX];[color setFill];NSBezierPath*dot=[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(q.x-4*s,q.y-4*s,8*s,8*s)];[dot fill];[(selected?NSColor.whiteColor:ink)setStroke];dot.lineWidth=(selected?2:1)*s;[dot stroke];}
[NSGraphicsContext restoreGraphicsState];
}
- (void)beginGraphicAt:(NSPoint)p{
if(!_snapshot||![self node])return;NSMutableDictionary*c=[[_captured mutableCopy]autorelease];NSRect r=[c[@"rect"]rectValue];NSString*xKey=nil,*yKey=nil;
if([c[@"kind"]isEqual:@"lensCenter"]||[c[@"kind"]isEqual:@"lensStrength"]){
BOOL center=[c[@"kind"]isEqual:@"lensCenter"];NSArray *keys=center?@[@"lensCenterX",@"lensCenterY"]:@[@"lensStrength"];NSMutableArray *descriptors=[NSMutableArray array];NSMutableDictionary *values=[NSMutableDictionary dictionary];for(NSString *key in keys){[descriptors addObject:[self descriptorForKey:key]];values[key]=@([self graphicValue:key]);}c[@"lensStart"]=values;[_captured release];_captured=[c copy];[_bridge beginParameters:descriptors node:_nodeID values:values atTime:_time];return;
}
if([c[@"kind"]isEqual:@"spiderPlot"]){auto mesh=DYSpiderMesh([self spiderData]);double best=12*_scale;NSInteger chosen=-1;for(int i=0;i<dy::meshCount(mesh);i++){double distance=hypot(p.x-(r.origin.x+mesh.x[i]*r.size.width),p.y-(r.origin.y+mesh.y[i]*r.size.height));if(distance<best){best=distance;chosen=i;}}if(chosen<0)return;_selectedSpider=chosen;c[@"meshStart"]=[NSData dataWithBytes:&mesh length:sizeof(mesh)];[_captured release];_captured=[c copy];[self beginSpiderData];return;}
if([c[@"kind"]isEqual:@"hslPlot"]){NSInteger color=MAX(0,MIN(7,(NSInteger)((p.x-r.origin.x)/r.size.width*8)));NSString *key=[NSString stringWithFormat:@"hsl%ld_%ld",(long)_hslAxis,(long)color];c[@"hslKey"]=key;[_captured release];_captured=[c copy];[_bridge beginParameter:[self descriptorForKey:key]node:_nodeID value:[self graphicValue:key]atTime:_time];return;}
if([c[@"kind"]isEqual:@"curvePlot"]){NSDictionary*nearest=nil;double distance=12*_scale;for(NSDictionary*point in [self curvePoints]){NSPoint q=[self curvePosition:point rect:r];double d=hypot(q.x-p.x,q.y-p.y);if(d<distance){distance=d;nearest=point;}}
if(!nearest)for(NSDictionary*slot in [self curveSlots])if([self graphicValue:slot[@"xKey"]]<0){nearest=slot;break;}
if(!nearest)return;xKey=nearest[@"xKey"];yKey=nearest[@"yKey"];[_selectedX release];_selectedX=[xKey copy];c[@"newPoint"]=@([self graphicValue:xKey]<0);
}else{if(hypot(p.x-NSMidX(r),p.y-NSMidY(r))>r.size.width*.5)return;NSInteger i=[c[@"index"]integerValue];xKey=[NSString stringWithFormat:@"grade%ld_0",(long)i];yKey=[NSString stringWithFormat:@"grade%ld_1",(long)i];}
NSDictionary*xd=[self descriptorForKey:xKey],*yd=[self descriptorForKey:yKey];if(!xd||!yd)return;c[@"xKey"]=xKey;c[@"yKey"]=yKey;
[_captured release];_captured=[c copy];NSDictionary*values=@{xKey:@([self graphicValue:xKey]),yKey:@([self graphicValue:yKey])};[_bridge beginParameters:@[xd,yd]node:_nodeID values:values atTime:_time];
if([c[@"newPoint"]boolValue]||[c[@"kind"]isEqual:@"wheel"])[self updateGraphicAt:p];
}
- (void)updateGraphicAt:(NSPoint)p{
if([_captured[@"kind"]isEqual:@"lensCenter"]||[_captured[@"kind"]isEqual:@"lensStrength"]){
NSDictionary *start=_captured[@"lensStart"];if(!start)return;NSDictionary *values;
if([_captured[@"kind"]isEqual:@"lensCenter"]){values=@{@"lensCenterX":@(MAX(0.,MIN(1.,[start[@"lensCenterX"]doubleValue]+(p.x-_down.x)/MAX(1.,_viewport.size.width)))),@"lensCenterY":@(MAX(0.,MIN(1.,[start[@"lensCenterY"]doubleValue]+(p.y-_down.y)/MAX(1.,_viewport.size.height))))};}
else values=@{@"lensStrength":@(MAX(-100.,MIN(100.,[start[@"lensStrength"]doubleValue]+(p.x-_down.x)/MAX(1.,_viewport.size.width)*400)))};
[_draftValues addEntriesFromDictionary:values];[_bridge changeValues:values];return;
}
if([_captured[@"kind"]isEqual:@"spiderPlot"]){NSData *start=_captured[@"meshStart"];if(!start||_selectedSpider<0)return;NSRect r=[_captured[@"rect"]rectValue];auto mesh=DYSpiderMesh(start);std::bitset<dy::SpiderMeshCapacity> selected;selected[_selectedSpider]=true;auto moved=dy::moveMesh(mesh,selected,(p.x-_down.x)/r.size.width,(p.y-_down.y)/r.size.height);[_spiderDraft release];_spiderDraft=[[NSData alloc]initWithBytes:&moved length:sizeof(moved)];[_bridge changeSpider:_spiderDraft];return;}
if([_captured[@"kind"]isEqual:@"hslPlot"]){NSString *key=_captured[@"hslKey"];if(!key)return;NSRect r=[_captured[@"rect"]rectValue];double v=MAX(-100.,MIN(100.,(NSMidY(r)-p.y)/r.size.height*200));_draftValues[key]=@(v);[_bridge changeValue:v];return;}
NSString*xKey=_captured[@"xKey"],*yKey=_captured[@"yKey"];if(!xKey||!yKey)return;NSRect r=[_captured[@"rect"]rectValue];double x,y;
if([_captured[@"kind"]isEqual:@"curvePlot"]){x=MAX(.001,MIN(.999,(p.x-r.origin.x)/MAX(1.,r.size.width)));double output=MAX(0.,MIN(1.,1-(p.y-r.origin.y)/MAX(1.,r.size.height)));
for(NSDictionary*point in [self curvePoints])if(![point[@"xKey"]isEqual:xKey]){double other=[point[@"x"]doubleValue];if(fabs(x-other)<.001)x=x<other?other-.001:other+.001;}x=MAX(.001,MIN(.999,x));y=(output-x)*100;
}else{double dx=p.x-NSMidX(r),dy=NSMidY(r)-p.y;x=atan2(dy,dx)*180/M_PI;if(x<0)x+=360;y=MIN(100.,hypot(dx,dy)/(r.size.width*.5)*100);}
NSDictionary*values=@{xKey:@(x),yKey:@(y)};[_draftValues addEntriesFromDictionary:values];[_bridge changeValues:values];
}
- (void)deleteSelectedPoint{
if(!_selectedX)return;for(NSDictionary*point in [self curvePoints])if([point[@"xKey"]isEqual:_selectedX]){NSString*x=point[@"xKey"],*y=point[@"yKey"];[_bridge beginParameters:@[[self descriptorForKey:x],[self descriptorForKey:y]]node:_nodeID values:@{x:@([self graphicValue:x]),y:@([self graphicValue:y])}atTime:_time];NSDictionary *values=@{x:@(-1),y:@0};[_bridge changeValues:values];[self finishCurveWithValues:values];break;}[_selectedX release];_selectedX=nil;
}