forked from jlamarche/Tile-Cutter
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Tile_CutterAppDelegate.m
195 lines (160 loc) · 5.84 KB
/
Tile_CutterAppDelegate.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
192
193
194
195
//
// Tile_CutterAppDelegate.m
// Tile Cutter
//
// Created by jeff on 10/7/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#include <dispatch/dispatch.h>
#import "Tile_CutterAppDelegate.h"
#import "TileCutterView.h"
#import "NSImage-Tile.h"
#import "NSUserDefaults-MCColor.h"
#import "TileOperation.h"
@interface Tile_CutterAppDelegate()
- (void)delayAlert:(NSString *)message;
@end
@implementation Tile_CutterAppDelegate
@synthesize window, tileCutterView, widthTextField, heightTextField, rowBar, columnBar, progressWindow, progressLabel, baseFilename;
- (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename
{
tileCutterView.filename = filename;
NSImage *theImage = [[NSImage alloc] initWithContentsOfFile:filename];
if (theImage == nil)
return NO;
tileCutterView.image = theImage;
[theImage release];
[tileCutterView setNeedsDisplay:YES];
return YES;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSColor *guideColor = [defaults colorForKey:@"guideColor"];
if (guideColor == nil)
{
[defaults setColor:[NSColor redColor] forKey:@"guideColor"];
[defaults setInteger:200 forKey:@"widthField"];
[defaults setBool:YES forKey:@"showGuides"];
[defaults setInteger:200 forKey:@"heightField"];
}
self.tileCore = [TileCutterCore new];
self.tileCore.operationsDelegate = self;
}
- (void)saveThread
{
NSLog(@"Save thread started");
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Setup Tile Core
self.tileCore.inputFilename = tileCutterView.filename;
self.tileCore.outputFormat = (TileCutterOutputPrefs)[[NSUserDefaults standardUserDefaults] integerForKey:@"OutputFormat"];
self.tileCore.outputBaseFilename = baseFilename;
if (![tileCutterView.skipCheckbox intValue])
self.tileCore.keepAllTiles = YES;
else
self.tileCore.keepAllTiles = NO;
// Start Tiling
[self.tileCore startSavingTiles];
[pool drain];
}
- (IBAction)saveButtonPressed:(id)sender
{
NSSavePanel *sp = [NSSavePanel savePanel];
[sp setRequiredFileType:@"jpg"];
[sp beginSheetForDirectory:nil
file:@"output.jpg"
modalForWindow:window
modalDelegate:self
didEndSelector:@selector(didEndSaveSheet:returnCode:conextInfo:)
contextInfo:nil];
}
-(void)didEndSaveSheet:(NSSavePanel *)savePanel
returnCode:(int)returnCode conextInfo:(void *)contextInfo
{
if (returnCode == NSOKButton)
{
self.baseFilename = [[savePanel filename] stringByDeletingPathExtension];
self.tileCore.tileHeight = [heightTextField intValue];
self.tileCore.tileWidth = [widthTextField intValue];
[self performSelector:@selector(delayPresentSheet) withObject:nil afterDelay:0.1];
}
}
- (void)delayPresentSheet
{
[progressLabel setStringValue:@"Analyzing image for tile cutting…"];
[rowBar setIndeterminate:YES];
[columnBar setIndeterminate:YES];
[rowBar startAnimation:self];
[columnBar startAnimation:self];
[NSApp beginSheet: progressWindow
modalForWindow: window
modalDelegate: self
didEndSelector: @selector(didEndSheet:returnCode:contextInfo:)
contextInfo: nil];
//[queue setSuspended:YES];
[self performSelectorInBackground:@selector(saveThread) withObject:nil];
}
- (void)didEndSheet:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
[sheet orderOut:self];
}
- (IBAction)openSelected:(id)sender
{
NSOpenPanel *op = [NSOpenPanel openPanel];
[op setAllowedFileTypes:[NSImage imageFileTypes]];
[op beginSheetModalForWindow:window completionHandler:^(NSInteger returnCode){
if (returnCode == NSOKButton)
{
NSString *filename = [op filename];
[self application:NSApp openFile:filename];
}
}];
}
- (void)dealloc
{
self.tileCore = nil;
[columnBar release], columnBar = nil;
[rowBar release], rowBar = nil;
[progressWindow release], progressWindow = nil;
[progressLabel release], progressLabel = nil;
[baseFilename release], baseFilename = nil;
[super dealloc];
}
#pragma mark -
- (void)updateProgress
{
if (self.tileCore.progressRow >= self.tileCore.tileRowCount)
{
[NSApp endSheet:progressWindow];
}
// [rowBar setDoubleValue:(double)progressRow];
// [columnBar setDoubleValue:(double)progressCol];
[progressLabel setStringValue:[NSString stringWithFormat:@"Processing row %d, column %d", self.tileCore.progressRow, self.tileCore.progressCol]];
}
- (void)operationDidFinishTile:(TileOperation *)op
{
if (self.tileCore.progressRow >= self.tileCore.tileRowCount)
[NSApp endSheet:progressWindow];
// [rowBar setDoubleValue:(double)progressRow];
// [columnBar setDoubleValue:(double)progressCol];
// [progressLabel setStringValue:[NSString stringWithFormat:@"Processing row %d, column %d", progressRow, progressCol]];
[self performSelectorOnMainThread:@selector(updateProgress) withObject:nil waitUntilDone:NO];
}
- (void)operationDidFinishSuccessfully:(TileOperation *)op
{
}
- (void)delayAlert:(NSString *)message
{
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert addButtonWithTitle:@"Crud"];
[alert setMessageText:@"There was an error tiling this image."];
[alert setInformativeText:message];
[alert setAlertStyle:NSCriticalAlertStyle];
[alert beginSheetModalForWindow:[self window] modalDelegate:nil didEndSelector:nil contextInfo:nil];
}
- (void)operation:(TileOperation *)op didFailWithMessage:(NSString *)message
{
[NSApp endSheet:progressWindow];
[self performSelector:@selector(delayAlert:) withObject:nil afterDelay:0.5];
}
@end