Class: CyberarmEngine::Element::Container
  
  
  
  Constant Summary
  
  Constants included
     from Theme
  Theme::THEME
  Instance Attribute Summary collapse
  
  
  
  
  #background_canvas, #border_canvas, #element_visible, #event_handler, #options, #parent, #style, #tip, #x, #y, #z
  
    
      Class Method Summary
      collapse
    
    
  
    
      Instance Method Summary
      collapse
    
    
  
  
  
  
  
  
  
  
  
  Methods included from Common
  #alt_down?, #control_down?, #current_state, #darken, #draw_rect, #fill, #get_asset, #get_image, #get_sample, #get_song, #lighten, #opacity, #pop_state, #previous_state, #push_state, #shift_down?, #shift_state, #show_cursor, #show_cursor=, #window
  
  
  
  
  
  
  
  
  
  #background=, #background_image=, #background_nine_slice=, #blur, #button_down, #button_up, #clicked_left_mouse_button, #content_height, #content_width, #default_events, #dimensional_size, #draggable?, #draw, #element_visible?, #enabled=, #enabled?, #enter, #focus, #height, #hide, #hit?, #inner_height, #inner_width, #inspect, #is_root?, #leave, #left_mouse_button, #max_scroll_height, #max_scroll_width, #noncontent_height, #noncontent_width, #outer_height, #outer_width, #released_left_mouse_button, #reposition, #root, #safe_style_fetch, #scroll_height, #scroll_width, #set_background, #set_background_image, #set_background_nine_slice, #set_border_color, #set_border_thickness, #set_color, #set_font, #set_margin, #set_padding, #set_static_position, #show, #stylize, #toggle, #update_background, #update_background_image, #update_background_nine_slice, #update_styles, #value=, #visible?, #width
  
  
  
  
  
  
  
  
  
  #event, #publish, #subscribe, #unsubscribe
  
  
  
  
  
  
  
  
  Methods included from Theme
  #deep_merge, #default, #theme_defaults
  Constructor Details
  
    
  
  
    #initialize(options = {}, block = nil)  ⇒ Container 
  
  
  
  
    
Returns a new instance of Container.
   
 
  
  
    | 
19
20
21
22
23
24
25
26
27
28
29
30
31 | # File 'lib/cyberarm_engine/ui/elements/container.rb', line 19
def initialize(options = {}, block = nil)
  @gui_state = options.delete(:gui_state)
  super
  @scroll_position = Vector.new(0, 0)
  @scroll_speed = 40
  @text_color = options[:color]
  @children = []
  event(:window_size_changed)
end | 
 
  
 
  
    Instance Attribute Details
    
      
      
      
  
  
    #children  ⇒ Object  
  
  
  
  
    
Returns the value of attribute children.
   
 
  
  
    | 
7
8
9 | # File 'lib/cyberarm_engine/ui/elements/container.rb', line 7
def children
  @children
end | 
 
    
      
      
      
  
  
    #fill_color  ⇒ Object 
  
  
  
  
    
Returns the value of attribute fill_color.
   
 
  
  
    | 
6
7
8 | # File 'lib/cyberarm_engine/ui/elements/container.rb', line 6
def fill_color
  @fill_color
end | 
 
    
      
      
      
  
  
    #gui_state  ⇒ Object  
  
  
  
  
    
Returns the value of attribute gui_state.
   
 
  
  
    | 
7
8
9 | # File 'lib/cyberarm_engine/ui/elements/container.rb', line 7
def gui_state
  @gui_state
end | 
 
    
      
      
      
  
  
    
Returns the value of attribute scroll_position.
   
 
  
  
    | 
7
8
9 | # File 'lib/cyberarm_engine/ui/elements/container.rb', line 7
def scroll_position
  @scroll_position
end | 
 
    
      
      
      
  
  
    #stroke_color  ⇒ Object 
  
  
  
  
    
Returns the value of attribute stroke_color.
   
 
  
  
    | 
6
7
8 | # File 'lib/cyberarm_engine/ui/elements/container.rb', line 6
def stroke_color
  @stroke_color
end | 
 
    
   
  
    Class Method Details
    
      
  
  
    .current_container  ⇒ Object 
  
  
  
  
    | 
9
10
11 | # File 'lib/cyberarm_engine/ui/elements/container.rb', line 9
def self.current_container
  @@current_container
end | 
 
    
      
  
  
    .current_container=(container)  ⇒ Object 
  
  
  
  
    | 
13
14
15
16
17 | # File 'lib/cyberarm_engine/ui/elements/container.rb', line 13
def self.current_container=(container)
  raise ArgumentError, "Expected container to an an instance of CyberarmEngine::Element::Container, got #{container.class}" unless container.is_a?(CyberarmEngine::Element::Container)
  @@current_container = container
