Skip to content

Commit

Permalink
Add Icon for symbolizer like the WindonMessage, need to refactore the…
Browse files Browse the repository at this point in the history
… PopupNode in symbology example to manage different kind of widget
  • Loading branch information
Cedric Pinson committed Apr 5, 2010
1 parent 54f8d4d commit 38dca80
Show file tree
Hide file tree
Showing 7 changed files with 439 additions and 69 deletions.
Binary file added data/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
146 changes: 125 additions & 21 deletions src/applications/osgearth_symbology/osgearth_symbology.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <osgEarthSymbology/ModelSymbolizer>
#include <osgEarthSymbology/WindowManager>
#include <osgEarthSymbology/WidgetMessageBox>
#include <osgEarthSymbology/WidgetIcon>
#include <osg/MatrixTransform>
#include <osg/Geometry>
#include <osgUtil/Tessellator>
Expand Down Expand Up @@ -120,7 +121,7 @@ struct SampleGeometryInput : public GeometryInput
struct NodePopup : public osg::Node
{

NodePopup() { setDataVariance(osg::Object::DYNAMIC); setCullingActive(false);}
NodePopup() { setDataVariance(osg::Object::DYNAMIC); setCullingActive(false); _hide = true;}

void setWindowManager(WindowManager* windowmanager) { _wm = windowmanager; }
void setMessageBox(const WidgetMessageBox& popup) { _popup = popup; }
Expand All @@ -135,17 +136,37 @@ struct NodePopup : public osg::Node
if (nv.validNodeMask(*this))
{
if (nv.getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR) {
_popup.getWindow()->setPosition(osgWidget::Point(osg::round(_positionOnScreen[0]), osg::round(_positionOnScreen[1]), _popup.getWindow()->getPosition()[2] ));
osg::Vec2 size = osg::Vec2(_popup.getWindow()->getWidth(), _popup.getWindow()->getHeight());
osg::Vec2 positionCenteredAnchor = _positionOnScreen - size/2;
_popup.getWindow()->setX(osg::round(positionCenteredAnchor[0]));
_popup.getWindow()->setY(osg::round(positionCenteredAnchor[1]));
if (_hide)
_wm->getWindowManager()->removeChild(_popup.getWindow());
else {
if (!_wm->getWindowManager()->containsNode(_popup.getWindow()))
_wm->getWindowManager()->addChild(_popup.getWindow());
}

} else if (nv.getVisitorType() == osg::NodeVisitor::CULL_VISITOR) {
osg::CullStack* cull = dynamic_cast<osg::CullStack*>(&nv);
if (cull) {

std::vector<osg::Vec3> vertices;
vertices.push_back(osg::Vec3(0,0,0));
if (cull->isCulled(vertices)) {
_hide = true;
return;
} else {
_hide = false;
}

// compute 2d on screen
osg::Matrix matrix = *(cull->getModelViewMatrix());
matrix *= *(cull->getProjectionMatrix());
osg::Vec3 point = matrix.getTrans();



point *= 1.0/matrix(3,3); // w
float x = cull->getViewport()->width()/2 * (1.0 + point[0]);
float y = cull->getViewport()->height()/2 * (1.0 + point[1]);
Expand All @@ -165,6 +186,75 @@ struct NodePopup : public osg::Node
};



struct NodePopup2 : public osg::Node
{

NodePopup2() { setDataVariance(osg::Object::DYNAMIC); setCullingActive(false); _hide = true;}

void setWindowManager(WindowManager* windowmanager) { _wm = windowmanager; }

const WidgetIcon& getIcon() const { return _icon; }
WidgetIcon& getIcon() { return _icon; }

WindowManager* getWindowManager() { return _wm.get(); }

void accept(osg::NodeVisitor& nv)
{
setNumChildrenRequiringUpdateTraversal(1);
if (nv.validNodeMask(*this))
{
if (nv.getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR) {
osg::Vec2 size = osg::Vec2(_icon.getWindow()->getWidth(), _icon.getWindow()->getHeight());
osg::Vec2 positionCenteredAnchor = _positionOnScreen - size/2;
_icon.getWindow()->setX(osg::round(positionCenteredAnchor[0]));
_icon.getWindow()->setY(osg::round(positionCenteredAnchor[1]));
if (_hide)
_wm->getWindowManager()->removeChild(_icon.getWindow());
else {
if (!_wm->getWindowManager()->containsNode(_icon.getWindow()))
_wm->getWindowManager()->addChild(_icon.getWindow());
}

} else if (nv.getVisitorType() == osg::NodeVisitor::CULL_VISITOR) {
osg::CullStack* cull = dynamic_cast<osg::CullStack*>(&nv);
if (cull) {

std::vector<osg::Vec3> vertices;
vertices.push_back(osg::Vec3(0,0,0));
if (cull->isCulled(vertices)) {
_hide = true;
return;
} else {
_hide = false;
}

// compute 2d on screen
osg::Matrix matrix = *(cull->getModelViewMatrix());
matrix *= *(cull->getProjectionMatrix());
osg::Vec3 point = matrix.getTrans();



point *= 1.0/matrix(3,3); // w
float x = cull->getViewport()->width()/2 * (1.0 + point[0]);
float y = cull->getViewport()->height()/2 * (1.0 + point[1]);

_positionOnScreen = osg::Vec2(x, y);
}
}
}
};

protected:
osg::ref_ptr<WindowManager> _wm;
WidgetIcon _icon;
osg::Vec2 _positionOnScreen;
bool _hide;

};


struct PopUpSymbolizerContext : public SymbolizerContext
{
PopUpSymbolizerContext(WindowManager* windowmanager, osg::Camera* camera) : _wm(windowmanager), _camera(camera) {}
Expand Down Expand Up @@ -231,27 +321,41 @@ struct PopUpSymbolizer : public GeometrySymbolizer
{
osg::MatrixTransform* transform = new osg::MatrixTransform;
transform->setMatrix(osg::Matrix::translate(*it));
NodePopup* popupNode = new NodePopup;
transform->addChild(popupNode);
std::stringstream ss;
ss << "Sacre bleu" << std::endl;
ss << "I am at position " << *it << " miles" << std::endl;
std::string text = ss.str();
std::string title = "Hello popup";
WidgetMessageBox popup = WidgetMessageBox::popUp(image,
title,
text,
"",
font,
size);
ctx->_wm->getWindowManager()->addChild(popup.getWindow());

popup.getWindow()->setPosition(osgWidget::Point(0,0, -PopUpIndex));
popupNode->setWindowManager(ctx->_wm.get());
popupNode->setMessageBox(popup);

PopUpIndex++;
if (PopUpIndex % 2) {
NodePopup* popupNode = new NodePopup;
transform->addChild(popupNode);
std::stringstream ss;
ss << "Sacre bleu" << std::endl;
ss << "I am at position " << *it << " miles" << std::endl;
std::string text = ss.str();
std::string title = "Hello popup";
popupNode->getMessageBox().create(image,
title,
text,
"",
font,
size);
popupNode->getMessageBox().getWindow()->setPosition(osgWidget::Point(0,0, -PopUpIndex));
popupNode->setWindowManager(ctx->_wm.get());

} else {
osg::Image* icon = osgDB::readImageFile("../data/icon.png");
if (!icon)
osg::notify(osg::WARN) << "can't load ../data/icon.png" << std::endl;

NodePopup2* popupNode = new NodePopup2;
transform->addChild(popupNode);
if (PopUpIndex %3)
popupNode->getIcon().create(icon);
else
popupNode->getIcon().createWithBorder(image, icon);

popupNode->getIcon().getWindow()->setPosition(osgWidget::Point(0,0, -PopUpIndex));
popupNode->setWindowManager(ctx->_wm.get());
}

PopUpIndex++;
newSymbolized->addChild(transform);
transform->addChild(osgDB::readNodeFile("../data/tree.ive"));
}
Expand Down
2 changes: 2 additions & 0 deletions src/osgEarthSymbology/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ SET(LIB_PUBLIC_HEADERS
SymbolicNode
Symbolizer
WindowManager
WidgetIcon
WidgetMessageBox
)

Expand All @@ -57,6 +58,7 @@ ADD_LIBRARY(${LIB_NAME} SHARED
SymbolicNode.cpp
Symbolizer.cpp
WindowManager.cpp
WidgetIcon.cpp
WidgetMessageBox.cpp
)

Expand Down
51 changes: 51 additions & 0 deletions src/osgEarthSymbology/WidgetIcon
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
* Copyright 2008-2009 Pelican Ventures, Inc.
* http://osgearth.org
*
* osgEarth is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/

#ifndef OSGEARTHSYMBOLOGY_WIDGET_ICON_H
#define OSGEARTHSYMBOLOGY_WIDGET_ICON 1

#include <osgEarthSymbology/Common>
#include <osgWidget/Frame>
#include <osgWidget/Window>
#include <osgWidget/Label>

namespace osgEarth { namespace Symbology
{

class OSGEARTHSYMBOLOGY_EXPORT WidgetIcon
{
public:

osgWidget::Frame* getWindow();

bool createWithBorder(osg::Image* themeBorder,
osg::Image* icon);

bool create(osg::Image* icon);

protected:

osg::ref_ptr<osgWidget::Frame> _window;

};


}}

#endif
Loading

0 comments on commit 38dca80

Please sign in to comment.