model_data->getNode(),
placement,
canvas->getTexture(),
- canvas->getCullCallback()
+ canvas->getCullCallback(),
+ canvas
);
}
&FGODGauge::set_aircraft_texture,
_1,
boost::bind(&sc::Canvas::getTexture, _2),
- boost::bind(&sc::Canvas::getCullCallback, _2)
+ boost::bind(&sc::Canvas::getCullCallback, _2),
+ _2
)
);
return false;
namespace sc = simgear::canvas;
- sc::MouseEventPtr event(new sc::MouseEvent);
- event->time = ea.getTime();
+ sc::MouseEventPtr event(new sc::MouseEvent(ea));
event->screen_pos.x() = 0.5 * (ea.getXnormalized() + 1) * _width + 0.5;
event->screen_pos.y() = 0.5 * (ea.getYnormalized() + 1) * _height + 0.5;
_last_y = event->getScreenY();
event->client_pos = event->screen_pos;
- event->button = ea.getButton();
- event->state = ea.getButtonMask();
- event->mod = ea.getModKeyMask();
if( !_resize_window.expired() )
{
#include <osg/Camera>
#include <osg/Geode>
#include <osg/NodeVisitor>
-#include <osg/Material>
#include <osg/Matrix>
#include <osg/PolygonMode>
#include <osg/ShadeModel>
#include <osgDB/FileNameUtils>
+#include <simgear/canvas/CanvasObjectPlacement.hxx>
#include <simgear/scene/material/EffectGeode.hxx>
#include <simgear/scene/util/RenderConstants.hxx>
osg::Texture2D* new_texture ):
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
_tex_name( osgDB::getSimpleFileName(name) ),
- _new_texture(new_texture)
+ _new_texture(new_texture),
+ _cull_callback(0)
{}
ReplaceStaticTextureVisitor( SGPropertyNode* placement,
osg::Texture2D* new_texture,
- osg::NodeCallback* cull_callback = 0 ):
+ osg::NodeCallback* cull_callback = 0,
+ const simgear::canvas::CanvasWeakPtr& canvas =
+ simgear::canvas::CanvasWeakPtr() ):
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
_tex_name( osgDB::getSimpleFileName(
placement->getStringValue("texture"))
_parent_name( placement->getStringValue("parent") ),
_node(placement),
_new_texture(new_texture),
- _cull_callback(cull_callback)
+ _cull_callback(cull_callback),
+ _canvas(canvas)
{
if( _tex_name.empty()
&& _node_name.empty()
osg::StateAttribute::ON );
_placements.push_back( simgear::canvas::PlacementPtr(
- new ObjectPlacement(_node, group)
+ new simgear::canvas::ObjectPlacement(_node, group, _canvas)
));
SG_LOG
protected:
- class ObjectPlacement:
- public simgear::canvas::Placement
- {
- public:
-
- ObjectPlacement( SGPropertyNode* node,
- GroupPtr group ):
- Placement(node),
- _group(group)
- {
- // TODO make more generic and extendable for more properties
- if( node->hasValue("emission") )
- setEmission( node->getFloatValue("emission") );
- }
-
- virtual bool childChanged(SGPropertyNode* node)
- {
- if( node->getParent() != _node )
- return false;
-
- if( node->getNameString() == "emission" )
- setEmission( node->getFloatValue() );
- else
- return false;
-
- return true;
- }
-
- void setEmission(float emit)
- {
- emit = SGMiscf::clip(emit, 0, 1);
-
- if( !_material )
- {
- _material = new osg::Material;
- _material->setColorMode(osg::Material::OFF);
- _material->setDataVariance(osg::Object::DYNAMIC);
- _group->getOrCreateStateSet()
- ->setAttribute(_material, ( osg::StateAttribute::ON
- | osg::StateAttribute::OVERRIDE ) );
- }
-
- _material->setEmission(
- osg::Material::FRONT_AND_BACK,
- osg::Vec4(emit, emit, emit, emit)
- );
- }
-
- /**
- * Remove placement from the scene
- */
- virtual ~ObjectPlacement()
- {
- assert( _group->getNumChildren() == 1 );
- osg::Node *child = _group->getChild(0);
-
- if( _group->getNumParents() )
- {
- osg::Group *parent = _group->getParent(0);
- parent->addChild(child);
- parent->removeChild(_group);
- }
-
- _group->removeChild(child);
- }
-
- private:
- GroupPtr _group;
- MaterialPtr _material;
- };
-
std::string _tex_name, ///<! Name of texture to be replaced
_node_name, ///<! Only replace if node name matches
_parent_name; ///<! Only replace if any parent node matches
osg::Texture2D *_new_texture;
osg::NodeCallback *_cull_callback;
- simgear::canvas::Placements _placements;
+ simgear::canvas::CanvasWeakPtr _canvas;
+ simgear::canvas::Placements _placements;
};
//------------------------------------------------------------------------------
FGODGauge::set_texture( osg::Node* branch,
SGPropertyNode* placement,
osg::Texture2D* new_texture,
- osg::NodeCallback* cull_callback )
+ osg::NodeCallback* cull_callback,
+ const simgear::canvas::CanvasWeakPtr& canvas )
{
- ReplaceStaticTextureVisitor visitor(placement, new_texture, cull_callback);
+ ReplaceStaticTextureVisitor visitor( placement,
+ new_texture,
+ cull_callback,
+ canvas );
branch->accept(visitor);
return visitor.getPlacements();
}
simgear::canvas::Placements
FGODGauge::set_aircraft_texture( SGPropertyNode* placement,
osg::Texture2D* new_texture,
- osg::NodeCallback* cull_callback )
+ osg::NodeCallback* cull_callback,
+ const simgear::canvas::CanvasWeakPtr& canvas )
{
return set_texture
(
globals->get_scenery()->get_aircraft_branch(),
placement,
new_texture,
- cull_callback
+ cull_callback,
+ canvas
);
}
set_texture( osg::Node* branch,
SGPropertyNode* placement,
osg::Texture2D* new_texture,
- osg::NodeCallback* cull_callback = 0 );
+ osg::NodeCallback* cull_callback = 0,
+ const simgear::canvas::CanvasWeakPtr& canvas =
+ simgear::canvas::CanvasWeakPtr() );
/**
* Replace an opengl texture name inside the aircraft scene graph.
simgear::canvas::Placements
set_aircraft_texture( SGPropertyNode* placement,
osg::Texture2D* new_texture,
- osg::NodeCallback* cull_callback = 0 );
+ osg::NodeCallback* cull_callback = 0,
+ const simgear::canvas::CanvasWeakPtr& canvas =
+ simgear::canvas::CanvasWeakPtr() );
};
const int MAX_MICE = 1;
const int MAX_MOUSE_BUTTONS = 8;
+typedef std::vector<SGSceneryPick> SGSceneryPicks;
+typedef SGSharedPtr<SGPickCallback> SGPickCallbackPtr;
+typedef std::list<SGPickCallbackPtr> SGPickCallbackList;
+
////////////////////////////////////////////////////////////////////////
/**
* List of currently pressed mouse button events
*/
-class ActivePickCallbacks : public std::map<int, std::list<SGSharedPtr<SGPickCallback> > > {
-public:
+class ActivePickCallbacks:
+ public std::map<int, SGPickCallbackList>
+{
+ public:
void update( double dt, unsigned int keyModState );
void init( int button, const osgGA::GUIEventAdapter* ea );
};
// That is they get sorted by distance and by scenegraph depth.
// The nearest one is the first one and the deepest
// (the most specialized one in the scenegraph) is the first.
- std::vector<SGSceneryPick> pickList;
+ SGSceneryPicks pickList;
if (!globals->get_renderer()->pick(pickList, windowPos)) {
return;
}
- std::vector<SGSceneryPick>::const_iterator i;
+ SGSceneryPicks::const_iterator i;
for (i = pickList.begin(); i != pickList.end(); ++i) {
- if (i->callback->buttonPressed(button, ea, i->info)) {
+ if (i->callback->buttonPressed(button, *ea, i->info)) {
(*this)[button].push_back(i->callback);
return;
}
{
// handle repeatable mouse press events
for( iterator mi = begin(); mi != end(); ++mi ) {
- std::list<SGSharedPtr<SGPickCallback> >::iterator li;
+ SGPickCallbackList::iterator li;
for (li = mi->second.begin(); li != mi->second.end(); ++li) {
(*li)->update(dt, keyModState);
}
mouse_mode * modes;
};
+static
+const SGSceneryPick*
+getPick( const SGSceneryPicks& pick_list,
+ const SGPickCallback* cb )
+{
+ for(size_t i = 0; i < pick_list.size(); ++i)
+ if( pick_list[i].callback == cb )
+ return &pick_list[i];
+
+ return 0;
+}
+
////////////////////////////////////////////////////////////////////////
class FGMouseInput::FGMouseInputPrivate : public SGPropertyChangeListener
void doHoverPick(const osg::Vec2d& windowPos)
{
- std::vector<SGSceneryPick> pickList;
- SGPickCallback::Priority priority = SGPickCallback::PriorityScenery;
-
FGMouseCursor::Cursor cur = FGMouseCursor::CURSOR_ARROW;
bool explicitCursor = false;
bool didPick = false;
-
- if (globals->get_renderer()->pick(pickList, windowPos)) {
+
+ SGPickCallback::Priority priority = SGPickCallback::PriorityScenery;
+ SGSceneryPicks pickList;
+ globals->get_renderer()->pick(pickList, windowPos);
+
+ SGSceneryPicks::const_iterator i;
+ for( i = pickList.begin(); i != pickList.end(); ++i )
+ {
+ bool done = i->callback->hover(windowPos, i->info);
+ std::string curName(i->callback->getCursor());
+ if (!curName.empty()) {
+ explicitCursor = true;
+ cur = FGMouseCursor::cursorFromString(curName.c_str());
+ }
- std::vector<SGSceneryPick>::const_iterator i;
- for (i = pickList.begin(); i != pickList.end(); ++i) {
- bool done = i->callback->hover(windowPos, i->info);
- std::string curName(i->callback->getCursor());
- if (!curName.empty()) {
- explicitCursor = true;
- cur = FGMouseCursor::cursorFromString(curName.c_str());
- }
-
// if the callback is of higher prioirty (lower enum index),
// record that.
- if (i->callback->getPriority() < priority) {
- priority = i->callback->getPriority();
- }
-
- if (done) {
- didPick = true;
- break;
- }
- } // of picks iteration
- } else { // of have valid pick
+ if (i->callback->getPriority() < priority) {
+ priority = i->callback->getPriority();
+ }
+
+ if (done) {
+ didPick = true;
+ break;
+ }
+ } // of picks iteration
+
+ // Check if any pick from the previous iteration has disappeared. If so
+ // notify the callback that the mouse has left its element.
+ for( i = _previous_picks.begin(); i != _previous_picks.end(); ++i )
+ {
+ if( !getPick(pickList, i->callback) )
+ i->callback->mouseLeave(windowPos);
}
+ _previous_picks = pickList;
if (!explicitCursor && (priority == SGPickCallback::PriorityPanel)) {
cur = FGMouseCursor::CURSOR_HAND;
{
FGMouseCursor::Cursor cur = FGMouseCursor::CURSOR_CLOSED_HAND;
- BOOST_FOREACH(SGPickCallback* cb, activePickCallbacks[0]) {
- cb->mouseMoved(ea);
- std::string curName(cb->getCursor());
- if (!curName.empty()) {
- cur = FGMouseCursor::cursorFromString(curName.c_str());
- }
+ osg::Vec2d windowPos;
+ flightgear::eventToWindowCoords(ea, windowPos.x(), windowPos.y());
+
+ SGSceneryPicks pickList;
+ if( !globals->get_renderer()->pick(pickList, windowPos) )
+ return;
+
+ for( ActivePickCallbacks::iterator mi = activePickCallbacks.begin();
+ mi != activePickCallbacks.end();
+ ++mi )
+ {
+ SGPickCallbackList::iterator li;
+ for( li = mi->second.begin(); li != mi->second.end(); ++li )
+ {
+ const SGSceneryPick* pick = getPick(pickList, *li);
+ (*li)->mouseMoved(*ea, pick ? &pick->info : 0);
+
+ std::string curName((*li)->getCursor());
+ if( !curName.empty() )
+ cur = FGMouseCursor::cursorFromString(curName.c_str());
+ }
}
FGMouseCursor::instance()->setCursor(cur);
}
ActivePickCallbacks activePickCallbacks;
+ SGSceneryPicks _previous_picks;
mouse mice[MAX_MICE];
// requested, and return if one of
// them consumes the event.
- if (updown != MOUSE_BUTTON_DOWN) {
+ osg::Vec2d windowPos;
+ flightgear::eventToWindowCoords(ea, windowPos.x(), windowPos.y());
+
+ SGSceneryPicks pickList;
+ globals->get_renderer()->pick(pickList, windowPos);
+
+ if( updown != MOUSE_BUTTON_DOWN )
+ {
// Execute the mouse up event in any case, may be we should
// stop processing here?
- while (!d->activePickCallbacks[b].empty()) {
- d->activePickCallbacks[b].front()->buttonReleased(ea->getModKeyMask());
- d->activePickCallbacks[b].pop_front();
+
+ SGPickCallbackList& callbacks = d->activePickCallbacks[b];
+
+ while( !callbacks.empty() )
+ {
+ SGPickCallbackPtr& cb = callbacks.front();
+ const SGSceneryPick* pick = getPick(pickList, cb);
+ cb->buttonReleased(ea->getModKeyMask(), *ea, pick ? &pick->info : 0);
+
+ callbacks.pop_front();
}
}
}
} else {
// do a hover pick now, to fix up cursor
- osg::Vec2d windowPos;
- flightgear::eventToWindowCoords(ea, windowPos.x(), windowPos.y());
d->doHoverPick(windowPos);
} // mouse button was released
} // of pass-through mode
}
}
+typedef osgUtil::LineSegmentIntersector::Intersection Intersection;
+SGVec2d uvFromIntersection(const Intersection& hit)
+{
+ // Taken from http://trac.openscenegraph.org/projects/osg/browser/OpenSceneGraph/trunk/examples/osgmovie/osgmovie.cpp
+
+ osg::Drawable* drawable = hit.drawable.get();
+ osg::Geometry* geometry = drawable ? drawable->asGeometry() : 0;
+ osg::Vec3Array* vertices =
+ geometry ? dynamic_cast<osg::Vec3Array*>(geometry->getVertexArray()) : 0;
+
+ if( !vertices )
+ {
+ SG_LOG(SG_INPUT, SG_WARN, "Unable to get vertices for intersection.");
+ return SGVec2d(-9999,-9999);
+ }
+
+ // get the vertex indices.
+ const Intersection::IndexList& indices = hit.indexList;
+ const Intersection::RatioList& ratios = hit.ratioList;
+
+ if( indices.size() != 3 || ratios.size() != 3 )
+ {
+ SG_LOG( SG_INPUT,
+ SG_WARN,
+ "Intersection has insufficient indices to work with." );
+ return SGVec2d(-9999,-9999);
+ }
+
+ unsigned int i1 = indices[0];
+ unsigned int i2 = indices[1];
+ unsigned int i3 = indices[2];
+
+ float r1 = ratios[0];
+ float r2 = ratios[1];
+ float r3 = ratios[2];
+
+ osg::Array* texcoords =
+ (geometry->getNumTexCoordArrays() > 0) ? geometry->getTexCoordArray(0) : 0;
+ osg::Vec2Array* texcoords_Vec2Array =
+ dynamic_cast<osg::Vec2Array*>(texcoords);
+
+ if( !texcoords_Vec2Array )
+ {
+ SG_LOG(SG_INPUT, SG_WARN, "Unable to get texcoords for intersection.");
+ return SGVec2d(-9999,-9999);
+ }
+
+ // we have tex coord array so now we can compute the final tex coord at the
+ // point of intersection.
+ osg::Vec2 tc1 = (*texcoords_Vec2Array)[i1];
+ osg::Vec2 tc2 = (*texcoords_Vec2Array)[i2];
+ osg::Vec2 tc3 = (*texcoords_Vec2Array)[i3];
+
+ return toSG( osg::Vec2d(tc1 * r1 + tc2 * r2 + tc3 * r3) );
+}
+
bool
FGRenderer::pick(std::vector<SGSceneryPick>& pickList, const osg::Vec2& windowPos)
{
SGSceneryPick sceneryPick;
sceneryPick.info.local = toSG(hit->getLocalIntersectPoint());
sceneryPick.info.wgs84 = toSG(hit->getWorldIntersectPoint());
+
+ if( pickCallback->needsUV() )
+ sceneryPick.info.uv = uvFromIntersection(*hit);
+
sceneryPick.callback = pickCallback;
pickList.push_back(sceneryPick);
}