2 # $Id: part.rb 22 2005-04-02 22:29:50Z 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 attr_accessor :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)
62 raise(ArgumentError, "cannot clip part to itself")
63 elsif !part.nil? && part.collection != @collection
64 raise(ArgumentError, "items not in the same collection")
70 def description(name = "default", value = 0.0) # :yields: desc
71 d = @descriptions[desc_key(name, value)]
73 block_given? ? (yield d) : d
86 other_desc = @descriptions.dup
87 other_desc.delete(desc_key("default", 0.0))
89 confine_id = @dragable.confine.nil? ?
90 -1 : @dragable.confine.id
94 "type" => [@type, :char],
95 "effect" => [0, :char],
96 "mouse_events" => [@mouse_events],
97 "repeat_events" => [@repeat_events],
98 "clip_to_id" => [@clip.nil? ? -1 : @clip.id],
99 "default_desc" => [description("default", 0.0)],
100 "other_desc" => [other_desc],
101 "dragable.x" => [@dragable.enabled[0], :char],
102 "dragable.step_x" => [@dragable.step[0]],
103 "dragable.count_x" => [@dragable.count[0]],
104 "dragable.y" => [@dragable.enabled[1], :char],
105 "dragable.step_y" => [@dragable.step[1]],
106 "dragable.count_y" => [@dragable.count[1]],
107 "dragable.counfine_id" => [confine_id]} # not a typo!
111 def desc_key(name, value)
112 name + "\0" + value.to_s
116 class SwallowPart < Part
117 def initialize(collection, id, name)
124 class TextPart < Part
125 attr_accessor :effect
127 def initialize(collection, id, name)
135 def description_class
139 def to_eet_properties
140 effect = case @effect
144 when :soft_outline: 3
147 when :outline_shadow: 6
148 when :outline_soft_shadow: 7
150 raise(RedactError, "invalid effect value - #{@effect}")
153 super.merge!({"effect" => [effect, :char]})
157 class ImagePart < Part
158 def initialize(collection, id, name)
165 def description_class
171 attr_reader :enabled, :step, :count, :confine
176 @enabled = [false, false]
184 raise(ArgumentError, "cannot confine part to itself")
185 elsif !part.nil? && part.collection != @part.collection
186 raise(ArgumentError, "items not in the same collection")
194 attr_reader :rel, :to_id, :offset
196 def initialize(rel, offset)
199 @offset = [offset, offset]
211 @to_id = [].fill(part.nil? ? -1 : part.id, 0..1)
216 attr_reader :rel, :aspect, :step
217 attr_accessor :visible, :aspect_preference, :color_class
219 def initialize(name = "default", value = 0.0)
220 @name = name.to_str.dup.freeze
221 @value = value.freeze
228 @aspect_preference = :none
229 @rel = [Relation.new(0.0, 0), Relation.new(1.0, -1)]
230 @color = [].fill(255, 0..3)
234 def set_step(x = 0, y = 0)
238 def set_aspect(x = 0.0, y = 0.0)
242 def set_align(x = 0.5, y = 0.5)
260 @color = parse_hex_color(c)
264 def parse_hex_color(c)
265 md = c.match(/^#?(([[:xdigit:]]{2}){1,4})$/)
267 raise(ArgumentError, "Argument is not a hex string")
270 pairs = md.captures.shift.split(/(..)/).delete_if do |item|
274 pairs.push("00") while pairs.length < 3
275 pairs.push("ff") if pairs.length == 3
277 pairs.map { |p| p.hex }
281 "Edje_Part_Description"
284 def to_eet_properties
285 asp_pref = case @aspect_preference
291 raise(RedactError, "invalid aspect preference value - " +
292 @aspect_preference.to_s)
295 {"state.name" => [@name],
296 "state.value" => [@value, :double],
297 "visible" => [@visible],
298 "align.x" => [@align[0], :double],
299 "align.y" => [@align[1], :double],
300 "min.w" => [@min[0]],
301 "min.h" => [@min[1]],
302 "max.w" => [@max[0]],
303 "max.h" => [@max[1]],
304 "step.x" => [@step[0]],
305 "step.y" => [@step[1]],
306 "aspect.min" => [@aspect[0], :double],
307 "aspect.max" => [@aspect[1], :double],
308 "aspect.prefer" => [asp_pref, :char],
309 "rel1.relative_x" => [@rel[0].rel[0], :double],
310 "rel1.relative_y" => [@rel[0].rel[1], :double],
311 "rel1.offset_x" => [@rel[0].offset[0]],
312 "rel1.offset_y" => [@rel[0].offset[1]],
313 "rel1.id_x" => [@rel[0].to_id[0]],
314 "rel1.id_y" => [@rel[0].to_id[1]],
315 "rel2.relative_x" => [@rel[1].rel[0], :double],
316 "rel2.relative_y" => [@rel[1].rel[1], :double],
317 "rel2.offset_x" => [@rel[1].offset[0]],
318 "rel2.offset_y" => [@rel[1].offset[1]],
319 "rel2.id_x" => [@rel[1].to_id[0]],
320 "rel2.id_y" => [@rel[1].to_id[1]],
321 "color_class" => [@color_class],
322 "color.r" => [@color[0], :char],
323 "color.g" => [@color[1], :char],
324 "color.b" => [@color[2], :char],
325 "color.a" => [@color[3], :char],
329 "image.tween_list" => [nil],
334 "fill.smooth" => [true],
335 "fill.pos_rel_x" => [0.0, :double],
336 "fill.pos_abs_x" => [0],
337 "fill.rel_x" => [1.0, :double],
339 "fill.pos_rel_y" => [0.0, :double],
340 "fill.pos_abs_y" => [0],
341 "fill.rel_y" => [1.0, :double],
345 "color2.r" => [0, :char],
346 "color2.g" => [0, :char],
347 "color2.b" => [0, :char],
348 "color2.a" => [255, :char],
349 "color3.r" => [0, :char],
350 "color3.g" => [0, :char],
351 "color3.b" => [0, :char],
352 "color3.a" => [128, :char],
354 "text.text_class" => [""],
357 "text.fit_x" => [false],
358 "text.fit_y" => [false],
361 "text.align.x" => [0.0, :double],
362 "text.align.y" => [0.0, :double],
363 "text.id_source" => [-1],
364 "text.id_text_source" => [-1]}
369 def initialize(image)
380 image = EDJE.image_dir.find { |e| e.filename == im }
382 image = ImageDirectoryEntry.new(im)
383 EDJE.image_dir << image
386 super(Tween.new(image))
390 class ImageDescription < Description
391 attr_reader :image, :auto_rel, :tweens
393 def initialize(name = "default", value = 0.0)
398 @border = [0, 0, 0, 0]
404 return if !@image.nil? && im == @image.filename
406 @image = EDJE.image_dir.find { |e| e.filename == im }
408 @image = ImageDirectoryEntry.new(im)
409 EDJE.image_dir << @image
412 self.auto_rel = @auto_rel
418 if @auto_rel && !@image.nil?
421 @rel[1].set_rel(0.0, 0.0)
422 @rel[1].set_offset(off[0] + @image.image.width - 1,
423 off[1] + @image.image.height - 1)
427 def set_border(l = 0, r = 0, t = 0, b = 0)
428 @border = [r, r, t, b]
431 def to_eet_properties
433 {"image.id" => [@image.nil? ? -1 : @image.id],
434 "image.tween_list" => [@tweens],
435 "border.l" => [@border[0]],
436 "border.r" => [@border[1]],
437 "border.t" => [@border[2]],
438 "border.b" => [@border[3]],
439 "fill.smooth" => [@fill_smooth],
440 "fill.pos_rel_x" => [0.0, :double],
441 "fill.pos_abs_x" => [0],
442 "fill.rel_x" => [1.0, :double],
444 "fill.pos_rel_y" => [0.0, :double],
445 "fill.pos_abs_y" => [0],
446 "fill.rel_y" => [1.0, :double],
447 "fill.abs_y" => [0]})
451 class TextDescription < Description
453 attr_accessor :text, :font_size, :text_class
455 def initialize(name = "default", value = 0.0)
458 @outline_color = [0, 0, 0, 255]
459 @shadow_color = [0, 0, 0, 128]
464 @fit = [false, false]
465 @text_min = [false, false]
466 @text_align = [0.5, 0.5]
468 @text_id_text_source = -1
471 def set_fit(x = false, y = false)
475 def set_text_min(x = false, y = false)
479 def set_text_align(x = 0.5, y = 0.5)
484 md = f.to_str.match(/.*\.ttf/)
486 found = EDJE.font_dir.find { |font| font.filename == f }
488 EDJE.font_dir << FontDirectoryEntry.new(f)
495 def outline_color=(c)
496 @outline_color = parse_hex_color(c)
500 @shadow_color = parse_hex_color(c)
503 def to_eet_properties
505 {"color2.r" => [@outline_color[0], :char],
506 "color2.g" => [@outline_color[1], :char],
507 "color2.b" => [@outline_color[2], :char],
508 "color2.a" => [@outline_color[3], :char],
509 "color3.r" => [@shadow_color[0], :char],
510 "color3.g" => [@shadow_color[1], :char],
511 "color3.b" => [@shadow_color[2], :char],
512 "color3.a" => [@shadow_color[3], :char],
513 "text.text" => [@text],
514 "text.text_class" => [@text_class],
515 "text.font" => [@font],
516 "text.size" => [@font_size],
517 "text.fit_x" => [@fit[0]],
518 "text.fit_y" => [@fit[1]],
519 "text.min_x" => [@text_min[0]],
520 "text.min_y" => [@text_min[1]],
521 "text.align.x" => [@text_align[0], :double],
522 "text.align.y" => [@text_align[1], :double],
523 "text.id_source" => [@text_id_source],
524 "text.id_text_source" => [@text_id_text_source]})