[New Feature] SceneManager + Transitions

  ... the case you feel the need for a new feature or want to submit one.

[New Feature] SceneManager + Transitions

Postby rkpost » Thu May 05, 2011 7:21 am

This is a very simple intent to add a scene control to AndEngine. I'm working in a best solution but this could be useful for someone.

It's a bad idea use a single Activity with all scenes there. I want to create different classes for each scene and have the possibility to change between them. A global access can be created by a Singleton class.


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.rkpost.test.mavenproject;
  2.  
  3. import android.util.DisplayMetrics;
  4. import com.rkpost.test.mavenproject.scenes.transitions.AbstractTransition;
  5. import com.rkpost.test.mavenproject.scenes.transitions.ITransitionListener;
  6. import org.anddev.andengine.engine.Engine;
  7. import org.anddev.andengine.entity.scene.Scene;
  8. import org.anddev.andengine.ui.activity.BaseGameActivity;
  9.  
  10. /**
  11.  *
  12.  * @author rkpost
  13.  */
  14. public class WindowManager {
  15.  
  16.     private static WindowManager INSTANCE = new WindowManager();
  17.  
  18.     public static void initialize(BaseGameActivity pActivity) {
  19.         INSTANCE.mDisplayMetrics = new DisplayMetrics();
  20.         INSTANCE.mEngine = pActivity.getEngine();
  21.         INSTANCE.mActivity = pActivity;
  22.         INSTANCE.mActivity.getWindowManager().getDefaultDisplay().getMetrics(INSTANCE.mDisplayMetrics);
  23.     }
  24.  
  25.     public static WindowManager getInstance() {
  26.         return INSTANCE;
  27.     }
  28.    
  29.     private BaseGameActivity mActivity;
  30.     private DisplayMetrics mDisplayMetrics;
  31.     private Engine mEngine;
  32.     private Scene mTransitionScene = new Scene(0);
  33.  
  34.     public float getDisplayHeight() {
  35.         return mDisplayMetrics.heightPixels * mDisplayMetrics.scaledDensity;
  36.     }
  37.  
  38.     public float getDisplayWidth() {
  39.         return mDisplayMetrics.widthPixels * mDisplayMetrics.scaledDensity;
  40.     }
  41.  
  42.     public float getSurfaceHeight() {
  43.         return mEngine.getCamera().getHeight();
  44.     }
  45.  
  46.     public float getSurfaceWidth() {
  47.         return mEngine.getCamera().getWidth();
  48.     }
  49.  
  50.     public Scene getCurrentScene() {
  51.         return mEngine.getScene();
  52.     }
  53.  
  54.     public void setScene(final Scene pScene) {
  55.         mEngine.runOnUpdateThread(new Runnable() {
  56.  
  57.             @Override
  58.             public void run() {
  59.                 mEngine.setScene(pScene);
  60.             }
  61.         });
  62.     }
  63.  
  64.     public void setScene(Scene pInScene, AbstractTransition pTransition) {
  65.  
  66.         this.setScene(mTransitionScene);
  67.         mTransitionScene.attachChild(this.getCurrentScene());
  68.         mTransitionScene.attachChild(pInScene);
  69.        
  70.         pTransition.execute(this.getCurrentScene(), pInScene, new ITransitionListener() {
  71.  
  72.             @Override
  73.             public void onTransitionFinished(Scene pOutScene, Scene pInScene, AbstractTransition pTransition) {
  74.                 mEngine.runOnUpdateThread(new Runnable() {
  75.  
  76.                     @Override
  77.                     public void run() {
  78.                         mTransitionScene.detachChildren();
  79.                     }
  80.                 });
  81.                 WindowManager.this.setScene(pInScene);
  82.             }
  83.         });
  84.  
  85.     }
  86. }
  87.  
Parsed in 0.014 seconds, using GeSHi 1.0.8.4


This class is initialized onLoadResources
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. WindowManager.initialize(this);
Parsed in 0.010 seconds, using GeSHi 1.0.8.4


With this class, from everywhere, I can get some screen information and set the main scene. The last method use transitions. (next post)
rkpost
 
Posts: 201
Joined: Tue Sep 14, 2010 5:52 am
Location: Buenos Aires

Re: [New Feature] SceneManager + Transitions

