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