Пример #1
0
    def _animate_set ( self ):
        if self.animate:
            self.animation = ConcurrentAnimation( items =
                self._animations_for( self.x ) + self._animations_for( self.y )
            ).run()
        else:
            self.animation.halt()
            for fa in self.animation.items:
                setattr( fa.object, fa.name, ( fa.begin[0], fa.end[0] ) )

            self.animation = None
Пример #2
0
 def _start_set ( self ):
     self._stop_set()
     self.animate_facet(
         'counter', self.time, self.target, 0,
         repeat  = self.repeat,
         reverse = self.reverse
     )
     if self._uis is not None:
         controls = [ ui.control
                      for ui in self._uis if ui.control is not None ]
         if len( controls ) > 0:
             self._animation = ConcurrentAnimation(
                 repeat  = 0,
                 reverse = False,
                 items   = [ control.animate_facet(
                     'bounds', self.time, ( 20, 40, 400, 100 ),
                     repeat  = self.repeat,
                     reverse = self.reverse,
                     tweener = EaseOutEaseIn,
                     start   = False
                 ) for control in controls ]
             ).run()
Пример #3
0
class AnimationDemo ( UIView ):

    #-- Facet Definitions ------------------------------------------------------

    counter    = Int
    target     = Range( 10, 10000, 100 )
    travellers = Range( 1, 20, 6 )
    time       = Range( 0.0, 20.0, 1.0 )
    repeat     = Range( 0, 50, 6 )
    reverse    = Bool( True )
    start      = Button( 'Start' )
    stop       = Button( 'Stop' )
    create     = Button( 'Create' )
    begin      = Property
    elapsed    = Property

    #-- Facet View Definitions -------------------------------------------------

    view = View(
        VGroup(
            Item( 'counter', style = 'readonly' ),
            Item( 'elapsed', style = 'readonly' ),
            group_theme = '@xform:b6?L35'
        ),
        VGroup(
            SEItem( 'target' ),
            SEItem( 'travellers' ),
            SEItem( 'time' ),
            SEItem( 'repeat' ),
            Item(   'reverse' ),
            group_theme = '@xform:b6?L35'
        ),
        HGroup(
            spring,
            Item( 'start'  ),
            Item( 'stop'   ),
            Item( 'create' ),
            show_labels = False,
            group_theme = '@xform:b6?L35'
        )
    )

    #-- Property Implementations -----------------------------------------------

    @property_depends_on( 'start' )
    def _get_begin ( self ):
        return time()

    @property_depends_on( 'begin, counter' )
    def _get_elapsed ( self ):
        return ('%.2f' % (time() - self.begin))

    #-- Facet Event Handlers ---------------------------------------------------

    def _start_set ( self ):
        self._stop_set()
        self.animate_facet(
            'counter', self.time, self.target, 0,
            repeat  = self.repeat,
            reverse = self.reverse
        )
        if self._uis is not None:
            controls = [ ui.control
                         for ui in self._uis if ui.control is not None ]
            if len( controls ) > 0:
                self._animation = ConcurrentAnimation(
                    repeat  = 0,
                    reverse = False,
                    items   = [ control.animate_facet(
                        'bounds', self.time, ( 20, 40, 400, 100 ),
                        repeat  = self.repeat,
                        reverse = self.reverse,
                        tweener = EaseOutEaseIn,
                        start   = False
                    ) for control in controls ]
                ).run()

    def _stop_set ( self ):
        self.halt_animated_facets()
        if self._animation is not None:
            self._animation.halt()

    def _create_set ( self ):
        if self._animation is not None:
            self._animation.halt()

        self._clean_up()

        self._travellers = [ Traveller( owner = self )
                             for i in range( self.travellers ) ]

        self._uis = [ traveller.edit_facets( parent = self.ui_info.ui.control )
                      for traveller in self._travellers ]
        for i, ui in enumerate( self._uis ):
            ui.control.position = ( 600, (90 * i) + 30 )

    def _ui_info_set ( self, ui_info ):
        if ui_info is None:
            self._clean_up()

    #-- Private Methods --------------------------------------------------------

    def _clean_up ( self ):
        if self._uis is not None:
            for ui in self._uis:
                ui.dispose()

        self._uis = None
