rxeventmanager.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. # rxeventmanager.py
  2. # print "rxeventmanager.................................."
  3. import sys
  4. import rxactor
  5. import rxevent
  6. import rxworld
  7. import rxavatar
  8. class EventManager(object):
  9. def __init__(self,vWorld):
  10. super(self.__class__,self).__init__()
  11. self.MyWorld = vWorld
  12. self.MyWorld.MyEventManager = self
  13. self.MyActiveListIndex = 0
  14. self.MyTickedActors = {}
  15. self.MyTimerList = []
  16. self.CurrTime = 0
  17. self.MyEventListA = []
  18. self.MyEventListB = []
  19. self.bShutDown = False
  20. # Register events
  21. self.MyEventClasses = {}
  22. self.MyEventClasses[rxevent.RexEventTouchStart.MyName] = getattr(self,"CreateEvent_"+rxevent.RexEventTouchStart.MyName),getattr(self,"HandleEvent_"+rxevent.RexEventTouchStart.MyName)
  23. self.MyEventClasses[rxevent.RexEventSetTimer.MyName] = getattr(self,"CreateEvent_"+rxevent.RexEventSetTimer.MyName),getattr(self,"HandleEvent_"+rxevent.RexEventSetTimer.MyName)
  24. self.MyEventClasses[rxevent.RexEventSetTick.MyName] = getattr(self,"CreateEvent_"+rxevent.RexEventSetTick.MyName),getattr(self,"HandleEvent_"+rxevent.RexEventSetTick.MyName)
  25. self.MyEventClasses[rxevent.RexEventAddEntity.MyName] = getattr(self,"CreateEvent_"+rxevent.RexEventAddEntity.MyName),getattr(self,"HandleEvent_"+rxevent.RexEventAddEntity.MyName)
  26. self.MyEventClasses[rxevent.RexEventRemoveEntity.MyName] = getattr(self,"CreateEvent_"+rxevent.RexEventRemoveEntity.MyName),getattr(self,"HandleEvent_"+rxevent.RexEventRemoveEntity.MyName)
  27. self.MyEventClasses[rxevent.RexEventAddPresence.MyName] = getattr(self,"CreateEvent_"+rxevent.RexEventAddPresence.MyName),getattr(self,"HandleEvent_"+rxevent.RexEventAddPresence.MyName)
  28. self.MyEventClasses[rxevent.RexEventRemovePresence.MyName] = getattr(self,"CreateEvent_"+rxevent.RexEventRemovePresence.MyName),getattr(self,"HandleEvent_"+rxevent.RexEventRemovePresence.MyName)
  29. self.MyEventClasses[rxevent.RexEventClientEvent.MyName] = getattr(self,"CreateEvent_"+rxevent.RexEventClientEvent.MyName),getattr(self,"HandleEvent_"+rxevent.RexEventClientEvent.MyName)
  30. self.MyEventClasses[rxevent.RexEventPrimVolumeCollision.MyName] = getattr(self,"CreateEvent_"+rxevent.RexEventPrimVolumeCollision.MyName),getattr(self,"HandleEvent_"+rxevent.RexEventPrimVolumeCollision.MyName)
  31. # RexEventTimer events are in their own list, so not added here.
  32. # Actors can register for receiving certain events
  33. self.EventNames = {}
  34. def ShutDown(self):
  35. try:
  36. self.bShutDown = True
  37. # Registered events
  38. self.MyEventClasses.clear()
  39. # Delete event lists
  40. #print "deleting MyEventListA A"
  41. while len(self.MyEventListA) > 0:
  42. TempEvent = self.MyEventListA.pop(0)
  43. del TempEvent
  44. #print "deleting MyEventListA B"
  45. while len(self.MyEventListB) > 0:
  46. TempEvent = self.MyEventListB.pop(0)
  47. del TempEvent
  48. # Delete timer event list
  49. #print "deleting Timerlist"
  50. while len(self.MyTimerList) > 0:
  51. TempEvent = self.MyTimerList.pop(0)
  52. del TempEvent
  53. # TickedActor list, clear list only
  54. #print "deleting MyTickedActors"
  55. while len(self.MyTickedActors) > 0:
  56. TempKey = self.MyTickedActors.keys()[0]
  57. TempActor = self.MyTickedActors.pop(TempKey)
  58. del TempActor
  59. #if len(self.MyTickedActors) > 0:
  60. # del self.MyTickedActors[:]
  61. #while len(self.MyTickedActors) > 0:
  62. # TempActor = self.MyTickedActors.pop(0)
  63. # Avatar list, clear list only
  64. #print "deleting AllAvatars"
  65. while len(self.MyWorld.AllAvatars) > 0:
  66. TempKey = self.MyWorld.AllAvatars.keys()[0]
  67. TempActor = self.MyWorld.AllAvatars.pop(TempKey)
  68. del TempActor
  69. #while len(self.MyWorld.AllAvatars) > 0:
  70. # TempActor = self.MyWorld.AllAvatars.pop()
  71. # Actor list
  72. # print "deleting AllActors"
  73. while len(self.MyWorld.AllActors) > 0:
  74. TempKey = self.MyWorld.AllActors.keys()[0]
  75. self.DeleteActor(TempKey)
  76. # Eventnames list
  77. while len(self.EventNames) > 0:
  78. TempKey = self.EventNames.keys()[0]
  79. TempList = self.EventNames.pop(TempKey)
  80. del TempList
  81. # print "Settings lists to none"
  82. self.MyEventClasses = None
  83. self.MyEventListA = None
  84. self.MyEventListB = None
  85. self.EventNames = None
  86. self.MyTimerList = None
  87. self.MyTickedActors = None
  88. except:
  89. print "EventManager,shutDown", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
  90. def CreateEventWithName(self,vName,*args):
  91. try:
  92. if self.bShutDown:
  93. return;
  94. TempEvent = None
  95. if(self.MyEventClasses.has_key(vName)):
  96. eventfuncdata = self.MyEventClasses[vName]
  97. TempEvent = eventfuncdata[0](*args)
  98. else:
  99. print "rxeventmanager,CreateEventWithName, no event named",vName
  100. if TempEvent != None:
  101. if self.MyActiveListIndex == 0:
  102. self.MyEventListB.append(TempEvent);
  103. else:
  104. self.MyEventListA.append(TempEvent);
  105. except:
  106. print "rxeventmanager,EventManager,CreateEventWithName", sys.exc_info()[0]
  107. print sys.exc_info()[1]
  108. print sys.exc_info()[2]
  109. def SwitchActiveList(self):
  110. if self.MyActiveListIndex == 0:
  111. self.MyActiveListIndex = 1
  112. else:
  113. self.MyActiveListIndex = 0
  114. def ProcessEvents(self,vDeltaTime):
  115. while True:
  116. try:
  117. TempEvent = None
  118. if self.MyActiveListIndex == 0:
  119. if len(self.MyEventListA) == 0:
  120. break
  121. else:
  122. TempEvent = self.MyEventListA.pop(0)
  123. else:
  124. if len(self.MyEventListB) == 0:
  125. break
  126. else:
  127. TempEvent = self.MyEventListB.pop(0)
  128. # What to do with events
  129. if(self.MyEventClasses.has_key(TempEvent.MyName)):
  130. eventfuncdata = self.MyEventClasses[TempEvent.MyName]
  131. eventfuncdata[1](TempEvent)
  132. else:
  133. print "Unknown event not processed",TempEvent.MyName
  134. del TempEvent
  135. except:
  136. print "rxeventmanager,ProcessEvents:", sys.exc_info()[0]
  137. print sys.exc_info()[1]
  138. print sys.exc_info()[2]
  139. def CallTimeEvents(self,vDeltaTime):
  140. try:
  141. # Tick
  142. for TempId,TempA in self.MyTickedActors.iteritems():
  143. try:
  144. TempA.EventTick(vDeltaTime)
  145. except:
  146. print "rxeventmanager,CallTimeEvents,tick:", sys.exc_info()[0]
  147. print sys.exc_info()[1]
  148. print sys.exc_info()[2]
  149. # Timer
  150. while len(self.MyTimerList) > 0:
  151. TObj = self.MyTimerList[0]
  152. TempActor = None
  153. if TObj.TTime <= self.CurrTime:
  154. TObj = self.MyTimerList.pop(0)
  155. if self.MyWorld.AllActors.has_key(TObj.ObjectId):
  156. try:
  157. TempActor = self.MyWorld.AllActors[TObj.ObjectId]
  158. TempActor.EventTimer()
  159. except:
  160. print "rxeventmanager,CallTimeEvents,timer:", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
  161. else:
  162. print "No Actor for timer event",TObj.ObjectId
  163. if TempActor != None:
  164. if TempActor.bTimerLoop and TempActor.MyTimerCount > 0:
  165. self.CreateTimerEventForLooping(TempActor)
  166. #self.SetTimerForActor(TempActor,TempActor.MyTimerCount,TempActor.bTimerLoop)
  167. else:
  168. TempActor.MyTimerCount = 0
  169. TempActor.bTimerLoop = False
  170. del TObj
  171. else:
  172. break
  173. except:
  174. print "CallTimeEvents", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
  175. def CreateTimerEventForLooping(self,vActor):
  176. try:
  177. TempObj = rxevent.RexEventTimer(vActor.Id,self.CurrTime + vActor.MyTimerCount,vActor.bTimerLoop)
  178. index = 0
  179. for i in self.MyTimerList:
  180. if TempObj.TTime < i.TTime:
  181. self.MyTimerList.insert(index,TempObj)
  182. del TempObj
  183. return
  184. else:
  185. index += 1
  186. # Insert as last
  187. self.MyTimerList.append(TempObj)
  188. del TempObj
  189. except:
  190. print "CreateTimerEventForLooping", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
  191. # Handle events
  192. # **********************************************************
  193. def CreateEvent_touch_start(self,*args):
  194. return rxevent.RexEventTouchStart(*args)
  195. def HandleEvent_touch_start(self,vEvent):
  196. try:
  197. if self.MyWorld.AllActors.has_key(vEvent.ObjectId):
  198. TempActor = self.MyWorld.AllActors[vEvent.ObjectId]
  199. if self.MyWorld.AllAvatars.has_key(vEvent.AgentId):
  200. TempActor.EventTouch(self.MyWorld.AllAvatars[vEvent.AgentId])
  201. else:
  202. print "MISSING AVATAR FOR TOUCH",vEvent.AgentId
  203. else:
  204. print "TOUCH EVENT FOR MISSING ACTOR",vEvent.ObjectId,vEvent.AgentId
  205. except:
  206. print "HandleEvent_TouchStart", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
  207. def CreateEvent_add_presence(self,*args):
  208. return rxevent.RexEventAddPresence(*args)
  209. def HandleEvent_add_presence(self,vEvent):
  210. try:
  211. # If already on list, leave there.
  212. if self.MyWorld.AllAvatars.has_key(vEvent.ObjectId):
  213. print "HandleEvent_AddPresence, avatar is already on avatarlist",vEvent.AgentId
  214. return
  215. TempAvatar = rxavatar.Avatar(vEvent.ObjectId)
  216. TempAvatar.AgentId = vEvent.AgentId
  217. self.MyWorld.AllAvatars[vEvent.AgentId] = TempAvatar
  218. self.AddActor(TempAvatar)
  219. except:
  220. print "HandleEvent_AddPresence", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
  221. def CreateEvent_remove_presence(self,*args):
  222. return rxevent.RexEventRemovePresence(*args)
  223. def HandleEvent_remove_presence(self,vEvent):
  224. try:
  225. if self.MyWorld.AllAvatars.has_key(vEvent.AgentId):
  226. TempAgent = self.MyWorld.AllAvatars.pop(vEvent.AgentId)
  227. AgentObjId = TempAgent.Id
  228. del TempAgent
  229. self.DeleteActor(AgentObjId)
  230. else:
  231. print "HandleEvent_RemovePresence, avatar not on avatarlist",vEvent.AgentId
  232. except:
  233. print "HandleEvent_RemovePresence", sys.exc_info()[0]
  234. print sys.exc_info()[1]
  235. print sys.exc_info()[2]
  236. def CreateEvent_add_entity(self,*args):
  237. return rxevent.RexEventAddEntity(*args)
  238. def HandleEvent_add_entity(self,vEvent):
  239. try:
  240. if self.MyWorld.AllActors.has_key(str(vEvent.ObjectId)):
  241. TempActor = self.MyWorld.AllActors[str(vEvent.ObjectId)]
  242. TempActor.EventPreCreated()
  243. TempActor.EventCreated()
  244. else:
  245. print "HandleEvent_AddEntity trying to be used the old way, actor not yet created."
  246. except:
  247. print "HandleEvent_AddEntity", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
  248. def CreateEvent_remove_entity(self,*args):
  249. return rxevent.RexEventRemoveEntity(*args)
  250. def HandleEvent_remove_entity(self,vEvent):
  251. try:
  252. if not self.DeleteActor(vEvent.ObjectId):
  253. print "HandleEvent_RemoveEntity, no entity found with id:",vEvent.ObjectId
  254. except:
  255. print "HandleEvent_RemoveEntity", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
  256. def CreateEvent_set_timer(self,*args):
  257. return rxevent.RexEventSetTimer(*args)
  258. def HandleEvent_set_timer(self,vEvent):
  259. try:
  260. # If has previous time on list, remove it!
  261. for i in self.MyTimerList:
  262. if i.ObjectId == vEvent.ObjectId:
  263. TempObj = self.MyTimerList.remove(i)
  264. del TempObj
  265. break
  266. if not self.MyWorld.AllActors.has_key(vEvent.ObjectId):
  267. #print "HandleEvent_SetTimer, actor not found ",vEvent.ObjectId
  268. return
  269. TempActor = self.MyWorld.AllActors[vEvent.ObjectId]
  270. TempActor.MyTimerCount = vEvent.TTime
  271. TempActor.bTimerLoop = vEvent.bLoop
  272. if vEvent.TTime <= 0:
  273. return;
  274. TempObj = rxevent.RexEventTimer(vEvent.ObjectId,self.CurrTime + vEvent.TTime,vEvent.bLoop)
  275. index = 0
  276. for i in self.MyTimerList:
  277. if TempObj.TTime < i.TTime:
  278. self.MyTimerList.insert(index,TempObj)
  279. del TempObj
  280. return
  281. else:
  282. index += 1
  283. # Insert as last
  284. self.MyTimerList.append(TempObj)
  285. del TempObj
  286. except:
  287. print "HandleEvent_SetTimer", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
  288. def CreateEvent_set_tick(self,*args):
  289. return rxevent.RexEventSetTick(*args)
  290. def HandleEvent_set_tick(self,vEvent):
  291. try:
  292. if vEvent.bTick:
  293. if not self.MyWorld.AllActors.has_key(vEvent.ObjectId):
  294. print "HandleEvent_SetTick, actor not found ",vEvent.ObjectId
  295. return
  296. TempActor = self.MyWorld.AllActors[vEvent.ObjectId]
  297. self.MyTickedActors[vEvent.ObjectId] = TempActor
  298. else:
  299. if self.MyTickedActors.has_key(vEvent.ObjectId):
  300. self.MyTickedActors.pop(vEvent.ObjectId)
  301. except:
  302. print "HandleEvent_SetTick", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
  303. def CreateEvent_client_event(self,*args):
  304. return rxevent.RexEventClientEvent(*args)
  305. # Client event which was sent by client
  306. # param 0 is avatarid (lluuid)
  307. # param 1 is command (str)
  308. def HandleEvent_client_event(self,vEvent):
  309. try:
  310. if self.MyWorld.AllAvatars.has_key(vEvent.AgentId):
  311. TempAgent = self.MyWorld.AllAvatars[vEvent.AgentId]
  312. else:
  313. print "HandleEvent_ClientEvent, avatar not on avatarlist",vEvent.AgentId
  314. return
  315. command = str(vEvent.Params[1])
  316. # Left mouse button pressed
  317. if(command == "lmb"):
  318. TempAgent.EventLeftMouseButtonPressed(TempAgent)
  319. if(self.EventNames.has_key(command)):
  320. templist = self.EventNames[command]
  321. for a in templist:
  322. a.EventLeftMouseButtonPressed(TempAgent)
  323. # Right mouse button pressed
  324. elif(command == "rmb"):
  325. TempAgent.EventRightMouseButtonPressed(TempAgent)
  326. if(self.EventNames.has_key(command)):
  327. templist = self.EventNames[command]
  328. for a in templist:
  329. a.EventRightMouseButtonPressed(TempAgent)
  330. # Mouse wheel
  331. elif(command == "mw"):
  332. TempAgent.EventMouseWheel(TempAgent,str(vEvent.Params[2]))
  333. if(self.EventNames.has_key(command)):
  334. templist = self.EventNames[command]
  335. for a in templist:
  336. a.EventMouseWheel(TempAgent,str(vEvent.Params[2]))
  337. else:
  338. print "HandleEvent_ClientEvent, unhandled command:",command + " from " + TempAgent.GetFullName()
  339. except:
  340. print "HandleEvent_ClientEvent", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
  341. def CreateEvent_primvol_col(self,*args):
  342. return rxevent.RexEventPrimVolumeCollision(*args)
  343. def HandleEvent_primvol_col(self,vEvent):
  344. try:
  345. if self.MyWorld.AllActors.has_key(vEvent.ObjectId):
  346. TempActor = self.MyWorld.AllActors[vEvent.ObjectId]
  347. if self.MyWorld.AllActors.has_key(vEvent.ColliderId):
  348. TempActor.EventPrimVolumeCollision(self.MyWorld.AllActors[vEvent.ColliderId])
  349. else:
  350. print "MISSING ACTOR FOR PRIMVOLUMECOLLISION",vEvent.ColliderId
  351. else:
  352. print "PRIMVOLUMECOLLISION EVENT FOR MISSING ACTOR",vEvent.ObjectId,vEvent.ColliderId
  353. except:
  354. print "HandleEvent_PrimVolumeCollision", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
  355. def AddActor(self,vActor):
  356. try:
  357. if self.MyWorld.AllActors.has_key(vActor.Id):
  358. print "AddActor, actor already on list",vActor.Id
  359. return False
  360. vActor.MyWorld = self.MyWorld
  361. self.MyWorld.AllActors[vActor.Id] = vActor
  362. vActor.EventPreCreated()
  363. vActor.EventCreated()
  364. return True
  365. except:
  366. print "AddActor", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
  367. def DeleteActor(self,vId):
  368. try:
  369. if self.MyWorld.AllActors.has_key(vId):
  370. TempActor = self.MyWorld.AllActors.pop(vId)
  371. TempActor.SetTimer(0,False)
  372. TempActor.DisableTick()
  373. self.UnSubsribeAll(TempActor)
  374. TempActor.EventDestroyed()
  375. del TempActor
  376. return True
  377. else:
  378. print "DeleteActor, no actor found with id:",vId
  379. return False
  380. except:
  381. print "DeleteActor", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
  382. return False
  383. # Actors subscribing for global events
  384. # ****************************************************************
  385. def SubsribeToEvent(self,vEvent,vActor):
  386. try:
  387. if(not self.EventNames.has_key(vEvent)):
  388. self.EventNames[vEvent] = []
  389. templist = self.EventNames[vEvent]
  390. templist.append(vActor)
  391. except:
  392. print "SubsribeToEvent", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
  393. def UnSubsribeToEvent(self,vEvent,vActor):
  394. try:
  395. if(self.EventNames.has_key(vEvent)):
  396. templist = self.EventNames[vEvent]
  397. templist.remove(vActor)
  398. else:
  399. print "Trying to unsubsrive to missing eventname:",vEvent
  400. except:
  401. print "UnSubsribeToEvent", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
  402. def UnSubsribeAll(self,vActor):
  403. try:
  404. for ename, elist in self.EventNames.iteritems():
  405. try:
  406. elist.remove(vActor)
  407. except:
  408. pass
  409. except:
  410. print "UnSubsribeAll", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
  411. def EnableTickForActor(self,vActor):
  412. self.CreateEventWithName("set_tick",vActor.Id,True)
  413. def DisableTickForActor(self,vActor):
  414. self.CreateEventWithName("set_tick",vActor.Id,False)
  415. def SetTimerForActor(self,vActor,vTime,vbLoop):
  416. self.CreateEventWithName("set_timer",vActor.Id,vTime,vbLoop)
  417. def PrintActorList(self):
  418. print "Printing actor list..."
  419. for iid, iactor in self.MyWorld.AllActors.iteritems():
  420. print iid,iactor