Postby rkpost » Thu May 05, 2011 7:26 am

With transitions a have some limitations because AndEngine is not prepare to use this feature.

Limitations:
It's not possible use ColorBackground
It's not possible use SpriteBackground larger than screen size.
It's not possible change between scenes with different size.
HUD isn't move.
Works fine with ResolutionPolicy Fill, maybe with Ratio.

First, Background isn't a child of scene, so, when I move the scene, background don't move. I create my own scene (extends from original) to solve this problem.


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.rkpost.test.mavenproject.scenes;
  2.  
  3. import java.util.ArrayList;
  4. import javax.microedition.khronos.opengles.GL10;
  5. import org.anddev.andengine.engine.camera.Camera;
  6. import org.anddev.andengine.entity.IEntity;
  7. import org.anddev.andengine.entity.scene.Scene;
  8. import org.anddev.andengine.opengl.texture.region.TextureRegionLibrary;
  9. import org.anddev.andengine.opengl.util.GLHelper;
  10.  
  11. /**
  12.  *
  13.  * @author rkpost
  14.  */
  15. public abstract class SceneBase extends Scene {
  16.  
  17.     protected TextureRegionLibrary mTextureRegionLibrary;
  18.  
  19.     public SceneBase(int pLayerCount) {
  20.         super(pLayerCount);
  21.     }
  22.  
  23.     @Override
  24.     protected void onManagedDraw(final GL10 pGL, final Camera pCamera) {
  25.         final Scene childScene = this.mChildScene;
  26.  
  27.         if (childScene == null || !this.mChildSceneModalDraw) {
  28.             if (this.isBackgroundEnabled()) {
  29.  
  30.                 pCamera.onApplyMatrix(pGL);
  31.                 GLHelper.setModelViewIdentityMatrix(pGL);
  32.  
  33.                 pGL.glPushMatrix();
  34.                 {
  35.                     this.onApplyTransformations(pGL);
  36.                     this.getBackground().onDraw(pGL, pCamera);
  37.                 }
  38.                 pGL.glPopMatrix();
  39.  
  40.             }
  41.  
  42.             pCamera.onApplyMatrix(pGL);
  43.             GLHelper.setModelViewIdentityMatrix(pGL);
  44.  
  45.             pGL.glPushMatrix();
  46.             {
  47.                 this.onApplyTransformations(pGL);
  48.  
  49.                 this.doDraw(pGL, pCamera);
  50.  
  51.                 if (this.mChildren != null) {
  52.                     final ArrayList<IEntity> entities = this.mChildren;
  53.                     final int entityCount = entities.size();
  54.                     for (int i = 0; i < entityCount; i++) {
  55.                         entities.get(i).onDraw(pGL, pCamera);
  56.                     }
  57.                 }
  58.             }
  59.             pGL.glPopMatrix();
  60.  
  61.         }
  62.  
  63.         if (childScene != null) {
  64.             childScene.onDraw(pGL, pCamera);
  65.         }
  66.     }
  67. }
Parsed in 0.013 seconds, using GeSHi 1.0.8.4
Last edited by rkpost on Thu May 05, 2011 7:41 am, edited 1 time in total.
rkpost
 
Posts: 201
Joined: Tue Sep 14, 2010 5:52 am
Location: Buenos Aires

Re: [New Feature] SceneManager + Transitions

Postby rkpost » Thu May 05, 2011 7:29 am

A listener tell me when a transition has finished.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.rkpost.test.mavenproject.scenes.transitions;
  2.  
  3. import org.anddev.andengine.entity.scene.Scene;
  4.  
  5. /**
  6.  *
  7.  * @author rkpost
  8.  */
  9. public interface ITransitionListener {
  10.  
  11.     public void onTransitionFinished(Scene pOutScene, Scene pInScene, AbstractTransition pTransition);
  12.    
  13. }
Parsed in 0.010 seconds, using GeSHi 1.0.8.4



