Class: Rabbit::Theme::ElementContainer

Inherits:
Array
  • Object
show all
Defined in:
lib/rabbit/theme/applier.rb

Instance Method Summary collapse

Constructor Details

#initialize(applier, ary) ⇒ ElementContainer

Returns a new instance of ElementContainer.



13
14
15
16
# File 'lib/rabbit/theme/applier.rb', line 13

def initialize(applier, ary)
  @applier = applier
  super(ary)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/rabbit/theme/applier.rb', line 202

def method_missing(meth, *args, &block)
  collect do |elem|
    if block
      proxy_block = Proc.new do |*block_args|
        block.call(elem, *block_args)
      end
    else
      proxy_block = nil
    end
    elem.__send__(meth, *args, &proxy_block)
  end
end

Instance Method Details

#[](*args) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/rabbit/theme/applier.rb', line 26

def [](*args)
  result = super
  if result.is_a?(Array)
    @applier.make_container(result)
  else
    result
  end
end

#collect(*args, &block) ⇒ Object



18
19
20
# File 'lib/rabbit/theme/applier.rb', line 18

def collect(*args, &block)
  @applier.make_container(super)
end

#draw_frame(params = {}, &block) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/rabbit/theme/applier.rb', line 149

def draw_frame(params={}, &block)
  proc_name = params[:proc_name] || "draw_frame"
  frame_color = params[:frame_color]
  fill_color = params[:fill_color]
  shadow_color = params[:shadow_color]
  shadow_offset = params[:shadow_offset] || 2
  shadow_width = params[:shadow_width] || 4
  frame_width = params[:frame_width] || 1

  add_pre_draw_proc(proc_name) do |target, canvas, x, y, w, h, simulation|
    unless simulation
      if block_given?
        fx, fy, fw, fh = yield(target, canvas, x, y, w, h)
      end
      fx ||= target.x
      fy ||= target.y + target.centering_adjusted_height
      fw ||= target.width
      fh ||= target.height
      if shadow_color
        fh -= shadow_width
      end
      size = [fx, fy, fw, fh]

      if fill_color
        args = size + [fill_color]
        canvas.draw_rectangle(true, *args)
      end
      
      if frame_color
        args = size + [frame_color, {:line_width => frame_width}]
        canvas.draw_rectangle(false, *args)
      end

      if shadow_color
        # Under Shadow
        usx = fx + shadow_offset
        usy = fy + fh + frame_width
        usw = fw + shadow_width - shadow_offset
        ush = shadow_width
        canvas.draw_rectangle(true, usx, usy, usw, ush, shadow_color)

        # Right Shadow
        rsx = fx + fw + frame_width
        rsy = fy + shadow_offset
        rsw = shadow_width
        rsh = fh + shadow_width - shadow_offset
        canvas.draw_rectangle(true, rsx, rsy, rsw, rsh, shadow_color)
      end
    end
    [x, y, w, h]
  end
end

#draw_image_mark(image_name, name = nil, options = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/rabbit/theme/applier.rb', line 92

def draw_image_mark(image_name, name=nil, options={})
  return if empty?

  loader = ImageLoader.new(image_name)

  width_proc = Proc.new {loader.width}
  height_proc = Proc.new {loader.height}
  custom_indent = options[:indent]
  indent_proc = Proc.new do |item, simulation|
    text_height = item.elements.first.original_height
    if text_height < loader.height
      loader.resize(nil, (text_height * 2.0 / 3.0).ceil)
    end
    if custom_indent.nil?
      loader.width * 2.5
    elsif custom_indent.respond_to?(:call)
      custom_indent.call(item, loader)
    else
      custom_indent
    end
  end

  draw_mark(indent_proc,
            width_proc, height_proc,
            name) do  |item, canvas, x, y, w, h|
    x -= loader.width * 0.5
    loader.draw(canvas, x, y)
  end
end

#draw_mark(indent_width, width_or_proc, height_or_proc, name = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rabbit/theme/applier.rb', line 65

def draw_mark(indent_width, width_or_proc, height_or_proc, name=nil)
  indent(indent_width, name) do |item, canvas, x, y, w, h|
    first_text = item.elements.first
    text_height = first_text.first_line_height
    text_height += first_text.padding_top + first_text.padding_bottom

    if width_or_proc.respond_to?(:call)
      mark_width = width_or_proc.call(item, canvas)
    else
      mark_width = width_or_proc
    end
    if height_or_proc.respond_to?(:call)
      mark_height = height_or_proc.call(item, canvas)
    else
      mark_height = height_or_proc
    end

    adjust_y = ((text_height / 2.0) - (mark_height / 2.0)).ceil

    indent_base_x = item.x - mark_width
    indent_base_y = item.base_y + first_text.margin_top + adjust_y
    width = mark_width
    height = mark_height
    yield(item, canvas, indent_base_x, indent_base_y, width, height)
  end
end

#draw_order(indent_width, name = nil, &block) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/rabbit/theme/applier.rb', line 122

def draw_order(indent_width, name=nil, &block)
  layouts = {}
  make_order_layout = Proc.new do |item, simulation|
    layout = layouts[item]
    if layout.nil?
      str = block.call(item)
      layout = @applier.make_layout(str)
      layouts[item] = layout
    end
    tw, th = layout.pixel_size
    [tw + indent_width, tw, th, layout]
  end

  draw_order = Proc.new do |item, canvas, x, y, w, h, tw, th, layout|
    first_text = item.elements.first
    text_height = first_text.first_line_height
    text_height += first_text.padding_top + first_text.padding_bottom
    adjust_y = ((text_height / 2.0) - (th / 2.0)).ceil

    new_x = item.base_x + indent_width
    new_y = item.base_y + first_text.margin_top + adjust_y
    canvas.draw_layout(layout, new_x, new_y)
  end

  indent(make_order_layout, name, &draw_order)
end

#indent(size_or_proc, name = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rabbit/theme/applier.rb', line 35

def indent(size_or_proc, name=nil)
  each do |element|
    element.delete_pre_draw_proc_by_name(name)
    element.delete_post_draw_proc_by_name(name)

    other_infos = []
    element.add_pre_draw_proc(name) do |canvas, x, y, w, h, simulation|
      if simulation
        if size_or_proc.respond_to?(:call)
          result = size_or_proc.call(element, simulation)
          indent_size, *other_infos = result
        else
          indent_size = size_or_proc
        end
        element.margin_left = indent_size
      end
      [x, y, w, h]
    end

    if block_given?
      element.add_post_draw_proc(name) do |canvas, x, y, w, h, simulation|
        unless simulation
          yield(element, canvas, x, y, w, h, *other_infos)
        end
        [x, y, w, h]
      end
    end
  end
end

#map(*args, &block) ⇒ Object



22
23
24
# File 'lib/rabbit/theme/applier.rb', line 22

def map(*args, &block)
  @applier.make_container(super)
end