From e103fdf4fed43f76cc298c98869bf395c187b650 Mon Sep 17 00:00:00 2001 From: Ichi Kanaya Date: Mon, 24 Oct 2011 20:26:26 +0900 Subject: [PATCH] works far better --- Clash/ClashDocument.m | 66 +++-- Clash/ClashViewController.h | 6 +- Clash/ClashViewController.m | 110 +++++---- Clash/en.lproj/ClashDocument.xib | 518 ++++++++++++++------------------------- 4 files changed, 279 insertions(+), 421 deletions(-) diff --git a/Clash/ClashDocument.m b/Clash/ClashDocument.m index a7f86d0..fadb9c2 100644 --- a/Clash/ClashDocument.m +++ b/Clash/ClashDocument.m @@ -10,47 +10,61 @@ @implementation ClashDocument -- (id)init -{ - self = [super init]; - if (self) { +- (id)init { + self = [super init]; + if (self) { // Add your subclass-specific initialization here. // If an error occurs here, return nil. - } - return self; + } + return self; } -- (NSString *)windowNibName -{ +- (NSString *)windowNibName { // Override returning the nib file name of the document - // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead. + // If you need to use a subclass of NSWindowController or if your document + // supports multiple NSWindowControllers, you should remove this method and + // override -makeWindowControllers instead. return @"ClashDocument"; } -- (void)windowControllerDidLoadNib:(NSWindowController *)aController -{ +- (void)windowControllerDidLoadNib: (NSWindowController *)aController { [super windowControllerDidLoadNib:aController]; - // Add any code here that needs to be executed once the windowController has loaded the document's window. + // Add any code here that needs to be executed once the windowController has + // loaded the document's window. } -- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError -{ - /* - Insert code here to write your document to data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning nil. - You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead. - */ - NSException *exception = [NSException exceptionWithName:@"UnimplementedMethod" reason:[NSString stringWithFormat:@"%@ is unimplemented", NSStringFromSelector(_cmd)] userInfo:nil]; +- (NSData *)dataOfType: (NSString *)typeName + error: (NSError **)outError { + // Insert code here to write your document to data of the specified type. If + // outError != NULL, ensure that you create and set an appropriate error when + // returning nil. + // You can also choose to override -fileWrapperOfType:error:, + // -writeToURL:ofType:error:, or -writeToURL:ofType: forSaveOperation: + // originalContentsURL: error: instead. + NSException *exception + = [NSException exceptionWithName: @"UnimplementedMethod" + reason: [NSString stringWithFormat: + @"%@ is unimplemented", + NSStringFromSelector(_cmd)] + userInfo: nil]; @throw exception; return nil; } -- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError -{ - /* - Insert code here to read your document from the given data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning NO. - You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead. - */ - NSException *exception = [NSException exceptionWithName:@"UnimplementedMethod" reason:[NSString stringWithFormat:@"%@ is unimplemented", NSStringFromSelector(_cmd)] userInfo:nil]; +- (BOOL)readFromData:(NSData *)data + ofType: (NSString *)typeName + error: (NSError **)outError { + // Insert code here to read your document from the given data of the + // specified type. If outError != NULL, ensure that you create and set an + // appropriate error when returning NO. + // You can also choose to override -readFromFileWrapper: ofType:error: or + // -readFromURL: ofType:error: instead. + NSException *exception + = [NSException exceptionWithName: @"UnimplementedMethod" + reason: [NSString stringWithFormat: + @"%@ is unimplemented", + NSStringFromSelector(_cmd)] + userInfo: nil]; @throw exception; return YES; } diff --git a/Clash/ClashViewController.h b/Clash/ClashViewController.h index f1e21fa..5681a6d 100644 --- a/Clash/ClashViewController.h +++ b/Clash/ClashViewController.h @@ -12,14 +12,14 @@ @interface ClashViewController: NSObject { IBOutlet NSTextField *addressField; IBOutlet NSView *view; - IBOutlet NSArrayController *frameArrayController; IBOutlet NSButton *goButton; + IBOutlet NSButton *autoRunMode; CALayer *backgroundLayer; NSMutableDictionary *layers; + NSTimer *timer; } -@property (assign) NSMutableArray *frames; - - (IBAction)go: (id)sender; +- (IBAction)autoRun: (id)sender; @end diff --git a/Clash/ClashViewController.m b/Clash/ClashViewController.m index 45f60a3..b328369 100644 --- a/Clash/ClashViewController.m +++ b/Clash/ClashViewController.m @@ -10,7 +10,27 @@ @implementation ClashViewController -@synthesize frames; +#define defaultViewRatio 0.1 +#define defaultViewSizeX 1000.0 +#define defalutViewSizeY 250.0 +#define viewAspectRatio (defaultViewSizeX / defalutViewSizeY) + + +- (CGFloat) mmToPix: (CGFloat)x { + NSRect currentViewFrame = [view frame]; + CGFloat currentViewSizeX = currentViewFrame.size.width; + CGFloat currentViewSizeY = currentViewFrame.size.height; + CGFloat currentViewSizeYMultipliedByAspectRatio = currentViewSizeY * viewAspectRatio; + CGFloat viewRatio; + if (currentViewSizeX >= currentViewSizeYMultipliedByAspectRatio) { + viewRatio = defaultViewRatio * currentViewSizeY / defalutViewSizeY; + } + else { + viewRatio = defaultViewRatio * currentViewSizeX / defaultViewSizeX; + } + return x * viewRatio; +} + - (id)init { self = [super init]; @@ -29,38 +49,10 @@ - (void)awakeFromNib { [view setLayer: backgroundLayer]; [view setWantsLayer: YES]; - [frameArrayController addObserver: self - forKeyPath: @"arrangedObjects.status" - options: NSKeyValueObservingOptionNew - context: nil]; -} - - - -- (void)observeValueForKeyPath: (NSString *)keyPath ofObject: (id)object change: (NSDictionary *)change context: (void *)context { - for (NSDictionary *frame in frameArrayController.content) { - NSString *idString = [frame objectForKey: @"id"]; - NSNumber *status = [frame objectForKey: @"status"]; - BOOL invisible = ![status boolValue]; - CALayer *layer = [layers objectForKey: idString]; - if (layer != nil) { - layer.hidden = invisible; - } - } } - (IBAction)go: (id)sender { - if (sender != goButton) { - return; - // Just in case. If you connect URL text field to the Go button, - // changing the check box will call this method. - } - - // Disable KVO temporary - [frameArrayController removeObserver: self - forKeyPath: @"arrangedObjects.status"]; - if (layers) { for (NSString *ident in layers) { CALayer *layer = [layers objectForKey: ident]; @@ -74,7 +66,7 @@ // Verify given URL. NSString *address = [addressField stringValue]; if ([address isEqualToString: @""]) { - address = @"http://localhost:8080/?time=all&event=none"; + address = @"http://localhost:8080/?time=now"; } NSURL *url = [NSURL URLWithString: address]; @@ -86,56 +78,70 @@ if (error) { NSLog(@"Error -> %@", [error localizedDescription]); } - + // Parse XML. NSXMLElement *rootElement = [xmlDocument rootElement]; - NSArray *imageElements = [rootElement elementsForName: @"image"]; + NSArray *frameElements = [rootElement elementsForName: @"frame"]; + NSXMLElement *frameElement = [frameElements objectAtIndex: 0]; + NSArray *imageElements = [frameElement elementsForName: @"image"]; for (NSXMLElement *imageElement in imageElements) { - NSString *text = [imageElement stringValue]; - NSLog(@"Loaded %@", text); + // NSString *text = [imageElement stringValue]; NSXMLNode *source = [imageElement attributeForName: @"source"]; // source must be a valid URL NSXMLNode *ident = [imageElement attributeForName: @"id"]; NSXMLNode *position_x = [imageElement attributeForName: @"position_x"]; NSXMLNode *position_y = [imageElement attributeForName: @"position_y"]; NSXMLNode *size_x = [imageElement attributeForName: @"size_x"]; NSXMLNode *size_y = [imageElement attributeForName: @"size_y"]; + NSXMLNode *alpha = [imageElement attributeForName: @"alpha"]; // Download the image NSURL *bitmapImageURL = [NSURL URLWithString: [source stringValue]]; - NSBitmapImageRep *bitmapImage = [NSBitmapImageRep imageRepWithContentsOfURL: bitmapImageURL]; - CGFloat positionX = [[position_x stringValue] floatValue]; - CGFloat positionY = [[position_y stringValue] floatValue]; - CGFloat sizeX = [[size_x stringValue] floatValue]; - CGFloat sizeY = [[size_y stringValue] floatValue]; + NSBitmapImageRep *bitmapImage + = [NSBitmapImageRep imageRepWithContentsOfURL: bitmapImageURL]; + CGFloat positionX = [self mmToPix: [[position_x stringValue] floatValue]]; + CGFloat positionY = [self mmToPix: [[position_y stringValue] floatValue]]; + CGFloat sizeX = [self mmToPix: [[size_x stringValue] floatValue]]; + CGFloat sizeY = [self mmToPix: [[size_y stringValue] floatValue]]; + CGFloat alphaValue = [[alpha stringValue] floatValue]; if (bitmapImage) { // Create a layer and set the image to the layer CGImageRef image = [bitmapImage CGImage]; CALayer *layerToAdd = [CALayer layer]; layerToAdd.contents = (id)image; layerToAdd.frame = CGRectMake(positionX, positionY, sizeX, sizeY); + layerToAdd.opacity = alphaValue; // must set up geometry and transformation and color etc. - - // Insert the layer to the view layerToAdd.hidden = NO; [backgroundLayer addSublayer: layerToAdd]; - // Register to the layer dictionary NSString *idString = [ident stringValue]; [layers setObject: layerToAdd forKey: idString]; + } + } +} + - // Register to frameArray - NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys: idString, @"id", [NSNumber numberWithBool: YES], @"status", nil]; - [frameArrayController addObject: dict]; +- (IBAction)autoRun:(id)sender { + if ([autoRunMode state] == NSOnState) { + if (timer) { + timer = nil; + } + timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 / 6.0 + target: self + selector: @selector(fire:) + userInfo: nil + repeats: YES]; + } + else { + if (timer) { + [timer invalidate]; + timer = nil; } } - - [frameArrayController addObserver: self - forKeyPath: @"arrangedObjects.status" - options: NSKeyValueObservingOptionNew - context: nil]; - } - +- (void)fire: (id)userInfo { + [self go: self]; +} @end diff --git a/Clash/en.lproj/ClashDocument.xib b/Clash/en.lproj/ClashDocument.xib index 6a0175b..9d3cc9a 100644 --- a/Clash/en.lproj/ClashDocument.xib +++ b/Clash/en.lproj/ClashDocument.xib @@ -11,20 +11,14 @@ 1934 - NSScroller - NSTableHeaderView - NSButton - NSArrayController + NSView + NSTextField + NSWindowTemplate NSTextFieldCell - NSScrollView - NSButtonCell - NSTableView NSCustomView + NSButtonCell + NSButton NSCustomObject - NSView - NSWindowTemplate - NSTextField - NSTableColumn com.apple.InterfaceBuilder.CocoaPlugin @@ -41,13 +35,14 @@ FirstResponder - 263 + 271 2 - {{133, 235}, {1426, 850}} + {{0, 1000}, {1040, 322}} 1886912512 Window NSWindow View + {1040, 322} 256 @@ -55,16 +50,18 @@ 274 - {{20, 20}, {1280, 800}} + {{20, 42}, {1000, 250}} - + + NSView 265 - {{1308, 826}, {98, 25}} + {{922, 298}, {98, 25}} + YES @@ -88,8 +85,9 @@ 266 - {{20, 828}, {1280, 22}} + {{20, 300}, {894, 22}} + YES @@ -97,14 +95,14 @@ 272630784 - http://localhost:8080/?time=all&event=none (default) + http://localhost:8080/?time=now (default) YES 6 System textBackgroundColor - + 3 MQA @@ -113,234 +111,55 @@ 6 System textColor - + 3 MAA - + 268 - - - - 2304 - - - - 256 - {96, 782} - - - YES - - - 256 - {96, 17} - - - - - - - -2147483392 - {{224, 0}, {16, 17}} - - - - - - 48.37109375 - 40 - 1000 - - 75628096 - 2048 - Frame - - LucidaGrande - 11 - 3100 - - - 3 - MC4zMzMzMzI5ODU2AA - - - 6 - System - headerTextColor - - - - - 337772096 - 2048 - Text Cell - - - - 6 - System - controlBackgroundColor - - 3 - MC42NjY2NjY2NjY3AA - - - - 6 - System - controlTextColor - - - - 3 - YES - YES - - - - 42 - 40 - 1000 - - 75628096 - 2048 - On - - - - - - 67239424 - 0 - - - - 1211912703 - 2 - - NSImage - NSSwitch - - - NSSwitch - - - - 200 - 25 - - 3 - YES - YES - - - - 3 - 2 - - - 6 - System - gridColor - - 3 - MC41AA - - - 17 - -700448768 - - - 4 - 15 - 0 - YES - 0 - - - {{1, 17}, {96, 782}} - - - - - 4 - - - - -2147483392 - {{224, 17}, {15, 102}} - - - - _doScroller: - 0.98081841432225059 - - - - -2147483392 - {{1, 784}, {96, 15}} - - 1 - - _doScroller: - 0.98969072164948457 + {{18, 18}, {52, 18}} + + + + YES + + 67239424 + 0 + Auto + + + 1211912703 + 2 + + NSImage + NSSwitch - - - 2304 - - - - {{1, 0}, {96, 17}} - - - - - 4 + + NSSwitch - - - {{1308, 20}, {98, 800}} - - - 562 - - - - - - QSAAAEEgAABBmAAAQZgAAA + + + 200 + 25 + - {1426, 850} + {1040, 322} + {{0, 0}, {2560, 1418}} + {1040, 344} {1e+13, 1e+13} ClashViewController - - - frame - status - - NSMutableArray - YES - - YES - YES - YES - YES - YES - NSApplication @@ -389,59 +208,35 @@ - frameTable + goButton - + - 100047 + 100066 - - frameArrayController + + go: - + - 100065 + 100068 - goButton + autoRunMode - + - 100066 + 100071 - - value: arrangedObjects.frame - - - - - - value: arrangedObjects.frame - value - arrangedObjects.frame - 2 - - - 100062 - - - - value: arrangedObjects.status - - - - - - value: arrangedObjects.status - value - arrangedObjects.status - 2 - + + autoRun: + + - 100064 + 100072 @@ -477,10 +272,10 @@ 6 - - - + + + @@ -527,71 +322,17 @@ - 100036 - + 100069 + - - - - + - 100037 - - - - - - - - - 100038 - - - - - 100039 - - - - - 100040 - - - - - 100041 - - - - - - - - 100042 - - - - - - - - 100044 - - - - - 100045 - - - - - 100051 - - - Frame Array Controller + 100070 + + @@ -605,16 +346,10 @@ com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin - com.apple.InterfaceBuilder.CocoaPlugin - com.apple.InterfaceBuilder.CocoaPlugin - com.apple.InterfaceBuilder.CocoaPlugin - com.apple.InterfaceBuilder.CocoaPlugin - com.apple.InterfaceBuilder.CocoaPlugin - com.apple.InterfaceBuilder.CocoaPlugin - com.apple.InterfaceBuilder.CocoaPlugin - com.apple.InterfaceBuilder.CocoaPlugin - com.apple.InterfaceBuilder.CocoaPlugin - com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + + com.apple.InterfaceBuilder.CocoaPlugin {{133, 170}, {507, 413}} com.apple.InterfaceBuilder.CocoaPlugin @@ -623,9 +358,112 @@ - 100067 + 100072 + + + + + ClashDocument + NSDocument + + IBProjectSource + ./Classes/ClashDocument.h + + + + ClashViewController + NSObject + + id + id + + + + autoRun: + id + + + go: + id + + + + NSTextField + NSButton + NSArrayController + NSButton + NSView + + + + addressField + NSTextField + + + autoRunMode + NSButton + + + frameArrayController + NSArrayController + + + goButton + NSButton + + + view + NSView + + + + IBProjectSource + ./Classes/ClashViewController.h + + + + NSDocument + + id + id + id + id + id + id + + + + printDocument: + id + + + revertDocumentToSaved: + id + + + runPageLayout: + id + + + saveDocument: + id + + + saveDocumentAs: + id + + + saveDocumentTo: + id + + + + IBProjectSource + ./Classes/NSDocument.h + + + - 0 IBCocoaFramework YES -- 2.11.0