Пример #4
0
class Equations ( UIView ):

    x          = Instance( Equation,
                           { 'label':    'x(t)',
                             'equation': 'a*sin(b*2*pi*t)+c*t',
                             'b':        ( 1.0, 2.4 ),
                             'b_time':   3.0,
                             'c':        ( 0.4, 1.4 ),
                             'c_time':   2.1 } )
    y          = Instance( Equation,
                           { 'label':    'y(t)',
                             'a':        ( 1.4, 2.0 ),
                             'a_time':   2.5,
                             'equation': 'a*cos(b*2*pi*t)' } )
    t          = Coefficient( ( -0.4, 1.6 ) )
    steps      = Range( 2, 500, 350 )
    animate    = Bool( True )
    animation  = Instance( ConcurrentAnimation )
    data       = List( Data )
    numpy      = Any

    def default_facets_view ( self ):
        return View(
            VGroup(
                HGroup(
                    UItem( 'x', style = 'custom' ),
                    UItem( 'y', style = 'custom' ),
                    group_theme = '#themes:toolbar_group'
                ),
                HGroup(
                    SItem( 'steps', editor = ScrubberEditor() ), '_',
                    Item(  't', label = 't', springy = True ), '_',
                    UItem( 'animate',
                           editor = ThemedCheckboxEditor(
                               image       = '@icons2:StopBox',
                               off_image   = '@icons2:PlayBox',
                               on_tooltip  = 'Click to stop the animation',
                               off_tooltip = 'Click to start the animation' )
                    ),
                    group_theme = '#themes:toolbar_group'
                ),
                HSplit(
                     VGroup(
                         UItem( 'data',
                                editor  = DataEditor( style = 'point' ),
                                tooltip = 'Click to reset bounds'
                         ),
                         label = 'Points',
                         dock  = 'tab'
                     ),
                     VGroup(
                         UItem( 'data',
                                editor  = DataEditor( style = 'line' ),
                                tooltip = 'Click to reset bounds'
                         ),
                         label = 'Lines',
                         dock  = 'tab'
                     )
                 )
            ),
            width  = 0.9,
            height = 0.9
        )

    def facets_init ( self ):
        self._animate_set()

    def _numpy_default ( self ):
        import numpy
        return dict( [ ( _, getattr( numpy, _ ) ) for _ in dir( numpy ) ] )

    def _ui_info_set ( self, ui_info ):
        if (ui_info is None) and (self.animation is not None):
            self.animation.halt()

    def _animate_set ( self ):
        if self.animate:
            self.animation = ConcurrentAnimation( items =
                self._animations_for( self.x ) + self._animations_for( self.y )
            ).run()
        else:
            self.animation.halt()
            for fa in self.animation.items:
                setattr( fa.object, fa.name, ( fa.begin[0], fa.end[0] ) )

            self.animation = None

    @on_facet_set( 'x:refresh, y:refresh, t, steps' )
    def _refresh_needed ( self ):
        self._generate_data()

    @on_facet_set( 'x:restart, y:restart' )
    def _restart_needed ( self ):
        if self.animate:
            do_after( 500, self._restart_animation )

    def _restart_animation ( self ):
        self.animate = False
        self.animate = True

    def _generate_data ( self ):
        t0, t1  = self.t
        dt      = (t1 - t0) / ( self.steps - 1)
        context = { 't': arange( t0, t1 + (dt / 2.0), dt ) }
        try:
            self.data = [ Data( x = self._eval( self.x, context ),
                                y = self._eval( self.y, context ) ) ]
        except:
            pass

    def _eval ( self, equation, context ):
        for name in Coefficients:
            context[ name ] = getattr( equation, name )[0]

        return eval( equation.equation_, self.numpy, context )

    def _animations_for ( self, equation ):
        animations = []
        for name in Coefficients:
            v0, v1 = getattr( equation, name )
            if v0 != v1:
                animations.append( equation.animate_facet(
                    name, getattr( equation, name + '_time' ),
                    ( v1, v1 ), ( v0, v0 ),
                    repeat = 0,
                    start  = False
                ) )

        return animations