2 /// The canvas for rendering with the 2d API
4 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Library General Public
8 // License as published by the Free Software Foundation; either
9 // version 2 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Library General Public License for more details.
16 // You should have received a copy of the GNU Library General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "canvas_fwd.hxx"
24 #include "ODGauge.hxx"
26 #include <simgear/canvas/elements/CanvasGroup.hxx>
27 #include <simgear/canvas/layout/Layout.hxx>
28 #include <simgear/math/SGRect.hxx>
29 #include <simgear/nasal/cppbind/NasalObject.hxx>
30 #include <simgear/props/PropertyBasedElement.hxx>
31 #include <simgear/props/propertyObject.hxx>
33 #include <osg/NodeCallback>
34 #include <osg/observer_ptr>
36 #include <boost/scoped_ptr.hpp>
41 /// Canvas 2D drawing API
48 * Canvas to draw onto (to an off-screen render target).
51 public PropertyBasedElement,
60 MISSING_SIZE_X = STATUS_DIRTY << 1,
61 MISSING_SIZE_Y = MISSING_SIZE_X << 1,
62 MISSING_SIZE = MISSING_SIZE_X | MISSING_SIZE_Y,
63 CREATE_FAILED = MISSING_SIZE_Y << 1
67 * This callback is installed on every placement of the canvas in the
68 * scene to only render the canvas if at least one placement is visible
71 public osg::NodeCallback
74 CullCallback(const CanvasWeakPtr& canvas);
77 CanvasWeakPtr _canvas;
79 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
81 typedef osg::ref_ptr<CullCallback> CullCallbackPtr;
83 Canvas(SGPropertyNode* node);
85 virtual void onDestroy();
87 void setCanvasMgr(CanvasMgr* canvas_mgr);
88 CanvasMgr* getCanvasMgr() const;
93 * Add a canvas which should be marked as dirty upon any change to this
96 * This mechanism is used to eg. redraw a canvas if it's displaying
97 * another canvas (recursive canvases)
99 void addParentCanvas(const CanvasWeakPtr& canvas);
102 * Add a canvas which should be marked visible if this canvas is visible.
104 void addChildCanvas(const CanvasWeakPtr& canvas);
107 * Stop notifying the given canvas upon changes
109 void removeParentCanvas(const CanvasWeakPtr& canvas);
110 void removeChildCanvas(const CanvasWeakPtr& canvas);
115 GroupPtr createGroup(const std::string& name = "");
118 * Get an existing group with the given name
120 GroupPtr getGroup(const std::string& name);
123 * Get an existing group with the given name or otherwise create a new
126 GroupPtr getOrCreateGroup(const std::string& name);
129 * Get the root group of the canvas
131 GroupPtr getRootGroup();
134 * Set the layout of the canvas (the layout will automatically update with
135 * the viewport size of the canvas)
137 void setLayout(const LayoutRef& layout);
140 * Set the focus to the given element.
142 * The focus element will receive all keyboard events propagated to this
143 * canvas. If there is no valid focus element the root group will receive
144 * the events instead.
146 void setFocusElement(const ElementPtr& el);
149 * Clear the focus element.
151 * @see setFocusElement()
153 void clearFocusElement();
156 * Enable rendering for the next frame
158 * @param force Force redraw even if nothing has changed (if dirty flag
161 void enableRendering(bool force = false);
163 void update(double delta_time_sec);
165 bool addEventListener(const std::string& type, const EventListener& cb);
166 bool dispatchEvent(const EventPtr& event);
168 void setSizeX(int sx);
169 void setSizeY(int sy);
171 int getSizeX() const;
172 int getSizeY() const;
174 void setViewWidth(int w);
175 void setViewHeight(int h);
177 int getViewWidth() const;
178 int getViewHeight() const;
179 SGRect<int> getViewport() const;
181 bool handleMouseEvent(const MouseEventPtr& event);
182 bool handleKeyboardEvent(const KeyboardEventPtr& event);
184 bool propagateEvent( EventPtr const& event,
185 EventPropagationPath const& path );
187 virtual void childAdded( SGPropertyNode * parent,
188 SGPropertyNode * child );
189 virtual void childRemoved( SGPropertyNode * parent,
190 SGPropertyNode * child );
191 virtual void valueChanged (SGPropertyNode * node);
193 osg::Texture2D* getTexture() const;
195 CullCallbackPtr getCullCallback() const;
197 void reloadPlacements( const std::string& type = std::string() );
198 static void addPlacementFactory( const std::string& type,
199 PlacementFactory factory );
200 static void removePlacementFactory(const std::string& type);
203 * Set global SystemAdapter for all Canvas/ODGauge instances.
205 * The SystemAdapter is responsible for application specific operations
206 * like loading images/fonts and adding/removing cameras to the scene
209 static void setSystemAdapter(const SystemAdapterPtr& system_adapter);
210 static SystemAdapterPtr getSystemAdapter();
214 CanvasMgr *_canvas_mgr;
216 boost::scoped_ptr<EventManager> _event_manager;
223 PropertyObject<int> _status;
224 PropertyObject<std::string> _status_msg;
226 bool _sampling_dirty,
232 GroupPtr _root_group;
235 ElementWeakPtr _focus_element;
237 CullCallbackPtr _cull_callback;
238 bool _render_always; //!< Used to disable automatic lazy rendering (culling)
240 std::vector<SGPropertyNode*> _dirty_placements;
241 std::vector<Placements> _placements;
242 std::set<CanvasWeakPtr> _parent_canvases, //!< Canvases showing this canvas
243 _child_canvases; //!< Canvases displayed within
246 typedef std::map<std::string, PlacementFactory> PlacementFactoryMap;
247 static PlacementFactoryMap _placement_factories;
249 void setStatusFlags(unsigned int flags, bool set = true);
253 static SystemAdapterPtr _system_adapter;
255 Canvas(const Canvas&); // = delete;
256 Canvas& operator=(const Canvas&); // = delete;
259 } // namespace canvas
260 } // namespace simgear
262 #endif /* CANVAS_HXX_ */