SmoothScrollDetector

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

SmoothScrollDetector

Postby bigpete591 » Mon Mar 26, 2012 2:52 pm

Here is a class you can use to allow smooth scrolling of your scenes.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.andengine.input.touch.detector;
  2.  
  3. import org.andengine.engine.camera.ZoomCamera;
  4. import org.andengine.engine.handler.IUpdateHandler;
  5. import org.andengine.entity.scene.Scene;
  6.  
  7. public class SmoothScrollDetector extends SurfaceScrollDetector {
  8.  
  9.         private static class ScrollListener implements IScrollDetectorListener {
  10.  
  11.                 @Override
  12.                 public void onScrollStarted(ScrollDetector pScollDetector,
  13.                                 int pPointerID, float pDistanceX, float pDistanceY) {
  14.                         SmoothScrollDetector smoothScrollDetector = (SmoothScrollDetector) pScollDetector;
  15.  
  16.                         smoothScrollDetector.mScrollAmountX += pDistanceX;
  17.                         smoothScrollDetector.mScrollAmountY += pDistanceY;
  18.                 }
  19.  
  20.                 @Override
  21.                 public void onScroll(ScrollDetector pScollDetector, int pPointerID,
  22.                                 float pDistanceX, float pDistanceY) {
  23.                         SmoothScrollDetector smoothScrollDetector = (SmoothScrollDetector) pScollDetector;
  24.  
  25.                         smoothScrollDetector.mScrollAmountX += pDistanceX;
  26.                         smoothScrollDetector.mScrollAmountY += pDistanceY;
  27.                 }
  28.  
  29.                 @Override
  30.                 public void onScrollFinished(ScrollDetector pScollDetector,
  31.                                 int pPointerID, float pDistanceX, float pDistanceY) {
  32.                         SmoothScrollDetector smoothScrollDetector = (SmoothScrollDetector) pScollDetector;
  33.  
  34.                         smoothScrollDetector.mScrollAmountX += pDistanceX;
  35.                         smoothScrollDetector.mScrollAmountY += pDistanceY;
  36.                 }
  37.  
  38.         }
  39.  
  40.         private final class ScrollUpdater implements IUpdateHandler {
  41.                 @Override
  42.                 public void onUpdate(float pSecondsElapsed) {
  43.                         if (mScrollAmountX == 0 && mScrollAmountY == 0)
  44.                                 return;
  45.  
  46.                         final float zoomFactor = mCamera.getZoomFactor();
  47.  
  48.                         float xScroll = mScrollAmountX * (pSecondsElapsed * mScrollSpeed);
  49.                         float yScroll = mScrollAmountY * (pSecondsElapsed * mScrollSpeed);
  50.  
  51.                         if (Math.abs(xScroll) > Math.abs(mScrollAmountX))
  52.                                 xScroll = mScrollAmountX;
  53.                         if (Math.abs(yScroll) > Math.abs(mScrollAmountY))
  54.                                 yScroll = mScrollAmountY;
  55.  
  56.                         mCamera.offsetCenter(-xScroll / zoomFactor, -yScroll / zoomFactor);
  57.  
  58.                         mScrollAmountX -= xScroll;
  59.                         mScrollAmountY -= yScroll;
  60.  
  61.                         if (Math.abs(mScrollAmountX) < 1)
  62.                                 mScrollAmountX = 0;
  63.                         if (Math.abs(mScrollAmountY) < 1)
  64.                                 mScrollAmountY = 0;
  65.                 }
  66.  
  67.                 @Override
  68.                 public void reset() {
  69.  
  70.                 }
  71.         }
  72.  
  73.         private static final float TRIGGER_SCROLL_MINIMUM_DISTANCE_DEFAULT = 10;
  74.         private static final float SCROLL_SPEED_DEFAULT = 12;
  75.  
  76.         private ZoomCamera mCamera;
  77.         private Scene mScene;
  78.         private float mScrollSpeed;
  79.  
  80.         public float mScrollAmountX = 0;
  81.         public float mScrollAmountY = 0;
  82.         private ScrollUpdater mUpdateHandler;
  83.  
  84.         public SmoothScrollDetector(ZoomCamera camera, Scene scene) {
  85.                 this(SmoothScrollDetector.TRIGGER_SCROLL_MINIMUM_DISTANCE_DEFAULT,
  86.                                 camera, scene, SmoothScrollDetector.SCROLL_SPEED_DEFAULT);
  87.         }
  88.  
  89.         public SmoothScrollDetector(float pTriggerScrollMinimumDistance,
  90.                         ZoomCamera camera, Scene scene, float scrollSpeed) {
  91.                 super(pTriggerScrollMinimumDistance, new ScrollListener());
  92.                 mCamera = camera;
  93.                 mScene = scene;
  94.                 mScrollSpeed = scrollSpeed;
  95.  
  96.                 mUpdateHandler = new ScrollUpdater();
  97.                 this.enable();
  98.         }
  99.  
  100.         public void disable() {
  101.                 mScene.unregisterUpdateHandler(mUpdateHandler);
  102.         }
  103.  
  104.         public void enable() {
  105.                 mScene.registerUpdateHandler(mUpdateHandler);
  106.         }
  107.  
  108. }
  109.  
