Changeset 12982 for OpenSceneGraph

Show
Ignore:
Timestamp:
02/10/12 18:24:08 (3 months ago)
Author:
robert
Message:

From Stephan Huber, "
Here's another small submission for IOS, which adds unique ids to the
touchpoints, so the ids stay the same during a touch-sequence.

(and some minor code enhancements)"

Location:
OpenSceneGraph/trunk/src/osgViewer
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/src/osgViewer/GraphicsWindowIOS.mm

    r12870 r12982  
    8686// ---------------------------------------------------------------------------------------------------------- 
    8787 
     88typedef std::map<void*, unsigned int> TouchPointsIdMapping; 
     89 
    8890@interface GraphicsWindowIOSGLView : UIView 
    8991{ 
     
    108110        // for multisampled antialiased rendering 
    109111        GLuint _msaaFramebuffer, _msaaRenderBuffer, _msaaDepthBuffer; 
     112         
     113        TouchPointsIdMapping* _touchPointsIdMapping; 
     114        unsigned int _lastTouchPointId; 
     115 
     116 
    110117     
    111118} 
     
    156163} 
    157164 
     165 
     166- (unsigned int)computeTouchId: (UITouch*) touch  
     167{ 
     168    unsigned int result(0); 
     169     
     170    if (!_touchPointsIdMapping) { 
     171        _lastTouchPointId = 0; 
     172        _touchPointsIdMapping = new TouchPointsIdMapping(); 
     173    } 
     174     
     175    switch([touch phase]) 
     176    { 
     177     
     178        case UITouchPhaseBegan: 
     179            { 
     180                TouchPointsIdMapping::iterator itr = _touchPointsIdMapping->find(touch); 
     181                // std::cout << "new: " << touch << " num: " << _touchPointsIdMapping->size() << " found: " << (itr != _touchPointsIdMapping->end()) << std::endl; 
     182                  
     183                if (itr == _touchPointsIdMapping->end())  
     184                { 
     185                    (*_touchPointsIdMapping)[touch] = result = _lastTouchPointId; 
     186                    _lastTouchPointId++; 
     187                    break; 
     188                } 
     189                
     190            } 
     191            // missing "break" by intention! 
     192         
     193        case UITouchPhaseMoved: 
     194        case UITouchPhaseStationary: 
     195            { 
     196                result = (*_touchPointsIdMapping)[touch]; 
     197            } 
     198            break; 
     199        
     200        case UITouchPhaseEnded: 
     201        case UITouchPhaseCancelled: 
     202            { 
     203                TouchPointsIdMapping::iterator itr = _touchPointsIdMapping->find(touch); 
     204                // std::cout<< "remove: " << touch << " num: " << _touchPointsIdMapping->size() << " found: " << (itr != _touchPointsIdMapping->end()) << std::endl; 
     205                 
     206                if (itr != _touchPointsIdMapping->end()) { 
     207                    result = itr->second; 
     208                    _touchPointsIdMapping->erase(itr); 
     209                } 
     210                if(_touchPointsIdMapping->size() == 0) { 
     211                    _lastTouchPointId = 0; 
     212                } 
     213                // std::cout<< "remove: " << touch << " num: " << _touchPointsIdMapping->size() << std::endl; 
     214            } 
     215            break; 
     216             
     217        default: 
     218            break; 
     219    } 
     220         
     221    return result; 
     222} 
     223 
     224 
    158225- (osg::Vec2) convertPointToPixel: (osg::Vec2) point 
    159226{ 
     
    161228    float scale = 1.0f; 
    162229     
    163 #ifdef __IPHONE_4_0 && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0) 
     230#if defined(__IPHONE_4_0) && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0) 
    164231    scale = self.contentScaleFactor; 
    165232#endif 
     
    171238{ 
    172239    _win = win; 
     240    _touchPointsIdMapping = new TouchPointsIdMapping(); 
     241    _lastTouchPointId = 0; 
    173242} 
    174243 
     
    223292{ 
    224293    OSG_INFO << "GraphicsWindowIOSGLView::dealloc" << std::endl; 
    225      
     294    if(_touchPointsIdMapping)  
     295        delete _touchPointsIdMapping; 
     296    _touchPointsIdMapping = NULL; 
    226297    [super dealloc]; 
    227298} 
     
    284355    //MSAA only available for >= 4.0 sdk 
    285356     
    286 #ifdef __IPHONE_4_0 && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0) 
     357#if defined(__IPHONE_4_0) && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0) 
    287358     
    288359    if(_win->getTraits()->sampleBuffers > 0)  
     
    365436 
    366437 
    367 #ifdef __IPHONE_4_0 && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0)     
     438#if defined(__IPHONE_4_0) && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0)     
    368439    if(_msaaFramebuffer)  
    369440    { 
     
    390461    glBindFramebufferOES(GL_FRAMEBUFFER_OES, _viewFramebuffer); 
    391462     
    392 #ifdef __IPHONE_4_0 && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0) 
     463#if defined(__IPHONE_4_0) && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0) 
    393464    if (_msaaFramebuffer) 
    394465        glBindFramebufferOES(GL_FRAMEBUFFER_OES, _msaaFramebuffer);; 
     
    404475    glBindFramebufferOES(GL_FRAMEBUFFER_OES, _viewFramebuffer); 
    405476     
    406 #ifdef __IPHONE_4_0 && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0) 
     477#if defined(__IPHONE_4_0) && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0) 
    407478    if (_msaaFramebuffer) 
    408479        glBindFramebufferOES(GL_READ_FRAMEBUFFER_APPLE, _msaaFramebuffer); 
     
    441512        CGPoint pos = [touch locationInView:touch.view]; 
    442513        osg::Vec2 pixelPos = [self convertPointToPixel: osg::Vec2(pos.x,pos.y)]; 
     514        unsigned int touch_id = [self computeTouchId: touch]; 
    443515         
    444516        if (!osg_event) { 
    445             osg_event = _win->getEventQueue()->touchBegan(i, [self convertTouchPhase: [touch phase]], pixelPos.x(), pixelPos.y()); 
     517            osg_event = _win->getEventQueue()->touchBegan(touch_id, [self convertTouchPhase: [touch phase]], pixelPos.x(), pixelPos.y()); 
    446518        } else { 
    447             osg_event->addTouchPoint(i, [self convertTouchPhase: [touch phase]], pixelPos.x(), pixelPos.y()); 
     519            osg_event->addTouchPoint(touch_id, [self convertTouchPhase: [touch phase]], pixelPos.x(), pixelPos.y()); 
    448520        } 
    449521    } 
     
    462534        CGPoint pos = [touch locationInView:touch.view]; 
    463535        osg::Vec2 pixelPos = [self convertPointToPixel: osg::Vec2(pos.x,pos.y)]; 
    464          
     536        unsigned int touch_id = [self computeTouchId: touch]; 
     537 
    465538        if (!osg_event) { 
    466             osg_event = _win->getEventQueue()->touchMoved(i, [self convertTouchPhase: [touch phase]], pixelPos.x(), pixelPos.y()); 
     539            osg_event = _win->getEventQueue()->touchMoved(touch_id, [self convertTouchPhase: [touch phase]], pixelPos.x(), pixelPos.y()); 
    467540        } else { 
    468             osg_event->addTouchPoint(i, [self convertTouchPhase: [touch phase]], pixelPos.x(), pixelPos.y()); 
     541            osg_event->addTouchPoint(touch_id, [self convertTouchPhase: [touch phase]], pixelPos.x(), pixelPos.y()); 
    469542        } 
    470543 
     
    485558        CGPoint pos = [touch locationInView:touch.view]; 
    486559        osg::Vec2 pixelPos = [self convertPointToPixel: osg::Vec2(pos.x,pos.y)]; 
    487          
     560        unsigned int touch_id = [self computeTouchId: touch]; 
    488561        if (!osg_event) { 
    489             osg_event = _win->getEventQueue()->touchEnded(i, [self convertTouchPhase: [touch phase]], pixelPos.x(), pixelPos.y(), [touch tapCount]); 
     562            osg_event = _win->getEventQueue()->touchEnded(touch_id, [self convertTouchPhase: [touch phase]], pixelPos.x(), pixelPos.y(), [touch tapCount]); 
    490563        } else { 
    491             osg_event->addTouchPoint(i, [self convertTouchPhase: [touch phase]], pixelPos.x(), pixelPos.y(), [touch tapCount]); 
     564            osg_event->addTouchPoint(touch_id, [self convertTouchPhase: [touch phase]], pixelPos.x(), pixelPos.y(), [touch tapCount]); 
    492565        } 
    493566 
     
    495568} 
    496569 
     570-(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event  
     571{ 
     572    [self touchesEnded: touches withEvent:event]; 
     573} 
    497574 
    498575@end 
     
    731808    //Apply our content scale factor to our view, this is what converts the views points 
    732809    //size to our desired context size. 
    733 #ifdef __IPHONE_4_0 && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0) 
     810#if defined(__IPHONE_4_0) && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0) 
    734811    theView.contentScaleFactor = _viewContentScaleFactor; 
    735812#endif     
  • OpenSceneGraph/trunk/src/osgViewer/IOSUtils.mm

    r12520 r12982  
    7373        float scale = 1.0f; 
    7474         
    75         #ifdef __IPHONE_4_0 && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0) 
     75        #if defined(__IPHONE_4_0) && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0) 
    7676            scale = [screen scale]; 
    7777        #endif 
     
    124124         
    125125        float scale = 1.0f; 
    126         #ifdef __IPHONE_4_0 && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0) 
     126        #if defined(__IPHONE_4_0) && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0) 
    127127            scale = [screen scale]; 
    128128        #endif 
     
    257257    { 
    258258        scaleFactor = 1.0f; 
    259 #ifdef __IPHONE_4_0 && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0) 
     259#if defined(__IPHONE_4_0) && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0) 
    260260        CGFloat scale = [screen scale]; 
    261261        scaleFactor = scale;