forked from los-cocos/cocos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandling_events.py
127 lines (93 loc) · 4.4 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
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
class KeyDisplay(cocos.layer.Layer):
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)
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
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()
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()
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)
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')
"""
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()))