Skip to content

Commit 07c26d4

Browse files
authored
Merge pull request #2484 from pygame-community/sdl_mouse_touch
Use SDL_MOUSE_TOUCH_EVENTS in touch test and docs
2 parents eb8faef + 4424a4c commit 07c26d4

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

docs/reST/ref/pygame.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,3 +535,15 @@ By default, when the window is not in focus, input devices do not get
535535
updated. However, using this environment variable it is possible to get
536536
joystick updates even when the window is in the background. Must be set
537537
before calling :func:`pygame.init()` or :func:`pygame.joystick.init()`.
538+
539+
|
540+
541+
::
542+
543+
SDL_MOUSE_TOUCH_EVENTS
544+
Set to "1" to make mouse events also generate touch events.
545+
546+
Useful for testing touch events on desktop platforms (e.g. with a trackpad)
547+
where this is set to 0 by default.
548+
549+

test/touch_test.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,33 @@
55
from pygame.tests.test_utils import question
66

77

8-
has_touchdevice = touch.get_num_devices() > 0
9-
10-
118
class TouchTest(unittest.TestCase):
129
@classmethod
1310
def setUpClass(cls):
14-
pygame.display.init()
11+
os.environ["SDL_MOUSE_TOUCH_EVENTS"] = "0"
12+
pygame.init()
1513

1614
@classmethod
1715
def tearDownClass(cls):
18-
pygame.display.quit()
16+
pygame.quit()
1917

2018
def test_num_devices(self):
2119
touch.get_num_devices()
2220

23-
@unittest.skipIf(not has_touchdevice, "no touch devices found")
2421
def test_get_device(self):
25-
touch.get_device(0)
22+
if touch.get_num_devices() > 0:
23+
touch.get_device(0)
2624

2725
def test_get_device__invalid(self):
2826
self.assertRaises(pygame.error, touch.get_device, -1234)
2927
self.assertRaises(TypeError, touch.get_device, "test")
3028

31-
@unittest.skipIf(not has_touchdevice, "no touch devices found")
3229
def test_num_fingers(self):
33-
touch.get_num_fingers(touch.get_device(0))
30+
if touch.get_num_devices() > 0:
31+
if touch.get_device(0) >= 0:
32+
# mice acting as touch devices have negative device IDs
33+
# they also do not work with get_num_fingers()
34+
touch.get_num_fingers(touch.get_device(0))
3435

3536
def test_num_fingers__invalid(self):
3637
self.assertRaises(TypeError, touch.get_num_fingers, "test")
@@ -40,15 +41,26 @@ def test_num_fingers__invalid(self):
4041
class TouchInteractiveTest(unittest.TestCase):
4142
__tags__ = ["interactive"]
4243

43-
@unittest.skipIf(not has_touchdevice, "no touch devices found")
44+
@classmethod
45+
def setUpClass(cls):
46+
# if we don't have a touch device, enable the mouse to act as a
47+
# touch device for the interactive tests.
48+
os.environ["SDL_MOUSE_TOUCH_EVENTS"] = "0"
49+
pygame.init()
50+
if touch.get_num_devices() == 0:
51+
pygame.quit()
52+
os.environ["SDL_MOUSE_TOUCH_EVENTS"] = "1"
53+
pygame.init()
54+
55+
@classmethod
56+
def tearDownClass(cls):
57+
pygame.quit()
58+
4459
def test_get_finger(self):
4560
"""ask for touch input and check the dict"""
4661

47-
pygame.display.init()
48-
pygame.font.init()
49-
5062
os.environ["SDL_VIDEO_WINDOW_POS"] = "50,50"
51-
screen = pygame.display.set_mode((800, 600))
63+
screen = pygame.display.set_mode((1000, 600))
5264
screen.fill((255, 255, 255))
5365

5466
font = pygame.font.Font(None, 32)
@@ -69,7 +81,10 @@ def test_get_finger(self):
6981
num_devices = pygame._sdl2.touch.get_num_devices()
7082
if num_devices > 0:
7183
first_device = pygame._sdl2.touch.get_device(0)
72-
num_fingers = pygame._sdl2.touch.get_num_fingers(first_device)
84+
if first_device >= 0:
85+
num_fingers = pygame._sdl2.touch.get_num_fingers(first_device)
86+
else:
87+
num_fingers = 1
7388
if num_fingers > 0:
7489
for finger_index in range(0, num_fingers):
7590
data = pygame._sdl2.touch.get_finger(first_device, finger_index)
@@ -90,8 +105,6 @@ def test_get_finger(self):
90105
response = question("Does the finger data seem correct?")
91106
self.assertTrue(response)
92107

93-
pygame.display.quit()
94-
95108

96109
if __name__ == "__main__":
97110
unittest.main()

0 commit comments

Comments
 (0)