Implemented layouting.
[kaya.git] / lib / animation_field.rb
blob800aa6568be9217d246c8b63a84a87a80f576cdc
1 require 'qtutils'
3 module AnimationBase
4   def to_s
5     "#<#{self.class.name}:#{@name}>"
6   end
7 end
9 class Animation
10   include AnimationBase
11   
12   def initialize(name, &blk)
13     @name = name
14     @anim = blk
15   end
16   
17   def [](t)
18     @anim[t]
19   end
20 end
22 class SimpleAnimation
23   include AnimationBase
24   
25   def initialize(name, length, before, animation, after = nil)
26     @name = name
27     @length = length
28     @animation = animation
29     @before = before
30     @after = after
31     @start = nil
32   end
34   def [](t)
35     unless @start
36       @start = t
37       @before[] if @before
38     end
39     
40     i = (t - @start).to_f / @length
41     @animation[i]
42     if i >= 1.0
43       @after[] if @after
44       @start = nil
45       return true
46     end
47     
48     return false
49   end
50 end
53 class AnimationField
54   def initialize(interval, timer_class = Qt::Timer)
55     @actions = []
56     @timer = timer_class.every(interval) {|t| tick(t) }
57     @ticks = 0
58   end
59   
60   def tick(t)
61     @ticks += 1
62     @actions.reject! do |action|
63       action[t]
64     end
65   end
66   
67   def run(action)
68     if action
69       @actions << action
70     end
71   end
72 end