PreviewSceneEngine

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

PreviewSceneEngine

Postby rkpost » Wed Sep 15, 2010 6:48 am

Is my second day working with andengine and I made a new Engine in order to show a preview, usefull when main screen is bigger and we have to use drag to scroll.

Maybe you can improve it and add it to the engine.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import javax.microedition.khronos.opengles.GL10;
  2.  
  3. import org.anddev.andengine.engine.Engine;
  4. import org.anddev.andengine.engine.camera.Camera;
  5. import org.anddev.andengine.engine.options.EngineOptions;
  6. import org.anddev.andengine.entity.primitive.Rectangle;
  7. import org.anddev.andengine.entity.scene.Scene;
  8. import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
  9. import org.anddev.andengine.entity.shape.Shape;
  10. import org.anddev.andengine.input.touch.TouchEvent;
  11.  
  12. import android.view.MotionEvent;
  13.  
  14. public class PreviewSceneEngine extends Engine implements IOnSceneTouchListener {
  15.  
  16.         private float mTouchX = 0, mTouchY = 0, mTouchOffsetX = 0, mTouchOffsetY = 0;
  17.  
  18.         private Shape ground;
  19.         private Shape roof;
  20.         private Shape left;
  21.         private Shape right;
  22.        
  23.         private boolean mMoving;
  24.         private final Camera mPreviewCamera;
  25.        
  26.         public PreviewSceneEngine(EngineOptions pEngineOptions, final Camera pPreviewCamera) {
  27.                 super(pEngineOptions);
  28.                 this.mPreviewCamera = pPreviewCamera;
  29.         }
  30.        
  31.         @Override
  32.         public void onLoadComplete(Scene pScene) {
  33.                 super.onLoadComplete(pScene);
  34.                 pScene.setOnSceneTouchListener(this);
  35.  
  36.                 ground = new Rectangle(0, getMainCamera().getHeight()-10, getMainCamera().getWidth()+20, 10);
  37.                 roof = new Rectangle(0, 0, getMainCamera().getWidth()+20, 10);
  38.                 left = new Rectangle(0, 0, 10, getMainCamera().getHeight());
  39.                 right = new Rectangle(getMainCamera().getWidth() - 10, 0, 10, getMainCamera().getHeight()+10);
  40.                 pScene.getTopLayer().addEntity(ground);
  41.                 pScene.getTopLayer().addEntity(roof);
  42.                 pScene.getTopLayer().addEntity(left);
  43.                 pScene.getTopLayer().addEntity(right);
  44.                
  45.         }
  46.        
  47.         @Deprecated
  48.         @Override
  49.         public Camera getCamera() {
  50.                 return super.mCamera;
  51.         }
  52.  
  53.         public Camera getMainCamera() {
  54.                 return super.mCamera;
  55.         }
  56.  
  57.         public Camera getPreviewCamera() {
  58.                 return this.mPreviewCamera;
  59.         }
  60.        
  61.         @Override
  62.         protected void onDrawScene(final GL10 pGL) {
  63.                 final Camera mainCamera = this.getMainCamera();
  64.                 final Camera previewCamera = this.getPreviewCamera();
  65.  
  66.                 final int surfaceWidth = this.mSurfaceWidth;
  67.                 final int surfaceCornerWidht = this.mSurfaceWidth / 3;
  68.                
  69.                 final int surfaceHeight = this.mSurfaceHeight;
  70.                 final int surfaceCornerHeight = this.mSurfaceHeight / 3;
  71.  
  72.                 pGL.glEnable(GL10.GL_SCISSOR_TEST); // TODO --> GLHelper
  73.  
  74.                 /* Main Screen. With first camera, on all screen. */
  75.                 {
  76.                         pGL.glScissor(0, 0, surfaceWidth, surfaceHeight);
  77.                         pGL.glViewport(0, 0, surfaceWidth, surfaceHeight);
  78.  
  79.                         super.mScene.onDraw(pGL, mainCamera);
  80.                         mainCamera.onDrawHUD(pGL);
  81.                 }
  82.  
  83.                 /* Preview Screen. With preview camera, on the corner. Only if  */
  84.                 if(mMoving){
  85.                         pGL.glScissor(15, 15, surfaceCornerWidht, surfaceCornerHeight);
  86.                         pGL.glViewport(15, 15, surfaceCornerWidht, surfaceCornerHeight);
  87.  
  88.                         super.mScene.onDraw(pGL, previewCamera);
  89.                         previewCamera.onDrawHUD(pGL);
  90.                 }
  91.  
  92.                 pGL.glDisable(GL10.GL_SCISSOR_TEST);
  93.         }
  94.  
  95.  
  96.         @Override
  97.         public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
  98.                 if(pSceneTouchEvent.getMotionEvent().getAction() == MotionEvent.ACTION_DOWN) { 
  99.                         mTouchX = pSceneTouchEvent.getMotionEvent().getX();
  100.                         mTouchY = pSceneTouchEvent.getMotionEvent().getY();
  101.                         mMoving = true;
  102.                         return true;
  103.                 }
  104.                 if(pSceneTouchEvent.getMotionEvent().getAction() == MotionEvent.ACTION_UP) {
  105.                         mMoving = false;
  106.                         return true;
  107.                 }
  108.                 if(pSceneTouchEvent.getMotionEvent().getAction() == MotionEvent.ACTION_MOVE) {
  109.                        
  110.                         float newX = pSceneTouchEvent.getMotionEvent().getX();
  111.                         float newY = pSceneTouchEvent.getMotionEvent().getY();
  112.  
  113.                         mTouchOffsetX = (newX - mTouchX);
  114.                         mTouchOffsetY = (newY - mTouchY);
  115.  
  116.                         float newScrollX = getMainCamera().getCenterX() - mTouchOffsetX;
  117.                         float newScrollY = getMainCamera().getCenterY() - mTouchOffsetY;
  118.  
  119.                         getMainCamera().setCenter(newScrollX, newScrollY);
  120.  
  121.                         mTouchX = newX;
  122.                         mTouchY = newY;
  123.                        
  124.                         ground.setPosition(getMainCamera().getMinX()-10, getMainCamera().getMaxY());
  125.                         roof.setPosition(getMainCamera().getMinX()-10, getMainCamera().getMinY()-10);
  126.                         left.setPosition(getMainCamera().getMinX()-10, getMainCamera().getMinY());
  127.                         right.setPosition(getMainCamera().getMaxX(), getMainCamera().getMinY());
  128.                        
  129.                         return true;
  130.                 }
  131.                 return false;
  132.         }
  133.  
  134. }
  135.  
  136.  