end | 
 
    
   
  
    Instance Method Details
    
      
  
  
    #add(element)  ⇒ Object 
  
  
  
  
    | 
39
40
41
42
43 | # File 'lib/cyberarm_engine/ui/elements/container.rb', line 39
def add(element)
  @children << element
  root.gui_state.request_recalculate
end | 
 
    
      
    
      
  
  
    #build  ⇒ Object 
  
  
  
  
    | 
33
34
35
36
37 | # File 'lib/cyberarm_engine/ui/elements/container.rb', line 33
def build
  @block.call(self) if @block
  root.gui_state.request_recalculate
end | 
 
    
      
    
      
  
  
    #debug_draw  ⇒ Object 
  
  
  
  
    | 
80
81
82
83
84
85
86 | # File 'lib/cyberarm_engine/ui/elements/container.rb', line 80
def debug_draw
  super
  @children.each do |child|
    child.debug_draw
  end
end | 
 
    
      
  
  
    #fits_on_line?(element)  ⇒ Boolean 
  
  
  
  
    | 
174
175
176
177
178 | # File 'lib/cyberarm_engine/ui/elements/container.rb', line 174
def fits_on_line?(element)   p [@options[:id], @width] if @options[:id]
  @current_position.x + element.outer_width <= max_width &&
    @current_position.x + element.outer_width <= window.width
end | 
 
    
      
  
  
    #hit_element?(x, y)  ⇒ Boolean 
  
  
  
  
    | 
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 | # File 'lib/cyberarm_engine/ui/elements/container.rb', line 92
def hit_element?(x, y)
  return unless hit?(x, y)
  @children.reverse_each do |child|
    next unless child.visible?
    case child
    when Container
      if element = child.hit_element?(x, y)
        return element
      end
    else
      return child if child.hit?(x, y)
    end
  end
  self if hit?(x, y)
end | 
 
    
      
  
  
    #layout  ⇒ Object 
  
  
  
  
    | 
159
160
161 | # File 'lib/cyberarm_engine/ui/elements/container.rb', line 159
def layout
  raise "Not overridden"
end | 
 
    
      
  
  
    #max_width  ⇒ Object 
  
  
  
  
    | 
163
164
165
166
167
168
169
170
171
172 | # File 'lib/cyberarm_engine/ui/elements/container.rb', line 163
def max_width
            
  outer_width
end | 
 
    
      
  
  
    #mouse_wheel_down(sender, x, y)  ⇒ Object 
  
  
  
  
    | 
228
229
230
231
232
233
234
235
236
237
238
239
240
241 | # File 'lib/cyberarm_engine/ui/elements/container.rb', line 228
def mouse_wheel_down(sender, x, y)
  return unless @style.scroll
  return unless height < scroll_height
  if @scroll_position.y.abs < max_scroll_height
    @scroll_position.y -= @scroll_speed
    @scroll_position.y = -max_scroll_height if @scroll_position.y.abs > max_scroll_height
    root.gui_state.request_recalculate_for(self)
    return :handled
  end
end | 
 
    
      
  
  
    #mouse_wheel_up(sender, x, y)  ⇒ Object 
  
  
  
  
    | 
215
216
217
218
219
220
221
222
223
224
225
226 | # File 'lib/cyberarm_engine/ui/elements/container.rb', line 215
def mouse_wheel_up(sender, x, y)
  return unless @style.scroll
  if @scroll_position.y < 0
    @scroll_position.y += @scroll_speed
    @scroll_position.y = 0 if @scroll_position.y > 0
    root.gui_state.request_recalculate_for(self)
    return :handled
  end
end | 
 
    
      
  
  
    #move_to_next_line(element)  ⇒ Object 
  
  
  
  
  
    | 
208
209
210
211
212
213 | # File 'lib/cyberarm_engine/ui/elements/container.rb', line 208
def move_to_next_line(element)   element.x = element.style.margin_left + @current_position.x
  element.y = element.style.margin_top  + @current_position.y
  @current_position.y += element.outer_height
end | 
 
    
      
  
  
    #position_on_current_line(element)  ⇒ Object 
  
  
  
  
  
    | 
180
181
182
183
184
185
186 | # File 'lib/cyberarm_engine/ui/elements/container.rb', line 180
def position_on_current_line(element)   element.x = element.style.margin_left + @current_position.x
  element.y = element.style.margin_top  + @current_position.y
  @current_position.x += element.outer_width
  @current_position.x = @style.margin_left if @current_position.x >= max_width
end | 
 
    
      
  
  
    #position_on_next_line(child)  ⇒ Object 
  
  
  
  
  
    | 
