-
Notifications
You must be signed in to change notification settings - Fork 2
/
handling_events.py
executable file
·141 lines (108 loc) · 5.25 KB
/
handling_events.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
#!/usr/bin/env python3
# Source: https://github.com/los-cocos/cocos/blob/master/samples/handling_events.py
from __future__ import division, print_function, unicode_literals
# This code is so you can run the samples without installing the package
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
import cocos
from cocos.director import director
import pyglet
# Our main KeyDisplay Class
class KeyDisplay(cocos.layer.Layer):
# If you want your layer to receive director.window events
# you have to set this variable to 'True'
is_event_handler = True #: enable pyglet's events
def __init__(self):
super(KeyDisplay, self).__init__()
self.text = cocos.text.Label("", x=100, y=280)
# To keep track of which keys are pressed:
self.keys_pressed = set()
self.update_text()
self.add(self.text)
print("Initialiazed KeyDisplay()")
# This method of the class updates the text
def update_text(self):
key_names = [pyglet.window.key.symbol_string(k) for k in self.keys_pressed]
text = 'Keys: ' + ','.join(key_names)
# Update self.text
self.text.element.text = text
print("Called update_text in KeyDisplay()")
def on_key_press(self, key, modifiers):
"""This function is called when a key is pressed.
'key' is a constant indicating which key was pressed.
'modifiers' is a bitwise or of several constants indicating which
modifiers are active at the time of the press (ctrl, shift, capslock, etc.)
See also on_key_release situations when a key press does not fire an
'on_key_press' event.
"""
self.keys_pressed.add(key)
self.update_text()
print("Called on_key_press in KeyDisplay()")
def on_key_release(self, key, modifiers):
"""This function is called when a key is released.
'key' is a constant indicating which key was pressed.
'modifiers' is a bitwise or of several constants indicating which
modifiers are active at the time of the press (ctrl, shift, capslock, etc.)
Constants are the ones from pyglet.window.key
Sometimes a key release can arrive without a previous 'press' event, so discard
is used instead of remove.
This can happen in Windows by example when you 'press ALT, release ALT, press B,
release B'; the events received are 'pressed ALT, released ALT, released B'.
This may depend on the pyglet version, here pyglet from repo at may 2014 was used.
"""
self.keys_pressed.discard(key)
self.update_text()
print("Called on_key_release in KeyDisplay()")
class MouseDisplay(cocos.layer.Layer):
# If you want that your layer receives events
# you must set this variable to 'True',
# otherwise it won't receive any event.
is_event_handler = True
def __init__(self):
super(MouseDisplay, self).__init__()
self.posx = 100
self.posy = 240
self.text = cocos.text.Label('No mouse events yet', font_size=18, x=self.posx, y=self.posy)
self.add(self.text)
print("Initialiazed MouseDisplay()")
def update_text(self, x, y):
text = 'Mouse @ %d,%d' % (x, y)
self.text.element.text = text
self.text.element.x = self.posx
self.text.element.y = self.posy
def on_mouse_motion(self, x, y, dx, dy):
"""Called when the mouse moves over the app window with no button pressed
(x, y) are the physical coordinates of the mouse
(dx, dy) is the distance vector covered by the mouse pointer since the
last call.
"""
self.update_text(x, y)
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
"""Called when the mouse moves over the app window with some button(s) pressed
(x, y) are the physical coordinates of the mouse
(dx, dy) is the distance vector covered by the mouse pointer since the
last call.
'buttons' is a bitwise or of pyglet.window.mouse constants LEFT, MIDDLE, RIGHT
'modifiers' is a bitwise or of pyglet.window.key modifier constants
(values like 'SHIFT', 'OPTION', 'ALT')
"""
self.update_text(x, y)
def on_mouse_press(self, x, y, buttons, modifiers):
"""This function is called when any mouse button is pressed
(x, y) are the physical coordinates of the mouse
'buttons' is a bitwise or of pyglet.window.mouse constants LEFT, MIDDLE, RIGHT
'modifiers' is a bitwise or of pyglet.window.key modifier constants
(values like 'SHIFT', 'OPTION', 'ALT')
cocos has 2 coordinates systems, a physical one and a virtual one.
pyglet sends physical coordinates, as we want to map it in a virtual context,
director.get_virtual_coordinates(x, y) does the correct mapping for us
doing it with self.posx, self.posy = x,y will work too but as soon
as we change the window size the mapping is off-set
"""
self.posx, self.posy = director.get_virtual_coordinates(x, y)
self.update_text(x, y)
if __name__ == "__main__":
director.init(resizable=True)
# Run a scene with our event displayers:
director.run(cocos.scene.Scene(KeyDisplay(), MouseDisplay()))