1
0
mirror of https://github.com/moparisthebest/Simba synced 2024-12-23 15:58:51 -05:00

PyMML changes, doc additions, fixes.

This commit is contained in:
Merlijn Wajer 2010-06-08 19:16:58 +02:00
parent 8fa17d8189
commit 2134e9a031
4 changed files with 86 additions and 81 deletions

View File

@ -2,12 +2,15 @@ from ctypes import *
from mmltypes import isiterable
from mmltypes import POINT, PPOINT, PINTEGER
from mmltypes import PascalArray
from mmltypes import RESULT_OK, RESULT_FALSE, RESULT_ERROR
class ColorException(Exception):
def __init__(self, err):
Exception.__init__(self, err)
# FIXME: Complete...
class Color(object):
'''
@ -17,35 +20,57 @@ class Color(object):
_mc = None
def __init__(self, MC):
"""
Initialise the Color object.
"""
self._mc = MC
self._initialiseDLLFuncs()
def find(self, box, color, tol = 0):
"""
find a color in a box, with a specific tolerance.
returns a tuple of the x, y value of a matched color.
None if no color was found.
"""
x, y = (c_int(-1), c_int(-1))
if tol is 0:
ret = self._mc.dll.findColor(byref(x), byref(y), color, *box)
else:
ret = self._mc.dll.findColorTolerance(byref(x), byref(y), color,
tol, *box)
return (x, y)
if ret is RESULT_OK then:
return (x, y)
return None
def findAll(self, box, color, tol = 0):
"""
find all colors in a box, with a specific tolerance.
returned are all the matching points/
"""
ptr = PPOINT()
if tol is 0:
self._mc.dll.findColors(byref(ptr), color, *box)
else:
self._mc.dll.findColors(byref(ptr), color, tol, *box)
self._mc.dll.findColorsTolerance(byref(ptr), color, tol, *box)
arr = PascalArray(POINT, ptr, self._mc)
print 'Length:', len(arr)
# print 'Length:', len(arr)
# for i in range(len(arr)):
# print i, arr[i].x, arr[i].y
# FIXME return python list?
return arr
def _initialiseDLLFuncs(self):
self._mc.dll.findColor.restype = c_int
self._mc.dll.findColor.argtypes = [PINTEGER, PINTEGER, c_int, c_int,
c_int, c_int, c_int]
self._mc.dll.findColorTolerance.restype = c_int
self._mc.dll.findColorTolerance.argtypes = [PINTEGER, PINTEGER, c_int, c_int,
c_int, c_int, c_int, c_int]
self._mc.dll.findColors.restype = c_int
self._mc.dll.findColors.argtypes = [POINTER(PPOINT), c_int, c_int,
c_int, c_int, c_int]
self._mc.dll.findColorsTolerance.restype = c_int
self._mc.dll.findColorsTolerance.argtypes = [POINTER(PPOINT), c_int, c_int,
c_int, c_int, c_int, c_int]

View File

@ -8,13 +8,13 @@ class MouseException(Exception):
# Usage:
class Mouse(object):
'''
"""
The MML Mouse object communicates directly with libmml,
but wraps it around a nice and easy to use layer.
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 [].
'''
"""
# _mc = MMLCore reference.
_mc = None
Left, Right, Middle, Pos = 'Left', 'Right', 'Middle', 'Pos'
@ -23,28 +23,40 @@ class Mouse(object):
_lpp = (0, 0)
def __init__(self, MC):
''' Initialize the Mouse object. Needs a DLL Mufasa Core object
(which contains the dll reference.)'''
""" Initialize the Mouse object. Needs a DLL Mufasa Core object
(which contains the dll reference.)"""
self._mc = MC
self._initialiseDLLFuncs()
pass
def setPos(self, pos):
"""
Set the mouse position to the tuple _pos_.
"""
return self.__setitem__(Mouse.Pos, pos)
def getPos(self):
"""
Get the current mouse position as a tuple.
"""
return self._getMousePos()
def getButtonStates(self):
"""
Get the current states of the mouse buttons.
"""
return zip(self._getButtons().keys(), \
self.__getitem__(self._getButtons().keys()))
def setButtonState(self, button, downup):
"""
Set the states of the mouse buttons.
"""
return self.__setitem__(button, downup)
def __getitem__(self, item):
'''Can currently return the state of mouse buttons as well as the
mouse position. Supports iterable arguments'''
"""Can currently return the state of mouse buttons as well as the
mouse position. Supports iterable arguments"""
if isiterable(item):
res = []
for i in item:
@ -65,8 +77,8 @@ class Mouse(object):
raise MouseException('item is not iterable nor a (valid) string')
def __setitem__(self, item, value):
'''Can currently set the state of mouse buttons as well as the
mouse position. Supports iterable arguments'''
"""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):
@ -101,11 +113,11 @@ class Mouse(object):
# Tools
def _getButtons(self):
'''Return mouse buttons with their corresponding button DLL number as dict'''
"""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):
'''Return button number for button'''
"""Return button number for button"""
return self._getButtons()[button]
@ -142,7 +154,7 @@ class Mouse(object):
return ok
def _initialiseDLLFuncs(self):
'''Define all mouse related DLL-calls'''
"""Define all mouse related DLL-calls"""
self._mc.dll.getMousePos.restype = c_int
self._mc.dll.getMousePos.argtypes = [PPOINT]