198
199
200
201
202
203
204
205
206 | # File 'lib/cyberarm_engine/ui/elements/container.rb', line 198
def position_on_next_line(child)   @current_position.x = @style.margin_left
  @current_position.y += tallest_neighbor(child, @current_position.y).outer_height
  child.x = child.style.margin_left + @current_position.x
  child.y = child.style.margin_top  + @current_position.y
  @current_position.x += child.outer_width
end | 
 
    
      
  
  
    #recalculate  ⇒ Object 
  
  
  
  
    | 
111
112
113
114
115
116
117
118
119
120
121
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
148
149
150
151
152
153
154
155
156
157 | # File 'lib/cyberarm_engine/ui/elements/container.rb', line 111
def recalculate
  @current_position = Vector.new(@style.margin_left + @style.padding_left, @style.margin_top + @style.padding_top)
  @current_position += @scroll_position
  return unless visible?
  Stats.increment(:gui_recalculations_last_frame, 1)
  stylize
  
  layout
  if is_root?
    @width  = @style.width  = window.width
    @height = @style.height = window.height
  else
    @width = 0
    @height = 0
    _width = dimensional_size(@style.width, :width)
    _height = dimensional_size(@style.height, :height)
    @width  = _width  || (@children.map { |c| c.x + c.outer_width }.max || 0).floor
    @height = _height || (@children.map { |c| c.y + c.outer_height }.max || 0).floor
  end
    @children.each do |child|
    child.x += (@x + @style.border_thickness_left) - style.margin_left
    child.y += (@y + @style.border_thickness_top) - style.margin_top
    child.stylize
    child.recalculate
    child.reposition 
    Stats.increment(:gui_recalculations_last_frame, 1)
    child.element_visible = child.x >= @x - child.width && child.x <= @x + width &&
                            child.y >= @y - child.height && child.y <= @y + height
  end
  
  update_background
end | 
 
    
      
  
  
    #render  ⇒ Object 
  
  
  
  
    | 
69
70
71
72
73
74
75
76
77
78 | # File 'lib/cyberarm_engine/ui/elements/container.rb', line 69
def render
  Gosu.clip_to(
    @x + @style.border_thickness_left + @style.padding_left,
    @y + @style.border_thickness_top + @style.padding_top,
    content_width + 1,
    content_height + 1
  ) do
    @children.each(&:draw)
  end
end | 
 
    
      
  
  
    | 
243
244
245 | # File 'lib/cyberarm_engine/ui/elements/container.rb', line 243
def scroll_top
  @scroll_position.y
end | 
 
    
      
  
  
    | 
247
248
249
250
251
252
253
254
255
256 | # File 'lib/cyberarm_engine/ui/elements/container.rb', line 247
def scroll_top=(n)
  n = 0 if n <= 0
  @scroll_position.y = -n
  if max_scroll_height.positive?
    @scroll_position.y = -max_scroll_height if @scroll_position.y.abs > max_scroll_height
  else
    @scroll_position.y = 0
  end
end | 
 
    
      
  
  
    #tallest_neighbor(querier, _y_position)  ⇒ Object 
  
  
  
  
  
    | 
188
189
190
191
192
193
194
195
196 | # File 'lib/cyberarm_engine/ui/elements/container.rb', line 188
def tallest_neighbor(querier, _y_position)   response = querier
  @children.each do |child|
    response = child if child.outer_height > response.outer_height
    break if child == querier
  end
  response
end | 
 
    
      
  
  
    #to_s  ⇒ Object 
  
  
  
  
    | 
262
263
264 | # File 'lib/cyberarm_engine/ui/elements/container.rb', line 262
def to_s
  "#{self.class} x=#{x} y=#{y} width=#{width} height=#{height} children=#{@children.size}"
end | 
 
    
      
  
  
    #update  ⇒ Object 
  
  
  
  
    | 
88
89
90 | # File 'lib/cyberarm_engine/ui/elements/container.rb', line 88
def update
  @children.each(&:update)
end | 
 
    
      
  
  
    #value  ⇒ Object 
  
  
  
  
    | 
258
259
260 | # File 'lib/cyberarm_engine/ui/elements/container.rb', line 258
def value
  @children.map { |c| c.class }.join(", ")
end | 
 
    
      
  
  
    #write_tree(indent = "", _index = 0)  ⇒ Object 
  
  
  
  
    | 
266
267
268
269
270
271
272
273
274
275
276
277
278
279 | # File 'lib/cyberarm_engine/ui/elements/container.rb', line 266
def write_tree(indent = "", _index = 0)
  puts self
  indent += "  "
  @children.each_with_index do |child, i|
    print "#{indent}#{i}: "
    if child.is_a?(Container)
      child.write_tree(indent)
    else
      puts child
    end
  end
end |