InputState.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. from direct.directnotify import DirectNotifyGlobal
  2. from direct.showbase import DirectObject
  3. from direct.showbase.PythonUtil import SerialNumGen
  4. # internal class, don't create these on your own
  5. class InputStateToken:
  6. _SerialGen = SerialNumGen()
  7. Inval = 'invalidatedToken'
  8. def __init__(self, inputState):
  9. self._id = InputStateToken._SerialGen.next()
  10. self._hash = self._id
  11. self._inputState = inputState
  12. def release(self):
  13. # subclasses will override
  14. assert False
  15. def isValid(self):
  16. return self._id != InputStateToken.Inval
  17. def invalidate(self):
  18. self._id = InputStateToken.Inval
  19. def __hash__(self):
  20. return self._hash
  21. #snake_case alias:
  22. is_valid = isValid
  23. class InputStateWatchToken(InputStateToken, DirectObject.DirectObject):
  24. def release(self):
  25. self._inputState._ignore(self)
  26. self.ignoreAll()
  27. class InputStateForceToken(InputStateToken):
  28. def release(self):
  29. self._inputState._unforce(self)
  30. class InputStateTokenGroup:
  31. def __init__(self):
  32. self._tokens = []
  33. def addToken(self, token):
  34. self._tokens.append(token)
  35. def release(self):
  36. for token in self._tokens:
  37. token.release()
  38. self._tokens = []
  39. #snake_case alias:
  40. add_token = addToken
  41. class InputState(DirectObject.DirectObject):
  42. """
  43. InputState is for tracking the on/off state of some events.
  44. The initial usage is to watch some keyboard keys so that another
  45. task can poll the key states. By the way, in general polling is
  46. not a good idea, but it is useful in some situations. Know when
  47. to use it:) If in doubt, don't use this class and listen for
  48. events instead.
  49. """
  50. notify = DirectNotifyGlobal.directNotify.newCategory("InputState")
  51. # standard input sources
  52. WASD = 'WASD'
  53. QE = 'QE'
  54. ArrowKeys = 'ArrowKeys'
  55. Keyboard = 'Keyboard'
  56. Mouse = 'Mouse'
  57. def __init__(self):
  58. # stateName->set(SourceNames)
  59. self._state = {}
  60. # stateName->set(SourceNames)
  61. self._forcingOn = {}
  62. # stateName->set(SourceNames)
  63. self._forcingOff = {}
  64. # tables to look up the info needed to undo operations
  65. self._token2inputSource = {}
  66. self._token2forceInfo = {}
  67. # inputSource->token->(name, eventOn, eventOff)
  68. self._watching = {}
  69. assert self.debugPrint("InputState()")
  70. def delete(self):
  71. del self._watching
  72. del self._token2forceInfo
  73. del self._token2inputSource
  74. del self._forcingOff
  75. del self._forcingOn
  76. del self._state
  77. self.ignoreAll()
  78. def isSet(self, name, inputSource=None):
  79. """
  80. returns True/False
  81. """
  82. #assert self.debugPrint("isSet(name=%s)"%(name))
  83. if name in self._forcingOn:
  84. return True
  85. elif name in self._forcingOff:
  86. return False
  87. if inputSource:
  88. s = self._state.get(name)
  89. if s:
  90. return inputSource in s
  91. else:
  92. return False
  93. else:
  94. return name in self._state
  95. def getEventName(self, name):
  96. return "InputState-%s" % (name,)
  97. def set(self, name, isActive, inputSource=None):
  98. assert self.debugPrint("set(name=%s, isActive=%s, inputSource=%s)"%(name, isActive, inputSource))
  99. # inputSource is a string that identifies where this input change
  100. # is coming from (like 'WASD', 'ArrowKeys', etc.)
  101. # Each unique inputSource is allowed to influence this input item
  102. # once: it's either 'active' or 'not active'. If at least one source
  103. # activates this input item, the input item is considered to be active
  104. if inputSource is None:
  105. inputSource = 'anonymous'
  106. if isActive:
  107. self._state.setdefault(name, set())
  108. self._state[name].add(inputSource)
  109. else:
  110. if name in self._state:
  111. self._state[name].discard(inputSource)
  112. if len(self._state[name]) == 0:
  113. del self._state[name]
  114. # We change the name before sending it because this may
  115. # be the same name that messenger used to call InputState.set()
  116. # this avoids running in circles:
  117. messenger.send(self.getEventName(name), [self.isSet(name)])
  118. def releaseInputs(self, name):
  119. # call this to act as if all inputs affecting this state have been released
  120. del self._state[name]
  121. def watch(self, name, eventOn, eventOff, startState=False, inputSource=None):
  122. """
  123. This returns a token; hold onto the token and call token.release() when
  124. you no longer want to watch for these events.
  125. Example::
  126. # set up
  127. token = inputState.watch('forward', 'w', 'w-up', inputSource=inputState.WASD)
  128. ...
  129. # tear down
  130. token.release()
  131. """
  132. assert self.debugPrint(
  133. "watch(name=%s, eventOn=%s, eventOff=%s, startState=%s)"%(
  134. name, eventOn, eventOff, startState))
  135. if inputSource is None:
  136. inputSource = "eventPair('%s','%s')" % (eventOn, eventOff)
  137. # Do we really need to reset the input state just because
  138. # we're watching it? Remember, there may be multiple things
  139. # watching this input state.
  140. self.set(name, startState, inputSource)
  141. token = InputStateWatchToken(self)
  142. # make the token listen for the events, to allow multiple listeners for the same event
  143. token.accept(eventOn, self.set, [name, True, inputSource])
  144. token.accept(eventOff, self.set, [name, False, inputSource])
  145. self._token2inputSource[token] = inputSource
  146. self._watching.setdefault(inputSource, {})
  147. self._watching[inputSource][token] = (name, eventOn, eventOff)
  148. return token
  149. def watchWithModifiers(self, name, event, startState=False, inputSource=None):
  150. patterns = ('%s', 'control-%s', 'shift-control-%s', 'alt-%s',
  151. 'control-alt-%s', 'shift-%s', 'shift-alt-%s')
  152. tGroup = InputStateTokenGroup()
  153. for pattern in patterns:
  154. tGroup.addToken(self.watch(name, pattern % event, '%s-up' % event, startState=startState, inputSource=inputSource))
  155. return tGroup
  156. def _ignore(self, token):
  157. """
  158. Undo a watch(). Don't call this directly, call release() on the token that watch() returned.
  159. """
  160. inputSource = self._token2inputSource.pop(token)
  161. name, eventOn, eventOff = self._watching[inputSource].pop(token)
  162. token.invalidate()
  163. DirectObject.DirectObject.ignore(self, eventOn)
  164. DirectObject.DirectObject.ignore(self, eventOff)
  165. if len(self._watching[inputSource]) == 0:
  166. del self._watching[inputSource]
  167. # I commented this out because we shouldn't be modifying an
  168. # input state simply because we're not looking at it anymore.
  169. # self.set(name, False, inputSource)
  170. def force(self, name, value, inputSource):
  171. """
  172. Force isSet(name) to return 'value'.
  173. This returns a token; hold onto the token and call token.release() when
  174. you no longer want to force the state.
  175. Example::
  176. # set up
  177. token = inputState.force('forward', True, inputSource='myForwardForcer')
  178. ...
  179. # tear down
  180. token.release()
  181. """
  182. token = InputStateForceToken(self)
  183. self._token2forceInfo[token] = (name, inputSource)
  184. if value:
  185. if name in self._forcingOff:
  186. self.notify.error(
  187. "%s is trying to force '%s' to ON, but '%s' is already being forced OFF by %s" %
  188. (inputSource, name, name, self._forcingOff[name])
  189. )
  190. self._forcingOn.setdefault(name, set())
  191. self._forcingOn[name].add(inputSource)
  192. else:
  193. if name in self._forcingOn:
  194. self.notify.error(
  195. "%s is trying to force '%s' to OFF, but '%s' is already being forced ON by %s" %
  196. (inputSource, name, name, self._forcingOn[name])
  197. )
  198. self._forcingOff.setdefault(name, set())
  199. self._forcingOff[name].add(inputSource)
  200. return token
  201. def _unforce(self, token):
  202. """
  203. Stop forcing a value. Don't call this directly, call release() on your token.
  204. """
  205. name, inputSource = self._token2forceInfo[token]
  206. token.invalidate()
  207. if name in self._forcingOn:
  208. self._forcingOn[name].discard(inputSource)
  209. if len(self._forcingOn[name]) == 0:
  210. del self._forcingOn[name]
  211. if name in self._forcingOff:
  212. self._forcingOff[name].discard(inputSource)
  213. if len(self._forcingOff[name]) == 0:
  214. del self._forcingOff[name]
  215. def debugPrint(self, message):
  216. """for debugging"""
  217. return self.notify.debug(
  218. "%s (%s) %s"%(id(self), len(self._state), message))
  219. #snake_case alias:
  220. watch_with_modifiers = watchWithModifiers
  221. is_set = isSet
  222. get_event_name = getEventName
  223. debug_print = debugPrint
  224. release_inputs = releaseInputs