From e1197e75bc085392a62d0707110e7ad45c2c8a82 Mon Sep 17 00:00:00 2001 From: cc9cii Date: Wed, 8 Oct 2014 07:54:22 +1100 Subject: [PATCH] Handle change of names or regions to dynamically update the overlay. --- .../view/render/pagedworldspacewidget.cpp | 70 +++++++++++++++---- .../view/render/pagedworldspacewidget.hpp | 4 +- apps/opencs/view/render/textoverlay.cpp | 2 +- apps/opencs/view/render/textoverlay.hpp | 3 +- 4 files changed, 60 insertions(+), 19 deletions(-) diff --git a/apps/opencs/view/render/pagedworldspacewidget.cpp b/apps/opencs/view/render/pagedworldspacewidget.cpp index 4a128d4af..d818de79b 100644 --- a/apps/opencs/view/render/pagedworldspacewidget.cpp +++ b/apps/opencs/view/render/pagedworldspacewidget.cpp @@ -32,7 +32,7 @@ bool CSVRender::PagedWorldspaceWidget::adjustCells() const CSMWorld::IdCollection& cells = mDocument.getData().getCells(); { - // remove + // remove (or name/region modified) std::map::iterator iter (mCells.begin()); while (iter!=mCells.end()) @@ -43,21 +43,61 @@ bool CSVRender::PagedWorldspaceWidget::adjustCells() cells.getRecord (index).mState==CSMWorld::RecordBase::State_Deleted) { delete iter->second; - mCells.erase (iter++); + mCells.erase (iter); + + // delete overlays + std::map::iterator itOverlay = mTextOverlays.find(iter->first); + if(itOverlay != mTextOverlays.end()) + { + delete itOverlay->second; + mTextOverlays.erase(itOverlay); + } // destroy manual objects and entities - std::map::iterator it = mEntities.find(iter->first.getId(mWorldspace)); - if(it != mEntities.end()) + std::map::iterator itEntity = mEntities.find(iter->first); + if(itEntity != mEntities.end()) { - getSceneManager()->destroyEntity(it->second); - mEntities.erase(it); + getSceneManager()->destroyEntity(itEntity->second); + mEntities.erase(itEntity); } getSceneManager()->destroyManualObject("manual"+iter->first.getId(mWorldspace)); + iter++; modified = true; } else + { + // check if name or region field has changed + std::string name = cells.getRecord(index).get().mName; + std::string region = cells.getRecord(index).get().mRegion; + + std::map::iterator it = mTextOverlays.find(iter->first); + if(it != mTextOverlays.end()) + { + if(it->second->getDesc() != "") // previously had name + { + if(name != it->second->getDesc()) // new name + { + if(name != "") + it->second->setDesc(name); + else // name deleted, use region + it->second->setDesc(region); + it->second->update(); + } + } + else if(name != "") // name added + { + it->second->setDesc(name); + it->second->update(); + } + else if(region != it->second->getDesc()) // new region + { + it->second->setDesc(region); + it->second->update(); + } + } ++iter; + } } } @@ -100,7 +140,7 @@ bool CSVRender::PagedWorldspaceWidget::adjustCells() entity->setVisible(false); // keep pointers so that they can be deleted later - mEntities.insert(std::make_pair(iter->getId(mWorldspace), entity)); + mEntities.insert(std::make_pair(*iter, entity)); CSVRender::TextOverlay *textDisp = new CSVRender::TextOverlay(entity, getCamera(), iter->getId(mWorldspace)); textDisp->enable(true); @@ -109,7 +149,7 @@ bool CSVRender::PagedWorldspaceWidget::adjustCells() if(desc == "") desc = cells.getRecord(index).get().mRegion; textDisp->setDesc(desc); textDisp->update(); - mTextOverlays.push_back(textDisp); + mTextOverlays.insert(std::make_pair(*iter, textDisp)); modified = true; } @@ -122,13 +162,14 @@ void CSVRender::PagedWorldspaceWidget::mouseReleaseEvent (QMouseEvent *event) { if(event->button() == Qt::RightButton) { - std::list::iterator iter = mTextOverlays.begin(); + std::map::iterator iter = mTextOverlays.begin(); for(; iter != mTextOverlays.end(); ++iter) { if(mDisplayCellCoord && - (*iter)->isEnabled() && (*iter)->container().contains(event->x(), event->y())) + iter->second->isEnabled() && iter->second->container().contains(event->x(), event->y())) { - std::cout << "clicked: " << (*iter)->getCaption() << std::endl; + std::cout << "clicked: " << iter->second->getCaption() << std::endl; + break; } } } @@ -172,10 +213,10 @@ void CSVRender::PagedWorldspaceWidget::updateOverlay() if(!mTextOverlays.empty()) { - std::list::iterator it = mTextOverlays.begin(); + std::map::iterator it = mTextOverlays.begin(); for(; it != mTextOverlays.end(); ++it) { - (*it)->update(); + it->second->update(); } } } @@ -259,7 +300,6 @@ std::string CSVRender::PagedWorldspaceWidget::getStartupInstruction() CSVRender::PagedWorldspaceWidget::PagedWorldspaceWidget (QWidget* parent, CSMDoc::Document& document) : WorldspaceWidget(document, parent), mDocument(document), mWorldspace("std::default"), mDisplayCellCoord(true) -, mTextOverlays(0) { QAbstractItemModel *cells = document.getData().getTableModel (CSMWorld::UniversalId::Type_Cells); @@ -279,7 +319,7 @@ CSVRender::PagedWorldspaceWidget::~PagedWorldspaceWidget() { delete iter->second; - std::map::iterator it = mEntities.find(iter->first.getId(mWorldspace)); + std::map::iterator it = mEntities.find(iter->first); if(it != mEntities.end()) { getSceneManager()->destroyEntity(it->second); diff --git a/apps/opencs/view/render/pagedworldspacewidget.hpp b/apps/opencs/view/render/pagedworldspacewidget.hpp index b3f3fb4ef..611ebd04b 100644 --- a/apps/opencs/view/render/pagedworldspacewidget.hpp +++ b/apps/opencs/view/render/pagedworldspacewidget.hpp @@ -23,8 +23,8 @@ namespace CSVRender std::string mWorldspace; CSVWidget::SceneToolToggle *mControlElements; bool mDisplayCellCoord; - std::list mTextOverlays; - std::map mEntities; + std::map mTextOverlays; + std::map mEntities; private: diff --git a/apps/opencs/view/render/textoverlay.cpp b/apps/opencs/view/render/textoverlay.cpp index 143d08fc4..901b601e8 100644 --- a/apps/opencs/view/render/textoverlay.cpp +++ b/apps/opencs/view/render/textoverlay.cpp @@ -27,7 +27,7 @@ namespace CSVRender // http://www.ogre3d.org/tikiwiki/tiki-index.php?page=Creating+dynamic+textures // http://www.ogre3d.org/tikiwiki/ManualObject TextOverlay::TextOverlay(const Ogre::MovableObject* obj, const Ogre::Camera* camera, const Ogre::String& id) - : mOverlay(0), mCaption(""), mEnabled(true), mCamera(camera), mObj(obj), mId(id), mOnScreen(false) + : mOverlay(0), mCaption(""), mDesc(""), mEnabled(true), mCamera(camera), mObj(obj), mId(id), mOnScreen(false) , mFontHeight(16) // FIXME: make this configurable { if(id == "" || !camera || !obj) diff --git a/apps/opencs/view/render/textoverlay.hpp b/apps/opencs/view/render/textoverlay.hpp index 7402e33a0..17530a010 100644 --- a/apps/opencs/view/render/textoverlay.hpp +++ b/apps/opencs/view/render/textoverlay.hpp @@ -54,7 +54,8 @@ namespace CSVRender void setDesc(const Ogre::String& text); void update(); QRect container(); - Ogre::String getCaption() { return mCaption; } // FIXME: debug + Ogre::String getCaption() { return mCaption; } // FIXME: debug + Ogre::String getDesc() { return mDesc; } }; }