DriverCurses: filter mousemove - generate event only when coordinates changes.
authorRadek Brich <radek.brich@devl.cz>
Wed, 23 Jan 2013 21:59:16 +0100
changeset 68 bc51b0220be2
parent 67 71f60bf6ebb7
child 69 4e7be77bafff
DriverCurses: filter mousemove - generate event only when coordinates changes.
tuikit/driver_curses.py
--- a/tuikit/driver_curses.py	Wed Jan 23 21:49:40 2013 +0100
+++ b/tuikit/driver_curses.py	Wed Jan 23 21:59:16 2013 +0100
@@ -82,6 +82,8 @@
         self.colorstack = [] # pushcolor/popcolor puts or gets attributes from this
         self.inputqueue = []
         self.mbtnstack = []
+        self._mouse_last_pos = (None, None)
+        self._mouse_last_bstate = None
 
     def init_curses(self):
         '''Initialize curses'''
@@ -301,20 +303,36 @@
         out = []
 
         if bstate & curses.REPORT_MOUSE_POSITION:
-            out += [('mousemove', x, y)]
+            if self._mouse_last_pos != (x, y):
+                self._mouse_last_pos = (x, y)
+                out += [('mousemove', x, y)]
+
+        # we are interested only in changes, not buttons already pressed before event
+        if self._mouse_last_bstate is not None:
+            old = self._mouse_last_bstate
+            new = bstate
+            bstate = ~old & new
+            self._mouse_last_bstate = new
+        else:
+            self._mouse_last_bstate = bstate
 
         if bstate & curses.BUTTON1_PRESSED:
             out += [('mousedown', x, y, 1)]
-
+        if bstate & curses.BUTTON2_PRESSED:
+            out += [('mousedown', x, y, 2)]
         if bstate & curses.BUTTON3_PRESSED:
             out += [('mousedown', x, y, 3)]
-
         if bstate & curses.BUTTON1_RELEASED:
             out += [('mouseup', x, y, 1)]
-
+        if bstate & curses.BUTTON2_RELEASED:
+            out += [('mouseup', x, y, 2)]
         if bstate & curses.BUTTON3_RELEASED:
             out += [('mouseup', x, y, 3)]
 
+        # reset last pos when pressed/released
+        if len(out) > 0 and out[-1][0] in ('mousedown', 'mouseup'):
+            self._mouse_last_pos = (None, None)
+
         return out
 
 
@@ -404,12 +422,16 @@
             btn = t - 0x1f
             if not btn in self.mbtnstack:
                 self.mbtnstack.append(btn)
+                self._mouse_last_pos = (None, None)
                 out += [('mousedown', x, y, btn)]
             else:
-                out += [('mousemove', x, y, btn)]
+                if self._mouse_last_pos != (x, y):
+                    self._mouse_last_pos = (x, y)
+                    out += [('mousemove', x, y, btn)]
 
         elif t == 0x23: # button release
             btn = self.mbtnstack.pop()
+            self._mouse_last_pos = (None, None)
             out += [('mouseup', x, y, btn)]
 
         elif t in (0x60, 0x61): # wheel up, down
@@ -444,7 +466,7 @@
         if len(params) == 0:
             params = [1]
 
-        # filter codes using
+        # filter codes using byte sequence
         while True:
             matching_codes = []
             for code in codes: