-
Notifications
You must be signed in to change notification settings - Fork 13
/
CCUIViewWrapper.m
149 lines (124 loc) · 3.8 KB
/
CCUIViewWrapper.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* CCUIViewWrapper
*
* http://www.cocos2d-iphone.org/forum/topic/6889
*
* Copyright (C) 2010 Blue Ether
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the 'cocos2d for iPhone' license.
*
* You will find a copy of this license within the cocos2d for iPhone
* distribution inside the "LICENSE" file.
*
*/
#import "CCUIViewWrapper.h"
@implementation CCUIViewWrapper
@synthesize uiItem;
/* -------------------------------------------------------------------------- */
+ (id)wrapperForUIView:(UIView*)ui
{
return [[[self alloc] initForUIView:ui] autorelease];
}
/* -------------------------------------------------------------------------- */
- (id)initForUIView:(UIView*)ui
{
self = [self init];
if (!self)
return nil;
self.uiItem = ui;
return self;
}
/* -------------------------------------------------------------------------- */
- (void)dealloc
{
self.uiItem = nil;
[super dealloc];
}
/* -------------------------------------------------------------------------- */
- (void)setParent:(CCNode *)parent
{
if (parent == nil)
[uiItem removeFromSuperview];
else if(uiItem != nil)
[[[CCDirector sharedDirector] openGLView] addSubview:uiItem];
[super setParent:parent];
}
/* -------------------------------------------------------------------------- */
- (void)updateUIViewTransform
{
float thisAngle, pAngle;
CGAffineTransform transform = CGAffineTransformMakeTranslation(0,
[[UIScreen mainScreen] bounds].size.height);
for (CCNode *p = self; p != nil; p = p.parent) {
thisAngle = CC_DEGREES_TO_RADIANS(p.rotation);
if (!p.isRelativeAnchorPoint)
transform = CGAffineTransformTranslate(
transform, p.anchorPointInPixels.x, p.anchorPointInPixels.y);
if (p.parent != nil) {
pAngle = CC_DEGREES_TO_RADIANS(p.parent.rotation);
transform = CGAffineTransformTranslate(transform,
(p.position.x * cosf(pAngle)) + (p.position.y * sinf(pAngle)),
(-p.position.y * cosf(pAngle)) + (p.position.x * sinf(pAngle)));
} else {
transform = CGAffineTransformTranslate(
transform, p.position.x, -p.position.y);
}
transform = CGAffineTransformRotate(transform, thisAngle);
transform = CGAffineTransformScale(transform, p.scaleX, p.scaleY);
transform = CGAffineTransformTranslate(
transform, -p.anchorPointInPixels.x, -p.anchorPointInPixels.y);
}
[uiItem setTransform:transform];
}
/* -------------------------------------------------------------------------- */
- (void)setVisible:(BOOL)v
{
[super setVisible:v];
[uiItem setHidden:!v];
}
/* -------------------------------------------------------------------------- */
- (void)setRotation:(float)protation
{
[super setRotation:protation];
[self updateUIViewTransform];
}
/* -------------------------------------------------------------------------- */
- (void)setScaleX:(float)sx
{
[super setScaleX:sx];
[self updateUIViewTransform];
}
/* -------------------------------------------------------------------------- */
- (void)setScaleY:(float)sy
{
[super setScaleY:sy];
[self updateUIViewTransform];
}
/* -------------------------------------------------------------------------- */
- (void)setOpacity:(GLubyte)opacity
{
[uiItem setAlpha:opacity / 255.0f];
[super setOpacity:opacity];
}
/* -------------------------------------------------------------------------- */
- (void)setContentSize:(CGSize)size
{
[super setContentSize:size];
CGRect rect = CGRectMake(0, 0, size.width, size.height);
uiItem.frame = rect;
uiItem.bounds = rect;
}
/* -------------------------------------------------------------------------- */
- (void)setAnchorPoint:(CGPoint)pnt
{
[super setAnchorPoint:pnt];
[self updateUIViewTransform];
}
/* -------------------------------------------------------------------------- */
- (void)setPosition:(CGPoint)pnt
{
[super setPosition:pnt];
[self updateUIViewTransform];
}
@end