📄 baseapplication.cpp
字号:
// Move camera down
mTranslateVector.y = -mMoveScale;
}
/* Move camera forward by keypress. */
if (mInputDevice->isKeyDown(KC_UP))
{
mTranslateVector.z = -mMoveScale;
}
if (mInputDevice->isKeyDown(KC_DOWN))
{
mTranslateVector.z = mMoveScale;
}
if (mInputDevice->isKeyDown(KC_RIGHT))
{
mTranslateVector.x = mMoveScale;
}
if (mInputDevice->isKeyDown(KC_LEFT))
{
mTranslateVector.x = -mMoveScale;
}
if( mInputDevice->isKeyDown( KC_ESCAPE) )
{
return false;
}
// see if switching is on, and you want to toggle
if (mInputTypeSwitchingOn && mInputDevice->isKeyDown(KC_M) && mTimeUntilNextToggle <= 0)
{
switchMouseMode();
mTimeUntilNextToggle = 1;
}
if (mInputTypeSwitchingOn && mInputDevice->isKeyDown(KC_K) && mTimeUntilNextToggle <= 0)
{
// must be going from immediate keyboard to buffered keyboard
switchKeyMode();
mTimeUntilNextToggle = 1;
}
if (mInputDevice->isKeyDown(KC_F) && mTimeUntilNextToggle <= 0)
{
mStatsOn = !mStatsOn;
showDebugOverlay(mStatsOn);
mTimeUntilNextToggle = 1;
}
if (mInputDevice->isKeyDown(KC_T) && mTimeUntilNextToggle <= 0)
{
switch(mFiltering)
{
case TFO_BILINEAR:
mFiltering = TFO_TRILINEAR;
mAniso = 1;
break;
case TFO_TRILINEAR:
mFiltering = TFO_ANISOTROPIC;
mAniso = 8;
break;
case TFO_ANISOTROPIC:
mFiltering = TFO_BILINEAR;
mAniso = 1;
break;
default:
break;
}
MaterialManager::getSingleton().setDefaultTextureFiltering(mFiltering);
MaterialManager::getSingleton().setDefaultAnisotropy(mAniso);
showDebugOverlay(mStatsOn);
mTimeUntilNextToggle = 1;
}
if (mInputDevice->isKeyDown(KC_SYSRQ) && mTimeUntilNextToggle <= 0)
{
std::ostringstream ss;
ss << "screenshot_" << ++mNumScreenShots << ".png";
mWindow->writeContentsToFile(ss.str());
mTimeUntilNextToggle = 0.5;
mDebugText = "Saved: " + ss.str();
}
if (mInputDevice->isKeyDown(KC_R) && mTimeUntilNextToggle <=0)
{
mSceneDetailIndex = (mSceneDetailIndex+1)%3 ;
switch(mSceneDetailIndex) {
case 0 : mCamera->setPolygonMode(PM_SOLID) ; break ;
case 1 : mCamera->setPolygonMode(PM_WIREFRAME) ; break ;
case 2 : mCamera->setPolygonMode(PM_POINTS) ; break ;
}
mTimeUntilNextToggle = 0.5;
}
static bool displayCameraDetails = false;
if (mInputDevice->isKeyDown(KC_P) && mTimeUntilNextToggle <= 0)
{
displayCameraDetails = !displayCameraDetails;
mTimeUntilNextToggle = 0.5;
if (!displayCameraDetails)
mDebugText = "";
}
if (displayCameraDetails)
{
// Print camera details
mDebugText = "P: " + StringConverter::toString(mCamera->getDerivedPosition()) +
" " + "O: " + StringConverter::toString(mCamera->getDerivedOrientation());
}
// Return true to continue rendering
return true;
}
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
bool BaseApplication::processUnbufferedMouseInput(const FrameEvent& evt)
{
using namespace OIS;
// Rotation factors, may not be used if the second mouse button is pressed
// 2nd mouse button - slide, otherwise rotate
const MouseState &ms = mMouse->getMouseState();
if( ms.buttonDown( MB_Right ) )
{
mTranslateVector.x += ms.X.rel * 0.13;
mTranslateVector.y -= ms.Y.rel * 0.13;
}
else
{
mRotX = Degree(-ms.X.rel * 0.13);
mRotY = Degree(-ms.Y.rel * 0.13);
}
return true;
}
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
void BaseApplication::moveCamera()
{
// Make all the changes to the camera
// Note that YAW direction is around a fixed axis (freelook style) rather than a natural YAW (e.g. airplane)
mCamera->yaw(mRotX);
mCamera->pitch(mRotY);
mCamera->moveRelative(mTranslateVector);
}
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
void BaseApplication::showDebugOverlay(bool show)
{
if (mDebugOverlay)
{
if (show)
{
mDebugOverlay->show();
}
else
{
mDebugOverlay->hide();
}
}
}
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
bool BaseApplication::frameStarted(const FrameEvent& evt)
{
if(mWindow->isClosed())
return false;
//Need to capture/update each device
mKeyboard->capture();
mMouse->capture();
mUseBufferedInputMouse = mMouse->buffered();
mUseBufferedInputKeys = mKeyboard->buffered();
if ( !mUseBufferedInputMouse || !mUseBufferedInputKeys)
{
// one of the input modes is immediate, so setup what is needed for immediate mouse/key movement
if (mTimeUntilNextToggle >= 0)
mTimeUntilNextToggle -= evt.timeSinceLastFrame;
// If this is the first frame, pick a speed
if (evt.timeSinceLastFrame == 0)
{
mMoveScale = 1;
mRotScale = 0.1;
}
// Otherwise scale movement units by time passed since last frame
else
{
// Move about 100 units per second,
mMoveScale = mMoveSpeed * evt.timeSinceLastFrame;
// Take about 10 seconds for full rotation
mRotScale = mRotateSpeed * evt.timeSinceLastFrame;
}
mRotX = 0;
mRotY = 0;
mTranslateVector = Vector3::ZERO;
}
if (mUseBufferedInputKeys)
{
// no need to do any processing here, it is handled by event processor and
// you get the results as KeyEvents
}
else
{
if (processUnbufferedKeyInput(evt) == false)
{
return false;
}
}
if (mUseBufferedInputMouse)
{
// no need to do any processing here, it is handled by event processor and
// you get the results as MouseEvents
}
else
{
if (processUnbufferedMouseInput(evt) == false)
{
return false;
}
}
if ( !mUseBufferedInputMouse || !mUseBufferedInputKeys)
{
// one of the input modes is immediate, so update the movement vector
moveCamera();
}
else
{
}
return true;
}
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
bool BaseApplication::frameEnded(const FrameEvent& evt)
{
updateStats();
return true;
}
//-------------------------------------------------------------------------------------
void BaseApplication::switchMouseMode()
{
mUseBufferedInputMouse = !mUseBufferedInputMouse;
mMouse->setBuffered(mUseBufferedInputMouse);
}
//-------------------------------------------------------------------------------------
void BaseApplication::switchKeyMode()
{
mUseBufferedInputKeys = !mUseBufferedInputKeys;
mKeyboard->setBuffered(mUseBufferedInputKeys);
}
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
bool BaseApplication::keyPressed( const OIS::KeyEvent &arg )
{
CEGUI::System *sys = CEGUI::System::getSingletonPtr();
sys->injectKeyDown(arg.key);
sys->injectChar(arg.text);
return true;
}
bool BaseApplication::keyReleased( const OIS::KeyEvent &arg )
{
CEGUI::System::getSingleton().injectKeyUp(arg.key);
if( arg.key == OIS::KC_M )
mMouse->setBuffered( !mMouse->buffered() );
else if( arg.key == OIS::KC_K )
mKeyboard->setBuffered( !mKeyboard->buffered() );
return true;
}
bool BaseApplication::mouseMoved( const OIS::MouseEvent &arg )
{
// CEGUI::System::getSingleton().injectMouseMove(arg.state.X.rel, arg.state.Y.rel);
mSystem->injectMouseMove(arg.state.X.rel, arg.state.Y.rel);
return true;
}
bool BaseApplication::mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{
CEGUI::System::getSingleton().injectMouseButtonDown(convertButton(id));
return true;
}
bool BaseApplication::mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{
CEGUI::System::getSingleton().injectMouseButtonUp(convertButton(id));
return true;
}
//Adjust mouse clipping area
void BaseApplication::windowResized(RenderWindow* rw)
{
unsigned int width, height, depth;
int left, top;
rw->getMetrics(width, height, depth, left, top);
const OIS::MouseState &ms = mMouse->getMouseState();
ms.width = width;
ms.height = height;
}
//Unattach OIS before window shutdown (very important under Linux)
void BaseApplication::windowClosed(RenderWindow* rw)
{
//Only close for window that created OIS (the main window in these demos)
if( rw == mWindow )
{
if( mInputManager )
{
mInputManager->destroyInputObject( mMouse );
mInputManager->destroyInputObject( mKeyboard );
OIS::InputManager::destroyInputSystem(mInputManager);
mInputManager = 0;
}
}
}
CEGUI::MouseButton BaseApplication::convertButton(OIS::MouseButtonID buttonID)
{
switch (buttonID)
{
case OIS::MB_Left:
return CEGUI::LeftButton;
case OIS::MB_Right:
return CEGUI::RightButton;
case OIS::MB_Middle:
return CEGUI::MiddleButton;
default:
return CEGUI::LeftButton;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -