]> git.mxchange.org Git - flightgear.git/blob - src/Canvas/canvas.hxx
29d9ee93e04d368bfb9af56fde5d71b3f2d8b2e5
[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
31 #include <memory>
32 #include <string>
33
34 class Canvas:
35   public PropertyBasedElement
36 {
37   public:
38
39     enum StatusFlags
40     {
41       STATUS_OK,
42       MISSING_SIZE_X = 0x0001,
43       MISSING_SIZE_Y = 0x0002,
44       CREATE_FAILED  = 0x0004
45     };
46
47     /**
48      * Callback used to disable/enable rendering to the texture if it is not
49      * visible
50      */
51     class CameraCullCallback:
52       public osg::NodeCallback
53     {
54       public:
55         CameraCullCallback();
56
57         /**
58          * Enable rendering for the next frame
59          */
60         void enableRendering();
61
62       private:
63         bool _render;
64         unsigned int _render_frame;
65
66         virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
67     };
68     typedef osg::ref_ptr<CameraCullCallback> CameraCullCallbackPtr;
69
70     /**
71      * This callback is installed on every placement of the canvas in the
72      * scene to only render the canvas if at least one placement is visible
73      */
74     class CullCallback:
75       public osg::NodeCallback
76     {
77       public:
78         CullCallback(CameraCullCallback* camera_cull);
79
80       private:
81         CameraCullCallback *_camera_cull;
82
83         virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
84     };
85     typedef osg::ref_ptr<CullCallback> CullCallbackPtr;
86
87     Canvas(SGPropertyNode* node);
88     virtual ~Canvas();
89
90     void update(double delta_time_sec);
91
92     int getSizeX() const
93     { return _size_x; }
94   
95     int getSizeY() const
96     { return _size_y; }
97   
98     void setSizeX(int sx);
99     void setSizeY(int sy);
100
101     void setViewWidth(int w);
102     void setViewHeight(int h);
103
104     bool handleMouseEvent(const canvas::MouseEvent& event);
105
106     virtual void childAdded( SGPropertyNode * parent,
107                              SGPropertyNode * child );
108     virtual void childRemoved( SGPropertyNode * parent,
109                                SGPropertyNode * child );
110     virtual void valueChanged (SGPropertyNode * node);
111
112     osg::Texture2D* getTexture() const;
113     GLuint getTexId() const;
114
115     CameraCullCallbackPtr getCameraCullCallback() const;
116     CullCallbackPtr getCullCallback() const;
117
118     static void addPlacementFactory( const std::string& type,
119                                      canvas::PlacementFactory factory );
120
121   private:
122
123     Canvas(const Canvas&); // = delete;
124     Canvas& operator=(const Canvas&); // = delete;
125
126     int _size_x,
127         _size_y,
128         _view_width,
129         _view_height;
130
131     simgear::PropertyObject<int>            _status;
132     simgear::PropertyObject<std::string>    _status_msg;
133
134     simgear::PropertyObject<int>    _mouse_x, _mouse_y,
135                                     _mouse_dx, _mouse_dy,
136                                     _mouse_button,
137                                     _mouse_state,
138                                     _mouse_mod,
139                                     _mouse_scroll,
140                                     _mouse_event;
141
142     bool _sampling_dirty,
143          _color_dirty;
144
145     FGODGauge _texture;
146     std::auto_ptr<canvas::Group> _root_group;
147
148     std::vector<SGPropertyNode_ptr> _color_background;
149
150     CameraCullCallbackPtr _camera_callback;
151     CullCallbackPtr _cull_callback;
152     bool _render_always; //<! Used to disable automatic lazy rendering (culling)
153
154     std::vector<SGPropertyNode*> _dirty_placements;
155     std::vector<canvas::Placements> _placements;
156
157     typedef std::map<std::string, canvas::PlacementFactory> PlacementFactoryMap;
158     static PlacementFactoryMap _placement_factories;
159
160     void setStatusFlags(unsigned int flags, bool set = true);
161 };
162
163 #endif /* CANVAS_HXX_ */