welcome to linkAR technical documentation

next

previous

Object Clicking

Sometimes we want certain actions to be triggered when an object is clicked. To achieve this we offer two ways:

4.1.- Add callback to object

We add a callback to each object we want to receive click events.

public class MainActivity extends Activity implements EngineRenderCallbacks,ModelTouchCallback{
   ...

Then, in the loadScene method, it is necessary to set our activity as touch receiver for each object we want to receive touches:

private void loadEADScene()
{
...
/** Load Button scene graphics from file*/
	    eadResult =  gl3DEngine.loadEAD("Data/Models/girlscene/buttons.EAD");
	    eadID = eadResult.getEAD_ID();
	    if(eadID != -1)
	    {
	    	/** Attach graphics to the model*/
	    	if(gl3DEngine.attachEAD(buttonsModelRootID, eadID))
	    	{
	    		/** Create left button from the Root button model*/
		    	LeftButtonModelID = gl3DEngine.getModel(eadID,"button01");
 
		    	/** Create middle button from the Root button model*/
		    	MidButtonModelID = gl3DEngine.getModel(eadID,"button02");
 
		    	/** Create right button from the Root button model*/
		    	RightButtonModelID = gl3DEngine.getModel(eadID,"button03");
 
		    	/** This way we set our view as touch receiver*/
		    	gl3DEngine.setOnModelTouchListener(LeftButtonModelID, this);
		    	gl3DEngine.setOnModelTouchListener(MidButtonModelID, this);
		    	gl3DEngine.setOnModelTouchListener(RightButtonModelID, this);
	    	}
 
	    }    
...
}

Finally, implement the event receiver method:

@Override
	public void onModelTouched(int modelId) {
		if (modelId==LeftButtonModelID) {
 
			/** Set animation to the model*/
   			if(gl3DEngine.setAnimation(skeletonAnimationID, "girl_animation_01"))
   			{
   				/** Play animation previously set*/
   				if(!gl3DEngine.playAnimation(skeletonAnimationID,true))
   					Log.d(TAG, "Error playing girl_animation_01");
 
   			}
   			Log.d(TAG, "Left Button clicked");
		}else 	if (modelId==MidButtonModelID) {
			/** Set animation to the model*/
   			if(gl3DEngine.setAnimation(skeletonAnimationID, "girl_animation_02"))
   			{
   				/** Play animation previously set*/
   				if(!gl3DEngine.playAnimation(skeletonAnimationID,true))
   					Log.d(TAG, "Error playing girl_animation_02");
 
   			}
   			Log.d(TAG, "Middle Button clicked");
		}else 	if (modelId==RightButtonModelID) {
			/** Set animation to the model*/
   			if(gl3DEngine.setAnimation(skeletonAnimationID, "girl_animation_03"))
   			{
   				/** Play animation previously set*/
   				if(!gl3DEngine.playAnimation(skeletonAnimationID,true))
   					Log.d(TAG, "Error playing girl_animation_03");
 
   			}
   			Log.d(TAG, "Right Button clicked");
   		}
 
 
	}

4.2.- Check if object was clicked

We get the view touch and check whether an object was clicked or not. First it is necessary to receive the View touches:

public class MainActivity extends Activity implements EngineRenderCallbacks,OnTouchListener{
   ...

Then, set the activity as touch listener for the 3DEngine:

@Override 
protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
 
       /** Create the OpenGL view instance  */
        gl3DEngine = new EAGLView(this, "9OMdoCXfeLMzLKpn2J2Cq62csrxUoC2Stu45eczEOA==", this);	 
 
        //set to receive the 3DEngine View touches
        gl3DEngine.setOnTouchListener(this);
 
        ...
    }

Finally implement the interface method, that will receive any view click:

@Override
	public boolean onTouch(View arg0, MotionEvent arg1) {
 
 
		// Get touch position 
		int touchX = (int) arg1.getX(),
			touchY = (int) arg1.getY();
 
		/** Check if left button was clicked*/
		if(gl3DEngine.isObjectClicked(leftButtonModelID, touchX, touchY))
		{
			/** Set animation to the model*/
			if(gl3DEngine.setAnimation(skeletonAnimationID, "girl_animation_01"))
			{
				/** Play animation previously set*/
    			if(!gl3DEngine.playAnimation(skeletonAnimationID, true))	
    				Log.d(TAG, "Error playing girl_animated");
			}
			Log.d(TAG, "Left Button clicked: ("+touchX+","+touchY+")");
			return true;
		}
 
		/** Check if middle button was clicked*/
		if(gl3DEngine.isObjectClicked(MidButtonModelID, touchX, touchY))
		{
			/** Set animation to the model*/
			if(gl3DEngine.setAnimation(skeletonAnimationID, "girl_animation_02"))
			{
				/** Play animation previously set*/
				if(!gl3DEngine.playAnimation(skeletonAnimationID,true))
					Log.d(TAG, "Error playing girl_animation_02");
			}
			Log.d(TAG, "Mid Button clicked: ("+touchX+","+touchY+")");
			return true;
		}	
 
		/** Check if right button was clicked*/
		if(gl3DEngine.isObjectClicked(RightButtonModelID, touchX, touchY))
		{
			/** Set animation to the model*/
			if(gl3DEngine.setAnimation(skeletonAnimationID, "girl_animation_03"))
			{
				/** Play animation previously set*/
				if(!gl3DEngine.playAnimation(skeletonAnimationID,true))
					Log.d(TAG, "Error playing girl_animation_03");
			}
			Log.d(TAG, "Right Button clicked: ("+touchX+","+touchY+")");
			return true;
		}	
		Log.i(TAG, "Global Touch Position: ("+touchX+","+touchY+")");
		return false;
	}

next

previous