Example #1
0
    def message(self,text,duration=5):
        """Display a text message to the user for a given duration in seconds.
        
        """
        # If we're already displaying a message, finish it early.
        if hasattr(self,'help'):
            self.help.detachNode()
            self.sequence.finish()

        # The new message.        
        self.help = DirectLabel(text = text, text_scale=.1,
                                text_fg=(.8,.8,.8,1), frameColor=(.2,.2,.2,0), 
                                frameVisibleScale=(1.2,1.2))
        self.help.setPos(0,0,-.7)
        self.help.setAlphaScale(0) # At first the message is fully transparent.

        # This function is used to fade the message in and out.
        def fade(t):
            """Set the alpha of the message to t (multiplied by a constant 
            factor)."""
            self.help.setAlphaScale(t*.9)
            self.help.setColor(.2,.2,.2,t*.7)
            
        # Create a sequence of intervals to fade in the message, wait for
        # `duration`, then fade it out.
        fade_in = LerpFunc(fade, fromData = 0, toData = 1, duration = .5,
                           blendType = 'noBlend', extraArgs = [], name = None)
        fade_out = LerpFunc(fade, fromData = 1, toData = 0, duration = .5,
                            blendType = 'noBlend', extraArgs = [], name = None)
        self.sequence = Sequence(fade_in,Wait(duration),fade_out)
        self.sequence.setDoneEvent('message ended')
        self.sequence.start()
        messager.send('message started',MessageStarted(text,duration))
Example #2
0
    def test_dropped_when_full(self):
        """After a 'drop done' message is sent with a given box and draggable in
        the DroppedEvent object, if the box is already full nothing should 
        change."""

        from znode import DroppedEvent
        from messager import messager
        b = Box()
        i = TestItem()
        j = TestItem()
        b.fill(i)
        # Drag an item onto an already filled box.
        dropped_event = DroppedEvent(draggable=j,
                                     startPos=(0, 0, 0),
                                     endPos=(0, 0, 0),
                                     droppable=b)
        messager.send('drop done', dropped_event)
        self._testContains(b, i)
        # Drag an item from a box and drop it onto the same box again.
        dropped_event = DroppedEvent(draggable=i,
                                     startPos=(0, 0, 0),
                                     endPos=(0, 0, 0),
                                     droppable=b)
        messager.send('drop done', dropped_event)
        self._testContains(b, i)
Example #3
0
    def drop(self):
        """Drop the node currently being dragged, if any."""

        if self._draggee is not None:
            messager.send('do drop',self._draggee)
            self._draggee.drop()
            self._draggee = None
Example #4
0
    def testIgnoreMessage(self):
        """foo and bar are subscribed to c, after ignore(c) neither foo nor bar
        should be called."""

        self.logger.accept('c',self.logger.foo)
        self.logger.accept('c',self.logger.bar)
        self.logger.ignore('c')
        messager.send('c')
        # No methods should have been called.
        self.assertEqual(self.logger.log,[])
Example #5
0
    def drag(self):
        """Begin dragging the draggable that is currently under the mouse 
        pointer, if any."""

        if self._dragMouseOver is not None:
            self._draggee = self._dragMouseOver
            self._dragMouseOver = None
            self._zoomMouseOver = None
            messager.send('do drag',self._draggee)
            self._draggee.drag()
Example #6
0
 def testSendWithNoSenderArgument(self):
     """If no argument is given to the send method, but an argument is given
     to the accept method, then the subscribed function(s) should be called
      with the accept argument only."""
     
     self.logger.accept('foo',self.logger.foo,'accepter arg')
     messager.send('foo')
     # One method should have been called.
     self.assertEqual(len(self.logger.log),1)
     # foo should have been called with the right argument.
     count = self.logger.log.count((self.logger.foo,('accepter arg',)))        
     self.assertEqual(count,1)
Example #7
0
 def testSendWithTwoArguments(self):
     """If accept is called with an argument and then send is called with an
     argument (and the same message name) the function subscribed via accept
     should be called with accept's argument followed by send's argument."""
     
     self.logger.accept('f',self.logger.foo,'accepter arg')
     messager.send('f','sender arg')
     # One method should have been called.
     self.assertEqual(len(self.logger.log),1)
     # foo should have been called with the two arguments in the right order.
     count = self.logger.log.count((self.logger.foo,('accepter arg','sender arg')))        
     self.assertEqual(count,1)
Example #8
0
    def testIgnore(self):
        """foo and bar are subscribed to c, after ignore(c,foo) only bar should
        be called when c is sent."""

        self.logger.accept('c',self.logger.foo)
        self.logger.accept('c',self.logger.bar)
        self.logger.ignore('c',self.logger.foo)
        messager.send('c')
        # Only one method should have been called.
        self.assertEqual(len(self.logger.log),1)
        # bar should have been called once.
        count = self.logger.log.count((self.logger.bar,()))
        self.assertEqual(count,1)
Example #9
0
    def testMultipleAccept(self):
        """foo and bar are subscribed to message a, when a is sent both foo and
        bar should be called."""

        self.logger.accept('a',self.logger.foo)
        self.logger.accept('a',self.logger.bar)

        messager.send('a')
        # Two methods were called.
        self.assertEqual(len(self.logger.log),2)
        # foo was called once.
        count = self.logger.log.count((self.logger.foo,()))
        self.assertEqual(count,1)
        # bar was called once.
        count = self.logger.log.count((self.logger.bar,()))
        self.assertEqual(count,1)
