~ubuntu-branches/ubuntu/gutsy/pygame/gutsy

« back to all changes in this revision

Viewing changes to lib/cursors.py

  • Committer: Bazaar Package Importer
  • Author(s): Ed Boraas
  • Date: 2002-02-20 06:39:24 UTC
  • Revision ID: james.westby@ubuntu.com-20020220063924-amlzj7tqkeods4eq
Tags: upstream-1.4
ImportĀ upstreamĀ versionĀ 1.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##    pygame - Python Game Library
 
2
##    Copyright (C) 2000-2001  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
 
 
21
"""Set of cursor resources available for use. These cursors come
 
22
in a sequence of values that are needed as the arguments for
 
23
pygame.mouse.set_cursor(). to dereference the sequence in place
 
24
and create the cursor in one step, call like this;
 
25
pygame.mouse.set_cursor(*pygame.cursors.arrow).
 
26
 
 
27
Here is a list of available cursors; arrow, diamond, ball,
 
28
        broken_x, tri_left, tri_right
 
29
 
 
30
There is also a sample string cursor named 'thickarrow_strings'.
 
31
The compile() function can convert these string cursors into cursor byte data.
 
32
"""
 
33
 
 
34
#default pygame black arrow
 
35
arrow = ((16, 16), (0, 0),
 
36
    (0x00,0x00,0x40,0x00,0x60,0x00,0x70,0x00,0x78,0x00,0x7C,0x00,0x7E,0x00,0x7F,0x00,
 
37
     0x7F,0x80,0x7C,0x00,0x6C,0x00,0x46,0x00,0x06,0x00,0x03,0x00,0x03,0x00,0x00,0x00),
 
38
    (0x40,0x00,0xE0,0x00,0xF0,0x00,0xF8,0x00,0xFC,0x00,0xFE,0x00,0xFF,0x00,0xFF,0x80,
 
39
     0xFF,0xC0,0xFF,0x80,0xFE,0x00,0xEF,0x00,0x4F,0x00,0x07,0x80,0x07,0x80,0x03,0x00))
 
40
 
 
41
diamond = ((16, 16), (7, 7),
 
42
    (0, 0, 1, 0, 3, 128, 7, 192, 14, 224, 28, 112, 56, 56, 112, 28, 56,
 
43
     56, 28, 112, 14, 224, 7, 192, 3, 128, 1, 0, 0, 0, 0, 0),
 
44
    (1, 0, 3, 128, 7, 192, 15, 224, 31, 240, 62, 248, 124, 124, 248, 62,
 
45
     124, 124, 62, 248, 31, 240, 15, 224, 7, 192, 3, 128, 1, 0, 0, 0))
 
46
 
 
47
ball = ((16, 16), (7, 7),
 
48
    (0, 0, 3, 192, 15, 240, 24, 248, 51, 252, 55, 252, 127, 254, 127, 254,
 
49
     127, 254, 127, 254, 63, 252, 63, 252, 31, 248, 15, 240, 3, 192, 0, 0),
 
50
    (3, 192, 15, 240, 31, 248, 63, 252, 127, 254, 127, 254, 255, 255, 255,
 
51
     255, 255, 255, 255, 255, 127, 254, 127, 254, 63, 252, 31, 248, 15, 240,
 
52
     3, 192))
 
53
 
 
54
broken_x = ((16, 16), (7, 7),
 
55
    (0, 0, 96, 6, 112, 14, 56, 28, 28, 56, 12, 48, 0, 0, 0, 0, 0, 0, 0, 0,
 
56
     12, 48, 28, 56, 56, 28, 112, 14, 96, 6, 0, 0),
 
57
    (224, 7, 240, 15, 248, 31, 124, 62, 62, 124, 30, 120, 14, 112, 0, 0, 0,
 
58
     0, 14, 112, 30, 120, 62, 124, 124, 62, 248, 31, 240, 15, 224, 7))
 
59
 
 
60
 
 
61
tri_left = ((16, 16), (1, 1),
 
62
    (0, 0, 96, 0, 120, 0, 62, 0, 63, 128, 31, 224, 31, 248, 15, 254, 15, 254,
 
63
     7, 128, 7, 128, 3, 128, 3, 128, 1, 128, 1, 128, 0, 0),
 
64
    (224, 0, 248, 0, 254, 0, 127, 128, 127, 224, 63, 248, 63, 254, 31, 255,
 
65
     31, 255, 15, 254, 15, 192, 7, 192, 7, 192, 3, 192, 3, 192, 1, 128))
 
66
 
 
67
tri_right = ((16, 16), (14, 1),
 
68
    (0, 0, 0, 6, 0, 30, 0, 124, 1, 252, 7, 248, 31, 248, 127, 240, 127, 240,
 
69
     1, 224, 1, 224, 1, 192, 1, 192, 1, 128, 1, 128, 0, 0),
 
70
    (0, 7, 0, 31, 0, 127, 1, 254, 7, 254, 31, 252, 127, 252, 255, 248, 255,
 
71
     248, 127, 240, 3, 240, 3, 224, 3, 224, 3, 192, 3, 192, 1, 128))
 
72
 
 
73
 
 
74
 
 
75
#here is an example string resource cursor. to use this;
 
