From 33f176b8f996bc7ed8e5cd5cdf1f2f3019cbd7a5 Mon Sep 17 00:00:00 2001 From: ehofman Date: Mon, 30 Jan 2006 10:56:34 +0000 Subject: [PATCH] Melchior FRANZ: add optional arg to SGPropertyNode::addChangeListener that triggers the listener function call initially. This is useful for cases where a freshly installed listener wants to treat the current property value as changed from 'unknown' to the actual value right away. Examples can be found in the Nasal incarnation setlistener(), where we have for example this (in $FG_ROOT/Nasal/gui.nas): INIT = func { ... setlistener("/sim/rendering/fps-display", fpsDisplay); if (getprop("/sim/rendering/fps-display")) { fgcommand("dialog-show", props.Node.new({"dialog-name": "fps"})); } } That is: we first attach a listener that cares for changes to the FPS display switch, but then we have to manually open the dialog initially. That's a duplication of code and could be as simple as this (INIT part only): INIT = func { ... setlistener("/sim/rendering/fps-display", fpsDisplay, 1); } That is: the optional third arg makes fpsDisplay be called initially, and then again with every write action. My first solution was in the Nasal code only, but Andy (rightfully) says that this should rather be in sg. --- simgear/props/props.cxx | 5 ++++- simgear/props/props.hxx | 6 ++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/simgear/props/props.cxx b/simgear/props/props.cxx index 673cde4c..2f26f2a6 100644 --- a/simgear/props/props.cxx +++ b/simgear/props/props.cxx @@ -2030,12 +2030,15 @@ SGPropertyNode::untie (const char * relative_path) } void -SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener) +SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener, + bool initial) { if (_listeners == 0) _listeners = new vector; _listeners->push_back(listener); listener->register_property(this); + if (initial) + listener->valueChanged(this); } void diff --git a/simgear/props/props.hxx b/simgear/props/props.hxx index fb04b6bf..332d47a2 100644 --- a/simgear/props/props.hxx +++ b/simgear/props/props.hxx @@ -1097,9 +1097,11 @@ public: /** - * Add a change listener to the property. + * Add a change listener to the property. If "initial" is set call the + * listener initially. */ - void addChangeListener (SGPropertyChangeListener * listener); + void addChangeListener (SGPropertyChangeListener * listener, + bool initial = false); /** -- 2.39.5