2 # $Id: part.rb 1 2005-03-26 01:32:38Z 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")
65 elsif part.collection != @collection
66 raise(ArgumentError, "parts' collections not identical")
72 def description(name = "default", value = 0.0)
73 d = @descriptions[desc_key(name, value)]
75 block_given? ? (yield d) : d
88 other_desc = @descriptions.dup
89 other_desc.delete(desc_key("default", 0.0))
91 confine_id = @dragable.confine.nil? ?
92 -1 : @dragable.confine.id
96 "type" => [@type, :char],
97 "effect" => [0, :char],
98 "mouse_events" => [@mouse_events],
99 "repeat_events" => [@repeat_events],
100 "clip_to_id" => [@clip.nil? ? -1 : @clip.id],
101 "default_desc" => [description("default", 0.0)],
102 "other_desc" => [other_desc],
103 "dragable.x" => [@dragable.enabled[0], :char],
104 "dragable.step_x" => [@dragable.step[0]],
105 "dragable.count_x" => [@dragable.count[0]],
106 "dragable.y" => [@dragable.enabled[1], :char],
107 "dragable.step_y" => [@dragable.step[1]],
108 "dragable.count_y" => [@dragable.count[1]],
109 "dragable.counfine_id" => [confine_id]} # not a typo!
113 def desc_key(name, value)
114 name + "\0" + value.to_s
118 class SwallowPart < Part
119 def initialize(collection, id, name)
126 class TextPart < Part
130 EFFECT_SOFT_OUTLINE = 3
132 EFFECT_SOFT_SHADOW = 5
133 EFFECT_OUTLINE_SHADOW = 6
134 EFFECT_OUTLINE_SOFT_SHADOW = 7
136 attr_accessor :effect
138 def initialize(collection, id, name)
142 @effect = EFFECT_NONE
146 def description_class
150 def to_eet_properties
151 super.merge!({"effect" => [@effect, :char]})
155 class ImagePart < Part
156 def initialize(collection, id, name)
163 def description_class
169 attr_reader :enabled, :step, :count, :confine
174 @enabled = [false, false]
182 raise(ArgumentError, "cannot confine part to itself")
183 elsif !part.nil? && part.collection != @part.collection
184 raise(ArgumentError, "parts' collections not identical")
192 attr_reader :rel, :to_id, :offset
194 def initialize(rel, offset)
197 @offset = [offset, offset]
209 @to_id = [].fill(part.nil? ? -1 : part.id, 0..1)
215 attr_accessor :visible
217 def initialize(name = "default", value = 0.0)
218 @name = name.to_str.dup.freeze
219 @value = value.freeze
226 @rel = [Relation.new(0.0, 0), Relation.new(1.0, -1)]
227 @color = [].fill(255, 0..3)
231 def set_align(x = 0.5, y = 0.5)
249 @color = parse_hex_color(c)
253 def parse_hex_color(c)
254 md = c.match(/^#?(([[:xdigit:]]{2}){1,4})$/)
256 raise(ArgumentError, "Argument is not a hex string")
259 pairs = md.captures.shift.split(/(..)/).delete_if do |item|
263 pairs.push("00") while pairs.length < 3
264 pairs.push("ff") if pairs.length == 3
266 pairs.map { |p| p.hex }
270 "Edje_Part_Description"
273 def to_eet_properties
274 {"state.name" => [@name],
275 "state.value" => [@value, :double],
276 "visible" => [@visible],
277 "align.x" => [@align[0], :double],
278 "align.y" => [@align[1], :double],
279 "min.w" => [@min[0]],
280 "min.h" => [@min[1]],
281 "max.w" => [@max[0]],
282 "max.h" => [@max[1]],
283 "step.x" => [@step_x],
284 "step.y" => [@step_y],
285 "aspect.min" => [0.0, :double],
286 "aspect.max" => [0.0, :double],
287 "aspect.prefer" => [0, :char],
288 "rel1.relative_x" => [@rel[0].rel[0], :double],
289 "rel1.relative_y" => [@rel[0].rel[1], :double],
290 "rel1.offset_x" => [@rel[0].offset[0]],
291 "rel1.offset_y" => [@rel[0].offset[1]],
292 "rel1.id_x" => [@rel[0].to_id[0]],
293 "rel1.id_y" => [@rel[0].to_id[1]],
294 "rel2.relative_x" => [@rel[1].rel[0], :double],
295 "rel2.relative_y" => [@rel[1].rel[1], :double],
296 "rel2.offset_x" => [@rel[1].offset[0]],
297 "rel2.offset_y" => [@rel[1].offset[1]],
298 "rel2.id_x" => [@rel[1].to_id[0]],
299 "rel2.id_y" => [@rel[1].to_id[1]],
300 "color_class" => [@color_class],
301 "color.r" => [@color[0], :char],
302 "color.g" => [@color[1], :char],
303 "color.b" => [@color[2], :char],
304 "color.a" => [@color[3], :char],
308 "image.tween_list" => [nil],
313 "fill.smooth" => [true],
314 "fill.pos_rel_x" => [0.0, :double],
315 "fill.pos_abs_x" => [0],
316 "fill.rel_x" => [1.0, :double],
318 "fill.pos_rel_y" => [0.0, :double],
319 "fill.pos_abs_y" => [0],
320 "fill.rel_y" => [1.0, :double],
324 "color2.r" => [0, :char],
325 "color2.g" => [0, :char],
326 "color2.b" => [0, :char],
327 "color2.a" => [255, :char],
328 "color3.r" => [0, :char],
329 "color3.g" => [0, :char],
330 "color3.b" => [0, :char],
331 "color3.a" => [128, :char],
333 "text.text_class" => [""],
336 "text.fit_x" => [false],
337 "text.fit_y" => [false],
340 "text.align.x" => [0.0, :double],
341 "text.align.y" => [0.0, :double],
342 "text.id_source" => [-1],
343 "text.id_text_source" => [-1]}
348 def initialize(image)
359 image = EDJE.image_dir.find { |e| e.filename == im }
361 image = ImageDirectoryEntry.new(im)
362 EDJE.image_dir << image
365 super(Tween.new(image))
369 class ImageDescription < Description
370 attr_reader :image, :auto_rel, :tweens
372 def initialize(name = "default", value = 0.0)
377 @border = [0, 0, 0, 0]
383 return if !@image.nil? && im == @image.filename
385 @image = EDJE.image_dir.find { |e| e.filename == im }
387 @image = ImageDirectoryEntry.new(im)
388 EDJE.image_dir << @image
391 self.auto_rel = @auto_rel
397 if @auto_rel && !@image.nil?
400 @rel[1].set_rel(0.0, 0.0)
401 @rel[1].set_offset(off[0] + @image.image.width - 1,
402 off[1] + @image.image.height - 1)
406 def set_border(l = 0, r = 0, t = 0, b = 0)
407 @border = [r, r, t, b]
410 def to_eet_properties
412 {"image.id" => [@image.nil? ? -1 : @image.id],
413 "image.tween_list" => [@tweens],
414 "border.l" => [@border[0]],
415 "border.r" => [@border[1]],
416 "border.t" => [@border[2]],
417 "border.b" => [@border[3]],
418 "fill.smooth" => [@fill_smooth],
419 "fill.pos_rel_x" => [0.0, :double],
420 "fill.pos_abs_x" => [0],
421 "fill.rel_x" => [1.0, :double],
423 "fill.pos_rel_y" => [0.0, :double],
424 "fill.pos_abs_y" => [0],
425 "fill.rel_y" => [1.0, :double],
426 "fill.abs_y" => [0]})
430 class TextDescription < Description
432 attr_accessor :text, :font_size
434 def initialize(name = "default", value = 0.0)
437 @outline_color = [0, 0, 0, 255]
438 @shadow_color = [0, 0, 0, 128]
443 @fit = [false, false]
444 @text_min = [false, false]
445 @text_align = [0.5, 0.5]
447 @text_id_text_source = -1
450 def set_fit(x = false, y = false)
454 def set_text_min(x = false, y = false)
458 def set_text_align(x = 0.5, y = 0.5)
463 md = f.to_str.match(/.*\.ttf/)
465 found = EDJE.font_dir.find { |font| font.filename == f }
467 EDJE.font_dir << FontDirectoryEntry.new(f)
474 def outline_color=(c)
475 @outline_color = parse_hex_color(c)
479 @shadow_color = parse_hex_color(c)
482 def to_eet_properties
484 {"color2.r" => [@outline_color[0], :char],
485 "color2.g" => [@outline_color[1], :char],
486 "color2.b" => [@outline_color[2], :char],
487 "color2.a" => [@outline_color[3], :char],
488 "color3.r" => [@shadow_color[0], :char],
489 "color3.g" => [@shadow_color[1], :char],
490 "color3.b" => [@shadow_color[2], :char],
491 "color3.a" => [@shadow_color[3], :char],
492 "text.text" => [@text],
493 "text.text_class" => [@text_class],
494 "text.font" => [@font],
495 "text.size" => [@font_size],
496 "text.fit_x" => [@fit[0]],
497 "text.fit_y" => [@fit[1]],
498 "text.min_x" => [@text_min[0]],
499 "text.min_y" => [@text_min[1]],
500 "text.align.x" => [@text_align[0], :double],
501 "text.align.y" => [@text_align[1], :double],
502 "text.id_source" => [@text_id_source],
503 "text.id_text_source" => [@text_id_text_source]})