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