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