View Javadoc

    1   /*
    2    * All Rights Reserved.
    3    * Copyright (C) 1999-2005 Tsukuba Bunko.
    4    *
    5    * Licensed under the BSD License ("the License"); you may not use
    6    * this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    *       http://www.tsukuba-bunko.org/licenses/LICENSE.txt
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   * $Id: ActorHandler.java,v 1.3 2005/08/22 02:21:38 ppoi Exp $
    18   */
    19  package tsukuba_bunko.peko.scenario.stage;
    20
    21  import	org.xml.sax.Attributes;
    22
    23  import	tsukuba_bunko.peko.Logger;
    24
    25  import	tsukuba_bunko.peko.canvas.stage.Actor;
    26
    27  import	tsukuba_bunko.peko.resource.ResourceManager;
    28
    29  import	tsukuba_bunko.peko.scenario.PSMLUtil;
    30
    31
    32  /***
    33   * <samp>enter</samp>, <samp>move</samp>, <samp>exit</samp> を処理する <code>ElementHandler</code> です。
    34   * @author	$Author: ppoi $
    35   * @version	$Revision: 1.3 $
    36   */
    37  public class ActorHandler	extends StageElementHandler	{
    38
    39  	/***
    40  	 * <code>ActorHandler</code> のインスタンスを作成します。
    41  	 */
    42  	public ActorHandler()
    43  	{
    44  		super();
    45  	}
    46
    47
    48  //
    49  //	ContentHandler の実装
    50  //
    51  	public void startElement( String namespaceURI, String localName, String qName, Attributes attrs )
    52  	{
    53  		String	name = PSMLUtil.getAttributeValue( attrs, "name" );
    54  		if( (name == null) || (name.length() == 0) )	{
    55  			Logger.error( MessageIDs.SCN3001E, new Object[]{getSceneContext().getCurrentPath()} );
    56  			return;
    57  		}
    58
    59  		String	looks = PSMLUtil.getAttributeValue( attrs, "looks" );
    60  		String	position = PSMLUtil.getAttributeValue( attrs, "position" );
    61
    62  		StageCoordinator	coordinator = getStageCoordinator();
    63  		if( localName.equals("enter") )	{
    64  			if( coordinator.getActor(name) != null )	{
    65  				Logger.warn( MessageIDs.SCN3008W, new Object[]{name, getSceneContext().getCurrentPath()} );
    66  			}
    67  			Actor	actor = new Actor( name );
    68  			actor.setLooks( looks );
    69  			setPosition( actor, ((position == null)?"center":position), "center" );
    70  			coordinator.enter( actor );
    71  		}
    72  		else if( localName.equals("action") )	{
    73  			Actor	actor = coordinator.getActor( name );
    74  			if( actor == null )	{
    75  				Logger.warn( MessageIDs.SCN3002W, new Object[]{name, getSceneContext().getCurrentPath()} );
    76  			}
    77  			else	{
    78  				if( looks != null )	{
    79  					actor.setLooks( looks );
    80  				}
    81  				setPosition( actor, position, null );
    82  			}
    83  			coordinator.action( actor );
    84  		}
    85  		else if( localName.equals("exit") )	{
    86  			if( coordinator.exit(name) == null )	{
    87  				Logger.warn( MessageIDs.SCN3002W, new Object[]{name, getSceneContext().getCurrentPath()} );
    88  			}
    89  		}
    90
    91  		String	effect = PSMLUtil.getAttributeValue( attrs, "effect" );
    92  		if( effect == null )	{
    93  			ResourceManager	resources = ResourceManager.getInstance();
    94  			effect = (String)resources.getResource( ResourceIDs.DEFAULT_EFFECT_ACTOR, true );
    95  		}
    96  		if( !coordinator.isSlideVisible() )	{
    97  			coordinator.updateStage( effect );
    98  		}
    99  	}
    100
    101 	protected void setPosition( Actor actor, String position, String defaultPosition )
    102 	{
    103 		if( position == null )	{
    104 			return;
    105 		}
    106
    107 		if( "center".equals(position) )	{
    108 			actor.setPosition( Actor.POSITION_CENTER );
    109 		}
    110 		else if( "left".equals(position) )	{
    111 			actor.setPosition( Actor.POSITION_LEFT );
    112 		}
    113 		else if( "right".equals(position) )	{
    114 			actor.setPosition( Actor.POSITION_RIGHT );
    115 		}
    116 		else	{
    117 			try	{
    118 				actor.setPosition( Float.parseFloat(position) );
    119 			}
    120 			catch( Exception e )	{
    121 				Logger.warn( MessageIDs.SCN3005W, new Object[]{getSceneContext().getCurrentPath()}, e );
    122 				setPosition( actor, defaultPosition, null );
    123 			}
    124 		}
    125 	}
    126 }