]> git.mxchange.org Git - flightgear.git/blob - src/Canvas/canvas.hxx
Separate instruments from cockpit displays.
[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 <Cockpit/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     /**
90      * Callback for resetting the render_dirty flag after rendering a frame.
91      */
92     class DrawCallback;
93
94     Canvas(SGPropertyNode* node);
95     virtual ~Canvas();
96
97     void update(double delta_time_sec);
98
99     int getSizeX() const
100     { return _size_x; }
101
102     int getSizeY() const
103     { return _size_y; }
104
105     void setSizeX(int sx);
106     void setSizeY(int sy);
107
108     void setViewWidth(int w);
109     void setViewHeight(int h);
110
111     bool handleMouseEvent(const canvas::MouseEvent& event);
112
113     virtual void childAdded( SGPropertyNode * parent,
114                              SGPropertyNode * child );
115     virtual void childRemoved( SGPropertyNode * parent,
116                                SGPropertyNode * child );
117     virtual void valueChanged (SGPropertyNode * node);
118
119     osg::Texture2D* getTexture() const;
120     GLuint getTexId() const;
121
122     CameraCullCallbackPtr getCameraCullCallback() const;
123     CullCallbackPtr getCullCallback() const;
124
125     static void addPlacementFactory( const std::string& type,
126                                      canvas::PlacementFactory factory );
127
128   private:
129
130     Canvas(const Canvas&); // = delete;
131     Canvas& operator=(const Canvas&); // = delete;
132
133     int _size_x,
134         _size_y,
135         _view_width,
136         _view_height;
137
138     simgear::PropertyObject<int>            _status;
139     simgear::PropertyObject<std::string>    _status_msg;
140
141     simgear::PropertyObject<int>    _mouse_x, _mouse_y,
142                                     _mouse_dx, _mouse_dy,
143                                     _mouse_button,
144                                     _mouse_state,
145                                     _mouse_mod,
146                                     _mouse_scroll,
147                                     _mouse_event;
148
149     bool _sampling_dirty,
150          _color_dirty,
151          _render_dirty;
152
153     FGODGauge _texture;
154     std::auto_ptr<canvas::Group> _root_group;
155
156     std::vector<SGPropertyNode_ptr> _color_background;
157
158     CameraCullCallbackPtr _camera_callback;
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<canvas::Placements> _placements;
164
165     typedef std::map<std::string, canvas::PlacementFactory> PlacementFactoryMap;
166     static PlacementFactoryMap _placement_factories;
167
168     void setStatusFlags(unsigned int flags, bool set = true);
169 };
170
171 #endif /* CANVAS_HXX_ */