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