]> git.mxchange.org Git - flightgear.git/blob - src/Canvas/canvas.hxx
Fix a Clang warning in Shiva.
[flightgear.git] / src / 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 program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
19 #ifndef CANVAS_HXX_
20 #define CANVAS_HXX_
21
22 #include "placement.hxx"
23 #include "property_based_element.hxx"
24
25 #include <Canvas/canvas_fwd.hpp>
26 #include <Instrumentation/od_gauge.hxx>
27
28 #include <simgear/props/propertyObject.hxx>
29 #include <osg/NodeCallback>
30 #include <osg/observer_ptr>
31
32 #include <memory>
33 #include <string>
34
35 class Canvas:
36   public PropertyBasedElement
37 {
38   public:
39
40     enum StatusFlags
41     {
42       STATUS_OK,
43       MISSING_SIZE_X = 0x0001,
44       MISSING_SIZE_Y = 0x0002,
45       CREATE_FAILED  = 0x0004
46     };
47
48     /**
49      * Callback used to disable/enable rendering to the texture if it is not
50      * visible
51      */
52     class CameraCullCallback:
53       public osg::NodeCallback
54     {
55       public:
56         CameraCullCallback();
57
58         /**
59          * Enable rendering for the next frame
60          */
61         void enableRendering();
62
63       private:
64         bool _render;
65         unsigned int _render_frame;
66
67         virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
68     };
69     typedef osg::ref_ptr<CameraCullCallback> CameraCullCallbackPtr;
70     typedef osg::observer_ptr<CameraCullCallback> CameraCullCallbackWeakPtr;
71
72     /**
73      * This callback is installed on every placement of the canvas in the
74      * scene to only render the canvas if at least one placement is visible
75      */
76     class CullCallback:
77       public osg::NodeCallback
78     {
79       public:
80         CullCallback(CameraCullCallback* camera_cull);
81
82       private:
83         CameraCullCallbackWeakPtr _camera_cull;
84
85         virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
86     };
87     typedef osg::ref_ptr<CullCallback> CullCallbackPtr;
88
89     Canvas(SGPropertyNode* node);
90     virtual ~Canvas();
91
92     void update(double delta_time_sec);
93
94     int getSizeX() const
95     { return _size_x; }
96
97     int getSizeY() const
98     { return _size_y; }
99
100     void setSizeX(int sx);
101     void setSizeY(int sy);
102
103     void setViewWidth(int w);
104     void setViewHeight(int h);
105
106     bool handleMouseEvent(const canvas::MouseEvent& event);
107
108     virtual void childAdded( SGPropertyNode * parent,
109                              SGPropertyNode * child );
110     virtual void childRemoved( SGPropertyNode * parent,
111                                SGPropertyNode * child );
112     virtual void valueChanged (SGPropertyNode * node);
113
114     osg::Texture2D* getTexture() const;
115     GLuint getTexId() const;
116
117     CameraCullCallbackPtr getCameraCullCallback() const;
118     CullCallbackPtr getCullCallback() const;
119
120     static void addPlacementFactory( const std::string& type,
121                                      canvas::PlacementFactory factory );
122
123   private:
124
125     Canvas(const Canvas&); // = delete;
126     Canvas& operator=(const Canvas&); // = delete;
127
128     int _size_x,
129         _size_y,
130         _view_width,
131         _view_height;
132
133     simgear::PropertyObject<int>            _status;
134     simgear::PropertyObject<std::string>    _status_msg;
135
136     simgear::PropertyObject<int>    _mouse_x, _mouse_y,
137                                     _mouse_dx, _mouse_dy,
138                                     _mouse_button,
139                                     _mouse_state,
140                                     _mouse_mod,
141                                     _mouse_scroll,
142                                     _mouse_event;
143
144     bool _sampling_dirty,
145          _color_dirty;
146
147     FGODGauge _texture;
148     std::auto_ptr<canvas::Group> _root_group;
149
150     std::vector<SGPropertyNode_ptr> _color_background;
151
152     CameraCullCallbackPtr _camera_callback;
153     CullCallbackPtr _cull_callback;
154     bool _render_always; //<! Used to disable automatic lazy rendering (culling)
155
156     std::vector<SGPropertyNode*> _dirty_placements;
157     std::vector<canvas::Placements> _placements;
158
159     typedef std::map<std::string, canvas::PlacementFactory> PlacementFactoryMap;
160     static PlacementFactoryMap _placement_factories;
161
162     void setStatusFlags(unsigned int flags, bool set = true);
163 };
164
165 #endif /* CANVAS_HXX_ */