-
Notifications
You must be signed in to change notification settings - Fork 28
/
Tile_CutterAppDelegate.m
213 lines (185 loc) · 6.88 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//
// 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()
{
int tileHeight, tileWidth;
int tileRowCount, tileColCount;
int progressCol, progressRow;
}
- (void)delayAlert:(NSString *)message;
@end
@implementation Tile_CutterAppDelegate
@synthesize window, tileCutterView, widthTextField, heightTextField, rowBar, columnBar, progressWindow, progressLabel, baseFilename, queue;
- (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.queue = [[[NSOperationQueue alloc] init] autorelease];
}
- (void)saveThread
{
NSLog(@"Save thread started");
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSImage *image = [[[NSImage alloc] initWithContentsOfFile:tileCutterView.filename] autorelease];
[rowBar setIndeterminate:NO];
[columnBar setIndeterminate:NO];
[rowBar setMaxValue:(double)[image rowsWithTileHeight:[heightTextField floatValue]]];
[rowBar setMinValue:0.];
[rowBar setDoubleValue:0.];
[columnBar setMinValue:0.];
[columnBar setMaxValue:(double)[image columnsWithTileWidth:[widthTextField floatValue]]];
[columnBar setDoubleValue:0.];
progressCol = 0;
progressRow = 0;
tileRowCount = [image rowsWithTileHeight:tileHeight];
tileColCount = [image columnsWithTileWidth:tileWidth];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
TileCutterOutputPrefs outputFormat = (TileCutterOutputPrefs)[defaults integerForKey:@"OutputFormat"];
for (int row = 0; row < tileRowCount; row++)
{
// Each row operation gets its own ImageRep to avoid contention
NSBitmapImageRep *imageRep = [[[NSBitmapImageRep alloc] initWithCGImage:[image CGImageForProposedRect:NULL context:NULL hints:nil]] autorelease];
TileOperation *op = [[TileOperation alloc] init];
op.row = row;
op.tileWidth = tileWidth;
op.tileHeight = tileHeight;
op.imageRep = imageRep;
op.baseFilename = baseFilename;
op.delegate = self;
op.outputFormat = outputFormat;
[queue addOperation:op];
[op release];
}
[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];
tileHeight = [heightTextField intValue];
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
{
[columnBar release], columnBar = nil;
[rowBar release], rowBar = nil;
[progressWindow release], progressWindow = nil;
[progressLabel release], progressLabel = nil;
[baseFilename release], baseFilename = nil;
[queue release], queue = nil;
[super dealloc];
}
#pragma mark -
- (void)updateProgress
{
if (progressRow >= tileRowCount)
[NSApp endSheet:progressWindow];
// [rowBar setDoubleValue:(double)progressRow];
// [columnBar setDoubleValue:(double)progressCol];
[progressLabel setStringValue:[NSString stringWithFormat:@"Processing row %d, column %d", progressRow, progressCol]];
}
- (void)operationDidFinishTile:(TileOperation *)op
{
progressCol++;
if (progressCol >= tileColCount)
{
progressCol = 0;
progressRow++;
}
if (progressRow >= 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)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