2 // ClashViewController.m
5 // Created by 金谷 一朗 on 11/09/06.
6 // Copyright (c) 2011 大阪大学. All rights reserved.
9 #import "ClashViewController.h"
11 @implementation ClashViewController
13 #define defaultViewRatio 0.1
14 #define defaultViewSizeX 1000.0
15 #define defalutViewSizeY 250.0
16 #define viewAspectRatio (defaultViewSizeX / defalutViewSizeY)
19 - (CGFloat) mmToPix: (CGFloat)x {
20 NSRect currentViewFrame = [view frame];
21 CGFloat currentViewSizeX = currentViewFrame.size.width;
22 CGFloat currentViewSizeY = currentViewFrame.size.height;
23 CGFloat currentViewSizeYMultipliedByAspectRatio = currentViewSizeY * viewAspectRatio;
25 if (currentViewSizeX >= currentViewSizeYMultipliedByAspectRatio) {
26 viewRatio = defaultViewRatio * currentViewSizeY / defalutViewSizeY;
29 viewRatio = defaultViewRatio * currentViewSizeX / defaultViewSizeX;
38 // Initialization code here.
39 backgroundLayer = [[CALayer alloc] init];
40 CGColorRef blackColor = CGColorCreateGenericGray(0, 1);
41 backgroundLayer.backgroundColor = blackColor;
42 CGColorRelease(blackColor);
49 - (void)awakeFromNib {
50 [view setLayer: backgroundLayer];
51 [view setWantsLayer: YES];
55 - (IBAction)go: (id)sender {
57 for (NSString *ident in layers) {
58 CALayer *layer = [layers objectForKey: ident];
59 [layer removeFromSuperlayer];
64 layers = [NSMutableDictionary dictionaryWithCapacity: 100];
67 NSString *address = [addressField stringValue];
68 if ([address isEqualToString: @""]) {
69 address = @"http://localhost:8080/?time=now";
71 NSURL *url = [NSURL URLWithString: address];
75 NSXMLDocument *xmlDocument = [[NSXMLDocument alloc] initWithContentsOfURL: url
79 NSLog(@"Error -> %@", [error localizedDescription]);
83 NSXMLElement *rootElement = [xmlDocument rootElement];
84 NSArray *frameElements = [rootElement elementsForName: @"frame"];
85 NSXMLElement *frameElement = [frameElements objectAtIndex: 0];
86 NSArray *imageElements = [frameElement elementsForName: @"image"];
87 for (NSXMLElement *imageElement in imageElements) {
88 // NSString *text = [imageElement stringValue];
89 NSXMLNode *source = [imageElement attributeForName: @"source"]; // source must be a valid URL
90 NSXMLNode *ident = [imageElement attributeForName: @"id"];
91 NSXMLNode *position_x = [imageElement attributeForName: @"position_x"];
92 NSXMLNode *position_y = [imageElement attributeForName: @"position_y"];
93 NSXMLNode *size_x = [imageElement attributeForName: @"size_x"];
94 NSXMLNode *size_y = [imageElement attributeForName: @"size_y"];
95 NSXMLNode *alpha = [imageElement attributeForName: @"alpha"];
97 NSURL *bitmapImageURL = [NSURL URLWithString: [source stringValue]];
98 NSBitmapImageRep *bitmapImage
99 = [NSBitmapImageRep imageRepWithContentsOfURL: bitmapImageURL];
100 CGFloat positionX = [self mmToPix: [[position_x stringValue] floatValue]];
101 CGFloat positionY = [self mmToPix: [[position_y stringValue] floatValue]];
102 CGFloat sizeX = [self mmToPix: [[size_x stringValue] floatValue]];
103 CGFloat sizeY = [self mmToPix: [[size_y stringValue] floatValue]];
104 CGFloat alphaValue = [[alpha stringValue] floatValue];
106 // Create a layer and set the image to the layer
107 CGImageRef image = [bitmapImage CGImage];
108 CALayer *layerToAdd = [CALayer layer];
109 layerToAdd.contents = (id)image;
110 layerToAdd.frame = CGRectMake(positionX, positionY, sizeX, sizeY);
111 layerToAdd.opacity = alphaValue;
112 // must set up geometry and transformation and color etc.
113 // Insert the layer to the view
114 layerToAdd.hidden = NO;
115 [backgroundLayer addSublayer: layerToAdd];
116 // Register to the layer dictionary
117 NSString *idString = [ident stringValue];
118 [layers setObject: layerToAdd forKey: idString];
124 - (IBAction)autoRun:(id)sender {
125 if ([autoRunMode state] == NSOnState) {
129 timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 / 6.0
131 selector: @selector(fire:)
143 - (void)fire: (id)userInfo {