lib/cyberarm_engine/ui/elements/container.rb in cyberarm_engine-0.13.1 vs lib/cyberarm_engine/ui/elements/container.rb in cyberarm_engine-0.14.0
- old
+ new
@@ -20,17 +20,17 @@
end
def build
@block.call(self) if @block
- recalculate
+ root.gui_state.request_recalculate
end
def add(element)
@children << element
- recalculate
+ root.gui_state.request_recalculate
end
def clear(&block)
@children.clear
@@ -39,11 +39,10 @@
$__current_container__ = self
block.call(self) if block
$__current_container__ = old_container
- recalculate
root.gui_state.request_recalculate
end
def render
Gosu.clip_to(@x, @y, width, height) do
@@ -55,10 +54,12 @@
@children.each(&:update)
end
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
@@ -71,10 +72,13 @@
end
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?
@@ -88,33 +92,36 @@
@width = _width ? _width : (@children.map {|c| c.x + c.outer_width }.max || 0).round
@height = _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
- child.y += @y
+ 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
def layout
raise "Not overridden"
end
def max_width
- @max_width ? @max_width : window.width - (@parent ? @parent.style.margin_right + @style.margin_right : @style.margin_right)
+ _width = dimensional_size(@style.width, :width)
+ _width ? outer_width : window.width - (@parent ? @parent.style.margin_right + @style.margin_right : @style.margin_right)
end
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
def position_on_current_line(element) # Flow
@@ -168,9 +175,28 @@
# @children.each {|c| c.recalculate}
# end
def value
@children.map {|c| c.class}.join(", ")
+ end
+
+ def to_s
+ "#{self.class} x=#{x} y=#{y} width=#{width} height=#{height} children=#{@children.size}"
+ end
+
+ def write_tree(indent = "", index = 0)
+ puts self
+
+ indent = 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
end
end
end