Made 'text_class' and 'color_class' parameters writable.
[redact.git] / lib / redact / part.rb
1 #--
2 # $Id: part.rb 19 2005-04-02 22:05:24Z 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                 attr_accessor :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 clip=(part)
61                         if self == part
62                                 raise(ArgumentError, "cannot clip part to itself")
63                         elsif part.nil?
64                                 @clip = nil
65                         elsif part.collection != @collection
66                                 raise(ArgumentError, "parts' collections not identical")
67                         else
68                                 @clip = part
69                         end
70                 end
71
72                 def description(name = "default", value = 0.0) # :yields: desc
73                         d = @descriptions[desc_key(name, value)]
74
75                         block_given? ? (yield d) : d
76                 end
77
78                 protected
79                 def description_class
80                         Description
81                 end
82
83                 def to_eet_name
84                         "Edje_Part"
85                 end
86
87                 def to_eet_properties
88                         other_desc = @descriptions.dup
89                         other_desc.delete(desc_key("default", 0.0))
90
91                         confine_id = @dragable.confine.nil? ?
92                                          -1 : @dragable.confine.id
93
94                         {"name" => [@name],
95                          "id" => [@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!
110                 end
111
112                 private
113                 def desc_key(name, value)
114                         name + "\0" + value.to_s
115                 end
116         end
117
118         class SwallowPart < Part
119                 def initialize(collection, id, name)
120                         super
121
122                         @type = TYPE_SWALLOW
123                 end
124         end
125
126         class TextPart < Part
127                 EFFECT_NONE = 0
128                 EFFECT_PLAIN = 1
129                 EFFECT_OUTLINE = 2
130                 EFFECT_SOFT_OUTLINE = 3
131                 EFFECT_SHADOW = 4
132                 EFFECT_SOFT_SHADOW = 5
133                 EFFECT_OUTLINE_SHADOW = 6
134                 EFFECT_OUTLINE_SOFT_SHADOW = 7
135
136                 attr_accessor :effect
137
138                 def initialize(collection, id, name)
139                         super
140
141                         @type = TYPE_TEXT
142                         @effect = EFFECT_NONE
143                 end
144
145                 protected
146                 def description_class
147                         TextDescription
148                 end
149
150                 def to_eet_properties
151                         super.merge!({"effect" => [@effect, :char]})
152                 end
153         end
154
155         class ImagePart < Part
156                 def initialize(collection, id, name)
157                         super
158
159                         @type = TYPE_IMAGE
160                 end
161
162                 protected
163                 def description_class
164                         ImageDescription
165                 end
166         end
167
168         class Dragable
169                 attr_reader :enabled, :step, :count, :confine
170
171                 def initialize(part)
172                         @part= part
173
174                         @enabled = [false, false]
175                         @step = [0, 0]
176                         @count = [0, 0]
177                         @confine = nil
178                 end
179
180                 def confine=(part)
181                         if part == self
182                                 raise(ArgumentError, "cannot confine part to itself")
183                         elsif !part.nil? && part.collection != @part.collection
184                                 raise(ArgumentError, "parts' collections not identical")
185                         else
186                                 @confine = part
187                         end
188                 end
189         end
190
191         class Relation
192                 attr_reader :rel, :to_id, :offset
193
194                 def initialize(rel, offset)
195                         @rel = [rel, rel]
196                         @to_id = [-1, -1]
197                         @offset = [offset, offset]
198                 end
199
200                 def set_rel(x, y)
201                         @rel = [x, y]
202                 end
203
204                 def set_offset(x, y)
205                         @offset = [x, y]
206                 end
207
208                 def to=(part)
209                         @to_id = [].fill(part.nil? ? -1 : part.id, 0..1)
210                 end
211         end
212
213         class Description
214                 attr_reader :rel, :aspect, :step
215                 attr_accessor :visible, :aspect_preference, :color_class
216
217                 def initialize(name = "default", value = 0.0)
218                         @name = name.to_str.dup.freeze
219                         @value = value.freeze
220                         @visible = true
221                         @align = [0.5, 0.5]
222                         @min = [0, 0]
223                         @max = [-1, -1]
224                         @step = [0, 0]
225                         @aspect = [0.0, 0.0]
226                         @aspect_preference = :none
227                         @rel = [Relation.new(0.0, 0), Relation.new(1.0, -1)]
228                         @color = [].fill(255, 0..3)
229                         @color_class = ""
230                 end
231
232                 def set_step(x = 0, y = 0)
233                         @step = [x, y]
234                 end
235
236                 def set_aspect(x = 0.0, y = 0.0)
237                         @aspect = [x, y]
238                 end
239
240                 def set_align(x = 0.5, y = 0.5)
241                         @align = [x, y]
242                 end
243
244                 def set_size(w, h)
245                         set_min(w, h)
246                         set_max(w, h)
247                 end
248
249                 def set_min(w, h)
250                         @min = [w, h]
251                 end
252
253                 def set_max(w, h)
254                         @max = [w, h]
255                 end
256
257                 def color=(c)
258                         @color = parse_hex_color(c)
259                 end
260
261                 protected
262                 def parse_hex_color(c)
263                         md = c.match(/^#?(([[:xdigit:]]{2}){1,4})$/)
264                         if md.nil?
265                                 raise(ArgumentError, "Argument is not a hex string")
266                         end
267
268                         pairs = md.captures.shift.split(/(..)/).delete_if do |item|
269                                 item == ""
270                         end
271
272                         pairs.push("00") while pairs.length < 3
273                         pairs.push("ff") if pairs.length == 3
274
275                         pairs.map { |p| p.hex }
276                 end
277
278                 def map_aspect_preference
279                         case @aspect_preference
280                         when :none
281                                 0
282                         when :vertical
283                                 1
284                         when :horizontal
285                                 2
286                         when :both
287                                 3
288                         else
289                                 raise(RedactError, "invalid aspect preference value - " +
290                                       @aspect_preference.to_s)
291                         end
292                 end
293
294                 def to_eet_name
295                         "Edje_Part_Description"
296                 end
297
298                 def to_eet_properties
299                         {"state.name" => [@name],
300                          "state.value" => [@value, :double],
301                          "visible" => [@visible],
302                          "align.x" => [@align[0], :double],
303                          "align.y" => [@align[1], :double],
304                          "min.w" => [@min[0]],
305                          "min.h" => [@min[1]],
306                          "max.w" => [@max[0]],
307                          "max.h" => [@max[1]],
308                          "step.x" => [@step[0]],
309                          "step.y" => [@step[1]],
310                          "aspect.min" => [@aspect[0], :double],
311                          "aspect.max" => [@aspect[1], :double],
312                          "aspect.prefer" => [map_aspect_preference, :char],
313                          "rel1.relative_x" => [@rel[0].rel[0], :double],
314                          "rel1.relative_y" => [@rel[0].rel[1], :double],
315                          "rel1.offset_x" => [@rel[0].offset[0]],
316                          "rel1.offset_y" => [@rel[0].offset[1]],
317                          "rel1.id_x" => [@rel[0].to_id[0]],
318                          "rel1.id_y" => [@rel[0].to_id[1]],
319                          "rel2.relative_x" => [@rel[1].rel[0], :double],
320                          "rel2.relative_y" => [@rel[1].rel[1], :double],
321                          "rel2.offset_x" => [@rel[1].offset[0]],
322                          "rel2.offset_y" => [@rel[1].offset[1]],
323                          "rel2.id_x" => [@rel[1].to_id[0]],
324                          "rel2.id_y" => [@rel[1].to_id[1]],
325                          "color_class" => [@color_class],
326                          "color.r" => [@color[0], :char],
327                          "color.g" => [@color[1], :char],
328                          "color.b" => [@color[2], :char],
329                          "color.a" => [@color[3], :char],
330
331                          # image properties
332                          "image.id" => [-1],
333                          "image.tween_list" => [nil],
334                          "border.l" => [0],
335                          "border.r" => [0],
336                          "border.t" => [0],
337                          "border.b" => [0],
338                          "fill.smooth" => [true],
339                          "fill.pos_rel_x" => [0.0, :double],
340                          "fill.pos_abs_x" => [0],
341                          "fill.rel_x" => [1.0, :double],
342                          "fill.abs_x" => [0],
343                          "fill.pos_rel_y" => [0.0, :double],
344                          "fill.pos_abs_y" => [0],
345                          "fill.rel_y" => [1.0, :double],
346                          "fill.abs_y" => [0],
347
348                          # text properties
349                          "color2.r" => [0, :char],
350                          "color2.g" => [0, :char],
351                          "color2.b" => [0, :char],
352                          "color2.a" => [255, :char],
353                          "color3.r" => [0, :char],
354                          "color3.g" => [0, :char],
355                          "color3.b" => [0, :char],
356                          "color3.a" => [128, :char],
357                          "text.text" => [""],
358                          "text.text_class" => [""],
359                          "text.font" => [""],
360                          "text.size" => [0],
361                          "text.fit_x" => [false],
362                          "text.fit_y" => [false],
363                          "text.min_x" => [0],
364                          "text.min_y" => [0],
365                          "text.align.x" => [0.0, :double],
366                          "text.align.y" => [0.0, :double],
367                          "text.id_source" => [-1],
368                          "text.id_text_source" => [-1]}
369                 end
370         end
371
372         class Tween
373                 def initialize(image)
374                         @id = image.id
375                 end
376
377                 def to_eet_name
378                         "Edje_Part_Image_Id"
379                 end
380         end
381
382         class Tweens < Array
383                 def <<(im)
384                         image = EDJE.image_dir.find { |e| e.filename == im }
385                         if image.nil?
386                                 image = ImageDirectoryEntry.new(im)
387                                 EDJE.image_dir << image
388                         end
389
390                         super(Tween.new(image))
391                 end
392         end
393
394         class ImageDescription < Description
395                 attr_reader :image, :auto_rel, :tweens
396
397                 def initialize(name = "default", value = 0.0)
398                         super
399
400                         @image = nil
401                         @tweens = Tweens.new
402                         @border = [0, 0, 0, 0]
403                         @fill_smooth = true
404                         @auto_rel = false
405                 end
406
407                 def image=(im)
408                         return if !@image.nil? && im == @image.filename
409
410                         @image = EDJE.image_dir.find { |e| e.filename == im }
411                         if @image.nil?
412                                 @image = ImageDirectoryEntry.new(im)
413                                 EDJE.image_dir << @image
414                         end
415
416                         self.auto_rel = @auto_rel
417                 end
418
419                 def auto_rel=(b)
420                         @auto_rel = b
421
422                         if @auto_rel && !@image.nil?
423                                 off = @rel[0].offset
424
425                                 @rel[1].set_rel(0.0, 0.0)
426                                 @rel[1].set_offset(off[0] + @image.image.width - 1,
427                                                    off[1] + @image.image.height - 1)
428                         end
429                 end
430
431                 def set_border(l = 0, r = 0, t = 0, b = 0)
432                         @border = [r, r, t, b]
433                 end
434
435                 def to_eet_properties
436                         super.merge!(
437                         {"image.id" => [@image.nil? ? -1 : @image.id],
438                          "image.tween_list" => [@tweens],
439                          "border.l" => [@border[0]],
440                          "border.r" => [@border[1]],
441                          "border.t" => [@border[2]],
442                          "border.b" => [@border[3]],
443                          "fill.smooth" => [@fill_smooth],
444                          "fill.pos_rel_x" => [0.0, :double],
445                          "fill.pos_abs_x" => [0],
446                          "fill.rel_x" => [1.0, :double],
447                          "fill.abs_x" => [0],
448                          "fill.pos_rel_y" => [0.0, :double],
449                          "fill.pos_abs_y" => [0],
450                          "fill.rel_y" => [1.0, :double],
451                          "fill.abs_y" => [0]})
452                 end
453         end
454
455         class TextDescription < Description
456                 attr_reader :font
457                 attr_accessor :text, :font_size, :text_class
458
459                 def initialize(name = "default", value = 0.0)
460                         super
461
462                         @outline_color = [0, 0, 0, 255]
463                         @shadow_color = [0, 0, 0, 128]
464                         @text = ""
465                         @text_class = ""
466                         @font = ""
467                         @font_size = 0
468                         @fit = [false, false]
469                         @text_min = [false, false]
470                         @text_align = [0.5, 0.5]
471                         @text_id_source = -1
472                         @text_id_text_source = -1
473                 end
474
475                 def set_fit(x = false, y = false)
476                         @fit = [x, y]
477                 end
478
479                 def set_text_min(x = false, y = false)
480                         @text_min = [x, y]
481                 end
482
483                 def set_text_align(x = 0.5, y = 0.5)
484                         @text_align = [x, y]
485                 end
486
487                 def font=(f)
488                         md = f.to_str.match(/.*\.ttf/)
489                         unless md.nil?
490                                 found = EDJE.font_dir.find { |font| font.filename == f }
491                                 if found.nil?
492                                         EDJE.font_dir << FontDirectoryEntry.new(f)
493                                 end
494                         end
495
496                         @font = f.dup
497                 end
498
499                 def outline_color=(c)
500                         @outline_color = parse_hex_color(c)
501                 end
502
503                 def shadow_color=(c)
504                         @shadow_color = parse_hex_color(c)
505                 end
506
507                 def to_eet_properties
508                         super.merge!(
509                         {"color2.r" => [@outline_color[0], :char],
510                          "color2.g" => [@outline_color[1], :char],
511                          "color2.b" => [@outline_color[2], :char],
512                          "color2.a" => [@outline_color[3], :char],
513                          "color3.r" => [@shadow_color[0], :char],
514                          "color3.g" => [@shadow_color[1], :char],
515                          "color3.b" => [@shadow_color[2], :char],
516                          "color3.a" => [@shadow_color[3], :char],
517                          "text.text" => [@text],
518                          "text.text_class" => [@text_class],
519                          "text.font" => [@font],
520                          "text.size" => [@font_size],
521                          "text.fit_x" => [@fit[0]],
522                          "text.fit_y" => [@fit[1]],
523                          "text.min_x" => [@text_min[0]],
524                          "text.min_y" => [@text_min[1]],
525                          "text.align.x" => [@text_align[0], :double],
526                          "text.align.y" => [@text_align[1], :double],
527                          "text.id_source" => [@text_id_source],
528                          "text.id_text_source" => [@text_id_text_source]})
529                 end
530         end
531 end