Parsed in 0.042 seconds, using GeSHi 1.0.8.4


To use it, create a new SmoothScrollDetector object, and pass it the reference to your camera and scene. Then pass the scene touch events to your scroll detector:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         @Override
  2.         public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
  3.                 this.mScrollDetector.onTouchEvent(pSceneTouchEvent);
  4.                 return true;
  5.         }      
  6.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
Last edited by bigpete591 on Sat Apr 21, 2012 9:03 am, edited 1 time in total.
bigpete591
 
Posts: 9
Joined: Sun Dec 25, 2011 9:16 am

Re: SmoothScrollDetector

Postby tomasz.k » Wed Apr 18, 2012 1:06 pm

Thank you very much for this detector.

But Im a noob and when I implement this class nothing happens.

I set up SmoothCamera;
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private ZoomCamera pCamera;
  2. ...
  3. pCamera = new ZoomCamera(0, 0, 800, 480);
  4.                
  5.                 return new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR,
  6.                                 new RatioResolutionPolicy(CAM_WIDTH, CAM_HEIGHT), pCamera);
  7. ...
  8.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

I load my textures and regions:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ...
  2.                 bgAtlas = new BitmapTextureAtlas(getTextureManager(), 1000, 480, TextureOptions.BILINEAR);
  3.                 bgRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
  4.                                 bgAtlas, this, "scene1.png", 0, 0);
  5.  
  6.                 bgAtlas.load();
  7.  
  8.                 pOnCreateResourcesCallback.onCreateResourcesFinished();
  9. ...
  10.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

I set up my scene and listeners.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. pScene = new Scene();
  2.                 Sprite sp = new Sprite(0, 0, bgRegion, this.getVertexBufferObjectManager());
  3.                 pScene.setBackground(new SpriteBackground(sp = new Sprite(0, 0, bgRegion, this.getVertexBufferObjectManager())));
  4.                 myScrollDetector = new SmoothScrollDetector(pCamera, pScene);        
  5.                 pScene.setOnSceneTouchListener(myScrollDetector);
  6.         pScene.setOnSceneTouchListenerBindingOnActionDownEnabled(true);
  7.        
  8.                 pOnCreateSceneCallback.onCreateSceneFinished(pScene);
  9.  
  10.  
Parsed in 0.043 seconds, using GeSHi 1.0.8.4


And nothing scrolls :/ when I for example add sprite on it, only he is scrolling. When I want to scroll my background and everything which is on it(sprite's) i must to use sprite as a background instead of pScene.setBackground(...) ?
tomasz.k
 
Posts: 6
Joined: Thu Apr 12, 2012 5:51 pm

Re: SmoothScrollDetector

Postby bigpete591 » Sat Apr 21, 2012 9:03 am

I'm not sure about scrolling of backgrounds. Does it scroll normally if you use the standard scroll detector?
bigpete591
 
Posts: 9
Joined: Sun Dec 25, 2011 9:16 am

Re: SmoothScrollDetector

Postby GabS » Fri Nov 30, 2012 10:47 am

Thanks a lot !
GabS
 
Posts: 20
Joined: Thu Oct 11, 2012 12:51 am


Return to Features

Who is online

Users browsing this forum: valka and 8 guests