lib/cyberarm_engine/ui/element.rb in cyberarm_engine-0.23.0 vs lib/cyberarm_engine/ui/element.rb in cyberarm_engine-0.24.0

- old
+ new

@@ -28,10 +28,12 @@ @x = @style.x @y = @style.y @z = @style.z + @old_width = 0 + @old_height = 0 @width = 0 @height = 0 @style.width = default(:width) || nil @style.height = default(:height) || nil @@ -194,11 +196,11 @@ event(:changed) end def enter(_sender) - @focus = false unless window.button_down?(Gosu::MsLeft) + @focus = false unless Gosu.button_down?(Gosu::MS_LEFT) if !@enabled update_styles(:disabled) elsif @focus update_styles(:active) @@ -314,11 +316,12 @@ render end def debug_draw - return if defined?(GUI_DEBUG_ONLY_ELEMENT) && self.class == GUI_DEBUG_ONLY_ELEMENT + # FIXME + return# if const_defined?(GUI_DEBUG_ONLY_ELEMENT) && self.class == GUI_DEBUG_ONLY_ELEMENT Gosu.draw_line( x, y, @debug_color, x + outer_width, y, @debug_color, Float::INFINITY @@ -339,10 +342,11 @@ Float::INFINITY ) end def update + recalculate_if_size_changed end def button_down(id) end @@ -408,14 +412,18 @@ def inner_height (@style.border_thickness_top + @style.padding_top) + (@style.padding_bottom + @style.border_thickness_bottom) end def scroll_width - @children.sum(&:outer_width) + return @cached_scroll_width if @cached_scroll_width && is_a?(Container) + + @cached_scroll_width = @children.sum(&:outer_width) end def scroll_height + return @cached_scroll_height if @cached_scroll_height && is_a?(Container) + if is_a?(CyberarmEngine::Element::Flow) return 0 if @children.size.zero? pairs_ = [] sorted_children_ = @children.sort_by(&:y) @@ -432,22 +440,22 @@ a_ << child end pairs_ << a_ unless pairs_.last == a_ - pairs_.sum { |pair| + @style.padding_top + @style.border_thickness_top + pair.map(&:outer_height).max } + @style.padding_bottom + @style.border_thickness_bottom + @cached_scroll_height = pairs_.sum { |pair| + @style.padding_top + @style.border_thickness_top + pair.map(&:outer_height).max } + @style.padding_bottom + @style.border_thickness_bottom else - @style.padding_top + @style.border_thickness_top + @children.sum(&:outer_height) + @style.padding_bottom + @style.border_thickness_bottom + @cached_scroll_height = @style.padding_top + @style.border_thickness_top + @children.sum(&:outer_height) + @style.padding_bottom + @style.border_thickness_bottom end end def max_scroll_width - scroll_width - outer_width + (scroll_width - outer_width).positive? ? scroll_width - outer_width : scroll_width end def max_scroll_height - scroll_height - outer_height + (scroll_height - outer_height).positive? ? scroll_height - outer_height : scroll_height end def dimensional_size(size, dimension) raise "dimension must be either :width or :height" unless %i[width height].include?(dimension) @@ -459,19 +467,17 @@ # Handle fill behavior if @parent && @style.fill && (dimension == :width && @parent.is_a?(Flow) || dimension == :height && @parent.is_a?(Stack)) - return space_available_width - noncontent_width if dimension == :width && @parent.is_a?(Flow) - return space_available_height - noncontent_height if dimension == :height && @parent.is_a?(Stack) - - # Handle min_width/height and max_width/height - else - return @style.send(:"min_#{dimension}") if @style.send(:"min_#{dimension}") && new_size.to_f < @style.send(:"min_#{dimension}") - return @style.send(:"max_#{dimension}") if @style.send(:"max_#{dimension}") && new_size.to_f > @style.send(:"max_#{dimension}") + new_size = space_available_width - noncontent_width if dimension == :width && @parent.is_a?(Flow) + new_size = space_available_height - noncontent_height if dimension == :height && @parent.is_a?(Stack) end + return @style.send(:"min_#{dimension}") if @style.send(:"min_#{dimension}") && new_size.to_f < @style.send(:"min_#{dimension}") + return @style.send(:"max_#{dimension}") if @style.send(:"max_#{dimension}") && new_size.to_f > @style.send(:"max_#{dimension}") + new_size end def space_available_width # TODO: This may get expensive if there are a lot of children, probably should cache it somehow @@ -553,10 +559,19 @@ @style.background_image_canvas.color = @style.background_image_color @style.background_image_canvas.image = @style.background_image end + def recalculate_if_size_changed + if !is_a?(ToolTip) && (@old_width != width || @old_height != height) + root.gui_state.request_recalculate + + @old_width = width + @old_height = height + end + end + def root return self if is_root? unless @root && @root.parent.nil? @root = parent @@ -571,9 +586,25 @@ @root end def is_root? @gui_state != nil + end + + def child_of?(element) + return element == self if is_root? + return false unless element.is_a?(Container) + return true if element.children.find { |child| child == self } + + element.children.find { |child| child.child_of?(element) if child.is_a?(Container) } + end + + def parent_of?(element) + return false if element == self + return false unless is_a?(Container) + return true if @children.find { |child| child == element } + + @children.find { |child| child.parent_of?(element) if child.is_a?(Container) } end def focus(_) warn "#{self.class}#focus was not overridden!"