]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/Canvas.hxx
Improve (mostly Canvas event related) documentation.
[simgear.git] / simgear / canvas / Canvas.hxx
1 ///@file
2 /// The canvas for rendering with the 2d API
3 //
4 // Copyright (C) 2012  Thomas Geymayer <tomgey@gmail.com>
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Library General Public
8 // License as published by the Free Software Foundation; either
9 // version 2 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Library General Public License for more details.
15 //
16 // You should have received a copy of the GNU Library General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
19
20 #ifndef CANVAS_HXX_
21 #define CANVAS_HXX_
22
23 #include "canvas_fwd.hxx"
24 #include "ODGauge.hxx"
25
26 #include <simgear/canvas/elements/CanvasGroup.hxx>
27 #include <simgear/canvas/layout/Layout.hxx>
28 #include <simgear/math/SGRect.hxx>
29 #include <simgear/nasal/cppbind/NasalObject.hxx>
30 #include <simgear/props/PropertyBasedElement.hxx>
31 #include <simgear/props/propertyObject.hxx>
32
33 #include <osg/NodeCallback>
34 #include <osg/observer_ptr>
35
36 #include <boost/scoped_ptr.hpp>
37 #include <string>
38
39 namespace simgear
40 {
41 /// Canvas 2D drawing API
42 namespace canvas
43 {
44   class CanvasMgr;
45   class MouseEvent;
46
47   /**
48    * Canvas to draw onto (to an off-screen render target).
49    */
50   class Canvas:
51     public PropertyBasedElement,
52     public nasal::Object
53   {
54     public:
55
56       enum StatusFlags
57       {
58         STATUS_OK,
59         STATUS_DIRTY   = 1,
60         MISSING_SIZE_X = STATUS_DIRTY << 1,
61         MISSING_SIZE_Y = MISSING_SIZE_X << 1,
62         MISSING_SIZE   = MISSING_SIZE_X | MISSING_SIZE_Y,
63         CREATE_FAILED  = MISSING_SIZE_Y << 1
64       };
65
66       /**
67        * This callback is installed on every placement of the canvas in the
68        * scene to only render the canvas if at least one placement is visible
69        */
70       class CullCallback:
71         public osg::NodeCallback
72       {
73         public:
74           CullCallback(const CanvasWeakPtr& canvas);
75
76         private:
77           CanvasWeakPtr _canvas;
78
79           virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
80       };
81       typedef osg::ref_ptr<CullCallback> CullCallbackPtr;
82
83       Canvas(SGPropertyNode* node);
84       virtual ~Canvas();
85       virtual void onDestroy();
86
87       void setCanvasMgr(CanvasMgr* canvas_mgr);
88       CanvasMgr* getCanvasMgr() const;
89
90       bool isInit() const;
91
92       /**
93        * Add a canvas which should be marked as dirty upon any change to this
94        * canvas.
95        *
96        * This mechanism is used to eg. redraw a canvas if it's displaying
97        * another canvas (recursive canvases)
98        */
99       void addParentCanvas(const CanvasWeakPtr& canvas);
100
101       /**
102        * Add a canvas which should be marked visible if this canvas is visible.
103        */
104       void addChildCanvas(const CanvasWeakPtr& canvas);
105
106       /**
107        * Stop notifying the given canvas upon changes
108        */
109       void removeParentCanvas(const CanvasWeakPtr& canvas);
110       void removeChildCanvas(const CanvasWeakPtr& canvas);
111
112       /**
113        * Create a new group
114        */
115       GroupPtr createGroup(const std::string& name = "");
116
117       /**
118        * Get an existing group with the given name
119        */
120       GroupPtr getGroup(const std::string& name);
121
122       /**
123        * Get an existing group with the given name or otherwise create a new
124        * group
125        */
126       GroupPtr getOrCreateGroup(const std::string& name);
127
128       /**
129        * Get the root group of the canvas
130        */
131       GroupPtr getRootGroup();
132
133       /**
134        * Set the layout of the canvas (the layout will automatically update with
135        * the viewport size of the canvas)
136        */
137       void setLayout(const LayoutRef& layout);
138
139       /**
140        * Set the focus to the given element.
141        *
142        * The focus element will receive all keyboard events propagated to this
143        * canvas. If there is no valid focus element the root group will receive
144        * the events instead.
145        */
146       void setFocusElement(const ElementPtr& el);
147
148       /**
149        * Enable rendering for the next frame
150        *
151        * @param force   Force redraw even if nothing has changed (if dirty flag
152        *                is not set)
153        */
154       void enableRendering(bool force = false);
155
156       void update(double delta_time_sec);
157
158       bool addEventListener(const std::string& type, const EventListener& cb);
159       bool dispatchEvent(const EventPtr& event);
160
161       void setSizeX(int sx);
162       void setSizeY(int sy);
163
164       int getSizeX() const;
165       int getSizeY() const;
166
167       void setViewWidth(int w);
168       void setViewHeight(int h);
169
170       int getViewWidth() const;
171       int getViewHeight() const;
172       SGRect<int> getViewport() const;
173
174       bool handleMouseEvent(const MouseEventPtr& event);
175       bool handleKeyboardEvent(const KeyboardEventPtr& event);
176
177       bool propagateEvent( EventPtr const& event,
178                            EventPropagationPath const& path );
179
180       virtual void childAdded( SGPropertyNode * parent,
181                                SGPropertyNode * child );
182       virtual void childRemoved( SGPropertyNode * parent,
183                                  SGPropertyNode * child );
184       virtual void valueChanged (SGPropertyNode * node);
185
186       osg::Texture2D* getTexture() const;
187
188       CullCallbackPtr getCullCallback() const;
189
190       void reloadPlacements( const std::string& type = std::string() );
191       static void addPlacementFactory( const std::string& type,
192                                        PlacementFactory factory );
193       static void removePlacementFactory(const std::string& type);
194
195       /**
196        * Set global SystemAdapter for all Canvas/ODGauge instances.
197        *
198        * The SystemAdapter is responsible for application specific operations
199        * like loading images/fonts and adding/removing cameras to the scene
200        * graph.
201        */
202       static void setSystemAdapter(const SystemAdapterPtr& system_adapter);
203       static SystemAdapterPtr getSystemAdapter();
204
205     protected:
206
207       CanvasMgr        *_canvas_mgr;
208
209       boost::scoped_ptr<EventManager> _event_manager;
210
211       int _size_x,
212           _size_y,
213           _view_width,
214           _view_height;
215
216       PropertyObject<int>           _status;
217       PropertyObject<std::string>   _status_msg;
218
219       bool _sampling_dirty,
220            _render_dirty,
221            _visible;
222
223       ODGauge _texture;
224
225       GroupPtr  _root_group;
226       LayoutRef _layout;
227
228       ElementWeakPtr _focus_element;
229
230       CullCallbackPtr _cull_callback;
231       bool _render_always; //<! Used to disable automatic lazy rendering (culling)
232
233       std::vector<SGPropertyNode*> _dirty_placements;
234       std::vector<Placements> _placements;
235       std::set<CanvasWeakPtr> _parent_canvases, //<! Canvases showing this canvas
236                               _child_canvases;  //<! Canvases displayed within
237                                                 //   this canvas
238
239       typedef std::map<std::string, PlacementFactory> PlacementFactoryMap;
240       static PlacementFactoryMap _placement_factories;
241
242       void setStatusFlags(unsigned int flags, bool set = true);
243
244     private:
245
246       static SystemAdapterPtr _system_adapter;
247
248       Canvas(const Canvas&); // = delete;
249       Canvas& operator=(const Canvas&); // = delete;
250   };
251
252 } // namespace canvas
253 } // namespace simgear
254
255 #endif /* CANVAS_HXX_ */