OSDN Git Service

works far better
[polyphonic/master.git] / Clash / ClashViewController.m
1 //
2 //  ClashViewController.m
3 //  Clash
4 //
5 //  Created by 金谷 一朗 on 11/09/06.
6 //  Copyright (c) 2011 大阪大学. All rights reserved.
7 //
8
9 #import "ClashViewController.h"
10
11 @implementation ClashViewController
12
13 #define defaultViewRatio 0.1
14 #define defaultViewSizeX 1000.0
15 #define defalutViewSizeY 250.0
16 #define viewAspectRatio (defaultViewSizeX / defalutViewSizeY)
17
18
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;
24         CGFloat viewRatio;
25         if (currentViewSizeX >= currentViewSizeYMultipliedByAspectRatio) {
26                 viewRatio = defaultViewRatio * currentViewSizeY / defalutViewSizeY;
27         }
28         else {
29                 viewRatio = defaultViewRatio * currentViewSizeX / defaultViewSizeX;
30         }
31         return x * viewRatio;
32 }
33
34
35 - (id)init {
36         self = [super init];
37         if (self) {
38                 // Initialization code here.
39                 backgroundLayer = [[CALayer alloc] init];
40                 CGColorRef blackColor = CGColorCreateGenericGray(0, 1);
41                 backgroundLayer.backgroundColor = blackColor;
42                 CGColorRelease(blackColor);
43                 layers = nil;
44         }
45         return self;
46 }
47
48
49 - (void)awakeFromNib {  
50         [view setLayer: backgroundLayer];
51         [view setWantsLayer: YES];
52 }
53
54
55 - (IBAction)go: (id)sender {
56         if (layers) {
57                 for (NSString *ident in layers) {
58                         CALayer *layer = [layers objectForKey: ident];
59                         [layer removeFromSuperlayer];
60                         layer = nil;
61                 }
62                 layers = nil;
63         }
64         layers = [NSMutableDictionary dictionaryWithCapacity: 100];
65         
66         // Verify given URL.
67         NSString *address = [addressField stringValue];
68         if ([address isEqualToString: @""]) {
69                 address = @"http://localhost:8080/?time=now";
70         }
71         NSURL *url = [NSURL URLWithString: address];
72         
73         // Download XML.
74         NSError *error;
75         NSXMLDocument *xmlDocument = [[NSXMLDocument alloc] initWithContentsOfURL: url
76                                                                                                                                                                                                                                                                                 options: 0
77                                                                                                                                                                                                                                                                                         error: &error];
78         if (error) {
79                 NSLog(@"Error -> %@", [error localizedDescription]);
80         }
81                 
82         // Parse XML.
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"];
96                 // Download the image
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];
105                 if (bitmapImage) {
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];
119                 }               
120         }
121 }
122
123
124 - (IBAction)autoRun:(id)sender {
125         if ([autoRunMode state] == NSOnState) {
126                 if (timer) {
127                         timer = nil;
128                 }
129                 timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 / 6.0
130                                                                                                                                                                                  target: self
131                                                                                                                                                                          selector: @selector(fire:)
132                                                                                                                                                                          userInfo: nil 
133                                                                                                                                                                                 repeats: YES];
134         }
135         else {
136                 if (timer) {
137                         [timer invalidate];
138                         timer = nil;
139                 }
140         }
141 }
142
143 - (void)fire: (id)userInfo {
144         [self go: self];
145 }
146
147 @end