/*----------------------------------------------------------------------------- The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License.You may obtain a copy of the License at http://www.mozilla.org/MPL/MPL-1.1.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. The Original Code is Fever Framework code. The Initial Developer of the Original Code is Romain Ecarnot. Portions created by Initial Developer are Copyright (C) 2006 the Initial Developer. All Rights Reserved. Contributor(s): Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -----------------------------------------------------------------------------*/ import com.bourre.commands.CommandManagerFPS; import com.bourre.commands.Delegate; import com.bourre.core.HashCodeFactory; import com.bourre.data.collections.Map; import com.bourre.events.EventBroadcaster; import com.bourre.events.EventType; import com.bourre.events.IEvent; import com.bourre.structures.Rectangle; import fever.core.CoreApplication; import fever.core.FeverRoot; import fever.display.background.Background; import fever.display.background.SolidBackGround; import fever.display.style.StageAlign; import fever.display.style.StageScaleMode; import fever.events.EventPriority; import fever.events.StageEvent; import fever.Fever; /** * Core stage for UI creation. * *
Use it to create UI component ( MovieClip or AsWing components for example ) * *
Support new player 9 feature 'Fullscreen mode'. * *
{@link FeverStage} allow better levels managment defining pre-level like : *
Always return the same instance ( Singleton desing ). */ public static function getInstance() : FeverStage { if( !_instance ) _instance = new FeverStage( ); return _instance; } /** * Adds passed-in {@code listener} for receiving passed-in {@code type} event type. * * @param type Name of the Event. * @param listener Listener object. */ public function addEventListener( type : EventType, listener ) : Void { _oEB.addEventListener.apply( _oEB, arguments ); } /** * Removes passed-in {@code listener} that subscribed to passed-in {@code type} event type. * * @param type Name of the Event. * @param listener Listener object. */ public function removeEventListener( type : EventType, listener ) : Void { _oEB.removeEventListener( type, listener ); } /** * Broadcasts event to suscribed listeners. * * @param event an IEvent instance */ public function broadcastEvent( event : IEvent ) : Void { _oEB.broadcastEvent( event ); } /** * Creates and returns a new empty MovieClip on {@code root} level. * *
MovieClip depth is automatically calculated. * * @param name ( optional ) MovieClip name */ public function createEmptyMovieClip( name : String ) : MovieClip { return _createLayer( _getLibLevel( ROOT_LIB_LEVEL ), name ); } /** * Creates and returns a new {@link fever.data.libs.FontLib} target. * * @param name ( optional ) FontLib name */ public function createFontLibTarget( name : String ) : MovieClip { return _createLayer( _getLibLevel( FONT_LIB_LEVEL ), name ); } /** * Creates and returns a new {@link fever.data.libs.ThemeLib} target. * * @param name ( optional ) ThemeLib name */ public function createThemeLibTarget( name : String ) : MovieClip { return _createLayer( _getLibLevel( THEME_LIB_LEVEL ), name ); } /** * Creates and returns a new {@link fever.data.libs.BitmapLib} target. * * @param name ( optional ) BitmapLib name */ public function createBitmapLibTarget( name : String ) : MovieClip { return _createLayer( _getLibLevel( BITMAP_LIB_LEVEL ), name ); } /** * Creates and returns a new {@link fever.data.libs.SoundLib} target. * * @param name ( optional ) SoundLib name */ public function createSoundLibTarget( name : String ) : MovieClip { return _createLayer( _getLibLevel( SOUND_LIB_LEVEL ), name ); } /** * Creates and returns a new {@link fever.data.libs.DLLLib} target. * * @param name ( optional ) SoundLib name */ public function createDLLTarget( name : String ) : MovieClip { return _createLayer( _getLibLevel( DLL_LIB_LEVEL ), name ); } /** * Creates a new MovieClip target on overlay level. * *
Means that MovieClip is at the top of rendering layer.
*/
public function createOverlayTarget() : MovieClip
{
return _createLayer( _getLibLevel( OVERLAY_LIB_LEVEL ) );
}
/**
* Creates cursor target in dedicated layer.
*/
public function createCursorTarget() : MovieClip
{
return _createLayer( _getLibLevel( CURSOR_LIB_LEVEL ) );
}
/**
* Returns specific level to keep a possible logging view
* on top of application.
*/
public function getLoggerTarget() : MovieClip
{
return _getLibLevel( LOGGER_LIB_LEVEL );
}
/**
* Sets a new {@link fever.display.background.Background} for
* application.
*/
public function setBackground( b : Background ) : Void
{
if( b != _backgroundStrategy )
{
_backgroundStrategy.clear( _getLibLevel( BACKGROUND_LIB_LEVEL ) );
_backgroundStrategy = b;
_repaint();
}
}
/**
* Returns current {@link fever.display.background.Background}
*/
public function getBackground() : Background
{
return _backgroundStrategy;
}
/**
* Don't use this method !
* Fever core class used it for correct stage initialisation.
*/
public function init() : Void
{
if( !DISABLED_FEATURE )
Fever.application.addEventListener( CoreApplication.onResizeEVENT, this, _repaint, EventPriority.HIGHER );
_repaint();
}
/**
*
*/
public function _getMainLevel( ) : MovieClip
{
return ( !context ) ? ( !FeverRoot.LEVEL ? _root : FeverRoot.LEVEL ) : context;
}
//-------------------------------------------------------------------------
// Private implementation
//-------------------------------------------------------------------------
/**
* Constructor.
*/
private function FeverStage( )
{
_oEB = new EventBroadcaster( this );
_levelMap = new Map();
_levelMap.put( FONT_LIB_LEVEL, '__fontLibLevel__' );
_levelMap.put( DLL_LIB_LEVEL, '__dllLevel__' );
_levelMap.put( BITMAP_LIB_LEVEL, '__bitmapLibLevel__' );
_levelMap.put( THEME_LIB_LEVEL, '__themeLibLevel__' );
_levelMap.put( SOUND_LIB_LEVEL, '__soundLibLevel__' );
_levelMap.put( BACKGROUND_LIB_LEVEL, '__backgroundLevel__' );
_levelMap.put( ROOT_LIB_LEVEL, '__root__' );
_levelMap.put( OVERLAY_LIB_LEVEL, '__overlay__' );
_levelMap.put( CURSOR_LIB_LEVEL, '__cursor__' );
_levelMap.put( LOGGER_LIB_LEVEL, '__logger__' );
if( DISABLED_FEATURE ) return;
Stage.scaleMode = StageScaleMode.NO_SCALE;
Stage.align = StageAlign.TOP_LEFT;
_backgroundStrategy = new SolidBackGround( DEFAULT_BACKGROUND_COLOR );
}
/**
* Returns MovieClip target registred with passed-in {@code levelID}
* identifier.
*
*
If not exist, creates target. */ private function _getLibLevel( levelID : Number ) : MovieClip { var mc : MovieClip; var mainLevel : MovieClip = _getMainLevel( ); if( mainLevel.getInstanceAtDepth( levelID ).getDepth() == undefined ) { mc = mainLevel.createEmptyMovieClip( _levelMap.get( levelID ), levelID ); } else mc = mainLevel.getInstanceAtDepth( levelID ); return mc; } private function _fireEvent( type : EventType ) : Void { broadcastEvent( new StageEvent( type, this ) ); } private function _createLayer( target : MovieClip, name : String ) : MovieClip { var depth : Number = target.getNextHighestDepth(); if( !name ) name = _getName( depth ); var mc : MovieClip = target.createEmptyMovieClip( name, depth ); return mc; } private function _getName( depth : Number ) : String { return HashCodeFactory.getNextName() + depth; } private function _repaint() : Void { var size : Rectangle = Fever.application.getSize(); if( size.width && size.height ) { _backgroundStrategy.paintBackground( _getLibLevel( BACKGROUND_LIB_LEVEL ), size.width, size.height ); } else CommandManagerFPS.getInstance().delay( new Delegate( this, _repaint ) ); } }