76
#  curs, mask = pygame.cursors.compile_cursor(pygame.cursors.thickarrow_strings, 'X', '.')
 
77
#  pygame.mouse.set_cursor((24, 24), (0, 0), curs, mask)
 
78
 
 
79
thickarrow_strings = (               #sized 24x24
 
80
  "XX                      ",
 
81
  "XXX                     ",
 
82
  "XXXX                    ",
 
83
  "XX.XX                   ",
 
84
  "XX..XX                  ",
 
85
  "XX...XX                 ",
 
86
  "XX....XX                ",
 
87
  "XX.....XX               ",
 
88
  "XX......XX              ",
 
89
  "XX.......XX             ",
 
90
  "XX........XX            ",
 
91
  "XX........XXX           ",
 
92
  "XX......XXXXX           ",
 
93
  "XX.XXX..XX              ",
 
94
  "XXXX XX..XX             ",
 
95
  "XX   XX..XX             ",
 
96
  "     XX..XX             ",
 
97
  "      XX..XX            ",
 
98
  "      XX..XX            ",
 
99
  "       XXXX             ",
 
100
  "       XX               ",
 
101
  "                        ",
 
102
  "                        ",
 
103
  "                        ",
 
104
)
 
105
 
 
106
 
 
107
def compile(strings, black, white):
 
108
    """pygame.cursors.compile(strings, black, white) -> data, mask
 
109
compile cursor strings into cursor data
 
110
 
 
111
This takes a set of strings with equal length and computes
 
112
the binary data for that cursor. The string widths must be
 
113
divisible by 8.
 
114
 
 
115
The black and white arguments are single letter strings that
 
116
tells which characters will represent black pixels, and which
 
117
characters represent white pixels. All other characters are
 
118
considered clear.
 
119
 
 
120
This returns a tuple containing the cursor data and cursor mask
 
121
data. Both these arguments are used when setting a cursor with
 
122
pygame.mouse.set_cursor().
 
123
"""
 
124
    
 
125
    #first check for consistent lengths
 
126
    size = len(strings[0]), len(strings)
 
127
    if size[0] % 8 or size[1] % 8:
 
128
        raise ValueError, "cursor string sizes must be divisible by 8 "+`size`
 
129
    for s in strings[1:]:
 
130
        if len(s) != size[0]:
 
131
            raise ValueError, "Cursor strings are inconsistent lengths"
 
132
 
 
133
    #create the data arrays.
 
134
    #this could stand a little optimizing
 
135
    maskdata = []
 
136
    filldata = []
 
137
    maskitem = fillitem = 0
 
138
    step = 8
 
139
    for s in strings:
 
140
        for c in s:
 
141
            maskitem = maskitem << 1
 
142
            fillitem = fillitem << 1
 
143
            step = step - 1
 
144
            if c == black:
 
145
                maskitem = maskitem | 1
 
146
            elif c == white:
 
147
                maskitem = maskitem | 1
 
148
                fillitem = fillitem | 1
 
149
            if not step:
 
150
                maskdata.append(maskitem)
 
151
                filldata.append(fillitem)
 
152
                maskitem = fillitem = 0
 
153
                step = 8
 
154
    return tuple(filldata), tuple(maskdata)
 
155
 
 
156
 
 
157
 
 
158
def load_xbm(curs, mask):
 
159
    """pygame.cursors.load_xbm(cursorfile, maskfile) -> cursor_args
 
160
reads a pair of XBM files into set_cursor arguments
 
161
 
 
162
Arguments can either be filenames or filelike objects
 
163
with the readlines method. Not largely tested, but
 
164
should work with typical XBM files.
 
165
"""
 
166
    def bitswap(num):
 
167
        val = 0
 
168
        for x in range(8):
 
169
            b = num&(1<<x) != 0
 
170
            val = val<<1 | b
 
171
        return val
 
172
    if type(curs) is type(''): curs = open(curs)
 
173
    if type(mask) is type(''): mask = open(mask)
 
174
    curs = curs.readlines()
 
175
    mask = mask.readlines()
 
176
    #load width,height
 
177
    width = int(curs[0].split()[-1])
 
178
    height = int(curs[1].split()[-1])
 
179
    #load hotspot position
 
180
    if curs[2].startswith('#define'):
 
181
        hotx = int(curs[2].split()[-1])
 
182
        hoty = int(curs[3].split()[-1])
 
183
    else:
 
184
        hotx = hoty = 0
 
185
 
 
186
    info = width, height, hotx, hoty    
 
187
 
 
188
    for line in range(len(curs)):
 
189
        if curs[line].startswith('static char'):
 
190
            break
 
191
    data = ' '.join(curs[line+1:]).replace('};', '').replace(',', '')
 
192
    cursdata = []
 
193
    for x in data.split():
 
194
        cursdata.append(bitswap(int(x, 16)))
 
195
    cursdata = tuple(cursdata)
 
196
 
 
197
    for line in range(len(mask)):
 
198
        if mask[line].startswith('static char'):
 
199
            break
 
200
    data = ' '.join(mask[line+1:]).replace('};', '').replace(',', '')
 
201
    maskdata = []
 
202
    for x in data.split():
 
203
        maskdata.append(bitswap(int(x, 16)))
 
204
    maskdata = tuple(maskdata)
 
205
    return info[:2], info[2:], cursdata, maskdata