-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyCamera.py
262 lines (223 loc) · 6.37 KB
/
myCamera.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
from direct.task import Task
from direct.showbase.DirectObject import DirectObject
from pandac.PandaModules import *
import libpanda
import math
'''
This class is used to move camera WASD in FPS style
'''
class KeyboardMover(DirectObject):
def __init__(self):
#ballancer
self.scrollSpeed = 2
#moving camera vars
self.pressedUp = False
self.pressedDown = False
self.pressedLeft = False
self.pressedRight = False
self.pressedFUp = False
self.pressedFDown = False
#setting up keys
keys = ["w","s","a","d","t","g"]
self.setupKeys(keys)
def moveCamera(self,task):
dt = globalClock.getDt()
if self.pressedUp == True:
camera.setY(camera, self.scrollSpeed*10*dt)
if self.pressedDown == True:
camera.setY(camera, -1*self.scrollSpeed*10*dt)
if self.pressedLeft == True:
camera.setX(camera, -1*self.scrollSpeed*10*dt)
if self.pressedRight == True:
camera.setX(camera, self.scrollSpeed*10*dt)
if self.pressedFUp == True:
camera.setZ(camera, self.scrollSpeed*10*dt)
if self.pressedFDown == True:
camera.setZ(camera, -1*self.scrollSpeed*10*dt)
return Task.cont
def setUnactive(self):
self.ignoreAll()
taskMgr.remove("keyboardMoverTask")
def setActive(self):
self.accept(self.up, self.pressKey, ["up"])
self.accept(self.down, self.pressKey, ["down"])
self.accept(self.left, self.pressKey, ["left"])
self.accept(self.right, self.pressKey, ["right"])
self.accept(self.FUp, self.pressKey, ["fup"])
self.accept(self.FDown, self.pressKey, ["fdown"])
self.accept(self.up+"-up", self.releaseKey, ["up"])
self.accept(self.down+"-up", self.releaseKey, ["down"])
self.accept(self.left+"-up", self.releaseKey, ["left"])
self.accept(self.right+"-up", self.releaseKey, ["right"])
self.accept(self.FUp+"-up", self.releaseKey, ["fup"])
self.accept(self.FDown+"-up", self.releaseKey, ["fdown"])
#self.ignore()
taskMgr.add(self.moveCamera, "keyboardMoverTask")
def releaseKey(self,key):
if key == "up":
self.pressedUp = False
if key == "down":
self.pressedDown = False
if key == "left":
self.pressedLeft = False
if key == "right":
self.pressedRight = False
if key == "fup":
self.pressedFUp = False
if key == "fdown":
self.pressedFDown = False
def pressKey(self,key):
if key == "up":
self.pressedUp = True
if key == "down":
self.pressedDown = True
if key == "left":
self.pressedLeft = True
if key == "right":
self.pressedRight = True
if key == "fup":
self.pressedFUp = True
if key == "fdown":
self.pressedFDown = True
def getKeys(self):
keys = [self.up, self.down, self.left, self.right, self.FUp, self.FDown]
return keys
def setupKeys(self,keys):
self.up = keys[0]
self.down = keys[1]
self.left = keys[2]
self.right = keys[3]
self.FUp = keys[4]
self.FDown = keys[5]
class CameraEvents(DirectObject):
def __init__(self):
pass
def keyPressed(self):
'''
Customize input handler keypress event
TODO: insert debug infos here to help other progs
'''
pass
def keyReleased(self):
'''
Customize input handler keyrelease event
TODO: insert debug infos here to help other progs
'''
pass
'''
This class is use to move mouse when in FPS (fly) mode
setActive() -> used to activate FPS mouse slide
setUnactive() -> inactivate FPS behaviour
'''
class MouseMover(DirectObject):
def __init__(self):
#camera rotation settings
self.heading = 0
self.pitch = 0
#used to restore last mouse position in editor
self.lastCoo = []
def flyCamera(self,task):
# figure out how much the mouse has moved (in pixels)
md = base.win.getPointer(0)
x = md.getX()
y = md.getY()
if base.win.movePointer(0, 300, 300):
self.heading = self.heading - (x - 300) * 0.1
self.pitch = self.pitch - (y - 300) * 0.1
if (self.pitch < -85): self.pitch = -85
if (self.pitch > 85): self.pitch = 85
base.camera.setH(self.heading)
base.camera.setP(self.pitch)
return Task.cont
def hideMouse(self):
#hiding mouse
props = WindowProperties()
props.setCursorHidden(True)
base.win.requestProperties(props)
def showMouse(self):
props = WindowProperties()
props.setCursorHidden(False)
base.win.requestProperties(props)
def setActive(self):
#hiding mouse
self.hideMouse()
#storing infos
md = base.win.getPointer(0)
x = md.getX()
y = md.getY()
self.lastCoo = [x,y]
#start activating this shit
base.win.movePointer(0,300,300)
taskMgr.add(self.flyCamera, "mouseMoverTask")
def setUnactive(self):
#showing mouse
self.showMouse()
#then removing task and resetting pointer to previous position
taskMgr.remove("mouseMoverTask")
base.win.movePointer(0,self.lastCoo[0],self.lastCoo[1])
class MyCamera(DirectObject):
def __init__(self):
#keyboard/mouse mover
self.km = KeyboardMover()
self.mm = MouseMover()
#Camera events
self.ce = CameraEvents()
#disabling mouse by default
base.disableMouse()
#setting status
self.state = "static"
self.setNearFar(1.0,10000)
self.setFov(75)
def getKeyboardMover(self):
return self.km
def getMouseMover(self):
return self.km
def getSelectionTool(self):
return self.st
def getFov(self):
return base.camLens.getFov()
def setNearFar(self,v1,v2):
base.camLens.setNearFar(v1,v2)
def setFov(self,value):
base.camLens.setFov(value)
'''
This is an interface method used to switch between fly and static
modes dinamically through a simple string
'''
def getSelected(self):
return self.st.listSelected
def setState(self,s):
#if there is a real change in camera state
if s != self.state:
#actually change state
if s == "fly":
#re-enabling all gui elements
self.mm.setActive()
self.km.setActive()
if s == "static":
self.mm.setUnactive()
self.km.setUnactive()
#changing state variable at the end of method execution
self.state = s
def setUtilsActive(self):
self.accept("tab", self.toggleView)
#self.accept("f", self.ps.toggleFullscreen)
def setUtilsUnactive(self):
self.ignore("tab")
self.ignore("f")
def toggleState(self):
if self.state == "static":
self.setState("fly")
else:
self.setState("static")
def toggleView(self):
if self.getState() == "fly":
#myGui.showAll() to be removed soon
myInputHandler.setActive()
if self.getState() == "static":
#myGui.hideAll() to be removed soon
myInputHandler.setInactive()
#switching camera in any case
self.toggleState()
def getState(self):
return self.state