Class: CyberarmEngine::Element::Container

Inherits:
CyberarmEngine::Element show all
Includes:
Common
Defined in:
lib/cyberarm_engine/ui/elements/container.rb

Direct Known Subclasses

Flow, Slider, Stack

Constant Summary

Constants included from Theme

Theme::THEME

Instance Attribute Summary collapse

Attributes inherited from CyberarmEngine::Element

#background_canvas, #border_canvas, #enabled, #event_handler, #options, #parent, #style, #tip, #x, #y, #z

Instance Method Summary collapse

Methods included from Common

#current_state, #darken, #draw_rect, #fill, #get_asset, #get_image, #get_sample, #get_song, #lighten, #opacity, #pop_state, #previous_state, #push_state, #show_cursor, #show_cursor=, #window

Methods inherited from CyberarmEngine::Element

#background=, #button_down, #button_up, #content_height, #content_width, #default_events, #dimensional_size, #draggable?, #draw, #enabled?, #height, #hide, #hit?, #inner_height, #inner_width, #is_root?, #noncontent_height, #noncontent_width, #outer_height, #outer_width, #reposition, #root, #set_background, #set_border_color, #set_border_thickness, #set_margin, #set_padding, #set_static_position, #show, #stylize, #toggle, #update_background, #value=, #visible?, #width

Methods included from CyberarmEngine::Event

#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.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 9

def initialize(options = {}, block = nil)
  @gui_state = options.delete(:gui_state)
  super

  @scroll_x = 0
  @scroll_y = 0
  @scroll_speed = 10

  @text_color = options[:color]

  @children = []
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



7
8
9
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 7

def children
  @children
end

#fill_colorObject

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_stateObject (readonly)

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

#scroll_xObject (readonly)

Returns the value of attribute scroll_x.



7
8
9
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 7

def scroll_x
  @scroll_x
end

#scroll_yObject (readonly)

Returns the value of attribute scroll_y.



7
8
9
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 7

def scroll_y
  @scroll_y
end

#stroke_colorObject

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

Instance Method Details

#add(element) ⇒ Object



28
29
30
31
32
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 28

def add(element)
  @children << element

  root.gui_state.request_recalculate
end

#buildObject



22
23
24
25
26
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 22

def build
  @block.call(self) if @block

  root.gui_state.request_recalculate
end

#clear(&block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 34

def clear(&block)
  @children.clear

  old_container = $__current_container__

  $__current_container__ = self
  block.call(self) if block

  $__current_container__ = old_container

  root.gui_state.request_recalculate
end

#fits_on_line?(element) ⇒ Boolean

Flow

Returns:

  • (Boolean)


151
152
153
154
155
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 151

def fits_on_line?(element) # Flow
  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

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 82

def hit_element?(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

#layoutObject



138
139
140
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 138

def layout
  raise "Not overridden"
end

#max_widthObject



142
143
144
145
146
147
148
149
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 142

def max_width
  _width = dimensional_size(@style.width, :width)
  if _width
    outer_width
  else
    window.width - (@parent ? @parent.style.margin_right + @style.margin_right : @style.margin_right)
  end
end

#move_to_next_line(element) ⇒ Object

Stack



185
186
187
188
189
190
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 185

def move_to_next_line(element) # Stack
  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

Flow



157
158
159
160
161
162
163
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 157

def position_on_current_line(element) # Flow
  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

Flow



175
176
177
178
179
180
181
182
183
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 175

def position_on_next_line(child) # Flow
  @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

#recalculateObject



99
100
101
102
103
104
105
106
107
108
109
110
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
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 99

def recalculate
  @current_position = Vector.new(@style.margin_left + @style.padding_left, @style.margin_top + @style.padding_top)
  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).round
    @height = _height || (@children.map { |c| c.y + c.outer_height }.max || 0).round
  end

  # Move child to parent after positioning
  @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 # TODO: Implement top,bottom,left,center, and right positioning

    Stats.increment(:gui_recalculations_last_frame, 1)
  end

  update_background
end

#renderObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 47

def render
  Gosu.clip_to(@x, @y, width, height) do
    @children.each(&:draw)
  end

  if false # DEBUG
    Gosu.flush

    Gosu.draw_line(
      x, y, Gosu::Color::RED,
      x + outer_width, y, Gosu::Color::RED,
      Float::INFINITY
    )
    Gosu.draw_line(
      x + outer_width, y, Gosu::Color::RED,
      x + outer_width, y + outer_height, Gosu::Color::RED,
      Float::INFINITY
    )
    Gosu.draw_line(
      x + outer_width, y + outer_height, Gosu::Color::RED,
      x, y + outer_height, Gosu::Color::RED,
      Float::INFINITY
    )
    Gosu.draw_line(
      x, outer_height, Gosu::Color::RED,
      x, y, Gosu::Color::RED,
      Float::INFINITY
    )
  end
end

#tallest_neighbor(querier, _y_position) ⇒ Object

Flow



165
166
167
168
169
170
171
172
173
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 165

def tallest_neighbor(querier, _y_position) # Flow
  response = querier
  @children.each do |child|
    response = child if child.outer_height > response.outer_height
    break if child == querier
  end

  response
end

#to_sObject



206
207
208
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 206

def to_s
  "#{self.class} x=#{x} y=#{y} width=#{width} height=#{height} children=#{@children.size}"
end

#updateObject



78
79
80
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 78

def update
  @children.each(&:update)
end

#valueObject

def mouse_wheel_down(sender, x, y)

@children.each {|c| c.y += @scroll_speed}
@children.each {|c| c.recalculate}

end



202
203
204
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 202

def value
  @children.map { |c| c.class }.join(", ")
end

#write_tree(indent = "", _index = 0) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/cyberarm_engine/ui/elements/container.rb', line 210

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