ControlManager.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. from direct.showbase.ShowBaseGlobal import *
  2. #from DirectGui import *
  3. #from PythonUtil import *
  4. #from IntervalGlobal import *
  5. #from otp.avatar import Avatar
  6. from direct.directnotify import DirectNotifyGlobal
  7. #import GhostWalker
  8. #import GravityWalker
  9. #import NonPhysicsWalker
  10. #import PhysicsWalker
  11. #if __debug__:
  12. # import DevWalker
  13. from direct.task import Task
  14. class ControlManager:
  15. notify = DirectNotifyGlobal.directNotify.newCategory("ControlManager")
  16. wantAvatarPhysicsIndicator = base.config.GetBool('want-avatar-physics-indicator', 0)
  17. wantAvatarPhysicsDebug = base.config.GetBool('want-avatar-physics-debug', 0)
  18. wantWASD = base.config.GetBool('want-WASD', 0)
  19. def __init__(self):
  20. assert self.notify.debugCall(id(self))
  21. self.enableJumpCounter = 1
  22. self.controls = {}
  23. self.currentControls = None
  24. self.isEnabled = 1
  25. #self.monitorTask = taskMgr.add(self.monitor, "ControlManager-%s"%(id(self)), priority=-1)
  26. inputState.watch("run", "running-on", "running-off")
  27. inputState.watch("forward", "arrow_up", "arrow_up-up")
  28. inputState.watch("forward", "control-arrow_up", "arrow_up-up")
  29. inputState.watch("forward", "shift-control-arrow_up", "arrow_up-up")
  30. inputState.watch("forward", "alt-arrow_up", "arrow_up-up")
  31. inputState.watch("forward", "control-alt-arrow_up", "arrow_up-up")
  32. inputState.watch("forward", "shift-arrow_up", "arrow_up-up")
  33. inputState.watch("forward", "force-forward", "force-forward-stop")
  34. inputState.watch("reverse", "arrow_down", "arrow_down-up")
  35. inputState.watch("reverse", "control-arrow_down", "arrow_down-up")
  36. inputState.watch("reverse", "shift-control-arrow_down", "arrow_down-up")
  37. inputState.watch("reverse", "alt-arrow_down", "arrow_down-up")
  38. inputState.watch("reverse", "control-alt-arrow_down", "arrow_down-up")
  39. inputState.watch("reverse", "shift-arrow_down", "arrow_down-up")
  40. inputState.watch("turnLeft", "arrow_left", "arrow_left-up")
  41. inputState.watch("turnLeft", "control-arrow_left", "arrow_left-up")
  42. inputState.watch("turnLeft", "shift-control-arrow_left", "arrow_left-up")
  43. inputState.watch("turnLeft", "alt-arrow_left", "alt-arrow_left-up")
  44. inputState.watch("turnLeft", "control-alt-arrow_left", "alt-arrow_left-up")
  45. inputState.watch("turnLeft", "shift-arrow_left", "arrow_left-up")
  46. inputState.watch("turnLeft", "mouse-look_left", "mouse-look_left-done")
  47. inputState.watch("turnLeft", "force-turnLeft", "force-turnLeft-stop")
  48. inputState.watch("turnRight", "arrow_right", "arrow_right-up")
  49. inputState.watch("turnRight", "control-arrow_right", "arrow_right-up")
  50. inputState.watch("turnRight", "shift-control-arrow_right", "arrow_right-up")
  51. inputState.watch("turnRight", "alt-arrow_right", "arrow_right-up")
  52. inputState.watch("turnRight", "control-alt-arrow_right", "arrow_right-up")
  53. inputState.watch("turnRight", "shift-arrow_right", "arrow_right-up")
  54. inputState.watch("turnRight", "mouse-look_right", "mouse-look_right-done")
  55. inputState.watch("turnRight", "force-turnRight", "force-turnRight-stop")
  56. inputState.watch("jump", "control", "control-up")
  57. inputState.watch("slideLeft", "home", "home-up")
  58. inputState.watch("slideRight", "end", "end-up")
  59. inputState.watch("levitateUp", "page_up", "page_up-up")
  60. inputState.watch("levitateDown", "page_down", "page_down-up")
  61. inputState.watch("run", "shift", "shift-up")
  62. # FYI, ghost mode uses jump for slide.
  63. #inputState.watch("slide", "slide-is-disabled", "slide-is-disabled")
  64. inputState.watch("slide", "mouse3", "mouse3-up")
  65. #inputState.watch("slideLeft", "shift-arrow_left", "shift-arrow_left-up")
  66. #inputState.watch("slideLeft", "control-arrow_left", "control-arrow_left-up")
  67. #inputState.watch("slideLeft", "alt-arrow_left", "alt-arrow_left-up")
  68. #inputState.watch("slideLeft", "shift-arrow_left", "shift-arrow_left-up")
  69. #inputState.watch("slideLeft", "slide-is-disabled", "slide-is-disabled")
  70. #inputState.watch("slideRight", "shift-arrow_right", "shift-arrow_right-up")
  71. #inputState.watch("slideRight", "control-arrow_right", "control-arrow_right-up")
  72. #inputState.watch("slideRight", "alt-arrow_right", "alt-arrow_right-up")
  73. #inputState.watch("slideRight", "shift-arrow_right", "shift-arrow_right-up")
  74. #inputState.watch("slideRight", "slide-is-disabled", "slide-is-disabled")
  75. if self.wantWASD:
  76. inputState.watch("forward", "w", "w-up")
  77. inputState.watch("forward", "control-w", "w-up")
  78. inputState.watch("forward", "shift-control-w", "w-up")
  79. inputState.watch("forward", "alt-w", "w-up")
  80. inputState.watch("forward", "control-alt-w", "w-up")
  81. inputState.watch("forward", "shift-w", "w-up")
  82. inputState.watch("reverse", "s", "s-up")
  83. inputState.watch("reverse", "control-s", "s-up")
  84. inputState.watch("reverse", "shift-control-s", "s-up")
  85. inputState.watch("reverse", "alt-s", "s-up")
  86. inputState.watch("reverse", "control-alt-s", "s-up")
  87. inputState.watch("reverse", "shift-s", "s-up")
  88. inputState.watch("turnLeft", "a", "a-up")
  89. inputState.watch("turnLeft", "control-a", "a-up")
  90. inputState.watch("turnLeft", "shift-control-a", "a-up")
  91. inputState.watch("turnLeft", "alt-a", "alt-a-up")
  92. inputState.watch("turnLeft", "control-alt-a", "alt-a-up")
  93. inputState.watch("turnLeft", "shift-a", "a-up")
  94. inputState.watch("turnRight", "d", "d-up")
  95. inputState.watch("turnRight", "control-d", "d-up")
  96. inputState.watch("turnRight", "shift-control-d", "d-up")
  97. inputState.watch("turnRight", "alt-d", "d-up")
  98. inputState.watch("turnRight", "control-alt-d", "d-up")
  99. inputState.watch("turnRight", "shift-d", "d-up")
  100. def add(self, controls, name="basic"):
  101. """
  102. controls is an avatar control system.
  103. name is any key that you want to use to refer to the
  104. the controls later (e.g. using the use(<name>) call).
  105. Add a control instance to the list of available control systems.
  106. See also: use().
  107. """
  108. assert self.notify.debugCall(id(self))
  109. assert controls is not None
  110. oldControls = self.controls.get(name)
  111. if oldControls is not None:
  112. print "Replacing controls:", name
  113. oldControls.disableAvatarControls()
  114. oldControls.setCollisionsActive(0)
  115. oldControls.delete()
  116. controls.disableAvatarControls()
  117. controls.setCollisionsActive(0)
  118. self.controls[name] = controls
  119. def remove(self, name):
  120. """
  121. name is any key that was used to refer to the
  122. the controls when they were added (e.g.
  123. using the add(<controls>, <name>) call).
  124. Remove a control instance from the list of available control systems.
  125. See also: add().
  126. """
  127. assert self.notify.debugCall(id(self))
  128. oldControls = self.controls.get(name)
  129. if oldControls is not None:
  130. print "Removing controls:", name
  131. oldControls.disableAvatarControls()
  132. oldControls.setCollisionsActive(0)
  133. oldControls.delete()
  134. del self.controls[name]
  135. if __debug__:
  136. def lockControls(self):
  137. self.ignoreUse=True
  138. def unlockControls(self):
  139. if hasattr(self, "ignoreUse"):
  140. del self.ignoreUse
  141. def use(self, name, avatar):
  142. """
  143. name is a key (string) that was previously passed to add().
  144. Use a previously added control system.
  145. See also: add().
  146. """
  147. assert self.notify.debugCall(id(self))
  148. if __debug__ and hasattr(self, "ignoreUse"):
  149. return
  150. controls = self.controls.get(name)
  151. if controls is not None:
  152. if controls is not self.currentControls:
  153. if self.currentControls is not None:
  154. self.currentControls.disableAvatarControls()
  155. self.currentControls.setCollisionsActive(0)
  156. self.currentControls.setAvatar(None)
  157. self.currentControls = controls
  158. self.currentControls.setAvatar(avatar)
  159. self.currentControls.setCollisionsActive(1)
  160. if self.isEnabled:
  161. self.currentControls.enableAvatarControls()
  162. messenger.send('use-%s-controls'%(name,), [avatar])
  163. #else:
  164. # print "Controls are already", name
  165. else:
  166. print "Unkown controls:", name
  167. def setSpeeds(self, forwardSpeed, jumpForce,
  168. reverseSpeed, rotateSpeed):
  169. assert self.notify.debugCall(id(self))
  170. for controls in self.controls.values():
  171. controls.setWalkSpeed(
  172. forwardSpeed, jumpForce, reverseSpeed, rotateSpeed)
  173. def delete(self):
  174. assert self.notify.debugCall(id(self))
  175. self.disable()
  176. del self.controls
  177. del self.currentControls
  178. inputState.ignore("forward")
  179. inputState.ignore("reverse")
  180. inputState.ignore("turnLeft")
  181. inputState.ignore("turnRight")
  182. inputState.ignore("jump")
  183. inputState.ignore("slideLeft")
  184. inputState.ignore("slideRight")
  185. inputState.ignore("levitateUp")
  186. inputState.ignore("levitateDown")
  187. inputState.ignore("run")
  188. inputState.ignore("slide")
  189. #self.monitorTask.remove()
  190. def getSpeeds(self):
  191. return self.currentControls.getSpeeds()
  192. def setTag(self, key, value):
  193. assert self.notify.debugCall(id(self))
  194. for controls in self.controls.values():
  195. controls.setTag(key, value)
  196. def deleteCollisions(self):
  197. assert self.notify.debugCall(id(self))
  198. for controls in self.controls.values():
  199. controls.deleteCollisions()
  200. def collisionsOn(self):
  201. assert self.notify.debugCall(id(self))
  202. self.currentControls.setCollisionsActive(1)
  203. def collisionsOff(self):
  204. assert self.notify.debugCall(id(self))
  205. self.currentControls.setCollisionsActive(0)
  206. def placeOnFloor(self):
  207. assert self.notify.debugCall(id(self))
  208. self.currentControls.placeOnFloor()
  209. def enable(self):
  210. assert self.notify.debugCall(id(self))
  211. self.isEnabled = 1
  212. self.currentControls.enableAvatarControls()
  213. def disable(self):
  214. assert self.notify.debugCall(id(self))
  215. self.isEnabled = 0
  216. self.currentControls.disableAvatarControls()
  217. def enableAvatarJump(self):
  218. """
  219. Stop forcing the ctrl key to return 0's
  220. """
  221. assert self.notify.debugCall(id(self))
  222. self.enableJumpCounter+=1
  223. if self.enableJumpCounter:
  224. assert self.enableJumpCounter == 1
  225. self.enableJumpCounter = 1
  226. inputState.unforce("jump")
  227. def disableAvatarJump(self):
  228. """
  229. Force the ctrl key to return 0's
  230. """
  231. assert self.notify.debugCall(id(self))
  232. self.enableJumpCounter-=1
  233. if self.enableJumpCounter <= 0:
  234. inputState.force("jump", 0)
  235. def monitor(self, foo):
  236. #assert(self.debugPrint("monitor()"))
  237. #if 1:
  238. # airborneHeight=self.avatar.getAirborneHeight()
  239. # onScreenDebug.add("airborneHeight", "% 10.4f"%(airborneHeight,))
  240. if 0:
  241. onScreenDebug.add("InputState forward", "%d"%(inputState.isSet("forward")))
  242. onScreenDebug.add("InputState reverse", "%d"%(inputState.isSet("reverse")))
  243. onScreenDebug.add("InputState turnLeft", "%d"%(inputState.isSet("turnLeft")))
  244. onScreenDebug.add("InputState turnRight", "%d"%(inputState.isSet("turnRight")))
  245. return Task.cont