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/props/PropertyBasedElement.hxx>
27 #include <simgear/props/propertyObject.hxx>
28 #include <osg/NodeCallback>
29 #include <osg/observer_ptr>
42 public PropertyBasedElement
50 MISSING_SIZE_X = STATUS_DIRTY << 1,
51 MISSING_SIZE_Y = MISSING_SIZE_X << 1,
52 CREATE_FAILED = MISSING_SIZE_Y << 1
56 * This callback is installed on every placement of the canvas in the
57 * scene to only render the canvas if at least one placement is visible
60 public osg::NodeCallback
63 CullCallback(const CanvasWeakPtr& canvas);
66 CanvasWeakPtr _canvas;
68 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
70 typedef osg::ref_ptr<CullCallback> CullCallbackPtr;
72 Canvas(SGPropertyNode* node);
75 void setSystemAdapter(const SystemAdapterPtr& system_adapter);
76 SystemAdapterPtr getSystemAdapter() const;
78 void setCanvasMgr(CanvasMgr* canvas_mgr);
79 CanvasMgr* getCanvasMgr() const;
82 * Add a canvas which should be mared as dirty upon any change to this
85 * This mechanism is used to eg. redraw a canvas if it's displaying
86 * another canvas (recursive canvases)
88 void addDependentCanvas(const CanvasWeakPtr& canvas);
91 * Stop notifying the given canvas upon changes
93 void removeDependentCanvas(const CanvasWeakPtr& canvas);
95 GroupPtr createGroup(const std::string& name = "");
98 * Enable rendering for the next frame
100 * @param force Force redraw even if nothing has changed (if dirty flag
103 void enableRendering(bool force = false);
105 void update(double delta_time_sec);
107 naRef addEventListener(const nasal::CallContext& ctx);
109 void setSizeX(int sx);
110 void setSizeY(int sy);
112 int getSizeX() const;
113 int getSizeY() const;
115 void setViewWidth(int w);
116 void setViewHeight(int h);
118 int getViewWidth() const;
119 int getViewHeight() const;
121 bool handleMouseEvent(const MouseEventPtr& event);
123 virtual void childAdded( SGPropertyNode * parent,
124 SGPropertyNode * child );
125 virtual void childRemoved( SGPropertyNode * parent,
126 SGPropertyNode * child );
127 virtual void valueChanged (SGPropertyNode * node);
129 osg::Texture2D* getTexture() const;
131 CullCallbackPtr getCullCallback() const;
133 void reloadPlacements( const std::string& type = std::string() );
134 static void addPlacementFactory( const std::string& type,
135 PlacementFactory factory );
139 SystemAdapterPtr _system_adapter;
140 CanvasMgr *_canvas_mgr;
142 std::auto_ptr<EventManager> _event_manager;
149 PropertyObject<int> _status;
150 PropertyObject<std::string> _status_msg;
152 bool _sampling_dirty,
157 GroupPtr _root_group;
159 CullCallbackPtr _cull_callback;
160 bool _render_always; //<! Used to disable automatic lazy rendering (culling)
162 std::vector<SGPropertyNode*> _dirty_placements;
163 std::vector<Placements> _placements;
164 std::set<CanvasWeakPtr> _dependent_canvases; //<! Canvases which use this
165 // canvas and should be
166 // notified about changes
168 typedef std::map<std::string, PlacementFactory> PlacementFactoryMap;
169 static PlacementFactoryMap _placement_factories;
171 virtual void setSelf(const PropertyBasedElementPtr& self);
172 void setStatusFlags(unsigned int flags, bool set = true);
176 Canvas(const Canvas&); // = delete;
177 Canvas& operator=(const Canvas&); // = delete;
180 } // namespace canvas
181 } // namespace simgear
183 #endif /* CANVAS_HXX_ */