1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-16 21:19:55 +00:00

Revert to storing Scene Node names as the key in RigidBody. Allow moving objects up/down using scroll wheel or touchpad while dragging. Cleanup code a little.

This commit is contained in:
cc9cii 2014-10-29 23:02:38 +11:00
parent 792fbd119f
commit 1741043f20
7 changed files with 252 additions and 201 deletions

View file

@ -33,8 +33,8 @@ void CSVRender::Object::clearSceneNode (Ogre::SceneNode *node)
void CSVRender::Object::clear() void CSVRender::Object::clear()
{ {
if(!mObject.isNull() && !mReferenceId.empty()) if(!mObject.isNull())
CSVWorld::PhysicsSystem::instance()->removeObject(mReferenceId); CSVWorld::PhysicsSystem::instance()->removeObject(mBase->getName());
mObject.setNull(); mObject.setNull();

View file

@ -170,6 +170,23 @@ bool CSVRender::PagedWorldspaceWidget::adjustCells()
return modified; return modified;
} }
void CSVRender::PagedWorldspaceWidget::mousePressEvent (QMouseEvent *event)
{
if(event->button() == Qt::RightButton)
{
std::map<CSMWorld::CellCoordinates, TextOverlay *>::iterator iter = mTextOverlays.begin();
for(; iter != mTextOverlays.end(); ++iter)
{
if(mDisplayCellCoord &&
iter->second->isEnabled() && iter->second->container().contains(event->x(), event->y()))
{
return;
}
}
}
WorldspaceWidget::mouseReleaseEvent(event);
}
void CSVRender::PagedWorldspaceWidget::mouseReleaseEvent (QMouseEvent *event) void CSVRender::PagedWorldspaceWidget::mouseReleaseEvent (QMouseEvent *event)
{ {
if(event->button() == Qt::RightButton) if(event->button() == Qt::RightButton)
@ -181,7 +198,7 @@ void CSVRender::PagedWorldspaceWidget::mouseReleaseEvent (QMouseEvent *event)
iter->second->isEnabled() && iter->second->container().contains(event->x(), event->y())) iter->second->isEnabled() && iter->second->container().contains(event->x(), event->y()))
{ {
std::cout << "clicked: " << iter->second->getCaption() << std::endl; std::cout << "clicked: " << iter->second->getCaption() << std::endl;
break; return;
} }
} }
} }
@ -194,7 +211,7 @@ void CSVRender::PagedWorldspaceWidget::mouseDoubleClickEvent (QMouseEvent *event
{ {
std::cout << "double clicked" << std::endl; std::cout << "double clicked" << std::endl;
} }
//WorldspaceWidget::mouseDoubleClickEvent(event); WorldspaceWidget::mouseDoubleClickEvent(event);
} }
void CSVRender::PagedWorldspaceWidget::updateOverlay() void CSVRender::PagedWorldspaceWidget::updateOverlay()

View file

@ -81,6 +81,8 @@ namespace CSVRender
virtual void updateOverlay(); virtual void updateOverlay();
virtual void mousePressEvent (QMouseEvent *event);
virtual void mouseReleaseEvent (QMouseEvent *event); virtual void mouseReleaseEvent (QMouseEvent *event);
virtual void mouseDoubleClickEvent (QMouseEvent *event); virtual void mouseDoubleClickEvent (QMouseEvent *event);

View file

