Removed RCS-style IDs.
[redact.git] / lib / redact / part.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 Part
25                 TYPE_RECTANGLE = 1
26                 TYPE_TEXT = 2
27                 TYPE_IMAGE = 3
28                 TYPE_SWALLOW = 4
29
30                 include Comparable
31
32                 attr_reader :collection, :id, :name, :dragable, :clip,
33                             :mouse_events, :repeat_events
34
35                 def initialize(collection, id, name)
36                         @collection = collection
37                         @id = id
38
39                         @name = name.to_str.dup.freeze
40                         @type = TYPE_RECTANGLE
41                         @mouse_events = true
42                         @repeat_events = false
43                         @clip = nil
44                         @dragable = Dragable.new(self)
45
46                         @descriptions = Hash.new do |h, k|
47                                 desc, value = k.split("\0")
48                                 value = value.to_f
49
50                                 h[k] = description_class.new(desc, value)
51                         end
52                 end
53
54                 def <=>(b)
55                         @id <=> b.id
56                 end
57
58                 def mouse_events=(val)
59                         @mouse_events = (val == true)
60                 end
61
62                 def repeat_events=(val)
63                         @repeat_events = (val == true)
64                 end
65
66                 def clip=(part)
67                         if part == self
68                                 raise(ArgumentError, "cannot clip part to itself")
69                         elsif !part.nil? && part.collection != @collection
70                                 raise(ArgumentError, "items not in the same collection")
71                         else
72                                 @clip = part
73                         end
74                 end
75
76                 def description(name = "default", value = 0.0) # :yields: desc
77                         d = @descriptions[desc_key(name, value)]
78
79                         block_given? ? (yield d) : d
80                 end
81
82                 protected
83                 def description_class
84                         Description
85                 end
86
87                 def to_eet_name
88                         "Edje_Part"
89                 end
90
91                 def to_eet_properties
92                         other_desc = @descriptions.dup
93                         other_desc.delete(desc_key("default", 0.0))
94
95                         confine_id = @dragable.confine.nil? ?
96                                          -1 : @dragable.confine.id
97
98                         {"name" => [@name],
99                          "id" => [@id],
100                          "type" => [@type, :char],
101                          "effect" => [0, :char],
102                          "mouse_events" => [@mouse_events],
103                          "repeat_events" => [@repeat_events],
104                          "clip_to_id" => [@clip.nil? ? -1 : @clip.id],
105                          "default_desc" => [description("default", 0.0)],
106                          "other_desc" => [other_desc],
107                          "dragable.x" => [@dragable.enabled[0], :char],
108                          "dragable.step_x" => [@dragable.step[0]],
109                          "dragable.count_x" => [@dragable.count[0]],
110                          "dragable.y" => [@dragable.enabled[1], :char],
111                          "dragable.step_y" => [@dragable.step[1]],
112                          "dragable.count_y" => [@dragable.count[1]],
113                          "dragable.events_id" => [-1],
114                          "dragable.counfine_id" => [confine_id]} # not a typo!
115                 end
116
117                 private
118                 def desc_key(name, value)
119                         name + "\0" + value.to_s
120                 end
121         end
122
123         class SwallowPart < Part
124                 def initialize(collection, id, name)
125                         super
126
127                         @type = TYPE_SWALLOW
128                 end
129         end
130
131         class TextPart < Part
132                 attr_accessor :effect
133
134                 def initialize(collection, id, name)
135                         super
136
137                         @type = TYPE_TEXT
138                         @effect = :none
139                 end
140
141                 protected
142                 def description_class
143                         TextDescription
144                 end
145
146                 def to_eet_properties
147                         effect = case @effect
148                         when :none: 0
149                         when :plain: 1
150                         when :outline: 2
151                         when :soft_outline: 3
152                         when :shadow: 4
153                         when :soft_shadow: 5
154                         when :outline_shadow: 6
155                         when :outline_soft_shadow: 7
156                         else
157                                 raise(RedactError, "invalid effect value - #{@effect}")
158                         end
159
160                         super.merge!({"effect" => [effect, :char]})
161                 end
162         end
163
164         class ImagePart < Part
165                 def initialize(collection, id, name)
166                         super
167
168                         @type = TYPE_IMAGE
169                 end
170
171                 protected
172                 def description_class
173                         ImageDescription
174                 end
175         end
176
177         class Dragable
178                 attr_reader :enabled, :step, :count, :confine
179
180                 def initialize(part)
181                         @part= part
182
183                         @enabled = [false, false]
184                         @step = [0, 0]
185                         @count = [0, 0]
186                         @confine = nil
187                 end
188
189                 def confine=(part)
190                         if part == @part
191                                 raise(ArgumentError, "cannot confine part to itself")
192                         elsif !part.nil? && part.collection != @part.collection
193                                 raise(ArgumentError, "items not in the same collection")
194                         else
195                                 @confine = part
196                         end
197                 end
198         end
199
200         class Relation
201                 attr_reader :rel, :to_id, :offset
202
203                 def initialize(rel, offset)
204                         @rel = [rel.to_f, rel.to_f]
205                         @to_id = [-1, -1]
206                         @offset = [offset, offset]
207                 end
208
209                 def set_rel(x, y)
210                         @rel = [x.to_f, y.to_f]
211                 end
212
213                 def set_offset(x, y)
214                         @offset = [x, y]
215                 end
216
217                 def to=(part)
218                         self.set_to(part)
219                 end
220
221                 def set_to(part_x, part_y = part_x)
222                         @to_id = [part_x.nil? ? -1 : part_x.id,
223                                   part_y.nil? ? -1 : part_y.id]
224                 end
225         end
226
227         class Description
228                 attr_reader :rel, :aspect, :step, :visible, :color_class
229                 attr_accessor :aspect_preference
230
231                 def initialize(name = "default", value = 0.0)
232                         @name = name.to_str.dup.freeze
233                         @value = value.freeze
234                         @visible = true
235                         @align = [0.5, 0.5]
236                         @min = [0, 0]
237                         @max = [-1, -1]
238                         @step = [0, 0]
239                         @aspect = [0.0, 0.0]
240                         @aspect_preference = :none
241                         @rel = [Relation.new(0.0, 0), Relation.new(1.0, -1)]
242                         @color = [].fill(255, 0..3)
243                         @color_class = ""
244                 end
245
246                 def inherit(other)
247                         unless other.is_a?(Description)
248                                 raise(ArgumentError, "Cannot inherit from description")
249                         end
250
251                         prot = ["@name", "@value"]
252
253                         (instance_variables - prot).each do |v|
254                                 n = other.instance_variable_get(v.intern)
255                                 n = n.dup rescue n
256                                 instance_variable_set(v.intern, n)
257                         end
258                 end
259
260                 def visible=(v)
261                         @visible = (v == true)
262                 end
263
264                 def color_class=(v)
265                         @color_class = v.to_str.dup
266                 end
267
268                 def set_step(x = 0, y = 0)
269                         @step = [x, y]
270                 end
271
272                 def set_aspect(x = 0.0, y = 0.0)
273                         @aspect = [x, y]
274                 end
275
276                 def set_align(x = 0.5, y = 0.5)
277                         @align = [x, y]
278                 end
279
280                 def set_size(w, h)
281                         set_min(w, h)
282                         set_max(w, h)
283                 end
284
285                 def set_min(w, h)
286                         @min = [w, h]
287                 end
288
289                 def set_max(w, h)
290                         @max = [w, h]
291                 end
292
293                 def color=(c)
294                         @color = parse_hex_color(c)
295                 end
296
297                 protected
298                 def parse_hex_color(c)
299                         md = c.match(/^#?(([[:xdigit:]]{2}){1,4})$/)
300                         if md.nil?
301                                 raise(ArgumentError, "Argument is not a hex string")
302                         end
303
304                         pairs = md.captures.shift.split(/(..)/).delete_if do |item|
305                                 item == ""
306                         end
307
308                         pairs.push("00") while pairs.length < 3
309                         pairs.push("ff") if pairs.length == 3
310
311                         pairs.map { |p| p.hex }
312                 end
313
314                 def to_eet_name
315                         "Edje_Part_Description"
316                 end
317
318                 def to_eet_properties
319                         asp_pref = case @aspect_preference
320                         when :none: 0
321                         when :vertical: 1
322                         when :horizontal: 2
323                         when :both: 3
324                         else
325                                 raise(RedactError, "invalid aspect preference value - " +
326                                       @aspect_preference.to_s)
327                         end
328
329                         {"state.name" => [@name],
330                          "state.value" => [@value, :double],
331                          "visible" => [@visible],
332                          "align.x" => [@align[0], :double],
333                          "align.y" => [@align[1], :double],
334                          "min.w" => [@min[0]],
335                          "min.h" => [@min[1]],
336                          "max.w" => [@max[0]],
337                          "max.h" => [@max[1]],
338                          "step.x" => [@step[0]],
339                          "step.y" => [@step[1]],
340                          "aspect.min" => [@aspect[0], :double],
341                          "aspect.max" => [@aspect[1], :double],
342                          "aspect.prefer" => [asp_pref, :char],
343                          "rel1.relative_x" => [@rel[0].rel[0], :double],
344                          "rel1.relative_y" => [@rel[0].rel[1], :double],
345                          "rel1.offset_x" => [@rel[0].offset[0]],
346                          "rel1.offset_y" => [@rel[0].offset[1]],
347                          "rel1.id_x" => [@rel[0].to_id[0]],
348                          "rel1.id_y" => [@rel[0].to_id[1]],
349                          "rel2.relative_x" => [@rel[1].rel[0], :double],
350                          "rel2.relative_y" => [@rel[1].rel[1], :double],
351                          "rel2.offset_x" => [@rel[1].offset[0]],
352                          "rel2.offset_y" => [@rel[1].offset[1]],
353                          "rel2.id_x" => [@rel[1].to_id[0]],
354                          "rel2.id_y" => [@rel[1].to_id[1]],
355                          "color_class" => [@color_class],
356                          "color.r" => [@color[0], :char],
357                          "color.g" => [@color[1], :char],
358                          "color.b" => [@color[2], :char],
359                          "color.a" => [@color[3], :char],
360
361                          # image properties
362                          "image.id" => [-1],
363                          "image.tween_list" => [nil],
364                          "border.l" => [0],
365                          "border.r" => [0],
366                          "border.t" => [0],
367                          "border.b" => [0],
368                          "border.no_fill" => [false],
369                          "fill.smooth" => [true],
370                          "fill.pos_rel_x" => [0.0, :double],
371                          "fill.pos_abs_x" => [0],
372                          "fill.rel_x" => [1.0, :double],
373                          "fill.abs_x" => [0],
374                          "fill.pos_rel_y" => [0.0, :double],
375                          "fill.pos_abs_y" => [0],
376                          "fill.rel_y" => [1.0, :double],
377                          "fill.abs_y" => [0],
378
379                          # text properties
380                          "color2.r" => [0, :char],
381                          "color2.g" => [0, :char],
382                          "color2.b" => [0, :char],
383                          "color2.a" => [255, :char],
384                          "color3.r" => [0, :char],
385                          "color3.g" => [0, :char],
386                          "color3.b" => [0, :char],
387                          "color3.a" => [128, :char],
388                          "text.text" => [""],
389                          "text.text_class" => [""],
390                          "text.font" => [""],
391                          "text.size" => [0],
392                          "text.fit_x" => [false],
393                          "text.fit_y" => [false],
394                          "text.min_x" => [0],
395                          "text.min_y" => [0],
396                          "text.align.x" => [0.0, :double],
397                          "text.align.y" => [0.0, :double],
398                          "text.id_source" => [-1],
399                          "text.id_text_source" => [-1]}
400                 end
401         end
402
403         class Tween
404                 def initialize(image)
405                         @id = image.id
406                 end
407
408                 protected
409                 def to_eet_name
410                         "Edje_Part_Image_Id"
411                 end
412         end
413
414         class Tweens < Array
415                 def <<(im)
416                         im2 = find_image(im.to_str.strip)
417                         raise(RedactError, "cannot find image - #{im}") if im2.nil?
418
419                         image = EDJE.image_dir.find { |e| e.filename == im2 }
420                         if image.nil?
421                                 image = ImageDirectoryEntry.new(im, im2)
422                                 EDJE.image_dir << image
423                         end
424
425                         super(Tween.new(image))
426                 end
427
428                 private
429                 def find_image(file)
430                         [".", OPTIONS.image_dir].each do |d|
431                                 f2 = File.join(d, file)
432                                 return Pathname.new(f2).cleanpath.to_s if File.file?(f2)
433                         end
434
435                         nil
436                 end
437
438         end
439
440         class ImageDescription < Description
441                 attr_reader :image, :auto_rel, :tweens, :border_fill_middle,
442                             :fill_smooth, :fill_pos_rel, :fill_pos_abs,
443                             :fill_rel, :fill_abs
444
445                 def initialize(name = "default", value = 0.0)
446                         super
447
448                         @image = nil
449                         @tweens = Tweens.new
450                         @border = [0, 0, 0, 0]
451                         @border_fill_middle = true
452
453                         @fill_smooth = true
454                         @fill_pos_rel = [0.0, 0.0]
455                         @fill_pos_abs = [0, 0]
456                         @fill_rel = [1.0, 1.0]
457                         @fill_abs = [0, 0]
458
459                         @auto_rel = false
460                 end
461
462                 def border_fill_middle=(var)
463                         @border_fill_middle = (var == true)
464                 end
465
466                 def image=(im)
467                         im2 = find_image(im.to_str.strip)
468                         raise(RedactError, "cannot find image - #{im}") if im2.nil?
469
470                         return if !@image.nil? && im2 == @image.filename
471
472                         @image = EDJE.image_dir.find { |e| e.filename == im2 }
473                         if @image.nil?
474                                 @image = ImageDirectoryEntry.new(im, im2)
475                                 EDJE.image_dir << @image
476                         end
477
478                         self.auto_rel = @auto_rel
479                 end
480
481                 def auto_rel=(b)
482                         @auto_rel = b
483
484                         if @auto_rel && !@image.nil?
485                                 off = @rel[0].offset
486
487                                 @rel[1].set_rel(0.0, 0.0)
488                                 @rel[1].set_offset(off[0] + @image.image.width - 1,
489                                                    off[1] + @image.image.height - 1)
490                         end
491                 end
492
493                 def set_border(l = 0, r = 0, t = 0, b = 0)
494                         @border = [l, r, t, b]
495                 end
496
497                 def fill_smooth=(v)
498                         @fill_smooth = (v == true)
499                 end
500
501                 def set_fill_pos_rel(x, y)
502                         @fill_pos_rel = [x.to_f, y.to_f]
503                 end
504
505                 def set_fill_pos_abs(x, y)
506                         @fill_pos_abs = [x.to_i, y.to_i]
507                 end
508
509                 def set_fill_rel(x, y)
510                         @fill_rel = [x.to_f, y.to_f]
511                 end
512
513                 def set_fill_abs(x, y)
514                         @fill_abs = [x.to_i, y.to_i]
515                 end
516
517                 protected
518                 def to_eet_properties
519                         super.merge!(
520                         {"image.id" => [@image.nil? ? -1 : @image.id],
521                          "image.tween_list" => [@tweens],
522                          "border.l" => [@border[0]],
523                          "border.r" => [@border[1]],
524                          "border.t" => [@border[2]],
525                          "border.b" => [@border[3]],
526                          "border.no_fill" => [!@border_fill_middle],
527                          "fill.smooth" => [@fill_smooth],
528                          "fill.pos_rel_x" => [@fill_pos_rel[0], :double],
529                          "fill.pos_abs_x" => [@fill_pos_abs[0]],
530                          "fill.rel_x" => [@fill_rel[0], :double],
531                          "fill.abs_x" => [@fill_abs[0]],
532                          "fill.pos_rel_y" => [@fill_pos_rel[1], :double],
533                          "fill.pos_abs_y" => [@fill_pos_abs[1]],
534                          "fill.rel_y" => [@fill_rel[1], :double],
535                          "fill.abs_y" => [@fill_abs[1]]})
536                 end
537
538                 private
539                 def find_image(file)
540                         [".", OPTIONS.image_dir].each do |d|
541                                 f2 = File.join(d, file)
542                                 return Pathname.new(f2).cleanpath.to_s if File.file?(f2)
543                         end
544
545                         nil
546                 end
547         end
548
549         class TextDescription < Description
550                 attr_reader :font, :text, :font_size, :text_class
551
552                 def initialize(name = "default", value = 0.0)
553                         super
554
555                         @outline_color = [0, 0, 0, 255]
556                         @shadow_color = [0, 0, 0, 128]
557                         @text = ""
558                         @text_class = ""
559                         @font = ""
560                         @font_size = 0
561                         @fit = [false, false]
562                         @text_min = [false, false]
563                         @text_align = [0.5, 0.5]
564                         @text_id_source = -1
565                         @text_id_text_source = -1
566                 end
567
568                 def text=(v)
569                         @text = v.to_str.dup
570                 end
571
572                 def font_size=(v)
573                         @font_size = v.to_int
574                 end
575
576                 def text_class=(v)
577                         @text_class = v.to_str.dup
578                 end
579
580                 def set_fit(x = false, y = false)
581                         @fit = [x, y]
582                 end
583
584                 def set_text_min(x = false, y = false)
585                         @text_min = [x, y]
586                 end
587
588                 def set_text_align(x = 0.5, y = 0.5)
589                         @text_align = [x, y]
590                 end
591
592                 def font=(f)
593                         f = f.to_str.strip
594                         md = f.match(/.*\.ttf$/)
595                         unless md.nil?
596                                 f2 = find_font(f)
597                                 raise(RedactError, "cannot find font - #{f}") if f2.nil?
598
599                                 found = EDJE.font_dir.find { |font| font.filename == f2 }
600                                 if found.nil?
601                                         EDJE.font_dir << FontDirectoryEntry.new(f, f2)
602                                         @font = EDJE.font_dir.last.alias
603                                 else
604                                         @font = found.alias
605                                 end
606                         else
607                                 @font = f
608                         end
609                 end
610
611                 def outline_color=(c)
612                         @outline_color = parse_hex_color(c)
613                 end
614
615                 def shadow_color=(c)
616                         @shadow_color = parse_hex_color(c)
617                 end
618
619                 protected
620                 def to_eet_properties
621                         super.merge!(
622                         {"color2.r" => [@outline_color[0], :char],
623                          "color2.g" => [@outline_color[1], :char],
624                          "color2.b" => [@outline_color[2], :char],
625                          "color2.a" => [@outline_color[3], :char],
626                          "color3.r" => [@shadow_color[0], :char],
627                          "color3.g" => [@shadow_color[1], :char],
628                          "color3.b" => [@shadow_color[2], :char],
629                          "color3.a" => [@shadow_color[3], :char],
630                          "text.text" => [@text],
631                          "text.text_class" => [@text_class],
632                          "text.font" => [@font],
633                          "text.size" => [@font_size],
634                          "text.fit_x" => [@fit[0]],
635                          "text.fit_y" => [@fit[1]],
636                          "text.min_x" => [@text_min[0]],
637                          "text.min_y" => [@text_min[1]],
638                          "text.align.x" => [@text_align[0], :double],
639                          "text.align.y" => [@text_align[1], :double],
640                          "text.id_source" => [@text_id_source],
641                          "text.id_text_source" => [@text_id_text_source]})
642                 end
643
644                 private
645                 def find_font(file)
646                         [".", OPTIONS.font_dir].each do |d|
647                                 f2 = File.join(d, file)
648                                 return Pathname.new(f2).cleanpath.to_s if File.file?(f2)
649                         end
650
651                         nil
652                 end
653         end
654 end