From 6870a88696213c3381f0747fb7ad8cfb47525b7b Mon Sep 17 00:00:00 2001 From: Philosopher Date: Thu, 15 Aug 2013 21:05:16 -0500 Subject: [PATCH] Fix for issue #999 Introduces delay-sec and release-delay-sec properties. The former is how long to wait to run the binding(s) after pressing the button, the latter is how long to wait after releasing the button. interval-sec now specifies the delay before a repeat event occurs. --- src/Input/FGButton.cxx | 2 ++ src/Input/FGButton.hxx | 2 +- src/Input/FGJoystickInput.cxx | 30 +++++++++++++++++++++++------- src/Input/FGJoystickInput.hxx | 2 +- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/Input/FGButton.cxx b/src/Input/FGButton.cxx index 315b4d3e9..400de289e 100644 --- a/src/Input/FGButton.cxx +++ b/src/Input/FGButton.cxx @@ -32,6 +32,8 @@ FGButton::FGButton () : is_repeatable(false), interval_sec(0), + delay_sec(0), + release_delay_sec(0), last_dt(0), last_state(0) { diff --git a/src/Input/FGButton.hxx b/src/Input/FGButton.hxx index 80c9eed6d..497be1c46 100644 --- a/src/Input/FGButton.hxx +++ b/src/Input/FGButton.hxx @@ -35,7 +35,7 @@ public: void init( const SGPropertyNode * node, const std::string & name, std::string & module ); void update( int modifiers, bool pressed, int x = -1, int y = -1); bool is_repeatable; - float interval_sec; + float interval_sec, delay_sec, release_delay_sec; float last_dt; int last_state; binding_list_t bindings[KEYMOD_MAX]; diff --git a/src/Input/FGJoystickInput.cxx b/src/Input/FGJoystickInput.cxx index b15649cda..4cbeb1ae1 100644 --- a/src/Input/FGJoystickInput.cxx +++ b/src/Input/FGJoystickInput.cxx @@ -42,6 +42,8 @@ FGJoystickInput::axis::axis () low_threshold(-0.9), high_threshold(0.9), interval_sec(0), + delay_sec(0), + release_delay_sec(0), last_dt(0) { } @@ -264,6 +266,8 @@ void FGJoystickInput::postinit() a.high.init(axis_node->getChild("high"), "high", module ); a.high_threshold = axis_node->getDoubleValue("high-threshold", 0.9); a.interval_sec = axis_node->getDoubleValue("interval-sec",0.0); + a.delay_sec = axis_node->getDoubleValue("delay-sec",0.0); + a.release_delay_sec = axis_node->getDoubleValue("release-delay-sec",0.0); a.last_dt = 0.0; } @@ -291,6 +295,8 @@ void FGJoystickInput::postinit() b.init(button_node, buf.str(), module ); // get interval-sec property b.interval_sec = button_node->getDoubleValue("interval-sec",0.0); + b.delay_sec = button_node->getDoubleValue("delay-sec",0.0); + b.release_delay_sec = button_node->getDoubleValue("release-delay-sec",0.0); b.last_dt = 0.0; } @@ -305,6 +311,8 @@ void FGJoystickInput::updateJoystick(int index, FGJoystickInput::joystick* joy, float axis_values[MAX_JOYSTICK_AXES]; int modifiers = fgGetKeyModifiers(); int buttons; + bool pressed, last_state; + float delay; jsJoystick * js = joy->plibJS.get(); if (js == 0 || js->notWorking()) @@ -340,25 +348,33 @@ void FGJoystickInput::updateJoystick(int index, FGJoystickInput::joystick* joy, } // do we have to emulate axis buttons? - a.last_dt += dt; - if(a.last_dt >= a.interval_sec) { + last_state = joy->axes[j].low.last_state || joy->axes[j].high.last_state; + pressed = axis_values[j] < a.low_threshold || axis_values[j] > a.high_threshold; + delay = (pressed ? last_state ? a.interval_sec : a.delay_sec : a.release_delay_sec ); + if(pressed || last_state) a.last_dt += dt; + else a.last_dt = 0; + if(a.last_dt >= delay) { if (a.low.bindings[modifiers].size()) joy->axes[j].low.update( modifiers, axis_values[j] < a.low_threshold ); if (a.high.bindings[modifiers].size()) joy->axes[j].high.update( modifiers, axis_values[j] > a.high_threshold ); - a.last_dt -= a.interval_sec; + a.last_dt -= delay; } } // of axes iteration // Fire bindings for the buttons. for (int j = 0; j < joy->nbuttons; j++) { FGButton &b = joy->buttons[j]; - b.last_dt += dt; - if(b.last_dt >= b.interval_sec) { - joy->buttons[j].update( modifiers, (buttons & (1u << j)) > 0 ); - b.last_dt -= b.interval_sec; + pressed = (buttons & (1u << j)) > 0; + last_state = joy->buttons[j].last_state; + delay = (pressed ? last_state ? b.interval_sec : b.delay_sec : b.release_delay_sec ); + if(pressed || last_state) b.last_dt += dt; + else b.last_dt = 0; + if(b.last_dt >= delay) { + joy->buttons[j].update( modifiers, pressed ); + b.last_dt -= delay; } } // of butotns iterations diff --git a/src/Input/FGJoystickInput.hxx b/src/Input/FGJoystickInput.hxx index 89a9c9f20..b161da9a3 100644 --- a/src/Input/FGJoystickInput.hxx +++ b/src/Input/FGJoystickInput.hxx @@ -68,7 +68,7 @@ private: float high_threshold; FGButton low; FGButton high; - float interval_sec; + float interval_sec, delay_sec, release_delay_sec; double last_dt; }; -- 2.39.5