Archive Project
This commit is contained in:
219
Kara Quick View/GeneratePreviewForURL.m
Executable file
219
Kara Quick View/GeneratePreviewForURL.m
Executable file
@@ -0,0 +1,219 @@
|
||||
#import <CoreFoundation/CoreFoundation.h>
|
||||
#import <CoreServices/CoreServices.h>
|
||||
#import <QuickLook/QuickLook.h>
|
||||
#import <Cocoa/Cocoa.h>
|
||||
|
||||
@interface FakeClass : NSObject
|
||||
|
||||
@end
|
||||
|
||||
@implementation FakeClass
|
||||
|
||||
@end
|
||||
|
||||
OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options);
|
||||
void CancelPreviewGeneration(void *thisInterface, QLPreviewRequestRef preview);
|
||||
|
||||
struct Kara {
|
||||
CGFloat x;
|
||||
CGFloat y;
|
||||
CGFloat direction;
|
||||
};
|
||||
typedef struct Kara Kara;
|
||||
|
||||
/* -----------------------------------------------------------------------------
|
||||
Generate a preview for file
|
||||
|
||||
This function's job is to create preview for designated file
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
void HandleError(NSError *error)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
NSPoint ConvertXMLElelementToPoint(NSXMLElement *element)
|
||||
{
|
||||
NSPoint point;
|
||||
point.x = [[[element attributeForName:@"x"] stringValue] integerValue];
|
||||
point.y = [[[element attributeForName:@"y"] stringValue] integerValue];
|
||||
return point;
|
||||
}
|
||||
|
||||
int fieldBorderLength = 10;
|
||||
|
||||
NSPoint ConvertPointToFlipped(NSPoint point, NSSize world)
|
||||
{
|
||||
NSPoint flippedPoint;
|
||||
flippedPoint.x = point.x;
|
||||
flippedPoint.y = world.height-point.y-1;
|
||||
return flippedPoint;
|
||||
}
|
||||
|
||||
void DrawBackground(NSSize world) {
|
||||
NSColor *greenBackground = [NSColor colorWithCalibratedRed:180/255.0 green:230/255.0 blue:180/255.0 alpha:1];
|
||||
[greenBackground set];
|
||||
NSRectFill(NSMakeRect(0, 0, world.width*fieldBorderLength, world.height*fieldBorderLength));
|
||||
}
|
||||
|
||||
void DrawField(NSSize world)
|
||||
{
|
||||
for (int y = 0; y<world.height; y++) {
|
||||
for (int x = 0; x<world.width; x++) {
|
||||
NSRect field = NSMakeRect(x*fieldBorderLength, y*fieldBorderLength, fieldBorderLength, fieldBorderLength);
|
||||
[[NSColor lightGrayColor] set];
|
||||
NSFrameRectWithWidth(field, 0.25);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DrawKara(Kara kara, NSSize world)
|
||||
{
|
||||
NSString *filename = [NSString stringWithFormat:@"%@%i", @"Kara", ((int)kara.direction)];
|
||||
NSString *imagePath = [[NSBundle bundleForClass:[FakeClass class]] pathForResource:filename ofType:@"tiff"];
|
||||
NSImage *image = [[NSImage alloc] initWithContentsOfFile:imagePath];
|
||||
|
||||
NSPoint flippedOrigin = ConvertPointToFlipped(NSMakePoint(kara.x, kara.y), world);
|
||||
NSPoint origin = NSMakePoint(flippedOrigin.x*fieldBorderLength, flippedOrigin.y*fieldBorderLength);
|
||||
|
||||
NSSize size = {fieldBorderLength, fieldBorderLength};
|
||||
|
||||
NSRect destRect;
|
||||
destRect.origin = origin;
|
||||
destRect.size = size;
|
||||
|
||||
[image drawInRect:destRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
|
||||
}
|
||||
|
||||
void DrawObjects(NSArray *objects, NSString *imageName, NSSize world)
|
||||
{
|
||||
NSString *imagePath = [[NSBundle bundleForClass:[FakeClass class]] pathForResource:imageName ofType:@"tiff"];
|
||||
NSImage *image = [[NSImage alloc] initWithContentsOfFile:imagePath];
|
||||
|
||||
NSSize imageSize = {fieldBorderLength, fieldBorderLength};
|
||||
|
||||
|
||||
for (NSXMLElement *element in objects) {
|
||||
[[NSGraphicsContext currentContext] setImageInterpolation: NSImageInterpolationHigh];
|
||||
|
||||
NSPoint originInWorld = ConvertXMLElelementToPoint(element);
|
||||
NSPoint flippedOriginInWorld = ConvertPointToFlipped(originInWorld, world);
|
||||
|
||||
NSPoint origin;
|
||||
origin.x = flippedOriginInWorld.x * fieldBorderLength;
|
||||
origin.y = flippedOriginInWorld.y * fieldBorderLength;
|
||||
|
||||
NSRect destRect;
|
||||
destRect.origin = origin;
|
||||
destRect.size = imageSize;
|
||||
|
||||
[image drawInRect: destRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
|
||||
}
|
||||
}
|
||||
|
||||
OSStatus GenerateWorldPreview(QLPreviewRequestRef preview, NSXMLElement *root)
|
||||
{
|
||||
NSArray *children = [root children];
|
||||
NSMutableArray *trees = [NSMutableArray new];
|
||||
NSMutableArray *leafs = [NSMutableArray new];
|
||||
NSMutableArray *mushrooms = [NSMutableArray new];
|
||||
Kara kara;
|
||||
BOOL karaExists = NO;
|
||||
|
||||
int worldWidth = [[[root attributeForName:@"sizex"] stringValue] intValue];
|
||||
int worldHeight = [[[root attributeForName:@"sizey"] stringValue] intValue];
|
||||
|
||||
for (NSXMLNode *node in children) {
|
||||
if ([node.name isEqualToString:@"XmlWallPoints"]) {
|
||||
for (NSXMLNode *object in node.children) {
|
||||
[trees addObject:object];
|
||||
}
|
||||
} else if ([node.name isEqualToString:@"XmlObstaclePoints"]) {
|
||||
for (NSXMLNode *object in node.children) {
|
||||
[mushrooms addObject:object];
|
||||
}
|
||||
} else if ([node.name isEqualToString:@"XmlPaintedfieldPoints"]) {
|
||||
for (NSXMLNode *object in node.children) {
|
||||
[leafs addObject:object];
|
||||
}
|
||||
} else if ([node.name isEqualToString:@"XmlKaraList"]) {
|
||||
if (node.children.count > 0) {
|
||||
NSXMLElement *karaElement = [node.children objectAtIndex:0];
|
||||
kara.x = [[[karaElement attributeForName:@"x"] stringValue] integerValue];
|
||||
kara.y = [[[karaElement attributeForName:@"y"] stringValue] integerValue];
|
||||
kara.direction = [[[karaElement attributeForName:@"direction"] stringValue] integerValue];
|
||||
karaExists = YES;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
NSSize world = NSMakeSize(worldWidth, worldHeight);
|
||||
NSSize field = NSMakeSize(worldWidth*fieldBorderLength, worldHeight*fieldBorderLength);
|
||||
|
||||
CGContextRef cgContext = QLPreviewRequestCreateContext(preview, *(CGSize *)&field, false, NULL);
|
||||
NSGraphicsContext* context = [NSGraphicsContext graphicsContextWithGraphicsPort:(void *)cgContext flipped:YES];
|
||||
|
||||
if (context) {
|
||||
[NSGraphicsContext setCurrentContext:context];
|
||||
|
||||
DrawBackground(world);
|
||||
DrawField(world);
|
||||
DrawObjects(trees, @"Tree", world);
|
||||
DrawObjects(leafs, @"Shamrock", world);
|
||||
DrawObjects(mushrooms, @"Mushroom", world);
|
||||
if (karaExists) {
|
||||
DrawKara(kara, world);
|
||||
}
|
||||
|
||||
QLPreviewRequestFlushContext(preview, cgContext);
|
||||
CFRelease(cgContext);
|
||||
}
|
||||
|
||||
return noErr;
|
||||
|
||||
}
|
||||
|
||||
OSStatus GenerateProgramPreview(QLPreviewRequestRef preview, NSXMLElement *root)
|
||||
{
|
||||
return noErr;
|
||||
}
|
||||
|
||||
OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options)
|
||||
{
|
||||
NSXMLDocument *xmlDoc;
|
||||
NSError *err=nil;
|
||||
NSURL *furl = (__bridge NSURL *)url;
|
||||
if (!furl) {
|
||||
NSLog(@"Can't create an URL from file %@.", furl.path);
|
||||
return noErr;
|
||||
}
|
||||
xmlDoc = [[NSXMLDocument alloc] initWithContentsOfURL:furl options:(NSXMLNodePreserveWhitespace|NSXMLNodePreserveCDATA) error:&err];
|
||||
if (xmlDoc == nil) {
|
||||
xmlDoc = [[NSXMLDocument alloc] initWithContentsOfURL:furl options:NSXMLDocumentTidyXML error:&err];
|
||||
}
|
||||
if (xmlDoc == nil) {
|
||||
if (err) {
|
||||
HandleError(err);
|
||||
}
|
||||
return noErr;
|
||||
}
|
||||
|
||||
if (err) {
|
||||
HandleError(err);
|
||||
return noErr;
|
||||
}
|
||||
|
||||
NSXMLElement *root = xmlDoc.rootElement;
|
||||
|
||||
if ([furl.pathExtension compare:@"world" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
|
||||
return GenerateWorldPreview(preview, root);
|
||||
} else if ([furl.pathExtension compare:@"kara" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
|
||||
return GenerateProgramPreview(preview, root);
|
||||
}
|
||||
return noErr;
|
||||
}
|
||||
|
||||
void CancelPreviewGeneration(void *thisInterface, QLPreviewRequestRef preview)
|
||||
{
|
||||
// Implement only if supported
|
||||
}
|
||||
Reference in New Issue
Block a user