#include <simgear/misc/interpolator.hxx>
#include <simgear/io/HTTPRequest.hxx>
-#include <Cockpit/panel.hxx>
-#include <Cockpit/panel_io.hxx>
#include <FDM/flight.hxx>
#include <GUI/gui.h>
#include <GUI/new_gui.hxx>
static bool
do_panel_load (const SGPropertyNode * arg)
{
- string panel_path =
- arg->getStringValue("path", fgGetString("/sim/panel/path"));
- if (panel_path.empty()) {
- return false;
+ string panel_path = arg->getStringValue("path");
+ if (!panel_path.empty()) {
+ // write to the standard property, which will force a load
+ fgSetString("/sim/panel/path", panel_path.c_str());
}
- FGPanel * new_panel = fgReadPanel(panel_path);
- if (new_panel == 0) {
- SG_LOG(SG_INPUT, SG_ALERT,
- "Error reading new panel from " << panel_path);
- return false;
- }
- SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
- globals->get_current_panel()->unbind();
- globals->set_current_panel( new_panel );
- globals->get_current_panel()->bind();
return true;
}
-
-/**
- * Built-in command: pass a mouse click to the panel.
- *
- * button: the mouse button number, zero-based.
- * is-down: true if the button is down, false if it is up.
- * x-pos: the x position of the mouse click.
- * y-pos: the y position of the mouse click.
- */
-static bool
-do_panel_mouse_click (const SGPropertyNode * arg)
-{
- if (globals->get_current_panel() != 0)
- return globals->get_current_panel()
- ->doMouseAction(arg->getIntValue("button"),
- arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
- arg->getIntValue("x-pos"),
- arg->getIntValue("y-pos"));
- else
- return false;
-}
-
-
/**
* Built-in command: (re)load preferences.
*
{ "load", do_load },
{ "save", do_save },
{ "panel-load", do_panel_load },
- { "panel-mouse-click", do_panel_mouse_click },
{ "preferences-load", do_preferences_load },
{ "view-cycle", do_view_cycle },
{ "screen-capture", do_screen_capture },
#include <algorithm>
#include <osg/Geode>
+#include <osg/Switch>
+#include <osg/BlendFunc>
#include <simgear/compiler.h>
#include <simgear/structure/exception.hxx>
#include <simgear/scene/util/SGSceneUserData.hxx>
#include <simgear/scene/util/SGNodeMasks.hxx>
+#include <plib/pu.h>
+
#include <Main/fg_os.hxx>
#include <Cockpit/panel.hxx>
#include <Cockpit/panel_io.hxx>
-
+#include "Viewer/viewer.hxx"
using std::vector;
+static FGPanelNode* global_panel = NULL;
+
+/**
+ * Built-in command: pass a mouse click to the panel.
+ *
+ * button: the mouse button number, zero-based.
+ * is-down: true if the button is down, false if it is up.
+ * x-pos: the x position of the mouse click.
+ * y-pos: the y position of the mouse click.
+ */
+static bool
+do_panel_mouse_click (const SGPropertyNode * arg)
+{
+ if (global_panel)
+ return global_panel->getPanel()
+ ->doMouseAction(arg->getIntValue("button"),
+ arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
+ arg->getIntValue("x-pos"),
+ arg->getIntValue("y-pos"));
+ else
+ return false;
+
+ return false;
+}
+
class FGPanelPickCallback : public SGPickCallback {
public:
FGPanelPickCallback(FGPanelNode* p) :
osg::Vec3 picked;
};
+class FGPanelSwitchCallback : public osg::NodeCallback {
+public:
+ FGPanelSwitchCallback(FGPanelNode* pn) :
+ panel(pn),
+ visProp(fgGetNode("/sim/panel/visibility"))
+ {
+ }
+
+ virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
+ {
+ assert(dynamic_cast<osg::Switch*>(node));
+ osg::Switch* sw = static_cast<osg::Switch*>(node);
+
+ if (!visProp->getBoolValue()) {
+ sw->setValue(0, false);
+ return;
+ }
+
+
+ panel->lazyLoad(); // isVisible check needs the panel loaded for auto-hide flag
+ bool enabled = panel->isVisible2d();
+ sw->setValue(0, enabled);
+ if (!enabled)
+ return;
+
+ traverse(node, nv);
+ }
+
+private:
+ FGPanelNode* panel;
+ SGPropertyNode* visProp;
+};
+
+
FGPanelNode::FGPanelNode(SGPropertyNode* props) :
_resizeToViewport(false)
{
- // Make an FGPanel object. But *don't* call init() or bind() on
- // it -- those methods touch static state.
- const char *path = props->getStringValue("path");
- _panel = fgReadPanel(path);
- if (!_panel)
- throw sg_io_exception(string("Failed to load panel ") + path);
-
- // And the corner points
- SGPropertyNode* pt = props->getChild("bottom-left");
- _bottomLeft[0] = pt->getFloatValue("x-m");
- _bottomLeft[1] = pt->getFloatValue("y-m");
- _bottomLeft[2] = pt->getFloatValue("z-m");
-
- pt = props->getChild("top-left");
- _topLeft[0] = pt->getFloatValue("x-m");
- _topLeft[1] = pt->getFloatValue("y-m");
- _topLeft[2] = pt->getFloatValue("z-m");
-
- pt = props->getChild("bottom-right");
- _bottomRight[0] = pt->getFloatValue("x-m");
- _bottomRight[1] = pt->getFloatValue("y-m");
- _bottomRight[2] = pt->getFloatValue("z-m");
+ commonInit();
+ _panelPath = props->getStringValue("path");
+
+ // And the corner points
+ SGPropertyNode* pt = props->getChild("bottom-left");
+ _bottomLeft[0] = pt->getFloatValue("x-m");
+ _bottomLeft[1] = pt->getFloatValue("y-m");
+ _bottomLeft[2] = pt->getFloatValue("z-m");
- _panel->setDepthTest( props->getBoolValue("depth-test") );
+ pt = props->getChild("top-left");
+ _topLeft[0] = pt->getFloatValue("x-m");
+ _topLeft[1] = pt->getFloatValue("y-m");
+ _topLeft[2] = pt->getFloatValue("z-m");
- initWithPanel();
+ pt = props->getChild("bottom-right");
+ _bottomRight[0] = pt->getFloatValue("x-m");
+ _bottomRight[1] = pt->getFloatValue("y-m");
+ _bottomRight[2] = pt->getFloatValue("z-m");
+
+ _depthTest = props->getBoolValue("depth-test");
}
-FGPanelNode::FGPanelNode(FGPanel* p) :
- _panel(p),
- _resizeToViewport(true)
+FGPanelNode::FGPanelNode() :
+ _resizeToViewport(true),
+ _depthTest(false)
{
- initWithPanel();
+ commonInit();
}
-void FGPanelNode::initWithPanel()
+void FGPanelNode::setPanelPath(const std::string& panel)
{
- int i;
-
- // Never mind. We *have* to call init to make sure the static
- // state is initialized (it's not, if there aren't any 2D
- // panels). This is a memory leak and should be fixed!`
- // FIXME
- _panel->init();
-
- // Read out the pixel-space info
- float panelWidth = _panel->getWidth();
- float panelHeight = _panel->getHeight();
-
- _panel->getLogicalExtent(_xmin, _ymin, _xmax, _ymax);
-
- // Now generate our transformation matrix. For shorthand, use
- // "a", "b", and "c" as our corners and "m" as the matrix. The
- // vector u goes from a to b, v from a to c, and w is a
- // perpendicular cross product.
- osg::Vec3 a = _bottomLeft;
- osg::Vec3 b = _bottomRight;
- osg::Vec3 c = _topLeft;
- osg::Vec3 u = b - a;
- osg::Vec3 v = c - a;
- osg::Vec3 w = u^v;
-
- osg::Matrix& m = _xform;
- // Now generate a trivial basis transformation matrix. If we want
- // to map the three unit vectors to three arbitrary vectors U, V,
- // and W, then those just become the columns of the 3x3 matrix.
- m(0,0) = u[0]; m(1,0) = v[0]; m(2,0) = w[0]; m(3,0) = a[0];// |Ux Vx Wx|
- m(0,1) = u[1]; m(1,1) = v[1]; m(2,1) = w[1]; m(3,1) = a[1];//m = |Uy Vy Wy|
- m(0,2) = u[2]; m(1,2) = v[2]; m(2,2) = w[2]; m(3,2) = a[2];// |Uz Vz Wz|
- m(0,3) = 0; m(1,3) = 0; m(2,3) = 0; m(3,3) = 1;
-
- // The above matrix maps the unit (!) square to the panel
- // rectangle. Postmultiply scaling factors that match the
- // pixel-space size of the panel.
- for(i=0; i<4; ++i) {
- m(0,i) *= 1.0/panelWidth;
- m(1,i) *= 1.0/panelHeight;
+ if (panel == _panelPath) {
+ return;
+ }
+
+ _panelPath = panel;
+ if (_panel) {
+ _panel.clear();
+ }
+}
+
+void FGPanelNode::lazyLoad()
+{
+ if (!_panelPath.empty() && !_panel) {
+ _panel = fgReadPanel(_panelPath);
+ if (!_panel) {
+ SG_LOG(SG_COCKPIT, SG_WARN, "failed to read panel from:" << _panelPath);
+ _panelPath = string(); // don't keep trying to read
+ return;
}
+
+ _panel->setDepthTest(_depthTest);
+ initWithPanel();
+ }
+}
- dirtyBound();
- setUseDisplayList(false);
- setDataVariance(Object::DYNAMIC);
+void FGPanelNode::commonInit()
+{
+ setUseDisplayList(false);
+ setDataVariance(Object::DYNAMIC);
+ getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
+ getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
+}
+
+void FGPanelNode::initWithPanel()
+{
+ int i;
+
+ // Read out the pixel-space info
+ float panelWidth = _panel->getWidth();
+ float panelHeight = _panel->getHeight();
+
+ _panel->getLogicalExtent(_xmin, _ymin, _xmax, _ymax);
+
+ // Now generate our transformation matrix. For shorthand, use
+ // "a", "b", and "c" as our corners and "m" as the matrix. The
+ // vector u goes from a to b, v from a to c, and w is a
+ // perpendicular cross product.
+ osg::Vec3 a = _bottomLeft;
+ osg::Vec3 b = _bottomRight;
+ osg::Vec3 c = _topLeft;
+ osg::Vec3 u = b - a;
+ osg::Vec3 v = c - a;
+ osg::Vec3 w = u^v;
+
+ osg::Matrix& m = _xform;
+ // Now generate a trivial basis transformation matrix. If we want
+ // to map the three unit vectors to three arbitrary vectors U, V,
+ // and W, then those just become the columns of the 3x3 matrix.
+ m(0,0) = u[0]; m(1,0) = v[0]; m(2,0) = w[0]; m(3,0) = a[0];// |Ux Vx Wx|
+ m(0,1) = u[1]; m(1,1) = v[1]; m(2,1) = w[1]; m(3,1) = a[1];//m = |Uy Vy Wy|
+ m(0,2) = u[2]; m(1,2) = v[2]; m(2,2) = w[2]; m(3,2) = a[2];// |Uz Vz Wz|
+ m(0,3) = 0; m(1,3) = 0; m(2,3) = 0; m(3,3) = 1;
+
+ // The above matrix maps the unit (!) square to the panel
+ // rectangle. Postmultiply scaling factors that match the
+ // pixel-space size of the panel.
+ for(i=0; i<4; ++i) {
+ m(0,i) *= 1.0/panelWidth;
+ m(1,i) *= 1.0/panelHeight;
+ }
- getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
- getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
+ dirtyBound();
}
FGPanelNode::~FGPanelNode()
osg::Matrix FGPanelNode::transformMatrix() const
{
+ if (!_panel) {
+ osg::Matrix();
+ }
+
if (!_resizeToViewport) {
return _xform;
}
void
FGPanelNode::drawImplementation(osg::State& state) const
{
+ if (!_panel) {
+ return;
+ }
+
osg::ref_ptr<osg::RefMatrix> mv = new osg::RefMatrix;
mv->set(transformMatrix() * state.getModelViewMatrix());
state.applyModelViewMatrix(mv.get());
functor.drawArrays( GL_QUADS, 0, 4);
}
+bool FGPanelNode::isVisible2d() const
+{
+ if (!_panel) {
+ return false;
+ }
+
+ if (_panel->getAutohide()) {
+ if (!globals->get_current_view()) {
+ return false;
+ }
+
+ return globals->get_current_view()->getHeadingOffset_deg() == 0;
+ }
+
+ return true;
+}
+
static osg::Node* createGeode(FGPanelNode* panel)
{
osg::Geode* geode = new osg::Geode;
return geode;
}
-osg::Node* FGPanelNode::createNode(FGPanel* p)
+class PanelPathObserver : public SGPropertyChangeListener
+{
+public:
+ PanelPathObserver(FGPanelNode* pn) : _panelNode(pn) {}
+
+ virtual void valueChanged (SGPropertyNode * node)
+ {
+ _panelNode->setPanelPath(node->getStringValue());
+ }
+private:
+ FGPanelNode* _panelNode;
+};
+
+osg::Node* FGPanelNode::create2DPanelNode()
{
- FGPanelNode* drawable = new FGPanelNode(p);
- return createGeode(drawable);
+ SGCommandMgr::instance()->addCommand("panel-mouse-click", do_panel_mouse_click);
+
+ SGPropertyNode* pathNode = fgGetNode("/sim/panel/path");
+
+ FGPanelNode* drawable = new FGPanelNode();
+// need a global to keep the panel_mouse_click command working, sadly
+ global_panel = drawable;
+
+ PanelPathObserver* ppo = new PanelPathObserver(drawable);
+ pathNode->addChangeListener(ppo);
+ drawable->setPanelPath(pathNode->getStringValue());
+
+ osg::Switch* ps = new osg::Switch;
+ osg::StateSet* stateSet = ps->getOrCreateStateSet();
+ stateSet->setRenderBinDetails(1000, "RenderBin");
+ ps->addChild(createGeode(drawable));
+
+ // speed optimization?
+ stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
+ stateSet->setAttribute(new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA));
+ stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
+ stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
+ stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
+
+ ps->setUpdateCallback(new FGPanelSwitchCallback(drawable));
+ return ps;
}
osg::Node* FGPanelNode::load(SGPropertyNode *n)
{
FGPanelNode* drawable = new FGPanelNode(n);
+ drawable->lazyLoad(); // force load now for 2.5D panels
return createGeode(drawable);
}