void InputEventHandler::MouseMove(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
{
	int x0 = ea.getXmin();
	int y0 = ea.getYmin();
	int x1 = ea.getXmax();
	int y1 = ea.getYmax();

	int centerX = (x0 + x1) / 2;
	int centerY = (y0 + y1) / 2;

	if (!m_cursorOn) {
		if (!((ea.getX() == centerX) && (ea.getY() == centerY))) {
			m_mouseX0 = centerX;
			m_mouseY0 = centerY;
			m_mouseX1 = ea.getX();
			m_mouseY1 = ea.getY();
			aa.requestWarpPointer (centerX, centerY);
		}
	} else {
		if (m_mouseMoveCount > (m_frameCount - 64)) {
			// track mouse inside frame window
			m_mouseX0 = m_mouseX1;
			m_mouseY0 = m_mouseY1;
			m_mouseX1 = ea.getX();
			m_mouseY1 = ea.getY();
		}
	}
	m_mouseMoveCount = m_frameCount;
}
bool KeyboardFpsManipulator::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
{
    bool result = FirstPersonManipulator::handle(ea, aa);

	bool moved = false;

	double t1;

	osg::Quat q = m_oculusDevice->orientation();
	osg::Vec3 eye;
	osg::Vec3 center;
	osg::Vec3 up;

	if (m_viewer->getNumSlaves() > 0)
		m_viewer->getSlave(0)._viewOffset.getLookAt(eye, center, up);

	
	//std::cout << eye.x() << ", " << eye.y() << ", " << eye.z() << " : ";
	//std::cout << center.x() << ", " << center.y() << ", " << center.z() << " : ";
	//std::cout << up.x() << ", " << up.y() << ", " << up.z() << std::endl;


	osg::Vec3 f = center - eye;
	osg::Vec3 r = f ^ up;

	osg::Vec3 forward;
	osg::Vec3 right;

	forward.set(f.x(), -f.z(), -f.y());
	right.set(r.x(), -r.z(), -r.y());
	//std::cout << forward.x() << ", " << forward.y() << ", " << forward.z() << std::endl;
	std::cout << right.x() << ", " << right.y() << ", " << right.z() << std::endl;

	/*
	osg::Quat xRot;
	xRot.makeRotate(-osg::PI_2, osg::X_AXIS);

	osg::Quat yRot;
	yRot.makeRotate(osg::PI_2, osg::Y_AXIS);

	q = q * xRot * yRot;
	*/



    switch (ea.getEventType())
    {
    case osgGA::GUIEventAdapter::FRAME:
        //std::cout << "FRAME" << std::endl;

        t1 = ea.getTime();
        if( m_t0 == 0.0 )
        {
            m_t0 = ea.getTime();
            m_dt = 0.0;
        }
        else
        {
            m_dt = t1 - m_t0;
            m_t0 = t1;
        }

		if (m_moveForward)
			_eye -= forward * 12.0 * m_dt;
		if (m_moveBackward)
			_eye += forward * 12.0 * m_dt;

		if (m_moveLeft)
			_eye += right *  12.0 * m_dt;
		if (m_moveRight)
			_eye -= right *  12.0 * m_dt;

		return false;
        break;
    case osgGA::GUIEventAdapter::KEYDOWN:
        switch (ea.getKey())
        {
        case 'w':
			m_moveForward = true;
            return false;
            break;
		case 'a':
			m_moveLeft = true;
			return false;
			break;
		case 'd':
			m_moveRight = true;
			return false;
			break;
		case 's':
			m_moveBackward = true;
			return false;
			break;
		default:
			break;
        }
		this->flushMouseEventStack();
		break;
    case osgGA::GUIEventAdapter::KEYUP:
        switch (ea.getKey())
        {
        default:
			m_moveForward = false;
			m_moveBackward = false;
			m_moveLeft = false;
			m_moveRight = false;
			break;
        }
		this->flushMouseEventStack();
		break;
	case osgGA::GUIEventAdapter::MOVE:
		moved = true;
		break;
	}

	if (!moved)
	{
		int _mouseCenterX = (ea.getXmin() + ea.getXmax()) / 2.0f;
		int _mouseCenterY = (ea.getYmin() + ea.getYmax()) / 2.0f;
		aa.requestWarpPointer(_mouseCenterX, _mouseCenterY);
		this->flushMouseEventStack();
	}
    return result;
}
Beispiel #3
0
    virtual bool handle (const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa, osg::Object *, osg::NodeVisitor *)
    {
        if (ea.getEventType() != osgGA::GUIEventAdapter::FRAME) {
            std::cout << ea.getTime() << ": ";
            switch(ea.getEventType()) {
                case osgGA::GUIEventAdapter::PUSH: std::cout << "PUSH"; break;
                case osgGA::GUIEventAdapter::DRAG:  std::cout << "DRAG"; break;
                case osgGA::GUIEventAdapter::MOVE:  std::cout << "MOVE"; break;
                case osgGA::GUIEventAdapter::RELEASE:  std::cout << "RELEASE"; break;
                default: std::cout << ea.getEventType();
            }
            std::cout << std::endl;
        }
        
        switch(ea.getEventType())
        {
            case osgGA::GUIEventAdapter::FRAME:
                if (_cleanupOnNextFrame) {
                    cleanup(0);
                    _cleanupOnNextFrame = false;
                }
                break;
            
            case osgGA::GUIEventAdapter::PUSH:
            case osgGA::GUIEventAdapter::DRAG:
            case osgGA::GUIEventAdapter::RELEASE:
                {
                    // is this a multi-touch event?
                    if (!ea.isMultiTouchEvent())
                        return false;
                    
                    unsigned int j(0);
                    
                    // iterate over all touch-points and update the geometry
                    unsigned num_touch_ended(0);
                    
                    for(osgGA::GUIEventAdapter::TouchData::iterator i = ea.getTouchData()->begin(); i != ea.getTouchData()->end(); ++i, ++j)
                    {
                        const osgGA::GUIEventAdapter::TouchData::TouchPoint& tp = (*i);
                        float x = ea.getTouchPointNormalizedX(j);
                        float y = ea.getTouchPointNormalizedY(j);
                        
                        // std::cout << j << ": " << tp.x << "/" << tp.y <<" "<< x << " " << y << " " << _w << " " << _h << std::endl;
                        
                        _mats[j]->setMatrix(osg::Matrix::translate((1+x) * 0.5 * _w, (1+y) * 0.5 * _h, 0));
                        _mats[j]->setNodeMask(0xffff);
                        
                        std::ostringstream ss;
                        ss << "Touch " << tp.id;
                        _texts[j]->setText(ss.str());
                        
                        switch (tp.phase) 
                        {
                            case osgGA::GUIEventAdapter::TOUCH_BEGAN:
                                    _drawables[j]->setColor(osg::Vec4(0,1,0,1));
                                    break;
                                    
                            case osgGA::GUIEventAdapter::TOUCH_MOVED:
                                    _drawables[j]->setColor(osg::Vec4(1,1,1,1));
                                    break;
                                    
                            case osgGA::GUIEventAdapter::TOUCH_ENDED:
                                    _drawables[j]->setColor(osg::Vec4(1,0,0,1));
                                    ++num_touch_ended;
                                    break;
                                    
                            case osgGA::GUIEventAdapter::TOUCH_STATIONERY:
                                    _drawables[j]->setColor(osg::Vec4(0.8,0.8,0.8,1));
                                    break;
                                    
                            default:
                                break;

                        }
                    }
                    
                    // hide unused geometry
                    cleanup(j);
                    
                    //check if all touches ended
                    if ((ea.getTouchData()->getNumTouchPoints() > 0) && (ea.getTouchData()->getNumTouchPoints() == num_touch_ended))
                    {
                        _cleanupOnNextFrame = true;
                    }
                    
                    // reposition mouse-pointer
                    aa.requestWarpPointer((ea.getWindowX() + ea.getWindowWidth()) / 2.0, (ea.getWindowY() + ea.getWindowHeight()) / 2.0);
                }
                break;
                
            default:
                break;
        }
        
        return false;
    }