-
Notifications
You must be signed in to change notification settings - Fork 3
/
LPCardFlipController.j
280 lines (222 loc) · 9.69 KB
/
LPCardFlipController.j
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* LPCardFlipController.j
*
* Created by Ludwig Pettersson on June 29, 2010.
*
* The MIT License
*
* Copyright (c) 2010 Ludwig Pettersson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
@import <Foundation/CPObject.j>
@import "LPViewAnimation.j"
var sharedCardFlipController = nil,
ShouldUseFlipAnimation = NO;
@implementation LPCardFlipController : CPObject
{
id _delegate @accessors(property=delegate);
CPWindow containerWindow;
CPWindow mainWindow;
float animationDuration;
CGPoint startCenter;
CGPoint endCenter;
id frontView @accessors;
id backView @accessors;
}
+ (id)sharedController
{
if (!sharedCardFlipController)
sharedCardFlipController = [[self alloc] init];
return sharedCardFlipController;
}
- (void)init
{
if (self = [super init])
{
ShouldUseFlipAnimation = LPTestCSSFeature("BackfaceVisibility");
containerWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessWindowMask];
//containerWindow._DOMElement.style["overflow"] = "visible";
var contentView = [containerWindow contentView];
//contentView._DOMElement.style["overflow"] = "visible";
//[contentView setClipsToBounds:NO];
var DOMElement = contentView._DOMElement;
DOMElement.style["-webkit-transform-style"] = @"preserve-3d";
DOMElement.style["-webkit-perspective"] = @"600";
animationDuration = 0.65;
}
return self;
}
- (void)setStartCenter:(CGPoint)aStartCenter endCenter:(CGPoint)anEndCenter
{
startCenter = aStartCenter;
endCenter = anEndCenter;
}
- (void)reverseFlipCurrentView
{
[self flipWithView:frontView backView:backView reverse:YES];
}
- (void)flipWithView:(id)aFrontView backView:(id)aBackView
{
[self flipWithView:aFrontView backView:aBackView reverse:NO];
}
- (void)flipWithView:(id)aFrontView backView:(id)aBackView reverse:(BOOL)isReverse
{
mainWindow = [aFrontView window];
var superView = [aFrontView superview];
if (ShouldUseFlipAnimation)
{
if (aBackView !== backView)
aBackView._DOMElement.style["-webkit-transform"] = @"rotateY(180deg)";
frontView = aFrontView;
backView = aBackView;
// Show the window
[containerWindow makeKeyAndOrderFront:nil];
var contentView = [containerWindow contentView];
// Prepeare each view
// no need to do this if we are just reversing it, since it has then already been done.
//if (!isReverse)
//{
// The window should be as big as the largest view
// plus some extra so we dont get the clipping
var containerWindowSize = [aBackView frame].size;
containerWindowSize.height += 200;
[containerWindow setFrameSize:containerWindowSize];
[self prepareViewWithCSS:frontView];
[frontView setCenter:[contentView center]];
[self prepareViewWithCSS:backView];
[backView setCenter:[contentView center]];
[contentView setSubviews:[frontView, backView]];
//}
var bounds = [[containerWindow contentView] bounds];
// Get the origins
var startOrigin = CGPointMakeCopy(isReverse ? endCenter : startCenter);
startOrigin.x -= CGRectGetMidX(bounds);
startOrigin.y -= CGRectGetMidY(bounds);
var endOrigin = CGPointMakeCopy(isReverse ? startCenter : endCenter);
endOrigin.x -= CGRectGetMidX(bounds);
endOrigin.y -= CGRectGetMidY(bounds);
[containerWindow setFrameOrigin:startOrigin];
// The moving animation
var animation = [[LPViewAnimation alloc] initWithViewAnimations:[
{
@"target": containerWindow,
@"animations": [
[LPOriginAnimationKey, [containerWindow frame].origin, endOrigin]
]
}
]];
[animation setShouldUseCSSAnimations:YES];
[animation setAnimationCurve:CPAnimationEaseInOut];
[animation setDuration:animationDuration];
var frontWidth = CGRectGetWidth([frontView bounds]),
backWidth = CGRectGetWidth([backView bounds]);
// Set an initial scale for the backview
backView._DOMElement.style["-webkit-transform"] += @" scale(" + (frontWidth / backWidth) + ")";
// Hide backView,
// because chrome does not support backface-visibility
backView._DOMElement.style["opacity"] = isReverse ? 1 : 0;
// Start the flip animation
// it's wrapped in a timeout so that the browser does not
// collapse all the style calls and doesnt run any of them.
setTimeout(function() {
// Start the moving
[animation startAnimation];
// Flip each view
var rotate = "rotateY(" + (isReverse ? 0 : -180) + "deg) ",
scale = "scale(" + (isReverse ? 1.0 : backWidth / frontWidth) + ")";
frontView._DOMElement.style["-webkit-transform"] = rotate + scale;
var rotate = "rotateY(" + (isReverse ? 180 : 0) + "deg) ",
scale = "scale(" + (isReverse ? frontWidth / backWidth : 1.0) + ")";
backView._DOMElement.style["-webkit-transform"] = rotate + scale;
// Show & Hide elements manually, because chrome does not support backface-visibility
setTimeout(function(){
frontView._DOMElement.style["opacity"] = isReverse ? 1 : 0;
backView._DOMElement.style["opacity"] = isReverse ? 0 : 1;
}, (1000 * animationDuration) / 2);
// Send delegate call when we have finished a reverse animation
setTimeout(function() {
if (_delegate && [_delegate respondsToSelector:@selector(cardFlipDidFinish:)])
[_delegate cardFlipDidFinish:self];
[self removeTransformFromView:backView];
[containerWindow orderOut:nil];
/*if (isReverse)
{
}
else
{
//console.log([superView window]);
//var superView = [aFrontView superview];
[aBackView removeFromSuperview];
//[superView replaceSubview:aFrontView with:aBackView];
//[aBackView setFrameOrigin:CGPointMakeZero()];
[superView addSubview:aBackView];
console.log(superView);
[containerWindow orderOut:nil];
}*/
}, 1000 * animationDuration);
}, 0);
}
// Boring, non animation version for non webkit browsers
else
{
if (isReverse)
{
[containerWindow orderOut:nil];
if (_delegate && [_delegate respondsToSelector:@selector(cardFlipDidFinish:)])
[_delegate cardFlipDidFinish:self];
}
else
{
backView = aBackView;
[backView setFrameOrigin:CGPointMakeZero()];
// Just show the window
var containerWindowSize = [backView frame].size;
[containerWindow setFrameSize:containerWindowSize];
// Replace the content
[[containerWindow contentView] setSubviews:[backView]];
// Get the end origin
var endOrigin = CGPointMakeCopy(endCenter);
endOrigin.x -= CGRectGetMidX([[containerWindow contentView] bounds]);
endOrigin.y -= CGRectGetMidY([[containerWindow contentView] bounds]);
[containerWindow setFrameOrigin:endOrigin];
[containerWindow orderFront:nil];
}
}
}
- (void)prepareViewWithCSS:(id)aView
{
var DOMElement = aView._DOMElement;
DOMElement.style["-webkit-transition-timing-function"] = @"ease-in-out";
DOMElement.style["-webkit-transition-property"] = @"-webkit-transform";
DOMElement.style["-webkit-transition-duration"] = animationDuration + @"s";
DOMElement.style["-webkit-backface-visibility"] = @"hidden";
//[aView setAlphaValue:0.5];
}
- (void)removeTransformFromView:(id)aView
{
var DOMElement = aView._DOMElement;
DOMElement.style["-webkit-transition-timing-function"] = "";//@"ease-in-out";
DOMElement.style["-webkit-transition-property"] = "";//@"-webkit-transform";
DOMElement.style["-webkit-transition-duration"] = "";//animationDuration + @"s";
DOMElement.style["-webkit-backface-visibility"] = "";//@"hidden";
DOMElement.style["-webkit-transform"] = "";
}
@end