-
Notifications
You must be signed in to change notification settings - Fork 130
/
PXPatternEditorController.m
executable file
·191 lines (142 loc) · 5.1 KB
/
PXPatternEditorController.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
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
//
// PXPatternEditorController.m
// Pixen
//
#import "PXPatternEditorController.h"
#import "PathUtilities.h"
#import "PXDeleteCollectionView.h"
#import "PXPattern.h"
#import "PXPatternEditorView.h"
#import "PXPatternItem.h"
#import "SBCenteringClipView.h"
@implementation PXPatternEditorController
@synthesize patternsController, collectionView, scrollView, editorView;
+ (id)sharedController
{
static PXPatternEditorController *sharedInstance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[PXPatternEditorController alloc] init];
});
return sharedInstance;
}
- (void)awakeFromNib
{
[self.collectionView addObserver:self forKeyPath:@"selectionIndexes" options:0 context:NULL];
SBCenteringClipView *clip = [[SBCenteringClipView alloc] initWithFrame:[[scrollView contentView] frame]];
[clip setBackgroundColor:[NSColor lightGrayColor]];
[clip setCopiesOnScroll:NO];
[(NSScrollView *)scrollView setContentView:clip];
[scrollView setDocumentView:editorView];
[self reloadPatterns];
}
- (void)setPattern:(PXPattern *)pattern
{
if (pattern == _pattern)
return;
_pattern = pattern;
NSSize patternSize = [_pattern size];
if (patternSize.width < 2)
patternSize.width = 2;
if (patternSize.height < 2)
patternSize.height = 2;
if (!NSEqualSizes([_pattern size], patternSize))
[_pattern setSize:patternSize];
if (editorView) {
[editorView setFrame:NSMakeRect(0.0f, 0.0f, patternSize.width * 32.0f, patternSize.height * 32.0f)];
[editorView setPattern:_pattern];
}
}
- (IBAction)newPattern:(id)sender
{
int line = 4;
PXPattern *pattern = [[PXPattern alloc] init];
[pattern setSize:NSMakeSize(line, line)];
for (int x=0; x<line; x++) {
for (int y=0; y<line; y++) {
[pattern addPoint:NSMakePoint(x, y)];
}
}
[self addPattern:pattern];
}
- (void)deleteSheetDidEnd:(NSAlert *)alert returnCode:(int)returnCode contextInfo:contextInfo
{
if (returnCode == NSAlertFirstButtonReturn)
{
PXPattern *selectedPattern = [[patternsController selectedObjects] objectAtIndex:0];
[self removePattern:selectedPattern];
}
}
- (id)init
{
return [super initWithWindowNibName:@"PXPatternEditor"];
}
- (void)reloadPatterns
{
BOOL isDirectory = NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *patternFileName = GetPixenPatternFile();
if (![fileManager fileExistsAtPath:patternFileName isDirectory:&isDirectory] || isDirectory)
return;
[patternsController removeObjects:[patternsController arrangedObjects]];
NSArray *p = [NSKeyedUnarchiver unarchiveObjectWithFile:patternFileName];
if (p)
[patternsController addObjects:p];
}
- (void)deleteKeyPressedInCollectionView:(NSCollectionView *)view
{
if ([patternsController selectionIndex] == NSNotFound)
return;
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:NSLocalizedString(@"Delete", @"DELETE")];
NSButton *button = [alert addButtonWithTitle:NSLocalizedString(@"Cancel", @"CANCEL")];
[button setKeyEquivalent:@"\r"];
[alert setMessageText:NSLocalizedString(@"Are you sure you want to delete the selected pattern?", @"PATTERN_DELETE_PROMPT")];
[alert setInformativeText:NSLocalizedString(@"This operation cannot be undone.", @"PATTERN_DELETE_INFORMATIVE_TEXT")];
[alert beginSheetModalForWindow:[self window]
modalDelegate:self
didEndSelector:@selector(deleteSheetDidEnd:returnCode:contextInfo:)
contextInfo:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"selectionIndexes"]) {
NSUInteger index = [[self.collectionView selectionIndexes] firstIndex];
if (index != NSNotFound) {
PXPattern *pattern = [[patternsController arrangedObjects] objectAtIndex:index];
[self setPattern:pattern];
}
}
}
- (void)addPattern:(PXPattern *)pattern
{
[patternsController addObject:pattern];
[NSKeyedArchiver archiveRootObject:[patternsController arrangedObjects]
toFile:GetPixenPatternFile()];
[[NSNotificationCenter defaultCenter] postNotificationName:PXPatternsChangedNotificationName
object:self
userInfo:nil];
}
- (void)removePattern:(PXPattern *)pattern
{
[patternsController removeObject:pattern];
[NSKeyedArchiver archiveRootObject:[patternsController arrangedObjects]
toFile:GetPixenPatternFile()];
[[NSNotificationCenter defaultCenter] postNotificationName:PXPatternsChangedNotificationName
object:self
userInfo:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[collectionView removeObserver:self forKeyPath:@"selectionIndexes"];
}
- (BOOL)collectionView:(NSCollectionView *)collectionView writeItemsAtIndexes:(NSIndexSet *)indexes toPasteboard:(NSPasteboard *)pasteboard
{
NSUInteger index = [indexes firstIndex];
PXPattern *pattern = [[patternsController arrangedObjects] objectAtIndex:index];
[pasteboard declareTypes:[NSArray arrayWithObject:PXPatternPboardType] owner:self];
[pasteboard setData:[NSKeyedArchiver archivedDataWithRootObject:pattern] forType:PXPatternPboardType];
return YES;
}
@end