Example #10
0
    def zoomToParent(self):
        """
        If self.focus is not None and we are not already zooming, focus the
        viewport on the next zoomable above self.focus in the scene graph, or
        if there is no such zoomable, move the viewport to it's home position.

        """
        if self.focus is not None and not self.isZooming():
            p = self.focus.getParent().getNetPythonTag('zoomable')
            if p is None:
                self._zoomToNodePath(self.home)
                self.focus = None
            else:
                self._zoomToNodePath(p.np)
                self.focus = p
            messager.send('zooming to zparent',self.focus)
Example #11
0
    def zoomTo(self):
        """
        If the mouse pointer is over a zoomable and we are not already zooming,
        focus the viewport on the zoomable that the mouse pointer is over.

        """
        if self.isZooming():
            # If we are already in the process of zooming we don't want to
            # initiate another zoom.
            return
        elif self._zoomMouseOver is None:
            # The mouse pointer is not over any zoomable.
            return
        else:
            self._zoomToNodePath(self._zoomMouseOver.np)
            self.focus = self._zoomMouseOver
            messager.send('zooming to znode',self._zoomMouseOver)
Example #12
0
    def test_dropped_when_full(self):
        """After a 'drop done' message is sent with a given box and draggable in
        the DroppedEvent object, if the box is already full nothing should 
        change."""

        from znode import DroppedEvent
        from messager import messager
        b = Box()
        i = TestItem()
        j = TestItem()
        b.fill(i)
        # Drag an item onto an already filled box.
        dropped_event = DroppedEvent(draggable = j, startPos = (0,0,0),
                                     endPos = (0,0,0), droppable = b)
        messager.send('drop done',dropped_event)
        self._testContains(b,i)
        # Drag an item from a box and drop it onto the same box again.
        dropped_event = DroppedEvent(draggable = i, startPos = (0,0,0),
                                     endPos = (0,0,0), droppable = b)
        messager.send('drop done',dropped_event)
        self._testContains(b,i)
Example #13
0
    def testAcceptOnce(self):
        """foo is subscribed to message b once only, bar is subscribed to it
        permanently. If b is sent twice, foo and bar should be called the first
        time, only bar should be called the second time."""

        self.logger.acceptOnce('b',self.logger.foo)
        self.logger.accept('b',self.logger.bar)
        
        messager.send('b')
        # foo should have been called once.
        count = self.logger.log.count((self.logger.foo,()))
        self.assertEqual(count,1)
        # bar should have been called once.
        count = self.logger.log.count((self.logger.bar,()))
        self.assertEqual(count,1)
        messager.send('b')
        # foo should still have been called only once.
        count = self.logger.log.count(( self.logger.foo,()))
        self.assertEqual(count,1)
        # bar should now have been called twice.
        count = self.logger.log.count((self.logger.bar,()))
        self.assertEqual(count,2)
Example #14
0
 def drop(self):
     """Signal from zcanvas that the user has dropped this nodepath. By
     default a Draggable resets itself to its state before it was dragged,
     then sends an event containing a DroppedEvent object. For the benefit of
     subclasses that want to extend this method, it returns the DroppedEvent
     as well."""
     
     # This is a hack to make drag-n-drop into boxlists not be effected by
     # the scaling up and down done by Highlightable. It tightly couples
     # Draggable to Highlightable. A better way might be to make
     # Highlightable listen for the drag and drop signals and immediately
     # reset the state when it gets them.
     if  hasattr(self,'scaleUpInterval') and self.scaleUpInterval.isPlaying():
         self.scaleUpInterval.finish()            
     elif  hasattr(self,'scaleDownInterval') and self.scaleDownInterval.isPlaying():
         self.scaleDownInterval.finish()
     if hasattr(self,'prevScale'):
         self.setScale(self.prevScale)
     
     dropped_event = DroppedEvent(draggable = self,
                                  startPos = self._draggedFrom,
                                  endPos = self.getPos(),
                                  droppable = zcanvas._highlightMouseOver)
    
     # Reset the state of this object to how it was before the drag began.
     self.clearBin()
     self.np.setCollideMask(self.prevCollideMask)
     taskMgr.remove(self._dragTask)
     self.setPos(self._draggedFrom)
             
     if self._mouseOver is not None:
         messager.send('drag over leave',DragOverLeave(self,self._mouseOver))
         self._mouseOver = None
         
     messager.send('drop done',dropped_event)                
     if zcanvas._highlightMouseOver is not None:
         zcanvas._highlightMouseOver.drop(dropped_event)
Example #15
0
    def testIgnoreAll(self):
        """After a Receiver object calls ignoreAll() no methods of this object
        should be called when any message is sent, but methods of other objects 
        should continue to be called."""

        self.logger.accept('d',self.logger.foo)
        self.logger.accept('e',self.logger.bar)
        second_logger = Logger()
        second_logger.accept('d',second_logger.foo)
        second_logger.accept('e',second_logger.bar)

        self.logger.ignoreAll()
        messager.send('d')
        messager.send('e')
        # No methods of logger should have been called.
        self.assertEqual(self.logger.log,[])
        # Two methods should have been called on second_logger.
        self.assertEqual(len(second_logger.log),2)
        # foo should have been called once.
        count = second_logger.log.count((second_logger.foo,()))
        self.assertEqual(count,1)
        # bar should have been called once.
        count = second_logger.log.count((second_logger.bar,()))
        self.assertEqual(count,1)
Example #16
0
 def testClear(self):
     """After clear has been called, sending a message should not result in
     any functions being called."""
     
     messager.clear()
     messager.send('a')
     messager.send('b')
     messager.send('c')
     # No methods should have been called.
     self.assertEqual(self.logger.log,[])
Example #17
0
 def setfocus(self,obj):
     self.__focus = obj
     messager.send('new focus',self.focus)