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
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>
43 public PropertyBasedElement
51 MISSING_SIZE_X = STATUS_DIRTY << 1,
52 MISSING_SIZE_Y = MISSING_SIZE_X << 1,
53 CREATE_FAILED = MISSING_SIZE_Y << 1
57 * This callback is installed on every placement of the canvas in the
58 * scene to only render the canvas if at least one placement is visible
61 public osg::NodeCallback
64 CullCallback(const CanvasWeakPtr& canvas);
67 CanvasWeakPtr _canvas;
69 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
71 typedef osg::ref_ptr<CullCallback> CullCallbackPtr;
73 Canvas(SGPropertyNode* node);
74 virtual void onDestroy();
76 void setSystemAdapter(const SystemAdapterPtr& system_adapter);
77 SystemAdapterPtr getSystemAdapter() const;
79 void setCanvasMgr(CanvasMgr* canvas_mgr);
80 CanvasMgr* getCanvasMgr() const;
85 * Add a canvas which should be marked as dirty upon any change to this
88 * This mechanism is used to eg. redraw a canvas if it's displaying
89 * another canvas (recursive canvases)
91 void addParentCanvas(const CanvasWeakPtr& canvas);
94 * Add a canvas which should be marked visible if this canvas is visible.
96 void addChildCanvas(const CanvasWeakPtr& canvas);
99 * Stop notifying the given canvas upon changes
101 void removeParentCanvas(const CanvasWeakPtr& canvas);
102 void removeChildCanvas(const CanvasWeakPtr& canvas);
107 GroupPtr createGroup(const std::string& name = "");
110 * Get an existing group with the given name
112 GroupPtr getGroup(const std::string& name);
115 * Get an existing group with the given name or otherwise create a new
118 GroupPtr getOrCreateGroup(const std::string& name);
121 * Get the root group of the canvas
123 GroupPtr getRootGroup();
126 * Enable rendering for the next frame
128 * @param force Force redraw even if nothing has changed (if dirty flag
131 void enableRendering(bool force = false);
133 void update(double delta_time_sec);
135 naRef addEventListener(const nasal::CallContext& ctx);
137 void setSizeX(int sx);
138 void setSizeY(int sy);
140 int getSizeX() const;
141 int getSizeY() const;
143 void setViewWidth(int w);
144 void setViewHeight(int h);
146 int getViewWidth() const;
147 int getViewHeight() const;
148 SGRect<int> getViewport() const;
150 bool handleMouseEvent(const MouseEventPtr& event);
152 virtual void childAdded( SGPropertyNode * parent,
153 SGPropertyNode * child );
154 virtual void childRemoved( SGPropertyNode * parent,
155 SGPropertyNode * child );
156 virtual void valueChanged (SGPropertyNode * node);
158 osg::Texture2D* getTexture() const;
160 CullCallbackPtr getCullCallback() const;
162 void reloadPlacements( const std::string& type = std::string() );
163 static void addPlacementFactory( const std::string& type,
164 PlacementFactory factory );
168 SystemAdapterPtr _system_adapter;
169 CanvasMgr *_canvas_mgr;
171 std::auto_ptr<EventManager> _event_manager;
178 PropertyObject<int> _status;
179 PropertyObject<std::string> _status_msg;
181 bool _sampling_dirty,
186 GroupPtr _root_group;
188 CullCallbackPtr _cull_callback;
189 bool _render_always; //<! Used to disable automatic lazy rendering (culling)
191 std::vector<SGPropertyNode*> _dirty_placements;
192 std::vector<Placements> _placements;
193 std::set<CanvasWeakPtr> _parent_canvases, //<! Canvases showing this canvas
194 _child_canvases; //<! Canvases displayed within
197 typedef std::map<std::string, PlacementFactory> PlacementFactoryMap;
198 static PlacementFactoryMap _placement_factories;
200 virtual void setSelf(const PropertyBasedElementPtr& self);
201 void setStatusFlags(unsigned int flags, bool set = true);
205 Canvas(const Canvas&); // = delete;
206 Canvas& operator=(const Canvas&); // = delete;
209 } // namespace canvas
210 } // namespace simgear
212 #endif /* CANVAS_HXX_ */