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

[pygame] Compile cursor



I have added some sample cursors and made the compile routine able to accept xor'd
bits. I also put defaults into the compile routine.

Hope it may be of use to some one.

Is there som other place to submit code or is this the only one ?
Bo)




#here is an example string resource cursor. to use this;
# curs, mask = pygame.cursors.compile(pygame.cursors.thickarrow_strings, 'X', '.','o')
# pygame.mouse.set_cursor((24, 24), (0, 0), curs, mask)

thickarrow_strings = ( #sized 24x24
"XX ",
"XXX ",
"XXXX ",
"XX.XX ",
"XX..XX ",
"XX...XX ",
"XX....XX ",
"XX.....XX ",
"XX......XX ",
"XX.......XX ",
"XX........XX ",
"XX........XXX ",
"XX......XXXXX ",
"XX.XXX..XX ",
"XXXX XX..XX ",
"XX XX..XX ",
" XX..XX ",
" XX..XX ",
" XX..XX ",
" XXXX ",
" XX ",
" ",
" ",
" ",
)

sizer_x = ( #sized 24x16
" X X ",
" XX XX ",
" X.X X.X ",
" X..X X..X ",
" X...XXXXXXXX...X ",
"X................X ",
" X...XXXXXXXX...X ",
" X..X X..X ",
" X.X X.X ",
" XX XX ",
" X X ",
" ",
" ",
" ",
" ",
" ",
)
sizer_y = ( #sized 16x24
" X ",
" X.X ",
" X...X ",
" X.....X ",
" X.......X ",
"XXXXX.XXXXX ",
" X.X ",
" X.X ",
" X.X ",
" X.X ",
" X.X ",
" X.X ",
" X.X ",
"XXXXX.XXXXX ",
" X.......X ",
" X.....X ",
" X...X ",
" X.X ",
" X ",
" ",
" ",
" ",
" ",
" ",
)
sizer_xy = ( #sized 24x16
"XXXXXXXX ",
"X.....X ",
"X....X ",
"X...X ",
"X..X.X ",
"X.X X.X ",
"XX X.X X ",
"X X.X XX ",
" X.XX.X ",
" X...X ",
" X...X ",
" X....X ",
" X.....X ",
" XXXXXXXX ",
" ",
" ",
)
textmarker = ( #sized 8x16
"ooo ooo ",
" o ",
" o ",
" o ",
" o ",
" o ",
" o ",
" o ",
" o ",
" o ",
" o ",
"ooo ooo ",
" ",
" ",
" ",
" ",
)

def compile(strings, black='X', white='.',xor='o'):
"""pygame.cursor.compile(strings, black, white,xor) -> data, mask
compile cursor strings into cursor data

This takes a set of strings with equal length and computes
the binary data for that cursor. The string widths must be
divisible by 8.

The black and white arguments are single letter strings that
tells which characters will represent black pixels, and which
characters represent white pixels. All other characters are
considered clear.

This returns a tuple containing the cursor data and cursor mask
data. Both these arguments are used when setting a cursor with
pygame.mouse.set_cursor().
"""

#first check for consistent lengths
size = len(strings[0]), len(strings)
if size[0] % 8 or size[1] % 8:
raise ValueError, "cursor string sizes must be divisible by 8 "+`size`
for s in strings[1:]:
if len(s) != size[0]:
raise ValueError, "Cursor strings are inconsistent lengths"

#create the data arrays.
#this could stand a little optimizing
maskdata = []
filldata = []
maskitem = fillitem = 0
step = 8
for s in strings:
for c in s:
maskitem = maskitem << 1
fillitem = fillitem << 1
step = step - 1
if c == black:
maskitem = maskitem | 1
elif c == white:
maskitem = maskitem | 1
fillitem = fillitem | 1
elif c == xor:
fillitem = fillitem | 1
if not step:
maskdata.append(maskitem)
filldata.append(fillitem)
maskitem = fillitem = 0
step = 8
return tuple(filldata), tuple(maskdata)