1 // The canvas for rendering with the 2d api
3 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
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.
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.
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.
22 #include "placement.hxx"
23 #include "property_based_element.hxx"
25 #include <Canvas/canvas_fwd.hpp>
26 #include <Instrumentation/od_gauge.hxx>
28 #include <simgear/props/propertyObject.hxx>
29 #include <osg/NodeCallback>
30 #include <osg/observer_ptr>
36 public PropertyBasedElement
43 MISSING_SIZE_X = 0x0001,
44 MISSING_SIZE_Y = 0x0002,
45 CREATE_FAILED = 0x0004
49 * Callback used to disable/enable rendering to the texture if it is not
52 class CameraCullCallback:
53 public osg::NodeCallback
59 * Enable rendering for the next frame
61 void enableRendering();
65 unsigned int _render_frame;
67 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
69 typedef osg::ref_ptr<CameraCullCallback> CameraCullCallbackPtr;
70 typedef osg::observer_ptr<CameraCullCallback> CameraCullCallbackWeakPtr;
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
77 public osg::NodeCallback
80 CullCallback(CameraCullCallback* camera_cull);
83 CameraCullCallbackWeakPtr _camera_cull;
85 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
87 typedef osg::ref_ptr<CullCallback> CullCallbackPtr;
89 Canvas(SGPropertyNode* node);
92 void update(double delta_time_sec);
100 void setSizeX(int sx);
101 void setSizeY(int sy);
103 void setViewWidth(int w);
104 void setViewHeight(int h);
106 bool handleMouseEvent(const canvas::MouseEvent& event);
108 virtual void childAdded( SGPropertyNode * parent,
109 SGPropertyNode * child );
110 virtual void childRemoved( SGPropertyNode * parent,
111 SGPropertyNode * child );
112 virtual void valueChanged (SGPropertyNode * node);
114 osg::Texture2D* getTexture() const;
115 GLuint getTexId() const;
117 CameraCullCallbackPtr getCameraCullCallback() const;
118 CullCallbackPtr getCullCallback() const;
120 static void addPlacementFactory( const std::string& type,
121 canvas::PlacementFactory factory );
125 Canvas(const Canvas&); // = delete;
126 Canvas& operator=(const Canvas&); // = delete;
133 simgear::PropertyObject<int> _status;
134 simgear::PropertyObject<std::string> _status_msg;
136 simgear::PropertyObject<int> _mouse_x, _mouse_y,
137 _mouse_dx, _mouse_dy,
144 bool _sampling_dirty,
149 std::auto_ptr<canvas::Group> _root_group;
151 std::vector<SGPropertyNode_ptr> _color_background;
153 CameraCullCallbackPtr _camera_callback;
154 CullCallbackPtr _cull_callback;
155 bool _render_always; //<! Used to disable automatic lazy rendering (culling)
157 std::vector<SGPropertyNode*> _dirty_placements;
158 std::vector<canvas::Placements> _placements;
160 typedef std::map<std::string, canvas::PlacementFactory> PlacementFactoryMap;
161 static PlacementFactoryMap _placement_factories;
163 void setStatusFlags(unsigned int flags, bool set = true);
166 #endif /* CANVAS_HXX_ */