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