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/math/SGRect.hxx>
27 #include <simgear/props/PropertyBasedElement.hxx>
28 #include <simgear/props/propertyObject.hxx>
29 #include <osg/NodeCallback>
30 #include <osg/observer_ptr>
32 #include <boost/scoped_ptr.hpp>
37 /// Canvas 2D drawing API
44 public PropertyBasedElement
52 MISSING_SIZE_X = STATUS_DIRTY << 1,
53 MISSING_SIZE_Y = MISSING_SIZE_X << 1,
54 CREATE_FAILED = MISSING_SIZE_Y << 1
58 * This callback is installed on every placement of the canvas in the
59 * scene to only render the canvas if at least one placement is visible
62 public osg::NodeCallback
65 CullCallback(const CanvasWeakPtr& canvas);
68 CanvasWeakPtr _canvas;
70 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
72 typedef osg::ref_ptr<CullCallback> CullCallbackPtr;
74 Canvas(SGPropertyNode* node);
76 virtual void onDestroy();
78 void setCanvasMgr(CanvasMgr* canvas_mgr);
79 CanvasMgr* getCanvasMgr() const;
84 * Add a canvas which should be marked as dirty upon any change to this
87 * This mechanism is used to eg. redraw a canvas if it's displaying
88 * another canvas (recursive canvases)
90 void addParentCanvas(const CanvasWeakPtr& canvas);
93 * Add a canvas which should be marked visible if this canvas is visible.
95 void addChildCanvas(const CanvasWeakPtr& canvas);
98 * Stop notifying the given canvas upon changes
100 void removeParentCanvas(const CanvasWeakPtr& canvas);
101 void removeChildCanvas(const CanvasWeakPtr& canvas);
106 GroupPtr createGroup(const std::string& name = "");
109 * Get an existing group with the given name
111 GroupPtr getGroup(const std::string& name);
114 * Get an existing group with the given name or otherwise create a new
117 GroupPtr getOrCreateGroup(const std::string& name);
120 * Get the root group of the canvas
122 GroupPtr getRootGroup();
125 * Enable rendering for the next frame
127 * @param force Force redraw even if nothing has changed (if dirty flag
130 void enableRendering(bool force = false);
132 void update(double delta_time_sec);
134 bool addEventListener(const std::string& type, const EventListener& cb);
136 void setSizeX(int sx);
137 void setSizeY(int sy);
139 int getSizeX() const;
140 int getSizeY() const;
142 void setViewWidth(int w);
143 void setViewHeight(int h);
145 int getViewWidth() const;
146 int getViewHeight() const;
147 SGRect<int> getViewport() const;
149 bool handleMouseEvent(const MouseEventPtr& event);
150 bool propagateEvent( EventPtr const& event,
151 EventPropagationPath const& path );
153 virtual void childAdded( SGPropertyNode * parent,
154 SGPropertyNode * child );
155 virtual void childRemoved( SGPropertyNode * parent,
156 SGPropertyNode * child );
157 virtual void valueChanged (SGPropertyNode * node);
159 osg::Texture2D* getTexture() const;
161 CullCallbackPtr getCullCallback() const;
163 void reloadPlacements( const std::string& type = std::string() );
164 static void addPlacementFactory( const std::string& type,
165 PlacementFactory factory );
166 static void removePlacementFactory(const std::string& type);
169 * Set global SystemAdapter for all Canvas/ODGauge instances.
171 * The SystemAdapter is responsible for application specific operations
172 * like loading images/fonts and adding/removing cameras to the scene
175 static void setSystemAdapter(const SystemAdapterPtr& system_adapter);
176 static SystemAdapterPtr getSystemAdapter();
180 CanvasMgr *_canvas_mgr;
182 boost::scoped_ptr<EventManager> _event_manager;
189 PropertyObject<int> _status;
190 PropertyObject<std::string> _status_msg;
192 bool _sampling_dirty,
197 GroupPtr _root_group;
199 CullCallbackPtr _cull_callback;
200 bool _render_always; //<! Used to disable automatic lazy rendering (culling)
202 std::vector<SGPropertyNode*> _dirty_placements;
203 std::vector<Placements> _placements;
204 std::set<CanvasWeakPtr> _parent_canvases, //<! Canvases showing this canvas
205 _child_canvases; //<! Canvases displayed within
208 typedef std::map<std::string, PlacementFactory> PlacementFactoryMap;
209 static PlacementFactoryMap _placement_factories;
211 void setStatusFlags(unsigned int flags, bool set = true);
215 static SystemAdapterPtr _system_adapter;
217 Canvas(const Canvas&); // = delete;
218 Canvas& operator=(const Canvas&); // = delete;
221 } // namespace canvas
222 } // namespace simgear
224 #endif /* CANVAS_HXX_ */