-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.py
More file actions
94 lines (73 loc) · 3.08 KB
/
input.py
File metadata and controls
94 lines (73 loc) · 3.08 KB
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
from pygame import KEYUP, KEYDOWN
class camera:
def __init__(self, bbox, screenWidth, screenHeight):
self.__origin = bbox[0:2]
self.__dim = (bbox[2] - bbox[0], bbox[3] - bbox[1])
self.__sDim = (screenWidth, screenHeight)
self.__pos = [0, 0]
self.__swid = self.__dim[0]
self._snap()
def aspectRatio(self):
return self.__dim[1] / self.__dim[0]
def bottomLeft(self):
return (self.__pos[0] - self.__swid / 2, self.__pos[1] - self.__swid * self.aspectRatio() / 2)
def widthHeight(self):
return (self.__swid, self.__swid * self.aspectRatio())
def zoom(self):
return self.__dim[0] / self.__swid
def _snap(self):
hwid = self.__swid / 2
if self.__pos[0] - hwid < self.__origin[0]:
self.__pos[0] = hwid + self.__origin[0]
elif self.__pos[0] + hwid > self.__dim[0] + self.__origin[0]:
self.__pos[0] = self.__dim[0] + self.__origin[0] - hwid
hhei = self.__swid * self.aspectRatio() / 2
if self.__pos[1] - hhei < self.__origin[1]:
self.__pos[1] = hhei + self.__origin[1]
elif self.__pos[1] + hhei > self.__dim[1] + self.__origin[1]:
self.__pos[1] = self.__dim[1] + self.__origin[1] - hhei
def move(self, dx=0, dy=0):
factor = self.__swid
self.__pos[0] += dx * factor
self.__pos[1] += dy * factor
self._snap()
def zoom_in(self, factor):
self.__swid /= factor
if (self.__swid > self.__dim[0]):
self.__swid = self.__dim[0]
self._snap()
def screenCoords(self, pos):
bbox = self.bbox()
return ((pos[0] - bbox[0]) * self.__sDim[0] / (bbox[2] - bbox[0]),
self.__sDim[1] - (pos[1] - bbox[1]) * self.__sDim[1] / (bbox[3] - bbox[1]))
def worldCoords(self, pos):
return (self.__pos[0] + self.__swid * (pos[0] / self.__sDim[0] - .5),
self.__pos[1] + self.__swid * self.aspectRatio() * (.5 - pos[1] / self.__sDim[1]))
def bbox(self):
xmin = self.__pos[0] - self.__swid / 2
xmax = self.__pos[0] + self.__swid / 2
ymin = self.__pos[1] - self.__swid * self.aspectRatio() / 2
ymax = self.__pos[1] + self.__swid * self.aspectRatio() / 2
return (xmin, ymin, xmax, ymax)
class keyListener:
RELEASED = 0
JUST_PRESSED = 1
PRESSED = 2
def __init__(self):
self.__dict = {}
def pressAction(self, key, val):
self.__dict[key] = [val, keyListener.RELEASED, False]
def tapAction(self, key, val):
self.__dict[key] = [val, keyListener.RELEASED, True]
def pollEvent(self, event):
if event.key in self.__dict:
if event.type == KEYUP:
self.__dict[event.key][1] = keyListener.RELEASED
elif event.type == KEYDOWN:
self.__dict[event.key][1] = keyListener.JUST_PRESSED
def act(self):
for pair in self.__dict.values():
if pair[1] == keyListener.JUST_PRESSED:
pair[0]()
if pair[2]:
pair[1] = keyListener.PRESSED