[mousetrap] Joystick mode is back



commit 12a6f56935d457876d284202651894e0e8e10f46
Author: Flavio Percoco Premoli <flaper87 gmail com>
Date:   Sun Mar 28 04:11:30 2010 +0200

    Joystick mode is back

 src/mousetrap/app/lib/mouse.py           |   46 ++++++++++++++++++++++-------
 src/mousetrap/app/lib/settings.py        |    3 ++
 src/mousetrap/app/main.py                |    4 +-
 src/mousetrap/app/ui/scripts/__init__.py |   21 +++++++++++++
 src/mousetrap/app/ui/settings_gui.py     |   20 ++++++++++--
 5 files changed, 77 insertions(+), 17 deletions(-)
---
diff --git a/src/mousetrap/app/lib/mouse.py b/src/mousetrap/app/lib/mouse.py
index 5f6f1a9..07efcfb 100644
--- a/src/mousetrap/app/lib/mouse.py
+++ b/src/mousetrap/app/lib/mouse.py
@@ -120,17 +120,41 @@ def move( x=0, y=0, point=None ):
 
     if point is not None:
         x, y = point.x, point.y
-
-    if isGnome:
-        try:
-            reg.generateMouseEvent( x, y, 'abs' )
-        except:
-            isGnome = False
-    else:
-        xtest.fake_input( xDisplay, X.MotionNotify, x = x, y = y)
-        #display.sync()
-        xDisplay.flush()
-
+        
+    
+    old_x, old_y = position()
+    x_diff = abs(old_x - x) 
+    y_diff = abs(old_y - y) 
+    
+    while True:
+        old_x, old_y = position()
+        
+        if x_diff <= 0 and y_diff <= 0:
+            break
+        
+        x_diff -= 1
+        y_diff -= 1
+        
+        if x > old_x:
+             new_x = x + 1
+        else:
+             new_x = x - 1
+             
+        if y > old_y:
+             new_y = y + 1
+        else:
+             new_y = y - 1
+             
+        if isGnome:
+            try:
+                reg.generateMouseEvent( x, y, 'abs' )
+            except:
+                isGnome = False
+        else:
+            xtest.fake_input( xDisplay, X.MotionNotify, x = x, y = y)
+            #display.sync()
+            xDisplay.flush()
+            
     return True
 
 ###########################################
diff --git a/src/mousetrap/app/lib/settings.py b/src/mousetrap/app/lib/settings.py
index 1a02e19..ac3cac7 100644
--- a/src/mousetrap/app/lib/settings.py
+++ b/src/mousetrap/app/lib/settings.py
@@ -86,6 +86,9 @@ class Settings( ConfigParser.ConfigParser ):
 
             conf.write("\n\n[access]")
             conf.write("\nreqMovement = 10")
+            
+            conf.write("\n\n[scripts]")
+            conf.write("\nname = joystick")
 
             conf.write("\n\n[cam]")
             conf.write("\nmouseMode = forehead")
diff --git a/src/mousetrap/app/main.py b/src/mousetrap/app/main.py
index 6e04c03..f1a633d 100644
--- a/src/mousetrap/app/main.py
+++ b/src/mousetrap/app/main.py
@@ -45,7 +45,7 @@ from mousetrap.ocvfw import pocv
 
 from ui.i18n import _
 from ui.main import MainGui
-from ui.scripts.screen import ScriptClass
+from ui.scripts import get_script_class
 
 from lib import httpd, dbusd, settings
 
@@ -221,7 +221,7 @@ class Controller():
         Arguments:
         - self: The main object pointer.
         """
-        return ScriptClass()
+        return get_script_class(self.cfg.get("scripts", "name"))()
 
     def update_frame(self):
         """
diff --git a/src/mousetrap/app/ui/scripts/__init__.py b/src/mousetrap/app/ui/scripts/__init__.py
index 7e8b8d7..06c39c8 100644
--- a/src/mousetrap/app/ui/scripts/__init__.py
+++ b/src/mousetrap/app/ui/scripts/__init__.py
@@ -23,3 +23,24 @@ __version__   = "$Revision$"
 __date__      = "$Date$"
 __copyright__ = "Copyright (c) 2008 Flavio Percoco Premoli"
 __license__   = "GPLv2"
+
+import re
+import os
+
+def get_scripts_list():
+    """
+    Checks the addons folder and gets the 
+    list of present addons.
+
+    Arguments:
+    - self: The main object pointer.
+    """
+    
+    reg = re.compile(r'([A-Za-z0-9]+)\.py$', re.DOTALL)
+    dirname = os.path.dirname(__file__)
+    return [ mod[0] for mod in [ reg.findall(f) for f in os.listdir("%s/" % dirname) if "__init__" not in f] if mod ]
+
+
+def get_script_class(script_name):
+    script = __import__(script_name, globals(), locals())
+    return getattr(script, "ScriptClass")
\ No newline at end of file
diff --git a/src/mousetrap/app/ui/settings_gui.py b/src/mousetrap/app/ui/settings_gui.py
index 6f90491..82d961a 100644
--- a/src/mousetrap/app/ui/settings_gui.py
+++ b/src/mousetrap/app/ui/settings_gui.py
@@ -32,6 +32,8 @@ import dialogs
 from i18n import _
 from mousetrap.ocvfw import pocv
 import mousetrap.app.environment as env
+
+from mousetrap.app.ui.scripts import get_scripts_list
 from mousetrap.app.addons.handler import AddonsHandler
 
 class PreffGui( gtk.Window ):
@@ -219,6 +221,16 @@ class PreffGui( gtk.Window ):
         conf_button.connect('clicked', self.show_alg_pref, liststore)
         conf_button.set_sensitive(False)
 
+        scripts_combo = gtk.combo_box_new_text()
+        scripts_combo.append_text(self.cfg.get("scripts", "name"))
+        
+        for script in get_scripts_list():
+            if script.lower() != self.cfg.get("scripts", "name"):
+                scripts_combo.append_text(script)
+
+        scripts_combo.connect('changed', self._comboChanged, "scripts", "name")
+        scripts_combo.set_active(0)
+        
         tree_view = gtk.TreeView(liststore)
         tree_view.connect("cursor-changed", self._tree_view_click, conf_button)
 
@@ -262,8 +274,7 @@ class PreffGui( gtk.Window ):
 
         algo_box.pack_start(tree_view)
         algo_box.pack_start(conf_button, False, False)
-
-
+        algo_box.pack_start(scripts_combo, False, False)
 
         algo_box.show_all()
 
@@ -535,7 +546,7 @@ class PreffGui( gtk.Window ):
         """
         self.cfg.write( open( env.configPath + 'userSettings.cfg', "w" ) )
 
-    def _comboChanged( self, widget, section, option, modes ):
+    def _comboChanged( self, widget, section, option, modes=None ):
         """
         On combo change. This function is the callback for the on_change
         event.
@@ -553,7 +564,8 @@ class PreffGui( gtk.Window ):
 
         model = widget.get_model()
         index = widget.get_active()
-        self.cfg.set( section, option, modes[model[index][0]] )
+        val = (modes and modes[model[index][0]]) or model[index][0] 
+        self.cfg.set( section, option, val)
 
     def addSpin( self, label, var, startValue, section, option, min_ = 1, max_ = 15):
         """



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]