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