Some cosmetic changes.
[redact.git] / lib / redact / part.rb
1 #--
2 # $Id: part.rb 20 2005-04-02 22:15:29Z 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 Part
27                 TYPE_RECTANGLE = 1
28                 TYPE_TEXT = 2
29                 TYPE_IMAGE = 3
30                 TYPE_SWALLOW = 4
31
32                 include Comparable
33
34                 attr_reader :collection, :id, :name, :dragable, :clip
35                 attr_accessor :mouse_events, :repeat_events
36
37                 def initialize(collection, id, name)
38                         @collection = collection
39                         @id = id
40
41                         @name = name.to_str.dup.freeze
42                         @type = TYPE_RECTANGLE
43                         @mouse_events = true
44                         @repeat_events = false
45                         @clip = nil
46                         @dragable = Dragable.new(self)
47
48                         @descriptions = Hash.new do |h, k|
49                                 desc, value = k.split("\0")
50                                 value = value.to_f
51
52                                 h[k] = description_class.new(desc, value)
53                         end
54                 end
55
56                 def <=>(b)
57                         @id <=> b.id
58                 end
59
60                 def clip=(part)
61                         if part == self
62                                 raise(ArgumentError, "cannot clip part to itself")
63                         elsif !part.nil? && part.collection != @collection
64                                 raise(ArgumentError, "items not in the same collection")
65                         else
66                                 @clip = part
67                         end
68                 end
69
70                 def description(name = "default", value = 0.0) # :yields: desc
71                         d = @descriptions[desc_key(name, value)]
72
73                         block_given? ? (yield d) : d
74                 end
75
76                 protected
77                 def description_class
78                         Description
79                 end
80
81                 def to_eet_name
82                         "Edje_Part"
83                 end
84
85                 def to_eet_properties
86                         other_desc = @descriptions.dup
87                         other_desc.delete(desc_key("default", 0.0))
88
89                         confine_id = @dragable.confine.nil? ?
90                                          -1 : @dragable.confine.id
91
92                         {"name" => [@name],
93                          "id" => [@id],
94                          "type" => [@type, :char],
95                          "effect" => [0, :char],
96                          "mouse_events" => [@mouse_events],
97                          "repeat_events" => [@repeat_events],
98                          "clip_to_id" => [@clip.nil? ? -1 : @clip.id],
99                          "default_desc" => [description("default", 0.0)],
100                          "other_desc" => [other_desc],
101                          "dragable.x" => [@dragable.enabled[0], :char],
102                          "dragable.step_x" => [@dragable.step[0]],
103                          "dragable.count_x" => [@dragable.count[0]],
104                          "dragable.y" => [@dragable.enabled[1], :char],
105                          "dragable.step_y" => [@dragable.step[1]],
106                          "dragable.count_y" => [@dragable.count[1]],
107                          "dragable.counfine_id" => [confine_id]} # not a typo!
108                 end
109
110                 private
111                 def desc_key(name, value)
112                         name + "\0" + value.to_s
113                 end
114         end
115
116         class SwallowPart < Part
117                 def initialize(collection, id, name)
118                         super
119
120                         @type = TYPE_SWALLOW
121                 end
122         end
123
124         class TextPart < Part
125                 EFFECT_NONE = 0
126                 EFFECT_PLAIN = 1
127                 EFFECT_OUTLINE = 2
128                 EFFECT_SOFT_OUTLINE = 3
129                 EFFECT_SHADOW = 4
130                 EFFECT_SOFT_SHADOW = 5
131                 EFFECT_OUTLINE_SHADOW = 6
132                 EFFECT_OUTLINE_SOFT_SHADOW = 7
133
134                 attr_accessor :effect
135
136                 def initialize(collection, id, name)
137                         super
138
139                         @type = TYPE_TEXT
140                         @effect = EFFECT_NONE
141                 end
142
143                 protected
144                 def description_class
145                         TextDescription
146                 end
147
148                 def to_eet_properties
149                         super.merge!({"effect" => [@effect, :char]})
150                 end
151         end
152
153         class ImagePart < Part
154                 def initialize(collection, id, name)
155                         super
156
157                         @type = TYPE_IMAGE
158                 end
159
160                 protected
161                 def description_class
162                         ImageDescription
163                 end
164         end
165
166         class Dragable
167                 attr_reader :enabled, :step, :count, :confine
168
169                 def initialize(part)
170                         @part= part
171
172                         @enabled = [false, false]
173                         @step = [0, 0]
174                         @count = [0, 0]
175                         @confine = nil
176                 end
177
178                 def confine=(part)
179                         if part == @part
180                                 raise(ArgumentError, "cannot confine part to itself")
181                         elsif !part.nil? && part.collection != @part.collection
182                                 raise(ArgumentError, "items not in the same collection")
183                         else
184                                 @confine = part
185                         end
186                 end
187         end
188
189         class Relation
190                 attr_reader :rel, :to_id, :offset
191
192                 def initialize(rel, offset)
193                         @rel = [rel, rel]
194                         @to_id = [-1, -1]
195                         @offset = [offset, offset]
196                 end
197
198                 def set_rel(x, y)
199                         @rel = [x, y]
200                 end
201
202                 def set_offset(x, y)
203                         @offset = [x, y]
204                 end
205
206                 def to=(part)
207                         @to_id = [].fill(part.nil? ? -1 : part.id, 0..1)
208                 end
209         end
210
211         class Description
212                 attr_reader :rel, :aspect, :step
213                 attr_accessor :visible, :aspect_preference, :color_class
214
215                 def initialize(name = "default", value = 0.0)
216                         @name = name.to_str.dup.freeze
217                         @value = value.freeze
218                         @visible = true
219                         @align = [0.5, 0.5]
220                         @min = [0, 0]
221                         @max = [-1, -1]
222                         @step = [0, 0]
223                         @aspect = [0.0, 0.0]
224                         @aspect_preference = :none
225                         @rel = [Relation.new(0.0, 0), Relation.new(1.0, -1)]
226                         @color = [].fill(255, 0..3)
227                         @color_class = ""
228                 end
229
230                 def set_step(x = 0, y = 0)
231                         @step = [x, y]
232                 end
233
234                 def set_aspect(x = 0.0, y = 0.0)
235                         @aspect = [x, y]
236                 end
237
238                 def set_align(x = 0.5, y = 0.5)
239                         @align = [x, y]
240                 end
241
242                 def set_size(w, h)
243                         set_min(w, h)
244                         set_max(w, h)
245                 end
246
247                 def set_min(w, h)
248                         @min = [w, h]
249                 end
250
251                 def set_max(w, h)
252                         @max = [w, h]
253                 end
254
255                 def color=(c)
256                         @color = parse_hex_color(c)
257                 end
258
259                 protected
260                 def parse_hex_color(c)
261                         md = c.match(/^#?(([[:xdigit:]]{2}){1,4})$/)
262                         if md.nil?
263                                 raise(ArgumentError, "Argument is not a hex string")
264                         end
265
266                         pairs = md.captures.shift.split(/(..)/).delete_if do |item|
267                                 item == ""
268                         end
269
270                         pairs.push("00") while pairs.length < 3
271                         pairs.push("ff") if pairs.length == 3
272
273                         pairs.map { |p| p.hex }
274                 end
275
276                 def map_aspect_preference
277                         case @aspect_preference
278                         when :none
279                                 0
280                         when :vertical
281                                 1
282                         when :horizontal
283                                 2
284                         when :both
285                                 3
286                         else
287                                 raise(RedactError, "invalid aspect preference value - " +
288                                       @aspect_preference.to_s)
289                         end
290                 end
291
292                 def to_eet_name
293                         "Edje_Part_Description"
294                 end
295
296                 def to_eet_properties
297                         {"state.name" => [@name],
298                          "state.value" => [@value, :double],
299                          "visible" => [@visible],
300                          "align.x" => [@align[0], :double],
301                          "align.y" => [@align[1], :double],
302                          "min.w" => [@min[0]],
303                          "min.h" => [@min[1]],
304                          "max.w" => [@max[0]],
305                          "max.h" => [@max[1]],
306                          "step.x" => [@step[0]],
307                          "step.y" => [@step[1]],
308                          "aspect.min" => [@aspect[0], :double],
309                          "aspect.max" => [@aspect[1], :double],
310                          "aspect.prefer" => [map_aspect_preference, :char],
311                          "rel1.relative_x" => [@rel[0].rel[0], :double],
312                          "rel1.relative_y" => [@rel[0].rel[1], :double],
313                          "rel1.offset_x" => [@rel[0].offset[0]],
314                          "rel1.offset_y" => [@rel[0].offset[1]],
315                          "rel1.id_x" => [@rel[0].to_id[0]],
316                          "rel1.id_y" => [@rel[0].to_id[1]],
317                          "rel2.relative_x" => [@rel[1].rel[0], :double],
318                          "rel2.relative_y" => [@rel[1].rel[1], :double],
319                          "rel2.offset_x" => [@rel[1].offset[0]],
320                          "rel2.offset_y" => [@rel[1].offset[1]],
321                          "rel2.id_x" => [@rel[1].to_id[0]],
322                          "rel2.id_y" => [@rel[1].to_id[1]],
323                          "color_class" => [@color_class],
324                          "color.r" => [@color[0], :char],
325                          "color.g" => [@color[1], :char],
326                          "color.b" => [@color[2], :char],
327                          "color.a" => [@color[3], :char],
328
329                          # image properties
330                          "image.id" => [-1],
331                          "image.tween_list" => [nil],
332                          "border.l" => [0],
333                          "border.r" => [0],
334                          "border.t" => [0],
335                          "border.b" => [0],
336                          "fill.smooth" => [true],
337                          "fill.pos_rel_x" => [0.0, :double],
338                          "fill.pos_abs_x" => [0],
339                          "fill.rel_x" => [1.0, :double],
340                          "fill.abs_x" => [0],
341                          "fill.pos_rel_y" => [0.0, :double],
342                          "fill.pos_abs_y" => [0],
343                          "fill.rel_y" => [1.0, :double],
344                          "fill.abs_y" => [0],
345
346                          # text properties
347                          "color2.r" => [0, :char],
348                          "color2.g" => [0, :char],
349                          "color2.b" => [0, :char],
350                          "color2.a" => [255, :char],
351                          "color3.r" => [0, :char],
352                          "color3.g" => [0, :char],
353                          "color3.b" => [0, :char],
354                          "color3.a" => [128, :char],
355                          "text.text" => [""],
356                          "text.text_class" => [""],
357                          "text.font" => [""],
358                          "text.size" => [0],
359                          "text.fit_x" => [false],
360                          "text.fit_y" => [false],
361                          "text.min_x" => [0],
362                          "text.min_y" => [0],
363                          "text.align.x" => [0.0, :double],
364                          "text.align.y" => [0.0, :double],
365                          "text.id_source" => [-1],
366                          "text.id_text_source" => [-1]}
367                 end
368         end
369
370         class Tween
371                 def initialize(image)
372                         @id = image.id
373                 end
374
375                 def to_eet_name
376                         "Edje_Part_Image_Id"
377                 end
378         end
379
380         class Tweens < Array
381                 def <<(im)
382                         image = EDJE.image_dir.find { |e| e.filename == im }
383                         if image.nil?
384                                 image = ImageDirectoryEntry.new(im)
385                                 EDJE.image_dir << image
386                         end
387
388                         super(Tween.new(image))
389                 end
390         end
391
392         class ImageDescription < Description
393                 attr_reader :image, :auto_rel, :tweens
394
395                 def initialize(name = "default", value = 0.0)
396                         super
397
398                         @image = nil
399                         @tweens = Tweens.new
400                         @border = [0, 0, 0, 0]
401                         @fill_smooth = true
402                         @auto_rel = false
403                 end
404
405                 def image=(im)
406                         return if !@image.nil? && im == @image.filename
407
408                         @image = EDJE.image_dir.find { |e| e.filename == im }
409                         if @image.nil?
410                                 @image = ImageDirectoryEntry.new(im)
411                                 EDJE.image_dir << @image
412                         end
413
414                         self.auto_rel = @auto_rel
415                 end
416
417                 def auto_rel=(b)
418                         @auto_rel = b
419
420                         if @auto_rel && !@image.nil?
421                                 off = @rel[0].offset
422
423                                 @rel[1].set_rel(0.0, 0.0)
424                                 @rel[1].set_offset(off[0] + @image.image.width - 1,
425                                                    off[1] + @image.image.height - 1)
426                         end
427                 end
428
429                 def set_border(l = 0, r = 0, t = 0, b = 0)
430                         @border = [r, r, t, b]
431                 end
432
433                 def to_eet_properties
434                         super.merge!(
435                         {"image.id" => [@image.nil? ? -1 : @image.id],
436                          "image.tween_list" => [@tweens],
437                          "border.l" => [@border[0]],
438                          "border.r" => [@border[1]],
439                          "border.t" => [@border[2]],
440                          "border.b" => [@border[3]],
441                          "fill.smooth" => [@fill_smooth],
442                          "fill.pos_rel_x" => [0.0, :double],
443                          "fill.pos_abs_x" => [0],
444                          "fill.rel_x" => [1.0, :double],
445                          "fill.abs_x" => [0],
446                          "fill.pos_rel_y" => [0.0, :double],
447                          "fill.pos_abs_y" => [0],
448                          "fill.rel_y" => [1.0, :double],
449                          "fill.abs_y" => [0]})
450                 end
451         end
452
453         class TextDescription < Description
454                 attr_reader :font
455                 attr_accessor :text, :font_size, :text_class
456
457                 def initialize(name = "default", value = 0.0)
458                         super
459
460                         @outline_color = [0, 0, 0, 255]
461                         @shadow_color = [0, 0, 0, 128]
462                         @text = ""
463                         @text_class = ""
464                         @font = ""
465                         @font_size = 0
466                         @fit = [false, false]
467                         @text_min = [false, false]
468                         @text_align = [0.5, 0.5]
469                         @text_id_source = -1
470                         @text_id_text_source = -1
471                 end
472
473                 def set_fit(x = false, y = false)
474                         @fit = [x, y]
475                 end
476
477                 def set_text_min(x = false, y = false)
478                         @text_min = [x, y]
479                 end
480
481                 def set_text_align(x = 0.5, y = 0.5)
482                         @text_align = [x, y]
483                 end
484
485                 def font=(f)
486                         md = f.to_str.match(/.*\.ttf/)
487                         unless md.nil?
488                                 found = EDJE.font_dir.find { |font| font.filename == f }
489                                 if found.nil?
490                                         EDJE.font_dir << FontDirectoryEntry.new(f)
491                                 end
492                         end
493
494                         @font = f.dup
495                 end
496
497                 def outline_color=(c)
498                         @outline_color = parse_hex_color(c)
499                 end
500
501                 def shadow_color=(c)
502                         @shadow_color = parse_hex_color(c)
503                 end
504
505                 def to_eet_properties
506                         super.merge!(
507                         {"color2.r" => [@outline_color[0], :char],
508                          "color2.g" => [@outline_color[1], :char],
509                          "color2.b" => [@outline_color[2], :char],
510                          "color2.a" => [@outline_color[3], :char],
511                          "color3.r" => [@shadow_color[0], :char],
512                          "color3.g" => [@shadow_color[1], :char],
513                          "color3.b" => [@shadow_color[2], :char],
514                          "color3.a" => [@shadow_color[3], :char],
515                          "text.text" => [@text],
516                          "text.text_class" => [@text_class],
517                          "text.font" => [@font],
518                          "text.size" => [@font_size],
519                          "text.fit_x" => [@fit[0]],
520                          "text.fit_y" => [@fit[1]],
521                          "text.min_x" => [@text_min[0]],
522                          "text.min_y" => [@text_min[1]],
523                          "text.align.x" => [@text_align[0], :double],
524                          "text.align.y" => [@text_align[1], :double],
525                          "text.id_source" => [@text_id_source],
526                          "text.id_text_source" => [@text_id_text_source]})
527                 end
528         end
529 end