Class: SlideField::Animator

Inherits:
Object
  • Object
show all
Defined in:
lib/slidefield/animator.rb

Instance Method Summary collapse

Constructor Details

#initialize(layout_size) ⇒ Animator

Returns a new instance of Animator.



2
3
4
5
# File 'lib/slidefield/animator.rb', line 2

def initialize(layout_size)
  @layout_size = layout_size
  reset
end

Instance Method Details

#frame(*args) ⇒ Object



16
17
18
19
20
21
# File 'lib/slidefield/animator.rb', line 16

def frame(*args)
  struct = Struct.new :time, :current?, :forward?
  @frame = struct.new *args
  yield
  @frame = nil
end

#need_redraw?Boolean

Returns:

  • (Boolean)


7
8
9
10
# File 'lib/slidefield/animator.rb', line 7

def need_redraw?
  @animations.each {|k,v| return true if v.enabled }
  @animations.empty?
end

#resetObject



12
13
14
# File 'lib/slidefield/animator.rb', line 12

def reset
  @animations = {}
end

#transform(obj) ⇒ Object



23
24
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
78
79
80
# File 'lib/slidefield/animator.rb', line 23

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?

  # direction disabled
  cur_direction = @frame.forward? ? @frame.current? : !@frame.current?
  dir_enabled = cur_direction ? anim.enter : anim.leave
  anim.enabled = false unless dir_enabled

  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 = slide_offset position, width, false
  when "slide left"
    tr.x_offset = slide_offset position, width, true
  when "slide down"
    tr.y_offset = slide_offset position, height, false
  when "slide up"
    tr.y_offset = slide_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