Sha256: a25015c790095bcf0b05de7d65d8a565ea79f06c1991739b4e547d920a526b9a

Contents?: true

Size: 1.94 KB

Versions: 21

Compression:

Stored size: 1.94 KB

Contents

module SugarCube
  class AnimationChain

    class << self

      def chains
        @chains ||= []
      end

      def start_chain(chain)
        chains << chain unless chains.include?(chain)
      end

      def stop_chain(chain)
        chains.delete(chain)
      end

    end

    def initialize
      raise "animation chains cannot be nested" if Thread.current[:sugarcube_chaining]
      @blocks = []
    end

    def wait(duration)
      and_then(duration: duration) {}
    end

    def and_then(options=nil, &block)
      if options
        options = options.dup
      else
        options = {}
      end
      @blocks << [options, block]
      self
    end

    def << block
      and_then(&block)
    end

    def do_next
      return nil if @block_index >= @blocks.length

      options, block = @blocks[@block_index]
      @after_block = ->(completed){
        if @abort || ! self.do_next
          @running = false
          if @loop
            start
          else
            AnimationChain.stop_chain(self)
          end
        end
      }
      options[:after] = @after_block

      UIView.animate(options) {
        Thread.current[:sugarcube_chaining] = true
        block.call
        Thread.current[:sugarcube_chaining] = nil
        @block_index += 1
      }
      true
    end

    def start
      return if @running
      AnimationChain.start_chain(self)
      @running = true
      @abort = nil
      @block_index = 0
      if Fixnum === @loop
        @loop -= 1
        @loop = nil if @loop == 0
      end
      do_next
      return self
    end

    # @param times [Fixnum,nil] number of times to loop, or any other truthy value to loop forever
    def loop(times=true)
      @loop = times
      start
    end

    # Cancels a loop, but lets the chain finish
    def stop
      @loop = nil
    end

    # stops the animation immediately
    def abort
      return unless @running
      @loop = nil
      @abort = true
      @running = false
    end

  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
sugarcube-0.20.11 lib/sugarcube/animation_chain.rb
sugarcube-0.20.10 lib/sugarcube/animation_chain.rb
sugarcube-0.20.9 lib/sugarcube/animation_chain.rb
sugarcube-0.20.8 lib/sugarcube/animation_chain.rb
sugarcube-0.20.7 lib/sugarcube/animation_chain.rb
sugarcube-0.20.6 lib/sugarcube/animation_chain.rb
sugarcube-0.20.5 lib/sugarcube/animation_chain.rb
sugarcube-0.20.4 lib/sugarcube/animation_chain.rb
sugarcube-0.20.3 lib/sugarcube/animation_chain.rb
sugarcube-0.20.1 lib/sugarcube/animation_chain.rb
sugarcube-0.20.0 lib/sugarcube/animation_chain.rb
sugarcube-0.19.5 lib/sugarcube/animation_chain.rb
sugarcube-0.19.4 lib/sugarcube/animation_chain.rb
sugarcube-0.19.2 lib/sugarcube/animation_chain.rb
sugarcube-0.19.0 lib/sugarcube/animation_chain.rb
sugarcube-0.18.21 lib/sugarcube/animation_chain.rb
sugarcube-0.18.20 lib/sugarcube/animation_chain.rb
sugarcube-0.18.19 lib/sugarcube/animation_chain.rb
sugarcube-0.18.18 lib/sugarcube/animation_chain.rb
sugarcube-0.18.17 lib/sugarcube/animation_chain.rb