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. At the loadEAD method, after loading the buttons EAD, we need to grab its ids and add callbacks(handleClick:) through the addTarget method.

-(void) loadEAD
{
   ...   
leftButton  = [glView getModel:@"button01" witheadId:buttonsEADRes.EADId];
    midButton   = [glView getModel:@"button02" witheadId:buttonsEADRes.EADId];
    rightButton = [glView getModel:@"button03" witheadId:buttonsEADRes.EADId];
 
    [glView addTarget:self withAction:@selector(handleClick:) withModeId:leftButton];
    [glView addTarget:self withAction:@selector(handleClick:) withModeId:midButton];
    [glView addTarget:self withAction:@selector(handleClick:) withModeId:rightButton];
 
    ...
}

The handleClick: method should check which button was clicked:

- (void)handleClick :(NSNotification*)notif
{
    NSNumber *modelId=notif.object;
    // Each button triggers one animation.
    // We first detect if any of the buttons has been clicked.
    // If so, we do play the animation.
 
    if ( [modelId isEqualToNumber:leftButton] )
    {
        if([glView setAnimation:@"girl_animation_01" withModelId:animationObjId])
        {
            if([glView playAnimation:YES withModelId:animationObjId])
            {
                NSLog(@"Playing animation girl_animation_01");
            }
        }
    }else     if ( [modelId isEqualToNumber:midButton] )
    {
        if([glView setAnimation:@"girl_animation_02" withModelId:animationObjId])
        {
            if([glView playAnimation:YES withModelId:animationObjId])
            {
                NSLog(@"Playing animation girl_animation_02");
            }
        }
    }else     if ( [modelId isEqualToNumber:rightButton] )
    {
        if([glView setAnimation:@"girl_animation_03" withModelId:animationObjId])
        {
            if([glView playAnimation:YES withModelId:animationObjId])
            {
                NSLog(@"Playing animation girl_animation_03");
            }
        }
    }
}

4.2.- Check which object was clicked

We want to check for a certain point in the screen which object is clicked. First it is necessary to receive the View touches:

#pragma mark -
#pragma Touch Events
 
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
 
}
 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // On touches began event we want to detect if any of the objects we placed in our world has been clicked.
 
    UITouch *touch = [[touches allObjects] objectAtIndex:0];
 
    [self handleClick:[touch locationInView:self.glView]];
}
 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
 
}
 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
 
}

Our handleClick method will use the isObjectClicked library function to check if any of the buttons has been clicked.

- (void)handleClick :(CGPoint)point
{
    int x = point.x;
    int y = point.y;
 
    NSLog(@"Touched at %d,%d", x,y);
 
    // Check if left button was clicked.
    if ( [glView isObjectClicked:leftButton xClick:IBOX(x) yClick:IBOX(y)] )
    {
        // BUTTON 1 CLICKED
    }
 
    // Check if middle button was clicked.
    if ( [glView isObjectClicked:midButton xClick:IBOX(x) yClick:IBOX(y)] )
    {
        // BUTTON 2 CLICKED
    }
 
    // Check if right button was clicked.
    if ( [glView isObjectClicked:rightButton xClick:IBOX(x) yClick:IBOX(y)])
    {
        // BUTTON 3 CLICKED
    }
}

next previous