]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/Canvas.hxx
CanvaImage: Enable texture repeat for images
[simgear.git] / simgear / canvas / Canvas.hxx
1 // 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/props/PropertyBasedElement.hxx>
27 #include <simgear/props/propertyObject.hxx>
28 #include <osg/NodeCallback>
29 #include <osg/observer_ptr>
30
31 #include <memory>
32 #include <string>
33
34 namespace simgear
35 {
36 namespace canvas
37 {
38   class CanvasMgr;
39   class MouseEvent;
40
41   class Canvas:
42     public PropertyBasedElement
43   {
44     public:
45
46       enum StatusFlags
47       {
48         STATUS_OK,
49         STATUS_DIRTY     = 1,
50         MISSING_SIZE_X = STATUS_DIRTY << 1,
51         MISSING_SIZE_Y = MISSING_SIZE_X << 1,
52         CREATE_FAILED  = MISSING_SIZE_Y << 1
53       };
54
55       /**
56        * This callback is installed on every placement of the canvas in the
57        * scene to only render the canvas if at least one placement is visible
58        */
59       class CullCallback:
60         public osg::NodeCallback
61       {
62         public:
63           CullCallback(const CanvasWeakPtr& canvas);
64
65         private:
66           CanvasWeakPtr _canvas;
67
68           virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
69       };
70       typedef osg::ref_ptr<CullCallback> CullCallbackPtr;
71
72       Canvas(SGPropertyNode* node);
73       virtual ~Canvas();
74
75       void setSystemAdapter(const SystemAdapterPtr& system_adapter);
76       SystemAdapterPtr getSystemAdapter() const;
77
78       void setCanvasMgr(CanvasMgr* canvas_mgr);
79       CanvasMgr* getCanvasMgr() const;
80
81       /**
82        * Add a canvas which should be mared as dirty upon any change to this
83        * canvas.
84        *
85        * This mechanism is used to eg. redraw a canvas if it's displaying
86        * another canvas (recursive canvases)
87        */
88       void addDependentCanvas(const CanvasWeakPtr& canvas);
89
90       /**
91        * Stop notifying the given canvas upon changes
92        */
93       void removeDependentCanvas(const CanvasWeakPtr& canvas);
94
95       GroupPtr createGroup(const std::string& name = "");
96
97       /**
98        * Enable rendering for the next frame
99        *
100        * @param force   Force redraw even if nothing has changed (if dirty flag
101        *                is not set)
102        */
103       void enableRendering(bool force = false);
104
105       void update(double delta_time_sec);
106
107       naRef addEventListener(const nasal::CallContext& ctx);
108
109       void setSizeX(int sx);
110       void setSizeY(int sy);
111
112       int getSizeX() const;
113       int getSizeY() const;
114
115       void setViewWidth(int w);
116       void setViewHeight(int h);
117
118       int getViewWidth() const;
119       int getViewHeight() const;
120
121       bool handleMouseEvent(const MouseEventPtr& event);
122
123       virtual void childAdded( SGPropertyNode * parent,
124                                SGPropertyNode * child );
125       virtual void childRemoved( SGPropertyNode * parent,
126                                  SGPropertyNode * child );
127       virtual void valueChanged (SGPropertyNode * node);
128
129       osg::Texture2D* getTexture() const;
130
131       CullCallbackPtr getCullCallback() const;
132
133       void reloadPlacements( const std::string& type = std::string() );
134       static void addPlacementFactory( const std::string& type,
135                                        PlacementFactory factory );
136
137     protected:
138
139       SystemAdapterPtr  _system_adapter;
140       CanvasMgr        *_canvas_mgr;
141
142       std::auto_ptr<EventManager>   _event_manager;
143
144       int _size_x,
145           _size_y,
146           _view_width,
147           _view_height;
148
149       PropertyObject<int>           _status;
150       PropertyObject<std::string>   _status_msg;
151
152       bool _sampling_dirty,
153            _render_dirty,
154            _visible;
155
156       ODGauge _texture;
157       GroupPtr _root_group;
158
159       CullCallbackPtr _cull_callback;
160       bool _render_always; //<! Used to disable automatic lazy rendering (culling)
161
162       std::vector<SGPropertyNode*> _dirty_placements;
163       std::vector<Placements> _placements;
164       std::set<CanvasWeakPtr> _dependent_canvases; //<! Canvases which use this
165                                                    //   canvas and should be
166                                                    //   notified about changes
167
168       typedef std::map<std::string, PlacementFactory> PlacementFactoryMap;
169       static PlacementFactoryMap _placement_factories;
170
171       virtual void setSelf(const PropertyBasedElementPtr& self);
172       void setStatusFlags(unsigned int flags, bool set = true);
173
174     private:
175
176       Canvas(const Canvas&); // = delete;
177       Canvas& operator=(const Canvas&); // = delete;
178   };
179
180 } // namespace canvas
181 } // namespace simgear
182
183 #endif /* CANVAS_HXX_ */