Initial commit.
[redact.git] / lib / redact / program.rb
1 #--
2 # $Id: program.rb 1 2005-03-26 01:32:38Z 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                 MODE_LINEAR = 1
77                 MODE_SINUSOIDAL = 2
78                 MODE_ACCELERATE = 3
79                 MODE_DECELERATE = 4
80
81                 attr_reader :targets
82                 attr_accessor :state, :value, :mode, :time
83
84                 def initialize(collection, id, name)
85                         super
86
87                         @type = TYPE_SET_STATE
88                         @state = "default"
89                         @value = 0.0
90                         @mode = MODE_LINEAR
91                         @time = 0.0
92                         @targets = ProgramArgs.new(collection)
93                 end
94
95                 def to_eet_properties
96                         super.merge!(
97                         {"state" => [@state],
98                          "value" => [@value, :double],
99                          "tween.mode" => [@mode],
100                          "tween.time" => [@time, :double],
101                          "targets" => [@targets]})
102                 end
103         end
104
105         class StopProgramProgram < Program
106                 def initialize(collection, id, name)
107                         super
108
109                         @type = TYPE_STOP_PROGRAM
110                         @targets = ProgramArgs.new(collection)
111                 end
112
113                 def to_eet_properties
114                         super.merge!({"targets" => [@targets]})
115                 end
116         end
117
118         class EmitSignalProgram < Program
119                 attr_accessor :emission_signal, :emission_source
120
121                 def initialize(collection, id, name)
122                         super
123
124                         @type = TYPE_EMIT_SIGNAL
125                         @emission_signal = nil
126                         @emission_source = nil
127                 end
128
129                 def to_eet_properties
130                         super.merge!(
131                         {"state" => [@emission_signal],
132                          "state2" => [@emission_source]})
133                 end
134         end
135
136         class ExecScriptProgram < Program
137                 attr_accessor :script
138
139                 def initialize(collection, id, name)
140                         super
141
142                         @type = TYPE_EXEC_SCRIPT
143                         @script = nil
144                 end
145         end
146
147         class ProgramArgs < Array
148                 attr_reader :collection
149
150                 def initialize(collection)
151                         @collection = collection
152                 end
153
154                 def <<(v)
155                         if v.collection != @collection
156                                 raise(ArgumentError, "items not in the same collection")
157                         end
158
159                         super
160                 end
161         end
162
163         class ProgramArg
164                 attr_reader :collection
165
166                 def initialize(v)
167                         @collection = v.collection
168                         @id = v.id
169                 end
170
171                 def to_eet_properties
172                         {"id" => [@id]}
173                 end
174         end
175
176         class ProgramAfter < ProgramArg
177                 def to_eet_name
178                         "Edje_Program_After"
179                 end
180         end
181
182         class ProgramTarget < ProgramArg
183                 def to_eet_name
184                         "Edje_Program_Target"
185                 end
186         end
187 end