]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/Canvas.hxx
10872f22d7f0574e35066cb9ec96257ebb7d2955
[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       bool dispatchEvent(const EventPtr& event);
136
137       void setSizeX(int sx);
138       void setSizeY(int sy);
139
140       int getSizeX() const;
141       int getSizeY() const;
142
143       void setViewWidth(int w);
144       void setViewHeight(int h);
145
146       int getViewWidth() const;
147       int getViewHeight() const;
148       SGRect<int> getViewport() const;
149
150       bool handleMouseEvent(const MouseEventPtr& event);
151       bool propagateEvent( EventPtr const& event,
152                            EventPropagationPath const& path );
153
154       virtual void childAdded( SGPropertyNode * parent,
155                                SGPropertyNode * child );
156       virtual void childRemoved( SGPropertyNode * parent,
157                                  SGPropertyNode * child );
158       virtual void valueChanged (SGPropertyNode * node);
159
160       osg::Texture2D* getTexture() const;
161
162       CullCallbackPtr getCullCallback() const;
163
164       void reloadPlacements( const std::string& type = std::string() );
165       static void addPlacementFactory( const std::string& type,
166                                        PlacementFactory factory );
167       static void removePlacementFactory(const std::string& type);
168
169       /**
170        * Set global SystemAdapter for all Canvas/ODGauge instances.
171        *
172        * The SystemAdapter is responsible for application specific operations
173        * like loading images/fonts and adding/removing cameras to the scene
174        * graph.
175        */
176       static void setSystemAdapter(const SystemAdapterPtr& system_adapter);
177       static SystemAdapterPtr getSystemAdapter();
178
179     protected:
180
181       CanvasMgr        *_canvas_mgr;
182
183       boost::scoped_ptr<EventManager> _event_manager;
184
185       int _size_x,
186           _size_y,
187           _view_width,
188           _view_height;
189
190       PropertyObject<int>           _status;
191       PropertyObject<std::string>   _status_msg;
192
193       bool _sampling_dirty,
194            _render_dirty,
195            _visible;
196
197       ODGauge _texture;
198       GroupPtr _root_group;
199
200       CullCallbackPtr _cull_callback;
201       bool _render_always; //<! Used to disable automatic lazy rendering (culling)
202
203       std::vector<SGPropertyNode*> _dirty_placements;
204       std::vector<Placements> _placements;
205       std::set<CanvasWeakPtr> _parent_canvases, //<! Canvases showing this canvas
206                               _child_canvases;  //<! Canvases displayed within
207                                                 //   this canvas
208
209       typedef std::map<std::string, PlacementFactory> PlacementFactoryMap;
210       static PlacementFactoryMap _placement_factories;
211
212       void setStatusFlags(unsigned int flags, bool set = true);
213
214     private:
215
216       static SystemAdapterPtr _system_adapter;
217
218       Canvas(const Canvas&); // = delete;
219       Canvas& operator=(const Canvas&); // = delete;
220   };
221
222 } // namespace canvas
223 } // namespace simgear
224
225 #endif /* CANVAS_HXX_ */