And all my transitions extends from this class

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.rkpost.test.mavenproject.scenes.transitions;
  2.  
  3. import org.anddev.andengine.entity.scene.Scene;
  4. import org.anddev.andengine.util.modifier.ease.EaseLinear;
  5. import org.anddev.andengine.util.modifier.ease.IEaseFunction;
  6.  
  7. /**
  8.  *
  9.  * @author rkpost
  10.  */
  11. public abstract class AbstractTransition {
  12.  
  13.     protected IEaseFunction mEaseFunction;
  14.     protected float mDuration;
  15.  
  16.     public AbstractTransition(float pDuration) {
  17.         this(pDuration, EaseLinear.getInstance());
  18.     }
  19.  
  20.     public AbstractTransition(float mDuration, IEaseFunction pEaseFunction) {
  21.         this.mDuration = mDuration;
  22.         this.mEaseFunction = pEaseFunction;
  23.     }
  24.  
  25.     public abstract void execute(final Scene pOutScene, final Scene pInScene, final ITransitionListener pTransitionListener);
  26.  
  27.     public abstract AbstractTransition getBackTransition();
  28.  
  29.     protected void onTransitionFinish(final Scene pOutScene, final Scene pInScene, final ITransitionListener pTransitionListener) {
  30.  
  31.         pOutScene.setPosition(0, 0);
  32.         pOutScene.setRotation(0);
  33.         pOutScene.setAlpha(1);
  34.         pOutScene.setScale(1);
  35.         pOutScene.setVisible(true);
  36.  
  37.         pInScene.setPosition(0, 0);
  38.         pInScene.setRotation(0);
  39.         pInScene.setAlpha(1);
  40.         pInScene.setScale(1);
  41.         pInScene.setVisible(true);
  42.  
  43.         pTransitionListener.onTransitionFinished(pOutScene, pInScene, this);
  44.        
  45.     }
  46. }
Parsed in 0.012 seconds, using GeSHi 1.0.8.4
rkpost
 
Posts: 201
Joined: Tue Sep 14, 2010 5:52 am
Location: Buenos Aires

Re: [New Feature] SceneManager + Transitions

Postby rkpost » Thu May 05, 2011 7:32 am

Now two simple implementations

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.rkpost.test.mavenproject.scenes.transitions;
  2.  
  3. import com.rkpost.test.mavenproject.WindowManager;
  4. import org.anddev.andengine.entity.IEntity;
  5. import org.anddev.andengine.entity.modifier.MoveXModifier;
  6. import org.anddev.andengine.entity.scene.Scene;
  7. import org.anddev.andengine.util.modifier.IModifier;
  8. import org.anddev.andengine.util.modifier.IModifier.IModifierListener;
  9. import org.anddev.andengine.util.modifier.ease.IEaseFunction;
  10.  
  11. /**
  12.  *
  13.  * @author rkpost
  14.  */
  15. public class LeftPushInTransition extends AbstractTransition {
  16.  
  17.     public LeftPushInTransition(float pDuration) {
  18.         super(pDuration);
  19.     }
  20.  
  21.     public LeftPushInTransition(float mDuration, IEaseFunction pEaseFunction) {
  22.         super(mDuration, pEaseFunction);
  23.     }
  24.  
  25.     @Override
  26.     public void execute(final Scene pOutScene, final Scene pInScene, final ITransitionListener pTransitionListener) {
  27.  
  28.         float width = WindowManager.getInstance().getSurfaceWidth();
  29.  
  30.         MoveXModifier outModifier = new MoveXModifier(1, 0, width, mEaseFunction);
  31.         MoveXModifier inModifier = new MoveXModifier(1, -width, 0, mEaseFunction);
  32.  
  33.         pInScene.registerEntityModifier(inModifier);
  34.         pOutScene.registerEntityModifier(outModifier);
  35.  
  36.         inModifier.setModifierListener(new IModifierListener<IEntity>() {
  37.  
  38.             @Override
  39.             public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
  40.                 onTransitionFinish(pOutScene, pInScene, pTransitionListener);
  41.             }
  42.         });
  43.  
  44.     }
  45.  
  46.     @Override
  47.     public AbstractTransition getBackTransition() {
  48.         return new RightPushInTransition(mDuration, mEaseFunction);
  49.     }
  50. }
