2 # $Id: part.rb 70 2005-09-21 15:56:55Z tilman $
4 # Copyright (c) 2005 Tilman Sauerbeck (tilman at code-monkey de)
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:
14 # The above copyright notice and this permission notice shall be
15 # included in all copies or substantial portions of the Software.
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.
34 attr_reader :collection, :id, :name, :dragable, :clip,
35 :mouse_events, :repeat_events
37 def initialize(collection, id, name)
38 @collection = collection
41 @name = name.to_str.dup.freeze
42 @type = TYPE_RECTANGLE
44 @repeat_events = false
46 @dragable = Dragable.new(self)
48 @descriptions = Hash.new do |h, k|
49 desc, value = k.split("\0")
52 h[k] = description_class.new(desc, value)
60 def mouse_events=(val)
61 @mouse_events = (val == true)
64 def repeat_events=(val)
65 @repeat_events = (val == true)
70 raise(ArgumentError, "cannot clip part to itself")
71 elsif !part.nil? && part.collection != @collection
72 raise(ArgumentError, "items not in the same collection")
78 def description(name = "default", value = 0.0) # :yields: desc
79 d = @descriptions[desc_key(name, value)]
81 block_given? ? (yield d) : d
94 other_desc = @descriptions.dup
95 other_desc.delete(desc_key("default", 0.0))
97 confine_id = @dragable.confine.nil? ?
98 -1 : @dragable.confine.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!
120 def desc_key(name, value)
121 name + "\0" + value.to_s
125 class SwallowPart < Part
126 def initialize(collection, id, name)
133 class TextPart < Part
134 attr_accessor :effect
136 def initialize(collection, id, name)
144 def description_class
148 def to_eet_properties
149 effect = case @effect
153 when :soft_outline: 3
156 when :outline_shadow: 6
157 when :outline_soft_shadow: 7
159 raise(RedactError, "invalid effect value - #{@effect}")
162 super.merge!({"effect" => [effect, :char]})
166 class ImagePart < Part
167 def initialize(collection, id, name)
174 def description_class
180 attr_reader :enabled, :step, :count, :confine
185 @enabled = [false, false]
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")
203 attr_reader :rel, :to_id, :offset
205 def initialize(rel, offset)
206 @rel = [rel.to_f, rel.to_f]
208 @offset = [offset, offset]
212 @rel = [x.to_f, y.to_f]
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]
230 attr_reader :rel, :aspect, :step, :visible, :color_class
231 attr_accessor :aspect_preference
233 def initialize(name = "default", value = 0.0)
234 @name = name.to_str.dup.freeze
235 @value = value.freeze
242 @aspect_preference = :none
243 @rel = [Relation.new(0.0, 0), Relation.new(1.0, -1)]
244 @color = [].fill(255, 0..3)
249 unless other.is_a?(Description)
250 raise(ArgumentError, "Cannot inherit from description")
253 prot = ["@name", "@value"]
255 (instance_variables - prot).each do |v|
256 n = other.instance_variable_get(v.intern)
258 instance_variable_set(v.intern, n)
263 @visible = (v == true)
267 @color_class = v.to_str.dup
270 def set_step(x = 0, y = 0)
274 def set_aspect(x = 0.0, y = 0.0)
278 def set_align(x = 0.5, y = 0.5)
296 @color = parse_hex_color(c)
300 def parse_hex_color(c)
301 md = c.match(/^#?(([[:xdigit:]]{2}){1,4})$/)
303 raise(ArgumentError, "Argument is not a hex string")
306 pairs = md.captures.shift.split(/(..)/).delete_if do |item|
310 pairs.push("00") while pairs.length < 3
311 pairs.push("ff") if pairs.length == 3
313 pairs.map { |p| p.hex }
317 "Edje_Part_Description"
320 def to_eet_properties
321 asp_pref = case @aspect_preference
327 raise(RedactError, "invalid aspect preference value - " +
328 @aspect_preference.to_s)
331 {"state.name" => [@name],
332 "state.value" => [@value, :double],
333 "visible" => [@visible],
334 "align.x" => [@align[0], :double],
335 "align.y" => [@align[1], :double],
336 "min.w" => [@min[0]],
337 "min.h" => [@min[1]],
338 "max.w" => [@max[0]],
339 "max.h" => [@max[1]],
340 "step.x" => [@step[0]],
341 "step.y" => [@step[1]],
342 "aspect.min" => [@aspect[0], :double],
343 "aspect.max" => [@aspect[1], :double],
344 "aspect.prefer" => [asp_pref, :char],
345 "rel1.relative_x" => [@rel[0].rel[0], :double],
346 "rel1.relative_y" => [@rel[0].rel[1], :double],
347 "rel1.offset_x" => [@rel[0].offset[0]],
348 "rel1.offset_y" => [@rel[0].offset[1]],
349 "rel1.id_x" => [@rel[0].to_id[0]],
350 "rel1.id_y" => [@rel[0].to_id[1]],
351 "rel2.relative_x" => [@rel[1].rel[0], :double],
352 "rel2.relative_y" => [@rel[1].rel[1], :double],
353 "rel2.offset_x" => [@rel[1].offset[0]],
354 "rel2.offset_y" => [@rel[1].offset[1]],
355 "rel2.id_x" => [@rel[1].to_id[0]],
356 "rel2.id_y" => [@rel[1].to_id[1]],
357 "color_class" => [@color_class],
358 "color.r" => [@color[0], :char],
359 "color.g" => [@color[1], :char],
360 "color.b" => [@color[2], :char],
361 "color.a" => [@color[3], :char],
365 "image.tween_list" => [nil],
370 "border.no_fill" => [false],
371 "fill.smooth" => [true],
372 "fill.pos_rel_x" => [0.0, :double],
373 "fill.pos_abs_x" => [0],
374 "fill.rel_x" => [1.0, :double],
376 "fill.pos_rel_y" => [0.0, :double],
377 "fill.pos_abs_y" => [0],
378 "fill.rel_y" => [1.0, :double],
382 "color2.r" => [0, :char],
383 "color2.g" => [0, :char],
384 "color2.b" => [0, :char],
385 "color2.a" => [255, :char],
386 "color3.r" => [0, :char],
387 "color3.g" => [0, :char],
388 "color3.b" => [0, :char],
389 "color3.a" => [128, :char],
391 "text.text_class" => [""],
394 "text.fit_x" => [false],
395 "text.fit_y" => [false],
398 "text.align.x" => [0.0, :double],
399 "text.align.y" => [0.0, :double],
400 "text.id_source" => [-1],
401 "text.id_text_source" => [-1]}
406 def initialize(image)
418 im2 = find_image(im.to_str.strip)
419 raise(RedactError, "cannot find image - #{im}") if im2.nil?
421 image = EDJE.image_dir.find { |e| e.filename == im2 }
423 image = ImageDirectoryEntry.new(im, im2)
424 EDJE.image_dir << image
427 super(Tween.new(image))
432 [".", OPTIONS.image_dir].each do |d|
433 f2 = File.join(d, file)
434 return Pathname.new(f2).cleanpath.to_s if File.file?(f2)
442 class ImageDescription < Description
443 attr_reader :image, :auto_rel, :tweens, :border_fill_middle,
444 :fill_smooth, :fill_pos_rel, :fill_pos_abs,
447 def initialize(name = "default", value = 0.0)
452 @border = [0, 0, 0, 0]
453 @border_fill_middle = true
456 @fill_pos_rel = [0.0, 0.0]
457 @fill_pos_abs = [0, 0]
458 @fill_rel = [1.0, 1.0]
464 def border_fill_middle=(var)
465 @border_fill_middle = (var == true)
469 im2 = find_image(im.to_str.strip)
470 raise(RedactError, "cannot find image - #{im}") if im2.nil?
472 return if !@image.nil? && im2 == @image.filename
474 @image = EDJE.image_dir.find { |e| e.filename == im2 }
476 @image = ImageDirectoryEntry.new(im, im2)
477 EDJE.image_dir << @image
480 self.auto_rel = @auto_rel
486 if @auto_rel && !@image.nil?
489 @rel[1].set_rel(0.0, 0.0)
490 @rel[1].set_offset(off[0] + @image.image.width - 1,
491 off[1] + @image.image.height - 1)
495 def set_border(l = 0, r = 0, t = 0, b = 0)
496 @border = [l, r, t, b]
500 @fill_smooth = (v == true)
503 def fill_pos_rel=(x, y)
504 @fill_pos_rel = [x.to_f, y.to_f]
507 def fill_pos_abs=(x, y)
508 @fill_pos_abs = [x.to_i, y.to_i]
512 @fill_rel = [x.to_f, y.to_f]
516 @fill_abs = [x.to_i, y.to_i]
520 def to_eet_properties
522 {"image.id" => [@image.nil? ? -1 : @image.id],
523 "image.tween_list" => [@tweens],
524 "border.l" => [@border[0]],
525 "border.r" => [@border[1]],
526 "border.t" => [@border[2]],
527 "border.b" => [@border[3]],
528 "border.no_fill" => [!@border_fill_middle],
529 "fill.smooth" => [@fill_smooth],
530 "fill.pos_rel_x" => [@fill_pos_rel[0], :double],
531 "fill.pos_abs_x" => [@fill_pos_abs[0]],
532 "fill.rel_x" => [@fill_rel[0], :double],
533 "fill.abs_x" => [@fill_abs[0]],
534 "fill.pos_rel_y" => [@fill_pos_rel[1], :double],
535 "fill.pos_abs_y" => [@fill_pos_abs[1]],
536 "fill.rel_y" => [@fill_rel[1], :double],
537 "fill.abs_y" => [@fill_abs[1]]})
542 [".", OPTIONS.image_dir].each do |d|
543 f2 = File.join(d, file)
544 return Pathname.new(f2).cleanpath.to_s if File.file?(f2)
551 class TextDescription < Description
552 attr_reader :font, :text, :font_size, :text_class
554 def initialize(name = "default", value = 0.0)
557 @outline_color = [0, 0, 0, 255]
558 @shadow_color = [0, 0, 0, 128]
563 @fit = [false, false]
564 @text_min = [false, false]
565 @text_align = [0.5, 0.5]
567 @text_id_text_source = -1
575 @font_size = v.to_int
579 @text_class = v.to_str.dup
582 def set_fit(x = false, y = false)
586 def set_text_min(x = false, y = false)
590 def set_text_align(x = 0.5, y = 0.5)
596 md = f.match(/.*\.ttf$/)
599 raise(RedactError, "cannot find font - #{f}") if f2.nil?
601 found = EDJE.font_dir.find { |font| font.filename == f2 }
603 EDJE.font_dir << FontDirectoryEntry.new(f, f2)
604 @font = EDJE.font_dir.last.alias
613 def outline_color=(c)
614 @outline_color = parse_hex_color(c)
618 @shadow_color = parse_hex_color(c)
622 def to_eet_properties
624 {"color2.r" => [@outline_color[0], :char],
625 "color2.g" => [@outline_color[1], :char],
626 "color2.b" => [@outline_color[2], :char],
627 "color2.a" => [@outline_color[3], :char],
628 "color3.r" => [@shadow_color[0], :char],
629 "color3.g" => [@shadow_color[1], :char],
630 "color3.b" => [@shadow_color[2], :char],
631 "color3.a" => [@shadow_color[3], :char],
632 "text.text" => [@text],
633 "text.text_class" => [@text_class],
634 "text.font" => [@font],
635 "text.size" => [@font_size],
636 "text.fit_x" => [@fit[0]],
637 "text.fit_y" => [@fit[1]],
638 "text.min_x" => [@text_min[0]],
639 "text.min_y" => [@text_min[1]],
640 "text.align.x" => [@text_align[0], :double],
641 "text.align.y" => [@text_align[1], :double],
642 "text.id_source" => [@text_id_source],
643 "text.id_text_source" => [@text_id_text_source]})
648 [".", OPTIONS.font_dir].each do |d|
649 f2 = File.join(d, file)
650 return Pathname.new(f2).cleanpath.to_s if File.file?(f2)