Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/pygame/cursors.py
blob: 80e20df0ceaa2579f6b95c9d3eb9ab102886c366 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
##    pygame - Python Game Library
##    Copyright (C) 2000-2003  Pete Shinners
##
##    This library is free software; you can redistribute it and/or
##    modify it under the terms of the GNU Library General Public
##    License as published by the Free Software Foundation; either
##    version 2 of the License, or (at your option) any later version.
##
##    This library is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
##    Library General Public License for more details.
##
##    You should have received a copy of the GNU Library General Public
##    License along with this library; if not, write to the Free
##    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
##
##    Pete Shinners
##    pete@shinners.org

"""Set of cursor resources available for use. These cursors come
in a sequence of values that are needed as the arguments for
pygame.mouse.set_cursor(). to dereference the sequence in place
and create the cursor in one step, call like this;
pygame.mouse.set_cursor(*pygame.cursors.arrow).

Here is a list of available cursors; arrow, diamond, ball,
        broken_x, tri_left, tri_right

There is also a sample string cursor named 'thickarrow_strings'.
The compile() function can convert these string cursors into cursor byte data.
"""

#default pygame black arrow
arrow = ((16, 16), (0, 0),
    (0x00,0x00,0x40,0x00,0x60,0x00,0x70,0x00,0x78,0x00,0x7C,0x00,0x7E,0x00,0x7F,0x00,
     0x7F,0x80,0x7C,0x00,0x6C,0x00,0x46,0x00,0x06,0x00,0x03,0x00,0x03,0x00,0x00,0x00),
    (0x40,0x00,0xE0,0x00,0xF0,0x00,0xF8,0x00,0xFC,0x00,0xFE,0x00,0xFF,0x00,0xFF,0x80,
     0xFF,0xC0,0xFF,0x80,0xFE,0x00,0xEF,0x00,0x4F,0x00,0x07,0x80,0x07,0x80,0x03,0x00))

diamond = ((16, 16), (7, 7),
    (0, 0, 1, 0, 3, 128, 7, 192, 14, 224, 28, 112, 56, 56, 112, 28, 56,
     56, 28, 112, 14, 224, 7, 192, 3, 128, 1, 0, 0, 0, 0, 0),
    (1, 0, 3, 128, 7, 192, 15, 224, 31, 240, 62, 248, 124, 124, 248, 62,
     124, 124, 62, 248, 31, 240, 15, 224, 7, 192, 3, 128, 1, 0, 0, 0))

ball = ((16, 16), (7, 7),
    (0, 0, 3, 192, 15, 240, 24, 248, 51, 252, 55, 252, 127, 254, 127, 254,
     127, 254, 127, 254, 63, 252, 63, 252, 31, 248, 15, 240, 3, 192, 0, 0),
    (3, 192, 15, 240, 31, 248, 63, 252, 127, 254, 127, 254, 255, 255, 255,
     255, 255, 255, 255, 255, 127, 254, 127, 254, 63, 252, 31, 248, 15, 240,
     3, 192))

broken_x = ((16, 16), (7, 7),
    (0, 0, 96, 6, 112, 14, 56, 28, 28, 56, 12, 48, 0, 0, 0, 0, 0, 0, 0, 0,
     12, 48, 28, 56, 56, 28, 112, 14, 96, 6, 0, 0),
    (224, 7, 240, 15, 248, 31, 124, 62, 62, 124, 30, 120, 14, 112, 0, 0, 0,
     0, 14, 112, 30, 120, 62, 124, 124, 62, 248, 31, 240, 15, 224, 7))


tri_left = ((16, 16), (1, 1),
    (0, 0, 96, 0, 120, 0, 62, 0, 63, 128, 31, 224, 31, 248, 15, 254, 15, 254,
     7, 128, 7, 128, 3, 128, 3, 128, 1, 128, 1, 128, 0, 0),
    (224, 0, 248, 0, 254, 0, 127, 128, 127, 224, 63, 248, 63, 254, 31, 255,
     31, 255, 15, 254, 15, 192, 7, 192, 7, 192, 3, 192, 3, 192, 1, 128))

tri_right = ((16, 16), (14, 1),
    (0, 0, 0, 6, 0, 30, 0, 124, 1, 252, 7, 248, 31, 248, 127, 240, 127, 240,
     1, 224, 1, 224, 1, 192, 1, 192, 1, 128, 1, 128, 0, 0),
    (0, 7, 0, 31, 0, 127, 1, 254, 7, 254, 31, 252, 127, 252, 255, 248, 255,
     248, 127, 240, 3, 240, 3, 224, 3, 224, 3, 192, 3, 192, 1, 128))



#here is an example string resource cursor. to use this;
#  curs, mask = pygame.cursors.compile_cursor(pygame.cursors.thickarrow_strings, 'X', '.')
#  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_strings = (               #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_strings = (               #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_strings = (               #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_strings = (               #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.cursors.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 %s" %
                        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)




def load_xbm(curs, mask):
    """pygame.cursors.load_xbm(cursorfile, maskfile) -> cursor_args
reads a pair of XBM files into set_cursor arguments

Arguments can either be filenames or filelike objects
with the readlines method. Not largely tested, but
should work with typical XBM files.
"""
    def bitswap(num):
        val = 0
        for x in range(8):
            b = num&(1<<x) != 0
            val = val<<1 | b
        return val
    if type(curs) is type(''): curs = open(curs)
    if type(mask) is type(''): mask = open(mask)
    curs = curs.readlines()
    mask = mask.readlines()
    #avoid comments
    for line in range(len(curs)):
        if curs[line].startswith("#define"):
            curs = curs[line:]
            break
    for line in range(len(mask)):
        if mask[line].startswith("#define"):
            mask = mask[line:]
            break
    #load width,height
    width = int(curs[0].split()[-1])
    height = int(curs[1].split()[-1])
    #load hotspot position
    if curs[2].startswith('#define'):
        hotx = int(curs[2].split()[-1])
        hoty = int(curs[3].split()[-1])
    else:
        hotx = hoty = 0

    info = width, height, hotx, hoty

    for line in range(len(curs)):
        if curs[line].startswith('static char') or curs[line].startswith('static unsigned char'):
            break
    data = ' '.join(curs[line+1:]).replace('};', '').replace(',', ' ')
    cursdata = []
    for x in data.split():
        cursdata.append(bitswap(int(x, 16)))
    cursdata = tuple(cursdata)

    for line in range(len(mask)):
        if mask[line].startswith('static char') or mask[line].startswith('static unsigned char'):
            break
    data = ' '.join(mask[line+1:]).replace('};', '').replace(',', ' ')
    maskdata = []
    for x in data.split():
        maskdata.append(bitswap(int(x, 16)))
    maskdata = tuple(maskdata)
    return info[:2], info[2:], cursdata, maskdata