]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/Canvas.hxx
Fix #1783: repeated error message on console
[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        * Clear the focus element.
150        *
151        * @see setFocusElement()
152        */
153       void clearFocusElement();
154
155       /**
156        * Enable rendering for the next frame
157        *
158        * @param force   Force redraw even if nothing has changed (if dirty flag
159        *                is not set)
160        */
161       void enableRendering(bool force = false);
162
163       void update(double delta_time_sec);
164
165       bool addEventListener(const std::string& type, const EventListener& cb);
166       bool dispatchEvent(const EventPtr& event);
167
168       void setSizeX(int sx);
169       void setSizeY(int sy);
170
171       int getSizeX() const;
172       int getSizeY() const;
173
174       void setViewWidth(int w);
175       void setViewHeight(int h);
176
177       int getViewWidth() const;
178       int getViewHeight() const;
179       SGRect<int> getViewport() const;
180
181       bool handleMouseEvent(const MouseEventPtr& event);
182       bool handleKeyboardEvent(const KeyboardEventPtr& event);
183
184       bool propagateEvent( EventPtr const& event,
185                            EventPropagationPath const& path );
186
187       virtual void childAdded( SGPropertyNode * parent,
188                                SGPropertyNode * child );
189       virtual void childRemoved( SGPropertyNode * parent,
190                                  SGPropertyNode * child );
191       virtual void valueChanged (SGPropertyNode * node);
192
193       osg::Texture2D* getTexture() const;
194
195       CullCallbackPtr getCullCallback() const;
196
197       void reloadPlacements( const std::string& type = std::string() );
198       static void addPlacementFactory( const std::string& type,
199                                        PlacementFactory factory );
200       static void removePlacementFactory(const std::string& type);
201
202       /**
203        * Set global SystemAdapter for all Canvas/ODGauge instances.
204        *
205        * The SystemAdapter is responsible for application specific operations
206        * like loading images/fonts and adding/removing cameras to the scene
207        * graph.
208        */
209       static void setSystemAdapter(const SystemAdapterPtr& system_adapter);
210       static SystemAdapterPtr getSystemAdapter();
211
212     protected:
213
214       CanvasMgr        *_canvas_mgr;
215
216       boost::scoped_ptr<EventManager> _event_manager;
217
218       int _size_x,
219           _size_y,
220           _view_width,
221           _view_height;
222
223       PropertyObject<int>           _status;
224       PropertyObject<std::string>   _status_msg;
225
226       bool _sampling_dirty,
227            _render_dirty,
228            _visible;
229
230       ODGauge _texture;
231
232       GroupPtr  _root_group;
233       LayoutRef _layout;
234
235       ElementWeakPtr _focus_element;
236
237       CullCallbackPtr _cull_callback;
238       bool _render_always; //!< Used to disable automatic lazy rendering (culling)
239
240       std::vector<SGPropertyNode*> _dirty_placements;
241       std::vector<Placements> _placements;
242       std::set<CanvasWeakPtr> _parent_canvases, //!< Canvases showing this canvas
243                               _child_canvases;  //!< Canvases displayed within
244                                                 //   this canvas
245
246       typedef std::map<std::string, PlacementFactory> PlacementFactoryMap;
247       static PlacementFactoryMap _placement_factories;
248
249       void setStatusFlags(unsigned int flags, bool set = true);
250
251     private:
252
253       static SystemAdapterPtr _system_adapter;
254
255       Canvas(const Canvas&); // = delete;
256       Canvas& operator=(const Canvas&); // = delete;
257   };
258
259 } // namespace canvas
260 } // namespace simgear
261
262 #endif /* CANVAS_HXX_ */