1
0
mirror of https://github.com/moparisthebest/Simba synced 2024-11-25 02:32:19 -05:00
Simba/Projects/libmml/pymml/mmlmouse.py

181 lines
5.9 KiB
Python
Raw Normal View History

2010-03-30 18:24:16 -04:00
from ctypes import *
from mmltypes import POINT, PPOINT
from mmltypes import isiterable
"""
The Mouseclass
---------------
The Mouse class controls the mouse movement and key presses.
"""
class MouseException(Exception):
def __init__(self, err):
Exception.__init__(self, err)
2010-03-30 18:24:16 -04:00
# Usage:
class Mouse(object):
2010-06-08 13:16:58 -04:00
"""
2010-04-14 18:13:31 -04:00
The MML Mouse object communicates directly with libmml,
but wraps it around a nice and easy to use layer.
2010-04-14 18:13:31 -04:00
It will allow several ways to set mouse positions and
buttons. __getitem__ and __setitem__ are also implemented,
so one can access mouse buttons states and positions with [].
2010-06-08 13:16:58 -04:00
"""
2010-03-30 18:24:16 -04:00
# _mc = MMLCore reference.
_mc = None
2010-05-19 14:05:40 -04:00
Left, Right, Middle, Pos = 'Left', 'Right', 'Middle', 'Pos'
2010-03-30 18:24:16 -04:00
2010-05-31 19:59:48 -04:00
# last mouse pointer position
2010-03-30 18:24:16 -04:00
_lpp = (0, 0)
def __init__(self, MC, cli):
2010-06-08 13:16:58 -04:00
""" Initialize the Mouse object. Needs a DLL Mufasa Core object
(which contains the dll reference.)"""
2010-03-30 18:24:16 -04:00
self._mc = MC
self._cli = cli
2010-03-30 18:24:16 -04:00
self._initialiseDLLFuncs()
pass
2010-04-02 05:36:47 -04:00
def setPos(self, pos):
2010-06-08 13:16:58 -04:00
"""
Set the mouse position to the tuple _pos_.
"""
2010-04-02 05:36:47 -04:00
return self.__setitem__(Mouse.Pos, pos)
def getPos(self):
2010-06-08 13:16:58 -04:00
"""
Get the current mouse position as a tuple.
"""
2010-04-02 05:36:47 -04:00
return self._getMousePos()
def getButtonStates(self):
2010-06-08 13:16:58 -04:00
"""
Get the current states of the mouse buttons.
"""
2010-04-02 05:36:47 -04:00
return zip(self._getButtons().keys(), \
self.__getitem__(self._getButtons().keys()))
def setButtonState(self, button, downup):
2010-06-08 13:16:58 -04:00
"""
Set the states of the mouse buttons.
"""
2010-04-02 05:36:47 -04:00
return self.__setitem__(button, downup)
2010-03-30 18:24:16 -04:00
def __getitem__(self, item):
2010-06-08 13:16:58 -04:00
"""Can currently return the state of mouse buttons as well as the
mouse position. Supports iterable arguments"""
if isiterable(item):
2010-04-02 05:36:47 -04:00
res = []
for i in item:
2010-04-02 05:36:47 -04:00
if i == self.Pos:
res.append(self._getMousePos())
elif i in self._getButtons().keys():
res.append(self._getMouseButtonState(self._buttonToInt(i)))
else:
raise MouseException('Invalid mouse button')
2010-04-02 05:36:47 -04:00
return res
2010-04-02 05:36:47 -04:00
else:
if item == self.Pos:
return self._getMousePos()
if item in self._getButtons().keys():
2010-04-02 06:00:40 -04:00
return self._getMouseButtonState(self_buttonToInt(item))
2010-04-02 05:36:47 -04:00
raise MouseException('item is not iterable nor a (valid) string')
2010-03-30 18:24:16 -04:00
def __setitem__(self, item, value):
2010-06-08 13:16:58 -04:00
"""Can currently set the state of mouse buttons as well as the
mouse position. Supports iterable arguments"""
ak = self._getButtons().keys() + [self.Pos]
if isiterable(item) and isiterable(value):
2010-04-02 05:36:47 -04:00
isfalse = lambda x: True if not x in ak else False
for i in map(isfalse, item):
if i:
raise MouseException('One of the items is not valid. Items:', item)
if len(item) != len(value):
raise MouseException('Not enough values for items')
for i, v in dict(zip(item, value)).iteritems():
if i == self.Pos:
self._setMousePos(v)
2010-04-02 05:36:47 -04:00
elif i in self._getButtons().keys():
self._setMouseButtonState(self._buttonToInt(i), \
1 if v else 0)
return
2010-04-02 05:36:47 -04:00
else:
if item in ak:
if item == self.Pos:
2010-05-31 19:59:48 -04:00
self._setMousePos(value)
2010-04-02 05:36:47 -04:00
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')
2010-04-01 19:56:26 -04:00
# Tools
def _getButtons(self):
2010-06-08 13:16:58 -04:00
"""Return mouse buttons with their corresponding button DLL number as dict"""
return {self.Left : 0, self.Right : 1, self.Middle : 2}
def _buttonToInt(self, button):
2010-06-08 13:16:58 -04:00
"""Return button number for button"""
return self._getButtons()[button]
2010-04-01 19:56:26 -04:00
2010-03-30 18:24:16 -04:00
2010-04-01 19:56:26 -04:00
# Internal DLL stuff
2010-03-30 18:24:16 -04:00
def _getMousePos(self):
ret = POINT()
ok = self._mc.dll.get_mouse_pos(self._cli, byref(ret))
2010-03-30 18:24:16 -04:00
# FIXME: Perhaps use some sort of assertion?
# We should print dll.last_error is ok != 0
self._lpp = (ret.x, ret.y)
return (ret.x, ret.y)
def _setMousePos(self, p):
ret = POINT()
ret.x, ret.y = p
ok = self._mc.dll.set_mouse_pos(self._cli, byref(ret))
2010-04-01 19:56:26 -04:00
if ok != 0:
pass # Raise exception
self._lpp = (ret.x, ret.y)
return ok
2010-03-30 18:24:16 -04:00
def _getMouseButtonState(self, button):
ok = self._mc.dll.get_mouse_button_state(self._cli, button)
if ok < 0:
pass #Raise exception
return ok == 1
def _setMouseButtonState(self, button, state):
ok = self._mc.dll.set_mouse_button_state(self._cli, c_int(button),
c_int(state), *map(lambda x: c_int(x), self._getMousePos()))
2010-04-01 19:56:26 -04:00
if ok != 0:
pass # Raise exception
return ok
def _initialiseDLLFuncs(self):
2010-06-08 13:16:58 -04:00
"""Define all mouse related DLL-calls"""
self._mc.dll.get_mouse_pos.restype = c_int
self._mc.dll.get_mouse_pos.argtypes = [c_ulong, PPOINT]
self._mc.dll.set_mouse_pos.restype = c_int
self._mc.dll.set_mouse_pos.argtypes = [c_ulong, PPOINT]
2010-04-01 19:56:26 -04:00
self._mc.dll.get_mouse_button_state.restype = c_int
self._mc.dll.get_mouse_button_state.argtypes = [c_ulong, c_int]
2010-04-01 19:56:26 -04:00
self._mc.dll.set_mouse_button_state.restype = c_int
self._mc.dll.set_mouse_button_state.argtypes = [c_ulong, c_int, c_int,
c_int, c_int]
2010-04-01 19:56:26 -04:00
pass