1 ///@file 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
22 #include "canvas_fwd.hxx"
23 #include "ODGauge.hxx"
25 #include <simgear/canvas/elements/CanvasGroup.hxx>
26 #include <simgear/canvas/layout/Layout.hxx>
27 #include <simgear/math/SGRect.hxx>
28 #include <simgear/nasal/cppbind/NasalObject.hxx>
29 #include <simgear/props/PropertyBasedElement.hxx>
30 #include <simgear/props/propertyObject.hxx>
32 #include <osg/NodeCallback>
33 #include <osg/observer_ptr>
35 #include <boost/scoped_ptr.hpp>
40 /// Canvas 2D drawing API
47 public PropertyBasedElement,
56 LAYOUT_DIRTY = STATUS_DIRTY << 1,
57 MISSING_SIZE_X = LAYOUT_DIRTY << 1,
58 MISSING_SIZE_Y = MISSING_SIZE_X << 1,
59 MISSING_SIZE = MISSING_SIZE_X | MISSING_SIZE_Y,
60 CREATE_FAILED = MISSING_SIZE_Y << 1
64 * This callback is installed on every placement of the canvas in the
65 * scene to only render the canvas if at least one placement is visible
68 public osg::NodeCallback
71 CullCallback(const CanvasWeakPtr& canvas);
74 CanvasWeakPtr _canvas;
76 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
78 typedef osg::ref_ptr<CullCallback> CullCallbackPtr;
80 Canvas(SGPropertyNode* node);
82 virtual void onDestroy();
84 void setCanvasMgr(CanvasMgr* canvas_mgr);
85 CanvasMgr* getCanvasMgr() const;
90 * Add a canvas which should be marked as dirty upon any change to this
93 * This mechanism is used to eg. redraw a canvas if it's displaying
94 * another canvas (recursive canvases)
96 void addParentCanvas(const CanvasWeakPtr& canvas);
99 * Add a canvas which should be marked visible if this canvas is visible.
101 void addChildCanvas(const CanvasWeakPtr& canvas);
104 * Stop notifying the given canvas upon changes
106 void removeParentCanvas(const CanvasWeakPtr& canvas);
107 void removeChildCanvas(const CanvasWeakPtr& canvas);
112 GroupPtr createGroup(const std::string& name = "");
115 * Get an existing group with the given name
117 GroupPtr getGroup(const std::string& name);
120 * Get an existing group with the given name or otherwise create a new
123 GroupPtr getOrCreateGroup(const std::string& name);
126 * Get the root group of the canvas
128 GroupPtr getRootGroup();
131 * Set the layout of the canvas (the layout will automatically update with
132 * the viewport size of the canvas)
134 void setLayout(const LayoutRef& layout);
137 * Enable rendering for the next frame
139 * @param force Force redraw even if nothing has changed (if dirty flag
142 void enableRendering(bool force = false);
144 void update(double delta_time_sec);
146 bool addEventListener(const std::string& type, const EventListener& cb);
147 bool dispatchEvent(const EventPtr& event);
149 void setSizeX(int sx);
150 void setSizeY(int sy);
152 int getSizeX() const;
153 int getSizeY() const;
155 void setViewWidth(int w);
156 void setViewHeight(int h);
158 int getViewWidth() const;
159 int getViewHeight() const;
160 SGRect<int> getViewport() const;
162 bool handleMouseEvent(const MouseEventPtr& event);
163 bool propagateEvent( EventPtr const& event,
164 EventPropagationPath const& path );
166 virtual void childAdded( SGPropertyNode * parent,
167 SGPropertyNode * child );
168 virtual void childRemoved( SGPropertyNode * parent,
169 SGPropertyNode * child );
170 virtual void valueChanged (SGPropertyNode * node);
172 osg::Texture2D* getTexture() const;
174 CullCallbackPtr getCullCallback() const;
176 void reloadPlacements( const std::string& type = std::string() );
177 static void addPlacementFactory( const std::string& type,
178 PlacementFactory factory );
179 static void removePlacementFactory(const std::string& type);
182 * Set global SystemAdapter for all Canvas/ODGauge instances.
184 * The SystemAdapter is responsible for application specific operations
185 * like loading images/fonts and adding/removing cameras to the scene
188 static void setSystemAdapter(const SystemAdapterPtr& system_adapter);
189 static SystemAdapterPtr getSystemAdapter();
193 CanvasMgr *_canvas_mgr;
195 boost::scoped_ptr<EventManager> _event_manager;
202 PropertyObject<int> _status;
203 PropertyObject<std::string> _status_msg;
205 bool _sampling_dirty,
211 GroupPtr _root_group;
214 CullCallbackPtr _cull_callback;
215 bool _render_always; //<! Used to disable automatic lazy rendering (culling)
217 std::vector<SGPropertyNode*> _dirty_placements;
218 std::vector<Placements> _placements;
219 std::set<CanvasWeakPtr> _parent_canvases, //<! Canvases showing this canvas
220 _child_canvases; //<! Canvases displayed within
223 typedef std::map<std::string, PlacementFactory> PlacementFactoryMap;
224 static PlacementFactoryMap _placement_factories;
226 void setStatusFlags(unsigned int flags, bool set = true);
230 static SystemAdapterPtr _system_adapter;
232 Canvas(const Canvas&); // = delete;
233 Canvas& operator=(const Canvas&); // = delete;
236 } // namespace canvas
237 } // namespace simgear
239 #endif /* CANVAS_HXX_ */