📄 guiobjectview.cc
字号:
//guiObjectView.cc
/*/////////////////////////////////////////////////////////////////
李亦
liease@163.com 4040719
2006-7-26
/*/////////////////////////////////////////////////////////////////
#include "gui/core/guiCanvas.h"
#include "gui/game/guiObjectView.h"
#include "console/consoleTypes.h"
#include "ts/tsSkinMan.h"
#include "game/player.h"
#include "rpg/gobjects/GPlayer.h"
static const F32 MaxOrbitDist = 50.0f;
static const S32 MaxAnimations = 6;
IMPLEMENT_CONOBJECT( GuiObjectView );
GuiObjectView::GuiObjectView()
: GuiTSCtrl()
{
m_bAllowRot = true;
m_bAllowZoom = true;
mActive = true;
mMouseState = None;
// Can zoom and spin by default
mZoom = true;
mSpin = true;
mLastMousePoint.set( 0, 0 );
//m_pCursorName = StringTable->insert("HandCursor");
m_pCursor = 0;
m_pGPlayer = NULL;
m_arSequences[GOV_NORMAL] = StringTable->insert("idle");
m_arSequences[GOV_HILIGHT] = StringTable->insert("1hwalk");
m_arSequences[GOV_SELECTED]= StringTable->insert("1hrun");
}
GuiObjectView::~GuiObjectView()
{
//mMeshObjects.Clear();
}
//void GuiObjectView::inspectPostApply()
//{
// //记得调用Parent的同样方法,否则会出错的呀
// Parent::inspectPostApply();
// //m_pCursor = dynamic_cast<GuiCursor*>(Sim::findObject(m_pCursorName));
//}
void GuiObjectView::getCursor(GuiCursor *&cursor, bool &visible, const GuiEvent &event)
{
cursor = m_pCursor;
visible = true;
event;
}
void GuiObjectView::consoleInit()
{
}
void GuiObjectView::initPersistFields()
{
Parent::initPersistFields();
addField("allowRot", TypeBool,Offset(m_bAllowRot,GuiObjectView));
addField("allowzoom", TypeBool,Offset(m_bAllowZoom,GuiObjectView));
addField("cursor", TypeGuiCursorPtr, Offset(m_pCursor, GuiObjectView));
addField("arSequences", TypeString, Offset(m_arSequences, GuiObjectView),GOV_MAX);
}
bool GuiObjectView::onWake()
{
if ( !Parent::onWake() )
return( false );
mCameraMatrix.identity();
mCameraRot.set( 0, 0, 3.9 );
mCameraPos.set( 0, 1.75, 1.25 );
mCameraMatrix.setColumn( 3, mCameraPos );
mOrbitPos.set( 0, 0, 0 );
mOrbitDist = 9.5f;
//if(m_pCursorName)
if(m_pCursor == NULL)
m_pCursor = dynamic_cast<GuiCursor*>(Sim::findObject(CURSOR_HAND_TXT));
return( true );
}
// Function to determine the ways the mouse can interact with the gui object
void GuiObjectView::setMouseOptions( bool zoom, bool spin )
{
mZoom = zoom;
mSpin = spin;
}
void GuiObjectView::onMouseEnter( const GuiEvent &event )
{
Con::executef(this, 1, "onMouseEnter");
}
void GuiObjectView::onMouseLeave( const GuiEvent &event )
{
Con::executef(this, 1, "onMouseLeave");
}
// Mouse is down, lock mouse input and get the mouse pointer coordinates and set mode to spin
void GuiObjectView::onMouseDown( const GuiEvent &event )
{
//m_bDragging = FALSE;
if ( !mActive || !mVisible || !mAwake || !mSpin )
return;
//if(m_bAllowRot)
//{
// mMouseState = Rotating;
// mLastMousePoint = event.mousePoint;
// mouseLock();
//}
}
// Mouse is up, unlock mouse input
void GuiObjectView::onMouseUp( const GuiEvent & event )
{
//if(!m_bDragging)
//if(!Canvas->mouseButtonDrag())
{
Con::executef(this, 2, "onMouseUp",avar("%d",event.mouseClickCount));
}
//m_bDragging = FALSE;
//if(m_bAllowRot)
//{
// mouseUnlock();
// mMouseState = None;
//}
}
// If mouse is dragged, adjust camera position accordingly. Makes model rotate
void GuiObjectView::onMouseDragged( const GuiEvent &event )
{
if(!m_bAllowRot)
return;
//if ( mMouseState != Rotating )
// return;
////m_bDragging = TRUE;
//Point2I delta = event.mousePoint - mLastMousePoint;
//mLastMousePoint = event.mousePoint;
//mCameraRot.x += ( delta.y * 0.01 );
//mCameraRot.z += ( delta.x * 0.01 );
}
// Right mouse is down, lock mouse input and get the mouse pointer coordinates and set mode to zoom
void GuiObjectView::onRightMouseDown( const GuiEvent &event )
{
//m_bRightDragging = FALSE;
if ( !mActive || !mVisible || !mAwake || !mZoom )
return;
if(m_bAllowRot)
{
mMouseState = Rotating;
mLastMousePoint = event.mousePoint;
mouseLock();
}
if(!m_bAllowRot || m_bAllowRot && (event.modifier & SI_CTRL))
{
if(m_bAllowZoom)
{
mMouseState = Zooming;
mLastMousePoint = event.mousePoint;
mouseLock();
}
}
}
// Right mouse is up, unlock mouse input
void GuiObjectView::onRightMouseUp( const GuiEvent &/*event*/ )
{
//m_bRightDragging = FALSE;
//if(m_bAllowZoom)
if ( mMouseState != None)
{
mouseUnlock();
mMouseState = None;
}
}
// If mouse is dragged, adjust camera position accordingly. Makes model zoom
void GuiObjectView::onRightMouseDragged( const GuiEvent &event )
{
//if(!m_bAllowZoom)
// return;
if ( mMouseState == Rotating )
{
Point2I delta = event.mousePoint - mLastMousePoint;
mLastMousePoint = event.mousePoint;
mCameraRot.x += ( delta.y * 0.01 );
mCameraRot.z += ( delta.x * 0.01 );
}
else if ( mMouseState == Zooming )
{
S32 delta = event.mousePoint.y - mLastMousePoint.y;
mLastMousePoint = event.mousePoint;
mOrbitDist += ( delta * 0.01 );
}
}
//-----------------------------------------------------------
void GuiObjectView::fitToCamera()
{
// Make sure there is a main object in the scene
if (mMeshObjects.mMainObject)
{
// Initialize camera values:
mOrbitPos = mMeshObjects.mMainObject->getShape()->center;
mMinOrbitDist = mMeshObjects.mMainObject->getShape()->radius;
mOrbitDist = mMinOrbitDist + 0.6f;
}
}
//------------------------------------------------------------------------------
bool GuiObjectView::processCameraQuery( CameraQuery* query )
{
// Make sure the orbit distance is within the acceptable range:
mOrbitDist = ( mOrbitDist < mMinOrbitDist ) ? mMinOrbitDist : ( ( mOrbitDist > MaxOrbitDist ) ? MaxOrbitDist : mOrbitDist );
// Adjust the camera so that we are still facing the model:
Point3F vec;
MatrixF xRot, zRot;
xRot.set( EulerF( mCameraRot.x, 0, 0 ) );
zRot.set( EulerF( 0, 0, mCameraRot.z ) );
mCameraMatrix.mul( zRot, xRot );
mCameraMatrix.getColumn( 1, &vec );
vec *= mOrbitDist;
mCameraPos = mOrbitPos - vec;
query->nearPlane = 0.1;
query->farPlane = 2100.0;
query->fov = 3.1415 / 3.5;
mCameraMatrix.setColumn( 3, mCameraPos );
query->cameraMatrix = mCameraMatrix;
return( true );
}
//------------------------------------------------------------------------------
void GuiObjectView::renderWorld( const RectI &updateRect )
{
if (!(bool)mMeshObjects.mMainObject)
return;
glClear( GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_MODELVIEW );
glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LEQUAL );
for (S32 i=0; i<33; i++)
{
if (mMeshObjects.mMesh[i].mesh)
{
// Animate and render
if(mMeshObjects.mMesh[i].mode == 1)
{
S32 time = Platform::getVirtualMilliseconds();
S32 dt = time - mMeshObjects.mMesh[i].lastRenderTime;
mMeshObjects.mMesh[i].lastRenderTime = time;
F32 fdt = dt;
mMeshObjects.mMesh[i].mesh->advanceTime( fdt/1000.f, mMeshObjects.mMesh[i].thread );
mMeshObjects.mMesh[i].mesh->animate();
}
// If this is a mounted object transform to the correct position
if (mMeshObjects.mMesh[i].parentNode != -1)
{
MatrixF mat;
getObjectTransform( &mat, i );
glPushMatrix();
dglMultMatrix( &mat );
mMeshObjects.mMesh[i].mesh->render();
glPopMatrix();
}
else
{
mMeshObjects.mMesh[i].mesh->render();
}
}
}
glDisable( GL_DEPTH_TEST );
dglSetClipRect( updateRect );
dglSetCanonicalState();
}
//-----------------------------------------------------------
void GuiObjectView::getObjectTransform( MatrixF *mat , S32 index)
{
MatrixF subTrans = mMeshObjects.mMesh[index].mesh->mNodeTransforms[mMeshObjects.mMesh[index].node];
Point3F subOffset = -subTrans.getPosition();
MatrixF mainTrans = mMeshObjects.mMainObject->mNodeTransforms[mMeshObjects.mMesh[index].parentNode];
mainTrans.mulP( subOffset );
mainTrans.setPosition( subOffset );
*mat = mainTrans;
}
//-----------------------------------------------------------
void GuiObjectView::loadObject(const char* name, const char* shape, const char* skin, const char* nodeName, S32 detail)
{
bool main = (dStrcmp(nodeName,"") == 0);
S32 index = 0;
if (main)
{
// This is the main object clear out any meshs already loaded
Clear();
mMeshObjects.m_bAttachShapeInst = FALSE;
mMeshObjects.load(0, name, shape, skin, -1, detail);
}
else
{
// Make sure there is a mMainObject
if (!mMeshObjects.mMainObject)
{
Con::printf("Error: Main object not found");
return;
}
// Check for target node
S32 pNode = mMeshObjects.mMainObject->getShape()->findNode(nodeName);
if(pNode == -1)
{
Con::printf("Error: Unable to find node %s", nodeName);
return;
}
// Check to see if something is already mounted to this mountPoint, or if the object name already exists.
for (S32 i=0; i<33; i++)
{
if (mMeshObjects.mMesh[i].parentNode == pNode || dStrcmp(mMeshObjects.mMesh[i].name, name) == 0)
{
Con::printf("Unloading object %s at %s", mMeshObjects.mMesh[i].name, nodeName);
mMeshObjects.unLoad(i);
}
}
// This is a mounted object find an open spot for this mesh
index = mMeshObjects.findOpen();
if (index == -1)
{
Con::printf("Error: Maximum mountable objects reached. Please unMount an object");
return;
}
mMeshObjects.load(index, name, shape, skin, pNode, detail);
}
}
//-----------------------------------------------------------
void GuiObjectView::unLoadObject(const char* name, const char* node)
{
if (dStrcmp(name,"") != 0)
{
S32 index = mMeshObjects.findMeshByName(name);
if (index != -1)
{
mMeshObjects.unLoad(index);
Con::printf("Unloading object %s", name);
}
else
{
Con::printf("Error: Unable to find object %s", name);
}
}
else if (dStrcmp(node,"") != 0)
{
S32 index = mMeshObjects.findMeshByNode(node);
if (index != -1)
{
mMeshObjects.unLoad(index);
Con::printf("Unloading object at %s", node);
}
else
{
Con::printf("Error: Unable to find object at %s", node);
}
}
}
void GuiObjectView::loadDSQ(const char* name, const char* dsq)
{
S32 index = mMeshObjects.findMeshByName(name);
if (index != -1)
{
mMeshObjects.mMesh[index].loadDSQ(dsq);
}
else
{
Con::printf("Error: Could not find object %s", name);
}
}
//-----------------------------------------------------------
void GuiObjectView::setSequence(const char* name, const char* seq, F32 time)
{
S32 index = mMeshObjects.findMeshByName(name);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -