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.
This movie requires Flash Player 10.0.12
//----------------------------------------------------------------------------------- /* 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; } } }




