]> git.mxchange.org Git - flightgear.git/blob - src/Model/panelnode.cxx
Update for tweaked SGPickCallback API.
[flightgear.git] / src / Model / panelnode.cxx
1 #ifdef HAVE_CONFIG_H
2 #  include <config.h>
3 #endif
4
5 #include "panelnode.hxx"
6
7 #include <vector>
8 #include <algorithm>
9
10 #include <osg/Geode>
11 #include <osg/Switch>
12 #include <osg/BlendFunc>
13
14 #include <simgear/compiler.h>
15 #include <simgear/structure/exception.hxx>
16 #include <simgear/debug/logstream.hxx>
17
18 #include <simgear/scene/util/OsgMath.hxx>
19 #include <simgear/scene/util/SGPickCallback.hxx>
20 #include <simgear/scene/util/SGSceneUserData.hxx>
21 #include <simgear/scene/util/SGNodeMasks.hxx>
22
23 #include <plib/pu.h>
24
25 #include <Main/fg_os.hxx>
26 #include <Cockpit/panel.hxx>
27 #include <Cockpit/panel_io.hxx>
28 #include "Viewer/viewer.hxx"
29 #include "Viewer/viewmgr.hxx"
30
31 using std::vector;
32
33 static FGPanelNode* global_panel = NULL;
34
35 /**
36  * Built-in command: pass a mouse click to the panel.
37  *
38  * button: the mouse button number, zero-based.
39  * is-down: true if the button is down, false if it is up.
40  * x-pos: the x position of the mouse click.
41  * y-pos: the y position of the mouse click.
42  */
43 static bool
44 do_panel_mouse_click (const SGPropertyNode * arg)
45 {
46   if (global_panel)
47     return global_panel->getPanel()
48     ->doMouseAction(arg->getIntValue("button"),
49                     arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
50                     arg->getIntValue("x-pos"),
51                     arg->getIntValue("y-pos"));
52   else
53     return false;
54   
55   return false;
56 }
57
58 class FGPanelPickCallback : public SGPickCallback {
59 public:
60   FGPanelPickCallback(FGPanelNode* p) :
61     panel(p)
62   {}
63   
64   virtual bool buttonPressed(int b, const osgGA::GUIEventAdapter*, const Info& info)
65   {    
66     button = b;
67   // convert to panel coordinates
68     osg::Matrixd m = osg::Matrixd::inverse(panel->transformMatrix());
69     picked = toOsg(info.local) * m;
70     SG_LOG( SG_INSTR, SG_DEBUG, "panel pick: " << toSG(picked) );
71   
72   // send to the panel
73     return panel->getPanel()->doLocalMouseAction(button, MOUSE_BUTTON_DOWN, 
74                                           picked.x(), picked.y());
75   }
76   
77   virtual void update(double dt)
78   {
79     panel->getPanel()->updateMouseDelay(dt);
80   }
81   
82   virtual void buttonReleased(void)
83   {
84     panel->getPanel()->doLocalMouseAction(button, MOUSE_BUTTON_UP, 
85                                           picked.x(), picked.y());
86   }
87   
88 private:
89   FGPanelNode* panel;
90   int button;
91   osg::Vec3 picked;
92 };
93
94 class FGPanelSwitchCallback : public osg::NodeCallback {
95 public:
96   FGPanelSwitchCallback(FGPanelNode* pn) : 
97     panel(pn),
98     visProp(fgGetNode("/sim/panel/visibility"))
99   {
100   }
101   
102   virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
103   {
104     assert(dynamic_cast<osg::Switch*>(node));
105     osg::Switch* sw = static_cast<osg::Switch*>(node);
106     
107     if (!visProp->getBoolValue()) {
108       sw->setValue(0, false);
109       return;
110     }
111   
112     
113     panel->lazyLoad(); // isVisible check needs the panel loaded for auto-hide flag
114     bool enabled = panel->isVisible2d();
115     sw->setValue(0, enabled);
116     if (!enabled)
117       return;
118     
119     traverse(node, nv);
120   }
121   
122 private:
123   FGPanelNode* panel;
124   SGPropertyNode* visProp;
125 };
126
127
128 FGPanelNode::FGPanelNode(SGPropertyNode* props) :
129   _resizeToViewport(false)
130 {  
131   commonInit();
132   _panelPath = props->getStringValue("path");
133
134   // And the corner points
135   SGPropertyNode* pt = props->getChild("bottom-left");
136   _bottomLeft[0] = pt->getFloatValue("x-m");
137   _bottomLeft[1] = pt->getFloatValue("y-m");
138   _bottomLeft[2] = pt->getFloatValue("z-m");
139   
140   pt = props->getChild("top-left");
141   _topLeft[0] = pt->getFloatValue("x-m");
142   _topLeft[1] = pt->getFloatValue("y-m");
143   _topLeft[2] = pt->getFloatValue("z-m");
144   
145   pt = props->getChild("bottom-right");
146   _bottomRight[0] = pt->getFloatValue("x-m");
147   _bottomRight[1] = pt->getFloatValue("y-m");
148   _bottomRight[2] = pt->getFloatValue("z-m");
149   
150   _depthTest = props->getBoolValue("depth-test");
151 }
152
153 FGPanelNode::FGPanelNode() :
154   _resizeToViewport(true),
155   _depthTest(false)
156 {
157   commonInit();
158 }
159
160 void FGPanelNode::setPanelPath(const std::string& panel)
161 {
162   if (panel == _panelPath) {
163     return;
164   }
165   
166   _panelPath = panel;
167   if (_panel) {
168     _panel.clear();
169   }
170 }
171
172 void FGPanelNode::lazyLoad()
173 {
174   if (!_panelPath.empty() && !_panel) {
175     _panel = fgReadPanel(_panelPath);
176     if (!_panel) {
177       SG_LOG(SG_COCKPIT, SG_WARN, "failed to read panel from:" << _panelPath);
178       _panelPath = string(); // don't keep trying to read
179       return;
180     }
181     
182     _panel->setDepthTest(_depthTest);
183     initWithPanel();
184   }
185 }
186
187 void FGPanelNode::commonInit()
188 {
189   setUseDisplayList(false);
190   setDataVariance(Object::DYNAMIC);
191   getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
192   getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
193 }
194
195 void FGPanelNode::initWithPanel()
196 {
197   int i;
198
199   // Read out the pixel-space info
200   float panelWidth = _panel->getWidth();
201   float panelHeight = _panel->getHeight();
202
203   _panel->getLogicalExtent(_xmin, _ymin, _xmax, _ymax);
204
205   // Now generate our transformation matrix.  For shorthand, use
206   // "a", "b", and "c" as our corners and "m" as the matrix. The
207   // vector u goes from a to b, v from a to c, and w is a
208   // perpendicular cross product.
209   osg::Vec3 a = _bottomLeft;
210   osg::Vec3 b = _bottomRight;
211   osg::Vec3 c = _topLeft;
212   osg::Vec3 u = b - a;
213   osg::Vec3 v = c - a;
214   osg::Vec3 w = u^v;
215
216   osg::Matrix& m = _xform;
217   // Now generate a trivial basis transformation matrix.  If we want
218   // to map the three unit vectors to three arbitrary vectors U, V,
219   // and W, then those just become the columns of the 3x3 matrix.
220   m(0,0) = u[0]; m(1,0) = v[0]; m(2,0) = w[0]; m(3,0) = a[0];//    |Ux Vx Wx|
221   m(0,1) = u[1]; m(1,1) = v[1]; m(2,1) = w[1]; m(3,1) = a[1];//m = |Uy Vy Wy|
222   m(0,2) = u[2]; m(1,2) = v[2]; m(2,2) = w[2]; m(3,2) = a[2];//    |Uz Vz Wz|
223   m(0,3) = 0;    m(1,3) = 0;    m(2,3) = 0;    m(3,3) = 1;
224
225   // The above matrix maps the unit (!) square to the panel
226   // rectangle.  Postmultiply scaling factors that match the
227   // pixel-space size of the panel.
228   for(i=0; i<4; ++i) {
229       m(0,i) *= 1.0/panelWidth;
230       m(1,i) *= 1.0/panelHeight;
231   }
232
233   dirtyBound();
234 }
235
236 FGPanelNode::~FGPanelNode()
237 {
238 }
239
240 osg::Matrix FGPanelNode::transformMatrix() const
241 {
242   if (!_panel) {
243     return osg::Matrix();
244   }
245
246   if (!_resizeToViewport) {
247     return _xform;
248   }
249   
250   double s = _panel->getAspectScale();
251   osg::Matrix m = osg::Matrix::scale(s, s, 1.0);
252   m *= osg::Matrix::translate(_panel->getXOffset(), _panel->getYOffset(), 0.0);
253
254   return m;
255 }
256
257 void
258 FGPanelNode::drawImplementation(osg::State& state) const
259 {  
260   if (!_panel) {
261     return;
262   }
263   
264   osg::ref_ptr<osg::RefMatrix> mv = new osg::RefMatrix;
265   mv->set(transformMatrix() * state.getModelViewMatrix());
266   state.applyModelViewMatrix(mv.get());
267   
268   _panel->draw(state);
269 }
270
271 osg::BoundingBox
272 FGPanelNode::computeBound() const
273 {
274
275   osg::Vec3 coords[3];
276   osg::Matrix m(transformMatrix());
277   coords[0] = m.preMult(osg::Vec3(_xmin,_ymin,0));
278   coords[1] = m.preMult(osg::Vec3(_xmax,_ymin,0));
279   coords[2] = m.preMult(osg::Vec3(_xmin,_ymax,0));
280
281   osg::BoundingBox bb;
282   bb.expandBy(coords[0]);
283   bb.expandBy(coords[1]);
284   bb.expandBy(coords[2]);
285   return bb;
286 }
287
288 void FGPanelNode::accept(osg::PrimitiveFunctor& functor) const
289 {
290   osg::Vec3 coords[4];
291   osg::Matrix m(transformMatrix());
292   
293   coords[0] = m.preMult(osg::Vec3(_xmin,_ymin,0));
294   coords[1] = m.preMult(osg::Vec3(_xmax,_ymin,0));
295   coords[2] = m.preMult(osg::Vec3(_xmax, _ymax, 0));
296   coords[3] = m.preMult(osg::Vec3(_xmin,_ymax,0));
297
298   functor.setVertexArray(4, coords);
299   functor.drawArrays( GL_QUADS, 0, 4);
300 }
301
302 bool FGPanelNode::isVisible2d() const
303 {
304   if (!_panel) {
305     return false;
306   }
307   
308   if (!_hideNonDefaultViews) {
309     _hideNonDefaultViews = fgGetNode("/sim/panel/hide-nonzero-view", true);
310   }
311   
312   if (_hideNonDefaultViews->getBoolValue()) {
313     if (globals->get_viewmgr()->get_current() != 0) {
314       return false;
315     }
316   }
317   
318   if (!_autoHide2d) {
319     _autoHide2d = fgGetNode("/sim/panel/hide-nonzero-heading-offset", true);
320   }
321   
322   if (_panel->getAutohide() && _autoHide2d->getBoolValue()) {
323     if (!globals->get_current_view()) {
324       return false;
325     }
326     
327     return globals->get_current_view()->getHeadingOffset_deg() == 0;
328   }
329   
330   return true;
331 }
332
333 static osg::Node* createGeode(FGPanelNode* panel)
334 {
335     osg::Geode* geode = new osg::Geode;
336     geode->addDrawable(panel);
337     
338     geode->setNodeMask(SG_NODEMASK_PICK_BIT | SG_NODEMASK_2DPANEL_BIT);
339     
340     SGSceneUserData* userData;
341     userData = SGSceneUserData::getOrCreateSceneUserData(geode);
342     userData->setPickCallback(new FGPanelPickCallback(panel));
343     return geode;
344 }
345
346 class PanelPathObserver : public SGPropertyChangeListener
347 {
348 public:
349   PanelPathObserver(FGPanelNode* pn) : _panelNode(pn) {}
350   
351   virtual void valueChanged (SGPropertyNode * node)
352   {
353     _panelNode->setPanelPath(node->getStringValue());
354   }
355 private:
356   FGPanelNode* _panelNode;
357 };
358
359 osg::Node* FGPanelNode::create2DPanelNode()
360 {
361   SGCommandMgr::instance()->addCommand("panel-mouse-click", do_panel_mouse_click);
362   
363   SGPropertyNode* pathNode = fgGetNode("/sim/panel/path");
364   
365   FGPanelNode* drawable = new FGPanelNode();
366 // need a global to keep the panel_mouse_click command working, sadly
367   global_panel = drawable;
368   
369   PanelPathObserver* ppo = new PanelPathObserver(drawable);
370   pathNode->addChangeListener(ppo);
371   drawable->setPanelPath(pathNode->getStringValue());
372   
373   osg::Switch* ps = new osg::Switch;
374   osg::StateSet* stateSet = ps->getOrCreateStateSet();
375   stateSet->setRenderBinDetails(1000, "RenderBin");
376   ps->addChild(createGeode(drawable));
377   
378   // speed optimization?
379   stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
380   stateSet->setAttribute(new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA));
381   stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
382   stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
383   stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
384   
385   ps->setUpdateCallback(new FGPanelSwitchCallback(drawable));
386   return ps;
387 }
388
389 osg::Node* FGPanelNode::load(SGPropertyNode *n)
390 {
391   FGPanelNode* drawable = new FGPanelNode(n);
392   drawable->lazyLoad(); // force load now for 2.5D panels
393   return createGeode(drawable);
394 }