Class: SlideField::Animator
- Inherits:
-
Object
- Object
- SlideField::Animator
- Defined in:
- lib/slidefield/animator.rb
Instance Attribute Summary collapse
-
#current ⇒ Object
Returns the value of attribute current.
-
#forward ⇒ Object
Returns the value of attribute forward.
Instance Method Summary collapse
- #frame(*args) ⇒ Object
-
#initialize(layout_size) ⇒ Animator
constructor
A new instance of Animator.
- #need_redraw? ⇒ Boolean
- #reset ⇒ Object
- #transform(obj) ⇒ Object
Constructor Details
#initialize(layout_size) ⇒ Animator
Returns a new instance of Animator.
4 5 6 7 |
# File 'lib/slidefield/animator.rb', line 4 def initialize(layout_size) @layout_size = layout_size reset end |
Instance Attribute Details
#current ⇒ Object
Returns the value of attribute current.
2 3 4 |
# File 'lib/slidefield/animator.rb', line 2 def current @current end |
#forward ⇒ Object
Returns the value of attribute forward.
2 3 4 |
# File 'lib/slidefield/animator.rb', line 2 def forward @forward end |
Instance Method Details
#frame(*args) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/slidefield/animator.rb', line 18 def frame(*args) struct = Struct.new :time, :current?, :forward? @frame = struct.new *args yield @frame = nil end |
#need_redraw? ⇒ Boolean
9 10 11 12 |
# File 'lib/slidefield/animator.rb', line 9 def need_redraw? @animations.each {|k,v| return true if v.enabled } @animations.empty? end |
#reset ⇒ Object
14 15 16 |
# File 'lib/slidefield/animator.rb', line 14 def reset @animations = {} end |
#transform(obj) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/slidefield/animator.rb', line 25 def transform(obj) raise "Can not animate outside a frame" unless @frame tr_struct = Struct.new :skip_draw?, :x_offset, :y_offset, :scale, :opacity tr = tr_struct.new false, 0, 0, 1.0, 1.0 anim = animation_for obj.ancestor(:animation) # no animation return tr if anim.nil? elapsed = @frame.time - anim.start_time position = elapsed / anim.duration anim.enabled = false if position > 1.0 # animation finished unless anim.enabled tr[:skip_draw?] = !@frame.current? # don't draw the previous slide anymore return tr end width, height = @layout_size case anim.name when "fade" if @frame.current? tr.opacity = position else tr.opacity = 1.0 - position end when "slide right" tr.x_offset = position, width, false when "slide left" tr.x_offset = position, width, true when "slide down" tr.y_offset = position, height, false when "slide up" tr.y_offset = position, height, true when "zoom" if @frame.current? tr.scale = position else tr.scale = 1.0 - position end else # the validator has missed ?! # TODO: validate at interpret time raise SlideField::RuntimeError, "Unsupported animation '#{anim.name}'" end tr end |