La classe Flip3DPannel

Aller sur www.electrofrog.com

Voici la classe Flip3DPanel pour Flex…Comme annoncé dans le billet précédent voici donc une petite classe pour Flex adaptée à partir du code de Lee Brimelow (à suivre la même pour Flash) qui permet de mapper deux DisplayObject sur les faces d’un panneau, et de faire pivoter ce panneau d’une face à l’autre dans le plan horizontal.

?View Code ACTIONSCRIPT3
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
//-----------------------------------------------------------------------------------
 
/*
	Copyleft (¢) 2009  Vincent Maitray - www.electrofrog.com
 
	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.
 
	Mention to the author would be appreciated !
*/
 
//-----------------------------------------------------------------------------------
 
package
{
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
 
	import gs.TweenLite;
	import gs.easing.Linear;
 
	import mx.core.UIComponent;
 
	/**
	 * The Flip3DPanel class is a simple 3D plane paradigm that can be used to map any DisplayObjectContainer on it.
	 * This class requires the free gs.TweenLite tweening library (http://blog.greensock.com/tweenliteas3/)
	 *
	 * @author	Vincent Maitray
	 * @version	1.0
	 *
	 */
	public class Flip3DPanel extends UIComponent
	{
 
		private var _frontFace:DisplayObjectContainer;
		private var _backFace:DisplayObjectContainer;
		private var _frontFaceHolder:Sprite;
		private var _backFaceHolder:Sprite;
		private var _world3d:Sprite;
		private var _transitionLength:Number;
		private var _transitionEase:Function;
		private var _firstSpin:Boolean;
 
		private var isSpinning:Boolean;
 
		/**
		 * Flip3DPanel constructor.
		 *
		 * @param pFrontFace 		 	The DisplayObjectContainer to be mapped on the front face of the panel.
		 * @param pBackFace 		 	The DisplayObjectContainer to be mapped on the back face of the panel.
		 * @param pTransitionLength						Length of the transition flip, in seconds.
		 * @param pTransitionEase						The transition ease function used to interpolate the plane.
		 * @return nothing.
		 *
		 */
		public function Flip3DPanel( pFrontFace:DisplayObjectContainer, pBackFace:DisplayObjectContainer, pTransitionLength:Number = 2 , pTransitionEase:Function = null )
		{
 
			_frontFaceHolder = new Sprite();
			_backFaceHolder = new Sprite();
			_world3d = new Sprite();
			addChild( _world3d );
 
			backFace = pBackFace;
			frontFace = pFrontFace;
			isSpinning = false;
 
			_transitionLength = pTransitionLength;
			pTransitionEase == null ? _transitionEase = Linear.easeNone : _transitionEase = pTransitionEase;
 
			_firstSpin = true;
 
			addEventListener( Event.ENTER_FRAME, refreshDisplay );
 
		}
 
		/**
		 *
		 * @param pFrontFace 		 	The DisplayObjectContainer to be mapped on the front face of the panel.
		 * You can change the front face content at runtime with this property.
		 * @return nothing.
		 *
		 */
		public function set frontFace( pFrontface:DisplayObjectContainer ):void
		{
 
			_frontFace = pFrontface;
			_frontFace.x = -_frontFace.width / 2;
			_frontFace.y = -_frontFace.height / 2;
			_frontFaceHolder.addChild( _frontFace );
 
			_world3d.x = _frontFace.width / 2;
			_world3d.y = _frontFace.height / 2;
			_world3d.addChild( _frontFaceHolder );
 
		}
 
		/**
		 * The front face of the panel.
		 * @return  the front face of the panel.
		 *
		 */
		public function get frontFace():DisplayObjectContainer
		{
 
			return _frontFace;
 
		}
 
		/**
		 *
		 * @param pBackFace 		 	The DisplayObjectContainer to be mapped on the back face of the panel.
		 * You can change the back face content at runtime with this property.
		 * @return nothing.
		 *
		 */
		public function set backFace( pBackFace:DisplayObjectContainer ):void
		{
 
			_backFace = pBackFace;
			_backFace.x = -_backFace.width / 2;
			_backFace.y = -_backFace.height / 2;
			_backFaceHolder.addChild( _backFace );
 
			_backFace.rotationY = -180;
			_backFaceHolder.x = _backFace.width;
 
			_world3d.x = _backFace.width / 2;
			_world3d.y = _backFace.height / 2;
			_world3d.addChild( _backFaceHolder );			
 
		}
 
		/**
		 * The back face of the panel.
		 * @return  the back face of the panel.
		 *
		 */
		public function get backFace():DisplayObjectContainer
		{
 
			return _backFace;
 
		}
 
		/**
		 * Spins the panel. Some minor width & x properties adjustments are applied after the first spin.
		 * For obscure reason, the Flash player doesn't render the objects excactly as they should when spining content by 180° in 3D.
		 * @return nothing.
		 *
		 */
		public function flip():void
		{
 
			if( !isSpinning )
			{
 
				TweenLite.to( _world3d, _transitionLength, { rotationY:_world3d.rotationY + 180, ease:_transitionEase, onComplete:onSpinComplete } );
				isSpinning = true;
 
				if( _firstSpin )
				{
 
					_firstSpin = false;
					_world3d.width -= 1;
					_world3d.x -= 0.5;
 
				}
 
			}
 
		}
 
		/**
		 * @private
		 * Fired when the spin is complete
		 */
		private function onSpinComplete():void
		{
 
			isSpinning = false;
 
		}
 
		/**
		 * @private
		 * Main refresh function fired by ENTER_FRAME event.
		 */
		private function refreshDisplay( e:Event ):void
		{
 
			if( _world3d.rotationY > 90 && _world3d.rotationY < 270 )
			{
				_frontFaceHolder.visible = false;
				_backFaceHolder.visible = true;
			}
			else
			{
				_frontFaceHolder.visible = true;
				_backFaceHolder.visible = false;
			}
 
			if( _world3d.rotationY >= 360 ) _world3d.rotationY = 0;
 
		}
 
		/**
		 * The width of the Flip3DPanel.
		 * @return  The width of the Flip3DPanel instance in pixels.
		 */
		public override function get width():Number
		{
 
			return _world3d.width;
 
		}
 
		/**
		 * The height of the Flip3DPanel.
		 * @return  The height of the Flip3DPanel instance in pixels.
		 */
		public override function get height():Number
		{
 
			return _world3d.height;
 
		}
 
	}
 
}
voir_code_source

telecharger_code_source

Une réponse Souscrire aux commentaires


  1. Tony

    Salut !

    Sympathique ce Flip3DPanel !
    J’ai remarqué un petit problème avec le SyntaxHighlighter de WordPress :

     » if( _world3d.rotationY > 90 && _world3d.rotationY < 270 )  »

    @+

    07 mar 2009 @ 0:25

Répondre