Replaced most attr_accessor calls by explicit setter methods.
[redact.git] / lib / redact / program.rb
1 #--
2 # $Id: program.rb 25 2005-04-14 19:42:06Z tilman $
3 #
4 # Copyright (c) 2005 Tilman Sauerbeck (tilman at code-monkey de)
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining
7 # a copy of this software and associated documentation files (the
8 # "Software"), to deal in the Software without restriction, including
9 # without limitation the rights to use, copy, modify, merge, publish,
10 # distribute, sublicense, and/or sell copies of the Software, and to
11 # permit persons to whom the Software is furnished to do so, subject to
12 # the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be
15 # included in all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25 module Redact
26         class Program
27                 TYPE_BASE = 0
28                 TYPE_SET_STATE = 1
29                 TYPE_STOP_PROGRAM = 2
30                 TYPE_EMIT_SIGNAL = 3
31                 #TYPE_SET_DRAG_VAL = 4
32                 #TYPE_STEP_DRAG_VAL = 5
33                 #TYPE_PAGE_DRAG_VAL = 6
34                 TYPE_EXEC_SCRIPT = 7
35
36                 include Comparable
37
38                 attr_reader :collection, :id, :name, :after, :signal, :source,
39                             :in_from, :in_range
40
41                 def initialize(collection, id, name)
42                         @collection = collection
43                         @id = id
44
45                         @name = name.to_str.dup.freeze
46                         @type = TYPE_BASE
47
48                         @after = ProgramArgs.new(collection)
49                         @in_from = 0.0
50                         @in_range = 0.0
51                         @signal = ""
52                         @source = ""
53                 end
54
55                 def <=>(b)
56                         @id <=> b.id
57                 end
58
59                 def signal=(v)
60                         @signal = v.to_str.dup
61                 end
62
63                 def source=(v)
64                         @source = v.to_str.dup
65                 end
66
67                 def in_from=(v)
68                         unless v.is_a?(Float)
69                                 raise(ArgumentError,
70                                       "wrong argument type #{v.class.name} " +
71                                       "(expected Float)")
72                         end
73
74                         @in_from = v
75                 end
76
77                 def in_range=(v)
78                         unless v.is_a?(Float)
79                                 raise(ArgumentError,
80                                       "wrong argument type #{v.class.name} " +
81                                       "(expected Float)")
82                         end
83
84                         @in_range = v
85                 end
86
87                 def to_eet_name
88                         "Edje_Program"
89                 end
90
91                 def to_eet_properties
92                         {"id" => [@id],
93                          "name" => [@name],
94                          "action" => [@type],
95                          "signal" => [@signal],
96                          "source" => [@source],
97                          "in.from" => [@in_from, :double],
98                          "in.range" => [@in_range, :double],
99                          "after" => [@after]}
100                 end
101         end
102
103         class SetStateProgram < Program
104                 attr_reader :targets, :state, :value, :time
105                 attr_accessor :mode
106
107                 def initialize(collection, id, name)
108                         super
109
110                         @type = TYPE_SET_STATE
111                         @state = "default"
112                         @value = 0.0
113                         @mode = :linear
114                         @time = 0.0
115                         @targets = ProgramArgs.new(collection)
116                 end
117
118                 def state=(v)
119                         @state = v.to_str.dup
120                 end
121
122                 def value=(v)
123                         unless v.is_a?(Float)
124                                 raise(ArgumentError,
125                                       "wrong argument type #{v.class.name} " +
126                                       "(expected Float)")
127                         end
128
129                         @value = v
130                 end
131
132                 def time=(v)
133                         unless v.is_a?(Float)
134                                 raise(ArgumentError,
135                                       "wrong argument type #{v.class.name} " +
136                                       "(expected Float)")
137                         end
138
139                         @time = v
140                 end
141
142                 def to_eet_properties
143                         mode = case @mode
144                         when :linear: 1
145                         when :sinusoidal: 2
146                         when :accelerate: 3
147                         when :decelerate: 4
148                         else
149                                 raise(RedactError, "invalid mode - #{@mode}")
150                         end
151
152                         super.merge!(
153                         {"state" => [@state],
154                          "value" => [@value, :double],
155                          "tween.mode" => [mode],
156                          "tween.time" => [@time, :double],
157                          "targets" => [@targets]})
158                 end
159         end
160
161         class StopProgramProgram < Program
162                 def initialize(collection, id, name)
163                         super
164
165                         @type = TYPE_STOP_PROGRAM
166                         @targets = ProgramArgs.new(collection)
167                 end
168
169                 def to_eet_properties
170                         super.merge!({"targets" => [@targets]})
171                 end
172         end
173
174         class EmitSignalProgram < Program
175                 attr_reader :emission_signal, :emission_source
176
177                 def initialize(collection, id, name)
178                         super
179
180                         @type = TYPE_EMIT_SIGNAL
181                         @emission_signal = nil
182                         @emission_source = nil
183                 end
184
185                 def emission_signal=(v)
186                         @emission_signal = v.to_str.dup
187                 end
188
189                 def emission_source=(v)
190                         @emission_source = v.to_str.dup
191                 end
192
193                 def to_eet_properties
194                         super.merge!(
195                         {"state" => [@emission_signal],
196                          "state2" => [@emission_source]})
197                 end
198         end
199
200         class ExecScriptProgram < Program
201                 attr_reader :script
202
203                 def initialize(collection, id, name)
204                         super
205
206                         @type = TYPE_EXEC_SCRIPT
207                         @script = nil
208                 end
209
210                 def script=(v)
211                         @script = v.to_str.dup
212                 end
213         end
214
215         class ProgramArgs < Array
216                 attr_reader :collection
217
218                 def initialize(collection)
219                         @collection = collection
220                 end
221
222                 def <<(v)
223                         if v.collection != @collection
224                                 raise(ArgumentError, "items not in the same collection")
225                         end
226
227                         super
228                 end
229         end
230
231         class ProgramArg
232                 attr_reader :collection
233
234                 def initialize(v)
235                         @collection = v.collection
236                         @id = v.id
237                 end
238
239                 def to_eet_properties
240                         {"id" => [@id]}
241                 end
242         end
243
244         class ProgramAfter < ProgramArg
245                 def to_eet_name
246                         "Edje_Program_After"
247                 end
248         end
249
250         class ProgramTarget < ProgramArg
251                 def to_eet_name
252                         "Edje_Program_Target"
253                 end
254         end
255 end