2 # $Id: part.rb 25 2005-04-14 19:42:06Z 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.counfine_id" => [confine_id]} # not a typo!
119 def desc_key(name, value)
120 name + "\0" + value.to_s
124 class SwallowPart < Part
125 def initialize(collection, id, name)
132 class TextPart < Part
133 attr_accessor :effect
135 def initialize(collection, id, name)
143 def description_class
147 def to_eet_properties
148 effect = case @effect
152 when :soft_outline: 3
155 when :outline_shadow: 6
156 when :outline_soft_shadow: 7
158 raise(RedactError, "invalid effect value - #{@effect}")
161 super.merge!({"effect" => [effect, :char]})
165 class ImagePart < Part
166 def initialize(collection, id, name)
173 def description_class
179 attr_reader :enabled, :step, :count, :confine
184 @enabled = [false, false]
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")
202 attr_reader :rel, :to_id, :offset
204 def initialize(rel, offset)
207 @offset = [offset, offset]
219 @to_id = [].fill(part.nil? ? -1 : part.id, 0..1)
224 attr_reader :rel, :aspect, :step, :visible, :color_class
225 attr_accessor :aspect_preference
227 def initialize(name = "default", value = 0.0)
228 @name = name.to_str.dup.freeze
229 @value = value.freeze
236 @aspect_preference = :none
237 @rel = [Relation.new(0.0, 0), Relation.new(1.0, -1)]
238 @color = [].fill(255, 0..3)
243 @visible = (v == true)
247 @color_class = v.to_str.dup
250 def set_step(x = 0, y = 0)
254 def set_aspect(x = 0.0, y = 0.0)
258 def set_align(x = 0.5, y = 0.5)
276 @color = parse_hex_color(c)
280 def parse_hex_color(c)
281 md = c.match(/^#?(([[:xdigit:]]{2}){1,4})$/)
283 raise(ArgumentError, "Argument is not a hex string")
286 pairs = md.captures.shift.split(/(..)/).delete_if do |item|
290 pairs.push("00") while pairs.length < 3
291 pairs.push("ff") if pairs.length == 3
293 pairs.map { |p| p.hex }
297 "Edje_Part_Description"
300 def to_eet_properties
301 asp_pref = case @aspect_preference
307 raise(RedactError, "invalid aspect preference value - " +
308 @aspect_preference.to_s)
311 {"state.name" => [@name],
312 "state.value" => [@value, :double],
313 "visible" => [@visible],
314 "align.x" => [@align[0], :double],
315 "align.y" => [@align[1], :double],
316 "min.w" => [@min[0]],
317 "min.h" => [@min[1]],
318 "max.w" => [@max[0]],
319 "max.h" => [@max[1]],
320 "step.x" => [@step[0]],
321 "step.y" => [@step[1]],
322 "aspect.min" => [@aspect[0], :double],
323 "aspect.max" => [@aspect[1], :double],
324 "aspect.prefer" => [asp_pref, :char],
325 "rel1.relative_x" => [@rel[0].rel[0], :double],
326 "rel1.relative_y" => [@rel[0].rel[1], :double],
327 "rel1.offset_x" => [@rel[0].offset[0]],
328 "rel1.offset_y" => [@rel[0].offset[1]],
329 "rel1.id_x" => [@rel[0].to_id[0]],
330 "rel1.id_y" => [@rel[0].to_id[1]],
331 "rel2.relative_x" => [@rel[1].rel[0], :double],
332 "rel2.relative_y" => [@rel[1].rel[1], :double],
333 "rel2.offset_x" => [@rel[1].offset[0]],
334 "rel2.offset_y" => [@rel[1].offset[1]],
335 "rel2.id_x" => [@rel[1].to_id[0]],
336 "rel2.id_y" => [@rel[1].to_id[1]],
337 "color_class" => [@color_class],
338 "color.r" => [@color[0], :char],
339 "color.g" => [@color[1], :char],
340 "color.b" => [@color[2], :char],
341 "color.a" => [@color[3], :char],
345 "image.tween_list" => [nil],
350 "fill.smooth" => [true],
351 "fill.pos_rel_x" => [0.0, :double],
352 "fill.pos_abs_x" => [0],
353 "fill.rel_x" => [1.0, :double],
355 "fill.pos_rel_y" => [0.0, :double],
356 "fill.pos_abs_y" => [0],
357 "fill.rel_y" => [1.0, :double],
361 "color2.r" => [0, :char],
362 "color2.g" => [0, :char],
363 "color2.b" => [0, :char],
364 "color2.a" => [255, :char],
365 "color3.r" => [0, :char],
366 "color3.g" => [0, :char],
367 "color3.b" => [0, :char],
368 "color3.a" => [128, :char],
370 "text.text_class" => [""],
373 "text.fit_x" => [false],
374 "text.fit_y" => [false],
377 "text.align.x" => [0.0, :double],
378 "text.align.y" => [0.0, :double],
379 "text.id_source" => [-1],
380 "text.id_text_source" => [-1]}
385 def initialize(image)
396 image = EDJE.image_dir.find { |e| e.filename == im }
398 image = ImageDirectoryEntry.new(im)
399 EDJE.image_dir << image
402 super(Tween.new(image))
406 class ImageDescription < Description
407 attr_reader :image, :auto_rel, :tweens, :border_fill_middle
409 def initialize(name = "default", value = 0.0)
414 @border = [0, 0, 0, 0]
420 return if !@image.nil? && im == @image.filename
422 @image = EDJE.image_dir.find { |e| e.filename == im }
424 @image = ImageDirectoryEntry.new(im)
425 EDJE.image_dir << @image
428 self.auto_rel = @auto_rel
434 if @auto_rel && !@image.nil?
437 @rel[1].set_rel(0.0, 0.0)
438 @rel[1].set_offset(off[0] + @image.image.width - 1,
439 off[1] + @image.image.height - 1)
443 def set_border(l = 0, r = 0, t = 0, b = 0)
444 @border = [r, r, t, b]
447 def to_eet_properties
449 {"image.id" => [@image.nil? ? -1 : @image.id],
450 "image.tween_list" => [@tweens],
451 "border.l" => [@border[0]],
452 "border.r" => [@border[1]],
453 "border.t" => [@border[2]],
454 "border.b" => [@border[3]],
455 "fill.smooth" => [@fill_smooth],
456 "fill.pos_rel_x" => [0.0, :double],
457 "fill.pos_abs_x" => [0],
458 "fill.rel_x" => [1.0, :double],
460 "fill.pos_rel_y" => [0.0, :double],
461 "fill.pos_abs_y" => [0],
462 "fill.rel_y" => [1.0, :double],
463 "fill.abs_y" => [0]})
467 class TextDescription < Description
468 attr_reader :font, :text, :font_size, :text_class
470 def initialize(name = "default", value = 0.0)
473 @outline_color = [0, 0, 0, 255]
474 @shadow_color = [0, 0, 0, 128]
479 @fit = [false, false]
480 @text_min = [false, false]
481 @text_align = [0.5, 0.5]
483 @text_id_text_source = -1
491 @font_size = v.to_int
495 @text_class = v.to_str.dup
498 def set_fit(x = false, y = false)
502 def set_text_min(x = false, y = false)
506 def set_text_align(x = 0.5, y = 0.5)
511 md = f.to_str.match(/.*\.ttf/)
513 found = EDJE.font_dir.find { |font| font.filename == f }
515 EDJE.font_dir << FontDirectoryEntry.new(f)
522 def outline_color=(c)
523 @outline_color = parse_hex_color(c)
527 @shadow_color = parse_hex_color(c)
530 def to_eet_properties
532 {"color2.r" => [@outline_color[0], :char],
533 "color2.g" => [@outline_color[1], :char],
534 "color2.b" => [@outline_color[2], :char],
535 "color2.a" => [@outline_color[3], :char],
536 "color3.r" => [@shadow_color[0], :char],
537 "color3.g" => [@shadow_color[1], :char],
538 "color3.b" => [@shadow_color[2], :char],
539 "color3.a" => [@shadow_color[3], :char],
540 "text.text" => [@text],
541 "text.text_class" => [@text_class],
542 "text.font" => [@font],
543 "text.size" => [@font_size],
544 "text.fit_x" => [@fit[0]],
545 "text.fit_y" => [@fit[1]],
546 "text.min_x" => [@text_min[0]],
547 "text.min_y" => [@text_min[1]],
548 "text.align.x" => [@text_align[0], :double],
549 "text.align.y" => [@text_align[1], :double],
550 "text.id_source" => [@text_id_source],
551 "text.id_text_source" => [@text_id_text_source]})