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