]> git.tdb.fi Git - libs/gui.git/commitdiff
Keep track of edges in Control
authorMikko Rasa <tdb@tdb.fi>
Thu, 17 Nov 2022 22:37:17 +0000 (00:37 +0200)
committerMikko Rasa <tdb@tdb.fi>
Thu, 17 Nov 2022 22:41:43 +0000 (00:41 +0200)
This offers a way to reliably tell if a control was actuated for
applications which don't want to use the signal interface.

source/input/binarycontrol.cpp
source/input/binarycontrol.h
source/input/control.cpp
source/input/control.h
source/input/smoothcontrol.cpp

index b0148ccf5332f023d22c1a9dfe436ba94fe93783..09efe40ac74f32500660030088b389076fdc5409 100644 (file)
@@ -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();
        }
 }
index 64e4ab17be4ca1698e78c880a164f299d61b89e3..ff773b8a3fb8f2d225ac01ea387aa5998c6c16a6 100644 (file)
@@ -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();
index b2f9badd45b673026b90645bca2fcfef104cb648..3e2398d15860471dd8dc8eee9cad89ce1596e9c5 100644 (file)
@@ -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)
index 450b057ce30496b27f20ec251d3310163c45ae72..7a4fd0e0a4ddbf6bd384b317f35e475b9719d6f9 100644 (file)
@@ -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;
index 6f4f5a487857deebe9fdaeaec277def92126937e..3218753157628fc477c968acaa7a692673913b1d 100644 (file)
@@ -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)))