1
0
Fork 0

SGBinding: allow Nasal function bindings

Allow wrapping of a naFunc as a custom binding class, without
the need to create a proxy command in the global command
manager.

Requires corresponding SG commit.
This commit is contained in:
James Turner 2023-08-25 12:06:53 +01:00
parent 1902a67611
commit 56876e486f
5 changed files with 50 additions and 51 deletions

View file

@ -39,7 +39,7 @@
class FGCommonInput {
public:
typedef std::vector<SGSharedPtr<SGBinding> > binding_list_t;
using binding_list_t = SGBindingList;
/*
read all "binding" nodes directly under the specified base node and fill the

View file

@ -136,7 +136,7 @@ void FGInputEvent::fire( FGEventData & eventData )
}
}
void FGInputEvent::fire( SGBinding * binding, FGEventData & eventData )
void FGInputEvent::fire( SGAbstractBinding * binding, FGEventData & eventData )
{
binding->fire();
}
@ -194,7 +194,7 @@ void FGAxisEvent::fire( FGEventData & eventData )
FGInputEvent::fire( ed );
}
void FGAbsAxisEvent::fire( SGBinding * binding, FGEventData & eventData )
void FGAbsAxisEvent::fire( SGAbstractBinding * binding, FGEventData & eventData )
{
// sets the "setting" node
binding->fire( eventData.value );
@ -207,7 +207,7 @@ FGRelAxisEvent::FGRelAxisEvent( FGInputDevice * device, SGPropertyNode_ptr node
tolerance = 0.0;
}
void FGRelAxisEvent::fire( SGBinding * binding, FGEventData & eventData )
void FGRelAxisEvent::fire( SGAbstractBinding * binding, FGEventData & eventData )
{
// sets the "offset" node
binding->fire( eventData.value, 1.0 );

View file

@ -151,7 +151,7 @@ public:
static FGInputEvent * NewObject( FGInputDevice * device, SGPropertyNode_ptr node );
protected:
virtual void fire( SGBinding * binding, FGEventData & eventData );
virtual void fire( SGAbstractBinding * binding, FGEventData & eventData );
/* A more or less meaningfull description of the event */
std::string desc;
@ -214,7 +214,7 @@ public:
FGRelAxisEvent( FGInputDevice * device, SGPropertyNode_ptr node );
protected:
virtual void fire( SGBinding * binding, FGEventData & eventData );
void fire( SGAbstractBinding * binding, FGEventData & eventData ) override;
};
class FGAbsAxisEvent : public FGAxisEvent
@ -223,7 +223,7 @@ public:
FGAbsAxisEvent( FGInputDevice * device, SGPropertyNode_ptr node ) : FGAxisEvent( device, node ) {}
protected:
virtual void fire( SGBinding * binding, FGEventData & eventData );
void fire( SGAbstractBinding * binding, FGEventData & eventData ) override;
};
typedef class SGSharedPtr<FGInputEvent> FGInputEvent_ptr;

View file

@ -1,26 +1,8 @@
// NasalCondition -- expose SGCondition to Nasal
//
// Written by James Turner, started 2012.
//
// Copyright (C) 2012 James Turner
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
// SPDX-FileComment: expose SGCondition and SGBinding to Nasal
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2012 James Turner <james@flightgear.org>
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "config.h"
#include "NasalCondition.hxx"
#include "NasalSys.hxx"
@ -30,8 +12,16 @@
#include <simgear/nasal/cppbind/NasalHash.hxx>
#include <simgear/props/condition.hxx>
using NasalBindingRef = SGSharedPtr<NasalBinding>;
typedef nasal::Ghost<SGConditionRef> NasalCondition;
void NasalBinding::innerFire() const
{
auto nas = globals->get_subsystem<FGNasalSys>();
m_callback(nas->wrappedPropsNode(_arg));
}
//------------------------------------------------------------------------------
static naRef f_createCondition(naContext c, naRef me, int argc, naRef* args)
{
@ -65,5 +55,7 @@ naRef initNasalCondition(naRef globals, naContext c)
nasal::Hash(globals, c).set("_createCondition", f_createCondition);
return naNil();
}

View file

@ -1,28 +1,35 @@
// NasalCondition.hxx -- expose SGCondition to Nasal
//
// Written by James Turner, started 2012.
//
// Copyright (C) 2012 James Turner
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
// SPDX-FileComment: expose SGCondition and SGBinding to Nasal
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2012 James Turner <james@flightgear.org>
#ifndef SCRIPTING_NASAL_CONDITION_HXX
#define SCRIPTING_NASAL_CONDITION_HXX
#pragma once
#include <simgear/nasal/nasal.h>
#include <simgear/structure/SGBinding.hxx>
naRef initNasalCondition(naRef globals, naContext c);
#endif // of SCRIPTING_NASAL_CONDITION_HXX
/**
* @brief implementation of SGAbstractBinding which
* invokes a Nasal callback. (without it being registered
* as a command).
*
* The binding argument properties are converted to a wrapped
* Nasal Props.Node before the binding is fired
*
*/
class NasalBinding : public SGAbstractBinding
{
public:
using NasalCallback = std::function<void(naRef)>;
NasalBinding(NasalCallback cb) :
m_callback(cb)
{};
private:
void innerFire() const override;
NasalCallback m_callback;
};