From 8f416c68e644e9347a9c9586583c4517f5ec25a8 Mon Sep 17 00:00:00 2001 From: Merlijn Wajer Date: Fri, 2 Apr 2010 01:48:06 +0200 Subject: [PATCH] Usable mouse class. Code is readable to the trained eye... I hope. --- Projects/MMLLib/libmml.lpr | 73 ++++++++++++++++++++----- Projects/MMLLib/pymml/mml.py | 14 ++++- Projects/MMLLib/pymml/mmlmouse.py | 89 ++++++++++++++++++++++++++----- Projects/MMLLib/pymml/mmltypes.py | 2 +- 4 files changed, 152 insertions(+), 26 deletions(-) diff --git a/Projects/MMLLib/libmml.lpr b/Projects/MMLLib/libmml.lpr index da42b68..4611afb 100644 --- a/Projects/MMLLib/libmml.lpr +++ b/Projects/MMLLib/libmml.lpr @@ -14,7 +14,10 @@ type Const RESULT_OK = 0; - RESULT_ERROR = 1; + RESULT_ERROR = -1; + + MOUSE_UP = 0; + MOUSE_DOWN = 1; var C: TClient; @@ -36,7 +39,7 @@ end; { Mouse } -function getmousepos(var t: tpoint): integer; cdecl; +function getMousePos(var t: tpoint): integer; cdecl; begin try @@ -50,17 +53,60 @@ begin end; end; -{function ConvIntClickType(Int : Integer) : TClickType;inline; -begin; - case int of - ps_mouse_right : result := mouse_Right; - ps_mouse_left : result := mouse_left; - ps_mouse_middle: result := mouse_middle; - else - raise exception.CreateFMT('Unknown Clicktype (%d) passed.',[int]); +function setMousePos(var t: tpoint): integer; cdecl; +begin + try + C.IOManager.SetMousePos(t.x,t.y); + result := RESULT_OK; + except on e : Exception do + begin + result := RESULT_ERROR; + last_error := PChar(e.Message); end; -end; } + end; +end; +function ConvIntClickType(Int : Integer) : TClickType;inline; +begin + case int of + 0 : result := mouse_Left; + 1 : result := mouse_Right; + 2: result := mouse_Middle; + end; +end; + +function getMouseButtonState(But: Integer): Integer; +begin + try + if C.IOManager.IsMouseButtonDown(ConvIntClickType(But)) then + result := MOUSE_DOWN; + except on e : Exception do + begin + result := RESULT_ERROR; + last_error := PChar(e.Message); + end; + end; +end; + +function setMouseButtonState(But, State, X, Y: Integer): Integer; +begin + try + if State = MOUSE_UP then + begin + C.IOManager.ReleaseMouse(X, Y, ConvIntClickType(But)); + result := RESULT_OK; + end else if state = MOUSE_DOWN then + begin + C.IOManager.HoldMouse(X, Y, ConvIntClickType(But)); + result := RESULT_OK; + end; + except on e : Exception do + begin + result := RESULT_ERROR; + last_error := PChar(e.Message); + end; + end; +end; function returnpoints: PTPoint; cdecl; @@ -170,7 +216,10 @@ end; exports test, init, - getmousepos, + getMousePos, + setMousePos, + getMouseButtonState, + setMouseButtonState, returnpoints, printpoints, hoi, diff --git a/Projects/MMLLib/pymml/mml.py b/Projects/MMLLib/pymml/mml.py index 22de916..99f1327 100755 --- a/Projects/MMLLib/pymml/mml.py +++ b/Projects/MMLLib/pymml/mml.py @@ -24,7 +24,19 @@ class MMLCore(object): DLL = MMLCore('../libmml.so') m = Mouse(DLL) -print m._getMousePos() + + + +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) +#print m._getMousePos() + +# Reset all buttons.. +m[(Mouse.Left, Mouse.Right, Mouse.Middle)] = [False for x in range(3)] del DLL diff --git a/Projects/MMLLib/pymml/mmlmouse.py b/Projects/MMLLib/pymml/mmlmouse.py index cb3c3dd..d8301b3 100644 --- a/Projects/MMLLib/pymml/mmlmouse.py +++ b/Projects/MMLLib/pymml/mmlmouse.py @@ -1,11 +1,19 @@ from ctypes import * -from mml import -from mmltypes import POINT +from mmltypes import POINT, PPOINT +from mmltypes import isiterable + +class MouseException(Exception): + def __init__(self, err): + Exception.__init__(self, err) # Usage: class Mouse(object): # _mc = MMLCore reference. _mc = None + Left ='Left' + Right = 'Right' + Middle = 'Middle' + Pos = 'Pos' # last pointer position _lpp = (0, 0) @@ -17,28 +25,85 @@ class Mouse(object): pass def _initialiseDLLFuncs(self): - self._mc.dll.getmousepos.restype = c_int - self._mc.dll.getmousepos.argtypes = [PPOINT] + self._mc.dll.getMousePos.restype = c_int + self._mc.dll.getMousePos.argtypes = [PPOINT] + + self._mc.dll.setMousePos.restype = c_int + self._mc.dll.setMousePos.argtypes = [PPOINT] + + self._mc.dll.getMouseButtonState.restype = c_int + self._mc.dll.getMouseButtonState.argtypes = [c_int] + + self._mc.dll.setMouseButtonState.restype = c_int + self._mc.dll.setMouseButtonState.argtypes = [c_int, c_int, c_int, c_int] pass - # Will be used to get the states of the mouse def __getitem__(self, item): - pass + if item == self.Pos: + return self._getMousePos() + if isiterable(item): + for i in item: + if i not in self._getButtons().keys(): + raise MouseException('Invalid mouse button') + + bs = [self._buttonToInt(x) for x in item] + return [self._getMouseButtonState(x) for x in bs] + + raise MouseException('item is not iterable, nor a (valid) string') - # Will be used to set states of the mouse def __setitem__(self, item, value): - pass + ak = self._getButtons().keys() + [self.Pos] + 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 isiterable(item) and isiterable(value): + 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) + if i in (self.Left, self.Right, self.Middle): + self._setMouseButtonState(self._buttonToInt(i), \ + 1 if v else 0) + return + + + raise MouseException('FIXME') + + # internal functions + + def _getButtons(self): + return {self.Left : 0, self.Right : 1, self.Middle : 2} + + def _buttonToInt(self, button): + return self._getButtons()[button] - # internal function def _getMousePos(self): ret = POINT() - ok = self._mc.dll.getmousepos(byref(ret)) + ok = self._mc.dll.getMousePos(byref(ret)) # 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) - # internal function + def _setMousePos(self, p): + ret = POINT() + ret.x, ret.y = p + ok = self._mc.dll.setMousePos(byref(ret)) + return ok + def _getMouseButtonState(self, button): - pass + ok = self._mc.dll.getMouseButtonState(button) + if ok < 0: + pass #Raise exception + return ok == 1 + + def _setMouseButtonState(self, button, state): + return self._mc.dll.setMouseButtonState(c_int(button), c_int(state), *map(lambda x: c_int(x), self._getMousePos())) + + diff --git a/Projects/MMLLib/pymml/mmltypes.py b/Projects/MMLLib/pymml/mmltypes.py index c5cef85..3d4abf6 100644 --- a/Projects/MMLLib/pymml/mmltypes.py +++ b/Projects/MMLLib/pymml/mmltypes.py @@ -6,4 +6,4 @@ class POINT(Structure): PPOINT = POINTER(POINT) - +isiterable = lambda x: hasattr(x, '__iter__')