コード例 #1
0
	void Primitive2D::handleEvent(SoHandleEventAction* action)
	{
		if (!eventsHandles)
		{
			SoSeparator::handleEvent(action);
			return;
		}

		eventAction = action;
		const SoEvent* event = action->getEvent();

		SbViewportRegion viewRegion = sketcherView.getViewer().getViewportRegion();
		SbVec2f normalizedPos(event->getNormalizedPosition(viewRegion));

		SbViewVolume volume;
		volume = SoViewVolumeElement::get(eventAction->getState());
		SbVec3f curPos(sketcherView.projectPoint(normalizedPos, volume));

        if (event->getTypeId().isDerivedFrom(SoKeyboardEvent::getClassTypeId()))
		{
            const SoKeyboardEvent* keyEvent = static_cast<const SoKeyboardEvent*>(event);

			int key = keyEvent->getKey();

			if (key == SoKeyboardEvent::RETURN)
				emitCreated();
			else if (key == SoKeyboardEvent::ESCAPE)
				emitCreationCanceled();

			if (keyPressed(key))
				action->setHandled();
        }
        else if (event->getTypeId().isDerivedFrom(SoMouseButtonEvent::getClassTypeId()))
		{
            const SoMouseButtonEvent* mouseEvent = static_cast<const SoMouseButtonEvent*>(event);
            int button = mouseEvent->getButton();
            SbBool press = mouseEvent->getState() == SoButtonEvent::DOWN ? TRUE : FALSE;

            if (mouseButtonPressed(button, press, curPos))
                action->setHandled();
        }
        else if (event->getTypeId().isDerivedFrom(SoLocation2Event::getClassTypeId()))
		{
            if (mouseMove(curPos))
                action->setHandled();
        }
	}
コード例 #2
0
void GuiVehicleControl::onMouseMove(const GuiEvent &evt)
{
	// Contribute mouse position to MoveManager::*Speed variables
	// Dead zone (mMouseDeadZone), with the rest contributing
	// mContributePitch, mContributeYaw, and mContriuteRoll.
	// (Mouse position normalized to 0.0->1.0)

	  // Calculate useful vectors (squared)
	Point2I realPos = globalToLocalCoord(evt.mousePoint);
	Point2F normalizedPos((F32)realPos.x / (F32)mBounds.extent.x,
                         (F32)realPos.y / (F32)mBounds.extent.y);
	Point2F centerPos(normalizedPos.x - 0.5, normalizedPos.y - 0.5);
   
	if (mCurveMode == 1) {
		normalizedPos.x = mCurveCoeff * (F32)(normalizedPos.x*normalizedPos.x);
		normalizedPos.y = mCurveCoeff * (F32)(normalizedPos.y*normalizedPos.y);
	} else if (mCurveMode == 2) {
		normalizedPos.x = mCurveCoeff * mExp((F32)mLog((F32)normalizedPos.x));
		normalizedPos.y = mCurveCoeff * mExp((F32)mLog((F32)normalizedPos.y));
	} else {
		normalizedPos *= mCurveCoeff;
	}
   
	//Con::printf("real =[%d,%d], normal=[%f,%f], center=[%f,%f]", realPos.x, realPos.y, normalizedPos.x, normalizedPos.y, centerPos.x, centerPos.y);

	// Send directly to *Speed variables
	// TODO: factor in sensitivity?
	// centerPos is used, expressed in the range -0.5,0.5

	// Pitch goes from up -> down,but needs to be converted to 0-1 range
	MoveManager::mPitchSpeed = centerPos.y * 2;
   	
	// Calculate YAW - must be in dead zone
	F32 absX = mFabs(centerPos.x);
	if (absX < mMouseDeadZone) {
		// Calculate in range (0.0-1.0) in dead zone
		MoveManager::mYawSpeed = absX * (1.0 / mMouseDeadZone);
		if (centerPos.x < 0.0f) MoveManager::mYawSpeed = -MoveManager::mYawSpeed;
	} else {
		MoveManager::mYawSpeed = 0;
	}
   	
	// Calculate ROLL
	if (absX > mMouseDeadZone)
	{
		// Negate dead zone and determine percentage outside dead zone
		// (0.5-mMouseDeadZone)
		MoveManager::mRollSpeed = (absX - mMouseDeadZone) * ( 1.0 / (0.5-mMouseDeadZone) );
		if (centerPos.x < 0.0f) MoveManager::mRollSpeed = -MoveManager::mRollSpeed;
	} else {
		MoveManager::mRollSpeed = 0;
	}

	// Calculate X and Y axis
	if (!mUseRightAxis) {
		MoveManager::mXAxis_L = centerPos.x;
		MoveManager::mYAxis_L = centerPos.y;
	} else {
		MoveManager::mXAxis_R = centerPos.x;
		MoveManager::mYAxis_R = centerPos.y;
	}