View File

@ -1,32 +1,65 @@
from ctypes import *
RESULT_OK, RESULT_FALSE, RESULT_ERROR = (0, 1, -1)
class POINT(Structure):
'''
"""
POINT represents a point with two variables, x and y.
POINT is mainly used for Python <-> MML communication.
'''
"""
_fields_ = [('x', c_int),
('y', c_int)]
class PascalArray(object):
"""
PascalArray is a class that allows one to easily use a Pascal-style
array. It has been changed to fit my own Pascal-style arrays. (The
length is no longer stored at -1, but at 0, and the data starts at 1.)
This makes freeing the data much easier.
The implementation is limited to reading and writing data.
It cannot resize arrays nor can it create them.
This class is more like a temporary solution to passing arrays and such.
The actual user should not be bothered by the external memory, so most
likely we will simply turn this data into python lists. The only
drawback would be the overhead created by doing so.
"""
def __init__(self, pastype, ptr, MC):
"""
Set the type of the data we are holding to _pastype_,
save the pointer _ptr_ and store the reference to the MMLCore.
"""
self._type = pastype
self._p = ptr
self._mc = MC
def __del__(self):
"""
Free the array. Perhaps we should do reference counting on the
pointer?
"""
self._mc.dll.fpc_freemem_(self._p)
def __len__(self):
"""
Return the length of the array.
"""
return cast(self._p, POINTER(c_ulong))[0]
def __getitem__(self, pos):
"""
Get an item at a specific position _pos_.
"""
if pos > len(self):
print 'Out of range'
return None
return cast(self._p, POINTER(self._type))[pos+1]
def __setitem__(self, pos, item):
"""
Set an item at a specific position _pos_.
"""
if pos > len(self):
print 'Out of range'
return

View File

@ -1,65 +0,0 @@
#!/usr/bin/env python
from ctypes import *
import platform
if platform.system() == 'Windows':
dll = CDLL('./libmml.dll')
print 'On Windows'
else:
dll = CDLL('./libmml.so')
dll.test.restype = c_char_p
a = dll.test()
print a
dll.init.restype = None
dll.init()
class POINT(Structure):
_fields_ = [('x', c_int),
('y', c_int)]
dll.getmousepos.restype = POINT
dll.getmousepos.argtypes = None
b = dll.getmousepos()
print b.x, b.y
PPOINT = POINTER(POINT)
dll.returnpoints.restype = PPOINT
c = dll.returnpoints()
print c[0].x
dll.printpoints.restype = c_int
dll.printpoints.argtypes = [PPOINT, c_int]
d = dll.printpoints(c, 2)
dll.hoi.restype = None
dll.hoi.argtypes = [POINTER(c_int)]
e = c_int(5)
dll.hoi(byref(e))
print e
class DTM(Structure):
_fields_ = [('l' , c_int),
('p' , PPOINT),
('c' , POINTER(c_int)),
('t' , POINTER(c_int)),
('asz' , POINTER(c_int)),
('ash' , POINTER(c_int)),
('bp' , POINTER(c_int)),
('n' , c_char_p)]
PDTM = POINTER(DTM)
dll.givedtm.restype = PDTM
dll.givedtm.argtypes = None
f = dll.givedtm()
print f
print f.contents.l