]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/Canvas.hxx
Canvas: allow also C++ callable entities as event callbacks.
[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 void onDestroy();
75
76       void setSystemAdapter(const SystemAdapterPtr& system_adapter);
77       SystemAdapterPtr getSystemAdapter() const;
78
79       void setCanvasMgr(CanvasMgr* canvas_mgr);
80       CanvasMgr* getCanvasMgr() const;
81
82       bool isInit() const;
83
84       /**
85        * Add a canvas which should be marked as dirty upon any change to this
86        * canvas.
87        *
88        * This mechanism is used to eg. redraw a canvas if it's displaying
89        * another canvas (recursive canvases)
90        */
91       void addParentCanvas(const CanvasWeakPtr& canvas);
92
93       /**
94        * Add a canvas which should be marked visible if this canvas is visible.
95        */
96       void addChildCanvas(const CanvasWeakPtr& canvas);
97
98       /**
99        * Stop notifying the given canvas upon changes
100        */
101       void removeParentCanvas(const CanvasWeakPtr& canvas);
102       void removeChildCanvas(const CanvasWeakPtr& canvas);
103
104       /**
105        * Create a new group
106        */
107       GroupPtr createGroup(const std::string& name = "");
108
109       /**
110        * Get an existing group with the given name
111        */
112       GroupPtr getGroup(const std::string& name);
113
114       /**
115        * Get an existing group with the given name or otherwise create a new
116        * group
117        */
118       GroupPtr getOrCreateGroup(const std::string& name);
119
120       /**
121        * Get the root group of the canvas
122        */
123       GroupPtr getRootGroup();
124
125       /**
126        * Enable rendering for the next frame
127        *
128        * @param force   Force redraw even if nothing has changed (if dirty flag
129        *                is not set)
130        */
131       void enableRendering(bool force = false);
132
133       void update(double delta_time_sec);
134
135       bool addEventListener(const std::string& type, const EventListener& cb);
136       bool addNasalEventListener(const std::string& type, naRef code);
137
138       void setSizeX(int sx);
139       void setSizeY(int sy);
140
141       int getSizeX() const;
142       int getSizeY() const;
143
144       void setViewWidth(int w);
145       void setViewHeight(int h);
146
147       int getViewWidth() const;
148       int getViewHeight() const;
149       SGRect<int> getViewport() const;
150
151       bool handleMouseEvent(const MouseEventPtr& event);
152
153       virtual void childAdded( SGPropertyNode * parent,
154                                SGPropertyNode * child );
155       virtual void childRemoved( SGPropertyNode * parent,
156                                  SGPropertyNode * child );
157       virtual void valueChanged (SGPropertyNode * node);
158
159       osg::Texture2D* getTexture() const;
160
161       CullCallbackPtr getCullCallback() const;
162
163       void reloadPlacements( const std::string& type = std::string() );
164       static void addPlacementFactory( const std::string& type,
165                                        PlacementFactory factory );
166
167     protected:
168
169       SystemAdapterPtr  _system_adapter;
170       CanvasMgr        *_canvas_mgr;
171
172       boost::scoped_ptr<EventManager> _event_manager;
173
174       int _size_x,
175           _size_y,
176           _view_width,
177           _view_height;
178
179       PropertyObject<int>           _status;
180       PropertyObject<std::string>   _status_msg;
181
182       bool _sampling_dirty,
183            _render_dirty,
184            _visible;
185
186       ODGauge _texture;
187       GroupPtr _root_group;
188
189       CullCallbackPtr _cull_callback;
190       bool _render_always; //<! Used to disable automatic lazy rendering (culling)
191
192       std::vector<SGPropertyNode*> _dirty_placements;
193       std::vector<Placements> _placements;
194       std::set<CanvasWeakPtr> _parent_canvases, //<! Canvases showing this canvas
195                               _child_canvases;  //<! Canvases displayed within
196                                                 //   this canvas
197
198       typedef std::map<std::string, PlacementFactory> PlacementFactoryMap;
199       static PlacementFactoryMap _placement_factories;
200
201       virtual void setSelf(const PropertyBasedElementPtr& self);
202       void setStatusFlags(unsigned int flags, bool set = true);
203
204     private:
205
206       Canvas(const Canvas&); // = delete;
207       Canvas& operator=(const Canvas&); // = delete;
208   };
209
210 } // namespace canvas
211 } // namespace simgear
212
213 #endif /* CANVAS_HXX_ */