-
Notifications
You must be signed in to change notification settings - Fork 13
/
TNFlipView.j
411 lines (328 loc) · 13.4 KB
/
TNFlipView.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
* TNFlipView.j
*
* Copyright (C) 2010 Antoine Mercadal <antoine.mercadal@inframonde.eu>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import <Foundation/Foundation.j>
@import <AppKit/CPView.j>
TNFlipViewAnimationStyleRotate = 1;
TNFlipViewAnimationStyleTranslate = 2;
TNFlipViewAnimationStyleTranslateHorizontal = 1;
TNFlipViewAnimationStyleTranslateVertical = 2;
var TNFlipView_didShowView_ = 1 << 1;
/*! @ingroup TNKit
this widget allow to set a back view and a front view and
is able to flip them just like widget in Mac OS X Dashboard
Note that is use webkit CSS transformation
*/
@implementation TNFlipView : CPView
{
BOOL _flipped @accessors(getter=isFlipped);
CPView _backView @accessors(property=backView);
CPView _frontView @accessors(property=frontView);
float _animationDuration @accessors(getter=animationDuration);
int _animationDirection @accessors(getter=animationDirection);
int _animationStyle @accessors(getter=animationStyle);
id _delegate @accessors(getter=delegate);
int _implementedDelegateMethods;
CPView _currentBackViewContent;
CPView _currentFrontViewContent;
}
#pragma mark -
#pragma mark Initialization
/*! initialize the TNFlipView
@param aRect the frame
*/
- (TNFlipView)initWithFrame:(CGRect)aRect
{
if (self = [super initWithFrame:aRect])
{
[self _init];
}
return self;
}
/*! @ignore
*/
- (void)_init
{
_implementedDelegateMethods = 0;
_animationDuration = 0.5;
_flipped = NO;
_animationDirection = TNFlipViewAnimationStyleTranslateHorizontal;
_backView = [[CPView alloc] initWithFrame:[self bounds]];
_frontView = [[CPView alloc] initWithFrame:[self bounds]];
_backView._DOMElement.style.backfaceVisibility = @"hidden";
_backView._DOMElement.style.WebkitBackfaceVisibility = @"hidden";
_backView._DOMElement.style.MozBackfaceVisibility = @"hidden";
_backView._DOMElement.style.transformStyle = @"preserve-3d";
_backView._DOMElement.style.WebkitTransformStyle = @"preserve-3d";
_backView._DOMElement.style.MozTransformStyle = @"preserve-3d";
_backView._DOMElement.style.transitionTimingFunction = @"ease";
_backView._DOMElement.style.WebkitTransitionTimingFunction = @"ease";
_backView._DOMElement.style.MozTransitionTimingFunction = @"ease";
_backView._DOMElement.style.perspective = 1000;
_backView._DOMElement.style.WebkitPerspective = 1000;
_backView._DOMElement.style.MozPerspective = 1000;
_frontView._DOMElement.style.backfaceVisibility = @"hidden";
_frontView._DOMElement.style.WebkitBackfaceVisibility = @"hidden";
_frontView._DOMElement.style.MozBackfaceVisibility = @"hidden";
_frontView._DOMElement.style.transformStyle = @"preserve-3d";
_frontView._DOMElement.style.WebkitTransformStyle = @"preserve-3d";
_frontView._DOMElement.style.MozTransformStyle = @"preserve-3d";
_frontView._DOMElement.style.transitionTimingFunction = @"ease";
_frontView._DOMElement.style.WebkitTransitionTimingFunction = @"ease";
_frontView._DOMElement.style.MozTransitionTimingFunction = @"ease";
_frontView._DOMElement.style.perspective = 1000;
_frontView._DOMElement.style.WebkitPerspective = 1000;
_frontView._DOMElement.style.MozPerspective = 1000;
[self setAnimationStyle:TNFlipViewAnimationStyleRotate direction:TNFlipViewAnimationStyleTranslateHorizontal];
[_backView setAutoresizingMask:CPViewHeightSizable | CPViewWidthSizable];
[_frontView setAutoresizingMask:CPViewHeightSizable | CPViewWidthSizable];
[self addSubview:_backView];
[self addSubview:_frontView];
}
#pragma mark -
#pragma mark Setters and getters
/*! set the animation style
style can be TNFlipViewAnimationStyleRotate or TNFlipViewAnimationStyleTranslateHorizontal.
If style is TNFlipViewAnimationStyleTranslate you can add a direction
(TNFlipViewAnimationStyleTranslateHorizontal or TNFlipViewAnimationStyleTranslateVertical)
@param anAnimationStyle the animation style
@param aDirection the animation direction
*/
- (void)setAnimationStyle:(int)anAnimationStyle direction:(int)aDirection
{
if ((anAnimationStyle == _animationStyle) && (aDirection == _animationDirection))
return;
_animationStyle = anAnimationStyle;
_animationDirection = aDirection || (_animationDirection ? _animationDirection : TNFlipViewAnimationStyleTranslateHorizontal);
if (_flipped)
[self _applyShowBackTransformation];
else
[self _applyShowFrontTransformatiom];
}
/*! set the durationof the flip animation
@param aDuration the duratiom
*/
- (void)setAnimationDuration:(float)aDuration
{
if (aDuration == _animationDuration)
return;
_animationDuration = aDuration;
var duration = _animationDuration + "s";
_frontView._DOMElement.style.transitionDuration = duration;
_frontView._DOMElement.style.WebkitTransitionDuration = duration;
_frontView._DOMElement.style.MozTransitionDuration = duration;
_frontView._DOMElement.style.OTransitionDuration = duration;
_backView._DOMElement.style.transitionDuration = duration;
_backView._DOMElement.style.WebkitTransitionDuration = duration;
_backView._DOMElement.style.MozTransitionDuration = duration;
_backView._DOMElement.style.OTransitionDuration = duration;
}
/*! set the content of the back view
@param aView the back view
*/
- (void)setBackView:(CPView)aView
{
if (_currentBackViewContent == aView)
return;
if (_currentBackViewContent)
[_currentBackViewContent removeFromSuperview];
_currentBackViewContent = aView;
if (!_currentBackViewContent)
return;
[_currentBackViewContent setFrame:[self bounds]];
[_currentBackViewContent setAutoresizingMask:CPViewHeightSizable | CPViewWidthSizable];
[_backView addSubview:aView];
_backView._DOMElement.style.transitionProperty = "translateX, translateY, rotateY";
_backView._DOMElement.style.transitionDuration = _animationDuration + "s";
}
/*! set the content of the front view
@param aView the front view
*/
- (void)setFrontView:(CPView)aView
{
if (_currentFrontViewContent == aView)
return;
if (_currentFrontViewContent)
[_currentFrontViewContent removeFromSuperview];
_currentFrontViewContent = aView;
if (!_currentFrontViewContent)
return;
[_currentFrontViewContent setFrame:[self bounds]];
[_currentFrontViewContent setAutoresizingMask:CPViewHeightSizable | CPViewWidthSizable];
[_frontView addSubview:aView];
_frontView._DOMElement.style.transitionProperty = "translateX, translateY, rotateY";
_frontView._DOMElement.style.transitionDuration = _animationDuration + "s";
}
/*! set the delegate. The delegate can implement
- (void)flipView:(TNFlipView)aFlipView didShowView:(CPView)aView;
@param aDelegate the delegate
*/
- (void)setDelegate:(id)aDelegate
{
if (aDelegate == _delegate)
return;
_delegate = aDelegate;
_implementedDelegateMethods = 0;
if ([_delegate respondsToSelector:@selector(flipView:didShowView:)])
_implementedDelegateMethods |= TNFlipView_didShowView_;
}
#pragma mark -
#pragma mark Utilities
/*! @ignore
*/
- (void)_delegateDidShowView
{
if (_implementedDelegateMethods & TNFlipView_didShowView_)
[_delegate flipView:self didShowView:_flipped ? _currentBackViewContent : _currentFrontViewContent];
}
/*! @ignore
*/
- (void)_listenNextTransformation
{
var frontFunction = function(e)
{
this.removeEventListener("transitionEnd", arguments.callee, NO);
this.removeEventListener("webkitAnimationEnd", arguments.callee, NO);
_flipped = !_flipped;
[[CPRunLoop currentRunLoop] limitDateForMode:CPDefaultRunLoopMode];
[self _delegateDidShowView];
};
_frontView._DOMElement.addEventListener("transitionEnd", frontFunction, NO);
_frontView._DOMElement.addEventListener("webkitAnimationEnd", frontFunction, NO);
var backFunction = function(e)
{
this.removeEventListener("transitionEnd", arguments.callee, NO);
this.removeEventListener("webkitAnimationEnd", arguments.callee, NO);
[[CPRunLoop currentRunLoop] limitDateForMode:CPDefaultRunLoopMode];
};
_backView._DOMElement.addEventListener("transitionEnd", backFunction, NO);
_backView._DOMElement.addEventListener("webkitAnimationEnd", backFunction, NO);
}
/*! @ignore
*/
- (void)_applyTransformation:(CPString)aTransformation toView:(CPView)aView
{
aView._DOMElement.style.transform = aTransformation;
aView._DOMElement.style.WebkitTransform = aTransformation;
aView._DOMElement.style.MozTransform = aTransformation;
}
/*! @ignore
*/
- (void)_applyShowFrontTransformatiom
{
switch (_animationStyle)
{
case TNFlipViewAnimationStyleTranslate:
var directionHorizontal = _animationDirection == TNFlipViewAnimationStyleTranslateHorizontal,
frameSize = [self frameSize],
CSSFunction = directionHorizontal ? "translateX" : "translateY",
offset = directionHorizontal ? frameSize.width : frameSize.height;
[self _applyTransformation:CSSFunction + "(0px)" toView:_frontView];
[self _applyTransformation:CSSFunction + "(" + offset + "px)" toView:_backView];
break;
case TNFlipViewAnimationStyleRotate:
[self _applyTransformation:"rotateY(0deg)" toView:_frontView];
[self _applyTransformation:"rotateY(180deg)" toView:_backView];
break;
}
}
/*! @ignore
*/
- (void)_applyShowBackTransformation
{
switch (_animationStyle)
{
case TNFlipViewAnimationStyleTranslate:
var directionHorizontal = _animationDirection == TNFlipViewAnimationStyleTranslateHorizontal,
frameSize = [self frameSize],
CSSFunction = directionHorizontal ? "translateX" : "translateY",
offset = directionHorizontal ? frameSize.width : frameSize.height;
[self _applyTransformation:CSSFunction + "(-" + offset + "px)" toView:_frontView];
[self _applyTransformation:CSSFunction + "(0px)" toView:_backView];
break;
case TNFlipViewAnimationStyleRotate:
[self _applyTransformation:"rotateY(180deg)" toView:_frontView];
[self _applyTransformation:"rotateY(0deg)" toView:_backView];
break;
}
}
/*! show the front view
*/
- (void)showFront
{
if (!_flipped)
return;
[self _listenNextTransformation];
[self _applyShowFrontTransformatiom];
}
/*! show the back view
*/
- (void)showBack
{
if (_flipped)
return;
[self _listenNextTransformation];
[self _applyShowBackTransformation];
}
#pragma mark -
#pragma mark Action
/*! flip or unflip the view
@param aSender the sender of the acion
*/
- (@action)flip:(id)aSender
{
if (_flipped)
[self showFront];
else
[self showBack];
}
@end
@implementation TNFlipView (CPCoding)
/*! CPCoder compliance
*/
- (id)initWithCoder:(CPCoder)aCoder
{
self = [super initWithCoder:aCoder];
if (self)
{
_flipped = [aCoder decodeObjectForKey:@"_flipped"];
_backView = [aCoder decodeObjectForKey:@"_backView"];
_frontView = [aCoder decodeObjectForKey:@"_frontView"];
_animationDuration = [aCoder decodeObjectForKey:@"_animationDuration"];
_currentBackViewContent = [aCoder decodeObjectForKey:@"_currentBackViewContent"];
_currentFrontViewContent = [aCoder decodeObjectForKey:@"_currentFrontViewContent"];
_animationDirection = [aCoder decodeIntForKey:@"_animationDirection"];
[self _init];
}
return self;
}
/*! CPCoder compliance
*/
- (void)encodeWithCoder:(CPCoder)aCoder
{
[super encodeWithCoder:aCoder];
[aCoder encodeObject:_flipped forKey:@"_flipped"];
[aCoder encodeObject:_backView forKey:@"_backView"];
[aCoder encodeObject:_frontView forKey:@"_frontView"];
[aCoder encodeObject:_animationDuration forKey:@"_animationDuration"];
[aCoder encodeObject:_currentBackViewContent forKey:@"_currentBackViewContent"];
[aCoder encodeObject:_currentFrontViewContent forKey:@"_currentFrontViewContent"];
[aCoder encodeInt:_animationDirection forKey:@"_animationDirection"];
}
@end