Parsed in 0.012 seconds, using GeSHi 1.0.8.4



Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.rkpost.test.mavenproject.scenes.transitions;
  2.  
  3. import com.rkpost.test.mavenproject.WindowManager;
  4. import org.anddev.andengine.entity.IEntity;
  5. import org.anddev.andengine.entity.modifier.MoveXModifier;
  6. import org.anddev.andengine.entity.scene.Scene;
  7. import org.anddev.andengine.util.modifier.IModifier;
  8. import org.anddev.andengine.util.modifier.IModifier.IModifierListener;
  9. import org.anddev.andengine.util.modifier.ease.IEaseFunction;
  10.  
  11. /**
  12.  *
  13.  * @author rkpost
  14.  */
  15. public class RightPushInTransition extends AbstractTransition {
  16.  
  17.     public RightPushInTransition(float pDuration) {
  18.         super(pDuration);
  19.     }
  20.  
  21.     public RightPushInTransition(float mDuration, IEaseFunction pEaseFunction) {
  22.         super(mDuration, pEaseFunction);
  23.     }
  24.  
  25.     @Override
  26.     public void execute(final Scene pOutScene, final Scene pInScene, final ITransitionListener pTransitionListener) {
  27.  
  28.         float width = WindowManager.getInstance().getSurfaceWidth();
  29.  
  30.         MoveXModifier outModifier = new MoveXModifier(1, 0, -width, mEaseFunction);
  31.         MoveXModifier inModifier = new MoveXModifier(1, width, 0, mEaseFunction);
  32.  
  33.         pInScene.registerEntityModifier(inModifier);
  34.         pOutScene.registerEntityModifier(outModifier);
  35.  
  36.         inModifier.setModifierListener(new IModifierListener<IEntity>() {
  37.  
  38.             @Override
  39.             public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
  40.                 onTransitionFinish(pOutScene, pInScene, pTransitionListener);
  41.             }
  42.         });
  43.  
  44.     }
  45.  
  46.     @Override
  47.     public AbstractTransition getBackTransition() {
  48.         return new LeftPushInTransition(mDuration, mEaseFunction);
  49.     }
  50. }
  51.  
Parsed in 0.012 seconds, using GeSHi 1.0.8.4
rkpost
 
Posts: 201
Joined: Tue Sep 14, 2010 5:52 am
Location: Buenos Aires

Re: [New Feature] SceneManager + Transitions

Postby rkpost » Thu May 05, 2011 7:48 am

Source code.

I'm working on with two features:

* ResourceManager, to load/unload resources from everywhere. A best control of resources, unload textures when no longer used, etc.

* A best implementation of transitions, using Engine+Camera.
Attachments
transitions.rar
(12.19 KiB) Downloaded 602 times
rkpost
 
Posts: 201
Joined: Tue Sep 14, 2010 5:52 am
Location: Buenos Aires

Re: [New Feature] SceneManager + Transitions

Postby Eksrow » Thu May 05, 2011 6:01 pm

Very nice, i'l try it.
Eksrow
 
Posts: 43
Joined: Fri Mar 11, 2011 5:07 pm

Re: [New Feature] SceneManager + Transitions

Postby oeN » Mon Jun 13, 2011 7:59 pm

sceneBase class has a problem with var mChildSceneModalDraw. This var is private in andengine. in oder you to access that variable you had to change it to protected right?
Blog: link
My Android Apps: link
oeN
 
Posts: 15
Joined: Wed Jun 08, 2011 8:21 pm

Re: [New Feature] SceneManager + Transitions

Postby oeN » Mon Jun 13, 2011 8:26 pm

I had done this little change to SceneBase to make it work:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private boolean mChildSceneModalDraw;
  2.        
  3.     @Override
  4.     public void setChildScene(final Scene pChildScene, final boolean pModalDraw, final boolean pModalUpdate, final boolean pModalTouch) {
  5.         super.setChildScene(pChildScene, pModalDraw, pModalUpdate, pModalTouch);
  6.                 this.mChildSceneModalDraw = pModalDraw;
  7.         }
  8.  
Parsed in 0.010 seconds, using GeSHi 1.0.8.4
Blog: link
My Android Apps: link
oeN
 
Posts: 15
Joined: Wed Jun 08, 2011 8:21 pm

Re: [New Feature] SceneManager + Transitions

Postby xastor » Wed Oct 10, 2012 9:48 pm

This is great stuff, works still, one year later :-)
I had this running in a few minutes with the GLES2 engine..
xastor
 
Posts: 8
Joined: Wed Oct 10, 2012 9:22 pm


Return to Features

Who is online

Users browsing this forum: No registered users and 8 guests