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/misc/parse_color.hxx>
22 #include <simgear/scene/util/RenderConstants.hxx>
26 #include <osgText/Text>
27 #include <osgViewer/Viewer>
29 #include <boost/algorithm/string/predicate.hpp>
30 #include <boost/foreach.hpp>
38 //----------------------------------------------------------------------------
39 Canvas::CullCallback::CullCallback(const CanvasWeakPtr& canvas):
45 //----------------------------------------------------------------------------
46 void Canvas::CullCallback::operator()( osg::Node* node,
47 osg::NodeVisitor* nv )
49 if( (nv->getTraversalMask() & simgear::MODEL_BIT) && !_canvas.expired() )
50 _canvas.lock()->enableRendering();
55 //----------------------------------------------------------------------------
56 Canvas::Canvas(SGPropertyNode* node):
57 PropertyBasedElement(node),
63 _status(node, "status"),
64 _status_msg(node, "status-msg"),
65 _mouse_x(node, "mouse/x"),
66 _mouse_y(node, "mouse/y"),
67 _mouse_dx(node, "mouse/dx"),
68 _mouse_dy(node, "mouse/dy"),
69 _mouse_button(node, "mouse/button"),
70 _mouse_state(node, "mouse/state"),
71 _mouse_mod(node, "mouse/mod"),
72 _mouse_scroll(node, "mouse/scroll"),
73 _mouse_event(node, "mouse/event"),
74 _sampling_dirty(false),
80 setStatusFlags(MISSING_SIZE_X | MISSING_SIZE_Y);
83 //----------------------------------------------------------------------------
89 //----------------------------------------------------------------------------
90 void Canvas::setSystemAdapter(const SystemAdapterPtr& system_adapter)
92 _system_adapter = system_adapter;
93 _texture.setSystemAdapter(system_adapter);
96 //----------------------------------------------------------------------------
97 SystemAdapterPtr Canvas::getSystemAdapter() const
99 return _system_adapter;
102 //----------------------------------------------------------------------------
103 void Canvas::setCanvasMgr(CanvasMgr* canvas_mgr)
105 _canvas_mgr = canvas_mgr;
108 //----------------------------------------------------------------------------
109 CanvasMgr* Canvas::getCanvasMgr() const
114 //----------------------------------------------------------------------------
115 void Canvas::addDependentCanvas(const CanvasWeakPtr& canvas)
117 if( canvas.expired() )
123 "Canvas::addDependentCanvas: got an expired Canvas dependent on "
129 _dependent_canvases.insert(canvas);
132 //----------------------------------------------------------------------------
133 void Canvas::removeDependentCanvas(const CanvasWeakPtr& canvas)
135 _dependent_canvases.erase(canvas);
138 //----------------------------------------------------------------------------
139 void Canvas::enableRendering(bool force)
143 _render_dirty = true;
146 //----------------------------------------------------------------------------
147 void Canvas::update(double delta_time_sec)
149 if( !_texture.serviceable() )
151 if( _status != STATUS_OK )
154 _texture.setSize(_size_x, _size_y);
155 _texture.useImageCoords(true);
156 _texture.useStencil(true);
157 _texture.allocRT(/*_camera_callback*/);
159 osg::Camera* camera = _texture.getCamera();
161 osg::Vec4 clear_color(0.0f, 0.0f, 0.0f , 1.0f);
162 parseColor(_node->getStringValue("background"), clear_color);
163 camera->setClearColor(clear_color);
165 camera->addChild(_root_group->getMatrixTransform());
167 // Ensure objects are drawn in order of traversal
168 camera->getOrCreateStateSet()->setBinName("TraversalOrderBin");
170 if( _texture.serviceable() )
172 setStatusFlags(STATUS_OK);
176 setStatusFlags(CREATE_FAILED);
181 if( _visible || _render_always )
185 // Also mark all dependent (eg. recursively used) canvases as dirty
186 BOOST_FOREACH(CanvasWeakPtr canvas, _dependent_canvases)
188 if( !canvas.expired() )
189 canvas.lock()->_render_dirty = true;
193 _texture.setRender(_render_dirty);
195 _render_dirty = false;
199 _texture.setRender(false);
201 _root_group->update(delta_time_sec);
203 if( _sampling_dirty )
205 _texture.setSampling(
206 _node->getBoolValue("mipmapping"),
207 _node->getIntValue("coverage-samples"),
208 _node->getIntValue("color-samples")
210 _sampling_dirty = false;
211 _render_dirty = true;
214 while( !_dirty_placements.empty() )
216 SGPropertyNode *node = _dirty_placements.back();
217 _dirty_placements.pop_back();
219 if( node->getIndex() >= static_cast<int>(_placements.size()) )
221 _placements.resize(node->getIndex() + 1);
223 // Remove possibly existing placements
224 _placements[ node->getIndex() ].clear();
226 // Get new placements
227 PlacementFactoryMap::const_iterator placement_factory =
228 _placement_factories.find( node->getStringValue("type", "object") );
229 if( placement_factory != _placement_factories.end() )
231 Placements& placements = _placements[ node->getIndex() ] =
232 placement_factory->second
235 boost::static_pointer_cast<Canvas>(_self.lock())
240 placements.empty() ? "No match" : "Ok"
244 node->setStringValue("status-msg", "Unknown placement type");
248 //----------------------------------------------------------------------------
249 void Canvas::setSizeX(int sx)
255 // TODO resize if texture already allocated
258 setStatusFlags(MISSING_SIZE_X);
260 setStatusFlags(MISSING_SIZE_X, false);
262 // reset flag to allow creation with new size
263 setStatusFlags(CREATE_FAILED, false);
266 //----------------------------------------------------------------------------
267 void Canvas::setSizeY(int sy)
273 // TODO resize if texture already allocated
276 setStatusFlags(MISSING_SIZE_Y);
278 setStatusFlags(MISSING_SIZE_Y, false);
280 // reset flag to allow creation with new size
281 setStatusFlags(CREATE_FAILED, false);
284 //----------------------------------------------------------------------------
285 int Canvas::getSizeX() const
290 //----------------------------------------------------------------------------
291 int Canvas::getSizeY() const
296 //----------------------------------------------------------------------------
297 void Canvas::setViewWidth(int w)
299 if( _view_width == w )
303 _texture.setViewSize(_view_width, _view_height);
306 //----------------------------------------------------------------------------
307 void Canvas::setViewHeight(int h)
309 if( _view_height == h )
313 _texture.setViewSize(_view_width, _view_height);
316 //----------------------------------------------------------------------------
317 bool Canvas::handleMouseEvent(const MouseEvent& event)
321 _mouse_dx = event.dx;
322 _mouse_dy = event.dy;
323 _mouse_button = event.button;
324 _mouse_state = event.state;
325 _mouse_mod = event.mod;
326 _mouse_scroll = event.scroll;
327 // Always set event type last because all listeners are attached to it
328 _mouse_event = event.type;
330 if( _root_group.get() )
331 return _root_group->handleMouseEvent(event);
336 //----------------------------------------------------------------------------
337 void Canvas::childAdded( SGPropertyNode * parent,
338 SGPropertyNode * child )
340 if( parent != _node )
343 if( child->getNameString() == "placement" )
344 _dirty_placements.push_back(child);
345 else if( _root_group.get() )
346 static_cast<Element*>(_root_group.get())->childAdded(parent, child);
349 //----------------------------------------------------------------------------
350 void Canvas::childRemoved( SGPropertyNode * parent,
351 SGPropertyNode * child )
353 _render_dirty = true;
355 if( parent != _node )
358 if( child->getNameString() == "placement" )
359 _placements[ child->getIndex() ].clear();
360 else if( _root_group.get() )
361 static_cast<Element*>(_root_group.get())->childRemoved(parent, child);
364 //----------------------------------------------------------------------------
365 void Canvas::valueChanged(SGPropertyNode* node)
367 if( boost::starts_with(node->getNameString(), "status")
368 || node->getParent()->getNameString() == "bounding-box" )
370 _render_dirty = true;
373 if( node->getParent()->getParent() == _node
374 && node->getParent()->getNameString() == "placement" )
376 // prevent double updates...
377 for( size_t i = 0; i < _dirty_placements.size(); ++i )
379 if( node->getParent() == _dirty_placements[i] )
383 _dirty_placements.push_back(node->getParent());
385 else if( node->getParent() == _node )
387 if( node->getNameString() == "background" )
390 if( _texture.getCamera() && parseColor(node->getStringValue(), color) )
392 _texture.getCamera()->setClearColor(color);
393 _render_dirty = true;
396 else if( node->getNameString() == "mipmapping"
397 || node->getNameString() == "coverage-samples"
398 || node->getNameString() == "color-samples" )
400 _sampling_dirty = true;
402 else if( node->getNameString() == "render-always" )
404 _render_always = node->getBoolValue();
406 else if( node->getNameString() == "size" )
408 if( node->getIndex() == 0 )
409 setSizeX( node->getIntValue() );
410 else if( node->getIndex() == 1 )
411 setSizeY( node->getIntValue() );
413 else if( node->getNameString() == "view" )
415 if( node->getIndex() == 0 )
416 setViewWidth( node->getIntValue() );
417 else if( node->getIndex() == 1 )
418 setViewHeight( node->getIntValue() );
420 else if( node->getNameString() == "freeze" )
421 _texture.setRender( node->getBoolValue() );
428 if( !handled && _root_group.get() )
429 _root_group->valueChanged(node);
432 //----------------------------------------------------------------------------
433 osg::Texture2D* Canvas::getTexture() const
435 return _texture.getTexture();
438 //----------------------------------------------------------------------------
439 Canvas::CullCallbackPtr Canvas::getCullCallback() const
441 return _cull_callback;
444 //----------------------------------------------------------------------------
445 void Canvas::addPlacementFactory( const std::string& type,
446 PlacementFactory factory )
448 if( _placement_factories.find(type) != _placement_factories.end() )
453 "Canvas::addPlacementFactory: replace existing factor for type " << type
456 _placement_factories[type] = factory;
459 //----------------------------------------------------------------------------
460 void Canvas::setSelf(const PropertyBasedElementPtr& self)
462 PropertyBasedElement::setSelf(self);
464 CanvasPtr canvas = boost::static_pointer_cast<Canvas>(self);
466 _root_group.reset( new Group(canvas, _node) );
468 // Remove automatically created property listener as we forward them on our
470 _root_group->removeListener();
472 _cull_callback = new CullCallback(canvas);
475 //----------------------------------------------------------------------------
476 void Canvas::setStatusFlags(unsigned int flags, bool set)
479 _status = _status | flags;
481 _status = _status & ~flags;
482 // TODO maybe extend simgear::PropertyObject to allow |=, &= etc.
484 if( (_status & MISSING_SIZE_X) && (_status & MISSING_SIZE_Y) )
485 _status_msg = "Missing size";
486 else if( _status & MISSING_SIZE_X )
487 _status_msg = "Missing size-x";
488 else if( _status & MISSING_SIZE_Y )
489 _status_msg = "Missing size-y";
490 else if( _status & CREATE_FAILED )
491 _status_msg = "Creating render target failed";
492 else if( _status == STATUS_OK && !_texture.serviceable() )
493 _status_msg = "Creation pending...";
498 //----------------------------------------------------------------------------
499 Canvas::PlacementFactoryMap Canvas::_placement_factories;
501 } // namespace canvas
502 } // namespace simgear