-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathzoompanscene.py
62 lines (46 loc) · 1.73 KB
/
zoompanscene.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
from scene import *
from Gestures import *
class ZoomPanScene(Scene):
def setup(self):
g = Gestures()
g.add_pan(self.view, self.on_pan, maximum_number_of_touches=1)
g.add_pinch(self.view, self.on_zoom)
self.really_panning = False
self.zoomer = Node(z_position=0.75, parent=self)
self.content_area = Node(z_position=1.0, parent=self.zoomer)
def on_pan(self, data):
if data.state == Gestures.BEGAN:
self.prev_location = data.location
self.really_panning = True
elif data.state == Gestures.CHANGED:
self.pan(data)
elif data.state == Gestures.ENDED:
self.really_panning = False
def pan(self, data):
if data.number_of_touches < 2 and not self.really_panning: return
delta = data.location - self.prev_location
delta.y = -delta.y
self.prev_location = data.location
self.content_area.position += delta/self.zoomer.scale
def on_zoom(self, data):
if data.state == Gestures.BEGAN:
self.prev_location = data.location
self.start_scale = self.zoomer.scale
elif data.state == Gestures.CHANGED:
scale = self.start_scale * data.scale
scn_pos = data.location / self.zoomer.scale
self.content_area.position -= scn_pos
self.zoomer.scale = scale
new_scn_pos = data.location/self.zoomer.scale
self.content_area.position += new_scn_pos
self.pan(data)
def add_child(self, child):
self.content_area.add_child(child)
if __name__ == '__main__':
class SpaceScene(ZoomPanScene):
def setup(self):
super().setup()
ship = SpriteNode('spc:PlayerShip1Orange')
ship.position = self.size / 2
self.add_child(ship)
run(SpaceScene())