]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/Canvas.hxx
Canvas: one global SystemAdapter is enough.
[simgear.git] / simgear / canvas / Canvas.hxx
1 // The canvas for rendering with the 2d API
2 //
3 // Copyright (C) 2012  Thomas Geymayer <tomgey@gmail.com>
4 //
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.
9 //
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.
14 //
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
18
19 #ifndef CANVAS_HXX_
20 #define CANVAS_HXX_
21
22 #include "canvas_fwd.hxx"
23 #include "ODGauge.hxx"
24
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>
31
32 #include <boost/scoped_ptr.hpp>
33 #include <string>
34
35 namespace simgear
36 {
37 namespace canvas
38 {
39   class CanvasMgr;
40   class MouseEvent;
41
42   class Canvas:
43     public PropertyBasedElement
44   {
45     public:
46
47       enum StatusFlags
48       {
49         STATUS_OK,
50         STATUS_DIRTY     = 1,
51         MISSING_SIZE_X = STATUS_DIRTY << 1,
52         MISSING_SIZE_Y = MISSING_SIZE_X << 1,
53         CREATE_FAILED  = MISSING_SIZE_Y << 1
54       };
55
56       /**
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
59        */
60       class CullCallback:
61         public osg::NodeCallback
62       {
63         public:
64           CullCallback(const CanvasWeakPtr& canvas);
65
66         private:
67           CanvasWeakPtr _canvas;
68
69           virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
70       };
71       typedef osg::ref_ptr<CullCallback> CullCallbackPtr;
72
73       Canvas(SGPropertyNode* node);
74       virtual ~Canvas();
75       virtual void onDestroy();
76
77       void setCanvasMgr(CanvasMgr* canvas_mgr);
78       CanvasMgr* getCanvasMgr() const;
79
80       bool isInit() const;
81
82       /**
83        * Add a canvas which should be marked as dirty upon any change to this
84        * canvas.
85        *
86        * This mechanism is used to eg. redraw a canvas if it's displaying
87        * another canvas (recursive canvases)
88        */
89       void addParentCanvas(const CanvasWeakPtr& canvas);
90
91       /**
92        * Add a canvas which should be marked visible if this canvas is visible.
93        */
94       void addChildCanvas(const CanvasWeakPtr& canvas);
95
96       /**
97        * Stop notifying the given canvas upon changes
98        */
99       void removeParentCanvas(const CanvasWeakPtr& canvas);
100       void removeChildCanvas(const CanvasWeakPtr& canvas);
101
102       /**
103        * Create a new group
104        */
105       GroupPtr createGroup(const std::string& name = "");
106
107       /**
108        * Get an existing group with the given name
109        */
110       GroupPtr getGroup(const std::string& name);
111
112       /**
113        * Get an existing group with the given name or otherwise create a new
114        * group
115        */
116       GroupPtr getOrCreateGroup(const std::string& name);
117
118       /**
119        * Get the root group of the canvas
120        */
121       GroupPtr getRootGroup();
122
123       /**
124        * Enable rendering for the next frame
125        *
126        * @param force   Force redraw even if nothing has changed (if dirty flag
127        *                is not set)
128        */
129       void enableRendering(bool force = false);
130
131       void update(double delta_time_sec);
132
133       bool addEventListener(const std::string& type, const EventListener& cb);
134
135       void setSizeX(int sx);
136       void setSizeY(int sy);
137
138       int getSizeX() const;
139       int getSizeY() const;
140
141       void setViewWidth(int w);
142       void setViewHeight(int h);
143
144       int getViewWidth() const;
145       int getViewHeight() const;
146       SGRect<int> getViewport() const;
147
148       bool handleMouseEvent(const MouseEventPtr& event);
149
150       virtual void childAdded( SGPropertyNode * parent,
151                                SGPropertyNode * child );
152       virtual void childRemoved( SGPropertyNode * parent,
153                                  SGPropertyNode * child );
154       virtual void valueChanged (SGPropertyNode * node);
155
156       osg::Texture2D* getTexture() const;
157
158       CullCallbackPtr getCullCallback() const;
159
160       void reloadPlacements( const std::string& type = std::string() );
161       static void addPlacementFactory( const std::string& type,
162                                        PlacementFactory factory );
163       static void removePlacementFactory(const std::string& type);
164
165       /**
166        * Set global SystemAdapter for all Canvas/ODGauge instances.
167        *
168        * The SystemAdapter is responsible for application specific operations
169        * like loading images/fonts and adding/removing cameras to the scene
170        * graph.
171        */
172       static void setSystemAdapter(const SystemAdapterPtr& system_adapter);
173       static SystemAdapterPtr getSystemAdapter();
174
175     protected:
176
177       CanvasMgr        *_canvas_mgr;
178
179       boost::scoped_ptr<EventManager> _event_manager;
180
181       int _size_x,
182           _size_y,
183           _view_width,
184           _view_height;
185
186       PropertyObject<int>           _status;
187       PropertyObject<std::string>   _status_msg;
188
189       bool _sampling_dirty,
190            _render_dirty,
191            _visible;
192
193       ODGauge _texture;
194       GroupPtr _root_group;
195
196       CullCallbackPtr _cull_callback;
197       bool _render_always; //<! Used to disable automatic lazy rendering (culling)
198
199       std::vector<SGPropertyNode*> _dirty_placements;
200       std::vector<Placements> _placements;
201       std::set<CanvasWeakPtr> _parent_canvases, //<! Canvases showing this canvas
202                               _child_canvases;  //<! Canvases displayed within
203                                                 //   this canvas
204
205       typedef std::map<std::string, PlacementFactory> PlacementFactoryMap;
206       static PlacementFactoryMap _placement_factories;
207
208       virtual void setSelf(const PropertyBasedElementPtr& self);
209       void setStatusFlags(unsigned int flags, bool set = true);
210
211     private:
212
213       static SystemAdapterPtr _system_adapter;
214
215       Canvas(const Canvas&); // = delete;
216       Canvas& operator=(const Canvas&); // = delete;
217   };
218
219 } // namespace canvas
220 } // namespace simgear
221
222 #endif /* CANVAS_HXX_ */