cursors.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. ## pygame - Python Game Library
  2. ## Copyright (C) 2000-2003 Pete Shinners
  3. ##
  4. ## This library is free software; you can redistribute it and/or
  5. ## modify it under the terms of the GNU Library General Public
  6. ## License as published by the Free Software Foundation; either
  7. ## version 2 of the License, or (at your option) any later version.
  8. ##
  9. ## This library is distributed in the hope that it will be useful,
  10. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ## Library General Public License for more details.
  13. ##
  14. ## You should have received a copy of the GNU Library General Public
  15. ## License along with this library; if not, write to the Free
  16. ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. ##
  18. ## Pete Shinners
  19. ## pete@shinners.org
  20. """Set of cursor resources available for use. These cursors come
  21. in a sequence of values that are needed as the arguments for
  22. pygame.mouse.set_cursor(). to dereference the sequence in place
  23. and create the cursor in one step, call like this;
  24. pygame.mouse.set_cursor(*pygame.cursors.arrow).
  25. Here is a list of available cursors; arrow, diamond, ball,
  26. broken_x, tri_left, tri_right
  27. There is also a sample string cursor named 'thickarrow_strings'.
  28. The compile() function can convert these string cursors into cursor byte data.
  29. """
  30. #default pygame black arrow
  31. arrow = ((16, 16), (0, 0),
  32. (0x00,0x00,0x40,0x00,0x60,0x00,0x70,0x00,0x78,0x00,0x7C,0x00,0x7E,0x00,0x7F,0x00,
  33. 0x7F,0x80,0x7C,0x00,0x6C,0x00,0x46,0x00,0x06,0x00,0x03,0x00,0x03,0x00,0x00,0x00),
  34. (0x40,0x00,0xE0,0x00,0xF0,0x00,0xF8,0x00,0xFC,0x00,0xFE,0x00,0xFF,0x00,0xFF,0x80,
  35. 0xFF,0xC0,0xFF,0x80,0xFE,0x00,0xEF,0x00,0x4F,0x00,0x07,0x80,0x07,0x80,0x03,0x00))
  36. diamond = ((16, 16), (7, 7),
  37. (0, 0, 1, 0, 3, 128, 7, 192, 14, 224, 28, 112, 56, 56, 112, 28, 56,
  38. 56, 28, 112, 14, 224, 7, 192, 3, 128, 1, 0, 0, 0, 0, 0),
  39. (1, 0, 3, 128, 7, 192, 15, 224, 31, 240, 62, 248, 124, 124, 248, 62,
  40. 124, 124, 62, 248, 31, 240, 15, 224, 7, 192, 3, 128, 1, 0, 0, 0))
  41. ball = ((16, 16), (7, 7),
  42. (0, 0, 3, 192, 15, 240, 24, 248, 51, 252, 55, 252, 127, 254, 127, 254,
  43. 127, 254, 127, 254, 63, 252, 63, 252, 31, 248, 15, 240, 3, 192, 0, 0),
  44. (3, 192, 15, 240, 31, 248, 63, 252, 127, 254, 127, 254, 255, 255, 255,
  45. 255, 255, 255, 255, 255, 127, 254, 127, 254, 63, 252, 31, 248, 15, 240,
  46. 3, 192))
  47. broken_x = ((16, 16), (7, 7),
  48. (0, 0, 96, 6, 112, 14, 56, 28, 28, 56, 12, 48, 0, 0, 0, 0, 0, 0, 0, 0,
  49. 12, 48, 28, 56, 56, 28, 112, 14, 96, 6, 0, 0),
  50. (224, 7, 240, 15, 248, 31, 124, 62, 62, 124, 30, 120, 14, 112, 0, 0, 0,
  51. 0, 14, 112, 30, 120, 62, 124, 124, 62, 248, 31, 240, 15, 224, 7))
  52. tri_left = ((16, 16), (1, 1),
  53. (0, 0, 96, 0, 120, 0, 62, 0, 63, 128, 31, 224, 31, 248, 15, 254, 15, 254,
  54. 7, 128, 7, 128, 3, 128, 3, 128, 1, 128, 1, 128, 0, 0),
  55. (224, 0, 248, 0, 254, 0, 127, 128, 127, 224, 63, 248, 63, 254, 31, 255,
  56. 31, 255, 15, 254, 15, 192, 7, 192, 7, 192, 3, 192, 3, 192, 1, 128))
  57. tri_right = ((16, 16), (14, 1),
  58. (0, 0, 0, 6, 0, 30, 0, 124, 1, 252, 7, 248, 31, 248, 127, 240, 127, 240,
  59. 1, 224, 1, 224, 1, 192, 1, 192, 1, 128, 1, 128, 0, 0),
  60. (0, 7, 0, 31, 0, 127, 1, 254, 7, 254, 31, 252, 127, 252, 255, 248, 255,
  61. 248, 127, 240, 3, 240, 3, 224, 3, 224, 3, 192, 3, 192, 1, 128))
  62. #here is an example string resource cursor. to use this;
  63. # curs, mask = pygame.cursors.compile_cursor(pygame.cursors.thickarrow_strings, 'X', '.')
  64. # pygame.mouse.set_cursor((24, 24), (0, 0), curs, mask)
  65. thickarrow_strings = ( #sized 24x24
  66. "XX ",
  67. "XXX ",
  68. "XXXX ",
  69. "XX.XX ",
  70. "XX..XX ",
  71. "XX...XX ",
  72. "XX....XX ",
  73. "XX.....XX ",
  74. "XX......XX ",
  75. "XX.......XX ",
  76. "XX........XX ",
  77. "XX........XXX ",
  78. "XX......XXXXX ",
  79. "XX.XXX..XX ",
  80. "XXXX XX..XX ",
  81. "XX XX..XX ",
  82. " XX..XX ",
  83. " XX..XX ",
  84. " XX..XX ",
  85. " XXXX ",
  86. " XX ",
  87. " ",
  88. " ",
  89. " ",
  90. )
  91. sizer_x_strings = ( #sized 24x16
  92. " X X ",
  93. " XX XX ",
  94. " X.X X.X ",
  95. " X..X X..X ",
  96. " X...XXXXXXXX...X ",
  97. "X................X ",
  98. " X...XXXXXXXX...X ",
  99. " X..X X..X ",
  100. " X.X X.X ",
  101. " XX XX ",
  102. " X X ",
  103. " ",
  104. " ",
  105. " ",
  106. " ",
  107. " ",
  108. )
  109. sizer_y_strings = ( #sized 16x24
  110. " X ",
  111. " X.X ",
  112. " X...X ",
  113. " X.....X ",
  114. " X.......X ",
  115. "XXXXX.XXXXX ",
  116. " X.X ",
  117. " X.X ",
  118. " X.X ",
  119. " X.X ",
  120. " X.X ",
  121. " X.X ",
  122. " X.X ",
  123. "XXXXX.XXXXX ",
  124. " X.......X ",
  125. " X.....X ",
  126. " X...X ",
  127. " X.X ",
  128. " X ",
  129. " ",
  130. " ",
  131. " ",
  132. " ",
  133. " ",
  134. )
  135. sizer_xy_strings = ( #sized 24x16
  136. "XXXXXXXX ",
  137. "X.....X ",
  138. "X....X ",
  139. "X...X ",
  140. "X..X.X ",
  141. "X.X X.X ",
  142. "XX X.X X ",
  143. "X X.X XX ",
  144. " X.XX.X ",
  145. " X...X ",
  146. " X...X ",
  147. " X....X ",
  148. " X.....X ",
  149. " XXXXXXXX ",
  150. " ",
  151. " ",
  152. )
  153. textmarker_strings = ( #sized 8x16
  154. "ooo ooo ",
  155. " o ",
  156. " o ",
  157. " o ",
  158. " o ",
  159. " o ",
  160. " o ",
  161. " o ",
  162. " o ",
  163. " o ",
  164. " o ",
  165. "ooo ooo ",
  166. " ",
  167. " ",
  168. " ",
  169. " ",
  170. )
  171. def compile(strings, black='X', white='.',xor='o'):
  172. """pygame.cursors.compile(strings, black, white,xor) -> data, mask
  173. compile cursor strings into cursor data
  174. This takes a set of strings with equal length and computes
  175. the binary data for that cursor. The string widths must be
  176. divisible by 8.
  177. The black and white arguments are single letter strings that
  178. tells which characters will represent black pixels, and which
  179. characters represent white pixels. All other characters are
  180. considered clear.
  181. This returns a tuple containing the cursor data and cursor mask
  182. data. Both these arguments are used when setting a cursor with
  183. pygame.mouse.set_cursor().
  184. """
  185. #first check for consistent lengths
  186. size = len(strings[0]), len(strings)
  187. if size[0] % 8 or size[1] % 8:
  188. raise ValueError("cursor string sizes must be divisible by 8 %s" %
  189. size)
  190. for s in strings[1:]:
  191. if len(s) != size[0]:
  192. raise ValueError("Cursor strings are inconsistent lengths")
  193. #create the data arrays.
  194. #this could stand a little optimizing
  195. maskdata = []
  196. filldata = []
  197. maskitem = fillitem = 0
  198. step = 8
  199. for s in strings:
  200. for c in s:
  201. maskitem = maskitem << 1
  202. fillitem = fillitem << 1
  203. step = step - 1
  204. if c == black:
  205. maskitem = maskitem | 1
  206. fillitem = fillitem | 1
  207. elif c == white:
  208. maskitem = maskitem | 1
  209. elif c == xor:
  210. fillitem = fillitem | 1
  211. if not step:
  212. maskdata.append(maskitem)
  213. filldata.append(fillitem)
  214. maskitem = fillitem = 0
  215. step = 8
  216. return tuple(filldata), tuple(maskdata)
  217. def load_xbm(curs, mask):
  218. """pygame.cursors.load_xbm(cursorfile, maskfile) -> cursor_args
  219. reads a pair of XBM files into set_cursor arguments
  220. Arguments can either be filenames or filelike objects
  221. with the readlines method. Not largely tested, but
  222. should work with typical XBM files.
  223. """
  224. def bitswap(num):
  225. val = 0
  226. for x in range(8):
  227. b = num&(1<<x) != 0
  228. val = val<<1 | b
  229. return val
  230. if type(curs) is type(''):
  231. with open(curs) as cursor_f:
  232. curs = cursor_f.readlines()
  233. else:
  234. curs = curs.readlines()
  235. if type(mask) is type(''):
  236. with open(mask) as mask_f:
  237. mask = mask_f.readlines()
  238. else:
  239. mask = mask.readlines()
  240. #avoid comments
  241. for line in range(len(curs)):
  242. if curs[line].startswith("#define"):
  243. curs = curs[line:]
  244. break
  245. for line in range(len(mask)):
  246. if mask[line].startswith("#define"):
  247. mask = mask[line:]
  248. break
  249. #load width,height
  250. width = int(curs[0].split()[-1])
  251. height = int(curs[1].split()[-1])
  252. #load hotspot position
  253. if curs[2].startswith('#define'):
  254. hotx = int(curs[2].split()[-1])
  255. hoty = int(curs[3].split()[-1])
  256. else:
  257. hotx = hoty = 0
  258. info = width, height, hotx, hoty
  259. for line in range(len(curs)):
  260. if curs[line].startswith('static char') or curs[line].startswith('static unsigned char'):
  261. break
  262. data = ' '.join(curs[line+1:]).replace('};', '').replace(',', ' ')
  263. cursdata = []
  264. for x in data.split():
  265. cursdata.append(bitswap(int(x, 16)))
  266. cursdata = tuple(cursdata)
  267. for line in range(len(mask)):
  268. if mask[line].startswith('static char') or mask[line].startswith('static unsigned char'):
  269. break
  270. data = ' '.join(mask[line+1:]).replace('};', '').replace(',', ' ')
  271. maskdata = []
  272. for x in data.split():
  273. maskdata.append(bitswap(int(x, 16)))
  274. maskdata = tuple(maskdata)
  275. return info[:2], info[2:], cursdata, maskdata