Skip to content

Commit 023aa69

Browse files
committed
- Handling key and mouse events on Cocos2d
1 parent 4ce4a50 commit 023aa69

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

handling_events.py

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#!/usr/bin/env python3
2+
# Source: https://github.com/los-cocos/cocos/blob/master/samples/handling_events.py
3+
from __future__ import division, print_function, unicode_literals
4+
5+
# This code is so you can run the samples without installing the package
6+
import sys
7+
import os
8+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
9+
#
10+
11+
import cocos
12+
from cocos.director import director
13+
14+
import pyglet
15+
16+
# Our main KeyDisplay Class
17+
class KeyDisplay(cocos.layer.Layer):
18+
19+
# If you want your layer to receive director.window events
20+
# you have to set this variable to 'True'
21+
is_event_handler = True #: enable pyglet's events
22+
23+
def __init__(self):
24+
25+
super(KeyDisplay, self).__init__()
26+
27+
self.text = cocos.text.Label("", x=100, y=280)
28+
29+
# To keep track of which keys are pressed:
30+
self.keys_pressed = set()
31+
self.update_text()
32+
self.add(self.text)
33+
34+
# This method of the class updates the text
35+
def update_text(self):
36+
key_names = [pyglet.window.key.symbol_string(k) for k in self.keys_pressed]
37+
text = 'Keys: ' + ','.join(key_names)
38+
# Update self.text
39+
self.text.element.text = text
40+
41+
def on_key_press(self, key, modifiers):
42+
"""This function is called when a key is pressed.
43+
44+
'key' is a constant indicating which key was pressed.
45+
'modifiers' is a bitwise or of several constants indicating which
46+
modifiers are active at the time of the press (ctrl, shift, capslock, etc.)
47+
48+
See also on_key_release situations when a key press does not fire an
49+
'on_key_press' event.
50+
"""
51+
self.keys_pressed.add(key)
52+
self.update_text()
53+
54+
def on_key_release(self, key, modifiers):
55+
"""This function is called when a key is released.
56+
57+
'key' is a constant indicating which key was pressed.
58+
'modifiers' is a bitwise or of several constants indicating which
59+
modifiers are active at the time of the press (ctrl, shift, capslock, etc.)
60+
61+
Constants are the ones from pyglet.window.key
62+
63+
Sometimes a key release can arrive without a previous 'press' event, so discard
64+
is used instead of remove.
65+
66+
This can happen in Windows by example when you 'press ALT, release ALT, press B,
67+
release B'; the events received are 'pressed ALT, released ALT, released B'.
68+
69+
This may depend on the pyglet version, here pyglet from repo at may 2014 was used.
70+
"""
71+
self.keys_pressed.discard(key)
72+
self.update_text()
73+
74+
75+
class MouseDisplay(cocos.layer.Layer):
76+
77+
# If you want that your layer receives events
78+
# you must set this variable to 'True',
79+
# otherwise it won't receive any event.
80+
is_event_handler = True
81+
82+
def __init__(self):
83+
super(MouseDisplay, self).__init__()
84+
85+
self.posx = 100
86+
self.posy = 240
87+
self.text = cocos.text.Label('No mouse events yet', font_size=18, x=self.posx, y=self.posy)
88+
self.add(self.text)
89+
90+
def update_text(self, x, y):
91+
text = 'Mouse @ %d,%d' % (x, y)
92+
self.text.element.text = text
93+
self.text.element.x = self.posx
94+
self.text.element.y = self.posy
95+
96+
def on_mouse_motion(self, x, y, dx, dy):
97+
"""Called when the mouse moves over the app window with no button pressed
98+
99+
(x, y) are the physical coordinates of the mouse
100+
(dx, dy) is the distance vector covered by the mouse pointer since the
101+
last call.
102+
"""
103+
self.update_text(x, y)
104+
105+
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
106+
"""Called when the mouse moves over the app window with some button(s) pressed
107+
108+
(x, y) are the physical coordinates of the mouse
109+
(dx, dy) is the distance vector covered by the mouse pointer since the
110+
last call.
111+
'buttons' is a bitwise or of pyglet.window.mouse constants LEFT, MIDDLE, RIGHT
112+
'modifiers' is a bitwise or of pyglet.window.key modifier constants
113+
(values like 'SHIFT', 'OPTION', 'ALT')
114+
"""
115+
self.update_text(x, y)
116+
117+
def on_mouse_press(self, x, y, buttons, modifiers):
118+
"""This function is called when any mouse button is pressed
119+
120+
(x, y) are the physical coordinates of the mouse
121+
'buttons' is a bitwise or of pyglet.window.mouse constants LEFT, MIDDLE, RIGHT
122+
'modifiers' is a bitwise or of pyglet.window.key modifier constants
123+
(values like 'SHIFT', 'OPTION', 'ALT')
124+
125+
cocos has 2 coordinates systems, a physical one and a virtual one.
126+
pyglet sends physical coordinates, as we want to map it in a virtual context,
127+
director.get_virtual_coordinates(x, y) does the correct mapping for us
128+
doing it with self.posx, self.posy = x,y will work too but as soon
129+
as we change the window size the mapping is off-set
130+
"""
131+
self.posx, self.posy = director.get_virtual_coordinates(x, y)
132+
self.update_text(x, y)
133+
134+
135+
if __name__ == "__main__":
136+
director.init(resizable=True)
137+
# Run a scene with our event displayers:
138+
director.run(cocos.scene.Scene(KeyDisplay(), MouseDisplay()))

0 commit comments

Comments
 (0)