Use symbols instead of constants for tween modes.
[redact.git] / lib / redact / program.rb
1 #--
2 # $Id: program.rb 23 2005-04-02 23:20:32Z 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
39                 attr_accessor :signal, :source, :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 to_eet_name
60                         "Edje_Program"
61                 end
62
63                 def to_eet_properties
64                         {"id" => [@id],
65                          "name" => [@name],
66                          "action" => [@type],
67                          "signal" => [@signal],
68                          "source" => [@source],
69                          "in.from" => [@in_from, :double],
70                          "in.range" => [@in_range, :double],
71                          "after" => [@after]}
72                 end
73         end
74
75         class SetStateProgram < Program
76                 attr_reader :targets
77                 attr_accessor :state, :value, :mode, :time
78
79                 def initialize(collection, id, name)
80                         super
81
82                         @type = TYPE_SET_STATE
83                         @state = "default"
84                         @value = 0.0
85                         @mode = :linear
86                         @time = 0.0
87                         @targets = ProgramArgs.new(collection)
88                 end
89
90                 def to_eet_properties
91                         mode = case @mode
92                         when :linear: 1
93                         when :sinusoidal: 2
94                         when :accelerate: 3
95                         when :decelerate: 4
96                         else
97                                 raise(RedactError, "invalid mode - #{@mode}")
98                         end
99
100                         super.merge!(
101                         {"state" => [@state],
102                          "value" => [@value, :double],
103                          "tween.mode" => [mode],
104                          "tween.time" => [@time, :double],
105                          "targets" => [@targets]})
106                 end
107         end
108
109         class StopProgramProgram < Program
110                 def initialize(collection, id, name)
111                         super
112
113                         @type = TYPE_STOP_PROGRAM
114                         @targets = ProgramArgs.new(collection)
115                 end
116
117                 def to_eet_properties
118                         super.merge!({"targets" => [@targets]})
119                 end
120         end
121
122         class EmitSignalProgram < Program
123                 attr_accessor :emission_signal, :emission_source
124
125                 def initialize(collection, id, name)
126                         super
127
128                         @type = TYPE_EMIT_SIGNAL
129                         @emission_signal = nil
130                         @emission_source = nil
131                 end
132
133                 def to_eet_properties
134                         super.merge!(
135                         {"state" => [@emission_signal],
136                          "state2" => [@emission_source]})
137                 end
138         end
139
140         class ExecScriptProgram < Program
141                 attr_accessor :script
142
143                 def initialize(collection, id, name)
144                         super
145
146                         @type = TYPE_EXEC_SCRIPT
147                         @script = nil
148                 end
149         end
150
151         class ProgramArgs < Array
152                 attr_reader :collection
153
154                 def initialize(collection)
155                         @collection = collection
156                 end
157
158                 def <<(v)
159                         if v.collection != @collection
160                                 raise(ArgumentError, "items not in the same collection")
161                         end
162
163                         super
164                 end
165         end
166
167         class ProgramArg
168                 attr_reader :collection
169
170                 def initialize(v)
171                         @collection = v.collection
172                         @id = v.id
173                 end
174
175                 def to_eet_properties
176                         {"id" => [@id]}
177                 end
178         end
179
180         class ProgramAfter < ProgramArg
181                 def to_eet_name
182                         "Edje_Program_After"
183                 end
184         end
185
186         class ProgramTarget < ProgramArg
187                 def to_eet_name
188                         "Edje_Program_Target"
189                 end
190         end
191 end