-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathDTSampleHoldPatch.m
123 lines (96 loc) · 3.38 KB
/
DTSampleHoldPatch.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#import "DTSampleHoldPatch.h"
bool DTIsValueSerializable(id v)
{
if(![v respondsToSelector:@selector(encodeWithCoder:)])
{
NSAlert *a = [NSAlert alertWithMessageText:@"Can't save current Sample & Hold value." defaultButton:@"Sorry" alternateButton:nil otherButton:nil informativeTextWithFormat:@"An instance of the Kineme Sample & Hold Patch is currently holding a value that can't be serialized (%@ doesn't respond to encodeWithCoder:), so this patch will restore with an empty value.",[v class]];
[a runModal];
return NO;
}
// @@@todo: see if there's some way to get QCMeshes to work..
if(objc_getClass("QCMesh") && [[v class] isSubclassOfClass:objc_getClass("QCMesh")])
{
NSAlert *a = [NSAlert alertWithMessageText:@"Can't save current Sample & Hold value." defaultButton:@"Sorry" alternateButton:nil otherButton:nil informativeTextWithFormat:@"An instance of the Kineme Sample & Hold Patch is currently holding a QCMesh. QCMeshes currently can't be serialized, so this patch will restore with an empty value.",[v class]];
[a runModal];
return NO;
}
if([[v class] isSubclassOfClass:[QCImage class]] && [v isInfinite])
{
NSAlert *a = [NSAlert alertWithMessageText:@"Can't save current Sample & Hold value." defaultButton:@"Sorry" alternateButton:nil otherButton:nil informativeTextWithFormat:@"An instance of the Kineme Sample & Hold Patch is currently holding an image with infinite dimensions. Such images can't be saved, so this patch will restore with an empty value."];
[a runModal];
return NO;
}
if([[v class] isSubclassOfClass:[QCStructure class]])
for(id s in [v _list])
if(!DTIsValueSerializable(s))
return NO;
return YES;
}
@interface DTSampleHoldPatch ()
@property (retain) id priorValue;
@end
@implementation DTSampleHoldPatch
@synthesize priorValue;
+(BOOL)isSafe
{
return YES;
}
+(BOOL)allowsSubpatchesWithIdentifier:(id)identifier
{
return NO;
}
-(id)initWithIdentifier:(id)identifier
{
if(self = [super initWithIdentifier:identifier])
{
[[self userInfo] setObject:@"Kineme Sample & Hold" forKey:@"name"];
self.priorValue = nil;
}
return self;
}
- (void) dealloc
{
[priorValue release];
[super dealloc];
}
-(BOOL)setup:(QCOpenGLContext*)context
{
if(priorValue)
{
[outputValue setRawValue:priorValue];
}
return YES;
}
-(BOOL)execute:(QCOpenGLContext*)context time:(double)time arguments:(NSDictionary*)arguments
{
// @@@todo: see if there's some sensible way to disable the upstream graph when sampling is disabled
if([inputSampling booleanValue])
{
[outputValue setRawValue:[inputValue rawValue]];
self.priorValue = [outputValue rawValue];
[self stateUpdated];
}
return YES;
}
- (NSDictionary*)state
{
NSMutableDictionary *stateDict = [[NSMutableDictionary alloc] initWithCapacity:2];
[stateDict addEntriesFromDictionary:[super state]];
if(priorValue)
if(DTIsValueSerializable(priorValue))
{
// placing a QCImage directly into stateDict causes a "Property list invalid for format" error, but archiving it and then placing NSData in there does seem to work.
NSData *d = [NSKeyedArchiver archivedDataWithRootObject:priorValue];
[stateDict setObject:d forKey:@"data"];
}
[stateDict autorelease];
return stateDict;
}
- (BOOL)setState:(NSDictionary*)state
{
NSData *d = [state objectForKey:@"data"];
if(d)
self.priorValue = [[NSKeyedUnarchiver unarchiveObjectWithData:d] retain];
return [super setState:state];
}
@end