123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529 |
- # rxeventmanager.py
- # print "rxeventmanager.................................."
- import sys
- import rxactor
- import rxevent
- import rxworld
- import rxavatar
- class EventManager(object):
- def __init__(self,vWorld):
- super(self.__class__,self).__init__()
- self.MyWorld = vWorld
- self.MyWorld.MyEventManager = self
-
- self.MyActiveListIndex = 0
- self.MyTickedActors = {}
- self.MyTimerList = []
- self.CurrTime = 0
- self.MyEventListA = []
- self.MyEventListB = []
- self.bShutDown = False
- # Register events
- self.MyEventClasses = {}
- self.MyEventClasses[rxevent.RexEventTouchStart.MyName] = getattr(self,"CreateEvent_"+rxevent.RexEventTouchStart.MyName),getattr(self,"HandleEvent_"+rxevent.RexEventTouchStart.MyName)
- self.MyEventClasses[rxevent.RexEventSetTimer.MyName] = getattr(self,"CreateEvent_"+rxevent.RexEventSetTimer.MyName),getattr(self,"HandleEvent_"+rxevent.RexEventSetTimer.MyName)
- self.MyEventClasses[rxevent.RexEventSetTick.MyName] = getattr(self,"CreateEvent_"+rxevent.RexEventSetTick.MyName),getattr(self,"HandleEvent_"+rxevent.RexEventSetTick.MyName)
- self.MyEventClasses[rxevent.RexEventAddEntity.MyName] = getattr(self,"CreateEvent_"+rxevent.RexEventAddEntity.MyName),getattr(self,"HandleEvent_"+rxevent.RexEventAddEntity.MyName)
- self.MyEventClasses[rxevent.RexEventRemoveEntity.MyName] = getattr(self,"CreateEvent_"+rxevent.RexEventRemoveEntity.MyName),getattr(self,"HandleEvent_"+rxevent.RexEventRemoveEntity.MyName)
- self.MyEventClasses[rxevent.RexEventAddPresence.MyName] = getattr(self,"CreateEvent_"+rxevent.RexEventAddPresence.MyName),getattr(self,"HandleEvent_"+rxevent.RexEventAddPresence.MyName)
- self.MyEventClasses[rxevent.RexEventRemovePresence.MyName] = getattr(self,"CreateEvent_"+rxevent.RexEventRemovePresence.MyName),getattr(self,"HandleEvent_"+rxevent.RexEventRemovePresence.MyName)
- self.MyEventClasses[rxevent.RexEventClientEvent.MyName] = getattr(self,"CreateEvent_"+rxevent.RexEventClientEvent.MyName),getattr(self,"HandleEvent_"+rxevent.RexEventClientEvent.MyName)
- self.MyEventClasses[rxevent.RexEventPrimVolumeCollision.MyName] = getattr(self,"CreateEvent_"+rxevent.RexEventPrimVolumeCollision.MyName),getattr(self,"HandleEvent_"+rxevent.RexEventPrimVolumeCollision.MyName)
- # RexEventTimer events are in their own list, so not added here.
- # Actors can register for receiving certain events
- self.EventNames = {}
- def ShutDown(self):
- try:
- self.bShutDown = True
-
- # Registered events
- self.MyEventClasses.clear()
-
- # Delete event lists
- #print "deleting MyEventListA A"
- while len(self.MyEventListA) > 0:
- TempEvent = self.MyEventListA.pop(0)
- del TempEvent
- #print "deleting MyEventListA B"
- while len(self.MyEventListB) > 0:
- TempEvent = self.MyEventListB.pop(0)
- del TempEvent
- # Delete timer event list
- #print "deleting Timerlist"
- while len(self.MyTimerList) > 0:
- TempEvent = self.MyTimerList.pop(0)
- del TempEvent
-
- # TickedActor list, clear list only
- #print "deleting MyTickedActors"
- while len(self.MyTickedActors) > 0:
- TempKey = self.MyTickedActors.keys()[0]
- TempActor = self.MyTickedActors.pop(TempKey)
- del TempActor
-
- #if len(self.MyTickedActors) > 0:
- # del self.MyTickedActors[:]
- #while len(self.MyTickedActors) > 0:
- # TempActor = self.MyTickedActors.pop(0)
-
- # Avatar list, clear list only
- #print "deleting AllAvatars"
- while len(self.MyWorld.AllAvatars) > 0:
- TempKey = self.MyWorld.AllAvatars.keys()[0]
- TempActor = self.MyWorld.AllAvatars.pop(TempKey)
- del TempActor
- #while len(self.MyWorld.AllAvatars) > 0:
- # TempActor = self.MyWorld.AllAvatars.pop()
- # Actor list
- # print "deleting AllActors"
- while len(self.MyWorld.AllActors) > 0:
- TempKey = self.MyWorld.AllActors.keys()[0]
- self.DeleteActor(TempKey)
- # Eventnames list
- while len(self.EventNames) > 0:
- TempKey = self.EventNames.keys()[0]
- TempList = self.EventNames.pop(TempKey)
- del TempList
- # print "Settings lists to none"
- self.MyEventClasses = None
- self.MyEventListA = None
- self.MyEventListB = None
- self.EventNames = None
- self.MyTimerList = None
- self.MyTickedActors = None
- except:
- print "EventManager,shutDown", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
-
- def CreateEventWithName(self,vName,*args):
- try:
- if self.bShutDown:
- return;
-
- TempEvent = None
-
- if(self.MyEventClasses.has_key(vName)):
- eventfuncdata = self.MyEventClasses[vName]
- TempEvent = eventfuncdata[0](*args)
- else:
- print "rxeventmanager,CreateEventWithName, no event named",vName
- if TempEvent != None:
- if self.MyActiveListIndex == 0:
- self.MyEventListB.append(TempEvent);
- else:
- self.MyEventListA.append(TempEvent);
- except:
- print "rxeventmanager,EventManager,CreateEventWithName", sys.exc_info()[0]
- print sys.exc_info()[1]
- print sys.exc_info()[2]
- def SwitchActiveList(self):
- if self.MyActiveListIndex == 0:
- self.MyActiveListIndex = 1
- else:
- self.MyActiveListIndex = 0
- def ProcessEvents(self,vDeltaTime):
- while True:
- try:
- TempEvent = None
-
- if self.MyActiveListIndex == 0:
- if len(self.MyEventListA) == 0:
- break
- else:
- TempEvent = self.MyEventListA.pop(0)
- else:
- if len(self.MyEventListB) == 0:
- break
- else:
- TempEvent = self.MyEventListB.pop(0)
- # What to do with events
- if(self.MyEventClasses.has_key(TempEvent.MyName)):
- eventfuncdata = self.MyEventClasses[TempEvent.MyName]
- eventfuncdata[1](TempEvent)
- else:
- print "Unknown event not processed",TempEvent.MyName
- del TempEvent
- except:
- print "rxeventmanager,ProcessEvents:", sys.exc_info()[0]
- print sys.exc_info()[1]
- print sys.exc_info()[2]
- def CallTimeEvents(self,vDeltaTime):
- try:
- # Tick
- for TempId,TempA in self.MyTickedActors.iteritems():
- try:
- TempA.EventTick(vDeltaTime)
- except:
- print "rxeventmanager,CallTimeEvents,tick:", sys.exc_info()[0]
- print sys.exc_info()[1]
- print sys.exc_info()[2]
- # Timer
- while len(self.MyTimerList) > 0:
- TObj = self.MyTimerList[0]
- TempActor = None
- if TObj.TTime <= self.CurrTime:
- TObj = self.MyTimerList.pop(0)
- if self.MyWorld.AllActors.has_key(TObj.ObjectId):
- try:
- TempActor = self.MyWorld.AllActors[TObj.ObjectId]
- TempActor.EventTimer()
- except:
- print "rxeventmanager,CallTimeEvents,timer:", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
- else:
- print "No Actor for timer event",TObj.ObjectId
- if TempActor != None:
- if TempActor.bTimerLoop and TempActor.MyTimerCount > 0:
- self.CreateTimerEventForLooping(TempActor)
- #self.SetTimerForActor(TempActor,TempActor.MyTimerCount,TempActor.bTimerLoop)
- else:
- TempActor.MyTimerCount = 0
- TempActor.bTimerLoop = False
- del TObj
- else:
- break
- except:
- print "CallTimeEvents", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
- def CreateTimerEventForLooping(self,vActor):
- try:
- TempObj = rxevent.RexEventTimer(vActor.Id,self.CurrTime + vActor.MyTimerCount,vActor.bTimerLoop)
- index = 0
- for i in self.MyTimerList:
- if TempObj.TTime < i.TTime:
- self.MyTimerList.insert(index,TempObj)
- del TempObj
- return
- else:
- index += 1
- # Insert as last
- self.MyTimerList.append(TempObj)
- del TempObj
- except:
- print "CreateTimerEventForLooping", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
-
- # Handle events
- # **********************************************************
- def CreateEvent_touch_start(self,*args):
- return rxevent.RexEventTouchStart(*args)
- def HandleEvent_touch_start(self,vEvent):
- try:
- if self.MyWorld.AllActors.has_key(vEvent.ObjectId):
- TempActor = self.MyWorld.AllActors[vEvent.ObjectId]
- if self.MyWorld.AllAvatars.has_key(vEvent.AgentId):
- TempActor.EventTouch(self.MyWorld.AllAvatars[vEvent.AgentId])
- else:
- print "MISSING AVATAR FOR TOUCH",vEvent.AgentId
- else:
- print "TOUCH EVENT FOR MISSING ACTOR",vEvent.ObjectId,vEvent.AgentId
- except:
- print "HandleEvent_TouchStart", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
- def CreateEvent_add_presence(self,*args):
- return rxevent.RexEventAddPresence(*args)
- def HandleEvent_add_presence(self,vEvent):
- try:
- # If already on list, leave there.
- if self.MyWorld.AllAvatars.has_key(vEvent.ObjectId):
- print "HandleEvent_AddPresence, avatar is already on avatarlist",vEvent.AgentId
- return
- TempAvatar = rxavatar.Avatar(vEvent.ObjectId)
- TempAvatar.AgentId = vEvent.AgentId
-
- self.MyWorld.AllAvatars[vEvent.AgentId] = TempAvatar
- self.AddActor(TempAvatar)
- except:
- print "HandleEvent_AddPresence", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
- def CreateEvent_remove_presence(self,*args):
- return rxevent.RexEventRemovePresence(*args)
-
- def HandleEvent_remove_presence(self,vEvent):
- try:
- if self.MyWorld.AllAvatars.has_key(vEvent.AgentId):
- TempAgent = self.MyWorld.AllAvatars.pop(vEvent.AgentId)
- AgentObjId = TempAgent.Id
- del TempAgent
- self.DeleteActor(AgentObjId)
- else:
- print "HandleEvent_RemovePresence, avatar not on avatarlist",vEvent.AgentId
- except:
- print "HandleEvent_RemovePresence", sys.exc_info()[0]
- print sys.exc_info()[1]
- print sys.exc_info()[2]
- def CreateEvent_add_entity(self,*args):
- return rxevent.RexEventAddEntity(*args)
- def HandleEvent_add_entity(self,vEvent):
- try:
- if self.MyWorld.AllActors.has_key(str(vEvent.ObjectId)):
- TempActor = self.MyWorld.AllActors[str(vEvent.ObjectId)]
- TempActor.EventPreCreated()
- TempActor.EventCreated()
- else:
- print "HandleEvent_AddEntity trying to be used the old way, actor not yet created."
- except:
- print "HandleEvent_AddEntity", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
- def CreateEvent_remove_entity(self,*args):
- return rxevent.RexEventRemoveEntity(*args)
-
- def HandleEvent_remove_entity(self,vEvent):
- try:
- if not self.DeleteActor(vEvent.ObjectId):
- print "HandleEvent_RemoveEntity, no entity found with id:",vEvent.ObjectId
- except:
- print "HandleEvent_RemoveEntity", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
- def CreateEvent_set_timer(self,*args):
- return rxevent.RexEventSetTimer(*args)
-
- def HandleEvent_set_timer(self,vEvent):
- try:
- # If has previous time on list, remove it!
- for i in self.MyTimerList:
- if i.ObjectId == vEvent.ObjectId:
- TempObj = self.MyTimerList.remove(i)
- del TempObj
- break
- if not self.MyWorld.AllActors.has_key(vEvent.ObjectId):
- #print "HandleEvent_SetTimer, actor not found ",vEvent.ObjectId
- return
- TempActor = self.MyWorld.AllActors[vEvent.ObjectId]
- TempActor.MyTimerCount = vEvent.TTime
- TempActor.bTimerLoop = vEvent.bLoop
- if vEvent.TTime <= 0:
- return;
- TempObj = rxevent.RexEventTimer(vEvent.ObjectId,self.CurrTime + vEvent.TTime,vEvent.bLoop)
- index = 0
- for i in self.MyTimerList:
- if TempObj.TTime < i.TTime:
- self.MyTimerList.insert(index,TempObj)
- del TempObj
- return
- else:
- index += 1
- # Insert as last
- self.MyTimerList.append(TempObj)
- del TempObj
- except:
- print "HandleEvent_SetTimer", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
- def CreateEvent_set_tick(self,*args):
- return rxevent.RexEventSetTick(*args)
- def HandleEvent_set_tick(self,vEvent):
- try:
- if vEvent.bTick:
- if not self.MyWorld.AllActors.has_key(vEvent.ObjectId):
- print "HandleEvent_SetTick, actor not found ",vEvent.ObjectId
- return
- TempActor = self.MyWorld.AllActors[vEvent.ObjectId]
- self.MyTickedActors[vEvent.ObjectId] = TempActor
- else:
- if self.MyTickedActors.has_key(vEvent.ObjectId):
- self.MyTickedActors.pop(vEvent.ObjectId)
- except:
- print "HandleEvent_SetTick", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
- def CreateEvent_client_event(self,*args):
- return rxevent.RexEventClientEvent(*args)
- # Client event which was sent by client
- # param 0 is avatarid (lluuid)
- # param 1 is command (str)
- def HandleEvent_client_event(self,vEvent):
- try:
- if self.MyWorld.AllAvatars.has_key(vEvent.AgentId):
- TempAgent = self.MyWorld.AllAvatars[vEvent.AgentId]
- else:
- print "HandleEvent_ClientEvent, avatar not on avatarlist",vEvent.AgentId
- return
- command = str(vEvent.Params[1])
- # Left mouse button pressed
- if(command == "lmb"):
- TempAgent.EventLeftMouseButtonPressed(TempAgent)
- if(self.EventNames.has_key(command)):
- templist = self.EventNames[command]
- for a in templist:
- a.EventLeftMouseButtonPressed(TempAgent)
- # Right mouse button pressed
- elif(command == "rmb"):
- TempAgent.EventRightMouseButtonPressed(TempAgent)
- if(self.EventNames.has_key(command)):
- templist = self.EventNames[command]
- for a in templist:
- a.EventRightMouseButtonPressed(TempAgent)
- # Mouse wheel
- elif(command == "mw"):
- TempAgent.EventMouseWheel(TempAgent,str(vEvent.Params[2]))
- if(self.EventNames.has_key(command)):
- templist = self.EventNames[command]
- for a in templist:
- a.EventMouseWheel(TempAgent,str(vEvent.Params[2]))
- else:
- print "HandleEvent_ClientEvent, unhandled command:",command + " from " + TempAgent.GetFullName()
- except:
- print "HandleEvent_ClientEvent", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
- def CreateEvent_primvol_col(self,*args):
- return rxevent.RexEventPrimVolumeCollision(*args)
- def HandleEvent_primvol_col(self,vEvent):
- try:
- if self.MyWorld.AllActors.has_key(vEvent.ObjectId):
- TempActor = self.MyWorld.AllActors[vEvent.ObjectId]
- if self.MyWorld.AllActors.has_key(vEvent.ColliderId):
- TempActor.EventPrimVolumeCollision(self.MyWorld.AllActors[vEvent.ColliderId])
- else:
- print "MISSING ACTOR FOR PRIMVOLUMECOLLISION",vEvent.ColliderId
- else:
- print "PRIMVOLUMECOLLISION EVENT FOR MISSING ACTOR",vEvent.ObjectId,vEvent.ColliderId
- except:
- print "HandleEvent_PrimVolumeCollision", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
- def AddActor(self,vActor):
- try:
- if self.MyWorld.AllActors.has_key(vActor.Id):
- print "AddActor, actor already on list",vActor.Id
- return False
- vActor.MyWorld = self.MyWorld
- self.MyWorld.AllActors[vActor.Id] = vActor
- vActor.EventPreCreated()
- vActor.EventCreated()
- return True
- except:
- print "AddActor", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
-
- def DeleteActor(self,vId):
- try:
- if self.MyWorld.AllActors.has_key(vId):
- TempActor = self.MyWorld.AllActors.pop(vId)
- TempActor.SetTimer(0,False)
- TempActor.DisableTick()
- self.UnSubsribeAll(TempActor)
- TempActor.EventDestroyed()
- del TempActor
- return True
- else:
- print "DeleteActor, no actor found with id:",vId
- return False
- except:
- print "DeleteActor", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
- return False
- # Actors subscribing for global events
- # ****************************************************************
- def SubsribeToEvent(self,vEvent,vActor):
- try:
- if(not self.EventNames.has_key(vEvent)):
- self.EventNames[vEvent] = []
- templist = self.EventNames[vEvent]
- templist.append(vActor)
- except:
- print "SubsribeToEvent", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
- def UnSubsribeToEvent(self,vEvent,vActor):
- try:
- if(self.EventNames.has_key(vEvent)):
- templist = self.EventNames[vEvent]
- templist.remove(vActor)
- else:
- print "Trying to unsubsrive to missing eventname:",vEvent
- except:
- print "UnSubsribeToEvent", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
- def UnSubsribeAll(self,vActor):
- try:
- for ename, elist in self.EventNames.iteritems():
- try:
- elist.remove(vActor)
- except:
- pass
- except:
- print "UnSubsribeAll", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
- def EnableTickForActor(self,vActor):
- self.CreateEventWithName("set_tick",vActor.Id,True)
-
- def DisableTickForActor(self,vActor):
- self.CreateEventWithName("set_tick",vActor.Id,False)
- def SetTimerForActor(self,vActor,vTime,vbLoop):
- self.CreateEventWithName("set_timer",vActor.Id,vTime,vbLoop)
- def PrintActorList(self):
- print "Printing actor list..."
- for iid, iactor in self.MyWorld.AllActors.iteritems():
- print iid,iactor
-
|