Implement ActionList::Entry#clear.
[kaya.git] / lib / animation_field.rb
blobc016a3245edea6be95cdfef84daccee9b2717792
1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 require 'toolkit'
10 module AnimationBase
11   attr_reader :name
12   
13   def to_s
14     "#<#{self.class.name}:#{name}>"
15   end
16 end
18 class Animation
19   include AnimationBase
20   
21   def initialize(name, &blk)
22     @name = name
23     @anim = blk
24   end
25   
26   def [](t)
27     @anim[t]
28   end
29 end
31 class SimpleAnimation
32   include AnimationBase
33   
34   def initialize(name, length, before, animation, after = nil)
35     @name = name
36     @length = length
37     @animation = animation
38     @before = before
39     @after = after
40     @start = nil
41   end
43   def [](t)
44     unless @start
45       @start = t
46       @before[] if @before
47     end
48     
49     i = (t - @start).to_f / @length
50     @animation[i]
51     if i >= 1.0
52       @after[] if @after
53       @start = nil
54       return true
55     end
56     
57     return false
58   end
59 end
62 class AnimationField
63   def initialize(interval, timer_class = Qt::Timer)
64     @actions = []
65     @timer = timer_class.every(interval) {|t| tick(t) }
66     @ticks = 0
67   end
68   
69   def tick(t)
70     @ticks += 1
71     @actions.reject! do |action|
72       action[t]
73     end
74   end
75   
76   def run(action)
77     if action
78       @actions << action
79     end
80   end
81 end