From: Mikko Rasa Date: Thu, 17 Nov 2022 22:37:17 +0000 (+0200) Subject: Keep track of edges in Control X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=8b985716894cd5f585a04a6253dd67060abe0b2f;p=libs%2Fgui.git Keep track of edges in Control This offers a way to reliably tell if a control was actuated for applications which don't want to use the signal interface. --- diff --git a/source/input/binarycontrol.cpp b/source/input/binarycontrol.cpp index b0148cc..09efe40 100644 --- a/source/input/binarycontrol.cpp +++ b/source/input/binarycontrol.cpp @@ -34,6 +34,7 @@ void BinaryControl::on_press() if(!state) { state = true; + rising_edge = true; signal_press.emit(); } } @@ -43,6 +44,7 @@ void BinaryControl::on_release() if(state) { state = false; + falling_edge = true; signal_release.emit(); } } diff --git a/source/input/binarycontrol.h b/source/input/binarycontrol.h index 64e4ab1..ff773b8 100644 --- a/source/input/binarycontrol.h +++ b/source/input/binarycontrol.h @@ -32,6 +32,8 @@ public: void set_threshold(float); bool get_state() const { return state; } + bool was_pressed() const { return rising_edge; } + bool was_released() const { return falling_edge; } private: virtual void on_press(); diff --git a/source/input/control.cpp b/source/input/control.cpp index b2f9bad..3e2398d 100644 --- a/source/input/control.cpp +++ b/source/input/control.cpp @@ -39,21 +39,27 @@ std::string ControlSource::str() const Control::Control(): capture_dev(0), activator(0), - origin(0) + origin(0), + rising_edge(false), + falling_edge(false) { } Control::Control(const ControlSource &s): src(s), capture_dev(0), activator(0), - origin(0) + origin(0), + rising_edge(false), + falling_edge(false) { } Control::Control(Device &d, ControlSrcType t, unsigned i): src(d, t, i), capture_dev(0), activator(0), - origin(0) + origin(0), + rising_edge(false), + falling_edge(false) { connect_signals(); } @@ -95,6 +101,12 @@ void Control::set_activator(BinaryControl *ctrl) connect_signals(); } +void Control::reset_edges() +{ + rising_edge = false; + falling_edge = false; +} + void Control::connect_signals() { switch(src.type) diff --git a/source/input/control.h b/source/input/control.h index 450b057..7a4fd0e 100644 --- a/source/input/control.h +++ b/source/input/control.h @@ -58,6 +58,8 @@ protected: Device *capture_dev; BinaryControl *activator; float origin; + bool rising_edge; + bool falling_edge; Control(); Control(const ControlSource &); @@ -72,6 +74,9 @@ public: const ControlSource &get_source() const { return src; } void set_activator(BinaryControl *); BinaryControl *get_activator() const { return activator; } + bool has_rising_edge() const { return rising_edge; } + bool has_falling_edge() const { return falling_edge; } + void reset_edges(); protected: virtual void on_press() = 0; virtual void on_release() = 0; diff --git a/source/input/smoothcontrol.cpp b/source/input/smoothcontrol.cpp index 6f4f5a4..3218753 100644 --- a/source/input/smoothcontrol.cpp +++ b/source/input/smoothcontrol.cpp @@ -87,6 +87,7 @@ void SmoothControl::on_release() void SmoothControl::on_motion(float v, float r) { + float old_value = value; if(v<-dead_zone) value = v+dead_zone; else if(v>dead_zone) @@ -104,6 +105,11 @@ void SmoothControl::on_motion(float v, float r) value /= threshold-dead_zone; } + if(value && !old_value) + rising_edge = true; + else if(!value && old_value) + falling_edge = true; + signal_motion.emit(value); if(paired_ctrl && (v>0 || (v==0 && paired_ctrl->value!=0)))