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/props/PropertyBasedElement.hxx>
29 #include <simgear/props/propertyObject.hxx>
30 #include <osg/NodeCallback>
31 #include <osg/observer_ptr>
33 #include <boost/scoped_ptr.hpp>
38 /// Canvas 2D drawing API
45 public PropertyBasedElement
53 LAYOUT_DIRTY = STATUS_DIRTY << 1,
54 MISSING_SIZE_X = LAYOUT_DIRTY << 1,
55 MISSING_SIZE_Y = MISSING_SIZE_X << 1,
56 MISSING_SIZE = MISSING_SIZE_X | MISSING_SIZE_Y,
57 CREATE_FAILED = MISSING_SIZE_Y << 1
61 * This callback is installed on every placement of the canvas in the
62 * scene to only render the canvas if at least one placement is visible
65 public osg::NodeCallback
68 CullCallback(const CanvasWeakPtr& canvas);
71 CanvasWeakPtr _canvas;
73 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
75 typedef osg::ref_ptr<CullCallback> CullCallbackPtr;
77 Canvas(SGPropertyNode* node);
79 virtual void onDestroy();
81 void setCanvasMgr(CanvasMgr* canvas_mgr);
82 CanvasMgr* getCanvasMgr() const;
87 * Add a canvas which should be marked as dirty upon any change to this
90 * This mechanism is used to eg. redraw a canvas if it's displaying
91 * another canvas (recursive canvases)
93 void addParentCanvas(const CanvasWeakPtr& canvas);
96 * Add a canvas which should be marked visible if this canvas is visible.
98 void addChildCanvas(const CanvasWeakPtr& canvas);
101 * Stop notifying the given canvas upon changes
103 void removeParentCanvas(const CanvasWeakPtr& canvas);
104 void removeChildCanvas(const CanvasWeakPtr& canvas);
109 GroupPtr createGroup(const std::string& name = "");
112 * Get an existing group with the given name
114 GroupPtr getGroup(const std::string& name);
117 * Get an existing group with the given name or otherwise create a new
120 GroupPtr getOrCreateGroup(const std::string& name);
123 * Get the root group of the canvas
125 GroupPtr getRootGroup();
128 * Set the layout of the canvas (the layout will automatically update with
129 * the viewport size of the canvas)
131 void setLayout(const LayoutRef& layout);
134 * Enable rendering for the next frame
136 * @param force Force redraw even if nothing has changed (if dirty flag
139 void enableRendering(bool force = false);
141 void update(double delta_time_sec);
143 bool addEventListener(const std::string& type, const EventListener& cb);
144 bool dispatchEvent(const EventPtr& event);
146 void setSizeX(int sx);
147 void setSizeY(int sy);
149 int getSizeX() const;
150 int getSizeY() const;
152 void setViewWidth(int w);
153 void setViewHeight(int h);
155 int getViewWidth() const;
156 int getViewHeight() const;
157 SGRect<int> getViewport() const;
159 bool handleMouseEvent(const MouseEventPtr& event);
160 bool propagateEvent( EventPtr const& event,
161 EventPropagationPath const& path );
163 virtual void childAdded( SGPropertyNode * parent,
164 SGPropertyNode * child );
165 virtual void childRemoved( SGPropertyNode * parent,
166 SGPropertyNode * child );
167 virtual void valueChanged (SGPropertyNode * node);
169 osg::Texture2D* getTexture() const;
171 CullCallbackPtr getCullCallback() const;
173 void reloadPlacements( const std::string& type = std::string() );
174 static void addPlacementFactory( const std::string& type,
175 PlacementFactory factory );
176 static void removePlacementFactory(const std::string& type);
179 * Set global SystemAdapter for all Canvas/ODGauge instances.
181 * The SystemAdapter is responsible for application specific operations
182 * like loading images/fonts and adding/removing cameras to the scene
185 static void setSystemAdapter(const SystemAdapterPtr& system_adapter);
186 static SystemAdapterPtr getSystemAdapter();
190 CanvasMgr *_canvas_mgr;
192 boost::scoped_ptr<EventManager> _event_manager;
199 PropertyObject<int> _status;
200 PropertyObject<std::string> _status_msg;
202 bool _sampling_dirty,
208 GroupPtr _root_group;
211 CullCallbackPtr _cull_callback;
212 bool _render_always; //<! Used to disable automatic lazy rendering (culling)
214 std::vector<SGPropertyNode*> _dirty_placements;
215 std::vector<Placements> _placements;
216 std::set<CanvasWeakPtr> _parent_canvases, //<! Canvases showing this canvas
217 _child_canvases; //<! Canvases displayed within
220 typedef std::map<std::string, PlacementFactory> PlacementFactoryMap;
221 static PlacementFactoryMap _placement_factories;
223 void setStatusFlags(unsigned int flags, bool set = true);
227 static SystemAdapterPtr _system_adapter;
229 Canvas(const Canvas&); // = delete;
230 Canvas& operator=(const Canvas&); // = delete;
233 } // namespace canvas
234 } // namespace simgear
236 #endif /* CANVAS_HXX_ */