@ -113,7 +113,9 @@ namespace
CSVRender::WorldspaceWidget::WorldspaceWidget (CSMDoc::Document& document, QWidget* parent) CSVRender::WorldspaceWidget::WorldspaceWidget (CSMDoc::Document& document, QWidget* parent)
: SceneWidget (parent), mDocument(document), mSceneElements(0), mRun(0), : SceneWidget (parent), mDocument(document), mSceneElements(0), mRun(0),
mCurrentObj(""), mMouseState(Mouse_Default), mOldPos(0,0), mMouseEventTimer(0), mPlane(0) mCurrentObj(""), mMouseState(Mouse_Default), mOldPos(0,0), mMouseEventTimer(0), mPlane(0),
mGrabbedSceneNode(""), mOrigObjPos(Ogre::Vector3()), mOrigMousePos(Ogre::Vector3()),
mCurrentMousePos(Ogre::Vector3()), mZOffset(0.0f)
{ {
setAcceptDrops(true); setAcceptDrops(true);
@ -461,10 +463,11 @@ void CSVRender::WorldspaceWidget::updateOverlay()
// State machine: // State machine:
// //
// [default] mousePressEvent->check if the mouse is pointing at an object // [default] mousePressEvent->check if the mouse is pointing at an object
// if yes, go to [grab] else stay at [default] // if yes, create collision planes then go to [grab]
// else check for terrain
// //
// [grab] mouseReleaseEvent->if same button and new obj, go to [edit] // [grab] mouseReleaseEvent->if same button and new obj, go to [edit]
// mouseMoveEvent->if same button, create collision planes then go to [drag] // mouseMoveEvent->if same button, go to [drag]
// other mouse events or buttons, go back to [default] (i.e. like 'cancel') // other mouse events or buttons, go back to [default] (i.e. like 'cancel')
// //
// [drag] mouseReleaseEvent->if same button, place the object at the new // [drag] mouseReleaseEvent->if same button, place the object at the new
@ -499,34 +502,29 @@ void CSVRender::WorldspaceWidget::mouseMoveEvent (QMouseEvent *event)
// check if min elapsed time to stop false detection of drag // check if min elapsed time to stop false detection of drag
if(!mMouseEventTimer->isValid() || !mMouseEventTimer->hasExpired(100)) // ms if(!mMouseEventTimer->isValid() || !mMouseEventTimer->hasExpired(100)) // ms
break; break;
else
{
mMouseEventTimer->invalidate();
mMouseState = Mouse_Drag; mMouseEventTimer->invalidate();
//std::cout << "grab->drag" << std::endl; mMouseState = Mouse_Drag;
}
/* FALL_THROUGH */ /* FALL_THROUGH */
} }
case Mouse_Drag: case Mouse_Drag:
{ {
// FIXME: don't update less than a quantum if(event->pos() != mOldPos) // TODO: maybe don't update less than a quantum?
//QPoint diff = mOldPos-event->pos();
if(event->pos() != mOldPos)
{ {
mOldPos = event->pos(); mOldPos = event->pos();
//std::cout << QString::number(event->pos().x()).toStdString() << ", "
//<< QString::number(event->pos().y()).toStdString() << std::endl;
// ray test against the plane to provide feedback to the user the // ray test against the plane to provide feedback to the user the
// relative movement of the object on the x-y plane // relative movement of the object on the x-y plane
std::pair<bool, Ogre::Vector3> planeResult = mousePositionOnPlane(event, *mPlane); std::pair<bool, Ogre::Vector3> planeResult = mousePositionOnPlane(event->pos(), *mPlane);
if(planeResult.first) if(planeResult.first)
{ {
if(mObjSceneNode) if(mGrabbedSceneNode != "")
{ {
mObjSceneNode->setPosition(mOrigObjPos+planeResult.second-mOrigMousePos); Ogre::Vector3 pos = mOrigObjPos;
pos.z += mZOffset;
getSceneManager()->getSceneNode(mGrabbedSceneNode)->setPosition(pos+planeResult.second-mOrigMousePos);
mCurrentMousePos = planeResult.second;
flagAsModified(); flagAsModified();
} }
} }
@ -546,52 +544,59 @@ void CSVRender::WorldspaceWidget::mouseMoveEvent (QMouseEvent *event)
void CSVRender::WorldspaceWidget::mousePressEvent (QMouseEvent *event) void CSVRender::WorldspaceWidget::mousePressEvent (QMouseEvent *event)
{ {
if(event->buttons() & Qt::RightButton) switch(mMouseState)
{ {
switch(mMouseState) case Mouse_Grab:
case Mouse_Drag:
{ {
case Mouse_Grab: if(event->buttons() & ~Qt::RightButton)
case Mouse_Drag:
{ {
break; // error event, ignore // cancel operation & return the object to the original position
placeObject(mGrabbedSceneNode, mOrigObjPos);
mMouseState = Mouse_Default;
// reset states
mCurrentMousePos = Ogre::Vector3();
mOrigMousePos = Ogre::Vector3();
mOrigObjPos = Ogre::Vector3();
mGrabbedSceneNode = "";
mCurrentObj = "";
mOldPos = QPoint(0, 0);
mMouseEventTimer->invalidate();
mZOffset = 0.0f;
} }
case Mouse_Edit: break;
case Mouse_Default: }
case Mouse_Edit:
case Mouse_Default:
{
if(event->buttons() & Qt::RightButton)
{ {
if(!getCamera()->getViewport()) std::pair<std::string, Ogre::Vector3> result = objectUnderCursor(event->x(), event->y());
if(result.first == "")
break; break;
std::pair<std::string, Ogre::Vector3> result = isObjectUnderCursor( mGrabbedSceneNode = result.first;
(float) event->x() / getCamera()->getViewport()->getActualWidth(), // ray test agaist the plane to get a starting position of the
(float) event->y() / getCamera()->getViewport()->getActualHeight()); // mouse in relation to the object position
if(result.first != "") mPlane->redefine(Ogre::Vector3::UNIT_Z, result.second);
std::pair<bool, Ogre::Vector3> planeResult = mousePositionOnPlane(event->pos(), *mPlane);
if(planeResult.first)
{ {
mCurrentObj = result.first; // FIXME mOrigMousePos = planeResult.second;
// ray test agaist the plane to get a starting position of the mCurrentMousePos = planeResult.second;
// mouse in relation to the object position mZOffset = 0.0f;
mPlane->redefine(Ogre::Vector3::UNIT_Z, result.second);
std::pair<bool, Ogre::Vector3> planeResult = mousePositionOnPlane(event, *mPlane);
if(planeResult.first)
mOrigMousePos = planeResult.second;
std::string sceneNodeName =
CSVWorld::PhysicsSystem::instance()->referenceToSceneNode(result.first);
mObjSceneNode = getSceneManager()->getSceneNode(sceneNodeName);
mOrigObjPos = mObjSceneNode->getPosition();
mMouseEventTimer->start();
mMouseState = Mouse_Grab;
//std::cout << "default/edit->grab" << std::endl;
} }
break;
mOrigObjPos = getSceneManager()->getSceneNode(mGrabbedSceneNode)->getPosition();
mMouseEventTimer->start();
mMouseState = Mouse_Grab;
} }
/* NO_DEFAULT_CASE */ break;
} }
/* NO_DEFAULT_CASE */
} }
// FIXME: other button press - cancel grab and/or drag and place the object back in the original
// position
//SceneWidget::mousePressEvent(event); //SceneWidget::mousePressEvent(event);
} }
@ -605,110 +610,98 @@ void CSVRender::WorldspaceWidget::mouseReleaseEvent (QMouseEvent *event)
return; return;
} }
// FIXME: skip this if overlay clicked above
// FIXME: stop/disable the timer
switch(mMouseState) switch(mMouseState)
{ {
case Mouse_Grab: case Mouse_Grab:
{ {
std::pair<std::string, Ogre::Vector3> result = isObjectUnderCursor( std::pair<std::string, Ogre::Vector3> result = objectUnderCursor(event->x(), event->y());
(float) event->x() / getCamera()->getViewport()->getActualWidth(),
(float) event->y() / getCamera()->getViewport()->getActualHeight());
if(result.first != "") if(result.first != "")
{ {
if(result.first == mCurrentObj) if(result.first == mCurrentObj)
{ {
// unselect object
mMouseState = Mouse_Default; mMouseState = Mouse_Default;
//std::cout << "grab->default" << std::endl;
mCurrentObj = ""; mCurrentObj = "";
} }
else else
{ {
// select object
mMouseState = Mouse_Edit; mMouseState = Mouse_Edit;
//std::cout << "grab->edit" << std::endl;
mCurrentObj = result.first; mCurrentObj = result.first;
// print some debug info // print some debug info
std::cout << "ReferenceId: " << result.first << std::endl; if(isDebug())
const CSMWorld::RefCollection& references = mDocument.getData().getReferences();
int index = references.searchId(result.first);
if (index != -1)
{ {
int columnIndex = std::string referenceId =
references.findColumnIndex(CSMWorld::Columns::ColumnId_ReferenceableId); CSVWorld::PhysicsSystem::instance()->sceneNodeToRefId(result.first);
std::cout << " index: " + QString::number(index).toStdString() std::cout << "ReferenceId: " << referenceId << std::endl;
+", column index: " + QString::number(columnIndex).toStdString() const CSMWorld::RefCollection& references = mDocument.getData().getReferences();
<< std::endl; int index = references.searchId(referenceId);
if (index != -1)
{
int columnIndex =
references.findColumnIndex(CSMWorld::Columns::ColumnId_ReferenceableId);
std::cout << " index: " + QString::number(index).toStdString()
+", column index: " + QString::number(columnIndex).toStdString()
<< std::endl;
}
} }
} }
// update highlighting the current object // update highlighting the current object
std::string sceneNode = if(isDebug())
CSVWorld::PhysicsSystem::instance()->referenceToSceneNode(result.first); updateSelectionHighlight(result.first, result.second);
uint32_t visibilityMask = getCamera()->getViewport()->getVisibilityMask();
bool ignoreObjects = !(visibilityMask & (uint32_t)CSVRender::Element_Reference);
if(!ignoreObjects && getSceneManager()->hasSceneNode(sceneNode))
{
CSMSettings::UserSettings &userSettings = CSMSettings::UserSettings::instance();
if(userSettings.setting("debug/mouse-picking", QString("false")) == "true" ? true : false)
updateSelectionHighlight(sceneNode, result.second);
}
flagAsModified();
} }
break; break;
} }
case Mouse_Drag: case Mouse_Drag:
{ {
// final placement // final placement
std::pair<bool, Ogre::Vector3> planeResult = mousePositionOnPlane(event, *mPlane); std::pair<bool, Ogre::Vector3> planeResult = mousePositionOnPlane(event->pos(), *mPlane);
if(planeResult.first) if(planeResult.first)
{ {
if(mObjSceneNode) if(mGrabbedSceneNode != "")
{ {
mObjSceneNode->setPosition(mOrigObjPos+planeResult.second-mOrigMousePos); mOrigObjPos.z += mZOffset;
flagAsModified(); Ogre::Vector3 pos = mOrigObjPos+planeResult.second-mOrigMousePos;
placeObject(mGrabbedSceneNode, pos);
//mCurrentObj = mGrabbedSceneNode; // FIXME
mCurrentObj = "";
// update physics // reset states
const CSMWorld::CellRef& cellref = mCurrentMousePos = Ogre::Vector3();
mDocument.getData().getReferences().getRecord (mCurrentObj).get(); mOrigMousePos = Ogre::Vector3();
Ogre::Quaternion xr (Ogre::Radian (-cellref.mPos.rot[0]), Ogre::Vector3::UNIT_X); mOrigObjPos = Ogre::Vector3();
Ogre::Quaternion yr (Ogre::Radian (-cellref.mPos.rot[1]), Ogre::Vector3::UNIT_Y); mGrabbedSceneNode = "";
Ogre::Quaternion zr (Ogre::Radian (-cellref.mPos.rot[2]), Ogre::Vector3::UNIT_Z); mOldPos = QPoint(0, 0);
mZOffset = 0.0f;
// FIXME: adjustRigidBody() seems to lose objects, delete and recreate for now // FIXME: update document
//CSVWorld::PhysicsSystem::instance()->moveObject(mCurrentObj, // FIXME: highlight current object?
//mOrigObjPos+planeResult.second-mOrigMousePos, xr*yr*zr); mMouseState = Mouse_Edit;
std::string sceneNodeName =
CSVWorld::PhysicsSystem::instance()->referenceToSceneNode(mCurrentObj);
std::string mesh =
CSVWorld::PhysicsSystem::instance()->sceneNodeToMesh(sceneNodeName);
CSVWorld::PhysicsSystem::instance()->removeObject(mCurrentObj);
CSVWorld::PhysicsSystem::instance()->addObject(mesh,
sceneNodeName, mCurrentObj, cellref.mScale,
mOrigObjPos+planeResult.second-mOrigMousePos, xr*yr*zr);
} }
} }
// FIXME: update document
// FIXME: highlight current object?
//std::cout << "final position" << std::endl;
mMouseState = Mouse_Edit;
//std::cout << "drag->edit" << std::endl;
break; break;
} }
case Mouse_Edit: case Mouse_Edit:
case Mouse_Default: case Mouse_Default:
{ {
// probably terrain // probably terrain, check
debugMousePicking( std::pair<std::string, Ogre::Vector3> result = terrainUnderCursor(event->x(), event->y());
(float) event->x() / getCamera()->getViewport()->getActualWidth(), if(result.first != "")
(float) event->y() / getCamera()->getViewport()->getActualHeight()); {
if(isDebug())
{
std::cout << "terrain: " << result.first << std::endl;
std::cout << " hit pos "+ QString::number(result.second.x).toStdString()
+ ", " + QString::number(result.second.y).toStdString()
+ ", " + QString::number(result.second.z).toStdString() << std::endl;
}
}
break; break;
} }
/* NO_DEFAULT_CASE */ /* NO_DEFAULT_CASE */
} }
mMouseEventTimer->invalidate();
} }
SceneWidget::mouseReleaseEvent(event); SceneWidget::mouseReleaseEvent(event);
} }
@ -717,8 +710,7 @@ void CSVRender::WorldspaceWidget::mouseDoubleClickEvent (QMouseEvent *event)
{ {
if(event->button() == Qt::RightButton) if(event->button() == Qt::RightButton)
{ {
CSMSettings::UserSettings &userSettings = CSMSettings::UserSettings::instance(); if(isDebug())
if(userSettings.setting ("debug/mouse-picking", QString("false")) == "true" ? true : false)
{ {
// FIXME: OEngine::PhysicEngine creates only one child scene node for the // FIXME: OEngine::PhysicEngine creates only one child scene node for the
// debug drawer. Hence only the first subview that creates the debug drawer // debug drawer. Hence only the first subview that creates the debug drawer
@ -734,68 +726,46 @@ void CSVRender::WorldspaceWidget::mouseDoubleClickEvent (QMouseEvent *event)
void CSVRender::WorldspaceWidget::wheelEvent (QWheelEvent *event) void CSVRender::WorldspaceWidget::wheelEvent (QWheelEvent *event)
{ {
// FIXME: add wheel event to move the object along the y axis during Mouse_Drag or switch(mMouseState)
// Mouse_Grab
SceneWidget::wheelEvent(event);
}
void CSVRender::WorldspaceWidget::debugMousePicking(float mouseX, float mouseY)
{
CSMSettings::UserSettings &userSettings = CSMSettings::UserSettings::instance();
bool debug = userSettings.setting ("debug/mouse-picking", QString("false")) == "true" ? true : false;
std::pair<std::string, Ogre::Vector3> result = CSVWorld::PhysicsSystem::instance()->castRay(
mouseX, mouseY, NULL, NULL, getCamera());
if(debug && result.first != "")
{ {
// FIXME: is there a better way to distinguish terrain from objects? case Mouse_Grab:
QString name = QString(result.first.c_str()); mMouseState = Mouse_Drag;
if(name.contains(QRegExp("^HeightField")))
/* FALL_THROUGH */
case Mouse_Drag:
{ {
// terrain // move the object along the z axis during Mouse_Drag or Mouse_Grab
std::cout << "terrain: " << result.first << std::endl; if (event->delta())
std::cout << " hit pos "+ QString::number(result.second.x).toStdString() {
+ ", " + QString::number(result.second.y).toStdString() // seems positive is up and negative is down
+ ", " + QString::number(result.second.z).toStdString() mZOffset += (event->delta()/5); // FIXME: arbitrary number, make config option?
<< std::endl;
Ogre::Vector3 pos = mOrigObjPos;
pos.z += mZOffset;
getSceneManager()->getSceneNode(mGrabbedSceneNode)->setPosition(pos+mCurrentMousePos-mOrigMousePos);
flagAsModified();
}
break;
} }
else case Mouse_Edit:
case Mouse_Default:
{ {
std::string sceneNode = SceneWidget::wheelEvent(event);
CSVWorld::PhysicsSystem::instance()->referenceToSceneNode(result.first); break;
uint32_t visibilityMask = getCamera()->getViewport()->getVisibilityMask();
bool ignoreObjects = !(visibilityMask & (uint32_t)CSVRender::Element_Reference);
if(!ignoreObjects && getSceneManager()->hasSceneNode(sceneNode))
{
if(userSettings.setting("debug/mouse-picking", QString("false")) == "true" ? true : false)
updateSelectionHighlight(sceneNode, result.second);
}
std::cout << "ReferenceId: " << result.first << std::endl;
const CSMWorld::CellRef& cellref = mDocument.getData().getReferences().getRecord (result.first).get();
//std::cout << "CellRef.mId: " << cellref.mId << std::endl; // Same as ReferenceId
std::cout << " CellRef.mCell: " << cellref.mCell << std::endl;
const CSMWorld::RefCollection& references = mDocument.getData().getReferences();
int index = references.searchId(result.first);
if (index != -1)
{
int columnIndex =
references.findColumnIndex(CSMWorld::Columns::ColumnId_ReferenceableId);
std::cout << " index: " + QString::number(index).toStdString()
+", column index: " + QString::number(columnIndex).toStdString() << std::endl;
}
flagAsModified();
} }
/* NO_DEFAULT_CASE */
} }
} }
// FIXME: for debugging only // FIXME: for debugging only
void CSVRender::WorldspaceWidget::updateSelectionHighlight(std::string sceneNode, const Ogre::Vector3 &position) void CSVRender::WorldspaceWidget::updateSelectionHighlight(const std::string sceneNode, const Ogre::Vector3 &position)
{ {
uint32_t visibilityMask = getCamera()->getViewport()->getVisibilityMask();
bool ignoreObjects = !(visibilityMask & (uint32_t)CSVRender::Element_Reference);
if(ignoreObjects || !getSceneManager()->hasSceneNode(sceneNode) || !isDebug())
return;
CSMSettings::UserSettings &userSettings = CSMSettings::UserSettings::instance(); CSMSettings::UserSettings &userSettings = CSMSettings::UserSettings::instance();
bool debugCursor = userSettings.setting( bool debugCursor = userSettings.setting(
"debug/mouse-position", QString("false")) == "true" ? true : false; "debug/mouse-position", QString("false")) == "true" ? true : false;
@ -858,41 +828,69 @@ void CSVRender::WorldspaceWidget::updateSelectionHighlight(std::string sceneNode
if(debugCursor) if(debugCursor)
showHitPoint(getSceneManager(), sceneNode, position); showHitPoint(getSceneManager(), sceneNode, position);
} }
flagAsModified();
} }
std::pair<std::string, Ogre::Vector3> CSVRender::WorldspaceWidget::isObjectUnderCursor(float mouseX, float mouseY) std::pair<std::string, Ogre::Vector3> CSVRender::WorldspaceWidget::terrainUnderCursor(const int mouseX, const int mouseY)
{ {
std::pair<std::string, Ogre::Vector3> result = CSVWorld::PhysicsSystem::instance()->castRay( if(!getCamera()->getViewport())
mouseX, mouseY, NULL, NULL, getCamera()); return std::make_pair("", Ogre::Vector3());
float x = (float) mouseX / getCamera()->getViewport()->getActualWidth();
float y = (float) mouseY / getCamera()->getViewport()->getActualHeight();
std::pair<std::string, Ogre::Vector3> result =
CSVWorld::PhysicsSystem::instance()->castRay(x, y, NULL, NULL, getCamera());
if(result.first != "") if(result.first != "")
{ {
// FIXME: is there a better way to distinguish terrain from objects?
QString name = QString(result.first.c_str());
if(name.contains(QRegExp("^HeightField")))
{
return result;
}
}
return std::make_pair("", Ogre::Vector3());
}
std::pair<std::string, Ogre::Vector3> CSVRender::WorldspaceWidget::objectUnderCursor(const int mouseX, const int mouseY)
{
if(!getCamera()->getViewport())
return std::make_pair("", Ogre::Vector3());
float x = (float) mouseX / getCamera()->getViewport()->getActualWidth();
float y = (float) mouseY / getCamera()->getViewport()->getActualHeight();
std::pair<std::string, Ogre::Vector3> result =
CSVWorld::PhysicsSystem::instance()->castRay(x, y, NULL, NULL, getCamera());
if(result.first != "")
{
// NOTE: anything not terrain is assumed to be an object
QString name = QString(result.first.c_str()); QString name = QString(result.first.c_str());
if(!name.contains(QRegExp("^HeightField"))) if(!name.contains(QRegExp("^HeightField")))
{ {
std::string sceneNode =
CSVWorld::PhysicsSystem::instance()->referenceToSceneNode(result.first);
uint32_t visibilityMask = getCamera()->getViewport()->getVisibilityMask(); uint32_t visibilityMask = getCamera()->getViewport()->getVisibilityMask();
bool ignoreObjects = !(visibilityMask & (uint32_t)CSVRender::Element_Reference); bool ignoreObjects = !(visibilityMask & (uint32_t)CSVRender::Element_Reference);
if(!ignoreObjects && getSceneManager()->hasSceneNode(sceneNode)) if(!ignoreObjects && getSceneManager()->hasSceneNode(result.first))
{ {
return result; return result;
} }
} }
} }
return std::make_pair("", Ogre::Vector3(0,0,0)); return std::make_pair("", Ogre::Vector3());
} }
std::pair<bool, Ogre::Vector3> CSVRender::WorldspaceWidget::mousePositionOnPlane(QMouseEvent *event, Ogre::Plane &plane) std::pair<bool, Ogre::Vector3> CSVRender::WorldspaceWidget::mousePositionOnPlane(const QPoint &pos, const Ogre::Plane &plane)
{ {
// using a really small value seems to mess up with the projections // using a really small value seems to mess up with the projections
float nearClipDistance = getCamera()->getNearClipDistance(); // save existing float nearClipDistance = getCamera()->getNearClipDistance(); // save existing
getCamera()->setNearClipDistance(10.0f); // arbitrary number getCamera()->setNearClipDistance(10.0f); // arbitrary number
Ogre::Ray mouseRay = getCamera()->getCameraToViewportRay( Ogre::Ray mouseRay = getCamera()->getCameraToViewportRay(
(float) event->x() / getCamera()->getViewport()->getActualWidth(), (float) pos.x() / getCamera()->getViewport()->getActualWidth(),
(float) event->y() / getCamera()->getViewport()->getActualHeight()); (float) pos.y() / getCamera()->getViewport()->getActualHeight());
getCamera()->setNearClipDistance(nearClipDistance); // restore getCamera()->setNearClipDistance(nearClipDistance); // restore
std::pair<bool, float> planeResult = mouseRay.intersects(plane); std::pair<bool, float> planeResult = mouseRay.intersects(plane);
@ -901,3 +899,30 @@ std::pair<bool, Ogre::Vector3> CSVRender::WorldspaceWidget::mousePositionOnPlane
else else
return std::make_pair(false, Ogre::Vector3()); // should only happen if the plane is too small return std::make_pair(false, Ogre::Vector3()); // should only happen if the plane is too small
} }
void CSVRender::WorldspaceWidget::placeObject(const std::string sceneNode, const Ogre::Vector3 &pos)
{
getSceneManager()->getSceneNode(sceneNode)->setPosition(pos);
flagAsModified();
// update physics
std::string refId = CSVWorld::PhysicsSystem::instance()->sceneNodeToRefId(sceneNode);
const CSMWorld::CellRef& cellref =
mDocument.getData().getReferences().getRecord (refId).get();
Ogre::Quaternion xr (Ogre::Radian (-cellref.mPos.rot[0]), Ogre::Vector3::UNIT_X);
Ogre::Quaternion yr (Ogre::Radian (-cellref.mPos.rot[1]), Ogre::Vector3::UNIT_Y);
Ogre::Quaternion zr (Ogre::Radian (-cellref.mPos.rot[2]), Ogre::Vector3::UNIT_Z);
// FIXME: adjustRigidBody() seems to lose objects, work around by deleting and recreating objects
//CSVWorld::PhysicsSystem::instance()->moveObject(sceneNode, pos, xr*yr*zr);
std::string mesh = CSVWorld::PhysicsSystem::instance()->sceneNodeToMesh(sceneNode);
CSVWorld::PhysicsSystem::instance()->removeObject(sceneNode);
CSVWorld::PhysicsSystem::instance()->addObject(mesh, sceneNode, refId, cellref.mScale, pos, xr*yr*zr);
}
bool CSVRender::WorldspaceWidget::isDebug()
{
CSMSettings::UserSettings &userSettings = CSMSettings::UserSettings::instance();
return userSettings.setting("debug/mouse-picking", QString("false")) == "true" ? true : false;
}

View file

@ -36,20 +36,24 @@ namespace CSVRender
CSVWidget::SceneToolRun *mRun; CSVWidget::SceneToolRun *mRun;
CSMDoc::Document& mDocument; CSMDoc::Document& mDocument;
enum MouseState { enum MouseState
{
Mouse_Grab, Mouse_Grab,
Mouse_Drag, Mouse_Drag,
Mouse_Edit, Mouse_Edit,
Mouse_Default Mouse_Default
}; };
MouseState mMouseState; MouseState mMouseState;
QPoint mOldPos; QPoint mOldPos;
std::string mCurrentObj; std::string mCurrentObj;
std::string mGrabbedSceneNode;
QElapsedTimer *mMouseEventTimer; QElapsedTimer *mMouseEventTimer;
Ogre::Plane *mPlane; Ogre::Plane *mPlane;
Ogre::SceneNode *mObjSceneNode;
Ogre::Vector3 mOrigObjPos; Ogre::Vector3 mOrigObjPos;
Ogre::Vector3 mOrigMousePos; Ogre::Vector3 mOrigMousePos;
Ogre::Vector3 mCurrentMousePos;
float mZOffset;
std::map<std::string, std::vector<std::string> > mSelectedEntities; std::map<std::string, std::vector<std::string> > mSelectedEntities;
public: public:
@ -125,10 +129,12 @@ namespace CSVRender
virtual std::string getStartupInstruction() = 0; virtual std::string getStartupInstruction() = 0;
std::pair<std::string, Ogre::Vector3> isObjectUnderCursor(float mouseX, float mouseY); void placeObject(const std::string sceneNode, const Ogre::Vector3 &pos);
std::pair<bool, Ogre::Vector3> mousePositionOnPlane(QMouseEvent *event, Ogre::Plane &plane); bool isDebug();
void debugMousePicking(float mouseX, float mouseY); std::pair<std::string, Ogre::Vector3> terrainUnderCursor(const int mouseX, const int mouseY);
void updateSelectionHighlight(std::string sceneNode, const Ogre::Vector3 &position); std::pair<std::string, Ogre::Vector3> objectUnderCursor(const int mouseX, const int mouseY);
std::pair<bool, Ogre::Vector3> mousePositionOnPlane(const QPoint &pos, const Ogre::Plane &plane);
void updateSelectionHighlight(const std::string sceneNode, const Ogre::Vector3 &position);
private slots: private slots:

View file

@ -40,27 +40,28 @@ namespace CSVWorld
const std::string &sceneNodeName, const std::string &referenceId, float scale, const std::string &sceneNodeName, const std::string &referenceId, float scale,
const Ogre::Vector3 &position, const Ogre::Quaternion &rotation, bool placeable) const Ogre::Vector3 &position, const Ogre::Quaternion &rotation, bool placeable)
{ {
mRefToSceneNode[referenceId] = sceneNodeName; // NOTE: referenceId may not be unique when editing multiple documents concurrently
mSceneNodeToRefId[sceneNodeName] = referenceId;
mSceneNodeToMesh[sceneNodeName] = mesh; mSceneNodeToMesh[sceneNodeName] = mesh;
mEngine->createAndAdjustRigidBody(mesh, mEngine->createAndAdjustRigidBody(mesh,
referenceId, scale, position, rotation, sceneNodeName, scale, position, rotation,
0, // scaledBoxTranslation 0, // scaledBoxTranslation
0, // boxRotation 0, // boxRotation
true, // raycasting true, // raycasting
placeable); placeable);
} }
void PhysicsSystem::removeObject(const std::string &referenceId) void PhysicsSystem::removeObject(const std::string &sceneNodeName)
{ {
mEngine->removeRigidBody(referenceId); mEngine->removeRigidBody(sceneNodeName);
mEngine->deleteRigidBody(referenceId); mEngine->deleteRigidBody(sceneNodeName);
} }
void PhysicsSystem::moveObject(const std::string &referenceId, void PhysicsSystem::moveObject(const std::string &sceneNodeName,
const Ogre::Vector3 &position, const Ogre::Quaternion &rotation) const Ogre::Vector3 &position, const Ogre::Quaternion &rotation)
{ {
mEngine->adjustRigidBody(mEngine->getRigidBody(referenceId, true /*raycasting*/), mEngine->adjustRigidBody(mEngine->getRigidBody(sceneNodeName, true /*raycasting*/),
position, rotation); position, rotation);
} }
@ -111,9 +112,9 @@ namespace CSVWorld
return std::make_pair(result.first, ray.getPoint(farClipDist*result.second)); return std::make_pair(result.first, ray.getPoint(farClipDist*result.second));
} }
std::string PhysicsSystem::referenceToSceneNode(std::string referenceId) std::string PhysicsSystem::sceneNodeToRefId(std::string sceneNodeName)
{ {
return mRefToSceneNode[referenceId]; return mSceneNodeToRefId[sceneNodeName];
} }
std::string PhysicsSystem::sceneNodeToMesh(std::string sceneNodeName) std::string PhysicsSystem::sceneNodeToMesh(std::string sceneNodeName)

View file

@ -26,7 +26,7 @@ namespace CSVWorld
class PhysicsSystem class PhysicsSystem
{ {
static PhysicsSystem *mPhysicsSystemInstance; static PhysicsSystem *mPhysicsSystemInstance;
std::map<std::string, std::string> mRefToSceneNode; std::map<std::string, std::string> mSceneNodeToRefId;
std::map<std::string, std::string> mSceneNodeToMesh; std::map<std::string, std::string> mSceneNodeToMesh;
OEngine::Physic::PhysicEngine* mEngine; OEngine::Physic::PhysicEngine* mEngine;
@ -46,9 +46,9 @@ namespace CSVWorld
const Ogre::Vector3 &position, const Ogre::Quaternion &rotation, const Ogre::Vector3 &position, const Ogre::Quaternion &rotation,
bool placeable=false); bool placeable=false);
void removeObject(const std::string &referenceId); void removeObject(const std::string &sceneNodeName);
void moveObject(const std::string &referenceId, void moveObject(const std::string &sceneNodeName,
const Ogre::Vector3 &position, const Ogre::Quaternion &rotation); const Ogre::Vector3 &position, const Ogre::Quaternion &rotation);
void addHeightField(float* heights, void addHeightField(float* heights,
@ -61,7 +61,7 @@ namespace CSVWorld
std::pair<std::string, Ogre::Vector3> castRay(float mouseX, std::pair<std::string, Ogre::Vector3> castRay(float mouseX,
float mouseY, Ogre::Vector3* normal, std::string* hit, Ogre::Camera *camera); float mouseY, Ogre::Vector3* normal, std::string* hit, Ogre::Camera *camera);
std::string referenceToSceneNode(std::string referenceId); std::string sceneNodeToRefId(std::string sceneNodeName);
std::string sceneNodeToMesh(std::string sceneNodeName); std::string sceneNodeToMesh(std::string sceneNodeName);
private: private: