smart-fun / smartGL

SmartGL is a Graphic Engine for creating Android Games and Apps. It is based on OpenGL and handles 2D Sprites and 3D Textured Objects.
Apache License 2.0
109 stars 24 forks source link

how to properly implement drag-and-drop? #24

Closed creek23 closed 5 years ago

creek23 commented 5 years ago

from onTouchEvent, I can check for event.getType() for the 3 sequence:

  1. TouchHelperEvent.TouchEventType.SINGLETOUCH
  2. TouchHelperEvent.TouchEventType.SINGLEMOVE
  3. TouchHelperEvent.TouchEventType.SINGLEUNTOUCH

With those 3 events, I'm trying to:

  1. get X,Y where SINGLETOUCH happened
  2. on SINGLEMOVE, I get X,Y in reference to SINGLETOUCH's X,Y
  3. I dereference on X,Y on SINGLEUNTOUCH

Using the X,Y from above, I apply it to a sprite to give the illusion of Drag-and-Drop, but the sprite moves around like crazy. Am I using Touch Events properly?

~creek23

creek23 commented 5 years ago

Never mind, found the logic error in my code. Sharing the snippet for others to reuse.

float referenceX, referenceY; @Override public void onTouchEvent(SmartGLView smartGLView, TouchHelperEvent event) { if (event.getType() == TouchHelperEvent.TouchEventType.SINGLETOUCH) { referenceX = event.getX(event.getNbFingers()-1); referenceY = event.getY(event.getNbFingers()-1); } else if (event.getType() == TouchHelperEvent.TouchEventType.SINGLEMOVE) { int newX = event.getX(event.getNbFingers()-1) - referenceX; int newY = event.getY(event.getNbFingers()-1) - referenceY; Sprite mSprite = renderPassSprite.getRenderObjects().get(i) instanceof Sprite ? ((Sprite) (renderPassSprite.getRenderObjects().get(i))) : null; if (mSprite != null) { mSprite.setPos(mSprite.getPosX() + newX, mSprite.getPosY() + newY); } referenceX = event.getX(event.getNbFingers()-1); referenceY = event.getY(event.getNbFingers()-1); } }

~creek23