Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 196 lines (160 sloc) 5.98 kB
fec8f94 Initial commit
jlamarche authored
1 //
2 // Tile_CutterAppDelegate.m
3 // Tile Cutter
4 //
5 // Created by jeff on 10/7/10.
6 // Copyright 2010 __MyCompanyName__. All rights reserved.
7 //
8
1c5e66f Moved tile cutting into an NSOperation to leverage all computing core…
jlamarche authored
9 #include <dispatch/dispatch.h>
fec8f94 Initial commit
jlamarche authored
10 #import "Tile_CutterAppDelegate.h"
11 #import "TileCutterView.h"
12 #import "NSImage-Tile.h"
b07d1ae Booyah, final.
jlamarche authored
13 #import "NSUserDefaults-MCColor.h"
1c5e66f Moved tile cutting into an NSOperation to leverage all computing core…
jlamarche authored
14 #import "TileOperation.h"
ea8d848 Working.
jlamarche authored
15
b54e258 Added ability to specify the output file type in the preferences
jlamarche authored
16
17
fec8f94 Initial commit
jlamarche authored
18 @interface Tile_CutterAppDelegate()
0d19983 @psineur Moved more stuff to TileCutterCore. AppDelegate compiles OK
authored
19
1c5e66f Moved tile cutting into an NSOperation to leverage all computing core…
jlamarche authored
20 - (void)delayAlert:(NSString *)message;
0d19983 @psineur Moved more stuff to TileCutterCore. AppDelegate compiles OK
authored
21
fec8f94 Initial commit
jlamarche authored
22 @end
23
24
25 @implementation Tile_CutterAppDelegate
26
0d19983 @psineur Moved more stuff to TileCutterCore. AppDelegate compiles OK
authored
27 @synthesize window, tileCutterView, widthTextField, heightTextField, rowBar, columnBar, progressWindow, progressLabel, baseFilename;
fec8f94 Initial commit
jlamarche authored
28
71f87b4 Fixed a problem with tiny images where the tiling finished before the…
jlamarche authored
29 - (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename
30 {
1e67893 finished tweaking operation
jlamarche authored
31
71f87b4 Fixed a problem with tiny images where the tiling finished before the…
jlamarche authored
32 tileCutterView.filename = filename;
33 NSImage *theImage = [[NSImage alloc] initWithContentsOfFile:filename];
34 if (theImage == nil)
35 return NO;
36
37 tileCutterView.image = theImage;
38 [theImage release];
39 [tileCutterView setNeedsDisplay:YES];
40 return YES;
41 }
42
fec8f94 Initial commit
jlamarche authored
43 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
44 {
b07d1ae Booyah, final.
jlamarche authored
45 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
46 NSColor *guideColor = [defaults colorForKey:@"guideColor"];
47 if (guideColor == nil)
48 {
49 [defaults setColor:[NSColor redColor] forKey:@"guideColor"];
50 [defaults setInteger:200 forKey:@"widthField"];
51 [defaults setBool:YES forKey:@"showGuides"];
52 [defaults setInteger:200 forKey:@"heightField"];
53 }
54
83693bc @psineur Moved Tiling Logics to TileCutterCore
authored
55 self.tileCore = [TileCutterCore new];
0d19983 @psineur Moved more stuff to TileCutterCore. AppDelegate compiles OK
authored
56 self.tileCore.operationsDelegate = self;
fec8f94 Initial commit
jlamarche authored
57 }
83693bc @psineur Moved Tiling Logics to TileCutterCore
authored
58
fec8f94 Initial commit
jlamarche authored
59 - (void)saveThread
60 {
0774566 Tweaked the operation queue functionality
jlamarche authored
61 NSLog(@"Save thread started");
fec8f94 Initial commit
jlamarche authored
62 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
63
9a4c068 @psineur Added allTilesInfo, that holds info about all tiles from last save.
authored
64
83693bc @psineur Moved Tiling Logics to TileCutterCore
authored
65 // Setup Tile Core
0d19983 @psineur Moved more stuff to TileCutterCore. AppDelegate compiles OK
authored
66 self.tileCore.inputFilename = tileCutterView.filename;
83693bc @psineur Moved Tiling Logics to TileCutterCore
authored
67 self.tileCore.outputFormat = (TileCutterOutputPrefs)[[NSUserDefaults standardUserDefaults] integerForKey:@"OutputFormat"];
68 self.tileCore.outputBaseFilename = baseFilename;
69 if (![tileCutterView.skipCheckbox intValue])
70 self.tileCore.keepAllTiles = YES;
71 else
72 self.tileCore.keepAllTiles = NO;
aa2efb7 @psineur Added source image info to the Plist output.
authored
73
83693bc @psineur Moved Tiling Logics to TileCutterCore
authored
74 // Start Tiling
75 [self.tileCore startSavingTiles];
1e67893 finished tweaking operation
jlamarche authored
76
fec8f94 Initial commit
jlamarche authored
77 [pool drain];
78 }
83693bc @psineur Moved Tiling Logics to TileCutterCore
authored
79
fec8f94 Initial commit
jlamarche authored
80 - (IBAction)saveButtonPressed:(id)sender
81 {
82 NSSavePanel *sp = [NSSavePanel savePanel];
83 [sp setRequiredFileType:@"jpg"];
84
85 [sp beginSheetForDirectory:nil
86 file:@"output.jpg"
87 modalForWindow:window
88 modalDelegate:self
89 didEndSelector:@selector(didEndSaveSheet:returnCode:conextInfo:)
90 contextInfo:nil];
91 }
92 -(void)didEndSaveSheet:(NSSavePanel *)savePanel
93 returnCode:(int)returnCode conextInfo:(void *)contextInfo
94 {
95 if (returnCode == NSOKButton)
96 {
97 self.baseFilename = [[savePanel filename] stringByDeletingPathExtension];
0d19983 @psineur Moved more stuff to TileCutterCore. AppDelegate compiles OK
authored
98 self.tileCore.tileHeight = [heightTextField intValue];
99 self.tileCore.tileWidth = [widthTextField intValue];
fec8f94 Initial commit
jlamarche authored
100
ea8d848 Working.
jlamarche authored
101 [self performSelector:@selector(delayPresentSheet) withObject:nil afterDelay:0.1];
fec8f94 Initial commit
jlamarche authored
102 }
103 }
104 - (void)delayPresentSheet
105 {
106 [progressLabel setStringValue:@"Analyzing image for tile cutting…"];
b07d1ae Booyah, final.
jlamarche authored
107 [rowBar setIndeterminate:YES];
108 [columnBar setIndeterminate:YES];
fec8f94 Initial commit
jlamarche authored
109 [rowBar startAnimation:self];
110 [columnBar startAnimation:self];
111
112 [NSApp beginSheet: progressWindow
113 modalForWindow: window
114 modalDelegate: self
115 didEndSelector: @selector(didEndSheet:returnCode:contextInfo:)
116 contextInfo: nil];
71f87b4 Fixed a problem with tiny images where the tiling finished before the…
jlamarche authored
117
0774566 Tweaked the operation queue functionality
jlamarche authored
118 //[queue setSuspended:YES];
71f87b4 Fixed a problem with tiny images where the tiling finished before the…
jlamarche authored
119 [self performSelectorInBackground:@selector(saveThread) withObject:nil];
fec8f94 Initial commit
jlamarche authored
120 }
83693bc @psineur Moved Tiling Logics to TileCutterCore
authored
121
fec8f94 Initial commit
jlamarche authored
122 - (void)didEndSheet:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
123 {
124 [sheet orderOut:self];
b07d1ae Booyah, final.
jlamarche authored
125
126 }
127 - (IBAction)openSelected:(id)sender
128 {
129 NSOpenPanel *op = [NSOpenPanel openPanel];
130 [op setAllowedFileTypes:[NSImage imageFileTypes]];
131
132 [op beginSheetModalForWindow:window completionHandler:^(NSInteger returnCode){
133 if (returnCode == NSOKButton)
134 {
135 NSString *filename = [op filename];
71f87b4 Fixed a problem with tiny images where the tiling finished before the…
jlamarche authored
136 [self application:NSApp openFile:filename];
b07d1ae Booyah, final.
jlamarche authored
137 }
138 }];
139
fec8f94 Initial commit
jlamarche authored
140 }
141 - (void)dealloc
142 {
83693bc @psineur Moved Tiling Logics to TileCutterCore
authored
143 self.tileCore = nil;
fec8f94 Initial commit
jlamarche authored
144 [columnBar release], columnBar = nil;
145 [rowBar release], rowBar = nil;
146 [progressWindow release], progressWindow = nil;
147 [progressLabel release], progressLabel = nil;
148 [baseFilename release], baseFilename = nil;
149 [super dealloc];
150 }
1c5e66f Moved tile cutting into an NSOperation to leverage all computing core…
jlamarche authored
151 #pragma mark -
f826d08 removed progress indicator to work around Apple bug
jlamarche authored
152 - (void)updateProgress
153 {
0d19983 @psineur Moved more stuff to TileCutterCore. AppDelegate compiles OK
authored
154 if (self.tileCore.progressRow >= self.tileCore.tileRowCount)
6b02814 @psineur Added autosaving plist file. A lot of duplicates now.
authored
155 {
f826d08 removed progress indicator to work around Apple bug
jlamarche authored
156 [NSApp endSheet:progressWindow];
6b02814 @psineur Added autosaving plist file. A lot of duplicates now.
authored
157 }
f826d08 removed progress indicator to work around Apple bug
jlamarche authored
158
159 // [rowBar setDoubleValue:(double)progressRow];
160 // [columnBar setDoubleValue:(double)progressCol];
0d19983 @psineur Moved more stuff to TileCutterCore. AppDelegate compiles OK
authored
161 [progressLabel setStringValue:[NSString stringWithFormat:@"Processing row %d, column %d", self.tileCore.progressRow, self.tileCore.progressCol]];
f826d08 removed progress indicator to work around Apple bug
jlamarche authored
162 }
0774566 Tweaked the operation queue functionality
jlamarche authored
163 - (void)operationDidFinishTile:(TileOperation *)op
1c5e66f Moved tile cutting into an NSOperation to leverage all computing core…
jlamarche authored
164 {
0d19983 @psineur Moved more stuff to TileCutterCore. AppDelegate compiles OK
authored
165 if (self.tileCore.progressRow >= self.tileCore.tileRowCount)
1c5e66f Moved tile cutting into an NSOperation to leverage all computing core…
jlamarche authored
166 [NSApp endSheet:progressWindow];
167
f826d08 removed progress indicator to work around Apple bug
jlamarche authored
168 // [rowBar setDoubleValue:(double)progressRow];
169 // [columnBar setDoubleValue:(double)progressCol];
170 // [progressLabel setStringValue:[NSString stringWithFormat:@"Processing row %d, column %d", progressRow, progressCol]];
1e67893 finished tweaking operation
jlamarche authored
171
f826d08 removed progress indicator to work around Apple bug
jlamarche authored
172 [self performSelectorOnMainThread:@selector(updateProgress) withObject:nil waitUntilDone:NO];
44cac0f @psineur Fixed duplicates in Plist file.
authored
173 }
174
175 - (void)operationDidFinishSuccessfully:(TileOperation *)op
176 {
83693bc @psineur Moved Tiling Logics to TileCutterCore
authored
177
1c5e66f Moved tile cutting into an NSOperation to leverage all computing core…
jlamarche authored
178 }
44cac0f @psineur Fixed duplicates in Plist file.
authored
179
1c5e66f Moved tile cutting into an NSOperation to leverage all computing core…
jlamarche authored
180 - (void)delayAlert:(NSString *)message
181 {
182 NSAlert *alert = [[[NSAlert alloc] init] autorelease];
183 [alert addButtonWithTitle:@"Crud"];
184 [alert setMessageText:@"There was an error tiling this image."];
185 [alert setInformativeText:message];
186 [alert setAlertStyle:NSCriticalAlertStyle];
187 [alert beginSheetModalForWindow:[self window] modalDelegate:nil didEndSelector:nil contextInfo:nil];
188 }
83693bc @psineur Moved Tiling Logics to TileCutterCore
authored
189
1c5e66f Moved tile cutting into an NSOperation to leverage all computing core…
jlamarche authored
190 - (void)operation:(TileOperation *)op didFailWithMessage:(NSString *)message
191 {
192 [NSApp endSheet:progressWindow];
193 [self performSelector:@selector(delayAlert:) withObject:nil afterDelay:0.5];
194 }
fec8f94 Initial commit
jlamarche authored
195 @end
Something went wrong with that request. Please try again.