RSS

(root)/iphone/common : 61 : Jigs/source/JigsawPiece.m

« back to all changes in this revision

Viewing changes to Jigs/source/JigsawPiece.m

Dömötör Gulyás
2010-01-18 09:03:51
Revision ID: dognotdog@gmail.com-20100118080351-ib2knxvk4w8ssw3h
made common a nested tree

Show diffs side-by-side

added added

removed removed

1
 
//
2
 
//  JigsawPiece.m
3
 
//  Jigs
4
 
//
5
 
//  Created by döme on 06.08.2009.
6
 
//  Copyright 2009 __MyCompanyName__. All rights reserved.
7
 
//
8
 
 
9
 
#import "JigsawPiece.h"
10
 
#import "Triangulation.h"
11
 
 
12
 
 
13
 
@implementation JigsawPiece
14
 
 
15
 
- (id) init
16
 
{
17
 
        if (!(self = [super init]))
18
 
                return nil;
19
 
 
20
 
        neighbours = [[NSMutableDictionary alloc] init];
21
 
        fillDrawables = [[NSMutableArray alloc] init];
22
 
        outlineDrawables = [[NSMutableArray alloc] init];
23
 
        outlines = [[NSMutableArray alloc] init];
24
 
        
25
 
        ghostZoom = 1.0f;
26
 
 
27
 
//      body = cpBodyNew(1, 1);
28
 
 
29
 
        return self;
30
 
}
31
 
 
32
 
- (void) dealloc
33
 
{
34
 
        [fillDrawables release];
35
 
        [outlineDrawables release];
36
 
        [outlines release];
37
 
//      cpBodyDestroy(body);
38
 
 
39
 
        [neighbours release];
40
 
 
41
 
        [super dealloc];
42
 
}
43
 
 
44
 
- (void) setOutlineColor: (v3*) cptr
45
 
{
46
 
        outlineColor = *cptr;
47
 
}
48
 
 
49
 
- (void) rotateByAngle: (float) alpha aroundPoint: (CGPoint) p
50
 
{
51
 
        CGPoint r = CGPointSub(position, p);
52
 
        
53
 
        CGAffineTransform R = CGAffineTransformMakeRotation(alpha);
54
 
        
55
 
        orientation += alpha;
56
 
        position = CGPointAdd(p, CGPointTransform(r, R));
57
 
 
58
 
}
59
 
 
60
 
NSString*       FillDrawablesKey = @"fillDrawables";
61
 
 
62
 
- (id) initWithCoder: (NSCoder*) coder
63
 
{
64
 
        if (!(self = [super init]))
65
 
                return nil;
66
 
 
67
 
        NSArray* array = [coder decodeObjectForKey: @"outlines"];
68
 
        outlines = [[NSMutableArray alloc] init];
69
 
        for (id data in array)
70
 
                [outlines addObject: [(id)CreateBezierPathFromData(data) autorelease]];
71
 
 
72
 
        orientation = [coder decodeFloatForKey: @"orientation"];
73
 
        initialPos.x = [coder decodeFloatForKey: @"initialPos.x"];
74
 
        initialPos.y = [coder decodeFloatForKey: @"initialPos.y"];
75
 
        position.x = [coder decodeFloatForKey: @"position.x"];
76
 
        position.y = [coder decodeFloatForKey: @"position.y"];
77
 
 
78
 
        NSArray* nkeys = [coder decodeObjectForKey: @"neighbourKeys"];
79
 
        NSArray* nvals = [coder decodeObjectForKey: @"neighbourValues"];
80
 
        
81
 
        neighbours = [[NSMutableDictionary alloc] init];
82
 
        for (size_t i = 0; i < [nkeys count]; ++i)
83
 
        {
84
 
                [neighbours setObject: [nvals objectAtIndex: i] forKey: [NSValue valueWithPointer: [nkeys objectAtIndex: i]]];
85
 
        }
86
 
        
87
 
        fillDrawables = [[coder decodeObjectForKey: FillDrawablesKey] mutableCopy];
88
 
        outlineDrawables = [[coder decodeObjectForKey: @"outlineDrawables"] mutableCopy];
89
 
 
90
 
        ghostZoom = 1.0f;
91
 
 
92
 
        return self;
93
 
}
94
 
 
95
 
- (void) encodeWithCoder: (NSCoder*) coder
96
 
{
97
 
        NSMutableArray* array = [NSMutableArray array];
98
 
        for (id outline in outlines)
99
 
        {
100
 
                [array addObject: BezierPathToData((CGPathRef)outline)];
101
 
        }
102
 
        [coder encodeObject: array forKey: @"outlines"];
103
 
                
104
 
        [coder encodeFloat: initialPos.x forKey: @"initialPos.x"];
105
 
        [coder encodeFloat: initialPos.y forKey: @"initialPos.y"];
106
 
        [coder encodeFloat: position.x forKey: @"position.x"];
107
 
        [coder encodeFloat: position.y forKey: @"position.y"];
108
 
        [coder encodeFloat: orientation  forKey: @"orientation"];
109
 
        
110
 
        NSMutableArray* keys = [NSMutableArray array];
111
 
        NSMutableArray* values = [NSMutableArray array];
112
 
        
113
 
        for (id key in neighbours)
114
 
        {
115
 
                [keys addObject: (id)[key pointerValue]];
116
 
                [values addObject: [neighbours objectForKey: key]];
117
 
        }
118
 
        
119
 
        [coder encodeObject: keys forKey: @"neighbourKeys"];
120
 
        [coder encodeObject: values forKey: @"neighbourValues"];
121
 
        [coder encodeObject: fillDrawables forKey: FillDrawablesKey];
122
 
        [coder encodeObject: outlineDrawables forKey: @"outlineDrawables"];
123
 
}
124
 
 
125
 
 
126
 
@synthesize fillDrawables, outlineDrawables, outlines, initialPos, position, orientation, neighbours, outlineColor, ghostZoom;
127
 
 
128
 
@end
129
 
 
130
 
@implementation JigsawNeighbour
131
 
 
132
 
- (id) initWithCoder: (NSCoder*) coder
133
 
{
134
 
        if (!(self = [super init]))
135
 
                return nil;
136
 
 
137
 
        attachmentPoint.x = [coder decodeFloatForKey: @"attachmentPoint.x"];
138
 
        attachmentPoint.y = [coder decodeFloatForKey: @"attachmentPoint.y"];
139
 
        direction = [coder decodeIntegerForKey: @"direction"];
140
 
 
141
 
        return self;
142
 
}
143
 
 
144
 
- (void) encodeWithCoder: (NSCoder*) coder
145
 
{
146
 
        [coder encodeFloat: attachmentPoint.x forKey: @"attachmentPoint.x"];
147
 
        [coder encodeFloat: attachmentPoint.y forKey: @"attachmentPoint.y"];
148
 
        [coder encodeInteger: direction forKey: @"direction"];
149
 
}
150
 
 
151
 
 
152
 
- (void) dealloc
153
 
{
154
 
 
155
 
        [super dealloc];
156
 
}
157
 
 
158
 
@synthesize attachmentPoint, direction;
159
 
@end

Loggerhead 1.17 is a web-based interface for Bazaar branches