Updated NEWS for 0.1.3.
[redact.git] / lib / redact / program.rb
1 #--
2 # $Id: program.rb 33 2005-04-24 15:54:39Z 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                 protected
88                 def to_eet_name
89                         "Edje_Program"
90                 end
91
92                 def to_eet_properties
93                         {"id" => [@id],
94                          "name" => [@name],
95                          "action" => [@type],
96                          "signal" => [@signal],
97                          "source" => [@source],
98                          "in.from" => [@in_from, :double],
99                          "in.range" => [@in_range, :double],
100                          "after" => [@after]}
101                 end
102         end
103
104         class SetStateProgram < Program
105                 attr_reader :targets, :state, :value, :time
106                 attr_accessor :mode
107
108                 def initialize(collection, id, name)
109                         super
110
111                         @type = TYPE_SET_STATE
112                         @state = "default"
113                         @value = 0.0
114                         @mode = :linear
115                         @time = 0.0
116                         @targets = ProgramArgs.new(collection)
117                 end
118
119                 def state=(v)
120                         @state = v.to_str.dup
121                 end
122
123                 def value=(v)
124                         unless v.is_a?(Float)
125                                 raise(ArgumentError,
126                                       "wrong argument type #{v.class.name} " +
127                                       "(expected Float)")
128                         end
129
130                         @value = v
131                 end
132
133                 def time=(v)
134                         unless v.is_a?(Float)
135                                 raise(ArgumentError,
136                                       "wrong argument type #{v.class.name} " +
137                                       "(expected Float)")
138                         end
139
140                         @time = v
141                 end
142
143                 protected
144                 def to_eet_properties
145                         mode = case @mode
146                         when :linear: 1
147                         when :sinusoidal: 2
148                         when :accelerate: 3
149                         when :decelerate: 4
150                         else
151                                 raise(RedactError, "invalid mode - #{@mode}")
152                         end
153
154                         super.merge!(
155                         {"state" => [@state],
156                          "value" => [@value, :double],
157                          "tween.mode" => [mode],
158                          "tween.time" => [@time, :double],
159                          "targets" => [@targets]})
160                 end
161         end
162
163         class StopProgramProgram < Program
164                 def initialize(collection, id, name)
165                         super
166
167                         @type = TYPE_STOP_PROGRAM
168                         @targets = ProgramArgs.new(collection)
169                 end
170
171                 protected
172                 def to_eet_properties
173                         super.merge!({"targets" => [@targets]})
174                 end
175         end
176
177         class EmitSignalProgram < Program
178                 attr_reader :emission_signal, :emission_source
179
180                 def initialize(collection, id, name)
181                         super
182
183                         @type = TYPE_EMIT_SIGNAL
184                         @emission_signal = ""
185                         @emission_source = ""
186                 end
187
188                 def emission_signal=(v)
189                         @emission_signal = v.to_str.dup
190                 end
191
192                 def emission_source=(v)
193                         @emission_source = v.to_str.dup
194                 end
195
196                 protected
197                 def to_eet_properties
198                         super.merge!(
199                         {"state" => [@emission_signal],
200                          "state2" => [@emission_source]})
201                 end
202         end
203
204         class ExecScriptProgram < Program
205                 attr_reader :script
206
207                 def initialize(collection, id, name)
208                         super
209
210                         @type = TYPE_EXEC_SCRIPT
211                         @script = nil
212                 end
213
214                 def script=(v)
215                         @script = v.to_str.dup
216                 end
217         end
218
219         class ProgramArgs < Array
220                 attr_reader :collection
221
222                 def initialize(collection)
223                         @collection = collection
224                 end
225
226                 def <<(v)
227                         if v.collection != @collection
228                                 raise(ArgumentError, "items not in the same collection")
229                         end
230
231                         super
232                 end
233         end
234
235         class ProgramArg
236                 attr_reader :collection
237
238                 def initialize(v)
239                         @collection = v.collection
240                         @id = v.id
241                 end
242
243                 protected
244                 def to_eet_properties
245                         {"id" => [@id]}
246                 end
247         end
248
249         class ProgramAfter < ProgramArg
250                 protected
251                 def to_eet_name
252                         "Edje_Program_After"
253                 end
254         end
255
256         class ProgramTarget < ProgramArg
257                 protected
258                 def to_eet_name
259                         "Edje_Program_Target"
260                 end
261         end
262 end