From 31e909eb30fbc39dbbe029c115b178de738ab069 Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Mon, 2 May 2022 19:08:47 -0500 Subject: [PATCH] Probably fix layout issues caused by floating point errors where an element is wrapped when it has space (e.g. 2 elements with a width 0.5 not fitting properly) --- lib/cyberarm_engine/ui/element.rb | 12 +++++++----- lib/cyberarm_engine/ui/elements/button.rb | 4 ++-- lib/cyberarm_engine/ui/elements/container.rb | 4 ++-- lib/cyberarm_engine/ui/elements/edit_box.rb | 2 +- lib/cyberarm_engine/ui/elements/image.rb | 4 ++-- lib/cyberarm_engine/ui/elements/text_block.rb | 4 ++-- 6 files changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/cyberarm_engine/ui/element.rb b/lib/cyberarm_engine/ui/element.rb index 37dc2af..cddba7f 100644 --- a/lib/cyberarm_engine/ui/element.rb +++ b/lib/cyberarm_engine/ui/element.rb @@ -441,7 +441,7 @@ module CyberarmEngine raise "dimension must be either :width or :height" unless %i[width height].include?(dimension) new_size = if size.is_a?(Numeric) && size.between?(0.0, 1.0) - (@parent.send(:"content_#{dimension}") * size).round - send(:"noncontent_#{dimension}").round + (@parent.send(:"content_#{dimension}") * size).floor - send(:"noncontent_#{dimension}").floor else size end @@ -450,11 +450,15 @@ module CyberarmEngine 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).round + 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).round + 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 @@ -509,8 +513,6 @@ module CyberarmEngine end def background_image=(image_path) - pp @style.background_image_canvas.image - @style.background_image = image_path.is_a?(Gosu::Image) ? image_path : get_image(image_path) update_background_image end diff --git a/lib/cyberarm_engine/ui/elements/button.rb b/lib/cyberarm_engine/ui/elements/button.rb index f2189ff..a6334b3 100644 --- a/lib/cyberarm_engine/ui/elements/button.rb +++ b/lib/cyberarm_engine/ui/elements/button.rb @@ -64,8 +64,8 @@ module CyberarmEngine @scale_y = 1 end - @width = _width || @image.width.round * @scale_x - @height = _height || @image.height.round * @scale_y + @width = _width || @image.width.floor * @scale_x + @height = _height || @image.height.floor * @scale_y update_background else diff --git a/lib/cyberarm_engine/ui/elements/container.rb b/lib/cyberarm_engine/ui/elements/container.rb index 098697d..88127e2 100644 --- a/lib/cyberarm_engine/ui/elements/container.rb +++ b/lib/cyberarm_engine/ui/elements/container.rb @@ -122,8 +122,8 @@ module CyberarmEngine _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 + @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 # Move child to parent after positioning diff --git a/lib/cyberarm_engine/ui/elements/edit_box.rb b/lib/cyberarm_engine/ui/elements/edit_box.rb index f92fd36..390d45e 100644 --- a/lib/cyberarm_engine/ui/elements/edit_box.rb +++ b/lib/cyberarm_engine/ui/elements/edit_box.rb @@ -98,7 +98,7 @@ module CyberarmEngine end def row_at(y) - ((y - @text.y) / @text.textobject.height).round + ((y - @text.y) / @text.textobject.height).floor end def column_at(x, y) diff --git a/lib/cyberarm_engine/ui/elements/image.rb b/lib/cyberarm_engine/ui/elements/image.rb index 2c569b2..9a7daf8 100644 --- a/lib/cyberarm_engine/ui/elements/image.rb +++ b/lib/cyberarm_engine/ui/elements/image.rb @@ -45,8 +45,8 @@ module CyberarmEngine @scale_y = 1 end - @width = _width || @image.width.round * @scale_x - @height = _height || @image.height.round * @scale_y + @width = _width || @image.width.floor * @scale_x + @height = _height || @image.height.floor * @scale_y update_background end diff --git a/lib/cyberarm_engine/ui/elements/text_block.rb b/lib/cyberarm_engine/ui/elements/text_block.rb index 0e065c9..aae0b4b 100644 --- a/lib/cyberarm_engine/ui/elements/text_block.rb +++ b/lib/cyberarm_engine/ui/elements/text_block.rb @@ -43,8 +43,8 @@ module CyberarmEngine handle_text_wrapping(_width) - @width = _width || @text.width.round - @height = _height || @text.height.round + @width = _width || @text.width.floor + @height = _height || @text.height.floor @text.y = @style.border_thickness_top + @style.padding_top + @y @text.z = @z + 3