forked from pygame-community/pygame-ce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcursors.py
984 lines (884 loc) · 23.4 KB
/
cursors.py
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
# pygame-ce - 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
"""Pygame module for cursor resources.
Pygame offers control over the system hardware cursor. Pygame supports
black and white cursors (bitmap cursors), as well as system variant cursors and color cursors.
You control the cursor with functions inside :mod:`pygame.mouse`.
This cursors module contains functions for loading and decoding various
cursor formats. These allow you to easily store your cursors in external files
or directly as encoded python strings.
The module includes several standard cursors. The :func:`pygame.mouse.set_cursor()`
function takes several arguments. All those arguments have been stored in a
single tuple you can call like this:
::
>>> pygame.mouse.set_cursor(*pygame.cursors.arrow)
The following variables can be passed to ``pygame.mouse.set_cursor`` function:
* ``pygame.cursors.arrow``
* ``pygame.cursors.diamond``
* ``pygame.cursors.broken_x``
* ``pygame.cursors.tri_left``
* ``pygame.cursors.tri_right``
This module also contains a few cursors as formatted strings. You'll need to
pass these to ``pygame.cursors.compile()`` function before you can use them.
The example call would look like this:
::
>>> cursor = pygame.cursors.compile(pygame.cursors.textmarker_strings)
>>> pygame.mouse.set_cursor((8, 16), (0, 0), *cursor)
The following strings can be converted into cursor bitmaps with
``pygame.cursors.compile()`` :
* ``pygame.cursors.thickarrow_strings``
* ``pygame.cursors.sizer_x_strings``
* ``pygame.cursors.sizer_y_strings``
* ``pygame.cursors.sizer_xy_strings``
* ``pygame.cursor.textmarker_strings``
"""
import pygame
_cursor_id_table = {
pygame.SYSTEM_CURSOR_ARROW: "SYSTEM_CURSOR_ARROW",
pygame.SYSTEM_CURSOR_IBEAM: "SYSTEM_CURSOR_IBEAM",
pygame.SYSTEM_CURSOR_WAIT: "SYSTEM_CURSOR_WAIT",
pygame.SYSTEM_CURSOR_CROSSHAIR: "SYSTEM_CURSOR_CROSSHAIR",
pygame.SYSTEM_CURSOR_WAITARROW: "SYSTEM_CURSOR_WAITARROW",
pygame.SYSTEM_CURSOR_SIZENWSE: "SYSTEM_CURSOR_SIZENWSE",
pygame.SYSTEM_CURSOR_SIZENESW: "SYSTEM_CURSOR_SIZENESW",
pygame.SYSTEM_CURSOR_SIZEWE: "SYSTEM_CURSOR_SIZEWE",
pygame.SYSTEM_CURSOR_SIZENS: "SYSTEM_CURSOR_SIZENS",
pygame.SYSTEM_CURSOR_SIZEALL: "SYSTEM_CURSOR_SIZEALL",
pygame.SYSTEM_CURSOR_NO: "SYSTEM_CURSOR_NO",
pygame.SYSTEM_CURSOR_HAND: "SYSTEM_CURSOR_HAND",
}
class Cursor:
"""Pygame object representing a cursor.
In pygame 2, there are 3 types of cursors you can create to give your
game that little bit of extra polish. There's **bitmap** type cursors,
which existed in pygame 1.x, and are compiled from a string or load from an xbm file.
Then there are **system** type cursors, where you choose a preset that will
convey the same meaning but look native across different operating systems.
Finally you can create a **color** cursor, which displays a pygame surface as the cursor.
**Creating a system cursor**
Choose a constant from this list, pass it into ``pygame.cursors.Cursor(constant)``,
and you're good to go. Be advised that not all systems support every system
cursor, and you may get a substitution instead. For example, on macOS,
WAIT/WAITARROW should show up as an arrow, and SIZENWSE/SIZENESW/SIZEALL
should show up as a closed hand. And on Wayland, every SIZE cursor should
show up as a hand.
::
Pygame Cursor Constant Description
--------------------------------------------
pygame.SYSTEM_CURSOR_ARROW arrow
pygame.SYSTEM_CURSOR_IBEAM i-beam
pygame.SYSTEM_CURSOR_WAIT wait
pygame.SYSTEM_CURSOR_CROSSHAIR crosshair
pygame.SYSTEM_CURSOR_WAITARROW small wait cursor
(or wait if not available)
pygame.SYSTEM_CURSOR_SIZENWSE double arrow pointing
northwest and southeast
pygame.SYSTEM_CURSOR_SIZENESW double arrow pointing
northeast and southwest
pygame.SYSTEM_CURSOR_SIZEWE double arrow pointing
west and east
pygame.SYSTEM_CURSOR_SIZENS double arrow pointing
north and south
pygame.SYSTEM_CURSOR_SIZEALL four pointed arrow pointing
north, south, east, and west
pygame.SYSTEM_CURSOR_NO slashed circle or crossbones
pygame.SYSTEM_CURSOR_HAND hand
**Creating a cursor without passing arguments**
In addition to the cursor constants available and described above, you can
also call ``pygame.cursors.Cursor()``, and your cursor is ready (doing that
is the same as calling ``pygame.cursors.Cursor(pygame.SYSTEM_CURSOR_ARROW)``.
Doing one of those calls actually creates a system cursor using the default
native image.
**Creating a color cursor**
To create a color cursor, create a ``Cursor`` from a ``hotspot`` and a ``surface``.
``hotspot`` are (x,y) coordinates that determine where in the cursor the exact point is.
The hotspot position must be within the bounds of the ``surface``.
**Creating a bitmap cursor**
When the mouse cursor is visible, it will be displayed as a black and white
bitmap using the given bitmask arrays. The ``size`` is a sequence containing
the cursor width and height. ``hotspot`` is a sequence containing the cursor
hotspot position.
A cursor has a width and height, but a mouse position is represented by a
set of point coordinates. So the value passed into the cursor ``hotspot``
variable helps pygame to actually determine at what exact point the cursor
is at.
``xormasks`` is a sequence of bytes containing the cursor xor data masks.
Lastly ``andmasks``, a sequence of bytes containing the cursor bitmask data.
To create these variables, we can make use of the
:func:`pygame.cursors.compile()` function.
Width and height must be a multiple of 8, and the mask arrays must be the
correct size for the given width and height. Otherwise an exception is raised.
.. versionaddedold:: 2.0.1
"""
def __init__(self, *args):
self.type = "system"
"""Get the cursor type.
The type will be ``"system"``, ``"bitmap"``, or ``"color"``.
"""
self.data = tuple()
"""Get the cursor data.
Returns the data that was used to create this cursor object, wrapped up in a tuple.
"""
if len(args) == 0:
self.type = "system"
self.data = (pygame.SYSTEM_CURSOR_ARROW,)
elif len(args) == 1 and args[0] in _cursor_id_table:
self.type = "system"
self.data = (args[0],)
elif len(args) == 1 and isinstance(args[0], Cursor):
self.type = args[0].type
self.data = args[0].data
elif (
len(args) == 2 and len(args[0]) == 2 and isinstance(args[1], pygame.Surface)
):
self.type = "color"
self.data = tuple(args)
elif len(args) == 4 and len(args[0]) == 2 and len(args[1]) == 2:
self.type = "bitmap"
# pylint: disable=consider-using-generator
# See https://github.com/pygame/pygame/pull/2509 for analysis
self.data = tuple(tuple(arg) for arg in args)
else:
raise TypeError("Arguments must match a cursor specification")
def __len__(self):
return len(self.data)
def __iter__(self):
return iter(self.data)
def __getitem__(self, index):
return self.data[index]
def __eq__(self, other):
return isinstance(other, Cursor) and self.data == other.data
def __ne__(self, other):
return not self.__eq__(other)
def copy(self):
"""Copy the current cursor.
Returns a new Cursor object with the same data and hotspot as the original.
"""
return self.__class__(self)
__copy__ = copy
def __hash__(self):
return hash(tuple([self.type] + list(self.data)))
def __repr__(self):
if self.type == "system":
id_string = _cursor_id_table.get(self.data[0], "constant lookup error")
return f"<Cursor(type: system, constant: {id_string})>"
if self.type == "bitmap":
size = f"size: {self.data[0]}"
hotspot = f"hotspot: {self.data[1]}"
return f"<Cursor(type: bitmap, {size}, {hotspot})>"
if self.type == "color":
hotspot = f"hotspot: {self.data[0]}"
surf = repr(self.data[1])
return f"<Cursor(type: color, {hotspot}, surf: {surf})>"
raise TypeError("Invalid Cursor")
# Python side of the set_cursor function: C side in mouse.c
def set_cursor(*args):
"""set_cursor(pygame.cursors.Cursor OR args for a pygame.cursors.Cursor) -> None
set the mouse cursor to a new cursor"""
cursor = Cursor(*args)
pygame.mouse._set_cursor(**{cursor.type: cursor.data})
pygame.mouse.set_cursor = set_cursor
del set_cursor # cleanup namespace
# Python side of the get_cursor function: C side in mouse.c
def get_cursor():
"""get_cursor() -> pygame.cursors.Cursor
get the current mouse cursor"""
return Cursor(*pygame.mouse._get_cursor())
pygame.mouse.get_cursor = get_cursor
del get_cursor # cleanup namespace
arrow = Cursor(
(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 = Cursor(
(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 = Cursor(
(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 = Cursor(
(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 = Cursor(
(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 = Cursor(
(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)
# Be warned, though, that cursors created from compiled strings do not support colors.
# sized 24x24
thickarrow_strings = (
"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 ",
" ",
" ",
" ",
)
# sized 24x16
sizer_x_strings = (
" 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 ",
" ",
" ",
" ",
" ",
" ",
)
# sized 16x24
sizer_y_strings = (
" 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 ",
" ",
" ",
" ",
" ",
" ",
)
# sized 24x16
sizer_xy_strings = (
"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 ",
" ",
" ",
)
# sized 8x16
textmarker_strings = (
"ooo ooo ",
" o ",
" o ",
" o ",
" o ",
" o ",
" o ",
" o ",
" o ",
" o ",
" o ",
"ooo ooo ",
" ",
" ",
" ",
" ",
)
def compile(strings, black="X", white=".", xor="o"):
"""Create binary cursor data from simple strings.
A sequence of strings can be used to create binary cursor data for the
system cursor. This returns the binary data in the form of two tuples.
Those can be passed as the third and fourth arguments respectively of the
:func:`pygame.mouse.set_cursor()` function.
If you are creating your own cursor strings, you can use any value represent
the black and white pixels. Some system allow you to set a special toggle
color for the system color, this is also called the xor color. If the system
does not support xor cursors, that color will simply be black.
The height must be divisible by 8. The width of the strings must all be equal
and be divisible by 8. If these two conditions are not met, ``ValueError`` is
raised.
An example set of cursor strings looks like this
::
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 ",
" ",
" ",
" ")
"""
# first check for consistent lengths
size = len(strings[0]), len(strings)
if size[0] % 8 or size[1] % 8:
raise ValueError(f"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
fillitem = fillitem | 1
elif c == white:
maskitem = maskitem | 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):
"""Load cursor data from an XBM file.
This loads cursors for a simple subset of ``XBM`` files. ``XBM`` files are
traditionally used to store cursors on UNIX systems, they are an ASCII
format used to represent simple images.
Sometimes the black and white color values will be split into two separate
``XBM`` files. You can pass a second maskfile argument to load the two
images into a single cursor.
The cursorfile and maskfile arguments can either be filenames or file-like
object with the readlines method.
The return value cursor_args can be passed directly to the
``pygame.mouse.set_cursor()`` function.
"""
def bitswap(num):
val = 0
for x in range(8):
b = num & (1 << x) != 0
val = val << 1 | b
return val
if hasattr(curs, "readlines"):
curs = curs.readlines()
else:
with open(curs, encoding="ascii") as cursor_f:
curs = cursor_f.readlines()
if hasattr(mask, "readlines"):
mask = mask.readlines()
else:
with open(mask, encoding="ascii") as mask_f:
mask = mask_f.readlines()
# avoid comments
for i, line in enumerate(curs):
if line.startswith("#define"):
curs = curs[i:]
break
for i, line in enumerate(mask):
if line.startswith("#define"):
mask = mask[i:]
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
possible_starts = ("static char", "static unsigned char")
for i, line in enumerate(curs):
if line.startswith(possible_starts):
break
data = " ".join(curs[i + 1 :]).replace("};", "").replace(",", " ")
cursdata = tuple(bitswap(int(x, 16)) for x in data.split())
for i, line in enumerate(mask):
if line.startswith(possible_starts):
break
data = " ".join(mask[i + 1 :]).replace("};", "").replace(",", " ")
maskdata = tuple(bitswap(int(x, 16)) for x in data.split())
return info[:2], info[2:], cursdata, maskdata