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

93 行
10 KiB
Objective-C

// Native spider geometry/gestures and CST wheel interaction are ported from
// the recorded sibling source repositories; the adapter writes this host model.
#include "DYSpiderDisc.inc.h"
@interface DYSpiderPanel : DYFlippedView
@property(nonatomic) BOOL horizontalLayout;
@property(nonatomic,assign) NSView *accessoryRow;
@property(nonatomic,assign) id<DYGraphicDelegate> delegate;
- (void)setMesh:(NSData *)mesh linked:(BOOL)linked;
- (void)setScopeRenderer:(DYRenderer *)renderer;
@end
@implementation DYSpiderPanel {
DYSpiderDisc *_disc; NSPopUpButton *_hues,*_saturation; NSButton *_link;
NSTextField *_status; BOOL _linked;
}
- (instancetype)initWithFrame:(NSRect)frame {
if((self=[super initWithFrame:frame])){
_linked=YES;_disc=[[DYSpiderDisc alloc]initWithFrame:NSZeroRect];[self addSubview:_disc];
__block DYSpiderPanel *owner=self;
_disc.changed=^(DYGrade g,NSData *pins){if(owner.delegate)[owner.delegate writeSpiderMesh:owner->_disc.meshData linked:owner->_linked];};
_disc.gesture=^(BOOL begin){[owner.delegate graphicGesture:begin title:@"调整蜘蛛盘"];};
_status=[DYLabel(@"拖动一点,同列自由点联动。⌘ 多选,Shift 固定,右键复位。",10)retain];_status.maximumNumberOfLines=2;[self addSubview:_status];
_disc.selectionChanged=^(NSString *text){owner->_status.stringValue=text;};
for(int axis=0;axis<2;axis++){
NSPopUpButton *menu=[[NSPopUpButton alloc]initWithFrame:NSZeroRect pullsDown:NO];for(int n:{6,8,12,16,24}){[menu addItemWithTitle:[NSString stringWithFormat:@"%d",n]];menu.lastItem.tag=n;}
menu.font=[NSFont systemFontOfSize:11];menu.target=self;menu.action=@selector(resolutionChanged:);menu.accessibilityLabel=axis?@"蜘蛛盘饱和度点数":@"蜘蛛盘色相点数";[self addSubview:menu];if(axis)_saturation=menu;else _hues=menu;
NSTextField *label=DYLabel(axis?@"饱和度":@"色相",10);label.tag=101+axis;[self addSubview:label];
}
_link=[[NSButton checkboxWithTitle:@"联动" target:self action:@selector(linkChanged:)]retain];_link.font=[NSFont systemFontOfSize:10];_link.accessibilityLabel=@"联动蜘蛛盘点数";[self addSubview:_link];
NSArray *titles=@[@"固定 / 取消固定",@"重置蜘蛛盘",@"选择整环",@"选择色相列",@"复位所选"];
for(int i=0;i<5;i++){NSButton *b=DYButton(titles[i],self,@selector(command:));b.tag=201+i;b.font=[NSFont systemFontOfSize:10];b.accessibilityLabel=titles[i];[self addSubview:b];}
[self setMesh:nil linked:YES];
}return self;
}
- (void)dealloc{_disc.changed=nil;_disc.gesture=nil;_disc.selectionChanged=nil;[_disc release];[_hues release];[_saturation release];[_link release];[_status release];[super dealloc];}
- (void)setScopeRenderer:(DYRenderer *)renderer{_disc.scopeRenderer=renderer;}
- (void)setMesh:(NSData *)data linked:(BOOL)linked{auto m=DYSpiderMesh(data);_disc.meshData=[NSData dataWithBytes:&m length:sizeof(m)];_linked=linked;[_hues selectItemWithTag:m.hues];[_saturation selectItemWithTag:m.rings+1];_link.state=linked?NSControlStateValueOn:NSControlStateValueOff;}
- (void)layout{[super layout];CGFloat w=self.bounds.size.width;
if(_horizontalLayout){CGFloat d=MIN(self.bounds.size.height,MAX(170,w*.48)),x=d+20,cw=MAX(200,w-x-10);_disc.frame=NSMakeRect(0,0,d,d);[[self viewWithTag:101]setFrame:NSMakeRect(x,7,48,18)];_hues.frame=NSMakeRect(x+58,2,cw-58,26);[[self viewWithTag:102]setFrame:NSMakeRect(x,37,48,18)];_saturation.frame=NSMakeRect(x+58,32,cw-58,26);_link.frame=NSMakeRect(x,64,cw,22);_accessoryRow.frame=NSMakeRect(x,89,cw,33);CGFloat bw=(cw-6)/2;for(int i=0;i<5;i++)[[self viewWithTag:201+i]setFrame:NSMakeRect(x+(i%2)*(bw+6),126+(i/2)*30,bw,26)];_status.frame=NSMakeRect(x,218,cw,MAX(18,self.bounds.size.height-218));_status.hidden=self.bounds.size.height<238;return;}
_disc.frame=NSMakeRect(0,0,w,300);
[[self viewWithTag:101]setFrame:NSMakeRect(0,307,32,20)];_hues.frame=NSMakeRect(33,301,60,27);
[[self viewWithTag:102]setFrame:NSMakeRect(99,307,43,20)];_saturation.frame=NSMakeRect(142,301,60,27);_link.frame=NSMakeRect(MAX(205,w-61),304,61,23);
_status.frame=NSMakeRect(2,334,w-4,36);CGFloat gap=5,bw=(w-gap)/2;
for(int i=0;i<2;i++)[[self viewWithTag:201+i]setFrame:NSMakeRect(i*(bw+gap),375,bw,27)];bw=(w-gap*2)/3;
for(int i=0;i<3;i++)[[self viewWithTag:203+i]setFrame:NSMakeRect(i*(bw+gap),406,bw,27)];}
- (void)resolutionChanged:(NSPopUpButton *)sender{int h=(int)_hues.selectedTag,s=(int)_saturation.selectedTag;if(_linked)h=s=(int)sender.selectedTag;[_disc changeHueCount:h saturationCount:s];}
- (void)linkChanged:(NSButton *)sender{_linked=sender.state==NSControlStateValueOn;if(_linked&&_disc.hueCount!=_disc.saturationCount)[_disc changeHueCount:(int)_disc.hueCount saturationCount:(int)_disc.hueCount];else[_delegate writeSpiderMesh:_disc.meshData linked:_linked];}
- (void)command:(NSButton *)sender{switch(sender.tag){case 201:[_disc toggleLock];break;case 202:[_disc resetMesh];break;case 203:[_disc selectRing];break;case 204:[_disc selectSpoke];break;case 205:[_disc resetSelected];break;}}
@end
@interface DYPrimaryBalanceWheel : NSView
@property(nonatomic,assign) id<DYGraphicDelegate> delegate;
- (void)setValues:(NSDictionary *)values;
@end
@implementation DYPrimaryBalanceWheel {
NSPoint _offset,_dragStartMouse,_dragStartOffset;BOOL _dragging,_gestureActive;
NSImage *_wheel;NSButton *_reset;float _angle,_strength;
}
- (instancetype)initWithFrame:(NSRect)frame{if((self=[super initWithFrame:frame])){NSString *path=[[NSBundle bundleForClass:self.class]pathForResource:@"Basis_balanceWheel" ofType:@"png"];if(path)_wheel=[[NSImage alloc]initWithContentsOfFile:path];self.accessibilityLabel=@"一级白平衡色轮";self.toolTip=@"拖动调整一级白平衡(精细移动);双击复位。方向键微调。";
_reset=[DYButton(@"复位",self,@selector(resetBalance:))retain];_reset.accessibilityLabel=@"复位一级白平衡";_reset.toolTip=@"仅复位一级白平衡的方向和强度";[self addSubview:_reset];}return self;}
- (void)dealloc{[self finishGesture];[_wheel release];[_reset release];[super dealloc];}
- (void)layout{[super layout];_reset.frame=NSMakeRect(MAX(0,self.bounds.size.width-62),0,60,24);}
- (BOOL)isAccessibilityElement{return YES;}
- (NSString *)accessibilityRole{return NSAccessibilityButtonRole;}
- (NSArray *)accessibilityChildren{return @[_reset];}
- (NSString *)accessibilityHelp{return self.toolTip;}
- (id)accessibilityValue{return [NSString stringWithFormat:@"方向 %.1f 度,强度 %.1f%%",_angle,_strength*100];}
- (BOOL)accessibilityPerformPress{[self.window makeFirstResponder:self];return YES;}
- (BOOL)acceptsFirstResponder{return YES;}
- (BOOL)acceptsFirstMouse:(NSEvent *)e{return YES;}
- (CGFloat)maxDistance{return MAX(1,MIN(self.bounds.size.width,self.bounds.size.height-28)*.5-12);}
- (NSPoint)center{return NSMakePoint(NSMidX(self.bounds),NSMidY(self.bounds)+10);}
- (void)setValues:(NSDictionary *)values{_angle=[values[@"cmbalanceAngle"]floatValue];_strength=[values[@"cmbalanceStrength"]floatValue];double angle=(_angle+104)*M_PI/180;_offset=NSMakePoint(cos(angle)*_strength*self.maxDistance,sin(angle)*_strength*self.maxDistance);[self setNeedsDisplay:YES];}
- (void)finishGesture{if(_gestureActive){_gestureActive=NO;[_delegate graphicGesture:NO title:@"调整一级白平衡"];}}
- (void)resetBalance:(id)sender{[self finishGesture];_dragging=NO;[_delegate graphicGesture:YES title:@"复位一级白平衡"];_offset=NSZeroPoint;_angle=_strength=0;[_delegate writeGraphicValues:@{@"cmbalanceAngle":@0,@"cmbalanceStrength":@0}];[_delegate graphicGesture:NO title:@"复位一级白平衡"];[self setNeedsDisplay:YES];}
- (void)viewWillMoveToWindow:(NSWindow *)w{if(!w)[self finishGesture];[super viewWillMoveToWindow:w];}
- (void)pushValue{
_strength=(float)(hypot(_offset.x,_offset.y)/self.maxDistance);_angle=(float)fmod(atan2(_offset.y,_offset.x)*180/M_PI+360-104,360);
[_delegate writeGraphicValues:@{@"cmbalanceAngle":@(_angle),@"cmbalanceStrength":@(_strength)}];[self setNeedsDisplay:YES];
}
- (void)drawRect:(NSRect)dirty{NSPoint c=self.center;CGFloat r=self.maxDistance;[_wheel drawInRect:NSMakeRect(c.x-r,c.y-r,2*r,2*r) fromRect:NSZeroRect operation:NSCompositingOperationSourceOver fraction:1];
[NSColor.whiteColor setFill];[[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(c.x+_offset.x-4,c.y+_offset.y-4,8,8)]fill];
NSString *text=[NSString stringWithFormat:@"一级白平衡 %.1f° · %.1f%%",_angle,_strength*100];[text drawAtPoint:NSMakePoint(8,3) withAttributes:@{NSFontAttributeName:[NSFont systemFontOfSize:10],NSForegroundColorAttributeName:DYInk()}];
}
- (void)mouseDown:(NSEvent *)e{[self.window makeFirstResponder:self];if(e.clickCount==2){[self resetBalance:nil];return;}
NSPoint p=[self convertPoint:e.locationInWindow fromView:nil],c=self.center;NSPoint knob=NSMakePoint(c.x+_offset.x,c.y+_offset.y);if(hypot(p.x-knob.x,p.y-knob.y)<self.maxDistance){_dragging=YES;_dragStartMouse=p;_dragStartOffset=_offset;}}
- (void)mouseDragged:(NSEvent *)e{if(!_dragging)return;if(!_gestureActive){_gestureActive=YES;[_delegate graphicGesture:YES title:@"调整一级白平衡"];}
NSPoint p=[self convertPoint:e.locationInWindow fromView:nil],v=NSMakePoint(_dragStartOffset.x+(p.x-_dragStartMouse.x)/5,_dragStartOffset.y+(p.y-_dragStartMouse.y)/5);double length=hypot(v.x,v.y);if(length>self.maxDistance){v.x=v.x/length*self.maxDistance;v.y=v.y/length*self.maxDistance;}_offset=v;[self pushValue];}
- (void)mouseUp:(NSEvent *)e{if(_dragging)[self mouseDragged:e];_dragging=NO;[self finishGesture];}
- (void)keyDown:(NSEvent *)e{unichar c=e.charactersIgnoringModifiers.length?[e.charactersIgnoringModifiers characterAtIndex:0]:0;float step=self.maxDistance*((e.modifierFlags&NSEventModifierFlagShift)?.02f:.005f);NSPoint v=_offset;
if(c==NSLeftArrowFunctionKey)v.x-=step;else if(c==NSRightArrowFunctionKey)v.x+=step;else if(c==NSUpArrowFunctionKey)v.y+=step;else if(c==NSDownArrowFunctionKey)v.y-=step;else if(c==NSDeleteCharacter||c==NSBackspaceCharacter)v=NSZeroPoint;else{[super keyDown:e];return;}
double length=hypot(v.x,v.y);if(length>self.maxDistance){v.x*=self.maxDistance/length;v.y*=self.maxDistance/length;}[_delegate graphicGesture:YES title:@"调整一级白平衡"];_offset=v;[self pushValue];[_delegate graphicGesture:NO title:@"调整一级白平衡"];}
@end