Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle iOS touch to emulate mouse for C++ input library

I've got existing cross-platform C++ code to handle mouse-input, with methods like:

onMouseDown(x,y,button)
onMouseUp(x,y,button)
onMouseMove(x,y,buttons)

I'm porting functionality into a C++ iOS app... using the minimum Objective-C as required. I'd like to handle single-touch gestures to emulate mouse functionality so I can pass parameters into existing methods (as well as adding multitouch).

What would code look like to do this, ideally as a minimal sample app - it's mainly the OBJ-C/C++ interaction that really confuses me?

like image 989
Mr. Boy Avatar asked Dec 09 '22 19:12

Mr. Boy


2 Answers

Here's how I pass multi-touch to C++ code for an OpenGL program (with 1 or 2 fingers):

// Set this in your ViewController's init code
[self setMultipleTouchEnabled:YES];

// Implement these in ViewController
int touchCount = 0;
UITouch* touch[2];

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{ 
    NSArray* array = [touches allObjects];  
    UITouch* touchTemp; 
    int t;
    int touchedPixel[2];

    for(int i = 0; i < [array count]; ++i){
        touchTemp = [array objectAtIndex:i];

        if(touchCount >= 2)
            return;

        if(touch[0] == NULL)    
            t = 0;
        else
            t = 1;      
        touch[t] = touchTemp;

        CGPoint touchLoc = [touch[t] locationInView:(EAGLView *)self.view]; 
        ++touchCount;

        touchedPixel[X] = touchLoc.x;
        touchedPixel[Y] = touchLoc.y;
        mainScreen->push(t, touchedPixel);  // mainScreen is a C++ object for GL drawing.
    }
}


- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event{
    NSArray* array = [touches allObjects];
    UITouch* touchTemp; 
    int t;
    int touchedPixel[2];

    for(int i = 0; i < [array count]; ++i){
        touchTemp = [array objectAtIndex:i];
        for(t = 0; t < 2; ++t){         // Find the matching touch in the array
            if(touchTemp == touch[t])
                break;
        }       
        if(t == 2)                      // Return if touch was not found
            continue;

        CGPoint touchLoc = [touch[t] locationInView:(EAGLView *)self.view]; 

        touchedPixel[X] = touchLoc.x;
        touchedPixel[Y] = touchLoc.y;
        mainScreen->move(t, touchedPixel);
    }
}


- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event{ 
    NSArray* array = [touches allObjects];
    UITouch* touchTemp; 
    int t;
    int touchedPixel[2];

    for(int i = 0; i < [array count]; ++i){
        touchTemp = [array objectAtIndex:i];
        for(t = 0; t < 2; ++t){         // Find the matching touch in the array
            if(touchTemp == touch[t])
                break;
        }       
        if(t == 2)                      // Return if touch was not found
            continue;               

        CGPoint touchLoc = [touch[t] locationInView:(EAGLView *)self.view];

        touch[t] = NULL;
        --touchCount;

        touchedPixel[X] = touchLoc.x;
        touchedPixel[Y] = touchLoc.y;
        mainScreen->release(t, touchedPixel);
    }
}

- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event{
    printf("Touch cancelled\n");
    [self touchesEnded:touches withEvent: event];
}
like image 85
Jonathan Chandler Avatar answered Dec 11 '22 10:12

Jonathan Chandler


for this situation I would implement touchesBegan, moved andEnded and forward the call to the c++ mouseDown/Moved/Up code

sample of what I would do: assumes the code is in the app's viewController!

static UITouch *_trackedTouch = nil;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    assert(!_trackedTouch && "no multitouch");

    _trackedTouch = [touches anyObject];
    CGPoint pt = [_trackedTouch locationInView:self.view];
    mouseDown(pt.x,pt.y,0);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    assert(_trackedTouch && "no touch began");

    if(![touches containsObject:_trackedTouch])
        return;

    CGPoint pt = [_trackedTouch locationInView:self.view];
    mouseMoved(pt.x,pt.y,0);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    assert(_trackedTouch && "no touch began");

    if(![touches containsObject:_trackedTouch])
        return;

    CGPoint pt = [_trackedTouch locationInView:self.view];
    mouseUp(pt.x,pt.y,0);
}

about books mhm... maybe : https://stackoverflow.com/questions/5308106/book-recommendations-for-objective-c-for-ios-development (but the arent C++ biased... objC is based on C. C++ is just something you can use in addition to ObjC but.. knowing C++ only provides limited help with learning ObjC)

like image 30
Daij-Djan Avatar answered Dec 11 '22 10:12

Daij-Djan