Parsed in 0.019 seconds, using GeSHi 1.0.8.4


Example
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private static final int CAMERA_WIDTH = 840 * 2;
  2. private static final int CAMERA_HEIGHT = 480 * 2;
  3.  
  4. public Engine onLoadEngine() { ....
  5.  boundCamera = new BoundCamera(0, 0, CAMERA_WIDTH / 2, CAMERA_HEIGHT / 2, 0, CAMERA_WIDTH, 0, CAMERA_HEIGHT);
  6.  camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
  7.  return new PreviewSceneEngine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH/2, CAMERA_HEIGHT/2), boundCamera), camera);
  8. }
  9.  
  10.  
Parsed in 0.011 seconds, using GeSHi 1.0.8.4
rkpost
 
Posts: 201
Joined: Tue Sep 14, 2010 5:52 am
Location: Buenos Aires

Re: PreviewSceneEngine

Postby Nicolas Gramlich » Wed Sep 15, 2010 11:47 am

Hi,

is this intended to be a kind of small MiniMap over the "mainScene" or so :?:

Best Regards,
Nicolas
Nicolas Gramlich
Site Admin
 
Posts: 1734
Joined: Mon Jun 07, 2010 6:20 pm
Location: Schriesheim, Germany

Re: PreviewSceneEngine

Postby rkpost » Wed Sep 15, 2010 3:21 pm

Yes, something like that.

I'm trying to create a puzzle game and I was experimenting with bigger screens. Maybe would be usefull with strategy games too.
rkpost
 
Posts: 201
Joined: Tue Sep 14, 2010 5:52 am
Location: Buenos Aires


Return to Features

Who is online

Users browsing this forum: No registered users and 3 guests