Add licence and installation instructions.
[kaya.git] / lib / animation_field.rb
blob90c53ed12138da129bf25bed4579877c516d11df
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 'qtutils'
10 module AnimationBase
11   def to_s
12     "#<#{self.class.name}:#{@name}>"
13   end
14 end
16 class Animation
17   include AnimationBase
18   
19   def initialize(name, &blk)
20     @name = name
21     @anim = blk
22   end
23   
24   def [](t)
25     @anim[t]
26   end
27 end
29 class SimpleAnimation
30   include AnimationBase
31   
32   def initialize(name, length, before, animation, after = nil)
33     @name = name
34     @length = length
35     @animation = animation
36     @before = before
37     @after = after
38     @start = nil
39   end
41   def [](t)
42     unless @start
43       @start = t
44       @before[] if @before
45     end
46     
47     i = (t - @start).to_f / @length
48     @animation[i]
49     if i >= 1.0
50       @after[] if @after
51       @start = nil
52       return true
53     end
54     
55     return false
56   end
57 end
60 class AnimationField
61   def initialize(interval, timer_class = Qt::Timer)
62     @actions = []
63     @timer = timer_class.every(interval) {|t| tick(t) }
64     @ticks = 0
65   end
66   
67   def tick(t)
68     @ticks += 1
69     @actions.reject! do |action|
70       action[t]
71     end
72   end
73   
74   def run(action)
75     if action
76       @actions << action
77     end
78   end
79 end