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