// Ported from dayu-colorplate; provenance in research/SpiderAndBalanceSources.json. #include "DYSpiderScopeDrawing.h" @interface DYSpiderDisc : NSView @property(nonatomic,retain) DYRenderer *scopeRenderer; @property(nonatomic,copy) NSData *meshData; @property(nonatomic,readonly) NSInteger hueCount,saturationCount; - (void)changeHueCount:(int)hues saturationCount:(int)saturation; - (void)resetMesh; @property(nonatomic) NSInteger selected; @property(nonatomic,retain) NSMutableIndexSet *pinned,*selection; @property(nonatomic) BOOL interactionEnabled; - (void)selectRing; - (void)selectSpoke; - (void)resetSelected; @property(nonatomic,copy) void(^changed)(NSData*); @property(nonatomic,copy) void(^selectionChanged)(NSString*); @property(nonatomic,copy) void(^gesture)(BOOL); - (void)toggleLock; - (void)finishGesture; @end @implementation DYSpiderDisc { BOOL _dragging,_marquee,_gestureActive,_pendingChange; NSPoint _dragOrigin,_marqueeEnd; dy::SpiderMesh _mesh,_dragMesh; NSMutableIndexSet *_selectionStart,*_pinsStart; NSData *_scopeRaster; NSImage *_scopeTrace; } - (void)dealloc{[self finishGesture];self.scopeRenderer=nil;[_pinned release];[_selection release];[_selectionStart release];[_pinsStart release];[_changed release];[_selectionChanged release];[_gesture release];[super dealloc];} - (void)setScopeRenderer:(DYRenderer *)renderer { if(renderer==_scopeRenderer)return; NSNotificationCenter *center=NSNotificationCenter.defaultCenter; if(_scopeRenderer){ [center removeObserver:self name:@"DYFinalOutputDidChange" object:_scopeRenderer]; [center removeObserver:self name:@"DYSpiderScopeDidChange" object:_scopeRenderer]; } [_scopeRenderer release];_scopeRenderer=[renderer retain]; DYUpdateSpiderTrace(nil,_scopeRaster,_scopeTrace); if(renderer){ [center addObserver:self selector:@selector(finalOutputChanged:) name:@"DYFinalOutputDidChange" object:renderer]; [center addObserver:self selector:@selector(spiderScopeChanged:) name:@"DYSpiderScopeDidChange" object:renderer]; } self.needsDisplay=YES; } - (BOOL)canDisplayScope { return self.window&&!self.hiddenOrHasHiddenAncestor&&!NSIsEmptyRect(self.visibleRect); } - (void)finalOutputChanged:(NSNotification *)note { if(!_gestureActive&&[self canDisplayScope])[_scopeRenderer outputSpiderScope]; } - (void)spiderScopeChanged:(NSNotification *)note { if(!_gestureActive&&[self canDisplayScope])self.needsDisplay=YES; } - (BOOL)isFlipped{return YES;} - (BOOL)acceptsFirstResponder{return YES;} - (BOOL)acceptsFirstMouse:(NSEvent*)event{return YES;} - (BOOL)isAccessibilityElement{return YES;} - (NSString*)accessibilityRole{return NSAccessibilityButtonRole;} - (NSString*)accessibilityHelp{return @"拖动一点,同一色相列的自由点自动联动。固定点仍可直接拖动。Command 多选,Shift 固定,右键复位。Tab 切点,方向键微调,Escape 取消。";} - (id)accessibilityValue{NSInteger i=_selected>=0?_selected:(NSInteger)_selection.firstIndex;return i>=0&&i<[self nodeCount]?[NSString stringWithFormat:@"已选 %lu 点,主点 %ld,X %.4f,Y %.4f,固定点仍可拖动",(unsigned long)_selection.count,(long)i+1,_mesh.x[i],_mesh.y[i]]:@"拖动一点,周围自由点自动联动";} - (BOOL)accessibilityPerformPress{[self.window makeFirstResponder:self];if(!_selection.count)[_selection addIndex:0];[self describeSelection];self.needsDisplay=YES;return YES;} - (void)flushMeshChange { if(!_pendingChange)return; _pendingChange=NO; if(_changed)_changed(self.meshData); } - (void)finishGesture { if(!_gestureActive)return; [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(flushMeshChange) object:nil]; [self flushMeshChange]; _gestureActive=NO; if(_gesture)_gesture(NO); [self finalOutputChanged:nil]; } - (void)viewWillMoveToWindow:(NSWindow*)window{ if(!window){_dragging=_marquee=NO;[self finishGesture];} [super viewWillMoveToWindow:window]; } - (NSInteger)nodeCount{return dy::meshCount(_mesh);} - (NSInteger)hueCount{return _mesh.hues;} - (NSInteger)saturationCount{return _mesh.rings+1;} - (NSData*)meshData{return [NSData dataWithBytes:&_mesh length:sizeof(_mesh)];} - (void)setMeshData:(NSData*)data{ if(_gestureActive)return; auto mesh=DYSpiderMesh(data); if(!memcmp(&mesh,&_mesh,sizeof(mesh)))return; BOOL topology=mesh.hues!=_mesh.hues||mesh.rings!=_mesh.rings; _mesh=mesh; if(topology){_dragging=_marquee=NO;[_selection removeAllIndexes];_selected=-1;} [self updatePins];self.needsDisplay=YES; } - (void)updatePins{self.pinned=[NSMutableIndexSet indexSet];for(int i=0;i<[self nodeCount];i++)if(_mesh.pins[i])[_pinned addIndex:i];} - (void)notifyChange{ for(int i=0;i<[self nodeCount];i++)_mesh.pins[i]=dy::meshBoundary(_mesh,i)||[_pinned containsIndex:i]; self.needsDisplay=YES; if(!_gestureActive){ [self describeSelection]; if(_changed)_changed(self.meshData); }else if(!_pendingChange){ // AppKit draws the local mesh immediately. Only the newest draft reaches FxPlug. _pendingChange=YES; [self performSelector:@selector(flushMeshChange) withObject:nil afterDelay:1.0/60.0 inModes:@[NSRunLoopCommonModes,NSEventTrackingRunLoopMode]]; } } - (void)changeHueCount:(int)hues saturationCount:(int)saturation{if(!_interactionEnabled||!dySpiderResolution(hues)||!dySpiderResolution(saturation))return;if(hues==_mesh.hues&&saturation==_mesh.rings+1)return;[self finishGesture];_dragging=_marquee=NO;_mesh=dy::resampleMesh(_mesh,hues,saturation);[_selection removeAllIndexes];_selected=-1;[self updatePins];[self notifyChange];} - (void)resetMesh{_mesh=dy::neutralMesh(_mesh.hues,_mesh.rings+1);[_selection removeAllIndexes];_selected=-1;[self updatePins];[self notifyChange];} - (void)moveMeshX:(float)x y:(float)y { if(!_selection.count)return; std::bitsetselected; for(NSUInteger i=_selection.firstIndex;i<[self nodeCount];i=[_selection indexGreaterThanIndex:i])selected[i]=1; auto moved=dy::moveMesh(_dragging?_dragMesh:_mesh,selected,x,y); if(!memcmp(&moved,&_mesh,sizeof(moved)))return; _mesh=moved;[self updatePins];[self notifyChange]; } - (void)keyDown:(NSEvent*)e { if(!_interactionEnabled)return;unichar c=e.charactersIgnoringModifiers.length?[e.charactersIgnoringModifiers characterAtIndex:0]:0; if(c==27){if(_dragging){_mesh=_dragMesh;self.pinned=[[_pinsStart mutableCopy]autorelease];[self notifyChange];}if(_marquee)self.selection=[[_selectionStart mutableCopy]autorelease];_dragging=_marquee=NO;[self finishGesture];self.needsDisplay=YES;return;} if((e.modifierFlags&NSEventModifierFlagCommand)&&c=='a'){[_selection addIndexesInRange:NSMakeRange(0,[self nodeCount])];[self describeSelection];self.needsDisplay=YES;return;} if(c=='\t'){_selected=(_selected+(e.modifierFlags&NSEventModifierFlagShift?[self nodeCount]-1:1)+[self nodeCount])%[self nodeCount];[_selection removeAllIndexes];[_selection addIndex:_selected];[self describeSelection];self.needsDisplay=YES;return;} if(c==' '){[self toggleLock];return;}if(c==NSDeleteCharacter||c==NSBackspaceCharacter){[self resetSelected];return;} float step=e.modifierFlags&NSEventModifierFlagShift?.02f:.005f; NSPoint move=NSZeroPoint; if(c==NSLeftArrowFunctionKey)move.x=-step; else if(c==NSRightArrowFunctionKey)move.x=step; else if(c==NSUpArrowFunctionKey)move.y=-step; else if(c==NSDownArrowFunctionKey)move.y=step; else{[super keyDown:e];return;} if(_selection.count){ NSInteger lead=_selected>=0&&[_selection containsIndex:_selected]?_selected:_selection.firstIndex; NSRect rect=self.discRect; NSPoint delta=DYSpiderMeshDelta(rect,_mesh.x[lead],_mesh.y[lead],NSMakePoint(move.x*NSWidth(rect),move.y*NSHeight(rect))); [self moveMeshX:delta.x y:delta.y]; } } - (instancetype)initWithFrame:(NSRect)r{if((self=[super initWithFrame:r])){_mesh=dy::neutralMesh();_selected=-1;[self updatePins];self.selection=[NSMutableIndexSet indexSet];_interactionEnabled=YES;self.accessibilityLabel=@"蜘蛛盘,含矢量示波器和肤色指示线";}return self;} - (NSRect)discRect{CGFloat s=MAX(0,MIN(self.bounds.size.width,self.bounds.size.height)-32);return NSMakeRect((self.bounds.size.width-s)/2,(self.bounds.size.height-s)/2,s,s);} - (NSPoint)point:(NSInteger)i{return DYSpiderPlotPoint(self.discRect,_mesh.x[i],_mesh.y[i]);} - (void)drawRect:(NSRect)dirty{ NSRect r=self.discRect;if(NSIsEmptyRect(r))return; if(!_gestureActive&&[self canDisplayScope])DYUpdateSpiderTrace([_scopeRenderer outputSpiderScope],_scopeRaster,_scopeTrace); DYDrawSpiderScope(r,_scopeTrace,1); NSPoint points[dy::SpiderMeshCapacity]; int count=dy::meshCount(_mesh); for(int i=0;i=0&&(e.modifierFlags&NSEventModifierFlagShift)){[_pinned addIndex:chosen];[self notifyChange];return;} if(chosen<0){_marquee=YES;if(!additive)[_selection removeAllIndexes];if(!additive)[_selectionStart removeAllIndexes];} else if(additive){if([_selection containsIndex:chosen])[_selection removeIndex:chosen];else[_selection addIndex:chosen];} else {if(![_selection containsIndex:chosen]){[_selection removeAllIndexes];[_selection addIndex:chosen];}if(e.clickCount>=2)[self resetSelected];else _dragging=YES;} [self describeSelection];self.needsDisplay=YES; } - (void)mouseDragged:(NSEvent*)e { if(!_interactionEnabled)return;NSPoint p=[self convertPoint:e.locationInWindow fromView:nil]; if(_marquee){_marqueeEnd=p;NSRect box=NSMakeRect(MIN(_dragOrigin.x,p.x),MIN(_dragOrigin.y,p.y),fabs(p.x-_dragOrigin.x),fabs(p.y-_dragOrigin.y));self.selection=[[_selectionStart mutableCopy]autorelease];for(int i=0;i<[self nodeCount];i++)if(NSPointInRect([self point:i],box))[_selection addIndex:i];[self describeSelection];self.needsDisplay=YES;} else if(_dragging){if(!_gestureActive){_gestureActive=YES;if(_gesture)_gesture(YES);}NSPoint delta=DYSpiderMeshDelta(self.discRect,_dragMesh.x[_selected],_dragMesh.y[_selected], NSMakePoint(p.x-_dragOrigin.x,p.y-_dragOrigin.y));[self moveMeshX:delta.x y:delta.y];} } - (void)mouseUp:(NSEvent*)e{if(_dragging&&_gestureActive)[self mouseDragged:e];_dragging=_marquee=NO;[self finishGesture];self.needsDisplay=YES;} @end