def testEngineCanHandleMouseEvents(self): """testEngineCanHandleMouseEvents: engine should be able to handle mouse events""" self.a1.setSpatial(50, 100, 51, 30) self.a2.setSpatial(100, 50, 20, 51) self.w.addZone(self.z1) self.w.addZone(self.z2) engine = self.engine mouse = serge.input.Mouse(engine) # Fake a click mouse.current_mouse_state.setState(serge.input.M_LEFT, True) mouse.getScreenPos = lambda: (x, y) mouse.getStaticScreenPos = lambda: (x, y) # self.a1.layer = "one" self.a2.layer = "two" # x, y = 55, 100 # engine.addWorld(self.w) engine.setCurrentWorld(self.w) engine._mouse = mouse engine.processEvents() # self.assertEqual(serge.events.E_LEFT_MOUSE_DOWN, self.a1.action[0]) self.assertEqual(mouse, self.a1.action[1]) self.assertEqual(None, self.a2.action)
def testEngineCanDelinkEvents(self): """testEngineCanDelinkEvents: should be able to delink events from callbacks""" self.a1.setSpatial(50, 100, 51, 30) self.w.addZone(self.z1) engine = self.engine mouse = serge.input.Mouse(engine) # Fake a click mouse.current_mouse_state.setState(serge.input.M_LEFT, True) mouse.getScreenPos = lambda: (x, y) # self.a1.layer = "one" x, y = 55, 100 # global test test = 0 def doit(obj, x): global test test += x # # Link events self.a1.linkEvent(serge.events.E_LEFT_MOUSE_DOWN, doit, 10) self.a1.unlinkEvent(serge.events.E_LEFT_MOUSE_DOWN) # engine.addWorld(self.w) engine.setCurrentWorld(self.w) engine._mouse = mouse engine.processEvents() # self.assertEqual(0, test) self.assertEqual(serge.events.E_LEFT_MOUSE_DOWN, self.a1.action[0]) self.assertEqual(mouse, self.a1.action[1])
def testEngineCanDelinkEvents(self): """testEngineCanDelinkEvents: should be able to delink events from callbacks""" self.a1.setSpatial(50, 100, 51, 30) self.w.addZone(self.z1) engine = self.engine mouse = serge.input.Mouse(engine) # Fake a click mouse.current_mouse_state.setState(serge.input.M_LEFT, True) mouse.getScreenPos = lambda : (x, y) # self.a1.layer = 'one' x, y = 55, 100 # global test test = 0 def doit(obj, x): global test test += x # # Link events self.a1.linkEvent(serge.events.E_LEFT_MOUSE_DOWN, doit, 10) self.a1.unlinkEvent(serge.events.E_LEFT_MOUSE_DOWN) # engine.addWorld(self.w) engine.setCurrentWorld(self.w) engine._mouse = mouse engine.processEvents() # self.assertEqual(0, test) self.assertEqual(serge.events.E_LEFT_MOUSE_DOWN, self.a1.action[0]) self.assertEqual(mouse, self.a1.action[1])
def testEngineCanAutoLinkEvents(self): """testEngineCanAutoLinkEvents: should be able to automatically link events to a callback""" self.a1.setSpatial(50, 100, 51, 30) self.w.addZone(self.z1) engine = self.engine mouse = serge.input.Mouse(engine) # Fake a click mouse.current_mouse_state.setState(serge.input.M_LEFT, True) mouse.getScreenPos = lambda : (x, y) # self.a1.layer = 'one' x, y = 55, 100 # global test test = 0 def doit(obj, x): global test test += x # # Link events self.a1.linkEvent(serge.events.E_LEFT_MOUSE_DOWN, doit, 10) # engine.addWorld(self.w) engine.setCurrentWorld(self.w) engine._mouse = mouse engine.processEvents() # self.assertEqual(10, test)
def testEngineCanHandleMouseEvents(self): """testEngineCanHandleMouseEvents: engine should be able to handle mouse events""" self.a1.setSpatial(50, 100, 51, 30) self.a2.setSpatial(100, 50, 20, 51) self.w.addZone(self.z1) self.w.addZone(self.z2) engine = self.engine mouse = serge.input.Mouse(engine) # Fake a click mouse.current_mouse_state.setState(serge.input.M_LEFT, True) mouse.getScreenPos = lambda : (x, y) mouse.getStaticScreenPos = lambda : (x, y) # self.a1.layer = 'one' self.a2.layer = 'two' # x, y = 55, 100 # engine.addWorld(self.w) engine.setCurrentWorld(self.w) engine._mouse = mouse engine.processEvents() # self.assertEqual(serge.events.E_LEFT_MOUSE_DOWN, self.a1.action[0]) self.assertEqual(mouse, self.a1.action[1]) self.assertEqual(None, self.a2.action)
def testCanConsumeMouseEvent(self): """testCanConsumeMouseEvent: should be able to consume a mouse event to prevent others seeing it""" self.a1.setSpatial(50, 100, 51, 30) self.a2.setSpatial(50, 100, 51, 30) self.w.addZone(self.z1) self.w.addZone(self.z2) engine = self.engine mouse = serge.input.Mouse(engine) # Fake a click mouse.current_mouse_state.setState(serge.input.M_LEFT, True) mouse.getScreenPos = lambda: (x, y) # self.a1.layer = "one" self.a2.layer = "one" x, y = 55, 100 # global test, consume test = 0 consume = False def doit(obj, x): global test test += x if consume: return serge.events.E_LEFT_MOUSE_DOWN else: return None # # Link events self.a1.linkEvent(serge.events.E_LEFT_MOUSE_DOWN, doit, 10) self.a2.linkEvent(serge.events.E_LEFT_MOUSE_DOWN, doit, 1) # # First non consuming engine.addWorld(self.w) engine.setCurrentWorld(self.w) engine._mouse = mouse engine.processEvents() # # Should fire twice self.assertEqual(11, test) # # Now with consuming - only one of the events should work consume = True test = 0 engine.processEvents() self.assertTrue(test in (1, 10))
def testCanConsumeMouseEvent(self): """testCanConsumeMouseEvent: should be able to consume a mouse event to prevent others seeing it""" self.a1.setSpatial(50, 100, 51, 30) self.a2.setSpatial(50, 100, 51, 30) self.w.addZone(self.z1) self.w.addZone(self.z2) engine = self.engine mouse = serge.input.Mouse(engine) # Fake a click mouse.current_mouse_state.setState(serge.input.M_LEFT, True) mouse.getScreenPos = lambda : (x, y) # self.a1.layer = 'one' self.a2.layer = 'one' x, y = 55, 100 # global test, consume test = 0 consume = False def doit(obj, x): global test test += x if consume: return serge.events.E_LEFT_MOUSE_DOWN else: return None # # Link events self.a1.linkEvent(serge.events.E_LEFT_MOUSE_DOWN, doit, 10) self.a2.linkEvent(serge.events.E_LEFT_MOUSE_DOWN, doit, 1) # # First non consuming engine.addWorld(self.w) engine.setCurrentWorld(self.w) engine._mouse = mouse engine.processEvents() # # Should fire twice self.assertEqual(11, test) # # Now with consuming - only one of the events should work consume = True test = 0 engine.processEvents() self.assertTrue(test in (1, 10))
def testStaticZonedActorsAreHandledFirst(self): """testStaticZonedActorsAreHandledFirst: should be able to specify an event handler as important by putting on static""" # Here is the key to ordering the event listeners self.engine.getRenderer().getLayer('two').setStatic(True) # self.a1.setSpatial(0, 0, 1000, 1000) self.a2.setSpatial(0, 0, 1000, 1000) self.w.addZone(self.z1) self.w.addZone(self.z2) engine = self.engine mouse = serge.input.Mouse(engine) # Fake a click mouse.current_mouse_state.setState(serge.input.M_LEFT, True) mouse.getScreenPos = lambda : (x, y) # self.a1.layer = 'one' self.a2.layer = 'two' x, y = 55, 100 # global test, consume test = 0 consume = True def doit(obj, x): global test test += x if consume: return serge.events.E_LEFT_MOUSE_DOWN else: return None # # Link events - normally a2 would fire first being the last # registered but since a2 is on a static layer now it will be the first self.a1.linkEvent(serge.events.E_LEFT_MOUSE_DOWN, doit, 10) self.a2.linkEvent(serge.events.E_LEFT_MOUSE_DOWN, doit, 1) # # First non consuming engine.addWorld(self.w) engine.setCurrentWorld(self.w) engine._mouse = mouse engine.processEvents() self.assertEqual(1, test)