1 // The canvas for rendering with the 2d API
3 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
15 // You should have received a copy of the GNU Library General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include <simgear/canvas/MouseEvent.hxx>
21 #include <simgear/canvas/CanvasPlacement.hxx>
22 #include <simgear/scene/util/parse_color.hxx>
23 #include <simgear/scene/util/RenderConstants.hxx>
27 #include <osgText/Text>
28 #include <osgViewer/Viewer>
30 #include <boost/algorithm/string/predicate.hpp>
31 #include <boost/foreach.hpp>
39 //----------------------------------------------------------------------------
40 Canvas::CullCallback::CullCallback(const CanvasWeakPtr& canvas):
46 //----------------------------------------------------------------------------
47 void Canvas::CullCallback::operator()( osg::Node* node,
48 osg::NodeVisitor* nv )
50 if( (nv->getTraversalMask() & simgear::MODEL_BIT) && !_canvas.expired() )
51 _canvas.lock()->enableRendering();
56 //----------------------------------------------------------------------------
57 Canvas::Canvas(SGPropertyNode* node):
58 PropertyBasedElement(node),
64 _status(node, "status"),
65 _status_msg(node, "status-msg"),
66 _mouse_x(node, "mouse/x"),
67 _mouse_y(node, "mouse/y"),
68 _mouse_dx(node, "mouse/dx"),
69 _mouse_dy(node, "mouse/dy"),
70 _mouse_button(node, "mouse/button"),
71 _mouse_state(node, "mouse/state"),
72 _mouse_mod(node, "mouse/mod"),
73 _mouse_scroll(node, "mouse/scroll"),
74 _mouse_event(node, "mouse/event"),
75 _sampling_dirty(false),
81 setStatusFlags(MISSING_SIZE_X | MISSING_SIZE_Y);
84 //----------------------------------------------------------------------------
90 //----------------------------------------------------------------------------
91 void Canvas::setSystemAdapter(const SystemAdapterPtr& system_adapter)
93 _system_adapter = system_adapter;
94 _texture.setSystemAdapter(system_adapter);
97 //----------------------------------------------------------------------------
98 SystemAdapterPtr Canvas::getSystemAdapter() const
100 return _system_adapter;
103 //----------------------------------------------------------------------------
104 void Canvas::setCanvasMgr(CanvasMgr* canvas_mgr)
106 _canvas_mgr = canvas_mgr;
109 //----------------------------------------------------------------------------
110 CanvasMgr* Canvas::getCanvasMgr() const
115 //----------------------------------------------------------------------------
116 void Canvas::addDependentCanvas(const CanvasWeakPtr& canvas)
118 if( canvas.expired() )
124 "Canvas::addDependentCanvas: got an expired Canvas dependent on "
130 _dependent_canvases.insert(canvas);
133 //----------------------------------------------------------------------------
134 void Canvas::removeDependentCanvas(const CanvasWeakPtr& canvas)
136 _dependent_canvases.erase(canvas);
139 //----------------------------------------------------------------------------
140 GroupPtr Canvas::createGroup(const std::string& name)
142 return boost::dynamic_pointer_cast<Group>
144 _root_group->createChild("group", name)
148 //----------------------------------------------------------------------------
149 void Canvas::enableRendering(bool force)
153 _render_dirty = true;
156 //----------------------------------------------------------------------------
157 void Canvas::update(double delta_time_sec)
159 if( !_texture.serviceable() )
161 if( _status != STATUS_OK )
164 _texture.setSize(_size_x, _size_y);
165 _texture.useImageCoords(true);
166 _texture.useStencil(true);
167 _texture.allocRT(/*_camera_callback*/);
169 osg::Camera* camera = _texture.getCamera();
171 osg::Vec4 clear_color(0.0f, 0.0f, 0.0f , 1.0f);
172 parseColor(_node->getStringValue("background"), clear_color);
173 camera->setClearColor(clear_color);
175 camera->addChild(_root_group->getMatrixTransform());
177 // Ensure objects are drawn in order of traversal
178 camera->getOrCreateStateSet()->setBinName("TraversalOrderBin");
180 if( _texture.serviceable() )
182 setStatusFlags(STATUS_OK);
186 setStatusFlags(CREATE_FAILED);
191 if( _visible || _render_always )
195 // Also mark all dependent (eg. recursively used) canvases as dirty
196 BOOST_FOREACH(CanvasWeakPtr canvas, _dependent_canvases)
198 if( !canvas.expired() )
199 canvas.lock()->_render_dirty = true;
203 _texture.setRender(_render_dirty);
205 _render_dirty = false;
209 _texture.setRender(false);
211 _root_group->update(delta_time_sec);
213 if( _sampling_dirty )
215 _texture.setSampling(
216 _node->getBoolValue("mipmapping"),
217 _node->getIntValue("coverage-samples"),
218 _node->getIntValue("color-samples")
220 _sampling_dirty = false;
221 _render_dirty = true;
224 while( !_dirty_placements.empty() )
226 SGPropertyNode *node = _dirty_placements.back();
227 _dirty_placements.pop_back();
229 if( node->getIndex() >= static_cast<int>(_placements.size()) )
231 _placements.resize(node->getIndex() + 1);
233 // Remove possibly existing placements
234 _placements[ node->getIndex() ].clear();
236 // Get new placements
237 PlacementFactoryMap::const_iterator placement_factory =
238 _placement_factories.find( node->getStringValue("type", "object") );
239 if( placement_factory != _placement_factories.end() )
241 Placements& placements = _placements[ node->getIndex() ] =
242 placement_factory->second
245 boost::static_pointer_cast<Canvas>(_self.lock())
250 placements.empty() ? "No match" : "Ok"
254 node->setStringValue("status-msg", "Unknown placement type");
258 //----------------------------------------------------------------------------
259 void Canvas::setSizeX(int sx)
265 // TODO resize if texture already allocated
268 setStatusFlags(MISSING_SIZE_X);
270 setStatusFlags(MISSING_SIZE_X, false);
272 // reset flag to allow creation with new size
273 setStatusFlags(CREATE_FAILED, false);
276 //----------------------------------------------------------------------------
277 void Canvas::setSizeY(int sy)
283 // TODO resize if texture already allocated
286 setStatusFlags(MISSING_SIZE_Y);
288 setStatusFlags(MISSING_SIZE_Y, false);
290 // reset flag to allow creation with new size
291 setStatusFlags(CREATE_FAILED, false);
294 //----------------------------------------------------------------------------
295 int Canvas::getSizeX() const
300 //----------------------------------------------------------------------------
301 int Canvas::getSizeY() const
306 //----------------------------------------------------------------------------
307 void Canvas::setViewWidth(int w)
309 if( _view_width == w )
313 _texture.setViewSize(_view_width, _view_height);
316 //----------------------------------------------------------------------------
317 void Canvas::setViewHeight(int h)
319 if( _view_height == h )
323 _texture.setViewSize(_view_width, _view_height);
326 //----------------------------------------------------------------------------
327 bool Canvas::handleMouseEvent(const MouseEvent& event)
331 _mouse_dx = event.dx;
332 _mouse_dy = event.dy;
333 _mouse_button = event.button;
334 _mouse_state = event.state;
335 _mouse_mod = event.mod;
336 _mouse_scroll = event.scroll;
337 // Always set event type last because all listeners are attached to it
338 _mouse_event = event.type;
340 if( _root_group.get() )
341 return _root_group->handleMouseEvent(event);
346 //----------------------------------------------------------------------------
347 void Canvas::childAdded( SGPropertyNode * parent,
348 SGPropertyNode * child )
350 if( parent != _node )
353 if( child->getNameString() == "placement" )
354 _dirty_placements.push_back(child);
355 else if( _root_group.get() )
356 static_cast<Element*>(_root_group.get())->childAdded(parent, child);
359 //----------------------------------------------------------------------------
360 void Canvas::childRemoved( SGPropertyNode * parent,
361 SGPropertyNode * child )
363 _render_dirty = true;
365 if( parent != _node )
368 if( child->getNameString() == "placement" )
369 _placements[ child->getIndex() ].clear();
370 else if( _root_group.get() )
371 static_cast<Element*>(_root_group.get())->childRemoved(parent, child);
374 //----------------------------------------------------------------------------
375 void Canvas::valueChanged(SGPropertyNode* node)
377 if( boost::starts_with(node->getNameString(), "status")
378 || node->getParent()->getNameString() == "bounding-box" )
380 _render_dirty = true;
383 if( node->getParent()->getParent() == _node
384 && node->getParent()->getNameString() == "placement" )
386 bool placement_dirty = false;
387 BOOST_FOREACH(Placements& placements, _placements)
389 BOOST_FOREACH(PlacementPtr& placement, placements)
391 // check if change can be directly handled by placement
392 if( placement->getProps() == node->getParent()
393 && !placement->childChanged(node) )
394 placement_dirty = true;
398 if( !placement_dirty )
401 // prevent double updates...
402 for( size_t i = 0; i < _dirty_placements.size(); ++i )
404 if( node->getParent() == _dirty_placements[i] )
408 _dirty_placements.push_back(node->getParent());
410 else if( node->getParent() == _node )
412 if( node->getNameString() == "background" )
415 if( _texture.getCamera() && parseColor(node->getStringValue(), color) )
417 _texture.getCamera()->setClearColor(color);
418 _render_dirty = true;
421 else if( node->getNameString() == "mipmapping"
422 || node->getNameString() == "coverage-samples"
423 || node->getNameString() == "color-samples" )
425 _sampling_dirty = true;
427 else if( node->getNameString() == "render-always" )
429 _render_always = node->getBoolValue();
431 else if( node->getNameString() == "size" )
433 if( node->getIndex() == 0 )
434 setSizeX( node->getIntValue() );
435 else if( node->getIndex() == 1 )
436 setSizeY( node->getIntValue() );
438 else if( node->getNameString() == "view" )
440 if( node->getIndex() == 0 )
441 setViewWidth( node->getIntValue() );
442 else if( node->getIndex() == 1 )
443 setViewHeight( node->getIntValue() );
445 else if( node->getNameString() == "freeze" )
446 _texture.setRender( node->getBoolValue() );
453 if( !handled && _root_group.get() )
454 _root_group->valueChanged(node);
457 //----------------------------------------------------------------------------
458 osg::Texture2D* Canvas::getTexture() const
460 return _texture.getTexture();
463 //----------------------------------------------------------------------------
464 Canvas::CullCallbackPtr Canvas::getCullCallback() const
466 return _cull_callback;
469 //----------------------------------------------------------------------------
470 void Canvas::addPlacementFactory( const std::string& type,
471 PlacementFactory factory )
473 if( _placement_factories.find(type) != _placement_factories.end() )
478 "Canvas::addPlacementFactory: replace existing factor for type " << type
481 _placement_factories[type] = factory;
484 //----------------------------------------------------------------------------
485 void Canvas::setSelf(const PropertyBasedElementPtr& self)
487 PropertyBasedElement::setSelf(self);
489 CanvasPtr canvas = boost::static_pointer_cast<Canvas>(self);
491 _root_group.reset( new Group(canvas, _node) );
493 // Remove automatically created property listener as we forward them on our
495 _root_group->removeListener();
497 _cull_callback = new CullCallback(canvas);
500 //----------------------------------------------------------------------------
501 void Canvas::setStatusFlags(unsigned int flags, bool set)
504 _status = _status | flags;
506 _status = _status & ~flags;
507 // TODO maybe extend simgear::PropertyObject to allow |=, &= etc.
509 if( (_status & MISSING_SIZE_X) && (_status & MISSING_SIZE_Y) )
510 _status_msg = "Missing size";
511 else if( _status & MISSING_SIZE_X )
512 _status_msg = "Missing size-x";
513 else if( _status & MISSING_SIZE_Y )
514 _status_msg = "Missing size-y";
515 else if( _status & CREATE_FAILED )
516 _status_msg = "Creating render target failed";
517 else if( _status == STATUS_OK && !_texture.serviceable() )
518 _status_msg = "Creation pending...";
523 //----------------------------------------------------------------------------
524 Canvas::PlacementFactoryMap Canvas::_placement_factories;
526 } // namespace canvas
527 } // namespace simgear