lib/cyberarm_engine/ui/element.rb in cyberarm_engine-0.20.0 vs lib/cyberarm_engine/ui/element.rb in cyberarm_engine-0.21.0

- old
+ new

@@ -36,10 +36,11 @@ @style.width = default(:width) || nil @style.height = default(:height) || nil @style.background_canvas = Background.new @style.background_nine_slice_canvas = BackgroundNineSlice.new + @style.background_image_canvas = BackgroundImage.new @style.border_canvas = BorderCanvas.new(element: self) @style_event = :default stylize @@ -58,10 +59,11 @@ set_padding set_margin set_background set_background_nine_slice + set_background_image set_border_thickness set_border_color end @@ -102,10 +104,18 @@ @style.background_nine_slice_top = safe_style_fetch(:background_nine_slice_top) || @style.background_nine_slice_from_edge @style.background_nine_slice_right = safe_style_fetch(:background_nine_slice_right) || @style.background_nine_slice_from_edge @style.background_nine_slice_bottom = safe_style_fetch(:background_nine_slice_bottom) || @style.background_nine_slice_from_edge end + def set_background_image + @style.background_image = safe_style_fetch(:background_image) + @style.background_image_mode = safe_style_fetch(:background_image_mode) || :stretch + @style.background_image_color = safe_style_fetch(:background_image_color) || Gosu::Color::WHITE + @style.background_image_canvas.mode = @style.background_image_mode + @style.background_image_canvas.color = @style.background_image_color + end + def set_border_thickness @style.border_thickness = safe_style_fetch(:border_thickness) @style.border_thickness_left = safe_style_fetch(:border_thickness_left) || @style.border_thickness @style.border_thickness_right = safe_style_fetch(:border_thickness_right) || @style.border_thickness @@ -286,10 +296,11 @@ return unless visible? return unless element_visible? @style.background_canvas.draw @style.background_nine_slice_canvas.draw + @style.background_image_canvas.draw @style.border_canvas.draw render end @@ -427,15 +438,38 @@ end def dimensional_size(size, dimension) raise "dimension must be either :width or :height" unless %i[width height].include?(dimension) - if size.is_a?(Numeric) && size.between?(0.0, 1.0) - (@parent.send(:"content_#{dimension}") * size).round - send(:"noncontent_#{dimension}").round + new_size = if size.is_a?(Numeric) && size.between?(0.0, 1.0) + (@parent.send(:"content_#{dimension}") * size).floor - send(:"noncontent_#{dimension}").floor else size end + + if @parent && @style.fill # Handle fill behavior + fill_siblings = @parent.children.select { |c| c.style.fill }.count.to_f # include self since we're dividing + + if dimension == :width && @parent.is_a?(Flow) + space_available_width = ((@parent.content_width - (@parent.children.reject { |c| c.style.fill }).map(&:outer_width).sum) / fill_siblings) + space_available_width = space_available_width.nan? ? 0 : space_available_width.floor # The parent element might not have its dimensions, yet. + + return space_available_width - noncontent_width + + elsif dimension == :height && @parent.is_a?(Stack) + space_available_height = ((@parent.content_height - (@parent.children.reject { |c| c.style.fill }).map(&:outer_height).sum) / fill_siblings) + space_available_height = space_available_height.nan? ? 0 : space_available_height.floor # The parent element might not have its dimensions, yet. + + return space_available_height - noncontent_height + end + + else # Handle min_width/height and max_width/height + return @style.send(:"min_#{dimension}") if @style.send(:"min_#{dimension}") && new_size < @style.send(:"min_#{dimension}") + return @style.send(:"max_#{dimension}") if @style.send(:"max_#{dimension}") && new_size > @style.send(:"max_#{dimension}") + end + + new_size end def background=(_background) @style.background_canvas.background = _background update_background @@ -448,10 +482,11 @@ @style.background_canvas.width = width @style.background_canvas.height = height @style.background_canvas.update update_background_nine_slice + update_background_image @style.border_canvas.update end def background_nine_slice=(_image_path) @style.background_nine_slice_canvas.image = _image_path @@ -473,9 +508,27 @@ @style.background_nine_slice_canvas.top = @style.background_nine_slice_top @style.background_nine_slice_canvas.right = @style.background_nine_slice_right @style.background_nine_slice_canvas.bottom = @style.background_nine_slice_bottom @style.background_nine_slice_canvas.image = @style.background_nine_slice + end + + def background_image=(image_path) + @style.background_image = image_path.is_a?(Gosu::Image) ? image_path : get_image(image_path) + update_background_image + end + + def update_background_image + @style.background_image_canvas.x = @x + @style.background_image_canvas.y = @y + @style.background_image_canvas.z = @z + @style.background_image_canvas.width = width + @style.background_image_canvas.height = height + + @style.background_image_canvas.mode = @style.background_image_mode + @style.background_image_canvas.color = @style.background_image_color + + @style.background_image_canvas.image = @style.background_image end def root return self if is_root?