diff --git a/apps/opencs/view/render/pagedworldspacewidget.cpp b/apps/opencs/view/render/pagedworldspacewidget.cpp index 200c1a9d7..e4677b78d 100644 --- a/apps/opencs/view/render/pagedworldspacewidget.cpp +++ b/apps/opencs/view/render/pagedworldspacewidget.cpp @@ -3,10 +3,13 @@ #include +#include + #include #include - #include +#include // FIXME +#include // FIXME #include #include "../../../../components/esm/loadland.hpp" @@ -78,7 +81,6 @@ bool CSVRender::PagedWorldspaceWidget::adjustCells() mCells.insert (std::make_pair (*iter, new Cell (mDocument.getData(), getSceneManager(), iter->getId (mWorldspace)))); - // FIXME: delete this later Ogre::ManualObject* manual = getSceneManager()->createManualObject("manual" + iter->getId(mWorldspace)); manual->begin("BaseWhite", Ogre::RenderOperation::OT_LINE_LIST); @@ -93,8 +95,9 @@ bool CSVRender::PagedWorldspaceWidget::adjustCells() Ogre::MeshPtr meshPtr = manual->convertToMesh("vLine" + iter->getId(mWorldspace)); Ogre::Entity* entity = getSceneManager()->createEntity(meshPtr); getSceneManager()->getRootSceneNode()->createChildSceneNode()->attachObject(entity); - //entity->setVisible(false); + entity->setVisible(false); + // keep pointers so that they can be deleted later mEntities.insert(std::make_pair(iter->getId(mWorldspace), entity)); CSVRender::TextOverlay *textDisp = new CSVRender::TextOverlay(entity, getCamera(), iter->getId(mWorldspace)); @@ -110,31 +113,41 @@ bool CSVRender::PagedWorldspaceWidget::adjustCells() return modified; } -void CSVRender::PagedWorldspaceWidget::updateOverlay(bool toggleOverlay) +void CSVRender::PagedWorldspaceWidget::mouseReleaseEvent (QMouseEvent *event) { - if(!mTextOverlays.empty()) + //std::cout << "Qt x: " + std::to_string(event->x()) << ", y: " + std::to_string(event->y()) << std::endl; + + int viewportWidth = getCamera()->getViewport()->getActualWidth(); + int viewportHeight = getCamera()->getViewport()->getActualHeight(); + + Ogre::Ray mouseRay = getCamera()->getCameraToViewportRay(event->x()/viewportWidth, event->y()/viewportHeight); + Ogre::RaySceneQuery *rayScnQuery = getSceneManager()->createRayQuery(Ogre::Ray()); + rayScnQuery->setRay(mouseRay); + Ogre::RaySceneQueryResult &result = rayScnQuery->execute(); + Ogre::RaySceneQueryResult::iterator it = result.begin(); + + std::list::iterator iter = mTextOverlays.begin(); + for(; iter != mTextOverlays.end(); ++iter) { - std::list::iterator it = mTextOverlays.begin(); - for(; it != mTextOverlays.end(); ++it) + if((*iter)->isEnabled() && (*iter)->container().contains(event->x(), event->y())) { - (*it)->update(toggleOverlay); + std::cout << "clicked: " << (*iter)->getCaption() << std::endl; + (*iter)->enable(false); } - std::map::iterator iter (mCells.begin()); + } + + SceneWidget::mouseReleaseEvent(event); +} -#if 0 - if(toggleOverlay) +void CSVRender::PagedWorldspaceWidget::updateOverlay() +{ + if(!mTextOverlays.empty()) + { + std::list::iterator it = mTextOverlays.begin(); + for(; it != mTextOverlays.end(); ++it) { - while (iter!=mCells.end()) - { - std::map::iterator it = mEntities.find(iter->first.getId(mWorldspace)); - if(it != mEntities.end()) - { - it->second->setVisible(!it->second->isVisible()); - } - ++iter; - } + (*it)->update(); } -#endif } } diff --git a/apps/opencs/view/render/pagedworldspacewidget.hpp b/apps/opencs/view/render/pagedworldspacewidget.hpp index 1013319d1..42a7ef45d 100644 --- a/apps/opencs/view/render/pagedworldspacewidget.hpp +++ b/apps/opencs/view/render/pagedworldspacewidget.hpp @@ -77,7 +77,9 @@ namespace CSVRender protected: - virtual void updateOverlay(bool toggleOverlay = false); + virtual void updateOverlay(); + + virtual void mouseReleaseEvent (QMouseEvent *event); signals: diff --git a/apps/opencs/view/render/scenewidget.cpp b/apps/opencs/view/render/scenewidget.cpp index 089f4a4f3..d70346f81 100644 --- a/apps/opencs/view/render/scenewidget.cpp +++ b/apps/opencs/view/render/scenewidget.cpp @@ -287,8 +287,6 @@ namespace CSVRender break; - case Qt::Key_M: updateOverlay(true); - default: QWidget::keyReleaseEvent (event); } } @@ -387,7 +385,7 @@ namespace CSVRender } } - void SceneWidget::updateOverlay(bool toggleOverlay) + void SceneWidget::updateOverlay() { } void SceneWidget::setLighting (Lighting *lighting) diff --git a/apps/opencs/view/render/scenewidget.hpp b/apps/opencs/view/render/scenewidget.hpp index 650b4d1b5..1dab9a978 100644 --- a/apps/opencs/view/render/scenewidget.hpp +++ b/apps/opencs/view/render/scenewidget.hpp @@ -59,7 +59,9 @@ namespace CSVRender void setDefaultAmbient (const Ogre::ColourValue& colour); ///< \note The actual ambient colour may differ based on lighting settings. - virtual void updateOverlay(bool toggleOverlay = false); + virtual void updateOverlay(); + + virtual void mouseReleaseEvent (QMouseEvent *event); private: void paintEvent(QPaintEvent* e); @@ -78,8 +80,6 @@ namespace CSVRender void mouseMoveEvent (QMouseEvent *event); - void mouseReleaseEvent (QMouseEvent *event); - void updateOgreWindow(); void setLighting (Lighting *lighting); diff --git a/apps/opencs/view/render/textoverlay.cpp b/apps/opencs/view/render/textoverlay.cpp index 31c9e50ac..91fb88385 100644 --- a/apps/opencs/view/render/textoverlay.cpp +++ b/apps/opencs/view/render/textoverlay.cpp @@ -11,20 +11,16 @@ #include #include #include -#include "OgreHardwarePixelBuffer.h" +#include namespace CSVRender { -static const int sMargin = 5; // margin around overlay text in pixels - // Things to do: // - configurable font size in pixels (automatically calulate everything else from it) // - configurable texture to use // - configurable toggle key to enable/disable the text overlays (currntly fixed 'm') // - try material script -// - setup common resources such as font in a central place, so that they are not -// repeated for each text overlay (also destroy once) // - decide whether to use QPaint // http://www.ogre3d.org/tikiwiki/tiki-index.php?page=ObjectTextDisplay @@ -32,7 +28,7 @@ static const int sMargin = 5; // margin around overlay text in pixels // 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(false), mCamera(camera), mObj(obj), mId(id), mOnScreen(false) + : mOverlay(0), mCaption(""), mEnabled(true), mCamera(camera), mObj(obj), mId(id), mOnScreen(false) , mFontHeight(16) // FIXME: make this configurable { if(id == "" || !camera || !obj) @@ -120,7 +116,7 @@ TextOverlay::TextOverlay(const Ogre::MovableObject* obj, const Ogre::Camera* cam mElement->setMetricsMode(Ogre::GMM_RELATIVE); mElement->setDimensions(1.0, 1.0); mElement->setMetricsMode(Ogre::GMM_PIXELS); - mElement->setPosition(sMargin*2, sMargin*1.3); // 1.3 & 2 = fudge factor + mElement->setPosition(2*fontHeight()/3, 1.3*fontHeight()/3); // 1.3 & 2 = fudge factor mElement->setFontName("DejaVuLGC"); mElement->setCharHeight(fontHeight()); // NOTE: seems that this is required as well as font->setTrueTypeSize() @@ -214,11 +210,21 @@ TextOverlay::~TextOverlay() void TextOverlay::enable(bool enable) { + if(enable == mContainer->isVisible()) + return; + mEnabled = enable; if (enable) - mOverlay->show(); + mContainer->show(); else - mOverlay->hide(); + mContainer->hide(); + + Ogre::Root::getSingleton().renderOneFrame(); +} + +bool TextOverlay::isEnabled() +{ + return mEnabled; } void TextOverlay::setCaption(const Ogre::String& text) @@ -287,15 +293,27 @@ void TextOverlay::update(bool toggleOverlay) float viewportWidth = std::max(overlayMgr.getViewportWidth(), 1); // zero at the start float viewportHeight = std::max(overlayMgr.getViewportHeight(), 1); // zero at the start - float relTextWidth = (2*sMargin + textWidth() + sMargin) / viewportWidth; // 2 = fudge factor - float relTextHeight = (sMargin + fontHeight() + sMargin) / viewportHeight; + int width = 2*fontHeight()/3 + textWidth() + fontHeight()/3; // 2 = fudge factor + int height = fontHeight()/3 + fontHeight() + fontHeight()/3; - mContainer->setMetricsMode(Ogre::GMM_RELATIVE); - mContainer->setPosition(1-(min_x + max_x + relTextWidth)/2, 1-max_y-(relTextHeight-sMargin/viewportHeight)); + float relTextWidth = width / viewportWidth; + float relTextHeight = height / viewportHeight; + float posX = 1 - (min_x + max_x + relTextWidth)/2; + float posY = 1 - max_y - (relTextHeight-fontHeight()/3/viewportHeight); + + mContainer->setMetricsMode(Ogre::GMM_RELATIVE); + mContainer->setPosition(posX, posY); mContainer->setDimensions(relTextWidth, relTextHeight); + mPos = QRect(posX*viewportWidth, posY*viewportHeight, width, height); + Ogre::Root::getSingleton().renderOneFrame(); } +QRect TextOverlay::container() +{ + return mPos; +} + } diff --git a/apps/opencs/view/render/textoverlay.hpp b/apps/opencs/view/render/textoverlay.hpp index a90b30194..3f0c6ac57 100644 --- a/apps/opencs/view/render/textoverlay.hpp +++ b/apps/opencs/view/render/textoverlay.hpp @@ -1,6 +1,8 @@ #ifndef OPENCS_VIEW_TEXTOVERLAY_H #define OPENCS_VIEW_TEXTOVERLAY_H +#include + #include #include @@ -29,6 +31,7 @@ namespace CSVRender Ogre::FontPtr mFont; int mFontHeight; // in pixels Ogre::String mId; + QRect mPos; bool mEnabled; bool mOnScreen; // not used @@ -45,8 +48,11 @@ namespace CSVRender virtual ~TextOverlay(); void enable(bool enable); + bool isEnabled(); void setCaption(const Ogre::String& text); void update(bool toggleOverlay = false); + QRect container(); + Ogre::String getCaption() { return mCaption; } // FIXME: debug }; } diff --git a/apps/opencs/view/render/worldspacewidget.cpp b/apps/opencs/view/render/worldspacewidget.cpp index eca00fcba..fa304dd82 100644 --- a/apps/opencs/view/render/worldspacewidget.cpp +++ b/apps/opencs/view/render/worldspacewidget.cpp @@ -314,6 +314,3 @@ void CSVRender::WorldspaceWidget::elementSelectionChanged() setVisibilityMask (getElementMask()); flagAsModified(); } - -void CSVRender::WorldspaceWidget::updateOverlay(bool toggleOverlay) -{ } diff --git a/apps/opencs/view/render/worldspacewidget.hpp b/apps/opencs/view/render/worldspacewidget.hpp index a7d3a7882..92b0bd71a 100644 --- a/apps/opencs/view/render/worldspacewidget.hpp +++ b/apps/opencs/view/render/worldspacewidget.hpp @@ -88,8 +88,6 @@ namespace CSVRender CSMDoc::Document& getDocument(); - virtual void updateOverlay(bool toggleOverlay = false); - private: void dragEnterEvent(QDragEnterEvent *event);