Skip to content

Commit

Permalink
Added custom cursor compilation examples. Need docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mekire committed Dec 17, 2013
1 parent 6b7649e commit 9633649
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
25 changes: 25 additions & 0 deletions cursors/cursor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pygame as pg


def cursor_from_image(image,size,hotspot,location=(0,0)):
"""This functions return value is of the form accepted by
pg.mouse.set_cursor() (passed using the *args syntax). The argument image
is an already loaded image surface containing your desired cursor; size is
a single integer corresponding to the width of the cursor (must be a
multiple of 8); hotspot is a 2-tuple representing the exact point in your
cursor that will represent the mouse pointer position; location is the
where your cursor is located on the passed in image."""
if size%8:
raise ValueError("Size must be a multiple of 8.")
colors = {(0,0,0,255):".", (255,255,255,255):"X"}
cursor_string = []
for j in range(size):
this_row = []
for i in range(size):
where = (i+location[0],j+location[1])
pixel = tuple(image.get_at(where))
this_row.append(colors.get(pixel," "))
cursor_string.append("".join(this_row))
xors,ands = pg.cursors.compile(cursor_string)
size = size,size
return size,hotspot,xors,ands
Binary file added cursors/cursor_images.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 76 additions & 0 deletions cursors/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import os
import sys
import pygame as pg

from cursor import cursor_from_image


SCREEN_SIZE = (500,500)
BACKGROUND_COLOR = (50,50,60)

CURSOR_TYPES = {"1" : "default",
"2" : "cross",
"3" : "dropper",
"4" : "knight",
"5" : "open"}


class Control(object):
def __init__(self):
pg.init()
os.environ["SDL_VIDEO_CENTERED"] = "TRUE"
self.screen = pg.display.set_mode(SCREEN_SIZE)
self.screen_rect = self.screen.get_rect()
self.done = False
self.keys = pg.key.get_pressed()
self.clock = pg.time.Clock()
self.fps = 60.0
self.cursor_dict = self.make_cursor_dict()
self.cursor = self.change_cursor("open")

def make_cursor_dict(self):
cursor_image = pg.image.load("cursor_images.png").convert()
cursors = {"cross" : cursor_from_image(cursor_image,16,(8,8)),
"dropper" : cursor_from_image(cursor_image,16,(0,15),(16,0)),
"open" : cursor_from_image(cursor_image,24,(12,12),(32,0)),
"closed" : cursor_from_image(cursor_image,24,(12,12),(32,24)),
"knight" : cursor_from_image(cursor_image,32,(16,16),(0,16)),
"default" : pg.mouse.get_cursor()}
return cursors

def change_cursor(self,cursor_name):
pg.mouse.set_cursor(*self.cursor_dict[cursor_name])
return cursor_name

def event_loop(self):
for event in pg.event.get():
self.keys = pg.key.get_pressed()
if event.type == pg.QUIT or self.keys[pg.K_ESCAPE]:
self.done = True
elif event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
if self.cursor == "open":
self.cursor = self.change_cursor("closed")
elif event.type == pg.MOUSEBUTTONUP and event.button == 1:
if self.cursor == "closed":
self.cursor = self.change_cursor("open")
elif event.type == pg.KEYDOWN:
key = event.unicode
if key in CURSOR_TYPES:
self.cursor = self.change_cursor(CURSOR_TYPES[key])

def main_loop(self):
while not self.done:
self.event_loop()
self.screen.fill(BACKGROUND_COLOR)
pg.display.update()
self.clock.tick(self.fps)


def main():
Control().main_loop()
pg.quit()
sys.exit()


if __name__ == "__main__":
main()

0 comments on commit 9633649

Please sign in to comment.