1
0
mirror of https://github.com/moparisthebest/Simba synced 2025-01-10 21:28:00 -05:00

Mouse class should be finished for now

This commit is contained in:
Merlijn Wajer 2010-04-02 11:36:47 +02:00
parent e413c07d61
commit e74cd3a92a
2 changed files with 53 additions and 20 deletions

View File

@ -3,6 +3,7 @@
from ctypes import * from ctypes import *
import platform import platform
from mmlmouse import Mouse from mmlmouse import Mouse
from time import sleep
class MMLCoreException(Exception): class MMLCoreException(Exception):
def __init__(self, err): def __init__(self, err):
@ -26,17 +27,18 @@ DLL = MMLCore('../libmml.so')
m = Mouse(DLL) m = Mouse(DLL)
print m[(Mouse.Pos, Mouse.Left, Mouse.Right)]
print m[Mouse.Pos]
for v in zip((Mouse.Left, Mouse.Right), m[(Mouse.Left, Mouse.Right)]):
print v
m[(Mouse.Pos, Mouse.Right)] = ((300,300), True) m[(Mouse.Pos, Mouse.Right)] = ((300,300), True)
#print m._getMousePos() print m.getButtonStates()
sleep(2)
# Reset all buttons.. # Reset all buttons..
m[(Mouse.Left, Mouse.Right, Mouse.Middle)] = [False for x in range(3)] m[(Mouse.Left, Mouse.Right, Mouse.Middle)] = [False for x in range(3)]
for v in zip((Mouse.Left, Mouse.Right), m[(Mouse.Left, Mouse.Right)]):
print v
print m.getPos()
del DLL del DLL

View File

@ -24,42 +24,73 @@ class Mouse(object):
self._initialiseDLLFuncs() self._initialiseDLLFuncs()
pass pass
def setPos(self, pos):
return self.__setitem__(Mouse.Pos, pos)
def getPos(self):
return self._getMousePos()
def getButtonStates(self):
return zip(self._getButtons().keys(), \
self.__getitem__(self._getButtons().keys()))
def setButtonState(self, button, downup):
return self.__setitem__(button, downup)
def __getitem__(self, item): def __getitem__(self, item):
'''Can currently return the state of mouse buttons as well as the '''Can currently return the state of mouse buttons as well as the
mouse position. Supports iterable arguments''' mouse position. Supports iterable arguments'''
if isiterable(item):
res = []
for i in item:
if i == self.Pos:
res.append(self._getMousePos())
elif i in self._getButtons().keys():
res.append(self._getMouseButtonState(self._buttonToInt(i)))
else:
print i
raise MouseException('Invalid mouse button')
return res
else:
if item == self.Pos: if item == self.Pos:
return self._getMousePos() return self._getMousePos()
if isiterable(item): if item in self._getButtons().keys():
for i in item: return self_getMouseButtonState(self_buttonToInt(item))
if i not in self._getButtons().keys():
raise MouseException('Invalid mouse button')
bs = [self._buttonToInt(x) for x in item] raise MouseException('item is not iterable nor a (valid) string')
return [self._getMouseButtonState(x) for x in bs]
raise MouseException('item is not iterable, nor a (valid) string')
def __setitem__(self, item, value): def __setitem__(self, item, value):
'''Can currently set the state of mouse buttons as well as the '''Can currently set the state of mouse buttons as well as the
mouse position. Supports iterable arguments''' mouse position. Supports iterable arguments'''
ak = self._getButtons().keys() + [self.Pos] ak = self._getButtons().keys() + [self.Pos]
isfalse = lambda x: True if not x in ak else False
if isiterable(item) and isiterable(value):
isfalse = lambda x: True if not x in ak else False
for i in map(isfalse, item): for i in map(isfalse, item):
if i: if i:
raise MouseException('One of the items is not valid. Items:', item) raise MouseException('One of the items is not valid. Items:', item)
if isiterable(item) and isiterable(value):
if len(item) != len(value): if len(item) != len(value):
raise MouseException('Not enough values for items') raise MouseException('Not enough values for items')
for i, v in dict(zip(item, value)).iteritems(): for i, v in dict(zip(item, value)).iteritems():
if i == self.Pos: if i == self.Pos:
self._setMousePos(v) self._setMousePos(v)
if i in self._getButtons().keys(): elif i in self._getButtons().keys():
self._setMouseButtonState(self._buttonToInt(i), \ self._setMouseButtonState(self._buttonToInt(i), \
1 if v else 0) 1 if v else 0)
return return
else:
if item in ak:
if item == self.Pos:
self_.setMousePos(value)
elif item in self._getButtons().keys():
self._setMouseButtonState(self._buttonToInt(item), \
1 if value else 0)
return
else:
raise MouseException('Invalid item / value')
raise MouseException('FIXME') raise MouseException('FIXME')