-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunction.mm
executable file
·199 lines (185 loc) · 5.96 KB
/
Function.mm
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//
// Function.m
// VisionSandbox
//
// Created by Joel Brogan on 8/9/14.
// Copyright (c) 2014 Magna Mirrors. All rights reserved.
//
#import "Function.h"
@implementation Function
@synthesize parametersView,functionName,documentation,runTime,displayStepAsLayer,inputType,outputType,functionTreePath,hasSettingsWindow;
-(id)initWithCoder:(NSCoder *)aDecoder
{
{
self = [super init];
if (self) {
functionName = [aDecoder decodeObjectForKey:@"functionName"];
functionTreePath = [aDecoder decodeObjectForKey:@"functionTreePath"];
runTime = 0;
displayStepAsLayer = [aDecoder decodeBoolForKey:@"displayStepAsLayer"];
hasSettingsWindow = [aDecoder decodeBoolForKey:@"hasSettingsWindow"];
inputType = NSClassFromString([aDecoder decodeObjectForKey:@"inputType"]);
outputType = NSClassFromString([aDecoder decodeObjectForKey:@"outputType"]);
timer = [[Timer alloc] init];
propertySettings = [[NSDictionary alloc] init];
inputType = OpenImageHandler.class;
outputType = inputType;
[self createParameterView];
}
return self;
}
}
- (id)init
{
self = [super init];
if (self) {
parametersView = [[FunctionVisualParametersView alloc] init];
timer = [[Timer alloc] init];
runTime = 0;
displayStepAsLayer = true;
hasSettingsWindow = true;
[self createParameterView];
propertySettings = [[NSDictionary alloc] init];
}
return self;
}
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:functionName forKey:@"functionName"];
[aCoder encodeObject:functionTreePath forKey:@"functionTreePath"];
[aCoder encodeBool:displayStepAsLayer forKey:@"displayStepAsLayer"];
[aCoder encodeBool:hasSettingsWindow forKey:@"hasSettingsWindow"];
[aCoder encodeObject:NSStringFromClass(inputType) forKey:@"inputType"];
[aCoder encodeObject:NSStringFromClass(outputType) forKey:@"outputType"];
}
-(id)copyWithZone:(NSZone *)zone
{
Function *newFunc = [[self.class allocWithZone:zone] init];
newFunc.functionName = functionName.copy;
newFunc.functionTreePath = functionTreePath.copy;
newFunc.documentation = documentation;
newFunc->dynamicProps = dynamicProps.copy;
newFunc->hasSettingsWindow = hasSettingsWindow;
newFunc->displayStepAsLayer = displayStepAsLayer;
newFunc->inputType = inputType;
newFunc->outputType = outputType;
newFunc->propertySettings = propertySettings.copy;
return newFunc;
}
-(id)runMethod:(id)input
{
return input;
}
- (id)run:(id)input;
{
[self beginRun];
id output = nil;
if ([input isKindOfClass:inputType]) {
output = [self runMethod:[(NSObject *)input copy]];
}
else{
[self sendError];
}
[self endRun];
return output;
}
- (void)beginRun
{
[self loadViewToParameters];
[timer startTimer];
}
- (void)endRun
{
[timer stopTimer];
runTime = [timer timeElapsedInMilliseconds];
}
-(void)loadParametersToView
{
for(NSString *key in dynamicProps.allKeys)
{
NSTableCellView *subview = [parametersView subviewForKey:key];
if (subview && [subview isKindOfClass:FunctionVisualTableCellView.class])
{
[(FunctionVisualTableCellView *)subview applyValue:[self valueForKey:key]];
}
}
}
-(void)loadViewToParameters
{
for(NSString *key in dynamicProps.allKeys)
{
NSTableCellView *subview = [parametersView subviewForKey:key];
if (subview && [subview isKindOfClass:FunctionVisualTableCellView.class])
{
[self setValue:[(FunctionVisualTableCellView *)subview getValue] forKey:key];
}
}
}
- (void)applyParameterViewSettings
{
for(int i = 0; i < propertySettings.count; i++)
{
NSString *key1 = [propertySettings.allKeys objectAtIndex:i];
NSDictionary *sets = [propertySettings objectForKey:key1];
NSTableCellView *subview = [parametersView subviewForKey:key1];
if (subview) {
for(int j = 0; j < sets.count; j++)
{
NSString *key = [sets.allKeys objectAtIndex:j];
if ([subview isKindOfClass:FunctionVisualTableCellView.class])
{
[(FunctionVisualTableCellView *)subview applySetting:[sets objectForKey:key] forKey:key];
}
}
}
}
}
- (void)createParameterView
{
VisualFunctionViewHolder *sharedHolder = [VisualFunctionViewHolder sharedViewHolder];
NSDictionary *props = [PropertyUtility classPropsFor:self.class];
NSArray *setKeys = [props.allKeys filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self BEGINSWITH 'set_'"]];
dynamicProps = [NSMutableDictionary dictionaryWithObjects:[props objectsForKeys:setKeys notFoundMarker:@""] forKeys:setKeys];
for(int i = 0; i < dynamicProps.count; i++)
{
NSString *key = [dynamicProps.allKeys objectAtIndex:i];
NSString *propType = [dynamicProps objectForKey:key];
if ([propType isEqualToString:@"f"]) propType = @"d";
FunctionVisualTableCellView *subview = [sharedHolder.viewHolder makeViewWithIdentifier:propType owner:self];
if(subview)[parametersView addSubview:subview forKey:key];
else NSLog(@"WARNING: A visual view does not exist for property %@. Please create a custom view.",key);
}
if (dynamicProps.count <1)hasSettingsWindow = false;
// [subview setBackgroundColor:[NSColor redColor]];
// if (subview) [parametersView addSubview:subview forKey:@"sliderThing"];
// ComboView *cView = [sharedHolder.viewHolder makeViewWithIdentifier:@"Combo" owner:self];
// [cView setItems:@{@"One": [NSNumber numberWithInteger:1],@"Two":[NSNumber numberWithInteger:2]}.mutableCopy];
// if (cView)[parametersView addSubview:cView forKey:@"comboBox"];
}
- (NSArray*)writableTypesForPasteboard:(NSPasteboard*)pasteboard
{
return @[@"Function.func"];
}
- (id)pasteboardPropertyListForType:(NSString *)type
{
return nil;
}
- (NSPasteboardWritingOptions)writingOptionsForType:(NSString*)type pasteboard:(NSPasteboard*)pasteboard
{
return 0;
}
+ (NSArray*)readableTypesForPasteboard:(NSPasteboard*)pasteboard
{
return @[@"Function.func"];
}
+ (NSPasteboardReadingOptions)readingOptionsForType:(NSString*)type pasteboard:(NSPasteboard*)pasteboard
{
// NSPasteboardReadingAsData
return NSPasteboardReadingAsString;
}
-(void)sendError
{
NSLog(@"error!");
[[NSNotificationCenter defaultCenter] postNotificationName:@"Runtime Error" object:self];
}
@end