Refactored dimentional_size to split out space_available_width/height, initial implementation of verticial and horizontal container alignment

This commit is contained in:
2022-06-12 16:03:23 -05:00
parent 705138f7ad
commit ab9f9e8e7a
4 changed files with 46 additions and 17 deletions

View File

@@ -8,8 +8,11 @@ module CyberarmEngine
window.current_state
end
def previous_state
window.previous_state
def previous_state(state = nil)
raise "Only available for CyberarmEngine::GameState and subclasses" unless is_a?(CyberarmEngine::GameState) || state.is_a?(CyberarmEngine::GameState)
i = window.states.index(state || self)
window.states[i - 1] unless (i - 1).negative?
end
def pop_state

View File

@@ -447,18 +447,10 @@ module CyberarmEngine
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
@@ -470,6 +462,22 @@ module CyberarmEngine
new_size
end
def space_available_width
# TODO: This may get expensive if there are a lot of children, probably should cache it somehow
fill_siblings = @parent.children.select { |c| c.style.fill }.count.to_f # include self since we're dividing
available_space = ((@parent.content_width - (@parent.children.reject { |c| c.style.fill }).map(&:outer_width).sum) / fill_siblings)
(available_space.nan? || available_space.infinite?) ? 0 : available_space.floor # The parent element might not have its dimensions, yet.
end
def space_available_height
# TODO: This may get expensive if there are a lot of children, probably should cache it somehow
fill_siblings = @parent.children.select { |c| c.style.fill }.count.to_f # include self since we're dividing
available_space = ((@parent.content_height - (@parent.children.reject { |c| c.style.fill }).map(&:outer_height).sum) / fill_siblings)
(available_space.nan? || available_space.infinite?) ? 0 : available_space.floor # The parent element might not have its dimensions, yet.
end
def background=(_background)
@style.background_canvas.background = _background
update_background

View File

@@ -136,7 +136,31 @@ module CyberarmEngine
@height = _height || (@children.map { |c| c.y + c.outer_height }.max || 0).floor
end
# Move child to parent after positioning
# FIXME: Correctly handle alignment when element has siblings
# FIXME: Enable alignment for any element, not just containers
if @style.v_align
space = space_available_height
case @style.v_align
when :center
@y = parent.height / 2 - height / 2
when :bottom
@y = parent.height - height
end
end
if @style.h_align
space = space_available_width
case @style.h_align
when :center
@x = parent.width / 2 - width / 2
when :right
@x = parent.width - width
end
end
# Move children 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

View File

@@ -139,12 +139,6 @@ module CyberarmEngine
@states.last
end
def previous_state
if @states.size > 1 && (state = @states[@states.size - 2])
state
end
end
def pop_state
@states.pop
end