bool InputSample::mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
{
    _mousePoint.set(x, y);
    _mouseString.clear();

    switch (evt)
    {
    case Mouse::MOUSE_PRESS_LEFT_BUTTON:
        _mouseString.append("MOUSE_PRESS_LEFT_BUTTON");
        break;
    case Mouse::MOUSE_RELEASE_LEFT_BUTTON:
        _mouseString.append("MOUSE_RELEASE_LEFT_BUTTON");
        break;
    case Mouse::MOUSE_PRESS_MIDDLE_BUTTON:
        _mouseString.append("MOUSE_PRESS_MIDDLE_BUTTON");
        break;
    case Mouse::MOUSE_RELEASE_MIDDLE_BUTTON:
        _mouseString.append("MOUSE_RELEASE_MIDDLE_BUTTON");
        break;
    case Mouse::MOUSE_PRESS_RIGHT_BUTTON:
        _mouseString.append("MOUSE_PRESS_RIGHT_BUTTON");
        break;
    case Mouse::MOUSE_RELEASE_RIGHT_BUTTON:
        _mouseString.append("MOUSE_RELEASE_RIGHT_BUTTON");
        break;
    case Mouse::MOUSE_MOVE:
        _mouseString.append("MOUSE_MOVE");
        if (isMouseCaptured())
        {
            // Control crosshair from captured mouse
            _crosshairDstRect.setPosition(_crosshairDstRect.x + x, _crosshairDstRect.y + y);

            // Use screen limits to clamp the crosshair position
            Vector2 pos(_crosshairDstRect.x, _crosshairDstRect.y);
            pos.clamp(_crosshairLowerLimit, _crosshairUpperLimit);
            _crosshairDstRect.setPosition(pos.x, pos.y);
        }
        break;
    case Mouse::MOUSE_WHEEL:
        _mouseString.append("MOUSE_WHEEL");
        _mouseWheelPoint.x = x;
        _mouseWheelPoint.y = y;
        _mouseWheel = wheelDelta;
        break;
    }
    return false;
}
void InputSample::setCaptured(bool captured)
{
    setMouseCaptured(captured);

    if (!captured || isMouseCaptured())
    {
        _inputSampleControls->getControl("showKeyboardButton")->setVisible(!captured);
        _inputSampleControls->getControl("captureMouseButton")->setVisible(!captured);
        _inputSampleControls->getControl("restoreMouseLabel")->setVisible(captured);
    }

    if (captured)
    {
        _crosshairDstRect.setPosition(
            (float)getWidth()/2.0f + _crosshairLowerLimit.x,
            (float)getHeight()/2.0f + _crosshairLowerLimit.y);
    }
}
void InputSample::render(float elapsedTime)
{
    // Clear the color and depth buffers
    clear(CLEAR_COLOR_DEPTH, Vector4::zero(), 1.0f, 0);

    _inputSampleControls->draw();

    // Draw text
    unsigned int fontSize = 18;
    Vector4 fontColor(1.0f, 1.0f, 1.0f, 1.0f);
    unsigned int width, height;
    char buffer[50];

    _font->start();

    if (isMouseCaptured())
    {
        // Draw crosshair at current offest w.r.t. center of screen
        _crosshair->start();
        _crosshair->draw(_crosshairDstRect, _crosshairSrcRect);
        _crosshair->finish();
    }
    else
    {
        for (std::list<TouchPoint>::const_iterator it = _touchPoints.begin(); it != _touchPoints.end(); ++it)
        {
            sprintf(buffer, "T_%u(%d,%d)", it->_id, (int)it->_coord.x, (int)it->_coord.y);
            _font->measureText(buffer, fontSize, &width, &height);
            int x = it->_coord.x - (int)(width>>1);
            int y = it->_coord.y - (int)(height>>1);
            _font->drawText(buffer, x, y, fontColor, fontSize);
        }

        // Mouse
        sprintf(buffer, "M(%d,%d)", (int)_mousePoint.x, (int)_mousePoint.y);
        _font->measureText(buffer, fontSize, &width, &height);
        int x = _mousePoint.x - (int)(width>>1);
        int y = _mousePoint.y - (int)(height>>1);
        _font->drawText(buffer, x, y, fontColor, fontSize);
        if (!_keyboardState && _mouseString.length() > 0)
        {
            int y = getHeight() - fontSize;
            _font->drawText(_mouseString.c_str(), 0, y, fontColor, fontSize);
        }
        if (_mouseWheel)
        {
            sprintf(buffer, "%d", _mouseWheel);
            _font->measureText(buffer, fontSize, &width, &height);
            int x = _mouseWheelPoint.x - (int)(width>>1);
            int y = _mouseWheelPoint.y + 4;
            _font->drawText(buffer, x, y, fontColor, fontSize);
        }
    }

    // Pressed keys
    if (_keyboardString.length() > 0)
    {
        _font->drawText(_keyboardString.c_str(), 0, 0, fontColor, fontSize);
    }

    // Printable symbols typed
    if (_symbolsString.length() > 0)
    {
        _font->drawText(_symbolsString.c_str(), 0, 18, fontColor, fontSize);
    }

    // Held keys
    if (!_downKeys.empty())
    {
        std::string displayKeys;
        for (std::set<int>::const_iterator i = _downKeys.begin(); i != _downKeys.end(); ++i)
        {
            const char* str = keyString(*i);
            displayKeys.append(str);
        }
        if (!displayKeys.empty())
        {
            _font->measureText(displayKeys.c_str(), 18, &width, &height);
            int x = Game::getInstance()->getWidth() - width;
            int y = 0;
            _font->drawText(displayKeys.c_str(), x, y, fontColor, fontSize);
        }
    }

    // Draw the accelerometer values in the bottom right corner.
    static float pitch, roll;
    static float accelerometerDrawRate = 1000.0f;
    accelerometerDrawRate += elapsedTime;
    if (accelerometerDrawRate > 100.0f)
    {
        accelerometerDrawRate = 0.0f;
        getAccelerometerValues(&pitch, &roll);
    }
    if (hasAccelerometer() && !_keyboardState)
    {
        _formNode->getDrawable()->draw();

        sprintf(buffer, "Pitch: %f   Roll: %f", pitch, roll);
        _font->measureText(buffer, 18, &width, &height);
        _font->drawText(buffer, getWidth() - width, getHeight() - height, fontColor, fontSize);
    }
    _font->finish();
}
예제 #4
0
///////////////////////////////////////////////////////////
//
// End the cursor definition
//
///////////////////////////////////////////////////////////
void view::endCursorDef(imbxInt32 cursorHotSpotX, imbxInt32 cursorHotSpotY)
{
	// Remove the current cursor's lines
	///////////////////////////////////////////////////////////
	invalidateLines(&m_cursorLines);

	// Copy the temporary lines into cursor's lines
	///////////////////////////////////////////////////////////
	m_cursorLines.clear();
	for(tCursorLinesList::iterator copyLines = m_tempCursorLines.begin(); copyLines != m_tempCursorLines.end(); ++copyLines)
	{
		m_cursorLines.push_back(*copyLines);
	}
	m_tempCursorLines.clear();


	// Do we have to scroll the window because the hotspot is
	//  outside the visible area?
	///////////////////////////////////////////////////////////
	if(!isMouseCaptured() || m_originalImage == 0)
	{
		invalidateLines(&m_cursorLines);
		return;
	}

	// Convert the hotspot into window's coordinates
	///////////////////////////////////////////////////////////
	imbxInt32 scrollX, scrollY;
	getScrollPosition(&scrollX, &scrollY);

	imbxUint32 imageSizeX, imageSizeY;
	m_originalImage->getSize(&imageSizeX, &imageSizeY);

	imbxInt32 windowHotSpotX = cursorHotSpotX * (m_rightPosition - m_leftPosition) / imageSizeX +m_leftPosition - scrollX;
	imbxInt32 windowHotSpotY = cursorHotSpotY * (m_bottomPosition - m_topPosition) / imageSizeY +m_topPosition - scrollY;

	// Find the amount of scroll to execute
	///////////////////////////////////////////////////////////
	imbxUint32 windowSizeX, windowSizeY;
	getWindowSize(&windowSizeX, &windowSizeY);

	imbxInt32 limitX = windowSizeX / 10;
	imbxInt32 limitY = windowSizeY / 10;

	imbxInt32 executeScrollX = 0;
	imbxInt32 executeScrollY = 0;

	if(windowHotSpotX < limitX)
	{
		executeScrollX = windowHotSpotX - limitX;
	}
	if(windowHotSpotY < limitY)
	{
		executeScrollY = windowHotSpotY - limitY;
	}
	if(windowHotSpotX > ((imbxInt32)windowSizeX - limitX) )
	{
		executeScrollX = windowHotSpotX - (imbxInt32)windowSizeX + limitX;
	}
	if(windowHotSpotY > ((imbxInt32)windowSizeY - limitY) )
	{
		executeScrollY = windowHotSpotY - (imbxInt32)windowSizeY + limitY;
	}

	if(executeScrollX != 0 && executeScrollY != 0)
	{
		updateWindow();
		setScrollPosition(scrollX + executeScrollX, scrollY + executeScrollY);
	}

	invalidateLines(&m_cursorLines);
}