83 行
8.3 KiB
Objective-C
83 行
8.3 KiB
Objective-C
// One OSC root owns a vertical 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;
|
|
@interface DYViewerControls : NSObject
|
|
@property(nonatomic,retain) DYViewerParameterBridge *bridge;
|
|
- (void)setViewport:(NSRect)viewport;
|
|
- (void)prepareWithSize:(NSSize)size scale:(CGFloat)scale atTime:(CMTime)time;
|
|
- (void)draw;
|
|
- (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;CMTime _time;NSInteger _pressed,_hover,_railPage;BOOL _hasViewport;
|
|
}
|
|
@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];}
|
|
- (void)setViewport:(NSRect)viewport{_viewport=viewport;_hasViewport=YES;}
|
|
- (void)prepareWithSize:(NSSize)size scale:(CGFloat)scale atTime:(CMTime)time{
|
|
_size=size;_time=time;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;
|
|
NSInteger toolCount=DYToolTypes().count;NSInteger cap=MAX(1,MIN(toolCount,(NSInteger)floor((v.size.height-72*s)/(30*s))));if(cap<toolCount)cap=MAX(1,MIN(toolCount-1,(NSInteger)floor((v.size.height-96*s)/(30*s))));NSInteger pages=(toolCount+cap-1)/cap;_railPage=MAX(0,_railPage)%pages;
|
|
CGFloat x=NSMinX(v)+8*s,y=NSMinY(v)+8*s,w=40*s;
|
|
_rail=NSMakeRect(x,y,w,(30*MIN(cap,toolCount-_railPage*cap)+54+(pages>1?24:0))*s);
|
|
NSArray*titles=@[@"一级",@"蜘蛛",@"基础",@"肤色",@"LUT",@"HSL",@"曲线",@"分级",@"原色",@"色阶",@"黑白",@"平衡",@"通道",@"胶片",@"畸变"];
|
|
for(NSInteger j=0;j<cap&&j+_railPage*cap<toolCount;j++){NSInteger i=j+_railPage*cap;[_areas addObject:@{@"part":@(20020+i),@"kind":@"tool",@"toolType":DYToolTypes()[i],@"title":titles[i],@"rect":[NSValue valueWithRect:NSMakeRect(x+4*s,y+5*s+j*30*s,w-8*s,26*s)]}];}
|
|
CGFloat end=NSMaxY(_rail)-47*s;if(pages>1){[_areas addObject:@{@"part":@20003,@"kind":@"railPage",@"title":@"↕",@"rect":[NSValue valueWithRect:NSMakeRect(x+4*s,end-24*s,w-8*s,22*s)]}];}
|
|
[_areas addObject:@{@"part":@20001,@"kind":@"workspace",@"title":@"◫",@"rect":[NSValue valueWithRect:NSMakeRect(x+4*s,end,w-8*s,26*s)]}];
|
|
[_areas addObject:@{@"part":@20002,@"kind":@"hidePanels",@"title":@"收",@"rect":[NSValue valueWithRect:NSMakeRect(x+4*s,end+27*s,w-8*s,18*s)]}];
|
|
}
|
|
- (NSInteger)partAtPoint:(NSPoint)p{
|
|
if(!_bridge||!std::isfinite(p.x)||!std::isfinite(p.y)||!NSPointInRect(p,_viewport))return 0;
|
|
for(NSDictionary*a in _areas)if(NSPointInRect(p,[a[@"rect"]rectValue]))return [a[@"part"]integerValue];if(NSPointInRect(p,_rail))return 20000;
|
|
for(NSInteger i=_panels.count-1;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(NSPointInRect(p,_rail))return NO;for(DYViewerParameterPanel*panel in [_panels reverseObjectEnumerator]){NSInteger part=[panel partAtPoint:p];if(part)return part==20000;}return NO;}
|
|
- (void)draw{
|
|
if(!_areas.count)return;[NSGraphicsContext saveGraphicsState];NSRectClip(_viewport);CGFloat s=_scale;NSColor*ink=DYThemeColor(@"text"),*accent=DYThemeColor(@"accent");for(DYViewerParameterPanel*p in _panels)[p draw];DYDrawThemeSurface([NSBezierPath bezierPathWithRoundedRect:_rail xRadius:8*s yRadius:8*s],YES);
|
|
for(NSDictionary*a in _areas){NSRect r=[a[@"rect"]rectValue];BOOL selected=NO;for(DYViewerParameterPanel*p in _panels)if([p.toolType isEqual:a[@"toolType"]])selected=YES;
|
|
BOOL hover=_hover==[a[@"part"]integerValue];DYDrawSoftControl(r,5*s,NO,selected||hover,_pressed==[a[@"part"]integerValue]);
|
|
NSMutableParagraphStyle*style=[[[NSMutableParagraphStyle alloc]init]autorelease];style.alignment=NSTextAlignmentCenter;
|
|
[a[@"title"]drawInRect:NSInsetRect(r,1*s,4*s) withAttributes:@{NSFontAttributeName:[NSFont systemFontOfSize:([a[@"kind"]isEqual:@"workspace"]?16:10)*s weight:NSFontWeightMedium],NSForegroundColorAttributeName:selected?accent:ink,NSParagraphStyleAttributeName:style}];
|
|
}
|
|
for(NSDictionary*a in _areas)if(_hover==[a[@"part"]integerValue]){NSString*help=[a[@"kind"]isEqual:@"workspace"]?@"打开示波器与 LUT 工作区":[a[@"kind"]isEqual:@"hidePanels"]?@"收起所有画布面板":a[@"toolType"]?DYToolSection(a[@"toolType"])[@"title"]:@"下一组工具";NSRect r=[a[@"rect"]rectValue];NSRect tip=NSMakeRect(NSMaxX(_rail)+6*s,NSMinY(r),MIN(200*s,NSMaxX(_viewport)-NSMaxX(_rail)-10*s),25*s);if(tip.size.width>40*s){DYDrawThemeSurface([NSBezierPath bezierPathWithRoundedRect:tip xRadius:4*s yRadius:4*s],YES);[help drawInRect:NSInsetRect(tip,6*s,5*s)withAttributes:@{NSFontAttributeName:[NSFont systemFontOfSize:10*s],NSForegroundColorAttributeName:ink}];}break;}
|
|
[NSGraphicsContext restoreGraphicsState];
|
|
}
|
|
- (void)activateTool:(NSString*)type{
|
|
for(DYViewerParameterPanel*p in [[_panels copy]autorelease])if([p.toolType isEqual:type]){[p finishCapture];p.bridge=nil;[_panels removeObjectIdenticalTo:p];return;}
|
|
[_bridge requestTool:type];DYViewerParameterPanel*p=[[[DYViewerParameterPanel alloc]init]autorelease];p.toolType=type;p.bridge=_bridge;
|
|
CGFloat x=(_rail.size.width+24*_scale)/MAX(1.,_viewport.size.width),offset=(_panels.count%4)*22*_scale;
|
|
[p setInitialPosition:NSMakePoint(x+offset/MAX(1.,_viewport.size.width),(8*_scale+offset)/MAX(1.,_viewport.size.height))];[_panels addObject:p];
|
|
}
|
|
- (BOOL)event:(NSEventType)type point:(NSPoint)p atTime:(CMTime)time{
|
|
if(!std::isfinite(p.x)||!std::isfinite(p.y))return NO;[self prepareWithSize:_size scale:_scale atTime:time];
|
|
if(_capture){BOOL used=[_capture event:type point:p atTime:time];if(![_capture capturing]){_capture=nil;[self prepareWithSize:_size scale:_scale 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:@"workspace"])[_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:_scale 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
|