コード例 #1
0
class Droppable:
    """An object that can be dropped onto. (Part of the Drag & Drop protocol.)

    Initially droppable is disabled, users must call set_droppable(True) to
    enable it.

    """
    def __init__(self):

        pass        

    def drop(self,event):
        """A draggable has been dropped onto this droppable.
        Subclasses should extend this if they want to do something."""
        
        # Finish any intervals that are playing.
        # FIXME: this tightly couples Droppable to Highlightable. Move this code
        # into highlightable.
        if  hasattr(self,'scaleDownInterval') and self.scaleDownInterval.isPlaying():
            self.scaleDownInterval.finish()
        elif hasattr(self,'scaleUpInterval') and self.scaleUpInterval.isPlaying():
            self.scaleUpInterval.finish()
        # Reset the scale.
        if hasattr(self,'prevScale'):
            self.setScale(self.prevScale)                
        
    def set_droppable(self,b):
        """Enable or disable dropping onto the NodePath."""

        # FIXME: move this into Highlightable.
        # Finish any intervals that are playing.
        if  hasattr(self,'scaleDownInterval') and self.scaleDownInterval.isPlaying():
            self.scaleDownInterval.finish()
        elif hasattr(self,'scaleUpInterval') and self.scaleUpInterval.isPlaying():
            self.scaleUpInterval.finish()
        # Reset the scale.
        if hasattr(self,'prevScale'):
            self.setScale(self.prevScale)
        
        if b is True:
            self.np.setPythonTag('droppable',self)
        else:
            self.np.clearPythonTag('droppable')

    def highlight(self):
        """Subclasses should override this method to implement custom drag-over
        behaviour."""
        
        # If we are already scaling up, do nothing.
        if  hasattr(self,'scaleUpInterval') and self.scaleUpInterval.isPlaying():
            return
        # If we are in the process of scaling down, finish it off immediately.
        if  hasattr(self,'scaleDownInterval') and self.scaleDownInterval.isPlaying():
            self.scaleDownInterval.finish()
  
        from direct.interval.IntervalGlobal import LerpScaleInterval        
        self.prevScale = self.getScale()
        self.scaleUpInterval = LerpScaleInterval(self.np, duration=0.2, scale=self.getScale()*1.2, startScale=self.getScale())
        self.scaleUpInterval.start()
    
    def unhighlight(self):
        """Subclasses should override this method to implement custom drag-over
        behaviour."""
        
        # If we are already scaling down, do nothing.
        if  hasattr(self,'scaleDownInterval') and self.scaleDownInterval.isPlaying():
            return
        # If we are in the process of scaling up, stop.
        if  hasattr(self,'scaleUpInterval') and self.scaleUpInterval.isPlaying():
            self.scaleUpInterval.pause()        
        from direct.interval.IntervalGlobal import LerpScaleInterval
        self.scaleDownInterval = LerpScaleInterval(self.np, duration=0.2, scale=self.prevScale, startScale=self.getScale())
        self.scaleDownInterval.start()
コード例 #2
0
class Highlightable:   
    """A mixin class that receives notifications from zcanvas when the mouse
    pointer enters or leaves its nodepath. These events can be used to highlight
    a nodepath as the mouse cursor moves over it. The default implementation
    scales the nodepath to 120% of its original size over a 0.2 second interval
    when the mouse enters the nodepath, and scales it back over the same amount
    of time when the mouse leaves."""

    def __init__(self):
            
        pass

    def highlight(self):
        """The mouse pointer has entered the bounds of this nodepath. Subclasses
        should override this method to implement custom mouse-over behaviour.
        
        """        
        # If we are already scaling up, do nothing.
        if  hasattr(self,'scaleUpInterval') and self.scaleUpInterval.isPlaying():
            return

        # If we are in the process of scaling down, finish it off immediately.
        if  hasattr(self,'scaleDownInterval') and self.scaleDownInterval.isPlaying():
            self.scaleDownInterval.finish()
        
        from direct.interval.IntervalGlobal import LerpScaleInterval        
        self.prevScale = self.getScale()
        self.scaleUpInterval = LerpScaleInterval(self.np, duration=0.2, scale=self.getScale()*1.2, startScale=self.getScale())
        self.scaleUpInterval.start()

    def unhighlight(self):
        """The mouse pointer has left the bounds of this nodepath. Subclasses 
        should override this method to implement custom mouse-over behaviour.
        
        """
        # If we are already scaling down, do nothing.
        if  hasattr(self,'scaleDownInterval') and self.scaleDownInterval.isPlaying():
            return

        # If we are in the process of scaling up, stop.
        if  hasattr(self,'scaleUpInterval') and self.scaleUpInterval.isPlaying():
            self.scaleUpInterval.pause()
        
        from direct.interval.IntervalGlobal import LerpScaleInterval
        self.scaleDownInterval = LerpScaleInterval(self.np, duration=0.2, scale=self.prevScale, startScale=self.getScale())
        self.scaleDownInterval.start()

    def set_highlightable(self,boolean):
        """Enable or disable mouse-over highlighting of this nodepath. (When
        disabled, the highlight and unhighlight methods will not be
        called.)
        
        """    
        # Finish any intervals that are playing.
        if  hasattr(self,'scaleDownInterval') and self.scaleDownInterval.isPlaying():
            self.scaleDownInterval.finish()
        elif hasattr(self,'scaleUpInterval') and self.scaleUpInterval.isPlaying():
            self.scaleUpInterval.finish()
    
        # Reset the scale.
        if hasattr(self,'prevScale'):
            self.setScale(self.prevScale)

        if boolean:
            self.np.setPythonTag("highlightable",self)
        else:    
            self.np.clearPythonTag("highlightable")